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