2 * Copyright (c) 2019 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 #endif // defined(DEBUG_ENABLED)
27 #include <dali/integration-api/debug.h>
28 #include <dali/integration-api/render-controller.h>
29 #include <dali/public-api/object/type-registry.h>
30 #include <dali/public-api/object/object-registry.h>
31 #include <dali/internal/event/actors/actor-impl.h>
32 #include <dali/internal/event/common/stage-impl.h>
33 #include <dali/internal/event/common/thread-local-storage.h>
43 #if defined(DEBUG_ENABLED)
45 Integration::Log::Filter* gLogFilter( Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_RELAYOUT_CONTROLLER") );
48 * Prints out all the children of the given actor when debug is enabled.
50 * @param[in] actor The actor whose children to print.
51 * @param[in] level The number of " | " to put in front of the children.
53 void PrintChildren( Dali::Actor actor, int level )
55 std::ostringstream output;
57 for ( int t = 0; t < level; ++t )
62 output << actor.GetTypeName();
64 output << ", " << actor.GetName();
66 output << " - Pos: " << actor.GetCurrentPosition() << " Size: " << actor.GetTargetSize();
68 output << ", Dirty: (" << ( GetImplementation( actor ).IsLayoutDirty( Dimension::WIDTH ) ? "TRUE" : "FALSE" ) << "," << ( GetImplementation( actor ).IsLayoutDirty( Dimension::HEIGHT ) ? "TRUE" : "FALSE" ) << ")";
69 output << ", Negotiated: (" << ( GetImplementation( actor ).IsLayoutNegotiated( Dimension::WIDTH ) ? "TRUE" : "FALSE" ) << "," << ( GetImplementation( actor ).IsLayoutNegotiated( Dimension::HEIGHT ) ? "TRUE" : "FALSE" ) << ")";
70 output << ", Enabled: " << ( GetImplementation( actor ).IsRelayoutEnabled() ? "TRUE" : "FALSE" );
72 output << ", (" << actor.GetObjectPtr() << ")" << std::endl;
74 DALI_LOG_INFO( gLogFilter, Debug::Verbose, output.str().c_str() );
77 uint32_t numChildren = actor.GetChildCount();
78 for( uint32_t i=0; i<numChildren; ++i )
80 PrintChildren( actor.GetChildAt(i), level );
86 * Prints the entire hierarchy of the scene.
90 if ( gLogFilter->IsEnabledFor( Debug::Verbose ) )
92 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 mRelayoutConnection( false ),
114 mRelayoutFlag( false ),
116 mPerformingRelayout( false ),
117 mProcessingCoreEvents( false )
119 // Make space for 32 controls to avoid having to copy construct a lot in the beginning
120 mRelayoutStack->Reserve( 32 );
123 RelayoutController::~RelayoutController()
125 delete mRelayoutStack;
128 RelayoutController* RelayoutController::Get()
130 return &ThreadLocalStorage::Get().GetRelayoutController();
133 void RelayoutController::QueueActor( Internal::Actor* actor, RelayoutContainer& actors, Vector2 size )
135 if( actor && actor->RelayoutRequired() )
137 Dali::Actor actorHandle = Dali::Actor( actor );
138 actors.Add( actorHandle, size );
142 void RelayoutController::RequestRelayout( Dali::Actor& actor, Dimension::Type dimension )
149 std::vector< Dali::Actor > potentialRedundantSubRoots;
150 std::vector< Dali::Actor > topOfSubTreeStack;
152 topOfSubTreeStack.push_back( actor );
154 // Propagate on all dimensions
155 for( uint32_t i = 0; i < Dimension::DIMENSION_COUNT; ++i )
157 if( dimension & ( 1 << i ) )
159 // Do the propagation
160 PropagateAll( actor, static_cast< Dimension::Type >( 1 << i ), topOfSubTreeStack, potentialRedundantSubRoots );
164 while( !topOfSubTreeStack.empty() )
166 // Request this actor as head of sub-tree if it is not dependent on a parent that is dirty
167 Dali::Actor subTreeActor = topOfSubTreeStack.back();
168 topOfSubTreeStack.pop_back();
170 Dali::Actor parent = subTreeActor.GetParent();
171 if( !parent || !( GetImplementation( subTreeActor ).RelayoutDependentOnParent() && GetImplementation( parent ).RelayoutRequired() ) )
173 // Add sub tree root to relayout list
174 AddRequest( subTreeActor );
176 // Flag request for end of frame
181 potentialRedundantSubRoots.push_back( subTreeActor );
185 // Remove any redundant sub-tree heads
186 for( std::vector< Dali::Actor >::iterator it = potentialRedundantSubRoots.begin(), itEnd = potentialRedundantSubRoots.end(); it != itEnd; ++it )
188 Dali::Actor subRoot = *it;
190 RemoveRequest( subRoot );
193 if ( !mProcessingCoreEvents )
195 mRenderController.RequestProcessEventsOnIdle( false );
199 void RelayoutController::OnApplicationSceneCreated()
201 DALI_LOG_INFO( gLogFilter, Debug::General, "[Internal::RelayoutController::OnApplicationSceneCreated]\n" );
203 // Open relayout controller to receive relayout requests
206 // Flag request for end of frame
210 void RelayoutController::RequestRelayoutTree( Dali::Actor& actor )
217 // Only set dirty flag if doing relayout and not already marked as dirty
218 Actor& actorImpl = GetImplementation( actor );
219 if( actorImpl.RelayoutPossible() )
221 // If parent is not in relayout we are at the top of a new sub-tree
222 Dali::Actor parent = actor.GetParent();
223 if( !parent || !GetImplementation( parent ).IsRelayoutEnabled() )
228 // Set dirty flag on actors that are enabled
229 actorImpl.SetLayoutDirty( true );
230 actorImpl.SetLayoutNegotiated( false ); // Reset this flag ready for next relayout
233 // Propagate down to children
234 for( uint32_t i = 0; i < actor.GetChildCount(); ++i )
236 Dali::Actor child = actor.GetChildAt( i );
238 RequestRelayoutTree( child );
242 void RelayoutController::PropagateAll( Dali::Actor& actor, Dimension::Type dimension, std::vector< Dali::Actor >& topOfSubTreeStack, std::vector< Dali::Actor >& potentialRedundantSubRoots )
244 // Only set dirty flag if doing relayout and not already marked as dirty
245 Actor& actorImpl = GetImplementation( actor );
246 if( actorImpl.RelayoutPossible( dimension ) )
248 // Set dirty and negotiated flags
249 actorImpl.SetLayoutDirty( true, dimension );
250 actorImpl.SetLayoutNegotiated( false, dimension ); // Reset this flag ready for next relayout
252 // Check for dimension dependecy: width for height/height for width etc
253 // Check each possible dimension and see if it is dependent on the input one
254 for( uint32_t i = 0; i < Dimension::DIMENSION_COUNT; ++i )
256 Dimension::Type dimensionToCheck = static_cast< Dimension::Type >( 1 << i );
258 if( actorImpl.RelayoutDependentOnDimension( dimension, dimensionToCheck ) &&
259 !actorImpl.IsLayoutDirty( dimensionToCheck ) )
261 PropagateAll( actor, dimensionToCheck, topOfSubTreeStack, potentialRedundantSubRoots );
265 // Propagate up to parent
266 Dali::Actor parent = actor.GetParent();
269 Actor& parentImpl = GetImplementation( parent );
270 if( parentImpl.RelayoutDependentOnChildren( dimension ) && !parentImpl.IsLayoutDirty( dimension ) )
272 // Store the highest parent reached
274 for( auto&& element : topOfSubTreeStack )
276 if( element == parent )
285 topOfSubTreeStack.push_back( parent );
289 PropagateAll( parent, dimension, topOfSubTreeStack, potentialRedundantSubRoots );
293 // Propagate down to children
294 for( unsigned int i = 0, childCount = actor.GetChildCount(); i < childCount; ++i )
296 Dali::Actor child = actor.GetChildAt( i );
297 Actor& childImpl = GetImplementation( child );
299 if( childImpl.IsRelayoutEnabled() && childImpl.RelayoutDependentOnParent( dimension ) )
301 if( childImpl.IsLayoutDirty( dimension ) )
303 // We have found a child that could potentially have already been collected for relayout
304 potentialRedundantSubRoots.push_back( child );
308 PropagateAll( child, dimension, topOfSubTreeStack, potentialRedundantSubRoots );
316 void RelayoutController::PropagateFlags( Dali::Actor& actor, Dimension::Type dimension )
318 // Only set dirty flag if doing relayout and not already marked as dirty
319 Actor& actorImpl = GetImplementation( actor );
320 if( actorImpl.IsRelayoutEnabled() )
322 // Set dirty and negotiated flags
323 actorImpl.SetLayoutDirty( true, dimension );
324 actorImpl.SetLayoutNegotiated( false, dimension ); // Reset this flag ready for next relayout
326 // Check for dimension dependecy: width for height/height for width etc
327 // Check each possible dimension and see if it is dependent on the input one
328 for( uint32_t i = 0; i < Dimension::DIMENSION_COUNT; ++i )
330 Dimension::Type dimensionToCheck = static_cast< Dimension::Type >( 1 << i );
332 if( actorImpl.RelayoutDependentOnDimension( dimension, dimensionToCheck ) )
334 PropagateFlags( actor, dimensionToCheck );
338 // Propagate up to parent
339 Dali::Actor parent = actor.GetParent();
342 Actor& parentImpl = GetImplementation( parent );
343 if( parentImpl.RelayoutDependentOnChildren( dimension ) )
346 PropagateFlags( parent, dimension );
350 // Propagate down to children
351 for( uint32_t i = 0, childCount = actor.GetChildCount(); i < childCount; ++i )
353 Dali::Actor child = actor.GetChildAt( i );
354 Actor& childImpl = GetImplementation( child );
356 if( childImpl.RelayoutDependentOnParent( dimension ) )
358 PropagateFlags( child, dimension );
364 void RelayoutController::AddRequest( Dali::Actor& actor )
366 Internal::Actor* actorPtr = &GetImplementation( actor );
368 // Only add the rootActor if it is not already recorded
370 for( auto&& item : mDirtyLayoutSubTrees )
372 if( item == actorPtr )
381 mDirtyLayoutSubTrees.PushBack( actorPtr );
385 void RelayoutController::RemoveRequest( Dali::Actor& actor )
387 Internal::Actor* actorPtr = &GetImplementation( actor );
389 // Remove actor from dirty sub trees
390 for( RawActorList::Iterator it = mDirtyLayoutSubTrees.Begin(), itEnd = mDirtyLayoutSubTrees.End(); it != itEnd; ++it )
392 if( *it == actorPtr )
394 mDirtyLayoutSubTrees.Erase( it );
400 void RelayoutController::Request()
402 mRelayoutFlag = true;
404 if( !mRelayoutConnection )
406 ThreadLocalStorage::Get().GetObjectRegistry().ObjectDestroyedSignal().Connect( mSlotDelegate, &RelayoutController::OnObjectDestroyed );
408 mRelayoutConnection = true;
412 void RelayoutController::OnObjectDestroyed( const Dali::RefObject* object )
414 // Search for and null the object if found in the following lists
415 FindAndZero( mDirtyLayoutSubTrees, object );
418 void RelayoutController::Relayout()
420 // Only do something when requested
423 mPerformingRelayout = true;
425 // Clear the flag as we're now doing the relayout
426 mRelayoutFlag = false;
428 // 1. Finds all top-level controls from the dirty list and allocate them the size of the stage
429 // These controls are paired with the parent/stage size and added to the stack.
430 for( RawActorList::Iterator it = mDirtyLayoutSubTrees.Begin(), itEnd = mDirtyLayoutSubTrees.End(); it != itEnd; ++it )
432 Internal::Actor* dirtyActor = *it;
434 // Need to test if actor is valid (could have been deleted and had the pointer cleared)
437 // Only negotiate actors that are on stage
438 if( dirtyActor->OnStage() )
440 Internal::Actor* parent = dirtyActor->GetParent();
441 QueueActor( dirtyActor, *mRelayoutStack, ( parent ) ? Vector2( parent->GetTargetSize() ) : dirtyActor->GetScene().GetSize() );
446 mDirtyLayoutSubTrees.Clear();
448 // 2. Iterate through the stack until it's empty.
449 if( mRelayoutStack->Size() > 0 )
453 while( mRelayoutStack->Size() > 0 )
457 mRelayoutStack->Get( mRelayoutStack->Size() - 1, actor, size );
458 Actor& actorImpl = GetImplementation( actor );
459 mRelayoutStack->PopBack();
461 if( actorImpl.RelayoutRequired() )
463 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 );
465 // 3. Negotiate the size with the current actor. Pass it an empty container which the actor
466 // has to fill with all the actors it has not done any size negotiation for.
468 actorImpl.NegotiateSize( size, *mRelayoutStack );
470 // Reset the flag so that size negotiation will respect the actor's original resize policy
471 actorImpl.SetUseAssignedSize( false );
475 // We are done with the RelayoutInfos now so delete the pool
476 mRelayoutInfoAllocator.ResetMemoryPool();
481 mPerformingRelayout = false;
483 // should not disconnect the signal as that causes some control size negotiations to not work correctly
484 // this algorithm needs more optimization as well
487 void RelayoutController::SetEnabled( bool enabled )
492 bool RelayoutController::IsPerformingRelayout() const
494 return mPerformingRelayout;
497 void RelayoutController::SetProcessingCoreEvents( bool processingEvents )
499 mProcessingCoreEvents = processingEvents;
502 void RelayoutController::FindAndZero( const RawActorList& list, const Dali::RefObject* object )
504 // Object has been destroyed so clear it from this list
505 for( RawActorList::Iterator it = list.Begin(), itEnd = list.End(); it != itEnd; ++it )
507 BaseObject* actor = *it;
509 if( actor && ( actor == object ) )
511 *it = NULL; // Reset the pointer in the list. We don't want to remove it in case something is iterating over the list.
516 } // namespace Internal