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: " << mTextureIds[mQueue[_i].mFrameNumber] << "}, "; \
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 mImageUrl(animatedImageLoading.GetUrl()),
71 mAnimatedImageLoading(animatedImageLoading),
72 mFrameCount(SINGLE_IMAGE_COUNT),
73 mFrameIndex(FIRST_FRAME_INDEX),
74 mCacheSize(cacheSize),
76 mIsSynchronousLoading(isSynchronousLoading)
78 mTextureIds.resize(mFrameCount);
79 mIntervals.assign(mFrameCount, 0);
82 RollingAnimatedImageCache::~RollingAnimatedImageCache()
85 mAnimatedImageLoading.Reset();
88 TextureSet RollingAnimatedImageCache::Frame(uint32_t frameIndex)
90 bool popExist = false;
91 while(!mQueue.IsEmpty() && mQueue.Front().mFrameNumber != frameIndex)
97 TextureSet textureSet;
98 uint32_t batchFrameIndex = frameIndex;
99 // If we need to load new frame that are not stored in queue.
100 // Load the frame synchronously.
101 bool synchronouslyLoaded = false;
102 if(mIsSynchronousLoading && mQueue.IsEmpty())
104 textureSet = RequestFrameLoading(frameIndex, true);
105 batchFrameIndex = (frameIndex + 1) % mFrameCount;
106 uint32_t interval = 0u;
109 synchronouslyLoaded = true;
110 interval = mAnimatedImageLoading.GetFrameInterval(mQueue.Back().mFrameNumber);
112 MakeFrameReady(synchronouslyLoaded, textureSet, interval);
115 if(popExist || mQueue.IsEmpty() || synchronouslyLoaded)
117 // If the frame of frameIndex was already loaded, load batch from the last frame of queue
118 if(!mQueue.IsEmpty())
120 if(!mLoadWaitingQueue.empty())
122 batchFrameIndex = (mLoadWaitingQueue.back() + 1) % mFrameCount;
126 batchFrameIndex = (mQueue.Back().mFrameNumber + 1) % mFrameCount;
131 // If the request is for the first frame or a jumped frame(JUMP_TO) remove current waiting queue.
132 mLoadWaitingQueue.clear();
133 // If the queue is empty, and the frame of frameIndex is not loaded synchronously. load batch from the frame of frameIndex
136 batchFrameIndex = frameIndex;
139 LoadBatch(batchFrameIndex);
142 if(!textureSet && mLoadState != TextureManager::LoadState::LOAD_FAILED && IsFrontReady() == true)
144 textureSet = GetFrontTextureSet();
150 TextureSet RollingAnimatedImageCache::FirstFrame()
152 TextureSet textureSet = Frame(FIRST_FRAME_INDEX);
156 uint32_t RollingAnimatedImageCache::GetFrameInterval(uint32_t frameIndex) const
158 if(frameIndex >= mIntervals.size())
162 return mIntervals[frameIndex];
165 int32_t RollingAnimatedImageCache::GetCurrentFrameIndex() const
171 return mQueue.Front().mFrameNumber;
174 int32_t RollingAnimatedImageCache::GetTotalFrameCount() const
179 bool RollingAnimatedImageCache::IsFrontReady() const
181 return (!mQueue.IsEmpty() && mQueue.Front().mReady);
184 TextureSet RollingAnimatedImageCache::RequestFrameLoading(uint32_t frameIndex, bool synchronousLoading)
186 ImageFrame imageFrame;
187 imageFrame.mFrameNumber = frameIndex;
188 imageFrame.mReady = false;
190 mQueue.PushBack(imageFrame);
192 mLoadState = TextureManager::LoadState::LOADING;
194 TextureManager::TextureId loadTextureId = TextureManager::INVALID_TEXTURE_ID;
195 TextureSet textureSet = mTextureManager.LoadAnimatedImageTexture(mImageUrl,
196 mAnimatedImageLoading,
200 SamplingMode::BOX_THEN_LINEAR,
201 Dali::WrapMode::Type::DEFAULT,
202 Dali::WrapMode::Type::DEFAULT,
206 mTextureIds[frameIndex] = loadTextureId;
211 void RollingAnimatedImageCache::LoadBatch(uint32_t frameIndex)
213 // Try and load up to mBatchSize images, until the cache is filled.
214 // Once the cache is filled, as frames progress, the old frame is
215 // removed, and another frame is loaded
216 uint32_t minimumSize = std::min(mCacheSize, mFrameCount);
217 for(uint32_t i = 0; i < mBatchSize && (mQueue.Count() + mLoadWaitingQueue.size()) < minimumSize; ++i)
219 if(mLoadState != TextureManager::LoadState::LOADING)
221 RequestFrameLoading(frameIndex, false);
225 mLoadWaitingQueue.push_back(frameIndex);
229 frameIndex %= mFrameCount;
235 void RollingAnimatedImageCache::SetImageFrameReady(TextureManager::TextureId textureId)
237 for(std::size_t i = 0; i < mQueue.Count(); ++i)
239 if(GetCachedTextureId(i) == textureId)
241 mQueue[i].mReady = true;
247 TextureSet RollingAnimatedImageCache::GetFrontTextureSet() const
249 DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "RollingAnimatedImageCache::GetFrontTextureSet() FrameNumber:%d\n", mQueue[0].mFrameNumber);
251 TextureManager::TextureId textureId = GetCachedTextureId(0);
252 return mTextureManager.GetTextureSet(textureId);
255 TextureManager::TextureId RollingAnimatedImageCache::GetCachedTextureId(int index) const
257 return mTextureIds[mQueue[index].mFrameNumber];
260 void RollingAnimatedImageCache::PopFrontCache()
262 ImageFrame imageFrame = mQueue.PopFront();
263 mTextureManager.Remove(mTextureIds[imageFrame.mFrameNumber], this);
264 mTextureIds[imageFrame.mFrameNumber] = TextureManager::INVALID_TEXTURE_ID;
266 if(mMaskingData && mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID)
268 mTextureManager.Remove(mMaskingData->mAlphaMaskId, this);
271 mMaskingData->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
276 void RollingAnimatedImageCache::ClearCache()
278 while(mTextureManagerAlive && !mQueue.IsEmpty())
282 mLoadWaitingQueue.clear();
283 mLoadState = TextureManager::LoadState::NOT_STARTED;
286 void RollingAnimatedImageCache::MakeFrameReady(bool loadSuccess, TextureSet textureSet, uint32_t interval)
290 mLoadState = TextureManager::LoadState::LOAD_FAILED;
291 mObserver.FrameReady(TextureSet(), 0);
295 mLoadState = TextureManager::LoadState::LOAD_FINISHED;
297 // Reset size of Queue according to the real frame count.
298 if(mFrameCount != mAnimatedImageLoading.GetImageCount())
300 mFrameCount = mAnimatedImageLoading.GetImageCount();
301 mTextureIds.resize(mFrameCount);
302 mIntervals.assign(mFrameCount, 0u);
305 bool frontFrameReady = IsFrontReady();
306 // Because only one frame is on loading and the others are in mLoadWaitingQueue,
307 // mQueue.Back() is always the frame currently loaded.
308 mQueue.Back().mReady = true;
309 mIntervals[mQueue.Back().mFrameNumber] = interval;
310 // Check whether currently loaded frame is front of queue or not.
311 // If it is, notify frame ready to observer.
312 if(frontFrameReady == false && IsFrontReady())
314 mObserver.FrameReady(textureSet, interval);
319 void RollingAnimatedImageCache::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
321 DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "AnimatedImageVisual::LoadComplete(textureId:%d) start\n", textureInformation.textureId);
324 MakeFrameReady(loadSuccess, mTextureManager.GetTextureSet(textureInformation.textureId), textureInformation.interval);
328 // The frames of a single animated image can not be loaded parallelly.
329 // Therefore, a frame is now loading, other orders are waiting.
330 // And, after the frame is loaded, requests load of next order.
331 if(!mLoadWaitingQueue.empty())
333 uint32_t loadingIndex = mLoadWaitingQueue.front();
334 mLoadWaitingQueue.erase(mLoadWaitingQueue.begin());
335 RequestFrameLoading(loadingIndex, false);
337 else if(mQueue.Count() == 1u && textureInformation.frameCount > SINGLE_IMAGE_COUNT)
339 // There is only an image in queue and no waiting queue.
340 // Request to load batch once again.
341 uint32_t batchFrameIndex = 0u;
342 if(!mLoadWaitingQueue.empty())
344 batchFrameIndex = (mLoadWaitingQueue.back() + 1) % mFrameCount;
348 batchFrameIndex = (mQueue.Back().mFrameNumber + 1) % mFrameCount;
350 LoadBatch(batchFrameIndex);
357 } //namespace Internal
358 } //namespace Toolkit