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