CPU Alpha Masking for Animated Image Visual
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-image / rolling-image-cache.cpp
index 8abe059..c0c598a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2021 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.
@@ -18,6 +18,7 @@
 #include <dali-toolkit/internal/visuals/animated-image/rolling-image-cache.h>
 
 // INTERNAL HEADERS
+#include <dali-toolkit/internal/visuals/image-atlas-manager.h> // For ImageAtlasManagerPtr
 #include <dali/integration-api/debug.h>
 
 // EXTERNAL HEADERS
@@ -27,25 +28,27 @@ namespace
 #if defined(DEBUG_ENABLED)
 Debug::Filter* gAnimImgLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_ANIMATED_IMAGE");
 
-#define LOG_CACHE                                                       \
-  {                                                                     \
-    std::ostringstream oss;                                             \
-    oss<<"Size:"<<mQueue.Count()<<" [ ";                                \
-    for(std::size_t _i=0; _i<mQueue.Count(); ++_i)                      \
-    {                                                                   \
-      oss<<_i<<                                                         \
-        "={ tex:"<<mImageUrls[mQueue[_i].mUrlIndex].mTextureId<<        \
-        " urlId:"<<mQueue[_i].mUrlIndex<<                               \
-        " rdy:"<<(mQueue[_i].mReady?"T":"F")<< "}, ";                   \
-    }                                                                   \
-    oss<<" ]"<<std::endl;                                               \
-    DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"%s",oss.str().c_str()); \
+#define LOG_CACHE                                                                                                                                                        \
+  {                                                                                                                                                                      \
+    std::ostringstream oss;                                                                                                                                              \
+    oss << "Size:" << mQueue.Count() << " [ ";                                                                                                                           \
+    for(std::size_t _i = 0; _i < mQueue.Count(); ++_i)                                                                                                                   \
+    {                                                                                                                                                                    \
+      oss << _i << "={ tex:" << mImageUrls[mQueue[_i].mUrlIndex].mTextureId << " urlId:" << mQueue[_i].mUrlIndex << " rdy:" << (mQueue[_i].mReady ? "T" : "F") << "}, "; \
+    }                                                                                                                                                                    \
+    oss << " ]" << std::endl;                                                                                                                                            \
+    DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "%s", oss.str().c_str());                                                                                           \
   }
 
 #else
-  #define LOG_CACHE
+#define LOG_CACHE
 #endif
