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/internal/visuals/svg/svg-rasterize-thread.h>
24 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
25 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
26 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
27 #include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
28 #include <dali/devel-api/adaptor-framework/file-loader.h>
31 #include <dali/public-api/common/stage.h>
45 const char * const UNITS("px");
47 const char * const IMAGE_ATLASING( "atlasing" );
49 const Dali::Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
52 SvgVisualPtr SvgVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties )
54 SvgVisualPtr svgVisual( new SvgVisual( factoryCache, shaderFactory, imageUrl ) );
55 svgVisual->ParseFromUrl( imageUrl );
56 svgVisual->SetProperties( properties );
61 SvgVisualPtr SvgVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
63 SvgVisualPtr svgVisual( new SvgVisual( factoryCache, shaderFactory, imageUrl ) );
64 svgVisual->ParseFromUrl( imageUrl );
69 SvgVisual::SvgVisual( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
70 : Visual::Base( factoryCache, Visual::FittingMode::FILL ),
71 mImageVisualShaderFactory( shaderFactory ),
72 mAtlasRect( FULL_TEXTURE_RECT ),
73 mImageUrl( imageUrl ),
76 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 nsvgDelete( mParsedImage );
91 void SvgVisual::DoSetProperties( const Property::Map& propertyMap )
93 // url already passed in from constructor
94 for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
96 KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
97 if( keyValue.first.type == Property::Key::INDEX )
99 DoSetProperty( keyValue.first.indexKey, keyValue.second );
101 else if( keyValue.first == IMAGE_ATLASING )
103 DoSetProperty( Toolkit::ImageVisual::Property::ATLASING, keyValue.second );
108 void SvgVisual::DoSetProperty( Property::Index index, const Property::Value& value )
112 case Toolkit::ImageVisual::Property::ATLASING:
114 value.Get( mAttemptAtlasing );
120 void SvgVisual::DoSetOnStage( Actor& actor )
123 if( !mImpl->mCustomShader )
125 shader = mImageVisualShaderFactory.GetShader( mFactoryCache, mAttemptAtlasing, true, false );
129 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource() : mImpl->mCustomShader->mVertexShader,
130 mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource() : mImpl->mCustomShader->mFragmentShader,
131 mImpl->mCustomShader->mHints );
133 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
136 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
137 TextureSet textureSet = TextureSet::New();
138 mImpl->mRenderer = Renderer::New( geometry, shader );
139 mImpl->mRenderer.SetTextures( textureSet );
141 // Register transform properties
142 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
144 // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
146 // Hold the weak handle of the placement actor and delay the adding of renderer until the svg rasterization is finished.
147 mPlacementActor = actor;
149 // SVG visual needs it's size set before it can be rasterized hence set ResourceReady once on stage
150 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
153 void SvgVisual::DoSetOffStage( Actor& actor )
155 mFactoryCache.GetSVGRasterizationThread()->RemoveTask( this );
157 actor.RemoveRenderer( mImpl->mRenderer );
158 mImpl->mRenderer.Reset();
159 mPlacementActor.Reset();
161 // Reset the visual size to zero so that when adding the actor back to stage the SVG rasterization is forced
162 mVisualSize = Vector2::ZERO;
165 void SvgVisual::GetNaturalSize( Vector2& naturalSize )
169 naturalSize.x = mParsedImage->width;
170 naturalSize.y = mParsedImage->height;
174 naturalSize = Vector2::ZERO;
178 void SvgVisual::DoCreatePropertyMap( Property::Map& map ) const
181 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::SVG );
182 if( mImageUrl.IsValid() )
184 map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl() );
185 map.Insert( Toolkit::ImageVisual::Property::ATLASING, mAttemptAtlasing );
189 void SvgVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
194 void SvgVisual::ParseFromUrl( const VisualUrl& imageUrl )
196 mImageUrl = imageUrl;
197 if( mImageUrl.IsLocalResource() )
199 Vector2 dpi = Stage::GetCurrent().GetDpi();
200 float meanDpi = ( dpi.height + dpi.width ) * 0.5f;
201 Dali::Vector<char> buffer;
202 if ( Dali::FileLoader::ReadFile( mImageUrl.GetUrl(), buffer ) )
204 buffer.PushBack( '\0' );
205 mParsedImage = nsvgParse( buffer.Begin(), UNITS, meanDpi );
210 void SvgVisual::AddRasterizationTask( const Vector2& size )
212 if( mImpl->mRenderer )
214 unsigned int width = static_cast<unsigned int>(size.width);
215 unsigned int height = static_cast<unsigned int>( size.height );
217 Vector2 dpi = Stage::GetCurrent().GetDpi();
218 float meanDpi = ( dpi.height + dpi.width ) * 0.5f;
220 RasterizingTaskPtr newTask = new RasterizingTask( this, mParsedImage, mImageUrl, meanDpi, width, height );
221 mFactoryCache.GetSVGRasterizationThread()->AddTask( newTask );
225 void SvgVisual::ApplyRasterizedImage( NSVGimage* parsedSvg, PixelData rasterizedPixelData )
227 if( mParsedImage == NULL)
229 mParsedImage = parsedSvg;
232 if( mParsedImage && IsOnStage() )
234 TextureSet currentTextureSet = mImpl->mRenderer.GetTextures();
235 if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED )
237 mFactoryCache.GetAtlasManager()->Remove( currentTextureSet, mAtlasRect );
240 TextureSet textureSet;
242 if( mAttemptAtlasing && !mImpl->mCustomShader )
245 textureSet = mFactoryCache.GetAtlasManager()->Add(atlasRect, rasterizedPixelData );
246 if( textureSet ) // atlasing
248 if( textureSet != currentTextureSet )
250 mImpl->mRenderer.SetTextures( textureSet );
252 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
253 mAtlasRect = atlasRect;
254 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
258 if( !textureSet ) // no atlasing - mAttemptAtlasing is false or adding to atlas is failed
260 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888,
261 rasterizedPixelData.GetWidth(), rasterizedPixelData.GetHeight() );
262 texture.Upload( rasterizedPixelData );
263 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
265 if( mAtlasRect == FULL_TEXTURE_RECT )
267 textureSet = currentTextureSet;
271 textureSet = TextureSet::New();
272 mImpl->mRenderer.SetTextures( textureSet );
274 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, FULL_TEXTURE_RECT );
275 mAtlasRect = FULL_TEXTURE_RECT;
280 textureSet.SetTexture( 0, texture );
284 // Rasterized pixels are uploaded to texture. If weak handle is holding a placement actor, it is the time to add the renderer to actor.
285 Actor actor = mPlacementActor.GetHandle();
288 actor.AddRenderer( mImpl->mRenderer );
289 // reset the weak handle so that the renderer only get added to actor once
290 mPlacementActor.Reset();
293 // Svg loaded and ready to display
294 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
296 else if( !mParsedImage )
298 ResourceReady( Toolkit::Visual::ResourceStatus::FAILED );
302 void SvgVisual::OnSetTransform()
304 Vector2 visualSize = mImpl->mTransform.GetVisualSize( mImpl->mControlSize );
308 if( visualSize != mVisualSize )
310 AddRasterizationTask( visualSize );
311 mVisualSize = visualSize;
317 mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
321 bool SvgVisual::IsResourceReady() const
323 return ( mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY ||
324 mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::FAILED );
327 } // namespace Internal
329 } // namespace Toolkit