2 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "rolling-animated-image-cache.h"
21 #include <dali-toolkit/devel-api/image-loader/texture-manager.h>
22 #include <dali-toolkit/internal/visuals/image-atlas-manager.h> // For ImageAtlasManagerPtr
23 #include <dali/integration-api/debug.h>
27 #if defined(DEBUG_ENABLED)
28 Debug::Filter* gAnimImgLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_ANIMATED_IMAGE");
32 std::ostringstream oss; \
33 oss << "Size:" << mQueue.Count() << " [ "; \
34 for(std::size_t _i = 0; _i < mQueue.Count(); ++_i) \
36 oss << _i << "={ frm#: " << mQueue[_i].mFrameNumber << " tex: " << mImageUrls[mQueue[_i].mFrameNumber].mTextureId << "}, "; \
38 oss << " ]" << std::endl; \
39 DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "%s", oss.str().c_str()); \
46 static constexpr bool ENABLE_ORIENTATION_CORRECTION(true);
58 static constexpr uint32_t SINGLE_IMAGE_COUNT = 1u;
59 static constexpr uint32_t FIRST_FRAME_INDEX = 0u;
62 RollingAnimatedImageCache::RollingAnimatedImageCache(TextureManager& textureManager,
63 AnimatedImageLoading& animatedImageLoading,
64 TextureManager::MaskingDataPointer& maskingData,
65 ImageCache::FrameReadyObserver& observer,
68 bool isSynchronousLoading)
69 : ImageCache(textureManager, maskingData, observer, batchSize, 0u),
70 mAnimatedImageLoading(animatedImageLoading),
71 mFrameCount(SINGLE_IMAGE_COUNT),
72 mFrameIndex(FIRST_FRAME_INDEX),
73 mCacheSize(cacheSize),
75 mIsSynchronousLoading(isSynchronousLoading)
77 mImageUrls.resize(mFrameCount);
78 mIntervals.assign(mFrameCount, 0);
81 RollingAnimatedImageCache::~RollingAnimatedImageCache()
84 mAnimatedImageLoading.Reset();
87 TextureSet RollingAnimatedImageCache::Frame(uint32_t frameIndex)
89 bool popExist = false;
90 while(!mQueue.IsEmpty() && mQueue.Front().mFrameNumber != frameIndex)
96 TextureSet textureSet;
97 uint32_t batchFrameIndex = frameIndex;
98 // If we need to load new frame that are not stored in queue.
99 // Load the frame synchronously.
100 bool synchronouslyLoaded = false;
101 if(mIsSynchronousLoading && mQueue.IsEmpty())
103 textureSet = RequestFrameLoading(frameIndex, true);
104 batchFrameIndex = (frameIndex + 1) % mFrameCount;
105 uint32_t interval = 0u;
108 synchronouslyLoaded = true;
109 interval = mAnimatedImageLoading.GetFrameInterval(mQueue.Back().mFrameNumber);
111 MakeFrameReady(synchronouslyLoaded, textureSet, interval);
114 if(popExist || mQueue.IsEmpty() || synchronouslyLoaded)
116 // If the frame of frameIndex was already loaded, load batch from the last frame of queue
117 if(!mQueue.IsEmpty())
119 if(!mLoadWaitingQueue.empty())
121 batchFrameIndex = (mLoadWaitingQueue.back() + 1) % mFrameCount;
125 batchFrameIndex = (mQueue.Back().mFrameNumber + 1) % mFrameCount;
130 // If the request is for the first frame or a jumped frame(JUMP_TO) remove current waiting queue.
131 mLoadWaitingQueue.clear();
132 // If the queue is empty, and the frame of frameIndex is not loaded synchronously. load batch from the frame of frameIndex
135 batchFrameIndex = frameIndex;
138 LoadBatch(batchFrameIndex);
141 if(!textureSet && mLoadState != TextureManager::LoadState::LOAD_FAILED && IsFrontReady() == true)
143 textureSet = GetFrontTextureSet();
149 TextureSet RollingAnimatedImageCache::FirstFrame()
151 TextureSet textureSet = Frame(FIRST_FRAME_INDEX);
155 uint32_t RollingAnimatedImageCache::GetFrameInterval(uint32_t frameIndex) const
157 if(frameIndex >= mIntervals.size())
161 return mIntervals[frameIndex];
164 int32_t RollingAnimatedImageCache::GetCurrentFrameIndex() const
170 return mQueue.Front().mFrameNumber;
173 int32_t RollingAnimatedImageCache::GetTotalFrameCount() const
178 bool RollingAnimatedImageCache::IsFrontReady() const
180 return (!mQueue.IsEmpty() && mQueue.Front().mReady);
183 TextureSet RollingAnimatedImageCache::RequestFrameLoading(uint32_t frameIndex, bool synchronousLoading)
185 ImageFrame imageFrame;
186 imageFrame.mFrameNumber = frameIndex;
187 imageFrame.mReady = false;
189 mQueue.PushBack(imageFrame);
191 mLoadState = TextureManager::LoadState::LOADING;
193 TextureManager::TextureId loadTextureId = TextureManager::INVALID_TEXTURE_ID;
194 TextureSet textureSet = mTextureManager.LoadAnimatedImageTexture(mAnimatedImageLoading,
198 SamplingMode::BOX_THEN_LINEAR,
199 Dali::WrapMode::Type::DEFAULT,
200 Dali::WrapMode::Type::DEFAULT,
204 mImageUrls[frameIndex].mTextureId = loadTextureId;
209 void RollingAnimatedImageCache::LoadBatch(uint32_t frameIndex)
211 // Try and load up to mBatchSize images, until the cache is filled.
212 // Once the cache is filled, as frames progress, the old frame is
213 // removed, and another frame is loaded
214 uint32_t minimumSize = std::min(mCacheSize, mFrameCount);
215 for(uint32_t i = 0; i < mBatchSize && (mQueue.Count() + mLoadWaitingQueue.size()) < minimumSize; ++i)
217 if(mLoadState != TextureManager::LoadState::LOADING)
219 RequestFrameLoading(frameIndex, false);
223 mLoadWaitingQueue.push_back(frameIndex);
227 frameIndex %= mFrameCount;
233 void RollingAnimatedImageCache::SetImageFrameReady(TextureManager::TextureId textureId)
235 for(std::size_t i = 0; i < mQueue.Count(); ++i)
237 if(GetCachedTextureId(i) == textureId)
239 mQueue[i].mReady = true;
245 TextureSet RollingAnimatedImageCache::GetFrontTextureSet() const
247 DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "RollingAnimatedImageCache::GetFrontTextureSet() FrameNumber:%d\n", mQueue[0].mFrameNumber);
249 TextureManager::TextureId textureId = GetCachedTextureId(0);
250 return mTextureManager.GetTextureSet(textureId);
253 TextureManager::TextureId RollingAnimatedImageCache::GetCachedTextureId(int index) const
255 return mImageUrls[mQueue[index].mFrameNumber].mTextureId;
258 void RollingAnimatedImageCache::PopFrontCache()
260 ImageFrame imageFrame = mQueue.PopFront();
261 mTextureManager.Remove(mImageUrls[imageFrame.mFrameNumber].mTextureId, this);
262 mImageUrls[imageFrame.mFrameNumber].mTextureId = TextureManager::INVALID_TEXTURE_ID;
264 if(mMaskingData && mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID)
266 mTextureManager.Remove(mMaskingData->mAlphaMaskId, this);
269 mMaskingData->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
274 void RollingAnimatedImageCache::ClearCache()
276 while(mTextureManagerAlive && !mQueue.IsEmpty())
280 mLoadWaitingQueue.clear();
281 mLoadState = TextureManager::LoadState::NOT_STARTED;
284 void RollingAnimatedImageCache::MakeFrameReady(bool loadSuccess, TextureSet textureSet, uint32_t interval)
288 mLoadState = TextureManager::LoadState::LOAD_FAILED;
289 mObserver.FrameReady(TextureSet(), 0);
293 mLoadState = TextureManager::LoadState::LOAD_FINISHED;
295 // Reset size of Queue according to the real frame count.
296 if(mFrameCount != mAnimatedImageLoading.GetImageCount())
298 mFrameCount = mAnimatedImageLoading.GetImageCount();
299 mImageUrls.resize(mFrameCount);
300 mIntervals.assign(mFrameCount, 0u);
303 bool frontFrameReady = IsFrontReady();
304 // Because only one frame is on loading and the others are in mLoadWaitingQueue,
305 // mQueue.Back() is always the frame currently loaded.
306 mQueue.Back().mReady = true;
307 mIntervals[mQueue.Back().mFrameNumber] = interval;
308 // Check whether currently loaded frame is front of queue or not.
309 // If it is, notify frame ready to observer.
310 if(frontFrameReady == false && IsFrontReady())
312 mObserver.FrameReady(textureSet, interval);
317 void RollingAnimatedImageCache::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
319 DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "AnimatedImageVisual::LoadComplete(textureId:%d) start\n", textureInformation.textureId);
322 MakeFrameReady(loadSuccess, mTextureManager.GetTextureSet(textureInformation.textureId), textureInformation.interval);
326 // The frames of a single animated image can not be loaded parallelly.
327 // Therefore, a frame is now loading, other orders are waiting.
328 // And, after the frame is loaded, requests load of next order.
329 if(!mLoadWaitingQueue.empty())
331 uint32_t loadingIndex = mLoadWaitingQueue.front();
332 mLoadWaitingQueue.erase(mLoadWaitingQueue.begin());
333 RequestFrameLoading(loadingIndex, false);
335 else if(mQueue.Count() == 1u && textureInformation.frameCount > SINGLE_IMAGE_COUNT)
337 // There is only an image in queue and no waiting queue.
338 // Request to load batch once again.
339 uint32_t batchFrameIndex = 0u;
340 if(!mLoadWaitingQueue.empty())
342 batchFrameIndex = (mLoadWaitingQueue.back() + 1) % mFrameCount;
346 batchFrameIndex = (mQueue.Back().mFrameNumber + 1) % mFrameCount;
348 LoadBatch(batchFrameIndex);
355 } //namespace Internal
356 } //namespace Toolkit