456c3a19f88c4c649230a164a4230ea96b7e6504
[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) 2019 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   ~NodePropertyMessageBase() override;
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   using MemberFunction = void ( AnimatableProperty<P>::* )( BufferIndex, typename ParameterType<P>::PassingType );
71
72   /**
73    * Create a message.
74    * @note The node is expected to be const in the thread which sends this message.
75    * However it can be modified when Process() is called in a different thread.
76    * @param[in] eventThreadServices The object used to send messages to the scene graph
77    * @param[in] node The node.
78    * @param[in] property The property to bake.
79    * @param[in] member The member function of the object.
80    * @param[in] value The new value of the property.
81    */
82   static void Send( EventThreadServices& eventThreadServices,
83                     const Node* node,
84                     const AnimatableProperty<P>* property,
85                     MemberFunction member,
86                     typename ParameterType< P >::PassingType value )
87   {
88     // Reserve some memory inside the message queue
89     uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( NodePropertyMessage ) );
90
91     // Construct message in the message queue memory; note that delete should not be called on the return value
92     new (slot) NodePropertyMessage( eventThreadServices.GetUpdateManager(), node, property, member, value );
93   }
94
95   /**
96    * Virtual destructor
97    */
98   ~NodePropertyMessage() override = default;
99
100   /**
101    * @copydoc MessageBase::Process
102    */
103   void Process( BufferIndex updateBufferIndex ) override
104   {
105     mNode->SetUpdated( true );
106     (mProperty->*mMemberFunction)( updateBufferIndex, mParam );
107   }
108
109 private:
110
111   /**
112    * Create a message.
113    * @note The node 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] updateManager The update-manager.
116    * @param[in] node The node.
117    * @param[in] property The property to bake.
118    * @param[in] member The member function of the object.
119    * @param[in] value The new value of the property.
120    */
121   NodePropertyMessage( UpdateManager& updateManager,
122                        const Node* node,
123                        const AnimatableProperty<P>* property,
124                        MemberFunction member,
125                        typename ParameterType< P >::PassingType value )
126   : NodePropertyMessageBase( updateManager ),
127     mNode( const_cast< Node* >( node ) ),
128     mProperty( const_cast< AnimatableProperty<P>* >( property ) ),
129     mMemberFunction( member ),
130     mParam( value )
131   {
132   }
133
134 private:
135
136   Node* mNode;
137   AnimatableProperty<P>* mProperty;
138   MemberFunction mMemberFunction;
139   typename ParameterType< P >::HolderType mParam;
140 };
141
142 /**
143  * Templated message which bakes a Node property.
144  */
145 template< typename P >
146 class NodePropertyComponentMessage : public NodePropertyMessageBase
147 {
148 public:
149   using MemberFunction = void ( AnimatableProperty<P>::* )( BufferIndex, float );
150
151   /**
152    * Send a message.
153    * @note The node is expected to be const in the thread which sends this message.
154    * However it can be modified when Process() is called in a different thread.
155    * @param[in] eventThreadServices The object used to send messages to the scene graph
156    * @param[in] node The node.
157    * @param[in] property The property to bake.
158    * @param[in] member The member function of the object.
159    * @param[in] value The new value of the X,Y,Z or W component.
160    */
161   static void Send( EventThreadServices& eventThreadServices,
162                     const Node* node,
163                     const AnimatableProperty<P>* property,
164                     MemberFunction member,
165                     float value )
166   {
167     // Reserve some memory inside the message queue
168     uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( NodePropertyComponentMessage ) );
169
170     // Construct message in the message queue memory; note that delete should not be called on the return value
171     new (slot) NodePropertyComponentMessage( eventThreadServices.GetUpdateManager(), node, property, member, value );
172   }
173
174   /**
175    * Virtual destructor
176    */
177   ~NodePropertyComponentMessage() override = default;
178
179   /**
180    * @copydoc MessageBase::Process
181    */
182   void Process( BufferIndex updateBufferIndex ) override
183   {
184     mNode->SetUpdated( true );
185     (mProperty->*mMemberFunction)( updateBufferIndex, mParam );
186   }
187
188 private:
189
190   /**
191    * Create a message.
192    * @note The node 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] updateManager The update-manager.
195    * @param[in] node The node.
196    * @param[in] property The property to bake.
197    * @param[in] member The member function of the object.
198    * @param[in] value The new value of the X,Y,Z or W component.
199   */
200   NodePropertyComponentMessage( UpdateManager& updateManager,
201                                 const Node* node,
202                                 const AnimatableProperty<P>* property,
203                                 MemberFunction member,
204                                 float value )
205   : NodePropertyMessageBase( updateManager ),
206     mNode( const_cast< Node* >( node ) ),
207     mProperty( const_cast< AnimatableProperty<P>* >( property ) ),
208     mMemberFunction( member ),
209     mParam( value )
210   {
211   }
212
213 private:
214
215   Node* mNode;
216   AnimatableProperty<P>* mProperty;
217   MemberFunction mMemberFunction;
218   float mParam;
219 };
220
221
222 template <typename P>
223 class NodeTransformPropertyMessage : public NodePropertyMessageBase
224 {
225 public:
226   using MemberFunction = void ( TransformManagerPropertyHandler<P>::* )( BufferIndex, const P& );
227
228   /**
229    * Create a message.
230    * @note The node is expected to be const in the thread which sends this message.
231    * However it can be modified when Process() is called in a different thread.
232    * @param[in] eventThreadServices The object used to send messages to the scene graph
233    * @param[in] node The node.
234    * @param[in] property The property to bake.
235    * @param[in] member The member function of the object.
236    * @param[in] value The new value of the property.
237    */
238   static void Send( EventThreadServices& eventThreadServices,
239                     const Node* node,
240                     const TransformManagerPropertyHandler<P>* property,
241                     MemberFunction member,
242                     const P& value )
243   {
244     // Reserve some memory inside the message queue
245     uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( NodeTransformPropertyMessage ) );
246
247     // Construct message in the message queue memory; note that delete should not be called on the return value
248     new (slot) NodeTransformPropertyMessage( eventThreadServices.GetUpdateManager(), node, property, member, value );
249   }
250
251   /**
252    * Virtual destructor
253    */
254   ~NodeTransformPropertyMessage() override = default;
255
256   /**
257    * @copydoc MessageBase::Process
258    */
259   void Process( BufferIndex updateBufferIndex ) override
260   {
261     mNode->SetUpdated( true );
262     (mProperty->*mMemberFunction)( updateBufferIndex, mParam );
263   }
264
265 private:
266
267   /**
268    * Create a message.
269    * @note The node is expected to be const in the thread which sends this message.
270    * However it can be modified when Process() is called in a different thread.
271    * @param[in] updateManager The update-manager.
272    * @param[in] node The node.
273    * @param[in] property The property to bake.
274    * @param[in] member The member function of the object.
275    * @param[in] value The new value of the property.
276    */
277   NodeTransformPropertyMessage( UpdateManager& updateManager,
278                        const Node* node,
279                        const TransformManagerPropertyHandler<P>* property,
280                        MemberFunction member,
281                        const P& value )
282   : NodePropertyMessageBase( updateManager ),
283     mNode( const_cast< Node* >( node ) ),
284     mProperty( const_cast< TransformManagerPropertyHandler<P>* >( property ) ),
285     mMemberFunction( member ),
286     mParam( value )
287   {
288   }
289
290 private:
291
292   Node* mNode;
293   TransformManagerPropertyHandler<P>* mProperty;
294   MemberFunction mMemberFunction;
295   P mParam;
296 };
297
298
299 template <typename P>
300 class NodeTransformComponentMessage : public NodePropertyMessageBase
301 {
302 public:
303   using MemberFunction = void ( TransformManagerPropertyHandler<P>::* )( BufferIndex, float );
304
305   /**
306    * Send a message.
307    * @note The node is expected to be const in the thread which sends this message.
308    * However it can be modified when Process() is called in a different thread.
309    * @param[in] eventThreadServices The object used to send messages to the scene graph
310    * @param[in] node The node.
311    * @param[in] property The property to bake.
312    * @param[in] member The member function of the object.
313    * @param[in] value The new value of the X,Y,Z or W component.
314    */
315   static void Send( EventThreadServices& eventThreadServices,
316                     const Node* node,
317                     const TransformManagerPropertyHandler<P>* property,
318                     MemberFunction member,
319                     float value )
320   {
321     // Reserve some memory inside the message queue
322     uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( NodeTransformComponentMessage ) );
323
324     // Construct message in the message queue memory; note that delete should not be called on the return value
325     new (slot) NodeTransformComponentMessage( eventThreadServices.GetUpdateManager(), node, property, member, value );
326   }
327
328   /**
329    * Virtual destructor
330    */
331   ~NodeTransformComponentMessage() override = default;
332
333   /**
334    * @copydoc MessageBase::Process
335    */
336   void Process( BufferIndex updateBufferIndex ) override
337   {
338     mNode->SetUpdated( true );
339     (mProperty->*mMemberFunction)( updateBufferIndex, mParam );
340   }
341
342 private:
343
344   /**
345    * Create a message.
346    * @note The node is expected to be const in the thread which sends this message.
347    * However it can be modified when Process() is called in a different thread.
348    * @param[in] updateManager The update-manager.
349    * @param[in] node The node.
350    * @param[in] property The property to bake.
351    * @param[in] member The member function of the object.
352    * @param[in] value The new value of the X,Y,Z or W component.
353   */
354   NodeTransformComponentMessage( UpdateManager& updateManager,
355                                 const Node* node,
356                                 const TransformManagerPropertyHandler<P>* property,
357                                 MemberFunction member,
358                                 float value )
359   : NodePropertyMessageBase( updateManager ),
360     mNode( const_cast< Node* >( node ) ),
361     mProperty( const_cast< TransformManagerPropertyHandler<P>* >( property ) ),
362     mMemberFunction( member ),
363     mParam( value )
364   {
365   }
366
367 private:
368
369   Node* mNode;
370   TransformManagerPropertyHandler<P>* mProperty;
371   MemberFunction mMemberFunction;
372   float mParam;
373 };
374
375 } // namespace SceneGraph
376
377 } // namespace Internal
378
379 } // namespace Dali
380
381 #endif // DALI_INTERNAL_SCENE_GRAPH_NODE_MESSAGES_H