[dali_2.3.41] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / svg / svg-visual.h
1 #ifndef DALI_TOOLKIT_INTERNAL_SVG_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_SVG_VISUAL_H
3
4 /*
5  * Copyright (c) 2024 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/common/intrusive-ptr.h>
23 #include <dali/public-api/object/weak-handle.h>
24
25 // INTERNAL INCLUDES
26 #include <dali-toolkit/internal/visuals/svg/svg-loader-observer.h>
27 #include <dali-toolkit/internal/visuals/svg/svg-loader.h>
28 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
29 #include <dali-toolkit/internal/visuals/visual-url.h>
30
31 namespace Dali
32 {
33 namespace Toolkit
34 {
35 namespace Internal
36 {
37 class ImageVisualShaderFactory;
38 class SvgVisual;
39 typedef IntrusivePtr<SvgVisual> SvgVisualPtr;
40
41 /**
42  * The visual which renders a svg image
43  *
44  * The following property is essential
45  *
46  * | %Property Name           | Type             |
47  * |--------------------------|------------------|
48  * | url                      | STRING           |
49  *
50  */
51 class SvgVisual : public Visual::Base,
52                   public SvgLoaderObserver
53 {
54 public:
55   /**
56    * @brief Create the SVG Visual using the image URL.
57    *
58    * The visual will parse the SVG image once it is set.
59    * And rasterize it into BufferImage synchronously when the associated actor is put on stage, and destroy the BufferImage when it is off stage
60    *
61    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
62    * @param[in] shaderFactory The ImageVisualShaderFactory object
63    * @param[in] imageUrl The URL to svg resource to use
64    * @param[in] properties A Property::Map containing settings for this visual
65    * @return A smart-pointer to the newly allocated visual.
66    */
67   static SvgVisualPtr New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties);
68
69   /**
70    * @brief Create the SVG Visual using the image URL.
71    *
72    * The visual will parse the SVG image once it is set.
73    * And rasterize it into BufferImage synchronously when the associated actor is put on stage, and destroy the BufferImage when it is off stage
74    *
75    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
76    * @param[in] shaderFactory The ImageVisualShaderFactory object
77    * @param[in] imageUrl The URL to svg resource to use
78    * @param[in] size The width and height of the rasterized buffer. The visual size will be used if these are 0.
79    * @return A smart-pointer to the newly allocated visual.
80    */
81   static SvgVisualPtr New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, ImageDimensions size = ImageDimensions());
82
83 public: // from Visual
84   /**
85    * @copydoc Visual::Base::GetNaturalSize
86    */
87   void GetNaturalSize(Vector2& naturalSize) override;
88
89   /**
90    * @copydoc Visual::Base::CreatePropertyMap
91    */
92   void DoCreatePropertyMap(Property::Map& map) const override;
93
94   /**
95    * @copydoc Visual::Base::CreateInstancePropertyMap
96    */
97   void DoCreateInstancePropertyMap(Property::Map& map) const override;
98
99   /**
100    * @copydoc Visual::Base::EnablePreMultipliedAlpha
101    */
102   void EnablePreMultipliedAlpha(bool preMultiplied) override;
103
104 protected:
105   /**
106    * @brief Constructor.
107    *
108    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
109    * @param[in] shaderFactory The ImageVisualShaderFactory object
110    * @param[in] imageUrl The URL to svg resource to use
111    * @param[in] size The width and height of the rasterized buffer. The visual size will be used if these are 0.
112    */
113   SvgVisual(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, ImageDimensions size);
114
115   /**
116    * @brief A reference counted object may only be deleted by calling Unreference().
117    */
118   virtual ~SvgVisual();
119
120   /**
121    * @copydoc Visual::Base::OnInitialize
122    */
123   void OnInitialize() override;
124
125   /**
126    * @copydoc Visual::Base::DoSetProperties
127    */
128   void DoSetProperties(const Property::Map& propertyMap) override;
129
130   /**
131    * @copydoc Visual::Base::DoSetOnScene
132    */
133   void DoSetOnScene(Actor& actor) override;
134
135   /**
136    * @copydoc Visual::Base::DoSetOffScene
137    */
138   void DoSetOffScene(Actor& actor) override;
139
140   /**
141    * @copydoc Visual::Base::OnSetTransform
142    */
143   void OnSetTransform() override;
144
145   /**
146    * @copydoc Visual::Base::UpdateShader
147    */
148   void UpdateShader() override;
149
150   /**
151    * @copydoc Visual::Base::GenerateShader
152    */
153   Shader GenerateShader() const override;
154
155 protected: // Implementation of  SvgLoaderObserver
156   /**
157    * @copydoc Dali::Toolkit::Internal::SvgLoaderObserver::LoadComplete
158    */
159   void LoadComplete(int32_t loadId, Dali::VectorImageRenderer vectorImageRenderer) override;
160
161   /**
162    * @copydoc Dali::Toolkit::Internal::SvgLoaderObserver::RasterizeComplete
163    */
164   void RasterizeComplete(int32_t rasterizeId, Dali::TextureSet textureSet, Vector4 atlasRect) override;
165
166 private:
167   /**
168    * @bried Rasterize the svg with the given size, and add it to the visual.
169    *
170    * @param[in] size The target size of the SVG rasterization.
171    */
172   void AddRasterizationTask(const Vector2& size);
173
174   /**
175    * Helper method to set individual values by index key.
176    * @param[in] index The index key of the value
177    * @param[in] value The value
178    */
179   void DoSetProperty(Property::Index index, const Property::Value& value);
180
181   /**
182    * @brief Checks if atlasing should be attempted
183    * @return bool returns true if atlasing can be attempted.
184    */
185   bool AttemptAtlasing() const;
186
187   // Undefined
188   SvgVisual(const SvgVisual& svgRenderer);
189
190   // Undefined
191   SvgVisual& operator=(const SvgVisual& svgRenderer);
192
193 private:
194   ImageVisualShaderFactory& mImageVisualShaderFactory;
195   SvgLoader&                mSvgLoader; ///< reference to Svg loader for fast access
196
197   SvgLoader::SvgLoadId      mSvgLoadId;
198   SvgLoader::SvgRasterizeId mSvgRasterizeId;
199
200   Vector4               mAtlasRect;
201   Property::Index       mAtlasRectIndex;
202   VisualUrl             mImageUrl;
203   uint32_t              mDefaultWidth;
204   uint32_t              mDefaultHeight;
205   WeakHandle<Actor>     mPlacementActor;
206   Vector2               mRasterizedSize;
207   Dali::ImageDimensions mDesiredSize{};
208   bool                  mLoadFailed : 1;
209   bool                  mAttemptAtlasing : 1; ///< If true will attempt atlasing, otherwise create unique texture
210 };
211
212 } // namespace Internal
213
214 } // namespace Toolkit
215
216 } // namespace Dali
217
218 #endif /* DALI_TOOLKIT_INTERNAL_SVG_VISUAL_H */