Merge pull request #18451 from OrestChura:oc/count_non_zero
[platform/upstream/opencv.git] / 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-2020 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::Scalar& inVal(int input);
66     cv::Scalar& outValR(int output); // FIXME: Avoid cv::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     template<typename T> T& outOpaqueR(int output) // FIXME: the same issue
72     {
73         return outOpaqueRef(output).wref<T>();
74     }
75
76 protected:
77     detail::VectorRef& outVecRef(int output);
78     detail::OpaqueRef& outOpaqueRef(int output);
79
80     std::vector<GArg> m_args;
81     std::unordered_map<std::size_t, GRunArgP> m_results;
82
83
84     friend class gimpl::GOCLExecutable;
85 };
86
87 class GAPI_EXPORTS GOCLKernel
88 {
89 public:
90     // This function is kernel's execution entry point (does the processing work)
91     using F = std::function<void(GOCLContext &)>;
92
93     GOCLKernel();
94     explicit GOCLKernel(const F& f);
95
96     void apply(GOCLContext &ctx);
97
98 protected:
99     F m_f;
100 };
101
102 // FIXME: This is an ugly ad-hoc implementation. TODO: refactor
103
104 namespace detail
105 {
106 template<class T> struct ocl_get_in;
107 template<> struct ocl_get_in<cv::GMat>
108 {
109     static cv::UMat    get(GOCLContext &ctx, int idx) { return ctx.inMat(idx); }
110 };
111 template<> struct ocl_get_in<cv::GScalar>
112 {
113     static cv::Scalar get(GOCLContext &ctx, int idx) { return ctx.inVal(idx); }
114 };
115 template<typename U> struct ocl_get_in<cv::GArray<U> >
116 {
117     static const std::vector<U>& get(GOCLContext &ctx, int idx) { return ctx.inArg<VectorRef>(idx).rref<U>(); }
118 };
119 template<typename U> struct ocl_get_in<cv::GOpaque<U> >
120 {
121     static const U& get(GOCLContext &ctx, int idx) { return ctx.inArg<OpaqueRef>(idx).rref<U>(); }
122 };
123 template<class T> struct ocl_get_in
124 {
125     static T get(GOCLContext &ctx, int idx) { return ctx.inArg<T>(idx); }
126 };
127
128 struct tracked_cv_umat{
129     //TODO Think if T - API could reallocate UMat to a proper size - how do we handle this ?
130     //tracked_cv_umat(cv::UMat& m) : r{(m)}, original_data{m.getMat(ACCESS_RW).data} {}
131     tracked_cv_umat(cv::UMat& m) : r(m), original_data{ nullptr } {}
132     cv::UMat &r; // FIXME: It was a value (not a reference) before.
133                  // Actually OCL backend should allocate its internal data!
134     uchar* original_data;
135
136     operator cv::UMat& (){ return r;}
137     void validate() const{
138         //if (r.getMat(ACCESS_RW).data != original_data)
139         //{
140         //    util::throw_error
141         //        (std::logic_error
142         //         ("OpenCV kernel output parameter was reallocated. \n"
143         //          "Incorrect meta data was provided ?"));
144         //}
145
146     }
147 };
148
149 template<typename... Outputs>
150 void postprocess_ocl(Outputs&... outs)
151 {
152     struct
153     {
154         void operator()(tracked_cv_umat* bm) { bm->validate(); }
155         void operator()(...) {                  }
156
157     } validate;
158     //dummy array to unfold parameter pack
159     int dummy[] = { 0, (validate(&outs), 0)... };
160     cv::util::suppress_unused_warning(dummy);
161 }
162
163 template<class T> struct ocl_get_out;
164 template<> struct ocl_get_out<cv::GMat>
165 {
166     static tracked_cv_umat get(GOCLContext &ctx, int idx)
167     {
168         auto& r = ctx.outMatR(idx);
169         return{ r };
170     }
171 };
172 template<> struct ocl_get_out<cv::GScalar>
173 {
174     static cv::Scalar& get(GOCLContext &ctx, int idx)
175     {
176         return ctx.outValR(idx);
177     }
178 };
179 template<typename U> struct ocl_get_out<cv::GArray<U> >
180 {
181     static std::vector<U>& get(GOCLContext &ctx, int idx) { return ctx.outVecR<U>(idx);  }
182 };
183 template<typename U> struct ocl_get_out<cv::GOpaque<U> >
184 {
185     static U& get(GOCLContext &ctx, int idx) { return ctx.outOpaqueR<U>(idx);  }
186 };
187
188 template<typename, typename, typename>
189 struct OCLCallHelper;
190
191 // FIXME: probably can be simplified with std::apply or analogue.
192 template<typename Impl, typename... Ins, typename... Outs>
193 struct OCLCallHelper<Impl, std::tuple<Ins...>, std::tuple<Outs...> >
194 {
195     template<typename... Inputs>
196     struct call_and_postprocess
197     {
198         template<typename... Outputs>
199         static void call(Inputs&&... ins, Outputs&&... outs)
200         {
201             //not using a std::forward on outs is deliberate in order to
202             //cause compilation error, by trying to bind rvalue references to lvalue references
203             Impl::run(std::forward<Inputs>(ins)..., outs...);
204
205             postprocess_ocl(outs...);
206         }
207     };
208
209     template<int... IIs, int... OIs>
210     static void call_impl(GOCLContext &ctx, detail::Seq<IIs...>, detail::Seq<OIs...>)
211     {
212         //TODO: Make sure that OpenCV kernels do not reallocate memory for output parameters
213         //by comparing it's state (data ptr) before and after the call.
214         //Convert own::Scalar to cv::Scalar before call kernel and run kernel
215         //convert cv::Scalar to own::Scalar after call kernel and write back results
216         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)...);
217     }
218
219     static void call(GOCLContext &ctx)
220     {
221         call_impl(ctx,
222             typename detail::MkSeq<sizeof...(Ins)>::type(),
223             typename detail::MkSeq<sizeof...(Outs)>::type());
224     }
225 };
226
227 } // namespace detail
228
229 template<class Impl, class K>
230 class GOCLKernelImpl: public cv::detail::OCLCallHelper<Impl, typename K::InArgs, typename K::OutArgs>,
231                       public cv::detail::KernelTag
232 {
233     using P = detail::OCLCallHelper<Impl, typename K::InArgs, typename K::OutArgs>;
234
235 public:
236     using API = K;
237
238     static cv::gapi::GBackend backend()  { return cv::gapi::ocl::backend(); }
239     static cv::GOCLKernel     kernel()   { return GOCLKernel(&P::call);     }
240 };
241
242 #define GAPI_OCL_KERNEL(Name, API) struct Name: public cv::GOCLKernelImpl<Name, API>
243
244 } // namespace cv
245
246 #endif // OPENCV_GAPI_GOCLKERNEL_HPP