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 )
59 return new SvgVisual( factoryCache );
62 SvgVisualPtr SvgVisual::New( VisualFactoryCache& factoryCache, const std::string& imageUrl, ImageDimensions size )
64 SvgVisual* svgVisual = new SvgVisual( factoryCache );
65 svgVisual->ParseFromUrl( imageUrl, size );
69 SvgVisual::SvgVisual( VisualFactoryCache& factoryCache )
70 : Visual::Base( factoryCache ),
71 mAtlasRect( FULL_TEXTURE_RECT ),
76 // the rasterized image is with pre-multiplied alpha format
77 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
80 SvgVisual::~SvgVisual()
84 nsvgDelete( mParsedImage );
88 void SvgVisual::DoSetProperties( const Property::Map& propertyMap )
90 Property::Value* imageURLValue = propertyMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
94 if( imageURLValue->Get( imageUrl ) )
96 ParseFromUrl( imageUrl );
100 DALI_LOG_ERROR( "The property '%s' is not a string\n", IMAGE_URL_NAME );
105 void SvgVisual::DoSetOnStage( Actor& actor )
107 Shader shader = ImageVisual::GetImageShader( mFactoryCache, true, true );
108 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
111 geometry = mFactoryCache.CreateQuadGeometry();
112 mFactoryCache.SaveGeometry( VisualFactoryCache::QUAD_GEOMETRY, geometry );
114 TextureSet textureSet = TextureSet::New();
115 mImpl->mRenderer = Renderer::New( geometry, shader );
116 mImpl->mRenderer.SetTextures( textureSet );
118 if( mImpl->mSize != Vector2::ZERO && mParsedImage )
120 AddRasterizationTask( mImpl->mSize );
123 // Hold the weak handle of the placement actor and delay the adding of renderer until the svg rasterization is finished.
124 mPlacementActor = actor;
127 void SvgVisual::DoSetOffStage( Actor& actor )
129 mFactoryCache.GetSVGRasterizationThread()->RemoveTask( this );
131 actor.RemoveRenderer( mImpl->mRenderer );
132 mImpl->mRenderer.Reset();
133 mPlacementActor.Reset();
136 void SvgVisual::GetNaturalSize( Vector2& naturalSize ) const
140 naturalSize.x = mParsedImage->width;
141 naturalSize.y = mParsedImage->height;
145 naturalSize = Vector2::ZERO;
149 void SvgVisual::SetSize( const Vector2& size )
151 if(mImpl->mSize != size && mParsedImage && IsOnStage() )
153 AddRasterizationTask( size );
158 void SvgVisual::DoCreatePropertyMap( Property::Map& map ) const
161 map.Insert( Toolkit::VisualProperty::TYPE, Toolkit::Visual::IMAGE );
162 if( !mImageUrl.empty() )
164 map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl );
168 void SvgVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
173 Dali::Property::Value SvgVisual::DoGetProperty( Dali::Property::Index index )
176 return Dali::Property::Value();
179 void SvgVisual::ParseFromUrl( const std::string& imageUrl, ImageDimensions size )
181 mImageUrl = imageUrl;
183 Vector2 dpi = Stage::GetCurrent().GetDpi();
184 float meanDpi = (dpi.height + dpi.width) * 0.5f;
185 mParsedImage = nsvgParseFromFile( imageUrl.c_str(), UNITS, meanDpi );
187 if( size.GetWidth() != 0u && size.GetHeight() != 0u)
189 mImpl->mSize.x = size.GetWidth();
190 mImpl->mSize.y = size.GetHeight();
194 void SvgVisual::AddRasterizationTask( const Vector2& size )
196 if( mImpl->mRenderer && mParsedImage )
198 unsigned int width = static_cast<unsigned int>(size.width);
199 unsigned int height = static_cast<unsigned int>( size.height );
200 BufferImage image = BufferImage::New( width, height, Pixel::RGBA8888);
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( mAtlasRect != FULL_TEXTURE_RECT )
214 mFactoryCache.GetAtlasManager()->Remove( currentTextureSet, mAtlasRect );
218 TextureSet textureSet = mFactoryCache.GetAtlasManager()->Add(atlasRect, rasterizedPixelData );
219 if( textureSet ) // atlasing
221 if( textureSet != currentTextureSet )
223 mImpl->mRenderer.SetTextures( textureSet );
225 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
226 mAtlasRect = atlasRect;
227 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
231 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888,
232 rasterizedPixelData.GetWidth(), rasterizedPixelData.GetHeight() );
233 texture.Upload( rasterizedPixelData );
234 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
236 if( mAtlasRect == FULL_TEXTURE_RECT )
238 textureSet = currentTextureSet;
242 textureSet = TextureSet::New();
243 mImpl->mRenderer.SetTextures( textureSet );
245 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, FULL_TEXTURE_RECT );
246 mAtlasRect = FULL_TEXTURE_RECT;
251 textureSet.SetTexture( 0, texture );
255 // Rasterized pixels are uploaded to texture. If weak handle is holding a placement actor, it is the time to add the renderer to actor.
256 Actor actor = mPlacementActor.GetHandle();
259 actor.AddRenderer( mImpl->mRenderer );
260 // reset the weak handle so that the renderer only get added to actor once
261 mPlacementActor.Reset();
267 } // namespace Internal
269 } // namespace Toolkit