2 * Copyright (c) 2014 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.
20 #include "relayout-controller-impl.h"
23 #if defined(DEBUG_ENABLED)
25 #include <dali/internal/event/common/system-overlay-impl.h>
26 #endif // defined(DEBUG_ENABLED)
29 #include <dali/public-api/actors/layer.h>
30 #include <dali/public-api/common/stage.h>
31 #include <dali/integration-api/debug.h>
32 #include <dali/integration-api/render-controller.h>
33 #include <dali/public-api/object/type-registry.h>
34 #include <dali/public-api/object/object-registry.h>
35 #include <dali/internal/event/actors/actor-impl.h>
36 #include <dali/internal/event/common/thread-local-storage.h>
46 #if defined(DEBUG_ENABLED)
48 Integration::Log::Filter* gLogFilter( Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_RELAYOUT_CONTROLLER") );
51 * Prints out all the children of the given actor when debug is enabled.
53 * @param[in] actor The actor whose children to print.
54 * @param[in] level The number of " | " to put in front of the children.
56 void PrintChildren( Dali::Actor actor, int level )
58 std::ostringstream output;
60 for ( int t = 0; t < level; ++t )
65 output << actor.GetTypeName();
67 output << ", " << actor.GetName();
69 output << " - Pos: " << actor.GetCurrentPosition() << " Size: " << actor.GetTargetSize();
71 output << ", Dirty: (" << ( GetImplementation( actor ).IsLayoutDirty( Dimension::WIDTH ) ? "TRUE" : "FALSE" ) << "," << ( GetImplementation( actor ).IsLayoutDirty( Dimension::HEIGHT ) ? "TRUE" : "FALSE" ) << ")";
72 output << ", Negotiated: (" << ( GetImplementation( actor ).IsLayoutNegotiated( Dimension::WIDTH ) ? "TRUE" : "FALSE" ) << "," << ( GetImplementation( actor ).IsLayoutNegotiated( Dimension::HEIGHT ) ? "TRUE" : "FALSE" ) << ")";
73 output << ", Enabled: " << ( GetImplementation( actor ).IsRelayoutEnabled() ? "TRUE" : "FALSE" );
75 output << ", (" << actor.GetObjectPtr() << ")" << std::endl;
77 DALI_LOG_INFO( gLogFilter, Debug::Verbose, output.str().c_str() );
80 unsigned int numChildren = actor.GetChildCount();
81 for( unsigned int i=0; i<numChildren; ++i )
83 PrintChildren( actor.GetChildAt(i), level );
89 * Prints the entire hierarchy of the scene.
93 if ( gLogFilter->IsEnabledFor( Debug::Verbose ) )
95 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "---------- ROOT LAYER ----------\n" );
96 PrintChildren( Dali::Stage().GetCurrent().GetRootLayer(), 0 );
100 #define PRINT_HIERARCHY PrintHierarchy()
102 #else // defined(DEBUG_ENABLED)
104 #define PRINT_HIERARCHY
106 #endif // defined(DEBUG_ENABLED)
108 } // unnamed namespace
110 RelayoutController* RelayoutController::Get()
112 return &ThreadLocalStorage::Get().GetRelayoutController();
115 RelayoutController::RelayoutController( Integration::RenderController& controller )
116 : mRenderController( controller ),
117 mRelayoutInfoAllocator(),
118 mSlotDelegate( this ),
119 mRelayoutStack( new MemoryPoolRelayoutContainer( mRelayoutInfoAllocator ) ),
120 mRelayoutConnection( false ),
121 mRelayoutFlag( false ),
123 mPerformingRelayout( false ),
124 mProcessingCoreEvents( false )
126 // Make space for 32 controls to avoid having to copy construct a lot in the beginning
127 mRelayoutStack->Reserve( 32 );
130 RelayoutController::~RelayoutController()
132 delete mRelayoutStack;
135 void RelayoutController::QueueActor( Dali::Actor& actor, RelayoutContainer& actors, Vector2 size )
137 if( GetImplementation( actor ).RelayoutRequired() )
139 actors.Add( actor, size );
143 void RelayoutController::RequestRelayout( Dali::Actor& actor, Dimension::Type dimension )
150 std::vector< Dali::Actor > potentialRedundantSubRoots;
151 std::vector< Dali::Actor > topOfSubTreeStack;
153 topOfSubTreeStack.push_back( actor );
155 // Propagate on all dimensions
156 for( unsigned int i = 0; i < Dimension::DIMENSION_COUNT; ++i )
158 if( dimension & ( 1 << i ) )
160 // Do the propagation
161 PropagateAll( actor, static_cast< Dimension::Type >( 1 << i ), topOfSubTreeStack, potentialRedundantSubRoots );
165 // Request this actor as head of sub-tree if it is not dependent on a parent that is dirty
166 Dali::Actor subTreeActor = topOfSubTreeStack.back();
167 Dali::Actor parent = subTreeActor.GetParent();
168 if( !parent || !( GetImplementation( subTreeActor ).RelayoutDependentOnParent() && GetImplementation( parent ).RelayoutRequired() ) )
170 // Add sub tree root to relayout list
171 AddRequest( subTreeActor );
173 // Flag request for end of frame
178 potentialRedundantSubRoots.push_back( subTreeActor );
181 // Remove any redundant sub-tree heads
182 for( std::vector< Dali::Actor >::iterator it = potentialRedundantSubRoots.begin(), itEnd = potentialRedundantSubRoots.end(); it != itEnd; ++it )
184 Dali::Actor subRoot = *it;
186 RemoveRequest( subRoot );
189 if ( !mProcessingCoreEvents )
191 mRenderController.RequestProcessEventsOnIdle();
195 void RelayoutController::OnApplicationSceneCreated()
197 DALI_LOG_INFO( gLogFilter, Debug::General, "[Internal::RelayoutController::OnApplicationSceneCreated]\n" );
199 // Open relayout controller to receive relayout requests
202 // Spread the dirty flag through whole tree - don't need to explicity
203 // add request on rootLayer as it will automatically be added below.
204 Dali::Actor rootLayer = Dali::Stage::GetCurrent().GetRootLayer();
205 RequestRelayoutTree( rootLayer );
207 // Flag request for end of frame
211 void RelayoutController::RequestRelayoutTree( Dali::Actor& actor )
218 // Only set dirty flag if doing relayout and not already marked as dirty
219 Actor& actorImpl = GetImplementation( actor );
220 if( actorImpl.RelayoutPossible() )
222 // If parent is not in relayout we are at the top of a new sub-tree
223 Dali::Actor parent = actor.GetParent();
224 if( !parent || !GetImplementation( parent ).IsRelayoutEnabled() )
229 // Set dirty flag on actors that are enabled
230 actorImpl.SetLayoutDirty( true );
231 actorImpl.SetLayoutNegotiated( false ); // Reset this flag ready for next relayout
234 // Propagate down to children
235 for( unsigned int i = 0; i < actor.GetChildCount(); ++i )
237 Dali::Actor child = actor.GetChildAt( i );
239 RequestRelayoutTree( child );
243 void RelayoutController::PropagateAll( Dali::Actor& actor, Dimension::Type dimension, std::vector< Dali::Actor >& topOfSubTreeStack, std::vector< Dali::Actor >& potentialRedundantSubRoots )
245 // Only set dirty flag if doing relayout and not already marked as dirty
246 Actor& actorImpl = GetImplementation( actor );
247 if( actorImpl.RelayoutPossible( dimension ) )
249 // Set dirty and negotiated flags
250 actorImpl.SetLayoutDirty( true, dimension );
251 actorImpl.SetLayoutNegotiated( false, dimension ); // Reset this flag ready for next relayout
253 // Check for dimension dependecy: width for height/height for width etc
254 // Check each possible dimension and see if it is dependent on the input one
255 for( unsigned int i = 0; i < Dimension::DIMENSION_COUNT; ++i )
257 Dimension::Type dimensionToCheck = static_cast< Dimension::Type >( 1 << i );
259 if( actorImpl.RelayoutDependentOnDimension( dimension, dimensionToCheck ) &&
260 !actorImpl.IsLayoutDirty( dimensionToCheck ) )
262 PropagateAll( actor, dimensionToCheck, topOfSubTreeStack, potentialRedundantSubRoots );
266 // Propagate up to parent
267 Dali::Actor parent = actor.GetParent();
270 Actor& parentImpl = GetImplementation( parent );
271 if( parentImpl.RelayoutDependentOnChildren( dimension ) && !parentImpl.IsLayoutDirty( dimension ) )
273 // Store the highest parent reached
275 for( unsigned int i = 0, count = topOfSubTreeStack.size(); i < count; ++i )
277 if( topOfSubTreeStack[ i ] == parent )
286 topOfSubTreeStack.push_back( parent );
290 PropagateAll( parent, dimension, topOfSubTreeStack, potentialRedundantSubRoots );
294 // Propagate down to children
295 for( unsigned int i = 0, childCount = actor.GetChildCount(); i < childCount; ++i )
297 Dali::Actor child = actor.GetChildAt( i );
298 Actor& childImpl = GetImplementation( child );
300 if( childImpl.IsRelayoutEnabled() && childImpl.RelayoutDependentOnParent( dimension ) )
302 if( childImpl.IsLayoutDirty( dimension ) )
304 // We have found a child that could potentially have already been collected for relayout
305 potentialRedundantSubRoots.push_back( child );
309 PropagateAll( child, dimension, topOfSubTreeStack, potentialRedundantSubRoots );
317 void RelayoutController::PropagateFlags( Dali::Actor& actor, Dimension::Type dimension )
319 // Only set dirty flag if doing relayout and not already marked as dirty
320 Actor& actorImpl = GetImplementation( actor );
321 if( actorImpl.IsRelayoutEnabled() )
323 // Set dirty and negotiated flags
324 actorImpl.SetLayoutDirty( true, dimension );
325 actorImpl.SetLayoutNegotiated( false, dimension ); // Reset this flag ready for next relayout
327 // Check for dimension dependecy: width for height/height for width etc
328 // Check each possible dimension and see if it is dependent on the input one
329 for( unsigned int i = 0; i < Dimension::DIMENSION_COUNT; ++i )
331 Dimension::Type dimensionToCheck = static_cast< Dimension::Type >( 1 << i );
333 if( actorImpl.RelayoutDependentOnDimension( dimension, dimensionToCheck ) )
335 PropagateFlags( actor, dimensionToCheck );
339 // Propagate up to parent
340 Dali::Actor parent = actor.GetParent();
343 Actor& parentImpl = GetImplementation( parent );
344 if( parentImpl.RelayoutDependentOnChildren( dimension ) )
347 PropagateFlags( parent, dimension );
351 // Propagate down to children
352 for( unsigned int i = 0, childCount = actor.GetChildCount(); i < childCount; ++i )
354 Dali::Actor child = actor.GetChildAt( i );
355 Actor& childImpl = GetImplementation( child );
357 if( childImpl.RelayoutDependentOnParent( dimension ) )
359 PropagateFlags( child, dimension );
365 void RelayoutController::AddRequest( Dali::Actor& actor )
367 BaseObject* actorPtr = &GetImplementation( actor );
369 // Only add the rootActor if it is not already recorded
371 for( unsigned int i = 0, count = mDirtyLayoutSubTrees.Size(); i < count; ++i )
373 if( mDirtyLayoutSubTrees[ i ] == actorPtr )
382 mDirtyLayoutSubTrees.PushBack( actorPtr );
386 void RelayoutController::RemoveRequest( Dali::Actor& actor )
388 BaseObject* actorPtr = &GetImplementation( actor );
390 // Remove actor from dirty sub trees
391 for( RawActorList::Iterator it = mDirtyLayoutSubTrees.Begin(), itEnd = mDirtyLayoutSubTrees.End(); it != itEnd; ++it )
393 if( *it == actorPtr )
395 mDirtyLayoutSubTrees.Erase( it );
401 void RelayoutController::Request()
403 mRelayoutFlag = true;
405 if( !mRelayoutConnection )
407 Dali::Stage stage = Dali::Stage::GetCurrent();
408 stage.GetObjectRegistry().ObjectDestroyedSignal().Connect( mSlotDelegate, &RelayoutController::OnObjectDestroyed );
410 mRelayoutConnection = true;
414 void RelayoutController::OnObjectDestroyed( const Dali::RefObject* object )
416 // Search for and null the object if found in the following lists
417 FindAndZero( mDirtyLayoutSubTrees, object );
420 void RelayoutController::Relayout()
422 // Only do something when requested
425 mPerformingRelayout = true;
427 // Clear the flag as we're now doing the relayout
428 mRelayoutFlag = false;
430 // 1. Finds all top-level controls from the dirty list and allocate them the size of the stage
431 // These controls are paired with the parent/stage size and added to the stack.
432 const Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
434 for( RawActorList::Iterator it = mDirtyLayoutSubTrees.Begin(), itEnd = mDirtyLayoutSubTrees.End(); it != itEnd; ++it )
436 BaseObject* dirtyActor = *it;
438 // Need to test if actor is valid (could have been deleted and had the pointer cleared)
441 // We know that BaseObject is a base class of Internal::Actor but need to instruct the compiler to do the cast
442 Dali::Actor actor = Dali::Actor( reinterpret_cast<Dali::Internal::Actor*>( dirtyActor ) );
444 // Only negotiate actors that are on stage
445 if( actor.OnStage() )
447 Dali::Actor parent = actor.GetParent();
448 QueueActor( actor, *mRelayoutStack, ( parent ) ? Vector2( parent.GetTargetSize() ) : stageSize );
453 mDirtyLayoutSubTrees.Clear();
455 // 2. Iterate through the stack until it's empty.
456 if( mRelayoutStack->Size() > 0 )
460 while( mRelayoutStack->Size() > 0 )
464 mRelayoutStack->Get( mRelayoutStack->Size() - 1, actor, size );
465 Actor& actorImpl = GetImplementation( actor );
466 mRelayoutStack->PopBack();
468 if( actorImpl.RelayoutRequired() )
470 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 );
472 // 3. Negotiate the size with the current actor. Pass it an empty container which the actor
473 // has to fill with all the actors it has not done any size negotiation for.
474 actorImpl.NegotiateSize( size, *mRelayoutStack );
478 // We are done with the RelayoutInfos now so delete the pool
479 mRelayoutInfoAllocator.ResetMemoryPool();
484 mPerformingRelayout = false;
486 // should not disconnect the signal as that causes some control size negotiations to not work correctly
487 // this algorithm needs more optimization as well
490 void RelayoutController::SetEnabled( bool enabled )
495 bool RelayoutController::IsPerformingRelayout() const
497 return mPerformingRelayout;
500 void RelayoutController::SetProcessingCoreEvents( bool processingEvents )
502 mProcessingCoreEvents = processingEvents;
505 void RelayoutController::FindAndZero( const RawActorList& list, const Dali::RefObject* object )
507 // Object has been destroyed so clear it from this list
508 for( RawActorList::Iterator it = list.Begin(), itEnd = list.End(); it != itEnd; ++it )
510 BaseObject* actor = *it;
512 if( actor && ( actor == object ) )
514 *it = NULL; // Reset the pointer in the list. We don't want to remove it in case something is iterating over the list.
519 } // namespace Internal