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_GCPUKERNEL_HPP
9 #define OPENCV_GAPI_GCPUKERNEL_HPP
12 #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>
20 #include <opencv2/gapi/own/convert.hpp> //to_ocv
21 #include <opencv2/gapi/util/compiler_hints.hpp> //suppress_unused_warning
23 // FIXME: namespace scheme for backends?
28 // Forward-declare an internal class
37 * \addtogroup gapi_std_backends
40 * @brief G-API backends available in this OpenCV version
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.
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
52 * @sa @ref gapi_hld, cv::gapi::lookup_order()
56 * @brief Get a reference to CPU (OpenCV) backend.
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
63 * @sa gapi_std_backends
65 GAPI_EXPORTS cv::gapi::GBackend backend();
70 // Represents arguments which are passed to a wrapped CPU function
71 // FIXME: put into detail?
72 class GAPI_EXPORTS GCPUContext
75 // Generic accessor API
77 const T& inArg(int input) { return m_args.at(input).get<T>(); }
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()
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
87 return outVecRef(output).wref<T>();
91 detail::VectorRef& outVecRef(int output);
93 std::vector<GArg> m_args;
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;
100 friend class gimpl::GCPUExecutable;
103 class GAPI_EXPORTS GCPUKernel
106 // This function is kernel's execution entry point (does the processing work)
107 using F = std::function<void(GCPUContext &)>;
110 explicit GCPUKernel(const F& f);
112 void apply(GCPUContext &ctx);
118 // FIXME: This is an ugly ad-hoc imlpementation. TODO: refactor
122 template<class T> struct get_in;
123 template<> struct get_in<cv::GMat>
125 static cv::Mat get(GCPUContext &ctx, int idx) { return to_ocv(ctx.inMat(idx)); }
127 template<> struct get_in<cv::GScalar>
129 static cv::Scalar get(GCPUContext &ctx, int idx) { return to_ocv(ctx.inVal(idx)); }
131 template<typename U> struct get_in<cv::GArray<U> >
133 static const std::vector<U>& get(GCPUContext &ctx, int idx) { return ctx.inArg<VectorRef>(idx).rref<U>(); }
135 template<class T> struct get_in
137 static T get(GCPUContext &ctx, int idx) { return ctx.inArg<T>(idx); }
140 struct tracked_cv_mat{
141 tracked_cv_mat(cv::gapi::own::Mat& m) : r{to_ocv(m)}, original_data{m.data} {}
143 uchar* original_data;
145 operator cv::Mat& (){ return r;}
146 void validate() const{
147 if (r.data != original_data)
151 ("OpenCV kernel output parameter was reallocated. \n"
152 "Incorrect meta data was provided ?"));
157 struct scalar_wrapper
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); }
164 cv::gapi::own::Scalar& m_org_s;
167 template<typename... Outputs>
168 void postprocess(Outputs&... outs)
172 void operator()(tracked_cv_mat* bm) { bm->validate(); }
173 void operator()(scalar_wrapper* sw) { sw->writeBack(); }
174 void operator()(...) { }
177 //dummy array to unfold parameter pack
178 int dummy[] = { 0, (validate(&outs), 0)... };
179 cv::util::suppress_unused_warning(dummy);
182 template<class T> struct get_out;
183 template<> struct get_out<cv::GMat>
185 static tracked_cv_mat get(GCPUContext &ctx, int idx)
187 auto& r = ctx.outMatR(idx);
191 template<> struct get_out<cv::GScalar>
193 static scalar_wrapper get(GCPUContext &ctx, int idx)
195 auto& s = ctx.outValR(idx);
199 template<typename U> struct get_out<cv::GArray<U>>
201 static std::vector<U>& get(GCPUContext &ctx, int idx)
203 return ctx.outVecR<U>(idx);
207 template<typename, typename, typename>
208 struct OCVCallHelper;
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...> >
214 template<typename... Inputs>
215 struct call_and_postprocess
217 template<typename... Outputs>
218 static void call(Inputs&&... ins, Outputs&&... outs)
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...);
224 postprocess(outs...);
228 template<int... IIs, int... OIs>
229 static void call_impl(GCPUContext &ctx, detail::Seq<IIs...>, detail::Seq<OIs...>)
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)...);
240 static void call(GCPUContext &ctx)
243 typename detail::MkSeq<sizeof...(Ins)>::type(),
244 typename detail::MkSeq<sizeof...(Outs)>::type());
248 } // namespace detail
250 template<class Impl, class K>
251 class GCPUKernelImpl: public detail::OCVCallHelper<Impl, typename K::InArgs, typename K::OutArgs>
253 using P = detail::OCVCallHelper<Impl, typename K::InArgs, typename K::OutArgs>;
258 static cv::gapi::GBackend backend() { return cv::gapi::cpu::backend(); }
259 static cv::GCPUKernel kernel() { return GCPUKernel(&P::call); }
262 #define GAPI_OCV_KERNEL(Name, API) struct Name: public cv::GCPUKernelImpl<Name, API>
266 #endif // OPENCV_GAPI_GCPUKERNEL_HPP