[Tizen] Implement partial update
[platform/core/uifw/dali-core.git] / dali / internal / event / common / object-impl.h
1 #ifndef DALI_INTERNAL_OBJECT_H
2 #define DALI_INTERNAL_OBJECT_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 <cstdint> // uint32_t
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/animation/constraint.h>
26 #include <dali/public-api/common/dali-vector.h>
27 #include <dali/public-api/common/vector-wrapper.h>
28 #include <dali/public-api/object/base-object.h>
29 #include <dali/public-api/object/handle.h>
30 #include <dali/public-api/object/property.h>
31 #include <dali/public-api/object/property-index-ranges.h>
32 #include <dali/public-api/object/property-input.h>
33 #include <dali/public-api/object/property-map.h>
34 #include <dali/public-api/object/property-notification.h>
35 #include <dali/devel-api/common/owner-container.h>
36 #include <dali/devel-api/object/handle-devel.h>
37 #include <dali/internal/event/animation/animation-impl.h>
38 #include <dali/internal/event/common/event-thread-services.h>
39 #include <dali/internal/event/common/property-input-impl.h>
40 #include <dali/internal/event/common/property-metadata.h>
41 #include <dali/internal/update/common/property-base.h>
42
43 namespace Dali
44 {
45
46 class PropertyNotification;
47
48 namespace Internal
49 {
50 class ConstraintBase;
51 class EventThreadServices;
52 class PropertyCondition;
53 class PropertyInputImpl;
54 class Stage;
55 class TypeInfo;
56
57 namespace SceneGraph
58 {
59 class PropertyBase;
60 class PropertyOwner;
61 }
62
63 using ConstraintContainer = std::vector< Dali::Constraint >;
64 using ConstraintIter = ConstraintContainer::iterator;
65 using ConstraintConstIter = ConstraintContainer::const_iterator;
66
67 /**
68  * A base class for objects which optionally provide properties.
69  * The concrete derived class is responsible for implementing the property system methods.
70  * Classes may derive from Dali::BaseObject, until any properties are required.
71  *
72  * An object for a property-owning object in the scene-graph.
73  * This provides an interface for observing the addition/removal of scene-objects.
74  *
75  * The derived class should either:
76  *   a) Create their own scene-graph object and pass it to Object constructor.
77  *   b) pass nullptr to Object constructor, in this case Object will create default scene object for property handling
78  */
79 class Object : public Dali::BaseObject
80 {
81 public:
82
83   using Capability = Dali::Handle::Capability;
84
85   class Observer
86   {
87   public:
88
89     /**
90      * Called immediately after the object has created & passed ownership of a scene-graph object.
91      * @param[in] object The object object.
92      */
93     virtual void SceneObjectAdded(Object& object) = 0;
94
95     /**
96      * Called shortly before the object sends a message to remove its scene object.
97      * @param[in] object The object object.
98      */
99     virtual void SceneObjectRemoved(Object& object) = 0;
100
101     /**
102      * Called shortly before the object itself is destroyed; no further callbacks will be received.
103      * @param[in] object The object object.
104      */
105     virtual void ObjectDestroyed(Object& object) = 0;
106
107   protected:
108
109     /**
110      * Virtual destructor
111      */
112     virtual ~Observer(){}
113   };
114
115   /**
116    * Creates a new object
117    *
118    * @return an smart pointer to the object
119    */
120   static IntrusivePtr<Object> New();
121
122   /**
123    * Add an observer to the object.
124    * @param[in] observer The observer to add.
125    */
126   void AddObserver( Observer& observer );
127
128   /**
129    * Remove an observer from the object
130    * @pre The observer has already been added.
131    * @param[in] observer The observer to remove.
132    */
133   void RemoveObserver( Observer& observer );
134
135   /**
136    * @copydoc Dali::Handle::Supports()
137    */
138   bool Supports( Capability capability ) const;
139
140   /**
141    * @copydoc Dali::Handle::GetPropertyCount()
142    */
143   uint32_t GetPropertyCount() const;
144
145   /**
146    * @copydoc Dali::Handle::GetPropertyName()
147    */
148   std::string GetPropertyName( Property::Index index ) const;
149
150   /**
151    * @copydoc Dali::Handle::GetPropertyIndex()
152    */
153   Property::Index GetPropertyIndex( const std::string& name ) const;
154
155   /**
156    * @copydoc Dali::Handle::GetPropertyIndex()
157    */
158   Property::Index GetPropertyIndex( Property::Index key ) const;
159
160   /**
161    * @copydoc Dali::Handle::GetPropertyIndex()
162    */
163   Property::Index GetPropertyIndex( Property::Key key ) const;
164
165   /**
166    * @copydoc Dali::Handle::IsPropertyWritable()
167    */
168   bool IsPropertyWritable( Property::Index index ) const;
169
170   /**
171    * @copydoc Dali::Handle::IsPropertyAnimatable()
172    */
173   bool IsPropertyAnimatable( Property::Index index ) const;
174
175   /**
176    * @copydoc Dali::Handle::IsPropertyAConstraintInput()
177    */
178   bool IsPropertyAConstraintInput( Property::Index index ) const;
179
180   /**
181    * @copydoc Dali::Handle::GetPropertyType()
182    */
183   Property::Type GetPropertyType( Property::Index index ) const;
184
185   /**
186    * @copydoc Dali::Handle::SetProperty()
187    */
188   void SetProperty( Property::Index index, const Property::Value& propertyValue );
189
190   /**
191    * @copydoc Dali::Handle::GetProperty()
192    */
193   Property::Value GetProperty( Property::Index index ) const;
194
195   /**
196    * @brief Retrieves the latest value of the property on the scene-graph.
197    * @param[in]  index  The index of the property required.
198    * @return The latest value of the property on the scene-graph.
199    */
200   Property::Value GetCurrentProperty( Property::Index index ) const;
201
202   /**
203    * @copydoc Dali::Handle::GetPropertyIndices()
204    */
205   void GetPropertyIndices( Property::IndexContainer& indices ) const;
206
207   /**
208    * @copydoc Dali::Handle::RegisterProperty()
209    */
210   Property::Index RegisterProperty( const std::string& name, const Property::Value& propertyValue );
211
212   /**
213    * @copydoc Dali::Handle::RegisterProperty()
214    */
215   Property::Index RegisterProperty( const std::string& name, Property::Index key, const Property::Value& propertyValue );
216
217   /**
218    * @copydoc Dali::DevelHandle::SetProperties()
219    */
220   void SetProperties( const Property::Map& properties );
221
222   /**
223    * @copydoc Dali::DevelHandle::GetProperties()
224    */
225   void GetProperties( Property::Map& properties );
226
227   /**
228    * @copydoc Dali::Handle::RegisterProperty(std::string name, Property::Value propertyValue, Property::AccessMode accessMode)
229    */
230   Property::Index RegisterProperty( const std::string& name, const Property::Value& propertyValue, Property::AccessMode accessMode );
231
232   /**
233    * @brief Implementing method for this override
234    */
235   Property::Index RegisterProperty( const std::string& name, Property::Index key, const Property::Value& propertyValue, Property::AccessMode accessMode );
236
237   /**
238    * @brief returns true if the custom property exists on this object.
239    *
240    * @note The property may be defined for a type within the type registry, but it isn't explicity
241    * defined in every consequent instantiation. It can be automatically added, e.g. parenting an actor
242    * automatically registers it's parent container's child properties.
243    *
244    * @param[in] handle The handle of the object to test
245    * @param[in] index The property index to look for.
246    * @return true if the property exists on the object, false otherwise.
247    */
248   bool DoesCustomPropertyExist( Property::Index index );
249
250   /**
251    * @copydoc Dali::Handle::AddPropertyNotification()
252    */
253   Dali::PropertyNotification AddPropertyNotification( Property::Index index,
254                                                       int32_t componentIndex,
255                                                       const Dali::PropertyCondition& condition );
256
257   /**
258    * @copydoc Dali::Handle::RemovePropertyNotification()
259    */
260   void RemovePropertyNotification( Dali::PropertyNotification propertyNotification );
261
262   /**
263    * @copydoc Dali::Handle::RemovePropertyNotifications()
264    */
265   void RemovePropertyNotifications();
266
267   /**
268    * Notifies that a property is being animated.
269    * @param[in] animation The animation animating the property.
270    * @param[in] index The index of the property.
271    * @param[in] value The value of the property after the animation.
272    * @param[in] animationType Whether the property value given is the target or a relative value.
273    */
274   void NotifyPropertyAnimation( Animation& animation, Property::Index index, const Property::Value& value, Animation::Type animationType );
275
276   /******************************** Uniform Mappings ********************************/
277
278   /**
279    * Adds uniform mapping for given property
280    * @param propertyIndex index of the property
281    * @param uniformName name of the uniform (same as property name)
282    */
283   void AddUniformMapping( Property::Index propertyIndex, const std::string& uniformName ) const;
284
285   /**
286    * Removes uniform mapping for given property
287    * @param uniformName name of the uniform (same as property name)
288    */
289   void RemoveUniformMapping( const std::string& uniformName ) const;
290
291   /******************************** Constraints ********************************/
292
293   /**
294    * Apply a constraint to an Object.
295    * @param[in] constraint The constraint to apply.
296    */
297   void ApplyConstraint( ConstraintBase& constraint );
298
299   /**
300    * Remove one constraint from an Object.
301    * @param[in] constraint The constraint to remove.
302    */
303   void RemoveConstraint( ConstraintBase& constraint );
304
305   /**
306    * Remove all constraints from a Object.
307    */
308   void RemoveConstraints();
309
310   /**
311    * @copydoc Dali::Handle::RemoveConstraints( uint32_t )
312    */
313   void RemoveConstraints( uint32_t tag );
314
315   /**
316    * Called by TypeInfo to set the type-info that this object-impl is created by.
317    * @param[in] typeInfo The TypeInfo that creates this object-impl.
318    */
319   void SetTypeInfo( const TypeInfo* typeInfo );
320
321   /**
322    * @return the index from which custom properties start
323    */
324   uint32_t CustomPropertyStartIndex()
325   {
326     return PROPERTY_CUSTOM_START_INDEX;
327   }
328
329   /**
330    * Retrieve the scene-graph object added by this object.
331    * @return reference to the scene-graph object, it will always exist
332    */
333   const SceneGraph::PropertyOwner& GetSceneObject() const;
334
335   /********************  Can be overridden by deriving classes ********************/
336
337   /**
338    * Retrieve an animatable property owned by the scene-graph object.
339    * @pre -1 < index < GetPropertyCount().
340    * @param[in] index The index of the property.
341    * @return A dereferenceable pointer to a property, or NULL if a scene-object does not exist with this property.
342    */
343   virtual const SceneGraph::PropertyBase* GetSceneObjectAnimatableProperty( Property::Index index ) const;
344
345   /**
346    * Retrieve a constraint input-property owned by the scene-graph object.
347    * @pre -1 < index < GetPropertyCount().
348    * @param[in] index The index of the property.
349    * @return A dereferenceable pointer to an input property, or NULL if a scene-object does not exist with this property.
350    */
351   virtual const PropertyInputImpl* GetSceneObjectInputProperty( Property::Index index ) const;
352
353   /**
354    * Query whether the property is a component of a scene-graph property.
355    * @pre -1 < index < GetPropertyCount().
356    * @param[in] index The index of the property.
357    * @return The index or Property::INVALID_COMPONENT_INDEX.
358    */
359   virtual int32_t GetPropertyComponentIndex( Property::Index index ) const;
360
361   /**
362    * @copydoc Dali::Handle::PropertySetSignal()
363    */
364   DevelHandle::PropertySetSignalType& PropertySetSignal();
365
366 protected:
367
368   /**
369    * Constructor. Protected so use New to construct an instance of this class
370    *
371    * @param sceneObject the scene graph property owner
372    */
373   Object( const SceneGraph::PropertyOwner* sceneObject );
374
375   /**
376    * A reference counted object may only be deleted by calling Unreference()
377    */
378   virtual ~Object();
379
380   /**
381    * Called immediately by derived classes, after the scene-object has been created & passed to the scene-graph.
382    */
383   void OnSceneObjectAdd();
384
385   /**
386    * Called by derived classes, shortly before send a message to remove the scene-object.
387    */
388   void OnSceneObjectRemove();
389
390   /**
391    * For overriding by derived classes to return the parent of this object.
392    */
393   virtual Object* GetParentObject() const
394   {
395     // By default the Object does not have a parent
396     return nullptr;
397   };
398
399   /**
400    * For use in derived classes.
401    * This is called after a property is set.
402    * @param [in] index The index of the property.
403    * @param [in] propertyValue The value of the property.
404    */
405   virtual void OnPropertySet( Property::Index index, Property::Value propertyValue ) {}
406
407   /**
408    * Retrieves the TypeInfo for this object. Only retrieves it from the type-registry once and then stores a pointer
409    * to it locally there-after. The type info will not change during the life-time of the application.
410    * @return The type-info for this object (Can be NULL)
411    */
412   const TypeInfo* GetTypeInfo() const;
413
414   /**
415    * Helper to find custom property
416    * @param index
417    * @return pointer to the property
418    */
419   CustomPropertyMetadata* FindCustomProperty( Property::Index index ) const;
420
421   /**
422    * Helper to find animatable property
423    * @param index
424    * @return pointer to the property
425    */
426   AnimatablePropertyMetadata* FindAnimatableProperty( Property::Index index ) const;
427
428   /**
429    * Helper to register a scene-graph property
430    * @param [in] name The name of the property.
431    * @param [in] key The key of the property
432    * @param [in] index The index of the property
433    * @param [in] value The value of the property.
434    * @return The index of the registered property or Property::INVALID_INDEX if registration failed.
435    */
436   Property::Index RegisterSceneGraphProperty( const std::string& name, Property::Index key, Property::Index index, const Property::Value& propertyValue ) const;
437
438   /**
439    * Registers animatable scene property
440    * @param typeInfo to check the default value
441    * @param index of the property to register
442    * @param value initial value or nullptr
443    */
444   void RegisterAnimatableProperty( const TypeInfo& typeInfo, Property::Index index, const Property::Value* value ) const;
445
446   /**
447    * Check whether the animatable property is registered already, if not then register on.
448    * @param [in] index The index of the property
449    * @param [in] value optional value for the property
450    * @return pointer to the property metadata
451    */
452   AnimatablePropertyMetadata* GetSceneAnimatableProperty( Property::Index index, const Property::Value* value ) const;
453
454   /**
455    * Resolve the index and name of child properties if any.
456    */
457   void ResolveChildProperties();
458
459 private: // Default property extensions for derived classes
460
461   /**
462    * Set the value of a default property.
463    * @pre The property types match i.e. propertyValue.GetType() is equal to GetPropertyType(index).
464    * @param [in] index The index of the property.
465    * @param [in] propertyValue The new value of the property.
466    */
467   virtual void SetDefaultProperty( Property::Index index, const Property::Value& propertyValue );
468
469   /**
470    * Retrieve a default property value.
471    * @param [in] index The index of the property.
472    * @return The property value.
473    */
474   virtual Property::Value GetDefaultProperty( Property::Index index ) const;
475
476   /**
477    * Retrieve the latest scene-graph value of a default property.
478    * @param[in] index The index of the property.
479    * @return The latest scene-graph value of a default property.
480    */
481   virtual Property::Value GetDefaultPropertyCurrentValue( Property::Index index ) const;
482
483   /**
484    * Notifies that a default property is being animated so the deriving class should update the cached value.
485    * @param[in] animation The animation animating the property.
486    * @param[in] index The index of the property.
487    * @param[in] value The value of the property after the animation.
488    * @param[in] animationType Whether the property value given is the target or a relative value.
489    */
490   virtual void OnNotifyDefaultPropertyAnimation( Animation& animation, Property::Index index, const Property::Value& value, Animation::Type propertyChangeType )
491   { }
492
493 private:
494
495   // no default, copy or assignment
496   Object() = delete;
497   Object(const Object& rhs) = delete;
498   Object& operator=(const Object& rhs) = delete;
499
500   /**
501    * Enable property notifications in scene graph
502    */
503   void EnablePropertyNotifications();
504
505   /**
506    * Enable property notifications in scene graph
507    */
508   void DisablePropertyNotifications();
509
510   /**
511    * Get the latest value of the property on the scene-graph.
512    * @param[in] entry An entry from the property lookup container.
513    * @return The latest value of the property.
514    */
515   Property::Value GetCurrentPropertyValue( const PropertyMetadata& entry ) const;
516
517   /**
518    * Set the value of scene graph property.
519    * @param [in] index The index of the property.
520    * @param [in] entry An entry from the property lookup container.
521    * @param [in] value The new value of the property.
522    */
523   virtual void SetSceneGraphProperty( Property::Index index, const PropertyMetadata& entry, const Property::Value& value );
524
525 protected:
526   /**
527    * Get the event thread services object - used for sending messages to the scene graph
528    * Assert if called from the wrong thread.
529    * This is intentionally inline for performance reasons.
530    *
531    * @return The event thread services object
532    */
533   inline EventThreadServices& GetEventThreadServices()
534   {
535     DALI_ASSERT_DEBUG( EventThreadServices::IsCoreRunning() );
536     return mEventThreadServices;
537   }
538
539   /**
540    * Get the event thread services object - used for sending messages to the scene graph
541    * Assert if called from the wrong thread
542    * This is intentionally inline for performance reasons.
543    *
544    * @return The event thread services object
545    */
546   inline const EventThreadServices& GetEventThreadServices() const
547   {
548     DALI_ASSERT_DEBUG( EventThreadServices::IsCoreRunning() );
549     return mEventThreadServices;
550   }
551
552 private:
553
554   EventThreadServices& mEventThreadServices;
555
556 protected:
557
558   // mutable because it's lazy initialised and GetSceneObject has to be const so it can be called from const methods
559   // const to prevent accidentally calling setters directly from event thread
560   // protected to allow fast access from derived classes that have their own scene object (no function call overhead)
561   mutable const SceneGraph::PropertyOwner* mUpdateObject; ///< Reference to object to hold the scene graph properties
562
563 private:
564
565   Dali::Vector<Observer*> mObservers;
566   mutable OwnerContainer<PropertyMetadata*> mCustomProperties; ///< Used for accessing custom Node properties
567   mutable OwnerContainer<PropertyMetadata*> mAnimatableProperties; ///< Used for accessing animatable Node properties
568   mutable const TypeInfo* mTypeInfo; ///< The type-info for this object, mutable so it can be lazy initialized from const method if it is required
569
570   ConstraintContainer* mConstraints;               ///< Container of owned -constraints.
571
572   using PropertyNotificationContainer = std::vector< Dali::PropertyNotification >;
573   PropertyNotificationContainer* mPropertyNotifications; ///< Container of owned property notifications.
574   DevelHandle::PropertySetSignalType mPropertySetSignal;
575
576 };
577
578 } // namespace Internal
579
580 // Helpers for public-api forwarding methods
581
582 inline Internal::Object& GetImplementation(Dali::Handle& object)
583 {
584   DALI_ASSERT_ALWAYS( object && "Object handle is empty" );
585
586   BaseObject& handle = object.GetBaseObject();
587
588   return static_cast<Internal::Object&>(handle);
589 }
590
591 inline const Internal::Object& GetImplementation(const Dali::Handle& object)
592 {
593   DALI_ASSERT_ALWAYS( object && "Object handle is empty" );
594
595   const BaseObject& handle = object.GetBaseObject();
596
597   return static_cast<const Internal::Object&>(handle);
598 }
599
600 } // namespace Dali
601
602 #endif // DALI_INTERNAL_OBJECT_H