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,
65 Dali::FittingMode::Type fittingMode,
66 Dali::SamplingMode::Type samplingMode,
67 AnimatedImageLoading& animatedImageLoading,
68 TextureManager::MaskingDataPointer& maskingData,
69 ImageCache::FrameReadyObserver& observer,
72 const Dali::WrapMode::Type& wrapModeU,
73 const Dali::WrapMode::Type& wrapModeV,
74 bool isSynchronousLoading,
75 bool preMultiplyOnLoad)
76 : ImageCache(textureManager, size, fittingMode, samplingMode, maskingData, observer, batchSize, 0u, preMultiplyOnLoad),
77 mImageUrl(animatedImageLoading.GetUrl()),
78 mAnimatedImageLoading(animatedImageLoading),
79 mFrameCount(SINGLE_IMAGE_COUNT),
80 mFrameIndex(FIRST_FRAME_INDEX),
81 mCacheSize(cacheSize),
83 mWrapModeU(wrapModeU),
84 mWrapModeV(wrapModeV),
85 mIsSynchronousLoading(isSynchronousLoading)
87 mTextureIds.resize(mFrameCount);
88 mIntervals.assign(mFrameCount, 0);
91 RollingAnimatedImageCache::~RollingAnimatedImageCache()
94 mAnimatedImageLoading.Reset();
97 TextureSet RollingAnimatedImageCache::Frame(uint32_t frameIndex)
99 bool popExist = false;
100 while(!mQueue.IsEmpty() && mQueue.Front().mFrameNumber != frameIndex)
106 TextureSet textureSet;
107 uint32_t batchFrameIndex = frameIndex;
108 // If we need to load new frame that are not stored in queue.
109 // Load the frame synchronously.
110 bool synchronouslyLoaded = false;
111 if(mIsSynchronousLoading && mQueue.IsEmpty())
113 auto preMultiplyOnLoading = mPreMultiplyOnLoad ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD
114 : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
116 textureSet = RequestFrameLoading(frameIndex, true, preMultiplyOnLoading);
117 batchFrameIndex = (frameIndex + 1) % mFrameCount;
118 uint32_t interval = 0u;
121 synchronouslyLoaded = true;
122 interval = mAnimatedImageLoading.GetFrameInterval(mQueue.Back().mFrameNumber);
124 MakeFrameReady(synchronouslyLoaded, textureSet, interval, preMultiplyOnLoading == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD);
127 if(popExist || mQueue.IsEmpty() || synchronouslyLoaded)
129 // If the frame of frameIndex was already loaded, load batch from the last frame of queue
130 if(!mQueue.IsEmpty())
132 if(!mLoadWaitingQueue.empty())
134 batchFrameIndex = (mLoadWaitingQueue.back() + 1) % mFrameCount;
138 batchFrameIndex = (mQueue.Back().mFrameNumber + 1) % mFrameCount;
143 // If the request is for the first frame or a jumped frame(JUMP_TO) remove current waiting queue.
144 mLoadWaitingQueue.clear();
145 // If the queue is empty, and the frame of frameIndex is not loaded synchronously. load batch from the frame of frameIndex
148 batchFrameIndex = frameIndex;
151 LoadBatch(batchFrameIndex);
154 if(!textureSet && mLoadState != TextureManager::LoadState::LOAD_FAILED && IsFrontReady() == true)
156 textureSet = GetFrontTextureSet();
162 TextureSet RollingAnimatedImageCache::FirstFrame()
164 TextureSet textureSet = Frame(FIRST_FRAME_INDEX);
168 uint32_t RollingAnimatedImageCache::GetFrameInterval(uint32_t frameIndex) const
170 if(frameIndex >= mIntervals.size())
174 return mIntervals[frameIndex];
177 int32_t RollingAnimatedImageCache::GetCurrentFrameIndex() const
183 return mQueue.Front().mFrameNumber;
186 int32_t RollingAnimatedImageCache::GetTotalFrameCount() const
191 bool RollingAnimatedImageCache::IsFrontReady() const
193 return (!mQueue.IsEmpty() && mQueue.Front().mReady);
196 TextureSet RollingAnimatedImageCache::RequestFrameLoading(uint32_t frameIndex)
198 auto preMultiplyOnLoading = mPreMultiplyOnLoad ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD
199 : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
201 return RequestFrameLoading(frameIndex, false, preMultiplyOnLoading);
204 TextureSet RollingAnimatedImageCache::RequestFrameLoading(uint32_t frameIndex, bool synchronousLoading, TextureManager::MultiplyOnLoad& preMultiplyOnLoading)
206 ImageFrame imageFrame;
207 imageFrame.mFrameNumber = frameIndex;
208 imageFrame.mReady = false;
210 mQueue.PushBack(imageFrame);
212 mLoadState = TextureManager::LoadState::LOADING;
214 TextureManager::TextureId loadTextureId = TextureManager::INVALID_TEXTURE_ID;
215 TextureSet textureSet = mTextureManager.LoadAnimatedImageTexture(mImageUrl,
216 mAnimatedImageLoading,
225 preMultiplyOnLoading);
228 Sampler sampler = Sampler::New();
229 sampler.SetWrapMode(mWrapModeU, mWrapModeV);
230 textureSet.SetSampler(0u, sampler);
233 mTextureIds[frameIndex] = loadTextureId;
238 void RollingAnimatedImageCache::LoadBatch(uint32_t frameIndex)
240 // Try and load up to mBatchSize images, until the cache is filled.
241 // Once the cache is filled, as frames progress, the old frame is
242 // removed, and another frame is loaded
243 uint32_t minimumSize = std::min(mCacheSize, mFrameCount);
244 for(uint32_t i = 0; i < mBatchSize && (mQueue.Count() + mLoadWaitingQueue.size()) < minimumSize; ++i)
246 if(mLoadState != TextureManager::LoadState::LOADING)
248 RequestFrameLoading(frameIndex);
252 mLoadWaitingQueue.push_back(frameIndex);
256 frameIndex %= mFrameCount;
262 void RollingAnimatedImageCache::SetImageFrameReady(TextureManager::TextureId textureId)
264 for(std::size_t i = 0; i < mQueue.Count(); ++i)
266 if(GetCachedTextureId(i) == textureId)
268 mQueue[i].mReady = true;
274 TextureSet RollingAnimatedImageCache::GetFrontTextureSet() const
276 DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "RollingAnimatedImageCache::GetFrontTextureSet() FrameNumber:%d\n", mQueue[0].mFrameNumber);
278 TextureManager::TextureId textureId = GetCachedTextureId(0);
279 TextureSet textureSet = mTextureManager.GetTextureSet(textureId);
282 Sampler sampler = Sampler::New();
283 sampler.SetWrapMode(mWrapModeU, mWrapModeV);
284 textureSet.SetSampler(0u, sampler);
289 TextureManager::TextureId RollingAnimatedImageCache::GetCachedTextureId(int index) const
291 return mTextureIds[mQueue[index].mFrameNumber];
294 void RollingAnimatedImageCache::PopFrontCache()
296 ImageFrame imageFrame = mQueue.PopFront();
297 mTextureManager.Remove(mTextureIds[imageFrame.mFrameNumber], this);
298 mTextureIds[imageFrame.mFrameNumber] = TextureManager::INVALID_TEXTURE_ID;
300 if(mMaskingData && mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID)
304 mMaskingData->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
309 void RollingAnimatedImageCache::ClearCache()
311 while(mTextureManagerAlive && !mQueue.IsEmpty())
315 mLoadWaitingQueue.clear();
316 mLoadState = TextureManager::LoadState::NOT_STARTED;
319 void RollingAnimatedImageCache::MakeFrameReady(bool loadSuccess, TextureSet textureSet, uint32_t interval, bool preMultiplied)
323 mLoadState = TextureManager::LoadState::LOAD_FAILED;
324 // preMultiplied should be false because broken image don't premultiply alpha on load
325 mObserver.FrameReady(TextureSet(), 0, false);
329 mLoadState = TextureManager::LoadState::LOAD_FINISHED;
331 // Reset size of Queue according to the real frame count.
332 if(mFrameCount != mAnimatedImageLoading.GetImageCount())
334 mFrameCount = mAnimatedImageLoading.GetImageCount();
335 mTextureIds.resize(mFrameCount);
336 mIntervals.assign(mFrameCount, 0u);
339 bool frontFrameReady = IsFrontReady();
340 // Because only one frame is on loading and the others are in mLoadWaitingQueue,
341 // mQueue.Back() is always the frame currently loaded.
342 mQueue.Back().mReady = true;
343 mIntervals[mQueue.Back().mFrameNumber] = interval;
344 // Check whether currently loaded frame is front of queue or not.
345 // If it is, notify frame ready to observer.
346 if(frontFrameReady == false && IsFrontReady())
348 mObserver.FrameReady(textureSet, interval, preMultiplied);
353 void RollingAnimatedImageCache::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
355 DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "AnimatedImageVisual::LoadComplete(textureId:%d) start\n", textureInformation.textureId);
358 if(textureInformation.textureSet)
360 Sampler sampler = Sampler::New();
361 sampler.SetWrapMode(mWrapModeU, mWrapModeV);
362 textureInformation.textureSet.SetSampler(0u, sampler);
365 MakeFrameReady(loadSuccess, textureInformation.textureSet, textureInformation.interval, textureInformation.preMultiplied);
369 // The frames of a single animated image can not be loaded parallelly.
370 // Therefore, a frame is now loading, other orders are waiting.
371 // And, after the frame is loaded, requests load of next order.
372 if(!mLoadWaitingQueue.empty())
374 uint32_t loadingIndex = mLoadWaitingQueue.front();
375 mLoadWaitingQueue.erase(mLoadWaitingQueue.begin());
376 RequestFrameLoading(loadingIndex);
378 else if(mQueue.Count() == 1u && textureInformation.frameCount > SINGLE_IMAGE_COUNT)
380 // There is only an image in queue and no waiting queue.
381 // Request to load batch once again.
382 uint32_t batchFrameIndex = 0u;
383 if(!mLoadWaitingQueue.empty())
385 batchFrameIndex = (mLoadWaitingQueue.back() + 1) % mFrameCount;
389 batchFrameIndex = (mQueue.Back().mFrameNumber + 1) % mFrameCount;
391 LoadBatch(batchFrameIndex);
398 } //namespace Internal
399 } //namespace Toolkit