[dali_2.3.23] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / update / common / property-owner-messages.h
1 #ifndef DALI_INTERNAL_SCENE_GRAPH_PROPERTY_OWNER_MESSAGES_H
2 #define DALI_INTERNAL_SCENE_GRAPH_PROPERTY_OWNER_MESSAGES_H
3
4 /*
5  * Copyright (c) 2022 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 // EXTERNAL INCLUDES
22 #include <string>
23
24 // INTERNAL INCLUDES
25 #include <dali/internal/common/const-string.h>
26 #include <dali/internal/event/common/event-thread-services.h>
27 #include <dali/internal/event/common/property-input-impl.h>
28 #include <dali/internal/update/animation/scene-graph-constraint-base.h>
29 #include <dali/internal/update/common/property-owner.h>
30
31 namespace Dali
32 {
33 namespace Internal
34 {
35 namespace SceneGraph
36 {
37 class UniformMap;
38 class PropertyOwner;
39
40 // Property Messages for PropertyOwner
41
42 /**
43  * A base class for property owner property messages.
44  * (For future optimization - see NodeMessageBase & Node.SetActive())
45  */
46 class PropertyOwnerMessageBase : public MessageBase
47 {
48 public:
49   /**
50    * Create a message.
51    */
52   PropertyOwnerMessageBase();
53
54   /**
55    * Virtual destructor
56    */
57   ~PropertyOwnerMessageBase() override;
58
59 private:
60   // Undefined
61   PropertyOwnerMessageBase(const PropertyOwnerMessageBase&);
62   PropertyOwnerMessageBase& operator=(const PropertyOwnerMessageBase& rhs);
63 };
64
65 /**
66  * Templated message which bakes a property.
67  */
68 template<typename P>
69 class AnimatablePropertyMessage : public PropertyOwnerMessageBase
70 {
71 public:
72   using MemberFunction = void (AnimatableProperty<P>::*)(BufferIndex, typename ParameterType<P>::PassingType);
73
74   /**
75    * Create a message.
76    * @note The scene object is expected to be const in the thread which sends this message.
77    * However it can be modified when Process() is called in a different thread.
78    * @param[in] eventThreadServices The object used to send messages to the scene graph
79    * @param[in] sceneObject The property owner scene object
80    * @param[in] property The property to bake.
81    * @param[in] member The member function of the object.
82    * @param[in] value The new value of the property.
83    */
84   static void Send(EventThreadServices&                   eventThreadServices,
85                    const PropertyOwner*                   sceneObject,
86                    const AnimatableProperty<P>*           property,
87                    MemberFunction                         member,
88                    typename ParameterType<P>::PassingType value)
89   {
90     // Reserve some memory inside the message queue
91     uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(AnimatablePropertyMessage));
92
93     // Construct message in the message queue memory; note that delete should not be called on the return value
94     new(slot) AnimatablePropertyMessage(sceneObject, property, member, value);
95   }
96
97   /**
98    * Virtual destructor
99    */
100   ~AnimatablePropertyMessage() override = default;
101
102   /**
103    * @copydoc MessageBase::Process
104    */
105   void Process(BufferIndex updateBufferIndex) override
106   {
107     (mProperty->*mMemberFunction)(updateBufferIndex, mParam);
108   }
109
110 private:
111   /**
112    * Create a message.
113    * @note The property owner 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] sceneObject the property owner scene object
116    * @param[in] property The property to bake.
117    * @param[in] member The member function of the object.
118    * @param[in] value The new value of the property.
119    */
120   AnimatablePropertyMessage(const PropertyOwner*                   sceneObject,
121                             const AnimatableProperty<P>*           property,
122                             MemberFunction                         member,
123                             typename ParameterType<P>::PassingType value)
124   : PropertyOwnerMessageBase(),
125     mSceneObject(const_cast<PropertyOwner*>(sceneObject)),
126     mProperty(const_cast<AnimatableProperty<P>*>(property)),
127     mMemberFunction(member),
128     mParam(value)
129   {
130   }
131
132 private:
133   PropertyOwner*                        mSceneObject;
134   AnimatableProperty<P>*                mProperty;
135   MemberFunction                        mMemberFunction;
136   typename ParameterType<P>::HolderType mParam;
137 };
138
139 /**
140  * Templated message which bakes a property.
141  */
142 template<typename P>
143 class AnimatablePropertyComponentMessage : public PropertyOwnerMessageBase
144 {
145 public:
146   using MemberFunction = void (AnimatableProperty<P>::*)(BufferIndex, float);
147
148   /**
149    * Send a message.
150    * @note The scene object is expected to be const in the thread which sends this message.
151    * However it can be modified when Process() is called in a different thread.
152    * @param[in] eventThreadServices The service object used for sending messages to the scene graph
153    * @param[in] sceneObject The property owner scene object
154    * @param[in] property The property to bake.
155    * @param[in] member The member function of the object.
156    * @param[in] value The new value of the X,Y,Z or W component.
157    */
158   static void Send(EventThreadServices&         eventThreadServices,
159                    const PropertyOwner*         sceneObject,
160                    const AnimatableProperty<P>* property,
161                    MemberFunction               member,
162                    float                        value)
163   {
164     // Reserve some memory inside the message queue
165     uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(AnimatablePropertyComponentMessage));
166
167     // Construct message in the message queue memory; note that delete should not be called on the return value
168     new(slot) AnimatablePropertyComponentMessage(sceneObject, property, member, value);
169   }
170
171   /**
172    * Virtual destructor
173    */
174   ~AnimatablePropertyComponentMessage() override = default;
175
176   /**
177    * @copydoc MessageBase::Process
178    */
179   void Process(BufferIndex updateBufferIndex) override
180   {
181     (mProperty->*mMemberFunction)(updateBufferIndex, mParam);
182   }
183
184 private:
185   /**
186    * Create a message.
187    * @note The scene object is expected to be const in the thread which sends this message.
188    * However it can be modified when Process() is called in a different thread.
189    * @param[in] sceneObject The property owner scene object
190    * @param[in] property The property to bake.
191    * @param[in] member The member function of the object.
192    * @param[in] value The new value of the X,Y,Z or W component.
193   */
194   AnimatablePropertyComponentMessage(const PropertyOwner*         sceneObject,
195                                      const AnimatableProperty<P>* property,
196                                      MemberFunction               member,
197                                      float                        value)
198   : PropertyOwnerMessageBase(),
199     mSceneObject(const_cast<PropertyOwner*>(sceneObject)),
200     mProperty(const_cast<AnimatableProperty<P>*>(property)),
201     mMemberFunction(member),
202     mParam(value)
203   {
204   }
205
206 private:
207   PropertyOwner*         mSceneObject;
208   AnimatableProperty<P>* mProperty;
209   MemberFunction         mMemberFunction;
210   float                  mParam;
211 };
212
213 // Messages for PropertyOwner
214
215 inline void InstallCustomPropertyMessage(EventThreadServices& eventThreadServices, const PropertyOwner& owner, OwnerPointer<PropertyBase>& property)
216 {
217   using LocalType = MessageValue1<PropertyOwner, OwnerPointer<PropertyBase> >;
218
219   // Reserve some memory inside the message queue
220   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
221
222   // Construct message in the message queue memory; note that delete should not be called on the return value
223   new(slot) LocalType(&owner, &PropertyOwner::InstallCustomProperty, property);
224 }
225
226 inline void ApplyConstraintMessage(EventThreadServices& eventThreadServices, const PropertyOwner& owner, OwnerPointer<ConstraintBase>& constraint)
227 {
228   using LocalType = MessageValue1<PropertyOwner, OwnerPointer<ConstraintBase> >;
229
230   // Reserve some memory inside the message queue
231   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
232
233   // Construct message in the message queue memory; note that delete should not be called on the return value
234   new(slot) LocalType(&owner, &PropertyOwner::ApplyConstraint, constraint);
235 }
236
237 inline void RemoveConstraintMessage(EventThreadServices& eventThreadServices, const PropertyOwner& owner, const ConstraintBase& constConstraint)
238 {
239   // The update-thread can modify this object.
240   ConstraintBase& constraint = const_cast<ConstraintBase&>(constConstraint);
241
242   using LocalType = MessageValue1<PropertyOwner, ConstraintBase*>;
243
244   // Reserve some memory inside the message queue
245   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
246
247   // Construct message in the message queue memory; note that delete should not be called on the return value
248   new(slot) LocalType(&owner, &PropertyOwner::RemoveConstraint, &constraint);
249 }
250
251 inline void ApplyPostConstraintMessage(EventThreadServices& eventThreadServices, const PropertyOwner& owner, OwnerPointer<ConstraintBase>& constraint)
252 {
253   using LocalType = MessageValue1<PropertyOwner, OwnerPointer<ConstraintBase> >;
254
255   // Reserve some memory inside the message queue
256   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
257
258   // Construct message in the message queue memory; note that delete should not be called on the return value
259   new(slot) LocalType(&owner, &PropertyOwner::ApplyPostConstraint, constraint);
260 }
261
262 inline void RemovePostConstraintMessage(EventThreadServices& eventThreadServices, const PropertyOwner& owner, const ConstraintBase& constConstraint)
263 {
264   // The update-thread can modify this object.
265   ConstraintBase& constraint = const_cast<ConstraintBase&>(constConstraint);
266
267   using LocalType = MessageValue1<PropertyOwner, ConstraintBase*>;
268
269   // Reserve some memory inside the message queue
270   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
271
272   // Construct message in the message queue memory; note that delete should not be called on the return value
273   new(slot) LocalType(&owner, &PropertyOwner::RemovePostConstraint, &constraint);
274 }
275
276 inline void AddUniformMapMessage(EventThreadServices& eventThreadServices, const PropertyOwner& owner, UniformPropertyMapping map)
277 {
278   using LocalType = MessageValue1<PropertyOwner, UniformPropertyMapping>;
279
280   // Reserve some memory inside the message queue
281   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
282
283   new(slot) LocalType(&owner, &PropertyOwner::AddUniformMapping, map);
284 }
285
286 inline void RemoveUniformMapMessage(EventThreadServices& eventThreadServices, const PropertyOwner& owner, ConstString uniformName)
287 {
288   using LocalType = MessageValue1<PropertyOwner, ConstString>;
289
290   // Reserve some memory inside the message queue
291   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
292
293   new(slot) LocalType(&owner, &PropertyOwner::RemoveUniformMapping, uniformName);
294 }
295
296 inline void ReservePropertiesMessage(EventThreadServices& eventThreadServices, const PropertyOwner& owner, int propertyCount)
297 {
298   using LocalType = MessageValue1<PropertyOwner, int>;
299
300   // Reserve some memory inside the message queue
301   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
302
303   new(slot) LocalType(&owner, &PropertyOwner::ReserveProperties, propertyCount);
304 }
305
306 } // namespace SceneGraph
307
308 } // namespace Internal
309
310 } // namespace Dali
311
312 #endif // DALI_INTERNAL_SCENE_GRAPH_PROPERTY_OWNER_MESSAGES_H