Merge "Fix memory leaks detected by Valgrind" into devel/master
[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::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   Dali::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   SceneGraph::AddPropertyBuffer(mEventThreadServices.GetUpdateManager(), *mRenderObject );
183
184   size_t numComponents = formatMap.Count();
185
186   // Create the format
187   Render::PropertyBuffer::Format* format = new Render::PropertyBuffer::Format();
188   format->components.resize( numComponents );
189
190   unsigned int currentAlignment = 0u;
191   unsigned int maxAlignmentRequired = 0u;
192
193   for( size_t i = 0u; i < numComponents; ++i )
194   {
195     KeyValuePair component = formatMap.GetKeyValue( i );
196
197     // Get the name
198     if(component.first.type == Property::Key::INDEX)
199     {
200       continue;
201     }
202     format->components[i].name = component.first.stringKey;
203
204     // enums are stored in the map as int
205     Property::Type type = Property::Type( component.second.Get<int>() );
206
207     // Get the size and alignment
208     if( ( type == Property::NONE   ) ||
209         ( type == Property::STRING ) ||
210         ( type == Property::ARRAY  ) ||
211         ( type == Property::MAP    ) )
212     {
213       DALI_ABORT( "Property::Type not supported in PropertyBuffer" );
214     }
215     unsigned int elementSize = GetPropertyImplementationSize( type );
216     unsigned int elementAlignment = GetPropertyImplementationAlignment( type );
217
218     // check if current alignment is compatible with new member
219     if( unsigned int offset = currentAlignment % elementAlignment )
220     {
221       // Not compatible, realign
222       currentAlignment = currentAlignment + elementSize - offset;
223     }
224
225     // write to the format
226     format->components[i].size = elementSize;
227     format->components[i].offset = currentAlignment;
228     format->components[i].type = type;
229
230     // update offset
231     currentAlignment += elementSize;
232
233     // update max alignment requirement
234     if( elementAlignment > maxAlignmentRequired )
235     {
236       maxAlignmentRequired = elementAlignment;
237     }
238
239   }
240
241   // Check the alignment for the maxAlignment required to calculate the size of the format
242   if( maxAlignmentRequired != 0 )
243   {
244     if( unsigned int offset = currentAlignment % maxAlignmentRequired )
245     {
246       // Not compatible, realign
247       currentAlignment = currentAlignment + maxAlignmentRequired - offset;
248     }
249   }
250
251   // Set the format size
252   format->size = currentAlignment;
253
254   mBufferFormatSize = format->size;
255
256   SceneGraph::SetPropertyBufferFormat(mEventThreadServices.GetUpdateManager(), *mRenderObject, format );
257 }
258
259 unsigned int GetPropertyImplementationSize( Property::Type& propertyType )
260 {
261   unsigned int size = 0u;
262
263   switch( propertyType )
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     case Property::NONE:
316     case Property::STRING:
317     case Property::ARRAY:
318     case Property::MAP:
319     {
320       // already handled by higher level code
321     }
322   }
323
324   return size;
325 }
326
327
328 } // namespace Internal
329 } // namespace Dali