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()
87 Shader shader = GenerateShader();
88 Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
89 mImpl->mRenderer = Renderer::New(geometry, shader);
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.RegisterUniforms(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 Texture brokenImage = mFactoryCache.GetBrokenVisualImage();
161 textureSet.SetTexture(0u, brokenImage);
163 actor.AddRenderer(mImpl->mRenderer);
165 ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
169 // SVG visual needs it's size set before it can be rasterized hence set ResourceReady once on stage
170 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
174 void SvgVisual::DoSetOffScene(Actor& actor)
176 mFactoryCache.GetSVGRasterizationThread()->RemoveTask(this);
178 actor.RemoveRenderer(mImpl->mRenderer);
179 mPlacementActor.Reset();
181 // Reset the visual size to zero so that when adding the actor back to stage the SVG rasterization is forced
182 mVisualSize = Vector2::ZERO;
185 void SvgVisual::GetNaturalSize(Vector2& naturalSize)
187 naturalSize.x = mDefaultWidth;
188 naturalSize.y = mDefaultHeight;
191 void SvgVisual::DoCreatePropertyMap(Property::Map& map) const
194 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::SVG);
195 if(mImageUrl.IsValid())
197 map.Insert(Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl());
198 map.Insert(Toolkit::ImageVisual::Property::ATLASING, mAttemptAtlasing);
200 map.Insert(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, IsSynchronousLoadingRequired());
203 void SvgVisual::DoCreateInstancePropertyMap(Property::Map& map) const
208 void SvgVisual::Load()
210 // load remote resource on svg rasterize thread.
211 if(mImageUrl.IsLocalResource())
213 Dali::Vector<uint8_t> buffer;
214 if(Dali::FileLoader::ReadFile(mImageUrl.GetUrl(), buffer))
216 buffer.PushBack('\0');
218 Vector2 dpi = Stage::GetCurrent().GetDpi();
219 float meanDpi = (dpi.height + dpi.width) * 0.5f;
220 if(!mVectorRenderer.Load(buffer, meanDpi))
223 DALI_LOG_ERROR("SvgVisual::Load: Failed to load file! [%s]\n", mImageUrl.GetUrl().c_str());
226 mVectorRenderer.GetDefaultSize(mDefaultWidth, mDefaultHeight);
231 DALI_LOG_ERROR("SvgVisual::Load: Failed to read file! [%s]\n", mImageUrl.GetUrl().c_str());
236 void SvgVisual::AddRasterizationTask(const Vector2& size)
240 unsigned int width = static_cast<unsigned int>(size.width);
241 unsigned int height = static_cast<unsigned int>(size.height);
243 Vector2 dpi = Stage::GetCurrent().GetDpi();
244 float meanDpi = (dpi.height + dpi.width) * 0.5f;
246 RasterizingTaskPtr newTask = new RasterizingTask(this, mVectorRenderer, mImageUrl, meanDpi, width, height);
247 if(IsSynchronousLoadingRequired() && mImageUrl.IsLocalResource())
250 newTask->Rasterize();
251 ApplyRasterizedImage(newTask->GetVectorRenderer(), newTask->GetPixelData(), newTask->IsLoaded());
255 mFactoryCache.GetSVGRasterizationThread()->AddTask(newTask);
260 void SvgVisual::ApplyRasterizedImage(VectorImageRenderer vectorRenderer, PixelData rasterizedPixelData, bool isLoaded)
262 if(isLoaded && rasterizedPixelData && IsOnScene())
264 TextureSet currentTextureSet = mImpl->mRenderer.GetTextures();
265 if(mImpl->mFlags & Impl::IS_ATLASING_APPLIED)
267 mFactoryCache.GetAtlasManager()->Remove(currentTextureSet, mAtlasRect);
270 TextureSet textureSet;
272 if(mAttemptAtlasing && !mImpl->mCustomShader)
275 textureSet = mFactoryCache.GetAtlasManager()->Add(atlasRect, rasterizedPixelData);
276 if(textureSet) // atlasing
278 if(textureSet != currentTextureSet)
280 mImpl->mRenderer.SetTextures(textureSet);
282 mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, atlasRect);
283 mAtlasRect = atlasRect;
284 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
288 if(!textureSet) // no atlasing - mAttemptAtlasing is false or adding to atlas is failed
290 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888, rasterizedPixelData.GetWidth(), rasterizedPixelData.GetHeight());
291 texture.Upload(rasterizedPixelData);
292 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
294 if(mAtlasRect == FULL_TEXTURE_RECT)
296 textureSet = currentTextureSet;
300 textureSet = TextureSet::New();
301 mImpl->mRenderer.SetTextures(textureSet);
303 mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, FULL_TEXTURE_RECT);
304 mAtlasRect = FULL_TEXTURE_RECT;
309 textureSet.SetTexture(0, texture);
313 // Rasterized pixels are uploaded to texture. If weak handle is holding a placement actor, it is the time to add the renderer to actor.
314 Actor actor = mPlacementActor.GetHandle();
317 actor.AddRenderer(mImpl->mRenderer);
318 // reset the weak handle so that the renderer only get added to actor once
319 mPlacementActor.Reset();
322 // Svg loaded and ready to display
323 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
325 else if(!isLoaded || !rasterizedPixelData)
327 Actor actor = mPlacementActor.GetHandle();
330 TextureSet textureSet = mImpl->mRenderer.GetTextures();
332 Texture brokenImage = mFactoryCache.GetBrokenVisualImage();
333 textureSet.SetTexture(0u, brokenImage);
335 actor.AddRenderer(mImpl->mRenderer);
338 ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
342 void SvgVisual::OnSetTransform()
344 Vector2 visualSize = mImpl->mTransform.GetVisualSize(mImpl->mControlSize);
346 if(IsOnScene() && !mLoadFailed)
348 if(visualSize != mVisualSize)
350 AddRasterizationTask(visualSize);
351 mVisualSize = visualSize;
357 mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
361 bool SvgVisual::IsResourceReady() const
363 return (mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY ||
364 mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::FAILED);
367 void SvgVisual::UpdateShader()
371 Shader shader = GenerateShader();
372 mImpl->mRenderer.SetShader(shader);
376 Shader SvgVisual::GenerateShader() const
379 if(!mImpl->mCustomShader)
381 shader = mImageVisualShaderFactory.GetShader(
383 mAttemptAtlasing ? TextureAtlas::ENABLED : TextureAtlas::DISABLED,
384 DefaultTextureWrapMode::APPLY,
385 IsRoundedCornerRequired() ? RoundedCorner::ENABLED : RoundedCorner::DISABLED,
386 IsBorderlineRequired() ? Borderline::ENABLED : Borderline::DISABLED
391 shader = Shader::New(mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource().data() : mImpl->mCustomShader->mVertexShader,
392 mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource().data() : mImpl->mCustomShader->mFragmentShader,
393 mImpl->mCustomShader->mHints);
395 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
400 } // namespace Internal
402 } // namespace Toolkit