[dali_1.1.14] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / update / common / property-owner-messages.h
1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_PROPERTY_OWNER_MESSAGES_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_PROPERTY_OWNER_MESSAGES_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
23 #include <dali/internal/event/common/event-thread-services.h>
24 #include <dali/internal/event/common/property-input-impl.h>
25 #include <dali/internal/update/common/property-owner.h>
26 #include <dali/internal/update/animation/scene-graph-constraint-base.h>
27 #include <string>
28
29 namespace Dali
30 {
31 namespace Internal
32 {
33 namespace SceneGraph
34 {
35 class UniformMap;
36 class PropertyOwner;
37
38 // Property Messages for PropertyOwner
39
40 /**
41  * A base class for property owner property messages.
42  * (For future optimization - see NodeMessageBase & Node.SetActive())
43  */
44 class PropertyOwnerMessageBase : public MessageBase
45 {
46 public:
47
48   /**
49    * Create a message.
50    */
51   PropertyOwnerMessageBase();
52
53   /**
54    * Virtual destructor
55    */
56   virtual ~PropertyOwnerMessageBase();
57
58 private:
59
60   // Undefined
61   PropertyOwnerMessageBase(const PropertyOwnerMessageBase&);
62   PropertyOwnerMessageBase& operator=(const PropertyOwnerMessageBase& rhs);
63 };
64
65 /**
66  * Templated message which bakes a property.
67  */
68 template< typename P >
69 class AnimatablePropertyMessage : public PropertyOwnerMessageBase
70 {
71 public:
72
73   typedef void(AnimatableProperty<P>::*MemberFunction)( BufferIndex, typename ParameterType< P >::PassingType );
74
75   /**
76    * Create a message.
77    * @note The scene object is expected to be const in the thread which sends this message.
78    * However it can be modified when Process() is called in a different thread.
79    * @param[in] eventThreadServices The object used to send messages to the scene graph
80    * @param[in] sceneObject The property owner scene object
81    * @param[in] property The property to bake.
82    * @param[in] member The member function of the object.
83    * @param[in] value The new value of the property.
84    */
85   static void Send( EventThreadServices& eventThreadServices,
86                     const PropertyOwner* sceneObject,
87                     const AnimatableProperty<P>* property,
88                     MemberFunction member,
89                     typename ParameterType< P >::PassingType value )
90   {
91     // Reserve some memory inside the message queue
92     unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( AnimatablePropertyMessage ) );
93
94     // Construct message in the message queue memory; note that delete should not be called on the return value
95     new (slot) AnimatablePropertyMessage( sceneObject, property, member, value );
96   }
97
98   /**
99    * Virtual destructor
100    */
101   virtual ~AnimatablePropertyMessage()
102   {
103   }
104
105   /**
106    * @copydoc MessageBase::Process
107    */
108   virtual void Process( BufferIndex updateBufferIndex )
109   {
110     (mProperty->*mMemberFunction)( updateBufferIndex, mParam );
111   }
112
113 private:
114
115   /**
116    * Create a message.
117    * @note The property owner is expected to be const in the thread which sends this message.
118    * However it can be modified when Process() is called in a different thread.
119    * @param[in] sceneObject the property owner scene object
120    * @param[in] property The property to bake.
121    * @param[in] member The member function of the object.
122    * @param[in] value The new value of the property.
123    */
124   AnimatablePropertyMessage( const PropertyOwner* sceneObject,
125                              const AnimatableProperty<P>* property,
126                              MemberFunction member,
127                              typename ParameterType< P >::PassingType value )
128   : PropertyOwnerMessageBase(),
129     mSceneObject( const_cast< PropertyOwner* >( sceneObject ) ),
130     mProperty( const_cast< AnimatableProperty<P>* >( property ) ),
131     mMemberFunction( member ),
132     mParam( value )
133   {
134   }
135
136 private:
137
138   PropertyOwner* mSceneObject;
139   AnimatableProperty<P>* mProperty;
140   MemberFunction mMemberFunction;
141   typename ParameterType< P >::HolderType mParam;
142 };
143
144 /**
145  * Templated message which bakes a property.
146  */
147 template< typename P >
148 class AnimatablePropertyComponentMessage : public PropertyOwnerMessageBase
149 {
150 public:
151
152   typedef void(AnimatableProperty<P>::*MemberFunction)( BufferIndex, float );
153
154   /**
155    * Send a message.
156    * @note The scene object is expected to be const in the thread which sends this message.
157    * However it can be modified when Process() is called in a different thread.
158    * @param[in] eventThreadServices The service object used for sending messages to the scene graph
159    * @param[in] sceneObject The property owner scene object
160    * @param[in] property The property to bake.
161    * @param[in] member The member function of the object.
162    * @param[in] value The new value of the X,Y,Z or W component.
163    */
164   static void Send( EventThreadServices& eventThreadServices,
165                     const PropertyOwner* sceneObject,
166                     const AnimatableProperty<P>* property,
167                     MemberFunction member,
168                     float value )
169   {
170     // Reserve some memory inside the message queue
171     unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( AnimatablePropertyComponentMessage ) );
172
173     // Construct message in the message queue memory; note that delete should not be called on the return value
174     new (slot) AnimatablePropertyComponentMessage( sceneObject, property, member, value );
175   }
176
177   /**
178    * Virtual destructor
179    */
180   virtual ~AnimatablePropertyComponentMessage()
181   {
182   }
183
184   /**
185    * @copydoc MessageBase::Process
186    */
187   virtual void Process( BufferIndex updateBufferIndex )
188   {
189     (mProperty->*mMemberFunction)( updateBufferIndex, mParam );
190   }
191
192 private:
193
194   /**
195    * Create a message.
196    * @note The scene object is expected to be const in the thread which sends this message.
197    * However it can be modified when Process() is called in a different thread.
198    * @param[in] sceneObject The property owner scene object
199    * @param[in] property The property to bake.
200    * @param[in] member The member function of the object.
201    * @param[in] value The new value of the X,Y,Z or W component.
202   */
203   AnimatablePropertyComponentMessage( const PropertyOwner* sceneObject,
204                                       const AnimatableProperty<P>* property,
205                                       MemberFunction member,
206                                       float value )
207   : PropertyOwnerMessageBase(),
208     mSceneObject( const_cast< PropertyOwner* >( sceneObject ) ),
209     mProperty( const_cast< AnimatableProperty<P>* >( property ) ),
210     mMemberFunction( member ),
211     mParam( value )
212   {
213   }
214
215 private:
216   PropertyOwner* mSceneObject;
217   AnimatableProperty<P>* mProperty;
218   MemberFunction mMemberFunction;
219   float mParam;
220 };
221
222
223 // Messages for PropertyOwner
224
225 inline void InstallCustomPropertyMessage( EventThreadServices& eventThreadServices, const PropertyOwner& owner, PropertyBase* property )
226 {
227   typedef MessageValue1< PropertyOwner, OwnerPointer<PropertyBase> > LocalType;
228
229   // Reserve some memory inside the message queue
230   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
231
232   // Construct message in the message queue memory; note that delete should not be called on the return value
233   new (slot) LocalType( &owner, &PropertyOwner::InstallCustomProperty, property );
234 }
235
236 inline void ApplyConstraintMessage( EventThreadServices& eventThreadServices, const PropertyOwner& owner, ConstraintBase& constraint )
237 {
238   typedef MessageValue1< PropertyOwner, OwnerPointer<ConstraintBase> > LocalType;
239
240   // Reserve some memory inside the message queue
241   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
242
243   // Construct message in the message queue memory; note that delete should not be called on the return value
244   new (slot) LocalType( &owner, &PropertyOwner::ApplyConstraint, &constraint );
245 }
246
247 inline void RemoveConstraintMessage( EventThreadServices& eventThreadServices, const PropertyOwner& owner, const ConstraintBase& constConstraint )
248 {
249   // The update-thread can modify this object.
250   ConstraintBase& constraint = const_cast< ConstraintBase& >( constConstraint );
251
252   typedef MessageValue1< PropertyOwner, ConstraintBase* > LocalType;
253
254   // Reserve some memory inside the message queue
255   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
256
257   // Construct message in the message queue memory; note that delete should not be called on the return value
258   new (slot) LocalType( &owner, &PropertyOwner::RemoveConstraint, &constraint );
259 }
260
261 inline void AddUniformMapMessage( EventThreadServices& eventThreadServices, const PropertyOwner& owner, UniformPropertyMapping* map )
262 {
263   typedef MessageValue1< PropertyOwner, OwnerPointer< UniformPropertyMapping > > LocalType;
264   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
265   new (slot) LocalType( &owner, &PropertyOwner::AddUniformMapping, map );
266 }
267
268 inline void RemoveUniformMapMessage( EventThreadServices& eventThreadServices, const PropertyOwner& owner, const std::string& uniformName )
269 {
270   typedef MessageValue1< PropertyOwner, std::string > LocalType;
271   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
272   new (slot) LocalType( &owner, &PropertyOwner::RemoveUniformMapping, uniformName );
273 }
274
275
276 } // namespace SceneGraph
277
278 } // namespace Internal
279
280 } // namespace Dali
281
282 #endif // __DALI_INTERNAL_SCENE_GRAPH_PROPERTY_OWNER_MESSAGES_H__