f3f8f081edee67aee32609afd2739a730191e533
[platform/core/uifw/dali-core.git] / dali / internal / update / animation / scene-graph-constraint-base.h
1 #ifndef DALI_INTERNAL_SCENE_GRAPH_CONSTRAINT_BASE_H
2 #define DALI_INTERNAL_SCENE_GRAPH_CONSTRAINT_BASE_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 // INTERNAL INCLUDES
22 #include <dali/internal/common/message.h>
23 #include <dali/internal/event/common/event-thread-services.h>
24 #include <dali/internal/update/animation/scene-graph-constraint-declarations.h>
25 #include <dali/internal/update/common/animatable-property.h>
26 #include <dali/internal/update/common/property-owner.h>
27 #include <dali/internal/update/common/scene-graph-buffers.h>
28 #include <dali/public-api/animation/constraint.h>
29 #include <dali/public-api/common/dali-common.h>
30
31 namespace Dali
32 {
33 namespace Internal
34 {
35 // value types used by messages
36 template<>
37 struct ParameterType<Dali::Constraint::RemoveAction>
38 : public BasicType<Dali::Constraint::RemoveAction>
39 {
40 };
41
42 namespace SceneGraph
43 {
44 using PropertyOwnerContainer = Dali::Vector<PropertyOwner*>;
45 using PropertyOwnerIter      = PropertyOwnerContainer::Iterator;
46
47 /**
48  * An abstract base class for Constraints.
49  * This can be used to constrain a property of a scene-object, after animations have been applied.
50  */
51 class ConstraintBase : public PropertyOwner::Observer
52 {
53 public:
54   /**
55    * Observer to determine when the constraint is no longer present
56    */
57   class LifecycleObserver
58   {
59   public:
60     /**
61      * Called shortly before the constraint is destroyed.
62      */
63     virtual void ObjectDestroyed() = 0;
64
65   protected:
66     /**
67      * Virtual destructor, no deletion through this interface
68      */
69     virtual ~LifecycleObserver() = default;
70   };
71
72 public:
73   using RemoveAction = Dali::Constraint::RemoveAction;
74
75   /**
76    * Constructor
77    * @param ownerContainer the properties to constraint
78    * @oparam removeAction perform when removed
79    */
80   ConstraintBase(PropertyOwnerContainer& ownerContainer, RemoveAction removeAction);
81
82   /**
83    * Virtual destructor.
84    */
85   ~ConstraintBase() override;
86
87   /**
88    * Property resetter observes the lifecycle of this object
89    */
90   void AddLifecycleObserver(LifecycleObserver& observer)
91   {
92     mLifecycleObserver = &observer;
93   }
94
95   /**
96    * Property resetter observers the lifecycle of this object
97    */
98   void RemoveLifecycleObserver(LifecycleObserver& observer)
99   {
100     mLifecycleObserver = nullptr;
101   }
102
103   /**
104    * Initialize the constraint.
105    * This should by called by a scene-object, when the constraint is connected.
106    */
107   void OnConnect()
108   {
109     StartObservation();
110
111     mDisconnected = false;
112   }
113
114   /**
115    * @copydoc Dali::Constraint::SetRemoveAction()
116    */
117   void SetRemoveAction(RemoveAction action)
118   {
119     mRemoveAction = action;
120   }
121
122   /**
123    * @copydoc Dali::Constraint::GetRemoveAction()
124    */
125   RemoveAction GetRemoveAction() const
126   {
127     return mRemoveAction;
128   }
129
130   /**
131    * Constrain the associated scene object.
132    * @param[in] updateBufferIndex The current update buffer index.
133    */
134   virtual void Apply(BufferIndex updateBufferIndex) = 0;
135
136   /**
137    * Helper for internal test cases; only available for debug builds.
138    * @return The current number of Constraint instances in existence.
139    */
140   static uint32_t GetCurrentInstanceCount();
141
142   /**
143    * Helper for internal test cases; only available for debug builds.
144    * @return The total number of Constraint instances created during the Dali core lifetime.
145    */
146   static uint32_t GetTotalInstanceCount();
147
148 private:
149   /**
150    * Helper to start observing property owners
151    */
152   void StartObservation()
153   {
154     const PropertyOwnerIter end = mObservedOwners.End();
155     for(PropertyOwnerIter iter = mObservedOwners.Begin(); end != iter; ++iter)
156     {
157       (*iter)->AddObserver(*this);
158     }
159   }
160
161   /**
162    * Helper to stop observing property owners
163    */
164   void StopObservation()
165   {
166     const PropertyOwnerIter end = mObservedOwners.End();
167     for(PropertyOwnerIter iter = mObservedOwners.Begin(); end != iter; ++iter)
168     {
169       (*iter)->RemoveObserver(*this);
170     }
171
172     mObservedOwners.Clear();
173   }
174
175   /**
176    * @copydoc PropertyOwner::Observer::PropertyOwnerConnected()
177    */
178   void PropertyOwnerConnected(PropertyOwner& owner) override
179   {
180   }
181
182   /**
183    * @copydoc PropertyOwner::Observer::PropertyOwnerDisconnected()
184    */
185   void PropertyOwnerDisconnected(BufferIndex bufferIndex, PropertyOwner& owner) override
186   {
187     if(!mDisconnected)
188     {
189       // Stop observing property owners
190       StopObservation();
191
192       // Notification for derived class
193       OnDisconnect();
194
195       mDisconnected = true;
196     }
197   }
198
199   /**
200    * @copydoc PropertyOwner::Observer::PropertyOwnerDestroyed()
201    */
202   void PropertyOwnerDestroyed(PropertyOwner& owner) override
203   {
204     if(!mDisconnected)
205     {
206       // Discard pointer to destroyed property owner. Otherwise StopObservation() would crash when trying to remove
207       //the constraint from the destroyed PropertyOwner's observers list
208       PropertyOwnerIter iter = std::find(mObservedOwners.Begin(), mObservedOwners.End(), &owner);
209       if(mObservedOwners.End() != iter)
210       {
211         mObservedOwners.Erase(iter);
212       }
213
214       // Stop observing the rest of property owners
215       StopObservation();
216
217       // Notification for derived class
218       OnDisconnect();
219
220       mDisconnected = true;
221     }
222   }
223
224   /**
225    * Notify the derived class to disconnect from property owners
226    */
227   virtual void OnDisconnect() = 0;
228
229 protected:
230   RemoveAction mRemoveAction;
231
232   bool mFirstApply : 1;
233   bool mDisconnected : 1;
234
235 private:
236   PropertyOwnerContainer mObservedOwners;    ///< A set of pointers to each observed object. Not owned.
237   LifecycleObserver*     mLifecycleObserver; ///< Resetter observers this object
238
239 #ifdef DEBUG_ENABLED
240   static uint32_t mCurrentInstanceCount; ///< The current number of Constraint instances in existence.
241   static uint32_t mTotalInstanceCount;   ///< The total number of Constraint instances created during the Dali core lifetime.
242 #endif
243 };
244
245 // Messages for ConstraintBase
246
247 inline void SetRemoveActionMessage(EventThreadServices& eventThreadServices, const ConstraintBase& constraint, Dali::Constraint::RemoveAction removeAction)
248 {
249   using LocalType = MessageValue1<ConstraintBase, Dali::Constraint::RemoveAction>;
250
251   // Reserve some memory inside the message queue
252   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
253
254   // Construct message in the message queue memory; note that delete should not be called on the return value
255   new(slot) LocalType(&constraint, &ConstraintBase::SetRemoveAction, removeAction);
256 }
257
258 } // namespace SceneGraph
259
260 } // namespace Internal
261
262 } // namespace Dali
263
264 #endif // DALI_INTERNAL_SCENE_GRAPH_CONSTRAINT_BASE_H