Added memory pool logging
[platform/core/uifw/dali-core.git] / dali / internal / update / common / property-owner.h
1 #ifndef DALI_INTERNAL_SCENE_GRAPH_PROPERTY_OWNER_H
2 #define DALI_INTERNAL_SCENE_GRAPH_PROPERTY_OWNER_H
3
4 /*
5  * Copyright (c) 2022 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 // INTERNAL INCLUDES
22 #include <dali/devel-api/common/owner-container.h>
23 #include <dali/internal/common/const-string.h>
24 #include <dali/internal/common/message.h>
25 #include <dali/internal/update/animation/scene-graph-constraint-declarations.h>
26 #include <dali/internal/update/common/property-base.h>
27 #include <dali/internal/update/common/scene-graph-buffers.h>
28 #include <dali/internal/update/common/uniform-map.h>
29 #include <dali/public-api/common/dali-vector.h>
30
31 namespace Dali
32 {
33 namespace Internal
34 {
35 namespace SceneGraph
36 {
37 class PropertyOwner;
38
39 using OwnedPropertyContainer = OwnerContainer<PropertyBase*>;
40 using OwnedPropertyIter      = OwnedPropertyContainer::Iterator;
41
42 /**
43  * An update-thread object which own properties.
44  * This allows observers to track the lifetime of the object & its properties.
45  */
46 class PropertyOwner
47 {
48 public:
49   class Observer
50   {
51   public:
52     /**
53      * Called when the observable object is connected to the scene graph.
54      * @param[in] owner A reference to the connected PropertyOwner
55      */
56     virtual void PropertyOwnerConnected(PropertyOwner& owner) = 0;
57
58     /**
59      * Called when the observable object is disconnected from the scene graph.
60      * @param[in] currentBufferIndex The buffer to reset.
61      * @param[in] owner A reference to the disconnected PropertyOwner
62      */
63     virtual void PropertyOwnerDisconnected(BufferIndex updateBufferIndex, PropertyOwner& owner) = 0;
64
65     /**
66      * Called shortly before the observable object is destroyed.
67      *
68      * @note Cleanup should be done in both this and PropertyOwnerDisconnected as PropertyOwnerDisconnected
69      * may not be called (i.e. when shutting down).
70      */
71     virtual void PropertyOwnerDestroyed(PropertyOwner& owner) = 0;
72
73   protected:
74     /**
75      * Virtual destructor, no deletion through this interface
76      */
77     virtual ~Observer() = default;
78   };
79
80   /**
81    * Create a property owner.
82    * @return A newly allocated object.
83    */
84   static PropertyOwner* New();
85
86   /**
87    * Virtual destructor; this is intended as a base class.
88    */
89   virtual ~PropertyOwner();
90
91   /**
92    * Add an observer.
93    * The observer is responsible for calling RemoveObserver(*this) during its own destruction.
94    * Connecting an actor-side object as an observer is not allowed, due to thread-safety issues.
95    * @param[in] observer The observer.
96    */
97   void AddObserver(Observer& observer);
98
99   /**
100    * Remove an observer.
101    * @param[in] observer The observer.
102    */
103   void RemoveObserver(Observer& observer);
104
105   /**
106    * This method can be used to determine if there is an animation or
107    * constraint that is using this property owner.
108    * @return true if there are observers.
109    */
110   bool IsObserved();
111
112   /**
113    * Called just before destruction to disconnect all observers and remove constraints.
114    * This occurs when the object is in the process of being destroyed.
115    */
116   void Destroy();
117
118   /**
119    * Notify all observers that the object has been connected
120    * This occurs when the object is connected to the scene-graph during UpdateManager::Update().
121    */
122   void ConnectToSceneGraph();
123
124   /**
125    * Notify all observers that the object has been disconnected and remove constraints.
126    * This occurs when the object is disconnected from the scene-graph during UpdateManager::Update().
127    * @param[in] currentBufferIndex The current update buffer.
128    */
129   void DisconnectFromSceneGraph(BufferIndex updateBufferIndex);
130
131   /**
132    * Reserve the given number of properties
133    */
134   void ReserveProperties(int propertyCount);
135
136   /**
137    * Install a custom property.
138    * @post The PropertyOwner takes ownership of the property.
139    * @param[in] property A pointer to a newly allocated property.
140    */
141   void InstallCustomProperty(OwnerPointer<PropertyBase>& property);
142
143   /**
144    * Retrieve the custom properties owned by the object.
145    * @return A container of properties.
146    */
147   OwnedPropertyContainer& GetCustomProperties()
148   {
149     return mCustomProperties;
150   }
151
152   /**
153    * Retrieve the custom properties owned by the object.
154    * @return A container of properties.
155    */
156   const OwnedPropertyContainer& GetCustomProperties() const
157   {
158     return mCustomProperties;
159   }
160
161   /**
162    * Mark an property owner with the updated flag.
163    * @param[in] updated The updated flag
164    */
165   virtual void SetUpdated(bool updated)
166   {
167     mUpdated = updated;
168   }
169
170   /**
171    * Retrieve if the property owner is updated due to the property is being animating.
172    * @return An updated flag
173    */
174   bool Updated() const
175   {
176     return mUpdated;
177   }
178
179   // Constraints
180
181   /**
182    * Apply a constraint.
183    * @param[in] constraint The constraint to apply.
184    */
185   void ApplyConstraint(OwnerPointer<ConstraintBase>& constraint);
186
187   /**
188    * Begin removal of constraints.
189    * @param[in] constraint The constraint to remove.
190    */
191   void RemoveConstraint(ConstraintBase* constraint);
192
193   /**
194    * Retrieve the constraints that are currently applied.
195    * @return A container of constraints.
196    */
197   ConstraintOwnerContainer& GetConstraints();
198
199   /**
200    * @copydoc UniformMap::Add
201    */
202   virtual void AddUniformMapping(const UniformPropertyMapping& map);
203
204   /**
205    * @copydoc UniformMap::Remove
206    */
207   virtual void RemoveUniformMapping(const ConstString& uniformName);
208
209   /**
210    * Get the mappings table
211    */
212   const UniformMap& GetUniformMap() const;
213
214   /**
215    * @copydoc UniformMap::AddUniformMapObserver
216    */
217   void AddUniformMapObserver(UniformMap::Observer& observer);
218
219   /**
220    * @copydoc UniformMap::RemoveUniformMapObserver
221    */
222   void RemoveUniformMapObserver(UniformMap::Observer& observer);
223
224   /**
225    * Query whether playing an animation is possible or not.
226    * @return true if playing an animation is possible.
227    */
228   virtual bool IsAnimationPossible() const
229   {
230     return true;
231   }
232
233 protected:
234   /**
235    * Protected constructor.
236    */
237   PropertyOwner();
238
239 private:
240   // Undefined
241   PropertyOwner(const PropertyOwner&);
242
243   // Undefined
244   PropertyOwner& operator=(const PropertyOwner& rhs);
245
246 protected:
247   OwnedPropertyContainer mCustomProperties; ///< Properties provided with InstallCustomProperty()
248   UniformMap             mUniformMaps;      ///< Container of owned uniform maps
249   bool                   mUpdated;
250   bool                   mIsConnectedToSceneGraph;
251
252 private:
253   using ObserverContainer = Dali::Vector<PropertyOwner::Observer*>;
254   using ObserverIter      = ObserverContainer::Iterator;
255   using ConstObserverIter = ObserverContainer::ConstIterator;
256
257   ObserverContainer mObservers; ///< Container of observer raw-pointers (not owned)
258
259   ConstraintOwnerContainer mConstraints; ///< Container of owned constraints
260 };
261
262 } // namespace SceneGraph
263
264 } // namespace Internal
265
266 } // namespace Dali
267
268 #endif // DALI_INTERNAL_SCENE_GRAPH_PROPERTY_OWNER_H