gcommon.hpp 11 KB

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