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-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()
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 mLoadingTask = new SvgLoadingTask(mVectorRenderer, mImageUrl, meanDpi, MakeCallback(this, &SvgVisual::ApplyRasterizedImage));
98 if(IsSynchronousLoadingRequired() && mImageUrl.IsLocalResource())
100 mLoadingTask->Process();
104 Dali::AsyncTaskManager::Get().AddTask(mLoadingTask);
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 // Remove loading & rasterizing task
222 Dali::AsyncTaskManager::Get().RemoveTask(mLoadingTask);
223 Dali::AsyncTaskManager::Get().RemoveTask(mRasterizingTask);
224 mLoadingTask.Reset();
225 mRasterizingTask.Reset();
227 actor.RemoveRenderer(mImpl->mRenderer);
228 mPlacementActor.Reset();
230 // Reset the visual size to zero so that when adding the actor back to stage the SVG rasterization is forced
231 mRasterizedSize = Vector2::ZERO;
234 void SvgVisual::GetNaturalSize(Vector2& naturalSize)
236 if(mDesiredSize.GetWidth() > 0 && mDesiredSize.GetHeight() > 0)
238 naturalSize.x = mDesiredSize.GetWidth();
239 naturalSize.y = mDesiredSize.GetHeight();
241 else if(mLoadFailed && mImpl->mRenderer)
243 // Load failed, use broken image size
244 auto textureSet = mImpl->mRenderer.GetTextures();
245 if(textureSet && textureSet.GetTextureCount())
247 auto texture = textureSet.GetTexture(0);
250 naturalSize.x = texture.GetWidth();
251 naturalSize.y = texture.GetHeight();
258 naturalSize.x = mDefaultWidth;
259 naturalSize.y = mDefaultHeight;
263 void SvgVisual::DoCreatePropertyMap(Property::Map& map) const
266 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::SVG);
267 if(mImageUrl.IsValid())
269 map.Insert(Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl());
270 map.Insert(Toolkit::ImageVisual::Property::ATLASING, mAttemptAtlasing);
272 map.Insert(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, IsSynchronousLoadingRequired());
273 map.Insert(Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth());
274 map.Insert(Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight());
277 void SvgVisual::DoCreateInstancePropertyMap(Property::Map& map) const
282 void SvgVisual::EnablePreMultipliedAlpha(bool preMultiplied)
284 // Make always enable pre multiplied alpha whether preMultiplied value is false.
287 DALI_LOG_WARNING("Note : SvgVisual cannot disable PreMultipliedAlpha\n");
291 void SvgVisual::AddRasterizationTask(const Vector2& size)
295 unsigned int width = static_cast<unsigned int>(size.width);
296 unsigned int height = static_cast<unsigned int>(size.height);
298 mRasterizingTask = new SvgRasterizingTask(mVectorRenderer, width, height, MakeCallback(this, &SvgVisual::ApplyRasterizedImage));
300 if(IsSynchronousLoadingRequired() && mImageUrl.IsLocalResource())
302 mRasterizingTask->Process();
303 ApplyRasterizedImage(mRasterizingTask);
307 Dali::AsyncTaskManager::Get().AddTask(mRasterizingTask);
312 void SvgVisual::ApplyRasterizedImage(SvgTaskPtr task)
314 if(task->HasSucceeded())
316 PixelData rasterizedPixelData = task->GetPixelData();
317 if(mDefaultWidth == 0 || mDefaultHeight == 0)
319 task->GetRenderer().GetDefaultSize(mDefaultWidth, mDefaultHeight);
322 // Rasterization success
323 if(rasterizedPixelData && IsOnScene())
325 mRasterizedSize.x = static_cast<float>(rasterizedPixelData.GetWidth());
326 mRasterizedSize.y = static_cast<float>(rasterizedPixelData.GetHeight());
328 TextureSet currentTextureSet = mImpl->mRenderer.GetTextures();
329 if(mImpl->mFlags & Impl::IS_ATLASING_APPLIED)
331 mFactoryCache.GetAtlasManager()->Remove(currentTextureSet, mAtlasRect);
334 TextureSet textureSet;
336 if(mAttemptAtlasing && !mImpl->mCustomShader)
339 textureSet = mFactoryCache.GetAtlasManager()->Add(atlasRect, rasterizedPixelData);
340 if(textureSet) // atlasing
342 if(textureSet != currentTextureSet)
344 mImpl->mRenderer.SetTextures(textureSet);
346 mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, atlasRect);
347 mAtlasRect = atlasRect;
348 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
352 if(!textureSet) // no atlasing - mAttemptAtlasing is false or adding to atlas is failed
354 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888, rasterizedPixelData.GetWidth(), rasterizedPixelData.GetHeight());
355 texture.Upload(rasterizedPixelData);
356 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
358 if(mAtlasRect == FULL_TEXTURE_RECT)
360 textureSet = currentTextureSet;
364 textureSet = TextureSet::New();
365 mImpl->mRenderer.SetTextures(textureSet);
367 mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, FULL_TEXTURE_RECT);
368 mAtlasRect = FULL_TEXTURE_RECT;
373 textureSet.SetTexture(0, texture);
377 // Rasterized pixels are uploaded to texture. If weak handle is holding a placement actor, it is the time to add the renderer to actor.
378 Actor actor = mPlacementActor.GetHandle();
381 actor.AddRenderer(mImpl->mRenderer);
382 // reset the weak handle so that the renderer only get added to actor once
383 mPlacementActor.Reset();
386 // Svg loaded and ready to display
387 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
390 else if(!mLoadFailed)
394 Actor actor = mPlacementActor.GetHandle();
397 Vector2 imageSize = Vector2::ZERO;
398 imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
399 mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize);
400 actor.AddRenderer(mImpl->mRenderer);
403 ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
407 void SvgVisual::OnSetTransform()
409 if(IsOnScene() && !mLoadFailed)
412 if(mDesiredSize.GetWidth() > 0 && mDesiredSize.GetHeight() > 0)
415 size = Vector2(mDesiredSize.GetWidth(), mDesiredSize.GetHeight());
420 size = mImpl->mTransform.GetVisualSize(mImpl->mControlSize);
423 if(size != mRasterizedSize || mDefaultWidth == 0 || mDefaultHeight == 0)
425 mRasterizedSize = size;
426 AddRasterizationTask(size);
432 mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
436 void SvgVisual::UpdateShader()
440 Shader shader = GenerateShader();
441 mImpl->mRenderer.SetShader(shader);
445 Shader SvgVisual::GenerateShader() const
448 if(!mImpl->mCustomShader)
450 shader = mImageVisualShaderFactory.GetShader(
452 ImageVisualShaderFeature::FeatureBuilder()
453 .EnableTextureAtlas(mAttemptAtlasing)
454 .EnableRoundedCorner(IsRoundedCornerRequired())
455 .EnableBorderline(IsBorderlineRequired()));
459 shader = Shader::New(mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource().data() : mImpl->mCustomShader->mVertexShader,
460 mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource().data() : mImpl->mCustomShader->mFragmentShader,
461 mImpl->mCustomShader->mHints);
463 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
468 } // namespace Internal
470 } // namespace Toolkit