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 while( !topOfSubTreeStack.empty() )
167 // Request this actor as head of sub-tree if it is not dependent on a parent that is dirty
168 Dali::Actor subTreeActor = topOfSubTreeStack.back();
169 topOfSubTreeStack.pop_back();
171 Dali::Actor parent = subTreeActor.GetParent();
172 if( !parent || !( GetImplementation( subTreeActor ).RelayoutDependentOnParent() && GetImplementation( parent ).RelayoutRequired() ) )
174 // Add sub tree root to relayout list
175 AddRequest( subTreeActor );
177 // Flag request for end of frame
182 potentialRedundantSubRoots.push_back( subTreeActor );
186 // Remove any redundant sub-tree heads
187 for( std::vector< Dali::Actor >::iterator it = potentialRedundantSubRoots.begin(), itEnd = potentialRedundantSubRoots.end(); it != itEnd; ++it )
189 Dali::Actor subRoot = *it;
191 RemoveRequest( subRoot );
194 if ( !mProcessingCoreEvents )
196 mRenderController.RequestProcessEventsOnIdle();
200 void RelayoutController::OnApplicationSceneCreated()
202 DALI_LOG_INFO( gLogFilter, Debug::General, "[Internal::RelayoutController::OnApplicationSceneCreated]\n" );
204 // Open relayout controller to receive relayout requests
207 // Spread the dirty flag through whole tree - don't need to explicity
208 // add request on rootLayer as it will automatically be added below.
209 Dali::Actor rootLayer = Dali::Stage::GetCurrent().GetRootLayer();
210 RequestRelayoutTree( rootLayer );
212 // Flag request for end of frame
216 void RelayoutController::RequestRelayoutTree( Dali::Actor& actor )
223 // Only set dirty flag if doing relayout and not already marked as dirty
224 Actor& actorImpl = GetImplementation( actor );
225 if( actorImpl.RelayoutPossible() )
227 // If parent is not in relayout we are at the top of a new sub-tree
228 Dali::Actor parent = actor.GetParent();
229 if( !parent || !GetImplementation( parent ).IsRelayoutEnabled() )
234 // Set dirty flag on actors that are enabled
235 actorImpl.SetLayoutDirty( true );
236 actorImpl.SetLayoutNegotiated( false ); // Reset this flag ready for next relayout
239 // Propagate down to children
240 for( unsigned int i = 0; i < actor.GetChildCount(); ++i )
242 Dali::Actor child = actor.GetChildAt( i );
244 RequestRelayoutTree( child );
248 void RelayoutController::PropagateAll( Dali::Actor& actor, Dimension::Type dimension, std::vector< Dali::Actor >& topOfSubTreeStack, std::vector< Dali::Actor >& potentialRedundantSubRoots )
250 // Only set dirty flag if doing relayout and not already marked as dirty
251 Actor& actorImpl = GetImplementation( actor );
252 if( actorImpl.RelayoutPossible( dimension ) )
254 // Set dirty and negotiated flags
255 actorImpl.SetLayoutDirty( true, dimension );
256 actorImpl.SetLayoutNegotiated( false, dimension ); // Reset this flag ready for next relayout
258 // Check for dimension dependecy: width for height/height for width etc
259 // Check each possible dimension and see if it is dependent on the input one
260 for( unsigned int i = 0; i < Dimension::DIMENSION_COUNT; ++i )
262 Dimension::Type dimensionToCheck = static_cast< Dimension::Type >( 1 << i );
264 if( actorImpl.RelayoutDependentOnDimension( dimension, dimensionToCheck ) &&
265 !actorImpl.IsLayoutDirty( dimensionToCheck ) )
267 PropagateAll( actor, dimensionToCheck, topOfSubTreeStack, potentialRedundantSubRoots );
271 // Propagate up to parent
272 Dali::Actor parent = actor.GetParent();
275 Actor& parentImpl = GetImplementation( parent );
276 if( parentImpl.RelayoutDependentOnChildren( dimension ) && !parentImpl.IsLayoutDirty( dimension ) )
278 // Store the highest parent reached
280 for( unsigned int i = 0, count = topOfSubTreeStack.size(); i < count; ++i )
282 if( topOfSubTreeStack[ i ] == parent )
291 topOfSubTreeStack.push_back( parent );
295 PropagateAll( parent, dimension, topOfSubTreeStack, potentialRedundantSubRoots );
299 // Propagate down to children
300 for( unsigned int i = 0, childCount = actor.GetChildCount(); i < childCount; ++i )
302 Dali::Actor child = actor.GetChildAt( i );
303 Actor& childImpl = GetImplementation( child );
305 if( childImpl.IsRelayoutEnabled() && childImpl.RelayoutDependentOnParent( dimension ) )
307 if( childImpl.IsLayoutDirty( dimension ) )
309 // We have found a child that could potentially have already been collected for relayout
310 potentialRedundantSubRoots.push_back( child );
314 PropagateAll( child, dimension, topOfSubTreeStack, potentialRedundantSubRoots );
322 void RelayoutController::PropagateFlags( Dali::Actor& actor, Dimension::Type dimension )
324 // Only set dirty flag if doing relayout and not already marked as dirty
325 Actor& actorImpl = GetImplementation( actor );
326 if( actorImpl.IsRelayoutEnabled() )
328 // Set dirty and negotiated flags
329 actorImpl.SetLayoutDirty( true, dimension );
330 actorImpl.SetLayoutNegotiated( false, dimension ); // Reset this flag ready for next relayout
332 // Check for dimension dependecy: width for height/height for width etc
333 // Check each possible dimension and see if it is dependent on the input one
334 for( unsigned int i = 0; i < Dimension::DIMENSION_COUNT; ++i )
336 Dimension::Type dimensionToCheck = static_cast< Dimension::Type >( 1 << i );
338 if( actorImpl.RelayoutDependentOnDimension( dimension, dimensionToCheck ) )
340 PropagateFlags( actor, dimensionToCheck );
344 // Propagate up to parent
345 Dali::Actor parent = actor.GetParent();
348 Actor& parentImpl = GetImplementation( parent );
349 if( parentImpl.RelayoutDependentOnChildren( dimension ) )
352 PropagateFlags( parent, dimension );
356 // Propagate down to children
357 for( unsigned int i = 0, childCount = actor.GetChildCount(); i < childCount; ++i )
359 Dali::Actor child = actor.GetChildAt( i );
360 Actor& childImpl = GetImplementation( child );
362 if( childImpl.RelayoutDependentOnParent( dimension ) )
364 PropagateFlags( child, dimension );
370 void RelayoutController::AddRequest( Dali::Actor& actor )
372 BaseObject* actorPtr = &GetImplementation( actor );
374 // Only add the rootActor if it is not already recorded
376 for( unsigned int i = 0, count = mDirtyLayoutSubTrees.Size(); i < count; ++i )
378 if( mDirtyLayoutSubTrees[ i ] == actorPtr )
387 mDirtyLayoutSubTrees.PushBack( actorPtr );
391 void RelayoutController::RemoveRequest( Dali::Actor& actor )
393 BaseObject* actorPtr = &GetImplementation( actor );
395 // Remove actor from dirty sub trees
396 for( RawActorList::Iterator it = mDirtyLayoutSubTrees.Begin(), itEnd = mDirtyLayoutSubTrees.End(); it != itEnd; ++it )
398 if( *it == actorPtr )
400 mDirtyLayoutSubTrees.Erase( it );
406 void RelayoutController::Request()
408 mRelayoutFlag = true;
410 if( !mRelayoutConnection )
412 Dali::Stage stage = Dali::Stage::GetCurrent();
413 stage.GetObjectRegistry().ObjectDestroyedSignal().Connect( mSlotDelegate, &RelayoutController::OnObjectDestroyed );
415 mRelayoutConnection = true;
419 void RelayoutController::OnObjectDestroyed( const Dali::RefObject* object )
421 // Search for and null the object if found in the following lists
422 FindAndZero( mDirtyLayoutSubTrees, object );
425 void RelayoutController::Relayout()
427 // Only do something when requested
430 mPerformingRelayout = true;
432 // Clear the flag as we're now doing the relayout
433 mRelayoutFlag = false;
435 // 1. Finds all top-level controls from the dirty list and allocate them the size of the stage
436 // These controls are paired with the parent/stage size and added to the stack.
437 const Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
439 for( RawActorList::Iterator it = mDirtyLayoutSubTrees.Begin(), itEnd = mDirtyLayoutSubTrees.End(); it != itEnd; ++it )
441 BaseObject* dirtyActor = *it;
443 // Need to test if actor is valid (could have been deleted and had the pointer cleared)
446 // We know that BaseObject is a base class of Internal::Actor but need to instruct the compiler to do the cast
447 Dali::Actor actor = Dali::Actor( reinterpret_cast<Dali::Internal::Actor*>( dirtyActor ) );
449 // Only negotiate actors that are on stage
450 if( actor.OnStage() )
452 Dali::Actor parent = actor.GetParent();
453 QueueActor( actor, *mRelayoutStack, ( parent ) ? Vector2( parent.GetTargetSize() ) : stageSize );
458 mDirtyLayoutSubTrees.Clear();
460 // 2. Iterate through the stack until it's empty.
461 if( mRelayoutStack->Size() > 0 )
465 while( mRelayoutStack->Size() > 0 )
469 mRelayoutStack->Get( mRelayoutStack->Size() - 1, actor, size );
470 Actor& actorImpl = GetImplementation( actor );
471 mRelayoutStack->PopBack();
473 if( actorImpl.RelayoutRequired() )
475 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 );
477 // 3. Negotiate the size with the current actor. Pass it an empty container which the actor
478 // has to fill with all the actors it has not done any size negotiation for.
479 actorImpl.NegotiateSize( size, *mRelayoutStack );
483 // We are done with the RelayoutInfos now so delete the pool
484 mRelayoutInfoAllocator.ResetMemoryPool();
489 mPerformingRelayout = false;
491 // should not disconnect the signal as that causes some control size negotiations to not work correctly
492 // this algorithm needs more optimization as well
495 void RelayoutController::SetEnabled( bool enabled )
500 bool RelayoutController::IsPerformingRelayout() const
502 return mPerformingRelayout;
505 void RelayoutController::SetProcessingCoreEvents( bool processingEvents )
507 mProcessingCoreEvents = processingEvents;
510 void RelayoutController::FindAndZero( const RawActorList& list, const Dali::RefObject* object )
512 // Object has been destroyed so clear it from this list
513 for( RawActorList::Iterator it = list.Begin(), itEnd = list.End(); it != itEnd; ++it )
515 BaseObject* actor = *it;
517 if( actor && ( actor == object ) )
519 *it = NULL; // Reset the pointer in the list. We don't want to remove it in case something is iterating over the list.
524 } // namespace Internal