From: huiyu.eun Date: Mon, 23 Dec 2019 11:14:56 +0000 (+0900) Subject: [Tizen] Download remote svg file X-Git-Tag: submit/tizen_5.5/20191226.075037^0 X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=commitdiff_plain;h=658c7cbfb9373e2ec8eb028b9c90d619b466326b [Tizen] Download remote svg file Change-Id: I39c0ea28d35dd0bb37b9181ae7d302aa3d318845 --- diff --git a/automated-tests/src/dali-toolkit-internal/utc-Dali-DebugRendering.cpp b/automated-tests/src/dali-toolkit-internal/utc-Dali-DebugRendering.cpp index 8b119d4..05de93d 100755 --- a/automated-tests/src/dali-toolkit-internal/utc-Dali-DebugRendering.cpp +++ b/automated-tests/src/dali-toolkit-internal/utc-Dali-DebugRendering.cpp @@ -16,6 +16,7 @@ */ #include +#include #include #include #include @@ -147,6 +148,8 @@ int UtcDaliDebugRenderingGetVisual1(void) propertyMap5.Insert( Toolkit::Visual::Property::TYPE, Visual::SVG ); propertyMap5.Insert( ImageVisual::Property::URL, TEST_SVG_FILE_NAME ); Visual::Base svgVisual = factory.CreateVisual( propertyMap5 ); + + DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION ); DALI_TEST_CHECK( svgVisual ); TestDebugVisual( svgVisual, Visual::SVG, Vector2(100.f, 100.f) ); diff --git a/automated-tests/src/dali-toolkit/utc-Dali-Visual.cpp b/automated-tests/src/dali-toolkit/utc-Dali-Visual.cpp index 374afe0..1f18c02 100644 --- a/automated-tests/src/dali-toolkit/utc-Dali-Visual.cpp +++ b/automated-tests/src/dali-toolkit/utc-Dali-Visual.cpp @@ -347,6 +347,8 @@ int UtcDaliVisualSize(void) // svg visual Visual::Base svgVisual = factory.CreateVisual( TEST_SVG_FILE_NAME, ImageDimensions() ); svgVisual.SetTransformAndSize(DefaultTransform(), controlSize ); + + DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION ); svgVisual.GetNaturalSize(naturalSize); // TEST_SVG_FILE: // @@ -356,6 +358,9 @@ int UtcDaliVisualSize(void) // svg visual with a size Visual::Base svgVisual2 = factory.CreateVisual( TEST_SVG_FILE_NAME, ImageDimensions(200, 200) ); + svgVisual.SetTransformAndSize(DefaultTransform(), controlSize ); + DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION ); + svgVisual2.GetNaturalSize(naturalSize); DALI_TEST_EQUALS( naturalSize, Vector2(100.f, 100.f), TEST_LOCATION ); // Natural size should still be 100, 100 @@ -3574,3 +3579,20 @@ int UtcDaliSvgVisualCustomShader(void) END_TEST; } + +int UtcDaliSvgVisualDownloadRemoteFile(void) +{ + ToolkitTestApplication application; + tet_infoline( "Download svg file from remote" ); + + VisualFactory factory = VisualFactory::Get(); // svg visual + Visual::Base svgVisual1 = factory.CreateVisual( "http://bar.org/foobar.svg", ImageDimensions() ); + DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION ); + DALI_TEST_CHECK( svgVisual1 ); + + Visual::Base svgVisual2 = factory.CreateVisual( "http://bar.org/wrong_name.svg", ImageDimensions() ); + DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION ); + DALI_TEST_CHECK( svgVisual2 ); + END_TEST; +} + diff --git a/dali-toolkit/internal/visuals/svg/svg-rasterize-thread.cpp b/dali-toolkit/internal/visuals/svg/svg-rasterize-thread.cpp index 13140a7..7cb6734 100644 --- a/dali-toolkit/internal/visuals/svg/svg-rasterize-thread.cpp +++ b/dali-toolkit/internal/visuals/svg/svg-rasterize-thread.cpp @@ -18,10 +18,14 @@ // CLASS HEADER #include "svg-rasterize-thread.h" +// EXTERNAL INCLUDES +#include +#include +#include + // INTERNAL INCLUDES #include #include -#include namespace Dali { @@ -31,21 +35,56 @@ namespace Toolkit namespace Internal { +const char * const UNITS("px"); -RasterizingTask::RasterizingTask( SvgVisual* svgRenderer, NSVGimage* parsedSvg, unsigned int width, unsigned int height ) +RasterizingTask::RasterizingTask( SvgVisual* svgRenderer, NSVGimage* parsedSvg, const VisualUrl& url, float dpi, unsigned int width, unsigned int height) : mSvgVisual( svgRenderer ), mParsedSvg( parsedSvg ), + mUrl( url ), + mDpi( dpi ), mWidth( width ), mHeight( height ) { } +void RasterizingTask::Load() +{ + if( mParsedSvg != NULL) + { + return; + } + + if( mUrl.IsLocalResource() ) + { + Dali::Vector buffer; + if ( !Dali::FileLoader::ReadFile( mUrl.GetUrl(), buffer ) ) + { + DALI_LOG_ERROR("Failed to read file!\n"); + return; + } + + mParsedSvg = nsvgParse( buffer.begin(), UNITS, mDpi ); + } + else + { + Dali::Vector remoteBuffer; + + if( !Dali::FileLoader::DownloadFileSynchronously( mUrl.GetUrl(), remoteBuffer )) + { + DALI_LOG_ERROR("Failed to download file!\n"); + return; + } + + mParsedSvg = nsvgParse( reinterpret_cast(remoteBuffer.begin()), UNITS, mDpi ); + } +} + void RasterizingTask::Rasterize( NSVGrasterizer* rasterizer ) { - if( mWidth > 0u && mHeight > 0u ) + if( mParsedSvg != NULL && mWidth > 0u && mHeight > 0u ) { - float scaleX = static_cast( mWidth ) / mParsedSvg->width; - float scaleY = static_cast( mHeight ) / mParsedSvg->height; + float scaleX = static_cast( mWidth ) / mParsedSvg->width; + float scaleY = static_cast( mHeight ) / mParsedSvg->height; float scale = scaleX < scaleY ? scaleX : scaleY; unsigned int bufferStride = mWidth*Pixel::GetBytesPerPixel( Pixel::RGBA8888 ); unsigned int bufferSize = bufferStride * mHeight; @@ -59,6 +98,11 @@ void RasterizingTask::Rasterize( NSVGrasterizer* rasterizer ) } } +NSVGimage* RasterizingTask::GetParsedImage() const +{ + return mParsedSvg; +} + SvgVisual* RasterizingTask::GetSvgVisual() const { return mSvgVisual.Get(); @@ -225,6 +269,7 @@ void SvgRasterizeThread::Run() SetThreadName( "SVGThread" ); while( RasterizingTaskPtr task = NextTaskToProcess() ) { + task->Load( ); task->Rasterize( mRasterizer ); AddCompletedTask( task ); } diff --git a/dali-toolkit/internal/visuals/svg/svg-rasterize-thread.h b/dali-toolkit/internal/visuals/svg/svg-rasterize-thread.h index f30c459..1c94ca9 100644 --- a/dali-toolkit/internal/visuals/svg/svg-rasterize-thread.h +++ b/dali-toolkit/internal/visuals/svg/svg-rasterize-thread.h @@ -28,6 +28,7 @@ #include #include #include +#include struct NSVGimage; struct NSVGrasterizer; @@ -65,10 +66,11 @@ public: * @param[in] parsedSvg The parsed svg for rasterizing. * Note, after the task is added to the worker thread, the worker thread takes over the ownership. * When the image is to be deleted, delete it in the worker thread by calling SvgRasterizeThread::DeleteImage( parsedSvg ). + * @param[in] url The URL to svg resource to use. * @param[in] width The rasterization width. * @param[in] height The rasterization height. */ - RasterizingTask( SvgVisual* svgRenderer, NSVGimage* parsedSvg, unsigned int width, unsigned int height ); + RasterizingTask( SvgVisual* svgRenderer, NSVGimage* parsedSvg, const VisualUrl& url, float dpi, unsigned int width, unsigned int height ); /** * Do the rasterization with the given rasterizer. @@ -87,8 +89,18 @@ public: */ PixelData GetPixelData() const; -private: + /** + * Get the parsed data. + * @return parsed image data. + */ + NSVGimage* GetParsedImage() const; + + /** + * Load svg file + */ + void Load(); +private: // Undefined RasterizingTask( const RasterizingTask& task ); @@ -96,14 +108,15 @@ private: RasterizingTask& operator=( const RasterizingTask& task ); private: - SvgVisualPtr mSvgVisual; - PixelData mPixelData; + SvgVisualPtr mSvgVisual; NSVGimage* mParsedSvg; + VisualUrl mUrl; + PixelData mPixelData; + float mDpi; unsigned int mWidth; unsigned int mHeight; }; - /** * The worker thread for SVG rasterization. */ diff --git a/dali-toolkit/internal/visuals/svg/svg-visual.cpp b/dali-toolkit/internal/visuals/svg/svg-visual.cpp index a3d3685..cc7b660 100644 --- a/dali-toolkit/internal/visuals/svg/svg-visual.cpp +++ b/dali-toolkit/internal/visuals/svg/svg-visual.cpp @@ -18,29 +18,19 @@ // 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 +// EXTERNAL INCLUDES +#include + namespace { -const char * const UNITS("px"); - // property name const char * const IMAGE_ATLASING( "atlasing" ); @@ -58,8 +48,7 @@ namespace Internal SvgVisualPtr SvgVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties ) { - SvgVisualPtr svgVisual( new SvgVisual( factoryCache, shaderFactory ) ); - svgVisual->ParseFromUrl( imageUrl ); + SvgVisualPtr svgVisual( new SvgVisual( factoryCache, shaderFactory, imageUrl ) ); svgVisual->SetProperties( properties ); return svgVisual; @@ -67,17 +56,16 @@ SvgVisualPtr SvgVisual::New( VisualFactoryCache& factoryCache, ImageVisualShader SvgVisualPtr SvgVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl ) { - SvgVisualPtr svgVisual( new SvgVisual( factoryCache, shaderFactory ) ); - svgVisual->ParseFromUrl( imageUrl ); + SvgVisualPtr svgVisual( new SvgVisual( factoryCache, shaderFactory, imageUrl ) ); return svgVisual; } -SvgVisual::SvgVisual( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory ) +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(), mVisualSize(Vector2::ZERO), @@ -198,36 +186,29 @@ void SvgVisual::DoCreateInstancePropertyMap( Property::Map& map ) const // Do nothing } -void SvgVisual::ParseFromUrl( const VisualUrl& imageUrl ) -{ - mImageUrl = imageUrl; - if( mImageUrl.IsLocalResource() ) - { - Vector2 dpi = Stage::GetCurrent().GetDpi(); - float meanDpi = ( dpi.height + dpi.width ) * 0.5f; - Dali::Vector buffer; - if ( Dali::FileLoader::ReadFile( mImageUrl.GetUrl(), buffer ) ) - { - 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 ); - 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 == NULL) + { + mParsedImage = parsedSvg; + } + + if( IsOnStage() ) { TextureSet currentTextureSet = mImpl->mRenderer.GetTextures(); if( mImpl->mFlags |= Impl::IS_ATLASING_APPLIED ) @@ -297,7 +278,7 @@ void SvgVisual::OnSetTransform() { Vector2 visualSize = mImpl->mTransform.GetVisualSize( mImpl->mControlSize ); - if( mParsedImage && IsOnStage() ) + if( IsOnStage() ) { if( visualSize != mVisualSize ) { diff --git a/dali-toolkit/internal/visuals/svg/svg-visual.h b/dali-toolkit/internal/visuals/svg/svg-visual.h index 8fc2ad8..9947376 100644 --- a/dali-toolkit/internal/visuals/svg/svg-visual.h +++ b/dali-toolkit/internal/visuals/svg/svg-visual.h @@ -106,8 +106,9 @@ protected: * * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object * @param[in] shaderFactory The ImageVisualShaderFactory object + * @param[in] imageUrl The URL to svg resource to use */ - SvgVisual( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory ); + SvgVisual( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl ); /** * @brief A reference counted object may only be deleted by calling Unreference(). @@ -139,20 +140,14 @@ public: /** * @bried Apply the rasterized image to the visual. * + * @param[in] parsedSvg The data of parsed image. * @param[in] rasterizedPixelData The pixel buffer with the rasterized pixels */ - void ApplyRasterizedImage( PixelData rasterizedPixelData ); + void ApplyRasterizedImage( NSVGimage* parsedSvg, PixelData rasterizedPixelData ); private: /** - * @brief Parses the SVG Image from the set URL. - * - * @param[in] imageUrl The URL of the image to parse the SVG from. - */ - void ParseFromUrl( const VisualUrl& imageUrl ); - - /** * @bried Rasterize the svg with the given size, and add it to the visual. * * @param[in] size The target size of the SVG rasterization. @@ -166,7 +161,6 @@ private: */ void DoSetProperty( Property::Index index, const Property::Value& value ); - // Undefined SvgVisual( const SvgVisual& svgRenderer ); diff --git a/dali-toolkit/internal/visuals/visual-factory-cache.cpp b/dali-toolkit/internal/visuals/visual-factory-cache.cpp index 3852361..594c12f 100644 --- a/dali-toolkit/internal/visuals/visual-factory-cache.cpp +++ b/dali-toolkit/internal/visuals/visual-factory-cache.cpp @@ -150,7 +150,7 @@ void VisualFactoryCache::ApplyRasterizedSVGToSampler() { while( RasterizingTaskPtr task = mSvgRasterizeThread->NextCompletedTask() ) { - task->GetSvgVisual()->ApplyRasterizedImage( task->GetPixelData() ); + task->GetSvgVisual()->ApplyRasterizedImage( task->GetParsedImage(), task->GetPixelData() ); } }