gcommon.hpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. #ifndef OPENCV_GAPI_GCOMMON_HPP
  7. #define OPENCV_GAPI_GCOMMON_HPP
  8. #include <functional> // std::hash
  9. #include <vector> // std::vector
  10. #include <type_traits> // decay
  11. #include <opencv2/gapi/opencv_includes.hpp>
  12. #include <opencv2/gapi/util/any.hpp>
  13. #include <opencv2/gapi/util/optional.hpp>
  14. #include <opencv2/gapi/own/exports.hpp>
  15. #include <opencv2/gapi/own/assert.hpp>
  16. #include <opencv2/gapi/render/render_types.hpp>
  17. #include <opencv2/gapi/s11n/base.hpp>
  18. namespace cv {
  19. class GMat; // FIXME: forward declaration for GOpaqueTraits
  20. namespace detail
  21. {
  22. // This is a trait-like structure to mark backend-specific compile arguments
  23. // with tags
  24. template<typename T> struct CompileArgTag;
  25. // These structures are tags which separate kernels and transformations
  26. struct KernelTag
  27. {};
  28. struct TransformTag
  29. {};
  30. // This enum is utilized mostly by GArray and GOpaque to store and recognize their internal data
  31. // types (aka Host type). Also it is widely used during serialization routine.
  32. enum class OpaqueKind: int
  33. {
  34. CV_UNKNOWN, // Unknown, generic, opaque-to-GAPI data type unsupported in graph seriallization
  35. CV_BOOL, // bool user G-API data
  36. CV_INT, // int user G-API data
  37. CV_INT64, // int64_t user G-API data
  38. CV_DOUBLE, // double user G-API data
  39. CV_FLOAT, // float user G-API data
  40. CV_UINT64, // uint64_t user G-API data
  41. CV_STRING, // std::string user G-API data
  42. CV_POINT, // cv::Point user G-API data
  43. CV_POINT2F, // cv::Point2f user G-API data
  44. CV_SIZE, // cv::Size user G-API data
  45. CV_RECT, // cv::Rect user G-API data
  46. CV_SCALAR, // cv::Scalar user G-API data
  47. CV_MAT, // cv::Mat user G-API data
  48. CV_DRAW_PRIM, // cv::gapi::wip::draw::Prim user G-API data
  49. };
  50. // Type traits helper which simplifies the extraction of kind from type
  51. template<typename T> struct GOpaqueTraits;
  52. template<typename T> struct GOpaqueTraits { static constexpr const OpaqueKind kind = OpaqueKind::CV_UNKNOWN; };
  53. template<> struct GOpaqueTraits<int> { static constexpr const OpaqueKind kind = OpaqueKind::CV_INT; };
  54. template<> struct GOpaqueTraits<int64_t> { static constexpr const OpaqueKind kind = OpaqueKind::CV_INT64; };
  55. template<> struct GOpaqueTraits<double> { static constexpr const OpaqueKind kind = OpaqueKind::CV_DOUBLE; };
  56. template<> struct GOpaqueTraits<float> { static constexpr const OpaqueKind kind = OpaqueKind::CV_FLOAT; };
  57. template<> struct GOpaqueTraits<uint64_t> { static constexpr const OpaqueKind kind = OpaqueKind::CV_UINT64; };
  58. template<> struct GOpaqueTraits<bool> { static constexpr const OpaqueKind kind = OpaqueKind::CV_BOOL; };
  59. template<> struct GOpaqueTraits<std::string> { static constexpr const OpaqueKind kind = OpaqueKind::CV_STRING; };
  60. template<> struct GOpaqueTraits<cv::Size> { static constexpr const OpaqueKind kind = OpaqueKind::CV_SIZE; };
  61. template<> struct GOpaqueTraits<cv::Scalar> { static constexpr const OpaqueKind kind = OpaqueKind::CV_SCALAR; };
  62. template<> struct GOpaqueTraits<cv::Point> { static constexpr const OpaqueKind kind = OpaqueKind::CV_POINT; };
  63. template<> struct GOpaqueTraits<cv::Point2f> { static constexpr const OpaqueKind kind = OpaqueKind::CV_POINT2F; };
  64. template<> struct GOpaqueTraits<cv::Mat> { static constexpr const OpaqueKind kind = OpaqueKind::CV_MAT; };
  65. template<> struct GOpaqueTraits<cv::Rect> { static constexpr const OpaqueKind kind = OpaqueKind::CV_RECT; };
  66. template<> struct GOpaqueTraits<cv::GMat> { static constexpr const OpaqueKind kind = OpaqueKind::CV_MAT; };
  67. template<> struct GOpaqueTraits<cv::gapi::wip::draw::Prim>
  68. { static constexpr const OpaqueKind kind = OpaqueKind::CV_DRAW_PRIM; };
  69. using GOpaqueTraitsArrayTypes = std::tuple<int, double, float, uint64_t, bool, std::string, cv::Size, cv::Scalar, cv::Point, cv::Point2f,
  70. cv::Mat, cv::Rect, cv::gapi::wip::draw::Prim>;
  71. // GOpaque is not supporting cv::Mat and cv::Scalar since there are GScalar and GMat types
  72. using GOpaqueTraitsOpaqueTypes = std::tuple<int, double, float, uint64_t, bool, std::string, cv::Size, cv::Point, cv::Point2f, cv::Rect,
  73. cv::gapi::wip::draw::Prim>;
  74. } // namespace detail
  75. // This definition is here because it is reused by both public(?) and internal
  76. // modules. Keeping it here wouldn't expose public details (e.g., API-level)
  77. // to components which are internal and operate on a lower-level entities
  78. // (e.g., compiler, backends).
  79. // FIXME: merge with ArgKind?
  80. // FIXME: replace with variant[format desc]?
  81. enum class GShape: int
  82. {
  83. GMAT,
  84. GSCALAR,
  85. GARRAY,
  86. GOPAQUE,
  87. GFRAME,
  88. };
  89. namespace gapi {
  90. namespace s11n {
  91. namespace detail {
  92. template<typename T> struct wrap_serialize;
  93. } // namespace detail
  94. } // namespace s11n
  95. } // namespace gapi
  96. struct GCompileArg;
  97. namespace detail {
  98. template<typename T>
  99. using is_compile_arg = std::is_same<GCompileArg, typename std::decay<T>::type>;
  100. } // namespace detail
  101. // CompileArg is an unified interface over backend-specific compilation
  102. // information
  103. // FIXME: Move to a separate file?
  104. /** \addtogroup gapi_compile_args
  105. * @{
  106. *
  107. * @brief Compilation arguments: data structures controlling the
  108. * compilation process
  109. *
  110. * G-API comes with a number of graph compilation options which can be
  111. * passed to cv::GComputation::apply() or
  112. * cv::GComputation::compile(). Known compilation options are listed
  113. * in this page, while extra backends may introduce their own
  114. * compilation options (G-API transparently accepts _everything_ which
  115. * can be passed to cv::compile_args(), it depends on underlying
  116. * backends if an option would be interpreted or not).
  117. *
  118. * For example, if an example computation is executed like this:
  119. *
  120. * @snippet samples/cpp/tutorial_code/gapi/doc_snippets/api_ref_snippets.cpp graph_decl_apply
  121. *
  122. * Extra parameter specifying which kernels to compile with can be
  123. * passed like this:
  124. *
  125. * @snippet samples/cpp/tutorial_code/gapi/doc_snippets/api_ref_snippets.cpp apply_with_param
  126. */
  127. /**
  128. * @brief Represents an arbitrary compilation argument.
  129. *
  130. * Any value can be wrapped into cv::GCompileArg, but only known ones
  131. * (to G-API or its backends) can be interpreted correctly.
  132. *
  133. * Normally objects of this class shouldn't be created manually, use
  134. * cv::compile_args() function which automatically wraps everything
  135. * passed in (a variadic template parameter pack) into a vector of
  136. * cv::GCompileArg objects.
  137. */
  138. struct GCompileArg
  139. {
  140. public:
  141. // NB: Required for pythnon bindings
  142. GCompileArg() = default;
  143. std::string tag;
  144. // FIXME: use decay in GArg/other trait-based wrapper before leg is shot!
  145. template<typename T, typename std::enable_if<!detail::is_compile_arg<T>::value, int>::type = 0>
  146. explicit GCompileArg(T &&t)
  147. : tag(detail::CompileArgTag<typename std::decay<T>::type>::tag())
  148. , serializeF(cv::gapi::s11n::detail::has_S11N_spec<T>::value ?
  149. &cv::gapi::s11n::detail::wrap_serialize<T>::serialize :
  150. nullptr)
  151. , arg(t)
  152. {
  153. }
  154. template<typename T> T& get()
  155. {
  156. return util::any_cast<T>(arg);
  157. }
  158. template<typename T> const T& get() const
  159. {
  160. return util::any_cast<T>(arg);
  161. }
  162. void serialize(cv::gapi::s11n::IOStream& os) const
  163. {
  164. if (serializeF)
  165. {
  166. serializeF(os, *this);
  167. }
  168. }
  169. private:
  170. std::function<void(cv::gapi::s11n::IOStream&, const GCompileArg&)> serializeF;
  171. util::any arg;
  172. };
  173. using GCompileArgs = std::vector<GCompileArg>;
  174. inline cv::GCompileArgs& operator += ( cv::GCompileArgs &lhs,
  175. const cv::GCompileArgs &rhs)
  176. {
  177. lhs.reserve(lhs.size() + rhs.size());
  178. lhs.insert(lhs.end(), rhs.begin(), rhs.end());
  179. return lhs;
  180. }
  181. /**
  182. * @brief Wraps a list of arguments (a parameter pack) into a vector of
  183. * compilation arguments (cv::GCompileArg).
  184. */
  185. template<typename... Ts> GCompileArgs compile_args(Ts&&... args)
  186. {
  187. return GCompileArgs{ GCompileArg(args)... };
  188. }
  189. namespace gapi
  190. {
  191. /**
  192. * @brief Retrieves particular compilation argument by its type from
  193. * cv::GCompileArgs
  194. */
  195. template<typename T>
  196. inline cv::util::optional<T> getCompileArg(const cv::GCompileArgs &args)
  197. {
  198. for (auto &compile_arg : args)
  199. {
  200. if (compile_arg.tag == cv::detail::CompileArgTag<T>::tag())
  201. {
  202. return cv::util::optional<T>(compile_arg.get<T>());
  203. }
  204. }
  205. return cv::util::optional<T>();
  206. }
  207. namespace s11n {
  208. namespace detail {
  209. template<typename T> struct wrap_serialize
  210. {
  211. static void serialize(IOStream& os, const GCompileArg& arg)
  212. {
  213. using DT = typename std::decay<T>::type;
  214. S11N<DT>::serialize(os, arg.get<DT>());
  215. }
  216. };
  217. } // namespace detail
  218. } // namespace s11n
  219. } // namespace gapi
  220. /**
  221. * @brief Ask G-API to dump compiled graph in Graphviz format under
  222. * the given file name.
  223. *
  224. * Specifies a graph dump path (path to .dot file to be generated).
  225. * G-API will dump a .dot file under specified path during a
  226. * compilation process if this flag is passed.
  227. */
  228. struct graph_dump_path
  229. {
  230. std::string m_dump_path;
  231. };
  232. /** @} */
  233. namespace detail
  234. {
  235. template<> struct CompileArgTag<cv::graph_dump_path>
  236. {
  237. static const char* tag() { return "gapi.graph_dump_path"; }
  238. };
  239. }
  240. } // namespace cv
  241. // std::hash overload for GShape
  242. namespace std
  243. {
  244. template<> struct hash<cv::GShape>
  245. {
  246. size_t operator() (cv::GShape sh) const
  247. {
  248. return std::hash<int>()(static_cast<int>(sh));
  249. }
  250. };
  251. } // namespace std
  252. #endif // OPENCV_GAPI_GCOMMON_HPP