[dali_2.1.26] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / texture-manager / texture-manager-impl.cpp
index 81a73db..445ad65 100644 (file)
@@ -113,14 +113,18 @@ TextureManager::MaskingData::MaskingData()
 : mAlphaMaskUrl(),
   mAlphaMaskId(INVALID_TEXTURE_ID),
   mContentScaleFactor(1.0f),
-  mCropToMask(true)
+  mCropToMask(true),
+  mPreappliedMasking(true),
+  mMaskImageLoadingFailed(false)
 {
 }
 
 TextureManager::TextureManager()
 : mTextureCacheManager(),
-  mAsyncLocalLoaders(GetNumberOfLocalLoaderThreads(), [&]() { return TextureAsyncLoadingHelper(*this); }),
-  mAsyncRemoteLoaders(GetNumberOfRemoteLoaderThreads(), [&]() { return TextureAsyncLoadingHelper(*this); }),
+  mAsyncLocalLoaders(GetNumberOfLocalLoaderThreads(), [&]()
+                     { return TextureAsyncLoadingHelper(*this); }),
+  mAsyncRemoteLoaders(GetNumberOfRemoteLoaderThreads(), [&]()
+                      { return TextureAsyncLoadingHelper(*this); }),
   mLifecycleObservers(),
   mLoadQueue(),
   mRemoveQueue(),
