[dali_2.3.24] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-image / fixed-image-cache.cpp
index 8cc69ff..e751f90 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2024 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.
@@ -21,6 +21,7 @@
 #include <dali-toolkit/internal/visuals/image-atlas-manager.h> // For ImageAtlasManagerPtr
 
 // EXTERNAL HEADERS
+#include <dali/integration-api/adaptor-framework/adaptor.h>
 #include <dali/integration-api/debug.h>
 
 namespace Dali
@@ -31,19 +32,23 @@ namespace Internal
 {
 namespace
 {
-constexpr bool ENABLE_ORIENTATION_CORRECTION(true);
+constexpr bool     ENABLE_ORIENTATION_CORRECTION(true);
 constexpr uint32_t FIRST_FRAME_INDEX = 0u;
 } // namespace
 
 FixedImageCache::FixedImageCache(TextureManager&                     textureManager,
+                                 ImageDimensions                     size,
+                                 Dali::FittingMode::Type             fittingMode,
+                                 Dali::SamplingMode::Type            samplingMode,
                                  UrlList&                            urlList,
                                  TextureManager::MaskingDataPointer& maskingData,
                                  ImageCache::FrameReadyObserver&     observer,
                                  uint32_t                            batchSize,
-                                 uint32_t                            interval)
-: ImageCache(textureManager, maskingData, observer, batchSize, interval),
+                                 uint32_t                            interval,
+                                 bool                                preMultiplyOnLoad)
+: ImageCache(textureManager, size, fittingMode, samplingMode, maskingData, observer, batchSize, interval, preMultiplyOnLoad),
   mImageUrls(urlList),
-  mFront(FIRST_FRAME_INDEX)
+  mCurrentFrameIndex(FIRST_FRAME_INDEX)
 {
   mReadyFlags.reserve(mImageUrls.size());
 }
@@ -63,17 +68,17 @@ TextureSet FixedImageCache::Frame(uint32_t frameIndex)
   }
 
   while(mReadyFlags.size() < mImageUrls.size() &&
-        (frameIndex > mFront || mReadyFlags.empty()))
+        (frameIndex > mCurrentFrameIndex || mReadyFlags.empty()))
   {
-    ++mFront;
+    ++mCurrentFrameIndex;
     LoadBatch();
   }
 
-  mFront = frameIndex;
+  mCurrentFrameIndex = frameIndex;
 
-  if(IsFrontReady() && mLoadState != TextureManager::LoadState::LOAD_FAILED)
+  if(IsFrameReady(mCurrentFrameIndex) && mLoadState != TextureManager::LoadState::LOAD_FAILED)
   {
-    textureSet = GetFrontTextureSet();
+    textureSet = GetTextureSet(mCurrentFrameIndex);
   }
 
   return textureSet;
@@ -93,7 +98,7 @@ uint32_t FixedImageCache::GetFrameInterval(uint32_t frameIndex) const
 
 int32_t FixedImageCache::GetCurrentFrameIndex() const
 {
-  return static_cast<int32_t>(mFront);
+  return static_cast<int32_t>(mCurrentFrameIndex);
 }
 
 int32_t FixedImageCache::GetTotalFrameCount() const
@@ -101,9 +106,9 @@ int32_t FixedImageCache::GetTotalFrameCount() const
   return mImageUrls.size();
 }
 
-bool FixedImageCache::IsFrontReady() const
+bool FixedImageCache::IsFrameReady(uint32_t frameIndex) const
 {
-  return (mReadyFlags.size() > 0 && mReadyFlags[mFront] == true);
+  return (mReadyFlags.size() > 0 && mReadyFlags[frameIndex] == true);
 }
 
 void FixedImageCache::LoadBatch()
