Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / fluid / modules / gapi / src / compiler / gcompiled.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 "precomp.hpp"
9
10 #include <ade/graph.hpp>
11
12 #include "opencv2/gapi/gproto.hpp" // descr_of
13 #include "opencv2/gapi/gcompiled.hpp"
14
15 #include "compiler/gcompiled_priv.hpp"
16 #include "backends/common/gbackend.hpp"
17
18 // GCompiled private implementation ////////////////////////////////////////////
19 void cv::GCompiled::Priv::setup(const GMetaArgs &_metaArgs,
20                                 const GMetaArgs &_outMetas,
21                                 std::unique_ptr<cv::gimpl::GExecutor> &&_pE)
22 {
23     m_metas    = _metaArgs;
24     m_outMetas = _outMetas;
25     m_exec     = std::move(_pE);
26 }
27
28 bool cv::GCompiled::Priv::isEmpty() const
29 {
30     return !m_exec;
31 }
32
33 void cv::GCompiled::Priv::run(cv::gimpl::GRuntimeArgs &&args)
34 {
35     // Strip away types since ADE knows nothing about that
36     // args will be taken by specific GBackendExecutables
37     checkArgs(args);
38     m_exec->run(std::move(args));
39 }
40
41 const cv::GMetaArgs& cv::GCompiled::Priv::metas() const
42 {
43     return m_metas;
44 }
45
46 const cv::GMetaArgs& cv::GCompiled::Priv::outMetas() const
47 {
48     return m_outMetas;
49 }
50
51 void cv::GCompiled::Priv::checkArgs(const cv::gimpl::GRuntimeArgs &args) const
52 {
53     const auto runtime_metas = descr_of(args.inObjs);
54     if (runtime_metas != m_metas)
55     {
56       util::throw_error(std::logic_error("This object was compiled "
57                                          "for different metadata!"));
58         // FIXME: Add details on what is actually wrong
59     }
60 }
61
62 bool cv::GCompiled::Priv::canReshape() const
63 {
64     GAPI_Assert(m_exec);
65     return m_exec->canReshape();
66 }
67
68 void cv::GCompiled::Priv::reshape(const GMetaArgs& inMetas, const GCompileArgs& args)
69 {
70     GAPI_Assert(m_exec);
71     m_exec->reshape(inMetas, args);
72     m_metas = inMetas;
73 }
74
75 const cv::gimpl::GModel::Graph& cv::GCompiled::Priv::model() const
76 {
77     GAPI_Assert(nullptr != m_exec);
78     return m_exec->model();
79 }
80
81 // GCompiled public implementation /////////////////////////////////////////////
82 cv::GCompiled::GCompiled()
83     : m_priv(new Priv())
84 {
85 }
86
87 cv::GCompiled::operator bool() const
88 {
89     return !m_priv->isEmpty();
90 }
91
92 void cv::GCompiled::operator() (GRunArgs &&ins, GRunArgsP &&outs)
93 {
94     // FIXME: Check that <outs> matches the protocol
95     m_priv->run(cv::gimpl::GRuntimeArgs{std::move(ins),std::move(outs)});
96 }
97
98 #if !defined(GAPI_STANDALONE)
99 void cv::GCompiled::operator ()(cv::Mat in, cv::Mat &out)
100 {
101     (*this)(cv::gin(in), cv::gout(out));
102 }
103
104 void cv::GCompiled::operator() (cv::Mat in, cv::Scalar &out)
105 {
106     (*this)(cv::gin(in), cv::gout(out));
107 }
108
109 void cv::GCompiled::operator() (cv::Mat in1, cv::Mat in2, cv::Mat &out)
110 {
111     (*this)(cv::gin(in1, in2), cv::gout(out));
112 }
113
114 void cv::GCompiled::operator() (cv::Mat in1, cv::Mat in2, cv::Scalar &out)
115 {
116     (*this)(cv::gin(in1, in2), cv::gout(out));
117 }
118
119 void cv::GCompiled::operator ()(const std::vector<cv::Mat> &ins,
120                                 const std::vector<cv::Mat> &outs)
121 {
122     GRunArgs call_ins;
123     GRunArgsP call_outs;
124
125     // Make a temporary copy of vector outs - cv::Mats are copies anyway
126     auto tmp = outs;
127     for (const cv::Mat &m : ins) { call_ins.emplace_back(m);   }
128     for (      cv::Mat &m : tmp) { call_outs.emplace_back(&m); }
129
130     (*this)(std::move(call_ins), std::move(call_outs));
131 }
132 #endif // !defined(GAPI_STANDALONE)
133
134 const cv::GMetaArgs& cv::GCompiled::metas() const
135 {
136     return m_priv->metas();
137 }
138
139 const cv::GMetaArgs& cv::GCompiled::outMetas() const
140 {
141     return m_priv->outMetas();
142 }
143
144 cv::GCompiled::Priv& cv::GCompiled::priv()
145 {
146     return *m_priv;
147 }
148
149 bool cv::GCompiled::canReshape() const
150 {
151     return m_priv->canReshape();
152 }
153
154 void cv::GCompiled::reshape(const GMetaArgs& inMetas, const GCompileArgs& args)
155 {
156     m_priv->reshape(inMetas, args);
157 }