gfluidbuffer.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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) 2018 Intel Corporation
  6. #ifndef OPENCV_GAPI_FLUID_BUFFER_HPP
  7. #define OPENCV_GAPI_FLUID_BUFFER_HPP
  8. #include <list>
  9. #include <numeric> // accumulate
  10. #include <ostream> // ostream
  11. #include <cstdint> // uint8_t
  12. #include <opencv2/gapi/opencv_includes.hpp>
  13. #include <opencv2/gapi/gmat.hpp>
  14. #include <opencv2/gapi/util/optional.hpp>
  15. namespace cv {
  16. namespace gapi {
  17. namespace fluid {
  18. struct Border
  19. {
  20. // This constructor is required to support existing kernels which are part of G-API
  21. Border(int _type, cv::Scalar _val) : type(_type), value(_val) {}
  22. int type;
  23. cv::Scalar value;
  24. };
  25. using BorderOpt = util::optional<Border>;
  26. bool operator == (const Border& b1, const Border& b2);
  27. class GAPI_EXPORTS Buffer;
  28. class GAPI_EXPORTS View
  29. {
  30. public:
  31. struct Cache
  32. {
  33. std::vector<const uint8_t*> m_linePtrs;
  34. GMatDesc m_desc;
  35. int m_border_size = 0;
  36. inline const uint8_t* linePtr(int index) const
  37. {
  38. // "out_of_window" check:
  39. // user must not request the lines which are outside of specified kernel window
  40. GAPI_DbgAssert(index >= -m_border_size
  41. && index < -m_border_size + static_cast<int>(m_linePtrs.size()));
  42. return m_linePtrs[index + m_border_size];
  43. }
  44. };
  45. const inline uint8_t* InLineB(int index) const // -(w-1)/2...0...+(w-1)/2 for Filters
  46. {
  47. return m_cache->linePtr(index);
  48. }
  49. template<typename T> const inline T* InLine(int i) const
  50. {
  51. const uint8_t* ptr = this->InLineB(i);
  52. return reinterpret_cast<const T*>(ptr);
  53. }
  54. inline operator bool() const { return m_priv != nullptr; }
  55. bool ready() const;
  56. inline int length() const { return m_cache->m_desc.size.width; }
  57. int y() const;
  58. inline const GMatDesc& meta() const { return m_cache->m_desc; }
  59. class GAPI_EXPORTS Priv; // internal use only
  60. Priv& priv(); // internal use only
  61. const Priv& priv() const; // internal use only
  62. View();
  63. View(std::unique_ptr<Priv>&& p);
  64. View(View&& v);
  65. View& operator=(View&& v);
  66. ~View();
  67. private:
  68. std::unique_ptr<Priv> m_priv;
  69. const Cache* m_cache = nullptr;
  70. };
  71. class GAPI_EXPORTS Buffer
  72. {
  73. public:
  74. struct Cache
  75. {
  76. std::vector<uint8_t*> m_linePtrs;
  77. GMatDesc m_desc;
  78. };
  79. // Default constructor (executable creation stage,
  80. // all following initialization performed in Priv::init())
  81. Buffer();
  82. // Scratch constructor (user kernels)
  83. Buffer(const cv::GMatDesc &desc);
  84. // Constructor for intermediate buffers (for tests)
  85. Buffer(const cv::GMatDesc &desc,
  86. int max_line_consumption, int border_size,
  87. int skew,
  88. int wlpi,
  89. BorderOpt border);
  90. // Constructor for in/out buffers (for tests)
  91. Buffer(const cv::Mat &data, bool is_input);
  92. ~Buffer();
  93. Buffer& operator=(Buffer&&);
  94. inline uint8_t* OutLineB(int index = 0)
  95. {
  96. return m_cache->m_linePtrs[index];
  97. }
  98. template<typename T> inline T* OutLine(int index = 0)
  99. {
  100. uint8_t* ptr = this->OutLineB(index);
  101. return reinterpret_cast<T*>(ptr);
  102. }
  103. int y() const;
  104. int linesReady() const;
  105. void debug(std::ostream &os) const;
  106. inline int length() const { return m_cache->m_desc.size.width; }
  107. int lpi() const; // LPI for WRITER
  108. inline const GMatDesc& meta() const { return m_cache->m_desc; }
  109. View mkView(int borderSize, bool ownStorage);
  110. void addView(const View* v);
  111. class GAPI_EXPORTS Priv; // internal use only
  112. Priv& priv(); // internal use only
  113. const Priv& priv() const; // internal use only
  114. private:
  115. std::unique_ptr<Priv> m_priv;
  116. const Cache* m_cache;
  117. };
  118. } // namespace cv::gapi::fluid
  119. } // namespace cv::gapi
  120. } // namespace cv
  121. #endif // OPENCV_GAPI_FLUID_BUFFER_HPP