/*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2022 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.
// CLASS HEADER
#include "svg-visual.h"
-// EXTERNAL INCLUDES
-#include <dali/public-api/images/buffer-image.h>
-#include <dali/public-api/common/stage.h>
-#include <dali/public-api/math/vector4.h>
-#include <dali/devel-api/images/atlas.h>
-#include <dali/devel-api/images/texture-set-image.h>
-#include <dali/integration-api/debug.h>
-
// INTERNAL INCLUDES
-#include <dali-toolkit/public-api/visuals/image-visual-properties.h>
-#include <dali-toolkit/third-party/nanosvg/nanosvg.h>
-#include <dali-toolkit/internal/visuals/svg/svg-rasterize-thread.h>
-#include <dali-toolkit/internal/visuals/image/image-visual.h>
#include <dali-toolkit/internal/visuals/image-atlas-manager.h>
-#include <dali-toolkit/internal/visuals/visual-factory-cache.h>
-#include <dali-toolkit/internal/visuals/visual-string-constants.h>
+#include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
+#include <dali-toolkit/internal/visuals/svg/svg-rasterize-thread.h>
#include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
+#include <dali-toolkit/internal/visuals/visual-string-constants.h>
+#include <dali-toolkit/public-api/visuals/image-visual-properties.h>
-
-namespace
-{
-const char * const UNITS("px");
-
-const Dali::Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
-}
+// EXTERNAL INCLUDES
+#include <dali/devel-api/common/stage.h>
+#include <dali/integration-api/debug.h>
namespace Dali
{
-
namespace Toolkit
{
-
namespace Internal
{
+namespace
+{
+const int CUSTOM_PROPERTY_COUNT(6); // atlas + corner/border
+
+// property name
+const Dali::Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
-SvgVisualPtr SvgVisual::New( VisualFactoryCache& factoryCache )
+} // namespace
+
+SvgVisualPtr SvgVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties)
{
- return new SvgVisual( factoryCache );
+ SvgVisualPtr svgVisual(new SvgVisual(factoryCache, shaderFactory, imageUrl));
+ svgVisual->SetProperties(properties);
+ svgVisual->Initialize();
+ return svgVisual;
}
-SvgVisualPtr SvgVisual::New( VisualFactoryCache& factoryCache, const std::string& imageUrl, ImageDimensions size )
+SvgVisualPtr SvgVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl)
{
- SvgVisual* svgVisual = new SvgVisual( factoryCache );
- svgVisual->ParseFromUrl( imageUrl, size );
+ SvgVisualPtr svgVisual(new SvgVisual(factoryCache, shaderFactory, imageUrl));
+ svgVisual->Initialize();
return svgVisual;
}
-SvgVisual::SvgVisual( VisualFactoryCache& factoryCache )
-: Visual::Base( factoryCache ),
- mAtlasRect( FULL_TEXTURE_RECT ),
- mImageUrl(),
- mParsedImage( NULL ),
- mPlacementActor()
+SvgVisual::SvgVisual(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl)
+: Visual::Base(factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::SVG),
+ mImageVisualShaderFactory(shaderFactory),
+ mAtlasRect(FULL_TEXTURE_RECT),
+ mImageUrl(imageUrl),
+ mVectorRenderer(VectorImageRenderer::New()),
+ mDefaultWidth(0),
+ mDefaultHeight(0),
+ mPlacementActor(),
+ mRasterizedSize(Vector2::ZERO),
+ mLoadFailed(false),
+ mAttemptAtlasing(false)
{
// the rasterized image is with pre-multiplied alpha format
mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
SvgVisual::~SvgVisual()
{
- if( mParsedImage )
- {
- nsvgDelete( mParsedImage );
- }
}
-void SvgVisual::DoInitialize( Actor& actor, const Property::Map& propertyMap )
+void SvgVisual::OnInitialize()
+{
+ Shader shader = GenerateShader();
+ Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
+ mImpl->mRenderer = VisualRenderer::New(geometry, shader);
+ mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT);
+}
+
+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)
+ {
+ DoSetProperty(Toolkit::ImageVisual::Property::ATLASING, keyValue.second);
+ }
+ else if(keyValue.first == SYNCHRONOUS_LOADING)
{
- DALI_LOG_ERROR( "The property '%s' is not a string\n", IMAGE_URL_NAME );
+ DoSetProperty(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, 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;
+ }
+ case Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING:
+ {
+ bool sync = false;
+ if(value.Get(sync))
+ {
+ if(sync)
+ {
+ mImpl->mFlags |= Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
+ }
+ else
+ {
+ mImpl->mFlags &= ~Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
+ }
+ }
+ else
+ {
+ DALI_LOG_ERROR("ImageVisual: synchronousLoading property has incorrect type\n");
+ }
+ break;
+ }
}
+}
+
+void SvgVisual::DoSetOnScene(Actor& actor)
+{
TextureSet textureSet = TextureSet::New();
- mImpl->mRenderer = Renderer::New( geometry, shader );
- mImpl->mRenderer.SetTextures( textureSet );
+ mImpl->mRenderer.SetTextures(textureSet);
- if( mImpl->mSize != Vector2::ZERO && mParsedImage )
- {
- AddRasterizationTask( mImpl->mSize );
- }
+ // Register transform properties
+ mImpl->mTransform.SetUniforms(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;
+
+ if(mLoadFailed)
+ {
+ Vector2 imageSize = Vector2::ZERO;
+ imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
+ mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize);
+ actor.AddRenderer(mImpl->mRenderer);
+
+ ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
+ }
+ else
+ {
+ if(mImpl->mEventObserver)
+ {
+ // SVG visual needs it's size set before it can be rasterized hence request relayout once on stage
+ mImpl->mEventObserver->RelayoutRequest(*this);
+ }
+ }
}
-void SvgVisual::DoSetOffStage( Actor& actor )
+void SvgVisual::DoSetOffScene(Actor& actor)
{
- mFactoryCache.GetSVGRasterizationThread()->RemoveTask( this );
+ mFactoryCache.GetSVGRasterizationThread()->RemoveTask(this);
- actor.RemoveRenderer( mImpl->mRenderer );
- mImpl->mRenderer.Reset();
+ actor.RemoveRenderer(mImpl->mRenderer);
mPlacementActor.Reset();
+
+ // Reset the visual size to zero so that when adding the actor back to stage the SVG rasterization is forced
+ mRasterizedSize = Vector2::ZERO;
}
-void SvgVisual::GetNaturalSize( Vector2& naturalSize ) const
+void SvgVisual::GetNaturalSize(Vector2& naturalSize)
{
- if( mParsedImage )
+ if(mLoadFailed && mImpl->mRenderer)
{
- naturalSize.x = mParsedImage->width;
- naturalSize.y = mParsedImage->height;
+ // Load failed, use broken image size
+ auto textureSet = mImpl->mRenderer.GetTextures();
+ if(textureSet && textureSet.GetTextureCount())
+ {
+ auto texture = textureSet.GetTexture(0);
+ if(texture)
+ {
+ naturalSize.x = texture.GetWidth();
+ naturalSize.y = texture.GetHeight();
+ return;
+ }
+ }
}
else
{
- naturalSize = Vector2::ZERO;
- }
-}
-
-void SvgVisual::SetSize( const Vector2& size )
-{
- if(mImpl->mSize != size && mParsedImage && IsOnStage() )
- {
- AddRasterizationTask( size );
+ naturalSize.x = mDefaultWidth;
+ naturalSize.y = mDefaultHeight;
}
- mImpl->mSize = size;
}
-void SvgVisual::DoCreatePropertyMap( Property::Map& map ) const
+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);
}
+ map.Insert(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, IsSynchronousLoadingRequired());
}
-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 )
+void SvgVisual::EnablePreMultipliedAlpha(bool preMultiplied)
{
- // TODO
- return Dali::Property::Value();
-}
-
-void SvgVisual::ParseFromUrl( const std::string& imageUrl, ImageDimensions size )
-{
- 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)
+ // Make always enable pre multiplied alpha whether preMultiplied value is false.
+ if(!preMultiplied)
{
- mImpl->mSize.x = size.GetWidth();
- mImpl->mSize.y = size.GetHeight();
+ DALI_LOG_WARNING("Note : SvgVisual cannot disable PreMultipliedAlpha\n");
}
}
-void SvgVisual::AddRasterizationTask( const Vector2& size )
+void SvgVisual::AddRasterizationTask(const Vector2& size)
{
- if( mImpl->mRenderer && mParsedImage )
+ if(mImpl->mRenderer)
{
- unsigned int width = static_cast<unsigned int>(size.width);
- unsigned int height = static_cast<unsigned int>( size.height );
- BufferImage image = BufferImage::New( width, height, Pixel::RGBA8888);
+ unsigned int width = static_cast<unsigned int>(size.width);
+ unsigned int height = static_cast<unsigned int>(size.height);
+
+ Vector2 dpi = Stage::GetCurrent().GetDpi();
+ float meanDpi = (dpi.height + dpi.width) * 0.5f;
+
+ RasterizingTaskPtr newTask = new RasterizingTask(this, mVectorRenderer, mImageUrl, meanDpi, width, height);
- RasterizingTaskPtr newTask = new RasterizingTask( this, mParsedImage, width, height );
- mFactoryCache.GetSVGRasterizationThread()->AddTask( newTask );
+ if(IsSynchronousLoadingRequired() && mImageUrl.IsLocalResource())
+ {
+ newTask->Load();
+ newTask->Rasterize();
+ ApplyRasterizedImage(newTask->GetPixelData(), newTask->IsLoaded());
+ }
+ else
+ {
+ mFactoryCache.GetSVGRasterizationThread()->AddTask(newTask);
+ }
}
}
-void SvgVisual::ApplyRasterizedImage( PixelData rasterizedPixelData )
+void SvgVisual::ApplyRasterizedImage(PixelData rasterizedPixelData, bool isLoaded)
{
- if( IsOnStage() )
+ if(isLoaded && rasterizedPixelData && IsOnScene())
{
+ if(mDefaultWidth == 0 || mDefaultHeight == 0)
+ {
+ mVectorRenderer.GetDefaultSize(mDefaultWidth, mDefaultHeight);
+ }
+
+ mRasterizedSize.x = static_cast<float>(rasterizedPixelData.GetWidth());
+ mRasterizedSize.y = static_cast<float>(rasterizedPixelData.GetHeight());
+
TextureSet currentTextureSet = mImpl->mRenderer.GetTextures();
- if( mAtlasRect != FULL_TEXTURE_RECT )
+ if(mImpl->mFlags & Impl::IS_ATLASING_APPLIED)
{
- mFactoryCache.GetAtlasManager()->Remove( currentTextureSet, mAtlasRect );
+ 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() );
- texture.Upload( rasterizedPixelData );
+ Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888, rasterizedPixelData.GetWidth(), rasterizedPixelData.GetHeight());
+ texture.Upload(rasterizedPixelData);
mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
- if( mAtlasRect == FULL_TEXTURE_RECT )
+ if(mAtlasRect == FULL_TEXTURE_RECT)
{
textureSet = currentTextureSet;
}
else
{
textureSet = TextureSet::New();
- mImpl->mRenderer.SetTextures( textureSet );
+ mImpl->mRenderer.SetTextures(textureSet);
- mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, FULL_TEXTURE_RECT );
+ mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, FULL_TEXTURE_RECT);
mAtlasRect = FULL_TEXTURE_RECT;
}
- if( textureSet )
+ if(textureSet)
{
- textureSet.SetTexture( 0, texture );
+ textureSet.SetTexture(0, texture);
}
}
// Rasterized pixels are uploaded to texture. If weak handle is holding a placement actor, it is the time to add the renderer to actor.
Actor actor = mPlacementActor.GetHandle();
- if( actor )
+ if(actor)
{
- actor.AddRenderer( mImpl->mRenderer );
+ actor.AddRenderer(mImpl->mRenderer);
// 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);
+ }
+ else if(!isLoaded || !rasterizedPixelData)
+ {
+ mLoadFailed = true;
+
+ Actor actor = mPlacementActor.GetHandle();
+ if(actor)
+ {
+ Vector2 imageSize = Vector2::ZERO;
+ imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
+ mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize);
+ actor.AddRenderer(mImpl->mRenderer);
+ }
+
+ ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
}
}
+void SvgVisual::OnSetTransform()
+{
+ Vector2 visualSize = mImpl->mTransform.GetVisualSize(mImpl->mControlSize);
+
+ if(IsOnScene() && !mLoadFailed)
+ {
+ if(visualSize != mRasterizedSize || mDefaultWidth == 0 || mDefaultHeight == 0)
+ {
+ mRasterizedSize = visualSize;
+ AddRasterizationTask(visualSize);
+ }
+ }
+
+ if(mImpl->mRenderer)
+ {
+ mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
+ }
+}
+
+void SvgVisual::UpdateShader()
+{
+ if(mImpl->mRenderer)
+ {
+ Shader shader = GenerateShader();
+ mImpl->mRenderer.SetShader(shader);
+ }
+}
+
+Shader SvgVisual::GenerateShader() const
+{
+ Shader shader;
+ if(!mImpl->mCustomShader)
+ {
+ shader = mImageVisualShaderFactory.GetShader(
+ mFactoryCache,
+ ImageVisualShaderFeature::FeatureBuilder()
+ .EnableTextureAtlas(mAttemptAtlasing)
+ .EnableRoundedCorner(IsRoundedCornerRequired())
+ .EnableBorderline(IsBorderlineRequired()));
+ }
+ else
+ {
+ shader = Shader::New(mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource().data() : mImpl->mCustomShader->mVertexShader,
+ mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource().data() : mImpl->mCustomShader->mFragmentShader,
+ mImpl->mCustomShader->mHints);
+
+ shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
+ }
+ return shader;
+}
} // namespace Internal