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(mFactoryCache, mAttemptAtlasing, true, IsRoundedCornerRequired());
94 shader = Shader::New(mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource().data() : mImpl->mCustomShader->mVertexShader,
95 mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource().data() : mImpl->mCustomShader->mFragmentShader,
96 mImpl->mCustomShader->mHints);
98 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
101 Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
102 mImpl->mRenderer = Renderer::New(geometry, shader);
105 void SvgVisual::DoSetProperties(const Property::Map& propertyMap)
107 // url already passed in from constructor
108 for(Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter)
110 KeyValuePair keyValue = propertyMap.GetKeyValue(iter);
111 if(keyValue.first.type == Property::Key::INDEX)
113 DoSetProperty(keyValue.first.indexKey, keyValue.second);
115 else if(keyValue.first == IMAGE_ATLASING)
117 DoSetProperty(Toolkit::ImageVisual::Property::ATLASING, keyValue.second);
119 else if(keyValue.first == SYNCHRONOUS_LOADING)
121 DoSetProperty(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second);
126 void SvgVisual::DoSetProperty(Property::Index index, const Property::Value& value)
130 case Toolkit::ImageVisual::Property::ATLASING:
132 value.Get(mAttemptAtlasing);
135 case Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING:
142 mImpl->mFlags |= Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
146 mImpl->mFlags &= ~Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
151 DALI_LOG_ERROR("ImageVisual: synchronousLoading property has incorrect type\n");
158 void SvgVisual::DoSetOnScene(Actor& actor)
160 TextureSet textureSet = TextureSet::New();
161 mImpl->mRenderer.SetTextures(textureSet);
163 // Register transform properties
164 mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
166 // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
168 // Hold the weak handle of the placement actor and delay the adding of renderer until the svg rasterization is finished.
169 mPlacementActor = actor;
173 Texture brokenImage = mFactoryCache.GetBrokenVisualImage();
174 textureSet.SetTexture(0u, brokenImage);
176 actor.AddRenderer(mImpl->mRenderer);
178 ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
182 // SVG visual needs it's size set before it can be rasterized hence set ResourceReady once on stage
183 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
187 void SvgVisual::DoSetOffScene(Actor& actor)
189 mFactoryCache.GetSVGRasterizationThread()->RemoveTask(this);
191 actor.RemoveRenderer(mImpl->mRenderer);
192 mPlacementActor.Reset();
194 // Reset the visual size to zero so that when adding the actor back to stage the SVG rasterization is forced
195 mVisualSize = Vector2::ZERO;
198 void SvgVisual::GetNaturalSize(Vector2& naturalSize)
200 naturalSize.x = mDefaultWidth;
201 naturalSize.y = mDefaultHeight;
204 void SvgVisual::DoCreatePropertyMap(Property::Map& map) const
207 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::SVG);
208 if(mImageUrl.IsValid())
210 map.Insert(Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl());
211 map.Insert(Toolkit::ImageVisual::Property::ATLASING, mAttemptAtlasing);
213 map.Insert(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, IsSynchronousLoadingRequired());
216 void SvgVisual::DoCreateInstancePropertyMap(Property::Map& map) const
221 void SvgVisual::Load()
223 // load remote resource on svg rasterize thread.
224 if(mImageUrl.IsLocalResource())
226 Dali::Vector<uint8_t> buffer;
227 if(Dali::FileLoader::ReadFile(mImageUrl.GetUrl(), buffer))
229 buffer.PushBack('\0');
231 Vector2 dpi = Stage::GetCurrent().GetDpi();
232 float meanDpi = (dpi.height + dpi.width) * 0.5f;
233 DALI_LOG_RELEASE_INFO("SvgVisual::Load: Try load file! [%s]\n", mImageUrl.GetUrl().c_str());
235 if(!mVectorRenderer.Load(buffer, meanDpi))
238 DALI_LOG_ERROR("SvgVisual::Load: Failed to load file! [%s]\n", mImageUrl.GetUrl().c_str());
241 mVectorRenderer.GetDefaultSize(mDefaultWidth, mDefaultHeight);
246 DALI_LOG_ERROR("SvgVisual::Load: Failed to read file! [%s]\n", mImageUrl.GetUrl().c_str());
251 void SvgVisual::AddRasterizationTask(const Vector2& size)
255 unsigned int width = static_cast<unsigned int>(size.width);
256 unsigned int height = static_cast<unsigned int>(size.height);
258 Vector2 dpi = Stage::GetCurrent().GetDpi();
259 float meanDpi = (dpi.height + dpi.width) * 0.5f;
261 RasterizingTaskPtr newTask = new RasterizingTask(this, mVectorRenderer, mImageUrl, meanDpi, width, height);
262 if(IsSynchronousLoadingRequired() && mImageUrl.IsLocalResource())
265 newTask->Rasterize();
266 ApplyRasterizedImage(newTask->GetVectorRenderer(), newTask->GetPixelData(), newTask->IsLoaded());
270 mFactoryCache.GetSVGRasterizationThread()->AddTask(newTask);
275 void SvgVisual::ApplyRasterizedImage(VectorImageRenderer vectorRenderer, PixelData rasterizedPixelData, bool isLoaded)
277 if(isLoaded && rasterizedPixelData && IsOnScene())
279 TextureSet currentTextureSet = mImpl->mRenderer.GetTextures();
280 if(mImpl->mFlags & Impl::IS_ATLASING_APPLIED)
282 mFactoryCache.GetAtlasManager()->Remove(currentTextureSet, mAtlasRect);
285 TextureSet textureSet;
287 if(mAttemptAtlasing && !mImpl->mCustomShader)
290 textureSet = mFactoryCache.GetAtlasManager()->Add(atlasRect, rasterizedPixelData);
291 if(textureSet) // atlasing
293 if(textureSet != currentTextureSet)
295 mImpl->mRenderer.SetTextures(textureSet);
297 mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, atlasRect);
298 mAtlasRect = atlasRect;
299 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
303 if(!textureSet) // no atlasing - mAttemptAtlasing is false or adding to atlas is failed
305 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888, rasterizedPixelData.GetWidth(), rasterizedPixelData.GetHeight());
306 texture.Upload(rasterizedPixelData);
307 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
309 if(mAtlasRect == FULL_TEXTURE_RECT)
311 textureSet = currentTextureSet;
315 textureSet = TextureSet::New();
316 mImpl->mRenderer.SetTextures(textureSet);
318 mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, FULL_TEXTURE_RECT);
319 mAtlasRect = FULL_TEXTURE_RECT;
324 textureSet.SetTexture(0, texture);
328 // Rasterized pixels are uploaded to texture. If weak handle is holding a placement actor, it is the time to add the renderer to actor.
329 Actor actor = mPlacementActor.GetHandle();
332 actor.AddRenderer(mImpl->mRenderer);
333 // reset the weak handle so that the renderer only get added to actor once
334 mPlacementActor.Reset();
337 // Svg loaded and ready to display
338 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
340 else if(!isLoaded || !rasterizedPixelData)
342 Actor actor = mPlacementActor.GetHandle();
345 TextureSet textureSet = mImpl->mRenderer.GetTextures();
347 Texture brokenImage = mFactoryCache.GetBrokenVisualImage();
348 textureSet.SetTexture(0u, brokenImage);
350 actor.AddRenderer(mImpl->mRenderer);
353 ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
357 void SvgVisual::OnSetTransform()
359 Vector2 visualSize = mImpl->mTransform.GetVisualSize(mImpl->mControlSize);
361 if(IsOnScene() && !mLoadFailed)
363 if(visualSize != mVisualSize)
365 AddRasterizationTask(visualSize);
366 mVisualSize = visualSize;
372 mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
376 bool SvgVisual::IsResourceReady() const
378 return (mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY ||
379 mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::FAILED);
382 } // namespace Internal
384 } // namespace Toolkit