2 * Copyright (c) 2021 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(
63 TextureManager& textureManager, AnimatedImageLoading& animatedImageLoading, ImageCache::FrameReadyObserver& observer, uint16_t cacheSize, uint16_t batchSize, bool isSynchronousLoading)
64 : ImageCache(textureManager, observer, batchSize, 0u),
65 mAnimatedImageLoading(animatedImageLoading),
66 mFrameCount(SINGLE_IMAGE_COUNT),
67 mFrameIndex(FIRST_FRAME_INDEX),
68 mCacheSize(cacheSize),
70 mIsSynchronousLoading(isSynchronousLoading)
72 mImageUrls.resize(mFrameCount);
73 mIntervals.assign(mFrameCount, 0);
76 RollingAnimatedImageCache::~RollingAnimatedImageCache()
79 mAnimatedImageLoading.Reset();
82 TextureSet RollingAnimatedImageCache::Frame(uint32_t frameIndex)
84 bool popExist = false;
85 while(!mQueue.IsEmpty() && mQueue.Front().mFrameNumber != frameIndex)
87 ImageFrame imageFrame = mQueue.PopFront();
88 mTextureManager.Remove(mImageUrls[imageFrame.mFrameNumber].mTextureId, this);
89 mImageUrls[imageFrame.mFrameNumber].mTextureId = TextureManager::INVALID_TEXTURE_ID;
93 TextureSet textureSet;
94 uint32_t batchFrameIndex = frameIndex;
95 // If we need to load new frame that are not stored in queue.
96 // Load the frame synchronously.
97 bool synchronouslyLoaded = false;
98 if(mIsSynchronousLoading && mQueue.IsEmpty())
100 textureSet = RequestFrameLoading(frameIndex, frameIndex == FIRST_FRAME_INDEX, true);
101 batchFrameIndex = (frameIndex + 1) % mFrameCount;
102 uint32_t interval = 0u;
105 synchronouslyLoaded = true;
106 interval = mAnimatedImageLoading.GetFrameInterval(mQueue.Back().mFrameNumber);
108 MakeFrameReady(synchronouslyLoaded, textureSet, interval);
111 if(popExist || mQueue.IsEmpty() || synchronouslyLoaded)
113 // If the frame of frameIndex was already loaded, load batch from the last frame of queue
114 if(!mQueue.IsEmpty())
116 if(!mLoadWaitingQueue.empty())
118 batchFrameIndex = (mLoadWaitingQueue.back() + 1) % mFrameCount;
122 batchFrameIndex = (mQueue.Back().mFrameNumber + 1) % mFrameCount;
127 // If the request is for the first frame or a jumped frame(JUMP_TO) remove current waiting queue.
128 mLoadWaitingQueue.clear();
129 // If the queue is empty, and the frame of frameIndex is not loaded synchronously. load batch from the frame of frameIndex
132 batchFrameIndex = frameIndex;
135 LoadBatch(batchFrameIndex);
138 if(!textureSet && mLoadState != TextureManager::LoadState::LOAD_FAILED && IsFrontReady() == true)
140 textureSet = GetFrontTextureSet();
146 TextureSet RollingAnimatedImageCache::FirstFrame()
148 TextureSet textureSet = Frame(FIRST_FRAME_INDEX);
152 uint32_t RollingAnimatedImageCache::GetFrameInterval(uint32_t frameIndex) const
154 if(frameIndex >= mIntervals.size())
158 return mIntervals[frameIndex];
161 int32_t RollingAnimatedImageCache::GetCurrentFrameIndex() const
167 return mQueue.Front().mFrameNumber;
170 int32_t RollingAnimatedImageCache::GetTotalFrameCount() const
175 bool RollingAnimatedImageCache::IsFrontReady() const
177 return (!mQueue.IsEmpty() && mQueue.Front().mReady);
180 TextureSet RollingAnimatedImageCache::RequestFrameLoading(uint32_t frameIndex, bool useCache, bool synchronousLoading)
182 ImageFrame imageFrame;
183 imageFrame.mFrameNumber = frameIndex;
184 imageFrame.mReady = false;
186 mQueue.PushBack(imageFrame);
188 mLoadState = TextureManager::LoadState::LOADING;
190 TextureManager::TextureId loadTextureId = TextureManager::INVALID_TEXTURE_ID;
191 TextureSet textureSet = mTextureManager.LoadAnimatedImageTexture(mAnimatedImageLoading,
194 SamplingMode::BOX_THEN_LINEAR,
195 Dali::WrapMode::Type::DEFAULT,
196 Dali::WrapMode::Type::DEFAULT,
201 mImageUrls[frameIndex].mTextureId = loadTextureId;
206 void RollingAnimatedImageCache::LoadBatch(uint32_t frameIndex)
208 // Try and load up to mBatchSize images, until the cache is filled.
209 // Once the cache is filled, as frames progress, the old frame is
210 // removed, and another frame is loaded
211 uint32_t minimumSize = std::min(mCacheSize, mFrameCount);
212 for(uint32_t i = 0; i < mBatchSize && (mQueue.Count() + mLoadWaitingQueue.size()) < minimumSize; ++i)
214 if(mLoadState != TextureManager::LoadState::LOADING)
216 RequestFrameLoading(frameIndex, frameIndex == FIRST_FRAME_INDEX, false);
220 mLoadWaitingQueue.push_back(frameIndex);
224 frameIndex %= mFrameCount;
230 void RollingAnimatedImageCache::SetImageFrameReady(TextureManager::TextureId textureId)
232 for(std::size_t i = 0; i < mQueue.Count(); ++i)
234 if(GetCachedTextureId(i) == textureId)
236 mQueue[i].mReady = true;
242 TextureSet RollingAnimatedImageCache::GetFrontTextureSet() const
244 DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "RollingAnimatedImageCache::GetFrontTextureSet() FrameNumber:%d\n", mQueue[0].mFrameNumber);
246 TextureManager::TextureId textureId = GetCachedTextureId(0);
247 return mTextureManager.GetTextureSet(textureId);
250 TextureManager::TextureId RollingAnimatedImageCache::GetCachedTextureId(int index) const
252 return mImageUrls[mQueue[index].mFrameNumber].mTextureId;
255 void RollingAnimatedImageCache::ClearCache()
257 while(mTextureManagerAlive && !mQueue.IsEmpty())
259 ImageFrame imageFrame = mQueue.PopFront();
260 mTextureManager.Remove(mImageUrls[imageFrame.mFrameNumber].mTextureId, this);
261 mImageUrls[imageFrame.mFrameNumber].mTextureId = TextureManager::INVALID_TEXTURE_ID;
263 mLoadWaitingQueue.clear();
264 mLoadState = TextureManager::LoadState::NOT_STARTED;
267 void RollingAnimatedImageCache::MakeFrameReady(bool loadSuccess, TextureSet textureSet, uint32_t interval)
271 mLoadState = TextureManager::LoadState::LOAD_FAILED;
272 mObserver.FrameReady(TextureSet(), 0);
276 mLoadState = TextureManager::LoadState::LOAD_FINISHED;
278 // Reset size of Queue according to the real frame count.
279 if(mFrameCount != mAnimatedImageLoading.GetImageCount())
281 mFrameCount = mAnimatedImageLoading.GetImageCount();
282 mImageUrls.resize(mFrameCount);
283 mIntervals.assign(mFrameCount, 0u);
286 bool frontFrameReady = IsFrontReady();
287 // Because only one frame is on loading and the others are in mLoadWaitingQueue,
288 // mQueue.Back() is always the frame currently loaded.
289 mQueue.Back().mReady = true;
290 mIntervals[mQueue.Back().mFrameNumber] = interval;
291 // Check whether currently loaded frame is front of queue or not.
292 // If it is, notify frame ready to observer.
293 if(frontFrameReady == false && IsFrontReady())
295 mObserver.FrameReady(textureSet, interval);
300 void RollingAnimatedImageCache::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
302 DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "AnimatedImageVisual::LoadComplete(textureId:%d) start\n", textureInformation.textureId);
305 MakeFrameReady(loadSuccess, mTextureManager.GetTextureSet(textureInformation.textureId), textureInformation.interval);
309 // The frames of a single animated image can not be loaded parallelly.
310 // Therefore, a frame is now loading, other orders are waiting.
311 // And, after the frame is loaded, requests load of next order.
312 if(!mLoadWaitingQueue.empty())
314 uint32_t loadingIndex = mLoadWaitingQueue.front();
315 mLoadWaitingQueue.erase(mLoadWaitingQueue.begin());
316 RequestFrameLoading(loadingIndex, loadingIndex == FIRST_FRAME_INDEX, false);
318 else if(mQueue.Count() == 1u && textureInformation.frameCount > SINGLE_IMAGE_COUNT)
320 // There is only an image in queue and no waiting queue.
321 // Request to load batch once again.
322 uint32_t batchFrameIndex = 0u;
323 if(!mLoadWaitingQueue.empty())
325 batchFrameIndex = (mLoadWaitingQueue.back() + 1) % mFrameCount;
329 batchFrameIndex = (mQueue.Back().mFrameNumber + 1) % mFrameCount;
331 LoadBatch(batchFrameIndex);
338 } //namespace Internal
339 } //namespace Toolkit