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-toolkit/public-api/visuals/image-visual-properties.h>
32 #include <dali/devel-api/common/stage.h>
33 #include <dali/devel-api/adaptor-framework/file-loader.h>
34 #include <dali/integration-api/debug.h>
48 const char * const UNITS("px");
50 const Dali::Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
54 SvgVisualPtr SvgVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties )
56 SvgVisualPtr svgVisual( new SvgVisual( factoryCache, shaderFactory, imageUrl ) );
57 svgVisual->ParseFromUrl( imageUrl );
58 svgVisual->SetProperties( properties );
63 SvgVisualPtr SvgVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
65 SvgVisualPtr svgVisual( new SvgVisual( factoryCache, shaderFactory, imageUrl ) );
66 svgVisual->ParseFromUrl( imageUrl );
71 SvgVisual::SvgVisual( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
72 : Visual::Base( factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::SVG ),
73 mImageVisualShaderFactory( shaderFactory ),
74 mAtlasRect( FULL_TEXTURE_RECT ),
75 mImageUrl( imageUrl ),
78 mVisualSize(Vector2::ZERO),
79 mAttemptAtlasing( false )
81 // the rasterized image is with pre-multiplied alpha format
82 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
85 SvgVisual::~SvgVisual()
89 nsvgDelete( mParsedImage );
93 void SvgVisual::DoSetProperties( const Property::Map& propertyMap )
95 // url already passed in from constructor
96 for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
98 KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
99 if( keyValue.first.type == Property::Key::INDEX )
101 DoSetProperty( keyValue.first.indexKey, keyValue.second );
103 else if( keyValue.first == IMAGE_ATLASING )
105 DoSetProperty( Toolkit::ImageVisual::Property::ATLASING, keyValue.second );
107 else if( keyValue.first == SYNCHRONOUS_LOADING )
109 DoSetProperty( Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second );
114 void SvgVisual::DoSetProperty( Property::Index index, const Property::Value& value )
118 case Toolkit::ImageVisual::Property::ATLASING:
120 value.Get( mAttemptAtlasing );
123 case Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING:
126 if( value.Get( sync ) )
130 mImpl->mFlags |= Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
134 mImpl->mFlags &= ~Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
139 DALI_LOG_ERROR("ImageVisual: synchronousLoading property has incorrect type\n");
146 void SvgVisual::DoSetOnScene( Actor& actor )
149 if( !mImpl->mCustomShader )
151 shader = mImageVisualShaderFactory.GetShader( mFactoryCache, mAttemptAtlasing, true, false );
155 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource() : mImpl->mCustomShader->mVertexShader,
156 mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource() : mImpl->mCustomShader->mFragmentShader,
157 mImpl->mCustomShader->mHints );
159 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
162 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
163 TextureSet textureSet = TextureSet::New();
164 mImpl->mRenderer = Renderer::New( geometry, shader );
165 mImpl->mRenderer.SetTextures( textureSet );
167 // Register transform properties
168 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
170 // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
172 // Hold the weak handle of the placement actor and delay the adding of renderer until the svg rasterization is finished.
173 mPlacementActor = actor;
175 // SVG visual needs it's size set before it can be rasterized hence set ResourceReady once on stage
176 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
179 void SvgVisual::DoSetOffScene( Actor& actor )
181 mFactoryCache.GetSVGRasterizationThread()->RemoveTask( this );
183 actor.RemoveRenderer( mImpl->mRenderer );
184 mImpl->mRenderer.Reset();
185 mPlacementActor.Reset();
187 // Reset the visual size to zero so that when adding the actor back to stage the SVG rasterization is forced
188 mVisualSize = Vector2::ZERO;
191 void SvgVisual::GetNaturalSize( Vector2& naturalSize )
195 naturalSize.x = mParsedImage->width;
196 naturalSize.y = mParsedImage->height;
200 naturalSize = Vector2::ZERO;
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::ParseFromUrl( const VisualUrl& imageUrl )
223 mImageUrl = imageUrl;
224 if( mImageUrl.IsLocalResource() )
226 Vector2 dpi = Stage::GetCurrent().GetDpi();
227 float meanDpi = ( dpi.height + dpi.width ) * 0.5f;
228 Dali::Vector<char> buffer;
229 if ( Dali::FileLoader::ReadFile( mImageUrl.GetUrl(), buffer ) )
231 buffer.PushBack( '\0' );
232 mParsedImage = nsvgParse( buffer.Begin(), UNITS, meanDpi );
237 void SvgVisual::AddRasterizationTask( const Vector2& size )
239 if( mImpl->mRenderer )
241 unsigned int width = static_cast<unsigned int>(size.width);
242 unsigned int height = static_cast<unsigned int>( size.height );
244 Vector2 dpi = Stage::GetCurrent().GetDpi();
245 float meanDpi = ( dpi.height + dpi.width ) * 0.5f;
247 RasterizingTaskPtr newTask = new RasterizingTask( this, mParsedImage, mImageUrl, meanDpi, width, height );
248 if ( IsSynchronousLoadingRequired() )
250 newTask->Rasterize();
251 ApplyRasterizedImage( newTask->GetParsedImage(), newTask->GetPixelData() );
255 mFactoryCache.GetSVGRasterizationThread()->AddTask( newTask );
260 void SvgVisual::ApplyRasterizedImage( NSVGimage* parsedSvg, PixelData rasterizedPixelData )
262 if( mParsedImage == NULL)
264 mParsedImage = parsedSvg;
267 if( mParsedImage && IsOnScene() )
269 TextureSet currentTextureSet = mImpl->mRenderer.GetTextures();
270 if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED )
272 mFactoryCache.GetAtlasManager()->Remove( currentTextureSet, mAtlasRect );
275 TextureSet textureSet;
277 if( mAttemptAtlasing && !mImpl->mCustomShader )
280 textureSet = mFactoryCache.GetAtlasManager()->Add(atlasRect, rasterizedPixelData );
281 if( textureSet ) // atlasing
283 if( textureSet != currentTextureSet )
285 mImpl->mRenderer.SetTextures( textureSet );
287 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
288 mAtlasRect = atlasRect;
289 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
293 if( !textureSet ) // no atlasing - mAttemptAtlasing is false or adding to atlas is failed
295 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888,
296 rasterizedPixelData.GetWidth(), rasterizedPixelData.GetHeight() );
297 texture.Upload( rasterizedPixelData );
298 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
300 if( mAtlasRect == FULL_TEXTURE_RECT )
302 textureSet = currentTextureSet;
306 textureSet = TextureSet::New();
307 mImpl->mRenderer.SetTextures( textureSet );
309 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, FULL_TEXTURE_RECT );
310 mAtlasRect = FULL_TEXTURE_RECT;
315 textureSet.SetTexture( 0, texture );
319 // Rasterized pixels are uploaded to texture. If weak handle is holding a placement actor, it is the time to add the renderer to actor.
320 Actor actor = mPlacementActor.GetHandle();
323 actor.AddRenderer( mImpl->mRenderer );
324 // reset the weak handle so that the renderer only get added to actor once
325 mPlacementActor.Reset();
328 // Svg loaded and ready to display
329 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
331 else if( !mParsedImage )
333 ResourceReady( Toolkit::Visual::ResourceStatus::FAILED );
337 void SvgVisual::OnSetTransform()
339 Vector2 visualSize = mImpl->mTransform.GetVisualSize( mImpl->mControlSize );
343 if( visualSize != mVisualSize )
345 AddRasterizationTask( visualSize );
346 mVisualSize = visualSize;
352 mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
356 bool SvgVisual::IsResourceReady() const
358 return ( mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY ||
359 mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::FAILED );
362 } // namespace Internal
364 } // namespace Toolkit