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/type-registry.h>
26 #include <dali/devel-api/object/property-buffer.h> // Dali::Internal::PropertyBuffer
28 #include <dali/internal/event/common/object-impl-helper.h> // Dali::Internal::ObjectHelper
29 #include <dali/internal/event/common/property-helper.h> // DALI_PROPERTY_TABLE_BEGIN, DALI_PROPERTY, DALI_PROPERTY_TABLE_END
30 #include <dali/internal/update/common/scene-graph-property-buffer.h>
31 #include <dali/internal/update/manager/update-manager.h>
38 using SceneGraph::PropertyBufferMetadata::Format;
39 using SceneGraph::PropertyBufferMetadata::Component;
45 * |name |type |writable|animatable|constraint-input|enum for index-checking|
47 DALI_PROPERTY_TABLE_BEGIN
48 DALI_PROPERTY( "size", UNSIGNED_INTEGER, true, false, true, Dali::PropertyBuffer::Property::SIZE )
49 DALI_PROPERTY( "buffer-format", MAP, false, false, false, Dali::PropertyBuffer::Property::BUFFER_FORMAT )
50 DALI_PROPERTY_TABLE_END( DEFAULT_ACTOR_PROPERTY_START_INDEX )
52 const ObjectImplHelper<DEFAULT_PROPERTY_COUNT> PROPERTY_BUFFER_IMPL = { DEFAULT_PROPERTY_DETAILS };
56 return Dali::BaseHandle();
59 TypeRegistration mType( typeid( Dali::PropertyBuffer ), typeid( Dali::Handle ), Create );
62 * Calculate the alignment requirements of a type
64 * This is used to calculate the memory alignment requirements of a type
65 * It creates a structure with a dummy char and a member of the type we want to check
66 * this will cause the second member to be aligned by it's alignment requirement.
68 template<Property::Type type>
69 struct PropertyImplementationTypeAlignment
71 // Create a structure that forces alignment of the data type
74 char oneChar; ///< Member with sizeof() == 1
75 typename PropertyImplementationType<type>::Type data;
77 enum { VALUE = offsetof( TestStructure, data ) };
80 unsigned int GetPropertyImplementationAlignment( Property::Type& propertyType )
82 unsigned int alignment = 0u;
84 switch( propertyType )
87 case Property::TYPE_COUNT:
88 case Property::STRING:
92 DALI_ASSERT_ALWAYS( "No size for properties with no type, or dynamic sizes" );
95 case Property::BOOLEAN:
97 alignment = PropertyImplementationTypeAlignment< Property::BOOLEAN >::VALUE;
100 case Property::INTEGER:
102 alignment = PropertyImplementationTypeAlignment< Property::INTEGER >::VALUE;
105 case Property::UNSIGNED_INTEGER:
107 alignment = PropertyImplementationTypeAlignment< Property::UNSIGNED_INTEGER >::VALUE;
110 case Property::FLOAT:
112 alignment = PropertyImplementationTypeAlignment< Property::FLOAT >::VALUE;
115 case Property::VECTOR2:
117 alignment = PropertyImplementationTypeAlignment< Property::VECTOR2 >::VALUE;
120 case Property::VECTOR3:
122 alignment = PropertyImplementationTypeAlignment< Property::VECTOR3 >::VALUE;
125 case Property::VECTOR4:
127 alignment = PropertyImplementationTypeAlignment< Property::VECTOR4 >::VALUE;
130 case Property::MATRIX3:
132 alignment = PropertyImplementationTypeAlignment< Property::MATRIX3 >::VALUE;
135 case Property::MATRIX:
137 alignment = PropertyImplementationTypeAlignment< Property::MATRIX >::VALUE;
140 case Property::RECTANGLE:
142 alignment = PropertyImplementationTypeAlignment< Property::RECTANGLE >::VALUE;
145 case Property::ROTATION:
147 alignment = PropertyImplementationTypeAlignment< Property::ROTATION >::VALUE;
155 } // unnamed namespace
157 PropertyBufferPtr PropertyBuffer::New()
159 PropertyBufferPtr propertyBuffer( new PropertyBuffer() );
160 propertyBuffer->Initialize();
162 return propertyBuffer;
165 void PropertyBuffer::SetSize( std::size_t size )
171 SceneGraph::SetSizeMessage( GetEventThreadServices(),
176 std::size_t PropertyBuffer::GetSize() const
181 void PropertyBuffer::SetData( const void* data )
183 DALI_ASSERT_DEBUG( mFormat.Count() && "Format must be set before setting the data." );
185 DALI_ASSERT_ALWAYS( mSize && "Size of the buffer must be set before setting the data." );
187 const char* source = static_cast<const char*>( data );
188 std::copy( source, source + mBuffer.Size(), &mBuffer[0] );
190 SceneGraph::SetDataMessage( GetEventThreadServices(),
192 new SceneGraph::PropertyBuffer::BufferType( mBuffer ) );
195 const SceneGraph::PropertyBuffer* PropertyBuffer::GetPropertyBufferSceneObject() const
200 void PropertyBuffer::SetFormat( Dali::Property::Map& format )
202 DALI_ASSERT_ALWAYS( format.Count() && "Format cannot be empty." );
204 DALI_ASSERT_DEBUG( 0 == mFormat.Count() && "Format of property buffer can only be set once." );
211 unsigned int PropertyBuffer::GetDefaultPropertyCount() const
213 return PROPERTY_BUFFER_IMPL.GetDefaultPropertyCount();
216 void PropertyBuffer::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
218 PROPERTY_BUFFER_IMPL.GetDefaultPropertyIndices( indices );
221 const char* PropertyBuffer::GetDefaultPropertyName(Property::Index index) const
223 return PROPERTY_BUFFER_IMPL.GetDefaultPropertyName( index );
226 Property::Index PropertyBuffer::GetDefaultPropertyIndex( const std::string& name ) const
228 return PROPERTY_BUFFER_IMPL.GetDefaultPropertyIndex( name );
231 bool PropertyBuffer::IsDefaultPropertyWritable( Property::Index index ) const
233 return PROPERTY_BUFFER_IMPL.IsDefaultPropertyWritable( index );
236 bool PropertyBuffer::IsDefaultPropertyAnimatable( Property::Index index ) const
238 return PROPERTY_BUFFER_IMPL.IsDefaultPropertyAnimatable( index );
241 bool PropertyBuffer::IsDefaultPropertyAConstraintInput( Property::Index index ) const
243 return PROPERTY_BUFFER_IMPL.IsDefaultPropertyAConstraintInput( index );
246 Property::Type PropertyBuffer::GetDefaultPropertyType( Property::Index index ) const
248 return PROPERTY_BUFFER_IMPL.GetDefaultPropertyType( index );
251 void PropertyBuffer::SetDefaultProperty( Property::Index index,
252 const Property::Value& propertyValue )
256 case Dali::PropertyBuffer::Property::SIZE:
258 SetSize( propertyValue.Get<unsigned int>() );
261 case Dali::PropertyBuffer::Property::BUFFER_FORMAT:
263 DALI_ASSERT_ALWAYS( 0 && "MESH_REWORK" );
269 void PropertyBuffer::SetSceneGraphProperty( Property::Index index,
270 const PropertyMetadata& entry,
271 const Property::Value& value )
273 PROPERTY_BUFFER_IMPL.SetSceneGraphProperty( GetEventThreadServices(), this, index, entry, value );
276 Property::Value PropertyBuffer::GetDefaultProperty( Property::Index index ) const
278 Property::Value value;
282 case Dali::PropertyBuffer::Property::SIZE:
284 value = static_cast<unsigned int>( GetSize() ); // @todo MESH_REWORK Add a size_t type to PropertyValue
287 case Dali::PropertyBuffer::Property::BUFFER_FORMAT:
289 DALI_ASSERT_ALWAYS( 0 && "MESH_REWORK" );
296 const SceneGraph::PropertyOwner* PropertyBuffer::GetPropertyOwner() const
301 const SceneGraph::PropertyOwner* PropertyBuffer::GetSceneObject() const
306 const SceneGraph::PropertyBase* PropertyBuffer::GetSceneObjectAnimatableProperty( Property::Index index ) const
308 DALI_ASSERT_ALWAYS( IsPropertyAnimatable(index) && "Property is not animatable" );
309 const SceneGraph::PropertyBase* property = NULL;
313 property = PROPERTY_BUFFER_IMPL.GetRegisteredSceneGraphProperty(
315 &PropertyBuffer::FindAnimatableProperty,
316 &PropertyBuffer::FindCustomProperty,
319 if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
321 DALI_ASSERT_ALWAYS( 0 && "Property is not animatable" );
328 const PropertyInputImpl* PropertyBuffer::GetSceneObjectInputProperty( Property::Index index ) const
330 const PropertyInputImpl* property = NULL;
334 const SceneGraph::PropertyBase* baseProperty =
335 PROPERTY_BUFFER_IMPL.GetRegisteredSceneGraphProperty( this,
336 &PropertyBuffer::FindAnimatableProperty,
337 &PropertyBuffer::FindCustomProperty,
339 property = static_cast<const PropertyInputImpl*>( baseProperty );
341 if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
343 if( index == Dali::PropertyBuffer::Property::SIZE )
346 DALI_ASSERT_ALWAYS( 0 && "MESH_REWORK" );
354 int PropertyBuffer::GetPropertyComponentIndex( Property::Index index ) const
356 return Property::INVALID_COMPONENT_INDEX;
359 bool PropertyBuffer::OnStage() const
364 void PropertyBuffer::Connect()
369 void PropertyBuffer::Disconnect()
374 PropertyBuffer::~PropertyBuffer()
376 if( EventThreadServices::IsCoreRunning() )
378 EventThreadServices& eventThreadServices = GetEventThreadServices();
379 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
380 RemoveMessage( updateManager, updateManager.GetPropertyBufferOwner(), *mSceneObject );
382 eventThreadServices.UnregisterObject( this );
386 PropertyBuffer::PropertyBuffer()
387 : mSceneObject( NULL ),
388 mBufferFormat( NULL ),
394 void PropertyBuffer::Initialize()
396 EventThreadServices& eventThreadServices = GetEventThreadServices();
397 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
399 mSceneObject = new SceneGraph::PropertyBuffer();
400 AddMessage( updateManager, updateManager.GetPropertyBufferOwner(), *mSceneObject );
402 eventThreadServices.RegisterObject( this );
405 void PropertyBuffer::FormatChanged()
407 size_t numComponents = mFormat.Count();
410 DALI_ASSERT_DEBUG( mBufferFormat == NULL && "PropertyFormat should not be set yet" );
411 Format* bufferFormat = new Format();
412 bufferFormat->components.resize( numComponents );
414 unsigned int currentAlignment = 0u;
415 unsigned int maxAlignmentRequired = 0u;
417 for( size_t i = 0u; i < numComponents; ++i )
419 StringValuePair component = mFormat.GetPair( i );
422 bufferFormat->components[i].name = component.first;
424 // enums are stored in the map as int
425 Property::Type type = Property::Type( component.second.Get<int>() );
427 // Get the size and alignment
428 unsigned int elementSize = GetPropertyImplementationSize( type );
429 unsigned int elementAlignment = GetPropertyImplementationAlignment( type );
431 // check if current alignment is compatible with new member
432 if( unsigned int offset = currentAlignment % elementAlignment )
434 // Not compatible, realign
435 currentAlignment = currentAlignment + elementSize - offset;
438 // write to the format
439 bufferFormat->components[i].size = elementSize;
440 bufferFormat->components[i].offset = currentAlignment;
441 bufferFormat->components[i].type = type;
444 currentAlignment += elementSize;
446 // update max alignment requirement
447 if( elementAlignment > maxAlignmentRequired )
449 maxAlignmentRequired = elementAlignment;
454 // Check the alignment for the maxAlignment required to calculate the size of the format
455 if( unsigned int offset = currentAlignment % maxAlignmentRequired )
457 // Not compatible, realign
458 currentAlignment = currentAlignment + maxAlignmentRequired - offset;
461 // Set the format size
462 bufferFormat->size = currentAlignment;
464 mBufferFormat = bufferFormat;
466 SceneGraph::SetFormatMessage( GetEventThreadServices(),
476 void PropertyBuffer::SizeChanged()
478 // Check if format and size have been set yet
479 if( mBufferFormat != NULL )
481 unsigned int bufferSize = mBufferFormat->size * mSize;
482 mBuffer.Resize( bufferSize );
486 unsigned int GetPropertyImplementationSize( Property::Type& propertyType )
488 unsigned int size = 0u;
490 switch( propertyType )
493 case Property::TYPE_COUNT:
494 case Property::STRING:
495 case Property::ARRAY:
498 DALI_ASSERT_ALWAYS( "No size for properties with no type, or dynamic sizes" );
501 case Property::BOOLEAN:
503 size = sizeof( PropertyImplementationType< Property::BOOLEAN >::Type );
506 case Property::INTEGER:
508 size = sizeof( PropertyImplementationType< Property::INTEGER >::Type );
511 case Property::UNSIGNED_INTEGER:
513 size = sizeof( PropertyImplementationType< Property::UNSIGNED_INTEGER >::Type );
516 case Property::FLOAT:
518 size = sizeof( PropertyImplementationType< Property::FLOAT >::Type );
521 case Property::VECTOR2:
523 size = sizeof( PropertyImplementationType< Property::VECTOR2 >::Type );
526 case Property::VECTOR3:
528 size = sizeof( PropertyImplementationType< Property::VECTOR3 >::Type );
531 case Property::VECTOR4:
533 size = sizeof( PropertyImplementationType< Property::VECTOR4 >::Type );
536 case Property::MATRIX3:
538 size = sizeof( PropertyImplementationType< Property::MATRIX3 >::Type );
541 case Property::MATRIX:
543 size = sizeof( PropertyImplementationType< Property::MATRIX >::Type );
546 case Property::RECTANGLE:
548 size = sizeof( PropertyImplementationType< Property::RECTANGLE >::Type );
551 case Property::ROTATION:
553 size = sizeof( PropertyImplementationType< Property::ROTATION >::Type );
562 } // namespace Internal