Support multiple window rendering
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-property-buffer.h
1 #ifndef DALI_INTERNAL_RENDER_PROPERTY_BUFFER_H
2 #define DALI_INTERNAL_RENDER_PROPERTY_BUFFER_H
3
4 /*
5  * Copyright (c) 2018 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
28 namespace Dali
29 {
30 namespace Internal
31 {
32 namespace Render
33 {
34
35 class PropertyBuffer
36 {
37 public:
38
39   struct Component
40   {
41     std::string    name;
42     uint32_t       offset;
43     uint32_t       size;
44     Property::Type type;
45   };
46
47   /**
48    * Structure that holds the meta-data of the format of PropertyBuffer.
49    */
50   struct Format
51   {
52     std::vector<Component> components;
53     uint32_t               size;
54   };
55
56   /**
57    * @brief Default constructor
58    */
59   PropertyBuffer();
60
61   /**
62    * @brief Destructor
63    */
64   ~PropertyBuffer();
65
66   /**
67    * @brief Set the format of the buffer
68    *
69    * This function takes ownership of the pointer
70    *
71    * @param[in] format The format for the PropertyBuffer
72    */
73   void SetFormat( PropertyBuffer::Format* format );
74
75   /**
76    * @brief Set the data of the PropertyBuffer
77    *
78    * This function takes ownership of the pointer
79    * @param[in] data The new data of the PropertyBuffer
80    * @param[in] size The new size of the buffer
81    */
82   void SetData( Dali::Vector<uint8_t>* data, uint32_t size );
83
84   /**
85    * @brief Set the number of elements
86    * @param[in] size The number of elements
87    */
88   void SetSize( uint32_t size );
89
90   /**
91    * @brief Bind the property buffer
92    * @param context The context to bind the the buffer
93    * @param[in] target The binding point
94    */
95   void BindBuffer( Context& context, GpuBuffer::Target target );
96
97   /**
98    * Perform the upload of the buffer only when requiered
99    * @param[in] context The GL context
100    */
101   bool Update( Context& context );
102
103   /**
104    * Enable the vertex attributes for each vertex buffer from the corresponding
105    * shader program.
106    * @param[in] context The GL context
107    * @param[in] vAttributeLocation Vector containing attributes location for current program
108    * @param[in] locationBase Index in vAttributeLocation corresponding to the first attribute defined by this buffer
109    */
110   uint32_t EnableVertexAttributes( Context& context, Vector<GLint>& vAttributeLocation, uint32_t locationBase );
111
112   /**
113    * Get the number of attributes present in the buffer
114    * @return The number of attributes stored in this buffer
115    */
116   inline uint32_t GetAttributeCount() const
117   {
118     DALI_ASSERT_DEBUG( mFormat && "Format should be set ");
119     return static_cast<uint32_t>( mFormat->components.size() );
120   }
121
122   /**
123    * Retrieve the i-essim attribute name
124    * @param[in] index The index of the attribute
125    * @return The name of the attribute
126    */
127   inline const std::string& GetAttributeName( uint32_t index ) const
128   {
129     DALI_ASSERT_DEBUG( mFormat && "Format should be set ");
130     return mFormat->components[index].name;
131   }
132
133   /**
134    * Retrieve the size of the buffer in bytes
135    * @return The total size of the buffer
136    */
137   inline uint32_t GetDataSize() const
138   {
139     DALI_ASSERT_DEBUG( mFormat && "Format should be set ");
140     return mFormat->size * mSize;
141   }
142
143   /**
144    * Retrieve the size of one element of the buffer
145    * @return The size of one element
146    */
147   inline uint32_t GetElementSize() const
148   {
149     DALI_ASSERT_DEBUG( mFormat && "Format should be set ");
150     return mFormat->size;
151   }
152
153   /**
154    * Retrieve the number of elements in the buffer
155    * @return The total number of elements
156    */
157   inline uint32_t GetElementCount() const
158   {
159     return mSize;
160   }
161
162   /**
163    * Retrieve reference to the data storage vector
164    * @return Reference to the data storage
165    */
166   inline const Dali::Vector< uint8_t >& GetData() const
167   {
168     return *mData.Get();
169   }
170
171   /**
172    * Retrieve data writeable pointer ( direct access to the buffer data )
173    * @return Pointer to data converted to requested type
174    */
175   template <typename T>
176   inline T* GetDataTypedPtr()
177   {
178     Dali::Vector< char >* data = mData.Release();
179     mData = data;
180     return reinterpret_cast<T*>( &data->operator[]( 0 ) );
181   }
182
183   inline const PropertyBuffer::Format* GetFormat() const
184   {
185     return mFormat.Get();
186   }
187
188 private:
189   OwnerPointer< PropertyBuffer::Format >  mFormat;    ///< Format of the buffer
190   OwnerPointer< Dali::Vector< uint8_t > > mData;      ///< Data
191   OwnerPointer< GpuBuffer >               mGpuBuffer; ///< Pointer to the GpuBuffer associated with this RenderPropertyBuffer
192
193   uint32_t mSize;       ///< Number of Elements in the buffer
194   bool mDataChanged;  ///< Flag to know if data has changed in a frame
195 };
196
197 } // namespace Render
198
199 } // namespace Internal
200
201 } // namespace Dali
202
203
204 #endif //  DALI_INTERNAL_RENDER_PROPERTY_BUFFER_H