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 bool preMultiplyOnLoad)
70 : ImageCache(textureManager, maskingData, observer, batchSize, 0u),
71 mImageUrl(animatedImageLoading.GetUrl()),
72 mAnimatedImageLoading(animatedImageLoading),
73 mFrameCount(SINGLE_IMAGE_COUNT),
74 mFrameIndex(FIRST_FRAME_INDEX),
75 mCacheSize(cacheSize),
77 mIsSynchronousLoading(isSynchronousLoading),
78 mPreMultiplyOnLoad(preMultiplyOnLoad)
80 mTextureIds.resize(mFrameCount);
81 mIntervals.assign(mFrameCount, 0);
84 RollingAnimatedImageCache::~RollingAnimatedImageCache()
87 mAnimatedImageLoading.Reset();
90 TextureSet RollingAnimatedImageCache::Frame(uint32_t frameIndex)
92 bool popExist = false;
93 while(!mQueue.IsEmpty() && mQueue.Front().mFrameNumber != frameIndex)
99 TextureSet textureSet;
100 uint32_t batchFrameIndex = frameIndex;
101 // If we need to load new frame that are not stored in queue.
102 // Load the frame synchronously.
103 bool synchronouslyLoaded = false;
104 if(mIsSynchronousLoading && mQueue.IsEmpty())
106 textureSet = RequestFrameLoading(frameIndex, true);
107 batchFrameIndex = (frameIndex + 1) % mFrameCount;
108 uint32_t interval = 0u;
111 synchronouslyLoaded = true;
112 interval = mAnimatedImageLoading.GetFrameInterval(mQueue.Back().mFrameNumber);
114 MakeFrameReady(synchronouslyLoaded, textureSet, interval);
117 if(popExist || mQueue.IsEmpty() || synchronouslyLoaded)
119 // If the frame of frameIndex was already loaded, load batch from the last frame of queue
120 if(!mQueue.IsEmpty())
122 if(!mLoadWaitingQueue.empty())
124 batchFrameIndex = (mLoadWaitingQueue.back() + 1) % mFrameCount;
128 batchFrameIndex = (mQueue.Back().mFrameNumber + 1) % mFrameCount;
133 // If the request is for the first frame or a jumped frame(JUMP_TO) remove current waiting queue.
134 mLoadWaitingQueue.clear();
135 // If the queue is empty, and the frame of frameIndex is not loaded synchronously. load batch from the frame of frameIndex
138 batchFrameIndex = frameIndex;
141 LoadBatch(batchFrameIndex);
144 if(!textureSet && mLoadState != TextureManager::LoadState::LOAD_FAILED && IsFrontReady() == true)
146 textureSet = GetFrontTextureSet();
152 TextureSet RollingAnimatedImageCache::FirstFrame()
154 TextureSet textureSet = Frame(FIRST_FRAME_INDEX);
158 uint32_t RollingAnimatedImageCache::GetFrameInterval(uint32_t frameIndex) const
160 if(frameIndex >= mIntervals.size())
164 return mIntervals[frameIndex];
167 int32_t RollingAnimatedImageCache::GetCurrentFrameIndex() const
173 return mQueue.Front().mFrameNumber;
176 int32_t RollingAnimatedImageCache::GetTotalFrameCount() const
181 bool RollingAnimatedImageCache::IsFrontReady() const
183 return (!mQueue.IsEmpty() && mQueue.Front().mReady);
186 TextureSet RollingAnimatedImageCache::RequestFrameLoading(uint32_t frameIndex, bool synchronousLoading)
188 ImageFrame imageFrame;
189 imageFrame.mFrameNumber = frameIndex;
190 imageFrame.mReady = false;
192 mQueue.PushBack(imageFrame);
194 mLoadState = TextureManager::LoadState::LOADING;
196 auto preMultiplyOnLoading = mPreMultiplyOnLoad ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD
197 : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
199 TextureManager::TextureId loadTextureId = TextureManager::INVALID_TEXTURE_ID;
200 TextureSet textureSet = mTextureManager.LoadAnimatedImageTexture(mImageUrl,
201 mAnimatedImageLoading,
205 SamplingMode::BOX_THEN_LINEAR,
206 Dali::WrapMode::Type::DEFAULT,
207 Dali::WrapMode::Type::DEFAULT,
210 preMultiplyOnLoading);
212 mTextureIds[frameIndex] = loadTextureId;
217 void RollingAnimatedImageCache::LoadBatch(uint32_t frameIndex)
219 // Try and load up to mBatchSize images, until the cache is filled.
220 // Once the cache is filled, as frames progress, the old frame is
221 // removed, and another frame is loaded
222 uint32_t minimumSize = std::min(mCacheSize, mFrameCount);
223 for(uint32_t i = 0; i < mBatchSize && (mQueue.Count() + mLoadWaitingQueue.size()) < minimumSize; ++i)
225 if(mLoadState != TextureManager::LoadState::LOADING)
227 RequestFrameLoading(frameIndex, false);
231 mLoadWaitingQueue.push_back(frameIndex);
235 frameIndex %= mFrameCount;
241 void RollingAnimatedImageCache::SetImageFrameReady(TextureManager::TextureId textureId)
243 for(std::size_t i = 0; i < mQueue.Count(); ++i)
245 if(GetCachedTextureId(i) == textureId)
247 mQueue[i].mReady = true;
253 TextureSet RollingAnimatedImageCache::GetFrontTextureSet() const
255 DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "RollingAnimatedImageCache::GetFrontTextureSet() FrameNumber:%d\n", mQueue[0].mFrameNumber);
257 TextureManager::TextureId textureId = GetCachedTextureId(0);
258 return mTextureManager.GetTextureSet(textureId);
261 TextureManager::TextureId RollingAnimatedImageCache::GetCachedTextureId(int index) const
263 return mTextureIds[mQueue[index].mFrameNumber];
266 void RollingAnimatedImageCache::PopFrontCache()
268 ImageFrame imageFrame = mQueue.PopFront();
269 mTextureManager.Remove(mTextureIds[imageFrame.mFrameNumber], this);
270 mTextureIds[imageFrame.mFrameNumber] = TextureManager::INVALID_TEXTURE_ID;
272 if(mMaskingData && mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID)
274 mTextureManager.Remove(mMaskingData->mAlphaMaskId, this);
277 mMaskingData->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
282 void RollingAnimatedImageCache::ClearCache()
284 while(mTextureManagerAlive && !mQueue.IsEmpty())
288 mLoadWaitingQueue.clear();
289 mLoadState = TextureManager::LoadState::NOT_STARTED;
292 void RollingAnimatedImageCache::MakeFrameReady(bool loadSuccess, TextureSet textureSet, uint32_t interval)
296 mLoadState = TextureManager::LoadState::LOAD_FAILED;
297 mObserver.FrameReady(TextureSet(), 0);
301 mLoadState = TextureManager::LoadState::LOAD_FINISHED;
303 // Reset size of Queue according to the real frame count.
304 if(mFrameCount != mAnimatedImageLoading.GetImageCount())
306 mFrameCount = mAnimatedImageLoading.GetImageCount();
307 mTextureIds.resize(mFrameCount);
308 mIntervals.assign(mFrameCount, 0u);
311 bool frontFrameReady = IsFrontReady();
312 // Because only one frame is on loading and the others are in mLoadWaitingQueue,
313 // mQueue.Back() is always the frame currently loaded.
314 mQueue.Back().mReady = true;
315 mIntervals[mQueue.Back().mFrameNumber] = interval;
316 // Check whether currently loaded frame is front of queue or not.
317 // If it is, notify frame ready to observer.
318 if(frontFrameReady == false && IsFrontReady())
320 mObserver.FrameReady(textureSet, interval);
325 void RollingAnimatedImageCache::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
327 DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "AnimatedImageVisual::LoadComplete(textureId:%d) start\n", textureInformation.textureId);
330 MakeFrameReady(loadSuccess, mTextureManager.GetTextureSet(textureInformation.textureId), textureInformation.interval);
334 // The frames of a single animated image can not be loaded parallelly.
335 // Therefore, a frame is now loading, other orders are waiting.
336 // And, after the frame is loaded, requests load of next order.
337 if(!mLoadWaitingQueue.empty())
339 uint32_t loadingIndex = mLoadWaitingQueue.front();
340 mLoadWaitingQueue.erase(mLoadWaitingQueue.begin());
341 RequestFrameLoading(loadingIndex, false);
343 else if(mQueue.Count() == 1u && textureInformation.frameCount > SINGLE_IMAGE_COUNT)
345 // There is only an image in queue and no waiting queue.
346 // Request to load batch once again.
347 uint32_t batchFrameIndex = 0u;
348 if(!mLoadWaitingQueue.empty())
350 batchFrameIndex = (mLoadWaitingQueue.back() + 1) % mFrameCount;
354 batchFrameIndex = (mQueue.Back().mFrameNumber + 1) % mFrameCount;
356 LoadBatch(batchFrameIndex);
363 } //namespace Internal
364 } //namespace Toolkit