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