d4fe4189c493640f3cf9afbdbef6eeb07ff16b06
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-property-buffer.cpp
1
2 #include <dali/internal/render/renderers/render-property-buffer.h>
3 #include <dali/internal/event/common/property-buffer-impl.h>  // Dali::Internal::PropertyBuffer
4
5 namespace
6 {
7
8 using namespace Dali;
9 using Dali::Property;
10 using Dali::Internal::PropertyImplementationType;
11
12 Dali::GLenum GetPropertyImplementationGlType( Property::Type& propertyType )
13 {
14   Dali::GLenum type = GL_BYTE;
15
16   switch( propertyType )
17   {
18     case Property::NONE:
19     case Property::STRING:
20     case Property::ARRAY:
21     case Property::MAP:
22     case Property::RECTANGLE:
23     case Property::ROTATION:
24     {
25       // types not supported by gl
26       break;
27     }
28     case Property::BOOLEAN:
29     {
30       type = GL_BYTE;
31       break;
32     }
33     case Property::INTEGER:
34     {
35       type = GL_SHORT;
36       break;
37     }
38     case Property::FLOAT:
39     case Property::VECTOR2:
40     case Property::VECTOR3:
41     case Property::VECTOR4:
42     case Property::MATRIX3:
43     case Property::MATRIX:
44     {
45       type = GL_FLOAT;
46       break;
47     }
48   }
49
50   return type;
51 }
52
53 size_t GetPropertyImplementationGlSize( Property::Type& propertyType )
54 {
55   size_t size = 1u;
56
57   switch( propertyType )
58   {
59     case Property::NONE:
60     case Property::STRING:
61     case Property::ARRAY:
62     case Property::MAP:
63     case Property::RECTANGLE:
64     case Property::ROTATION:
65     {
66       // types not supported by gl
67       break;
68     }
69     case Property::BOOLEAN:
70     {
71       size = 1u;
72       break;
73     }
74     case Property::INTEGER:
75     {
76       size = 2u;
77       break;
78     }
79     case Property::FLOAT:
80     case Property::VECTOR2:
81     case Property::VECTOR3:
82     case Property::VECTOR4:
83     case Property::MATRIX3:
84     case Property::MATRIX:
85     {
86       size = 4u;
87       break;
88     }
89   }
90
91   return size;
92 }
93 } //Unnamed namespace
94
95 namespace Dali
96 {
97 namespace Internal
98 {
99 namespace Render
100 {
101
102 PropertyBuffer::PropertyBuffer()
103 :mFormat(NULL),
104  mData(NULL),
105  mGpuBuffer(NULL),
106  mSize(0),
107  mDataChanged(true)
108 {
109 }
110
111 PropertyBuffer::~PropertyBuffer()
112 {
113 }
114
115 void PropertyBuffer::SetFormat( PropertyBuffer::Format* format )
116 {
117   mFormat = format;
118   mDataChanged = true;
119 }
120
121 void PropertyBuffer::SetData( Dali::Vector<char>* data )
122 {
123   mData = data;
124   mDataChanged = true;
125 }
126
127 void PropertyBuffer::SetSize( unsigned int size )
128 {
129   mSize = size;
130   mDataChanged = true;
131 }
132
133
134 bool PropertyBuffer::Update( Context& context, bool isIndexBuffer )
135 {
136   if( !mData || !mFormat || !mSize )
137   {
138     return false;
139   }
140
141   if( !mGpuBuffer || mDataChanged )
142   {
143     if ( ! mGpuBuffer )
144     {
145       mGpuBuffer = new GpuBuffer( context );
146     }
147
148     // Update the GpuBuffer
149     if ( mGpuBuffer )
150     {
151       DALI_ASSERT_DEBUG( mSize && "No data in the property buffer!" );
152
153       const void *data = &((*mData)[0]);
154
155       // Index buffer needs to be unsigned short which is not supported by the property system
156       Vector<unsigned short> ushortData;
157       if( isIndexBuffer )
158       {
159         ushortData.Resize(mSize);
160         const unsigned int* unsignedData = static_cast<const unsigned int*>(data);
161         for( unsigned int i = 0; i < mSize; ++i )
162         {
163           ushortData[i] = unsignedData[i];
164         }
165         data = &(ushortData[0]);
166       }
167
168       mGpuBuffer->UpdateDataBuffer( GetDataSize(), data, GpuBuffer::STATIC_DRAW );
169     }
170
171     mDataChanged = false;
172   }
173
174   return true;
175 }
176
177 void PropertyBuffer::BindBuffer(GpuBuffer::Target target)
178 {
179   if(mGpuBuffer)
180   {
181     mGpuBuffer->Bind(target);
182   }
183 }
184
185 unsigned int PropertyBuffer::EnableVertexAttributes( Context& context, Vector<GLint>& vAttributeLocation, unsigned int locationBase )
186 {
187
188   unsigned int attributeCount = mFormat->components.size();
189
190   GLsizei elementSize = mFormat->size;
191
192   for( unsigned int i = 0; i < attributeCount; ++i )
193   {
194     GLint attributeLocation = vAttributeLocation[i+locationBase];
195     if( attributeLocation != -1 )
196     {
197       context.EnableVertexAttributeArray( attributeLocation );
198
199       GLint attributeSize = mFormat->components[i].size;
200       size_t attributeOffset = mFormat->components[i].offset;
201       Property::Type attributeType = mFormat->components[i].type;
202
203       context.VertexAttribPointer( attributeLocation,
204                                    attributeSize  / GetPropertyImplementationGlSize(attributeType),
205                                    GetPropertyImplementationGlType(attributeType),
206                                    GL_FALSE,  // Not normalized
207                                    elementSize,
208                                    (void*)attributeOffset );
209     }
210   }
211
212   return attributeCount;
213
214 }
215
216 } //Render
217 } //Internal
218 } //Dali