@@ -112,8 +117,8 @@ void FixedImageCache::LoadBatch()
   // Once the cache is filled, no more images are loaded.
   for(unsigned int i = 0; i < mBatchSize && mReadyFlags.size() < mImageUrls.size(); ++i)
   {
-    uint32_t frameIndex = mReadyFlags.size();
-    std::string& url = mImageUrls[frameIndex].mUrl;
+    uint32_t   frameIndex = mReadyFlags.size();
+    VisualUrl& url        = mImageUrls[frameIndex].mUrl;
 
     mReadyFlags.push_back(false);
 
@@ -121,52 +126,46 @@ void FixedImageCache::LoadBatch()
     // from within this method. This means it won't yet have a texture id, so we
     // need to account for this inside the LoadComplete method using mRequestingLoad.
     mRequestingLoad = true;
-    mLoadState = TextureManager::LoadState::LOADING;
+    mLoadState      = TextureManager::LoadState::LOADING;
 
-    bool                               synchronousLoading = false;
-    bool                               atlasingStatus     = false;
-    bool                               loadingStatus      = false;
-    AtlasUploadObserver*               atlasObserver      = nullptr;
-    ImageAtlasManagerPtr               imageAtlasManager  = nullptr;
-    Vector4                            textureRect;
-    Dali::ImageDimensions              textureRectSize;
-    auto                               preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
+    bool                  synchronousLoading = false;
+    bool                  atlasingStatus     = false;
+    bool                  loadingStatus      = false;
+    AtlasUploadObserver*  atlasObserver      = nullptr;
+    ImageAtlasManagerPtr  imageAtlasManager  = nullptr;
+    Vector4               textureRect;
+    Dali::ImageDimensions textureRectSize;
 
-    mTextureManager.LoadTexture(
-      url, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::BOX_THEN_LINEAR, mMaskingData, synchronousLoading, mImageUrls[frameIndex].mTextureId, textureRect, textureRectSize, atlasingStatus, loadingStatus, Dali::WrapMode::Type::DEFAULT, Dali::WrapMode::Type::DEFAULT, this, atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED, preMultiply);
+    auto preMultiplyOnLoading = mPreMultiplyOnLoad ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD
+                                                   : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
 
+    mTextureManager.LoadTexture(url, mDesiredSize, mFittingMode, mSamplingMode, mMaskingData, synchronousLoading, mImageUrls[frameIndex].mTextureId, textureRect, textureRectSize, atlasingStatus, loadingStatus, this, atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED, preMultiplyOnLoading);
     mRequestingLoad = false;
   }
 }
 
-TextureSet FixedImageCache::GetFrontTextureSet() const
+TextureSet FixedImageCache::GetTextureSet(uint32_t frameIndex) const
 {
-  return mTextureManager.GetTextureSet(mImageUrls[mFront].mTextureId);
+  TextureSet textureSet = mTextureManager.GetTextureSet(mImageUrls[frameIndex].mTextureId);
+  return textureSet;
 }
 
-void FixedImageCache::CheckFrontFrame(bool wasReady)
+void FixedImageCache::MakeReady(bool wasReady, uint32_t frameIndex, bool preMultiplied)
 {
-  if(wasReady == false && IsFrontReady())
+  if(wasReady == false && IsFrameReady(frameIndex))
   {
-    mObserver.FrameReady(GetFrontTextureSet(), mInterval);
+    mObserver.FrameReady(GetTextureSet(frameIndex), mInterval, preMultiplied);
   }
 }
 
 void FixedImageCache::ClearCache()
 {
-  if(mTextureManagerAlive)
+  if(Dali::Adaptor::IsAvailable())
   {
     for(std::size_t i = 0; i < mImageUrls.size(); ++i)
     {
-      mTextureManager.Remove(mImageUrls[i].mTextureId, this);
+      mTextureManager.RequestRemove(mImageUrls[i].mTextureId, this);
       mImageUrls[i].mTextureId = TextureManager::INVALID_TEXTURE_ID;
-
-      if(mMaskingData && mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID)
-      {
-        // In the CPU alpha masking, each frame increases reference count of masking texture.
-        // We should call TextureManager::Remove to decrease reference count when each frame is removed.
-        mTextureManager.Remove(mMaskingData->mAlphaMaskId, this);
-      }
     }
   }
   mReadyFlags.clear();
@@ -181,8 +180,8 @@ void FixedImageCache::LoadComplete(bool loadSuccess, TextureInformation textureI
 {
   if(loadSuccess)
   {
-    mLoadState = TextureManager::LoadState::LOAD_FINISHED;
-    bool frontFrameReady = IsFrontReady();
+    mLoadState               = TextureManager::LoadState::LOAD_FINISHED;
+    bool isCurrentFrameReady = IsFrameReady(mCurrentFrameIndex);
     if(!mRequestingLoad)
     {
       for(std::size_t i = 0; i < mImageUrls.size(); ++i)
@@ -198,12 +197,13 @@ void FixedImageCache::LoadComplete(bool loadSuccess, TextureInformation textureI
     {
       mReadyFlags.back() = true;
     }
-    CheckFrontFrame(frontFrameReady);
+    MakeReady(isCurrentFrameReady, mCurrentFrameIndex, textureInformation.preMultiplied);
   }
   else
   {
     mLoadState = TextureManager::LoadState::LOAD_FAILED;
-    mObserver.FrameReady(TextureSet(), 0);
+    // preMultiplied should be false because broken image don't premultiply alpha on load
+    mObserver.FrameReady(TextureSet(), 0, false);
   }
 }