2 * Copyright (c) 2021 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/adaptor-framework/file-loader.h>
31 #include <dali/devel-api/common/stage.h>
32 #include <dali/integration-api/debug.h>
43 const Dali::Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
47 SvgVisualPtr SvgVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties)
49 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));
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 mVisualSize(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()
88 if(!mImpl->mCustomShader)
90 shader = mImageVisualShaderFactory.GetShader(
92 mAttemptAtlasing ? TextureAtlas::ENABLED : TextureAtlas::DISABLED,
93 DefaultTextureWrapMode::APPLY,
94 IsRoundedCornerRequired() ? RoundedCorner::ENABLED : RoundedCorner::DISABLED,
95 IsBorderlineRequired() ? Borderline::ENABLED : Borderline::DISABLED
100 shader = Shader::New(mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource().data() : mImpl->mCustomShader->mVertexShader,
101 mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource().data() : mImpl->mCustomShader->mFragmentShader,
102 mImpl->mCustomShader->mHints);
104 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
107 Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
108 mImpl->mRenderer = Renderer::New(geometry, shader);
111 void SvgVisual::DoSetProperties(const Property::Map& propertyMap)
113 // url already passed in from constructor
114 for(Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter)
116 KeyValuePair keyValue = propertyMap.GetKeyValue(iter);
117 if(keyValue.first.type == Property::Key::INDEX)
119 DoSetProperty(keyValue.first.indexKey, keyValue.second);
121 else if(keyValue.first == IMAGE_ATLASING)
123 DoSetProperty(Toolkit::ImageVisual::Property::ATLASING, keyValue.second);
125 else if(keyValue.first == SYNCHRONOUS_LOADING)
127 DoSetProperty(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second);
132 void SvgVisual::DoSetProperty(Property::Index index, const Property::Value& value)
136 case Toolkit::ImageVisual::Property::ATLASING:
138 value.Get(mAttemptAtlasing);
141 case Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING:
148 mImpl->mFlags |= Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
152 mImpl->mFlags &= ~Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
157 DALI_LOG_ERROR("ImageVisual: synchronousLoading property has incorrect type\n");
164 void SvgVisual::DoSetOnScene(Actor& actor)
166 TextureSet textureSet = TextureSet::New();
167 mImpl->mRenderer.SetTextures(textureSet);
169 // Register transform properties
170 mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
172 // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
174 // Hold the weak handle of the placement actor and delay the adding of renderer until the svg rasterization is finished.
175 mPlacementActor = actor;
179 Texture brokenImage = mFactoryCache.GetBrokenVisualImage();
180 textureSet.SetTexture(0u, brokenImage);
182 actor.AddRenderer(mImpl->mRenderer);
184 ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
188 // SVG visual needs it's size set before it can be rasterized hence set ResourceReady once on stage
189 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
193 void SvgVisual::DoSetOffScene(Actor& actor)
195 mFactoryCache.GetSVGRasterizationThread()->RemoveTask(this);
197 actor.RemoveRenderer(mImpl->mRenderer);
198 mPlacementActor.Reset();
200 // Reset the visual size to zero so that when adding the actor back to stage the SVG rasterization is forced
201 mVisualSize = Vector2::ZERO;
204 void SvgVisual::GetNaturalSize(Vector2& naturalSize)
206 naturalSize.x = mDefaultWidth;
207 naturalSize.y = mDefaultHeight;
210 void SvgVisual::DoCreatePropertyMap(Property::Map& map) const
213 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::SVG);
214 if(mImageUrl.IsValid())
216 map.Insert(Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl());
217 map.Insert(Toolkit::ImageVisual::Property::ATLASING, mAttemptAtlasing);
219 map.Insert(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, IsSynchronousLoadingRequired());
222 void SvgVisual::DoCreateInstancePropertyMap(Property::Map& map) const
227 void SvgVisual::Load()
229 // load remote resource on svg rasterize thread.
230 if(mImageUrl.IsLocalResource())
232 Dali::Vector<uint8_t> buffer;
233 if(Dali::FileLoader::ReadFile(mImageUrl.GetUrl(), buffer))
235 buffer.PushBack('\0');
237 Vector2 dpi = Stage::GetCurrent().GetDpi();
238 float meanDpi = (dpi.height + dpi.width) * 0.5f;
239 if(!mVectorRenderer.Load(buffer, meanDpi))
242 DALI_LOG_ERROR("SvgVisual::Load: Failed to load file! [%s]\n", mImageUrl.GetUrl().c_str());
245 mVectorRenderer.GetDefaultSize(mDefaultWidth, mDefaultHeight);
250 DALI_LOG_ERROR("SvgVisual::Load: Failed to read file! [%s]\n", mImageUrl.GetUrl().c_str());
255 void SvgVisual::AddRasterizationTask(const Vector2& size)
259 unsigned int width = static_cast<unsigned int>(size.width);
260 unsigned int height = static_cast<unsigned int>(size.height);
262 Vector2 dpi = Stage::GetCurrent().GetDpi();
263 float meanDpi = (dpi.height + dpi.width) * 0.5f;
265 RasterizingTaskPtr newTask = new RasterizingTask(this, mVectorRenderer, mImageUrl, meanDpi, width, height);
266 if(IsSynchronousLoadingRequired() && mImageUrl.IsLocalResource())
269 newTask->Rasterize();
270 ApplyRasterizedImage(newTask->GetVectorRenderer(), newTask->GetPixelData(), newTask->IsLoaded());
274 mFactoryCache.GetSVGRasterizationThread()->AddTask(newTask);
279 void SvgVisual::ApplyRasterizedImage(VectorImageRenderer vectorRenderer, PixelData rasterizedPixelData, bool isLoaded)
281 if(isLoaded && rasterizedPixelData && IsOnScene())
283 TextureSet currentTextureSet = mImpl->mRenderer.GetTextures();
284 if(mImpl->mFlags & Impl::IS_ATLASING_APPLIED)
286 mFactoryCache.GetAtlasManager()->Remove(currentTextureSet, mAtlasRect);
289 TextureSet textureSet;
291 if(mAttemptAtlasing && !mImpl->mCustomShader)
294 textureSet = mFactoryCache.GetAtlasManager()->Add(atlasRect, rasterizedPixelData);
295 if(textureSet) // atlasing
297 if(textureSet != currentTextureSet)
299 mImpl->mRenderer.SetTextures(textureSet);
301 mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, atlasRect);
302 mAtlasRect = atlasRect;
303 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
307 if(!textureSet) // no atlasing - mAttemptAtlasing is false or adding to atlas is failed
309 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888, rasterizedPixelData.GetWidth(), rasterizedPixelData.GetHeight());
310 texture.Upload(rasterizedPixelData);
311 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
313 if(mAtlasRect == FULL_TEXTURE_RECT)
315 textureSet = currentTextureSet;
319 textureSet = TextureSet::New();
320 mImpl->mRenderer.SetTextures(textureSet);
322 mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, FULL_TEXTURE_RECT);
323 mAtlasRect = FULL_TEXTURE_RECT;
328 textureSet.SetTexture(0, texture);
332 // Rasterized pixels are uploaded to texture. If weak handle is holding a placement actor, it is the time to add the renderer to actor.
333 Actor actor = mPlacementActor.GetHandle();
336 actor.AddRenderer(mImpl->mRenderer);
337 // reset the weak handle so that the renderer only get added to actor once
338 mPlacementActor.Reset();
341 // Svg loaded and ready to display
342 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
344 else if(!isLoaded || !rasterizedPixelData)
346 Actor actor = mPlacementActor.GetHandle();
349 TextureSet textureSet = mImpl->mRenderer.GetTextures();
351 Texture brokenImage = mFactoryCache.GetBrokenVisualImage();
352 textureSet.SetTexture(0u, brokenImage);
354 actor.AddRenderer(mImpl->mRenderer);
357 ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
361 void SvgVisual::OnSetTransform()
363 Vector2 visualSize = mImpl->mTransform.GetVisualSize(mImpl->mControlSize);
365 if(IsOnScene() && !mLoadFailed)
367 if(visualSize != mVisualSize)
369 AddRasterizationTask(visualSize);
370 mVisualSize = visualSize;
376 mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
380 bool SvgVisual::IsResourceReady() const
382 return (mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY ||
383 mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::FAILED);
386 } // namespace Internal
388 } // namespace Toolkit