Fix an issue not to unload alpha mask texture
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / texture-manager-impl.cpp
index 9a06862..a2381e8 100644 (file)
@@ -1,5 +1,5 @@
  /*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2020 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.
@@ -146,7 +146,7 @@ TextureManager::~TextureManager()
 
 TextureSet TextureManager::LoadTexture(
   const VisualUrl& url, Dali::ImageDimensions desiredSize, Dali::FittingMode::Type fittingMode,
-  Dali::SamplingMode::Type samplingMode, const MaskingDataPointer& maskInfo,
+  Dali::SamplingMode::Type samplingMode, MaskingDataPointer& maskInfo,
   bool synchronousLoading, TextureManager::TextureId& textureId, Vector4& textureRect,
   Dali::ImageDimensions& textureRectSize, bool& atlasingStatus, bool& loadingStatus,
   Dali::WrapMode::Type wrapModeU, Dali::WrapMode::Type wrapModeV, TextureUploadObserver* textureObserver,
@@ -182,6 +182,15 @@ TextureSet TextureManager::LoadTexture(
     {
       Devel::PixelBuffer pixelBuffer = LoadImageFromFile( url.GetUrl(), desiredSize, fittingMode, samplingMode,
                                        orientationCorrection  );
+      if( maskInfo )
+      {
+        Devel::PixelBuffer maskPixelBuffer = LoadImageFromFile( maskInfo->mAlphaMaskUrl.GetUrl(), ImageDimensions(),
+                                             FittingMode::SCALE_TO_FILL, SamplingMode::NO_FILTER, true  );
+        if( maskPixelBuffer )
+        {
+          pixelBuffer.ApplyMask( maskPixelBuffer, maskInfo->mContentScaleFactor, maskInfo->mCropToMask );
+        }
+      }
       if( pixelBuffer )
       {
         PreMultiply( pixelBuffer, preMultiplyOnLoad );
@@ -243,6 +252,7 @@ TextureSet TextureManager::LoadTexture(
       }
       else
       {
+        maskInfo->mAlphaMaskId = RequestMaskLoad( maskInfo->mAlphaMaskUrl );
         textureId = RequestLoad( url,
                                  maskInfo->mAlphaMaskId,
                                  maskInfo->mContentScaleFactor,
@@ -367,6 +377,10 @@ TextureManager::TextureId TextureManager::RequestLoadInternal(
       ++( mTextureInfoContainer[ cacheIndex ].referenceCount );
     }
     textureId = mTextureInfoContainer[ cacheIndex ].textureId;
+
+    // Update preMultiplyOnLoad value. It should be changed according to preMultiplied value of the cached info.
+    preMultiplyOnLoad = mTextureInfoContainer[ cacheIndex ].preMultiplied ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
+
     DALI_LOG_INFO( gTextureManagerLogFilter, Debug::General, "TextureManager::RequestLoad( url=%s observer=%p ) Using cached texture id@%d, textureId=%d\n",
                    url.GetUrl().c_str(), observer, cacheIndex, textureId );
   }
@@ -703,7 +717,8 @@ void TextureManager::LoadTexture( TextureInfo& textureInfo, TextureUploadObserve
   {
     auto& loadersContainer = textureInfo.url.IsLocalResource() ? mAsyncLocalLoaders : mAsyncRemoteLoaders;
     auto loadingHelperIt = loadersContainer.GetNext();
-    auto premultiplyOnLoad = textureInfo.preMultiplyOnLoad? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
+    auto premultiplyOnLoad = ( textureInfo.preMultiplyOnLoad && textureInfo.maskTextureId == INVALID_TEXTURE_ID ) ?
+                               DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
     DALI_ASSERT_ALWAYS(loadingHelperIt != loadersContainer.End());
     loadingHelperIt->Load(textureInfo.textureId, textureInfo.url,
                           textureInfo.desiredSize, textureInfo.fittingMode,
@@ -889,8 +904,9 @@ void TextureManager::ApplyMask( TextureInfo& textureInfo, TextureId maskTextureI
     textureInfo.loadState = MASK_APPLYING;
     auto& loadersContainer = textureInfo.url.IsLocalResource() ? mAsyncLocalLoaders : mAsyncRemoteLoaders;
     auto loadingHelperIt = loadersContainer.GetNext();
+    auto premultiplyOnLoad = textureInfo.preMultiplyOnLoad ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
     DALI_ASSERT_ALWAYS(loadingHelperIt != loadersContainer.End());
-    loadingHelperIt->ApplyMask( textureInfo.textureId, pixelBuffer, maskPixelBuffer, textureInfo.scaleFactor, textureInfo.cropToMask );
+    loadingHelperIt->ApplyMask( textureInfo.textureId, pixelBuffer, maskPixelBuffer, textureInfo.scaleFactor, textureInfo.cropToMask, premultiplyOnLoad );
   }
 }
 
@@ -1151,13 +1167,14 @@ void TextureManager::AsyncLoadingHelper::Load(TextureId          textureId,
 }
 
 void TextureManager::AsyncLoadingHelper::ApplyMask( TextureId textureId,
-                                               Devel::PixelBuffer pixelBuffer,
-                                               Devel::PixelBuffer maskPixelBuffer,
-                                               float contentScale,
-                                               bool cropToMask )
+                                                    Devel::PixelBuffer pixelBuffer,
+                                                    Devel::PixelBuffer maskPixelBuffer,
+                                                    float contentScale,
+                                                    bool cropToMask,
+                                                    DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad )
 {
   mLoadingInfoContainer.push_back(AsyncLoadingInfo(textureId));
-  auto id = DevelAsyncImageLoader::ApplyMask( mLoader, pixelBuffer, maskPixelBuffer, contentScale, cropToMask );
+  auto id = DevelAsyncImageLoader::ApplyMask( mLoader, pixelBuffer, maskPixelBuffer, contentScale, cropToMask, preMultiplyOnLoad );
   mLoadingInfoContainer.back().loadId = id;
 }