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