rmat_tests.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. //
  5. // Copyright (C) 2020 Intel Corporation
  6. #include "../test_precomp.hpp"
  7. #include <opencv2/gapi/rmat.hpp>
  8. #include "rmat_test_common.hpp"
  9. namespace opencv_test {
  10. namespace {
  11. void randomizeMat(cv::Mat& m) {
  12. auto ref = m.clone();
  13. while (cv::norm(m, ref, cv::NORM_INF) == 0) {
  14. cv::randu(m, cv::Scalar::all(127), cv::Scalar::all(40));
  15. }
  16. }
  17. template <typename RMatAdapterT>
  18. struct RMatTest {
  19. using AdapterT = RMatAdapterT;
  20. RMatTest()
  21. : m_deviceMat(cv::Mat::zeros(8,8,CV_8UC1))
  22. , m_rmat(make_rmat<RMatAdapterT>(m_deviceMat, m_callbackCalled)) {
  23. randomizeMat(m_deviceMat);
  24. expectNoCallbackCalled();
  25. }
  26. RMat& rmat() { return m_rmat; }
  27. cv::Mat cloneDeviceMat() { return m_deviceMat.clone(); }
  28. void expectCallbackCalled() { EXPECT_TRUE(m_callbackCalled); }
  29. void expectNoCallbackCalled() { EXPECT_FALSE(m_callbackCalled); }
  30. void expectDeviceDataEqual(const cv::Mat& mat) {
  31. EXPECT_EQ(0, cv::norm(mat, m_deviceMat, NORM_INF));
  32. }
  33. void expectDeviceDataNotEqual(const cv::Mat& mat) {
  34. EXPECT_NE(0, cv::norm(mat, m_deviceMat, NORM_INF));
  35. }
  36. private:
  37. cv::Mat m_deviceMat;
  38. bool m_callbackCalled = false;
  39. cv::RMat m_rmat;
  40. };
  41. } // anonymous namespace
  42. template<typename T>
  43. struct RMatTypedTest : public ::testing::Test, public T { using Type = T; };
  44. using RMatTestTypes = ::testing::Types< RMatTest<RMatAdapterRef>
  45. , RMatTest<RMatAdapterCopy>
  46. >;
  47. TYPED_TEST_CASE(RMatTypedTest, RMatTestTypes);
  48. TYPED_TEST(RMatTypedTest, Smoke) {
  49. auto view = this->rmat().access(RMat::Access::R);
  50. auto matFromDevice = cv::Mat(view.size(), view.type(), view.ptr());
  51. EXPECT_TRUE(cv::descr_of(this->cloneDeviceMat()) == this->rmat().desc());
  52. this->expectDeviceDataEqual(matFromDevice);
  53. }
  54. static Mat asMat(RMat::View& view) {
  55. return Mat(view.size(), view.type(), view.ptr(), view.step());
  56. }
  57. TYPED_TEST(RMatTypedTest, BasicWorkflow) {
  58. {
  59. auto view = this->rmat().access(RMat::Access::R);
  60. this->expectDeviceDataEqual(asMat(view));
  61. }
  62. this->expectNoCallbackCalled();
  63. cv::Mat dataToWrite = this->cloneDeviceMat();
  64. randomizeMat(dataToWrite);
  65. this->expectDeviceDataNotEqual(dataToWrite);
  66. {
  67. auto view = this->rmat().access(RMat::Access::W);
  68. dataToWrite.copyTo(asMat(view));
  69. }
  70. this->expectCallbackCalled();
  71. this->expectDeviceDataEqual(dataToWrite);
  72. }
  73. TEST(RMat, TestEmptyAdapter) {
  74. RMat rmat;
  75. EXPECT_ANY_THROW(rmat.get<RMatAdapterCopy>());
  76. }
  77. TYPED_TEST(RMatTypedTest, CorrectAdapterCast) {
  78. using T = typename TestFixture::Type::AdapterT;
  79. EXPECT_NE(nullptr, this->rmat().template get<T>());
  80. }
  81. class DummyAdapter : public RMat::IAdapter {
  82. virtual RMat::View access(RMat::Access) override { return {}; }
  83. virtual cv::GMatDesc desc() const override { return {}; }
  84. };
  85. TYPED_TEST(RMatTypedTest, IncorrectAdapterCast) {
  86. EXPECT_EQ(nullptr, this->rmat().template get<DummyAdapter>());
  87. }
  88. class RMatAdapterForBackend : public RMat::IAdapter {
  89. int m_i;
  90. public:
  91. RMatAdapterForBackend(int i) : m_i(i) {}
  92. virtual RMat::View access(RMat::Access) override { return {}; }
  93. virtual GMatDesc desc() const override { return {}; }
  94. int deviceSpecificData() const { return m_i; }
  95. };
  96. // RMat's usage scenario in the backend:
  97. // we have some specific data hidden under RMat,
  98. // test that we can obtain it via RMat.as<T>() method
  99. TEST(RMat, UsageInBackend) {
  100. int i = 123456;
  101. auto rmat = cv::make_rmat<RMatAdapterForBackend>(i);
  102. auto adapter = rmat.get<RMatAdapterForBackend>();
  103. ASSERT_NE(nullptr, adapter);
  104. EXPECT_EQ(i, adapter->deviceSpecificData());
  105. }
  106. } // namespace opencv_test