rmat_integration_tests.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. #include <opencv2/gapi/fluid/imgproc.hpp>
  10. #include <opencv2/gapi/cpu/imgproc.hpp>
  11. namespace opencv_test
  12. {
  13. // This test set takes RMat type as a template parameter and launces simple
  14. // blur(isl1) -> blur(isl2) computation passing RMat as input, as output
  15. // and both input and output
  16. template<typename RMatAdapterT>
  17. struct RMatIntTestBase {
  18. cv::Mat in_mat;
  19. cv::Mat out_mat;
  20. cv::Mat out_mat_ref;
  21. cv::GComputation comp;
  22. bool inCallbackCalled;
  23. bool outCallbackCalled;
  24. static constexpr int w = 8;
  25. static constexpr int h = 8;
  26. RMatIntTestBase()
  27. : in_mat(h, w, CV_8UC1)
  28. , out_mat(h, w, CV_8UC1)
  29. , out_mat_ref(h, w, CV_8UC1)
  30. , comp([](){
  31. cv::GMat in;
  32. auto tmp = cv::gapi::blur(in, {3,3});
  33. auto out = cv::gapi::blur(tmp, {3,3});
  34. cv::gapi::island("test", cv::GIn(in), cv::GOut(tmp));
  35. return cv::GComputation(in, out);
  36. })
  37. , inCallbackCalled(false)
  38. , outCallbackCalled(false) {
  39. cv::randu(in_mat, cv::Scalar::all(127), cv::Scalar::all(40));
  40. }
  41. void check() {
  42. comp.apply(in_mat, out_mat_ref);
  43. EXPECT_EQ(0, cvtest::norm(out_mat_ref, out_mat, NORM_INF));
  44. }
  45. RMat createRMat(cv::Mat& mat, bool& callbackCalled) {
  46. return {cv::make_rmat<RMatAdapterT>(mat, callbackCalled)};
  47. }
  48. };
  49. template<typename RMatAdapterT>
  50. struct RMatIntTest : public RMatIntTestBase<RMatAdapterT>
  51. {
  52. template<typename In, typename Out>
  53. void run(const In& in, Out& out, cv::GCompileArgs&& compile_args) {
  54. for (int i = 0; i < 2; i++) {
  55. EXPECT_FALSE(this->inCallbackCalled);
  56. EXPECT_FALSE(this->outCallbackCalled);
  57. auto compile_args_copy = compile_args;
  58. this->comp.apply(cv::gin(in), cv::gout(out), std::move(compile_args_copy));
  59. EXPECT_FALSE(this->inCallbackCalled);
  60. if (std::is_same<RMat,Out>::value) {
  61. EXPECT_TRUE(this->outCallbackCalled);
  62. } else {
  63. EXPECT_FALSE(this->outCallbackCalled);
  64. }
  65. this->outCallbackCalled = false;
  66. }
  67. this->check();
  68. }
  69. };
  70. template<typename RMatAdapterT>
  71. struct RMatIntTestStreaming : public RMatIntTestBase<RMatAdapterT>
  72. {
  73. template <typename M>
  74. cv::GMatDesc getDesc(const M& m) { return cv::descr_of(m); }
  75. void checkOutput(const cv::Mat&) { this->check(); }
  76. void checkOutput(const RMat& rm) {
  77. auto view = rm.access(RMat::Access::R);
  78. this->out_mat = cv::Mat(view.size(), view.type(), view.ptr());
  79. this->check();
  80. }
  81. template<typename In, typename Out>
  82. void run(const In& in, Out& out, cv::GCompileArgs&& compile_args) {
  83. auto sc = this->comp.compileStreaming(getDesc(in), std::move(compile_args));
  84. sc.setSource(cv::gin(in));
  85. sc.start();
  86. std::size_t frame = 0u;
  87. constexpr std::size_t num_frames = 10u;
  88. EXPECT_FALSE(this->inCallbackCalled);
  89. EXPECT_FALSE(this->outCallbackCalled);
  90. while (sc.pull(cv::gout(out)) && frame < num_frames) {
  91. frame++;
  92. this->checkOutput(out);
  93. EXPECT_FALSE(this->inCallbackCalled);
  94. EXPECT_FALSE(this->outCallbackCalled);
  95. }
  96. EXPECT_EQ(num_frames, frame);
  97. }
  98. };
  99. struct OcvKernels {
  100. cv::GKernelPackage kernels() { return cv::gapi::imgproc::cpu::kernels(); }
  101. };
  102. struct FluidKernels {
  103. cv::GKernelPackage kernels() { return cv::gapi::imgproc::fluid::kernels(); }
  104. };
  105. struct RMatIntTestCpuRef : public
  106. RMatIntTest<RMatAdapterRef>, OcvKernels {};
  107. struct RMatIntTestCpuCopy : public
  108. RMatIntTest<RMatAdapterCopy>, OcvKernels {};
  109. struct RMatIntTestCpuRefStreaming : public
  110. RMatIntTestStreaming<RMatAdapterRef>, OcvKernels {};
  111. struct RMatIntTestCpuCopyStreaming : public
  112. RMatIntTestStreaming<RMatAdapterCopy>, OcvKernels {};
  113. struct RMatIntTestCpuRefFluid : public
  114. RMatIntTest<RMatAdapterRef>, FluidKernels {};
  115. struct RMatIntTestCpuCopyFluid : public
  116. RMatIntTest<RMatAdapterCopy>, FluidKernels {};
  117. struct RMatIntTestCpuRefStreamingFluid : public
  118. RMatIntTestStreaming<RMatAdapterRef>, FluidKernels {};
  119. struct RMatIntTestCpuCopyStreamingFluid : public
  120. RMatIntTestStreaming<RMatAdapterCopy>, FluidKernels {};
  121. template<typename T>
  122. struct RMatIntTypedTest : public ::testing::Test, public T {};
  123. using RMatIntTestTypes = ::testing::Types< RMatIntTestCpuRef
  124. , RMatIntTestCpuCopy
  125. , RMatIntTestCpuRefStreaming
  126. , RMatIntTestCpuCopyStreaming
  127. , RMatIntTestCpuRefFluid
  128. , RMatIntTestCpuCopyFluid
  129. , RMatIntTestCpuRefStreamingFluid
  130. , RMatIntTestCpuCopyStreamingFluid
  131. >;
  132. TYPED_TEST_CASE(RMatIntTypedTest, RMatIntTestTypes);
  133. TYPED_TEST(RMatIntTypedTest, In) {
  134. auto in_rmat = this->createRMat(this->in_mat, this->inCallbackCalled);
  135. this->run(in_rmat, this->out_mat, cv::compile_args(this->kernels()));
  136. }
  137. TYPED_TEST(RMatIntTypedTest, Out) {
  138. auto out_rmat = this->createRMat(this->out_mat, this->outCallbackCalled);
  139. this->run(this->in_mat, out_rmat, cv::compile_args(this->kernels()));
  140. }
  141. TYPED_TEST(RMatIntTypedTest, InOut) {
  142. auto in_rmat = this->createRMat(this->in_mat, this->inCallbackCalled);
  143. auto out_rmat = this->createRMat(this->out_mat, this->outCallbackCalled);
  144. this->run(in_rmat, out_rmat, cv::compile_args(this->kernels()));
  145. }
  146. } // namespace opencv_test