@@ -148,7 +152,8 @@ TextureSet TextureManager::LoadAnimatedImageTexture(
   const Dali::WrapMode::Type&     wrapModeU,
   const Dali::WrapMode::Type&     wrapModeV,
   const bool&                     synchronousLoading,
-  TextureUploadObserver*          textureObserver)
+  TextureUploadObserver*          textureObserver,
+  TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
 {
   TextureSet textureSet;
 
@@ -165,18 +170,34 @@ TextureSet TextureManager::LoadAnimatedImageTexture(
     }
     else
     {
+      Texture maskTexture;
       if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
       {
         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(!maskInfo->mPreappliedMasking)
+          {
+            PixelData maskPixelData = Devel::PixelBuffer::Convert(maskPixelBuffer); // takes ownership of buffer
+            maskTexture             = Texture::New(Dali::TextureType::TEXTURE_2D, maskPixelData.GetPixelFormat(), maskPixelData.GetWidth(), maskPixelData.GetHeight());
+            maskTexture.Upload(maskPixelData);
+          }
+          else
+          {
+            pixelBuffer.ApplyMask(maskPixelBuffer, maskInfo->mContentScaleFactor, maskInfo->mCropToMask);
+          }
         }
         else
         {
           DALI_LOG_ERROR("TextureManager::LoadAnimatedImageTexture: Synchronous mask image loading is failed\n");
         }
       }
+
+      if(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
+      {
+        PreMultiply(pixelBuffer, preMultiplyOnLoad);
+      }
+
       PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
       if(!textureSet)
       {
@@ -184,6 +205,10 @@ TextureSet TextureManager::LoadAnimatedImageTexture(
         texture.Upload(pixelData);
         textureSet = TextureSet::New();
         textureSet.SetTexture(0u, texture);
+        if(maskTexture)
+        {
+          textureSet.SetTexture(1u, maskTexture);
+        }
       }
     }
   }
@@ -194,14 +219,16 @@ TextureSet TextureManager::LoadAnimatedImageTexture(
     bool      cropToMask         = false;
     if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
     {
-      maskInfo->mAlphaMaskId = RequestMaskLoad(maskInfo->mAlphaMaskUrl);
+      maskInfo->mAlphaMaskId = RequestMaskLoad(maskInfo->mAlphaMaskUrl, maskInfo->mPreappliedMasking ? StorageType::KEEP_PIXEL_BUFFER : StorageType::KEEP_TEXTURE);
       alphaMaskId            = maskInfo->mAlphaMaskId;
-      contentScaleFactor     = maskInfo->mContentScaleFactor;
-      cropToMask             = maskInfo->mCropToMask;
+      if(maskInfo->mPreappliedMasking)
+      {
+        contentScaleFactor = maskInfo->mContentScaleFactor;
+        cropToMask         = maskInfo->mCropToMask;
+      }
     }
 
-    auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
-    textureId        = RequestLoadInternal(url, alphaMaskId, contentScaleFactor, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::BOX_THEN_LINEAR, UseAtlas::NO_ATLAS, cropToMask, StorageType::UPLOAD_TO_TEXTURE, textureObserver, true, TextureManager::ReloadPolicy::CACHED, preMultiply, animatedImageLoading, frameIndex, false);
+    textureId = RequestLoadInternal(url, alphaMaskId, contentScaleFactor, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::BOX_THEN_LINEAR, UseAtlas::NO_ATLAS, cropToMask, StorageType::UPLOAD_TO_TEXTURE, textureObserver, true, TextureManager::ReloadPolicy::CACHED, preMultiplyOnLoad, animatedImageLoading, frameIndex, false);
 
     TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
     if(loadState == TextureManager::LoadState::UPLOADED)
@@ -375,39 +402,36 @@ TextureSet TextureManager::LoadTexture(
       if(!textureSet) // big image, no atlasing or atlasing failed
       {
         atlasingStatus = false;
-        if(!maskInfo || !maskInfo->mAlphaMaskUrl.IsValid())
-        {
-          textureId = RequestLoad(
-            url,
-            desiredSize,
-            fittingMode,
-            samplingMode,
-            UseAtlas::NO_ATLAS,
-            textureObserver,
-            orientationCorrection,
-            reloadPolicy,
-            preMultiplyOnLoad,
-            synchronousLoading);
-        }
-        else
+
+        TextureId alphaMaskId        = INVALID_TEXTURE_ID;
+        float     contentScaleFactor = 1.0f;
+        bool      cropToMask         = false;
+        if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
         {
-          maskInfo->mAlphaMaskId = RequestMaskLoad(maskInfo->mAlphaMaskUrl, synchronousLoading);
-          textureId              = RequestLoad(
-            url,
-            maskInfo->mAlphaMaskId,
-            maskInfo->mContentScaleFactor,
-            desiredSize,
-            fittingMode,
-            samplingMode,
-            UseAtlas::NO_ATLAS,
-            maskInfo->mCropToMask,
-            textureObserver,
-            orientationCorrection,
-            reloadPolicy,
-            preMultiplyOnLoad,
-            synchronousLoading);
+          maskInfo->mAlphaMaskId = RequestMaskLoad(maskInfo->mAlphaMaskUrl, maskInfo->mPreappliedMasking ? StorageType::KEEP_PIXEL_BUFFER : StorageType::KEEP_TEXTURE, synchronousLoading);
+          alphaMaskId            = maskInfo->mAlphaMaskId;
+          if(maskInfo && maskInfo->mPreappliedMasking)
+          {
+            contentScaleFactor = maskInfo->mContentScaleFactor;
+            cropToMask         = maskInfo->mCropToMask;
+          }
         }
 
+        textureId = RequestLoad(
+          url,
+          alphaMaskId,
+          contentScaleFactor,
+          desiredSize,
+          fittingMode,
+          samplingMode,
+          UseAtlas::NO_ATLAS,
+          cropToMask,
+          textureObserver,
+          orientationCorrection,
+          reloadPolicy,
+          preMultiplyOnLoad,
+          synchronousLoading);
+
         TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
         if(loadState == TextureManager::LoadState::UPLOADED)
         {
@@ -481,11 +505,12 @@ TextureManager::TextureId TextureManager::RequestLoad(
 
 TextureManager::TextureId TextureManager::RequestMaskLoad(
   const VisualUrl& maskUrl,
+  StorageType      storageType,
   const bool&      synchronousLoading)
 {
   // Use the normal load procedure to get the alpha mask.
   auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
-  return RequestLoadInternal(maskUrl, INVALID_TEXTURE_ID, 1.0f, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::NO_FILTER, UseAtlas::NO_ATLAS, false, StorageType::KEEP_PIXEL_BUFFER, NULL, true, TextureManager::ReloadPolicy::CACHED, preMultiply, Dali::AnimatedImageLoading(), 0u, synchronousLoading);
+  return RequestLoadInternal(maskUrl, INVALID_TEXTURE_ID, 1.0f, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::NO_FILTER, UseAtlas::NO_ATLAS, false, storageType, NULL, true, TextureManager::ReloadPolicy::CACHED, preMultiply, Dali::AnimatedImageLoading(), 0u, synchronousLoading);
 }
 
 TextureManager::TextureId TextureManager::RequestLoadInternal(
@@ -508,12 +533,12 @@ TextureManager::TextureId TextureManager::RequestLoadInternal(
 {
   TextureHash       textureHash = INITIAL_HASH_NUMBER;
   TextureCacheIndex cacheIndex  = INVALID_CACHE_INDEX;
-  if(storageType != StorageType::RETURN_PIXEL_BUFFER)
+  if(storageType != StorageType::RETURN_PIXEL_BUFFER && frameIndex == 0)
   {
     textureHash = mTextureCacheManager.GenerateHash(url, desiredSize, fittingMode, samplingMode, useAtlas, maskTextureId, cropToMask, frameIndex);
 
     // Look up the texture by hash. Note: The extra parameters are used in case of a hash collision.
-    cacheIndex = mTextureCacheManager.FindCachedTexture(textureHash, url, desiredSize, fittingMode, samplingMode, useAtlas, maskTextureId, cropToMask, preMultiplyOnLoad, (animatedImageLoading) ? true : false, frameIndex);
+    cacheIndex = mTextureCacheManager.FindCachedTexture(textureHash, url, desiredSize, fittingMode, samplingMode, useAtlas, storageType, maskTextureId, cropToMask, preMultiplyOnLoad, (animatedImageLoading) ? true : false, frameIndex);
   }
 
   TextureManager::TextureId textureId = INVALID_TEXTURE_ID;
@@ -639,19 +664,31 @@ TextureManager::TextureId TextureManager::RequestLoadInternal(
       }
       else // For the image loading.
       {
+        Texture maskTexture;
         if(maskTextureId != INVALID_TEXTURE_ID)
         {
           TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
           if(maskCacheIndex != INVALID_CACHE_INDEX)
           {
-            Devel::PixelBuffer maskPixelBuffer = mTextureCacheManager[maskCacheIndex].pixelBuffer;
-            if(maskPixelBuffer)
+            if(mTextureCacheManager[maskCacheIndex].storageType == StorageType::KEEP_TEXTURE)
             {
-              pixelBuffer.ApplyMask(maskPixelBuffer, contentScale, cropToMask);
+              TextureSet maskTextures = mTextureCacheManager[maskCacheIndex].textureSet;
+              if(maskTextures && maskTextures.GetTextureCount())
+              {
+                maskTexture = maskTextures.GetTexture(0u);
+              }
             }
-            else
+            else if(mTextureCacheManager[maskCacheIndex].storageType == StorageType::KEEP_PIXEL_BUFFER)
             {
-              DALI_LOG_ERROR("Mask image cached invalid pixel buffer!\n");
+              Devel::PixelBuffer maskPixelBuffer = mTextureCacheManager[maskCacheIndex].pixelBuffer;
+              if(maskPixelBuffer)
+              {
+                pixelBuffer.ApplyMask(maskPixelBuffer, contentScale, cropToMask);
+              }
+              else
+              {
+                DALI_LOG_ERROR("Mask image cached invalid pixel buffer!\n");
+              }
             }
           }
           else
@@ -663,6 +700,10 @@ TextureManager::TextureId TextureManager::RequestLoadInternal(
 
         // Upload texture
         UploadTexture(pixelBuffer, textureInfo);
+        if(maskTexture && textureInfo.textureSet)
+        {
+          textureInfo.textureSet.SetTexture(1u, maskTexture);
+        }
       }
     }
   }
@@ -810,7 +851,7 @@ void TextureManager::LoadTexture(TextureManager::TextureInfo& textureInfo, Textu
     DALI_ASSERT_ALWAYS(loadingHelperIt != loadersContainer.End());
     if(textureInfo.animatedImageLoading)
     {
-      loadingHelperIt->LoadAnimatedImage(textureInfo.textureId, textureInfo.animatedImageLoading, textureInfo.frameIndex);
+      loadingHelperIt->LoadAnimatedImage(textureInfo.textureId, textureInfo.animatedImageLoading, textureInfo.frameIndex, premultiplyOnLoad);
     }
     else
     {
@@ -922,10 +963,31 @@ void TextureManager::PostLoad(TextureManager::TextureInfo& textureInfo, Devel::P
           {
             textureInfo.loadState = LoadState::WAITING_FOR_MASK;
           }
-          else if(maskLoadState == LoadState::LOAD_FINISHED)
+          else if(maskLoadState == LoadState::LOAD_FINISHED || maskLoadState == LoadState::UPLOADED)
           {
             // Send New Task to Thread
-            ApplyMask(textureInfo, textureInfo.maskTextureId);
+            TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureInfo.maskTextureId);
+            if(maskCacheIndex != INVALID_CACHE_INDEX)
+            {
+              TextureInfo& maskTextureInfo(mTextureCacheManager[maskCacheIndex]);
+              if(maskTextureInfo.storageType == StorageType::KEEP_PIXEL_BUFFER)
+              {
+                // Send New Task to Thread
+                ApplyMask(textureInfo, textureInfo.maskTextureId);
+              }
+              else if(maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
+              {
+                // Upload image texture. textureInfo.loadState will be UPLOADED.
+                UploadTexture(textureInfo.pixelBuffer, textureInfo);
+                if(maskTextureInfo.textureSet.GetTextureCount() > 0u)
+                {
+                  Texture maskTexture = maskTextureInfo.textureSet.GetTexture(0u);
+                  textureInfo.textureSet.SetTexture(1u, maskTexture);
+                }
+                // notify mask texture set.
+                NotifyObservers(textureInfo, true);
+              }
+            }
           }
           else // maskLoadState == LoadState::LOAD_FAILED
           {
@@ -951,7 +1013,7 @@ void TextureManager::PostLoad(TextureManager::TextureInfo& textureInfo, Devel::P
       {
         NotifyObservers(textureInfo, true);
       }
-      else
+      else // for the StorageType::KEEP_PIXEL_BUFFER and StorageType::KEEP_TEXTURE
       {
         // Check if there was another texture waiting for this load to complete
         // (e.g. if this was an image mask, and its load is on a different thread)
@@ -962,27 +1024,32 @@ void TextureManager::PostLoad(TextureManager::TextureInfo& textureInfo, Devel::P
   else
   {
     textureInfo.loadState = LoadState::LOAD_FAILED;
-    if(textureInfo.storageType != StorageType::KEEP_PIXEL_BUFFER)
-    {
-      NotifyObservers(textureInfo, false);
-    }
-    else // if(textureInfo.storageType == StorageType::KEEP_PIXEL_BUFFER) // image mask case
+    if(textureInfo.storageType == StorageType::KEEP_PIXEL_BUFFER || textureInfo.storageType == StorageType::KEEP_TEXTURE)
     {
       // Check if there was another texture waiting for this load to complete
       // (e.g. if this was an image mask, and its load is on a different thread)
       CheckForWaitingTexture(textureInfo);
     }
+    else
+    {
+      NotifyObservers(textureInfo, false);
+    }
   }
 }
 
 void TextureManager::CheckForWaitingTexture(TextureManager::TextureInfo& maskTextureInfo)
 {
+  if(maskTextureInfo.loadState == LoadState::LOAD_FINISHED &&
+     maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
+  {
+    // Upload mask texture. textureInfo.loadState will be UPLOADED.
+    UploadTexture(maskTextureInfo.pixelBuffer, maskTextureInfo);
+  }
+
   // Search the cache, checking if any texture has this texture id as a
   // maskTextureId:
   const std::size_t size = mTextureCacheManager.size();
 
-  const bool maskLoadSuccess = maskTextureInfo.loadState == LoadState::LOAD_FINISHED ? true : false;
-
   // TODO : Refactorize here to not iterate whole cached image.
   for(TextureCacheIndex cacheIndex = TextureCacheIndex(TextureManagerType::TEXTURE_CACHE_INDEX_TYPE_LOCAL, 0u); cacheIndex.GetIndex() < size; ++cacheIndex.detailValue.index)
   {
@@ -991,10 +1058,28 @@ void TextureManager::CheckForWaitingTexture(TextureManager::TextureInfo& maskTex
     {
       TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
 
-      if(maskLoadSuccess)
+      if(maskTextureInfo.loadState == LoadState::LOAD_FINISHED)
+      {
+        if(maskTextureInfo.storageType == StorageType::KEEP_PIXEL_BUFFER)
+        {
+          // Send New Task to Thread
+          ApplyMask(textureInfo, maskTextureInfo.textureId);
+        }
+      }
+      else if(maskTextureInfo.loadState == LoadState::UPLOADED)
       {
-        // Send New Task to Thread
-        ApplyMask(textureInfo, maskTextureInfo.textureId);
+        if(maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
+        {
+          // Upload image texture. textureInfo.loadState will be UPLOADED.
+          UploadTexture(textureInfo.pixelBuffer, textureInfo);
+          if(maskTextureInfo.textureSet.GetTextureCount() > 0u)
+          {
+            Texture maskTexture = maskTextureInfo.textureSet.GetTexture(0u);
+            textureInfo.textureSet.SetTexture(1u, maskTexture);
+          }
+          // notify mask texture set.
+          NotifyObservers(textureInfo, true);
+        }
       }
       else
       {