2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/event/common/property-buffer-impl.h>
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>
35 * Calculate the alignment requirements of a type
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.
41 template<Property::Type type>
42 struct PropertyImplementationTypeAlignment
44 // Create a structure that forces alignment of the data type
47 char oneChar; ///< Member with sizeof() == 1
48 typename PropertyImplementationType<type>::Type data;
50 enum { VALUE = offsetof( TestStructure, data ) };
53 unsigned int GetPropertyImplementationAlignment( Property::Type& propertyType )
55 unsigned int alignment = 0u;
57 switch( propertyType )
59 case Property::BOOLEAN:
61 alignment = PropertyImplementationTypeAlignment< Property::BOOLEAN >::VALUE;
64 case Property::INTEGER:
66 alignment = PropertyImplementationTypeAlignment< Property::INTEGER >::VALUE;
71 alignment = PropertyImplementationTypeAlignment< Property::FLOAT >::VALUE;
74 case Property::VECTOR2:
76 alignment = PropertyImplementationTypeAlignment< Property::VECTOR2 >::VALUE;
79 case Property::VECTOR3:
81 alignment = PropertyImplementationTypeAlignment< Property::VECTOR3 >::VALUE;
84 case Property::VECTOR4:
86 alignment = PropertyImplementationTypeAlignment< Property::VECTOR4 >::VALUE;
89 case Property::MATRIX3:
91 alignment = PropertyImplementationTypeAlignment< Property::MATRIX3 >::VALUE;
94 case Property::MATRIX:
96 alignment = PropertyImplementationTypeAlignment< Property::MATRIX >::VALUE;
99 case Property::RECTANGLE:
101 alignment = PropertyImplementationTypeAlignment< Property::RECTANGLE >::VALUE;
104 case Property::ROTATION:
106 alignment = PropertyImplementationTypeAlignment< Property::ROTATION >::VALUE;
110 case Property::STRING:
111 case Property::ARRAY:
114 // already handled by higher level code
121 } // unnamed namespace
123 PropertyBufferPtr PropertyBuffer::New( Dali::Property::Map& format )
125 DALI_ASSERT_ALWAYS( format.Count() && "Format cannot be empty." );
127 PropertyBufferPtr propertyBuffer( new PropertyBuffer() );
128 propertyBuffer->Initialize( format );
130 return propertyBuffer;
133 void PropertyBuffer::SetData( const void* data, std::size_t size )
135 mSize = size; // size is the number of elements
137 unsigned int bufferSize = mBufferFormatSize * mSize;
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 );
145 const char* source = static_cast<const char*>( data );
146 char *destination = &((*bufferCopy)[0]);
147 std::copy( source, source + bufferSize, destination );
149 // Ownership of the bufferCopy is passed to the message ( uses an owner pointer )
150 SceneGraph::SetPropertyBufferData( mEventThreadServices.GetUpdateManager(), *mRenderObject, bufferCopy, mSize );
153 std::size_t PropertyBuffer::GetSize() const
158 const Render::PropertyBuffer* PropertyBuffer::GetRenderObject() const
160 return mRenderObject;
163 PropertyBuffer::~PropertyBuffer()
165 if( EventThreadServices::IsCoreRunning() && mRenderObject)
167 SceneGraph::RemovePropertyBuffer( mEventThreadServices.GetUpdateManager(), *mRenderObject );
171 PropertyBuffer::PropertyBuffer()
172 : mEventThreadServices( *Stage::GetCurrent() ),
173 mRenderObject( NULL ),
174 mBufferFormatSize( 0 ),
179 void PropertyBuffer::Initialize( Dali::Property::Map& formatMap )
181 mRenderObject = new Render::PropertyBuffer();
182 SceneGraph::AddPropertyBuffer(mEventThreadServices.GetUpdateManager(), *mRenderObject );
184 size_t numComponents = formatMap.Count();
187 Render::PropertyBuffer::Format* format = new Render::PropertyBuffer::Format();
188 format->components.resize( numComponents );
190 unsigned int currentAlignment = 0u;
191 unsigned int maxAlignmentRequired = 0u;
193 for( size_t i = 0u; i < numComponents; ++i )
195 KeyValuePair component = formatMap.GetKeyValue( i );
198 if(component.first.type == Property::Key::INDEX)
202 format->components[i].name = component.first.stringKey;
204 // enums are stored in the map as int
205 Property::Type type = Property::Type( component.second.Get<int>() );
207 // Get the size and alignment
208 if( ( type == Property::NONE ) ||
209 ( type == Property::STRING ) ||
210 ( type == Property::ARRAY ) ||
211 ( type == Property::MAP ) )
213 DALI_ABORT( "Property::Type not supported in PropertyBuffer" );
215 unsigned int elementSize = GetPropertyImplementationSize( type );
216 unsigned int elementAlignment = GetPropertyImplementationAlignment( type );
218 // check if current alignment is compatible with new member
219 if( unsigned int offset = currentAlignment % elementAlignment )
221 // Not compatible, realign
222 currentAlignment = currentAlignment + elementSize - offset;
225 // write to the format
226 format->components[i].size = elementSize;
227 format->components[i].offset = currentAlignment;
228 format->components[i].type = type;
231 currentAlignment += elementSize;
233 // update max alignment requirement
234 if( elementAlignment > maxAlignmentRequired )
236 maxAlignmentRequired = elementAlignment;
241 // Check the alignment for the maxAlignment required to calculate the size of the format
242 if( maxAlignmentRequired != 0 )
244 if( unsigned int offset = currentAlignment % maxAlignmentRequired )
246 // Not compatible, realign
247 currentAlignment = currentAlignment + maxAlignmentRequired - offset;
251 // Set the format size
252 format->size = currentAlignment;
254 mBufferFormatSize = format->size;
256 SceneGraph::SetPropertyBufferFormat(mEventThreadServices.GetUpdateManager(), *mRenderObject, format );
259 unsigned int GetPropertyImplementationSize( Property::Type& propertyType )
261 unsigned int size = 0u;
263 switch( propertyType )
265 case Property::BOOLEAN:
267 size = sizeof( PropertyImplementationType< Property::BOOLEAN >::Type );
270 case Property::INTEGER:
272 size = sizeof( PropertyImplementationType< Property::INTEGER >::Type );
275 case Property::FLOAT:
277 size = sizeof( PropertyImplementationType< Property::FLOAT >::Type );
280 case Property::VECTOR2:
282 size = sizeof( PropertyImplementationType< Property::VECTOR2 >::Type );
285 case Property::VECTOR3:
287 size = sizeof( PropertyImplementationType< Property::VECTOR3 >::Type );
290 case Property::VECTOR4:
292 size = sizeof( PropertyImplementationType< Property::VECTOR4 >::Type );
295 case Property::MATRIX3:
297 size = sizeof( PropertyImplementationType< Property::MATRIX3 >::Type );
300 case Property::MATRIX:
302 size = sizeof( PropertyImplementationType< Property::MATRIX >::Type );
305 case Property::RECTANGLE:
307 size = sizeof( PropertyImplementationType< Property::RECTANGLE >::Type );
310 case Property::ROTATION:
312 size = sizeof( PropertyImplementationType< Property::ROTATION >::Type );
316 case Property::STRING:
317 case Property::ARRAY:
320 // already handled by higher level code
328 } // namespace Internal