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>
32 #include <dali/public-api/rendering/decorated-visual-renderer.h>
42 const int CUSTOM_PROPERTY_COUNT(1); // atlas
45 const Dali::Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
49 SvgVisualPtr SvgVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties)
51 SvgVisualPtr svgVisual(new SvgVisual(factoryCache, shaderFactory, imageUrl));
52 svgVisual->SetProperties(properties);
53 svgVisual->Initialize();
57 SvgVisualPtr SvgVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl)
59 SvgVisualPtr svgVisual(new SvgVisual(factoryCache, shaderFactory, imageUrl));
60 svgVisual->Initialize();
64 SvgVisual::SvgVisual(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl)
65 : Visual::Base(factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::SVG),
66 mImageVisualShaderFactory(shaderFactory),
67 mAtlasRect(FULL_TEXTURE_RECT),
69 mVectorRenderer(VectorImageRenderer::New()),
73 mRasterizedSize(Vector2::ZERO),
75 mAttemptAtlasing(false)
77 // the rasterized image is with pre-multiplied alpha format
78 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
81 SvgVisual::~SvgVisual()
85 void SvgVisual::OnInitialize()
87 Shader shader = GenerateShader();
88 Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
89 mImpl->mRenderer = DecoratedVisualRenderer::New(geometry, shader);
90 mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT);
92 Vector2 dpi = Stage::GetCurrent().GetDpi();
93 float meanDpi = (dpi.height + dpi.width) * 0.5f;
95 SvgTaskPtr newTask = new SvgLoadingTask(this, mVectorRenderer, mImageUrl, meanDpi);
97 if(IsSynchronousLoadingRequired() && mImageUrl.IsLocalResource())
103 mFactoryCache.GetSVGRasterizationThread()->AddTask(newTask);
107 void SvgVisual::DoSetProperties(const Property::Map& propertyMap)
109 // url already passed in from constructor
110 for(Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter)
112 KeyValuePair keyValue = propertyMap.GetKeyValue(iter);
113 if(keyValue.first.type == Property::Key::INDEX)
115 DoSetProperty(keyValue.first.indexKey, keyValue.second);
117 else if(keyValue.first == IMAGE_ATLASING)
119 DoSetProperty(Toolkit::ImageVisual::Property::ATLASING, keyValue.second);
121 else if(keyValue.first == SYNCHRONOUS_LOADING)
123 DoSetProperty(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second);
128 void SvgVisual::DoSetProperty(Property::Index index, const Property::Value& value)
132 case Toolkit::ImageVisual::Property::ATLASING:
134 value.Get(mAttemptAtlasing);
137 case Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING:
144 mImpl->mFlags |= Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
148 mImpl->mFlags &= ~Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
153 DALI_LOG_ERROR("ImageVisual: synchronousLoading property has incorrect type\n");
160 void SvgVisual::DoSetOnScene(Actor& actor)
162 TextureSet textureSet = TextureSet::New();
163 mImpl->mRenderer.SetTextures(textureSet);
165 // Register transform properties
166 mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
168 // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
170 // Hold the weak handle of the placement actor and delay the adding of renderer until the svg rasterization is finished.
171 mPlacementActor = actor;
175 Vector2 imageSize = Vector2::ZERO;
176 imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
177 mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize);
178 actor.AddRenderer(mImpl->mRenderer);
180 ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
184 if(mImpl->mEventObserver)
186 // SVG visual needs it's size set before it can be rasterized hence request relayout once on stage
187 mImpl->mEventObserver->RelayoutRequest(*this);
192 void SvgVisual::DoSetOffScene(Actor& actor)
194 mFactoryCache.GetSVGRasterizationThread()->RemoveTask(this);
196 actor.RemoveRenderer(mImpl->mRenderer);
197 mPlacementActor.Reset();
199 // Reset the visual size to zero so that when adding the actor back to stage the SVG rasterization is forced
200 mRasterizedSize = Vector2::ZERO;
203 void SvgVisual::GetNaturalSize(Vector2& naturalSize)
205 if(mLoadFailed && mImpl->mRenderer)
207 // Load failed, use broken image size
208 auto textureSet = mImpl->mRenderer.GetTextures();
209 if(textureSet && textureSet.GetTextureCount())
211 auto texture = textureSet.GetTexture(0);
214 naturalSize.x = texture.GetWidth();
215 naturalSize.y = texture.GetHeight();
222 naturalSize.x = mDefaultWidth;
223 naturalSize.y = mDefaultHeight;
227 void SvgVisual::DoCreatePropertyMap(Property::Map& map) const
230 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::SVG);
231 if(mImageUrl.IsValid())
233 map.Insert(Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl());
234 map.Insert(Toolkit::ImageVisual::Property::ATLASING, mAttemptAtlasing);
236 map.Insert(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, IsSynchronousLoadingRequired());
239 void SvgVisual::DoCreateInstancePropertyMap(Property::Map& map) const
244 void SvgVisual::EnablePreMultipliedAlpha(bool preMultiplied)
246 // Make always enable pre multiplied alpha whether preMultiplied value is false.
249 DALI_LOG_WARNING("Note : SvgVisual cannot disable PreMultipliedAlpha\n");
253 void SvgVisual::AddRasterizationTask(const Vector2& size)
257 unsigned int width = static_cast<unsigned int>(size.width);
258 unsigned int height = static_cast<unsigned int>(size.height);
260 SvgTaskPtr newTask = new SvgRasterizingTask(this, mVectorRenderer, width, height);
262 if(IsSynchronousLoadingRequired() && mImageUrl.IsLocalResource())
265 ApplyRasterizedImage(newTask->GetPixelData(), newTask->HasSucceeded());
269 mFactoryCache.GetSVGRasterizationThread()->AddTask(newTask);
274 void SvgVisual::ApplyRasterizedImage(PixelData rasterizedPixelData, bool success)
278 if(mDefaultWidth == 0 || mDefaultHeight == 0)
280 mVectorRenderer.GetDefaultSize(mDefaultWidth, mDefaultHeight);
283 // Rasterization success
284 if(rasterizedPixelData && IsOnScene())
286 mRasterizedSize.x = static_cast<float>(rasterizedPixelData.GetWidth());
287 mRasterizedSize.y = static_cast<float>(rasterizedPixelData.GetHeight());
289 TextureSet currentTextureSet = mImpl->mRenderer.GetTextures();
290 if(mImpl->mFlags & Impl::IS_ATLASING_APPLIED)
292 mFactoryCache.GetAtlasManager()->Remove(currentTextureSet, mAtlasRect);
295 TextureSet textureSet;
297 if(mAttemptAtlasing && !mImpl->mCustomShader)
300 textureSet = mFactoryCache.GetAtlasManager()->Add(atlasRect, rasterizedPixelData);
301 if(textureSet) // atlasing
303 if(textureSet != currentTextureSet)
305 mImpl->mRenderer.SetTextures(textureSet);
307 mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, atlasRect);
308 mAtlasRect = atlasRect;
309 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
313 if(!textureSet) // no atlasing - mAttemptAtlasing is false or adding to atlas is failed
315 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888, rasterizedPixelData.GetWidth(), rasterizedPixelData.GetHeight());
316 texture.Upload(rasterizedPixelData);
317 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
319 if(mAtlasRect == FULL_TEXTURE_RECT)
321 textureSet = currentTextureSet;
325 textureSet = TextureSet::New();
326 mImpl->mRenderer.SetTextures(textureSet);
328 mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, FULL_TEXTURE_RECT);
329 mAtlasRect = FULL_TEXTURE_RECT;
334 textureSet.SetTexture(0, texture);
338 // Rasterized pixels are uploaded to texture. If weak handle is holding a placement actor, it is the time to add the renderer to actor.
339 Actor actor = mPlacementActor.GetHandle();
342 actor.AddRenderer(mImpl->mRenderer);
343 // reset the weak handle so that the renderer only get added to actor once
344 mPlacementActor.Reset();
347 // Svg loaded and ready to display
348 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
351 else if(!success && !mLoadFailed)
355 Actor actor = mPlacementActor.GetHandle();
358 Vector2 imageSize = Vector2::ZERO;
359 imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
360 mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize);
361 actor.AddRenderer(mImpl->mRenderer);
364 ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
368 void SvgVisual::OnSetTransform()
370 Vector2 visualSize = mImpl->mTransform.GetVisualSize(mImpl->mControlSize);
372 if(IsOnScene() && !mLoadFailed)
374 if(visualSize != mRasterizedSize || mDefaultWidth == 0 || mDefaultHeight == 0)
376 mRasterizedSize = visualSize;
377 AddRasterizationTask(visualSize);
383 mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
387 void SvgVisual::UpdateShader()
391 Shader shader = GenerateShader();
392 mImpl->mRenderer.SetShader(shader);
396 Shader SvgVisual::GenerateShader() const
399 if(!mImpl->mCustomShader)
401 shader = mImageVisualShaderFactory.GetShader(
403 ImageVisualShaderFeature::FeatureBuilder()
404 .EnableTextureAtlas(mAttemptAtlasing)
405 .EnableRoundedCorner(IsRoundedCornerRequired())
406 .EnableBorderline(IsBorderlineRequired()));
410 shader = Shader::New(mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource().data() : mImpl->mCustomShader->mVertexShader,
411 mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource().data() : mImpl->mCustomShader->mFragmentShader,
412 mImpl->mCustomShader->mHints);
414 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
419 } // namespace Internal
421 } // namespace Toolkit