[dali_1.2.62] Merge branch 'devel/master'
[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::EXTENTS:
39     case Property::RECTANGLE:
40     case Property::ROTATION:
41     {
42       // types not supported by gl
43       break;
44     }
45     case Property::BOOLEAN:
46     {
47       type = GL_BYTE;
48       break;
49     }
50     case Property::INTEGER:
51     {
52       type = GL_SHORT;
53       break;
54     }
55     case Property::FLOAT:
56     case Property::VECTOR2:
57     case Property::VECTOR3:
58     case Property::VECTOR4:
59     case Property::MATRIX3:
60     case Property::MATRIX:
61     {
62       type = GL_FLOAT;
63       break;
64     }
65   }
66
67   return type;
68 }
69
70 size_t GetPropertyImplementationGlSize( Property::Type propertyType )
71 {
72   size_t size = 1u;
73
74   switch( propertyType )
75   {
76     case Property::NONE:
77     case Property::STRING:
78     case Property::ARRAY:
79     case Property::MAP:
80     case Property::EXTENTS:
81     case Property::RECTANGLE:
82     case Property::ROTATION:
83     {
84       // types not supported by gl
85       break;
86     }
87     case Property::BOOLEAN:
88     {
89       size = 1u;
90       break;
91     }
92     case Property::INTEGER:
93     {
94       size = 2u;
95       break;
96     }
97     case Property::FLOAT:
98     case Property::VECTOR2:
99     case Property::VECTOR3:
100     case Property::VECTOR4:
101     case Property::MATRIX3:
102     case Property::MATRIX:
103     {
104       size = 4u;
105       break;
106     }
107   }
108
109   return size;
110 }
111 } //Unnamed namespace
112
113 namespace Dali
114 {
115 namespace Internal
116 {
117 namespace Render
118 {
119
120 PropertyBuffer::PropertyBuffer()
121 :mFormat(NULL),
122  mData(NULL),
123  mGpuBuffer(NULL),
124  mSize(0),
125  mDataChanged(true)
126 {
127 }
128
129 PropertyBuffer::~PropertyBuffer()
130 {
131 }
132
133 void PropertyBuffer::SetFormat( PropertyBuffer::Format* format )
134 {
135   mFormat = format;
136   mDataChanged = true;
137 }
138
139 void PropertyBuffer::SetData( Dali::Vector<char>* data, size_t size )
140 {
141   mData = data;
142   mSize = size;
143   mDataChanged = true;
144 }
145
146 bool PropertyBuffer::Update( Context& context )
147 {
148   if( !mData || !mFormat || !mSize )
149   {
150     return false;
151   }
152
153   if( !mGpuBuffer || mDataChanged )
154   {
155     if ( ! mGpuBuffer )
156     {
157       mGpuBuffer = new GpuBuffer( context );
158     }
159
160     // Update the GpuBuffer
161     if ( mGpuBuffer )
162     {
163       DALI_ASSERT_DEBUG( mSize && "No data in the property buffer!" );
164       mGpuBuffer->UpdateDataBuffer( GetDataSize(), &((*mData)[0]), GpuBuffer::STATIC_DRAW, GpuBuffer::ARRAY_BUFFER );
165     }
166
167     mDataChanged = false;
168   }
169
170   return true;
171 }
172
173 void PropertyBuffer::BindBuffer(GpuBuffer::Target target)
174 {
175   if(mGpuBuffer)
176   {
177     mGpuBuffer->Bind(target);
178   }
179 }
180
181 unsigned int PropertyBuffer::EnableVertexAttributes( Context& context, Vector<GLint>& vAttributeLocation, unsigned int locationBase )
182 {
183
184   unsigned int attributeCount = mFormat->components.size();
185
186   GLsizei elementSize = mFormat->size;
187
188   for( unsigned int i = 0; i < attributeCount; ++i )
189   {
190     GLint attributeLocation = vAttributeLocation[i+locationBase];
191     if( attributeLocation != -1 )
192     {
193       context.EnableVertexAttributeArray( attributeLocation );
194
195       const GLint attributeSize = mFormat->components[i].size;
196       size_t attributeOffset = mFormat->components[i].offset;
197       const Property::Type attributeType = mFormat->components[i].type;
198
199       context.VertexAttribPointer( attributeLocation,
200                                    attributeSize  / GetPropertyImplementationGlSize(attributeType),
201                                    GetPropertyImplementationGlType(attributeType),
202                                    GL_FALSE,  // Not normalized
203                                    elementSize,
204                                    reinterpret_cast< void* >( attributeOffset ) );
205     }
206   }
207
208   return attributeCount;
209
210 }
211
212 } //Render
213 } //Internal
214 } //Dali