Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / fluid / modules / gapi / include / opencv2 / gapi / ocl / goclkernel.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_GOCLKERNEL_HPP
9 #define OPENCV_GAPI_GOCLKERNEL_HPP
10
11 #include <vector>
12 #include <functional>
13 #include <map>
14 #include <unordered_map>
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
21 // FIXME: namespace scheme for backends?
22 namespace cv {
23
24 namespace gimpl
25 {
26     // Forward-declare an internal class
27     class GOCLExecutable;
28 } // namespace gimpl
29
30 namespace gapi
31 {
32 namespace ocl
33 {
34     /**
35      * \addtogroup gapi_std_backends G-API Standard backends
36      * @{
37      */
38     /**
39      * @brief Get a reference to OCL backend.
40      *
41      * At the moment, the OCL backend is built atop of OpenCV
42      * "Transparent API" (T-API), see cv::UMat for details.
43      *
44      * @sa gapi_std_backends
45      */
46     GAPI_EXPORTS cv::gapi::GBackend backend();
47     /** @} */
48 } // namespace ocl
49 } // namespace gapi
50
51
52 // Represents arguments which are passed to a wrapped OCL function
53 // FIXME: put into detail?
54 class GAPI_EXPORTS GOCLContext
55 {
56 public:
57     // Generic accessor API
58     template<typename T>
59     const T& inArg(int input) { return m_args.at(input).get<T>(); }
60
61     // Syntax sugar
62     const cv::UMat&  inMat(int input);
63     cv::UMat&  outMatR(int output); // FIXME: Avoid cv::Mat m = ctx.outMatR()
64
65     const cv::gapi::own::Scalar& inVal(int input);
66     cv::gapi::own::Scalar& outValR(int output); // FIXME: Avoid cv::gapi::own::Scalar s = ctx.outValR()
67     template<typename T> std::vector<T>& outVecR(int output) // FIXME: the same issue
68     {
69         return outVecRef(output).wref<T>();
70     }
71
72 protected:
73     detail::VectorRef& outVecRef(int output);
74
75     std::vector<GArg> m_args;
76     std::unordered_map<std::size_t, GRunArgP> m_results;
77
78
79     friend class gimpl::GOCLExecutable;
80 };
81
82 class GAPI_EXPORTS GOCLKernel
83 {
84 public:
85     // This function is kernel's execution entry point (does the processing work)
86     using F = std::function<void(GOCLContext &)>;
87
88     GOCLKernel();
89     explicit GOCLKernel(const F& f);
90
91     void apply(GOCLContext &ctx);
92
93 protected:
94     F m_f;
95 };
96
97 // FIXME: This is an ugly ad-hoc imlpementation. TODO: refactor
98
99 namespace detail
100 {
101 template<class T> struct ocl_get_in;
102 template<> struct ocl_get_in<cv::GMat>
103 {
104     static cv::UMat    get(GOCLContext &ctx, int idx) { return ctx.inMat(idx); }
105 };
106 template<> struct ocl_get_in<cv::GScalar>
107 {
108     static cv::Scalar get(GOCLContext &ctx, int idx) { return to_ocv(ctx.inVal(idx)); }
109 };
110 template<typename U> struct ocl_get_in<cv::GArray<U> >
111 {
112     static const std::vector<U>& get(GOCLContext &ctx, int idx) { return ctx.inArg<VectorRef>(idx).rref<U>(); }
113 };
114 template<class T> struct ocl_get_in
115 {
116     static T get(GOCLContext &ctx, int idx) { return ctx.inArg<T>(idx); }
117 };
118
119 struct tracked_cv_umat{
120     //TODO Think if T - API could reallocate UMat to a proper size - how do we handle this ?
121     //tracked_cv_umat(cv::UMat& m) : r{(m)}, original_data{m.getMat(ACCESS_RW).data} {}
122     tracked_cv_umat(cv::UMat& m) : r{ (m) }, original_data{ nullptr } {}
123     cv::UMat r;
124     uchar* original_data;
125
126     operator cv::UMat& (){ return r;}
127     void validate() const{
128         //if (r.getMat(ACCESS_RW).data != original_data)
129         //{
130         //    util::throw_error
131         //        (std::logic_error
132         //         ("OpenCV kernel output parameter was reallocated. \n"
133         //          "Incorrect meta data was provided ?"));
134         //}
135
136     }
137 };
138
139 struct scalar_wrapper_ocl
140 {
141     //FIXME reuse CPU (OpenCV) plugin code
142     scalar_wrapper_ocl(cv::gapi::own::Scalar& s) : m_s{cv::gapi::own::to_ocv(s)}, m_org_s(s) {};
143     operator cv::Scalar& () { return m_s; }
144     void writeBack() const  { m_org_s = to_own(m_s); }
145
146     cv::Scalar m_s;
147     cv::gapi::own::Scalar& m_org_s;
148 };
149
150 template<typename... Outputs>
151 void postprocess_ocl(Outputs&... outs)
152 {
153     struct
154     {
155         void operator()(tracked_cv_umat* bm) { bm->validate(); }
156         void operator()(scalar_wrapper_ocl* sw) { sw->writeBack(); }
157         void operator()(...) {                  }
158
159     } validate;
160     //dummy array to unfold parameter pack
161     int dummy[] = { 0, (validate(&outs), 0)... };
162     cv::util::suppress_unused_warning(dummy);
163 }
164
165 template<class T> struct ocl_get_out;
166 template<> struct ocl_get_out<cv::GMat>
167 {
168     static tracked_cv_umat get(GOCLContext &ctx, int idx)
169     {
170         auto& r = ctx.outMatR(idx);
171         return{ r };
172     }
173 };
174 template<> struct ocl_get_out<cv::GScalar>
175 {
176     static scalar_wrapper_ocl get(GOCLContext &ctx, int idx)
177     {
178         auto& s = ctx.outValR(idx);
179         return{ s };
180     }
181 };
182 template<typename U> struct ocl_get_out<cv::GArray<U> >
183 {
184     static std::vector<U>& get(GOCLContext &ctx, int idx) { return ctx.outVecR<U>(idx);  }
185 };
186
187 template<typename, typename, typename>
188 struct OCLCallHelper;
189
190 // FIXME: probably can be simplified with std::apply or analogue.
191 template<typename Impl, typename... Ins, typename... Outs>
192 struct OCLCallHelper<Impl, std::tuple<Ins...>, std::tuple<Outs...> >
193 {
194     template<typename... Inputs>
195     struct call_and_postprocess
196     {
197         template<typename... Outputs>
198         static void call(Inputs&&... ins, Outputs&&... outs)
199         {
200             //not using a std::forward on outs is deliberate in order to
201             //cause compilation error, by tring to bind rvalue references to lvalue references
202             Impl::run(std::forward<Inputs>(ins)..., outs...);
203
204             postprocess_ocl(outs...);
205         }
206     };
207
208     template<int... IIs, int... OIs>
209     static void call_impl(GOCLContext &ctx, detail::Seq<IIs...>, detail::Seq<OIs...>)
210     {
211         //TODO: Make sure that OpenCV kernels do not reallocate memory for output parameters
212         //by comparing it's state (data ptr) before and after the call.
213         //Convert own::Scalar to cv::Scalar before call kernel and run kernel
214         //convert cv::Scalar to own::Scalar after call kernel and write back results
215         call_and_postprocess<decltype(ocl_get_in<Ins>::get(ctx, IIs))...>::call(ocl_get_in<Ins>::get(ctx, IIs)..., ocl_get_out<Outs>::get(ctx, OIs)...);
216     }
217
218     static void call(GOCLContext &ctx)
219     {
220         call_impl(ctx,
221             typename detail::MkSeq<sizeof...(Ins)>::type(),
222             typename detail::MkSeq<sizeof...(Outs)>::type());
223     }
224 };
225
226 } // namespace detail
227
228 template<class Impl, class K>
229 class GOCLKernelImpl: public detail::OCLCallHelper<Impl, typename K::InArgs, typename K::OutArgs>
230 {
231     using P = detail::OCLCallHelper<Impl, typename K::InArgs, typename K::OutArgs>;
232
233 public:
234     using API = K;
235
236     static cv::gapi::GBackend backend()  { return cv::gapi::ocl::backend(); }
237     static cv::GOCLKernel     kernel()   { return GOCLKernel(&P::call);     }
238 };
239
240 #define GAPI_OCL_KERNEL(Name, API) struct Name: public cv::GOCLKernelImpl<Name, API>
241
242 } // namespace cv
243
244 #endif // OPENCV_GAPI_GOCLKERNEL_HPP