Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / fluid / modules / gapi / src / backends / fluid / gfluidbackend.hpp
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 #ifndef OPENCV_GAPI_FLUID_BACKEND_HPP
9 #define OPENCV_GAPI_FLUID_BACKEND_HPP
10
11 #include "opencv2/gapi/garg.hpp"
12 #include "opencv2/gapi/gproto.hpp"
13 #include "opencv2/gapi/fluid/gfluidkernel.hpp"
14 #include "opencv2/gapi/fluid/gfluidbuffer.hpp"
15
16 // PRIVATE STUFF!
17 #include "backends/common/gbackend.hpp"
18 #include "compiler/gislandmodel.hpp"
19
20 namespace cv { namespace gimpl {
21
22 struct FluidUnit
23 {
24     static const char *name() { return "FluidUnit"; }
25     GFluidKernel k;
26     gapi::fluid::BorderOpt border;
27     int border_size;
28     int line_consumption;
29     double ratio;
30 };
31
32 struct FluidUseOwnBorderBuffer
33 {
34     static const char *name() { return "FluidUseOwnBorderBuffer"; }
35     bool use;
36 };
37
38 struct FluidData
39 {
40     static const char *name() { return "FluidData"; }
41
42     // FIXME: This structure starts looking like "FluidBuffer" meta
43     int  latency         = 0;
44     int  skew            = 0;
45     int  max_consumption = 1;
46     int  border_size     = 0;
47     int  lpi_write       = 1;
48     bool internal        = false; // is node internal to any fluid island
49     gapi::fluid::BorderOpt border;
50 };
51
52 struct FluidAgent
53 {
54 public:
55     virtual ~FluidAgent() = default;
56     FluidAgent(const ade::Graph &g, ade::NodeHandle nh);
57
58     GFluidKernel k;
59     ade::NodeHandle op_handle; // FIXME: why it is here??//
60     std::string op_name;
61
62     // <  0 - not a buffer
63     // >= 0 - a buffer with RcID
64     std::vector<int> in_buffer_ids;
65     std::vector<int> out_buffer_ids;
66
67     cv::GArgs in_args;
68     std::vector<cv::gapi::fluid::View>   in_views; // sparce list of IN views
69     std::vector<cv::gapi::fluid::Buffer*> out_buffers;
70
71     // FIXME Current assumption is that outputs have EQUAL SIZES
72     int m_outputLines = 0;
73     int m_producedLines = 0;
74
75     // Execution methods
76     void reset();
77     bool canWork() const;
78     bool canRead() const;
79     bool canWrite() const;
80     void doWork();
81     bool done() const;
82
83     void debug(std::ostream& os);
84
85     // FIXME:
86     // refactor (implement a more solid replacement or
87     // drop this method completely)
88     virtual void setRatio(double ratio) = 0;
89
90 private:
91     // FIXME!!!
92     // move to another class
93     virtual int firstWindow() const = 0;
94     virtual std::pair<int,int> linesReadAndnextWindow() const = 0;
95 };
96
97 class GFluidExecutable final: public GIslandExecutable
98 {
99     const ade::Graph &m_g;
100     GModel::ConstGraph m_gm;
101
102     std::vector<std::unique_ptr<FluidAgent>> m_agents;
103     std::vector<cv::gapi::fluid::Buffer> m_buffers;
104
105     std::vector<FluidAgent*> m_script;
106
107     using Magazine = detail::magazine<cv::gapi::own::Scalar>;
108     Magazine m_res;
109
110     std::size_t m_num_int_buffers; // internal buffers counter (m_buffers - num_scratch)
111     std::vector<std::size_t> m_scratch_users;
112
113     std::unordered_map<int, std::size_t> m_id_map; // GMat id -> buffer idx map
114     std::map<std::size_t, ade::NodeHandle> m_all_gmat_ids;
115
116     void bindInArg (const RcDesc &rc, const GRunArg &arg);
117     void bindOutArg(const RcDesc &rc, const GRunArgP &arg);
118     void packArg   (GArg &in_arg, const GArg &op_arg);
119
120     void initBufferRois(std::vector<int>& readStarts, std::vector<cv::gapi::own::Rect>& rois, const std::vector<gapi::own::Rect> &out_rois);
121     void makeReshape(const std::vector<cv::gapi::own::Rect>& out_rois);
122
123 public:
124     GFluidExecutable(const ade::Graph &g,
125                      const std::vector<ade::NodeHandle> &nodes,
126                      const std::vector<cv::gapi::own::Rect> &outputRois);
127
128     virtual inline bool canReshape() const override { return true; }
129     virtual void reshape(ade::Graph& g, const GCompileArgs& args) override;
130
131     virtual void run(std::vector<InObj>  &&input_objs,
132                      std::vector<OutObj> &&output_objs) override;
133 };
134 }} // cv::gimpl
135
136
137 #endif // OPENCV_GAPI_FLUID_BACKEND_HPP