Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / fluid / modules / gapi / test / internal / gapi_int_backend_tests.cpp
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-2019 Intel Corporation
6
7
8 #include "test_precomp.hpp"
9 #include "gapi_mock_kernels.hpp"
10
11 #include "compiler/gmodel.hpp"
12 #include "compiler/gcompiler.hpp"
13
14 namespace opencv_test {
15
16 namespace {
17
18 struct MockMeta
19 {
20     static const char* name() { return "MockMeta"; }
21 };
22
23 class GMockBackendImpl final: public cv::gapi::GBackend::Priv
24 {
25     virtual void unpackKernel(ade::Graph            &,
26                               const ade::NodeHandle &,
27                               const cv::GKernelImpl &) override
28     {
29         // Do nothing here
30     }
31
32     virtual EPtr compile(const ade::Graph &,
33                          const cv::GCompileArgs &,
34                          const std::vector<ade::NodeHandle> &) const override
35     {
36         // Do nothing here as well
37         return {};
38     }
39
40     virtual void addBackendPasses(ade::ExecutionEngineSetupContext &ectx) override
41     {
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())
45                 {
46                     me.metadata(nh).set(MockMeta{});
47                 }
48             });
49     }
50 };
51
52 static cv::gapi::GBackend mock_backend(std::make_shared<GMockBackendImpl>());
53
54 GAPI_OCV_KERNEL(MockFoo, I::Foo)
55 {
56     static void run(const cv::Mat &, cv::Mat &) { /*Do nothing*/ }
57     static cv::gapi::GBackend backend() { return mock_backend; } // FIXME: Must be removed
58 };
59
60 } // anonymous namespace
61
62 TEST(GBackend, CustomPassesExecuted)
63 {
64     cv::GMat in;
65     cv::GMat out = I::Foo::on(in);
66     cv::GComputation c(in, out);
67
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>();
71
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);
76
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())
81     {
82         EXPECT_TRUE(me.metadata(nh).contains<MockMeta>());
83     }
84 }
85
86 } // namespace opencv_test