2 * Copyright (c) 2018 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>
30 #include <dali/public-api/common/stage.h>
35 const char * const IMAGE_ATLASING( "atlasing" );
37 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 ) );
52 svgVisual->SetProperties( properties );
57 SvgVisualPtr SvgVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
59 SvgVisualPtr svgVisual( new SvgVisual( factoryCache, shaderFactory, imageUrl ) );
64 SvgVisual::SvgVisual( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
65 : Visual::Base( factoryCache, Visual::FittingMode::FILL ),
66 mImageVisualShaderFactory( shaderFactory ),
67 mAtlasRect( FULL_TEXTURE_RECT ),
68 mImageUrl( imageUrl ),
71 mVisualSize(Vector2::ZERO),
72 mAttemptAtlasing( false )
74 // the rasterized image is with pre-multiplied alpha format
75 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
78 SvgVisual::~SvgVisual()
82 nsvgDelete( mParsedImage );
86 void SvgVisual::DoSetProperties( const Property::Map& propertyMap )
88 // url already passed in from constructor
89 for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
91 KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
92 if( keyValue.first.type == Property::Key::INDEX )
94 DoSetProperty( keyValue.first.indexKey, keyValue.second );
96 else if( keyValue.first == IMAGE_ATLASING )
98 DoSetProperty( Toolkit::ImageVisual::Property::ATLASING, keyValue.second );
103 void SvgVisual::DoSetProperty( Property::Index index, const Property::Value& value )
107 case Toolkit::ImageVisual::Property::ATLASING:
109 value.Get( mAttemptAtlasing );
115 void SvgVisual::DoSetOnStage( Actor& actor )
118 if( !mImpl->mCustomShader )
120 shader = mImageVisualShaderFactory.GetShader( mFactoryCache, mAttemptAtlasing, true );
124 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource() : mImpl->mCustomShader->mVertexShader,
125 mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource() : mImpl->mCustomShader->mFragmentShader,
126 mImpl->mCustomShader->mHints );
128 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
131 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
132 TextureSet textureSet = TextureSet::New();
133 mImpl->mRenderer = Renderer::New( geometry, shader );
134 mImpl->mRenderer.SetTextures( textureSet );
136 // Register transform properties
137 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
139 // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
141 // Hold the weak handle of the placement actor and delay the adding of renderer until the svg rasterization is finished.
142 mPlacementActor = actor;
144 // SVG visual needs it's size set before it can be rasterized hence set ResourceReady once on stage
145 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
148 void SvgVisual::DoSetOffStage( Actor& actor )
150 mFactoryCache.GetSVGRasterizationThread()->RemoveTask( this );
152 actor.RemoveRenderer( mImpl->mRenderer );
153 mImpl->mRenderer.Reset();
154 mPlacementActor.Reset();
156 // Reset the visual size to zero so that when adding the actor back to stage the SVG rasterization is forced
157 mVisualSize = Vector2::ZERO;
160 void SvgVisual::GetNaturalSize( Vector2& naturalSize )
164 naturalSize.x = mParsedImage->width;
165 naturalSize.y = mParsedImage->height;
169 naturalSize = Vector2::ZERO;
173 void SvgVisual::DoCreatePropertyMap( Property::Map& map ) const
176 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::SVG );
177 if( mImageUrl.IsValid() )
179 map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl() );
180 map.Insert( Toolkit::ImageVisual::Property::ATLASING, mAttemptAtlasing );
184 void SvgVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
189 void SvgVisual::AddRasterizationTask( const Vector2& size )
191 if( mImpl->mRenderer )
193 unsigned int width = static_cast<unsigned int>(size.width);
194 unsigned int height = static_cast<unsigned int>( size.height );
196 Vector2 dpi = Stage::GetCurrent().GetDpi();
197 float meanDpi = ( dpi.height + dpi.width ) * 0.5f;
199 RasterizingTaskPtr newTask = new RasterizingTask( this, mParsedImage, mImageUrl, meanDpi, width, height );
200 mFactoryCache.GetSVGRasterizationThread()->AddTask( newTask );
204 void SvgVisual::ApplyRasterizedImage( NSVGimage* parsedSvg, PixelData rasterizedPixelData )
206 if( mParsedImage == NULL)
208 mParsedImage = parsedSvg;
213 TextureSet currentTextureSet = mImpl->mRenderer.GetTextures();
214 if( mImpl->mFlags |= Impl::IS_ATLASING_APPLIED )
216 mFactoryCache.GetAtlasManager()->Remove( currentTextureSet, mAtlasRect );
219 TextureSet textureSet;
221 if( mAttemptAtlasing && !mImpl->mCustomShader )
224 textureSet = mFactoryCache.GetAtlasManager()->Add(atlasRect, rasterizedPixelData );
225 if( textureSet ) // atlasing
227 if( textureSet != currentTextureSet )
229 mImpl->mRenderer.SetTextures( textureSet );
231 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
232 mAtlasRect = atlasRect;
233 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
237 if( !textureSet ) // no atlasing - mAttemptAtlasing is false or adding to atlas is failed
239 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888,
240 rasterizedPixelData.GetWidth(), rasterizedPixelData.GetHeight() );
241 texture.Upload( rasterizedPixelData );
242 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
244 if( mAtlasRect == FULL_TEXTURE_RECT )
246 textureSet = currentTextureSet;
250 textureSet = TextureSet::New();
251 mImpl->mRenderer.SetTextures( textureSet );
253 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, FULL_TEXTURE_RECT );
254 mAtlasRect = FULL_TEXTURE_RECT;
259 textureSet.SetTexture( 0, texture );
263 // Rasterized pixels are uploaded to texture. If weak handle is holding a placement actor, it is the time to add the renderer to actor.
264 Actor actor = mPlacementActor.GetHandle();
267 actor.AddRenderer( mImpl->mRenderer );
268 // reset the weak handle so that the renderer only get added to actor once
269 mPlacementActor.Reset();
272 // Svg loaded and ready to display
273 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
277 void SvgVisual::OnSetTransform()
279 Vector2 visualSize = mImpl->mTransform.GetVisualSize( mImpl->mControlSize );
283 if( visualSize != mVisualSize )
285 AddRasterizationTask( visualSize );
286 mVisualSize = visualSize;
292 mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
296 } // namespace Internal
298 } // namespace Toolkit