2 * Copyright (c) 2020 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/third-party/nanosvg/nanosvg.h>
23 #include <dali-toolkit/third-party/nanosvg/nanosvgrast.h>
24 #include <dali-toolkit/internal/visuals/svg/svg-rasterize-thread.h>
25 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
26 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
27 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
28 #include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
29 #include <dali/devel-api/adaptor-framework/file-loader.h>
32 #include <dali/public-api/common/stage.h>
33 #include <dali/integration-api/debug.h>
47 const char * const UNITS("px");
49 const char * const IMAGE_ATLASING( "atlasing" );
51 const char * const SYNCHRONOUS_LOADING( "synchronousLoading" );
53 const Dali::Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
57 SvgVisualPtr SvgVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties )
59 SvgVisualPtr svgVisual( new SvgVisual( factoryCache, shaderFactory, imageUrl ) );
60 svgVisual->ParseFromUrl( imageUrl );
61 svgVisual->SetProperties( properties );
66 SvgVisualPtr SvgVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
68 SvgVisualPtr svgVisual( new SvgVisual( factoryCache, shaderFactory, imageUrl ) );
69 svgVisual->ParseFromUrl( imageUrl );
74 SvgVisual::SvgVisual( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
75 : Visual::Base( factoryCache, Visual::FittingMode::FILL ),
76 mImageVisualShaderFactory( shaderFactory ),
77 mAtlasRect( FULL_TEXTURE_RECT ),
78 mImageUrl( imageUrl ),
81 mVisualSize(Vector2::ZERO),
82 mAttemptAtlasing( false )
84 // the rasterized image is with pre-multiplied alpha format
85 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
88 SvgVisual::~SvgVisual()
92 nsvgDelete( mParsedImage );
96 void SvgVisual::DoSetProperties( const Property::Map& propertyMap )
98 // url already passed in from constructor
99 for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
101 KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
102 if( keyValue.first.type == Property::Key::INDEX )
104 DoSetProperty( keyValue.first.indexKey, keyValue.second );
106 else if( keyValue.first == IMAGE_ATLASING )
108 DoSetProperty( Toolkit::ImageVisual::Property::ATLASING, keyValue.second );
110 else if( keyValue.first == SYNCHRONOUS_LOADING )
112 DoSetProperty( Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second );
117 void SvgVisual::DoSetProperty( Property::Index index, const Property::Value& value )
121 case Toolkit::ImageVisual::Property::ATLASING:
123 value.Get( mAttemptAtlasing );
126 case Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING:
129 if( value.Get( sync ) )
133 mImpl->mFlags |= Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
137 mImpl->mFlags &= ~Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
142 DALI_LOG_ERROR("ImageVisual: synchronousLoading property has incorrect type\n");
149 void SvgVisual::DoSetOnStage( Actor& actor )
152 if( !mImpl->mCustomShader )
154 shader = mImageVisualShaderFactory.GetShader( mFactoryCache, mAttemptAtlasing, true, false );
158 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource() : mImpl->mCustomShader->mVertexShader,
159 mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource() : mImpl->mCustomShader->mFragmentShader,
160 mImpl->mCustomShader->mHints );
162 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
165 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
166 TextureSet textureSet = TextureSet::New();
167 mImpl->mRenderer = Renderer::New( geometry, shader );
168 mImpl->mRenderer.SetTextures( textureSet );
170 // Register transform properties
171 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
173 // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
175 // Hold the weak handle of the placement actor and delay the adding of renderer until the svg rasterization is finished.
176 mPlacementActor = actor;
178 // SVG visual needs it's size set before it can be rasterized hence set ResourceReady once on stage
179 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
182 void SvgVisual::DoSetOffStage( Actor& actor )
184 mFactoryCache.GetSVGRasterizationThread()->RemoveTask( this );
186 actor.RemoveRenderer( mImpl->mRenderer );
187 mImpl->mRenderer.Reset();
188 mPlacementActor.Reset();
190 // Reset the visual size to zero so that when adding the actor back to stage the SVG rasterization is forced
191 mVisualSize = Vector2::ZERO;
194 void SvgVisual::GetNaturalSize( Vector2& naturalSize )
198 naturalSize.x = mParsedImage->width;
199 naturalSize.y = mParsedImage->height;
203 naturalSize = Vector2::ZERO;
207 void SvgVisual::DoCreatePropertyMap( Property::Map& map ) const
210 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::SVG );
211 if( mImageUrl.IsValid() )
213 map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl() );
214 map.Insert( Toolkit::ImageVisual::Property::ATLASING, mAttemptAtlasing );
218 void SvgVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
223 void SvgVisual::ParseFromUrl( const VisualUrl& imageUrl )
225 mImageUrl = imageUrl;
226 if( mImageUrl.IsLocalResource() )
228 Vector2 dpi = Stage::GetCurrent().GetDpi();
229 float meanDpi = ( dpi.height + dpi.width ) * 0.5f;
230 Dali::Vector<char> buffer;
231 if ( Dali::FileLoader::ReadFile( mImageUrl.GetUrl(), buffer ) )
233 buffer.PushBack( '\0' );
234 mParsedImage = nsvgParse( buffer.Begin(), UNITS, meanDpi );
239 void SvgVisual::AddRasterizationTask( const Vector2& size )
241 if( mImpl->mRenderer )
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, mParsedImage, mImageUrl, meanDpi, width, height );
250 if ( IsSynchronousLoadingRequired() )
252 newTask->Rasterize();
253 ApplyRasterizedImage( newTask->GetParsedImage(), newTask->GetPixelData() );
257 mFactoryCache.GetSVGRasterizationThread()->AddTask( newTask );
262 void SvgVisual::ApplyRasterizedImage( NSVGimage* parsedSvg, PixelData rasterizedPixelData )
264 if( mParsedImage == NULL)
266 mParsedImage = parsedSvg;
269 if( mParsedImage && IsOnStage() )
271 TextureSet currentTextureSet = mImpl->mRenderer.GetTextures();
272 if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED )
274 mFactoryCache.GetAtlasManager()->Remove( currentTextureSet, mAtlasRect );
277 TextureSet textureSet;
279 if( mAttemptAtlasing && !mImpl->mCustomShader )
282 textureSet = mFactoryCache.GetAtlasManager()->Add(atlasRect, rasterizedPixelData );
283 if( textureSet ) // atlasing
285 if( textureSet != currentTextureSet )
287 mImpl->mRenderer.SetTextures( textureSet );
289 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
290 mAtlasRect = atlasRect;
291 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
295 if( !textureSet ) // no atlasing - mAttemptAtlasing is false or adding to atlas is failed
297 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888,
298 rasterizedPixelData.GetWidth(), rasterizedPixelData.GetHeight() );
299 texture.Upload( rasterizedPixelData );
300 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
302 if( mAtlasRect == FULL_TEXTURE_RECT )
304 textureSet = currentTextureSet;
308 textureSet = TextureSet::New();
309 mImpl->mRenderer.SetTextures( textureSet );
311 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, FULL_TEXTURE_RECT );
312 mAtlasRect = FULL_TEXTURE_RECT;
317 textureSet.SetTexture( 0, texture );
321 // Rasterized pixels are uploaded to texture. If weak handle is holding a placement actor, it is the time to add the renderer to actor.
322 Actor actor = mPlacementActor.GetHandle();
325 actor.AddRenderer( mImpl->mRenderer );
326 // reset the weak handle so that the renderer only get added to actor once
327 mPlacementActor.Reset();
330 // Svg loaded and ready to display
331 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
333 else if( !mParsedImage )
335 ResourceReady( Toolkit::Visual::ResourceStatus::FAILED );
339 void SvgVisual::OnSetTransform()
341 Vector2 visualSize = mImpl->mTransform.GetVisualSize( mImpl->mControlSize );
345 if( visualSize != mVisualSize )
347 AddRasterizationTask( visualSize );
348 mVisualSize = visualSize;
354 mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
358 bool SvgVisual::IsResourceReady() const
360 return ( mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY ||
361 mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::FAILED );
364 } // namespace Internal
366 } // namespace Toolkit