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 );
109 void SvgVisual::DoSetOffStage( Actor& actor )
111 mFactoryCache.GetSVGRasterizationThread()->RemoveTask( this );
113 actor.RemoveRenderer( mImpl->mRenderer );
114 mImpl->mRenderer.Reset();
117 void SvgVisual::GetNaturalSize( Vector2& naturalSize ) const
121 naturalSize.x = mParsedImage->width;
122 naturalSize.y = mParsedImage->height;
126 naturalSize = Vector2::ZERO;
130 void SvgVisual::SetSize( const Vector2& size )
132 if(mImpl->mSize != size && mParsedImage && GetIsOnStage() )
134 AddRasterizationTask( size );
139 void SvgVisual::DoCreatePropertyMap( Property::Map& map ) const
142 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
143 if( !mImageUrl.empty() )
145 map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl );
149 void SvgVisual::SetImage( const std::string& imageUrl, ImageDimensions size )
151 if( mImageUrl != imageUrl )
153 mImageUrl = imageUrl;
155 NSVGimage* parsedImageOld = mParsedImage;
157 Vector2 dpi = Stage::GetCurrent().GetDpi();
158 float meanDpi = (dpi.height + dpi.width) * 0.5f;
159 mParsedImage = nsvgParseFromFile(mImageUrl.c_str(), UNITS, meanDpi);
161 if( size.GetWidth() != 0u && size.GetHeight() != 0u)
163 mImpl->mSize.x = size.GetWidth();
164 mImpl->mSize.y = size.GetHeight();
167 if( mImpl->mSize != Vector2::ZERO && GetIsOnStage() )
169 AddRasterizationTask( mImpl->mSize );
172 mFactoryCache.GetSVGRasterizationThread()->DeleteImage( parsedImageOld );
176 void SvgVisual::AddRasterizationTask( const Vector2& size )
178 if( mImpl->mRenderer && mParsedImage )
180 unsigned int width = static_cast<unsigned int>(size.width);
181 unsigned int height = static_cast<unsigned int>( size.height );
182 BufferImage image = BufferImage::New( width, height, Pixel::RGBA8888);
184 RasterizingTaskPtr newTask = new RasterizingTask( this, mParsedImage, width, height );
185 mFactoryCache.GetSVGRasterizationThread()->AddTask( newTask );
189 void SvgVisual::ApplyRasterizedImage( PixelData rasterizedPixelData )
193 TextureSet currentTextureSet = mImpl->mRenderer.GetTextures();
194 if( mAtlasRect != FULL_TEXTURE_RECT )
196 mFactoryCache.GetAtlasManager()->Remove( currentTextureSet, mAtlasRect );
200 TextureSet textureSet = mFactoryCache.GetAtlasManager()->Add(atlasRect, rasterizedPixelData );
201 if( textureSet ) // atlasing
203 if( textureSet != currentTextureSet )
205 mImpl->mRenderer.SetTextures( textureSet );
207 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
208 mAtlasRect = atlasRect;
209 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
213 Atlas texture = Atlas::New( rasterizedPixelData.GetWidth(), rasterizedPixelData.GetHeight() );
214 texture.Upload( rasterizedPixelData, 0, 0 );
215 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
217 if( mAtlasRect == FULL_TEXTURE_RECT )
219 textureSet = currentTextureSet;
223 textureSet = TextureSet::New();
224 mImpl->mRenderer.SetTextures( textureSet );
226 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, FULL_TEXTURE_RECT );
227 mAtlasRect = FULL_TEXTURE_RECT;
232 TextureSetImage( textureSet, 0u, texture );
240 } // namespace Internal
242 } // namespace Toolkit