Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / fluid / modules / gapi / include / opencv2 / gapi / fluid / gfluidbuffer.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_BUFFER_HPP
9 #define OPENCV_GAPI_FLUID_BUFFER_HPP
10
11 #include <list>
12 #include <numeric> // accumulate
13 #include <ostream> // ostream
14 #include <cstdint> // uint8_t
15
16 #include <opencv2/gapi/opencv_includes.hpp>
17 #include <opencv2/gapi/own/mat.hpp>
18 #include <opencv2/gapi/gmat.hpp>
19
20 #include "opencv2/gapi/util/optional.hpp"
21 #include "opencv2/gapi/own/scalar.hpp"
22 #include "opencv2/gapi/own/mat.hpp"
23
24 namespace cv {
25 namespace gapi {
26 namespace fluid {
27
28 struct Border
29 {
30 #if !defined(GAPI_STANDALONE)
31     // This constructor is required to support existing kernels which are part of G-API
32     Border(int _type, cv::Scalar _val) : type(_type), value(to_own(_val)) {};
33 #endif // !defined(GAPI_STANDALONE)
34     Border(int _type, cv::gapi::own::Scalar _val) : type(_type), value(_val) {};
35     int type;
36     cv::gapi::own::Scalar value;
37 };
38
39 using BorderOpt = util::optional<Border>;
40
41 bool operator == (const Border& b1, const Border& b2);
42
43 class GAPI_EXPORTS Buffer;
44
45 class GAPI_EXPORTS View
46 {
47 public:
48     struct Cache
49     {
50         std::vector<const uint8_t*> m_linePtrs;
51         GMatDesc m_desc;
52         int m_border_size = 0;
53
54         inline const uint8_t* linePtr(int index) const
55         {
56             return m_linePtrs[index + m_border_size];
57         }
58     };
59
60     View() = default;
61
62     const inline uint8_t* InLineB(int index) const // -(w-1)/2...0...+(w-1)/2 for Filters
63     {
64         return m_cache->linePtr(index);
65     }
66
67     template<typename T> const inline T* InLine(int i) const
68     {
69         const uint8_t* ptr = this->InLineB(i);
70         return reinterpret_cast<const T*>(ptr);
71     }
72
73     inline operator bool() const { return m_priv != nullptr; }
74     bool ready() const;
75     inline int length() const { return m_cache->m_desc.size.width; }
76     int y() const;
77
78     inline const GMatDesc& meta() const { return m_cache->m_desc; }
79
80     class GAPI_EXPORTS Priv;      // internal use only
81     Priv& priv();               // internal use only
82     const Priv& priv() const;   // internal use only
83
84     View(Priv* p);
85
86 private:
87     std::shared_ptr<Priv> m_priv;
88     const Cache* m_cache;
89 };
90
91 class GAPI_EXPORTS Buffer
92 {
93 public:
94     struct Cache
95     {
96         std::vector<uint8_t*> m_linePtrs;
97         GMatDesc m_desc;
98     };
99
100     // Default constructor (executable creation stage,
101     // all following initialization performed in Priv::init())
102     Buffer();
103     // Scratch constructor (user kernels)
104     Buffer(const cv::GMatDesc &desc);
105
106     // Constructor for intermediate buffers (for tests)
107     Buffer(const cv::GMatDesc &desc,
108            int max_line_consumption, int border_size,
109            int skew,
110            int wlpi,
111            BorderOpt border);
112     // Constructor for in/out buffers (for tests)
113     Buffer(const cv::gapi::own::Mat &data, bool is_input);
114
115     inline uint8_t* OutLineB(int index = 0)
116     {
117         return m_cache->m_linePtrs[index];
118     }
119
120     template<typename T> inline T* OutLine(int index = 0)
121     {
122         uint8_t* ptr = this->OutLineB(index);
123         return reinterpret_cast<T*>(ptr);
124     }
125
126     int y() const;
127
128     int linesReady() const;
129     void debug(std::ostream &os) const;
130     inline int length() const { return m_cache->m_desc.size.width; }
131     int lpi() const;  // LPI for WRITER
132
133     inline const GMatDesc& meta() const { return m_cache->m_desc; }
134
135     View mkView(int borderSize, bool ownStorage);
136
137     class GAPI_EXPORTS Priv;      // internal use only
138     Priv& priv();               // internal use only
139     const Priv& priv() const;   // internal use only
140
141 private:
142     std::shared_ptr<Priv> m_priv;
143     const Cache* m_cache;
144 };
145
146 } // namespace cv::gapi::fluid
147 } // namespace cv::gapi
148 } // namespace cv
149
150 #endif // OPENCV_GAPI_FLUID_BUFFER_HPP