gapi_transform_tests.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 Intel Corporation
  6. #include <tuple>
  7. #include "test_precomp.hpp"
  8. #include "opencv2/gapi/gtransform.hpp"
  9. #include "opencv2/gapi/gtype_traits.hpp"
  10. // explicit include to use GComputation::Priv
  11. #include "api/gcomputation_priv.hpp"
  12. namespace opencv_test
  13. {
  14. namespace
  15. {
  16. using GMat = cv::GMat;
  17. using GMat2 = std::tuple<GMat, GMat>;
  18. using GMat3 = std::tuple<GMat, GMat, GMat>;
  19. using GScalar = cv::GScalar;
  20. template <typename T> using GArray = cv::GArray<T>;
  21. template <typename T> using GOpaque = cv::GOpaque<T>;
  22. using ArrayT = int;
  23. using WrongArrayT = char;
  24. struct CustomType{
  25. cv::Mat mat;
  26. int i;
  27. void *v;
  28. CustomType* next;
  29. };
  30. struct AnotherCustomType{
  31. cv::Mat mat;
  32. int i;
  33. void *v;
  34. };
  35. using OpaqueT = CustomType;
  36. using WrongOpaqueT = AnotherCustomType;
  37. GAPI_TRANSFORM(gmat_in_gmat_out, <GMat(GMat)>, "gmat_in_gmat_out")
  38. {
  39. static GMat pattern(GMat) { return {}; }
  40. static GMat substitute(GMat) { return {}; }
  41. };
  42. GAPI_TRANSFORM(gmat2_in_gmat_out, <GMat(GMat, GMat)>, "gmat2_in_gmat_out")
  43. {
  44. static GMat pattern(GMat, GMat) { return {}; }
  45. static GMat substitute(GMat, GMat) { return {}; }
  46. };
  47. GAPI_TRANSFORM(gmat2_in_gmat3_out, <GMat3(GMat, GMat)>, "gmat2_in_gmat3_out")
  48. {
  49. static GMat3 pattern(GMat, GMat) { return {}; }
  50. static GMat3 substitute(GMat, GMat) { return {}; }
  51. };
  52. GAPI_TRANSFORM(gmatp_in_gmatp_out, <GMatP(GMatP)>, "gmatp_in_gmatp_out")
  53. {
  54. static GMatP pattern(GMatP) { return {}; }
  55. static GMatP substitute(GMatP) { return {}; }
  56. };
  57. GAPI_TRANSFORM(gsc_in_gmat_out, <GMat(GScalar)>, "gsc_in_gmat_out")
  58. {
  59. static GMat pattern(GScalar) { return {}; }
  60. static GMat substitute(GScalar) { return {}; }
  61. };
  62. GAPI_TRANSFORM(gmat_in_gsc_out, <GScalar(GMat)>, "gmat_in_gsc_out")
  63. {
  64. static GScalar pattern(GMat) { return {}; }
  65. static GScalar substitute(GMat) { return {}; }
  66. };
  67. GAPI_TRANSFORM(garr_in_gmat_out, <GMat(GArray<ArrayT>)>, "garr_in_gmat_out")
  68. {
  69. static GMat pattern(GArray<ArrayT>) { return {}; }
  70. static GMat substitute(GArray<ArrayT>) { return {}; }
  71. };
  72. GAPI_TRANSFORM(gmat_in_garr_out, <GArray<ArrayT>(GMat)>, "gmat_in_garr_out")
  73. {
  74. static GArray<ArrayT> pattern(GMat) { return {}; }
  75. static GArray<ArrayT> substitute(GMat) { return {}; }
  76. };
  77. GAPI_TRANSFORM(garr_in_gscalar_out, <GScalar(GArray<ArrayT>)>, "garr_in_gscalar_out")
  78. {
  79. static GScalar pattern(GArray<ArrayT>) { return {}; }
  80. static GScalar substitute(GArray<ArrayT>) { return {}; }
  81. };
  82. GAPI_TRANSFORM(gscalar_in_garr_out, <GArray<ArrayT>(GScalar)>, "gscalar_in_garr_out")
  83. {
  84. static GArray<ArrayT> pattern(GScalar) { return {}; }
  85. static GArray<ArrayT> substitute(GScalar) { return {}; }
  86. };
  87. GAPI_TRANSFORM(gmat_gsc_garray_in_gmat2_out, <GMat2(GMat, GScalar, GArray<ArrayT>)>, "gmat_gsc_garray_in_gmat2_out")
  88. {
  89. static GMat2 pattern(GMat, GScalar, GArray<ArrayT>) { return {}; }
  90. static GMat2 substitute(GMat, GScalar, GArray<ArrayT>) { return {}; }
  91. };
  92. GAPI_TRANSFORM(gop_in_gmat_out, <GMat(GOpaque<OpaqueT>)>, "gop_in_gmat_out")
  93. {
  94. static GMat pattern(GOpaque<OpaqueT>) { return {}; }
  95. static GMat substitute(GOpaque<OpaqueT>) { return {}; }
  96. };
  97. GAPI_TRANSFORM(gmat_in_gop_out, <GOpaque<OpaqueT>(GMat)>, "gmat_in_gop_out")
  98. {
  99. static GOpaque<OpaqueT> pattern(GMat) { return {}; }
  100. static GOpaque<OpaqueT> substitute(GMat) { return {}; }
  101. };
  102. GAPI_TRANSFORM(gop_in_gscalar_out, <GScalar(GOpaque<OpaqueT>)>, "gop_in_gscalar_out")
  103. {
  104. static GScalar pattern(GOpaque<OpaqueT>) { return {}; }
  105. static GScalar substitute(GOpaque<OpaqueT>) { return {}; }
  106. };
  107. GAPI_TRANSFORM(gscalar_in_gop_out, <GOpaque<OpaqueT>(GScalar)>, "gscalar_in_gop_out")
  108. {
  109. static GOpaque<OpaqueT> pattern(GScalar) { return {}; }
  110. static GOpaque<OpaqueT> substitute(GScalar) { return {}; }
  111. };
  112. GAPI_TRANSFORM(gmat_gsc_gopaque_in_gmat2_out, <GMat2(GMat, GScalar, GOpaque<OpaqueT>)>, "gmat_gsc_gopaque_in_gmat2_out")
  113. {
  114. static GMat2 pattern(GMat, GScalar, GOpaque<OpaqueT>) { return {}; }
  115. static GMat2 substitute(GMat, GScalar, GOpaque<OpaqueT>) { return {}; }
  116. };
  117. } // anonymous namespace
  118. TEST(KernelPackageTransform, CreatePackage)
  119. {
  120. auto pkg = cv::gapi::kernels
  121. < gmat_in_gmat_out
  122. , gmat2_in_gmat_out
  123. , gmat2_in_gmat3_out
  124. , gmatp_in_gmatp_out
  125. , gsc_in_gmat_out
  126. , gmat_in_gsc_out
  127. , garr_in_gmat_out
  128. , gmat_in_garr_out
  129. , garr_in_gscalar_out
  130. , gscalar_in_garr_out
  131. , gmat_gsc_garray_in_gmat2_out
  132. , gop_in_gmat_out
  133. , gmat_in_gop_out
  134. , gop_in_gscalar_out
  135. , gscalar_in_gop_out
  136. , gmat_gsc_gopaque_in_gmat2_out
  137. >();
  138. auto tr = pkg.get_transformations();
  139. EXPECT_EQ(16u, tr.size());
  140. }
  141. TEST(KernelPackageTransform, Include)
  142. {
  143. cv::GKernelPackage pkg;
  144. pkg.include<gmat_in_gmat_out>();
  145. pkg.include<gmat2_in_gmat_out>();
  146. pkg.include<gmat2_in_gmat3_out>();
  147. auto tr = pkg.get_transformations();
  148. EXPECT_EQ(3u, tr.size());
  149. }
  150. TEST(KernelPackageTransform, Combine)
  151. {
  152. auto pkg1 = cv::gapi::kernels<gmat_in_gmat_out>();
  153. auto pkg2 = cv::gapi::kernels<gmat2_in_gmat_out>();
  154. auto pkg_comb = cv::gapi::combine(pkg1, pkg2);
  155. auto tr = pkg_comb.get_transformations();
  156. EXPECT_EQ(2u, tr.size());
  157. }
  158. namespace
  159. {
  160. template <typename T>
  161. inline bool ProtoContainsT(const cv::GProtoArg &arg) {
  162. return cv::GProtoArg::index_of<T>() == arg.index();
  163. }
  164. } // anonymous namespace
  165. TEST(KernelPackageTransform, gmat_gsc_garray_in_gmat2_out)
  166. {
  167. auto tr = gmat_gsc_garray_in_gmat2_out::transformation();
  168. auto check = [](const cv::GComputation &comp){
  169. const auto &p = cv::util::get<cv::GComputation::Priv::Expr>(comp.priv().m_shape);
  170. EXPECT_EQ(3u, p.m_ins.size());
  171. EXPECT_EQ(2u, p.m_outs.size());
  172. EXPECT_TRUE(ProtoContainsT<GMat>(p.m_ins[0]));
  173. EXPECT_TRUE(ProtoContainsT<GScalar>(p.m_ins[1]));
  174. EXPECT_TRUE(ProtoContainsT<cv::detail::GArrayU>(p.m_ins[2]));
  175. EXPECT_TRUE(cv::util::get<cv::detail::GArrayU>(p.m_ins[2]).holds<ArrayT>());
  176. EXPECT_FALSE(cv::util::get<cv::detail::GArrayU>(p.m_ins[2]).holds<WrongArrayT>());
  177. EXPECT_TRUE(ProtoContainsT<GMat>(p.m_outs[0]));
  178. EXPECT_TRUE(ProtoContainsT<GMat>(p.m_outs[1]));
  179. };
  180. check(tr.pattern());
  181. check(tr.substitute());
  182. }
  183. TEST(KernelPackageTransform, gmat_gsc_gopaque_in_gmat2_out)
  184. {
  185. auto tr = gmat_gsc_gopaque_in_gmat2_out::transformation();
  186. auto check = [](const cv::GComputation &comp){
  187. const auto &p = cv::util::get<cv::GComputation::Priv::Expr>(comp.priv().m_shape);
  188. EXPECT_EQ(3u, p.m_ins.size());
  189. EXPECT_EQ(2u, p.m_outs.size());
  190. EXPECT_TRUE(ProtoContainsT<GMat>(p.m_ins[0]));
  191. EXPECT_TRUE(ProtoContainsT<GScalar>(p.m_ins[1]));
  192. EXPECT_TRUE(ProtoContainsT<cv::detail::GOpaqueU>(p.m_ins[2]));
  193. EXPECT_TRUE(cv::util::get<cv::detail::GOpaqueU>(p.m_ins[2]).holds<OpaqueT>());
  194. EXPECT_FALSE(cv::util::get<cv::detail::GOpaqueU>(p.m_ins[2]).holds<WrongOpaqueT>());
  195. EXPECT_TRUE(ProtoContainsT<GMat>(p.m_outs[0]));
  196. EXPECT_TRUE(ProtoContainsT<GMat>(p.m_outs[1]));
  197. };
  198. check(tr.pattern());
  199. check(tr.substitute());
  200. }
  201. namespace
  202. {
  203. template<typename ArgT>
  204. typename std::enable_if<(cv::detail::GTypeTraits<ArgT>::kind == cv::detail::ArgKind::GARRAY), void>::type
  205. arg_check(const cv::GProtoArg &arg)
  206. {
  207. EXPECT_TRUE(ProtoContainsT<cv::detail::GArrayU>(arg));
  208. EXPECT_TRUE(cv::util::get<cv::detail::GArrayU>(arg).holds<ArrayT>());
  209. EXPECT_FALSE(cv::util::get<cv::detail::GArrayU>(arg).holds<WrongArrayT>());
  210. }
  211. template<typename ArgT>
  212. typename std::enable_if<(cv::detail::GTypeTraits<ArgT>::kind == cv::detail::ArgKind::GOPAQUE), void>::type
  213. arg_check(const cv::GProtoArg &arg)
  214. {
  215. EXPECT_TRUE(ProtoContainsT<cv::detail::GOpaqueU>(arg));
  216. EXPECT_TRUE(cv::util::get<cv::detail::GOpaqueU>(arg).holds<OpaqueT>());
  217. EXPECT_FALSE(cv::util::get<cv::detail::GOpaqueU>(arg).holds<WrongOpaqueT>());
  218. }
  219. template<typename ArgT>
  220. typename std::enable_if<(cv::detail::GTypeTraits<ArgT>::kind != cv::detail::ArgKind::GARRAY &&
  221. cv::detail::GTypeTraits<ArgT>::kind != cv::detail::ArgKind::GOPAQUE), void>::type
  222. arg_check(const cv::GProtoArg &arg)
  223. {
  224. EXPECT_TRUE(ProtoContainsT<ArgT>(arg));
  225. }
  226. template<typename InType, typename OutType>
  227. void args_check(const cv::GComputation &comp)
  228. {
  229. const auto &p = cv::util::get<cv::GComputation::Priv::Expr>(comp.priv().m_shape);
  230. EXPECT_EQ(1u, p.m_ins.size());
  231. EXPECT_EQ(1u, p.m_outs.size());
  232. arg_check<InType>(p.m_ins[0]);
  233. arg_check<OutType>(p.m_outs[0]);
  234. }
  235. } // anonymous namespace
  236. template <typename Transformation, typename InType, typename OutType>
  237. static void transformTest()
  238. {
  239. auto tr = Transformation::transformation();
  240. args_check<InType, OutType>(tr.pattern());
  241. args_check<InType, OutType>(tr.substitute());
  242. }
  243. TEST(KernelPackageTransform, gmat_in_gmat_out)
  244. {
  245. transformTest<gmat_in_gmat_out, GMat, GMat>();
  246. }
  247. TEST(KernelPackageTransform, gmatp_in_gmatp_out)
  248. {
  249. transformTest<gmatp_in_gmatp_out, GMatP, GMatP>();
  250. }
  251. TEST(KernelPackageTransform, gsc_in_gmat_out)
  252. {
  253. transformTest<gsc_in_gmat_out, GScalar, GMat>();
  254. }
  255. TEST(KernelPackageTransform, gmat_in_gsc_out)
  256. {
  257. transformTest<gmat_in_gsc_out, GMat, GScalar>();
  258. }
  259. TEST(KernelPackageTransform, garr_in_gmat_out)
  260. {
  261. transformTest<garr_in_gmat_out, GArray<ArrayT>, GMat>();
  262. }
  263. TEST(KernelPackageTransform, gmat_in_garr_out)
  264. {
  265. transformTest<gmat_in_garr_out, GMat, GArray<ArrayT>>();
  266. }
  267. TEST(KernelPackageTransform, garr_in_gscalar_out)
  268. {
  269. transformTest<garr_in_gscalar_out, GArray<ArrayT>, GScalar>();
  270. }
  271. TEST(KernelPackageTransform, gscalar_in_garr_out)
  272. {
  273. transformTest<gscalar_in_garr_out, GScalar, GArray<ArrayT>>();
  274. }
  275. TEST(KernelPackageTransform, gop_in_gmat_out)
  276. {
  277. transformTest<gop_in_gmat_out, GOpaque<OpaqueT>, GMat>();
  278. }
  279. TEST(KernelPackageTransform, gmat_in_gop_out)
  280. {
  281. transformTest<gmat_in_gop_out, GMat, GOpaque<OpaqueT>>();
  282. }
  283. TEST(KernelPackageTransform, gop_in_gscalar_out)
  284. {
  285. transformTest<gop_in_gscalar_out, GOpaque<OpaqueT>, GScalar>();
  286. }
  287. TEST(KernelPackageTransform, gscalar_in_gop_out)
  288. {
  289. transformTest<gscalar_in_gop_out, GScalar, GOpaque<OpaqueT>>();
  290. }
  291. } // namespace opencv_test