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.
5 // Copyright (C) 2018-2019 Intel Corporation
8 #ifndef OPENCV_GAPI_GCOMPOUNDKERNEL_HPP
9 #define OPENCV_GAPI_GCOMPOUNDKERNEL_HPP
11 #include <opencv2/gapi/opencv_includes.hpp>
12 #include <opencv2/gapi/gcommon.hpp>
13 #include <opencv2/gapi/gkernel.hpp>
14 #include <opencv2/gapi/garg.hpp>
21 // FIXME User does not need to know about this function
22 // Needs that user may define compound kernels(as cpu kernels)
23 GAPI_EXPORTS cv::gapi::GBackend backend();
24 } // namespace compound
30 struct GCompoundContext
32 explicit GCompoundContext(const GArgs& in_args);
34 const T& inArg(int input) { return m_args.at(input).get<T>(); }
40 class GAPI_EXPORTS GCompoundKernel
42 // Compound kernel must use all of it's inputs
44 using F = std::function<void(GCompoundContext& ctx)>;
46 explicit GCompoundKernel(const F& f);
47 void apply(GCompoundContext& ctx);
53 template<typename T> struct get_compound_in
55 static T get(GCompoundContext &ctx, int idx) { return ctx.inArg<T>(idx); }
58 template<typename U> struct get_compound_in<cv::GArray<U>>
60 static cv::GArray<U> get(GCompoundContext &ctx, int idx)
62 auto array = cv::GArray<U>();
63 ctx.m_args[idx] = GArg(array);
68 // Kernel may return one object(GMat, GScalar) or a tuple of objects.
69 // This helper is needed to cast return value to the same form(tuple)
71 struct tuple_wrap_helper;
73 template<typename T> struct tuple_wrap_helper
75 static std::tuple<T> get(T&& obj) { return std::make_tuple(std::move(obj)); }
78 template<typename... Objs>
79 struct tuple_wrap_helper<std::tuple<Objs...>>
81 static std::tuple<Objs...> get(std::tuple<Objs...>&& objs) { return objs; }
84 template<typename, typename, typename>
85 struct GCompoundCallHelper;
87 template<typename Impl, typename... Ins, typename... Outs>
88 struct GCompoundCallHelper<Impl, std::tuple<Ins...>, std::tuple<Outs...> >
90 template<int... IIs, int... OIs>
91 static void expand_impl(GCompoundContext &ctx, detail::Seq<IIs...>, detail::Seq<OIs...>)
93 auto result = Impl::expand(get_compound_in<Ins>::get(ctx, IIs)...);
94 auto tuple_return = tuple_wrap_helper<decltype(result)>::get(std::move(result));
95 ctx.m_results = { cv::GArg(std::get<OIs>(tuple_return))... };
98 static void expand(GCompoundContext &ctx)
101 typename detail::MkSeq<sizeof...(Ins)>::type(),
102 typename detail::MkSeq<sizeof...(Outs)>::type());
106 template<class Impl, class K>
107 class GCompoundKernelImpl: public cv::detail::GCompoundCallHelper<Impl, typename K::InArgs, typename K::OutArgs>
109 using P = cv::detail::GCompoundCallHelper<Impl, typename K::InArgs, typename K::OutArgs>;
114 static cv::gapi::GBackend backend() { return cv::gapi::compound::backend(); }
115 static GCompoundKernel kernel() { return GCompoundKernel(&P::expand); }
118 } // namespace detail
119 #define GAPI_COMPOUND_KERNEL(Name, API) struct Name: public cv::detail::GCompoundKernelImpl<Name, API>
123 #endif // OPENCV_GAPI_GCOMPOUNDKERNEL_HPP