2 * Copyright (c) 2016 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/atlas.h>
26 #include <dali/devel-api/images/texture-set-image.h>
27 #include <dali/integration-api/debug.h>
30 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
31 #include <dali-toolkit/devel-api/visual-factory/devel-visual-properties.h>
32 #include <dali-toolkit/third-party/nanosvg/nanosvg.h>
33 #include <dali-toolkit/internal/visuals/svg/svg-rasterize-thread.h>
34 #include <dali-toolkit/internal/visuals/image/image-visual.h>
35 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
36 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
37 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
38 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
43 const char * const UNITS("px");
45 const Dali::Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
57 SvgVisualPtr SvgVisual::New( VisualFactoryCache& factoryCache, const std::string& imageUrl, ImageDimensions size )
59 SvgVisual* svgVisual = new SvgVisual( factoryCache );
60 svgVisual->ParseFromUrl( imageUrl, size );
64 SvgVisual::SvgVisual( VisualFactoryCache& factoryCache )
65 : Visual::Base( factoryCache ),
66 mAtlasRect( FULL_TEXTURE_RECT ),
71 // the rasterized image is with pre-multiplied alpha format
72 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
75 SvgVisual::~SvgVisual()
79 nsvgDelete( mParsedImage );
83 void SvgVisual::DoSetProperties( const Property::Map& propertyMap )
85 // url already passed in from constructor
88 void SvgVisual::DoSetOnStage( Actor& actor )
90 Shader shader = ImageVisual::GetImageShader( mFactoryCache, true, true );
91 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
94 geometry = mFactoryCache.CreateQuadGeometry();
95 mFactoryCache.SaveGeometry( VisualFactoryCache::QUAD_GEOMETRY, geometry );
97 TextureSet textureSet = TextureSet::New();
98 mImpl->mRenderer = Renderer::New( geometry, shader );
99 mImpl->mRenderer.SetTextures( textureSet );
101 if( mImpl->mSize != Vector2::ZERO && mParsedImage )
103 AddRasterizationTask( mImpl->mSize );
106 // Hold the weak handle of the placement actor and delay the adding of renderer until the svg rasterization is finished.
107 mPlacementActor = actor;
110 void SvgVisual::DoSetOffStage( Actor& actor )
112 mFactoryCache.GetSVGRasterizationThread()->RemoveTask( this );
114 actor.RemoveRenderer( mImpl->mRenderer );
115 mImpl->mRenderer.Reset();
116 mPlacementActor.Reset();
119 void SvgVisual::GetNaturalSize( Vector2& naturalSize ) const
123 naturalSize.x = mParsedImage->width;
124 naturalSize.y = mParsedImage->height;
128 naturalSize = Vector2::ZERO;
132 void SvgVisual::SetSize( const Vector2& size )
134 if(mImpl->mSize != size && mParsedImage && IsOnStage() )
136 AddRasterizationTask( size );
141 void SvgVisual::DoCreatePropertyMap( Property::Map& map ) const
144 map.Insert( Toolkit::VisualProperty::TYPE, Toolkit::Visual::IMAGE );
145 if( !mImageUrl.empty() )
147 map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl );
151 void SvgVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
156 Dali::Property::Value SvgVisual::DoGetProperty( Dali::Property::Index index )
159 return Dali::Property::Value();
162 void SvgVisual::ParseFromUrl( const std::string& imageUrl, ImageDimensions size )
164 mImageUrl = imageUrl;
166 Vector2 dpi = Stage::GetCurrent().GetDpi();
167 float meanDpi = (dpi.height + dpi.width) * 0.5f;
168 mParsedImage = nsvgParseFromFile( imageUrl.c_str(), UNITS, meanDpi );
170 if( size.GetWidth() != 0u && size.GetHeight() != 0u)
172 mImpl->mSize.x = size.GetWidth();
173 mImpl->mSize.y = size.GetHeight();
177 void SvgVisual::AddRasterizationTask( const Vector2& size )
179 if( mImpl->mRenderer && mParsedImage )
181 unsigned int width = static_cast<unsigned int>(size.width);
182 unsigned int height = static_cast<unsigned int>( size.height );
183 BufferImage image = BufferImage::New( width, height, Pixel::RGBA8888);
185 RasterizingTaskPtr newTask = new RasterizingTask( this, mParsedImage, width, height );
186 mFactoryCache.GetSVGRasterizationThread()->AddTask( newTask );
190 void SvgVisual::ApplyRasterizedImage( PixelData rasterizedPixelData )
194 TextureSet currentTextureSet = mImpl->mRenderer.GetTextures();
195 if( mAtlasRect != FULL_TEXTURE_RECT )
197 mFactoryCache.GetAtlasManager()->Remove( currentTextureSet, mAtlasRect );
201 TextureSet textureSet = mFactoryCache.GetAtlasManager()->Add(atlasRect, rasterizedPixelData );
202 if( textureSet ) // atlasing
204 if( textureSet != currentTextureSet )
206 mImpl->mRenderer.SetTextures( textureSet );
208 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
209 mAtlasRect = atlasRect;
210 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
214 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888,
215 rasterizedPixelData.GetWidth(), rasterizedPixelData.GetHeight() );
216 texture.Upload( rasterizedPixelData );
217 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
219 if( mAtlasRect == FULL_TEXTURE_RECT )
221 textureSet = currentTextureSet;
225 textureSet = TextureSet::New();
226 mImpl->mRenderer.SetTextures( textureSet );
228 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, FULL_TEXTURE_RECT );
229 mAtlasRect = FULL_TEXTURE_RECT;
234 textureSet.SetTexture( 0, texture );
238 // Rasterized pixels are uploaded to texture. If weak handle is holding a placement actor, it is the time to add the renderer to actor.
239 Actor actor = mPlacementActor.GetHandle();
242 actor.AddRenderer( mImpl->mRenderer );
243 // reset the weak handle so that the renderer only get added to actor once
244 mPlacementActor.Reset();
250 } // namespace Internal
252 } // namespace Toolkit