/*
- * Copyright (c) 2020 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2022 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.
// CLASS HEADER
#include "rolling-animated-image-cache.h"
-// EXTERNAL HEADERS
-
// INTERNAL HEADERS
#include <dali-toolkit/devel-api/image-loader/texture-manager.h>
#include <dali-toolkit/internal/visuals/image-atlas-manager.h> // For ImageAtlasManagerPtr
#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<< \
- "={ frm#: " << mQueue[_i].mFrameNumber << \
- " tex: " << mImageUrls[mQueue[_i].mFrameNumber].mTextureId<<"}, "; \
- } \
- 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 << "={ frm#: " << mQueue[_i].mFrameNumber << " tex: " << mImageUrls[mQueue[_i].mFrameNumber].mTextureId << "}, "; \
+ } \
+ oss << " ]" << std::endl; \
+ DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "%s", oss.str().c_str()); \
}
#else
- #define LOG_CACHE
+#define LOG_CACHE
#endif
-const bool ENABLE_ORIENTATION_CORRECTION( true );
+static constexpr bool ENABLE_ORIENTATION_CORRECTION(true);
-}
+} // namespace
namespace Dali
{
{
namespace Internal
{
-
-RollingAnimatedImageCache::RollingAnimatedImageCache(
- TextureManager& textureManager, AnimatedImageLoading& animatedImageLoading, uint32_t frameCount, ImageCache::FrameReadyObserver& observer,
- uint16_t cacheSize, uint16_t batchSize )
-: ImageCache( textureManager, observer, batchSize ),
- mAnimatedImageLoading( animatedImageLoading ),
- mFrameCount( frameCount ),
- mFrameIndex( 0 ),
- mCacheSize( cacheSize ),
- mQueue( cacheSize )
+namespace
+{
+static constexpr uint32_t SINGLE_IMAGE_COUNT = 1u;
+static constexpr uint32_t FIRST_FRAME_INDEX = 0u;
+} // namespace
+
+RollingAnimatedImageCache::RollingAnimatedImageCache(TextureManager& textureManager,
+ AnimatedImageLoading& animatedImageLoading,
+ TextureManager::MaskingDataPointer& maskingData,
+ ImageCache::FrameReadyObserver& observer,
+ uint16_t cacheSize,
+ uint16_t batchSize,
+ bool isSynchronousLoading)
+: ImageCache(textureManager, maskingData, observer, batchSize, 0u),
+ mAnimatedImageLoading(animatedImageLoading),
+ mFrameCount(SINGLE_IMAGE_COUNT),
+ mFrameIndex(FIRST_FRAME_INDEX),
+ mCacheSize(cacheSize),
+ mQueue(cacheSize),
+ mIsSynchronousLoading(isSynchronousLoading)
{
- mImageUrls.resize( mFrameCount );
- LoadBatch();
+ mImageUrls.resize(mFrameCount);
+ mIntervals.assign(mFrameCount, 0);
}
RollingAnimatedImageCache::~RollingAnimatedImageCache()
{
- if( mTextureManagerAlive )
- {
- while( IsFrontReady() )
- {
- ImageFrame imageFrame = mQueue.PopFront();
- Dali::Toolkit::TextureManager::RemoveTexture( mImageUrls[ imageFrame.mFrameNumber ].mUrl );
- }
- }
+ ClearCache();
+ mAnimatedImageLoading.Reset();
}
-TextureSet RollingAnimatedImageCache::Frame( uint32_t frameIndex )
+TextureSet RollingAnimatedImageCache::Frame(uint32_t frameIndex)
{
bool popExist = false;
- while( IsFrontReady() && mQueue.Front().mFrameNumber != frameIndex )
+ while(!mQueue.IsEmpty() && mQueue.Front().mFrameNumber != frameIndex)
{
- ImageFrame imageFrame = mQueue.PopFront();
- Dali::Toolkit::TextureManager::RemoveTexture( mImageUrls[ imageFrame.mFrameNumber ].mUrl );
- mImageUrls[ imageFrame.mFrameNumber ].mTextureId = TextureManager::INVALID_TEXTURE_ID;
+ PopFrontCache();
popExist = true;
}
- if( popExist || mImageUrls[ frameIndex ].mTextureId == TextureManager::INVALID_TEXTURE_ID )
+
+ TextureSet textureSet;
+ uint32_t batchFrameIndex = frameIndex;
+ // If we need to load new frame that are not stored in queue.
+ // Load the frame synchronously.
+ bool synchronouslyLoaded = false;
+ if(mIsSynchronousLoading && mQueue.IsEmpty())
+ {
+ textureSet = RequestFrameLoading(frameIndex, true);
+ batchFrameIndex = (frameIndex + 1) % mFrameCount;
+ uint32_t interval = 0u;
+ if(textureSet)
+ {
+ synchronouslyLoaded = true;
+ interval = mAnimatedImageLoading.GetFrameInterval(mQueue.Back().mFrameNumber);
+ }
+ MakeFrameReady(synchronouslyLoaded, textureSet, interval);
+ }
+
+ if(popExist || mQueue.IsEmpty() || synchronouslyLoaded)
{
// If the frame of frameIndex was already loaded, load batch from the last frame of queue
- if( IsFrontReady() )
+ if(!mQueue.IsEmpty())
{
- mFrameIndex = ( mQueue.Back().mFrameNumber + 1 ) % mFrameCount;
+ if(!mLoadWaitingQueue.empty())
+ {
+ batchFrameIndex = (mLoadWaitingQueue.back() + 1) % mFrameCount;
+ }
+ else
+ {
+ batchFrameIndex = (mQueue.Back().mFrameNumber + 1) % mFrameCount;
+ }
}
- // If the queue is empty, load batch from the frame of frameIndex
else
{
- mFrameIndex = frameIndex;
+ // If the request is for the first frame or a jumped frame(JUMP_TO) remove current waiting queue.
+ mLoadWaitingQueue.clear();
+ // If the queue is empty, and the frame of frameIndex is not loaded synchronously. load batch from the frame of frameIndex
+ if(!textureSet)
+ {
+ batchFrameIndex = frameIndex;
+ }
}
- LoadBatch();
+ LoadBatch(batchFrameIndex);
+ }
+
+ if(!textureSet && mLoadState != TextureManager::LoadState::LOAD_FAILED && IsFrontReady() == true)
+ {
+ textureSet = GetFrontTextureSet();
}
- return GetFrontTextureSet();
+ return textureSet;
}
TextureSet RollingAnimatedImageCache::FirstFrame()
{
- return Frame( 0u );
+ TextureSet textureSet = Frame(FIRST_FRAME_INDEX);
+ return textureSet;
}
-uint32_t RollingAnimatedImageCache::GetFrameInterval( uint32_t frameIndex )
+uint32_t RollingAnimatedImageCache::GetFrameInterval(uint32_t frameIndex) const
{
- Frame( frameIndex );
- return mAnimatedImageLoading.GetFrameInterval( frameIndex );
+ if(frameIndex >= mIntervals.size())
+ {
+ return 0u;
+ }
+ return mIntervals[frameIndex];
+}
+
+int32_t RollingAnimatedImageCache::GetCurrentFrameIndex() const
+{
+ if(mQueue.IsEmpty())
+ {
+ return -1;
+ }
+ return mQueue.Front().mFrameNumber;
+}
+
+int32_t RollingAnimatedImageCache::GetTotalFrameCount() const
+{
+ return mFrameCount;
}
bool RollingAnimatedImageCache::IsFrontReady() const
{
- return ( !mQueue.IsEmpty() );
+ return (!mQueue.IsEmpty() && mQueue.Front().mReady);
+}
+
+TextureSet RollingAnimatedImageCache::RequestFrameLoading(uint32_t frameIndex, bool synchronousLoading)
+{
+ ImageFrame imageFrame;
+ imageFrame.mFrameNumber = frameIndex;
+ imageFrame.mReady = false;
+
+ mQueue.PushBack(imageFrame);
+
+ mLoadState = TextureManager::LoadState::LOADING;
+
+ TextureManager::TextureId loadTextureId = TextureManager::INVALID_TEXTURE_ID;
+ TextureSet textureSet = mTextureManager.LoadAnimatedImageTexture(mAnimatedImageLoading,
+ frameIndex,
+ loadTextureId,
+ mMaskingData,
+ SamplingMode::BOX_THEN_LINEAR,
+ Dali::WrapMode::Type::DEFAULT,
+ Dali::WrapMode::Type::DEFAULT,
+ synchronousLoading,
+ this);
+
+ mImageUrls[frameIndex].mTextureId = loadTextureId;
+
+ return textureSet;
}
-void RollingAnimatedImageCache::LoadBatch()
+void RollingAnimatedImageCache::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
// removed, and another frame is loaded
-
- std::vector<Dali::PixelData> pixelDataList;
-
- // Get the smallest number of frames we need to load
- int batchSize = std::min( std::size_t(mBatchSize), mCacheSize - mQueue.Count() );
- DALI_LOG_INFO( gAnimImgLogFilter, Debug::Concise, "RollingAnimatedImageCache::LoadBatch() mFrameIndex:%d batchSize:%d\n", mFrameIndex, batchSize );
- if( mAnimatedImageLoading.LoadNextNFrames( mFrameIndex, batchSize, pixelDataList) )
+ uint32_t minimumSize = std::min(mCacheSize, mFrameCount);
+ for(uint32_t i = 0; i < mBatchSize && (mQueue.Count() + mLoadWaitingQueue.size()) < minimumSize; ++i)
{
- unsigned int pixelDataListCount = pixelDataList.size();
-
- for( unsigned int i = 0; i < pixelDataListCount && !mQueue.IsFull(); ++i )
+ if(mLoadState != TextureManager::LoadState::LOADING)
{
- ImageFrame imageFrame;
-
- // create the texture for uploading the pixel data
- Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D,
- pixelDataList[i].GetPixelFormat(),
- pixelDataList[i].GetWidth(),
- pixelDataList[i].GetHeight() );
-
- texture.Upload( pixelDataList[i] );
-
- mImageUrls[ mUrlIndex ].mUrl = Dali::Toolkit::TextureManager::AddTexture(texture);
- imageFrame.mFrameNumber = mUrlIndex;
-
- ++mUrlIndex;
- mUrlIndex %= mImageUrls.size();
-
- mQueue.PushBack( imageFrame );
-
- 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;
-
- mTextureManager.LoadTexture(
- mImageUrls[ imageFrame.mFrameNumber ].mUrl, ImageDimensions(), FittingMode::SCALE_TO_FILL,
- SamplingMode::BOX_THEN_LINEAR, maskInfo,
- synchronousLoading, mImageUrls[ imageFrame.mFrameNumber ].mTextureId, textureRect, textureRectSize,
- atlasingStatus, loadingStatus, Dali::WrapMode::Type::DEFAULT,
- Dali::WrapMode::Type::DEFAULT, NULL,
- atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED, preMultiply );
+ RequestFrameLoading(frameIndex, false);
+ }
+ else
+ {
+ mLoadWaitingQueue.push_back(frameIndex);
}
- mFrameIndex += batchSize;
- mFrameIndex %= mFrameCount;
+ frameIndex++;
+ frameIndex %= mFrameCount;
}
LOG_CACHE;
}
+void RollingAnimatedImageCache::SetImageFrameReady(TextureManager::TextureId textureId)
+{
+ for(std::size_t i = 0; i < mQueue.Count(); ++i)
+ {
+ if(GetCachedTextureId(i) == textureId)
+ {
+ mQueue[i].mReady = true;
+ break;
+ }
+ }
+}
+
TextureSet RollingAnimatedImageCache::GetFrontTextureSet() const
{
- DALI_LOG_INFO( gAnimImgLogFilter, Debug::Concise, "RollingAnimatedImageCache::GetFrontTextureSet() FrameNumber:%d\n", mQueue[ 0 ].mFrameNumber );
+ DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "RollingAnimatedImageCache::GetFrontTextureSet() FrameNumber:%d\n", mQueue[0].mFrameNumber);
+
+ TextureManager::TextureId textureId = GetCachedTextureId(0);
+ return mTextureManager.GetTextureSet(textureId);
+}
+
+TextureManager::TextureId RollingAnimatedImageCache::GetCachedTextureId(int index) const
+{
+ return mImageUrls[mQueue[index].mFrameNumber].mTextureId;
+}
+
+void RollingAnimatedImageCache::PopFrontCache()
+{
+ ImageFrame imageFrame = mQueue.PopFront();
+ mTextureManager.Remove(mImageUrls[imageFrame.mFrameNumber].mTextureId, this);
+ mImageUrls[imageFrame.mFrameNumber].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 RollingAnimatedImageCache::ClearCache()
+{
+ while(mTextureManagerAlive && !mQueue.IsEmpty())
+ {
+ PopFrontCache();
+ }
+ mLoadWaitingQueue.clear();
+ mLoadState = TextureManager::LoadState::NOT_STARTED;
+}
+
+void RollingAnimatedImageCache::MakeFrameReady(bool loadSuccess, TextureSet textureSet, uint32_t interval)
+{
+ if(!loadSuccess)
+ {
+ mLoadState = TextureManager::LoadState::LOAD_FAILED;
+ mObserver.FrameReady(TextureSet(), 0);
+ }
+ else
+ {
+ mLoadState = TextureManager::LoadState::LOAD_FINISHED;
- TextureManager::TextureId textureId = GetCachedTextureId( 0 );
- return mTextureManager.GetTextureSet( textureId );
+ // Reset size of Queue according to the real frame count.
+ if(mFrameCount != mAnimatedImageLoading.GetImageCount())
+ {
+ mFrameCount = mAnimatedImageLoading.GetImageCount();
+ mImageUrls.resize(mFrameCount);
+ mIntervals.assign(mFrameCount, 0u);
+ }
+
+ bool frontFrameReady = IsFrontReady();
+ // Because only one frame is on loading and the others are in mLoadWaitingQueue,
+ // mQueue.Back() is always the frame currently loaded.
+ mQueue.Back().mReady = true;
+ mIntervals[mQueue.Back().mFrameNumber] = interval;
+ // Check whether currently loaded frame is front of queue or not.
+ // If it is, notify frame ready to observer.
+ if(frontFrameReady == false && IsFrontReady())
+ {
+ mObserver.FrameReady(textureSet, interval);
+ }
+ }
}
-TextureManager::TextureId RollingAnimatedImageCache::GetCachedTextureId( int index ) const
+void RollingAnimatedImageCache::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
{
- return mImageUrls[ mQueue[ index ].mFrameNumber ].mTextureId;
+ DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "AnimatedImageVisual::LoadComplete(textureId:%d) start\n", textureInformation.textureId);
+ LOG_CACHE;
+
+ MakeFrameReady(loadSuccess, mTextureManager.GetTextureSet(textureInformation.textureId), textureInformation.interval);
+
+ if(loadSuccess)
+ {
+ // The frames of a single animated image can not be loaded parallelly.
+ // Therefore, a frame is now loading, other orders are waiting.
+ // And, after the frame is loaded, requests load of next order.
+ if(!mLoadWaitingQueue.empty())
+ {
+ uint32_t loadingIndex = mLoadWaitingQueue.front();
+ mLoadWaitingQueue.erase(mLoadWaitingQueue.begin());
+ RequestFrameLoading(loadingIndex, false);
+ }
+ else if(mQueue.Count() == 1u && textureInformation.frameCount > SINGLE_IMAGE_COUNT)
+ {
+ // There is only an image in queue and no waiting queue.
+ // Request to load batch once again.
+ uint32_t batchFrameIndex = 0u;
+ if(!mLoadWaitingQueue.empty())
+ {
+ batchFrameIndex = (mLoadWaitingQueue.back() + 1) % mFrameCount;
+ }
+ else
+ {
+ batchFrameIndex = (mQueue.Back().mFrameNumber + 1) % mFrameCount;
+ }
+ LoadBatch(batchFrameIndex);
+ }
+ }
+
+ LOG_CACHE;
}
} //namespace Internal