Merge "Ensured that messages are aligned to 8 byte words on 64bit ARM" into tizen
[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) 2014 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/animation/constraint.h>
23 #include <dali/public-api/common/dali-common.h>
24 #include <dali/internal/common/message.h>
25 #include <dali/internal/common/event-to-update.h>
26 #include <dali/internal/update/common/animatable-property.h>
27 #include <dali/internal/update/common/property-owner.h>
28 #include <dali/internal/update/common/scene-graph-buffers.h>
29 #include <dali/internal/update/animation/scene-graph-constraint-declarations.h>
30
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36
37 // value types used by messages
38 template <> struct ParameterType< Dali::Constraint::RemoveAction >
39 : public BasicType< Dali::Constraint::RemoveAction > {};
40
41 namespace SceneGraph
42 {
43
44 typedef Dali::Vector<PropertyOwner*>     PropertyOwnerContainer;
45 typedef PropertyOwnerContainer::Iterator PropertyOwnerIter;
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, public PropertyOwner::Observer
52 {
53 public:
54
55   typedef Dali::Constraint::RemoveAction RemoveAction;
56
57   /**
58    * Constructor
59    */
60   ConstraintBase( PropertyOwnerContainer& ownerContainer );
61
62   /**
63    * Virtual destructor.
64    */
65   virtual ~ConstraintBase();
66
67   /**
68    * Initialize the constraint.
69    * This should by called by a scene-object, when the constraint is connected.
70    */
71   void OnConnect()
72   {
73     StartObservation();
74
75     mDisconnected = false;
76   }
77
78   /**
79    * @copydoc Dali::Constraint::SetRemoveAction()
80    */
81   void SetRemoveAction( RemoveAction action )
82   {
83     mRemoveAction = action;
84   }
85
86   /**
87    * @copydoc Dali::Constraint::GetRemoveAction()
88    */
89   RemoveAction GetRemoveAction() const
90   {
91     return mRemoveAction;
92   }
93
94   /**
95    * Bake the weight property.
96    * @param[in] updateBufferIndex The current update buffer index.
97    * @param[in] weight The new weight.
98    */
99   void BakeWeight( BufferIndex updateBufferIndex, float weight )
100   {
101     mWeight.Bake( updateBufferIndex, weight );
102   }
103
104   /**
105    * Set the initial weight.
106    * @pre The constraint has not been connected to the scene-graph.
107    * @param[in] weight The new weight.
108    */
109   void SetInitialWeight( float weight )
110   {
111     mWeight.SetInitial( weight );
112   }
113
114   /**
115    * Retrieve the weight property.
116    * @param[in] bufferIndex The buffer index to read from.
117    * @return The current weight.
118    */
119   float GetWeight( BufferIndex bufferIndex ) const
120   {
121     return mWeight[ bufferIndex ];
122   }
123
124   /**
125    * Constrain the associated scene object.
126    * @param[in] updateBufferIndex The current update buffer index.
127    */
128   virtual void Apply( BufferIndex updateBufferIndex ) = 0;
129
130   /**
131    * Helper for internal test cases; only available for debug builds.
132    * @return The current number of Constraint instances in existence.
133    */
134   static unsigned int GetCurrentInstanceCount();
135
136   /**
137    * Helper for internal test cases; only available for debug builds.
138    * @return The total number of Constraint instances created during the Dali core lifetime.
139    */
140   static unsigned int GetTotalInstanceCount();
141
142 private:
143
144   /**
145    * Helper to start observing property owners
146    */
147   void StartObservation()
148   {
149     const PropertyOwnerIter end =  mObservedOwners.End();
150     for( PropertyOwnerIter iter = mObservedOwners.Begin(); end != iter; ++iter )
151     {
152       (*iter)->AddObserver( *this );
153     }
154   }
155
156   /**
157    * Helper to stop observing property owners
158    */
159   void StopObservation()
160   {
161     const PropertyOwnerIter end =  mObservedOwners.End();
162     for( PropertyOwnerIter iter = mObservedOwners.Begin(); end != iter; ++iter )
163     {
164       (*iter)->RemoveObserver( *this );
165     }
166
167     mObservedOwners.Clear();
168   }
169
170   /**
171    * @copydoc PropertyOwner::Observer::PropertyOwnerConnected()
172    */
173   virtual void PropertyOwnerConnected( PropertyOwner& owner )
174   {
175   }
176
177   /**
178    * @copydoc PropertyOwner::Observer::PropertyOwnerDisconnected()
179    */
180   virtual void PropertyOwnerDisconnected( BufferIndex bufferIndex, PropertyOwner& owner )
181   {
182     if ( !mDisconnected )
183     {
184       // Stop observing property owners
185       StopObservation();
186
187       // Notification for derived class
188       OnDisconnect();
189
190       mDisconnected = true;
191     }
192   }
193
194   /**
195    * @copydoc PropertyOwner::Observer::PropertyOwnerDestroyed()
196    */
197   virtual void PropertyOwnerDestroyed( PropertyOwner& owner )
198   {
199     if ( !mDisconnected )
200     {
201       // Discard pointer to destroyed property owner. Otherwise StopObservation() would crash when trying to remove
202       //the constraint from the destroyed PropertyOwner's observers list
203       PropertyOwnerIter iter = std::find( mObservedOwners.Begin(), mObservedOwners.End(), &owner );
204       if( mObservedOwners.End() != iter )
205       {
206         mObservedOwners.Erase( iter );
207       }
208
209       // Stop observing the rest of property owners
210       StopObservation();
211
212       // Notification for derived class
213       OnDisconnect();
214
215       mDisconnected = true;
216
217     }
218   }
219
220   /**
221    * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
222    */
223   virtual void ResetDefaultProperties( BufferIndex updateBufferIndex );
224
225   /**
226    * Notify the derived class to disconnect from property owners
227    */
228   virtual void OnDisconnect() = 0;
229
230 public:
231
232   AnimatableProperty<float> mWeight; ///< The constraint is "fully-applied" when weight = 1
233
234 protected:
235
236   RemoveAction mRemoveAction;
237
238   bool mFirstApply   : 1;
239   bool mDisconnected : 1;
240
241 private:
242
243   PropertyOwnerContainer mObservedOwners; ///< A set of pointers to each observed object. Not owned.
244
245 #ifdef DEBUG_ENABLED
246   static unsigned int mCurrentInstanceCount;  ///< The current number of Constraint instances in existence.
247   static unsigned int mTotalInstanceCount;    ///< The total number of Constraint instances created during the Dali core lifetime.
248 #endif
249 };
250
251 // Messages for ConstraintBase
252
253 inline void  BakeWeightMessage( EventToUpdate& eventToUpdate, const ConstraintBase& constraint, float weight )
254 {
255   typedef MessageDoubleBuffered1< ConstraintBase, float > LocalType;
256
257   // Reserve some memory inside the message queue
258   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
259
260   // Construct message in the message queue memory; note that delete should not be called on the return value
261   new (slot) LocalType( &constraint, &ConstraintBase::BakeWeight, weight );
262 }
263
264 inline void  SetRemoveActionMessage( EventToUpdate& eventToUpdate, const ConstraintBase& constraint, Dali::Constraint::RemoveAction removeAction )
265 {
266   typedef MessageValue1< ConstraintBase, Dali::Constraint::RemoveAction > LocalType;
267
268   // Reserve some memory inside the message queue
269   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
270
271   // Construct message in the message queue memory; note that delete should not be called on the return value
272   new (slot) LocalType( &constraint, &ConstraintBase::SetRemoveAction, removeAction );
273 }
274
275 } // namespace SceneGraph
276
277 } // namespace Internal
278
279 } // namespace Dali
280
281 #endif // __DALI_INTERNAL_SCENE_GRAPH_CONSTRAINT_BASE_H__