Add AlphaMask to synchronousLoading
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / texture-manager-impl.cpp
index 2be3aaa..fbb8114 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.
@@ -85,6 +85,8 @@ Debug::Filter* gTextureManagerLogFilter = Debug::Filter::New( Debug::NoLogging,
     loadState == TextureManager::LOADING ? "LOADING" :                   \
     loadState == TextureManager::LOAD_FINISHED ? "LOAD_FINISHED" :       \
     loadState == TextureManager::WAITING_FOR_MASK ? "WAITING_FOR_MASK" : \
+    loadState == TextureManager::MASK_APPLYING ? "MASK_APPLYING" :         \
+    loadState == TextureManager::MASK_APPLIED ? "MASK_APPLIED" :         \
     loadState == TextureManager::UPLOADED ? "UPLOADED" :                 \
     loadState == TextureManager::CANCELLED ? "CANCELLED" :               \
     loadState == TextureManager::LOAD_FAILED ? "LOAD_FAILED" : "Unknown"
@@ -180,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 );
@@ -241,8 +252,9 @@ TextureSet TextureManager::LoadTexture(
       }
       else
       {
+        TextureId alphaMaskId = RequestMaskLoad( maskInfo->mAlphaMaskUrl );
         textureId = RequestLoad( url,
-                                 maskInfo->mAlphaMaskId,
+                                 alphaMaskId,
                                  maskInfo->mContentScaleFactor,
                                  desiredSize,
                                  fittingMode, samplingMode,
@@ -263,6 +275,9 @@ TextureSet TextureManager::LoadTexture(
       // If we are loading the texture, or waiting for the ready signal handler to complete, inform
       // caller that they need to wait.
       loadingStatus = ( loadState == TextureManager::LOADING ||
+                        loadState == TextureManager::WAITING_FOR_MASK ||
+                        loadState == TextureManager::MASK_APPLYING ||
+                        loadState == TextureManager::MASK_APPLIED ||
                         loadState == TextureManager::NOT_STARTED ||
                         mQueueLoadFlag );
 
@@ -362,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 );
   }
@@ -393,7 +412,11 @@ TextureManager::TextureId TextureManager::RequestLoadInternal(
                  GET_LOAD_STATE_STRING(textureInfo.loadState ) );
 
   // Force reloading of texture by setting loadState unless already loading or cancelled.
-  if ( TextureManager::ReloadPolicy::FORCED == reloadPolicy && TextureManager::LOADING != textureInfo.loadState &&
+  if ( TextureManager::ReloadPolicy::FORCED == reloadPolicy &&
+       TextureManager::LOADING != textureInfo.loadState &&
+       TextureManager::WAITING_FOR_MASK != textureInfo.loadState &&
+       TextureManager::MASK_APPLYING != textureInfo.loadState &&
+       TextureManager::MASK_APPLIED != textureInfo.loadState &&
        TextureManager::CANCELLED != textureInfo.loadState )
   {
     DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Verbose, "TextureManager::RequestLoad( url=%s observer=%p ) ForcedReload cacheIndex:%d, textureId=%d\n",
@@ -412,6 +435,9 @@ TextureManager::TextureId TextureManager::RequestLoadInternal(
       break;
     }
     case TextureManager::LOADING:
+    case TextureManager::WAITING_FOR_MASK:
+    case TextureManager::MASK_APPLYING:
+    case TextureManager::MASK_APPLIED:
     {
       ObserveTexture( textureInfo, observer );
       break;
@@ -433,7 +459,6 @@ TextureManager::TextureId TextureManager::RequestLoadInternal(
       break;
     }
     case TextureManager::LOAD_FINISHED:
-    case TextureManager::WAITING_FOR_MASK:
       // Loading has already completed. Do nothing.
       break;
   }
@@ -648,7 +673,6 @@ void TextureManager::LoadOrQueueTexture( TextureInfo& textureInfo, TextureUpload
       }
       break;
     }
-    case LOADING:
     case UPLOADED:
     {
       if( mQueueLoadFlag )
@@ -665,9 +689,12 @@ void TextureManager::LoadOrQueueTexture( TextureInfo& textureInfo, TextureUpload
       }
       break;
     }
+    case LOADING:
     case CANCELLED:
     case LOAD_FINISHED:
     case WAITING_FOR_MASK:
+    case MASK_APPLYING:
+    case MASK_APPLIED:
     {
       break;
     }
@@ -690,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,
@@ -787,18 +815,26 @@ void TextureManager::PostLoad( TextureInfo& textureInfo, Devel::PixelBuffer& pix
       // wait for the mask to finish loading.
       if( textureInfo.maskTextureId != INVALID_TEXTURE_ID )
       {
-        LoadState maskLoadState = GetTextureStateInternal( textureInfo.maskTextureId );
-        if( maskLoadState == LOADING )
-        {
-          textureInfo.pixelBuffer = pixelBuffer; // Store the pixel buffer temporarily
-          textureInfo.loadState = WAITING_FOR_MASK;
-        }
-        else if( maskLoadState == LOAD_FINISHED )
+        if( textureInfo.loadState == MASK_APPLYING )
         {
-          ApplyMask( pixelBuffer, textureInfo.maskTextureId, textureInfo.scaleFactor, textureInfo.cropToMask );
+          textureInfo.loadState = MASK_APPLIED;
           UploadTexture( pixelBuffer, textureInfo );
           NotifyObservers( textureInfo, true );
         }
+        else
+        {
+          LoadState maskLoadState = GetTextureStateInternal( textureInfo.maskTextureId );
+          textureInfo.pixelBuffer = pixelBuffer; // Store the pixel buffer temporarily
+          if( maskLoadState == LOADING )
+          {
+            textureInfo.loadState = WAITING_FOR_MASK;
+          }
+          else if( maskLoadState == LOAD_FINISHED )
+          {
+            // Send New Task to Thread
+            ApplyMask( textureInfo, textureInfo.maskTextureId );
+          }
+        }
       }
       else
       {
@@ -818,7 +854,6 @@ void TextureManager::PostLoad( TextureInfo& textureInfo, Devel::PixelBuffer& pix
   }
   else
   {
-    DALI_LOG_ERROR( "TextureManager::AsyncImageLoad(%s) failed\n", textureInfo.url.GetUrl().c_str() );
     // @todo If the load was unsuccessful, upload the broken image.
     textureInfo.loadState = LOAD_FAILED;
     CheckForWaitingTexture( textureInfo );
@@ -838,18 +873,15 @@ void TextureManager::CheckForWaitingTexture( TextureInfo& maskTextureInfo )
         mTextureInfoContainer[cacheIndex].loadState == WAITING_FOR_MASK )
     {
       TextureInfo& textureInfo( mTextureInfoContainer[cacheIndex] );
-      Devel::PixelBuffer pixelBuffer = textureInfo.pixelBuffer;
-      textureInfo.pixelBuffer.Reset();
 
       if( maskTextureInfo.loadState == LOAD_FINISHED )
       {
-        ApplyMask( pixelBuffer, maskTextureInfo.textureId, textureInfo.scaleFactor, textureInfo.cropToMask );
-        UploadTexture( pixelBuffer, textureInfo );
-        NotifyObservers( textureInfo, true );
+        // Send New Task to Thread
+        ApplyMask( textureInfo, maskTextureInfo.textureId );
       }
       else
       {
-        DALI_LOG_ERROR( "TextureManager::ApplyMask to %s failed\n", textureInfo.url.GetUrl().c_str() );
+        textureInfo.pixelBuffer.Reset();
         textureInfo.loadState = LOAD_FAILED;
         NotifyObservers( textureInfo, false );
       }
@@ -857,15 +889,24 @@ void TextureManager::CheckForWaitingTexture( TextureInfo& maskTextureInfo )
   }
 }
 
-void TextureManager::ApplyMask(
-  Devel::PixelBuffer& pixelBuffer, TextureId maskTextureId,
-  float contentScale, bool cropToMask )
+void TextureManager::ApplyMask( TextureInfo& textureInfo, TextureId maskTextureId )
 {
   int maskCacheIndex = GetCacheIndexFromId( maskTextureId );
   if( maskCacheIndex != INVALID_CACHE_INDEX )
   {
     Devel::PixelBuffer maskPixelBuffer = mTextureInfoContainer[maskCacheIndex].pixelBuffer;
-    pixelBuffer.ApplyMask( maskPixelBuffer, contentScale, cropToMask );
+    Devel::PixelBuffer pixelBuffer = textureInfo.pixelBuffer;
+    textureInfo.pixelBuffer.Reset();
+
+    DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise, "TextureManager::ApplyMask(): url:%s sync:%s\n",
+                   textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously?"T":"F" );
+
+    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, premultiplyOnLoad );
   }
 }
 
@@ -1121,7 +1162,19 @@ void TextureManager::AsyncLoadingHelper::Load(TextureId          textureId,
                                               DevelAsyncImageLoader::PreMultiplyOnLoad  preMultiplyOnLoad)
 {
   mLoadingInfoContainer.push_back(AsyncLoadingInfo(textureId));
-  auto id = DevelAsyncImageLoader::Load(mLoader, url.GetUrl(), desiredSize, fittingMode, samplingMode, orientationCorrection, preMultiplyOnLoad);
+  auto id = DevelAsyncImageLoader::Load( mLoader, url.GetUrl(), desiredSize, fittingMode, samplingMode, orientationCorrection, preMultiplyOnLoad );
+  mLoadingInfoContainer.back().loadId = id;
+}
+
+void TextureManager::AsyncLoadingHelper::ApplyMask( TextureId textureId,
+                                                    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, preMultiplyOnLoad );
   mLoadingInfoContainer.back().loadId = id;
 }
 
@@ -1143,9 +1196,9 @@ TextureManager::AsyncLoadingHelper::AsyncLoadingHelper(
 }
 
 void TextureManager::AsyncLoadingHelper::AsyncLoadComplete(uint32_t           id,
-                                                           Devel::PixelBuffer pixelBuffer)
+                                                           Devel::PixelBuffer pixelBuffer )
 {
-  mTextureManager.AsyncLoadComplete(mLoadingInfoContainer, id, pixelBuffer);
+  mTextureManager.AsyncLoadComplete( mLoadingInfoContainer, id, pixelBuffer );
 }
 
 void TextureManager::SetBrokenImageUrl(const std::string& brokenImageUrl)