[dali_1.2.39] Merge branch 'devel/master'
[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, size_t size )
122 {
123   mData = data;
124   mSize = size;
125   mDataChanged = true;
126 }
127
128 bool PropertyBuffer::Update( Context& context )
129 {
130   if( !mData || !mFormat || !mSize )
131   {
132     return false;
133   }
134
135   if( !mGpuBuffer || mDataChanged )
136   {
137     if ( ! mGpuBuffer )
138     {
139       mGpuBuffer = new GpuBuffer( context );
140     }
141
142     // Update the GpuBuffer
143     if ( mGpuBuffer )
144     {
145       DALI_ASSERT_DEBUG( mSize && "No data in the property buffer!" );
146       mGpuBuffer->UpdateDataBuffer( GetDataSize(), &((*mData)[0]), GpuBuffer::STATIC_DRAW, GpuBuffer::ARRAY_BUFFER );
147     }
148
149     mDataChanged = false;
150   }
151
152   return true;
153 }
154
155 void PropertyBuffer::BindBuffer(GpuBuffer::Target target)
156 {
157   if(mGpuBuffer)
158   {
159     mGpuBuffer->Bind(target);
160   }
161 }
162
163 unsigned int PropertyBuffer::EnableVertexAttributes( Context& context, Vector<GLint>& vAttributeLocation, unsigned int locationBase )
164 {
165
166   unsigned int attributeCount = mFormat->components.size();
167
168   GLsizei elementSize = mFormat->size;
169
170   for( unsigned int i = 0; i < attributeCount; ++i )
171   {
172     GLint attributeLocation = vAttributeLocation[i+locationBase];
173     if( attributeLocation != -1 )
174     {
175       context.EnableVertexAttributeArray( attributeLocation );
176
177       const GLint attributeSize = mFormat->components[i].size;
178       size_t attributeOffset = mFormat->components[i].offset;
179       const Property::Type attributeType = mFormat->components[i].type;
180
181       context.VertexAttribPointer( attributeLocation,
182                                    attributeSize  / GetPropertyImplementationGlSize(attributeType),
183                                    GetPropertyImplementationGlType(attributeType),
184                                    GL_FALSE,  // Not normalized
185                                    elementSize,
186                                    (void*)attributeOffset );
187     }
188   }
189
190   return attributeCount;
191
192 }
193
194 } //Render
195 } //Internal
196 } //Dali