[dali_2.3.22] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-image / rolling-image-cache.cpp
index 08fe0d0..d818417 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.
 
 // INTERNAL HEADERS
 #include <dali-toolkit/internal/visuals/image-atlas-manager.h> // For ImageAtlasManagerPtr
-#include <dali/integration-api/debug.h>
 
 // EXTERNAL HEADERS
+#include <dali/integration-api/adaptor-framework/adaptor.h>
+#include <dali/integration-api/debug.h>
 
 namespace
 {
@@ -29,6 +30,7 @@ namespace
 Debug::Filter* gAnimImgLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_ANIMATED_IMAGE");
 
 #define LOG_CACHE                                                                                                                                                        \
+  if(gAnimImgLogFilter->IsEnabledFor(Debug::Concise))                                                                                                                    \
   {                                                                                                                                                                      \
     std::ostringstream oss;                                                                                                                                              \
     oss << "Size:" << mQueue.Count() << " [ ";                                                                                                                           \
@@ -56,9 +58,18 @@ namespace Toolkit
 {
 namespace Internal
 {
-RollingImageCache::RollingImageCache(
-  TextureManager& textureManager, UrlList& urlList, ImageCache::FrameReadyObserver& observer, uint16_t cacheSize, uint16_t batchSize, uint32_t interval)
-: ImageCache(textureManager, observer, batchSize, interval),
+RollingImageCache::RollingImageCache(TextureManager&                     textureManager,
+                                     ImageDimensions                     size,
+                                     Dali::FittingMode::Type             fittingMode,
+                                     Dali::SamplingMode::Type            samplingMode,
+                                     UrlList&                            urlList,
+                                     TextureManager::MaskingDataPointer& maskingData,
+                                     ImageCache::FrameReadyObserver&     observer,
+                                     uint16_t                            cacheSize,
+                                     uint16_t                            batchSize,
+                                     uint32_t                            interval,
+                                     bool                                preMultiplyOnLoad)
+: ImageCache(textureManager, size, fittingMode, samplingMode, maskingData, observer, batchSize, interval, preMultiplyOnLoad),
   mImageUrls(urlList),
   mQueue(cacheSize)
 {
@@ -75,10 +86,8 @@ TextureSet RollingImageCache::Frame(uint32_t frameIndex)
   bool popExist = false;
   while(!mQueue.IsEmpty() && mQueue.Front().mUrlIndex != frameIndex)
   {
-    ImageFrame imageFrame = mQueue.PopFront();
-    mTextureManager.Remove(mImageUrls[imageFrame.mUrlIndex].mTextureId, this);
-    mImageUrls[imageFrame.mUrlIndex].mTextureId = TextureManager::INVALID_TEXTURE_ID;
-    popExist                                    = true;
+    PopFrontCache();
+    popExist = true;
   }
 
   // TODO: synchronous loading of first frame.
@@ -88,7 +97,7 @@ TextureSet RollingImageCache::Frame(uint32_t frameIndex)
     // If the frame of frameIndex was already loaded, load batch from the last frame of queue
     if(!mQueue.IsEmpty())
     {
-      batchFrameIndex = (mQueue.Back().mUrlIndex + 1) % mImageUrls.size();
+      batchFrameIndex = (mQueue.Back().mUrlIndex + 1) % static_cast<uint32_t>(mImageUrls.size());
     }
     LoadBatch(batchFrameIndex);
   }
@@ -141,7 +150,7 @@ void RollingImageCache::LoadBatch(uint32_t frameIndex)
   {
     ImageFrame imageFrame;
 
-    std::string& url     = mImageUrls[frameIndex].mUrl;
+    VisualUrl& url       = mImageUrls[frameIndex].mUrl;
     imageFrame.mUrlIndex = frameIndex;
     imageFrame.mReady    = false;
 
@@ -151,21 +160,22 @@ void RollingImageCache::LoadBatch(uint32_t frameIndex)
     // 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;
-
-    bool                               synchronousLoading = false;
-    bool                               atlasingStatus     = false;
-    bool                               loadingStatus      = false;
-    TextureManager::MaskingDataPointer maskInfo           = nullptr;
-    AtlasUploadObserver*               atlasObserver      = nullptr;
-    ImageAtlasManagerPtr               imageAtlasManager  = nullptr;
-    Vector4                            textureRect;
-    Dali::ImageDimensions              textureRectSize;
-    auto                               preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
+    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 preMultiplyOnLoading = mPreMultiplyOnLoad ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD
+                                                   : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
 
     TextureManager::TextureId loadTextureId = TextureManager::INVALID_TEXTURE_ID;
     TextureSet                textureSet    = mTextureManager.LoadTexture(
-      url, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::BOX_THEN_LINEAR, maskInfo, synchronousLoading, loadTextureId, textureRect, textureRectSize, atlasingStatus, loadingStatus, Dali::WrapMode::Type::DEFAULT, Dali::WrapMode::Type::DEFAULT, this, atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED, preMultiply);
+      url, mDesiredSize, mFittingMode, mSamplingMode, mMaskingData, synchronousLoading, loadTextureId, textureRect, textureRectSize, atlasingStatus, loadingStatus, this, atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED, preMultiplyOnLoading);
     mImageUrls[imageFrame.mUrlIndex].mTextureId = loadTextureId;
 
     mRequestingLoad = false;
@@ -177,8 +187,15 @@ void RollingImageCache::LoadBatch(uint32_t frameIndex)
 
 TextureSet RollingImageCache::GetFrontTextureSet() const
 {
-  TextureManager::TextureId textureId = GetCachedTextureId(0);
-  return mTextureManager.GetTextureSet(textureId);
+  TextureManager::TextureId textureId  = GetCachedTextureId(0);
+  TextureSet                textureSet = mTextureManager.GetTextureSet(textureId);
+  if(textureSet)
+  {
+    Sampler sampler = Sampler::New();
+    sampler.SetWrapMode(Dali::WrapMode::Type::DEFAULT, Dali::WrapMode::Type::DEFAULT);
+    textureSet.SetSampler(0u, sampler);
+  }
+  return textureSet;
 }
 
 TextureManager::TextureId RollingImageCache::GetCachedTextureId(int index) const
@@ -186,13 +203,27 @@ TextureManager::TextureId RollingImageCache::GetCachedTextureId(int index) const
   return mImageUrls[mQueue[index].mUrlIndex].mTextureId;
 }
 
+void RollingImageCache::PopFrontCache()
+{
+  ImageFrame imageFrame = mQueue.PopFront();
+
+  mTextureManager.RequestRemove(mImageUrls[imageFrame.mUrlIndex].mTextureId, this);
+  mImageUrls[imageFrame.mUrlIndex].mTextureId = TextureManager::INVALID_TEXTURE_ID;
+
+  if(mMaskingData && mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID)
+  {
+    if(mQueue.IsEmpty())
+    {
+      mMaskingData->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
+    }
+  }
+}
+
 void RollingImageCache::ClearCache()
 {
-  while(mTextureManagerAlive && !mQueue.IsEmpty())
+  while(Dali::Adaptor::IsAvailable() && !mQueue.IsEmpty())
   {
-    ImageFrame imageFrame = mQueue.PopFront();
-    mTextureManager.Remove(mImageUrls[imageFrame.mUrlIndex].mTextureId, this);
-    mImageUrls[imageFrame.mUrlIndex].mTextureId = TextureManager::INVALID_TEXTURE_ID;
+    PopFrontCache();
   }
   mLoadState = TextureManager::LoadState::NOT_STARTED;
 }
@@ -204,7 +235,7 @@ void RollingImageCache::LoadComplete(bool loadSuccess, TextureInformation textur
 
   if(loadSuccess)
   {
-    mLoadState = TextureManager::LoadState::LOAD_FINISHED;
+    mLoadState           = TextureManager::LoadState::LOAD_FINISHED;
     bool frontFrameReady = IsFrontReady();
     if(!mRequestingLoad)
     {
@@ -227,13 +258,14 @@ void RollingImageCache::LoadComplete(bool loadSuccess, TextureInformation textur
 
     if(!frontFrameReady && IsFrontReady())
     {
-      mObserver.FrameReady(mTextureManager.GetTextureSet(textureInformation.textureId), mInterval);
+      mObserver.FrameReady(textureInformation.textureSet, mInterval, 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);
   }
 
   LOG_CACHE;