[dali_1.1.17] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / update / nodes / node-messages.h
1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_NODE_MESSAGES_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_NODE_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 #include <dali/internal/common/buffer-index.h>
23 #include <dali/internal/common/message.h>
24 #include <dali/internal/update/nodes/node.h>
25 #include <dali/internal/update/manager/update-manager.h>
26
27 namespace Dali
28 {
29
30 namespace Internal
31 {
32
33 namespace SceneGraph
34 {
35
36 // Messages for Node
37
38 class NodePropertyMessageBase : public MessageBase
39 {
40 public:
41
42   /**
43    * Create a message.
44    */
45   NodePropertyMessageBase(UpdateManager& updateManager);
46
47   /**
48    * Virtual destructor
49    */
50   virtual ~NodePropertyMessageBase();
51
52 private:
53
54   // Undefined
55   NodePropertyMessageBase(const NodePropertyMessageBase&);
56   NodePropertyMessageBase& operator=(const NodePropertyMessageBase& rhs);
57
58 protected:
59
60   UpdateManager& mUpdateManager;
61 };
62
63 /**
64  * Templated message which bakes a Node property.
65  */
66 template< typename P >
67 class NodePropertyMessage : public NodePropertyMessageBase
68 {
69 public:
70
71   typedef void(AnimatableProperty<P>::*MemberFunction)( BufferIndex, typename ParameterType< P >::PassingType );
72
73   /**
74    * Create a message.
75    * @note The node is expected to be const in the thread which sends this message.
76    * However it can be modified when Process() is called in a different thread.
77    * @param[in] eventThreadServices The object used to send messages to the scene graph
78    * @param[in] node The node.
79    * @param[in] property The property to bake.
80    * @param[in] member The member function of the object.
81    * @param[in] value The new value of the property.
82    */
83   static void Send( EventThreadServices& eventThreadServices,
84                     const Node* node,
85                     const AnimatableProperty<P>* property,
86                     MemberFunction member,
87                     typename ParameterType< P >::PassingType value )
88   {
89     // Reserve some memory inside the message queue
90     unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( NodePropertyMessage ) );
91
92     // Construct message in the message queue memory; note that delete should not be called on the return value
93     new (slot) NodePropertyMessage( eventThreadServices.GetUpdateManager(), node, property, member, value );
94   }
95
96   /**
97    * Virtual destructor
98    */
99   virtual ~NodePropertyMessage()
100   {
101   }
102
103   /**
104    * @copydoc MessageBase::Process
105    */
106   virtual void Process( BufferIndex updateBufferIndex )
107   {
108     (mProperty->*mMemberFunction)( updateBufferIndex, mParam );
109   }
110
111 private:
112
113   /**
114    * Create a message.
115    * @note The node is expected to be const in the thread which sends this message.
116    * However it can be modified when Process() is called in a different thread.
117    * @param[in] updateManager The update-manager.
118    * @param[in] node The node.
119    * @param[in] property The property to bake.
120    * @param[in] member The member function of the object.
121    * @param[in] value The new value of the property.
122    */
123   NodePropertyMessage( UpdateManager& updateManager,
124                        const Node* node,
125                        const AnimatableProperty<P>* property,
126                        MemberFunction member,
127                        typename ParameterType< P >::PassingType value )
128   : NodePropertyMessageBase( updateManager ),
129     mNode( const_cast< Node* >( node ) ),
130     mProperty( const_cast< AnimatableProperty<P>* >( property ) ),
131     mMemberFunction( member ),
132     mParam( value )
133   {
134   }
135
136 private:
137
138   Node* mNode;
139   AnimatableProperty<P>* mProperty;
140   MemberFunction mMemberFunction;
141   typename ParameterType< P >::HolderType mParam;
142 };
143
144 /**
145  * Templated message which bakes a Node property.
146  */
147 template< typename P >
148 class NodePropertyComponentMessage : public NodePropertyMessageBase
149 {
150 public:
151
152   typedef void(AnimatableProperty<P>::*MemberFunction)( BufferIndex, float );
153
154   /**
155    * Send a message.
156    * @note The node 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 object used to send messages to the scene graph
159    * @param[in] node The node.
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 Node* node,
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( NodePropertyComponentMessage ) );
172
173     // Construct message in the message queue memory; note that delete should not be called on the return value
174     new (slot) NodePropertyComponentMessage( eventThreadServices.GetUpdateManager(), node, property, member, value );
175   }
176
177   /**
178    * Virtual destructor
179    */
180   virtual ~NodePropertyComponentMessage()
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 node 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] updateManager The update-manager.
199    * @param[in] node The node.
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   NodePropertyComponentMessage( UpdateManager& updateManager,
205                                 const Node* node,
206                                 const AnimatableProperty<P>* property,
207                                 MemberFunction member,
208                                 float value )
209   : NodePropertyMessageBase( updateManager ),
210     mNode( const_cast< Node* >( node ) ),
211     mProperty( const_cast< AnimatableProperty<P>* >( property ) ),
212     mMemberFunction( member ),
213     mParam( value )
214   {
215   }
216
217 private:
218
219   Node* mNode;
220   AnimatableProperty<P>* mProperty;
221   MemberFunction mMemberFunction;
222   float mParam;
223 };
224
225 } // namespace SceneGraph
226
227 } // namespace Internal
228
229 } // namespace Dali
230
231 #endif // __DALI_INTERNAL_SCENE_GRAPH_NODE_MESSAGES_H__