Removed ResourceClient
[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   DALI_ASSERT_DEBUG( mFormat.Count() && "Format must be set before setting the data." );
136
137   mSize = size;
138
139   // Check if format and size have been set yet
140   if( mBufferFormat != NULL )
141   {
142     unsigned int bufferSize = mBufferFormat->size * mSize;
143     mBuffer.Resize( bufferSize );
144   }
145
146   const char* source = static_cast<const char*>( data );
147   std::copy( source, source + mBuffer.Size(), &mBuffer[0] );
148
149   SceneGraph::SetPropertyBufferData( mEventThreadServices.GetUpdateManager(), *mRenderObject, new Dali::Vector<char>( mBuffer ), mSize );
150 }
151
152 std::size_t PropertyBuffer::GetSize() const
153 {
154   return mSize;
155 }
156
157 const Render::PropertyBuffer* PropertyBuffer::GetRenderObject() const
158 {
159   return mRenderObject;
160 }
161
162 PropertyBuffer::~PropertyBuffer()
163 {
164   if( EventThreadServices::IsCoreRunning() && mRenderObject)
165   {
166     SceneGraph::RemovePropertyBuffer( mEventThreadServices.GetUpdateManager(), *mRenderObject );
167   }
168 }
169
170 PropertyBuffer::PropertyBuffer()
171 : mEventThreadServices( *Stage::GetCurrent() ),
172   mRenderObject( NULL ),
173   mBufferFormat( NULL ),
174   mSize( 0 )
175 {
176 }
177
178 void PropertyBuffer::Initialize( Dali::Property::Map& formatMap )
179 {
180   mRenderObject = new Render::PropertyBuffer();
181   SceneGraph::AddPropertyBuffer(mEventThreadServices.GetUpdateManager(), *mRenderObject );
182
183   mFormat = formatMap;
184
185   size_t numComponents = mFormat.Count();
186
187   // Create the format
188   DALI_ASSERT_DEBUG( mBufferFormat == NULL && "PropertyFormat should not be set yet" );
189   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 = mFormat.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   mBufferFormat = format;
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     {
322       // already handled by higher level code
323     }
324   }
325
326   return size;
327 }
328
329
330 } // namespace Internal
331 } // namespace Dali