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-2020 Intel Corporation
8 #ifndef OPENCV_GAPI_GBACKEND_HPP
9 #define OPENCV_GAPI_GBACKEND_HPP
14 #include <ade/node.hpp>
16 #include "opencv2/gapi/garg.hpp"
18 #include "opencv2/gapi/util/optional.hpp"
20 #include "compiler/gmodel.hpp"
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());
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));
33 class RMatAdapter : public RMat::Adapter {
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); }
42 // Forward declarations
47 template<typename... Ts> struct Class
49 template<typename T> using MapT = std::unordered_map<int, T>;
50 template<typename T> MapT<T>& slot()
52 return std::get<ade::util::type_list_index<T, Ts...>::value>(slots);
54 template<typename T> const MapT<T>& slot() const
56 return std::get<ade::util::type_list_index<T, Ts...>::value>(slots);
59 std::tuple<MapT<Ts>...> slots;
62 } // namespace magazine
64 using Mag = magazine::Class< cv::Mat
66 , cv::detail::VectorRef
67 , cv::detail::OpaqueRef
71 #if !defined(GAPI_STANDALONE)
78 enum class HandleRMat { BIND, SKIP };
79 // Extracts a memory object from GRunArg, stores it in appropriate slot in a magazine
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.
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);
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);
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);
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
108 template<typename... Ts> struct magazine
110 template<typename T> using MapT = std::unordered_map<int, T>;
111 template<typename T> MapT<T>& slot()
113 return std::get<util::type_list_index<T, Ts...>::value>(slots);
115 template<typename T> const MapT<T>& slot() const
117 return std::get<util::type_list_index<T, Ts...>::value>(slots);
120 std::tuple<MapT<Ts>...> slots;
122 } // namespace detail
131 inline cv::util::optional<T> getCompileArg(const cv::GCompileArgs &args)
133 return cv::gapi::getCompileArg<T>(args);
136 void createMat(const cv::GMatDesc& desc, cv::Mat& mat);
140 #endif // OPENCV_GAPI_GBACKEND_HPP