/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
// INTERNAL INCLUDES
#include <dali/internal/common/internal-constants.h>
#include <dali/internal/common/memory-pool-object-allocator.h>
-#include <dali/internal/update/node-attachments/node-attachment.h>
#include <dali/internal/update/common/discard-queue.h>
#include <dali/public-api/common/dali-common.h>
#include <dali/public-api/common/constants.h>
-namespace //Unnamed namespace
+namespace
{
-//Memory pool used to allocate new nodes. Memory used by this pool will be released when shutting down DALi
+//Memory pool used to allocate new nodes. Memory used by this pool will be released when process dies
+// or DALI library is unloaded
Dali::Internal::MemoryPoolObjectAllocator<Dali::Internal::SceneGraph::Node> gNodeMemoryPool;
+#ifdef DEBUG_ENABLED
+// keep track of nodes alive, to ensure we have 0 when the process exits or DALi library is unloaded
+int32_t gNodeCount = 0;
+
+// Called when the process is about to exit, Node count should be zero at this point.
+void __attribute__ ((destructor)) ShutDown(void)
+{
+DALI_ASSERT_DEBUG( (gNodeCount == 0) && "Node memory leak");
}
+#endif
+} // Unnamed namespace
namespace Dali
{
namespace SceneGraph
{
-const PositionInheritanceMode Node::DEFAULT_POSITION_INHERITANCE_MODE( INHERIT_PARENT_POSITION );
const ColorMode Node::DEFAULT_COLOR_MODE( USE_OWN_MULTIPLY_PARENT_ALPHA );
+uint32_t Node::mNodeCounter = 0; ///< A counter to provide unique node ids, up-to 4 billion
+
Node* Node::New()
{
- return new ( gNodeMemoryPool.AllocateRawThreadSafe() ) Node();
+ return new ( gNodeMemoryPool.AllocateRawThreadSafe() ) Node;
+}
+
+void Node::Delete( Node* node )
+{
+ // check we have a node not a layer
+ if( !node->mIsLayer )
+ {
+ // Manually call the destructor
+ node->~Node();
+
+ // Mark the memory it used as free in the memory pool
+ gNodeMemoryPool.FreeThreadSafe( node );
+ }
+ else
+ {
+ // not in the pool, just delete it.
+ delete node;
+ }
}
Node::Node()
-: mParentOrigin( ParentOrigin::DEFAULT ),
- mAnchorPoint( AnchorPoint::DEFAULT ),
- mSize(), // zero initialized by default
- mPosition(), // zero initialized by default
- mOrientation(), // initialized to identity by default
- mScale( Vector3::ONE ),
+: mTransformManager( nullptr ),
+ mTransformId( INVALID_TRANSFORM_ID ),
+ mParentOrigin( TRANSFORM_PROPERTY_PARENT_ORIGIN ),
+ mAnchorPoint( TRANSFORM_PROPERTY_ANCHOR_POINT ),
+ mSize( TRANSFORM_PROPERTY_SIZE ), // Zero initialized by default
+ mPosition( TRANSFORM_PROPERTY_POSITION ), // Zero initialized by default
+ mOrientation(), // Initialized to identity by default
+ mScale( TRANSFORM_PROPERTY_SCALE ),
mVisible( true ),
+ mCulled( false ),
mColor( Color::WHITE ),
- mWorldPosition(), // zero initialized by default
- mWorldOrientation(), // initialized to identity by default
- mWorldScale( Vector3::ONE ),
+ mUpdateSizeHint( Vector3::ZERO ),
+ mWorldPosition( TRANSFORM_PROPERTY_WORLD_POSITION, Vector3( 0.0f,0.0f,0.0f ) ), // Zero initialized by default
+ mWorldScale( TRANSFORM_PROPERTY_WORLD_SCALE, Vector3( 1.0f,1.0f,1.0f ) ),
+ mWorldOrientation(), // Initialized to identity by default
mWorldMatrix(),
mWorldColor( Color::WHITE ),
- mParent( NULL ),
- mExclusiveRenderTask( NULL ),
- mAttachment( NULL ),
+ mClippingSortModifier( 0u ),
+ mId( ++mNodeCounter ),
+ mParent( nullptr ),
+ mExclusiveRenderTask( nullptr ),
mChildren(),
+ mClippingDepth( 0u ),
+ mScissorDepth( 0u ),
+ mDepthIndex( 0u ),
+ mDirtyFlags( NodePropertyFlags::ALL ),
mRegenerateUniformMap( 0 ),
- mDepth(0u),
- mDirtyFlags(AllFlags),
- mIsRoot( false ),
- mInheritOrientation( true ),
- mInheritScale( true ),
mDrawMode( DrawMode::NORMAL ),
- mPositionInheritanceMode( DEFAULT_POSITION_INHERITANCE_MODE ),
- mColorMode( DEFAULT_COLOR_MODE )
+ mColorMode( DEFAULT_COLOR_MODE ),
+ mClippingMode( ClippingMode::DISABLED ),
+ mIsRoot( false ),
+ mIsLayer( false ),
+ mPositionUsesAnchorPoint( true )
{
mUniformMapChanged[0] = 0u;
mUniformMapChanged[1] = 0u;
+
+#ifdef DEBUG_ENABLED
+ gNodeCount++;
+#endif
+
}
Node::~Node()
{
-}
+ if( mTransformId != INVALID_TRANSFORM_ID )
+ {
+ mTransformManager->RemoveTransform(mTransformId);
+ }
-void Node::operator delete( void* ptr )
-{
- gNodeMemoryPool.FreeThreadSafe( static_cast<Node*>( ptr ) );
+#ifdef DEBUG_ENABLED
+ gNodeCount--;
+#endif
}
void Node::OnDestroy()
{
- // Node attachments should be notified about the disconnection.
- if ( mAttachment )
- {
- mAttachment->OnDestroy();
- }
-
// Animators, Constraints etc. should be disconnected from the child's properties.
PropertyOwner::Destroy();
}
-void Node::Attach( NodeAttachment& object )
+uint32_t Node::GetId() const
{
- DALI_ASSERT_DEBUG(!mAttachment);
-
- object.SetParent(*this);
-
- mAttachment = &object;
- SetAllDirtyFlags();
+ return mId;
+}
- mAttachment->ConnectedToSceneGraph();
+void Node::CreateTransform( SceneGraph::TransformManager* transformManager )
+{
+ //Create a new transform
+ mTransformManager = transformManager;
+ mTransformId = transformManager->CreateTransform();
+
+ //Initialize all the animatable properties
+ mPosition.Initialize( transformManager, mTransformId );
+ mScale.Initialize( transformManager, mTransformId );
+ mOrientation.Initialize( transformManager, mTransformId );
+ mSize.Initialize( transformManager, mTransformId );
+ mParentOrigin.Initialize( transformManager, mTransformId );
+ mAnchorPoint.Initialize( transformManager, mTransformId );
+
+ //Initialize all the input properties
+ mWorldPosition.Initialize( transformManager, mTransformId );
+ mWorldScale.Initialize( transformManager, mTransformId );
+ mWorldOrientation.Initialize( transformManager, mTransformId );
+ mWorldMatrix.Initialize( transformManager, mTransformId );
+
+ //Set whether the position should use the anchor point
+ transformManager->SetPositionUsesAnchorPoint( mTransformId, mPositionUsesAnchorPoint );
}
void Node::SetRoot(bool isRoot)
mIsRoot = isRoot;
}
-void Node::AddUniformMapping( UniformPropertyMapping* map )
+void Node::AddUniformMapping(const UniformPropertyMapping& map)
{
PropertyOwner::AddUniformMapping( map );
mRegenerateUniformMap = 2;
}
-void Node::RemoveUniformMapping( const std::string& uniformName )
+void Node::RemoveUniformMapping( const ConstString& uniformName )
{
PropertyOwner::RemoveUniformMapping( uniformName );
mRegenerateUniformMap = 2;
}
+bool Node::IsAnimationPossible() const
+{
+ return mIsConnectedToSceneGraph;
+}
+
void Node::PrepareRender( BufferIndex bufferIndex )
{
- if(mRegenerateUniformMap != 0 )
+ if( mRegenerateUniformMap != 0 )
{
if( mRegenerateUniformMap == 2 )
{
CollectedUniformMap& localMap = mCollectedUniformMap[ bufferIndex ];
localMap.Resize(0);
- for( unsigned int i=0, count=mUniformMaps.Count(); i<count; ++i )
+ for( UniformMap::SizeType i = 0, count=mUniformMaps.Count(); i<count; ++i )
{
- localMap.PushBack( &mUniformMaps[i] );
+ localMap.PushBack(mUniformMaps[i]);
}
}
else if( mRegenerateUniformMap == 1 )
localMap.Resize( oldMap.Count() );
- unsigned int index=0;
+ CollectedUniformMap::SizeType index = 0;
for( CollectedUniformMap::Iterator iter = oldMap.Begin(), end = oldMap.End() ; iter != end ; ++iter, ++index )
{
localMap[index] = *iter;
void Node::ConnectChild( Node* childNode )
{
DALI_ASSERT_ALWAYS( this != childNode );
- DALI_ASSERT_ALWAYS( IsRoot() || NULL != mParent ); // Parent should be connected first
- DALI_ASSERT_ALWAYS( !childNode->IsRoot() && NULL == childNode->GetParent() ); // Child should be disconnected
+ DALI_ASSERT_ALWAYS( IsRoot() || nullptr != mParent ); // Parent should be connected first
+ DALI_ASSERT_ALWAYS( !childNode->IsRoot() && nullptr == childNode->GetParent() ); // Child should be disconnected
childNode->SetParent( *this );
// Inform property observers of new connection
childNode->ConnectToSceneGraph();
-
- // Inform child node attachment that the node has been added to the stage
- if( childNode->mAttachment )
- {
- childNode->mAttachment->ConnectedToSceneGraph();
- }
}
void Node::DisconnectChild( BufferIndex updateBufferIndex, Node& childNode )
DALI_ASSERT_ALWAYS( childNode.GetParent() == this );
// Find the childNode and remove it
- Node* found( NULL );
+ Node* found( nullptr );
const NodeIter endIter = mChildren.End();
for ( NodeIter iter = mChildren.Begin(); iter != endIter; ++iter )
break; // iter is no longer valid
}
}
- DALI_ASSERT_ALWAYS( NULL != found );
+ DALI_ASSERT_ALWAYS( nullptr != found );
found->RecursiveDisconnectFromSceneGraph( updateBufferIndex );
}
-void Node::RemoveRenderer( Renderer* renderer )
+void Node::AddRenderer( Renderer* renderer )
{
- unsigned int rendererCount( mRenderer.Size() );
- for( unsigned int i(0); i<rendererCount; ++i )
+ // If it is the first renderer added, make sure the world transform will be calculated
+ // in the next update as world transform is not computed if node has no renderers.
+ if( mRenderer.Empty() )
{
- if( mRenderer[i] == renderer )
+ mDirtyFlags |= NodePropertyFlags::TRANSFORM;
+ }
+ else
+ {
+ // Check that it has not been already added.
+ for( auto&& existingRenderer : mRenderer )
{
- mRenderer.Erase( mRenderer.Begin()+i);
- return;
+ if( existingRenderer == renderer )
+ {
+ // Renderer is already in the list.
+ return;
+ }
}
}
+
+ mRenderer.PushBack( renderer );
}
-int Node::GetDirtyFlags() const
+void Node::RemoveRenderer( const Renderer* renderer )
{
- // get initial dirty flags, they are reset ResetDefaultProperties, but setters may have made the node dirty already
- int flags = mDirtyFlags;
- const bool sizeFlag = mSize.IsClean();
-
- if ( !(flags & TransformFlag) )
+ RendererContainer::SizeType rendererCount( mRenderer.Size() );
+ for( RendererContainer::SizeType i = 0; i < rendererCount; ++i )
{
- // Check whether the transform related properties have changed
- if( !sizeFlag ||
- !mPosition.IsClean() ||
- !mOrientation.IsClean() ||
- !mScale.IsClean() ||
- mParentOrigin.InputChanged() || // parent origin and anchor point rarely change
- mAnchorPoint.InputChanged() )
+ if( mRenderer[i] == renderer )
{
- flags |= TransformFlag;
+ mRenderer.Erase( mRenderer.Begin()+i);
+ return;
}
}
+}
+
+NodePropertyFlags Node::GetDirtyFlags() const
+{
+ // get initial dirty flags, they are reset ResetDefaultProperties, but setters may have made the node dirty already
+ NodePropertyFlags flags = mDirtyFlags;
// Check whether the visible property has changed
if ( !mVisible.IsClean() )
{
- flags |= VisibleFlag;
+ flags |= NodePropertyFlags::VISIBLE;
}
// Check whether the color property has changed
if ( !mColor.IsClean() )
{
- flags |= ColorFlag;
+ flags |= NodePropertyFlags::COLOR;
}
- // Check whether the size property has changed
- if ( !sizeFlag )
- {
- flags |= SizeFlag;
- }
-
return flags;
}
-void Node::ResetDefaultProperties( BufferIndex updateBufferIndex )
+NodePropertyFlags Node::GetInheritedDirtyFlags( NodePropertyFlags parentFlags ) const
+{
+ // Size is not inherited. VisibleFlag is inherited
+ static const NodePropertyFlags InheritedDirtyFlags = NodePropertyFlags::TRANSFORM | NodePropertyFlags::VISIBLE | NodePropertyFlags::COLOR;
+ using UnderlyingType = typename std::underlying_type<NodePropertyFlags>::type;
+
+ return static_cast<NodePropertyFlags>( static_cast<UnderlyingType>( mDirtyFlags ) |
+ ( static_cast<UnderlyingType>( parentFlags ) & static_cast<UnderlyingType>( InheritedDirtyFlags ) ) );
+}
+
+void Node::ResetDirtyFlags( BufferIndex updateBufferIndex )
{
- // clear dirty flags in parent origin & anchor point
- mParentOrigin.Clear();
- mAnchorPoint.Clear();
- // Reset default properties
- mSize.ResetToBaseValue( updateBufferIndex );
- mPosition.ResetToBaseValue( updateBufferIndex );
- mOrientation.ResetToBaseValue( updateBufferIndex );
- mScale.ResetToBaseValue( updateBufferIndex );
- mVisible.ResetToBaseValue( updateBufferIndex );
- mColor.ResetToBaseValue( updateBufferIndex );
-
- mDirtyFlags = NothingFlag;
+ mDirtyFlags = NodePropertyFlags::NOTHING;
}
-void Node::SetParent(Node& parentNode)
+void Node::SetParent( Node& parentNode )
{
DALI_ASSERT_ALWAYS(this != &parentNode);
DALI_ASSERT_ALWAYS(!mIsRoot);
- DALI_ASSERT_ALWAYS(mParent == NULL);
+ DALI_ASSERT_ALWAYS(mParent == nullptr);
mParent = &parentNode;
- mDepth = mParent->GetDepth() + 1u;
+
+ if( mTransformId != INVALID_TRANSFORM_ID )
+ {
+ mTransformManager->SetParent( mTransformId, parentNode.GetTransformId() );
+ }
}
void Node::RecursiveDisconnectFromSceneGraph( BufferIndex updateBufferIndex )
{
DALI_ASSERT_ALWAYS(!mIsRoot);
- DALI_ASSERT_ALWAYS(mParent != NULL);
+ DALI_ASSERT_ALWAYS(mParent != nullptr);
const NodeIter endIter = mChildren.End();
for ( NodeIter iter = mChildren.Begin(); iter != endIter; ++iter )
PropertyOwner::DisconnectFromSceneGraph( updateBufferIndex );
// Remove back-pointer to parent
- mParent = NULL;
- mDepth = 0u;
+ mParent = nullptr;
// Remove all child pointers
mChildren.Clear();
- // Inform child node attachment that the node has been removed from the stage
- if( mAttachment )
+ if( mTransformId != INVALID_TRANSFORM_ID )
{
- mAttachment->DisconnectedFromSceneGraph();
+ mTransformManager->SetParent( mTransformId, INVALID_TRANSFORM_ID );
}
-
}
} // namespace SceneGraph