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");
31 if(gAnimImgLogFilter->IsEnabledFor(Debug::Concise)) \
33 std::ostringstream oss; \
34 oss << "Size:" << mQueue.Count() << " [ "; \
35 for(std::size_t _i = 0; _i < mQueue.Count(); ++_i) \
37 oss << _i << "={ frm#: " << mQueue[_i].mFrameNumber << " tex: " << mTextureIds[mQueue[_i].mFrameNumber] << "}, "; \
39 oss << " ]" << std::endl; \
40 DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "%s", oss.str().c_str()); \
47 static constexpr bool ENABLE_ORIENTATION_CORRECTION(true);
59 static constexpr uint32_t SINGLE_IMAGE_COUNT = 1u;
60 static constexpr uint32_t FIRST_FRAME_INDEX = 0u;
63 RollingAnimatedImageCache::RollingAnimatedImageCache(TextureManager& textureManager,
64 AnimatedImageLoading& animatedImageLoading,
65 TextureManager::MaskingDataPointer& maskingData,
66 ImageCache::FrameReadyObserver& observer,
69 const Dali::WrapMode::Type& wrapModeU,
70 const Dali::WrapMode::Type& wrapModeV,
71 bool isSynchronousLoading,
72 bool preMultiplyOnLoad)
73 : ImageCache(textureManager, maskingData, observer, batchSize, 0u),
74 mImageUrl(animatedImageLoading.GetUrl()),
75 mAnimatedImageLoading(animatedImageLoading),
76 mFrameCount(SINGLE_IMAGE_COUNT),
77 mFrameIndex(FIRST_FRAME_INDEX),
78 mCacheSize(cacheSize),
80 mWrapModeU(wrapModeU),
81 mWrapModeV(wrapModeV),
82 mIsSynchronousLoading(isSynchronousLoading),
83 mPreMultiplyOnLoad(preMultiplyOnLoad)
85 mTextureIds.resize(mFrameCount);
86 mIntervals.assign(mFrameCount, 0);
89 RollingAnimatedImageCache::~RollingAnimatedImageCache()
92 mAnimatedImageLoading.Reset();
95 TextureSet RollingAnimatedImageCache::Frame(uint32_t frameIndex)
97 bool popExist = false;
98 while(!mQueue.IsEmpty() && mQueue.Front().mFrameNumber != frameIndex)
104 TextureSet textureSet;
105 uint32_t batchFrameIndex = frameIndex;
106 // If we need to load new frame that are not stored in queue.
107 // Load the frame synchronously.
108 bool synchronouslyLoaded = false;
109 if(mIsSynchronousLoading && mQueue.IsEmpty())
111 textureSet = RequestFrameLoading(frameIndex, true);
112 batchFrameIndex = (frameIndex + 1) % mFrameCount;
113 uint32_t interval = 0u;
116 synchronouslyLoaded = true;
117 interval = mAnimatedImageLoading.GetFrameInterval(mQueue.Back().mFrameNumber);
119 MakeFrameReady(synchronouslyLoaded, textureSet, interval);
122 if(popExist || mQueue.IsEmpty() || synchronouslyLoaded)
124 // If the frame of frameIndex was already loaded, load batch from the last frame of queue
125 if(!mQueue.IsEmpty())
127 if(!mLoadWaitingQueue.empty())
129 batchFrameIndex = (mLoadWaitingQueue.back() + 1) % mFrameCount;
133 batchFrameIndex = (mQueue.Back().mFrameNumber + 1) % mFrameCount;
138 // If the request is for the first frame or a jumped frame(JUMP_TO) remove current waiting queue.
139 mLoadWaitingQueue.clear();
140 // If the queue is empty, and the frame of frameIndex is not loaded synchronously. load batch from the frame of frameIndex
143 batchFrameIndex = frameIndex;
146 LoadBatch(batchFrameIndex);
149 if(!textureSet && mLoadState != TextureManager::LoadState::LOAD_FAILED && IsFrontReady() == true)
151 textureSet = GetFrontTextureSet();
157 TextureSet RollingAnimatedImageCache::FirstFrame()
159 TextureSet textureSet = Frame(FIRST_FRAME_INDEX);
163 uint32_t RollingAnimatedImageCache::GetFrameInterval(uint32_t frameIndex) const
165 if(frameIndex >= mIntervals.size())
169 return mIntervals[frameIndex];
172 int32_t RollingAnimatedImageCache::GetCurrentFrameIndex() const
178 return mQueue.Front().mFrameNumber;
181 int32_t RollingAnimatedImageCache::GetTotalFrameCount() const
186 bool RollingAnimatedImageCache::IsFrontReady() const
188 return (!mQueue.IsEmpty() && mQueue.Front().mReady);
191 TextureSet RollingAnimatedImageCache::RequestFrameLoading(uint32_t frameIndex, bool synchronousLoading)
193 ImageFrame imageFrame;
194 imageFrame.mFrameNumber = frameIndex;
195 imageFrame.mReady = false;
197 mQueue.PushBack(imageFrame);
199 mLoadState = TextureManager::LoadState::LOADING;
201 auto preMultiplyOnLoading = mPreMultiplyOnLoad ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD
202 : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
204 TextureManager::TextureId loadTextureId = TextureManager::INVALID_TEXTURE_ID;
205 TextureSet textureSet = mTextureManager.LoadAnimatedImageTexture(mImageUrl,
206 mAnimatedImageLoading,
210 SamplingMode::BOX_THEN_LINEAR,
215 preMultiplyOnLoading);
217 mTextureIds[frameIndex] = loadTextureId;
222 void RollingAnimatedImageCache::LoadBatch(uint32_t frameIndex)
224 // Try and load up to mBatchSize images, until the cache is filled.
225 // Once the cache is filled, as frames progress, the old frame is
226 // removed, and another frame is loaded
227 uint32_t minimumSize = std::min(mCacheSize, mFrameCount);
228 for(uint32_t i = 0; i < mBatchSize && (mQueue.Count() + mLoadWaitingQueue.size()) < minimumSize; ++i)
230 if(mLoadState != TextureManager::LoadState::LOADING)
232 RequestFrameLoading(frameIndex, false);
236 mLoadWaitingQueue.push_back(frameIndex);
240 frameIndex %= mFrameCount;
246 void RollingAnimatedImageCache::SetImageFrameReady(TextureManager::TextureId textureId)
248 for(std::size_t i = 0; i < mQueue.Count(); ++i)
250 if(GetCachedTextureId(i) == textureId)
252 mQueue[i].mReady = true;
258 TextureSet RollingAnimatedImageCache::GetFrontTextureSet() const
260 DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "RollingAnimatedImageCache::GetFrontTextureSet() FrameNumber:%d\n", mQueue[0].mFrameNumber);
262 TextureManager::TextureId textureId = GetCachedTextureId(0);
263 return mTextureManager.GetTextureSet(textureId);
266 TextureManager::TextureId RollingAnimatedImageCache::GetCachedTextureId(int index) const
268 return mTextureIds[mQueue[index].mFrameNumber];
271 void RollingAnimatedImageCache::PopFrontCache()
273 ImageFrame imageFrame = mQueue.PopFront();
274 mTextureManager.Remove(mTextureIds[imageFrame.mFrameNumber], this);
275 mTextureIds[imageFrame.mFrameNumber] = TextureManager::INVALID_TEXTURE_ID;
277 if(mMaskingData && mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID)
279 mTextureManager.Remove(mMaskingData->mAlphaMaskId, this);
282 mMaskingData->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
287 void RollingAnimatedImageCache::ClearCache()
289 while(mTextureManagerAlive && !mQueue.IsEmpty())
293 mLoadWaitingQueue.clear();
294 mLoadState = TextureManager::LoadState::NOT_STARTED;
297 void RollingAnimatedImageCache::MakeFrameReady(bool loadSuccess, TextureSet textureSet, uint32_t interval)
301 mLoadState = TextureManager::LoadState::LOAD_FAILED;
302 mObserver.FrameReady(TextureSet(), 0);
306 mLoadState = TextureManager::LoadState::LOAD_FINISHED;
308 // Reset size of Queue according to the real frame count.
309 if(mFrameCount != mAnimatedImageLoading.GetImageCount())
311 mFrameCount = mAnimatedImageLoading.GetImageCount();
312 mTextureIds.resize(mFrameCount);
313 mIntervals.assign(mFrameCount, 0u);
316 bool frontFrameReady = IsFrontReady();
317 // Because only one frame is on loading and the others are in mLoadWaitingQueue,
318 // mQueue.Back() is always the frame currently loaded.
319 mQueue.Back().mReady = true;
320 mIntervals[mQueue.Back().mFrameNumber] = interval;
321 // Check whether currently loaded frame is front of queue or not.
322 // If it is, notify frame ready to observer.
323 if(frontFrameReady == false && IsFrontReady())
325 mObserver.FrameReady(textureSet, interval);
330 void RollingAnimatedImageCache::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
332 DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "AnimatedImageVisual::LoadComplete(textureId:%d) start\n", textureInformation.textureId);
335 TextureSet textureSet = mTextureManager.GetTextureSet(textureInformation.textureId);
338 Sampler sampler = Sampler::New();
339 sampler.SetWrapMode(mWrapModeU, mWrapModeV);
340 textureSet.SetSampler(0u, sampler);
343 MakeFrameReady(loadSuccess, textureSet, textureInformation.interval);
347 // The frames of a single animated image can not be loaded parallelly.
348 // Therefore, a frame is now loading, other orders are waiting.
349 // And, after the frame is loaded, requests load of next order.
350 if(!mLoadWaitingQueue.empty())
352 uint32_t loadingIndex = mLoadWaitingQueue.front();
353 mLoadWaitingQueue.erase(mLoadWaitingQueue.begin());
354 RequestFrameLoading(loadingIndex, false);
356 else if(mQueue.Count() == 1u && textureInformation.frameCount > SINGLE_IMAGE_COUNT)
358 // There is only an image in queue and no waiting queue.
359 // Request to load batch once again.
360 uint32_t batchFrameIndex = 0u;
361 if(!mLoadWaitingQueue.empty())
363 batchFrameIndex = (mLoadWaitingQueue.back() + 1) % mFrameCount;
367 batchFrameIndex = (mQueue.Back().mFrameNumber + 1) % mFrameCount;
369 LoadBatch(batchFrameIndex);
376 } //namespace Internal
377 } //namespace Toolkit