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/adaptor-framework/file-loader.h>
31 #include <dali/devel-api/common/stage.h>
32 #include <dali/integration-api/debug.h>
42 const int CUSTOM_PROPERTY_COUNT(6); // atlas + corner/border
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));
53 svgVisual->SetProperties(properties);
54 svgVisual->Initialize();
58 SvgVisualPtr SvgVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl)
60 SvgVisualPtr svgVisual(new SvgVisual(factoryCache, shaderFactory, imageUrl));
62 svgVisual->Initialize();
66 SvgVisual::SvgVisual(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl)
67 : Visual::Base(factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::SVG),
68 mImageVisualShaderFactory(shaderFactory),
69 mAtlasRect(FULL_TEXTURE_RECT),
71 mVectorRenderer(VectorImageRenderer::New()),
75 mVisualSize(Vector2::ZERO),
77 mAttemptAtlasing(false)
79 // the rasterized image is with pre-multiplied alpha format
80 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
83 SvgVisual::~SvgVisual()
87 void SvgVisual::OnInitialize()
89 Shader shader = GenerateShader();
90 Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
91 mImpl->mRenderer = VisualRenderer::New(geometry, shader);
92 mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT);
95 void SvgVisual::DoSetProperties(const Property::Map& propertyMap)
97 // url already passed in from constructor
98 for(Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter)
100 KeyValuePair keyValue = propertyMap.GetKeyValue(iter);
101 if(keyValue.first.type == Property::Key::INDEX)
103 DoSetProperty(keyValue.first.indexKey, keyValue.second);
105 else if(keyValue.first == IMAGE_ATLASING)
107 DoSetProperty(Toolkit::ImageVisual::Property::ATLASING, keyValue.second);
109 else if(keyValue.first == SYNCHRONOUS_LOADING)
111 DoSetProperty(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second);
116 void SvgVisual::DoSetProperty(Property::Index index, const Property::Value& value)
120 case Toolkit::ImageVisual::Property::ATLASING:
122 value.Get(mAttemptAtlasing);
125 case Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING:
132 mImpl->mFlags |= Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
136 mImpl->mFlags &= ~Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
141 DALI_LOG_ERROR("ImageVisual: synchronousLoading property has incorrect type\n");
148 void SvgVisual::DoSetOnScene(Actor& actor)
150 TextureSet textureSet = TextureSet::New();
151 mImpl->mRenderer.SetTextures(textureSet);
153 // Register transform properties
154 mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
156 // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
158 // Hold the weak handle of the placement actor and delay the adding of renderer until the svg rasterization is finished.
159 mPlacementActor = actor;
163 Vector2 imageSize = Vector2::ZERO;
164 imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
165 mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize);
166 actor.AddRenderer(mImpl->mRenderer);
168 ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
172 // SVG visual needs it's size set before it can be rasterized hence set ResourceReady once on stage
173 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
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 mVisualSize = Vector2::ZERO;
188 void SvgVisual::GetNaturalSize(Vector2& naturalSize)
190 naturalSize.x = mDefaultWidth;
191 naturalSize.y = mDefaultHeight;
194 void SvgVisual::DoCreatePropertyMap(Property::Map& map) const
197 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::SVG);
198 if(mImageUrl.IsValid())
200 map.Insert(Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl());
201 map.Insert(Toolkit::ImageVisual::Property::ATLASING, mAttemptAtlasing);
203 map.Insert(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, IsSynchronousLoadingRequired());
206 void SvgVisual::DoCreateInstancePropertyMap(Property::Map& map) const
211 void SvgVisual::Load()
213 // load remote resource on svg rasterize thread.
214 if(mImageUrl.IsLocalResource())
216 Dali::Vector<uint8_t> buffer;
217 if(Dali::FileLoader::ReadFile(mImageUrl.GetUrl(), buffer))
219 buffer.PushBack('\0');
221 Vector2 dpi = Stage::GetCurrent().GetDpi();
222 float meanDpi = (dpi.height + dpi.width) * 0.5f;
223 if(!mVectorRenderer.Load(buffer, meanDpi))
226 DALI_LOG_ERROR("SvgVisual::Load: Failed to load file! [%s]\n", mImageUrl.GetUrl().c_str());
229 mVectorRenderer.GetDefaultSize(mDefaultWidth, mDefaultHeight);
234 DALI_LOG_ERROR("SvgVisual::Load: Failed to read file! [%s]\n", mImageUrl.GetUrl().c_str());
239 void SvgVisual::AddRasterizationTask(const Vector2& size)
243 unsigned int width = static_cast<unsigned int>(size.width);
244 unsigned int height = static_cast<unsigned int>(size.height);
246 Vector2 dpi = Stage::GetCurrent().GetDpi();
247 float meanDpi = (dpi.height + dpi.width) * 0.5f;
249 RasterizingTaskPtr newTask = new RasterizingTask(this, mVectorRenderer, mImageUrl, meanDpi, width, height);
250 if(IsSynchronousLoadingRequired() && mImageUrl.IsLocalResource())
253 newTask->Rasterize();
254 ApplyRasterizedImage(newTask->GetVectorRenderer(), newTask->GetPixelData(), newTask->IsLoaded());
258 mFactoryCache.GetSVGRasterizationThread()->AddTask(newTask);
263 void SvgVisual::ApplyRasterizedImage(VectorImageRenderer vectorRenderer, PixelData rasterizedPixelData, bool isLoaded)
265 if(isLoaded && rasterizedPixelData && IsOnScene())
267 TextureSet currentTextureSet = mImpl->mRenderer.GetTextures();
268 if(mImpl->mFlags & Impl::IS_ATLASING_APPLIED)
270 mFactoryCache.GetAtlasManager()->Remove(currentTextureSet, mAtlasRect);
273 TextureSet textureSet;
275 if(mAttemptAtlasing && !mImpl->mCustomShader)
278 textureSet = mFactoryCache.GetAtlasManager()->Add(atlasRect, rasterizedPixelData);
279 if(textureSet) // atlasing
281 if(textureSet != currentTextureSet)
283 mImpl->mRenderer.SetTextures(textureSet);
285 mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, atlasRect);
286 mAtlasRect = atlasRect;
287 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
291 if(!textureSet) // no atlasing - mAttemptAtlasing is false or adding to atlas is failed
293 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888, rasterizedPixelData.GetWidth(), rasterizedPixelData.GetHeight());
294 texture.Upload(rasterizedPixelData);
295 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
297 if(mAtlasRect == FULL_TEXTURE_RECT)
299 textureSet = currentTextureSet;
303 textureSet = TextureSet::New();
304 mImpl->mRenderer.SetTextures(textureSet);
306 mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, FULL_TEXTURE_RECT);
307 mAtlasRect = FULL_TEXTURE_RECT;
312 textureSet.SetTexture(0, texture);
316 // Rasterized pixels are uploaded to texture. If weak handle is holding a placement actor, it is the time to add the renderer to actor.
317 Actor actor = mPlacementActor.GetHandle();
320 actor.AddRenderer(mImpl->mRenderer);
321 // reset the weak handle so that the renderer only get added to actor once
322 mPlacementActor.Reset();
325 // Svg loaded and ready to display
326 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
328 else if(!isLoaded || !rasterizedPixelData)
330 Actor actor = mPlacementActor.GetHandle();
333 Vector2 imageSize = Vector2::ZERO;
334 imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
335 mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize);
336 actor.AddRenderer(mImpl->mRenderer);
339 ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
343 void SvgVisual::OnSetTransform()
345 Vector2 visualSize = mImpl->mTransform.GetVisualSize(mImpl->mControlSize);
347 if(IsOnScene() && !mLoadFailed)
349 if(visualSize != mVisualSize)
351 AddRasterizationTask(visualSize);
352 mVisualSize = visualSize;
358 mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
362 bool SvgVisual::IsResourceReady() const
364 return (mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY ||
365 mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::FAILED);
368 void SvgVisual::UpdateShader()
372 Shader shader = GenerateShader();
373 mImpl->mRenderer.SetShader(shader);
377 Shader SvgVisual::GenerateShader() const
380 if(!mImpl->mCustomShader)
382 shader = mImageVisualShaderFactory.GetShader(
384 ImageVisualShaderFeature::FeatureBuilder()
385 .EnableTextureAtlas(mAttemptAtlasing)
386 .EnableRoundedCorner(IsRoundedCornerRequired())
387 .EnableBorderline(IsBorderlineRequired()));
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