2 * Copyright (c) 2017 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/public-api/images/buffer-image.h>
23 #include <dali/public-api/common/stage.h>
24 #include <dali/public-api/math/vector4.h>
25 #include <dali/devel-api/images/texture-set-image.h>
26 #include <dali/integration-api/debug.h>
29 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
30 #include <dali-toolkit/public-api/visuals/visual-properties.h>
31 #include <dali-toolkit/third-party/nanosvg/nanosvg.h>
32 #include <dali-toolkit/internal/visuals/svg/svg-rasterize-thread.h>
33 #include <dali-toolkit/internal/visuals/image/image-visual.h>
34 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
35 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
36 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
37 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
42 const char * const UNITS("px");
45 const char * const IMAGE_ATLASING( "atlasing" );
47 const Dali::Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
59 SvgVisualPtr SvgVisual::New( VisualFactoryCache& factoryCache, const VisualUrl& imageUrl, const Property::Map& properties )
61 SvgVisualPtr svgVisual( new SvgVisual( factoryCache ) );
62 svgVisual->ParseFromUrl( imageUrl );
63 svgVisual->SetProperties( properties );
68 SvgVisualPtr SvgVisual::New( VisualFactoryCache& factoryCache, const VisualUrl& imageUrl )
70 SvgVisualPtr svgVisual( new SvgVisual( factoryCache ) );
71 svgVisual->ParseFromUrl( imageUrl );
76 SvgVisual::SvgVisual( VisualFactoryCache& factoryCache )
77 : Visual::Base( factoryCache ),
78 mAtlasRect( FULL_TEXTURE_RECT ),
82 mVisualSize(Vector2::ZERO),
83 mAttemptAtlasing( false )
85 // the rasterized image is with pre-multiplied alpha format
86 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
89 SvgVisual::~SvgVisual()
93 nsvgDelete( mParsedImage );
97 void SvgVisual::DoSetProperties( const Property::Map& propertyMap )
99 // url already passed in from constructor
100 for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
102 KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
103 if( keyValue.first.type == Property::Key::INDEX )
105 DoSetProperty( keyValue.first.indexKey, keyValue.second );
107 else if( keyValue.first == IMAGE_ATLASING )
109 DoSetProperty( Toolkit::ImageVisual::Property::ATLASING, keyValue.second );
114 void SvgVisual::DoSetProperty( Property::Index index, const Property::Value& value )
118 case Toolkit::ImageVisual::Property::ATLASING:
120 value.Get( mAttemptAtlasing );
126 void SvgVisual::DoSetOnStage( Actor& actor )
128 Shader shader = ImageVisual::GetImageShader( mFactoryCache, mAttemptAtlasing, true );
129 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
130 TextureSet textureSet = TextureSet::New();
131 mImpl->mRenderer = Renderer::New( geometry, shader );
132 mImpl->mRenderer.SetTextures( textureSet );
134 // Register transform properties
135 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
137 // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
139 // Hold the weak handle of the placement actor and delay the adding of renderer until the svg rasterization is finished.
140 mPlacementActor = actor;
142 // SVG visual needs it's size set before it can be rasterized hence set ResourceReady once on stage
143 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
146 void SvgVisual::DoSetOffStage( Actor& actor )
148 mFactoryCache.GetSVGRasterizationThread()->RemoveTask( this );
150 actor.RemoveRenderer( mImpl->mRenderer );
151 mImpl->mRenderer.Reset();
152 mPlacementActor.Reset();
155 void SvgVisual::GetNaturalSize( Vector2& naturalSize )
159 naturalSize.x = mParsedImage->width;
160 naturalSize.y = mParsedImage->height;
164 naturalSize = Vector2::ZERO;
168 void SvgVisual::DoCreatePropertyMap( Property::Map& map ) const
171 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::SVG );
172 if( mImageUrl.IsValid() )
174 map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl() );
175 map.Insert( Toolkit::ImageVisual::Property::ATLASING, mAttemptAtlasing );
179 void SvgVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
184 void SvgVisual::ParseFromUrl( const VisualUrl& imageUrl )
186 mImageUrl = imageUrl;
187 if( mImageUrl.IsLocalResource() )
189 Vector2 dpi = Stage::GetCurrent().GetDpi();
190 float meanDpi = (dpi.height + dpi.width) * 0.5f;
191 mParsedImage = nsvgParseFromFile( mImageUrl.GetUrl().c_str(), UNITS, meanDpi );
195 void SvgVisual::AddRasterizationTask( const Vector2& size )
197 if( mImpl->mRenderer && mParsedImage )
199 unsigned int width = static_cast<unsigned int>(size.width);
200 unsigned int height = static_cast<unsigned int>( size.height );
202 RasterizingTaskPtr newTask = new RasterizingTask( this, mParsedImage, width, height );
203 mFactoryCache.GetSVGRasterizationThread()->AddTask( newTask );
207 void SvgVisual::ApplyRasterizedImage( PixelData rasterizedPixelData )
211 TextureSet currentTextureSet = mImpl->mRenderer.GetTextures();
212 if( mImpl->mFlags |= Impl::IS_ATLASING_APPLIED )
214 mFactoryCache.GetAtlasManager()->Remove( currentTextureSet, mAtlasRect );
217 TextureSet textureSet;
219 if( mAttemptAtlasing )
222 textureSet = mFactoryCache.GetAtlasManager()->Add(atlasRect, rasterizedPixelData );
223 if( textureSet ) // atlasing
225 if( textureSet != currentTextureSet )
227 mImpl->mRenderer.SetTextures( textureSet );
229 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
230 mAtlasRect = atlasRect;
231 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
235 if( !textureSet ) // no atlasing - mAttemptAtlasing is false or adding to atlas is failed
237 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888,
238 rasterizedPixelData.GetWidth(), rasterizedPixelData.GetHeight() );
239 texture.Upload( rasterizedPixelData );
240 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
242 if( mAtlasRect == FULL_TEXTURE_RECT )
244 textureSet = currentTextureSet;
248 textureSet = TextureSet::New();
249 mImpl->mRenderer.SetTextures( textureSet );
251 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, FULL_TEXTURE_RECT );
252 mAtlasRect = FULL_TEXTURE_RECT;
257 textureSet.SetTexture( 0, texture );
261 // Rasterized pixels are uploaded to texture. If weak handle is holding a placement actor, it is the time to add the renderer to actor.
262 Actor actor = mPlacementActor.GetHandle();
265 actor.AddRenderer( mImpl->mRenderer );
266 // reset the weak handle so that the renderer only get added to actor once
267 mPlacementActor.Reset();
270 // Svg loaded and ready to display
271 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
275 void SvgVisual::OnSetTransform()
277 Vector2 visualSize = mImpl->mTransform.GetVisualSize( mImpl->mControlSize );
279 if( mParsedImage && IsOnStage() )
281 if( visualSize != mVisualSize )
283 AddRasterizationTask( visualSize );
284 mVisualSize = visualSize;
289 } // namespace Internal
291 } // namespace Toolkit