G-API: Integrated cv::MediaFrame as I/O type + CPU backend
[platform/upstream/opencv.git] / modules / gapi / src / backends / common / gbackend.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-2020 Intel Corporation
6
7
8 #ifndef OPENCV_GAPI_GBACKEND_HPP
9 #define OPENCV_GAPI_GBACKEND_HPP
10
11 #include <string>
12 #include <memory>
13
14 #include <ade/node.hpp>
15
16 #include "opencv2/gapi/garg.hpp"
17
18 #include "opencv2/gapi/util/optional.hpp"
19
20 #include "compiler/gmodel.hpp"
21
22 namespace cv {
23 namespace gimpl {
24
25     inline cv::Mat asMat(RMat::View& v) {
26         return v.dims().empty() ? cv::Mat(v.rows(), v.cols(), v.type(), v.ptr(), v.step())
27                                 : cv::Mat(v.dims(), v.type(), v.ptr());
28     }
29     inline RMat::View asView(const Mat& m, RMat::View::DestroyCallback&& cb = nullptr) {
30         return RMat::View(cv::descr_of(m), m.data, m.step, std::move(cb));
31     }
32
33     class RMatAdapter : public RMat::Adapter {
34         cv::Mat m_mat;
35     public:
36         const void* data() const { return m_mat.data; }
37         RMatAdapter(cv::Mat m) : m_mat(m) {}
38         virtual RMat::View access(RMat::Access) override { return asView(m_mat); }
39         virtual cv::GMatDesc desc() const override { return cv::descr_of(m_mat); }
40     };
41
42     // Forward declarations
43     struct Data;
44     struct RcDesc;
45
46 namespace magazine {
47     template<typename... Ts> struct Class
48     {
49         template<typename T> using MapT = std::unordered_map<int, T>;
50         template<typename T>       MapT<T>& slot()
51         {
52             return std::get<ade::util::type_list_index<T, Ts...>::value>(slots);
53         }
54         template<typename T> const MapT<T>& slot() const
55         {
56             return std::get<ade::util::type_list_index<T, Ts...>::value>(slots);
57         }
58     private:
59         std::tuple<MapT<Ts>...> slots;
60     };
61
62 } // namespace magazine
63
64 using Mag = magazine::Class< cv::Mat
65                            , cv::Scalar
66                            , cv::detail::VectorRef
67                            , cv::detail::OpaqueRef
68                            , cv::RMat
69                            , cv::RMat::View
70                            , cv::MediaFrame
71 #if !defined(GAPI_STANDALONE)
72                            , cv::UMat
73 #endif
74                            >;
75
76 namespace magazine
77 {
78     enum class HandleRMat { BIND, SKIP };
79     // Extracts a memory object from GRunArg, stores it in appropriate slot in a magazine
80     // Note:
81     // Only RMats are expected here as a memory object for GMat shape.
82     // If handleRMat is BIND, RMat will be accessed, and RMat::View and wrapping cv::Mat
83     // will be placed into the magazine.
84     // If handleRMat is SKIP, this function skips'RMat handling assuming that backend will do it on its own.
85     // FIXME?
86     // handleRMat parameter might be redundant if all device specific backends implement own bind routines
87     // without utilizing magazine at all
88     void GAPI_EXPORTS bindInArg (Mag& mag, const RcDesc &rc, const GRunArg  &arg, HandleRMat handleRMat = HandleRMat::BIND);
89
90     // Extracts a memory object reference fro GRunArgP, stores it in appropriate slot in a magazine
91     // Note on RMat handling from bindInArg above is also applied here
92     void GAPI_EXPORTS bindOutArg(Mag& mag, const RcDesc &rc, const GRunArgP &arg, HandleRMat handleRMat = HandleRMat::BIND);
93
94     void         resetInternalData(Mag& mag, const Data &d);
95     cv::GRunArg  getArg    (const Mag& mag, const RcDesc &ref);
96     cv::GRunArgP getObjPtr (      Mag& mag, const RcDesc &rc, bool is_umat = false);
97     void         writeBack (const Mag& mag, const RcDesc &rc, GRunArgP &g_arg);
98
99     // A mandatory clean-up procedure to force proper lifetime of wrappers (cv::Mat, cv::RMat::View)
100     // over not-owned data
101     // FIXME? Add an RAII wrapper for that?
102     // Or put objects which need to be cleaned-up into a separate stack allocated magazine?
103     void         unbind(Mag &mag, const RcDesc &rc);
104 } // namespace magazine
105
106 namespace detail
107 {
108 template<typename... Ts> struct magazine
109 {
110     template<typename T> using MapT = std::unordered_map<int, T>;
111     template<typename T>       MapT<T>& slot()
112     {
113         return std::get<util::type_list_index<T, Ts...>::value>(slots);
114     }
115     template<typename T> const MapT<T>& slot() const
116     {
117         return std::get<util::type_list_index<T, Ts...>::value>(slots);
118     }
119 private:
120     std::tuple<MapT<Ts>...> slots;
121 };
122 } // namespace detail
123
124 struct GRuntimeArgs
125 {
126     GRunArgs   inObjs;
127     GRunArgsP outObjs;
128 };
129
130 template<typename T>
131 inline cv::util::optional<T> getCompileArg(const cv::GCompileArgs &args)
132 {
133     return cv::gapi::getCompileArg<T>(args);
134 }
135
136 void createMat(const cv::GMatDesc& desc, cv::Mat& mat);
137
138 }} // cv::gimpl
139
140 #endif // OPENCV_GAPI_GBACKEND_HPP