License conversion from Flora to Apache 2.0
[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 protected:
53
54   /**
55    * Inform UpdateManager that the Node is now active
56    */
57   void NotifyUpdateManager( Node* node );
58
59 private:
60
61   // Undefined
62   NodePropertyMessageBase(const NodePropertyMessageBase&);
63   NodePropertyMessageBase& operator=(const NodePropertyMessageBase& rhs);
64
65 protected:
66
67   UpdateManager& mUpdateManager;
68 };
69
70 /**
71  * Templated message which bakes a Node property.
72  */
73 template< typename P >
74 class NodePropertyMessage : public NodePropertyMessageBase
75 {
76 public:
77
78   typedef void(AnimatableProperty<P>::*MemberFunction)( BufferIndex, typename ParameterType< P >::PassingType );
79
80   /**
81    * Create a message.
82    * @note The node is expected to be const in the thread which sends this message.
83    * However it can be modified when Process() is called in a different thread.
84    * @param[in] updateManager The update-manager.
85    * @param[in] node The node.
86    * @param[in] property The property to bake.
87    * @param[in] member The member function of the object.
88    * @param[in] value The new value of the property.
89    */
90   static void Send( UpdateManager& updateManager,
91                     const Node* node,
92                     const AnimatableProperty<P>* property,
93                     MemberFunction member,
94                     typename ParameterType< P >::PassingType value )
95   {
96     // Reserve some memory inside the message queue
97     unsigned int* slot = updateManager.GetEventToUpdate().ReserveMessageSlot( sizeof( NodePropertyMessage ) );
98
99     // Construct message in the message queue memory; note that delete should not be called on the return value
100     new (slot) NodePropertyMessage( updateManager, node, property, member, value );
101   }
102
103   /**
104    * Virtual destructor
105    */
106   virtual ~NodePropertyMessage()
107   {
108   }
109
110   /**
111    * @copydoc MessageBase::Process
112    */
113   virtual void Process( BufferIndex updateBufferIndex )
114   {
115     (mProperty->*mMemberFunction)( updateBufferIndex, mParam );
116
117     if( ! mNode->IsActive() )
118     {
119       // Inform UpdateManager that the Node is now active
120       NotifyUpdateManager( mNode );
121     }
122   }
123
124 private:
125
126   /**
127    * Create a message.
128    * @note The node is expected to be const in the thread which sends this message.
129    * However it can be modified when Process() is called in a different thread.
130    * @param[in] updateManager The update-manager.
131    * @param[in] node The node.
132    * @param[in] property The property to bake.
133    * @param[in] member The member function of the object.
134    * @param[in] value The new value of the property.
135    */
136   NodePropertyMessage( UpdateManager& updateManager,
137                        const Node* node,
138                        const AnimatableProperty<P>* property,
139                        MemberFunction member,
140                        typename ParameterType< P >::PassingType value )
141   : NodePropertyMessageBase( updateManager ),
142     mNode( const_cast< Node* >( node ) ),
143     mProperty( const_cast< AnimatableProperty<P>* >( property ) ),
144     mMemberFunction( member ),
145     mParam( value )
146   {
147   }
148
149 private:
150
151   Node* mNode;
152   AnimatableProperty<P>* mProperty;
153   MemberFunction mMemberFunction;
154   typename ParameterType< P >::HolderType mParam;
155 };
156
157 /**
158  * Templated message which bakes a Node property.
159  */
160 template< typename P >
161 class NodePropertyComponentMessage : public NodePropertyMessageBase
162 {
163 public:
164
165   typedef void(AnimatableProperty<P>::*MemberFunction)( BufferIndex, float );
166
167   /**
168    * Send a message.
169    * @note The node is expected to be const in the thread which sends this message.
170    * However it can be modified when Process() is called in a different thread.
171    * @param[in] updateManager The update-manager.
172    * @param[in] node The node.
173    * @param[in] property The property to bake.
174    * @param[in] member The member function of the object.
175    * @param[in] value The new value of the X,Y,Z or W component.
176    */
177   static void Send( UpdateManager& updateManager,
178                     const Node* node,
179                     const AnimatableProperty<P>* property,
180                     MemberFunction member,
181                     float value )
182   {
183     // Reserve some memory inside the message queue
184     unsigned int* slot = updateManager.GetEventToUpdate().ReserveMessageSlot( sizeof( NodePropertyComponentMessage ) );
185
186     // Construct message in the message queue memory; note that delete should not be called on the return value
187     new (slot) NodePropertyComponentMessage( updateManager, node, property, member, value );
188   }
189
190   /**
191    * Virtual destructor
192    */
193   virtual ~NodePropertyComponentMessage()
194   {
195   }
196
197   /**
198    * @copydoc MessageBase::Process
199    */
200   virtual void Process( BufferIndex updateBufferIndex )
201   {
202     (mProperty->*mMemberFunction)( updateBufferIndex, mParam );
203
204     if( ! mNode->IsActive() )
205     {
206       // Inform UpdateManager that the Node is now active
207       NotifyUpdateManager( mNode );
208     }
209   }
210
211 private:
212
213   /**
214    * Create a message.
215    * @note The node is expected to be const in the thread which sends this message.
216    * However it can be modified when Process() is called in a different thread.
217    * @param[in] updateManager The update-manager.
218    * @param[in] node The node.
219    * @param[in] property The property to bake.
220    * @param[in] member The member function of the object.
221    * @param[in] value The new value of the X,Y,Z or W component.
222   */
223   NodePropertyComponentMessage( UpdateManager& updateManager,
224                                 const Node* node,
225                                 const AnimatableProperty<P>* property,
226                                 MemberFunction member,
227                                 float value )
228   : NodePropertyMessageBase( updateManager ),
229     mNode( const_cast< Node* >( node ) ),
230     mProperty( const_cast< AnimatableProperty<P>* >( property ) ),
231     mMemberFunction( member ),
232     mParam( value )
233   {
234   }
235
236 private:
237
238   Node* mNode;
239   AnimatableProperty<P>* mProperty;
240   MemberFunction mMemberFunction;
241   float mParam;
242 };
243
244 } // namespace SceneGraph
245
246 } // namespace Internal
247
248 } // namespace Dali
249
250 #endif // __DALI_INTERNAL_SCENE_GRAPH_NODE_MESSAGES_H__