2 * Copyright (c) 2023 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-task.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()
84 if(Stage::IsInstalled())
88 Dali::AsyncTaskManager::Get().RemoveTask(mLoadingTask);
92 Dali::AsyncTaskManager::Get().RemoveTask(mRasterizingTask);
97 void SvgVisual::OnInitialize()
99 Shader shader = GenerateShader();
100 Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
101 mImpl->mRenderer = DecoratedVisualRenderer::New(geometry, shader);
102 mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT);
104 Vector2 dpi = Stage::GetCurrent().GetDpi();
105 float meanDpi = (dpi.height + dpi.width) * 0.5f;
107 mLoadingTask = new SvgLoadingTask(mVectorRenderer, mImageUrl, meanDpi, MakeCallback(this, &SvgVisual::ApplyRasterizedImage));
109 if(IsSynchronousLoadingRequired() && mImageUrl.IsLocalResource())
111 mLoadingTask->Process();
112 mLoadingTask.Reset(); // We don't need it now
116 Dali::AsyncTaskManager::Get().AddTask(mLoadingTask);
120 void SvgVisual::DoSetProperties(const Property::Map& propertyMap)
122 // url already passed in from constructor
123 for(Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter)
125 KeyValuePair keyValue = propertyMap.GetKeyValue(iter);
126 if(keyValue.first.type == Property::Key::INDEX)
128 DoSetProperty(keyValue.first.indexKey, keyValue.second);
130 else if(keyValue.first == IMAGE_ATLASING)
132 DoSetProperty(Toolkit::ImageVisual::Property::ATLASING, keyValue.second);
134 else if(keyValue.first == SYNCHRONOUS_LOADING)
136 DoSetProperty(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second);
138 else if(keyValue.first == IMAGE_DESIRED_WIDTH)
140 DoSetProperty(Toolkit::ImageVisual::Property::DESIRED_WIDTH, keyValue.second);
142 else if(keyValue.first == IMAGE_DESIRED_HEIGHT)
144 DoSetProperty(Toolkit::ImageVisual::Property::DESIRED_HEIGHT, keyValue.second);
149 void SvgVisual::DoSetProperty(Property::Index index, const Property::Value& value)
153 case Toolkit::ImageVisual::Property::ATLASING:
155 value.Get(mAttemptAtlasing);
158 case Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING:
165 mImpl->mFlags |= Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
169 mImpl->mFlags &= ~Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
174 DALI_LOG_ERROR("ImageVisual: synchronousLoading property has incorrect type\n");
178 case Toolkit::ImageVisual::Property::DESIRED_WIDTH:
180 int32_t desiredWidth = 0;
181 if(value.Get(desiredWidth))
183 mDesiredSize.SetWidth(desiredWidth);
187 case Toolkit::ImageVisual::Property::DESIRED_HEIGHT:
189 int32_t desiredHeight = 0;
190 if(value.Get(desiredHeight))
192 mDesiredSize.SetHeight(desiredHeight);
199 void SvgVisual::DoSetOnScene(Actor& actor)
201 TextureSet textureSet = TextureSet::New();
202 mImpl->mRenderer.SetTextures(textureSet);
204 // Register transform properties
205 mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
207 // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
209 // Hold the weak handle of the placement actor and delay the adding of renderer until the svg rasterization is finished.
210 mPlacementActor = actor;
214 Vector2 imageSize = Vector2::ZERO;
215 imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
216 mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize);
217 actor.AddRenderer(mImpl->mRenderer);
219 ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
223 if(mImpl->mEventObserver)
225 // SVG visual needs it's size set before it can be rasterized hence request relayout once on stage
226 mImpl->mEventObserver->RelayoutRequest(*this);
231 void SvgVisual::DoSetOffScene(Actor& actor)
233 // Remove loading & rasterizing task
236 Dali::AsyncTaskManager::Get().RemoveTask(mLoadingTask);
237 mLoadingTask.Reset();
241 Dali::AsyncTaskManager::Get().RemoveTask(mRasterizingTask);
242 mRasterizingTask.Reset();
245 actor.RemoveRenderer(mImpl->mRenderer);
246 mPlacementActor.Reset();
248 // Reset the visual size to zero so that when adding the actor back to stage the SVG rasterization is forced
249 mRasterizedSize = Vector2::ZERO;
252 void SvgVisual::GetNaturalSize(Vector2& naturalSize)
254 if(mDesiredSize.GetWidth() > 0 && mDesiredSize.GetHeight() > 0)
256 naturalSize.x = mDesiredSize.GetWidth();
257 naturalSize.y = mDesiredSize.GetHeight();
259 else if(mLoadFailed && mImpl->mRenderer)
261 // Load failed, use broken image size
262 auto textureSet = mImpl->mRenderer.GetTextures();
263 if(textureSet && textureSet.GetTextureCount())
265 auto texture = textureSet.GetTexture(0);
268 naturalSize.x = texture.GetWidth();
269 naturalSize.y = texture.GetHeight();
276 naturalSize.x = mDefaultWidth;
277 naturalSize.y = mDefaultHeight;
281 void SvgVisual::DoCreatePropertyMap(Property::Map& map) const
284 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::SVG);
285 if(mImageUrl.IsValid())
287 map.Insert(Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl());
288 map.Insert(Toolkit::ImageVisual::Property::ATLASING, mAttemptAtlasing);
290 map.Insert(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, IsSynchronousLoadingRequired());
291 map.Insert(Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth());
292 map.Insert(Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight());
295 void SvgVisual::DoCreateInstancePropertyMap(Property::Map& map) const
300 void SvgVisual::EnablePreMultipliedAlpha(bool preMultiplied)
302 // Make always enable pre multiplied alpha whether preMultiplied value is false.
305 DALI_LOG_WARNING("Note : SvgVisual cannot disable PreMultipliedAlpha\n");
309 void SvgVisual::AddRasterizationTask(const Vector2& size)
313 unsigned int width = static_cast<unsigned int>(size.width);
314 unsigned int height = static_cast<unsigned int>(size.height);
316 mRasterizingTask = new SvgRasterizingTask(mVectorRenderer, width, height, MakeCallback(this, &SvgVisual::ApplyRasterizedImage));
318 if(IsSynchronousLoadingRequired() && mImageUrl.IsLocalResource())
320 mRasterizingTask->Process();
321 ApplyRasterizedImage(mRasterizingTask);
322 mRasterizingTask.Reset(); // We don't need it now
326 Dali::AsyncTaskManager::Get().AddTask(mRasterizingTask);
331 void SvgVisual::ApplyRasterizedImage(SvgTaskPtr task)
333 if(task->HasSucceeded())
335 PixelData rasterizedPixelData = task->GetPixelData();
336 if(mDefaultWidth == 0 || mDefaultHeight == 0)
338 task->GetRenderer().GetDefaultSize(mDefaultWidth, mDefaultHeight);
341 // Rasterization success
342 if(rasterizedPixelData && IsOnScene())
344 mRasterizedSize.x = static_cast<float>(rasterizedPixelData.GetWidth());
345 mRasterizedSize.y = static_cast<float>(rasterizedPixelData.GetHeight());
347 TextureSet currentTextureSet = mImpl->mRenderer.GetTextures();
348 if(mImpl->mFlags & Impl::IS_ATLASING_APPLIED)
350 mFactoryCache.GetAtlasManager()->Remove(currentTextureSet, mAtlasRect);
353 TextureSet textureSet;
355 if(mAttemptAtlasing && !mImpl->mCustomShader)
358 textureSet = mFactoryCache.GetAtlasManager()->Add(atlasRect, rasterizedPixelData);
359 if(textureSet) // atlasing
361 if(textureSet != currentTextureSet)
363 mImpl->mRenderer.SetTextures(textureSet);
365 mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, atlasRect);
366 mAtlasRect = atlasRect;
367 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
371 if(!textureSet) // no atlasing - mAttemptAtlasing is false or adding to atlas is failed
373 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888, rasterizedPixelData.GetWidth(), rasterizedPixelData.GetHeight());
374 texture.Upload(rasterizedPixelData);
375 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
377 if(mAtlasRect == FULL_TEXTURE_RECT)
379 textureSet = currentTextureSet;
383 textureSet = TextureSet::New();
384 mImpl->mRenderer.SetTextures(textureSet);
386 mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, FULL_TEXTURE_RECT);
387 mAtlasRect = FULL_TEXTURE_RECT;
392 textureSet.SetTexture(0, texture);
396 // Rasterized pixels are uploaded to texture. If weak handle is holding a placement actor, it is the time to add the renderer to actor.
397 Actor actor = mPlacementActor.GetHandle();
400 actor.AddRenderer(mImpl->mRenderer);
401 // reset the weak handle so that the renderer only get added to actor once
402 mPlacementActor.Reset();
405 // Svg loaded and ready to display
406 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
409 else if(!mLoadFailed)
413 Actor actor = mPlacementActor.GetHandle();
416 Vector2 imageSize = Vector2::ZERO;
417 imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
418 mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize);
419 actor.AddRenderer(mImpl->mRenderer);
422 ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
426 void SvgVisual::OnSetTransform()
428 if(IsOnScene() && !mLoadFailed)
431 if(mDesiredSize.GetWidth() > 0 && mDesiredSize.GetHeight() > 0)
434 size = Vector2(mDesiredSize.GetWidth(), mDesiredSize.GetHeight());
439 size = mImpl->mTransform.GetVisualSize(mImpl->mControlSize);
442 if(size != mRasterizedSize || mDefaultWidth == 0 || mDefaultHeight == 0)
444 mRasterizedSize = size;
445 AddRasterizationTask(size);
451 mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
455 void SvgVisual::UpdateShader()
459 Shader shader = GenerateShader();
460 mImpl->mRenderer.SetShader(shader);
464 Shader SvgVisual::GenerateShader() const
467 if(!mImpl->mCustomShader)
469 shader = mImageVisualShaderFactory.GetShader(
471 ImageVisualShaderFeature::FeatureBuilder()
472 .EnableTextureAtlas(mAttemptAtlasing)
473 .EnableRoundedCorner(IsRoundedCornerRequired())
474 .EnableBorderline(IsBorderlineRequired()));
478 shader = Shader::New(mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource().data() : mImpl->mCustomShader->mVertexShader,
479 mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource().data() : mImpl->mCustomShader->mFragmentShader,
480 mImpl->mCustomShader->mHints);
482 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
487 } // namespace Internal
489 } // namespace Toolkit