-}
+
+static constexpr bool ENABLE_ORIENTATION_CORRECTION(true);
+
+static constexpr uint32_t FIRST_FRAME_INDEX = 0u;
+
+} // namespace
 
 namespace Dali
 {
@@ -53,155 +56,200 @@ namespace Toolkit
 {
 namespace Internal
 {
-
-RollingImageCache::RollingImageCache(
-  TextureManager& textureManager, UrlList& urlList, ImageCache::FrameReadyObserver& observer,
-  uint16_t cacheSize, uint16_t batchSize )
-: ImageCache( textureManager, urlList, observer, batchSize ),
-  mQueue( cacheSize )
+RollingImageCache::RollingImageCache(TextureManager&                     textureManager,
+                                     UrlList&                            urlList,
+                                     TextureManager::MaskingDataPointer& maskingData,
+                                     ImageCache::FrameReadyObserver&     observer,
+                                     uint16_t                            cacheSize,
+                                     uint16_t                            batchSize,
+                                     uint32_t                            interval)
+: ImageCache(textureManager, maskingData, observer, batchSize, interval),
+  mImageUrls(urlList),
+  mQueue(cacheSize)
 {
-  LoadBatch();
 }
 
 RollingImageCache::~RollingImageCache()
 {
-  while( !mQueue.IsEmpty() )
-  {
-    ImageFrame imageFrame = mQueue.PopFront();
-    mTextureManager.Remove( mImageUrls[ imageFrame.mUrlIndex ].mTextureId );
-  }
+  ClearCache();
 }
 
-TextureSet RollingImageCache::FirstFrame()
+TextureSet RollingImageCache::Frame(uint32_t frameIndex)
 {
-  TextureSet textureSet = GetFrontTextureSet();
+  // Pop frames until the frame of frameIndex become front frame.
+  bool popExist = false;
+  while(!mQueue.IsEmpty() && mQueue.Front().mUrlIndex != frameIndex)
+  {
+    PopFrontCache();
+    popExist                                    = true;
+  }
+
+  // TODO: synchronous loading of first frame.
+  if(popExist || mQueue.IsEmpty())
+  {
+    uint32_t batchFrameIndex = 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();
+    }
+    LoadBatch(batchFrameIndex);
+  }
 
-  if( ! textureSet )
+  TextureSet textureSet;
+  if(IsFrontReady() == true && mLoadState != TextureManager::LoadState::LOAD_FAILED)
   {
-    mWaitingForReadyFrame = true;
+    textureSet = GetFrontTextureSet();
   }
 
   return textureSet;
 }
 
-TextureSet RollingImageCache::NextFrame()
+TextureSet RollingImageCache::FirstFrame()
 {
-  TextureSet textureSet;
+  TextureSet textureSet = Frame(FIRST_FRAME_INDEX);
+  return textureSet;
+}
 
-  ImageFrame imageFrame = mQueue.PopFront();
-  mTextureManager.Remove( mImageUrls[ imageFrame.mUrlIndex ].mTextureId );
-  mImageUrls[ imageFrame.mUrlIndex ].mTextureId = TextureManager::INVALID_TEXTURE_ID;
+uint32_t RollingImageCache::GetFrameInterval(uint32_t frameIndex) const
+{
+  return mInterval;
+}
 
-  if( IsFrontReady() == true )
-  {
-    textureSet = GetFrontTextureSet();
-  }
-  else
+int32_t RollingImageCache::GetCurrentFrameIndex() const
+{
+  if(mQueue.IsEmpty())
   {
-    mWaitingForReadyFrame = true;
+    return -1;
   }
+  return mQueue.Front().mUrlIndex;
+}
 
-  LoadBatch();
-
-  return textureSet;
+int32_t RollingImageCache::GetTotalFrameCount() const
+{
+  return mImageUrls.size();
 }
 
 bool RollingImageCache::IsFrontReady() const
 {
-  return ( !mQueue.IsEmpty() && mQueue.Front().mReady );
+  return (!mQueue.IsEmpty() && mQueue.Front().mReady);
 }
 
-void RollingImageCache::LoadBatch()
+void RollingImageCache::LoadBatch(uint32_t frameIndex)
 {
   // Try and load up to mBatchSize images, until the cache is filled.
   // Once the cache is filled, as frames progress, the old frame is
   // cleared, but not erased, and another image is loaded
-  bool frontFrameReady = IsFrontReady();;
-
-  for( unsigned int i=0; i< mBatchSize && !mQueue.IsFull(); ++i )
+  for(unsigned int i = 0; i < mBatchSize && !mQueue.IsFull(); ++i)
   {
     ImageFrame imageFrame;
 
-    std::string& url = mImageUrls[ mUrlIndex ].mUrl;
-    imageFrame.mUrlIndex = mUrlIndex;
-    imageFrame.mReady = false;
-
-    ++mUrlIndex;
-    mUrlIndex %= mImageUrls.size();
+    std::string& url     = mImageUrls[frameIndex].mUrl;
+    imageFrame.mUrlIndex = frameIndex;
+    imageFrame.mReady    = false;
 
-    mQueue.PushBack( imageFrame );
+    mQueue.PushBack(imageFrame);
 
-    // Note, if the image is already loaded, then UploadComplete will get called
+    // Note, if the image is already loaded, then LoadComplete will get called
     // from within this method. This means it won't yet have a texture id, so we
-    // need to account for this inside the UploadComplete method using mRequestingLoad.
+    // 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;
+    AtlasUploadObserver*               atlasObserver      = nullptr;
+    ImageAtlasManagerPtr               imageAtlasManager  = nullptr;
+    Vector4                            textureRect;
+    Dali::ImageDimensions              textureRectSize;
+    auto                               preMultiply = 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, mMaskingData, synchronousLoading, loadTextureId, textureRect, textureRectSize, atlasingStatus, loadingStatus, Dali::WrapMode::Type::DEFAULT, Dali::WrapMode::Type::DEFAULT, this, atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED, preMultiply);
+    mImageUrls[imageFrame.mUrlIndex].mTextureId = loadTextureId;
 
-    mImageUrls[ imageFrame.mUrlIndex ].mTextureId =
-      mTextureManager.RequestLoad( url, ImageDimensions(), FittingMode::SCALE_TO_FILL,
-                                   SamplingMode::BOX_THEN_LINEAR, TextureManager::NO_ATLAS,
-                                   this );
     mRequestingLoad = false;
-  }
 
-  CheckFrontFrame( frontFrameReady );
+    ++frameIndex;
+    frameIndex %= mImageUrls.size();
+  }
 }
 
