00de6997ed47b39721bc8b631a492d38d1b145b1
[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 Intel Corporation
6
7
8 #include "precomp.hpp"
9
10 #include <ade/graph.hpp>
11
12 #include "opencv2/gapi/gproto.hpp" // can_describe
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     if (!can_describe(m_metas, args.inObjs))
54     {
55         util::throw_error(std::logic_error("This object was compiled "
56                                            "for different metadata!"));
57         // FIXME: Add details on what is actually wrong
58     }
59 }
60
61 bool cv::GCompiled::Priv::canReshape() const
62 {
63     GAPI_Assert(m_exec);
64     return m_exec->canReshape();
65 }
66
67 void cv::GCompiled::Priv::reshape(const GMetaArgs& inMetas, const GCompileArgs& args)
68 {
69     GAPI_Assert(m_exec);
70     m_exec->reshape(inMetas, args);
71     m_metas = inMetas;
72 }
73
74 const cv::gimpl::GModel::Graph& cv::GCompiled::Priv::model() const
75 {
76     GAPI_Assert(nullptr != m_exec);
77     return m_exec->model();
78 }
79
80 // GCompiled public implementation /////////////////////////////////////////////
81 cv::GCompiled::GCompiled()
82     : m_priv(new Priv())
83 {
84 }
85
86 cv::GCompiled::operator bool() const
87 {
88     return !m_priv->isEmpty();
89 }
90
91 void cv::GCompiled::operator() (GRunArgs &&ins, GRunArgsP &&outs)
92 {
93     // FIXME: Check that <outs> matches the protocol
94     m_priv->run(cv::gimpl::GRuntimeArgs{std::move(ins),std::move(outs)});
95 }
96
97 #if !defined(GAPI_STANDALONE)
98 void cv::GCompiled::operator ()(cv::Mat in, cv::Mat &out)
99 {
100     (*this)(cv::gin(in), cv::gout(out));
101 }
102
103 void cv::GCompiled::operator() (cv::Mat in, cv::Scalar &out)
104 {
105     (*this)(cv::gin(in), cv::gout(out));
106 }
107
108 void cv::GCompiled::operator() (cv::Mat in1, cv::Mat in2, cv::Mat &out)
109 {
110     (*this)(cv::gin(in1, in2), cv::gout(out));
111 }
112
113 void cv::GCompiled::operator() (cv::Mat in1, cv::Mat in2, cv::Scalar &out)
114 {
115     (*this)(cv::gin(in1, in2), cv::gout(out));
116 }
117
118 void cv::GCompiled::operator ()(const std::vector<cv::Mat> &ins,
119                                 const std::vector<cv::Mat> &outs)
120 {
121     GRunArgs call_ins;
122     GRunArgsP call_outs;
123
124     // Make a temporary copy of vector outs - cv::Mats are copies anyway
125     auto tmp = outs;
126     for (const cv::Mat &m : ins) { call_ins.emplace_back(m);   }
127     for (      cv::Mat &m : tmp) { call_outs.emplace_back(&m); }
128
129     (*this)(std::move(call_ins), std::move(call_outs));
130 }
131 #endif // !defined(GAPI_STANDALONE)
132
133 const cv::GMetaArgs& cv::GCompiled::metas() const
134 {
135     return m_priv->metas();
136 }
137
138 const cv::GMetaArgs& cv::GCompiled::outMetas() const
139 {
140     return m_priv->outMetas();
141 }
142
143 cv::GCompiled::Priv& cv::GCompiled::priv()
144 {
145     return *m_priv;
146 }
147
148 bool cv::GCompiled::canReshape() const
149 {
150     return m_priv->canReshape();
151 }
152
153 void cv::GCompiled::reshape(const GMetaArgs& inMetas, const GCompileArgs& args)
154 {
155     m_priv->reshape(inMetas, args);
156 }