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