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::STRING:
91 DALI_ASSERT_ALWAYS( "No size for properties with no type, or dynamic sizes" );
94 case Property::BOOLEAN:
96 alignment = PropertyImplementationTypeAlignment< Property::BOOLEAN >::VALUE;
99 case Property::INTEGER:
101 alignment = PropertyImplementationTypeAlignment< Property::INTEGER >::VALUE;
104 case Property::UNSIGNED_INTEGER:
106 alignment = PropertyImplementationTypeAlignment< Property::UNSIGNED_INTEGER >::VALUE;
109 case Property::FLOAT:
111 alignment = PropertyImplementationTypeAlignment< Property::FLOAT >::VALUE;
114 case Property::VECTOR2:
116 alignment = PropertyImplementationTypeAlignment< Property::VECTOR2 >::VALUE;
119 case Property::VECTOR3:
121 alignment = PropertyImplementationTypeAlignment< Property::VECTOR3 >::VALUE;
124 case Property::VECTOR4:
126 alignment = PropertyImplementationTypeAlignment< Property::VECTOR4 >::VALUE;
129 case Property::MATRIX3:
131 alignment = PropertyImplementationTypeAlignment< Property::MATRIX3 >::VALUE;
134 case Property::MATRIX:
136 alignment = PropertyImplementationTypeAlignment< Property::MATRIX >::VALUE;
139 case Property::RECTANGLE:
141 alignment = PropertyImplementationTypeAlignment< Property::RECTANGLE >::VALUE;
144 case Property::ROTATION:
146 alignment = PropertyImplementationTypeAlignment< Property::ROTATION >::VALUE;
154 } // unnamed namespace
156 PropertyBufferPtr PropertyBuffer::New()
158 PropertyBufferPtr propertyBuffer( new PropertyBuffer() );
159 propertyBuffer->Initialize();
161 return propertyBuffer;
164 void PropertyBuffer::SetSize( std::size_t size )
170 SceneGraph::SetSizeMessage( GetEventThreadServices(),
175 std::size_t PropertyBuffer::GetSize() const
180 void PropertyBuffer::SetData( const void* data )
182 DALI_ASSERT_DEBUG( mFormat.Count() && "Format must be set before setting the data." );
184 DALI_ASSERT_ALWAYS( mSize && "Size of the buffer must be set before setting the data." );
186 const char* source = static_cast<const char*>( data );
187 std::copy( source, source + mBuffer.Size(), &mBuffer[0] );
189 SceneGraph::SetDataMessage( GetEventThreadServices(),
191 new SceneGraph::PropertyBuffer::BufferType( mBuffer ) );
194 const SceneGraph::PropertyBuffer* PropertyBuffer::GetPropertyBufferSceneObject() const
199 void PropertyBuffer::SetFormat( Dali::Property::Map& format )
201 DALI_ASSERT_ALWAYS( format.Count() && "Format cannot be empty." );
203 DALI_ASSERT_DEBUG( 0 == mFormat.Count() && "Format of property buffer can only be set once." );
210 unsigned int PropertyBuffer::GetDefaultPropertyCount() const
212 return PROPERTY_BUFFER_IMPL.GetDefaultPropertyCount();
215 void PropertyBuffer::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
217 PROPERTY_BUFFER_IMPL.GetDefaultPropertyIndices( indices );
220 const char* PropertyBuffer::GetDefaultPropertyName(Property::Index index) const
222 return PROPERTY_BUFFER_IMPL.GetDefaultPropertyName( index );
225 Property::Index PropertyBuffer::GetDefaultPropertyIndex( const std::string& name ) const
227 return PROPERTY_BUFFER_IMPL.GetDefaultPropertyIndex( name );
230 bool PropertyBuffer::IsDefaultPropertyWritable( Property::Index index ) const
232 return PROPERTY_BUFFER_IMPL.IsDefaultPropertyWritable( index );
235 bool PropertyBuffer::IsDefaultPropertyAnimatable( Property::Index index ) const
237 return PROPERTY_BUFFER_IMPL.IsDefaultPropertyAnimatable( index );
240 bool PropertyBuffer::IsDefaultPropertyAConstraintInput( Property::Index index ) const
242 return PROPERTY_BUFFER_IMPL.IsDefaultPropertyAConstraintInput( index );
245 Property::Type PropertyBuffer::GetDefaultPropertyType( Property::Index index ) const
247 return PROPERTY_BUFFER_IMPL.GetDefaultPropertyType( index );
250 void PropertyBuffer::SetDefaultProperty( Property::Index index,
251 const Property::Value& propertyValue )
255 case Dali::PropertyBuffer::Property::SIZE:
257 SetSize( propertyValue.Get<unsigned int>() );
260 case Dali::PropertyBuffer::Property::BUFFER_FORMAT:
262 DALI_ASSERT_ALWAYS( 0 && "MESH_REWORK" );
268 void PropertyBuffer::SetSceneGraphProperty( Property::Index index,
269 const PropertyMetadata& entry,
270 const Property::Value& value )
272 PROPERTY_BUFFER_IMPL.SetSceneGraphProperty( GetEventThreadServices(), this, index, entry, value );
275 Property::Value PropertyBuffer::GetDefaultProperty( Property::Index index ) const
277 Property::Value value;
281 case Dali::PropertyBuffer::Property::SIZE:
283 value = static_cast<unsigned int>( GetSize() ); // @todo MESH_REWORK Add a size_t type to PropertyValue
286 case Dali::PropertyBuffer::Property::BUFFER_FORMAT:
288 DALI_ASSERT_ALWAYS( 0 && "MESH_REWORK" );
295 const SceneGraph::PropertyOwner* PropertyBuffer::GetPropertyOwner() const
300 const SceneGraph::PropertyOwner* PropertyBuffer::GetSceneObject() const
305 const SceneGraph::PropertyBase* PropertyBuffer::GetSceneObjectAnimatableProperty( Property::Index index ) const
307 DALI_ASSERT_ALWAYS( IsPropertyAnimatable(index) && "Property is not animatable" );
308 const SceneGraph::PropertyBase* property = NULL;
312 property = PROPERTY_BUFFER_IMPL.GetRegisteredSceneGraphProperty(
314 &PropertyBuffer::FindAnimatableProperty,
315 &PropertyBuffer::FindCustomProperty,
318 if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
320 DALI_ASSERT_ALWAYS( 0 && "Property is not animatable" );
327 const PropertyInputImpl* PropertyBuffer::GetSceneObjectInputProperty( Property::Index index ) const
329 const PropertyInputImpl* property = NULL;
333 const SceneGraph::PropertyBase* baseProperty =
334 PROPERTY_BUFFER_IMPL.GetRegisteredSceneGraphProperty( this,
335 &PropertyBuffer::FindAnimatableProperty,
336 &PropertyBuffer::FindCustomProperty,
338 property = static_cast<const PropertyInputImpl*>( baseProperty );
340 if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
342 if( index == Dali::PropertyBuffer::Property::SIZE )
345 DALI_ASSERT_ALWAYS( 0 && "MESH_REWORK" );
353 int PropertyBuffer::GetPropertyComponentIndex( Property::Index index ) const
355 return Property::INVALID_COMPONENT_INDEX;
358 bool PropertyBuffer::OnStage() const
363 void PropertyBuffer::Connect()
368 void PropertyBuffer::Disconnect()
373 PropertyBuffer::~PropertyBuffer()
375 if( EventThreadServices::IsCoreRunning() )
377 EventThreadServices& eventThreadServices = GetEventThreadServices();
378 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
379 RemoveMessage( updateManager, updateManager.GetPropertyBufferOwner(), *mSceneObject );
381 eventThreadServices.UnregisterObject( this );
385 PropertyBuffer::PropertyBuffer()
386 : mSceneObject( NULL ),
387 mBufferFormat( NULL ),
393 void PropertyBuffer::Initialize()
395 EventThreadServices& eventThreadServices = GetEventThreadServices();
396 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
398 mSceneObject = new SceneGraph::PropertyBuffer();
399 AddMessage( updateManager, updateManager.GetPropertyBufferOwner(), *mSceneObject );
401 eventThreadServices.RegisterObject( this );
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::STRING:
493 case Property::ARRAY:
496 DALI_ASSERT_ALWAYS( "No size for properties with no type, or dynamic sizes" );
499 case Property::BOOLEAN:
501 size = sizeof( PropertyImplementationType< Property::BOOLEAN >::Type );
504 case Property::INTEGER:
506 size = sizeof( PropertyImplementationType< Property::INTEGER >::Type );
509 case Property::UNSIGNED_INTEGER:
511 size = sizeof( PropertyImplementationType< Property::UNSIGNED_INTEGER >::Type );
514 case Property::FLOAT:
516 size = sizeof( PropertyImplementationType< Property::FLOAT >::Type );
519 case Property::VECTOR2:
521 size = sizeof( PropertyImplementationType< Property::VECTOR2 >::Type );
524 case Property::VECTOR3:
526 size = sizeof( PropertyImplementationType< Property::VECTOR3 >::Type );
529 case Property::VECTOR4:
531 size = sizeof( PropertyImplementationType< Property::VECTOR4 >::Type );
534 case Property::MATRIX3:
536 size = sizeof( PropertyImplementationType< Property::MATRIX3 >::Type );
539 case Property::MATRIX:
541 size = sizeof( PropertyImplementationType< Property::MATRIX >::Type );
544 case Property::RECTANGLE:
546 size = sizeof( PropertyImplementationType< Property::RECTANGLE >::Type );
549 case Property::ROTATION:
551 size = sizeof( PropertyImplementationType< Property::ROTATION >::Type );
560 } // namespace Internal