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