0a83cc9939cb865b569d0ef1b91b68f33722fa74
[platform/core/uifw/dali-core.git] / dali / internal / event / common / property-buffer-impl.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 // 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::BOOLEAN:
60     {
61       alignment = PropertyImplementationTypeAlignment< Property::BOOLEAN >::VALUE;
62       break;
63     }
64     case Property::INTEGER:
65     {
66       alignment = PropertyImplementationTypeAlignment< Property::INTEGER >::VALUE;
67       break;
68     }
69     case Property::FLOAT:
70     {
71       alignment = PropertyImplementationTypeAlignment< Property::FLOAT >::VALUE;
72       break;
73     }
74     case Property::VECTOR2:
75     {
76       alignment = PropertyImplementationTypeAlignment< Property::VECTOR2 >::VALUE;
77       break;
78     }
79     case Property::VECTOR3:
80     {
81       alignment = PropertyImplementationTypeAlignment< Property::VECTOR3 >::VALUE;
82       break;
83     }
84     case Property::VECTOR4:
85     {
86       alignment = PropertyImplementationTypeAlignment< Property::VECTOR4 >::VALUE;
87       break;
88     }
89     case Property::MATRIX3:
90     {
91       alignment = PropertyImplementationTypeAlignment< Property::MATRIX3 >::VALUE;
92       break;
93     }
94     case Property::MATRIX:
95     {
96       alignment = PropertyImplementationTypeAlignment< Property::MATRIX >::VALUE;
97       break;
98     }
99     case Property::RECTANGLE:
100     {
101       alignment = PropertyImplementationTypeAlignment< Property::RECTANGLE >::VALUE;
102       break;
103     }
104     case Property::ROTATION:
105     {
106       alignment = PropertyImplementationTypeAlignment< Property::ROTATION >::VALUE;
107       break;
108     }
109     case Property::NONE:
110     case Property::STRING:
111     case Property::ARRAY:
112     case Property::MAP:
113     {
114       // already handled by higher level code
115     }
116   }
117
118   return alignment;
119 }
120
121 } // unnamed namespace
122
123 PropertyBufferPtr PropertyBuffer::New( Dali::Property::Map& format )
124 {
125   DALI_ASSERT_ALWAYS( format.Count() && "Format cannot be empty." );
126
127   PropertyBufferPtr propertyBuffer( new PropertyBuffer() );
128   propertyBuffer->Initialize( format );
129
130   return propertyBuffer;
131 }
132
133 void PropertyBuffer::SetData( const void* data, std::size_t size )
134 {
135   mSize = size; // size is the number of elements
136
137   unsigned int bufferSize = mBufferFormatSize * mSize;
138
139   // create a new DALi vector to store the buffer data
140   // the heap allocated vector will end up being owned by Render::PropertyBuffer
141   OwnerPointer< Vector<char> > bufferCopy = new Dali::Vector<char>();
142   bufferCopy->Resize( bufferSize );
143
144   // copy the data
145   const char* source = static_cast<const char*>( data );
146   char *destination = &((*bufferCopy)[0]);
147   std::copy( source, source + bufferSize, destination );
148
149   // Ownership of the bufferCopy is passed to the message ( uses an owner pointer )
150   SceneGraph::SetPropertyBufferData( mEventThreadServices.GetUpdateManager(), *mRenderObject, bufferCopy, 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   mBufferFormatSize( 0 ),
175   mSize( 0 )
176 {
177 }
178
179 void PropertyBuffer::Initialize( Dali::Property::Map& formatMap )
180 {
181   mRenderObject = new Render::PropertyBuffer();
182   OwnerPointer< Render::PropertyBuffer > transferOwnership( mRenderObject );
183   SceneGraph::AddPropertyBuffer( mEventThreadServices.GetUpdateManager(), transferOwnership );
184
185   size_t numComponents = formatMap.Count();
186
187   // Create the format
188   OwnerPointer< Render::PropertyBuffer::Format> format = new Render::PropertyBuffer::Format();
189   format->components.resize( numComponents );
190
191   unsigned int currentAlignment = 0u;
192   unsigned int maxAlignmentRequired = 0u;
193
194   for( size_t i = 0u; i < numComponents; ++i )
195   {
196     KeyValuePair component = formatMap.GetKeyValue( i );
197
198     // Get the name
199     if(component.first.type == Property::Key::INDEX)
200     {
201       continue;
202     }
203     format->components[i].name = component.first.stringKey;
204
205     // enums are stored in the map as int
206     Property::Type type = Property::Type( component.second.Get<int>() );
207
208     // Get the size and alignment
209     if( ( type == Property::NONE   ) ||
210         ( type == Property::STRING ) ||
211         ( type == Property::ARRAY  ) ||
212         ( type == Property::MAP    ) )
213     {
214       DALI_ABORT( "Property::Type not supported in PropertyBuffer" );
215     }
216     unsigned int elementSize = GetPropertyImplementationSize( type );
217     unsigned int elementAlignment = GetPropertyImplementationAlignment( type );
218
219     // check if current alignment is compatible with new member
220     if( unsigned int offset = currentAlignment % elementAlignment )
221     {
222       // Not compatible, realign
223       currentAlignment = currentAlignment + elementSize - offset;
224     }
225
226     // write to the format
227     format->components[i].size = elementSize;
228     format->components[i].offset = currentAlignment;
229     format->components[i].type = type;
230
231     // update offset
232     currentAlignment += elementSize;
233
234     // update max alignment requirement
235     if( elementAlignment > maxAlignmentRequired )
236     {
237       maxAlignmentRequired = elementAlignment;
238     }
239
240   }
241
242   // Check the alignment for the maxAlignment required to calculate the size of the format
243   if( maxAlignmentRequired != 0 )
244   {
245     if( unsigned int offset = currentAlignment % maxAlignmentRequired )
246     {
247       // Not compatible, realign
248       currentAlignment = currentAlignment + maxAlignmentRequired - offset;
249     }
250   }
251
252   // Set the format size
253   format->size = currentAlignment;
254
255   mBufferFormatSize = format->size;
256
257   SceneGraph::SetPropertyBufferFormat(mEventThreadServices.GetUpdateManager(), *mRenderObject, format );
258 }
259
260 unsigned int GetPropertyImplementationSize( Property::Type& propertyType )
261 {
262   unsigned int size = 0u;
263
264   switch( propertyType )
265   {
266     case Property::BOOLEAN:
267     {
268       size = sizeof( PropertyImplementationType< Property::BOOLEAN >::Type );
269       break;
270     }
271     case Property::INTEGER:
272     {
273       size = sizeof( PropertyImplementationType< Property::INTEGER >::Type );
274       break;
275     }
276     case Property::FLOAT:
277     {
278       size = sizeof( PropertyImplementationType< Property::FLOAT >::Type );
279       break;
280     }
281     case Property::VECTOR2:
282     {
283       size = sizeof( PropertyImplementationType< Property::VECTOR2 >::Type );
284       break;
285     }
286     case Property::VECTOR3:
287     {
288       size = sizeof( PropertyImplementationType< Property::VECTOR3 >::Type );
289       break;
290     }
291     case Property::VECTOR4:
292     {
293       size = sizeof( PropertyImplementationType< Property::VECTOR4 >::Type );
294       break;
295     }
296     case Property::MATRIX3:
297     {
298       size = sizeof( PropertyImplementationType< Property::MATRIX3 >::Type );
299       break;
300     }
301     case Property::MATRIX:
302     {
303       size = sizeof( PropertyImplementationType< Property::MATRIX >::Type );
304       break;
305     }
306     case Property::RECTANGLE:
307     {
308       size = sizeof( PropertyImplementationType< Property::RECTANGLE >::Type );
309       break;
310     }
311     case Property::ROTATION:
312     {
313       size = sizeof( PropertyImplementationType< Property::ROTATION >::Type );
314       break;
315     }
316     case Property::NONE:
317     case Property::STRING:
318     case Property::ARRAY:
319     case Property::MAP:
320     {
321       // already handled by higher level code
322     }
323   }
324
325   return size;
326 }
327
328
329 } // namespace Internal
330 } // namespace Dali