85cb31a094dbad00cd3dbce158c845db45604ff5
[platform/core/uifw/dali-core.git] / dali / internal / event / size-negotiation / relayout-controller-impl.h
1 #ifndef DALI_INTERNAL_RELAYOUT_CONTROLLER_IMPL_H
2 #define DALI_INTERNAL_RELAYOUT_CONTROLLER_IMPL_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <cstdint>
23
24 // INTERNAL INCLUDES
25 #include <dali/internal/common/memory-pool-object-allocator.h>
26 #include <dali/internal/event/size-negotiation/memory-pool-relayout-container.h>
27 #include <dali/public-api/common/vector-wrapper.h>
28 #include <dali/public-api/object/base-object.h>
29 #include <dali/public-api/size-negotiation/relayout-container.h>
30
31 namespace Dali
32 {
33 namespace Integration
34 {
35 class RenderController;
36 }
37
38 namespace Internal
39 {
40 /**
41  * @brief The relayout controller is responsible for taking request from actors to relayout their sizes.
42  * The requests are actioned on at the end of the frame where all actors that have made a request are
43  * resized.
44  */
45 class RelayoutController : public Dali::BaseObject
46 {
47 public:
48   /**
49    * @brief Constructor.
50    * We should only create a unique instance.
51    * @param[in] controller to request a render from the RenderController if core is not processing events.
52    */
53   RelayoutController(Integration::RenderController& controller);
54
55   /**
56    * Destructor
57    */
58   ~RelayoutController() override;
59
60   /**
61    * @brief Get the singleton of RelayoutController object.
62    *
63    * @return A handle to the RelayoutController control.
64    */
65   static RelayoutController* Get();
66
67   /**
68    * @brief Request to relayout the given actor and all sub-actors of it.
69    *
70    * This flags the actor and all actors dependent on it for relayout. The actual
71    * relayout is performed at the end of the frame. This means that multiple calls to relayout
72    * will not cause multiple relayouts to occur.
73    *
74    * @param[in] actor The actor to request relayout on
75    * @param[in] dimension The dimension(s) to request the relayout on. Defaults to all dimensions
76    */
77   void RequestRelayout(Dali::Actor& actor, Dimension::Type dimension = Dimension::ALL_DIMENSIONS);
78
79   /**
80    * @brief Request to relayout of all actors in the sub-tree below the given actor.
81    *
82    * This flags the actor and all actors below it for relayout. The actual
83    * relayout is performed at the end of the frame. This means that multiple calls to relayout
84    * will not cause multiple relayouts to occur.
85    *
86    * @param[in] actor The actor to request relayout on
87    */
88   void RequestRelayoutTree(Dali::Actor& actor);
89
90   /**
91    * @brief Force propagate relayout flags through the tree. This is similiar to Request Relayout
92    * except all dependencies have their flags reset in spite of whether they are all ready set.
93    *
94    * This is useful for resetting layout flags during the layout process.
95    *
96    * @param[in] actor The actor to propagate from
97    * @param[in] dimension The dimension to propagate on
98    */
99   void PropagateFlags(Dali::Actor& actor, Dimension::Type dimension = Dimension::ALL_DIMENSIONS);
100
101   /**
102    * @brief Relayouts all actors that have been marked as dirty
103    */
104   void Relayout();
105
106   /**
107    * @brief Enable/disable the controller
108    *
109    * @param[in] enabled Flag to indicate whether the controller should be enabled or disabled
110    */
111   void SetEnabled(bool enabled);
112
113   /**
114    * @brief Return true if the relayout controller is currently performing a relayout
115    *
116    * @return Return true if the relayout controller is currently performing a relayout
117    */
118   bool IsPerformingRelayout() const;
119
120   /**
121    * @brief Sets whether core is processing events.
122    *
123    * @param[in] processingEvents whether core is processing events.
124    */
125   void SetProcessingCoreEvents(bool processingEvents);
126
127 public: // CALLBACKS
128   /**
129    * @brief Callback raised after the application creates the scene
130    */
131   void OnApplicationSceneCreated();
132
133   /**
134    * @brief Callback for when an object is destroyed
135    *
136    * @param[in] object The object being destroyed
137    */
138   void OnObjectDestroyed(const Dali::RefObject* object);
139
140 private:
141   using RawActorList = Dali::Vector<Dali::Internal::Actor*>;
142
143   /**
144    * @brief Request for relayout. Relays out whole scene.
145    */
146   void Request();
147
148   /**
149    * @brief Add actor to request list
150    *
151    * @param[in] actor The root of the sub tree to add
152    */
153   void AddRequest(Dali::Actor& actor);
154
155   /**
156    * @brief Remove actor from request list
157    *
158    * @param[in] actor The root of the sub tree to remove
159    */
160   void RemoveRequest(Dali::Actor& actor);
161
162   /**
163    * @brief Disconnect the Relayout() method from the Stage::EventProcessingFinishedSignal().
164    */
165   void Disconnect();
166
167   /**
168    * @brief Propagate dirty layout flags to actor and all sub-actors. This will stop propagating when a dirty actor
169    * is found.
170    *
171    * @param[in] actor The actor to propagate on
172    * @param[in] dimension The dimension to propagate on
173    * @param[in] topOfSubTreeStack The top of the sub tree that this actor is in
174    * @param[in] potentialRedundantSubRoots Actors collected as potentially already being included in relayout
175    */
176   void PropagateAll(Dali::Actor& actor, Dimension::Type dimension, std::vector<Dali::Actor>& topOfSubTreeStack, std::vector<Dali::Actor>& potentialRedundantSubRoots);
177
178   /**
179    * Queue an actor on the relayout container
180    *
181    * @param[in] actor The actor to be queued
182    * @param[in] actors The container to add the actor to
183    * @param[in] size The size that this actor should be
184    */
185   void QueueActor(Internal::Actor* actor, RelayoutContainer& actors, Vector2 size);
186
187   /**
188    * @brief Find the given object in the list and null it out
189    *
190    * @param[in] list The list to search
191    * @param[in] object The object to search for
192    */
193   void FindAndZero(const RawActorList& list, const Dali::RefObject* object);
194
195   // Undefined
196   RelayoutController(const RelayoutController&) = delete;
197   RelayoutController& operator=(const RelayoutController&) = delete;
198
199 private:
200   Integration::RenderController&                                       mRenderController;
201   MemoryPoolObjectAllocator<MemoryPoolRelayoutContainer::RelayoutInfo> mRelayoutInfoAllocator;
202
203   SlotDelegate<RelayoutController> mSlotDelegate;
204
205   RawActorList                 mDirtyLayoutSubTrees; ///< List of roots of sub trees that are dirty
206   MemoryPoolRelayoutContainer* mRelayoutStack;       ///< Stack for relayouting
207
208   bool mRelayoutConnection : 1;   ///< Whether EventProcessingFinishedSignal signal is connected.
209   bool mRelayoutFlag : 1;         ///< Relayout flag to avoid unnecessary calls
210   bool mEnabled : 1;              ///< Initially disabled. Must be enabled at some point.
211   bool mPerformingRelayout : 1;   ///< The relayout controller is currently performing a relayout
212   bool mProcessingCoreEvents : 1; ///< Whether core is processing events.
213 };
214
215 } // namespace Internal
216
217 } // namespace Dali
218
219 #endif // DALI_INTERNAL_RELAYOUT_CONTROLLER_IMPL_H