github.com/kaydxh/golang@v0.0.131/pkg/gocv/cgo/third_path/opencv4/include/opencv2/gapi/rmat.hpp (about)

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