Fix memory leak of visual renderer
[platform/core/uifw/dali-core.git] / dali / internal / event / rendering / visual-renderer-impl.h
1 #ifndef DALI_INTERNAL_VISUAL_RENDERER_H
2 #define DALI_INTERNAL_VISUAL_RENDERER_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 // INTERNAL INCLUDES
22 #include <dali/internal/event/rendering/renderer-impl.h> // Dali::Internal::Renderer
23 #include <dali/internal/update/common/animatable-property.h>
24 #include <dali/public-api/rendering/visual-renderer.h> // Dali::VisualRenderer
25
26 namespace Dali
27 {
28 namespace Internal
29 {
30 namespace SceneGraph
31 {
32 class Renderer;
33 }
34
35 class VisualRenderer;
36 using VisualRendererPtr = IntrusivePtr<VisualRenderer>;
37
38 /**
39  * VisualRenderer is a Renderer that has additional default properties for toolkit
40  */
41 class VisualRenderer : public Renderer
42 {
43 public:
44   /**
45    * Create a new VisualRenderer.
46    * @return A smart-pointer to the newly allocated VisualRenderer.
47    */
48   static VisualRendererPtr New();
49
50   /**
51    * @brief Get the scene graph object
52    *
53    * @return the scene object
54    */
55   const SceneGraph::Renderer& GetVisualRendererSceneObject() const;
56
57 public: // Default property extensions from Object
58   /**
59    * @copydoc Dali::Internal::Object::SetDefaultProperty()
60    */
61   void SetDefaultProperty(Property::Index index, const Property::Value& propertyValue) override;
62
63   /**
64    * @copydoc Dali::Internal::Object::GetDefaultProperty()
65    */
66   Property::Value GetDefaultProperty(Property::Index index) const override;
67
68   /**
69    * @copydoc Dali::Internal::Object::GetDefaultPropertyCurrentValue()
70    */
71   Property::Value GetDefaultPropertyCurrentValue(Property::Index index) const override;
72
73   /**
74     * @copydoc Dali::Internal::Object::OnNotifyDefaultPropertyAnimation()
75     */
76   void OnNotifyDefaultPropertyAnimation(Animation& animation, Property::Index index, const Property::Value& value, Animation::Type animationType) override;
77
78   /**
79    * @copydoc Dali::Internal::Object::GetSceneObjectAnimatableProperty()
80    */
81   const SceneGraph::PropertyBase* GetSceneObjectAnimatableProperty(Property::Index index) const override;
82
83   /**
84    * @copydoc Dali::Internal::Object::GetSceneObjectInputProperty()
85    */
86   const PropertyInputImpl* GetSceneObjectInputProperty(Property::Index index) const override;
87
88 private: // implementation
89   /**
90    * @brief Constructor.
91    *
92    * @param sceneObject the scene graph renderer
93    */
94   VisualRenderer(const SceneGraph::Renderer* sceneObject);
95
96   /**
97    * @brief Retrieves the current value of a default property from the scene-graph.
98    * @param[in]  index  The index of the property
99    * @param[out] value  Is set with the current scene-graph value of the property
100    * @return True if value set, false otherwise.
101    */
102   bool GetCurrentPropertyValue(Property::Index index, Property::Value& value) const;
103
104 protected:
105   /**
106    * A reference counted object may only be deleted by calling Unreference()
107    */
108   ~VisualRenderer() override;
109
110 private:
111   VisualRenderer(const VisualRenderer&) = delete;
112   VisualRenderer& operator=(const VisualRenderer&) = delete;
113
114   /**
115    * @brief Ensure that properties are mapped to uniforms
116    */
117   void AddUniformMappings();
118
119 public:
120   struct VisualPropertyCache
121   {
122     Vector2 mTransformOffset{Vector2::ZERO};
123     Vector2 mTransformSize{Vector2::ZERO};
124     Vector2 mTransformOrigin{Vector2::ZERO};
125     Vector2 mTransformAnchorPoint{Vector2::ZERO};
126     Vector4 mTransformOffsetSizeMode{Vector2::ZERO};
127     Vector2 mExtraSize{Vector2::ZERO};
128     Vector3 mMixColor{Vector3::ONE};
129     float   mPreMultipliedAlpha{0.0f};
130   };
131
132   struct AnimatableVisualProperties
133   {
134     AnimatableVisualProperties()
135     : mTransformOffset(Vector2::ZERO),
136       mTransformSize(Vector2::ZERO),
137       mTransformOrigin(Vector2::ZERO),
138       mTransformAnchorPoint(Vector2::ZERO),
139       mTransformOffsetSizeMode(Vector4::ZERO),
140       mExtraSize(Vector2::ZERO),
141       mMixColor(Vector3::ONE),
142       mPreMultipliedAlpha(0.0f)
143     {
144     }
145
146     SceneGraph::AnimatableProperty<Vector2> mTransformOffset;
147     SceneGraph::AnimatableProperty<Vector2> mTransformSize;
148     SceneGraph::AnimatableProperty<Vector2> mTransformOrigin;
149     SceneGraph::AnimatableProperty<Vector2> mTransformAnchorPoint;
150     SceneGraph::AnimatableProperty<Vector4> mTransformOffsetSizeMode;
151     SceneGraph::AnimatableProperty<Vector2> mExtraSize;
152     SceneGraph::AnimatableProperty<Vector3> mMixColor;
153     SceneGraph::AnimatableProperty<float>   mPreMultipliedAlpha;
154
155     void* mExtendedProperties{nullptr}; // Enable derived class to extend properties further
156   };
157
158 private:
159   VisualPropertyCache mPropertyCache;
160 };
161
162 } // namespace Internal
163
164 // Helpers for public-api forwarding methods
165 inline Internal::VisualRenderer& GetImplementation(Dali::VisualRenderer& handle)
166 {
167   DALI_ASSERT_ALWAYS(handle && "VisualRenderer handle is empty");
168
169   BaseObject& object = handle.GetBaseObject();
170
171   return static_cast<Internal::VisualRenderer&>(object);
172 }
173
174 inline const Internal::VisualRenderer& GetImplementation(const Dali::VisualRenderer& handle)
175 {
176   DALI_ASSERT_ALWAYS(handle && "VisualRenderer handle is empty");
177
178   const BaseObject& object = handle.GetBaseObject();
179
180   return static_cast<const Internal::VisualRenderer&>(object);
181 }
182
183 } // namespace Dali
184
185 #endif // DALI_INTERNAL_RENDERER_H