gopaque.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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) 2019-2020 Intel Corporation
  6. #ifndef OPENCV_GAPI_GOPAQUE_HPP
  7. #define OPENCV_GAPI_GOPAQUE_HPP
  8. #include <functional>
  9. #include <ostream>
  10. #include <memory>
  11. #include <opencv2/gapi/own/exports.hpp>
  12. #include <opencv2/gapi/opencv_includes.hpp>
  13. #include <opencv2/gapi/util/any.hpp>
  14. #include <opencv2/gapi/util/variant.hpp>
  15. #include <opencv2/gapi/util/throw.hpp>
  16. #include <opencv2/gapi/util/type_traits.hpp>
  17. #include <opencv2/gapi/own/assert.hpp>
  18. namespace cv
  19. {
  20. // Forward declaration; GNode and GOrigin are an internal
  21. // (user-inaccessible) classes.
  22. class GNode;
  23. struct GOrigin;
  24. template<typename T> class GOpaque;
  25. /**
  26. * \addtogroup gapi_meta_args
  27. * @{
  28. */
  29. struct GOpaqueDesc
  30. {
  31. // FIXME: Body
  32. // FIXME: Also implement proper operator== then
  33. bool operator== (const GOpaqueDesc&) const { return true; }
  34. };
  35. template<typename U> GOpaqueDesc descr_of(const U &) { return {};}
  36. static inline GOpaqueDesc empty_gopaque_desc() {return {}; }
  37. /** @} */
  38. std::ostream& operator<<(std::ostream& os, const cv::GOpaqueDesc &desc);
  39. namespace detail
  40. {
  41. // ConstructOpaque is a callback which stores information about T and is used by
  42. // G-API runtime to construct an object in host memory (T remains opaque for G-API).
  43. // ConstructOpaque is carried into G-API internals by GOpaqueU.
  44. // Currently it is suitable for Host (CPU) plugins only, real offload may require
  45. // more information for manual memory allocation on-device.
  46. class OpaqueRef;
  47. using ConstructOpaque = std::function<void(OpaqueRef&)>;
  48. // FIXME: garray.hpp already contains hint classes (for actual T type verification),
  49. // need to think where it can be moved (currently opaque uses it from garray)
  50. // This class strips type information from GOpaque<T> and makes it usable
  51. // in the G-API graph compiler (expression unrolling, graph generation, etc).
  52. // Part of GProtoArg.
  53. class GAPI_EXPORTS GOpaqueU
  54. {
  55. public:
  56. GOpaqueU(const GNode &n, std::size_t out); // Operation result constructor
  57. template <typename T>
  58. bool holds() const; // Check if was created from GOpaque<T>
  59. GOrigin& priv(); // Internal use only
  60. const GOrigin& priv() const; // Internal use only
  61. protected:
  62. GOpaqueU(); // Default constructor
  63. template<class> friend class cv::GOpaque; // (available for GOpaque<T> only)
  64. void setConstructFcn(ConstructOpaque &&cv); // Store T-aware constructor
  65. template <typename T>
  66. void specifyType(); // Store type of initial GOpaque<T>
  67. template <typename T>
  68. void storeKind();
  69. void setKind(cv::detail::OpaqueKind);
  70. std::shared_ptr<GOrigin> m_priv;
  71. std::shared_ptr<TypeHintBase> m_hint;
  72. };
  73. template <typename T>
  74. bool GOpaqueU::holds() const{
  75. GAPI_Assert(m_hint != nullptr);
  76. using U = util::decay_t<T>;
  77. return dynamic_cast<TypeHint<U>*>(m_hint.get()) != nullptr;
  78. };
  79. template <typename T>
  80. void GOpaqueU::specifyType(){
  81. m_hint.reset(new TypeHint<util::decay_t<T>>);
  82. };
  83. template <typename T>
  84. void GOpaqueU::storeKind(){
  85. // FIXME: Add assert here on cv::Mat and cv::Scalar?
  86. setKind(cv::detail::GOpaqueTraits<T>::kind);
  87. };
  88. // This class represents a typed object reference.
  89. // Depending on origins, this reference may be either "just a" reference to
  90. // an object created externally, OR actually own the underlying object
  91. // (be value holder).
  92. class BasicOpaqueRef
  93. {
  94. public:
  95. cv::GOpaqueDesc m_desc;
  96. virtual ~BasicOpaqueRef() {}
  97. virtual void mov(BasicOpaqueRef &ref) = 0;
  98. virtual const void* ptr() const = 0;
  99. virtual void set(const cv::util::any &a) = 0;
  100. };
  101. template<typename T> class OpaqueRefT final: public BasicOpaqueRef
  102. {
  103. using empty_t = util::monostate;
  104. using ro_ext_t = const T *;
  105. using rw_ext_t = T *;
  106. using rw_own_t = T ;
  107. util::variant<empty_t, ro_ext_t, rw_ext_t, rw_own_t> m_ref;
  108. inline bool isEmpty() const { return util::holds_alternative<empty_t>(m_ref); }
  109. inline bool isROExt() const { return util::holds_alternative<ro_ext_t>(m_ref); }
  110. inline bool isRWExt() const { return util::holds_alternative<rw_ext_t>(m_ref); }
  111. inline bool isRWOwn() const { return util::holds_alternative<rw_own_t>(m_ref); }
  112. void init(const T* obj = nullptr)
  113. {
  114. if (obj) m_desc = cv::descr_of(*obj);
  115. }
  116. public:
  117. OpaqueRefT() { init(); }
  118. virtual ~OpaqueRefT() {}
  119. explicit OpaqueRefT(const T& obj) : m_ref(&obj) { init(&obj); }
  120. explicit OpaqueRefT( T& obj) : m_ref(&obj) { init(&obj); }
  121. explicit OpaqueRefT( T&& obj) : m_ref(std::move(obj)) { init(&obj); }
  122. // Reset a OpaqueRefT. Called only for objects instantiated
  123. // internally in G-API (e.g. temporary GOpaque<T>'s within a
  124. // computation). Reset here means both initialization
  125. // (creating an object) and reset (discarding its existing
  126. // content before the next execution). Must never be called
  127. // for external OpaqueRefTs.
  128. void reset()
  129. {
  130. if (isEmpty())
  131. {
  132. T empty_obj{};
  133. m_desc = cv::descr_of(empty_obj);
  134. m_ref = std::move(empty_obj);
  135. GAPI_Assert(isRWOwn());
  136. }
  137. else if (isRWOwn())
  138. {
  139. util::get<rw_own_t>(m_ref) = {};
  140. }
  141. else GAPI_Assert(false); // shouldn't be called in *EXT modes
  142. }
  143. // Obtain a WRITE reference to underlying object
  144. // Used by CPU kernel API wrappers when a kernel execution frame
  145. // is created
  146. T& wref()
  147. {
  148. GAPI_Assert(isRWExt() || isRWOwn());
  149. if (isRWExt()) return *util::get<rw_ext_t>(m_ref);
  150. if (isRWOwn()) return util::get<rw_own_t>(m_ref);
  151. util::throw_error(std::logic_error("Impossible happened"));
  152. }
  153. // Obtain a READ reference to underlying object
  154. // Used by CPU kernel API wrappers when a kernel execution frame
  155. // is created
  156. const T& rref() const
  157. {
  158. // ANY object can be accessed for reading, even if it declared for
  159. // output. Example -- a GComputation from [in] to [out1,out2]
  160. // where [out2] is a result of operation applied to [out1]:
  161. //
  162. // GComputation boundary
  163. // . . . . . . .
  164. // . .
  165. // [in] ----> foo() ----> [out1]
  166. // . . :
  167. // . . . .:. . .
  168. // . V .
  169. // . bar() ---> [out2]
  170. // . . . . . . . . . . . .
  171. //
  172. if (isROExt()) return *util::get<ro_ext_t>(m_ref);
  173. if (isRWExt()) return *util::get<rw_ext_t>(m_ref);
  174. if (isRWOwn()) return util::get<rw_own_t>(m_ref);
  175. util::throw_error(std::logic_error("Impossible happened"));
  176. }
  177. virtual void mov(BasicOpaqueRef &v) override {
  178. OpaqueRefT<T> *tv = dynamic_cast<OpaqueRefT<T>*>(&v);
  179. GAPI_Assert(tv != nullptr);
  180. wref() = std::move(tv->wref());
  181. }
  182. virtual const void* ptr() const override { return &rref(); }
  183. virtual void set(const cv::util::any &a) override {
  184. wref() = util::any_cast<T>(a);
  185. }
  186. };
  187. // This class strips type information from OpaqueRefT<> and makes it usable
  188. // in the G-API executables (carrying run-time data/information to kernels).
  189. // Part of GRunArg.
  190. // Its methods are typed proxies to OpaqueRefT<T>.
  191. // OpaqueRef maintains "reference" semantics so two copies of OpaqueRef refer
  192. // to the same underlying object.
  193. class OpaqueRef
  194. {
  195. std::shared_ptr<BasicOpaqueRef> m_ref;
  196. cv::detail::OpaqueKind m_kind;
  197. template<typename T> inline void check() const
  198. {
  199. GAPI_DbgAssert(dynamic_cast<OpaqueRefT<T>*>(m_ref.get()) != nullptr);
  200. }
  201. public:
  202. OpaqueRef() = default;
  203. template<
  204. typename T,
  205. typename = util::are_different_t<OpaqueRef, T>
  206. >
  207. // FIXME: probably won't work with const object
  208. explicit OpaqueRef(T&& obj) :
  209. m_ref(new OpaqueRefT<util::decay_t<T>>(std::forward<T>(obj))),
  210. m_kind(GOpaqueTraits<util::decay_t<T>>::kind) {}
  211. cv::detail::OpaqueKind getKind() const
  212. {
  213. return m_kind;
  214. }
  215. template<typename T> void reset()
  216. {
  217. if (!m_ref) m_ref.reset(new OpaqueRefT<T>());
  218. check<T>();
  219. storeKind<T>();
  220. static_cast<OpaqueRefT<T>&>(*m_ref).reset();
  221. }
  222. template <typename T>
  223. void storeKind()
  224. {
  225. m_kind = cv::detail::GOpaqueTraits<T>::kind;
  226. }
  227. template<typename T> T& wref()
  228. {
  229. check<T>();
  230. return static_cast<OpaqueRefT<T>&>(*m_ref).wref();
  231. }
  232. template<typename T> const T& rref() const
  233. {
  234. check<T>();
  235. return static_cast<OpaqueRefT<T>&>(*m_ref).rref();
  236. }
  237. void mov(OpaqueRef &v)
  238. {
  239. m_ref->mov(*v.m_ref);
  240. }
  241. cv::GOpaqueDesc descr_of() const
  242. {
  243. return m_ref->m_desc;
  244. }
  245. // May be used to uniquely identify this object internally
  246. const void *ptr() const { return m_ref->ptr(); }
  247. // Introduced for in-graph meta handling
  248. OpaqueRef& operator= (const cv::util::any &a)
  249. {
  250. m_ref->set(a);
  251. return *this;
  252. }
  253. };
  254. } // namespace detail
  255. /** \addtogroup gapi_data_objects
  256. * @{
  257. */
  258. template<typename T> class GOpaque
  259. {
  260. public:
  261. // Host type (or Flat type) - the type this GOpaque is actually
  262. // specified to.
  263. using HT = typename detail::flatten_g<util::decay_t<T>>::type;
  264. GOpaque() { putDetails(); } // Empty constructor
  265. explicit GOpaque(detail::GOpaqueU &&ref) // GOpaqueU-based constructor
  266. : m_ref(ref) { putDetails(); } // (used by GCall, not for users)
  267. /// @private
  268. detail::GOpaqueU strip() const {
  269. return m_ref;
  270. }
  271. /// @private
  272. static void Ctor(detail::OpaqueRef& ref) {
  273. ref.reset<HT>();
  274. }
  275. private:
  276. void putDetails() {
  277. m_ref.setConstructFcn(&Ctor);
  278. m_ref.specifyType<HT>();
  279. m_ref.storeKind<HT>();
  280. }
  281. detail::GOpaqueU m_ref;
  282. };
  283. /** @} */
  284. } // namespace cv
  285. #endif // OPENCV_GAPI_GOPAQUE_HPP