Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/gapi/src/api/gbackend.cpp
16338 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
#include "precomp.hpp"
9
#include <memory> // unique_ptr
10
11
#include "opencv2/gapi/gkernel.hpp"
12
#include "opencv2/gapi/own/convert.hpp"
13
14
#include "api/gbackend_priv.hpp"
15
#include "backends/common/gbackend.hpp"
16
#include "compiler/gobjref.hpp"
17
#include "compiler/gislandmodel.hpp"
18
19
// GBackend private implementation /////////////////////////////////////////////
20
void cv::gapi::GBackend::Priv::unpackKernel(ade::Graph & /*graph */ ,
21
const ade::NodeHandle & /*op_node*/ ,
22
const GKernelImpl & /*impl */ )
23
{
24
// Default implementation is still there as Priv
25
// is instantiated by some tests.
26
// Priv is even instantiated as a mock object in a number of tests
27
// as a backend and this method is called for mock objects (doing nothing).
28
// FIXME: add a warning message here
29
// FIXME: Do something with this! Ideally this function should be "=0";
30
}
31
32
std::unique_ptr<cv::gimpl::GIslandExecutable>
33
cv::gapi::GBackend::Priv::compile(const ade::Graph&,
34
const GCompileArgs&,
35
const std::vector<ade::NodeHandle> &) const
36
{
37
// ...and this method is here for the same reason!
38
GAPI_Assert(false);
39
return {};
40
}
41
42
void cv::gapi::GBackend::Priv::addBackendPasses(ade::ExecutionEngineSetupContext &)
43
{
44
// Do nothing by default, plugins may override this to
45
// add custom (backend-specific) graph transformations
46
}
47
48
// GBackend public implementation //////////////////////////////////////////////
49
cv::gapi::GBackend::GBackend()
50
{
51
}
52
53
cv::gapi::GBackend::GBackend(std::shared_ptr<cv::gapi::GBackend::Priv> &&p)
54
: m_priv(std::move(p))
55
{
56
}
57
58
cv::gapi::GBackend::Priv& cv::gapi::GBackend::priv()
59
{
60
return *m_priv;
61
}
62
63
const cv::gapi::GBackend::Priv& cv::gapi::GBackend::priv() const
64
{
65
return *m_priv;
66
}
67
68
std::size_t cv::gapi::GBackend::hash() const
69
{
70
return std::hash<const cv::gapi::GBackend::Priv*>{}(m_priv.get());
71
}
72
73
bool cv::gapi::GBackend::operator== (const cv::gapi::GBackend &rhs) const
74
{
75
return m_priv == rhs.m_priv;
76
}
77
78
// Abstract Host-side data manipulation ////////////////////////////////////////
79
// Reused between CPU backend and more generic GExecutor
80
namespace cv {
81
namespace gimpl {
82
namespace magazine {
83
84
// FIXME implement the below functions with visit()?
85
86
void bindInArg(Mag& mag, const RcDesc &rc, const GRunArg &arg)
87
{
88
switch (rc.shape)
89
{
90
case GShape::GMAT:
91
{
92
auto& mag_mat = mag.template slot<cv::gapi::own::Mat>()[rc.id];
93
switch (arg.index())
94
{
95
case GRunArg::index_of<cv::gapi::own::Mat>() : mag_mat = util::get<cv::gapi::own::Mat>(arg); break;
96
#if !defined(GAPI_STANDALONE)
97
case GRunArg::index_of<cv::Mat>() : mag_mat = to_own(util::get<cv::Mat>(arg)); break;
98
#endif // !defined(GAPI_STANDALONE)
99
default: util::throw_error(std::logic_error("content type of the runtime argument does not match to resource description ?"));
100
}
101
break;
102
}
103
104
105
case GShape::GSCALAR:
106
{
107
auto& mag_scalar = mag.template slot<cv::gapi::own::Scalar>()[rc.id];
108
switch (arg.index())
109
{
110
case GRunArg::index_of<cv::gapi::own::Scalar>() : mag_scalar = util::get<cv::gapi::own::Scalar>(arg); break;
111
#if !defined(GAPI_STANDALONE)
112
case GRunArg::index_of<cv::Scalar>() : mag_scalar = to_own(util::get<cv::Scalar>(arg)); break;
113
#endif // !defined(GAPI_STANDALONE)
114
default: util::throw_error(std::logic_error("content type of the runtime argument does not match to resource description ?"));
115
}
116
break;
117
}
118
119
case GShape::GARRAY:
120
mag.template slot<cv::detail::VectorRef>()[rc.id] = util::get<cv::detail::VectorRef>(arg);
121
break;
122
123
default:
124
util::throw_error(std::logic_error("Unsupported GShape type"));
125
}
126
}
127
128
void bindOutArg(Mag& mag, const RcDesc &rc, const GRunArgP &arg)
129
{
130
switch (rc.shape)
131
{
132
case GShape::GMAT:
133
{
134
auto& mag_mat = mag.template slot<cv::gapi::own::Mat>()[rc.id];
135
switch (arg.index())
136
{
137
case GRunArgP::index_of<cv::gapi::own::Mat*>() : mag_mat = * util::get<cv::gapi::own::Mat*>(arg); break;
138
#if !defined(GAPI_STANDALONE)
139
case GRunArgP::index_of<cv::Mat*>() : mag_mat = to_own(* util::get<cv::Mat*>(arg)); break;
140
#endif // !defined(GAPI_STANDALONE)
141
default: util::throw_error(std::logic_error("content type of the runtime argument does not match to resource description ?"));
142
}
143
break;
144
}
145
146
case GShape::GSCALAR:
147
{
148
auto& mag_scalar = mag.template slot<cv::gapi::own::Scalar>()[rc.id];
149
switch (arg.index())
150
{
151
case GRunArgP::index_of<cv::gapi::own::Scalar*>() : mag_scalar = *util::get<cv::gapi::own::Scalar*>(arg); break;
152
#if !defined(GAPI_STANDALONE)
153
case GRunArgP::index_of<cv::Scalar*>() : mag_scalar = to_own(*util::get<cv::Scalar*>(arg)); break;
154
#endif // !defined(GAPI_STANDALONE)
155
default: util::throw_error(std::logic_error("content type of the runtime argument does not match to resource description ?"));
156
}
157
break;
158
}
159
case GShape::GARRAY:
160
mag.template slot<cv::detail::VectorRef>()[rc.id] = util::get<cv::detail::VectorRef>(arg);
161
break;
162
163
default:
164
util::throw_error(std::logic_error("Unsupported GShape type"));
165
break;
166
}
167
}
168
169
void resetInternalData(Mag& mag, const Data &d)
170
{
171
if (d.storage != Data::Storage::INTERNAL)
172
return;
173
174
switch (d.shape)
175
{
176
case GShape::GARRAY:
177
util::get<cv::detail::ConstructVec>(d.ctor)
178
(mag.template slot<cv::detail::VectorRef>()[d.rc]);
179
break;
180
181
case GShape::GSCALAR:
182
mag.template slot<cv::gapi::own::Scalar>()[d.rc] = cv::gapi::own::Scalar();
183
break;
184
185
case GShape::GMAT:
186
// Do nothign here - FIXME unify with initInternalData?
187
break;
188
189
default:
190
util::throw_error(std::logic_error("Unsupported GShape type"));
191
break;
192
}
193
}
194
195
cv::GRunArg getArg(const Mag& mag, const RcDesc &ref)
196
{
197
// Wrap associated CPU object (either host or an internal one)
198
switch (ref.shape)
199
{
200
case GShape::GMAT: return GRunArg(mag.template slot<cv::gapi::own::Mat>().at(ref.id));
201
case GShape::GSCALAR: return GRunArg(mag.template slot<cv::gapi::own::Scalar>().at(ref.id));
202
// Note: .at() is intentional for GArray as object MUST be already there
203
// (and constructed by either bindIn/Out or resetInternal)
204
case GShape::GARRAY: return GRunArg(mag.template slot<cv::detail::VectorRef>().at(ref.id));
205
default:
206
util::throw_error(std::logic_error("Unsupported GShape type"));
207
break;
208
}
209
}
210
211
cv::GRunArgP getObjPtr(Mag& mag, const RcDesc &rc)
212
{
213
switch (rc.shape)
214
{
215
case GShape::GMAT: return GRunArgP(&mag.template slot<cv::gapi::own::Mat>() [rc.id]);
216
case GShape::GSCALAR: return GRunArgP(&mag.template slot<cv::gapi::own::Scalar>()[rc.id]);
217
// Note: .at() is intentional for GArray as object MUST be already there
218
// (and constructer by either bindIn/Out or resetInternal)
219
case GShape::GARRAY:
220
// FIXME(DM): For some absolutely unknown to me reason, move
221
// semantics is involved here without const_cast to const (and
222
// value from map is moved into return value GRunArgP, leaving
223
// map with broken value I've spent few late Friday hours
224
// debugging this!!!1
225
return GRunArgP(const_cast<const Mag&>(mag)
226
.template slot<cv::detail::VectorRef>().at(rc.id));
227
default:
228
util::throw_error(std::logic_error("Unsupported GShape type"));
229
break;
230
}
231
}
232
233
void writeBack(const Mag& mag, const RcDesc &rc, GRunArgP &g_arg)
234
{
235
switch (rc.shape)
236
{
237
case GShape::GARRAY:
238
// Do nothing - should we really do anything here?
239
break;
240
241
case GShape::GMAT:
242
{
243
//simply check that memory was not reallocated, i.e.
244
//both instances of Mat pointing to the same memory
245
uchar* out_arg_data = nullptr;
246
switch (g_arg.index())
247
{
248
case GRunArgP::index_of<cv::gapi::own::Mat*>() : out_arg_data = util::get<cv::gapi::own::Mat*>(g_arg)->data; break;
249
#if !defined(GAPI_STANDALONE)
250
case GRunArgP::index_of<cv::Mat*>() : out_arg_data = util::get<cv::Mat*>(g_arg)->data; break;
251
#endif // !defined(GAPI_STANDALONE)
252
default: util::throw_error(std::logic_error("content type of the runtime argument does not match to resource description ?"));
253
}
254
255
auto& in_mag = mag.template slot<cv::gapi::own::Mat>().at(rc.id);
256
GAPI_Assert((out_arg_data == in_mag.data) && " data for output parameters was reallocated ?");
257
break;
258
}
259
260
case GShape::GSCALAR:
261
{
262
switch (g_arg.index())
263
{
264
case GRunArgP::index_of<cv::gapi::own::Scalar*>() : *util::get<cv::gapi::own::Scalar*>(g_arg) = mag.template slot<cv::gapi::own::Scalar>().at(rc.id); break;
265
#if !defined(GAPI_STANDALONE)
266
case GRunArgP::index_of<cv::Scalar*>() : *util::get<cv::Scalar*>(g_arg) = cv::gapi::own::to_ocv(mag.template slot<cv::gapi::own::Scalar>().at(rc.id)); break;
267
#endif // !defined(GAPI_STANDALONE)
268
default: util::throw_error(std::logic_error("content type of the runtime argument does not match to resource description ?"));
269
}
270
break;
271
}
272
273
default:
274
util::throw_error(std::logic_error("Unsupported GShape type"));
275
break;
276
}
277
}
278
279
} // namespace magazine
280
} // namespace gimpl
281
} // namespace cv
282
283