70c5edca68a5cb8c2cdc9b2f22266408448363f6
[platform/core/uifw/dali-core.git] / dali / internal / event / common / property-buffer-impl.cpp
1 /*
2  * Copyright (c) 2016 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 // CLASS HEADER
19 #include <dali/internal/event/common/property-buffer-impl.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/rendering/property-buffer.h>
23 #include <dali/internal/event/common/stage-impl.h>
24 #include <dali/internal/update/manager/update-manager.h>
25
26 namespace Dali
27 {
28 namespace Internal
29 {
30
31 namespace
32 {
33
34 /**
35  * Calculate the alignment requirements of a type
36  *
37  * This is used to calculate the memory alignment requirements of a type
38  * It creates a structure with a dummy char and a member of the type we want to check
39  * this will cause the second member to be aligned by it's alignment requirement.
40  */
41 template<Property::Type type>
42 struct PropertyImplementationTypeAlignment
43 {
44   // Create a structure that forces alignment of the data type
45   struct TestStructure
46   {
47     char oneChar;  ///< Member with sizeof() == 1
48     typename PropertyImplementationType<type>::Type data;
49   };
50   enum { VALUE = offsetof( TestStructure, data ) };
51 };
52
53 unsigned int GetPropertyImplementationAlignment( Property::Type& propertyType )
54 {
55   unsigned int alignment = 0u;
56
57   switch( propertyType )
58   {
59     case Property::NONE:
60     case Property::STRING:
61     case Property::ARRAY:
62     case Property::MAP:
63     {
64       DALI_ASSERT_ALWAYS( false && "No size for properties with no type, or dynamic sizes" );
65       break;
66     }
67     case Property::BOOLEAN:
68     {
69       alignment = PropertyImplementationTypeAlignment< Property::BOOLEAN >::VALUE;
70       break;
71     }
72     case Property::INTEGER:
73     {
74       alignment = PropertyImplementationTypeAlignment< Property::INTEGER >::VALUE;
75       break;
76     }
77     case Property::FLOAT:
78     {
79       alignment = PropertyImplementationTypeAlignment< Property::FLOAT >::VALUE;
80       break;
81     }
82     case Property::VECTOR2:
83     {
84       alignment = PropertyImplementationTypeAlignment< Property::VECTOR2 >::VALUE;
85       break;
86     }
87     case Property::VECTOR3:
88     {
89       alignment = PropertyImplementationTypeAlignment< Property::VECTOR3 >::VALUE;
90       break;
91     }
92     case Property::VECTOR4:
93     {
94       alignment = PropertyImplementationTypeAlignment< Property::VECTOR4 >::VALUE;
95       break;
96     }
97     case Property::MATRIX3:
98     {
99       alignment = PropertyImplementationTypeAlignment< Property::MATRIX3 >::VALUE;
100       break;
101     }
102     case Property::MATRIX:
103     {
104       alignment = PropertyImplementationTypeAlignment< Property::MATRIX >::VALUE;
105       break;
106     }
107     case Property::RECTANGLE:
108     {
109       alignment = PropertyImplementationTypeAlignment< Property::RECTANGLE >::VALUE;
110       break;
111     }
112     case Property::ROTATION:
113     {
114       alignment = PropertyImplementationTypeAlignment< Property::ROTATION >::VALUE;
115       break;
116     }
117   }
118
119   return alignment;
120 }
121
122 } // unnamed namespace
123
124 PropertyBufferPtr PropertyBuffer::New( Dali::Property::Map& format )
125 {
126   DALI_ASSERT_ALWAYS( format.Count() && "Format cannot be empty." );
127
128   PropertyBufferPtr propertyBuffer( new PropertyBuffer() );
129   propertyBuffer->Initialize( format );
130
131   return propertyBuffer;
132 }
133
134 void PropertyBuffer::SetData( const void* data, std::size_t size )
135 {
136   DALI_ASSERT_DEBUG( mFormat.Count() && "Format must be set before setting the data." );
137
138   mSize = size;
139
140   // Check if format and size have been set yet
141   if( mBufferFormat != NULL )
142   {
143     unsigned int bufferSize = mBufferFormat->size * mSize;
144     mBuffer.Resize( bufferSize );
145   }
146
147   const char* source = static_cast<const char*>( data );
148   std::copy( source, source + mBuffer.Size(), &mBuffer[0] );
149
150   SceneGraph::SetPropertyBufferData( mEventThreadServices.GetUpdateManager(), *mRenderObject, new Dali::Vector<char>( mBuffer ), mSize );
151 }
152
153 std::size_t PropertyBuffer::GetSize() const
154 {
155   return mSize;
156 }
157
158 const Render::PropertyBuffer* PropertyBuffer::GetRenderObject() const
159 {
160   return mRenderObject;
161 }
162
163 PropertyBuffer::~PropertyBuffer()
164 {
165   if( EventThreadServices::IsCoreRunning() && mRenderObject)
166   {
167     SceneGraph::RemovePropertyBuffer( mEventThreadServices.GetUpdateManager(), *mRenderObject );
168   }
169 }
170
171 PropertyBuffer::PropertyBuffer()
172 : mEventThreadServices( *Stage::GetCurrent() ),
173   mRenderObject( NULL ),
174   mBufferFormat( NULL ),
175   mSize( 0 )
176 {
177 }
178
179 void PropertyBuffer::Initialize( Dali::Property::Map& formatMap )
180 {
181   mRenderObject = new Render::PropertyBuffer();
182   SceneGraph::AddPropertyBuffer(mEventThreadServices.GetUpdateManager(), *mRenderObject );
183
184   mFormat = formatMap;
185
186   size_t numComponents = mFormat.Count();
187
188   // Create the format
189   DALI_ASSERT_DEBUG( mBufferFormat == NULL && "PropertyFormat should not be set yet" );
190   Render::PropertyBuffer::Format* format = new Render::PropertyBuffer::Format();
191   format->components.resize( numComponents );
192
193   unsigned int currentAlignment = 0u;
194   unsigned int maxAlignmentRequired = 0u;
195
196   for( size_t i = 0u; i < numComponents; ++i )
197   {
198     KeyValuePair component = mFormat.GetKeyValue( i );
199
200     // Get the name
201     if(component.first.type == Property::Key::INDEX)
202     {
203       continue;
204     }
205     format->components[i].name = component.first.stringKey;
206
207     // enums are stored in the map as int
208     Property::Type type = Property::Type( component.second.Get<int>() );
209
210     // Get the size and alignment
211     unsigned int elementSize = GetPropertyImplementationSize( type );
212     unsigned int elementAlignment = GetPropertyImplementationAlignment( type );
213
214     // check if current alignment is compatible with new member
215     if( unsigned int offset = currentAlignment % elementAlignment )
216     {
217       // Not compatible, realign
218       currentAlignment = currentAlignment + elementSize - offset;
219     }
220
221     // write to the format
222     format->components[i].size = elementSize;
223     format->components[i].offset = currentAlignment;
224     format->components[i].type = type;
225
226     // update offset
227     currentAlignment += elementSize;
228
229     // update max alignment requirement
230     if( elementAlignment > maxAlignmentRequired )
231     {
232       maxAlignmentRequired = elementAlignment;
233     }
234
235   }
236
237   // Check the alignment for the maxAlignment required to calculate the size of the format
238   if( maxAlignmentRequired != 0 )
239   {
240     if( unsigned int offset = currentAlignment % maxAlignmentRequired )
241     {
242       // Not compatible, realign
243       currentAlignment = currentAlignment + maxAlignmentRequired - offset;
244     }
245   }
246
247   // Set the format size
248   format->size = currentAlignment;
249
250   mBufferFormat = format;
251
252   SceneGraph::SetPropertyBufferFormat(mEventThreadServices.GetUpdateManager(), *mRenderObject, format );
253 }
254
255 unsigned int GetPropertyImplementationSize( Property::Type& propertyType )
256 {
257   unsigned int size = 0u;
258
259   switch( propertyType )
260   {
261     case Property::NONE:
262     case Property::STRING:
263     case Property::ARRAY:
264     case Property::MAP:
265     {
266       DALI_ASSERT_ALWAYS( "No size for properties with no type, or dynamic sizes" );
267       break;
268     }
269     case Property::BOOLEAN:
270     {
271       size = sizeof( PropertyImplementationType< Property::BOOLEAN >::Type );
272       break;
273     }
274     case Property::INTEGER:
275     {
276       size = sizeof( PropertyImplementationType< Property::INTEGER >::Type );
277       break;
278     }
279     case Property::FLOAT:
280     {
281       size = sizeof( PropertyImplementationType< Property::FLOAT >::Type );
282       break;
283     }
284     case Property::VECTOR2:
285     {
286       size = sizeof( PropertyImplementationType< Property::VECTOR2 >::Type );
287       break;
288     }
289     case Property::VECTOR3:
290     {
291       size = sizeof( PropertyImplementationType< Property::VECTOR3 >::Type );
292       break;
293     }
294     case Property::VECTOR4:
295     {
296       size = sizeof( PropertyImplementationType< Property::VECTOR4 >::Type );
297       break;
298     }
299     case Property::MATRIX3:
300     {
301       size = sizeof( PropertyImplementationType< Property::MATRIX3 >::Type );
302       break;
303     }
304     case Property::MATRIX:
305     {
306       size = sizeof( PropertyImplementationType< Property::MATRIX >::Type );
307       break;
308     }
309     case Property::RECTANGLE:
310     {
311       size = sizeof( PropertyImplementationType< Property::RECTANGLE >::Type );
312       break;
313     }
314     case Property::ROTATION:
315     {
316       size = sizeof( PropertyImplementationType< Property::ROTATION >::Type );
317       break;
318     }
319   }
320
321   return size;
322 }
323
324
325 } // namespace Internal
326 } // namespace Dali