From e15a4820108d180331611d918b383771f16c1c9e Mon Sep 17 00:00:00 2001 From: Eunki Hong Date: Tue, 10 Aug 2021 23:42:00 -0700 Subject: [PATCH] Disconnect relationship with VisualUrl and VisualFactory When VisualFactory destory, VisualFactoryCache also destroy, and TextureManager also destory. At this time, mTextureInfoContainer clear, and TextureInfo's VisualUrl destroy. When VisualUrl destuctor called, we call VisaulFactory::Get() and try to get TextureManager. But We are now on VisualFactory's destroy called... This is why image-view-encoded-image-buffer.example make Segfault. This patch move previous VisualUrl's job to TextureManager and ImageVisual. Change-Id: Iefc6fe7f8bda6cee08fdaafded4979935cbc87a2 Signed-off-by: Eunki Hong --- .../internal/visuals/image/image-visual.cpp | 26 +++++++++ .../internal/visuals/texture-manager-impl.cpp | 9 ++++ dali-toolkit/internal/visuals/visual-url.cpp | 62 ---------------------- 3 files changed, 35 insertions(+), 62 deletions(-) diff --git a/dali-toolkit/internal/visuals/image/image-visual.cpp b/dali-toolkit/internal/visuals/image/image-visual.cpp index 16a4a4a..4ae09f6 100644 --- a/dali-toolkit/internal/visuals/image/image-visual.cpp +++ b/dali-toolkit/internal/visuals/image/image-visual.cpp @@ -187,6 +187,23 @@ ImageVisual::~ImageVisual() } } + if(mImageUrl.IsValid()) + { + // Decrease reference count of External Resources : + // EncodedImageBuffer or ExternalTextures. + // Ensure the stage is still valid before accessing texture manager. + if(mImageUrl.GetProtocolType() == VisualUrl::TEXTURE) + { + TextureManager& textureManager = mFactoryCache.GetTextureManager(); + textureManager.RemoveExternalTexture(mImageUrl.GetUrl()); + } + else if(mImageUrl.IsBufferResource()) + { + TextureManager& textureManager = mFactoryCache.GetTextureManager(); + textureManager.RemoveExternalEncodedImageBuffer(mImageUrl.GetUrl()); + } + } + // ImageVisual destroyed so remove texture unless ReleasePolicy is set to never release if((mTextureId != TextureManager::INVALID_TEXTURE_ID) && (mReleasePolicy != Toolkit::ImageVisual::ReleasePolicy::NEVER)) { @@ -530,6 +547,15 @@ void ImageVisual::OnInitialize() } } + // Increase reference count of External Resources : + // EncodedImageBuffer or ExternalTextures. + // Reference count will be decreased at destructor of the visual. + if(mImageUrl.IsValid() && (mImageUrl.IsBufferResource() || mImageUrl.GetProtocolType() == VisualUrl::TEXTURE)) + { + TextureManager& textureManager = mFactoryCache.GetTextureManager(); + textureManager.UseExternalResource(mImageUrl.GetUrl()); + } + Shader shader = GetShader(); // Create the renderer diff --git a/dali-toolkit/internal/visuals/texture-manager-impl.cpp b/dali-toolkit/internal/visuals/texture-manager-impl.cpp index c6fc0c2..093954e 100644 --- a/dali-toolkit/internal/visuals/texture-manager-impl.cpp +++ b/dali-toolkit/internal/visuals/texture-manager-impl.cpp @@ -469,6 +469,10 @@ TextureManager::TextureId TextureManager::RequestLoadInternal( if(encodedImageBuffer) { textureId = targetId; + + // Increase EncodedImageBuffer reference during it contains mTextureInfoContainer. + UseExternalResource(url.GetUrl()); + // Insert this buffer at mTextureInfoContainer. // This buffer will decode at ImageLoaderThread. bool preMultiply = (preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD); @@ -604,6 +608,11 @@ void TextureManager::Remove(const TextureManager::TextureId textureId, TextureUp // If the state allows us to remove the TextureInfo data, we do so. if(removeTextureInfo) { + // If url location is BUFFER, decrease reference count of EncodedImageBuffer. + if(textureInfo.url.IsBufferResource()) + { + RemoveExternalEncodedImageBuffer(textureInfo.url.GetUrl()); + } // Permanently remove the textureInfo struct. mTextureInfoContainer.erase(mTextureInfoContainer.begin() + textureInfoIndex); } diff --git a/dali-toolkit/internal/visuals/visual-url.cpp b/dali-toolkit/internal/visuals/visual-url.cpp index 70c6dc2..30d9c02 100644 --- a/dali-toolkit/internal/visuals/visual-url.cpp +++ b/dali-toolkit/internal/visuals/visual-url.cpp @@ -20,10 +20,6 @@ // EXTERNAL HEADERS #include // for toupper() -// INTERNAL HEADERS -#include -#include - namespace Dali { namespace Toolkit @@ -228,14 +224,6 @@ VisualUrl::VisualUrl(const std::string& url) // TEXTURE and BUFFER location url doesn't need type resolving, REGULAR_IMAGE is fine mType = ResolveType(url); } - else - { - Toolkit::VisualFactory factory = Toolkit::VisualFactory::Get(); - if(factory) - { - GetImplementation(factory).GetTextureManager().UseExternalResource(*this); - } - } } } @@ -244,69 +232,19 @@ VisualUrl::VisualUrl(const VisualUrl& url) mType(url.mType), mLocation(url.mLocation) { - if(VisualUrl::TEXTURE == mLocation || VisualUrl::BUFFER == mLocation) - { - Toolkit::VisualFactory factory = Toolkit::VisualFactory::Get(); - if(factory) - { - GetImplementation(factory).GetTextureManager().UseExternalResource(*this); - } - } } VisualUrl::~VisualUrl() { - if(VisualUrl::TEXTURE == mLocation) - { - Toolkit::VisualFactory factory = Toolkit::VisualFactory::Get(); - if(factory) - { - GetImplementation(factory).GetTextureManager().RemoveExternalTexture(mUrl); - } - } - else if(VisualUrl::BUFFER == mLocation) - { - Toolkit::VisualFactory factory = Toolkit::VisualFactory::Get(); - if(factory) - { - GetImplementation(factory).GetTextureManager().RemoveExternalEncodedImageBuffer(mUrl); - } - } } VisualUrl& VisualUrl::operator=(const VisualUrl& url) { if(&url != this) { - if(VisualUrl::TEXTURE == mLocation) - { - Toolkit::VisualFactory factory = Toolkit::VisualFactory::Get(); - if(factory) - { - GetImplementation(factory).GetTextureManager().RemoveExternalTexture(mUrl); - } - } - else if(VisualUrl::BUFFER == mLocation) - { - Toolkit::VisualFactory factory = Toolkit::VisualFactory::Get(); - if(factory) - { - GetImplementation(factory).GetTextureManager().RemoveExternalEncodedImageBuffer(mUrl); - } - } - mUrl = url.mUrl; mType = url.mType; mLocation = url.mLocation; - - if(VisualUrl::TEXTURE == mLocation || VisualUrl::BUFFER == mLocation) - { - Toolkit::VisualFactory factory = Toolkit::VisualFactory::Get(); - if(factory) - { - GetImplementation(factory).GetTextureManager().UseExternalResource(*this); - } - } } return *this; } -- 2.7.4