c44df2ce8b1e27adf27d763d9c90b5dde9dae6f0
[platform/core/uifw/dali-core.git] / dali / internal / event / common / property-buffer-impl.cpp
1 /*
2  * Copyright (c) 2019 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/update/manager/update-manager.h>
24
25 namespace Dali
26 {
27 namespace Internal
28 {
29
30 namespace
31 {
32
33 /**
34  * Calculate the alignment requirements of a type
35  *
36  * This is used to calculate the memory alignment requirements of a type
37  * It creates a structure with a dummy char and a member of the type we want to check
38  * this will cause the second member to be aligned by it's alignment requirement.
39  */
40 template<Property::Type type>
41 struct PropertyImplementationTypeAlignment
42 {
43   // Create a structure that forces alignment of the data type
44   struct TestStructure
45   {
46     char oneChar;  ///< Member with sizeof() == 1
47     typename PropertyImplementationType<type>::Type data;
48   };
49   enum { VALUE = offsetof( TestStructure, data ) };
50 };
51
52 uint32_t GetPropertyImplementationAlignment( Property::Type& propertyType )
53 {
54   uint32_t alignment = 0u;
55
56   switch( propertyType )
57   {
58     case Property::BOOLEAN:
59     {
60       alignment = PropertyImplementationTypeAlignment< Property::BOOLEAN >::VALUE;
61       break;
62     }
63     case Property::INTEGER:
64     {
65       alignment = PropertyImplementationTypeAlignment< Property::INTEGER >::VALUE;
66       break;
67     }
68     case Property::FLOAT:
69     {
70       alignment = PropertyImplementationTypeAlignment< Property::FLOAT >::VALUE;
71       break;
72     }
73     case Property::VECTOR2:
74     {
75       alignment = PropertyImplementationTypeAlignment< Property::VECTOR2 >::VALUE;
76       break;
77     }
78     case Property::VECTOR3:
79     {
80       alignment = PropertyImplementationTypeAlignment< Property::VECTOR3 >::VALUE;
81       break;
82     }
83     case Property::VECTOR4:
84     {
85       alignment = PropertyImplementationTypeAlignment< Property::VECTOR4 >::VALUE;
86       break;
87     }
88     case Property::MATRIX3:
89     {
90       alignment = PropertyImplementationTypeAlignment< Property::MATRIX3 >::VALUE;
91       break;
92     }
93     case Property::MATRIX:
94     {
95       alignment = PropertyImplementationTypeAlignment< Property::MATRIX >::VALUE;
96       break;
97     }
98     case Property::RECTANGLE:
99     {
100       alignment = PropertyImplementationTypeAlignment< Property::RECTANGLE >::VALUE;
101       break;
102     }
103     case Property::ROTATION:
104     {
105       alignment = PropertyImplementationTypeAlignment< Property::ROTATION >::VALUE;
106       break;
107     }
108     case Property::NONE:
109     case Property::STRING:
110     case Property::ARRAY:
111     case Property::MAP:
112     case Property::EXTENTS:
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, uint32_t size )
134 {
135   mSize = size; // size is the number of elements
136
137   uint32_t 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<uint8_t> > bufferCopy = new Dali::Vector<uint8_t>();
142   bufferCopy->Resize( bufferSize );
143
144   // copy the data
145   const uint8_t* source = static_cast<const uint8_t*>( data );
146   uint8_t* 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 uint32_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( EventThreadServices::Get() ),
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   uint32_t numComponents = static_cast<uint32_t>( formatMap.Count() );
186
187   // Create the format
188   OwnerPointer< Render::PropertyBuffer::Format> format = new Render::PropertyBuffer::Format();
189   format->components.resize( numComponents );
190
191   uint32_t currentAlignment = 0u;
192   uint32_t maxAlignmentRequired = 0u;
193
194   for( uint32_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     uint32_t elementSize = GetPropertyImplementationSize( type );
217     uint32_t elementAlignment = GetPropertyImplementationAlignment( type );
218
219     // check if current alignment is compatible with new member
220     if( uint32_t 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( uint32_t 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 uint32_t GetPropertyImplementationSize( Property::Type& propertyType )
261 {
262   uint32_t 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     case Property::EXTENTS:
321     {
322       // already handled by higher level code
323     }
324   }
325
326   return size;
327 }
328
329
330 } // namespace Internal
331 } // namespace Dali