7b00e9ec1edaf849b8a02cabe1cb9fbce711eea9
[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     StringValuePair component = mFormat.GetPair( i );
199
200     // Get the name
201     format->components[i].name = component.first;
202
203     // enums are stored in the map as int
204     Property::Type type = Property::Type( component.second.Get<int>() );
205
206     // Get the size and alignment
207     unsigned int elementSize = GetPropertyImplementationSize( type );
208     unsigned int elementAlignment = GetPropertyImplementationAlignment( type );
209
210     // check if current alignment is compatible with new member
211     if( unsigned int offset = currentAlignment % elementAlignment )
212     {
213       // Not compatible, realign
214       currentAlignment = currentAlignment + elementSize - offset;
215     }
216
217     // write to the format
218     format->components[i].size = elementSize;
219     format->components[i].offset = currentAlignment;
220     format->components[i].type = type;
221
222     // update offset
223     currentAlignment += elementSize;
224
225     // update max alignment requirement
226     if( elementAlignment > maxAlignmentRequired )
227     {
228       maxAlignmentRequired = elementAlignment;
229     }
230
231   }
232
233   // Check the alignment for the maxAlignment required to calculate the size of the format
234   if( maxAlignmentRequired != 0 )
235   {
236     if( unsigned int offset = currentAlignment % maxAlignmentRequired )
237     {
238       // Not compatible, realign
239       currentAlignment = currentAlignment + maxAlignmentRequired - offset;
240     }
241   }
242
243   // Set the format size
244   format->size = currentAlignment;
245
246   mBufferFormat = format;
247
248   SceneGraph::SetPropertyBufferFormat(mEventThreadServices.GetUpdateManager(), *mRenderObject, format );
249 }
250
251 unsigned int GetPropertyImplementationSize( Property::Type& propertyType )
252 {
253   unsigned int size = 0u;
254
255   switch( propertyType )
256   {
257     case Property::NONE:
258     case Property::STRING:
259     case Property::ARRAY:
260     case Property::MAP:
261     {
262       DALI_ASSERT_ALWAYS( "No size for properties with no type, or dynamic sizes" );
263       break;
264     }
265     case Property::BOOLEAN:
266     {
267       size = sizeof( PropertyImplementationType< Property::BOOLEAN >::Type );
268       break;
269     }
270     case Property::INTEGER:
271     {
272       size = sizeof( PropertyImplementationType< Property::INTEGER >::Type );
273       break;
274     }
275     case Property::FLOAT:
276     {
277       size = sizeof( PropertyImplementationType< Property::FLOAT >::Type );
278       break;
279     }
280     case Property::VECTOR2:
281     {
282       size = sizeof( PropertyImplementationType< Property::VECTOR2 >::Type );
283       break;
284     }
285     case Property::VECTOR3:
286     {
287       size = sizeof( PropertyImplementationType< Property::VECTOR3 >::Type );
288       break;
289     }
290     case Property::VECTOR4:
291     {
292       size = sizeof( PropertyImplementationType< Property::VECTOR4 >::Type );
293       break;
294     }
295     case Property::MATRIX3:
296     {
297       size = sizeof( PropertyImplementationType< Property::MATRIX3 >::Type );
298       break;
299     }
300     case Property::MATRIX:
301     {
302       size = sizeof( PropertyImplementationType< Property::MATRIX >::Type );
303       break;
304     }
305     case Property::RECTANGLE:
306     {
307       size = sizeof( PropertyImplementationType< Property::RECTANGLE >::Type );
308       break;
309     }
310     case Property::ROTATION:
311     {
312       size = sizeof( PropertyImplementationType< Property::ROTATION >::Type );
313       break;
314     }
315   }
316
317   return size;
318 }
319
320
321 } // namespace Internal
322 } // namespace Dali