gapi_int_backend_tests.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #include "../test_precomp.hpp"
  7. #include "../gapi_mock_kernels.hpp"
  8. #include "compiler/gmodel.hpp"
  9. #include "compiler/gcompiler.hpp"
  10. namespace opencv_test {
  11. namespace {
  12. struct MockMeta
  13. {
  14. static const char* name() { return "MockMeta"; }
  15. };
  16. class GMockBackendImpl final: public cv::gapi::GBackend::Priv
  17. {
  18. virtual void unpackKernel(ade::Graph &,
  19. const ade::NodeHandle &,
  20. const cv::GKernelImpl &) override
  21. {
  22. // Do nothing here
  23. }
  24. virtual EPtr compile(const ade::Graph &,
  25. const cv::GCompileArgs &,
  26. const std::vector<ade::NodeHandle> &) const override
  27. {
  28. // Do nothing here as well
  29. return {};
  30. }
  31. virtual void addBackendPasses(ade::ExecutionEngineSetupContext &ectx) override
  32. {
  33. ectx.addPass("transform", "set_mock_meta", [](ade::passes::PassContext &ctx) {
  34. ade::TypedGraph<MockMeta> me(ctx.graph);
  35. for (const auto &nh : me.nodes())
  36. {
  37. me.metadata(nh).set(MockMeta{});
  38. }
  39. });
  40. }
  41. };
  42. static cv::gapi::GBackend mock_backend(std::make_shared<GMockBackendImpl>());
  43. GAPI_OCV_KERNEL(MockFoo, I::Foo)
  44. {
  45. static void run(const cv::Mat &, cv::Mat &) { /*Do nothing*/ }
  46. static cv::gapi::GBackend backend() { return mock_backend; } // FIXME: Must be removed
  47. };
  48. } // anonymous namespace
  49. TEST(GBackend, CustomPassesExecuted)
  50. {
  51. cv::GMat in;
  52. cv::GMat out = I::Foo::on(in);
  53. cv::GComputation c(in, out);
  54. // Prepare compilation parameters manually
  55. const auto in_meta = cv::GMetaArg(cv::GMatDesc{CV_8U,1,cv::Size(32,32)});
  56. const auto pkg = cv::gapi::kernels<MockFoo>();
  57. // Directly instantiate G-API graph compiler and run partial compilation
  58. cv::gimpl::GCompiler compiler(c, {in_meta}, cv::compile_args(pkg));
  59. cv::gimpl::GCompiler::GPtr graph = compiler.generateGraph();
  60. compiler.runPasses(*graph);
  61. // Inspect the graph and verify the metadata written by Mock backend
  62. ade::TypedGraph<MockMeta> me(*graph);
  63. EXPECT_LT(0u, static_cast<std::size_t>(me.nodes().size()));
  64. for (const auto &nh : me.nodes())
  65. {
  66. EXPECT_TRUE(me.metadata(nh).contains<MockMeta>());
  67. }
  68. }
  69. } // namespace opencv_test