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