rmat_test_common.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. #ifndef OPENCV_GAPI_RMAT_TESTS_COMMON_HPP
  7. #define OPENCV_GAPI_RMAT_TESTS_COMMON_HPP
  8. #include "../test_precomp.hpp"
  9. #include <opencv2/gapi/rmat.hpp>
  10. namespace opencv_test {
  11. class RMatAdapterRef : public RMat::IAdapter {
  12. cv::Mat& m_mat;
  13. bool& m_callbackCalled;
  14. public:
  15. RMatAdapterRef(cv::Mat& m, bool& callbackCalled)
  16. : m_mat(m), m_callbackCalled(callbackCalled)
  17. {}
  18. virtual RMat::View access(RMat::Access access) override {
  19. RMat::View::stepsT steps(m_mat.dims);
  20. for (int i = 0; i < m_mat.dims; i++) {
  21. steps[i] = m_mat.step[i];
  22. }
  23. if (access == RMat::Access::W) {
  24. return RMat::View(cv::descr_of(m_mat), m_mat.data, steps,
  25. [this](){
  26. EXPECT_FALSE(m_callbackCalled);
  27. m_callbackCalled = true;
  28. });
  29. } else {
  30. return RMat::View(cv::descr_of(m_mat), m_mat.data, steps);
  31. }
  32. }
  33. virtual cv::GMatDesc desc() const override { return cv::descr_of(m_mat); }
  34. };
  35. class RMatAdapterCopy : public RMat::IAdapter {
  36. cv::Mat& m_deviceMat;
  37. cv::Mat m_hostMat;
  38. bool& m_callbackCalled;
  39. public:
  40. RMatAdapterCopy(cv::Mat& m, bool& callbackCalled)
  41. : m_deviceMat(m), m_hostMat(m.clone()), m_callbackCalled(callbackCalled)
  42. {}
  43. virtual RMat::View access(RMat::Access access) override {
  44. RMat::View::stepsT steps(m_hostMat.dims);
  45. for (int i = 0; i < m_hostMat.dims; i++) {
  46. steps[i] = m_hostMat.step[i];
  47. }
  48. if (access == RMat::Access::W) {
  49. return RMat::View(cv::descr_of(m_hostMat), m_hostMat.data, steps,
  50. [this](){
  51. EXPECT_FALSE(m_callbackCalled);
  52. m_callbackCalled = true;
  53. m_hostMat.copyTo(m_deviceMat);
  54. });
  55. } else {
  56. m_deviceMat.copyTo(m_hostMat);
  57. return RMat::View(cv::descr_of(m_hostMat), m_hostMat.data, steps);
  58. }
  59. }
  60. virtual cv::GMatDesc desc() const override { return cv::descr_of(m_hostMat); }
  61. };
  62. } // namespace opencv_test
  63. #endif // OPENCV_GAPI_RMAT_TESTS_COMMON_HPP