2 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "svg-visual.h"
22 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
23 #include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
24 #include <dali-toolkit/internal/visuals/svg/svg-rasterize-thread.h>
25 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
26 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
27 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
30 #include <dali/devel-api/common/stage.h>
31 #include <dali/integration-api/debug.h>
41 const int CUSTOM_PROPERTY_COUNT(6); // atlas + corner/border
44 const Dali::Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
48 SvgVisualPtr SvgVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties)
50 SvgVisualPtr svgVisual(new SvgVisual(factoryCache, shaderFactory, imageUrl));
51 svgVisual->SetProperties(properties);
52 svgVisual->Initialize();
56 SvgVisualPtr SvgVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl)
58 SvgVisualPtr svgVisual(new SvgVisual(factoryCache, shaderFactory, imageUrl));
59 svgVisual->Initialize();
63 SvgVisual::SvgVisual(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl)
64 : Visual::Base(factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::SVG),
65 mImageVisualShaderFactory(shaderFactory),
66 mAtlasRect(FULL_TEXTURE_RECT),
68 mVectorRenderer(VectorImageRenderer::New()),
72 mRasterizedSize(Vector2::ZERO),
74 mAttemptAtlasing(false)
76 // the rasterized image is with pre-multiplied alpha format
77 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
80 SvgVisual::~SvgVisual()
84 void SvgVisual::OnInitialize()
86 Shader shader = GenerateShader();
87 Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
88 mImpl->mRenderer = VisualRenderer::New(geometry, shader);
89 mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT);
91 Vector2 dpi = Stage::GetCurrent().GetDpi();
92 float meanDpi = (dpi.height + dpi.width) * 0.5f;
94 SvgTaskPtr newTask = new SvgLoadingTask(this, mVectorRenderer, mImageUrl, meanDpi);
96 if(IsSynchronousLoadingRequired() && mImageUrl.IsLocalResource())
102 mFactoryCache.GetSVGRasterizationThread()->AddTask(newTask);
106 void SvgVisual::DoSetProperties(const Property::Map& propertyMap)
108 // url already passed in from constructor
109 for(Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter)
111 KeyValuePair keyValue = propertyMap.GetKeyValue(iter);
112 if(keyValue.first.type == Property::Key::INDEX)
114 DoSetProperty(keyValue.first.indexKey, keyValue.second);
116 else if(keyValue.first == IMAGE_ATLASING)
118 DoSetProperty(Toolkit::ImageVisual::Property::ATLASING, keyValue.second);
120 else if(keyValue.first == SYNCHRONOUS_LOADING)
122 DoSetProperty(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second);
127 void SvgVisual::DoSetProperty(Property::Index index, const Property::Value& value)
131 case Toolkit::ImageVisual::Property::ATLASING:
133 value.Get(mAttemptAtlasing);
136 case Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING:
143 mImpl->mFlags |= Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
147 mImpl->mFlags &= ~Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
152 DALI_LOG_ERROR("ImageVisual: synchronousLoading property has incorrect type\n");
159 void SvgVisual::DoSetOnScene(Actor& actor)
161 TextureSet textureSet = TextureSet::New();
162 mImpl->mRenderer.SetTextures(textureSet);
164 // Register transform properties
165 mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
167 // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
169 // Hold the weak handle of the placement actor and delay the adding of renderer until the svg rasterization is finished.
170 mPlacementActor = actor;
174 Vector2 imageSize = Vector2::ZERO;
175 imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
176 mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize);
177 actor.AddRenderer(mImpl->mRenderer);
179 ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
183 if(mImpl->mEventObserver)
185 // SVG visual needs it's size set before it can be rasterized hence request relayout once on stage
186 mImpl->mEventObserver->RelayoutRequest(*this);
191 void SvgVisual::DoSetOffScene(Actor& actor)
193 mFactoryCache.GetSVGRasterizationThread()->RemoveTask(this);
195 actor.RemoveRenderer(mImpl->mRenderer);
196 mPlacementActor.Reset();
198 // Reset the visual size to zero so that when adding the actor back to stage the SVG rasterization is forced
199 mRasterizedSize = Vector2::ZERO;
202 void SvgVisual::GetNaturalSize(Vector2& naturalSize)
204 if(mLoadFailed && mImpl->mRenderer)
206 // Load failed, use broken image size
207 auto textureSet = mImpl->mRenderer.GetTextures();
208 if(textureSet && textureSet.GetTextureCount())
210 auto texture = textureSet.GetTexture(0);
213 naturalSize.x = texture.GetWidth();
214 naturalSize.y = texture.GetHeight();
221 naturalSize.x = mDefaultWidth;
222 naturalSize.y = mDefaultHeight;
226 void SvgVisual::DoCreatePropertyMap(Property::Map& map) const
229 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::SVG);
230 if(mImageUrl.IsValid())
232 map.Insert(Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl());
233 map.Insert(Toolkit::ImageVisual::Property::ATLASING, mAttemptAtlasing);
235 map.Insert(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, IsSynchronousLoadingRequired());
238 void SvgVisual::DoCreateInstancePropertyMap(Property::Map& map) const
243 void SvgVisual::EnablePreMultipliedAlpha(bool preMultiplied)
245 // Make always enable pre multiplied alpha whether preMultiplied value is false.
248 DALI_LOG_WARNING("Note : SvgVisual cannot disable PreMultipliedAlpha\n");
252 void SvgVisual::AddRasterizationTask(const Vector2& size)
256 unsigned int width = static_cast<unsigned int>(size.width);
257 unsigned int height = static_cast<unsigned int>(size.height);
259 SvgTaskPtr newTask = new SvgRasterizingTask(this, mVectorRenderer, width, height);
261 if(IsSynchronousLoadingRequired() && mImageUrl.IsLocalResource())
264 ApplyRasterizedImage(newTask->GetPixelData(), newTask->HasSucceeded());
268 mFactoryCache.GetSVGRasterizationThread()->AddTask(newTask);
273 void SvgVisual::ApplyRasterizedImage(PixelData rasterizedPixelData, bool success)
277 if(mDefaultWidth == 0 || mDefaultHeight == 0)
279 mVectorRenderer.GetDefaultSize(mDefaultWidth, mDefaultHeight);
282 // Rasterization success
283 if(rasterizedPixelData && IsOnScene())
285 mRasterizedSize.x = static_cast<float>(rasterizedPixelData.GetWidth());
286 mRasterizedSize.y = static_cast<float>(rasterizedPixelData.GetHeight());
288 TextureSet currentTextureSet = mImpl->mRenderer.GetTextures();
289 if(mImpl->mFlags & Impl::IS_ATLASING_APPLIED)
291 mFactoryCache.GetAtlasManager()->Remove(currentTextureSet, mAtlasRect);
294 TextureSet textureSet;
296 if(mAttemptAtlasing && !mImpl->mCustomShader)
299 textureSet = mFactoryCache.GetAtlasManager()->Add(atlasRect, rasterizedPixelData);
300 if(textureSet) // atlasing
302 if(textureSet != currentTextureSet)
304 mImpl->mRenderer.SetTextures(textureSet);
306 mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, atlasRect);
307 mAtlasRect = atlasRect;
308 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
312 if(!textureSet) // no atlasing - mAttemptAtlasing is false or adding to atlas is failed
314 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888, rasterizedPixelData.GetWidth(), rasterizedPixelData.GetHeight());
315 texture.Upload(rasterizedPixelData);
316 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
318 if(mAtlasRect == FULL_TEXTURE_RECT)
320 textureSet = currentTextureSet;
324 textureSet = TextureSet::New();
325 mImpl->mRenderer.SetTextures(textureSet);
327 mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, FULL_TEXTURE_RECT);
328 mAtlasRect = FULL_TEXTURE_RECT;
333 textureSet.SetTexture(0, texture);
337 // Rasterized pixels are uploaded to texture. If weak handle is holding a placement actor, it is the time to add the renderer to actor.
338 Actor actor = mPlacementActor.GetHandle();
341 actor.AddRenderer(mImpl->mRenderer);
342 // reset the weak handle so that the renderer only get added to actor once
343 mPlacementActor.Reset();
346 // Svg loaded and ready to display
347 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
350 else if(!success && !mLoadFailed)
354 Actor actor = mPlacementActor.GetHandle();
357 Vector2 imageSize = Vector2::ZERO;
358 imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
359 mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize);
360 actor.AddRenderer(mImpl->mRenderer);
363 ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
367 void SvgVisual::OnSetTransform()
369 Vector2 visualSize = mImpl->mTransform.GetVisualSize(mImpl->mControlSize);
371 if(IsOnScene() && !mLoadFailed)
373 if(visualSize != mRasterizedSize || mDefaultWidth == 0 || mDefaultHeight == 0)
375 mRasterizedSize = visualSize;
376 AddRasterizationTask(visualSize);
382 mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
386 void SvgVisual::UpdateShader()
390 Shader shader = GenerateShader();
391 mImpl->mRenderer.SetShader(shader);
395 Shader SvgVisual::GenerateShader() const
398 if(!mImpl->mCustomShader)
400 shader = mImageVisualShaderFactory.GetShader(
402 ImageVisualShaderFeature::FeatureBuilder()
403 .EnableTextureAtlas(mAttemptAtlasing)
404 .EnableRoundedCorner(IsRoundedCornerRequired())
405 .EnableBorderline(IsBorderlineRequired()));
409 shader = Shader::New(mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource().data() : mImpl->mCustomShader->mVertexShader,
410 mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource().data() : mImpl->mCustomShader->mFragmentShader,
411 mImpl->mCustomShader->mHints);
413 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
418 } // namespace Internal
420 } // namespace Toolkit