Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / fluid / modules / gapi / src / backends / fluid / gfluidbuffer_priv.hpp
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-2019 Intel Corporation
6
7
8 #ifndef OPENCV_GAPI_FLUID_BUFFER_PRIV_HPP
9 #define OPENCV_GAPI_FLUID_BUFFER_PRIV_HPP
10
11 #include <vector>
12
13 #include "opencv2/gapi/fluid/gfluidbuffer.hpp"
14 #include "opencv2/gapi/own/exports.hpp" // GAPI_EXPORTS
15
16 namespace cv {
17 namespace gapi {
18 namespace fluid {
19
20 class BufferStorageWithBorder;
21
22 class BorderHandler
23 {
24 protected:
25     int m_border_size;
26
27 public:
28     BorderHandler(int border_size);
29     virtual ~BorderHandler() = default;
30     virtual const uint8_t* inLineB(int log_idx, const BufferStorageWithBorder &data, int desc_height) const = 0;
31
32     // Fills border pixels after buffer allocation (if possible (for const border))
33     inline virtual void fillCompileTimeBorder(BufferStorageWithBorder &) { /* nothing */ }
34
35     // Fills required border lines
36     inline virtual void updateBorderPixels(BufferStorageWithBorder& /*data*/, int /*startLine*/, int /*lpi*/) const { /* nothing */ }
37
38     inline int borderSize() const { return m_border_size; }
39     inline virtual std::size_t size() const { return 0; }
40 };
41
42 template<int BorderType>
43 class BorderHandlerT : public BorderHandler
44 {
45     std::function<void(uint8_t*,int,int,int)> m_fill_border_row;
46 public:
47     BorderHandlerT(int border_size, int data_type);
48     virtual void updateBorderPixels(BufferStorageWithBorder& data, int startLine, int lpi) const override;
49     virtual const uint8_t* inLineB(int log_idx, const BufferStorageWithBorder &data, int desc_height) const override;
50 };
51
52 template<>
53 class BorderHandlerT<cv::BORDER_CONSTANT> : public BorderHandler
54 {
55     cv::gapi::own::Scalar m_border_value;
56     cv::gapi::own::Mat m_const_border;
57
58 public:
59     BorderHandlerT(int border_size, cv::gapi::own::Scalar border_value);
60     virtual const uint8_t* inLineB(int log_idx, const BufferStorageWithBorder &data, int desc_height) const override;
61     virtual void fillCompileTimeBorder(BufferStorageWithBorder &) override;
62     virtual std::size_t size() const override;
63 };
64
65 class BufferStorage
66 {
67 protected:
68     cv::gapi::own::Mat m_data;
69
70 public:
71     void updateInCache(View::Cache& cache, int start_log_idx, int nLines) const;
72     void updateOutCache(Buffer::Cache& cache, int start_log_idx, int nLines);
73
74     virtual void copyTo(BufferStorageWithBorder &dst, int startLine, int nLines) const = 0;
75
76     virtual ~BufferStorage() = default;
77
78     virtual const uint8_t* ptr(int idx) const = 0;
79     virtual       uint8_t* ptr(int idx) = 0;
80
81     inline bool empty() const { return m_data.empty(); }
82
83     inline const cv::gapi::own::Mat& data() const { return m_data; }
84     inline       cv::gapi::own::Mat& data()       { return m_data; }
85
86     inline int rows() const { return m_data.rows; }
87     inline int cols() const { return m_data.cols; }
88     inline int type() const { return m_data.type(); }
89
90     virtual const uint8_t* inLineB(int log_idx, int desc_height) const = 0;
91
92     // FIXME? remember parent and remove src parameter?
93     virtual void updateBeforeRead(int startLine, int nLines, const BufferStorage& src) = 0;
94     virtual void updateAfterWrite(int startLine, int nLines) = 0;
95
96     virtual inline int physIdx(int logIdx) const = 0;
97
98     virtual size_t size() const = 0;
99 };
100
101 class BufferStorageWithoutBorder final : public BufferStorage
102 {
103     bool m_is_virtual = true;
104     cv::gapi::own::Rect m_roi;
105
106 public:
107     virtual void copyTo(BufferStorageWithBorder &dst, int startLine, int nLines) const override;
108
109     inline virtual const uint8_t* ptr(int idx) const override
110     {
111         GAPI_DbgAssert((m_is_virtual && m_roi == cv::gapi::own::Rect{}) || (!m_is_virtual && m_roi != cv::gapi::own::Rect{}));
112         return m_data.ptr(physIdx(idx), 0);
113     }
114     inline virtual uint8_t* ptr(int idx) override
115     {
116         GAPI_DbgAssert((m_is_virtual && m_roi == cv::gapi::own::Rect{}) || (!m_is_virtual && m_roi != cv::gapi::own::Rect{}));
117         return m_data.ptr(physIdx(idx), 0);
118     }
119
120     inline void attach(const cv::gapi::own::Mat& _data, cv::gapi::own::Rect _roi)
121     {
122         m_data = _data(_roi);
123         m_roi = _roi;
124         m_is_virtual = false;
125     }
126
127     void create(int capacity, int desc_width, int type);
128
129     inline virtual const uint8_t* inLineB(int log_idx, int /*desc_height*/) const override { return ptr(log_idx); }
130
131     virtual void updateBeforeRead(int startLine, int nLines, const BufferStorage& src) override;
132     virtual void updateAfterWrite(int startLine, int nLines) override;
133
134     inline virtual int physIdx(int logIdx) const override { return (logIdx - m_roi.y) % m_data.rows; }
135
136     virtual size_t size() const override;
137 };
138
139 class BufferStorageWithBorder final: public BufferStorage
140 {
141     std::unique_ptr<BorderHandler> m_borderHandler;
142
143 public:
144     inline int borderSize() const { return m_borderHandler->borderSize(); }
145
146     virtual void copyTo(BufferStorageWithBorder &dst, int startLine, int nLines) const override;
147
148     inline virtual const uint8_t* ptr(int idx) const override
149     {
150         return m_data.ptr(physIdx(idx), borderSize());
151     }
152     inline virtual uint8_t* ptr(int idx) override
153     {
154         return m_data.ptr(physIdx(idx), borderSize());
155     }
156
157     void init(int depth, int border_size, Border border);
158     void create(int capacity, int desc_width, int dtype);
159
160     virtual const uint8_t* inLineB(int log_idx, int desc_height) const override;
161
162     virtual void updateBeforeRead(int startLine, int nLines, const BufferStorage &src) override;
163     virtual void updateAfterWrite(int startLine, int nLines) override;
164
165     inline virtual int physIdx(int logIdx) const override { return logIdx % m_data.rows; }
166
167     virtual size_t size() const override;
168 };
169
170 // FIXME: GAPI_EXPORTS is used here only to access internal methods
171 // like readDone/writeDone in low-level tests
172 class GAPI_EXPORTS View::Priv
173 {
174     friend class View;
175 protected:
176     View::Cache m_cache;
177
178     const Buffer *m_p           = nullptr; // FIXME replace with weak_ptr
179     int           m_read_caret  = -1;
180     int           m_lines_next_iter = -1;
181     int m_border_size = -1;
182
183 public:
184     virtual ~Priv() = default;
185     // API used by actors/backend
186
187     const View::Cache& cache() const { return m_cache; }
188     void initCache(int lineConsumption);
189
190     virtual void allocate(int lineConsumption, BorderOpt border) = 0;
191     virtual void prepareToRead() = 0;
192
193     void readDone(int linesRead, int linesForNextIteration);
194     void reset(int linesForFirstIteration);
195
196     virtual std::size_t size() const = 0;
197
198     // Does the view have enough unread lines for next iteration
199     bool ready() const;
200
201     // API used (indirectly) by user code
202     virtual const uint8_t* InLineB(int index) const = 0;
203 };
204
205 class ViewPrivWithoutOwnBorder final : public View::Priv
206 {
207 public:
208     // API used by actors/backend
209     ViewPrivWithoutOwnBorder(const Buffer *p, int borderSize);
210
211     virtual void allocate(int lineConsumption, BorderOpt) override;
212     virtual void prepareToRead() override;
213
214     inline virtual std::size_t size() const override { return 0; }
215
216     // API used (indirectly) by user code
217     virtual const uint8_t* InLineB(int index) const override;
218 };
219
220 class ViewPrivWithOwnBorder final : public View::Priv
221 {
222     BufferStorageWithBorder m_own_storage;
223
224 public:
225     // API used by actors/backend
226     ViewPrivWithOwnBorder(const Buffer *p, int borderSize);
227
228     virtual void allocate(int lineConsumption, BorderOpt border) override;
229     virtual void prepareToRead() override;
230     virtual std::size_t size() const override;
231
232     // API used (indirectly) by user code
233     virtual const uint8_t* InLineB(int index) const override;
234 };
235
236 void debugBufferPriv(const Buffer& buffer, std::ostream &os);
237
238 // FIXME: GAPI_EXPORTS is used here only to access internal methods
239 // like readDone/writeDone in low-level tests
240 class GAPI_EXPORTS Buffer::Priv
241 {
242     Buffer::Cache m_cache;
243
244     int m_writer_lpi       =  1;
245
246     cv::GMatDesc m_desc    = cv::GMatDesc{-1,-1,{-1,-1}};
247     bool m_is_input        = false;
248
249     int m_write_caret      = -1;
250
251     std::vector<View> m_views;
252
253     std::unique_ptr<BufferStorage> m_storage;
254
255     // Coordinate starting from which this buffer is assumed
256     // to be read (with border not being taken into account)
257     int m_readStart;
258     cv::gapi::own::Rect m_roi;
259
260     friend void debugBufferPriv(const Buffer& p, std::ostream &os);
261
262 public:
263     Priv() = default;
264     Priv(int read_start, cv::gapi::own::Rect roi);
265
266     inline const BufferStorage& storage() const { return *m_storage.get(); }
267
268     // API used by actors/backend
269     void init(const cv::GMatDesc &desc,
270               int writer_lpi,
271               int readStart,
272               cv::gapi::own::Rect roi);
273
274     void allocate(BorderOpt border, int border_size, int line_consumption, int skew);
275     void bindTo(const cv::gapi::own::Mat &data, bool is_input);
276
277     inline void addView(const View& view) { m_views.push_back(view); }
278
279     inline const GMatDesc& meta() const { return m_desc; }
280
281     bool full() const;
282     void writeDone();
283     void reset();
284     int size() const;
285
286     int linesReady() const;
287
288     inline int y() const { return m_write_caret; }
289
290     inline int writer_lpi()     const { return m_writer_lpi; }
291
292     // API used (indirectly) by user code
293     uint8_t* OutLineB(int index = 0);
294     int lpi() const;
295
296     inline int readStart()   const { return m_readStart; }
297     inline int writeStart()  const { return m_roi.y; }
298     inline int writeEnd()    const { return m_roi.y + m_roi.height; }
299     inline int outputLines() const { return m_roi.height; }
300
301     inline const Buffer::Cache& cache() const { return m_cache; }
302 };
303
304 } // namespace cv::gapi::fluid
305 } // namespace cv::gapi
306 } // namespace cv
307
308 #endif // OPENCV_GAPI_FLUID_BUFFER_PRIV_HPP