2 * Copyright (c) 2015 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> // Dali::Internal::PropertyBuffer
22 #include <algorithm> // std::sort
25 #include <dali/public-api/object/property-buffer.h> // Dali::Internal::PropertyBuffer
26 #include <dali/internal/event/common/object-impl-helper.h> // Dali::Internal::ObjectHelper
27 #include <dali/internal/event/common/property-helper.h> // DALI_PROPERTY_TABLE_BEGIN, DALI_PROPERTY, DALI_PROPERTY_TABLE_END
28 #include <dali/internal/update/common/scene-graph-property-buffer.h>
29 #include <dali/internal/update/manager/update-manager.h>
36 using SceneGraph::PropertyBufferMetadata::Format;
37 using SceneGraph::PropertyBufferMetadata::Component;
43 * |name |type |writable|animatable|constraint-input|enum for index-checking|
45 DALI_PROPERTY_TABLE_BEGIN
46 DALI_PROPERTY( "size", UNSIGNED_INTEGER, true, false, true, Dali::PropertyBuffer::Property::SIZE )
47 DALI_PROPERTY( "buffer-format", MAP, false, false, false, Dali::PropertyBuffer::Property::BUFFER_FORMAT )
48 DALI_PROPERTY_TABLE_END( DEFAULT_ACTOR_PROPERTY_START_INDEX )
50 const ObjectImplHelper<DEFAULT_PROPERTY_COUNT> PROPERTY_BUFFER_IMPL = { DEFAULT_PROPERTY_DETAILS };
53 * Calculate the alignment requirements of a type
55 * This is used to calculate the memory alignment requirements of a type
56 * It creates a structure with a dummy char and a member of the type we want to check
57 * this will cause the second member to be aligned by it's alignment requirement.
59 template<Property::Type type>
60 struct PropertyImplementationTypeAlignment
62 // Create a structure that forces alignment of the data type
65 char oneChar; ///< Member with sizeof() == 1
66 typename PropertyImplementationType<type>::Type data;
68 enum { VALUE = offsetof( TestStructure, data ) };
71 unsigned int GetPropertyImplementationAlignment( Property::Type& propertyType )
73 unsigned int alignment = 0u;
75 switch( propertyType )
78 case Property::TYPE_COUNT:
79 case Property::STRING:
83 DALI_ASSERT_ALWAYS( "No size for properties with no type, or dynamic sizes" );
86 case Property::BOOLEAN:
88 alignment = PropertyImplementationTypeAlignment< Property::BOOLEAN >::VALUE;
91 case Property::INTEGER:
93 alignment = PropertyImplementationTypeAlignment< Property::INTEGER >::VALUE;
96 case Property::UNSIGNED_INTEGER:
98 alignment = PropertyImplementationTypeAlignment< Property::UNSIGNED_INTEGER >::VALUE;
101 case Property::FLOAT:
103 alignment = PropertyImplementationTypeAlignment< Property::FLOAT >::VALUE;
106 case Property::VECTOR2:
108 alignment = PropertyImplementationTypeAlignment< Property::VECTOR2 >::VALUE;
111 case Property::VECTOR3:
113 alignment = PropertyImplementationTypeAlignment< Property::VECTOR3 >::VALUE;
116 case Property::VECTOR4:
118 alignment = PropertyImplementationTypeAlignment< Property::VECTOR4 >::VALUE;
121 case Property::MATRIX3:
123 alignment = PropertyImplementationTypeAlignment< Property::MATRIX3 >::VALUE;
126 case Property::MATRIX:
128 alignment = PropertyImplementationTypeAlignment< Property::MATRIX >::VALUE;
131 case Property::RECTANGLE:
133 alignment = PropertyImplementationTypeAlignment< Property::RECTANGLE >::VALUE;
136 case Property::ROTATION:
138 alignment = PropertyImplementationTypeAlignment< Property::ROTATION >::VALUE;
146 } // unnamed namespace
148 PropertyBufferPtr PropertyBuffer::New()
150 PropertyBufferPtr propertyBuffer( new PropertyBuffer() );
151 propertyBuffer->Initialize();
153 return propertyBuffer;
156 void PropertyBuffer::SetSize( std::size_t size )
162 SceneGraph::SetSizeMessage( GetEventThreadServices(),
167 std::size_t PropertyBuffer::GetSize() const
172 void PropertyBuffer::SetData( const void* data )
174 DALI_ASSERT_DEBUG( mFormat.Count() && "Format must be set before setting the data." );
176 DALI_ASSERT_ALWAYS( mSize && "Size of the buffer must be set before setting the data." );
178 const char* source = static_cast<const char*>( data );
179 std::copy( source, source + mBuffer.Size(), &mBuffer[0] );
181 SceneGraph::SetDataMessage( GetEventThreadServices(),
183 new SceneGraph::PropertyBuffer::BufferType( mBuffer ) );
186 Dali::Property::Index PropertyBuffer::GetPropertyIndex( const std::string name, std::size_t index )
189 DALI_ASSERT_ALWAYS( false && "MESH_REWORK" );
193 const SceneGraph::PropertyBuffer* PropertyBuffer::GetPropertyBufferSceneObject() const
198 void PropertyBuffer::SetType( Dali::PropertyBuffer::Type type )
200 DALI_ASSERT_DEBUG( mType == Dali::PropertyBuffer::TYPE_COUNT && "Type can only be set once." );
201 DALI_ASSERT_DEBUG( type != Dali::PropertyBuffer::TYPE_COUNT && "Type must be set to a valid value." );
206 void PropertyBuffer::SetFormat( Dali::Property::Map& format )
208 DALI_ASSERT_ALWAYS( format.Count() && "Format cannot be empty." );
210 DALI_ASSERT_DEBUG( 0 == mFormat.Count() && "Format of property buffer can only be set once." );
217 unsigned int PropertyBuffer::GetDefaultPropertyCount() const
219 return PROPERTY_BUFFER_IMPL.GetDefaultPropertyCount();
222 void PropertyBuffer::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
224 PROPERTY_BUFFER_IMPL.GetDefaultPropertyIndices( indices );
227 const char* PropertyBuffer::GetDefaultPropertyName(Property::Index index) const
229 return PROPERTY_BUFFER_IMPL.GetDefaultPropertyName( index );
232 Property::Index PropertyBuffer::GetDefaultPropertyIndex( const std::string& name ) const
234 return PROPERTY_BUFFER_IMPL.GetDefaultPropertyIndex( name );
237 bool PropertyBuffer::IsDefaultPropertyWritable( Property::Index index ) const
239 return PROPERTY_BUFFER_IMPL.IsDefaultPropertyWritable( index );
242 bool PropertyBuffer::IsDefaultPropertyAnimatable( Property::Index index ) const
244 return PROPERTY_BUFFER_IMPL.IsDefaultPropertyAnimatable( index );
247 bool PropertyBuffer::IsDefaultPropertyAConstraintInput( Property::Index index ) const
249 return PROPERTY_BUFFER_IMPL.IsDefaultPropertyAConstraintInput( index );
252 Property::Type PropertyBuffer::GetDefaultPropertyType( Property::Index index ) const
254 return PROPERTY_BUFFER_IMPL.GetDefaultPropertyType( index );
257 void PropertyBuffer::SetDefaultProperty( Property::Index index,
258 const Property::Value& propertyValue )
262 case Dali::PropertyBuffer::Property::SIZE:
264 SetSize( propertyValue.Get<int>() );
267 case Dali::PropertyBuffer::Property::BUFFER_FORMAT:
269 DALI_ASSERT_ALWAYS( 0 && "MESH_REWORK" );
275 void PropertyBuffer::SetSceneGraphProperty( Property::Index index,
276 const PropertyMetadata& entry,
277 const Property::Value& value )
279 PROPERTY_BUFFER_IMPL.SetSceneGraphProperty( GetEventThreadServices(), this, index, entry, value );
282 Property::Value PropertyBuffer::GetDefaultProperty( Property::Index index ) const
284 Property::Value value;
288 case Dali::PropertyBuffer::Property::SIZE:
290 value = static_cast<int>( GetSize() ); // @todo MESH_REWORK Add a size_t type to PropertyValue
293 case Dali::PropertyBuffer::Property::BUFFER_FORMAT:
295 DALI_ASSERT_ALWAYS( 0 && "MESH_REWORK" );
302 const SceneGraph::PropertyOwner* PropertyBuffer::GetPropertyOwner() const
307 const SceneGraph::PropertyOwner* PropertyBuffer::GetSceneObject() const
312 const SceneGraph::PropertyBase* PropertyBuffer::GetSceneObjectAnimatableProperty( Property::Index index ) const
314 DALI_ASSERT_ALWAYS( IsPropertyAnimatable(index) && "Property is not animatable" );
315 const SceneGraph::PropertyBase* property = NULL;
319 property = PROPERTY_BUFFER_IMPL.GetRegisteredSceneGraphProperty(
321 &PropertyBuffer::FindAnimatableProperty,
322 &PropertyBuffer::FindCustomProperty,
325 if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
327 DALI_ASSERT_ALWAYS( 0 && "Property is not animatable" );
334 const PropertyInputImpl* PropertyBuffer::GetSceneObjectInputProperty( Property::Index index ) const
336 const PropertyInputImpl* property = NULL;
340 const SceneGraph::PropertyBase* baseProperty =
341 PROPERTY_BUFFER_IMPL.GetRegisteredSceneGraphProperty( this,
342 &PropertyBuffer::FindAnimatableProperty,
343 &PropertyBuffer::FindCustomProperty,
345 property = static_cast<const PropertyInputImpl*>( baseProperty );
347 if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
349 if( index == Dali::PropertyBuffer::Property::SIZE )
352 DALI_ASSERT_ALWAYS( 0 && "MESH_REWORK" );
360 int PropertyBuffer::GetPropertyComponentIndex( Property::Index index ) const
362 return PROPERTY_BUFFER_IMPL.GetPropertyComponentIndex( index );
365 bool PropertyBuffer::OnStage() const
370 void PropertyBuffer::Connect()
375 void PropertyBuffer::Disconnect()
380 PropertyBuffer::~PropertyBuffer()
384 PropertyBuffer::PropertyBuffer()
385 : mSceneObject( NULL ),
386 mBufferFormat( NULL ),
388 mType( Dali::PropertyBuffer::TYPE_COUNT ),
393 void PropertyBuffer::Initialize()
395 EventThreadServices& eventThreadServices = GetEventThreadServices();
396 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
398 DALI_ASSERT_ALWAYS( EventThreadServices::IsCoreRunning() && "Core is not running" );
400 mSceneObject = new SceneGraph::PropertyBuffer();
401 AddMessage( updateManager, updateManager.GetPropertyBufferOwner(), *mSceneObject );
404 void PropertyBuffer::FormatChanged()
406 size_t numComponents = mFormat.Count();
409 DALI_ASSERT_DEBUG( mBufferFormat == NULL && "PropertyFormat should not be set yet" );
410 Format* bufferFormat = new Format();
411 bufferFormat->components.resize( numComponents );
413 unsigned int currentAlignment = 0u;
414 unsigned int maxAlignmentRequired = 0u;
416 for( size_t i = 0u; i < numComponents; ++i )
418 StringValuePair component = mFormat.GetPair( i );
421 bufferFormat->components[i].name = component.first;
423 // enums are stored in the map as int
424 Property::Type type = Property::Type( component.second.Get<int>() );
426 // Get the size and alignment
427 unsigned int elementSize = GetPropertyImplementationSize( type );
428 unsigned int elementAlignment = GetPropertyImplementationAlignment( type );
430 // check if current alignment is compatible with new member
431 if( unsigned int offset = currentAlignment % elementAlignment )
433 // Not compatible, realign
434 currentAlignment = currentAlignment + elementSize - offset;
437 // write to the format
438 bufferFormat->components[i].size = elementSize;
439 bufferFormat->components[i].offset = currentAlignment;
440 bufferFormat->components[i].type = type;
443 currentAlignment += elementSize;
445 // update max alignment requirement
446 if( elementAlignment > maxAlignmentRequired )
448 maxAlignmentRequired = elementAlignment;
453 // Check the alignment for the maxAlignment required to calculate the size of the format
454 if( unsigned int offset = currentAlignment % maxAlignmentRequired )
456 // Not compatible, realign
457 currentAlignment = currentAlignment + maxAlignmentRequired - offset;
460 // Set the format size
461 bufferFormat->size = currentAlignment;
463 mBufferFormat = bufferFormat;
465 SceneGraph::SetFormatMessage( GetEventThreadServices(),
475 void PropertyBuffer::SizeChanged()
477 // Check if format and size have been set yet
478 if( mBufferFormat != NULL )
480 unsigned int bufferSize = mBufferFormat->size * mSize;
481 mBuffer.Resize( bufferSize );
485 unsigned int GetPropertyImplementationSize( Property::Type& propertyType )
487 unsigned int size = 0u;
489 switch( propertyType )
492 case Property::TYPE_COUNT:
493 case Property::STRING:
494 case Property::ARRAY:
497 DALI_ASSERT_ALWAYS( "No size for properties with no type, or dynamic sizes" );
500 case Property::BOOLEAN:
502 size = sizeof( PropertyImplementationType< Property::BOOLEAN >::Type );
505 case Property::INTEGER:
507 size = sizeof( PropertyImplementationType< Property::INTEGER >::Type );
510 case Property::UNSIGNED_INTEGER:
512 size = sizeof( PropertyImplementationType< Property::UNSIGNED_INTEGER >::Type );
515 case Property::FLOAT:
517 size = sizeof( PropertyImplementationType< Property::FLOAT >::Type );
520 case Property::VECTOR2:
522 size = sizeof( PropertyImplementationType< Property::VECTOR2 >::Type );
525 case Property::VECTOR3:
527 size = sizeof( PropertyImplementationType< Property::VECTOR3 >::Type );
530 case Property::VECTOR4:
532 size = sizeof( PropertyImplementationType< Property::VECTOR4 >::Type );
535 case Property::MATRIX3:
537 size = sizeof( PropertyImplementationType< Property::MATRIX3 >::Type );
540 case Property::MATRIX:
542 size = sizeof( PropertyImplementationType< Property::MATRIX >::Type );
545 case Property::RECTANGLE:
547 size = sizeof( PropertyImplementationType< Property::RECTANGLE >::Type );
550 case Property::ROTATION:
552 size = sizeof( PropertyImplementationType< Property::ROTATION >::Type );
561 } // namespace Internal