Merge "Set FullRelayoutNeeded after GetNaturalSize/GetHeightForWidth to avoid the...
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / visual-base-impl.h
1 #ifndef DALI_TOOLKIT_INTERNAL_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_VISUAL_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 // EXTERNAL INCLUDES
22 #include <dali/public-api/animation/animation.h>
23 #include <dali/public-api/common/intrusive-ptr.h>
24 #include <dali/public-api/images/image-operations.h>
25 #include <dali/public-api/object/base-object.h>
26 #include <dali/public-api/rendering/renderer.h>
27 #include <dali/public-api/rendering/shader.h>
28
29 // INTERNAL INCLUDES
30 #include <dali-toolkit/devel-api/direction-enums.h>
31 #include <dali-toolkit/devel-api/visual-factory/visual-base.h>
32 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
33 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
34 #include <dali-toolkit/internal/visuals/transition-data-impl.h>
35 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
36 #include <dali-toolkit/public-api/visuals/visual-properties.h>
37
38 namespace Dali
39 {
40 namespace Toolkit
41 {
42 namespace Internal
43 {
44 namespace Visual
45 {
46 class EventObserver;
47
48 using FittingMode = DevelVisual::FittingMode;
49
50 /**
51  * Base class for all Control rendering logic. A control may have multiple visuals.
52  *
53  * Note: The visual responds to the the Actor::COLOR by blending it with the 'Multiply' operator.
54  *
55  * The following properties are optional
56  *
57  * | %Property Name          | Type             |
58  * |-------------------------|------------------|
59  * | customShader            | MAP              |
60  *
61  * where custom-shader is a map with the following properties:
62  * | %Property Name          | Type             |
63  * |-------------------------|------------------|
64  * | vertexShader            | STRING           |
65  * | fragmentShader          | STRING           |
66  * | subdivideGridX          | INT              |
67  * | subdivideGridY          | INT              |
68  * | shaderHints             | INT              |
69  */
70 class Base : public BaseObject
71 {
72 public:
73   /**
74    * Setting the properties of the visual, this API should only called by the VisualFactory
75    * @param[in] propertyMap The properties for the requested Visual object.
76    */
77   void SetProperties(const Property::Map& propertyMap);
78
79   /**
80    * @copydoc Toolkit::Visual::Base::SetName
81    */
82   void SetName(const std::string& name);
83
84   /**
85    * @copydoc Toolkit::Visual::Base::GetName
86    */
87   const std::string& GetName() const;
88
89   /**
90    * @copydoc Toolkit::Visual::Base::SetSize
91    */
92   void SetTransformAndSize(const Property::Map& transform, Size controlSize);
93
94   /**
95    * @brief Performs an action on the visual with the given action id and attributes.
96    *
97    * @param[in] actionId The id of the action to perform this API only takes an Index
98    * @param[in] attributes The list of attributes for the action. ( optional for this data structure to have content )
99    */
100   void DoAction(const Dali::Property::Index actionId, const Dali::Property::Value attributes);
101
102   /**
103    * @copydoc Toolkit::Visual::Base::GetHeightForWidth
104    */
105   virtual float GetHeightForWidth(float width);
106
107   /**
108    * @copydoc Toolkit::Visual::Base::GetWidthForHeight
109    */
110   virtual float GetWidthForHeight(float height);
111
112   /**
113    * @copydoc Toolkit::Visual::Base::GetNaturalSize
114    */
115   virtual void GetNaturalSize(Vector2& naturalSize);
116
117   /**
118    * @copydoc Toolkit::Visual::Base::SetDepthIndex
119    */
120   void SetDepthIndex(int index);
121
122   /**
123    * @copydoc Toolkit::Visual::Base::GetDepthIndex
124    */
125   int GetDepthIndex() const;
126
127   /**
128    * @copydoc Toolkit::Visual::Base::SetOnScene
129    * @pre Impl->mGeometry must be created before this method is called
130    */
131   void SetOnScene(Actor& actor);
132
133   /**
134    * @copydoc Toolkit::Visual::Base::SetOffScene
135    */
136   void SetOffScene(Actor& actor);
137
138   /**
139    * @copydoc Toolkit::Visual::Base::CreatePropertyMap
140    */
141   void CreatePropertyMap(Property::Map& map) const;
142
143   /**
144    * @brief Create a property map containing per-instance visual properties.
145    *
146    * This will enable creation of new visuals on control state change with
147    * any alternative style properties and the relevant instance properties
148    * (e.g. for image visual, the desired size, and for text visual, the actual text).
149    * @param[in] map The property map into which to write
150    */
151   void CreateInstancePropertyMap(Property::Map& map) const;
152
153   /**
154    * @brief Set whether the Pre-multiplied Alpha Blending is required
155    *
156    * @param[in] preMultiplied whether alpha is pre-multiplied.
157    */
158   void EnablePreMultipliedAlpha(bool preMultiplied);
159
160   /**
161    * @brief Query whether alpha is pre-multiplied.
162    *
163    * @return True is alpha is pre-multiplied, false otherwise.
164    */
165   bool IsPreMultipliedAlphaEnabled() const;
166
167   /**
168    * @brief Sets properties of custom shader
169    * @param[in] propertyMap Property map containing the custom shader data
170    */
171   void SetCustomShader(const Property::Map& propertyMap);
172
173   /**
174    * @copydoc Toolkit::Visual::Base::SetProperty
175    */
176   void SetProperty(Dali::Property::Index index, const Dali::Property::Value& propertyValue);
177
178   /**
179    * @copydoc Toolkit::Visual::Base::GetProperty
180    */
181   Dali::Property::Value GetProperty(Dali::Property::Index index);
182
183   /**
184    * Gets currently staged renderer, or an empty handle if not staged
185    */
186   Renderer GetRenderer();
187
188   /**
189    * Sets the mix color ( including opacity )  of the visual.
190    * @param[in] mixColor The new mix color
191    */
192   void SetMixColor(const Vector4& color);
193
194   /**
195    * Sets the mix color of the visual.
196    * @param[in] mixColor The new mix color
197    */
198   void SetMixColor(const Vector3& color);
199
200   /**
201    * Animate the property if it exists in the visual or renderer.
202    *
203    * If it's a visual property such as mix color or a transform property,
204    * saves the target value to the local data.
205    *
206    * If the visual isn't staged (i.e. it doesn't have a renderer),
207    * then this will not add an animation.
208    *
209    * If the animator is valid and the transition handle is empty - it will
210    * be created.
211    *
212    * @param[in] transition The animation to create or attach to
213    * @param[in] animator The animation parameters of the property.
214    */
215   void AnimateProperty(Dali::Animation&                    transition,
216                        Internal::TransitionData::Animator& animator);
217
218   /**
219    * @brief Add an observer to watch for when the Visuals have events to notify
220    * Currently only supports a single observer
221    */
222   void AddEventObserver(Visual::EventObserver& observer);
223
224   /**
225    * @brief Remove an observer
226    */
227   void RemoveEventObserver(Visual::EventObserver& observer);
228
229   /**
230    * @brief Called when the visuals resources are loaded / ready
231    */
232   void ResourceReady(Toolkit::Visual::ResourceStatus resourceStatus);
233
234   /**
235    * @brief Called when the visuals resources are loaded / ready
236    * @return true if ready, false otherwise
237    */
238   virtual bool IsResourceReady() const;
239
240   /**
241    * @brief Get the loading state of the visual resource
242    * @return Return the loading status (PREPARING, READY and FAILED) of visual resource
243    */
244   Toolkit::Visual::ResourceStatus GetResourceStatus() const;
245
246   /**
247    * @brief Get the fitting mode for the visual
248    */
249   FittingMode GetFittingMode() const;
250
251   /**
252    * @brief Get the actual Visual Object.
253    * @return The actual visual object
254    * @note Should be overridden by deriving controls if they are acting as a proxy to other visual objects.
255    */
256   virtual Base& GetVisualObject();
257
258   /**
259    * @brief Query whether resources requires to be loaded synchronously.
260    * @return Returns true if synchronous resource loading is required, false otherwise.
261    */
262   bool IsSynchronousLoadingRequired() const;
263
264   /**
265    * @brief Get the type of this visual.
266    *
267    * @return The the type of this visual.
268    */
269   Toolkit::Visual::Type GetType() const;
270
271   /**
272    * @brief Retrieve the property object associated with the property key.
273    *
274    * @param[in] key The Property key of the visual.
275    * @return The Property object
276    */
277   Dali::Property GetPropertyObject(Dali::Property::Key key);
278
279 protected:
280   /**
281    * @brief Constructor.
282    *
283    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
284    * @param[in] fittingMode The value that determines how the visual should be fit to the view
285    * @param[in] type The type of the this visual
286    */
287   Base(VisualFactoryCache& factoryCache, FittingMode fittingMode, Toolkit::Visual::Type type);
288
289   /**
290    * @brief A reference counted object may only be deleted by calling Unreference().
291    */
292   ~Base() override;
293
294   /**
295    * @brief Second-phase constructor.
296    */
297   void Initialize();
298
299 protected:
300   /**
301    * @brief Called by Initialize() allowing sub classes to initialize them.
302    */
303   virtual void OnInitialize() = 0;
304
305   /**
306    * @brief Called by CreatePropertyMap() allowing sub classes to respond to the CreatePropertyMap event
307    *
308    * @param[out] map The visual property map.
309    */
310   virtual void DoCreatePropertyMap(Property::Map& map) const = 0;
311
312   /**
313    * @brief Called by CreateInstancePropertyMap() allowing derived
314    * classes to store instanced data (separate to styled data) that
315    * needs copying between visuals on state change.
316    *
317    * @param[out] map The visual property map
318    */
319   virtual void DoCreateInstancePropertyMap(Property::Map& map) const = 0;
320
321   /**
322    * @brief Called by SetProperties() allowing sub classes to set their properties
323    *
324    * @param[in] propertyMap The properties for the requested Visual object.
325    */
326   virtual void DoSetProperties(const Property::Map& propertyMap) = 0;
327
328   /**
329    * @brief Called when transform or control size changes
330    * ( Of use to SVG and Text visuals )
331    */
332   virtual void OnSetTransform() = 0;
333
334   /**
335    * @brief Called by SetOnScene() allowing sub classes to respond to the SetOnScene event
336    *
337    * @note The derived class is required to create the renderer, and add it to the actor when all the resources are in place.
338    *
339    * @param[in] actor The actor applying this visual.
340    */
341   virtual void DoSetOnScene(Actor& actor) = 0;
342
343   /**
344    * @brief Called by SetOffScene() allowing sub classes to respond to the SetOffScene event
345    *
346    * @param[in] actor The actor applying this visual.
347    */
348   virtual void DoSetOffScene(Actor& actor);
349
350   /**
351    * @brief Called by DoAction() allowing sub classes to do the given action.
352    *
353    * @param[in] actionId The action to perform
354    * @param[in] attributes The list of attributes for the action. ( optional for this data structure to have content )
355    */
356   virtual void OnDoAction(const Property::Index actionId, const Property::Value& attributes);
357
358   /**
359    * @brief Update the shader when some properties are changed.
360    */
361   virtual void UpdateShader()
362   {
363   }
364
365   /**
366    * @brief Generate a shader by the current properties from each sub classes's own shader-generate logic.
367    * @param[in] factoryCache The visual factory cache to store the generated shader.
368    * @return If the function defined, Generated shader by the current properties. Else, empty shader.
369    */
370   virtual Dali::Shader GenerateShader() const
371   {
372     return Dali::Shader();
373   }
374
375   /**
376    * @brief Called by GetPropertyObject() allowing sub classes to respond to the GetPropertyObject event
377    * @note The derived class is required to register the given property.
378    * @param[in] key The key of the visual's property.
379    * @return The Property object
380    */
381   virtual Dali::Property OnGetPropertyObject(Dali::Property::Key key)
382   {
383     Handle handle;
384     return Dali::Property(handle, Property::INVALID_INDEX);
385   }
386
387 protected:
388   /**
389    * @brief Gets the on scene state for this Visual
390    *
391    * @return Returns true if this Visual is on the scene, false if it is off the scene
392    */
393   bool IsOnScene() const;
394
395   /**
396    * @brief Query whether the corners of the visual requires to be rounded.
397    *
398    * @return Returns true if the rounded corner is required, false otherwise.
399    */
400   bool IsRoundedCornerRequired() const;
401
402   /**
403    * @brief Query whether the borderline of the visual requires to be rendered.
404    *
405    * @return Returns true if the outline is required, false otherwise.
406    */
407   bool IsBorderlineRequired() const;
408
409 private:
410   /**
411    * Register the mix color uniform on the Renderer and store the property index.
412    * Note, this is not used by Color or Primitive Visuals, which will use their
413    * own property index.
414    */
415   void RegisterMixColor();
416
417   /**
418    * Find the matching property on the renderer or shader. If it's a shader
419    * property, register it on the renderer in order to animate it for this
420    * visual independently.
421    * @param[in] key The key to match.
422    * @return the matching index, or INVALID_INDEX if it's not found
423    */
424   Property::Index GetPropertyIndex(Property::Key key);
425
426   /**
427    * Set up the transition. If no animation is required, then
428    * transition will be untouched.
429    *
430    * @param[in] transition The transition to use or set up.
431    * @param[in] animator The animation data to use
432    * @param[in] index The property index on the renderer to animate
433    * @param[in] initialValue The optional initial value
434    * @param[in] targetValue The target value to use
435    */
436   void SetupTransition(Dali::Animation&                    transition,
437                        Internal::TransitionData::Animator& animator,
438                        Property::Index                     index,
439                        Property::Value&                    initialValue,
440                        Property::Value&                    targetValue);
441
442   /**
443    * Animate the opacity property - Special handling to
444    * ensure that the blend mode is set to ON whilst animating,
445    * and set back to AUTO if it's opaque at the end of the
446    * animation.
447    *
448    * @param[in] transition The transition to use or set up.
449    * @param[in] animator The animation data to use
450    */
451   void AnimateOpacityProperty(Dali::Animation&                    transition,
452                               Internal::TransitionData::Animator& animator);
453
454   /**
455    * Animate the renderer property - no special handling
456    *
457    * @param[in] transition The transition to use or set up.
458    * @param[in] animator The animation data to use
459    */
460   void AnimateRendererProperty(Dali::Animation&                    transition,
461                                Internal::TransitionData::Animator& animator);
462
463   /**
464    * Animate the mix color property.
465    *
466    * If the animator is a vec3, then it only animates the color
467    * channels without animating the opacity.  If it's a vec4, then it
468    * runs 2 animators, one for the the vec3 mixColor, and one for the
469    * opacity. (They are separate uniforms in the shader )
470    *
471    * @param[in] transition The transition to use or set up.
472    * @param[in] animator The animation data to use
473    */
474   void AnimateMixColorProperty(Dali::Animation&                    transition,
475                                Internal::TransitionData::Animator& animator);
476
477   // Undefined
478   Base(const Visual::Base& visual);
479
480   // Undefined
481   Base& operator=(const Visual::Base& visual);
482
483 protected:
484   struct Impl;
485   Impl*               mImpl;
486   VisualFactoryCache& mFactoryCache;
487 };
488
489 typedef IntrusivePtr<Base> BasePtr;
490
491 } // namespace Visual
492
493 } // namespace Internal
494
495 inline const Internal::Visual::Base& GetImplementation(const Toolkit::Visual::Base& visualBase)
496 {
497   DALI_ASSERT_ALWAYS(visualBase && "visual base handle is empty");
498
499   const BaseObject& handle = visualBase.GetBaseObject();
500
501   return static_cast<const Internal::Visual::Base&>(handle);
502 }
503
504 inline Internal::Visual::Base& GetImplementation(Toolkit::Visual::Base& visualBase)
505 {
506   DALI_ASSERT_ALWAYS(visualBase && "visual base handle is empty");
507
508   BaseObject& handle = visualBase.GetBaseObject();
509
510   return static_cast<Internal::Visual::Base&>(handle);
511 }
512
513 } // namespace Toolkit
514
515 } // namespace Dali
516
517 #endif // DALI_TOOLKIT_INTERNAL_VISUAL_H