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