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);
92 void SvgVisual::DoSetProperties(const Property::Map& propertyMap)
94 // url already passed in from constructor
95 for(Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter)
97 KeyValuePair keyValue = propertyMap.GetKeyValue(iter);
98 if(keyValue.first.type == Property::Key::INDEX)
100 DoSetProperty(keyValue.first.indexKey, keyValue.second);
102 else if(keyValue.first == IMAGE_ATLASING)
104 DoSetProperty(Toolkit::ImageVisual::Property::ATLASING, keyValue.second);
106 else if(keyValue.first == SYNCHRONOUS_LOADING)
108 DoSetProperty(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second);
113 void SvgVisual::DoSetProperty(Property::Index index, const Property::Value& value)
117 case Toolkit::ImageVisual::Property::ATLASING:
119 value.Get(mAttemptAtlasing);
122 case Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING:
129 mImpl->mFlags |= Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
133 mImpl->mFlags &= ~Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
138 DALI_LOG_ERROR("ImageVisual: synchronousLoading property has incorrect type\n");
145 void SvgVisual::DoSetOnScene(Actor& actor)
147 TextureSet textureSet = TextureSet::New();
148 mImpl->mRenderer.SetTextures(textureSet);
150 // Register transform properties
151 mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
153 // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
155 // Hold the weak handle of the placement actor and delay the adding of renderer until the svg rasterization is finished.
156 mPlacementActor = actor;
160 Vector2 imageSize = Vector2::ZERO;
161 imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
162 mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize);
163 actor.AddRenderer(mImpl->mRenderer);
165 ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
169 if(mImpl->mEventObserver)
171 // SVG visual needs it's size set before it can be rasterized hence request relayout once on stage
172 mImpl->mEventObserver->RelayoutRequest(*this);
177 void SvgVisual::DoSetOffScene(Actor& actor)
179 mFactoryCache.GetSVGRasterizationThread()->RemoveTask(this);
181 actor.RemoveRenderer(mImpl->mRenderer);
182 mPlacementActor.Reset();
184 // Reset the visual size to zero so that when adding the actor back to stage the SVG rasterization is forced
185 mRasterizedSize = Vector2::ZERO;
188 void SvgVisual::GetNaturalSize(Vector2& naturalSize)
190 if(mLoadFailed && mImpl->mRenderer)
192 // Load failed, use broken image size
193 auto textureSet = mImpl->mRenderer.GetTextures();
194 if(textureSet && textureSet.GetTextureCount())
196 auto texture = textureSet.GetTexture(0);
199 naturalSize.x = texture.GetWidth();
200 naturalSize.y = texture.GetHeight();
207 naturalSize.x = mDefaultWidth;
208 naturalSize.y = mDefaultHeight;
212 void SvgVisual::DoCreatePropertyMap(Property::Map& map) const
215 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::SVG);
216 if(mImageUrl.IsValid())
218 map.Insert(Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl());
219 map.Insert(Toolkit::ImageVisual::Property::ATLASING, mAttemptAtlasing);
221 map.Insert(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, IsSynchronousLoadingRequired());
224 void SvgVisual::DoCreateInstancePropertyMap(Property::Map& map) const
229 void SvgVisual::EnablePreMultipliedAlpha(bool preMultiplied)
231 // Make always enable pre multiplied alpha whether preMultiplied value is false.
234 DALI_LOG_WARNING("Note : SvgVisual cannot disable PreMultipliedAlpha\n");
238 void SvgVisual::AddRasterizationTask(const Vector2& size)
242 unsigned int width = static_cast<unsigned int>(size.width);
243 unsigned int height = static_cast<unsigned int>(size.height);
245 Vector2 dpi = Stage::GetCurrent().GetDpi();
246 float meanDpi = (dpi.height + dpi.width) * 0.5f;
248 RasterizingTaskPtr newTask = new RasterizingTask(this, mVectorRenderer, mImageUrl, meanDpi, width, height);
250 if(IsSynchronousLoadingRequired() && mImageUrl.IsLocalResource())
253 newTask->Rasterize();
254 ApplyRasterizedImage(newTask->GetPixelData(), newTask->IsLoaded());
258 mFactoryCache.GetSVGRasterizationThread()->AddTask(newTask);
263 void SvgVisual::ApplyRasterizedImage(PixelData rasterizedPixelData, bool isLoaded)
265 if(isLoaded && rasterizedPixelData && IsOnScene())
267 if(mDefaultWidth == 0 || mDefaultHeight == 0)
269 mVectorRenderer.GetDefaultSize(mDefaultWidth, mDefaultHeight);
272 mRasterizedSize.x = static_cast<float>(rasterizedPixelData.GetWidth());
273 mRasterizedSize.y = static_cast<float>(rasterizedPixelData.GetHeight());
275 TextureSet currentTextureSet = mImpl->mRenderer.GetTextures();
276 if(mImpl->mFlags & Impl::IS_ATLASING_APPLIED)
278 mFactoryCache.GetAtlasManager()->Remove(currentTextureSet, mAtlasRect);
281 TextureSet textureSet;
283 if(mAttemptAtlasing && !mImpl->mCustomShader)
286 textureSet = mFactoryCache.GetAtlasManager()->Add(atlasRect, rasterizedPixelData);
287 if(textureSet) // atlasing
289 if(textureSet != currentTextureSet)
291 mImpl->mRenderer.SetTextures(textureSet);
293 mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, atlasRect);
294 mAtlasRect = atlasRect;
295 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
299 if(!textureSet) // no atlasing - mAttemptAtlasing is false or adding to atlas is failed
301 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888, rasterizedPixelData.GetWidth(), rasterizedPixelData.GetHeight());
302 texture.Upload(rasterizedPixelData);
303 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
305 if(mAtlasRect == FULL_TEXTURE_RECT)
307 textureSet = currentTextureSet;
311 textureSet = TextureSet::New();
312 mImpl->mRenderer.SetTextures(textureSet);
314 mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, FULL_TEXTURE_RECT);
315 mAtlasRect = FULL_TEXTURE_RECT;
320 textureSet.SetTexture(0, texture);
324 // Rasterized pixels are uploaded to texture. If weak handle is holding a placement actor, it is the time to add the renderer to actor.
325 Actor actor = mPlacementActor.GetHandle();
328 actor.AddRenderer(mImpl->mRenderer);
329 // reset the weak handle so that the renderer only get added to actor once
330 mPlacementActor.Reset();
333 // Svg loaded and ready to display
334 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
336 else if(!isLoaded || !rasterizedPixelData)
340 Actor actor = mPlacementActor.GetHandle();
343 Vector2 imageSize = Vector2::ZERO;
344 imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
345 mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize);
346 actor.AddRenderer(mImpl->mRenderer);
349 ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
353 void SvgVisual::OnSetTransform()
355 Vector2 visualSize = mImpl->mTransform.GetVisualSize(mImpl->mControlSize);
357 if(IsOnScene() && !mLoadFailed)
359 if(visualSize != mRasterizedSize || mDefaultWidth == 0 || mDefaultHeight == 0)
361 mRasterizedSize = visualSize;
362 AddRasterizationTask(visualSize);
368 mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
372 void SvgVisual::UpdateShader()
376 Shader shader = GenerateShader();
377 mImpl->mRenderer.SetShader(shader);
381 Shader SvgVisual::GenerateShader() const
384 if(!mImpl->mCustomShader)
386 shader = mImageVisualShaderFactory.GetShader(
388 ImageVisualShaderFeature::FeatureBuilder()
389 .EnableTextureAtlas(mAttemptAtlasing)
390 .EnableRoundedCorner(IsRoundedCornerRequired())
391 .EnableBorderline(IsBorderlineRequired()));
395 shader = Shader::New(mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource().data() : mImpl->mCustomShader->mVertexShader,
396 mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource().data() : mImpl->mCustomShader->mFragmentShader,
397 mImpl->mCustomShader->mHints);
399 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
404 } // namespace Internal
406 } // namespace Toolkit