gfluidbuffer.hpp 4.1 KB

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