rmat.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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_HPP
  7. #define OPENCV_GAPI_RMAT_HPP
  8. #include <opencv2/gapi/gmat.hpp>
  9. #include <opencv2/gapi/own/exports.hpp>
  10. // Forward declaration
  11. namespace cv {
  12. namespace gapi {
  13. namespace s11n {
  14. struct IOStream;
  15. struct IIStream;
  16. } // namespace s11n
  17. } // namespace gapi
  18. } // namespace cv
  19. namespace cv {
  20. // "Remote Mat", a general class which provides an abstraction layer over the data
  21. // storage and placement (host, remote device etc) and allows to access this data.
  22. //
  23. // The device specific implementation is hidden in the RMat::IAdapter class
  24. //
  25. // The basic flow is the following:
  26. // * Backend which is aware of the remote device:
  27. // - Implements own AdapterT class which is derived from RMat::IAdapter
  28. // - Wraps device memory into RMat via make_rmat utility function:
  29. // cv::RMat rmat = cv::make_rmat<AdapterT>(args);
  30. //
  31. // * End user:
  32. // - Writes the code which works with RMats without any knowledge of the remote device:
  33. // void func(const cv::RMat& in_rmat, cv::RMat& out_rmat) {
  34. // // Fetch input data from the device, get mapped memory for output
  35. // cv::RMat::View in_view = in_rmat.access(Access::R);
  36. // cv::RMat::View out_view = out_rmat.access(Access::W);
  37. // performCalculations(in_view, out_view);
  38. // // data from out_view is transferred to the device when out_view is destroyed
  39. // }
  40. /** \addtogroup gapi_data_structures
  41. * @{
  42. */
  43. class GAPI_EXPORTS RMat
  44. {
  45. public:
  46. // A lightweight wrapper on image data:
  47. // - Doesn't own the memory;
  48. // - Doesn't implement copy semantics (it's assumed that a view is created each time
  49. // wrapped data is being accessed);
  50. // - Has an optional callback which is called when the view is destroyed.
  51. class GAPI_EXPORTS View
  52. {
  53. public:
  54. using DestroyCallback = std::function<void()>;
  55. using stepsT = std::vector<size_t>;
  56. View() = default;
  57. View(const GMatDesc& desc, uchar* data, const stepsT& steps = {}, DestroyCallback&& cb = nullptr);
  58. View(const GMatDesc& desc, uchar* data, size_t step, DestroyCallback&& cb = nullptr);
  59. View(const View&) = delete;
  60. View& operator=(const View&) = delete;
  61. View(View&&) = default;
  62. View& operator=(View&& v);
  63. ~View() { if (m_cb) m_cb(); }
  64. cv::Size size() const { return m_desc.size; }
  65. const std::vector<int>& dims() const { return m_desc.dims; }
  66. int cols() const { return m_desc.size.width; }
  67. int rows() const { return m_desc.size.height; }
  68. int type() const;
  69. int depth() const { return m_desc.depth; }
  70. int chan() const { return m_desc.chan; }
  71. size_t elemSize() const { return CV_ELEM_SIZE(type()); }
  72. template<typename T = uchar> T* ptr(int y = 0) {
  73. return reinterpret_cast<T*>(m_data + step()*y);
  74. }
  75. template<typename T = uchar> const T* ptr(int y = 0) const {
  76. return reinterpret_cast<T*>(m_data + step()*y);
  77. }
  78. template<typename T = uchar> T* ptr(int y, int x) {
  79. return reinterpret_cast<T*>(m_data + step()*y + step(1)*x);
  80. }
  81. template<typename T = uchar> const T* ptr(int y, int x) const {
  82. return reinterpret_cast<const T*>(m_data + step()*y + step(1)*x);
  83. }
  84. size_t step(size_t i = 0) const { GAPI_DbgAssert(i<m_steps.size()); return m_steps[i]; }
  85. const stepsT& steps() const { return m_steps; }
  86. private:
  87. GMatDesc m_desc;
  88. uchar* m_data = nullptr;
  89. stepsT m_steps = {0u};
  90. DestroyCallback m_cb = nullptr;
  91. };
  92. enum class Access { R, W };
  93. class IAdapter
  94. // Adapter class is going to be deleted and renamed as IAdapter
  95. {
  96. public:
  97. virtual ~IAdapter() = default;
  98. virtual GMatDesc desc() const = 0;
  99. // Implementation is responsible for setting the appropriate callback to
  100. // the view when accessed for writing, to ensure that the data from the view
  101. // is transferred to the device when the view is destroyed
  102. virtual View access(Access) = 0;
  103. virtual void serialize(cv::gapi::s11n::IOStream&) {
  104. GAPI_Assert(false && "Generic serialize method of RMat::IAdapter does nothing by default. "
  105. "Please, implement it in derived class to properly serialize the object.");
  106. }
  107. virtual void deserialize(cv::gapi::s11n::IIStream&) {
  108. GAPI_Assert(false && "Generic deserialize method of RMat::IAdapter does nothing by default. "
  109. "Please, implement it in derived class to properly deserialize the object.");
  110. }
  111. };
  112. using Adapter = IAdapter; // Keep backward compatibility
  113. using AdapterP = std::shared_ptr<IAdapter>;
  114. RMat() = default;
  115. RMat(AdapterP&& a) : m_adapter(std::move(a)) {}
  116. GMatDesc desc() const { return m_adapter->desc(); }
  117. // Note: When accessed for write there is no guarantee that returned view
  118. // will contain actual snapshot of the mapped device memory
  119. // (no guarantee that fetch from a device is performed). The only
  120. // guaranty is that when the view is destroyed, its data will be
  121. // transferred to the device
  122. View access(Access a) const { return m_adapter->access(a); }
  123. // Cast underlying RMat adapter to the particular adapter type,
  124. // return nullptr if underlying type is different
  125. template<typename T> T* get() const
  126. {
  127. static_assert(std::is_base_of<IAdapter, T>::value, "T is not derived from IAdapter!");
  128. GAPI_Assert(m_adapter != nullptr);
  129. return dynamic_cast<T*>(m_adapter.get());
  130. }
  131. void serialize(cv::gapi::s11n::IOStream& os) const {
  132. m_adapter->serialize(os);
  133. }
  134. private:
  135. AdapterP m_adapter = nullptr;
  136. };
  137. template<typename T, typename... Ts>
  138. RMat make_rmat(Ts&&... args) { return { std::make_shared<T>(std::forward<Ts>(args)...) }; }
  139. /** @} */
  140. } //namespace cv
  141. #endif /* OPENCV_GAPI_RMAT_HPP */