File: gapi_int_backend_tests.cpp

package info (click to toggle)
opencv 4.10.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 282,092 kB
  • sloc: cpp: 1,178,079; xml: 682,621; python: 49,092; lisp: 31,150; java: 25,469; ansic: 11,039; javascript: 6,085; sh: 1,214; cs: 601; perl: 494; objc: 210; makefile: 173
file content (86 lines) | stat: -rw-r--r-- 2,576 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
//
// Copyright (C) 2018 Intel Corporation


#include "../test_precomp.hpp"
#include "../gapi_mock_kernels.hpp"

#include "compiler/gmodel.hpp"
#include "compiler/gcompiler.hpp"

namespace opencv_test {

namespace {

struct MockMeta
{
    static const char* name() { return "MockMeta"; }
};

class GMockBackendImpl final: public cv::gapi::GBackend::Priv
{
    virtual void unpackKernel(ade::Graph            &,
                              const ade::NodeHandle &,
                              const cv::GKernelImpl &) override
    {
        // Do nothing here
    }

    virtual EPtr compile(const ade::Graph &,
                         const cv::GCompileArgs &,
                         const std::vector<ade::NodeHandle> &) const override
    {
        // Do nothing here as well
        return {};
    }

    virtual void addBackendPasses(ade::ExecutionEngineSetupContext &ectx) override
    {
        ectx.addPass("transform", "set_mock_meta", [](ade::passes::PassContext &ctx) {
                ade::TypedGraph<MockMeta> me(ctx.graph);
                for (const auto &nh : me.nodes())
                {
                    me.metadata(nh).set(MockMeta{});
                }
            });
    }
};

static cv::gapi::GBackend mock_backend(std::make_shared<GMockBackendImpl>());

GAPI_OCV_KERNEL(MockFoo, I::Foo)
{
    static void run(const cv::Mat &, cv::Mat &) { /*Do nothing*/ }
    static cv::gapi::GBackend backend() { return mock_backend; } // FIXME: Must be removed
};

} // anonymous namespace

TEST(GBackend, CustomPassesExecuted)
{
    cv::GMat in;
    cv::GMat out = I::Foo::on(in);
    cv::GComputation c(in, out);

    // Prepare compilation parameters manually
    const auto in_meta = cv::GMetaArg(cv::GMatDesc{CV_8U,1,cv::Size(32,32)});
    const auto pkg     = cv::gapi::kernels<MockFoo>();

    // Directly instantiate G-API graph compiler and run partial compilation
    cv::gimpl::GCompiler compiler(c, {in_meta}, cv::compile_args(pkg));
    cv::gimpl::GCompiler::GPtr graph = compiler.generateGraph();
    compiler.runPasses(*graph);

    // Inspect the graph and verify the metadata written by Mock backend
    ade::TypedGraph<MockMeta> me(*graph);
    EXPECT_LT(0u, static_cast<std::size_t>(me.nodes().size()));
    for (const auto &nh : me.nodes())
    {
        EXPECT_TRUE(me.metadata(nh).contains<MockMeta>());
    }
}

} // namespace opencv_test