gcompoundkernel.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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_GCOMPOUNDKERNEL_HPP
  7. #define OPENCV_GAPI_GCOMPOUNDKERNEL_HPP
  8. #include <opencv2/gapi/opencv_includes.hpp>
  9. #include <opencv2/gapi/gcommon.hpp>
  10. #include <opencv2/gapi/gkernel.hpp>
  11. #include <opencv2/gapi/garg.hpp>
  12. namespace cv {
  13. namespace gapi
  14. {
  15. namespace compound
  16. {
  17. // FIXME User does not need to know about this function
  18. // Needs that user may define compound kernels(as cpu kernels)
  19. GAPI_EXPORTS cv::gapi::GBackend backend();
  20. } // namespace compound
  21. } // namespace gapi
  22. namespace detail
  23. {
  24. struct GCompoundContext
  25. {
  26. explicit GCompoundContext(const GArgs& in_args);
  27. template<typename T>
  28. const T& inArg(int input) { return m_args.at(input).get<T>(); }
  29. GArgs m_args;
  30. GArgs m_results;
  31. };
  32. class GAPI_EXPORTS GCompoundKernel
  33. {
  34. // Compound kernel must use all of it's inputs
  35. public:
  36. using F = std::function<void(GCompoundContext& ctx)>;
  37. explicit GCompoundKernel(const F& f);
  38. void apply(GCompoundContext& ctx);
  39. protected:
  40. F m_f;
  41. };
  42. template<typename T> struct get_compound_in
  43. {
  44. static T get(GCompoundContext &ctx, int idx) { return ctx.inArg<T>(idx); }
  45. };
  46. template<typename U> struct get_compound_in<cv::GArray<U>>
  47. {
  48. static cv::GArray<U> get(GCompoundContext &ctx, int idx)
  49. {
  50. auto array = cv::GArray<U>();
  51. ctx.m_args[idx] = GArg(array);
  52. return array;
  53. }
  54. };
  55. template<typename U> struct get_compound_in<cv::GOpaque<U>>
  56. {
  57. static cv::GOpaque<U> get(GCompoundContext &ctx, int idx)
  58. {
  59. auto opaq = cv::GOpaque<U>();
  60. ctx.m_args[idx] = GArg(opaq);
  61. return opaq;
  62. }
  63. };
  64. template<> struct get_compound_in<cv::GMatP>
  65. {
  66. static cv::GMatP get(GCompoundContext &ctx, int idx)
  67. {
  68. auto mat = cv::GMatP();
  69. ctx.m_args[idx] = GArg(mat);
  70. return mat;
  71. }
  72. };
  73. template<typename, typename, typename>
  74. struct GCompoundCallHelper;
  75. template<typename Impl, typename... Ins, typename... Outs>
  76. struct GCompoundCallHelper<Impl, std::tuple<Ins...>, std::tuple<Outs...> >
  77. {
  78. template<int... IIs, int... OIs>
  79. static void expand_impl(GCompoundContext &ctx, detail::Seq<IIs...>, detail::Seq<OIs...>)
  80. {
  81. auto result = Impl::expand(get_compound_in<Ins>::get(ctx, IIs)...);
  82. auto tuple_return = tuple_wrap_helper<decltype(result)>::get(std::move(result));
  83. ctx.m_results = { cv::GArg(std::get<OIs>(tuple_return))... };
  84. }
  85. static void expand(GCompoundContext &ctx)
  86. {
  87. expand_impl(ctx,
  88. typename detail::MkSeq<sizeof...(Ins)>::type(),
  89. typename detail::MkSeq<sizeof...(Outs)>::type());
  90. }
  91. };
  92. template<class Impl, class K>
  93. class GCompoundKernelImpl: public cv::detail::GCompoundCallHelper<Impl, typename K::InArgs, typename K::OutArgs>,
  94. public cv::detail::KernelTag
  95. {
  96. using P = cv::detail::GCompoundCallHelper<Impl, typename K::InArgs, typename K::OutArgs>;
  97. public:
  98. using API = K;
  99. static cv::gapi::GBackend backend() { return cv::gapi::compound::backend(); }
  100. static GCompoundKernel kernel() { return GCompoundKernel(&P::expand); }
  101. };
  102. } // namespace detail
  103. /**
  104. * Declares a new compound kernel. See this
  105. * [documentation chapter](@ref gapi_kernel_compound)
  106. * on compound kernels for more details.
  107. *
  108. * @param Name type name for new kernel
  109. * @param API the interface this kernel implements
  110. */
  111. #define GAPI_COMPOUND_KERNEL(Name, API) \
  112. struct Name: public cv::detail::GCompoundKernelImpl<Name, API>
  113. } // namespace cv
  114. #endif // OPENCV_GAPI_GCOMPOUNDKERNEL_HPP