Merge branch devel/master (1.0.49) into tizen
[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/common/double-buffered-property.h>
27 #include <dali/internal/update/animation/scene-graph-constraint-base.h>
28 #include <string>
29
30 namespace Dali
31 {
32 namespace Internal
33 {
34 namespace SceneGraph
35 {
36 class UniformMap;
37 class PropertyOwner;
38
39 // Property Messages for PropertyOwner
40
41 /**
42  * A base class for property owner property messages.
43  * (For future optimization - see NodeMessageBase & Node.SetActive())
44  */
45 class PropertyOwnerMessageBase : public MessageBase
46 {
47 public:
48
49   /**
50    * Create a message.
51    */
52   PropertyOwnerMessageBase();
53
54   /**
55    * Virtual destructor
56    */
57   virtual ~PropertyOwnerMessageBase();
58
59 private:
60
61   // Undefined
62   PropertyOwnerMessageBase(const PropertyOwnerMessageBase&);
63   PropertyOwnerMessageBase& operator=(const PropertyOwnerMessageBase& rhs);
64 };
65
66 /**
67  * Templated message which bakes a property.
68  */
69 template< typename P >
70 class AnimatablePropertyMessage : public PropertyOwnerMessageBase
71 {
72 public:
73
74   typedef void(AnimatableProperty<P>::*MemberFunction)( BufferIndex, typename ParameterType< P >::PassingType );
75
76   /**
77    * Create a message.
78    * @note The scene object is expected to be const in the thread which sends this message.
79    * However it can be modified when Process() is called in a different thread.
80    * @param[in] eventThreadServices The object used to send messages to the scene graph
81    * @param[in] sceneObject The property owner scene object
82    * @param[in] property The property to bake.
83    * @param[in] member The member function of the object.
84    * @param[in] value The new value of the property.
85    */
86   static void Send( EventThreadServices& eventThreadServices,
87                     const PropertyOwner* sceneObject,
88                     const AnimatableProperty<P>* property,
89                     MemberFunction member,
90                     typename ParameterType< P >::PassingType value )
91   {
92     // Reserve some memory inside the message queue
93     unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( AnimatablePropertyMessage ) );
94
95     // Construct message in the message queue memory; note that delete should not be called on the return value
96     new (slot) AnimatablePropertyMessage( sceneObject, property, member, value );
97   }
98
99   /**
100    * Virtual destructor
101    */
102   virtual ~AnimatablePropertyMessage()
103   {
104   }
105
106   /**
107    * @copydoc MessageBase::Process
108    */
109   virtual void Process( BufferIndex updateBufferIndex )
110   {
111     (mProperty->*mMemberFunction)( updateBufferIndex, mParam );
112   }
113
114 private:
115
116   /**
117    * Create a message.
118    * @note The property owner is expected to be const in the thread which sends this message.
119    * However it can be modified when Process() is called in a different thread.
120    * @param[in] sceneObject the property owner scene object
121    * @param[in] property The property to bake.
122    * @param[in] member The member function of the object.
123    * @param[in] value The new value of the property.
124    */
125   AnimatablePropertyMessage( const PropertyOwner* sceneObject,
126                              const AnimatableProperty<P>* property,
127                              MemberFunction member,
128                              typename ParameterType< P >::PassingType value )
129   : PropertyOwnerMessageBase(),
130     mSceneObject( const_cast< PropertyOwner* >( sceneObject ) ),
131     mProperty( const_cast< AnimatableProperty<P>* >( property ) ),
132     mMemberFunction( member ),
133     mParam( value )
134   {
135   }
136
137 private:
138
139   PropertyOwner* mSceneObject;
140   AnimatableProperty<P>* mProperty;
141   MemberFunction mMemberFunction;
142   typename ParameterType< P >::HolderType mParam;
143 };
144
145 /**
146  * Templated message which bakes a property.
147  */
148 template< typename P >
149 class AnimatablePropertyComponentMessage : public PropertyOwnerMessageBase
150 {
151 public:
152
153   typedef void(AnimatableProperty<P>::*MemberFunction)( BufferIndex, float );
154
155   /**
156    * Send a message.
157    * @note The scene object is expected to be const in the thread which sends this message.
158    * However it can be modified when Process() is called in a different thread.
159    * @param[in] eventThreadServices The service object used for sending messages to the scene graph
160    * @param[in] sceneObject The property owner scene object
161    * @param[in] property The property to bake.
162    * @param[in] member The member function of the object.
163    * @param[in] value The new value of the X,Y,Z or W component.
164    */
165   static void Send( EventThreadServices& eventThreadServices,
166                     const PropertyOwner* sceneObject,
167                     const AnimatableProperty<P>* property,
168                     MemberFunction member,
169                     float value )
170   {
171     // Reserve some memory inside the message queue
172     unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( AnimatablePropertyComponentMessage ) );
173
174     // Construct message in the message queue memory; note that delete should not be called on the return value
175     new (slot) AnimatablePropertyComponentMessage( sceneObject, property, member, value );
176   }
177
178   /**
179    * Virtual destructor
180    */
181   virtual ~AnimatablePropertyComponentMessage()
182   {
183   }
184
185   /**
186    * @copydoc MessageBase::Process
187    */
188   virtual void Process( BufferIndex updateBufferIndex )
189   {
190     (mProperty->*mMemberFunction)( updateBufferIndex, mParam );
191   }
192
193 private:
194
195   /**
196    * Create a message.
197    * @note The scene object is expected to be const in the thread which sends this message.
198    * However it can be modified when Process() is called in a different thread.
199    * @param[in] sceneObject The property owner scene object
200    * @param[in] property The property to bake.
201    * @param[in] member The member function of the object.
202    * @param[in] value The new value of the X,Y,Z or W component.
203   */
204   AnimatablePropertyComponentMessage( const PropertyOwner* sceneObject,
205                                       const AnimatableProperty<P>* property,
206                                       MemberFunction member,
207                                       float value )
208   : PropertyOwnerMessageBase(),
209     mSceneObject( const_cast< PropertyOwner* >( sceneObject ) ),
210     mProperty( const_cast< AnimatableProperty<P>* >( property ) ),
211     mMemberFunction( member ),
212     mParam( value )
213   {
214   }
215
216 private:
217   PropertyOwner* mSceneObject;
218   AnimatableProperty<P>* mProperty;
219   MemberFunction mMemberFunction;
220   float mParam;
221 };
222
223
224 /**
225  * Template class for sending messages to double buffered properties in a PropertyOwner
226  */
227 template< typename P >
228 class DoubleBufferedPropertyMessage : public PropertyOwnerMessageBase
229 {
230 public:
231
232   typedef void(DoubleBufferedProperty<P>::*MemberFunction)( BufferIndex, typename ParameterType< P >::PassingType );
233
234   /**
235    * Create a message.
236    * @note The scene object is expected to be const in the thread which sends this message.
237    * However it can be modified when Process() is called in a different thread.
238    * @param[in] eventThreadServices The object used to send messages to the scene graph
239    * @param[in] sceneObject The property owner scene object
240    * @param[in] property The property to set.
241    * @param[in] member The member function of the object.
242    * @param[in] value The new value of the property.
243    */
244   static void Send( EventThreadServices& eventThreadServices,
245                     const PropertyOwner* sceneObject,
246                     const DoubleBufferedProperty<P>* property,
247                     MemberFunction member,
248                     typename ParameterType< P >::PassingType value )
249   {
250     // Reserve some memory inside the message queue
251     unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( DoubleBufferedPropertyMessage ) );
252
253     // Construct message in the message queue memory; note that delete should not be called on the return value
254     new (slot) DoubleBufferedPropertyMessage( sceneObject, property, member, value );
255   }
256
257   /**
258    * Virtual destructor
259    */
260   virtual ~DoubleBufferedPropertyMessage()
261   {
262   }
263
264   /**
265    * @copydoc MessageBase::Process
266    */
267   virtual void Process( BufferIndex updateBufferIndex )
268   {
269     DALI_ASSERT_DEBUG( mProperty && "Message does not have an object" );
270     (mProperty->*mMemberFunction)( updateBufferIndex,
271                                    ParameterType< P >::PassObject( mParam ) );
272   }
273
274 private:
275
276   /**
277    * Create a message.
278    * @note The property owner is expected to be const in the thread which sends this message.
279    * However it can be modified when Process() is called in a different thread.
280    * @param[in] sceneObject the property owner scene object
281    * @param[in] property The property to set.
282    * @param[in] member The member function of the object.
283    * @param[in] value The new value of the property.
284    */
285   DoubleBufferedPropertyMessage( const PropertyOwner* sceneObject,
286                                  const DoubleBufferedProperty<P>* property,
287                                  MemberFunction member,
288                                  typename ParameterType< P >::PassingType value )
289   : PropertyOwnerMessageBase(),
290     mSceneObject( const_cast< PropertyOwner* >( sceneObject ) ),
291     mProperty( const_cast< DoubleBufferedProperty<P>* >( property ) ),
292     mMemberFunction( member ),
293     mParam( value )
294   {
295   }
296
297 private:
298   PropertyOwner* mSceneObject;
299   DoubleBufferedProperty<P>* mProperty;
300   MemberFunction mMemberFunction;
301   typename ParameterType< P >::HolderType mParam;
302 };
303
304
305 // Messages for PropertyOwner
306
307 inline void InstallCustomPropertyMessage( EventThreadServices& eventThreadServices, const PropertyOwner& owner, PropertyBase* property )
308 {
309   typedef MessageValue1< PropertyOwner, OwnerPointer<PropertyBase> > LocalType;
310
311   // Reserve some memory inside the message queue
312   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
313
314   // Construct message in the message queue memory; note that delete should not be called on the return value
315   new (slot) LocalType( &owner, &PropertyOwner::InstallCustomProperty, property );
316 }
317
318 inline void ApplyConstraintMessage( EventThreadServices& eventThreadServices, const PropertyOwner& owner, ConstraintBase& constraint )
319 {
320   typedef MessageValue1< PropertyOwner, OwnerPointer<ConstraintBase> > LocalType;
321
322   // Reserve some memory inside the message queue
323   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
324
325   // Construct message in the message queue memory; note that delete should not be called on the return value
326   new (slot) LocalType( &owner, &PropertyOwner::ApplyConstraint, &constraint );
327 }
328
329 inline void RemoveConstraintMessage( EventThreadServices& eventThreadServices, const PropertyOwner& owner, const ConstraintBase& constConstraint )
330 {
331   // The update-thread can modify this object.
332   ConstraintBase& constraint = const_cast< ConstraintBase& >( constConstraint );
333
334   typedef MessageValue1< PropertyOwner, ConstraintBase* > LocalType;
335
336   // Reserve some memory inside the message queue
337   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
338
339   // Construct message in the message queue memory; note that delete should not be called on the return value
340   new (slot) LocalType( &owner, &PropertyOwner::RemoveConstraint, &constraint );
341 }
342
343 inline void AddUniformMapMessage( EventThreadServices& eventThreadServices, const PropertyOwner& owner, UniformPropertyMapping* map )
344 {
345   typedef MessageValue1< PropertyOwner, OwnerPointer< UniformPropertyMapping > > LocalType;
346   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
347   new (slot) LocalType( &owner, &PropertyOwner::AddUniformMapping, map );
348 }
349
350 inline void RemoveUniformMapMessage( EventThreadServices& eventThreadServices, const PropertyOwner& owner, const std::string& uniformName )
351 {
352   typedef MessageValue1< PropertyOwner, std::string > LocalType;
353   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
354   new (slot) LocalType( &owner, &PropertyOwner::RemoveUniformMapping, uniformName );
355 }
356
357
358 } // namespace SceneGraph
359
360 } // namespace Internal
361
362 } // namespace Dali
363
364 #endif // __DALI_INTERNAL_SCENE_GRAPH_PROPERTY_OWNER_MESSAGES_H__