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(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, frameIndex == FIRST_FRAME_INDEX, 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 useCache, 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,
205 mImageUrls[frameIndex].mTextureId = loadTextureId;
210 void RollingAnimatedImageCache::LoadBatch(uint32_t frameIndex)
212 // Try and load up to mBatchSize images, until the cache is filled.
213 // Once the cache is filled, as frames progress, the old frame is
214 // removed, and another frame is loaded
215 uint32_t minimumSize = std::min(mCacheSize, mFrameCount);
216 for(uint32_t i = 0; i < mBatchSize && (mQueue.Count() + mLoadWaitingQueue.size()) < minimumSize; ++i)
218 if(mLoadState != TextureManager::LoadState::LOADING)
220 RequestFrameLoading(frameIndex, frameIndex == FIRST_FRAME_INDEX, false);
224 mLoadWaitingQueue.push_back(frameIndex);
228 frameIndex %= mFrameCount;
234 void RollingAnimatedImageCache::SetImageFrameReady(TextureManager::TextureId textureId)
236 for(std::size_t i = 0; i < mQueue.Count(); ++i)
238 if(GetCachedTextureId(i) == textureId)
240 mQueue[i].mReady = true;
246 TextureSet RollingAnimatedImageCache::GetFrontTextureSet() const
248 DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "RollingAnimatedImageCache::GetFrontTextureSet() FrameNumber:%d\n", mQueue[0].mFrameNumber);
250 TextureManager::TextureId textureId = GetCachedTextureId(0);
251 return mTextureManager.GetTextureSet(textureId);
254 TextureManager::TextureId RollingAnimatedImageCache::GetCachedTextureId(int index) const
256 return mImageUrls[mQueue[index].mFrameNumber].mTextureId;
259 void RollingAnimatedImageCache::PopFrontCache()
261 ImageFrame imageFrame = mQueue.PopFront();
262 mTextureManager.Remove(mImageUrls[imageFrame.mFrameNumber].mTextureId, this);
263 mImageUrls[imageFrame.mFrameNumber].mTextureId = TextureManager::INVALID_TEXTURE_ID;
265 if(mMaskingData && mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID)
267 mTextureManager.Remove(mMaskingData->mAlphaMaskId, this);
270 mMaskingData->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
275 void RollingAnimatedImageCache::ClearCache()
277 while(mTextureManagerAlive && !mQueue.IsEmpty())
281 mLoadWaitingQueue.clear();
282 mLoadState = TextureManager::LoadState::NOT_STARTED;
285 void RollingAnimatedImageCache::MakeFrameReady(bool loadSuccess, TextureSet textureSet, uint32_t interval)
289 mLoadState = TextureManager::LoadState::LOAD_FAILED;
290 mObserver.FrameReady(TextureSet(), 0);
294 mLoadState = TextureManager::LoadState::LOAD_FINISHED;
296 // Reset size of Queue according to the real frame count.
297 if(mFrameCount != mAnimatedImageLoading.GetImageCount())
299 mFrameCount = mAnimatedImageLoading.GetImageCount();
300 mImageUrls.resize(mFrameCount);
301 mIntervals.assign(mFrameCount, 0u);
304 bool frontFrameReady = IsFrontReady();
305 // Because only one frame is on loading and the others are in mLoadWaitingQueue,
306 // mQueue.Back() is always the frame currently loaded.
307 mQueue.Back().mReady = true;
308 mIntervals[mQueue.Back().mFrameNumber] = interval;
309 // Check whether currently loaded frame is front of queue or not.
310 // If it is, notify frame ready to observer.
311 if(frontFrameReady == false && IsFrontReady())
313 mObserver.FrameReady(textureSet, interval);
318 void RollingAnimatedImageCache::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
320 DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "AnimatedImageVisual::LoadComplete(textureId:%d) start\n", textureInformation.textureId);
323 MakeFrameReady(loadSuccess, mTextureManager.GetTextureSet(textureInformation.textureId), textureInformation.interval);
327 // The frames of a single animated image can not be loaded parallelly.
328 // Therefore, a frame is now loading, other orders are waiting.
329 // And, after the frame is loaded, requests load of next order.
330 if(!mLoadWaitingQueue.empty())
332 uint32_t loadingIndex = mLoadWaitingQueue.front();
333 mLoadWaitingQueue.erase(mLoadWaitingQueue.begin());
334 RequestFrameLoading(loadingIndex, loadingIndex == FIRST_FRAME_INDEX, false);
336 else if(mQueue.Count() == 1u && textureInformation.frameCount > SINGLE_IMAGE_COUNT)
338 // There is only an image in queue and no waiting queue.
339 // Request to load batch once again.
340 uint32_t batchFrameIndex = 0u;
341 if(!mLoadWaitingQueue.empty())
343 batchFrameIndex = (mLoadWaitingQueue.back() + 1) % mFrameCount;
347 batchFrameIndex = (mQueue.Back().mFrameNumber + 1) % mFrameCount;
349 LoadBatch(batchFrameIndex);
356 } //namespace Internal
357 } //namespace Toolkit