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.
5 // Copyright (C) 2018-2020 Intel Corporation
8 #ifndef OPENCV_GAPI_GOCLKERNEL_HPP
9 #define OPENCV_GAPI_GOCLKERNEL_HPP
14 #include <unordered_map>
16 #include <opencv2/core/mat.hpp>
17 #include <opencv2/gapi/gcommon.hpp>
18 #include <opencv2/gapi/gkernel.hpp>
19 #include <opencv2/gapi/garg.hpp>
21 // FIXME: namespace scheme for backends?
26 // Forward-declare an internal class
35 * \addtogroup gapi_std_backends G-API Standard Backends
39 * @brief Get a reference to OCL backend.
41 * At the moment, the OCL backend is built atop of OpenCV
42 * "Transparent API" (T-API), see cv::UMat for details.
44 * @sa gapi_std_backends
46 GAPI_EXPORTS cv::gapi::GBackend backend();
52 // Represents arguments which are passed to a wrapped OCL function
53 // FIXME: put into detail?
54 class GAPI_EXPORTS GOCLContext
57 // Generic accessor API
59 const T& inArg(int input) { return m_args.at(input).get<T>(); }
62 const cv::UMat& inMat(int input);
63 cv::UMat& outMatR(int output); // FIXME: Avoid cv::Mat m = ctx.outMatR()
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
69 return outVecRef(output).wref<T>();
71 template<typename T> T& outOpaqueR(int output) // FIXME: the same issue
73 return outOpaqueRef(output).wref<T>();
77 detail::VectorRef& outVecRef(int output);
78 detail::OpaqueRef& outOpaqueRef(int output);
80 std::vector<GArg> m_args;
81 std::unordered_map<std::size_t, GRunArgP> m_results;
84 friend class gimpl::GOCLExecutable;
87 class GAPI_EXPORTS GOCLKernel
90 // This function is kernel's execution entry point (does the processing work)
91 using F = std::function<void(GOCLContext &)>;
94 explicit GOCLKernel(const F& f);
96 void apply(GOCLContext &ctx);
102 // FIXME: This is an ugly ad-hoc implementation. TODO: refactor
106 template<class T> struct ocl_get_in;
107 template<> struct ocl_get_in<cv::GMat>
109 static cv::UMat get(GOCLContext &ctx, int idx) { return ctx.inMat(idx); }
111 template<> struct ocl_get_in<cv::GScalar>
113 static cv::Scalar get(GOCLContext &ctx, int idx) { return ctx.inVal(idx); }
115 template<typename U> struct ocl_get_in<cv::GArray<U> >
117 static const std::vector<U>& get(GOCLContext &ctx, int idx) { return ctx.inArg<VectorRef>(idx).rref<U>(); }
119 template<typename U> struct ocl_get_in<cv::GOpaque<U> >
121 static const U& get(GOCLContext &ctx, int idx) { return ctx.inArg<OpaqueRef>(idx).rref<U>(); }
123 template<class T> struct ocl_get_in
125 static T get(GOCLContext &ctx, int idx) { return ctx.inArg<T>(idx); }
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;
136 operator cv::UMat& (){ return r;}
137 void validate() const{
138 //if (r.getMat(ACCESS_RW).data != original_data)
142 // ("OpenCV kernel output parameter was reallocated. \n"
143 // "Incorrect meta data was provided ?"));
149 template<typename... Outputs>
150 void postprocess_ocl(Outputs&... outs)
154 void operator()(tracked_cv_umat* bm) { bm->validate(); }
155 void operator()(...) { }
158 //dummy array to unfold parameter pack
159 int dummy[] = { 0, (validate(&outs), 0)... };
160 cv::util::suppress_unused_warning(dummy);
163 template<class T> struct ocl_get_out;
164 template<> struct ocl_get_out<cv::GMat>
166 static tracked_cv_umat get(GOCLContext &ctx, int idx)
168 auto& r = ctx.outMatR(idx);
172 template<> struct ocl_get_out<cv::GScalar>
174 static cv::Scalar& get(GOCLContext &ctx, int idx)
176 return ctx.outValR(idx);
179 template<typename U> struct ocl_get_out<cv::GArray<U> >
181 static std::vector<U>& get(GOCLContext &ctx, int idx) { return ctx.outVecR<U>(idx); }
183 template<typename U> struct ocl_get_out<cv::GOpaque<U> >
185 static U& get(GOCLContext &ctx, int idx) { return ctx.outOpaqueR<U>(idx); }
188 template<typename, typename, typename>
189 struct OCLCallHelper;
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...> >
195 template<typename... Inputs>
196 struct call_and_postprocess
198 template<typename... Outputs>
199 static void call(Inputs&&... ins, Outputs&&... outs)
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...);
205 postprocess_ocl(outs...);
209 template<int... IIs, int... OIs>
210 static void call_impl(GOCLContext &ctx, detail::Seq<IIs...>, detail::Seq<OIs...>)
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)...);
219 static void call(GOCLContext &ctx)
222 typename detail::MkSeq<sizeof...(Ins)>::type(),
223 typename detail::MkSeq<sizeof...(Outs)>::type());
227 } // namespace detail
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
233 using P = detail::OCLCallHelper<Impl, typename K::InArgs, typename K::OutArgs>;
238 static cv::gapi::GBackend backend() { return cv::gapi::ocl::backend(); }
239 static cv::GOCLKernel kernel() { return GOCLKernel(&P::call); }
242 #define GAPI_OCL_KERNEL(Name, API) struct Name: public cv::GOCLKernelImpl<Name, API>
246 #endif // OPENCV_GAPI_GOCLKERNEL_HPP