From 826db34a0666662f7cd8adb89f25c9c9b0c01b5f Mon Sep 17 00:00:00 2001 From: Heeyong Song Date: Tue, 4 Oct 2022 11:58:40 +0900 Subject: [PATCH] (SVG) Support desired size Change-Id: I2b27ac032f6fa322525af76ddb9b38012b24746d --- .../src/dali-toolkit/utc-Dali-ImageView.cpp | 60 ++++++++++++++++++++ dali-toolkit/internal/visuals/svg/svg-visual.cpp | 64 ++++++++++++++++++---- dali-toolkit/internal/visuals/svg/svg-visual.h | 7 ++- .../internal/visuals/visual-factory-impl.cpp | 2 +- 4 files changed, 120 insertions(+), 13 deletions(-) diff --git a/automated-tests/src/dali-toolkit/utc-Dali-ImageView.cpp b/automated-tests/src/dali-toolkit/utc-Dali-ImageView.cpp index dbd84e6..dd400db 100644 --- a/automated-tests/src/dali-toolkit/utc-Dali-ImageView.cpp +++ b/automated-tests/src/dali-toolkit/utc-Dali-ImageView.cpp @@ -3245,6 +3245,66 @@ int UtcDaliImageViewTVGLoading(void) END_TEST; } +int UtcDaliImageViewSvgDesiredSize01(void) +{ + ToolkitTestApplication application; + + TestGlAbstraction& gl = application.GetGlAbstraction(); + TraceCallStack& textureTrace = gl.GetTextureTrace(); + textureTrace.Enable(true); + + int desiredWidth = 100, desiredHeight = 150; + ImageView imageView = ImageView::New(TEST_SVG_FILE_NAME, ImageDimensions(desiredWidth, desiredHeight)); + + application.GetScene().Add(imageView); + + application.SendNotification(); + + // Wait for loading & rasterization + DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(2), true, TEST_LOCATION); + + application.SendNotification(); + application.Render(16); + + { + std::stringstream out; + out << GL_TEXTURE_2D << ", " << 0u << ", " << desiredWidth << ", " << desiredHeight; + DALI_TEST_CHECK(textureTrace.FindMethodAndParams("TexImage2D", out.str().c_str())); + } + + END_TEST; +} + +int UtcDaliImageViewSvgDesiredSize02(void) +{ + ToolkitTestApplication application; + + TestGlAbstraction& gl = application.GetGlAbstraction(); + TraceCallStack& textureTrace = gl.GetTextureTrace(); + textureTrace.Enable(true); + + int desiredWidth = 150, desiredHeight = 100; + ImageView imageView = ImageView::New(); + imageView[ImageView::Property::IMAGE] = Property::Map().Add("url", TEST_SVG_FILE_NAME).Add("desiredWidth", desiredWidth).Add("desiredHeight", desiredHeight); + application.GetScene().Add(imageView); + + application.SendNotification(); + + // Wait for loading & rasterization + DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(2), true, TEST_LOCATION); + + application.SendNotification(); + application.Render(16); + + { + std::stringstream out; + out << GL_TEXTURE_2D << ", " << 0u << ", " << desiredWidth << ", " << desiredHeight; + DALI_TEST_CHECK(textureTrace.FindMethodAndParams("TexImage2D", out.str().c_str())); + } + + END_TEST; +} + int UtcDaliImageViewImageLoadFailure01(void) { ToolkitTestApplication application; diff --git a/dali-toolkit/internal/visuals/svg/svg-visual.cpp b/dali-toolkit/internal/visuals/svg/svg-visual.cpp index a0ced87..cd59107 100644 --- a/dali-toolkit/internal/visuals/svg/svg-visual.cpp +++ b/dali-toolkit/internal/visuals/svg/svg-visual.cpp @@ -48,20 +48,20 @@ 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)); + SvgVisualPtr svgVisual(new SvgVisual(factoryCache, shaderFactory, imageUrl, ImageDimensions{})); svgVisual->SetProperties(properties); svgVisual->Initialize(); return svgVisual; } -SvgVisualPtr SvgVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl) +SvgVisualPtr SvgVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, ImageDimensions size) { - SvgVisualPtr svgVisual(new SvgVisual(factoryCache, shaderFactory, imageUrl)); + SvgVisualPtr svgVisual(new SvgVisual(factoryCache, shaderFactory, imageUrl, size)); svgVisual->Initialize(); return svgVisual; } -SvgVisual::SvgVisual(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl) +SvgVisual::SvgVisual(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, ImageDimensions size) : Visual::Base(factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::SVG), mImageVisualShaderFactory(shaderFactory), mAtlasRect(FULL_TEXTURE_RECT), @@ -71,6 +71,7 @@ SvgVisual::SvgVisual(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& mDefaultHeight(0), mPlacementActor(), mRasterizedSize(Vector2::ZERO), + mDesiredSize(size), mLoadFailed(false), mAttemptAtlasing(false) { @@ -122,6 +123,14 @@ void SvgVisual::DoSetProperties(const Property::Map& propertyMap) { DoSetProperty(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second); } + else if(keyValue.first == IMAGE_DESIRED_WIDTH) + { + DoSetProperty(Toolkit::ImageVisual::Property::DESIRED_WIDTH, keyValue.second); + } + else if(keyValue.first == IMAGE_DESIRED_HEIGHT) + { + DoSetProperty(Toolkit::ImageVisual::Property::DESIRED_HEIGHT, keyValue.second); + } } } @@ -154,6 +163,24 @@ void SvgVisual::DoSetProperty(Property::Index index, const Property::Value& valu } break; } + case Toolkit::ImageVisual::Property::DESIRED_WIDTH: + { + int32_t desiredWidth = 0; + if(value.Get(desiredWidth)) + { + mDesiredSize.SetWidth(desiredWidth); + } + break; + } + case Toolkit::ImageVisual::Property::DESIRED_HEIGHT: + { + int32_t desiredHeight = 0; + if(value.Get(desiredHeight)) + { + mDesiredSize.SetHeight(desiredHeight); + } + break; + } } } @@ -202,7 +229,12 @@ void SvgVisual::DoSetOffScene(Actor& actor) void SvgVisual::GetNaturalSize(Vector2& naturalSize) { - if(mLoadFailed && mImpl->mRenderer) + if(mDesiredSize.GetWidth() > 0 && mDesiredSize.GetHeight() > 0) + { + naturalSize.x = mDesiredSize.GetWidth(); + naturalSize.y = mDesiredSize.GetHeight(); + } + else if(mLoadFailed && mImpl->mRenderer) { // Load failed, use broken image size auto textureSet = mImpl->mRenderer.GetTextures(); @@ -234,6 +266,8 @@ void SvgVisual::DoCreatePropertyMap(Property::Map& map) const map.Insert(Toolkit::ImageVisual::Property::ATLASING, mAttemptAtlasing); } map.Insert(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, IsSynchronousLoadingRequired()); + map.Insert(Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth()); + map.Insert(Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight()); } void SvgVisual::DoCreateInstancePropertyMap(Property::Map& map) const @@ -367,14 +401,24 @@ void SvgVisual::ApplyRasterizedImage(PixelData rasterizedPixelData, bool success void SvgVisual::OnSetTransform() { - Vector2 visualSize = mImpl->mTransform.GetVisualSize(mImpl->mControlSize); - if(IsOnScene() && !mLoadFailed) { - if(visualSize != mRasterizedSize || mDefaultWidth == 0 || mDefaultHeight == 0) + Vector2 size; + if(mDesiredSize.GetWidth() > 0 && mDesiredSize.GetHeight() > 0) + { + // Use desired size + size = Vector2(mDesiredSize.GetWidth(), mDesiredSize.GetHeight()); + } + else + { + // Use visual size + size = mImpl->mTransform.GetVisualSize(mImpl->mControlSize); + } + + if(size != mRasterizedSize || mDefaultWidth == 0 || mDefaultHeight == 0) { - mRasterizedSize = visualSize; - AddRasterizationTask(visualSize); + mRasterizedSize = size; + AddRasterizationTask(size); } } diff --git a/dali-toolkit/internal/visuals/svg/svg-visual.h b/dali-toolkit/internal/visuals/svg/svg-visual.h index 5c606f1..93decde 100644 --- a/dali-toolkit/internal/visuals/svg/svg-visual.h +++ b/dali-toolkit/internal/visuals/svg/svg-visual.h @@ -72,9 +72,10 @@ public: * @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 + * @param[in] size The width and height of the rasterized buffer. The visual size will be used if these are 0. * @return A smart-pointer to the newly allocated visual. */ - static SvgVisualPtr New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl); + static SvgVisualPtr New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, ImageDimensions size = ImageDimensions()); public: // from Visual /** @@ -104,8 +105,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 + * @param[in] size The width and height of the rasterized buffer. The visual size will be used if these are 0. */ - SvgVisual(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl); + SvgVisual(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, ImageDimensions size); /** * @brief A reference counted object may only be deleted by calling Unreference(). @@ -186,6 +188,7 @@ private: uint32_t mDefaultHeight; WeakHandle mPlacementActor; Vector2 mRasterizedSize; + Dali::ImageDimensions mDesiredSize{}; bool mLoadFailed; bool mAttemptAtlasing; ///< If true will attempt atlasing, otherwise create unique texture }; diff --git a/dali-toolkit/internal/visuals/visual-factory-impl.cpp b/dali-toolkit/internal/visuals/visual-factory-impl.cpp index f5bed54..60fb25e 100644 --- a/dali-toolkit/internal/visuals/visual-factory-impl.cpp +++ b/dali-toolkit/internal/visuals/visual-factory-impl.cpp @@ -332,7 +332,7 @@ Toolkit::Visual::Base VisualFactory::CreateVisual(const std::string& url, ImageD case VisualUrl::TVG: case VisualUrl::SVG: { - visualPtr = SvgVisual::New(GetFactoryCache(), GetImageVisualShaderFactory(), visualUrl); + visualPtr = SvgVisual::New(GetFactoryCache(), GetImageVisualShaderFactory(), visualUrl, size); break; } case VisualUrl::GIF: -- 2.7.4