-void RollingImageCache::SetImageFrameReady( TextureManager::TextureId textureId )
+TextureSet RollingImageCache::GetFrontTextureSet() const
 {
-  for( std::size_t i = 0; i < mQueue.Count() ; ++i )
-  {
-    if( GetCachedTextureId(i) == textureId )
-    {
-      mQueue[i].mReady = true;
-      break;
-    }
-  }
+  TextureManager::TextureId textureId = GetCachedTextureId(0);
+  return mTextureManager.GetTextureSet(textureId);
 }
 
-TextureSet RollingImageCache::GetFrontTextureSet() const
+TextureManager::TextureId RollingImageCache::GetCachedTextureId(int index) const
 {
-  TextureManager::TextureId textureId = GetCachedTextureId( 0 );
-  return mTextureManager.GetTextureSet( textureId );
+  return mImageUrls[mQueue[index].mUrlIndex].mTextureId;
 }
 
-TextureManager::TextureId RollingImageCache::GetCachedTextureId( int index ) const
+void RollingImageCache::PopFrontCache()
 {
-  return mImageUrls[ mQueue[ index ].mUrlIndex ].mTextureId;
+  ImageFrame imageFrame = mQueue.PopFront();
+  mTextureManager.Remove(mImageUrls[imageFrame.mUrlIndex].mTextureId, this);
+  mImageUrls[imageFrame.mUrlIndex].mTextureId = TextureManager::INVALID_TEXTURE_ID;
+
+  if(mMaskingData && mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID)
+  {
+    mTextureManager.Remove(mMaskingData->mAlphaMaskId, this);
+    if(mQueue.IsEmpty())
+    {
+      mMaskingData->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
+    }
+  }
 }
 
-void RollingImageCache::CheckFrontFrame( bool wasReady )
+void RollingImageCache::ClearCache()
 {
-  if( mWaitingForReadyFrame && wasReady == false && IsFrontReady() )
+  while(mTextureManagerAlive && !mQueue.IsEmpty())
   {
-    mWaitingForReadyFrame = false;
-    mObserver.FrameReady( GetFrontTextureSet() );
+    PopFrontCache();
   }
+  mLoadState = TextureManager::LoadState::NOT_STARTED;
 }
 
-void RollingImageCache::UploadComplete(
-  bool           loadSuccess,
-  int32_t        textureId,
-  TextureSet     textureSet,
-  bool           useAtlasing,
-  const Vector4& atlasRect )
+void RollingImageCache::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
 {
-  DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"AnimatedImageVisual::UploadComplete(textureId:%d) start\n", textureId);
+  DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "AnimatedImageVisual::LoadComplete(textureId:%d) start\n", textureInformation.textureId);
   LOG_CACHE;
 
-  bool frontFrameReady = IsFrontReady();
-
-  if( ! mRequestingLoad )
+  if(loadSuccess)
   {
-    SetImageFrameReady( textureId );
+    mLoadState = TextureManager::LoadState::LOAD_FINISHED;
+    bool frontFrameReady = IsFrontReady();
+    if(!mRequestingLoad)
+    {
+      for(std::size_t i = 0; i < mQueue.Count(); ++i)
+      {
+        if(GetCachedTextureId(i) == textureInformation.textureId)
+        {
+          mQueue[i].mReady = true;
+          break;
+        }
+      }
+    }
+    else
+    {
+      // LoadComplete has been called from within RequestLoad. TextureManager must
+      // therefore already have the texture cached, so make the texture ready.
+      // (Use the last texture, as the texture id hasn't been assigned yet)
+      mQueue.Back().mReady = true;
+    }
 
-    CheckFrontFrame( frontFrameReady );
+    if(!frontFrameReady && IsFrontReady())
+    {
+      mObserver.FrameReady(mTextureManager.GetTextureSet(textureInformation.textureId), mInterval);
+    }
   }
   else
   {
-    // UploadComplete has been called from within RequestLoad. TextureManager must
-    // therefore already have the texture cached, so make the texture ready.
-    // (Use the last texture, as the texture id hasn't been assigned yet)
-    mQueue.Back().mReady = true;
+    mLoadState = TextureManager::LoadState::LOAD_FAILED;
+    mObserver.FrameReady(TextureSet(), 0);
   }
 
   LOG_CACHE;