Add Property::EXTENTS type
[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     case Property::EXTENTS:
114     {
115       // already handled by higher level code
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   mSize = size; // size is the number of elements
137
138   unsigned int bufferSize = mBufferFormatSize * mSize;
139
140   // create a new DALi vector to store the buffer data
141   // the heap allocated vector will end up being owned by Render::PropertyBuffer
142   OwnerPointer< Vector<char> > bufferCopy = new Dali::Vector<char>();
143   bufferCopy->Resize( bufferSize );
144
145   // copy the data
146   const char* source = static_cast<const char*>( data );
147   char *destination = &((*bufferCopy)[0]);
148   std::copy( source, source + bufferSize, destination );
149
150   // Ownership of the bufferCopy is passed to the message ( uses an owner pointer )
151   SceneGraph::SetPropertyBufferData( mEventThreadServices.GetUpdateManager(), *mRenderObject, bufferCopy, mSize );
152 }
153
154 std::size_t PropertyBuffer::GetSize() const
155 {
156   return mSize;
157 }
158
159 const Render::PropertyBuffer* PropertyBuffer::GetRenderObject() const
160 {
161   return mRenderObject;
162 }
163
164 PropertyBuffer::~PropertyBuffer()
165 {
166   if( EventThreadServices::IsCoreRunning() && mRenderObject)
167   {
168     SceneGraph::RemovePropertyBuffer( mEventThreadServices.GetUpdateManager(), *mRenderObject );
169   }
170 }
171
172 PropertyBuffer::PropertyBuffer()
173 : mEventThreadServices( *Stage::GetCurrent() ),
174   mRenderObject( NULL ),
175   mBufferFormatSize( 0 ),
176   mSize( 0 )
177 {
178 }
179
180 void PropertyBuffer::Initialize( Dali::Property::Map& formatMap )
181 {
182   mRenderObject = new Render::PropertyBuffer();
183   OwnerPointer< Render::PropertyBuffer > transferOwnership( mRenderObject );
184   SceneGraph::AddPropertyBuffer( mEventThreadServices.GetUpdateManager(), transferOwnership );
185
186   size_t numComponents = formatMap.Count();
187
188   // Create the format
189   OwnerPointer< Render::PropertyBuffer::Format> format = new Render::PropertyBuffer::Format();
190   format->components.resize( numComponents );
191
192   unsigned int currentAlignment = 0u;
193   unsigned int maxAlignmentRequired = 0u;
194
195   for( size_t i = 0u; i < numComponents; ++i )
196   {
197     KeyValuePair component = formatMap.GetKeyValue( i );
198
199     // Get the name
200     if(component.first.type == Property::Key::INDEX)
201     {
202       continue;
203     }
204     format->components[i].name = component.first.stringKey;
205
206     // enums are stored in the map as int
207     Property::Type type = Property::Type( component.second.Get<int>() );
208
209     // Get the size and alignment
210     if( ( type == Property::NONE   ) ||
211         ( type == Property::STRING ) ||
212         ( type == Property::ARRAY  ) ||
213         ( type == Property::MAP    ) )
214     {
215       DALI_ABORT( "Property::Type not supported in PropertyBuffer" );
216     }
217     unsigned int elementSize = GetPropertyImplementationSize( type );
218     unsigned int elementAlignment = GetPropertyImplementationAlignment( type );
219
220     // check if current alignment is compatible with new member
221     if( unsigned int offset = currentAlignment % elementAlignment )
222     {
223       // Not compatible, realign
224       currentAlignment = currentAlignment + elementSize - offset;
225     }
226
227     // write to the format
228     format->components[i].size = elementSize;
229     format->components[i].offset = currentAlignment;
230     format->components[i].type = type;
231
232     // update offset
233     currentAlignment += elementSize;
234
235     // update max alignment requirement
236     if( elementAlignment > maxAlignmentRequired )
237     {
238       maxAlignmentRequired = elementAlignment;
239     }
240
241   }
242
243   // Check the alignment for the maxAlignment required to calculate the size of the format
244   if( maxAlignmentRequired != 0 )
245   {
246     if( unsigned int offset = currentAlignment % maxAlignmentRequired )
247     {
248       // Not compatible, realign
249       currentAlignment = currentAlignment + maxAlignmentRequired - offset;
250     }
251   }
252
253   // Set the format size
254   format->size = currentAlignment;
255
256   mBufferFormatSize = format->size;
257
258   SceneGraph::SetPropertyBufferFormat(mEventThreadServices.GetUpdateManager(), *mRenderObject, format );
259 }
260
261 unsigned int GetPropertyImplementationSize( Property::Type& propertyType )
262 {
263   unsigned int size = 0u;
264
265   switch( propertyType )
266   {
267     case Property::BOOLEAN:
268     {
269       size = sizeof( PropertyImplementationType< Property::BOOLEAN >::Type );
270       break;
271     }
272     case Property::INTEGER:
273     {
274       size = sizeof( PropertyImplementationType< Property::INTEGER >::Type );
275       break;
276     }
277     case Property::FLOAT:
278     {
279       size = sizeof( PropertyImplementationType< Property::FLOAT >::Type );
280       break;
281     }
282     case Property::VECTOR2:
283     {
284       size = sizeof( PropertyImplementationType< Property::VECTOR2 >::Type );
285       break;
286     }
287     case Property::VECTOR3:
288     {
289       size = sizeof( PropertyImplementationType< Property::VECTOR3 >::Type );
290       break;
291     }
292     case Property::VECTOR4:
293     {
294       size = sizeof( PropertyImplementationType< Property::VECTOR4 >::Type );
295       break;
296     }
297     case Property::MATRIX3:
298     {
299       size = sizeof( PropertyImplementationType< Property::MATRIX3 >::Type );
300       break;
301     }
302     case Property::MATRIX:
303     {
304       size = sizeof( PropertyImplementationType< Property::MATRIX >::Type );
305       break;
306     }
307     case Property::RECTANGLE:
308     {
309       size = sizeof( PropertyImplementationType< Property::RECTANGLE >::Type );
310       break;
311     }
312     case Property::ROTATION:
313     {
314       size = sizeof( PropertyImplementationType< Property::ROTATION >::Type );
315       break;
316     }
317     case Property::NONE:
318     case Property::STRING:
319     case Property::ARRAY:
320     case Property::MAP:
321     case Property::EXTENTS:
322     {
323       // already handled by higher level code
324     }
325   }
326
327   return size;
328 }
329
330
331 } // namespace Internal
332 } // namespace Dali