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-2019 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::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
69 return outVecRef(output).wref<T>();
73 detail::VectorRef& outVecRef(int output);
75 std::vector<GArg> m_args;
76 std::unordered_map<std::size_t, GRunArgP> m_results;
79 friend class gimpl::GOCLExecutable;
82 class GAPI_EXPORTS GOCLKernel
85 // This function is kernel's execution entry point (does the processing work)
86 using F = std::function<void(GOCLContext &)>;
89 explicit GOCLKernel(const F& f);
91 void apply(GOCLContext &ctx);
97 // FIXME: This is an ugly ad-hoc imlpementation. TODO: refactor
101 template<class T> struct ocl_get_in;
102 template<> struct ocl_get_in<cv::GMat>
104 static cv::UMat get(GOCLContext &ctx, int idx) { return ctx.inMat(idx); }
106 template<> struct ocl_get_in<cv::GScalar>
108 static cv::Scalar get(GOCLContext &ctx, int idx) { return to_ocv(ctx.inVal(idx)); }
110 template<typename U> struct ocl_get_in<cv::GArray<U> >
112 static const std::vector<U>& get(GOCLContext &ctx, int idx) { return ctx.inArg<VectorRef>(idx).rref<U>(); }
114 template<class T> struct ocl_get_in
116 static T get(GOCLContext &ctx, int idx) { return ctx.inArg<T>(idx); }
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 } {}
124 uchar* original_data;
126 operator cv::UMat& (){ return r;}
127 void validate() const{
128 //if (r.getMat(ACCESS_RW).data != original_data)
132 // ("OpenCV kernel output parameter was reallocated. \n"
133 // "Incorrect meta data was provided ?"));
139 struct scalar_wrapper_ocl
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); }
147 cv::gapi::own::Scalar& m_org_s;
150 template<typename... Outputs>
151 void postprocess_ocl(Outputs&... outs)
155 void operator()(tracked_cv_umat* bm) { bm->validate(); }
156 void operator()(scalar_wrapper_ocl* sw) { sw->writeBack(); }
157 void operator()(...) { }
160 //dummy array to unfold parameter pack
161 int dummy[] = { 0, (validate(&outs), 0)... };
162 cv::util::suppress_unused_warning(dummy);
165 template<class T> struct ocl_get_out;
166 template<> struct ocl_get_out<cv::GMat>
168 static tracked_cv_umat get(GOCLContext &ctx, int idx)
170 auto& r = ctx.outMatR(idx);
174 template<> struct ocl_get_out<cv::GScalar>
176 static scalar_wrapper_ocl get(GOCLContext &ctx, int idx)
178 auto& s = ctx.outValR(idx);
182 template<typename U> struct ocl_get_out<cv::GArray<U> >
184 static std::vector<U>& get(GOCLContext &ctx, int idx) { return ctx.outVecR<U>(idx); }
187 template<typename, typename, typename>
188 struct OCLCallHelper;
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...> >
194 template<typename... Inputs>
195 struct call_and_postprocess
197 template<typename... Outputs>
198 static void call(Inputs&&... ins, Outputs&&... outs)
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...);
204 postprocess_ocl(outs...);
208 template<int... IIs, int... OIs>
209 static void call_impl(GOCLContext &ctx, detail::Seq<IIs...>, detail::Seq<OIs...>)
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)...);
218 static void call(GOCLContext &ctx)
221 typename detail::MkSeq<sizeof...(Ins)>::type(),
222 typename detail::MkSeq<sizeof...(Outs)>::type());
226 } // namespace detail
228 template<class Impl, class K>
229 class GOCLKernelImpl: public detail::OCLCallHelper<Impl, typename K::InArgs, typename K::OutArgs>
231 using P = detail::OCLCallHelper<Impl, typename K::InArgs, typename K::OutArgs>;
236 static cv::gapi::GBackend backend() { return cv::gapi::ocl::backend(); }
237 static cv::GOCLKernel kernel() { return GOCLKernel(&P::call); }
240 #define GAPI_OCL_KERNEL(Name, API) struct Name: public cv::GOCLKernelImpl<Name, API>
244 #endif // OPENCV_GAPI_GOCLKERNEL_HPP