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=6344510453ee9fe8e4d70596f878e0858f32f5f4;hp=f3ad9565852902134d44fc79945f4cf9bfd78b0c;hb=6ee3158c2bb9ef4993947d65f520c00eca6edde3;hpb=6f0cec093ffdac0afee6ad28441d1398281f4c4c diff --git a/dali-toolkit/internal/visuals/svg/svg-visual.cpp b/dali-toolkit/internal/visuals/svg/svg-visual.cpp index f3ad956..6344510 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) 2018 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. @@ -22,12 +22,12 @@ #include #include #include -#include #include #include // INTERNAL INCLUDES #include +#include #include #include #include @@ -41,6 +41,9 @@ namespace { const char * const UNITS("px"); +// property name +const char * const IMAGE_ATLASING( "atlasing" ); + const Dali::Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f); } @@ -53,24 +56,31 @@ namespace Toolkit namespace Internal { -SvgVisualPtr SvgVisual::New( VisualFactoryCache& factoryCache ) +SvgVisualPtr SvgVisual::New( VisualFactoryCache& factoryCache, const VisualUrl& imageUrl, const Property::Map& properties ) { - return new SvgVisual( factoryCache ); + SvgVisualPtr svgVisual( new SvgVisual( factoryCache ) ); + svgVisual->ParseFromUrl( imageUrl ); + svgVisual->SetProperties( properties ); + + return svgVisual; } -SvgVisualPtr SvgVisual::New( VisualFactoryCache& factoryCache, const std::string& imageUrl, ImageDimensions size ) +SvgVisualPtr SvgVisual::New( VisualFactoryCache& factoryCache, const VisualUrl& imageUrl ) { - SvgVisual* svgVisual = new SvgVisual( factoryCache ); - svgVisual->ParseFromUrl( imageUrl, size ); + SvgVisualPtr svgVisual( new SvgVisual( factoryCache ) ); + svgVisual->ParseFromUrl( imageUrl ); + return svgVisual; } SvgVisual::SvgVisual( VisualFactoryCache& factoryCache ) -: Visual::Base( factoryCache ), +: Visual::Base( factoryCache, Visual::FittingMode::FILL ), mAtlasRect( FULL_TEXTURE_RECT ), - mImageUrl(), + mImageUrl( ), mParsedImage( NULL ), - mPlacementActor() + mPlacementActor(), + mVisualSize(Vector2::ZERO), + mAttemptAtlasing( false ) { // the rasterized image is with pre-multiplied alpha format mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA; @@ -84,43 +94,53 @@ SvgVisual::~SvgVisual() } } -void SvgVisual::DoInitialize( Actor& actor, const Property::Map& propertyMap ) +void SvgVisual::DoSetProperties( const Property::Map& propertyMap ) { - Property::Value* imageURLValue = propertyMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME ); - if( imageURLValue ) + // url already passed in from constructor + for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter ) { - std::string imageUrl; - if( imageURLValue->Get( imageUrl ) ) + KeyValuePair keyValue = propertyMap.GetKeyValue( iter ); + if( keyValue.first.type == Property::Key::INDEX ) { - ParseFromUrl( imageUrl ); + DoSetProperty( keyValue.first.indexKey, keyValue.second ); } - else + else if( keyValue.first == IMAGE_ATLASING ) { - DALI_LOG_ERROR( "The property '%s' is not a string\n", IMAGE_URL_NAME ); + DoSetProperty( Toolkit::ImageVisual::Property::ATLASING, keyValue.second ); } } } -void SvgVisual::DoSetOnStage( Actor& actor ) +void SvgVisual::DoSetProperty( Property::Index index, const Property::Value& value ) { - Shader shader = ImageVisual::GetImageShader( mFactoryCache, true, true ); - Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY ); - if( !geometry ) + switch( index ) { - geometry = mFactoryCache.CreateQuadGeometry(); - mFactoryCache.SaveGeometry( VisualFactoryCache::QUAD_GEOMETRY, geometry ); + case Toolkit::ImageVisual::Property::ATLASING: + { + value.Get( mAttemptAtlasing ); + break; + } } +} + +void SvgVisual::DoSetOnStage( Actor& actor ) +{ + Shader shader = ImageVisual::GetImageShader( mFactoryCache, mAttemptAtlasing, true ); + 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 ) @@ -132,7 +152,7 @@ void SvgVisual::DoSetOffStage( Actor& actor ) mPlacementActor.Reset(); } -void SvgVisual::GetNaturalSize( Vector2& naturalSize ) const +void SvgVisual::GetNaturalSize( Vector2& naturalSize ) { if( mParsedImage ) { @@ -145,48 +165,30 @@ void SvgVisual::GetNaturalSize( Vector2& naturalSize ) const } } -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::Visual::Property::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 ) -{ - // TODO -} - -Dali::Property::Value SvgVisual::DoGetProperty( Dali::Property::Index index ) +void SvgVisual::DoCreateInstancePropertyMap( Property::Map& map ) const { - // TODO - return Dali::Property::Value(); + // Do nothing } -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; + mParsedImage = nsvgParseFromFile( mImageUrl.GetUrl().c_str(), UNITS, meanDpi ); } } @@ -196,7 +198,6 @@ void SvgVisual::AddRasterizationTask( const Vector2& size ) { 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 ); mFactoryCache.GetSVGRasterizationThread()->AddTask( newTask ); @@ -208,24 +209,30 @@ void SvgVisual::ApplyRasterizedImage( PixelData rasterizedPixelData ) if( 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 ) { - 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() ); @@ -259,9 +266,25 @@ 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( mParsedImage && IsOnStage() ) + { + if( visualSize != mVisualSize ) + { + AddRasterizationTask( visualSize ); + mVisualSize = visualSize; + } + } +} } // namespace Internal