Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/gapi/src/backends/fluid/gfluidbuffer_priv.hpp
16344 views
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
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 &) const { /* 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, int data_type, int desc_width);
60
virtual const uint8_t* inLineB(int log_idx, const BufferStorageWithBorder &data, int desc_height) const override;
61
virtual void fillCompileTimeBorder(BufferStorageWithBorder &) const 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
virtual void copyTo(BufferStorageWithBorder &dst, int startLine, int nLines) const = 0;
72
73
virtual ~BufferStorage() = default;
74
75
virtual const uint8_t* ptr(int idx) const = 0;
76
virtual uint8_t* ptr(int idx) = 0;
77
78
inline bool empty() const { return m_data.empty(); }
79
80
inline const cv::gapi::own::Mat& data() const { return m_data; }
81
inline cv::gapi::own::Mat& data() { return m_data; }
82
83
inline int rows() const { return m_data.rows; }
84
inline int cols() const { return m_data.cols; }
85
inline int type() const { return m_data.type(); }
86
87
virtual const uint8_t* inLineB(int log_idx, int desc_height) const = 0;
88
89
// FIXME? remember parent and remove src parameter?
90
virtual void updateBeforeRead(int startLine, int nLines, const BufferStorage& src) = 0;
91
virtual void updateAfterWrite(int startLine, int nLines) = 0;
92
93
virtual int physIdx(int logIdx) const = 0;
94
95
virtual size_t size() const = 0;
96
};
97
98
class BufferStorageWithoutBorder final : public BufferStorage
99
{
100
bool m_is_virtual = true;
101
cv::gapi::own::Rect m_roi;
102
103
public:
104
virtual void copyTo(BufferStorageWithBorder &dst, int startLine, int nLines) const override;
105
106
inline virtual const uint8_t* ptr(int idx) const override
107
{
108
GAPI_DbgAssert((m_is_virtual && m_roi == cv::gapi::own::Rect{}) || (!m_is_virtual && m_roi != cv::gapi::own::Rect{}));
109
return m_data.ptr(physIdx(idx), 0);
110
}
111
inline virtual uint8_t* ptr(int idx) override
112
{
113
GAPI_DbgAssert((m_is_virtual && m_roi == cv::gapi::own::Rect{}) || (!m_is_virtual && m_roi != cv::gapi::own::Rect{}));
114
return m_data.ptr(physIdx(idx), 0);
115
}
116
117
inline void attach(const cv::gapi::own::Mat& _data, cv::gapi::own::Rect _roi)
118
{
119
m_data = _data(_roi);
120
m_roi = _roi;
121
m_is_virtual = false;
122
}
123
124
void create(int capacity, int desc_width, int type);
125
126
inline virtual const uint8_t* inLineB(int log_idx, int desc_height) const override;
127
128
virtual void updateBeforeRead(int startLine, int nLines, const BufferStorage& src) override;
129
virtual void updateAfterWrite(int startLine, int nLines) override;
130
131
inline virtual int physIdx(int logIdx) const override { return (logIdx - m_roi.y) % m_data.rows; }
132
133
virtual size_t size() const override;
134
};
135
136
class BufferStorageWithBorder final: public BufferStorage
137
{
138
std::unique_ptr<BorderHandler> m_borderHandler;
139
140
public:
141
inline int borderSize() const { return m_borderHandler->borderSize(); }
142
143
virtual void copyTo(BufferStorageWithBorder &dst, int startLine, int nLines) const override;
144
145
inline virtual const uint8_t* ptr(int idx) const override
146
{
147
return m_data.ptr(physIdx(idx), borderSize());
148
}
149
inline virtual uint8_t* ptr(int idx) override
150
{
151
return m_data.ptr(physIdx(idx), borderSize());
152
}
153
154
void create(int capacity, int desc_width, int type, int border_size, Border border);
155
156
virtual const uint8_t* inLineB(int log_idx, int desc_height) const override;
157
158
virtual void updateBeforeRead(int startLine, int nLines, const BufferStorage &src) override;
159
virtual void updateAfterWrite(int startLine, int nLines) override;
160
161
inline virtual int physIdx(int logIdx) const override { return logIdx % m_data.rows; }
162
163
virtual size_t size() const override;
164
};
165
166
// FIXME: GAPI_EXPORTS is used here only to access internal methods
167
// like readDone/writeDone in low-level tests
168
class GAPI_EXPORTS View::Priv
169
{
170
friend class View;
171
protected:
172
const Buffer *m_p = nullptr; // FIXME replace with weak_ptr
173
int m_read_caret = -1;
174
int m_lines_next_iter = -1;
175
int m_border_size = -1;
176
177
public:
178
virtual ~Priv() = default;
179
// API used by actors/backend
180
181
virtual void prepareToRead() = 0;
182
183
void readDone(int linesRead, int linesForNextIteration);
184
void reset(int linesForFirstIteration);
185
186
virtual std::size_t size() const = 0;
187
188
// Does the view have enough unread lines for next iteration
189
bool ready() const;
190
191
// API used (indirectly) by user code
192
virtual const uint8_t* InLineB(int index) const = 0;
193
};
194
195
class ViewPrivWithoutOwnBorder final : public View::Priv
196
{
197
public:
198
// API used by actors/backend
199
ViewPrivWithoutOwnBorder(const Buffer *p, int borderSize);
200
201
inline virtual void prepareToRead() override { /* nothing */ }
202
203
inline virtual std::size_t size() const override { return 0; }
204
205
// API used (indirectly) by user code
206
virtual const uint8_t* InLineB(int index) const override;
207
};
208
209
class ViewPrivWithOwnBorder final : public View::Priv
210
{
211
BufferStorageWithBorder m_own_storage;
212
213
public:
214
// API used by actors/backend
215
ViewPrivWithOwnBorder(const Buffer *p, int lineCapacity, int borderSize, Border border);
216
217
virtual void prepareToRead() override;
218
virtual std::size_t size() const override;
219
220
// API used (indirectly) by user code
221
virtual const uint8_t* InLineB(int index) const override;
222
};
223
224
void debugBufferPriv(const Buffer& buffer, std::ostream &os);
225
226
// FIXME: GAPI_EXPORTS is used here only to access internal methods
227
// like readDone/writeDone in low-level tests
228
class GAPI_EXPORTS Buffer::Priv
229
{
230
int m_writer_lpi = 1;
231
232
cv::GMatDesc m_desc = cv::GMatDesc{-1,-1,{-1,-1}};
233
bool m_is_input = false;
234
235
int m_write_caret = -1;
236
237
std::vector<View> m_views;
238
239
std::unique_ptr<BufferStorage> m_storage;
240
241
// Coordinate starting from which this buffer is assumed
242
// to be read (with border not being taken into account)
243
int m_readStart;
244
cv::gapi::own::Rect m_roi;
245
246
friend void debugBufferPriv(const Buffer& p, std::ostream &os);
247
248
public:
249
Priv() = default;
250
Priv(int read_start, cv::gapi::own::Rect roi);
251
252
inline const BufferStorage& storage() const { return *m_storage.get(); }
253
254
// API used by actors/backend
255
void init(const cv::GMatDesc &desc,
256
int wlpi,
257
int readStart,
258
cv::gapi::own::Rect roi);
259
260
void allocate(BorderOpt border, int border_size, int line_consumption, int skew);
261
void bindTo(const cv::gapi::own::Mat &data, bool is_input);
262
263
inline void addView(const View& view) { m_views.push_back(view); }
264
265
inline const GMatDesc& meta() const { return m_desc; }
266
267
bool full() const;
268
void writeDone();
269
void reset();
270
int size() const;
271
272
int linesReady() const;
273
274
inline int y() const { return m_write_caret; }
275
276
inline int writer_lpi() const { return m_writer_lpi; }
277
278
// API used (indirectly) by user code
279
uint8_t* OutLineB(int index = 0);
280
int lpi() const;
281
282
inline int readStart() const { return m_readStart; }
283
inline int writeStart() const { return m_roi.y; }
284
inline int writeEnd() const { return m_roi.y + m_roi.height; }
285
inline int outputLines() const { return m_roi.height; }
286
};
287
288
} // namespace cv::gapi::fluid
289
} // namespace cv::gapi
290
} // namespace cv
291
292
#endif // OPENCV_GAPI_FLUID_BUFFER_PRIV_HPP
293
294