goclkernel.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. #ifndef OPENCV_GAPI_GOCLKERNEL_HPP
  7. #define OPENCV_GAPI_GOCLKERNEL_HPP
  8. #include <vector>
  9. #include <functional>
  10. #include <map>
  11. #include <unordered_map>
  12. #include <opencv2/core/mat.hpp>
  13. #include <opencv2/gapi/gcommon.hpp>
  14. #include <opencv2/gapi/gkernel.hpp>
  15. #include <opencv2/gapi/garg.hpp>
  16. // FIXME: namespace scheme for backends?
  17. namespace cv {
  18. namespace gimpl
  19. {
  20. // Forward-declare an internal class
  21. class GOCLExecutable;
  22. } // namespace gimpl
  23. namespace gapi
  24. {
  25. namespace ocl
  26. {
  27. /**
  28. * \addtogroup gapi_std_backends G-API Standard Backends
  29. * @{
  30. */
  31. /**
  32. * @brief Get a reference to OCL backend.
  33. *
  34. * At the moment, the OCL backend is built atop of OpenCV
  35. * "Transparent API" (T-API), see cv::UMat for details.
  36. *
  37. * @sa gapi_std_backends
  38. */
  39. GAPI_EXPORTS cv::gapi::GBackend backend();
  40. /** @} */
  41. } // namespace ocl
  42. } // namespace gapi
  43. // Represents arguments which are passed to a wrapped OCL function
  44. // FIXME: put into detail?
  45. class GAPI_EXPORTS GOCLContext
  46. {
  47. public:
  48. // Generic accessor API
  49. template<typename T>
  50. const T& inArg(int input) { return m_args.at(input).get<T>(); }
  51. // Syntax sugar
  52. const cv::UMat& inMat(int input);
  53. cv::UMat& outMatR(int output); // FIXME: Avoid cv::Mat m = ctx.outMatR()
  54. const cv::Scalar& inVal(int input);
  55. cv::Scalar& outValR(int output); // FIXME: Avoid cv::Scalar s = ctx.outValR()
  56. template<typename T> std::vector<T>& outVecR(int output) // FIXME: the same issue
  57. {
  58. return outVecRef(output).wref<T>();
  59. }
  60. template<typename T> T& outOpaqueR(int output) // FIXME: the same issue
  61. {
  62. return outOpaqueRef(output).wref<T>();
  63. }
  64. protected:
  65. detail::VectorRef& outVecRef(int output);
  66. detail::OpaqueRef& outOpaqueRef(int output);
  67. std::vector<GArg> m_args;
  68. std::unordered_map<std::size_t, GRunArgP> m_results;
  69. friend class gimpl::GOCLExecutable;
  70. };
  71. class GAPI_EXPORTS GOCLKernel
  72. {
  73. public:
  74. // This function is kernel's execution entry point (does the processing work)
  75. using F = std::function<void(GOCLContext &)>;
  76. GOCLKernel();
  77. explicit GOCLKernel(const F& f);
  78. void apply(GOCLContext &ctx);
  79. protected:
  80. F m_f;
  81. };
  82. // FIXME: This is an ugly ad-hoc implementation. TODO: refactor
  83. namespace detail
  84. {
  85. template<class T> struct ocl_get_in;
  86. template<> struct ocl_get_in<cv::GMat>
  87. {
  88. static cv::UMat get(GOCLContext &ctx, int idx) { return ctx.inMat(idx); }
  89. };
  90. template<> struct ocl_get_in<cv::GScalar>
  91. {
  92. static cv::Scalar get(GOCLContext &ctx, int idx) { return ctx.inVal(idx); }
  93. };
  94. template<typename U> struct ocl_get_in<cv::GArray<U> >
  95. {
  96. static const std::vector<U>& get(GOCLContext &ctx, int idx) { return ctx.inArg<VectorRef>(idx).rref<U>(); }
  97. };
  98. template<typename U> struct ocl_get_in<cv::GOpaque<U> >
  99. {
  100. static const U& get(GOCLContext &ctx, int idx) { return ctx.inArg<OpaqueRef>(idx).rref<U>(); }
  101. };
  102. template<class T> struct ocl_get_in
  103. {
  104. static T get(GOCLContext &ctx, int idx) { return ctx.inArg<T>(idx); }
  105. };
  106. struct tracked_cv_umat{
  107. //TODO Think if T - API could reallocate UMat to a proper size - how do we handle this ?
  108. //tracked_cv_umat(cv::UMat& m) : r{(m)}, original_data{m.getMat(ACCESS_RW).data} {}
  109. tracked_cv_umat(cv::UMat& m) : r(m), original_data{ nullptr } {}
  110. cv::UMat &r; // FIXME: It was a value (not a reference) before.
  111. // Actually OCL backend should allocate its internal data!
  112. uchar* original_data;
  113. operator cv::UMat& (){ return r;}
  114. void validate() const{
  115. //if (r.getMat(ACCESS_RW).data != original_data)
  116. //{
  117. // util::throw_error
  118. // (std::logic_error
  119. // ("OpenCV kernel output parameter was reallocated. \n"
  120. // "Incorrect meta data was provided ?"));
  121. //}
  122. }
  123. };
  124. template<typename... Outputs>
  125. void postprocess_ocl(Outputs&... outs)
  126. {
  127. struct
  128. {
  129. void operator()(tracked_cv_umat* bm) { bm->validate(); }
  130. void operator()(...) { }
  131. } validate;
  132. //dummy array to unfold parameter pack
  133. int dummy[] = { 0, (validate(&outs), 0)... };
  134. cv::util::suppress_unused_warning(dummy);
  135. }
  136. template<class T> struct ocl_get_out;
  137. template<> struct ocl_get_out<cv::GMat>
  138. {
  139. static tracked_cv_umat get(GOCLContext &ctx, int idx)
  140. {
  141. auto& r = ctx.outMatR(idx);
  142. return{ r };
  143. }
  144. };
  145. template<> struct ocl_get_out<cv::GScalar>
  146. {
  147. static cv::Scalar& get(GOCLContext &ctx, int idx)
  148. {
  149. return ctx.outValR(idx);
  150. }
  151. };
  152. template<typename U> struct ocl_get_out<cv::GArray<U> >
  153. {
  154. static std::vector<U>& get(GOCLContext &ctx, int idx) { return ctx.outVecR<U>(idx); }
  155. };
  156. template<typename U> struct ocl_get_out<cv::GOpaque<U> >
  157. {
  158. static U& get(GOCLContext &ctx, int idx) { return ctx.outOpaqueR<U>(idx); }
  159. };
  160. template<typename, typename, typename>
  161. struct OCLCallHelper;
  162. // FIXME: probably can be simplified with std::apply or analogue.
  163. template<typename Impl, typename... Ins, typename... Outs>
  164. struct OCLCallHelper<Impl, std::tuple<Ins...>, std::tuple<Outs...> >
  165. {
  166. template<typename... Inputs>
  167. struct call_and_postprocess
  168. {
  169. template<typename... Outputs>
  170. static void call(Inputs&&... ins, Outputs&&... outs)
  171. {
  172. //not using a std::forward on outs is deliberate in order to
  173. //cause compilation error, by trying to bind rvalue references to lvalue references
  174. Impl::run(std::forward<Inputs>(ins)..., outs...);
  175. postprocess_ocl(outs...);
  176. }
  177. };
  178. template<int... IIs, int... OIs>
  179. static void call_impl(GOCLContext &ctx, detail::Seq<IIs...>, detail::Seq<OIs...>)
  180. {
  181. //TODO: Make sure that OpenCV kernels do not reallocate memory for output parameters
  182. //by comparing it's state (data ptr) before and after the call.
  183. //Convert own::Scalar to cv::Scalar before call kernel and run kernel
  184. //convert cv::Scalar to own::Scalar after call kernel and write back results
  185. 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)...);
  186. }
  187. static void call(GOCLContext &ctx)
  188. {
  189. call_impl(ctx,
  190. typename detail::MkSeq<sizeof...(Ins)>::type(),
  191. typename detail::MkSeq<sizeof...(Outs)>::type());
  192. }
  193. };
  194. } // namespace detail
  195. template<class Impl, class K>
  196. class GOCLKernelImpl: public cv::detail::OCLCallHelper<Impl, typename K::InArgs, typename K::OutArgs>,
  197. public cv::detail::KernelTag
  198. {
  199. using P = detail::OCLCallHelper<Impl, typename K::InArgs, typename K::OutArgs>;
  200. public:
  201. using API = K;
  202. static cv::gapi::GBackend backend() { return cv::gapi::ocl::backend(); }
  203. static cv::GOCLKernel kernel() { return GOCLKernel(&P::call); }
  204. };
  205. #define GAPI_OCL_KERNEL(Name, API) struct Name: public cv::GOCLKernelImpl<Name, API>
  206. } // namespace cv
  207. #endif // OPENCV_GAPI_GOCLKERNEL_HPP