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/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");
44 const Dali::Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
56 SvgVisual::SvgVisual( VisualFactoryCache& factoryCache )
57 : Visual::Base( factoryCache ),
58 mAtlasRect( FULL_TEXTURE_RECT ),
61 // the rasterized image is with pre-multiplied alpha format
62 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
65 SvgVisual::~SvgVisual()
69 nsvgDelete( mParsedImage );
73 void SvgVisual::DoInitialize( Actor& actor, const Property::Map& propertyMap )
75 Property::Value* imageURLValue = propertyMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
79 if( imageURLValue->Get( imageUrl ) )
85 DALI_LOG_ERROR( "The property '%s' is not a string\n", IMAGE_URL_NAME );
90 void SvgVisual::DoSetOnStage( Actor& actor )
92 Shader shader = ImageVisual::GetImageShader( mFactoryCache, true, true );
93 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
96 geometry = mFactoryCache.CreateQuadGeometry();
97 mFactoryCache.SaveGeometry( VisualFactoryCache::QUAD_GEOMETRY, geometry );
99 TextureSet textureSet = TextureSet::New();
100 mImpl->mRenderer = Renderer::New( geometry, shader );
101 mImpl->mRenderer.SetTextures( textureSet );
103 if( mImpl->mSize != Vector2::ZERO && mParsedImage )
105 AddRasterizationTask( mImpl->mSize );
108 // Hold the weak handle of the placement actor and delay the adding of renderer until the svg rasterization is finished.
109 mPlacementActor = actor;
112 void SvgVisual::DoSetOffStage( Actor& actor )
114 mFactoryCache.GetSVGRasterizationThread()->RemoveTask( this );
116 actor.RemoveRenderer( mImpl->mRenderer );
117 mImpl->mRenderer.Reset();
118 mPlacementActor.Reset();
121 void SvgVisual::GetNaturalSize( Vector2& naturalSize ) const
125 naturalSize.x = mParsedImage->width;
126 naturalSize.y = mParsedImage->height;
130 naturalSize = Vector2::ZERO;
134 void SvgVisual::SetSize( const Vector2& size )
136 if(mImpl->mSize != size && mParsedImage && GetIsOnStage() )
138 AddRasterizationTask( size );
143 void SvgVisual::DoCreatePropertyMap( Property::Map& map ) const
146 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
147 if( !mImageUrl.empty() )
149 map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl );
153 void SvgVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
158 Dali::Property::Value SvgVisual::DoGetProperty( Dali::Property::Index index )
161 return Dali::Property::Value();
164 void SvgVisual::SetImage( const std::string& imageUrl, ImageDimensions size )
166 if( mImageUrl != imageUrl )
168 mImageUrl = imageUrl;
170 NSVGimage* parsedImageOld = mParsedImage;
172 Vector2 dpi = Stage::GetCurrent().GetDpi();
173 float meanDpi = (dpi.height + dpi.width) * 0.5f;
174 mParsedImage = nsvgParseFromFile(mImageUrl.c_str(), UNITS, meanDpi);
176 if( size.GetWidth() != 0u && size.GetHeight() != 0u)
178 mImpl->mSize.x = size.GetWidth();
179 mImpl->mSize.y = size.GetHeight();
182 if( mImpl->mSize != Vector2::ZERO && GetIsOnStage() )
184 AddRasterizationTask( mImpl->mSize );
187 mFactoryCache.GetSVGRasterizationThread()->DeleteImage( parsedImageOld );
191 void SvgVisual::AddRasterizationTask( const Vector2& size )
193 if( mImpl->mRenderer && mParsedImage )
195 unsigned int width = static_cast<unsigned int>(size.width);
196 unsigned int height = static_cast<unsigned int>( size.height );
197 BufferImage image = BufferImage::New( width, height, Pixel::RGBA8888);
199 RasterizingTaskPtr newTask = new RasterizingTask( this, mParsedImage, width, height );
200 mFactoryCache.GetSVGRasterizationThread()->AddTask( newTask );
204 void SvgVisual::ApplyRasterizedImage( PixelData rasterizedPixelData )
208 TextureSet currentTextureSet = mImpl->mRenderer.GetTextures();
209 if( mAtlasRect != FULL_TEXTURE_RECT )
211 mFactoryCache.GetAtlasManager()->Remove( currentTextureSet, mAtlasRect );
215 TextureSet textureSet = mFactoryCache.GetAtlasManager()->Add(atlasRect, rasterizedPixelData );
216 if( textureSet ) // atlasing
218 if( textureSet != currentTextureSet )
220 mImpl->mRenderer.SetTextures( textureSet );
222 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
223 mAtlasRect = atlasRect;
224 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
228 Atlas texture = Atlas::New( rasterizedPixelData.GetWidth(), rasterizedPixelData.GetHeight() );
229 texture.Upload( rasterizedPixelData, 0, 0 );
230 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
232 if( mAtlasRect == FULL_TEXTURE_RECT )
234 textureSet = currentTextureSet;
238 textureSet = TextureSet::New();
239 mImpl->mRenderer.SetTextures( textureSet );
241 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, FULL_TEXTURE_RECT );
242 mAtlasRect = FULL_TEXTURE_RECT;
247 TextureSetImage( textureSet, 0u, texture );
251 // Rasterized pixels are uploaded to texture. If weak handle is holding a placement actor, it is the time to add the renderer to actor.
252 Actor actor = mPlacementActor.GetHandle();
255 actor.AddRenderer( mImpl->mRenderer );
256 // reset the weak handle so that the renderer only get added to actor once
257 mPlacementActor.Reset();
263 } // namespace Internal
265 } // namespace Toolkit