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/svg/svg-rasterize-thread.h>
23 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
24 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
25 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
26 #include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
27 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
30 #include <dali/devel-api/common/stage.h>
31 #include <dali/devel-api/adaptor-framework/file-loader.h>
32 #include <dali/integration-api/debug.h>
46 const Dali::Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
50 SvgVisualPtr SvgVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties )
52 SvgVisualPtr svgVisual( new SvgVisual( factoryCache, shaderFactory, imageUrl ) );
54 svgVisual->SetProperties(properties);
55 svgVisual->Initialize();
59 SvgVisualPtr SvgVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
61 SvgVisualPtr svgVisual( new SvgVisual( factoryCache, shaderFactory, imageUrl ) );
63 svgVisual->Initialize();
67 SvgVisual::SvgVisual( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
68 : Visual::Base( factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::SVG ),
69 mImageVisualShaderFactory( shaderFactory ),
70 mAtlasRect( FULL_TEXTURE_RECT ),
71 mImageUrl( imageUrl ),
72 mVectorRenderer( VectorImageRenderer::New() ),
77 mVisualSize(Vector2::ZERO),
78 mAttemptAtlasing( false )
80 // the rasterized image is with pre-multiplied alpha format
81 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
84 SvgVisual::~SvgVisual()
88 void SvgVisual::OnInitialize()
91 if(!mImpl->mCustomShader)
93 shader = mImageVisualShaderFactory.GetShader(mFactoryCache, mAttemptAtlasing, true, IsRoundedCornerRequired());
97 shader = Shader::New(mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource().data() : mImpl->mCustomShader->mVertexShader,
98 mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource().data() : mImpl->mCustomShader->mFragmentShader,
99 mImpl->mCustomShader->mHints);
101 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
104 Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
105 mImpl->mRenderer = Renderer::New(geometry, shader);
108 void SvgVisual::DoSetProperties( const Property::Map& propertyMap )
110 // url already passed in from constructor
111 for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
113 KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
114 if( keyValue.first.type == Property::Key::INDEX )
116 DoSetProperty( keyValue.first.indexKey, keyValue.second );
118 else if( keyValue.first == IMAGE_ATLASING )
120 DoSetProperty( Toolkit::ImageVisual::Property::ATLASING, keyValue.second );
122 else if( keyValue.first == SYNCHRONOUS_LOADING )
124 DoSetProperty( Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second );
129 void SvgVisual::DoSetProperty( Property::Index index, const Property::Value& value )
133 case Toolkit::ImageVisual::Property::ATLASING:
135 value.Get( mAttemptAtlasing );
138 case Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING:
141 if( value.Get( sync ) )
145 mImpl->mFlags |= Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
149 mImpl->mFlags &= ~Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
154 DALI_LOG_ERROR("ImageVisual: synchronousLoading property has incorrect type\n");
161 void SvgVisual::DoSetOnScene( Actor& actor )
163 TextureSet textureSet = TextureSet::New();
164 mImpl->mRenderer.SetTextures( textureSet );
166 // Register transform properties
167 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
169 // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
171 // Hold the weak handle of the placement actor and delay the adding of renderer until the svg rasterization is finished.
172 mPlacementActor = actor;
174 // SVG visual needs it's size set before it can be rasterized hence set ResourceReady once on stage
175 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
178 void SvgVisual::DoSetOffScene( Actor& actor )
180 mFactoryCache.GetSVGRasterizationThread()->RemoveTask( this );
182 actor.RemoveRenderer( mImpl->mRenderer );
183 mPlacementActor.Reset();
185 // Reset the visual size to zero so that when adding the actor back to stage the SVG rasterization is forced
186 mVisualSize = Vector2::ZERO;
189 void SvgVisual::GetNaturalSize( Vector2& naturalSize )
193 naturalSize.x = mDefaultWidth;
194 naturalSize.y = mDefaultHeight;
198 naturalSize = Vector2::ZERO;
202 void SvgVisual::DoCreatePropertyMap( Property::Map& map ) const
205 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::SVG );
206 if( mImageUrl.IsValid() )
208 map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl() );
209 map.Insert( Toolkit::ImageVisual::Property::ATLASING, mAttemptAtlasing );
211 map.Insert( Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, IsSynchronousLoadingRequired() );
214 void SvgVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
219 void SvgVisual::Load()
221 // load remote resource on svg rasterize thread.
222 if(!mLoaded && mImageUrl.IsLocalResource())
224 Dali::Vector<uint8_t> buffer;
225 if(Dali::FileLoader::ReadFile(mImageUrl.GetUrl(), buffer))
227 buffer.PushBack('\0');
229 Vector2 dpi = Stage::GetCurrent().GetDpi();
230 float meanDpi = (dpi.height + dpi.width) * 0.5f;
231 if(!mVectorRenderer.Load(buffer, meanDpi))
233 DALI_LOG_ERROR("SvgVisual::Load: Failed to load file! [%s]\n", mImageUrl.GetUrl().c_str());
236 mVectorRenderer.GetDefaultSize(mDefaultWidth, mDefaultHeight);
241 DALI_LOG_ERROR("SvgVisual::Load: Failed to read file! [%s]\n", mImageUrl.GetUrl().c_str());
246 void SvgVisual::AddRasterizationTask( const Vector2& size )
248 if( mImpl->mRenderer )
250 unsigned int width = static_cast<unsigned int>(size.width);
251 unsigned int height = static_cast<unsigned int>( size.height );
253 Vector2 dpi = Stage::GetCurrent().GetDpi();
254 float meanDpi = ( dpi.height + dpi.width ) * 0.5f;
256 RasterizingTaskPtr newTask = new RasterizingTask(this, mVectorRenderer, mImageUrl, meanDpi, width, height, mLoaded);
257 if(IsSynchronousLoadingRequired())
260 newTask->Rasterize();
261 ApplyRasterizedImage(newTask->GetVectorRenderer(), newTask->GetPixelData(), newTask->IsLoaded());
265 mFactoryCache.GetSVGRasterizationThread()->AddTask( newTask );
270 void SvgVisual::ApplyRasterizedImage( VectorImageRenderer vectorRenderer, PixelData rasterizedPixelData, bool isLoaded )
274 if(isLoaded && rasterizedPixelData && IsOnScene())
276 TextureSet currentTextureSet = mImpl->mRenderer.GetTextures();
277 if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED )
279 mFactoryCache.GetAtlasManager()->Remove( currentTextureSet, mAtlasRect );
282 TextureSet textureSet;
284 if( mAttemptAtlasing && !mImpl->mCustomShader )
287 textureSet = mFactoryCache.GetAtlasManager()->Add(atlasRect, rasterizedPixelData );
288 if( textureSet ) // atlasing
290 if( textureSet != currentTextureSet )
292 mImpl->mRenderer.SetTextures( textureSet );
294 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
295 mAtlasRect = atlasRect;
296 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
300 if( !textureSet ) // no atlasing - mAttemptAtlasing is false or adding to atlas is failed
302 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888,
303 rasterizedPixelData.GetWidth(), rasterizedPixelData.GetHeight() );
304 texture.Upload( rasterizedPixelData );
305 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
307 if( mAtlasRect == FULL_TEXTURE_RECT )
309 textureSet = currentTextureSet;
313 textureSet = TextureSet::New();
314 mImpl->mRenderer.SetTextures( textureSet );
316 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, FULL_TEXTURE_RECT );
317 mAtlasRect = FULL_TEXTURE_RECT;
322 textureSet.SetTexture( 0, texture );
326 // Rasterized pixels are uploaded to texture. If weak handle is holding a placement actor, it is the time to add the renderer to actor.
327 Actor actor = mPlacementActor.GetHandle();
330 actor.AddRenderer( mImpl->mRenderer );
331 // reset the weak handle so that the renderer only get added to actor once
332 mPlacementActor.Reset();
335 // Svg loaded and ready to display
336 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
338 else if(!isLoaded || !rasterizedPixelData)
340 ResourceReady( Toolkit::Visual::ResourceStatus::FAILED );
344 void SvgVisual::OnSetTransform()
346 Vector2 visualSize = mImpl->mTransform.GetVisualSize( mImpl->mControlSize );
350 if( visualSize != mVisualSize )
352 AddRasterizationTask( visualSize );
353 mVisualSize = visualSize;
359 mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
363 bool SvgVisual::IsResourceReady() const
365 return ( mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY ||
366 mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::FAILED );
369 } // namespace Internal
371 } // namespace Toolkit