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()); \
56 static constexpr uint32_t SINGLE_IMAGE_COUNT = 1u;
57 static constexpr uint32_t FIRST_FRAME_INDEX = 0u;
60 RollingAnimatedImageCache::RollingAnimatedImageCache(TextureManager& textureManager,
62 Dali::FittingMode::Type fittingMode,
63 Dali::SamplingMode::Type samplingMode,
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, size, fittingMode, samplingMode, maskingData, observer, batchSize, 0u, preMultiplyOnLoad),
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)
84 mTextureIds.resize(mFrameCount);
85 mIntervals.assign(mFrameCount, 0);
88 RollingAnimatedImageCache::~RollingAnimatedImageCache()
91 mAnimatedImageLoading.Reset();
94 TextureSet RollingAnimatedImageCache::Frame(uint32_t frameIndex)
96 bool popExist = false;
97 while(!mQueue.IsEmpty() && mQueue.Front().mFrameNumber != frameIndex)
103 TextureSet textureSet;
104 uint32_t batchFrameIndex = frameIndex;
105 // If we need to load new frame that are not stored in queue.
106 // Load the frame synchronously.
107 bool synchronouslyLoaded = false;
108 if(mIsSynchronousLoading && mQueue.IsEmpty())
110 auto preMultiplyOnLoading = mPreMultiplyOnLoad ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD
111 : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
113 textureSet = RequestFrameLoading(frameIndex, true, preMultiplyOnLoading);
114 batchFrameIndex = (frameIndex + 1) % mFrameCount;
115 uint32_t interval = 0u;
118 synchronouslyLoaded = true;
119 interval = mAnimatedImageLoading.GetFrameInterval(mQueue.Back().mFrameNumber);
121 MakeFrameReady(synchronouslyLoaded, textureSet, interval, preMultiplyOnLoading == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD);
124 if(popExist || mQueue.IsEmpty() || synchronouslyLoaded)
126 // If the frame of frameIndex was already loaded, load batch from the last frame of queue
127 if(!mQueue.IsEmpty())
129 if(!mLoadWaitingQueue.empty())
131 batchFrameIndex = (mLoadWaitingQueue.back() + 1) % mFrameCount;
135 batchFrameIndex = (mQueue.Back().mFrameNumber + 1) % mFrameCount;
140 // If the request is for the first frame or a jumped frame(JUMP_TO) remove current waiting queue.
141 mLoadWaitingQueue.clear();
142 // If the queue is empty, and the frame of frameIndex is not loaded synchronously. load batch from the frame of frameIndex
145 batchFrameIndex = frameIndex;
148 LoadBatch(batchFrameIndex);
151 if(!textureSet && mLoadState != TextureManager::LoadState::LOAD_FAILED && IsFrontReady() == true)
153 textureSet = GetFrontTextureSet();
159 TextureSet RollingAnimatedImageCache::FirstFrame()
161 TextureSet textureSet = Frame(FIRST_FRAME_INDEX);
165 uint32_t RollingAnimatedImageCache::GetFrameInterval(uint32_t frameIndex) const
167 if(frameIndex >= mIntervals.size())
171 return mIntervals[frameIndex];
174 int32_t RollingAnimatedImageCache::GetCurrentFrameIndex() const
180 return mQueue.Front().mFrameNumber;
183 int32_t RollingAnimatedImageCache::GetTotalFrameCount() const
188 bool RollingAnimatedImageCache::IsFrontReady() const
190 return (!mQueue.IsEmpty() && mQueue.Front().mReady);
193 TextureSet RollingAnimatedImageCache::RequestFrameLoading(uint32_t frameIndex)
195 auto preMultiplyOnLoading = mPreMultiplyOnLoad ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD
196 : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
198 return RequestFrameLoading(frameIndex, false, preMultiplyOnLoading);
201 TextureSet RollingAnimatedImageCache::RequestFrameLoading(uint32_t frameIndex, bool synchronousLoading, TextureManager::MultiplyOnLoad& preMultiplyOnLoading)
203 ImageFrame imageFrame;
204 imageFrame.mFrameNumber = frameIndex;
205 imageFrame.mReady = false;
207 mQueue.PushBack(imageFrame);
209 mLoadState = TextureManager::LoadState::LOADING;
211 TextureManager::TextureId loadTextureId = TextureManager::INVALID_TEXTURE_ID;
212 TextureSet textureSet = mTextureManager.LoadAnimatedImageTexture(mImageUrl,
213 mAnimatedImageLoading,
222 preMultiplyOnLoading);
225 Sampler sampler = Sampler::New();
226 sampler.SetWrapMode(mWrapModeU, mWrapModeV);
227 textureSet.SetSampler(0u, sampler);
230 mTextureIds[frameIndex] = loadTextureId;
235 void RollingAnimatedImageCache::LoadBatch(uint32_t frameIndex)
237 // Try and load up to mBatchSize images, until the cache is filled.
238 // Once the cache is filled, as frames progress, the old frame is
239 // removed, and another frame is loaded
240 uint32_t minimumSize = std::min(mCacheSize, mFrameCount);
241 for(uint32_t i = 0; i < mBatchSize && (mQueue.Count() + mLoadWaitingQueue.size()) < minimumSize; ++i)
243 if(mLoadState != TextureManager::LoadState::LOADING)
245 RequestFrameLoading(frameIndex);
249 mLoadWaitingQueue.push_back(frameIndex);
253 frameIndex %= mFrameCount;
259 void RollingAnimatedImageCache::SetImageFrameReady(TextureManager::TextureId textureId)
261 for(std::size_t i = 0; i < mQueue.Count(); ++i)
263 if(GetCachedTextureId(i) == textureId)
265 mQueue[i].mReady = true;
271 TextureSet RollingAnimatedImageCache::GetFrontTextureSet() const
273 DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "RollingAnimatedImageCache::GetFrontTextureSet() FrameNumber:%d\n", mQueue[0].mFrameNumber);
275 TextureManager::TextureId textureId = GetCachedTextureId(0);
276 TextureSet textureSet = mTextureManager.GetTextureSet(textureId);
279 Sampler sampler = Sampler::New();
280 sampler.SetWrapMode(mWrapModeU, mWrapModeV);
281 textureSet.SetSampler(0u, sampler);
286 TextureManager::TextureId RollingAnimatedImageCache::GetCachedTextureId(int index) const
288 return mTextureIds[mQueue[index].mFrameNumber];
291 void RollingAnimatedImageCache::PopFrontCache()
293 ImageFrame imageFrame = mQueue.PopFront();
294 mTextureManager.Remove(mTextureIds[imageFrame.mFrameNumber], this);
295 mTextureIds[imageFrame.mFrameNumber] = TextureManager::INVALID_TEXTURE_ID;
297 if(mMaskingData && mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID)
301 mMaskingData->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
306 void RollingAnimatedImageCache::ClearCache()
308 while(mTextureManagerAlive && !mQueue.IsEmpty())
312 mLoadWaitingQueue.clear();
313 mLoadState = TextureManager::LoadState::NOT_STARTED;
316 void RollingAnimatedImageCache::MakeFrameReady(bool loadSuccess, TextureSet textureSet, uint32_t interval, bool preMultiplied)
320 mLoadState = TextureManager::LoadState::LOAD_FAILED;
321 // preMultiplied should be false because broken image don't premultiply alpha on load
322 mObserver.FrameReady(TextureSet(), 0, false);
326 mLoadState = TextureManager::LoadState::LOAD_FINISHED;
328 // Reset size of Queue according to the real frame count.
329 if(mFrameCount != mAnimatedImageLoading.GetImageCount())
331 mFrameCount = mAnimatedImageLoading.GetImageCount();
332 mTextureIds.resize(mFrameCount);
333 mIntervals.assign(mFrameCount, 0u);
336 bool frontFrameReady = IsFrontReady();
337 // Because only one frame is on loading and the others are in mLoadWaitingQueue,
338 // mQueue.Back() is always the frame currently loaded.
339 mQueue.Back().mReady = true;
340 mIntervals[mQueue.Back().mFrameNumber] = interval;
341 // Check whether currently loaded frame is front of queue or not.
342 // If it is, notify frame ready to observer.
343 if(frontFrameReady == false && IsFrontReady())
345 mObserver.FrameReady(textureSet, interval, preMultiplied);
350 void RollingAnimatedImageCache::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
352 DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "AnimatedImageVisual::LoadComplete(textureId:%d) start\n", textureInformation.textureId);
355 if(textureInformation.textureSet)
357 Sampler sampler = Sampler::New();
358 sampler.SetWrapMode(mWrapModeU, mWrapModeV);
359 textureInformation.textureSet.SetSampler(0u, sampler);
362 MakeFrameReady(loadSuccess, textureInformation.textureSet, textureInformation.interval, textureInformation.preMultiplied);
366 // The frames of a single animated image can not be loaded parallelly.
367 // Therefore, a frame is now loading, other orders are waiting.
368 // And, after the frame is loaded, requests load of next order.
369 if(!mLoadWaitingQueue.empty())
371 uint32_t loadingIndex = mLoadWaitingQueue.front();
372 mLoadWaitingQueue.erase(mLoadWaitingQueue.begin());
373 RequestFrameLoading(loadingIndex);
375 else if(mQueue.Count() == 1u && textureInformation.frameCount > SINGLE_IMAGE_COUNT)
377 // There is only an image in queue and no waiting queue.
378 // Request to load batch once again.
379 uint32_t batchFrameIndex = 0u;
380 if(!mLoadWaitingQueue.empty())
382 batchFrameIndex = (mLoadWaitingQueue.back() + 1) % mFrameCount;
386 batchFrameIndex = (mQueue.Back().mFrameNumber + 1) % mFrameCount;
388 LoadBatch(batchFrameIndex);
395 } //namespace Internal
396 } //namespace Toolkit