Merge "[dali-core] add all X11 colors to Color namespace" into devel/master
[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) 2020 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 #include <dali/internal/common/owner-pointer.h>
25 #include <dali/internal/render/renderers/render-sampler.h>
26 #include <dali/internal/render/gl-resources/gpu-buffer.h>
27 #include <dali/internal/common/const-string.h>
28
29 namespace Dali
30 {
31 namespace Internal
32 {
33 namespace Render
34 {
35
36 class VertexBuffer
37 {
38 public:
39
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    * @brief Bind the property buffer
93    * @param context The context to bind the the buffer
94    * @param[in] target The binding point
95    */
96   void BindBuffer( Context& context, GpuBuffer::Target target );
97
98   /**
99    * Perform the upload of the buffer only when requiered
100    * @param[in] context The GL context
101    */
102   bool Update( Context& context );
103
104   /**
105    * Enable the vertex attributes for each vertex buffer from the corresponding
106    * shader program.
107    * @param[in] context The GL context
108    * @param[in] vAttributeLocation Vector containing attributes location for current program
109    * @param[in] locationBase Index in vAttributeLocation corresponding to the first attribute defined by this buffer
110    */
111   uint32_t EnableVertexAttributes( Context& context, Vector<GLint>& vAttributeLocation, uint32_t locationBase );
112
113   /**
114    * Get the number of attributes present in the buffer
115    * @return The number of attributes stored in this buffer
116    */
117   inline uint32_t GetAttributeCount() const
118   {
119     DALI_ASSERT_DEBUG( mFormat && "Format should be set ");
120     return static_cast<uint32_t>( mFormat->components.size() );
121   }
122
123   /**
124    * Retrieve the i-essim attribute name
125    * @param[in] index The index of the attribute
126    * @return The name of the attribute
127    */
128   inline ConstString GetAttributeName( uint32_t index ) const
129   {
130     DALI_ASSERT_DEBUG( mFormat && "Format should be set ");
131     return mFormat->components[index].name;
132   }
133
134   /**
135    * Retrieve the size of the buffer in bytes
136    * @return The total size of the buffer
137    */
138   inline uint32_t GetDataSize() const
139   {
140     DALI_ASSERT_DEBUG( mFormat && "Format should be set ");
141     return mFormat->size * mSize;
142   }
143
144   /**
145    * Retrieve the size of one element of the buffer
146    * @return The size of one element
147    */
148   inline uint32_t GetElementSize() const
149   {
150     DALI_ASSERT_DEBUG( mFormat && "Format should be set ");
151     return mFormat->size;
152   }
153
154   /**
155    * Retrieve the number of elements in the buffer
156    * @return The total number of elements
157    */
158   inline uint32_t GetElementCount() const
159   {
160     return mSize;
161   }
162
163   /**
164    * Retrieve reference to the data storage vector
165    * @return Reference to the data storage
166    */
167   inline const Dali::Vector< uint8_t >& GetData() const
168   {
169     return *mData.Get();
170   }
171
172   /**
173    * Retrieve data writeable pointer ( direct access to the buffer data )
174    * @return Pointer to data converted to requested type
175    */
176   template <typename T>
177   inline T* GetDataTypedPtr()
178   {
179     Dali::Vector< uint8_t >* data = mData.Release();
180     mData = data;
181     return reinterpret_cast<T*>( &data->operator[]( 0 ) );
182   }
183
184   inline const VertexBuffer::Format* GetFormat() const
185   {
186     return mFormat.Get();
187   }
188
189 private:
190   OwnerPointer< VertexBuffer::Format >  mFormat;    ///< Format of the buffer
191   OwnerPointer< Dali::Vector< uint8_t > > mData;      ///< Data
192   OwnerPointer< GpuBuffer >               mGpuBuffer; ///< Pointer to the GpuBuffer associated with this RenderVertexBuffer
193
194   uint32_t mSize;       ///< Number of Elements in the buffer
195   bool mDataChanged;  ///< Flag to know if data has changed in a frame
196 };
197
198 } // namespace Render
199
200 } // namespace Internal
201
202 } // namespace Dali
203
204
205 #endif //  DALI_INTERNAL_RENDER_VERTEX_BUFFER_H