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 "relayout-controller-impl.h"
22 #if defined(DEBUG_ENABLED)
24 #include <dali/internal/event/common/system-overlay-impl.h>
25 #endif // defined(DEBUG_ENABLED)
28 #include <dali/integration-api/debug.h>
29 #include <dali/integration-api/render-controller.h>
30 #include <dali/public-api/object/type-registry.h>
31 #include <dali/public-api/object/object-registry.h>
32 #include <dali/internal/event/actors/actor-impl.h>
33 #include <dali/internal/event/common/stage-impl.h>
34 #include <dali/internal/event/common/thread-local-storage.h>
44 #if defined(DEBUG_ENABLED)
46 Integration::Log::Filter* gLogFilter( Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_RELAYOUT_CONTROLLER") );
49 * Prints out all the children of the given actor when debug is enabled.
51 * @param[in] actor The actor whose children to print.
52 * @param[in] level The number of " | " to put in front of the children.
54 void PrintChildren( Dali::Actor actor, int level )
56 std::ostringstream output;
58 for ( int t = 0; t < level; ++t )
63 output << actor.GetTypeName();
65 output << ", " << actor.GetName();
67 output << " - Pos: " << actor.GetCurrentPosition() << " Size: " << actor.GetTargetSize();
69 output << ", Dirty: (" << ( GetImplementation( actor ).IsLayoutDirty( Dimension::WIDTH ) ? "TRUE" : "FALSE" ) << "," << ( GetImplementation( actor ).IsLayoutDirty( Dimension::HEIGHT ) ? "TRUE" : "FALSE" ) << ")";
70 output << ", Negotiated: (" << ( GetImplementation( actor ).IsLayoutNegotiated( Dimension::WIDTH ) ? "TRUE" : "FALSE" ) << "," << ( GetImplementation( actor ).IsLayoutNegotiated( Dimension::HEIGHT ) ? "TRUE" : "FALSE" ) << ")";
71 output << ", Enabled: " << ( GetImplementation( actor ).IsRelayoutEnabled() ? "TRUE" : "FALSE" );
73 output << ", (" << actor.GetObjectPtr() << ")" << std::endl;
75 DALI_LOG_INFO( gLogFilter, Debug::Verbose, output.str().c_str() );
78 unsigned int numChildren = actor.GetChildCount();
79 for( unsigned int i=0; i<numChildren; ++i )
81 PrintChildren( actor.GetChildAt(i), level );
87 * Prints the entire hierarchy of the scene.
91 if ( gLogFilter->IsEnabledFor( Debug::Verbose ) )
93 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "---------- ROOT LAYER ----------\n" );
94 PrintChildren( Stage::GetCurrent()->GetRootLayer(), 0 );
98 #define PRINT_HIERARCHY PrintHierarchy()
100 #else // defined(DEBUG_ENABLED)
102 #define PRINT_HIERARCHY
104 #endif // defined(DEBUG_ENABLED)
106 } // unnamed namespace
108 RelayoutController::RelayoutController( Integration::RenderController& controller )
109 : mRenderController( controller ),
110 mRelayoutInfoAllocator(),
111 mSlotDelegate( this ),
112 mRelayoutStack( new MemoryPoolRelayoutContainer( mRelayoutInfoAllocator ) ),
113 mStageSize(), // zero initialized
114 mRelayoutConnection( false ),
115 mRelayoutFlag( false ),
117 mPerformingRelayout( false ),
118 mProcessingCoreEvents( false )
120 // Make space for 32 controls to avoid having to copy construct a lot in the beginning
121 mRelayoutStack->Reserve( 32 );
124 RelayoutController::~RelayoutController()
126 delete mRelayoutStack;
129 RelayoutController* RelayoutController::Get()
131 return &ThreadLocalStorage::Get().GetRelayoutController();
134 void RelayoutController::SetStageSize( unsigned int width, unsigned int height )
136 mStageSize.width = width;
137 mStageSize.height = height;
140 void RelayoutController::QueueActor( Dali::Actor& actor, RelayoutContainer& actors, Vector2 size )
142 if( GetImplementation( actor ).RelayoutRequired() )
144 actors.Add( actor, size );
148 void RelayoutController::RequestRelayout( Dali::Actor& actor, Dimension::Type dimension )
155 std::vector< Dali::Actor > potentialRedundantSubRoots;
156 std::vector< Dali::Actor > topOfSubTreeStack;
158 topOfSubTreeStack.push_back( actor );
160 // Propagate on all dimensions
161 for( unsigned int i = 0; i < Dimension::DIMENSION_COUNT; ++i )
163 if( dimension & ( 1 << i ) )
165 // Do the propagation
166 PropagateAll( actor, static_cast< Dimension::Type >( 1 << i ), topOfSubTreeStack, potentialRedundantSubRoots );
170 while( !topOfSubTreeStack.empty() )
172 // Request this actor as head of sub-tree if it is not dependent on a parent that is dirty
173 Dali::Actor subTreeActor = topOfSubTreeStack.back();
174 topOfSubTreeStack.pop_back();
176 Dali::Actor parent = subTreeActor.GetParent();
177 if( !parent || !( GetImplementation( subTreeActor ).RelayoutDependentOnParent() && GetImplementation( parent ).RelayoutRequired() ) )
179 // Add sub tree root to relayout list
180 AddRequest( subTreeActor );
182 // Flag request for end of frame
187 potentialRedundantSubRoots.push_back( subTreeActor );
191 // Remove any redundant sub-tree heads
192 for( std::vector< Dali::Actor >::iterator it = potentialRedundantSubRoots.begin(), itEnd = potentialRedundantSubRoots.end(); it != itEnd; ++it )
194 Dali::Actor subRoot = *it;
196 RemoveRequest( subRoot );
199 if ( !mProcessingCoreEvents )
201 mRenderController.RequestProcessEventsOnIdle();
205 void RelayoutController::OnApplicationSceneCreated()
207 DALI_LOG_INFO( gLogFilter, Debug::General, "[Internal::RelayoutController::OnApplicationSceneCreated]\n" );
209 // Open relayout controller to receive relayout requests
212 // Spread the dirty flag through whole tree - don't need to explicity
213 // add request on rootLayer as it will automatically be added below.
214 Dali::Actor rootLayer = Dali::Stage::GetCurrent().GetRootLayer();
215 RequestRelayoutTree( rootLayer );
217 // Flag request for end of frame
221 void RelayoutController::RequestRelayoutTree( Dali::Actor& actor )
228 // Only set dirty flag if doing relayout and not already marked as dirty
229 Actor& actorImpl = GetImplementation( actor );
230 if( actorImpl.RelayoutPossible() )
232 // If parent is not in relayout we are at the top of a new sub-tree
233 Dali::Actor parent = actor.GetParent();
234 if( !parent || !GetImplementation( parent ).IsRelayoutEnabled() )
239 // Set dirty flag on actors that are enabled
240 actorImpl.SetLayoutDirty( true );
241 actorImpl.SetLayoutNegotiated( false ); // Reset this flag ready for next relayout
244 // Propagate down to children
245 for( unsigned int i = 0; i < actor.GetChildCount(); ++i )
247 Dali::Actor child = actor.GetChildAt( i );
249 RequestRelayoutTree( child );
253 void RelayoutController::PropagateAll( Dali::Actor& actor, Dimension::Type dimension, std::vector< Dali::Actor >& topOfSubTreeStack, std::vector< Dali::Actor >& potentialRedundantSubRoots )
255 // Only set dirty flag if doing relayout and not already marked as dirty
256 Actor& actorImpl = GetImplementation( actor );
257 if( actorImpl.RelayoutPossible( dimension ) )
259 // Set dirty and negotiated flags
260 actorImpl.SetLayoutDirty( true, dimension );
261 actorImpl.SetLayoutNegotiated( false, dimension ); // Reset this flag ready for next relayout
263 // Check for dimension dependecy: width for height/height for width etc
264 // Check each possible dimension and see if it is dependent on the input one
265 for( unsigned int i = 0; i < Dimension::DIMENSION_COUNT; ++i )
267 Dimension::Type dimensionToCheck = static_cast< Dimension::Type >( 1 << i );
269 if( actorImpl.RelayoutDependentOnDimension( dimension, dimensionToCheck ) &&
270 !actorImpl.IsLayoutDirty( dimensionToCheck ) )
272 PropagateAll( actor, dimensionToCheck, topOfSubTreeStack, potentialRedundantSubRoots );
276 // Propagate up to parent
277 Dali::Actor parent = actor.GetParent();
280 Actor& parentImpl = GetImplementation( parent );
281 if( parentImpl.RelayoutDependentOnChildren( dimension ) && !parentImpl.IsLayoutDirty( dimension ) )
283 // Store the highest parent reached
285 for( unsigned int i = 0, count = topOfSubTreeStack.size(); i < count; ++i )
287 if( topOfSubTreeStack[ i ] == parent )
296 topOfSubTreeStack.push_back( parent );
300 PropagateAll( parent, dimension, topOfSubTreeStack, potentialRedundantSubRoots );
304 // Propagate down to children
305 for( unsigned int i = 0, childCount = actor.GetChildCount(); i < childCount; ++i )
307 Dali::Actor child = actor.GetChildAt( i );
308 Actor& childImpl = GetImplementation( child );
310 if( childImpl.IsRelayoutEnabled() && childImpl.RelayoutDependentOnParent( dimension ) )
312 if( childImpl.IsLayoutDirty( dimension ) )
314 // We have found a child that could potentially have already been collected for relayout
315 potentialRedundantSubRoots.push_back( child );
319 PropagateAll( child, dimension, topOfSubTreeStack, potentialRedundantSubRoots );
327 void RelayoutController::PropagateFlags( Dali::Actor& actor, Dimension::Type dimension )
329 // Only set dirty flag if doing relayout and not already marked as dirty
330 Actor& actorImpl = GetImplementation( actor );
331 if( actorImpl.IsRelayoutEnabled() )
333 // Set dirty and negotiated flags
334 actorImpl.SetLayoutDirty( true, dimension );
335 actorImpl.SetLayoutNegotiated( false, dimension ); // Reset this flag ready for next relayout
337 // Check for dimension dependecy: width for height/height for width etc
338 // Check each possible dimension and see if it is dependent on the input one
339 for( unsigned int i = 0; i < Dimension::DIMENSION_COUNT; ++i )
341 Dimension::Type dimensionToCheck = static_cast< Dimension::Type >( 1 << i );
343 if( actorImpl.RelayoutDependentOnDimension( dimension, dimensionToCheck ) )
345 PropagateFlags( actor, dimensionToCheck );
349 // Propagate up to parent
350 Dali::Actor parent = actor.GetParent();
353 Actor& parentImpl = GetImplementation( parent );
354 if( parentImpl.RelayoutDependentOnChildren( dimension ) )
357 PropagateFlags( parent, dimension );
361 // Propagate down to children
362 for( unsigned int i = 0, childCount = actor.GetChildCount(); i < childCount; ++i )
364 Dali::Actor child = actor.GetChildAt( i );
365 Actor& childImpl = GetImplementation( child );
367 if( childImpl.RelayoutDependentOnParent( dimension ) )
369 PropagateFlags( child, dimension );
375 void RelayoutController::AddRequest( Dali::Actor& actor )
377 BaseObject* actorPtr = &GetImplementation( actor );
379 // Only add the rootActor if it is not already recorded
381 for( unsigned int i = 0, count = mDirtyLayoutSubTrees.Size(); i < count; ++i )
383 if( mDirtyLayoutSubTrees[ i ] == actorPtr )
392 mDirtyLayoutSubTrees.PushBack( actorPtr );
396 void RelayoutController::RemoveRequest( Dali::Actor& actor )
398 BaseObject* actorPtr = &GetImplementation( actor );
400 // Remove actor from dirty sub trees
401 for( RawActorList::Iterator it = mDirtyLayoutSubTrees.Begin(), itEnd = mDirtyLayoutSubTrees.End(); it != itEnd; ++it )
403 if( *it == actorPtr )
405 mDirtyLayoutSubTrees.Erase( it );
411 void RelayoutController::Request()
413 mRelayoutFlag = true;
415 if( !mRelayoutConnection )
417 Dali::Stage stage = Dali::Stage::GetCurrent();
418 stage.GetObjectRegistry().ObjectDestroyedSignal().Connect( mSlotDelegate, &RelayoutController::OnObjectDestroyed );
420 mRelayoutConnection = true;
424 void RelayoutController::OnObjectDestroyed( const Dali::RefObject* object )
426 // Search for and null the object if found in the following lists
427 FindAndZero( mDirtyLayoutSubTrees, object );
430 void RelayoutController::Relayout()
432 // Only do something when requested
435 mPerformingRelayout = true;
437 // Clear the flag as we're now doing the relayout
438 mRelayoutFlag = false;
440 // 1. Finds all top-level controls from the dirty list and allocate them the size of the stage
441 // These controls are paired with the parent/stage size and added to the stack.
442 for( RawActorList::Iterator it = mDirtyLayoutSubTrees.Begin(), itEnd = mDirtyLayoutSubTrees.End(); it != itEnd; ++it )
444 BaseObject* dirtyActor = *it;
446 // Need to test if actor is valid (could have been deleted and had the pointer cleared)
449 // We know that BaseObject is a base class of Internal::Actor but need to instruct the compiler to do the cast
450 Dali::Actor actor = Dali::Actor( reinterpret_cast<Dali::Internal::Actor*>( dirtyActor ) );
452 // Only negotiate actors that are on stage
453 if( actor.OnStage() )
455 Dali::Actor parent = actor.GetParent();
456 QueueActor( actor, *mRelayoutStack, ( parent ) ? Vector2( parent.GetTargetSize() ) : mStageSize );
461 mDirtyLayoutSubTrees.Clear();
463 // 2. Iterate through the stack until it's empty.
464 if( mRelayoutStack->Size() > 0 )
468 while( mRelayoutStack->Size() > 0 )
472 mRelayoutStack->Get( mRelayoutStack->Size() - 1, actor, size );
473 Actor& actorImpl = GetImplementation( actor );
474 mRelayoutStack->PopBack();
476 if( actorImpl.RelayoutRequired() )
478 DALI_LOG_INFO( gLogFilter, Debug::General, "[Internal::RelayoutController::Relayout] Negotiating %p %s %s (%.2f, %.2f)\n", &actorImpl, actor.GetTypeName().c_str(), actor.GetName().c_str(), size.width, size.height );
480 // 3. Negotiate the size with the current actor. Pass it an empty container which the actor
481 // has to fill with all the actors it has not done any size negotiation for.
482 actorImpl.NegotiateSize( size, *mRelayoutStack );
486 // We are done with the RelayoutInfos now so delete the pool
487 mRelayoutInfoAllocator.ResetMemoryPool();
492 mPerformingRelayout = false;
494 // should not disconnect the signal as that causes some control size negotiations to not work correctly
495 // this algorithm needs more optimization as well
498 void RelayoutController::SetEnabled( bool enabled )
503 bool RelayoutController::IsPerformingRelayout() const
505 return mPerformingRelayout;
508 void RelayoutController::SetProcessingCoreEvents( bool processingEvents )
510 mProcessingCoreEvents = processingEvents;
513 void RelayoutController::FindAndZero( const RawActorList& list, const Dali::RefObject* object )
515 // Object has been destroyed so clear it from this list
516 for( RawActorList::Iterator it = list.Begin(), itEnd = list.End(); it != itEnd; ++it )
518 BaseObject* actor = *it;
520 if( actor && ( actor == object ) )
522 *it = NULL; // Reset the pointer in the list. We don't want to remove it in case something is iterating over the list.
527 } // namespace Internal