gcommon.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 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/own/exports.hpp"
  14. #include "opencv2/gapi/own/assert.hpp"
  15. namespace cv {
  16. namespace detail
  17. {
  18. // This is a trait-like structure to mark backend-specific compile arguments
  19. // with tags
  20. template<typename T> struct CompileArgTag;
  21. template<typename T> struct CompileArgTag
  22. {
  23. static const char* tag() { return ""; };
  24. };
  25. }
  26. // This definition is here because it is reused by both public(?) and internal
  27. // modules. Keeping it here wouldn't expose public details (e.g., API-level)
  28. // to components which are internal and operate on a lower-level entities
  29. // (e.g., compiler, backends).
  30. // FIXME: merge with ArgKind?
  31. // FIXME: replace with variant[format desc]?
  32. enum class GShape: int
  33. {
  34. GMAT,
  35. GSCALAR,
  36. GARRAY,
  37. };
  38. struct GCompileArg;
  39. namespace detail {
  40. template<typename T>
  41. using is_compile_arg = std::is_same<GCompileArg, typename std::decay<T>::type>;
  42. }
  43. // CompileArg is an unified interface over backend-specific compilation
  44. // information
  45. // FIXME: Move to a separate file?
  46. /** \addtogroup gapi_compile_args
  47. * @{
  48. *
  49. * @brief Compilation arguments: a set of data structures which can be
  50. * passed to control compilation process
  51. *
  52. * G-API comes with a number of graph compilation options which can be
  53. * passed to cv::GComputation::apply() or
  54. * cv::GComputation::compile(). Known compilation options are listed
  55. * in this page, while extra backends may introduce their own
  56. * compilation options (G-API transparently accepts _everything_ which
  57. * can be passed to cv::compile_args(), it depends on underlying
  58. * backends if an option would be interpreted or not).
  59. *
  60. * For example, if an example computation is executed like this:
  61. *
  62. * @snippet modules/gapi/samples/api_ref_snippets.cpp graph_decl_apply
  63. *
  64. * Extra parameter specifying which kernels to compile with can be
  65. * passed like this:
  66. *
  67. * @snippet modules/gapi/samples/api_ref_snippets.cpp apply_with_param
  68. */
  69. /**
  70. * @brief Represents an arbitrary compilation argument.
  71. *
  72. * Any value can be wrapped into cv::GCompileArg, but only known ones
  73. * (to G-API or its backends) can be interpreted correctly.
  74. *
  75. * Normally objects of this class shouldn't be created manually, use
  76. * cv::compile_args() function which automatically wraps everything
  77. * passed in (a variadic template parameter pack) into a vector of
  78. * cv::GCompileArg objects.
  79. */
  80. struct GAPI_EXPORTS GCompileArg
  81. {
  82. public:
  83. std::string tag;
  84. // FIXME: use decay in GArg/other trait-based wrapper before leg is shot!
  85. template<typename T, typename std::enable_if<!detail::is_compile_arg<T>::value, int>::type = 0>
  86. explicit GCompileArg(T &&t)
  87. : tag(detail::CompileArgTag<typename std::decay<T>::type>::tag())
  88. , arg(t)
  89. {
  90. }
  91. template<typename T> T& get()
  92. {
  93. return util::any_cast<T>(arg);
  94. }
  95. template<typename T> const T& get() const
  96. {
  97. return util::any_cast<T>(arg);
  98. }
  99. private:
  100. util::any arg;
  101. };
  102. using GCompileArgs = std::vector<GCompileArg>;
  103. /**
  104. * Wraps a list of arguments (a parameter pack) into a vector of
  105. * compilation arguments (cv::GCompileArg).
  106. */
  107. template<typename... Ts> GCompileArgs compile_args(Ts&&... args)
  108. {
  109. return GCompileArgs{ GCompileArg(args)... };
  110. }
  111. /**
  112. * @brief Ask G-API to dump compiled graph in Graphviz format under
  113. * the given file name.
  114. *
  115. * Specifies a graph dump path (path to .dot file to be generated).
  116. * G-API will dump a .dot file under specified path during a
  117. * compilation process if this flag is passed.
  118. */
  119. struct graph_dump_path
  120. {
  121. std::string m_dump_path;
  122. };
  123. /** @} */
  124. namespace detail
  125. {
  126. template<> struct CompileArgTag<cv::graph_dump_path>
  127. {
  128. static const char* tag() { return "gapi.graph_dump_path"; }
  129. };
  130. }
  131. } // namespace cv
  132. // std::hash overload for GShape
  133. namespace std
  134. {
  135. template<> struct hash<cv::GShape>
  136. {
  137. size_t operator() (cv::GShape sh) const
  138. {
  139. return std::hash<int>()(static_cast<int>(sh));
  140. }
  141. };
  142. } // namespace std
  143. #endif // OPENCV_GAPI_GCOMMON_HPP