gcompoundkernel.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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_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. // Kernel may return one object(GMat, GScalar) or a tuple of objects.
  56. // This helper is needed to cast return value to the same form(tuple)
  57. template<typename>
  58. struct tuple_wrap_helper;
  59. template<typename T> struct tuple_wrap_helper
  60. {
  61. static std::tuple<T> get(T&& obj) { return std::make_tuple(std::move(obj)); }
  62. };
  63. template<typename... Objs>
  64. struct tuple_wrap_helper<std::tuple<Objs...>>
  65. {
  66. static std::tuple<Objs...> get(std::tuple<Objs...>&& objs) { return std::forward<std::tuple<Objs...>>(objs); }
  67. };
  68. template<typename, typename, typename>
  69. struct GCompoundCallHelper;
  70. template<typename Impl, typename... Ins, typename... Outs>
  71. struct GCompoundCallHelper<Impl, std::tuple<Ins...>, std::tuple<Outs...> >
  72. {
  73. template<int... IIs, int... OIs>
  74. static void expand_impl(GCompoundContext &ctx, detail::Seq<IIs...>, detail::Seq<OIs...>)
  75. {
  76. auto result = Impl::expand(get_compound_in<Ins>::get(ctx, IIs)...);
  77. auto tuple_return = tuple_wrap_helper<decltype(result)>::get(std::move(result));
  78. ctx.m_results = { cv::GArg(std::get<OIs>(tuple_return))... };
  79. }
  80. static void expand(GCompoundContext &ctx)
  81. {
  82. expand_impl(ctx,
  83. typename detail::MkSeq<sizeof...(Ins)>::type(),
  84. typename detail::MkSeq<sizeof...(Outs)>::type());
  85. }
  86. };
  87. template<class Impl, class K>
  88. class GCompoundKernelImpl: public cv::detail::GCompoundCallHelper<Impl, typename K::InArgs, typename K::OutArgs>
  89. {
  90. using P = cv::detail::GCompoundCallHelper<Impl, typename K::InArgs, typename K::OutArgs>;
  91. public:
  92. using API = K;
  93. static cv::gapi::GBackend backend() { return cv::gapi::compound::backend(); }
  94. static GCompoundKernel kernel() { return GCompoundKernel(&P::expand); }
  95. };
  96. } // namespace detail
  97. #define GAPI_COMPOUND_KERNEL(Name, API) struct Name: public cv::detail::GCompoundKernelImpl<Name, API>
  98. } // namespace cv
  99. #endif // OPENCV_GAPI_GCOMPOUNDKERNEL_HPP