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", 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::FLOAT:
106 alignment = PropertyImplementationTypeAlignment< Property::FLOAT >::VALUE;
109 case Property::VECTOR2:
111 alignment = PropertyImplementationTypeAlignment< Property::VECTOR2 >::VALUE;
114 case Property::VECTOR3:
116 alignment = PropertyImplementationTypeAlignment< Property::VECTOR3 >::VALUE;
119 case Property::VECTOR4:
121 alignment = PropertyImplementationTypeAlignment< Property::VECTOR4 >::VALUE;
124 case Property::MATRIX3:
126 alignment = PropertyImplementationTypeAlignment< Property::MATRIX3 >::VALUE;
129 case Property::MATRIX:
131 alignment = PropertyImplementationTypeAlignment< Property::MATRIX >::VALUE;
134 case Property::RECTANGLE:
136 alignment = PropertyImplementationTypeAlignment< Property::RECTANGLE >::VALUE;
139 case Property::ROTATION:
141 alignment = PropertyImplementationTypeAlignment< Property::ROTATION >::VALUE;
149 } // unnamed namespace
151 PropertyBufferPtr PropertyBuffer::New()
153 PropertyBufferPtr propertyBuffer( new PropertyBuffer() );
154 propertyBuffer->Initialize();
156 return propertyBuffer;
159 void PropertyBuffer::SetSize( std::size_t size )
165 SceneGraph::SetSizeMessage( GetEventThreadServices(),
170 std::size_t PropertyBuffer::GetSize() const
175 void PropertyBuffer::SetData( const void* data )
177 DALI_ASSERT_DEBUG( mFormat.Count() && "Format must be set before setting the data." );
179 DALI_ASSERT_ALWAYS( mSize && "Size of the buffer must be set before setting the data." );
181 const char* source = static_cast<const char*>( data );
182 std::copy( source, source + mBuffer.Size(), &mBuffer[0] );
184 SceneGraph::SetDataMessage( GetEventThreadServices(),
186 new SceneGraph::PropertyBuffer::BufferType( mBuffer ) );
189 const SceneGraph::PropertyBuffer* PropertyBuffer::GetPropertyBufferSceneObject() const
194 void PropertyBuffer::SetFormat( Dali::Property::Map& format )
196 DALI_ASSERT_ALWAYS( format.Count() && "Format cannot be empty." );
198 DALI_ASSERT_DEBUG( 0 == mFormat.Count() && "Format of property buffer can only be set once." );
205 unsigned int PropertyBuffer::GetDefaultPropertyCount() const
207 return PROPERTY_BUFFER_IMPL.GetDefaultPropertyCount();
210 void PropertyBuffer::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
212 PROPERTY_BUFFER_IMPL.GetDefaultPropertyIndices( indices );
215 const char* PropertyBuffer::GetDefaultPropertyName(Property::Index index) const
217 return PROPERTY_BUFFER_IMPL.GetDefaultPropertyName( index );
220 Property::Index PropertyBuffer::GetDefaultPropertyIndex( const std::string& name ) const
222 return PROPERTY_BUFFER_IMPL.GetDefaultPropertyIndex( name );
225 bool PropertyBuffer::IsDefaultPropertyWritable( Property::Index index ) const
227 return PROPERTY_BUFFER_IMPL.IsDefaultPropertyWritable( index );
230 bool PropertyBuffer::IsDefaultPropertyAnimatable( Property::Index index ) const
232 return PROPERTY_BUFFER_IMPL.IsDefaultPropertyAnimatable( index );
235 bool PropertyBuffer::IsDefaultPropertyAConstraintInput( Property::Index index ) const
237 return PROPERTY_BUFFER_IMPL.IsDefaultPropertyAConstraintInput( index );
240 Property::Type PropertyBuffer::GetDefaultPropertyType( Property::Index index ) const
242 return PROPERTY_BUFFER_IMPL.GetDefaultPropertyType( index );
245 void PropertyBuffer::SetDefaultProperty( Property::Index index,
246 const Property::Value& propertyValue )
250 case Dali::PropertyBuffer::Property::SIZE:
252 SetSize( propertyValue.Get<int>() );
255 case Dali::PropertyBuffer::Property::BUFFER_FORMAT:
257 DALI_ASSERT_ALWAYS( 0 && "MESH_REWORK" );
263 void PropertyBuffer::SetSceneGraphProperty( Property::Index index,
264 const PropertyMetadata& entry,
265 const Property::Value& value )
267 PROPERTY_BUFFER_IMPL.SetSceneGraphProperty( GetEventThreadServices(), this, index, entry, value );
270 Property::Value PropertyBuffer::GetDefaultProperty( Property::Index index ) const
272 Property::Value value;
276 case Dali::PropertyBuffer::Property::SIZE:
278 value = static_cast<int>( GetSize() );
281 case Dali::PropertyBuffer::Property::BUFFER_FORMAT:
283 DALI_ASSERT_ALWAYS( 0 && "MESH_REWORK" );
290 const SceneGraph::PropertyOwner* PropertyBuffer::GetPropertyOwner() const
295 const SceneGraph::PropertyOwner* PropertyBuffer::GetSceneObject() const
300 const SceneGraph::PropertyBase* PropertyBuffer::GetSceneObjectAnimatableProperty( Property::Index index ) const
302 DALI_ASSERT_ALWAYS( IsPropertyAnimatable(index) && "Property is not animatable" );
303 const SceneGraph::PropertyBase* property = NULL;
307 property = PROPERTY_BUFFER_IMPL.GetRegisteredSceneGraphProperty(
309 &PropertyBuffer::FindAnimatableProperty,
310 &PropertyBuffer::FindCustomProperty,
313 if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
315 DALI_ASSERT_ALWAYS( 0 && "Property is not animatable" );
322 const PropertyInputImpl* PropertyBuffer::GetSceneObjectInputProperty( Property::Index index ) const
324 const PropertyInputImpl* property = NULL;
328 const SceneGraph::PropertyBase* baseProperty =
329 PROPERTY_BUFFER_IMPL.GetRegisteredSceneGraphProperty( this,
330 &PropertyBuffer::FindAnimatableProperty,
331 &PropertyBuffer::FindCustomProperty,
333 property = static_cast<const PropertyInputImpl*>( baseProperty );
335 if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
337 if( index == Dali::PropertyBuffer::Property::SIZE )
340 DALI_ASSERT_ALWAYS( 0 && "MESH_REWORK" );
348 int PropertyBuffer::GetPropertyComponentIndex( Property::Index index ) const
350 return Property::INVALID_COMPONENT_INDEX;
353 bool PropertyBuffer::OnStage() const
358 void PropertyBuffer::Connect()
363 void PropertyBuffer::Disconnect()
368 PropertyBuffer::~PropertyBuffer()
370 if( EventThreadServices::IsCoreRunning() )
372 EventThreadServices& eventThreadServices = GetEventThreadServices();
373 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
374 RemoveMessage( updateManager, updateManager.GetPropertyBufferOwner(), *mSceneObject );
376 eventThreadServices.UnregisterObject( this );
380 PropertyBuffer::PropertyBuffer()
381 : mSceneObject( NULL ),
382 mBufferFormat( NULL ),
388 void PropertyBuffer::Initialize()
390 EventThreadServices& eventThreadServices = GetEventThreadServices();
391 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
393 mSceneObject = new SceneGraph::PropertyBuffer();
394 AddMessage( updateManager, updateManager.GetPropertyBufferOwner(), *mSceneObject );
396 eventThreadServices.RegisterObject( this );
399 void PropertyBuffer::FormatChanged()
401 size_t numComponents = mFormat.Count();
404 DALI_ASSERT_DEBUG( mBufferFormat == NULL && "PropertyFormat should not be set yet" );
405 Format* bufferFormat = new Format();
406 bufferFormat->components.resize( numComponents );
408 unsigned int currentAlignment = 0u;
409 unsigned int maxAlignmentRequired = 0u;
411 for( size_t i = 0u; i < numComponents; ++i )
413 StringValuePair component = mFormat.GetPair( i );
416 bufferFormat->components[i].name = component.first;
418 // enums are stored in the map as int
419 Property::Type type = Property::Type( component.second.Get<int>() );
421 // Get the size and alignment
422 unsigned int elementSize = GetPropertyImplementationSize( type );
423 unsigned int elementAlignment = GetPropertyImplementationAlignment( type );
425 // check if current alignment is compatible with new member
426 if( unsigned int offset = currentAlignment % elementAlignment )
428 // Not compatible, realign
429 currentAlignment = currentAlignment + elementSize - offset;
432 // write to the format
433 bufferFormat->components[i].size = elementSize;
434 bufferFormat->components[i].offset = currentAlignment;
435 bufferFormat->components[i].type = type;
438 currentAlignment += elementSize;
440 // update max alignment requirement
441 if( elementAlignment > maxAlignmentRequired )
443 maxAlignmentRequired = elementAlignment;
448 // Check the alignment for the maxAlignment required to calculate the size of the format
449 if( unsigned int offset = currentAlignment % maxAlignmentRequired )
451 // Not compatible, realign
452 currentAlignment = currentAlignment + maxAlignmentRequired - offset;
455 // Set the format size
456 bufferFormat->size = currentAlignment;
458 mBufferFormat = bufferFormat;
460 SceneGraph::SetFormatMessage( GetEventThreadServices(),
470 void PropertyBuffer::SizeChanged()
472 // Check if format and size have been set yet
473 if( mBufferFormat != NULL )
475 unsigned int bufferSize = mBufferFormat->size * mSize;
476 mBuffer.Resize( bufferSize );
480 unsigned int GetPropertyImplementationSize( Property::Type& propertyType )
482 unsigned int size = 0u;
484 switch( propertyType )
487 case Property::STRING:
488 case Property::ARRAY:
491 DALI_ASSERT_ALWAYS( "No size for properties with no type, or dynamic sizes" );
494 case Property::BOOLEAN:
496 size = sizeof( PropertyImplementationType< Property::BOOLEAN >::Type );
499 case Property::INTEGER:
501 size = sizeof( PropertyImplementationType< Property::INTEGER >::Type );
504 case Property::FLOAT:
506 size = sizeof( PropertyImplementationType< Property::FLOAT >::Type );
509 case Property::VECTOR2:
511 size = sizeof( PropertyImplementationType< Property::VECTOR2 >::Type );
514 case Property::VECTOR3:
516 size = sizeof( PropertyImplementationType< Property::VECTOR3 >::Type );
519 case Property::VECTOR4:
521 size = sizeof( PropertyImplementationType< Property::VECTOR4 >::Type );
524 case Property::MATRIX3:
526 size = sizeof( PropertyImplementationType< Property::MATRIX3 >::Type );
529 case Property::MATRIX:
531 size = sizeof( PropertyImplementationType< Property::MATRIX >::Type );
534 case Property::RECTANGLE:
536 size = sizeof( PropertyImplementationType< Property::RECTANGLE >::Type );
539 case Property::ROTATION:
541 size = sizeof( PropertyImplementationType< Property::ROTATION >::Type );
550 } // namespace Internal