goclkernel.hpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 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::gapi::own::Scalar& inVal(int input);
  55. cv::gapi::own::Scalar& outValR(int output); // FIXME: Avoid cv::gapi::own::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. protected:
  61. detail::VectorRef& outVecRef(int output);
  62. std::vector<GArg> m_args;
  63. std::unordered_map<std::size_t, GRunArgP> m_results;
  64. friend class gimpl::GOCLExecutable;
  65. };
  66. class GAPI_EXPORTS GOCLKernel
  67. {
  68. public:
  69. // This function is kernel's execution entry point (does the processing work)
  70. using F = std::function<void(GOCLContext &)>;
  71. GOCLKernel();
  72. explicit GOCLKernel(const F& f);
  73. void apply(GOCLContext &ctx);
  74. protected:
  75. F m_f;
  76. };
  77. // FIXME: This is an ugly ad-hoc imlpementation. TODO: refactor
  78. namespace detail
  79. {
  80. template<class T> struct ocl_get_in;
  81. template<> struct ocl_get_in<cv::GMat>
  82. {
  83. static cv::UMat get(GOCLContext &ctx, int idx) { return ctx.inMat(idx); }
  84. };
  85. template<> struct ocl_get_in<cv::GScalar>
  86. {
  87. static cv::Scalar get(GOCLContext &ctx, int idx) { return to_ocv(ctx.inVal(idx)); }
  88. };
  89. template<typename U> struct ocl_get_in<cv::GArray<U> >
  90. {
  91. static const std::vector<U>& get(GOCLContext &ctx, int idx) { return ctx.inArg<VectorRef>(idx).rref<U>(); }
  92. };
  93. template<class T> struct ocl_get_in
  94. {
  95. static T get(GOCLContext &ctx, int idx) { return ctx.inArg<T>(idx); }
  96. };
  97. struct tracked_cv_umat{
  98. //TODO Think if T - API could reallocate UMat to a proper size - how do we handle this ?
  99. //tracked_cv_umat(cv::UMat& m) : r{(m)}, original_data{m.getMat(ACCESS_RW).data} {}
  100. tracked_cv_umat(cv::UMat& m) : r{ (m) }, original_data{ nullptr } {}
  101. cv::UMat r;
  102. uchar* original_data;
  103. operator cv::UMat& (){ return r;}
  104. void validate() const{
  105. //if (r.getMat(ACCESS_RW).data != original_data)
  106. //{
  107. // util::throw_error
  108. // (std::logic_error
  109. // ("OpenCV kernel output parameter was reallocated. \n"
  110. // "Incorrect meta data was provided ?"));
  111. //}
  112. }
  113. };
  114. struct scalar_wrapper_ocl
  115. {
  116. //FIXME reuse CPU (OpenCV) plugin code
  117. scalar_wrapper_ocl(cv::gapi::own::Scalar& s) : m_s{cv::gapi::own::to_ocv(s)}, m_org_s(s) {};
  118. operator cv::Scalar& () { return m_s; }
  119. void writeBack() const { m_org_s = to_own(m_s); }
  120. cv::Scalar m_s;
  121. cv::gapi::own::Scalar& m_org_s;
  122. };
  123. template<typename... Outputs>
  124. void postprocess_ocl(Outputs&... outs)
  125. {
  126. struct
  127. {
  128. void operator()(tracked_cv_umat* bm) { bm->validate(); }
  129. void operator()(scalar_wrapper_ocl* sw) { sw->writeBack(); }
  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 scalar_wrapper_ocl get(GOCLContext &ctx, int idx)
  148. {
  149. auto& s = ctx.outValR(idx);
  150. return{ s };
  151. }
  152. };
  153. template<typename U> struct ocl_get_out<cv::GArray<U> >
  154. {
  155. static std::vector<U>& get(GOCLContext &ctx, int idx) { return ctx.outVecR<U>(idx); }
  156. };
  157. template<typename, typename, typename>
  158. struct OCLCallHelper;
  159. // FIXME: probably can be simplified with std::apply or analogue.
  160. template<typename Impl, typename... Ins, typename... Outs>
  161. struct OCLCallHelper<Impl, std::tuple<Ins...>, std::tuple<Outs...> >
  162. {
  163. template<typename... Inputs>
  164. struct call_and_postprocess
  165. {
  166. template<typename... Outputs>
  167. static void call(Inputs&&... ins, Outputs&&... outs)
  168. {
  169. //not using a std::forward on outs is deliberate in order to
  170. //cause compilation error, by tring to bind rvalue references to lvalue references
  171. Impl::run(std::forward<Inputs>(ins)..., outs...);
  172. postprocess_ocl(outs...);
  173. }
  174. };
  175. template<int... IIs, int... OIs>
  176. static void call_impl(GOCLContext &ctx, detail::Seq<IIs...>, detail::Seq<OIs...>)
  177. {
  178. //TODO: Make sure that OpenCV kernels do not reallocate memory for output parameters
  179. //by comparing it's state (data ptr) before and after the call.
  180. //Convert own::Scalar to cv::Scalar before call kernel and run kernel
  181. //convert cv::Scalar to own::Scalar after call kernel and write back results
  182. 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)...);
  183. }
  184. static void call(GOCLContext &ctx)
  185. {
  186. call_impl(ctx,
  187. typename detail::MkSeq<sizeof...(Ins)>::type(),
  188. typename detail::MkSeq<sizeof...(Outs)>::type());
  189. }
  190. };
  191. } // namespace detail
  192. template<class Impl, class K>
  193. class GOCLKernelImpl: public detail::OCLCallHelper<Impl, typename K::InArgs, typename K::OutArgs>
  194. {
  195. using P = detail::OCLCallHelper<Impl, typename K::InArgs, typename K::OutArgs>;
  196. public:
  197. using API = K;
  198. static cv::gapi::GBackend backend() { return cv::gapi::ocl::backend(); }
  199. static cv::GOCLKernel kernel() { return GOCLKernel(&P::call); }
  200. };
  201. #define GAPI_OCL_KERNEL(Name, API) struct Name: public cv::GOCLKernelImpl<Name, API>
  202. } // namespace cv
  203. #endif // OPENCV_GAPI_GOCLKERNEL_HPP