1 #ifndef DALI_TOOLKIT_INTERNAL_SVG_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_SVG_VISUAL_H
5 * Copyright (c) 2024 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <dali/public-api/common/intrusive-ptr.h>
23 #include <dali/public-api/object/weak-handle.h>
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>
37 class ImageVisualShaderFactory;
39 typedef IntrusivePtr<SvgVisual> SvgVisualPtr;
42 * The visual which renders a svg image
44 * The following property is essential
46 * | %Property Name | Type |
47 * |--------------------------|------------------|
51 class SvgVisual : public Visual::Base,
52 public SvgLoaderObserver
56 * @brief Create the SVG Visual using the image URL.
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
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.
67 static SvgVisualPtr New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties);
70 * @brief Create the SVG Visual using the image URL.
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
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.
81 static SvgVisualPtr New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, ImageDimensions size = ImageDimensions());
83 public: // from Visual
85 * @copydoc Visual::Base::GetNaturalSize
87 void GetNaturalSize(Vector2& naturalSize) override;
90 * @copydoc Visual::Base::CreatePropertyMap
92 void DoCreatePropertyMap(Property::Map& map) const override;
95 * @copydoc Visual::Base::CreateInstancePropertyMap
97 void DoCreateInstancePropertyMap(Property::Map& map) const override;
100 * @copydoc Visual::Base::EnablePreMultipliedAlpha
102 void EnablePreMultipliedAlpha(bool preMultiplied) override;
106 * @brief Constructor.
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.
113 SvgVisual(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, ImageDimensions size);
116 * @brief A reference counted object may only be deleted by calling Unreference().
118 virtual ~SvgVisual();
121 * @copydoc Visual::Base::OnInitialize
123 void OnInitialize() override;
126 * @copydoc Visual::Base::DoSetProperties
128 void DoSetProperties(const Property::Map& propertyMap) override;
131 * @copydoc Visual::Base::DoSetOnScene
133 void DoSetOnScene(Actor& actor) override;
136 * @copydoc Visual::Base::DoSetOffScene
138 void DoSetOffScene(Actor& actor) override;
141 * @copydoc Visual::Base::OnSetTransform
143 void OnSetTransform() override;
146 * @copydoc Visual::Base::UpdateShader
148 void UpdateShader() override;
151 * @copydoc Visual::Base::GenerateShader
153 Shader GenerateShader() const override;
155 protected: // Implementation of SvgLoaderObserver
157 * @copydoc Dali::Toolkit::Internal::SvgLoaderObserver::LoadComplete
159 void LoadComplete(int32_t loadId, Dali::VectorImageRenderer vectorImageRenderer) override;
162 * @copydoc Dali::Toolkit::Internal::SvgLoaderObserver::RasterizeComplete
164 void RasterizeComplete(int32_t rasterizeId, Dali::TextureSet textureSet, Vector4 atlasRect) override;
168 * @bried Rasterize the svg with the given size, and add it to the visual.
170 * @param[in] size The target size of the SVG rasterization.
172 void AddRasterizationTask(const Vector2& size);
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
179 void DoSetProperty(Property::Index index, const Property::Value& value);
182 * @brief Checks if atlasing should be attempted
183 * @return bool returns true if atlasing can be attempted.
185 bool AttemptAtlasing() const;
188 SvgVisual(const SvgVisual& svgRenderer);
191 SvgVisual& operator=(const SvgVisual& svgRenderer);
194 ImageVisualShaderFactory& mImageVisualShaderFactory;
195 SvgLoader& mSvgLoader; ///< reference to Svg loader for fast access
197 SvgLoader::SvgLoadId mSvgLoadId;
198 SvgLoader::SvgRasterizeId mSvgRasterizeId;
201 Property::Index mAtlasRectIndex;
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
212 } // namespace Internal
214 } // namespace Toolkit
218 #endif /* DALI_TOOLKIT_INTERNAL_SVG_VISUAL_H */