Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / fluid / modules / gapi / include / opencv2 / gapi / cpu / gcpukernel.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_GCPUKERNEL_HPP
9 #define OPENCV_GAPI_GCPUKERNEL_HPP
10
11 #include <functional>
12 #include <unordered_map>
13 #include <utility>
14 #include <vector>
15
16 #include <opencv2/core/mat.hpp>
17 #include <opencv2/gapi/gcommon.hpp>
18 #include <opencv2/gapi/gkernel.hpp>
19 #include <opencv2/gapi/garg.hpp>
20 #include <opencv2/gapi/own/convert.hpp> //to_ocv
21 #include <opencv2/gapi/util/compiler_hints.hpp> //suppress_unused_warning
22
23 // FIXME: namespace scheme for backends?
24 namespace cv {
25
26 namespace gimpl
27 {
28     // Forward-declare an internal class
29     class GCPUExecutable;
30 } // namespace gimpl
31
32 namespace gapi
33 {
34 namespace cpu
35 {
36     /**
37      * \addtogroup gapi_std_backends
38      * @{
39      *
40      * @brief G-API backends available in this OpenCV version
41      *
42      * G-API backends play a corner stone role in G-API execution
43      * stack. Every backend is hardware-oriented and thus can run its
44      * kernels efficiently on the target platform.
45      *
46      * Backends are usually "back boxes" for G-API users -- on the API
47      * side, all backends are represented as different objects of the
48      * same class cv::gapi::GBackend. User can manipulate with backends
49      * mainly by specifying which kernels to use or where to look up
50      * for kernels first.
51      *
52      * @sa @ref gapi_hld, cv::gapi::lookup_order()
53      */
54
55     /**
56      * @brief Get a reference to CPU (OpenCV) backend.
57      *
58      * This is the default backend in G-API at the moment, providing
59      * broader functional coverage but losing some graph model
60      * advantages. Provided mostly for reference and prototyping
61      * purposes.
62      *
63      * @sa gapi_std_backends
64      */
65     GAPI_EXPORTS cv::gapi::GBackend backend();
66     /** @} */
67 } // namespace cpu
68 } // namespace gapi
69
70 // Represents arguments which are passed to a wrapped CPU function
71 // FIXME: put into detail?
72 class GAPI_EXPORTS GCPUContext
73 {
74 public:
75     // Generic accessor API
76     template<typename T>
77     const T& inArg(int input) { return m_args.at(input).get<T>(); }
78
79     // Syntax sugar
80     const cv::gapi::own::Mat&   inMat(int input);
81     cv::gapi::own::Mat&         outMatR(int output); // FIXME: Avoid cv::gapi::own::Mat m = ctx.outMatR()
82
83     const cv::gapi::own::Scalar& inVal(int input);
84     cv::gapi::own::Scalar& outValR(int output); // FIXME: Avoid cv::gapi::own::Scalar s = ctx.outValR()
85     template<typename T> std::vector<T>& outVecR(int output) // FIXME: the same issue
86     {
87         return outVecRef(output).wref<T>();
88     }
89
90 protected:
91     detail::VectorRef& outVecRef(int output);
92
93     std::vector<GArg> m_args;
94
95     //FIXME: avoid conversion of arguments from internal representaion to OpenCV one on each call
96     //to OCV kernel. (This can be achieved by a two single time conversions in GCPUExecutable::run,
97     //once on enter for input and output arguments, and once before return for output arguments only
98     std::unordered_map<std::size_t, GRunArgP> m_results;
99
100     friend class gimpl::GCPUExecutable;
101 };
102
103 class GAPI_EXPORTS GCPUKernel
104 {
105 public:
106     // This function is kernel's execution entry point (does the processing work)
107     using F = std::function<void(GCPUContext &)>;
108
109     GCPUKernel();
110     explicit GCPUKernel(const F& f);
111
112     void apply(GCPUContext &ctx);
113
114 protected:
115     F m_f;
116 };
117
118 // FIXME: This is an ugly ad-hoc imlpementation. TODO: refactor
119
120 namespace detail
121 {
122 template<class T> struct get_in;
123 template<> struct get_in<cv::GMat>
124 {
125     static cv::Mat    get(GCPUContext &ctx, int idx) { return to_ocv(ctx.inMat(idx)); }
126 };
127 template<> struct get_in<cv::GScalar>
128 {
129     static cv::Scalar get(GCPUContext &ctx, int idx) { return to_ocv(ctx.inVal(idx)); }
130 };
131 template<typename U> struct get_in<cv::GArray<U> >
132 {
133     static const std::vector<U>& get(GCPUContext &ctx, int idx) { return ctx.inArg<VectorRef>(idx).rref<U>(); }
134 };
135 template<class T> struct get_in
136 {
137     static T get(GCPUContext &ctx, int idx) { return ctx.inArg<T>(idx); }
138 };
139
140 struct tracked_cv_mat{
141     tracked_cv_mat(cv::gapi::own::Mat& m) : r{to_ocv(m)}, original_data{m.data} {}
142     cv::Mat r;
143     uchar* original_data;
144
145     operator cv::Mat& (){ return r;}
146     void validate() const{
147         if (r.data != original_data)
148         {
149             util::throw_error
150                 (std::logic_error
151                  ("OpenCV kernel output parameter was reallocated. \n"
152                   "Incorrect meta data was provided ?"));
153         }
154     }
155 };
156
157 struct scalar_wrapper
158 {
159     scalar_wrapper(cv::gapi::own::Scalar& s) : m_s{cv::gapi::own::to_ocv(s)}, m_org_s(s) {};
160     operator cv::Scalar& () { return m_s; }
161     void writeBack() const  { m_org_s = to_own(m_s); }
162
163     cv::Scalar m_s;
164     cv::gapi::own::Scalar& m_org_s;
165 };
166
167 template<typename... Outputs>
168 void postprocess(Outputs&... outs)
169 {
170     struct
171     {
172         void operator()(tracked_cv_mat* bm) { bm->validate();  }
173         void operator()(scalar_wrapper* sw) { sw->writeBack(); }
174         void operator()(...)                {                  }
175
176     } validate;
177     //dummy array to unfold parameter pack
178     int dummy[] = { 0, (validate(&outs), 0)... };
179     cv::util::suppress_unused_warning(dummy);
180 }
181
182 template<class T> struct get_out;
183 template<> struct get_out<cv::GMat>
184 {
185     static tracked_cv_mat get(GCPUContext &ctx, int idx)
186     {
187         auto& r = ctx.outMatR(idx);
188         return {r};
189     }
190 };
191 template<> struct get_out<cv::GScalar>
192 {
193     static scalar_wrapper get(GCPUContext &ctx, int idx)
194     {
195         auto& s = ctx.outValR(idx);
196         return {s};
197     }
198 };
199 template<typename U> struct get_out<cv::GArray<U>>
200 {
201     static std::vector<U>& get(GCPUContext &ctx, int idx)
202     {
203         return ctx.outVecR<U>(idx);
204     }
205 };
206
207 template<typename, typename, typename>
208 struct OCVCallHelper;
209
210 // FIXME: probably can be simplified with std::apply or analogue.
211 template<typename Impl, typename... Ins, typename... Outs>
212 struct OCVCallHelper<Impl, std::tuple<Ins...>, std::tuple<Outs...> >
213 {
214     template<typename... Inputs>
215     struct call_and_postprocess
216     {
217         template<typename... Outputs>
218         static void call(Inputs&&... ins, Outputs&&... outs)
219         {
220             //not using a std::forward on outs is deliberate in order to
221             //cause compilation error, by tring to bind rvalue references to lvalue references
222             Impl::run(std::forward<Inputs>(ins)..., outs...);
223
224             postprocess(outs...);
225         }
226     };
227
228     template<int... IIs, int... OIs>
229     static void call_impl(GCPUContext &ctx, detail::Seq<IIs...>, detail::Seq<OIs...>)
230     {
231         //Make sure that OpenCV kernels do not reallocate memory for output parameters
232         //by comparing it's state (data ptr) before and after the call.
233         //This is done by converting each output Mat into tracked_cv_mat object, and binding
234         //them to parameters of ad-hoc function
235         //Convert own::Scalar to cv::Scalar before call kernel and run kernel
236         //convert cv::Scalar to own::Scalar after call kernel and write back results
237         call_and_postprocess<decltype(get_in<Ins>::get(ctx, IIs))...>::call(get_in<Ins>::get(ctx, IIs)..., get_out<Outs>::get(ctx, OIs)...);
238     }
239
240     static void call(GCPUContext &ctx)
241     {
242         call_impl(ctx,
243                   typename detail::MkSeq<sizeof...(Ins)>::type(),
244                   typename detail::MkSeq<sizeof...(Outs)>::type());
245     }
246 };
247
248 } // namespace detail
249
250 template<class Impl, class K>
251 class GCPUKernelImpl: public detail::OCVCallHelper<Impl, typename K::InArgs, typename K::OutArgs>
252 {
253     using P = detail::OCVCallHelper<Impl, typename K::InArgs, typename K::OutArgs>;
254
255 public:
256     using API = K;
257
258     static cv::gapi::GBackend backend()  { return cv::gapi::cpu::backend(); }
259     static cv::GCPUKernel     kernel()   { return GCPUKernel(&P::call);     }
260 };
261
262 #define GAPI_OCV_KERNEL(Name, API) struct Name: public cv::GCPUKernelImpl<Name, API>
263
264 } // namespace cv
265
266 #endif // OPENCV_GAPI_GCPUKERNEL_HPP