gcommon.hpp 9.8 KB

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