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.
5 // Copyright (C) 2018-2019 Intel Corporation
8 #include "test_precomp.hpp"
9 #include "gapi_mock_kernels.hpp"
11 #include "compiler/gmodel.hpp"
12 #include "compiler/gcompiler.hpp"
14 namespace opencv_test {
20 static const char* name() { return "MockMeta"; }
23 class GMockBackendImpl final: public cv::gapi::GBackend::Priv
25 virtual void unpackKernel(ade::Graph &,
26 const ade::NodeHandle &,
27 const cv::GKernelImpl &) override
32 virtual EPtr compile(const ade::Graph &,
33 const cv::GCompileArgs &,
34 const std::vector<ade::NodeHandle> &) const override
36 // Do nothing here as well
40 virtual void addBackendPasses(ade::ExecutionEngineSetupContext &ectx) override
42 ectx.addPass("transform", "set_mock_meta", [](ade::passes::PassContext &ctx) {
43 ade::TypedGraph<MockMeta> me(ctx.graph);
44 for (const auto &nh : me.nodes())
46 me.metadata(nh).set(MockMeta{});
52 static cv::gapi::GBackend mock_backend(std::make_shared<GMockBackendImpl>());
54 GAPI_OCV_KERNEL(MockFoo, I::Foo)
56 static void run(const cv::Mat &, cv::Mat &) { /*Do nothing*/ }
57 static cv::gapi::GBackend backend() { return mock_backend; } // FIXME: Must be removed
60 } // anonymous namespace
62 TEST(GBackend, CustomPassesExecuted)
65 cv::GMat out = I::Foo::on(in);
66 cv::GComputation c(in, out);
68 // Prepare compilation parameters manually
69 const auto in_meta = cv::GMetaArg(cv::GMatDesc{CV_8U,1,cv::Size(32,32)});
70 const auto pkg = cv::gapi::kernels<MockFoo>();
72 // Directly instantiate G-API graph compiler and run partial compilation
73 cv::gimpl::GCompiler compiler(c, {in_meta}, cv::compile_args(pkg));
74 cv::gimpl::GCompiler::GPtr graph = compiler.generateGraph();
75 compiler.runPasses(*graph);
77 // Inspect the graph and verify the metadata written by Mock backend
78 ade::TypedGraph<MockMeta> me(*graph);
79 EXPECT_LT(0u, static_cast<std::size_t>(me.nodes().size()));
80 for (const auto &nh : me.nodes())
82 EXPECT_TRUE(me.metadata(nh).contains<MockMeta>());
86 } // namespace opencv_test