X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Finternal%2Fvisuals%2Fsvg%2Fsvg-visual.cpp;h=051affee8cc3191cb1cb20258a1c8a14dc976f7f;hp=e7c7aa8e674b541f3b8928954db38ba7bf53ae49;hb=5a6809a40da6a50d2edce9202466aa289ad96162;hpb=83066ea18044c04fbbf0378aa6863416c532e10b diff --git a/dali-toolkit/internal/visuals/svg/svg-visual.cpp b/dali-toolkit/internal/visuals/svg/svg-visual.cpp index e7c7aa8..051affe 100644 --- a/dali-toolkit/internal/visuals/svg/svg-visual.cpp +++ b/dali-toolkit/internal/visuals/svg/svg-visual.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * Copyright (c) 2020 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,55 +18,63 @@ // CLASS HEADER #include "svg-visual.h" -// EXTERNAL INCLUDES -#include -#include -#include -#include -#include -#include - // INTERNAL INCLUDES -#include -#include #include #include -#include #include -#include #include #include +#include +#include + +// EXTERNAL INCLUDES +#include +namespace Dali +{ + +namespace Toolkit +{ + +namespace Internal +{ namespace { +// property name const char * const UNITS("px"); +const char * const IMAGE_ATLASING( "atlasing" ); + const Dali::Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f); } -namespace Dali +SvgVisualPtr SvgVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties ) { + SvgVisualPtr svgVisual( new SvgVisual( factoryCache, shaderFactory, imageUrl ) ); + svgVisual->ParseFromUrl( imageUrl ); + svgVisual->SetProperties( properties ); -namespace Toolkit -{ + return svgVisual; +} -namespace Internal +SvgVisualPtr SvgVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl ) { + SvgVisualPtr svgVisual( new SvgVisual( factoryCache, shaderFactory, imageUrl ) ); + svgVisual->ParseFromUrl( imageUrl ); -SvgVisualPtr SvgVisual::New( VisualFactoryCache& factoryCache, const std::string& imageUrl, ImageDimensions size ) -{ - SvgVisual* svgVisual = new SvgVisual( factoryCache ); - svgVisual->ParseFromUrl( imageUrl, size ); return svgVisual; } -SvgVisual::SvgVisual( VisualFactoryCache& factoryCache ) -: Visual::Base( factoryCache ), +SvgVisual::SvgVisual( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl ) +: Visual::Base( factoryCache, Visual::FittingMode::FILL ), + mImageVisualShaderFactory( shaderFactory ), mAtlasRect( FULL_TEXTURE_RECT ), - mImageUrl(), + mImageUrl( imageUrl ), mParsedImage( NULL ), - mPlacementActor() + mPlacementActor(), + mVisualSize(Vector2::ZERO), + mAttemptAtlasing( false ) { // the rasterized image is with pre-multiplied alpha format mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA; @@ -83,28 +91,63 @@ SvgVisual::~SvgVisual() void SvgVisual::DoSetProperties( const Property::Map& propertyMap ) { // url already passed in from constructor + for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter ) + { + KeyValuePair keyValue = propertyMap.GetKeyValue( iter ); + if( keyValue.first.type == Property::Key::INDEX ) + { + DoSetProperty( keyValue.first.indexKey, keyValue.second ); + } + else if( keyValue.first == IMAGE_ATLASING ) + { + DoSetProperty( Toolkit::ImageVisual::Property::ATLASING, keyValue.second ); + } + } +} + +void SvgVisual::DoSetProperty( Property::Index index, const Property::Value& value ) +{ + switch( index ) + { + case Toolkit::ImageVisual::Property::ATLASING: + { + value.Get( mAttemptAtlasing ); + break; + } + } } void SvgVisual::DoSetOnStage( Actor& actor ) { - Shader shader = ImageVisual::GetImageShader( mFactoryCache, true, true ); - Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY ); - if( !geometry ) + Shader shader; + if( !mImpl->mCustomShader ) + { + shader = mImageVisualShaderFactory.GetShader( mFactoryCache, mAttemptAtlasing, true ); + } + else { - geometry = mFactoryCache.CreateQuadGeometry(); - mFactoryCache.SaveGeometry( VisualFactoryCache::QUAD_GEOMETRY, geometry ); + shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource() : mImpl->mCustomShader->mVertexShader, + mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource() : mImpl->mCustomShader->mFragmentShader, + mImpl->mCustomShader->mHints ); + + shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT ); } + + Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY ); TextureSet textureSet = TextureSet::New(); mImpl->mRenderer = Renderer::New( geometry, shader ); mImpl->mRenderer.SetTextures( textureSet ); - if( mImpl->mSize != Vector2::ZERO && mParsedImage ) - { - AddRasterizationTask( mImpl->mSize ); - } + // Register transform properties + mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT ); + + // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm) // Hold the weak handle of the placement actor and delay the adding of renderer until the svg rasterization is finished. mPlacementActor = actor; + + // SVG visual needs it's size set before it can be rasterized hence set ResourceReady once on stage + ResourceReady( Toolkit::Visual::ResourceStatus::READY ); } void SvgVisual::DoSetOffStage( Actor& actor ) @@ -114,6 +157,9 @@ void SvgVisual::DoSetOffStage( Actor& actor ) actor.RemoveRenderer( mImpl->mRenderer ); mImpl->mRenderer.Reset(); mPlacementActor.Reset(); + + // Reset the visual size to zero so that when adding the actor back to stage the SVG rasterization is forced + mVisualSize = Vector2::ZERO; } void SvgVisual::GetNaturalSize( Vector2& naturalSize ) @@ -129,87 +175,82 @@ void SvgVisual::GetNaturalSize( Vector2& naturalSize ) } } -void SvgVisual::SetSize( const Vector2& size ) -{ - if(mImpl->mSize != size && mParsedImage && IsOnStage() ) - { - AddRasterizationTask( size ); - } - mImpl->mSize = size; -} - void SvgVisual::DoCreatePropertyMap( Property::Map& map ) const { map.Clear(); - map.Insert( Toolkit::VisualProperty::TYPE, Toolkit::Visual::IMAGE ); - if( !mImageUrl.empty() ) + map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::SVG ); + if( mImageUrl.IsValid() ) { - map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl ); + map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl() ); + map.Insert( Toolkit::ImageVisual::Property::ATLASING, mAttemptAtlasing ); } } -void SvgVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue ) +void SvgVisual::DoCreateInstancePropertyMap( Property::Map& map ) const { - // TODO + // Do nothing } -Dali::Property::Value SvgVisual::DoGetProperty( Dali::Property::Index index ) -{ - // TODO - return Dali::Property::Value(); -} - -void SvgVisual::ParseFromUrl( const std::string& imageUrl, ImageDimensions size ) +void SvgVisual::ParseFromUrl( const VisualUrl& imageUrl ) { mImageUrl = imageUrl; - - Vector2 dpi = Stage::GetCurrent().GetDpi(); - float meanDpi = (dpi.height + dpi.width) * 0.5f; - mParsedImage = nsvgParseFromFile( imageUrl.c_str(), UNITS, meanDpi ); - - if( size.GetWidth() != 0u && size.GetHeight() != 0u) + if( mImageUrl.IsLocalResource() ) { - mImpl->mSize.x = size.GetWidth(); - mImpl->mSize.y = size.GetHeight(); + Vector2 dpi = Stage::GetCurrent().GetDpi(); + float meanDpi = ( dpi.height + dpi.width ) * 0.5f; + Dali::Vector buffer; + if ( Dali::FileLoader::ReadFile( mImageUrl.GetUrl(), buffer ) ) + { + buffer.PushBack( '\0' ); + mParsedImage = nsvgParse( buffer.Begin(), UNITS, meanDpi ); + } } } void SvgVisual::AddRasterizationTask( const Vector2& size ) { - if( mImpl->mRenderer && mParsedImage ) + if( mImpl->mRenderer ) { unsigned int width = static_cast(size.width); unsigned int height = static_cast( size.height ); - BufferImage image = BufferImage::New( width, height, Pixel::RGBA8888); - RasterizingTaskPtr newTask = new RasterizingTask( this, mParsedImage, width, height ); + Vector2 dpi = Stage::GetCurrent().GetDpi(); + float meanDpi = ( dpi.height + dpi.width ) * 0.5f; + + RasterizingTaskPtr newTask = new RasterizingTask( this, mParsedImage, mImageUrl, meanDpi, width, height ); mFactoryCache.GetSVGRasterizationThread()->AddTask( newTask ); } } -void SvgVisual::ApplyRasterizedImage( PixelData rasterizedPixelData ) +void SvgVisual::ApplyRasterizedImage( NSVGimage* parsedSvg, PixelData rasterizedPixelData ) { - if( IsOnStage() ) + if( mParsedImage && IsOnStage() ) { TextureSet currentTextureSet = mImpl->mRenderer.GetTextures(); - if( mAtlasRect != FULL_TEXTURE_RECT ) + if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED ) { mFactoryCache.GetAtlasManager()->Remove( currentTextureSet, mAtlasRect ); } - Vector4 atlasRect; - TextureSet textureSet = mFactoryCache.GetAtlasManager()->Add(atlasRect, rasterizedPixelData ); - if( textureSet ) // atlasing + TextureSet textureSet; + + if( mAttemptAtlasing && !mImpl->mCustomShader ) { - if( textureSet != currentTextureSet ) + Vector4 atlasRect; + textureSet = mFactoryCache.GetAtlasManager()->Add(atlasRect, rasterizedPixelData ); + if( textureSet ) // atlasing { - mImpl->mRenderer.SetTextures( textureSet ); + if( textureSet != currentTextureSet ) + { + mImpl->mRenderer.SetTextures( textureSet ); + } + mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect ); + mAtlasRect = atlasRect; + mImpl->mFlags |= Impl::IS_ATLASING_APPLIED; } - mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect ); - mAtlasRect = atlasRect; - mImpl->mFlags |= Impl::IS_ATLASING_APPLIED; } - else // no atlasing + + if( !textureSet ) // no atlasing - mAttemptAtlasing is false or adding to atlas is failed { Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888, rasterizedPixelData.GetWidth(), rasterizedPixelData.GetHeight() ); @@ -243,9 +284,30 @@ void SvgVisual::ApplyRasterizedImage( PixelData rasterizedPixelData ) // reset the weak handle so that the renderer only get added to actor once mPlacementActor.Reset(); } + + // Svg loaded and ready to display + ResourceReady( Toolkit::Visual::ResourceStatus::READY ); } } +void SvgVisual::OnSetTransform() +{ + Vector2 visualSize = mImpl->mTransform.GetVisualSize( mImpl->mControlSize ); + + if( IsOnStage() ) + { + if( visualSize != mVisualSize ) + { + AddRasterizationTask( visualSize ); + mVisualSize = visualSize; + } + } + + if(mImpl->mRenderer) + { + mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT); + } +} } // namespace Internal