From 65c40fb2f2ea1c8b677be4ad1cee04d271869d0b Mon Sep 17 00:00:00 2001 From: Heeyong Song Date: Wed, 11 May 2022 18:54:06 +0900 Subject: [PATCH] Support asynchronous svg loading Change-Id: I46e977fa00fd9a173663b253867e198ac9c40cc8 --- .../utc-Dali-DebugRendering.cpp | 2 +- .../toolkit-vector-image-renderer.cpp | 41 ++++++-- .../toolkit-vector-image-renderer.h | 30 ++++++ .../src/dali-toolkit/utc-Dali-Control.cpp | 13 ++- .../src/dali-toolkit/utc-Dali-ImageView.cpp | 112 +++++++++++++++------ .../src/dali-toolkit/utc-Dali-Visual.cpp | 15 --- .../internal/visuals/svg/svg-rasterize-thread.cpp | 47 +++++---- .../internal/visuals/svg/svg-rasterize-thread.h | 28 +++--- dali-toolkit/internal/visuals/svg/svg-visual.cpp | 38 +------ dali-toolkit/internal/visuals/svg/svg-visual.h | 8 +- 10 files changed, 201 insertions(+), 133 deletions(-) create mode 100644 automated-tests/src/dali-toolkit/dali-toolkit-test-utils/toolkit-vector-image-renderer.h 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 d39b00c..e2ff409 100755 --- a/automated-tests/src/dali-toolkit-internal/utc-Dali-DebugRendering.cpp +++ b/automated-tests/src/dali-toolkit-internal/utc-Dali-DebugRendering.cpp @@ -160,7 +160,7 @@ int UtcDaliDebugRenderingGetVisual1(void) propertyMap5.Insert(ImageVisual::Property::URL, TEST_SVG_FILE_NAME); Visual::Base svgVisual = factory.CreateVisual(propertyMap5); DALI_TEST_CHECK(svgVisual); - TestDebugVisual(application.GetScene(), svgVisual, Visual::SVG, Vector2(100.f, 100.f)); + TestDebugVisual(application.GetScene(), svgVisual, Visual::SVG, Vector2::ZERO); // Test that AnimatedImageVisual is replaced with debug visual // TEST_GIF_FILE: anim.gif diff --git a/automated-tests/src/dali-toolkit/dali-toolkit-test-utils/toolkit-vector-image-renderer.cpp b/automated-tests/src/dali-toolkit/dali-toolkit-test-utils/toolkit-vector-image-renderer.cpp index cf276f4..d809e66 100644 --- a/automated-tests/src/dali-toolkit/dali-toolkit-test-utils/toolkit-vector-image-renderer.cpp +++ b/automated-tests/src/dali-toolkit/dali-toolkit-test-utils/toolkit-vector-image-renderer.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -30,13 +31,15 @@ namespace Internal { namespace Adaptor { +namespace +{ +Dali::Internal::Adaptor::VectorImageRenderer* gVectorImageRenderer = nullptr; +} + class VectorImageRenderer : public Dali::BaseObject { public: VectorImageRenderer() - : mWidth(0), - mHeight(0), - mRasterizeSuccess(true) { } @@ -46,6 +49,7 @@ public: bool Load(const Vector& data, float dpi) { + mLoadCount++; if(strncmp(reinterpret_cast(data.Begin()), "invalid", 7) == 0) { return false; @@ -57,6 +61,11 @@ public: return true; } + bool IsLoaded() const + { + return mLoadCount > 0 ? true : false; + } + Dali::Devel::PixelBuffer Rasterize(uint32_t width, uint32_t height) { if(mRasterizeSuccess) @@ -74,9 +83,10 @@ public: } public: - uint32_t mWidth; - uint32_t mHeight; - bool mRasterizeSuccess; + uint32_t mWidth{0}; + uint32_t mHeight{0}; + uint32_t mLoadCount{0}; + bool mRasterizeSuccess{true}; }; inline VectorImageRenderer& GetImplementation(Dali::VectorImageRenderer& renderer) @@ -105,6 +115,8 @@ VectorImageRenderer VectorImageRenderer::New() { Internal::Adaptor::VectorImageRenderer* imageRenderer = new Internal::Adaptor::VectorImageRenderer(); + Internal::Adaptor::gVectorImageRenderer = imageRenderer; + return VectorImageRenderer(imageRenderer); } @@ -126,6 +138,11 @@ bool VectorImageRenderer::Load(const Vector& data, float dpi) return Internal::Adaptor::GetImplementation(*this).Load(data, dpi); } +bool VectorImageRenderer::IsLoaded() const +{ + return Internal::Adaptor::GetImplementation(*this).IsLoaded(); +} + Dali::Devel::PixelBuffer VectorImageRenderer::Rasterize(uint32_t width, uint32_t height) { return Internal::Adaptor::GetImplementation(*this).Rasterize(width, height); @@ -137,3 +154,15 @@ void VectorImageRenderer::GetDefaultSize(uint32_t& width, uint32_t& height) cons } } // namespace Dali + +namespace Test +{ +namespace VectorImageRenderer +{ +uint32_t GetLoadCount() +{ + return Dali::Internal::Adaptor::gVectorImageRenderer->mLoadCount; +} + +} // namespace VectorImageRenderer +} // namespace Test diff --git a/automated-tests/src/dali-toolkit/dali-toolkit-test-utils/toolkit-vector-image-renderer.h b/automated-tests/src/dali-toolkit/dali-toolkit-test-utils/toolkit-vector-image-renderer.h new file mode 100644 index 0000000..37cc362 --- /dev/null +++ b/automated-tests/src/dali-toolkit/dali-toolkit-test-utils/toolkit-vector-image-renderer.h @@ -0,0 +1,30 @@ +#ifndef DALI_TOOLKIT_TEST_VECTOR_IMAGE_RENDERER_H +#define DALI_TOOLKIT_TEST_VECTOR_IMAGE_RENDERER_H + +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +namespace Test +{ +namespace VectorImageRenderer +{ +uint32_t GetLoadCount(); + +} // namespace VectorImageRenderer +} // namespace Test + +#endif // DALI_TOOLKIT_TEST_VECTOR_IMAGE_RENDERER_H diff --git a/automated-tests/src/dali-toolkit/utc-Dali-Control.cpp b/automated-tests/src/dali-toolkit/utc-Dali-Control.cpp index 6745fea..3ae49cc 100644 --- a/automated-tests/src/dali-toolkit/utc-Dali-Control.cpp +++ b/automated-tests/src/dali-toolkit/utc-Dali-Control.cpp @@ -73,7 +73,7 @@ static void TestKeyInputFocusCallback(Control control) const char* TEST_LARGE_IMAGE_FILE_NAME = TEST_RESOURCE_DIR "/tbcol.png"; const char* TEST_IMAGE_FILE_NAME = TEST_RESOURCE_DIR "/gallery-small-1.jpg"; -const char* TEST_SVG_FILE_NAME = TEST_RESOURCE_DIR "/Kid1.svg"; +const char* TEST_SVG_FILE_NAME = TEST_RESOURCE_DIR "/svg1.svg"; Vector4 GetControlBackgroundColor(Control& control) { @@ -269,8 +269,8 @@ int UtcDaliControlNavigationProperties(void) DALI_TEST_EQUALS(-1, control.GetProperty(DevelControl::Property::RIGHT_FOCUSABLE_ACTOR_ID).Get(), TEST_LOCATION); DALI_TEST_EQUALS(-1, control.GetProperty(DevelControl::Property::UP_FOCUSABLE_ACTOR_ID).Get(), TEST_LOCATION); DALI_TEST_EQUALS(-1, control.GetProperty(DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID).Get(), TEST_LOCATION); - DALI_TEST_EQUALS(-1, control.GetProperty( DevelControl::Property::CLOCKWISE_FOCUSABLE_ACTOR_ID ).Get< int >(), TEST_LOCATION); - DALI_TEST_EQUALS(-1, control.GetProperty( DevelControl::Property::COUNTER_CLOCKWISE_FOCUSABLE_ACTOR_ID ).Get< int >(), TEST_LOCATION); + DALI_TEST_EQUALS(-1, control.GetProperty(DevelControl::Property::CLOCKWISE_FOCUSABLE_ACTOR_ID).Get(), TEST_LOCATION); + DALI_TEST_EQUALS(-1, control.GetProperty(DevelControl::Property::COUNTER_CLOCKWISE_FOCUSABLE_ACTOR_ID).Get(), TEST_LOCATION); control.SetProperty(DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID, 1); DALI_TEST_EQUALS(1, control.GetProperty(DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID).Get(), TEST_LOCATION); @@ -290,9 +290,9 @@ int UtcDaliControlNavigationProperties(void) control.SetProperty(DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID, 18); DALI_TEST_EQUALS(18, control.GetProperty(DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID).Get(), TEST_LOCATION); control.SetProperty(DevelControl::Property::CLOCKWISE_FOCUSABLE_ACTOR_ID, 19); - DALI_TEST_EQUALS(19, control.GetProperty( DevelControl::Property::CLOCKWISE_FOCUSABLE_ACTOR_ID ).Get< int >(), TEST_LOCATION); + DALI_TEST_EQUALS(19, control.GetProperty(DevelControl::Property::CLOCKWISE_FOCUSABLE_ACTOR_ID).Get(), TEST_LOCATION); control.SetProperty(DevelControl::Property::COUNTER_CLOCKWISE_FOCUSABLE_ACTOR_ID, 20); - DALI_TEST_EQUALS(20, control.GetProperty( DevelControl::Property::COUNTER_CLOCKWISE_FOCUSABLE_ACTOR_ID ).Get< int >(), TEST_LOCATION); + DALI_TEST_EQUALS(20, control.GetProperty(DevelControl::Property::COUNTER_CLOCKWISE_FOCUSABLE_ACTOR_ID).Get(), TEST_LOCATION); END_TEST; } @@ -1074,6 +1074,9 @@ int UtcDaliControlResourcesReady02(void) application.SendNotification(); application.Render(); + // Wait for rasterization + DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION); + DALI_TEST_EQUALS(control.IsResourceReady(), true, TEST_LOCATION); DALI_TEST_EQUALS(gResourceReadySignalFired, true, TEST_LOCATION); gResourceReadySignalFired = false; diff --git a/automated-tests/src/dali-toolkit/utc-Dali-ImageView.cpp b/automated-tests/src/dali-toolkit/utc-Dali-ImageView.cpp index e16e93e..6a0d8fc 100644 --- a/automated-tests/src/dali-toolkit/utc-Dali-ImageView.cpp +++ b/automated-tests/src/dali-toolkit/utc-Dali-ImageView.cpp @@ -23,6 +23,7 @@ #include #include +#include #include "dummy-control.h" #include @@ -2705,12 +2706,7 @@ int UtcDaliImageViewSyncSVGLoading(void) tet_infoline("ImageView Testing SVG image sync loading"); - // Sync loading, automatic atlasing for small size image { - TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace(); - callStack.Reset(); - callStack.Enable(true); - ImageView imageView = ImageView::New(); // Sync loading is used @@ -2724,7 +2720,6 @@ int UtcDaliImageViewSyncSVGLoading(void) DALI_TEST_CHECK(imageView); application.SendNotification(); - application.Render(16); Vector3 naturalSize = imageView.GetNaturalSize(); DALI_TEST_EQUALS(naturalSize.width, 100.0f, TEST_LOCATION); @@ -2739,28 +2734,27 @@ int UtcDaliImageViewAsyncSVGLoading(void) tet_infoline("ImageView Testing SVG image async loading"); - // Sync loading, automatic atlasing for small size image { - TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace(); - callStack.Reset(); - callStack.Enable(true); - ImageView imageView = ImageView::New(); - // Sync loading is used - Property::Map syncLoadingMap; - syncLoadingMap.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE); - syncLoadingMap.Insert(Toolkit::ImageVisual::Property::URL, TEST_RESOURCE_DIR "/svg1.svg"); - syncLoadingMap.Insert(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, false); - imageView.SetProperty(ImageView::Property::IMAGE, syncLoadingMap); + // Async loading is used - default value of SYNCHRONOUS_LOADING is false. + Property::Map propertyMap; + propertyMap.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE); + propertyMap.Insert(Toolkit::ImageVisual::Property::URL, TEST_RESOURCE_DIR "/svg1.svg"); + imageView.SetProperty(ImageView::Property::IMAGE, propertyMap); application.GetScene().Add(imageView); DALI_TEST_CHECK(imageView); application.SendNotification(); + + // Wait for rasterization + DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION); + + application.SendNotification(); application.Render(16); - Vector3 naturalSize = imageView.GetNaturalSize(); + Vector3 naturalSize = imageView.GetNaturalSize(); DALI_TEST_EQUALS(naturalSize.width, 100.0f, TEST_LOCATION); DALI_TEST_EQUALS(naturalSize.height, 100.0f, TEST_LOCATION); } @@ -2773,12 +2767,8 @@ int UtcDaliImageViewSVGLoadingSyncSetInvalidValue(void) tet_infoline("ImageView Testing SVG image async loading"); - // Sync loading, automatic atlasing for small size image + // Sync loading { - TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace(); - callStack.Reset(); - callStack.Enable(true); - ImageView imageView = ImageView::New(); // Sync loading is used @@ -2797,7 +2787,13 @@ int UtcDaliImageViewSVGLoadingSyncSetInvalidValue(void) DALI_TEST_CHECK(imageView); application.SendNotification(); + + // Wait for rasterization + DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION); + + application.SendNotification(); application.Render(16); + Vector3 naturalSize = imageView.GetNaturalSize(); DALI_TEST_EQUALS(naturalSize.width, 100.0f, TEST_LOCATION); DALI_TEST_EQUALS(naturalSize.height, 100.0f, TEST_LOCATION); @@ -2834,6 +2830,11 @@ int UtcDaliImageViewSvgLoadingFailure(void) application.GetScene().Add(imageView); application.SendNotification(); + + // loading started, this waits for the loader thread + DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION); + + application.SendNotification(); application.Render(16); DALI_TEST_EQUALS(gResourceReadySignalFired, true, TEST_LOCATION); @@ -2858,6 +2859,11 @@ int UtcDaliImageViewSvgLoadingFailure(void) application.GetScene().Add(imageView); application.SendNotification(); + + // loading started, this waits for the loader thread + DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION); + + application.SendNotification(); application.Render(16); DALI_TEST_EQUALS(gResourceReadySignalFired, true, TEST_LOCATION); @@ -2883,6 +2889,11 @@ int UtcDaliImageViewSvgLoadingFailure(void) application.GetScene().Add(imageView); application.SendNotification(); + + // loading started, this waits for the loader thread + DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION); + + application.SendNotification(); application.Render(16); DALI_TEST_EQUALS(gResourceReadySignalFired, true, TEST_LOCATION); @@ -2993,6 +3004,44 @@ int UtcDaliImageViewSvgRasterizationFailure(void) END_TEST; } +int UtcDaliImageViewSvgChageSize(void) +{ + ToolkitTestApplication application; + + TestGlAbstraction& gl = application.GetGlAbstraction(); + TraceCallStack& textureTrace = gl.GetTextureTrace(); + textureTrace.Enable(true); + + ImageView imageView = ImageView::New(TEST_SVG_FILE_NAME); + application.GetScene().Add(imageView); + + application.SendNotification(); + + // loading started, this waits for the loader thread + DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION); + + application.SendNotification(); + application.Render(16); + + DALI_TEST_EQUALS(Test::VectorImageRenderer::GetLoadCount(), 1, TEST_LOCATION); + + // Change actor size, then rasterization should be done again + imageView.SetProperty(Actor::Property::SIZE, Vector2(200.f, 200.f)); + + application.SendNotification(); + + // loading started, this waits for the loader thread + DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION); + + application.SendNotification(); + application.Render(16); + + // We should not load the file again. + DALI_TEST_EQUALS(Test::VectorImageRenderer::GetLoadCount(), 1, TEST_LOCATION); + + END_TEST; +} + int UtcDaliImageViewTVGLoading(void) { ToolkitTestApplication application; @@ -3000,12 +3049,17 @@ int UtcDaliImageViewTVGLoading(void) tet_infoline("ImageView Testing TVG image loading"); { - ImageView imageView = ImageView::New(); - - imageView.SetImage(TEST_RESOURCE_DIR "/test.tvg"); - + ImageView imageView = ImageView::New(TEST_RESOURCE_DIR "/test.tvg"); application.GetScene().Add(imageView); DALI_TEST_CHECK(imageView); + + application.SendNotification(); + + DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION); + + application.SendNotification(); + application.Render(16); + Vector3 naturalSize = imageView.GetNaturalSize(); DALI_TEST_EQUALS(naturalSize.width, 100.0f, TEST_LOCATION); @@ -3581,7 +3635,7 @@ int UtcDaliImageViewCheckVariousCaseSendOnResourceReadySignal(void) // Test broken case TestResourceReadyUrl(1, 0, 0, "invalid.jpg", "", TEST_LOCATION); - TestResourceReadyUrl(0, 0, 0, "invalid.svg", "", TEST_LOCATION); // 0 rasterize + TestResourceReadyUrl(1, 0, 0, "invalid.svg", "", TEST_LOCATION); TestResourceReadyUrl(1, 0, 0, "invalid.9.png", "", TEST_LOCATION); TestResourceReadyUrl(1, 0, 0, "invalid.gif", "", TEST_LOCATION); // 1 image loading TestResourceReadyUrl(0, 0, 0, "invalid.json", "", TEST_LOCATION); // 0 rasterize @@ -3611,7 +3665,7 @@ int UtcDaliImageViewCheckVariousCaseSendOnResourceReadySignal(void) // Test broken case TestResourceReadyUrl(0, 1, 0, "invalid.jpg", "", TEST_LOCATION); - TestResourceReadyUrl(0, 1, 0, "invalid.svg", "", TEST_LOCATION); // 0 rasterize + TestResourceReadyUrl(0, 1, 0, "invalid.svg", "", TEST_LOCATION); TestResourceReadyUrl(0, 1, 0, "invalid.9.png", "", TEST_LOCATION); TestResourceReadyUrl(0, 1, 0, "invalid.gif", "", TEST_LOCATION); TestResourceReadyUrl(0, 1, 0, "invalid.json", "", TEST_LOCATION); // 0 rasterize diff --git a/automated-tests/src/dali-toolkit/utc-Dali-Visual.cpp b/automated-tests/src/dali-toolkit/utc-Dali-Visual.cpp index c17567a..bd3469a 100644 --- a/automated-tests/src/dali-toolkit/utc-Dali-Visual.cpp +++ b/automated-tests/src/dali-toolkit/utc-Dali-Visual.cpp @@ -325,7 +325,6 @@ int UtcDaliVisualSize(void) DALI_TEST_EQUALS(naturalSize, Vector2::ZERO, TEST_LOCATION); // animated gradient visual - Vector2 animated_gradient_visual_size(10.f, 10.f); propertyMap.Clear(); propertyMap.Insert(Toolkit::Visual::Property::TYPE, DevelVisual::ANIMATED_GRADIENT); Visual::Base animatedGradientVisual = factory.CreateVisual(propertyMap); @@ -333,20 +332,6 @@ int UtcDaliVisualSize(void) animatedGradientVisual.SetTransformAndSize(DefaultTransform(), controlSize); DALI_TEST_EQUALS(naturalSize, Vector2::ZERO, TEST_LOCATION); - // svg visual - Visual::Base svgVisual = factory.CreateVisual(TEST_SVG_FILE_NAME, ImageDimensions()); - svgVisual.GetNaturalSize(naturalSize); - // TEST_SVG_FILE: - // - // - // - DALI_TEST_EQUALS(naturalSize, Vector2(100.f, 100.f), TEST_LOCATION); - - // svg visual with a size - Visual::Base svgVisual2 = factory.CreateVisual(TEST_SVG_FILE_NAME, ImageDimensions(200, 200)); - svgVisual2.GetNaturalSize(naturalSize); - DALI_TEST_EQUALS(naturalSize, Vector2(100.f, 100.f), TEST_LOCATION); // Natural size should still be 100, 100 - // Text visual. // Load some fonts to get the same metrics on different platforms. diff --git a/dali-toolkit/internal/visuals/svg/svg-rasterize-thread.cpp b/dali-toolkit/internal/visuals/svg/svg-rasterize-thread.cpp index 08e416e..24f9981 100644 --- a/dali-toolkit/internal/visuals/svg/svg-rasterize-thread.cpp +++ b/dali-toolkit/internal/visuals/svg/svg-rasterize-thread.cpp @@ -33,8 +33,8 @@ namespace Toolkit { namespace Internal { -RasterizingTask::RasterizingTask(SvgVisual* svgRenderer, VectorImageRenderer vectorRenderer, const VisualUrl& url, float dpi, unsigned int width, unsigned int height) -: mSvgVisual(svgRenderer), +RasterizingTask::RasterizingTask(SvgVisual* svgVisual, VectorImageRenderer vectorRenderer, const VisualUrl& url, float dpi, unsigned int width, unsigned int height) +: mSvgVisual(svgVisual), mVectorRenderer(vectorRenderer), mUrl(url), mDpi(dpi), @@ -50,29 +50,41 @@ RasterizingTask::~RasterizingTask() void RasterizingTask::Load() { + if(mVectorRenderer.IsLoaded()) + { + // Already loaded + mLoadSuccess = true; + return; + } + + Dali::Vector buffer; + if(!mUrl.IsLocalResource()) { - Dali::Vector remoteBuffer; - if(!Dali::FileLoader::DownloadFileSynchronously(mUrl.GetUrl(), remoteBuffer)) + if(!Dali::FileLoader::DownloadFileSynchronously(mUrl.GetUrl(), buffer)) { - DALI_LOG_ERROR("RasterizingTask::Load: Failed to download file! [%s]\n", mUrl.GetUrl().c_str()); + DALI_LOG_ERROR("Failed to download file! [%s]\n", mUrl.GetUrl().c_str()); return; } - - remoteBuffer.PushBack('\0'); - - if(!mVectorRenderer.Load(remoteBuffer, mDpi)) + } + else + { + if(!Dali::FileLoader::ReadFile(mUrl.GetUrl(), buffer)) { - DALI_LOG_ERROR("RasterizingTask::Load:Failed to load data! [%s]\n", mUrl.GetUrl().c_str()); + DALI_LOG_ERROR("Failed to read file! [%s]\n", mUrl.GetUrl().c_str()); return; } - - mLoadSuccess = true; } - else + + buffer.PushBack('\0'); + + if(!mVectorRenderer.Load(buffer, mDpi)) { - mLoadSuccess = true; + DALI_LOG_ERROR("Failed to load data! [%s]\n", mUrl.GetUrl().c_str()); + return; } + + mLoadSuccess = true; } void RasterizingTask::Rasterize() @@ -87,11 +99,6 @@ void RasterizingTask::Rasterize() mPixelData = Devel::PixelBuffer::Convert(pixelBuffer); } -VectorImageRenderer RasterizingTask::GetVectorRenderer() const -{ - return mVectorRenderer; -} - bool RasterizingTask::IsLoaded() const { return mLoadSuccess; @@ -258,7 +265,7 @@ void SvgRasterizeThread::ApplyRasterizedSVGToSampler() { while(RasterizingTaskPtr task = NextCompletedTask()) { - task->GetSvgVisual()->ApplyRasterizedImage(task->GetVectorRenderer(), task->GetPixelData(), task->IsLoaded()); + task->GetSvgVisual()->ApplyRasterizedImage(task->GetPixelData(), task->IsLoaded()); } UnregisterProcessor(); diff --git a/dali-toolkit/internal/visuals/svg/svg-rasterize-thread.h b/dali-toolkit/internal/visuals/svg/svg-rasterize-thread.h index 0204bef..c0cf726 100644 --- a/dali-toolkit/internal/visuals/svg/svg-rasterize-thread.h +++ b/dali-toolkit/internal/visuals/svg/svg-rasterize-thread.h @@ -2,7 +2,7 @@ #define DALI_TOOLKIT_SVG_RASTERIZE_THREAD_H /* - * Copyright (c) 2021 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. @@ -29,7 +29,6 @@ #include #include #include -#include #include // INTERNAL INCLUDES @@ -60,12 +59,14 @@ class RasterizingTask : public RefObject public: /** * Constructor - * @param[in] svgRenderer The renderer which the rasterized image to be applied. + * @param[in] svgVisual The visual which the rasterized image to be applied. + * @param[in] vectorRenderer The vector rasterizer. * @param[in] url The URL to svg resource to use. + * @param[in] dpi The DPI of the screen. * @param[in] width The rasterization width. * @param[in] height The rasterization height. */ - RasterizingTask(SvgVisual* svgRenderer, VectorImageRenderer vectorRenderer, const VisualUrl& url, float dpi, unsigned int width, unsigned int height); + RasterizingTask(SvgVisual* svgVisual, VectorImageRenderer vectorRenderer, const VisualUrl& url, float dpi, unsigned int width, unsigned int height); /** * Destructor. @@ -73,6 +74,11 @@ public: ~RasterizingTask() override; /** + * Load svg file + */ + void Load(); + + /** * Do the rasterization with the mRasterizer. */ void Rasterize(); @@ -89,21 +95,11 @@ public: PixelData GetPixelData() const; /** - * Get the VectorRenderer. - * @return VectorRenderer. - */ - VectorImageRenderer GetVectorRenderer() const; - /** * Whether the resource is loaded. * @return True if the resource is loaded. */ bool IsLoaded() const; - /** - * Load svg file - */ - void Load(); - private: // Undefined RasterizingTask(const RasterizingTask& task); @@ -117,8 +113,8 @@ private: VisualUrl mUrl; PixelData mPixelData; float mDpi; - unsigned int mWidth; - unsigned int mHeight; + uint32_t mWidth; + uint32_t mHeight; bool mLoadSuccess; }; diff --git a/dali-toolkit/internal/visuals/svg/svg-visual.cpp b/dali-toolkit/internal/visuals/svg/svg-visual.cpp index 6851c75..bddb7f0 100644 --- a/dali-toolkit/internal/visuals/svg/svg-visual.cpp +++ b/dali-toolkit/internal/visuals/svg/svg-visual.cpp @@ -27,7 +27,6 @@ #include // EXTERNAL INCLUDES -#include #include #include @@ -49,7 +48,6 @@ const Dali::Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f); SvgVisualPtr SvgVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties) { SvgVisualPtr svgVisual(new SvgVisual(factoryCache, shaderFactory, imageUrl)); - svgVisual->Load(); svgVisual->SetProperties(properties); svgVisual->Initialize(); return svgVisual; @@ -58,7 +56,6 @@ SvgVisualPtr SvgVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderF SvgVisualPtr SvgVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl) { SvgVisualPtr svgVisual(new SvgVisual(factoryCache, shaderFactory, imageUrl)); - svgVisual->Load(); svgVisual->Initialize(); return svgVisual; } @@ -238,34 +235,6 @@ void SvgVisual::EnablePreMultipliedAlpha(bool preMultiplied) } } -void SvgVisual::Load() -{ - // load remote resource on svg rasterize thread. - if(mImageUrl.IsLocalResource()) - { - Dali::Vector buffer; - if(Dali::FileLoader::ReadFile(mImageUrl.GetUrl(), buffer)) - { - buffer.PushBack('\0'); - - Vector2 dpi = Stage::GetCurrent().GetDpi(); - float meanDpi = (dpi.height + dpi.width) * 0.5f; - if(!mVectorRenderer.Load(buffer, meanDpi)) - { - mLoadFailed = true; - DALI_LOG_ERROR("SvgVisual::Load: Failed to load file! [%s]\n", mImageUrl.GetUrl().c_str()); - return; - } - mVectorRenderer.GetDefaultSize(mDefaultWidth, mDefaultHeight); - } - else - { - mLoadFailed = true; - DALI_LOG_ERROR("SvgVisual::Load: Failed to read file! [%s]\n", mImageUrl.GetUrl().c_str()); - } - } -} - void SvgVisual::AddRasterizationTask(const Vector2& size) { if(mImpl->mRenderer) @@ -277,11 +246,12 @@ void SvgVisual::AddRasterizationTask(const Vector2& size) float meanDpi = (dpi.height + dpi.width) * 0.5f; RasterizingTaskPtr newTask = new RasterizingTask(this, mVectorRenderer, mImageUrl, meanDpi, width, height); + if(IsSynchronousLoadingRequired() && mImageUrl.IsLocalResource()) { newTask->Load(); newTask->Rasterize(); - ApplyRasterizedImage(newTask->GetVectorRenderer(), newTask->GetPixelData(), newTask->IsLoaded()); + ApplyRasterizedImage(newTask->GetPixelData(), newTask->IsLoaded()); } else { @@ -290,7 +260,7 @@ void SvgVisual::AddRasterizationTask(const Vector2& size) } } -void SvgVisual::ApplyRasterizedImage(VectorImageRenderer vectorRenderer, PixelData rasterizedPixelData, bool isLoaded) +void SvgVisual::ApplyRasterizedImage(PixelData rasterizedPixelData, bool isLoaded) { if(isLoaded && rasterizedPixelData && IsOnScene()) { @@ -388,8 +358,8 @@ void SvgVisual::OnSetTransform() { if(visualSize != mRasterizedSize || mDefaultWidth == 0 || mDefaultHeight == 0) { - AddRasterizationTask(visualSize); mRasterizedSize = visualSize; + AddRasterizationTask(visualSize); } } diff --git a/dali-toolkit/internal/visuals/svg/svg-visual.h b/dali-toolkit/internal/visuals/svg/svg-visual.h index 2b1dbe1..9dd6807 100644 --- a/dali-toolkit/internal/visuals/svg/svg-visual.h +++ b/dali-toolkit/internal/visuals/svg/svg-visual.h @@ -151,19 +151,13 @@ public: /** * @bried Apply the rasterized image to the visual. * - * @param[in] vectorImage The data of vector image. * @param[in] rasterizedPixelData The pixel buffer with the rasterized pixels * @param[in] bool Whether the resource is loaded */ - void ApplyRasterizedImage(VectorImageRenderer vectorImage, PixelData rasterizedPixelData, bool isLoaded); + void ApplyRasterizedImage(PixelData rasterizedPixelData, bool isLoaded); private: /** - * @brief Load the SVG Image from the set URL. - */ - void Load(); - - /** * @bried Rasterize the svg with the given size, and add it to the visual. * * @param[in] size The target size of the SVG rasterization. -- 2.7.4