gfluidkernel.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. #ifndef OPENCV_GAPI_FLUID_KERNEL_HPP
  7. #define OPENCV_GAPI_FLUID_KERNEL_HPP
  8. #include <vector>
  9. #include <functional>
  10. #include <map>
  11. #include <unordered_map>
  12. #include <opencv2/gapi/opencv_includes.hpp>
  13. #include <opencv2/gapi/gcommon.hpp>
  14. #include <opencv2/gapi/gkernel.hpp>
  15. #include <opencv2/gapi/garg.hpp>
  16. #include <opencv2/gapi/fluid/gfluidbuffer.hpp>
  17. // FIXME: namespace scheme for backends?
  18. namespace cv {
  19. namespace gapi
  20. {
  21. namespace fluid
  22. {
  23. /**
  24. * \addtogroup gapi_std_backends G-API Standard Backends
  25. * @{
  26. */
  27. /**
  28. * @brief Get a reference to Fluid backend.
  29. *
  30. * @sa gapi_std_backends
  31. */
  32. GAPI_EXPORTS cv::gapi::GBackend backend();
  33. /** @} */
  34. } // namespace fluid
  35. } // namespace gapi
  36. class GAPI_EXPORTS GFluidKernel
  37. {
  38. public:
  39. enum class Kind
  40. {
  41. Filter,
  42. Resize,
  43. YUV420toRGB //Color conversion of 4:2:0 chroma sub-sampling formats (NV12, I420 ..etc) to RGB
  44. };
  45. // This function is a generic "doWork" callback
  46. using F = std::function<void(const cv::GArgs&, const std::vector<gapi::fluid::Buffer*> &)>;
  47. // This function is a generic "initScratch" callback
  48. using IS = std::function<void(const cv::GMetaArgs &, const cv::GArgs&, gapi::fluid::Buffer &)>;
  49. // This function is a generic "resetScratch" callback
  50. using RS = std::function<void(gapi::fluid::Buffer &)>;
  51. // This function describes kernel metadata inference rule.
  52. using M = std::function<GMetaArgs(const GMetaArgs &, const GArgs &)>;
  53. // This function is a generic "getBorder" callback (extracts border-related data from kernel's input parameters)
  54. using B = std::function<gapi::fluid::BorderOpt(const GMetaArgs&, const GArgs&)>;
  55. // This function is a generic "getWindow" callback (extracts window-related data from kernel's input parameters)
  56. using GW = std::function<int(const GMetaArgs&, const GArgs&)>;
  57. // FIXME: move implementations out of header file
  58. GFluidKernel() {}
  59. GFluidKernel(Kind k, int l, bool scratch, const F& f, const IS &is, const RS &rs, const B& b, const GW& win)
  60. : m_kind(k)
  61. , m_lpi(l)
  62. , m_scratch(scratch)
  63. , m_f(f)
  64. , m_is(is)
  65. , m_rs(rs)
  66. , m_b(b)
  67. , m_gw(win) {}
  68. Kind m_kind;
  69. const int m_lpi = -1;
  70. const bool m_scratch = false;
  71. const F m_f;
  72. const IS m_is;
  73. const RS m_rs;
  74. const B m_b;
  75. const GW m_gw;
  76. };
  77. // FIXME!!!
  78. // This is the temporary and experimental API
  79. // which should be replaced by runtime roi-based scheduling
  80. /** \addtogroup gapi_compile_args
  81. * @{
  82. */
  83. /**
  84. * @brief This structure allows to control the output image region
  85. * which Fluid backend will produce in the graph.
  86. *
  87. * This feature is useful for external tiling and parallelism, but
  88. * will be deprecated in the future releases.
  89. */
  90. struct GFluidOutputRois
  91. {
  92. std::vector<cv::Rect> rois;
  93. };
  94. /**
  95. * @brief This structure forces Fluid backend to generate multiple
  96. * parallel output regions in the graph. These regions execute in parallel.
  97. *
  98. * This feature may be deprecated in the future releases.
  99. */
  100. struct GFluidParallelOutputRois
  101. {
  102. std::vector<GFluidOutputRois> parallel_rois;
  103. };
  104. /**
  105. * @brief This structure allows to customize the way how Fluid executes
  106. * parallel regions.
  107. *
  108. * For example, user can utilize his own threading runtime via this parameter.
  109. * The `parallel_for` member functor is called by the Fluid runtime with the
  110. * following arguments:
  111. *
  112. * @param size Size of the parallel range to process
  113. * @param f A function which should be called for every integer index
  114. * in this range by the specified parallel_for implementation.
  115. *
  116. * This feature may be deprecated in the future releases.
  117. */
  118. struct GFluidParallelFor
  119. {
  120. //this function accepts:
  121. // - size of the "parallel" range as the first argument
  122. // - and a function to be called on the range items, designated by item index
  123. std::function<void(std::size_t size, std::function<void(std::size_t index)>)> parallel_for;
  124. };
  125. /** @} gapi_compile_args */
  126. namespace detail
  127. {
  128. template<> struct CompileArgTag<GFluidOutputRois>
  129. {
  130. static const char* tag() { return "gapi.fluid.outputRois"; }
  131. };
  132. template<> struct CompileArgTag<GFluidParallelFor>
  133. {
  134. static const char* tag() { return "gapi.fluid.parallelFor"; }
  135. };
  136. template<> struct CompileArgTag<GFluidParallelOutputRois>
  137. {
  138. static const char* tag() { return "gapi.fluid.parallelOutputRois"; }
  139. };
  140. } // namespace detail
  141. namespace detail
  142. {
  143. template<class T> struct fluid_get_in;
  144. template<> struct fluid_get_in<cv::GMat>
  145. {
  146. static const cv::gapi::fluid::View& get(const cv::GArgs &in_args, int idx)
  147. {
  148. return *in_args[idx].unsafe_get<cv::gapi::fluid::View*>();
  149. }
  150. };
  151. template<> struct fluid_get_in<cv::GScalar>
  152. {
  153. // FIXME: change to return by reference when moved to own::Scalar
  154. static const cv::Scalar get(const cv::GArgs &in_args, int idx)
  155. {
  156. return in_args[idx].unsafe_get<cv::Scalar>();
  157. }
  158. };
  159. template<typename U> struct fluid_get_in<cv::GArray<U>>
  160. {
  161. static const std::vector<U>& get(const cv::GArgs &in_args, int idx)
  162. {
  163. return in_args.at(idx).unsafe_get<cv::detail::VectorRef>().rref<U>();
  164. }
  165. };
  166. template<typename U> struct fluid_get_in<cv::GOpaque<U>>
  167. {
  168. static const U& get(const cv::GArgs &in_args, int idx)
  169. {
  170. return in_args.at(idx).unsafe_get<cv::detail::OpaqueRef>().rref<U>();
  171. }
  172. };
  173. template<class T> struct fluid_get_in
  174. {
  175. static const T& get(const cv::GArgs &in_args, int idx)
  176. {
  177. return in_args[idx].unsafe_get<T>();
  178. }
  179. };
  180. template<bool, typename Impl, typename... Ins>
  181. struct scratch_helper;
  182. template<typename Impl, typename... Ins>
  183. struct scratch_helper<true, Impl, Ins...>
  184. {
  185. // Init
  186. template<int... IIs>
  187. static void help_init_impl(const cv::GMetaArgs &metas,
  188. const cv::GArgs &in_args,
  189. gapi::fluid::Buffer &scratch_buf,
  190. detail::Seq<IIs...>)
  191. {
  192. Impl::initScratch(get_in_meta<Ins>(metas, in_args, IIs)..., scratch_buf);
  193. }
  194. static void help_init(const cv::GMetaArgs &metas,
  195. const cv::GArgs &in_args,
  196. gapi::fluid::Buffer &b)
  197. {
  198. help_init_impl(metas, in_args, b, typename detail::MkSeq<sizeof...(Ins)>::type());
  199. }
  200. // Reset
  201. static void help_reset(gapi::fluid::Buffer &b)
  202. {
  203. Impl::resetScratch(b);
  204. }
  205. };
  206. template<typename Impl, typename... Ins>
  207. struct scratch_helper<false, Impl, Ins...>
  208. {
  209. static void help_init(const cv::GMetaArgs &,
  210. const cv::GArgs &,
  211. gapi::fluid::Buffer &)
  212. {
  213. GAPI_Assert(false);
  214. }
  215. static void help_reset(gapi::fluid::Buffer &)
  216. {
  217. GAPI_Assert(false);
  218. }
  219. };
  220. template<typename T> struct is_gmat_type
  221. {
  222. static const constexpr bool value = std::is_same<cv::GMat, T>::value;
  223. };
  224. template<bool CallCustomGetBorder, typename Impl, typename... Ins>
  225. struct get_border_helper;
  226. template<typename Impl, typename... Ins>
  227. struct get_border_helper<true, Impl, Ins...>
  228. {
  229. template<int... IIs>
  230. static gapi::fluid::BorderOpt get_border_impl(const GMetaArgs &metas,
  231. const cv::GArgs &in_args,
  232. cv::detail::Seq<IIs...>)
  233. {
  234. return util::make_optional(Impl::getBorder(cv::detail::get_in_meta<Ins>(metas, in_args, IIs)...));
  235. }
  236. static gapi::fluid::BorderOpt help(const GMetaArgs &metas,
  237. const cv::GArgs &in_args)
  238. {
  239. return get_border_impl(metas, in_args, typename detail::MkSeq<sizeof...(Ins)>::type());
  240. }
  241. };
  242. template<typename Impl, typename... Ins>
  243. struct get_border_helper<false, Impl, Ins...>
  244. {
  245. static gapi::fluid::BorderOpt help(const cv::GMetaArgs &,
  246. const cv::GArgs &)
  247. {
  248. return {};
  249. }
  250. };
  251. template<bool CallCustomGetWindow, typename, typename... Ins>
  252. struct get_window_helper;
  253. template<typename Impl, typename... Ins>
  254. struct get_window_helper<true, Impl, Ins...>
  255. {
  256. template<int... IIs>
  257. static int get_window_impl(const GMetaArgs &metas,
  258. const cv::GArgs &in_args,
  259. cv::detail::Seq<IIs...>)
  260. {
  261. return Impl::getWindow(cv::detail::get_in_meta<Ins>(metas, in_args, IIs)...);
  262. }
  263. static int help(const GMetaArgs &metas, const cv::GArgs &in_args)
  264. {
  265. return get_window_impl(metas, in_args, typename detail::MkSeq<sizeof...(Ins)>::type());
  266. }
  267. };
  268. template<typename Impl, typename... Ins>
  269. struct get_window_helper<false, Impl, Ins...>
  270. {
  271. static int help(const cv::GMetaArgs &,
  272. const cv::GArgs &)
  273. {
  274. return Impl::Window;
  275. }
  276. };
  277. template<typename C, typename T>
  278. struct has_Window
  279. {
  280. private:
  281. template<class U>
  282. static constexpr auto Check(U*) -> typename std::is_same<decltype(U::Window), T>::type;
  283. template<typename>
  284. static constexpr std::false_type Check(...);
  285. typedef decltype(Check<C>(0)) Result;
  286. public:
  287. static constexpr bool value = Result::value;
  288. };
  289. template<bool hasWindow, typename Impl>
  290. struct callCustomGetBorder;
  291. template<typename Impl>
  292. struct callCustomGetBorder<true, Impl>
  293. {
  294. static constexpr bool value = (Impl::Window != 1);
  295. };
  296. template<typename Impl>
  297. struct callCustomGetBorder<false, Impl>
  298. {
  299. static constexpr bool value = true;
  300. };
  301. template<typename, typename, typename, bool UseScratch>
  302. struct FluidCallHelper;
  303. template<typename Impl, typename... Ins, typename... Outs, bool UseScratch>
  304. struct FluidCallHelper<Impl, std::tuple<Ins...>, std::tuple<Outs...>, UseScratch>
  305. {
  306. static_assert(all_satisfy<is_gmat_type, Outs...>::value, "return type must be GMat");
  307. static_assert(contains<GMat, Ins...>::value, "input must contain at least one GMat");
  308. // Execution dispatcher ////////////////////////////////////////////////////
  309. template<int... IIs, int... OIs>
  310. static void call_impl(const cv::GArgs &in_args,
  311. const std::vector<gapi::fluid::Buffer*> &out_bufs,
  312. detail::Seq<IIs...>,
  313. detail::Seq<OIs...>)
  314. {
  315. Impl::run(fluid_get_in<Ins>::get(in_args, IIs)..., *out_bufs[OIs]...);
  316. }
  317. static void call(const cv::GArgs &in_args,
  318. const std::vector<gapi::fluid::Buffer*> &out_bufs)
  319. {
  320. constexpr int numOuts = (sizeof...(Outs)) + (UseScratch ? 1 : 0);
  321. call_impl(in_args, out_bufs,
  322. typename detail::MkSeq<sizeof...(Ins)>::type(),
  323. typename detail::MkSeq<numOuts>::type());
  324. }
  325. // Scratch buffer initialization dispatcher ////////////////////////////////
  326. static void init_scratch(const GMetaArgs &metas,
  327. const cv::GArgs &in_args,
  328. gapi::fluid::Buffer &b)
  329. {
  330. scratch_helper<UseScratch, Impl, Ins...>::help_init(metas, in_args, b);
  331. }
  332. // Scratch buffer reset dispatcher /////////////////////////////////////////
  333. static void reset_scratch(gapi::fluid::Buffer &scratch_buf)
  334. {
  335. scratch_helper<UseScratch, Impl, Ins...>::help_reset(scratch_buf);
  336. }
  337. static gapi::fluid::BorderOpt getBorder(const GMetaArgs &metas, const cv::GArgs &in_args)
  338. {
  339. constexpr bool hasWindow = has_Window<Impl, const int>::value;
  340. // User must provide "init" callback if Window != 1
  341. // TODO: move to constexpr if when we enable C++17
  342. return get_border_helper<callCustomGetBorder<hasWindow, Impl>::value, Impl, Ins...>::help(metas, in_args);
  343. }
  344. static int getWindow(const GMetaArgs &metas, const cv::GArgs &in_args)
  345. {
  346. constexpr bool callCustomGetWindow = !(has_Window<Impl, const int>::value);
  347. return get_window_helper<callCustomGetWindow, Impl, Ins...>::help(metas, in_args);
  348. }
  349. };
  350. } // namespace detail
  351. template<class Impl, class K, bool UseScratch>
  352. class GFluidKernelImpl : public cv::detail::KernelTag
  353. {
  354. static const int LPI = 1;
  355. static const auto Kind = GFluidKernel::Kind::Filter;
  356. using P = detail::FluidCallHelper<Impl, typename K::InArgs, typename K::OutArgs, UseScratch>;
  357. public:
  358. using API = K;
  359. static GFluidKernel kernel()
  360. {
  361. // FIXME: call() and getOutMeta() needs to be renamed so it is clear these
  362. // functions are internal wrappers, not user API
  363. return GFluidKernel(Impl::Kind, Impl::LPI,
  364. UseScratch,
  365. &P::call, &P::init_scratch, &P::reset_scratch, &P::getBorder, &P::getWindow);
  366. }
  367. static cv::gapi::GBackend backend() { return cv::gapi::fluid::backend(); }
  368. };
  369. #define GAPI_FLUID_KERNEL(Name, API, Scratch) struct Name: public cv::GFluidKernelImpl<Name, API, Scratch>
  370. } // namespace cv
  371. #endif // OPENCV_GAPI_GCPUKERNEL_HPP