Merge branch 'devel/master' into devel/graphics
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-vertex-buffer.h
1 #ifndef DALI_INTERNAL_RENDER_VERTEX_BUFFER_H
2 #define DALI_INTERNAL_RENDER_VERTEX_BUFFER_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 // INTERNAL INCLUDES
21 #include <dali/public-api/actors/sampling.h>
22 #include <dali/public-api/common/vector-wrapper.h>
23 #include <dali/public-api/rendering/sampler.h>
24
25 #include <dali/graphics-api/graphics-types.h>
26 #include <dali/internal/common/const-string.h>
27 #include <dali/internal/common/owner-pointer.h>
28 #include <dali/internal/render/gl-resources/gpu-buffer.h>
29 #include <dali/internal/render/renderers/render-sampler.h>
30
31 namespace Dali
32 {
33 namespace Internal
34 {
35 namespace Render
36 {
37 class VertexBuffer
38 {
39 public:
40   struct Component
41   {
42     ConstString    name;
43     uint32_t       offset;
44     uint32_t       size;
45     Property::Type type;
46   };
47
48   /**
49    * Structure that holds the meta-data of the format of VertexBuffer.
50    */
51   struct Format
52   {
53     std::vector<Component> components;
54     uint32_t               size;
55   };
56
57   /**
58    * @brief Default constructor
59    */
60   VertexBuffer();
61
62   /**
63    * @brief Destructor
64    */
65   ~VertexBuffer();
66
67   /**
68    * @brief Set the format of the buffer
69    *
70    * This function takes ownership of the pointer
71    *
72    * @param[in] format The format for the VertexBuffer
73    */
74   void SetFormat(VertexBuffer::Format* format);
75
76   /**
77    * @brief Set the data of the VertexBuffer
78    *
79    * This function takes ownership of the pointer
80    * @param[in] data The new data of the VertexBuffer
81    * @param[in] size The new size of the buffer
82    */
83   void SetData(Dali::Vector<uint8_t>* data, uint32_t size);
84
85   /**
86    * @brief Set the number of elements
87    * @param[in] size The number of elements
88    */
89   void SetSize(uint32_t size);
90
91   /**
92    * Perform the upload of the buffer only when required
93    * @param[in] graphicsController The controller
94    */
95   bool Update(Graphics::Controller& graphicsController);
96
97   /**
98    * Get the number of attributes present in the buffer
99    * @return The number of attributes stored in this buffer
100    */
101   inline uint32_t GetAttributeCount() const
102   {
103     DALI_ASSERT_DEBUG(mFormat && "Format should be set ");
104     return static_cast<uint32_t>(mFormat->components.size());
105   }
106
107   /**
108    * Retrieve the i-essim attribute name
109    * @param[in] index The index of the attribute
110    * @return The name of the attribute
111    */
112   inline ConstString GetAttributeName(uint32_t index) const
113   {
114     DALI_ASSERT_DEBUG(mFormat && "Format should be set ");
115     return mFormat->components[index].name;
116   }
117
118   /**
119    * Retrieve the size of the buffer in bytes
120    * @return The total size of the buffer
121    */
122   inline uint32_t GetDataSize() const
123   {
124     DALI_ASSERT_DEBUG(mFormat && "Format should be set ");
125     return mFormat->size * mSize;
126   }
127
128   /**
129    * Retrieve the size of one element of the buffer
130    * @return The size of one element
131    */
132   inline uint32_t GetElementSize() const
133   {
134     DALI_ASSERT_DEBUG(mFormat && "Format should be set ");
135     return mFormat->size;
136   }
137
138   /**
139    * Retrieve the number of elements in the buffer
140    * @return The total number of elements
141    */
142   inline uint32_t GetElementCount() const
143   {
144     return mSize;
145   }
146
147   /**
148    * Retrieve reference to the data storage vector
149    * @return Reference to the data storage
150    */
151   inline const Dali::Vector<uint8_t>& GetData() const
152   {
153     return *mData.Get();
154   }
155
156   /**
157    * Retrieve data writeable pointer ( direct access to the buffer data )
158    * @return Pointer to data converted to requested type
159    */
160   template<typename T>
161   inline T* GetDataTypedPtr()
162   {
163     Dali::Vector<uint8_t>* data = mData.Release();
164     mData                       = data;
165     return reinterpret_cast<T*>(&data->operator[](0));
166   }
167
168   inline const VertexBuffer::Format* GetFormat() const
169   {
170     return mFormat.Get();
171   }
172
173   inline const GpuBuffer* GetGpuBuffer() const
174   {
175     return mGpuBuffer.Get();
176   }
177
178 private:
179   OwnerPointer<VertexBuffer::Format>   mFormat;    ///< Format of the buffer
180   OwnerPointer<Dali::Vector<uint8_t> > mData;      ///< Data
181   OwnerPointer<GpuBuffer>              mGpuBuffer; ///< Pointer to the GpuBuffer associated with this RenderVertexBuffer
182
183   uint32_t mSize;        ///< Number of Elements in the buffer
184   bool     mDataChanged; ///< Flag to know if data has changed in a frame
185 };
186
187 } // namespace Render
188
189 } // namespace Internal
190
191 } // namespace Dali
192
193 #endif //  DALI_INTERNAL_RENDER_VERTEX_BUFFER_H