Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / fluid / modules / gapi / include / opencv2 / gapi / gcompoundkernel.hpp
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
7
8 #ifndef OPENCV_GAPI_GCOMPOUNDKERNEL_HPP
9 #define OPENCV_GAPI_GCOMPOUNDKERNEL_HPP
10
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>
15
16 namespace cv {
17 namespace gapi
18 {
19 namespace compound
20 {
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
25 } // namespace gapi
26
27 namespace detail
28 {
29
30 struct GCompoundContext
31 {
32     explicit GCompoundContext(const GArgs& in_args);
33     template<typename T>
34     const T& inArg(int input) { return m_args.at(input).get<T>(); }
35
36     GArgs m_args;
37     GArgs m_results;
38 };
39
40 class GAPI_EXPORTS GCompoundKernel
41 {
42 // Compound kernel must use all of it's inputs
43 public:
44     using F = std::function<void(GCompoundContext& ctx)>;
45
46     explicit GCompoundKernel(const F& f);
47     void apply(GCompoundContext& ctx);
48
49 protected:
50     F m_f;
51 };
52
53 template<typename T> struct get_compound_in
54 {
55     static T get(GCompoundContext &ctx, int idx) { return ctx.inArg<T>(idx); }
56 };
57
58 template<typename U> struct get_compound_in<cv::GArray<U>>
59 {
60     static cv::GArray<U> get(GCompoundContext &ctx, int idx)
61     {
62         auto array = cv::GArray<U>();
63         ctx.m_args[idx] = GArg(array);
64         return array;
65     }
66 };
67
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)
70 template<typename>
71 struct tuple_wrap_helper;
72
73 template<typename T> struct tuple_wrap_helper
74 {
75     static std::tuple<T> get(T&& obj) { return std::make_tuple(std::move(obj)); }
76 };
77
78 template<typename... Objs>
79 struct tuple_wrap_helper<std::tuple<Objs...>>
80 {
81     static std::tuple<Objs...> get(std::tuple<Objs...>&& objs) { return objs; }
82 };
83
84 template<typename, typename, typename>
85 struct GCompoundCallHelper;
86
87 template<typename Impl, typename... Ins, typename... Outs>
88 struct GCompoundCallHelper<Impl, std::tuple<Ins...>, std::tuple<Outs...> >
89 {
90     template<int... IIs, int... OIs>
91     static void expand_impl(GCompoundContext &ctx, detail::Seq<IIs...>, detail::Seq<OIs...>)
92     {
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))... };
96     }
97
98     static void expand(GCompoundContext &ctx)
99     {
100         expand_impl(ctx,
101                     typename detail::MkSeq<sizeof...(Ins)>::type(),
102                     typename detail::MkSeq<sizeof...(Outs)>::type());
103     }
104 };
105
106 template<class Impl, class K>
107 class GCompoundKernelImpl: public cv::detail::GCompoundCallHelper<Impl, typename K::InArgs, typename K::OutArgs>
108 {
109     using P = cv::detail::GCompoundCallHelper<Impl, typename K::InArgs, typename K::OutArgs>;
110
111 public:
112     using API = K;
113
114     static cv::gapi::GBackend backend() { return cv::gapi::compound::backend(); }
115     static GCompoundKernel    kernel()  { return GCompoundKernel(&P::expand);   }
116 };
117
118 } // namespace detail
119 #define GAPI_COMPOUND_KERNEL(Name, API) struct Name: public cv::detail::GCompoundKernelImpl<Name, API>
120
121 } // namespace cv
122
123 #endif // OPENCV_GAPI_GCOMPOUNDKERNEL_HPP