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, ImageDimensions{}));
52 svgVisual->SetProperties(properties);
53 svgVisual->Initialize();
57 SvgVisualPtr SvgVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, ImageDimensions size)
59 SvgVisualPtr svgVisual(new SvgVisual(factoryCache, shaderFactory, imageUrl, size));
60 svgVisual->Initialize();
64 SvgVisual::SvgVisual(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, ImageDimensions size)
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),
76 mAttemptAtlasing(false)
78 // the rasterized image is with pre-multiplied alpha format
79 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
82 SvgVisual::~SvgVisual()
86 void SvgVisual::OnInitialize()
88 Shader shader = GenerateShader();
89 Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
90 mImpl->mRenderer = DecoratedVisualRenderer::New(geometry, shader);
91 mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT);
93 Vector2 dpi = Stage::GetCurrent().GetDpi();
94 float meanDpi = (dpi.height + dpi.width) * 0.5f;
96 SvgTaskPtr newTask = new SvgLoadingTask(this, mVectorRenderer, mImageUrl, meanDpi);
98 if(IsSynchronousLoadingRequired() && mImageUrl.IsLocalResource())
104 mFactoryCache.GetSVGRasterizationManager()->AddTask(newTask);
108 void SvgVisual::DoSetProperties(const Property::Map& propertyMap)
110 // url already passed in from constructor
111 for(Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter)
113 KeyValuePair keyValue = propertyMap.GetKeyValue(iter);
114 if(keyValue.first.type == Property::Key::INDEX)
116 DoSetProperty(keyValue.first.indexKey, keyValue.second);
118 else if(keyValue.first == IMAGE_ATLASING)
120 DoSetProperty(Toolkit::ImageVisual::Property::ATLASING, keyValue.second);
122 else if(keyValue.first == SYNCHRONOUS_LOADING)
124 DoSetProperty(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second);
126 else if(keyValue.first == IMAGE_DESIRED_WIDTH)
128 DoSetProperty(Toolkit::ImageVisual::Property::DESIRED_WIDTH, keyValue.second);
130 else if(keyValue.first == IMAGE_DESIRED_HEIGHT)
132 DoSetProperty(Toolkit::ImageVisual::Property::DESIRED_HEIGHT, keyValue.second);
137 void SvgVisual::DoSetProperty(Property::Index index, const Property::Value& value)
141 case Toolkit::ImageVisual::Property::ATLASING:
143 value.Get(mAttemptAtlasing);
146 case Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING:
153 mImpl->mFlags |= Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
157 mImpl->mFlags &= ~Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
162 DALI_LOG_ERROR("ImageVisual: synchronousLoading property has incorrect type\n");
166 case Toolkit::ImageVisual::Property::DESIRED_WIDTH:
168 int32_t desiredWidth = 0;
169 if(value.Get(desiredWidth))
171 mDesiredSize.SetWidth(desiredWidth);
175 case Toolkit::ImageVisual::Property::DESIRED_HEIGHT:
177 int32_t desiredHeight = 0;
178 if(value.Get(desiredHeight))
180 mDesiredSize.SetHeight(desiredHeight);
187 void SvgVisual::DoSetOnScene(Actor& actor)
189 TextureSet textureSet = TextureSet::New();
190 mImpl->mRenderer.SetTextures(textureSet);
192 // Register transform properties
193 mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
195 // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
197 // Hold the weak handle of the placement actor and delay the adding of renderer until the svg rasterization is finished.
198 mPlacementActor = actor;
202 Vector2 imageSize = Vector2::ZERO;
203 imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
204 mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize);
205 actor.AddRenderer(mImpl->mRenderer);
207 ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
211 if(mImpl->mEventObserver)
213 // SVG visual needs it's size set before it can be rasterized hence request relayout once on stage
214 mImpl->mEventObserver->RelayoutRequest(*this);
219 void SvgVisual::DoSetOffScene(Actor& actor)
221 mFactoryCache.GetSVGRasterizationManager()->RemoveTask(this);
223 actor.RemoveRenderer(mImpl->mRenderer);
224 mPlacementActor.Reset();
226 // Reset the visual size to zero so that when adding the actor back to stage the SVG rasterization is forced
227 mRasterizedSize = Vector2::ZERO;
230 void SvgVisual::GetNaturalSize(Vector2& naturalSize)
232 if(mDesiredSize.GetWidth() > 0 && mDesiredSize.GetHeight() > 0)
234 naturalSize.x = mDesiredSize.GetWidth();
235 naturalSize.y = mDesiredSize.GetHeight();
237 else if(mLoadFailed && mImpl->mRenderer)
239 // Load failed, use broken image size
240 auto textureSet = mImpl->mRenderer.GetTextures();
241 if(textureSet && textureSet.GetTextureCount())
243 auto texture = textureSet.GetTexture(0);
246 naturalSize.x = texture.GetWidth();
247 naturalSize.y = texture.GetHeight();
254 naturalSize.x = mDefaultWidth;
255 naturalSize.y = mDefaultHeight;
259 void SvgVisual::DoCreatePropertyMap(Property::Map& map) const
262 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::SVG);
263 if(mImageUrl.IsValid())
265 map.Insert(Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl());
266 map.Insert(Toolkit::ImageVisual::Property::ATLASING, mAttemptAtlasing);
268 map.Insert(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, IsSynchronousLoadingRequired());
269 map.Insert(Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth());
270 map.Insert(Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight());
273 void SvgVisual::DoCreateInstancePropertyMap(Property::Map& map) const
278 void SvgVisual::EnablePreMultipliedAlpha(bool preMultiplied)
280 // Make always enable pre multiplied alpha whether preMultiplied value is false.
283 DALI_LOG_WARNING("Note : SvgVisual cannot disable PreMultipliedAlpha\n");
287 void SvgVisual::AddRasterizationTask(const Vector2& size)
291 unsigned int width = static_cast<unsigned int>(size.width);
292 unsigned int height = static_cast<unsigned int>(size.height);
294 SvgTaskPtr newTask = new SvgRasterizingTask(this, mVectorRenderer, width, height);
296 if(IsSynchronousLoadingRequired() && mImageUrl.IsLocalResource())
299 ApplyRasterizedImage(newTask->GetPixelData(), newTask->HasSucceeded());
303 mFactoryCache.GetSVGRasterizationManager()->AddTask(newTask);
308 void SvgVisual::ApplyRasterizedImage(PixelData rasterizedPixelData, bool success)
312 if(mDefaultWidth == 0 || mDefaultHeight == 0)
314 mVectorRenderer.GetDefaultSize(mDefaultWidth, mDefaultHeight);
317 // Rasterization success
318 if(rasterizedPixelData && IsOnScene())
320 mRasterizedSize.x = static_cast<float>(rasterizedPixelData.GetWidth());
321 mRasterizedSize.y = static_cast<float>(rasterizedPixelData.GetHeight());
323 TextureSet currentTextureSet = mImpl->mRenderer.GetTextures();
324 if(mImpl->mFlags & Impl::IS_ATLASING_APPLIED)
326 mFactoryCache.GetAtlasManager()->Remove(currentTextureSet, mAtlasRect);
329 TextureSet textureSet;
331 if(mAttemptAtlasing && !mImpl->mCustomShader)
334 textureSet = mFactoryCache.GetAtlasManager()->Add(atlasRect, rasterizedPixelData);
335 if(textureSet) // atlasing
337 if(textureSet != currentTextureSet)
339 mImpl->mRenderer.SetTextures(textureSet);
341 mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, atlasRect);
342 mAtlasRect = atlasRect;
343 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
347 if(!textureSet) // no atlasing - mAttemptAtlasing is false or adding to atlas is failed
349 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888, rasterizedPixelData.GetWidth(), rasterizedPixelData.GetHeight());
350 texture.Upload(rasterizedPixelData);
351 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
353 if(mAtlasRect == FULL_TEXTURE_RECT)
355 textureSet = currentTextureSet;
359 textureSet = TextureSet::New();
360 mImpl->mRenderer.SetTextures(textureSet);
362 mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, FULL_TEXTURE_RECT);
363 mAtlasRect = FULL_TEXTURE_RECT;
368 textureSet.SetTexture(0, texture);
372 // Rasterized pixels are uploaded to texture. If weak handle is holding a placement actor, it is the time to add the renderer to actor.
373 Actor actor = mPlacementActor.GetHandle();
376 actor.AddRenderer(mImpl->mRenderer);
377 // reset the weak handle so that the renderer only get added to actor once
378 mPlacementActor.Reset();
381 // Svg loaded and ready to display
382 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
385 else if(!success && !mLoadFailed)
389 Actor actor = mPlacementActor.GetHandle();
392 Vector2 imageSize = Vector2::ZERO;
393 imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
394 mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize);
395 actor.AddRenderer(mImpl->mRenderer);
398 ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
402 void SvgVisual::OnSetTransform()
404 if(IsOnScene() && !mLoadFailed)
407 if(mDesiredSize.GetWidth() > 0 && mDesiredSize.GetHeight() > 0)
410 size = Vector2(mDesiredSize.GetWidth(), mDesiredSize.GetHeight());
415 size = mImpl->mTransform.GetVisualSize(mImpl->mControlSize);
418 if(size != mRasterizedSize || mDefaultWidth == 0 || mDefaultHeight == 0)
420 mRasterizedSize = size;
421 AddRasterizationTask(size);
427 mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
431 void SvgVisual::UpdateShader()
435 Shader shader = GenerateShader();
436 mImpl->mRenderer.SetShader(shader);
440 Shader SvgVisual::GenerateShader() const
443 if(!mImpl->mCustomShader)
445 shader = mImageVisualShaderFactory.GetShader(
447 ImageVisualShaderFeature::FeatureBuilder()
448 .EnableTextureAtlas(mAttemptAtlasing)
449 .EnableRoundedCorner(IsRoundedCornerRequired())
450 .EnableBorderline(IsBorderlineRequired()));
454 shader = Shader::New(mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource().data() : mImpl->mCustomShader->mVertexShader,
455 mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource().data() : mImpl->mCustomShader->mFragmentShader,
456 mImpl->mCustomShader->mHints);
458 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
463 } // namespace Internal
465 } // namespace Toolkit