Cleanup in Aisle #1
[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/common/vector-wrapper.h>
22 #include <dali/public-api/object/property.h>
23
24 #include <dali/graphics-api/graphics-types.h>
25 #include <dali/internal/common/const-string.h>
26 #include <dali/internal/common/owner-pointer.h>
27 #include <dali/internal/render/renderers/gpu-buffer.h>
28
29 namespace Dali
30 {
31 namespace Internal
32 {
33 namespace Render
34 {
35 class VertexBuffer
36 {
37 public:
38   struct Component
39   {
40     ConstString    name;
41     uint32_t       offset;
42     uint32_t       size;
43     Property::Type type;
44   };
45
46   /**
47    * Structure that holds the meta-data of the format of VertexBuffer.
48    */
49   struct Format
50   {
51     std::vector<Component> components;
52     uint32_t               size;
53   };
54
55   /**
56    * @brief Default constructor
57    */
58   VertexBuffer();
59
60   /**
61    * @brief Destructor
62    */
63   ~VertexBuffer();
64
65   /**
66    * @brief Set the format of the buffer
67    *
68    * This function takes ownership of the pointer
69    *
70    * @param[in] format The format for the VertexBuffer
71    */
72   void SetFormat(VertexBuffer::Format* format);
73
74   /**
75    * @brief Set the data of the VertexBuffer
76    *
77    * This function takes ownership of the pointer
78    * @param[in] data The new data of the VertexBuffer
79    * @param[in] size The new size of the buffer
80    */
81   void SetData(Dali::Vector<uint8_t>* data, uint32_t size);
82
83   /**
84    * @brief Set the number of elements
85    * @param[in] size The number of elements
86    */
87   void SetSize(uint32_t size);
88
89   /**
90    * Perform the upload of the buffer only when required
91    * @param[in] graphicsController The controller
92    */
93   bool Update(Graphics::Controller& graphicsController);
94
95   /**
96    * Get the number of attributes present in the buffer
97    * @return The number of attributes stored in this buffer
98    */
99   inline uint32_t GetAttributeCount() const
100   {
101     DALI_ASSERT_DEBUG(mFormat && "Format should be set ");
102     return static_cast<uint32_t>(mFormat->components.size());
103   }
104
105   /**
106    * Retrieve the i-essim attribute name
107    * @param[in] index The index of the attribute
108    * @return The name of the attribute
109    */
110   inline ConstString GetAttributeName(uint32_t index) const
111   {
112     DALI_ASSERT_DEBUG(mFormat && "Format should be set ");
113     return mFormat->components[index].name;
114   }
115
116   /**
117    * Retrieve the size of the buffer in bytes
118    * @return The total size of the buffer
119    */
120   inline uint32_t GetDataSize() const
121   {
122     DALI_ASSERT_DEBUG(mFormat && "Format should be set ");
123     return mFormat->size * mSize;
124   }
125
126   /**
127    * Retrieve the size of one element of the buffer
128    * @return The size of one element
129    */
130   inline uint32_t GetElementSize() const
131   {
132     DALI_ASSERT_DEBUG(mFormat && "Format should be set ");
133     return mFormat->size;
134   }
135
136   /**
137    * Retrieve the number of elements in the buffer
138    * @return The total number of elements
139    */
140   inline uint32_t GetElementCount() const
141   {
142     return mSize;
143   }
144
145   /**
146    * Retrieve reference to the data storage vector
147    * @return Reference to the data storage
148    */
149   inline const Dali::Vector<uint8_t>& GetData() const
150   {
151     return *mData.Get();
152   }
153
154   /**
155    * Retrieve data writeable pointer ( direct access to the buffer data )
156    * @return Pointer to data converted to requested type
157    */
158   template<typename T>
159   inline T* GetDataTypedPtr()
160   {
161     Dali::Vector<uint8_t>* data = mData.Release();
162     mData                       = data;
163     return reinterpret_cast<T*>(&data->operator[](0));
164   }
165
166   inline const VertexBuffer::Format* GetFormat() const
167   {
168     return mFormat.Get();
169   }
170
171   inline const GpuBuffer* GetGpuBuffer() const
172   {
173     return mGpuBuffer.Get();
174   }
175
176 private:
177   OwnerPointer<VertexBuffer::Format>   mFormat;    ///< Format of the buffer
178   OwnerPointer<Dali::Vector<uint8_t> > mData;      ///< Data
179   OwnerPointer<GpuBuffer>              mGpuBuffer; ///< Pointer to the GpuBuffer associated with this RenderVertexBuffer
180
181   uint32_t mSize;        ///< Number of Elements in the buffer
182   bool     mDataChanged; ///< Flag to know if data has changed in a frame
183 };
184
185 } // namespace Render
186
187 } // namespace Internal
188
189 } // namespace Dali
190
191 #endif //  DALI_INTERNAL_RENDER_VERTEX_BUFFER_H