X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Finternal%2Fvisuals%2Fanimated-image%2Frolling-animated-image-cache.cpp;h=b8a6961d2c5eefe7a0e253ecd59e8d5ea05af532;hp=c42167844db86b3a16edc4d86be4200fd6a259e0;hb=e483020277e28cd5f93a93cf50119edaac49a9e9;hpb=e5d4cd830e75a208dd18c743e1564a4ed1fde780 diff --git a/dali-toolkit/internal/visuals/animated-image/rolling-animated-image-cache.cpp b/dali-toolkit/internal/visuals/animated-image/rolling-animated-image-cache.cpp index c421678..b8a6961 100644 --- a/dali-toolkit/internal/visuals/animated-image/rolling-animated-image-cache.cpp +++ b/dali-toolkit/internal/visuals/animated-image/rolling-animated-image-cache.cpp @@ -1,5 +1,5 @@ /* - * 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. @@ -17,8 +17,6 @@ // CLASS HEADER #include "rolling-animated-image-cache.h" -// EXTERNAL HEADERS - // INTERNAL HEADERS #include #include // For ImageAtlasManagerPtr @@ -29,27 +27,26 @@ 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:"<IsEnabledFor(Debug::Concise)) \ + { \ + std::ostringstream oss; \ + oss << "Size:" << mQueue.Count() << " [ "; \ + for(std::size_t _i = 0; _i < mQueue.Count(); ++_i) \ + { \ + oss << _i << "={ frm#: " << mQueue[_i].mFrameNumber << " tex: " << mTextureIds[mQueue[_i].mFrameNumber] << "}, "; \ + } \ + 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 { @@ -57,149 +54,323 @@ namespace Toolkit { 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, + const Dali::WrapMode::Type& wrapModeU, + const Dali::WrapMode::Type& wrapModeV, + bool isSynchronousLoading, + bool preMultiplyOnLoad) +: ImageCache(textureManager, maskingData, observer, batchSize, 0u), + mImageUrl(animatedImageLoading.GetUrl()), + mAnimatedImageLoading(animatedImageLoading), + mFrameCount(SINGLE_IMAGE_COUNT), + mFrameIndex(FIRST_FRAME_INDEX), + mCacheSize(cacheSize), + mQueue(cacheSize), + mWrapModeU(wrapModeU), + mWrapModeV(wrapModeV), + mIsSynchronousLoading(isSynchronousLoading), + mPreMultiplyOnLoad(preMultiplyOnLoad) { - mImageUrls.resize( mFrameCount ); - LoadBatch(); + mTextureIds.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); } -void RollingAnimatedImageCache::LoadBatch() +TextureSet RollingAnimatedImageCache::RequestFrameLoading(uint32_t frameIndex, bool synchronousLoading) +{ + ImageFrame imageFrame; + imageFrame.mFrameNumber = frameIndex; + imageFrame.mReady = false; + + mQueue.PushBack(imageFrame); + + mLoadState = TextureManager::LoadState::LOADING; + + auto preMultiplyOnLoading = mPreMultiplyOnLoad ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD + : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY; + + TextureManager::TextureId loadTextureId = TextureManager::INVALID_TEXTURE_ID; + TextureSet textureSet = mTextureManager.LoadAnimatedImageTexture(mImageUrl, + mAnimatedImageLoading, + frameIndex, + loadTextureId, + mMaskingData, + SamplingMode::BOX_THEN_LINEAR, + mWrapModeU, + mWrapModeV, + synchronousLoading, + this, + preMultiplyOnLoading); + + mTextureIds[frameIndex] = loadTextureId; + + return textureSet; +} + +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 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 mTextureIds[mQueue[index].mFrameNumber]; +} + +void RollingAnimatedImageCache::PopFrontCache() +{ + ImageFrame imageFrame = mQueue.PopFront(); + mTextureManager.Remove(mTextureIds[imageFrame.mFrameNumber], this); + mTextureIds[imageFrame.mFrameNumber] = 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; + + // Reset size of Queue according to the real frame count. + if(mFrameCount != mAnimatedImageLoading.GetImageCount()) + { + mFrameCount = mAnimatedImageLoading.GetImageCount(); + mTextureIds.resize(mFrameCount); + mIntervals.assign(mFrameCount, 0u); + } - TextureManager::TextureId textureId = GetCachedTextureId( 0 ); - return mTextureManager.GetTextureSet( textureId ); + 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; + + TextureSet textureSet = mTextureManager.GetTextureSet(textureInformation.textureId); + if(textureSet) + { + Sampler sampler = Sampler::New(); + sampler.SetWrapMode(mWrapModeU, mWrapModeV); + textureSet.SetSampler(0u, sampler); + } + + MakeFrameReady(loadSuccess, textureSet, 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