[Tizen] Revert "Support multiple window rendering"
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-property-buffer.cpp
1 /*
2  * Copyright (c) 2018 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 Dali::Property;
25 using Dali::Internal::PropertyImplementationType;
26
27 Dali::GLenum GetPropertyImplementationGlType( Property::Type propertyType )
28 {
29   Dali::GLenum type = GL_BYTE;
30
31   switch( propertyType )
32   {
33     case Property::NONE:
34     case Property::STRING:
35     case Property::ARRAY:
36     case Property::MAP:
37     case Property::EXTENTS:
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 Dali::GLint GetPropertyImplementationGlSize( Property::Type propertyType )
70 {
71   Dali::GLint 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::EXTENTS:
80     case Property::RECTANGLE:
81     case Property::ROTATION:
82     {
83       // types not supported by gl
84       break;
85     }
86     case Property::BOOLEAN:
87     {
88       size = 1u;
89       break;
90     }
91     case Property::INTEGER:
92     {
93       size = 2u;
94       break;
95     }
96     case Property::FLOAT:
97     case Property::VECTOR2:
98     case Property::VECTOR3:
99     case Property::VECTOR4:
100     case Property::MATRIX3:
101     case Property::MATRIX:
102     {
103       size = 4u;
104       break;
105     }
106   }
107
108   return size;
109 }
110 } //Unnamed namespace
111
112 namespace Dali
113 {
114 namespace Internal
115 {
116 namespace Render
117 {
118
119 PropertyBuffer::PropertyBuffer()
120 :mFormat(NULL),
121  mData(NULL),
122  mGpuBuffer(NULL),
123  mSize(0),
124  mDataChanged(true)
125 {
126 }
127
128 PropertyBuffer::~PropertyBuffer()
129 {
130 }
131
132 void PropertyBuffer::SetFormat( PropertyBuffer::Format* format )
133 {
134   mFormat = format;
135   mDataChanged = true;
136 }
137
138 void PropertyBuffer::SetData( Dali::Vector<uint8_t>* data, uint32_t size )
139 {
140   mData = data;
141   mSize = size;
142   mDataChanged = true;
143 }
144
145 bool PropertyBuffer::Update( Context& context )
146 {
147   if( !mData || !mFormat || !mSize )
148   {
149     return false;
150   }
151
152   if( !mGpuBuffer || mDataChanged )
153   {
154     if ( ! mGpuBuffer )
155     {
156       mGpuBuffer = new GpuBuffer( context );
157     }
158
159     // Update the GpuBuffer
160     if ( mGpuBuffer )
161     {
162       DALI_ASSERT_DEBUG( mSize && "No data in the property buffer!" );
163       mGpuBuffer->UpdateDataBuffer( GetDataSize(), &((*mData)[0]), GpuBuffer::STATIC_DRAW, GpuBuffer::ARRAY_BUFFER );
164     }
165
166     mDataChanged = false;
167   }
168
169   return true;
170 }
171
172 void PropertyBuffer::BindBuffer(GpuBuffer::Target target)
173 {
174   if(mGpuBuffer)
175   {
176     mGpuBuffer->Bind(target);
177   }
178 }
179
180 uint32_t PropertyBuffer::EnableVertexAttributes( Context& context, Vector<GLint>& vAttributeLocation, uint32_t locationBase )
181 {
182   const uint32_t attributeCount = static_cast<uint32_t>( mFormat->components.size() );
183
184   GLsizei elementSize = mFormat->size;
185
186   for( uint32_t 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       uint32_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 } //Render
210 } //Internal
211 } //Dali