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