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 const SceneGraph::PropertyBuffer* PropertyBuffer::GetPropertyBufferSceneObject() const
191 void PropertyBuffer::SetFormat( Dali::Property::Map& format )
193 DALI_ASSERT_ALWAYS( format.Count() && "Format cannot be empty." );
195 DALI_ASSERT_DEBUG( 0 == mFormat.Count() && "Format of property buffer can only be set once." );
202 unsigned int PropertyBuffer::GetDefaultPropertyCount() const
204 return PROPERTY_BUFFER_IMPL.GetDefaultPropertyCount();
207 void PropertyBuffer::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
209 PROPERTY_BUFFER_IMPL.GetDefaultPropertyIndices( indices );
212 const char* PropertyBuffer::GetDefaultPropertyName(Property::Index index) const
214 return PROPERTY_BUFFER_IMPL.GetDefaultPropertyName( index );
217 Property::Index PropertyBuffer::GetDefaultPropertyIndex( const std::string& name ) const
219 return PROPERTY_BUFFER_IMPL.GetDefaultPropertyIndex( name );
222 bool PropertyBuffer::IsDefaultPropertyWritable( Property::Index index ) const
224 return PROPERTY_BUFFER_IMPL.IsDefaultPropertyWritable( index );
227 bool PropertyBuffer::IsDefaultPropertyAnimatable( Property::Index index ) const
229 return PROPERTY_BUFFER_IMPL.IsDefaultPropertyAnimatable( index );
232 bool PropertyBuffer::IsDefaultPropertyAConstraintInput( Property::Index index ) const
234 return PROPERTY_BUFFER_IMPL.IsDefaultPropertyAConstraintInput( index );
237 Property::Type PropertyBuffer::GetDefaultPropertyType( Property::Index index ) const
239 return PROPERTY_BUFFER_IMPL.GetDefaultPropertyType( index );
242 void PropertyBuffer::SetDefaultProperty( Property::Index index,
243 const Property::Value& propertyValue )
247 case Dali::PropertyBuffer::Property::SIZE:
249 SetSize( propertyValue.Get<int>() );
252 case Dali::PropertyBuffer::Property::BUFFER_FORMAT:
254 DALI_ASSERT_ALWAYS( 0 && "MESH_REWORK" );
260 void PropertyBuffer::SetSceneGraphProperty( Property::Index index,
261 const PropertyMetadata& entry,
262 const Property::Value& value )
264 PROPERTY_BUFFER_IMPL.SetSceneGraphProperty( GetEventThreadServices(), this, index, entry, value );
267 Property::Value PropertyBuffer::GetDefaultProperty( Property::Index index ) const
269 Property::Value value;
273 case Dali::PropertyBuffer::Property::SIZE:
275 value = static_cast<int>( GetSize() ); // @todo MESH_REWORK Add a size_t type to PropertyValue
278 case Dali::PropertyBuffer::Property::BUFFER_FORMAT:
280 DALI_ASSERT_ALWAYS( 0 && "MESH_REWORK" );
287 const SceneGraph::PropertyOwner* PropertyBuffer::GetPropertyOwner() const
292 const SceneGraph::PropertyOwner* PropertyBuffer::GetSceneObject() const
297 const SceneGraph::PropertyBase* PropertyBuffer::GetSceneObjectAnimatableProperty( Property::Index index ) const
299 DALI_ASSERT_ALWAYS( IsPropertyAnimatable(index) && "Property is not animatable" );
300 const SceneGraph::PropertyBase* property = NULL;
304 property = PROPERTY_BUFFER_IMPL.GetRegisteredSceneGraphProperty(
306 &PropertyBuffer::FindAnimatableProperty,
307 &PropertyBuffer::FindCustomProperty,
310 if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
312 DALI_ASSERT_ALWAYS( 0 && "Property is not animatable" );
319 const PropertyInputImpl* PropertyBuffer::GetSceneObjectInputProperty( Property::Index index ) const
321 const PropertyInputImpl* property = NULL;
325 const SceneGraph::PropertyBase* baseProperty =
326 PROPERTY_BUFFER_IMPL.GetRegisteredSceneGraphProperty( this,
327 &PropertyBuffer::FindAnimatableProperty,
328 &PropertyBuffer::FindCustomProperty,
330 property = static_cast<const PropertyInputImpl*>( baseProperty );
332 if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
334 if( index == Dali::PropertyBuffer::Property::SIZE )
337 DALI_ASSERT_ALWAYS( 0 && "MESH_REWORK" );
345 int PropertyBuffer::GetPropertyComponentIndex( Property::Index index ) const
347 return Property::INVALID_COMPONENT_INDEX;
350 bool PropertyBuffer::OnStage() const
355 void PropertyBuffer::Connect()
360 void PropertyBuffer::Disconnect()
365 PropertyBuffer::~PropertyBuffer()
369 PropertyBuffer::PropertyBuffer()
370 : mSceneObject( NULL ),
371 mBufferFormat( NULL ),
377 void PropertyBuffer::Initialize()
379 EventThreadServices& eventThreadServices = GetEventThreadServices();
380 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
382 DALI_ASSERT_ALWAYS( EventThreadServices::IsCoreRunning() && "Core is not running" );
384 mSceneObject = new SceneGraph::PropertyBuffer();
385 AddMessage( updateManager, updateManager.GetPropertyBufferOwner(), *mSceneObject );
388 void PropertyBuffer::FormatChanged()
390 size_t numComponents = mFormat.Count();
393 DALI_ASSERT_DEBUG( mBufferFormat == NULL && "PropertyFormat should not be set yet" );
394 Format* bufferFormat = new Format();
395 bufferFormat->components.resize( numComponents );
397 unsigned int currentAlignment = 0u;
398 unsigned int maxAlignmentRequired = 0u;
400 for( size_t i = 0u; i < numComponents; ++i )
402 StringValuePair component = mFormat.GetPair( i );
405 bufferFormat->components[i].name = component.first;
407 // enums are stored in the map as int
408 Property::Type type = Property::Type( component.second.Get<int>() );
410 // Get the size and alignment
411 unsigned int elementSize = GetPropertyImplementationSize( type );
412 unsigned int elementAlignment = GetPropertyImplementationAlignment( type );
414 // check if current alignment is compatible with new member
415 if( unsigned int offset = currentAlignment % elementAlignment )
417 // Not compatible, realign
418 currentAlignment = currentAlignment + elementSize - offset;
421 // write to the format
422 bufferFormat->components[i].size = elementSize;
423 bufferFormat->components[i].offset = currentAlignment;
424 bufferFormat->components[i].type = type;
427 currentAlignment += elementSize;
429 // update max alignment requirement
430 if( elementAlignment > maxAlignmentRequired )
432 maxAlignmentRequired = elementAlignment;
437 // Check the alignment for the maxAlignment required to calculate the size of the format
438 if( unsigned int offset = currentAlignment % maxAlignmentRequired )
440 // Not compatible, realign
441 currentAlignment = currentAlignment + maxAlignmentRequired - offset;
444 // Set the format size
445 bufferFormat->size = currentAlignment;
447 mBufferFormat = bufferFormat;
449 SceneGraph::SetFormatMessage( GetEventThreadServices(),
459 void PropertyBuffer::SizeChanged()
461 // Check if format and size have been set yet
462 if( mBufferFormat != NULL )
464 unsigned int bufferSize = mBufferFormat->size * mSize;
465 mBuffer.Resize( bufferSize );
469 unsigned int GetPropertyImplementationSize( Property::Type& propertyType )
471 unsigned int size = 0u;
473 switch( propertyType )
476 case Property::TYPE_COUNT:
477 case Property::STRING:
478 case Property::ARRAY:
481 DALI_ASSERT_ALWAYS( "No size for properties with no type, or dynamic sizes" );
484 case Property::BOOLEAN:
486 size = sizeof( PropertyImplementationType< Property::BOOLEAN >::Type );
489 case Property::INTEGER:
491 size = sizeof( PropertyImplementationType< Property::INTEGER >::Type );
494 case Property::UNSIGNED_INTEGER:
496 size = sizeof( PropertyImplementationType< Property::UNSIGNED_INTEGER >::Type );
499 case Property::FLOAT:
501 size = sizeof( PropertyImplementationType< Property::FLOAT >::Type );
504 case Property::VECTOR2:
506 size = sizeof( PropertyImplementationType< Property::VECTOR2 >::Type );
509 case Property::VECTOR3:
511 size = sizeof( PropertyImplementationType< Property::VECTOR3 >::Type );
514 case Property::VECTOR4:
516 size = sizeof( PropertyImplementationType< Property::VECTOR4 >::Type );
519 case Property::MATRIX3:
521 size = sizeof( PropertyImplementationType< Property::MATRIX3 >::Type );
524 case Property::MATRIX:
526 size = sizeof( PropertyImplementationType< Property::MATRIX >::Type );
529 case Property::RECTANGLE:
531 size = sizeof( PropertyImplementationType< Property::RECTANGLE >::Type );
534 case Property::ROTATION:
536 size = sizeof( PropertyImplementationType< Property::ROTATION >::Type );
545 } // namespace Internal