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/public-api/object/type-registry.h>
33 #include <dali/public-api/object/object-registry.h>
34 #include <dali/internal/event/actors/actor-impl.h>
35 #include <dali/internal/event/common/thread-local-storage.h>
45 #if defined(DEBUG_ENABLED)
47 Integration::Log::Filter* gLogFilter( Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_RELAYOUT_CONTROLLER") );
50 * Prints out all the children of the given actor when debug is enabled.
52 * @param[in] actor The actor whose children to print.
53 * @param[in] level The number of " | " to put in front of the children.
55 void PrintChildren( Dali::Actor actor, int level )
57 std::ostringstream output;
59 for ( int t = 0; t < level; ++t )
64 output << actor.GetTypeName();
66 output << ", " << actor.GetName();
68 output << " - Pos: " << actor.GetCurrentPosition() << " Size: " << actor.GetTargetSize();
70 output << ", Dirty: (" << ( GetImplementation( actor ).IsLayoutDirty( Dimension::WIDTH ) ? "TRUE" : "FALSE" ) << "," << ( GetImplementation( actor ).IsLayoutDirty( Dimension::HEIGHT ) ? "TRUE" : "FALSE" ) << ")";
71 output << ", Negotiated: (" << ( GetImplementation( actor ).IsLayoutNegotiated( Dimension::WIDTH ) ? "TRUE" : "FALSE" ) << "," << ( GetImplementation( actor ).IsLayoutNegotiated( Dimension::HEIGHT ) ? "TRUE" : "FALSE" ) << ")";
72 output << ", Enabled: " << ( GetImplementation( actor ).IsRelayoutEnabled() ? "TRUE" : "FALSE" );
74 output << ", (" << actor.GetObjectPtr() << ")" << std::endl;
76 DALI_LOG_INFO( gLogFilter, Debug::Verbose, output.str().c_str() );
79 unsigned int numChildren = actor.GetChildCount();
80 for( unsigned int i=0; i<numChildren; ++i )
82 PrintChildren( actor.GetChildAt(i), level );
88 * Prints the entire hierarchy of the scene.
92 if ( gLogFilter->IsEnabledFor( Debug::Verbose ) )
94 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "---------- ROOT LAYER ----------\n" );
95 PrintChildren( Dali::Stage().GetCurrent().GetRootLayer(), 0 );
99 #define PRINT_HIERARCHY PrintHierarchy()
101 #else // defined(DEBUG_ENABLED)
103 #define PRINT_HIERARCHY
105 #endif // defined(DEBUG_ENABLED)
107 } // unnamed namespace
109 RelayoutController* RelayoutController::Get()
111 return &ThreadLocalStorage::Get().GetRelayoutController();
114 RelayoutController::RelayoutController()
115 : mRelayoutInfoAllocator(),
116 mSlotDelegate( this ),
117 mRelayoutStack( new MemoryPoolRelayoutContainer( mRelayoutInfoAllocator ) ),
118 mRelayoutConnection( false ),
119 mRelayoutFlag( false ),
121 mPerformingRelayout( false )
123 // Make space for 32 controls to avoid having to copy construct a lot in the beginning
124 mRelayoutStack->Reserve( 32 );
127 RelayoutController::~RelayoutController()
129 delete mRelayoutStack;
132 void RelayoutController::QueueActor( Dali::Actor& actor, RelayoutContainer& actors, Vector2 size )
134 if( GetImplementation( actor ).RelayoutRequired() )
136 actors.Add( actor, size );
140 void RelayoutController::RequestRelayout( Dali::Actor& actor, Dimension::Type dimension )
147 Dali::ActorContainer potentialRedundantSubRoots;
148 Dali::ActorContainer topOfSubTreeStack;
150 topOfSubTreeStack.push_back( actor );
152 // Propagate on all dimensions
153 for( unsigned int i = 0; i < Dimension::DIMENSION_COUNT; ++i )
155 if( dimension & ( 1 << i ) )
157 // Do the propagation
158 PropagateAll( actor, static_cast< Dimension::Type >( 1 << i ), topOfSubTreeStack, potentialRedundantSubRoots );
162 // Request this actor as head of sub-tree if it is not dependent on a parent that is dirty
163 Dali::Actor subTreeActor = topOfSubTreeStack.back();
164 Dali::Actor parent = subTreeActor.GetParent();
165 if( !parent || !( GetImplementation( subTreeActor ).RelayoutDependentOnParent() && GetImplementation( parent ).RelayoutRequired() ) )
167 // Add sub tree root to relayout list
168 AddRequest( subTreeActor );
170 // Flag request for end of frame
175 potentialRedundantSubRoots.push_back( subTreeActor );
178 // Remove any redundant sub-tree heads
179 for( ActorContainer::iterator it = potentialRedundantSubRoots.begin(), itEnd = potentialRedundantSubRoots.end(); it != itEnd; ++it )
181 Dali::Actor subRoot = *it;
183 RemoveRequest( subRoot );
187 void RelayoutController::OnApplicationSceneCreated()
189 DALI_LOG_INFO( gLogFilter, Debug::General, "[Internal::RelayoutController::OnApplicationSceneCreated]\n" );
191 // Open relayout controller to receive relayout requests
194 // Spread the dirty flag through whole tree - don't need to explicity
195 // add request on rootLayer as it will automatically be added below.
196 Dali::Actor rootLayer = Dali::Stage::GetCurrent().GetRootLayer();
197 RequestRelayoutTree( rootLayer );
199 // Flag request for end of frame
203 void RelayoutController::RequestRelayoutTree( Dali::Actor& actor )
210 // Only set dirty flag if doing relayout and not already marked as dirty
211 Actor& actorImpl = GetImplementation( actor );
212 if( actorImpl.RelayoutPossible() )
214 // If parent is not in relayout we are at the top of a new sub-tree
215 Dali::Actor parent = actor.GetParent();
216 if( !parent || !GetImplementation( parent ).IsRelayoutEnabled() )
221 // Set dirty flag on actors that are enabled
222 actorImpl.SetLayoutDirty( true );
223 actorImpl.SetLayoutNegotiated( false ); // Reset this flag ready for next relayout
226 // Propagate down to children
227 for( unsigned int i = 0; i < actor.GetChildCount(); ++i )
229 Dali::Actor child = actor.GetChildAt( i );
231 RequestRelayoutTree( child );
235 void RelayoutController::PropagateAll( Dali::Actor& actor, Dimension::Type dimension, Dali::ActorContainer& topOfSubTreeStack, Dali::ActorContainer& potentialRedundantSubRoots )
237 // Only set dirty flag if doing relayout and not already marked as dirty
238 Actor& actorImpl = GetImplementation( actor );
239 if( actorImpl.RelayoutPossible( dimension ) )
241 // Set dirty and negotiated flags
242 actorImpl.SetLayoutDirty( true, dimension );
243 actorImpl.SetLayoutNegotiated( false, dimension ); // Reset this flag ready for next relayout
245 // Check for dimension dependecy: width for height/height for width etc
246 // Check each possible dimension and see if it is dependent on the input one
247 for( unsigned int i = 0; i < Dimension::DIMENSION_COUNT; ++i )
249 Dimension::Type dimensionToCheck = static_cast< Dimension::Type >( 1 << i );
251 if( actorImpl.RelayoutDependentOnDimension( dimension, dimensionToCheck ) &&
252 !actorImpl.IsLayoutDirty( dimensionToCheck ) )
254 PropagateAll( actor, dimensionToCheck, topOfSubTreeStack, potentialRedundantSubRoots );
258 // Propagate up to parent
259 Dali::Actor parent = actor.GetParent();
262 Actor& parentImpl = GetImplementation( parent );
263 if( parentImpl.RelayoutDependentOnChildren( dimension ) && !parentImpl.IsLayoutDirty( dimension ) )
265 // Store the highest parent reached
267 for( unsigned int i = 0, count = topOfSubTreeStack.size(); i < count; ++i )
269 if( topOfSubTreeStack[ i ] == parent )
278 topOfSubTreeStack.push_back( parent );
282 PropagateAll( parent, dimension, topOfSubTreeStack, potentialRedundantSubRoots );
286 // Propagate down to children
287 for( unsigned int i = 0, childCount = actor.GetChildCount(); i < childCount; ++i )
289 Dali::Actor child = actor.GetChildAt( i );
290 Actor& childImpl = GetImplementation( child );
292 if( childImpl.IsRelayoutEnabled() && childImpl.RelayoutDependentOnParent( dimension ) )
294 if( childImpl.IsLayoutDirty( dimension ) )
296 // We have found a child that could potentially have already been collected for relayout
297 potentialRedundantSubRoots.push_back( child );
301 PropagateAll( child, dimension, topOfSubTreeStack, potentialRedundantSubRoots );
309 void RelayoutController::PropagateFlags( Dali::Actor& actor, Dimension::Type dimension )
311 // Only set dirty flag if doing relayout and not already marked as dirty
312 Actor& actorImpl = GetImplementation( actor );
313 if( actorImpl.IsRelayoutEnabled() )
315 // Set dirty and negotiated flags
316 actorImpl.SetLayoutDirty( true, dimension );
317 actorImpl.SetLayoutNegotiated( false, dimension ); // Reset this flag ready for next relayout
319 // Check for dimension dependecy: width for height/height for width etc
320 // Check each possible dimension and see if it is dependent on the input one
321 for( unsigned int i = 0; i < Dimension::DIMENSION_COUNT; ++i )
323 Dimension::Type dimensionToCheck = static_cast< Dimension::Type >( 1 << i );
325 if( actorImpl.RelayoutDependentOnDimension( dimension, dimensionToCheck ) )
327 PropagateFlags( actor, dimensionToCheck );
331 // Propagate up to parent
332 Dali::Actor parent = actor.GetParent();
335 Actor& parentImpl = GetImplementation( parent );
336 if( parentImpl.RelayoutDependentOnChildren( dimension ) )
339 PropagateFlags( parent, dimension );
343 // Propagate down to children
344 for( unsigned int i = 0, childCount = actor.GetChildCount(); i < childCount; ++i )
346 Dali::Actor child = actor.GetChildAt( i );
347 Actor& childImpl = GetImplementation( child );
349 if( childImpl.RelayoutDependentOnParent( dimension ) )
351 PropagateFlags( child, dimension );
357 void RelayoutController::AddRequest( Dali::Actor& actor )
359 BaseObject* actorPtr = &GetImplementation( actor );
361 // Only add the rootActor if it is not already recorded
363 for( unsigned int i = 0, count = mDirtyLayoutSubTrees.Size(); i < count; ++i )
365 if( mDirtyLayoutSubTrees[ i ] == actorPtr )
374 mDirtyLayoutSubTrees.PushBack( actorPtr );
378 void RelayoutController::RemoveRequest( Dali::Actor& actor )
380 BaseObject* actorPtr = &GetImplementation( actor );
382 // Remove actor from dirty sub trees
383 for( RawActorList::Iterator it = mDirtyLayoutSubTrees.Begin(), itEnd = mDirtyLayoutSubTrees.End(); it != itEnd; ++it )
385 if( *it == actorPtr )
387 mDirtyLayoutSubTrees.Erase( it );
393 void RelayoutController::Request()
395 mRelayoutFlag = true;
397 if( !mRelayoutConnection )
399 Dali::Stage stage = Dali::Stage::GetCurrent();
400 stage.GetObjectRegistry().ObjectDestroyedSignal().Connect( mSlotDelegate, &RelayoutController::OnObjectDestroyed );
402 mRelayoutConnection = true;
406 void RelayoutController::OnObjectDestroyed( const Dali::RefObject* object )
408 // Search for and null the object if found in the following lists
409 FindAndZero( mDirtyLayoutSubTrees, object );
412 void RelayoutController::Relayout()
414 // Only do something when requested
417 mPerformingRelayout = true;
419 // Clear the flag as we're now doing the relayout
420 mRelayoutFlag = false;
422 // 1. Finds all top-level controls from the dirty list and allocate them the size of the stage
423 // These controls are paired with the parent/stage size and added to the stack.
424 const Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
426 for( RawActorList::Iterator it = mDirtyLayoutSubTrees.Begin(), itEnd = mDirtyLayoutSubTrees.End(); it != itEnd; ++it )
428 BaseObject* dirtyActor = *it;
430 // Need to test if actor is valid (could have been deleted and had the pointer cleared)
433 // We know that BaseObject is a base class of Internal::Actor but need to instruct the compiler to do the cast
434 Dali::Actor actor = Dali::Actor( reinterpret_cast<Dali::Internal::Actor*>( dirtyActor ) );
436 // Only negotiate actors that are on stage
437 if( actor.OnStage() )
439 Dali::Actor parent = actor.GetParent();
440 QueueActor( actor, *mRelayoutStack, ( parent ) ? Vector2( parent.GetTargetSize() ) : stageSize );
445 mDirtyLayoutSubTrees.Clear();
447 // 2. Iterate through the stack until it's empty.
448 if( mRelayoutStack->Size() > 0 )
452 while( mRelayoutStack->Size() > 0 )
456 mRelayoutStack->Get( mRelayoutStack->Size() - 1, actor, size );
457 Actor& actorImpl = GetImplementation( actor );
458 mRelayoutStack->PopBack();
460 if( actorImpl.RelayoutRequired() )
462 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 );
464 // 3. Negotiate the size with the current actor. Pass it an empty container which the actor
465 // has to fill with all the actors it has not done any size negotiation for.
466 actorImpl.NegotiateSize( size, *mRelayoutStack );
470 // We are done with the RelayoutInfos now so delete the pool
471 mRelayoutInfoAllocator.ResetMemoryPool();
476 mPerformingRelayout = false;
478 // should not disconnect the signal as that causes some control size negotiations to not work correctly
479 // this algorithm needs more optimization as well
482 void RelayoutController::SetEnabled( bool enabled )
487 bool RelayoutController::IsPerformingRelayout() const
489 return mPerformingRelayout;
492 void RelayoutController::FindAndZero( const RawActorList& list, const Dali::RefObject* object )
494 // Object has been destroyed so clear it from this list
495 for( RawActorList::Iterator it = list.Begin(), itEnd = list.End(); it != itEnd; ++it )
497 BaseObject* actor = *it;
499 if( actor && ( actor == object ) )
501 *it = NULL; // Reset the pointer in the list. We don't want to remove it in case something is iterating over the list.
506 } // namespace Internal