Merge "Remove extension from shader." into devel/new_mesh
[platform/core/uifw/dali-core.git] / dali / internal / event / size-negotiation / relayout-controller-impl.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // FILE HEADER
19
20 #include "relayout-controller-impl.h"
21
22 // EXTERNAL INCLUDES
23 #if defined(DEBUG_ENABLED)
24 #include <sstream>
25 #include <dali/internal/event/common/system-overlay-impl.h>
26 #endif // defined(DEBUG_ENABLED)
27
28 // INTERNAL INCLUDES
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>
37
38 namespace Dali
39 {
40
41 namespace Internal
42 {
43
44 namespace
45 {
46 #if defined(DEBUG_ENABLED)
47
48 Integration::Log::Filter* gLogFilter( Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_RELAYOUT_CONTROLLER") );
49
50 /**
51  * Prints out all the children of the given actor when debug is enabled.
52  *
53  * @param[in]  actor  The actor whose children to print.
54  * @param[in]  level  The number of " | " to put in front of the children.
55  */
56 void PrintChildren( Dali::Actor actor, int level )
57 {
58   std::ostringstream output;
59
60   for ( int t = 0; t < level; ++t )
61   {
62     output << " | ";
63   }
64
65   output << actor.GetTypeName();
66
67   output << ", " << actor.GetName();
68
69   output << " - Pos: " << actor.GetCurrentPosition() << " Size: " << actor.GetTargetSize();
70
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" );
74
75   output << ", (" << actor.GetObjectPtr() << ")" << std::endl;
76
77   DALI_LOG_INFO( gLogFilter, Debug::Verbose, output.str().c_str() );
78
79   ++level;
80   unsigned int numChildren = actor.GetChildCount();
81   for( unsigned int i=0; i<numChildren; ++i )
82   {
83     PrintChildren( actor.GetChildAt(i), level );
84   }
85   --level;
86 }
87
88 /**
89  * Prints the entire hierarchy of the scene.
90  */
91 void PrintHierarchy()
92 {
93   if ( gLogFilter->IsEnabledFor( Debug::Verbose ) )
94   {
95     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "---------- ROOT LAYER ----------\n" );
96     PrintChildren( Dali::Stage().GetCurrent().GetRootLayer(), 0 );
97   }
98 }
99
100 #define PRINT_HIERARCHY PrintHierarchy()
101
102 #else // defined(DEBUG_ENABLED)
103
104 #define PRINT_HIERARCHY
105
106 #endif // defined(DEBUG_ENABLED)
107
108 } // unnamed namespace
109
110 RelayoutController* RelayoutController::Get()
111 {
112   return &ThreadLocalStorage::Get().GetRelayoutController();
113 }
114
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 ),
122   mEnabled( false ),
123   mPerformingRelayout( false ),
124   mProcessingCoreEvents( false )
125 {
126   // Make space for 32 controls to avoid having to copy construct a lot in the beginning
127   mRelayoutStack->Reserve( 32 );
128 }
129
130 RelayoutController::~RelayoutController()
131 {
132   delete mRelayoutStack;
133 }
134
135 void RelayoutController::QueueActor( Dali::Actor& actor, RelayoutContainer& actors, Vector2 size )
136 {
137   if( GetImplementation( actor ).RelayoutRequired() )
138   {
139     actors.Add( actor, size );
140   }
141 }
142
143 void RelayoutController::RequestRelayout( Dali::Actor& actor, Dimension::Type dimension )
144 {
145   if( !mEnabled )
146   {
147     return;
148   }
149
150   std::vector< Dali::Actor > potentialRedundantSubRoots;
151   std::vector< Dali::Actor > topOfSubTreeStack;
152
153   topOfSubTreeStack.push_back( actor );
154
155   // Propagate on all dimensions
156   for( unsigned int i = 0; i < Dimension::DIMENSION_COUNT; ++i )
157   {
158     if( dimension & ( 1 << i ) )
159     {
160       // Do the propagation
161       PropagateAll( actor, static_cast< Dimension::Type >( 1 << i ), topOfSubTreeStack, potentialRedundantSubRoots );
162     }
163   }
164
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() ) )
169   {
170     // Add sub tree root to relayout list
171     AddRequest( subTreeActor );
172
173     // Flag request for end of frame
174     Request();
175   }
176   else
177   {
178     potentialRedundantSubRoots.push_back( subTreeActor );
179   }
180
181   // Remove any redundant sub-tree heads
182   for( std::vector< Dali::Actor >::iterator it = potentialRedundantSubRoots.begin(), itEnd = potentialRedundantSubRoots.end(); it != itEnd; ++it )
183   {
184     Dali::Actor subRoot = *it;
185
186     RemoveRequest( subRoot );
187   }
188
189   if ( !mProcessingCoreEvents )
190   {
191     mRenderController.RequestProcessEventsOnIdle();
192   }
193 }
194
195 void RelayoutController::OnApplicationSceneCreated()
196 {
197   DALI_LOG_INFO( gLogFilter, Debug::General, "[Internal::RelayoutController::OnApplicationSceneCreated]\n" );
198
199   // Open relayout controller to receive relayout requests
200   mEnabled = true;
201
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 );
206
207   // Flag request for end of frame
208   Request();
209 }
210
211 void RelayoutController::RequestRelayoutTree( Dali::Actor& actor )
212 {
213   if( !mEnabled )
214   {
215     return;
216   }
217
218   // Only set dirty flag if doing relayout and not already marked as dirty
219   Actor& actorImpl = GetImplementation( actor );
220   if( actorImpl.RelayoutPossible() )
221   {
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() )
225     {
226       AddRequest( actor );
227     }
228
229     // Set dirty flag on actors that are enabled
230     actorImpl.SetLayoutDirty( true );
231     actorImpl.SetLayoutNegotiated( false );    // Reset this flag ready for next relayout
232   }
233
234   // Propagate down to children
235   for( unsigned int i = 0; i < actor.GetChildCount(); ++i )
236   {
237     Dali::Actor child = actor.GetChildAt( i );
238
239     RequestRelayoutTree( child );
240   }
241 }
242
243 void RelayoutController::PropagateAll( Dali::Actor& actor, Dimension::Type dimension, std::vector< Dali::Actor >& topOfSubTreeStack, std::vector< Dali::Actor >& potentialRedundantSubRoots )
244 {
245   // Only set dirty flag if doing relayout and not already marked as dirty
246   Actor& actorImpl = GetImplementation( actor );
247   if( actorImpl.RelayoutPossible( dimension ) )
248   {
249     // Set dirty and negotiated flags
250     actorImpl.SetLayoutDirty( true, dimension );
251     actorImpl.SetLayoutNegotiated( false, dimension );    // Reset this flag ready for next relayout
252
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 )
256     {
257       Dimension::Type dimensionToCheck = static_cast< Dimension::Type >( 1 << i );
258
259       if( actorImpl.RelayoutDependentOnDimension( dimension, dimensionToCheck ) &&
260           !actorImpl.IsLayoutDirty( dimensionToCheck ) )
261       {
262         PropagateAll( actor, dimensionToCheck, topOfSubTreeStack, potentialRedundantSubRoots );
263       }
264     }
265
266     // Propagate up to parent
267     Dali::Actor parent = actor.GetParent();
268     if( parent )
269     {
270       Actor& parentImpl = GetImplementation( parent );
271       if( parentImpl.RelayoutDependentOnChildren( dimension ) && !parentImpl.IsLayoutDirty( dimension ) )
272       {
273         // Store the highest parent reached
274         bool found = false;
275         for( unsigned int i = 0, count = topOfSubTreeStack.size(); i < count; ++i )
276         {
277           if( topOfSubTreeStack[ i ] == parent )
278           {
279             found = true;
280             break;
281           }
282         }
283
284         if( !found )
285         {
286           topOfSubTreeStack.push_back( parent );
287         }
288
289         // Propagate up
290         PropagateAll( parent, dimension, topOfSubTreeStack, potentialRedundantSubRoots );
291       }
292     }
293
294     // Propagate down to children
295     for( unsigned int i = 0, childCount = actor.GetChildCount(); i < childCount; ++i )
296     {
297       Dali::Actor child = actor.GetChildAt( i );
298       Actor& childImpl = GetImplementation( child );
299
300       if( childImpl.IsRelayoutEnabled() && childImpl.RelayoutDependentOnParent( dimension ) )
301       {
302         if( childImpl.IsLayoutDirty( dimension ) )
303         {
304           // We have found a child that could potentially have already been collected for relayout
305           potentialRedundantSubRoots.push_back( child );
306         }
307         else
308         {
309           PropagateAll( child, dimension, topOfSubTreeStack, potentialRedundantSubRoots );
310         }
311       }
312     }
313   }
314 }
315
316
317 void RelayoutController::PropagateFlags( Dali::Actor& actor, Dimension::Type dimension )
318 {
319   // Only set dirty flag if doing relayout and not already marked as dirty
320   Actor& actorImpl = GetImplementation( actor );
321   if( actorImpl.IsRelayoutEnabled() )
322   {
323     // Set dirty and negotiated flags
324     actorImpl.SetLayoutDirty( true, dimension );
325     actorImpl.SetLayoutNegotiated( false, dimension );    // Reset this flag ready for next relayout
326
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 )
330     {
331       Dimension::Type dimensionToCheck = static_cast< Dimension::Type >( 1 << i );
332
333       if( actorImpl.RelayoutDependentOnDimension( dimension, dimensionToCheck ) )
334       {
335         PropagateFlags( actor, dimensionToCheck );
336       }
337     }
338
339     // Propagate up to parent
340     Dali::Actor parent = actor.GetParent();
341     if( parent )
342     {
343       Actor& parentImpl = GetImplementation( parent );
344       if( parentImpl.RelayoutDependentOnChildren( dimension ) )
345       {
346         // Propagate up
347         PropagateFlags( parent, dimension );
348       }
349     }
350
351     // Propagate down to children
352     for( unsigned int i = 0, childCount = actor.GetChildCount(); i < childCount; ++i )
353     {
354       Dali::Actor child = actor.GetChildAt( i );
355       Actor& childImpl = GetImplementation( child );
356
357       if( childImpl.RelayoutDependentOnParent( dimension ) )
358       {
359         PropagateFlags( child, dimension );
360       }
361     }
362   }
363 }
364
365 void RelayoutController::AddRequest( Dali::Actor& actor )
366 {
367   BaseObject* actorPtr = &GetImplementation( actor );
368
369   // Only add the rootActor if it is not already recorded
370   bool found = false;
371   for( unsigned int i = 0, count = mDirtyLayoutSubTrees.Size(); i < count; ++i )
372   {
373     if( mDirtyLayoutSubTrees[ i ] == actorPtr )
374     {
375       found = true;
376       break;
377     }
378   }
379
380   if( !found )
381   {
382     mDirtyLayoutSubTrees.PushBack( actorPtr );
383   }
384 }
385
386 void RelayoutController::RemoveRequest( Dali::Actor& actor )
387 {
388   BaseObject* actorPtr = &GetImplementation( actor );
389
390   // Remove actor from dirty sub trees
391   for( RawActorList::Iterator it = mDirtyLayoutSubTrees.Begin(), itEnd = mDirtyLayoutSubTrees.End(); it != itEnd; ++it )
392   {
393     if( *it == actorPtr )
394     {
395       mDirtyLayoutSubTrees.Erase( it );
396       break;
397     }
398   }
399 }
400
401 void RelayoutController::Request()
402 {
403   mRelayoutFlag = true;
404
405   if( !mRelayoutConnection )
406   {
407     Dali::Stage stage = Dali::Stage::GetCurrent();
408     stage.GetObjectRegistry().ObjectDestroyedSignal().Connect( mSlotDelegate, &RelayoutController::OnObjectDestroyed );
409
410     mRelayoutConnection = true;
411   }
412 }
413
414 void RelayoutController::OnObjectDestroyed( const Dali::RefObject* object )
415 {
416   // Search for and null the object if found in the following lists
417   FindAndZero( mDirtyLayoutSubTrees, object );
418 }
419
420 void RelayoutController::Relayout()
421 {
422   // Only do something when requested
423   if( mRelayoutFlag )
424   {
425     mPerformingRelayout = true;
426
427     // Clear the flag as we're now doing the relayout
428     mRelayoutFlag = false;
429
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();
433
434     for( RawActorList::Iterator it = mDirtyLayoutSubTrees.Begin(), itEnd = mDirtyLayoutSubTrees.End(); it != itEnd; ++it )
435     {
436       BaseObject* dirtyActor = *it;
437
438       // Need to test if actor is valid (could have been deleted and had the pointer cleared)
439       if( dirtyActor )
440       {
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 ) );
443
444         // Only negotiate actors that are on stage
445         if( actor.OnStage() )
446         {
447           Dali::Actor parent = actor.GetParent();
448           QueueActor( actor, *mRelayoutStack, ( parent ) ? Vector2( parent.GetTargetSize() ) : stageSize );
449         }
450       }
451     }
452
453     mDirtyLayoutSubTrees.Clear();
454
455     // 2. Iterate through the stack until it's empty.
456     if( mRelayoutStack->Size() > 0 )
457     {
458       PRINT_HIERARCHY;
459
460       while( mRelayoutStack->Size() > 0 )
461       {
462         Dali::Actor actor;
463         Vector2 size;
464         mRelayoutStack->Get( mRelayoutStack->Size() - 1, actor, size );
465         Actor& actorImpl = GetImplementation( actor );
466         mRelayoutStack->PopBack();
467
468         if( actorImpl.RelayoutRequired() )
469         {
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 );
471
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 );
475         }
476       }
477
478       // We are done with the RelayoutInfos now so delete the pool
479       mRelayoutInfoAllocator.ResetMemoryPool();
480
481       PRINT_HIERARCHY;
482     }
483
484     mPerformingRelayout = false;
485   }
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
488 }
489
490 void RelayoutController::SetEnabled( bool enabled )
491 {
492   mEnabled = enabled;
493 }
494
495 bool RelayoutController::IsPerformingRelayout() const
496 {
497   return mPerformingRelayout;
498 }
499
500 void RelayoutController::SetProcessingCoreEvents( bool processingEvents )
501 {
502   mProcessingCoreEvents = processingEvents;
503 }
504
505 void RelayoutController::FindAndZero( const RawActorList& list, const Dali::RefObject* object )
506 {
507   // Object has been destroyed so clear it from this list
508   for( RawActorList::Iterator it = list.Begin(), itEnd = list.End(); it != itEnd; ++it )
509   {
510     BaseObject* actor = *it;
511
512     if( actor && ( actor == object ) )
513     {
514       *it = NULL;    // Reset the pointer in the list. We don't want to remove it in case something is iterating over the list.
515     }
516   }
517 }
518
519 } // namespace Internal
520
521 } // namespace Dali