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),
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),
86 mPreMultiplyOnLoad(preMultiplyOnLoad)
88 mTextureIds.resize(mFrameCount);
89 mIntervals.assign(mFrameCount, 0);
92 RollingAnimatedImageCache::~RollingAnimatedImageCache()
95 mAnimatedImageLoading.Reset();
98 TextureSet RollingAnimatedImageCache::Frame(uint32_t frameIndex)
100 bool popExist = false;
101 while(!mQueue.IsEmpty() && mQueue.Front().mFrameNumber != frameIndex)
107 TextureSet textureSet;
108 uint32_t batchFrameIndex = frameIndex;
109 // If we need to load new frame that are not stored in queue.
110 // Load the frame synchronously.
111 bool synchronouslyLoaded = false;
112 if(mIsSynchronousLoading && mQueue.IsEmpty())
114 textureSet = RequestFrameLoading(frameIndex, true);
115 batchFrameIndex = (frameIndex + 1) % mFrameCount;
116 uint32_t interval = 0u;
119 synchronouslyLoaded = true;
120 interval = mAnimatedImageLoading.GetFrameInterval(mQueue.Back().mFrameNumber);
122 MakeFrameReady(synchronouslyLoaded, textureSet, interval);
125 if(popExist || mQueue.IsEmpty() || synchronouslyLoaded)
127 // If the frame of frameIndex was already loaded, load batch from the last frame of queue
128 if(!mQueue.IsEmpty())
130 if(!mLoadWaitingQueue.empty())
132 batchFrameIndex = (mLoadWaitingQueue.back() + 1) % mFrameCount;
136 batchFrameIndex = (mQueue.Back().mFrameNumber + 1) % mFrameCount;
141 // If the request is for the first frame or a jumped frame(JUMP_TO) remove current waiting queue.
142 mLoadWaitingQueue.clear();
143 // If the queue is empty, and the frame of frameIndex is not loaded synchronously. load batch from the frame of frameIndex
146 batchFrameIndex = frameIndex;
149 LoadBatch(batchFrameIndex);
152 if(!textureSet && mLoadState != TextureManager::LoadState::LOAD_FAILED && IsFrontReady() == true)
154 textureSet = GetFrontTextureSet();
160 TextureSet RollingAnimatedImageCache::FirstFrame()
162 TextureSet textureSet = Frame(FIRST_FRAME_INDEX);
166 uint32_t RollingAnimatedImageCache::GetFrameInterval(uint32_t frameIndex) const
168 if(frameIndex >= mIntervals.size())
172 return mIntervals[frameIndex];
175 int32_t RollingAnimatedImageCache::GetCurrentFrameIndex() const
181 return mQueue.Front().mFrameNumber;
184 int32_t RollingAnimatedImageCache::GetTotalFrameCount() const
189 bool RollingAnimatedImageCache::IsFrontReady() const
191 return (!mQueue.IsEmpty() && mQueue.Front().mReady);
194 TextureSet RollingAnimatedImageCache::RequestFrameLoading(uint32_t frameIndex, bool synchronousLoading)
196 ImageFrame imageFrame;
197 imageFrame.mFrameNumber = frameIndex;
198 imageFrame.mReady = false;
200 mQueue.PushBack(imageFrame);
202 mLoadState = TextureManager::LoadState::LOADING;
204 auto preMultiplyOnLoading = mPreMultiplyOnLoad ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD
205 : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
207 TextureManager::TextureId loadTextureId = TextureManager::INVALID_TEXTURE_ID;
208 TextureSet textureSet = mTextureManager.LoadAnimatedImageTexture(mImageUrl,
209 mAnimatedImageLoading,
218 preMultiplyOnLoading);
221 Sampler sampler = Sampler::New();
222 sampler.SetWrapMode(mWrapModeU, mWrapModeV);
223 textureSet.SetSampler(0u, sampler);
226 mTextureIds[frameIndex] = loadTextureId;
231 void RollingAnimatedImageCache::LoadBatch(uint32_t frameIndex)
233 // Try and load up to mBatchSize images, until the cache is filled.
234 // Once the cache is filled, as frames progress, the old frame is
235 // removed, and another frame is loaded
236 uint32_t minimumSize = std::min(mCacheSize, mFrameCount);
237 for(uint32_t i = 0; i < mBatchSize && (mQueue.Count() + mLoadWaitingQueue.size()) < minimumSize; ++i)
239 if(mLoadState != TextureManager::LoadState::LOADING)
241 RequestFrameLoading(frameIndex, false);
245 mLoadWaitingQueue.push_back(frameIndex);
249 frameIndex %= mFrameCount;
255 void RollingAnimatedImageCache::SetImageFrameReady(TextureManager::TextureId textureId)
257 for(std::size_t i = 0; i < mQueue.Count(); ++i)
259 if(GetCachedTextureId(i) == textureId)
261 mQueue[i].mReady = true;
267 TextureSet RollingAnimatedImageCache::GetFrontTextureSet() const
269 DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "RollingAnimatedImageCache::GetFrontTextureSet() FrameNumber:%d\n", mQueue[0].mFrameNumber);
271 TextureManager::TextureId textureId = GetCachedTextureId(0);
272 TextureSet textureSet = mTextureManager.GetTextureSet(textureId);
275 Sampler sampler = Sampler::New();
276 sampler.SetWrapMode(mWrapModeU, mWrapModeV);
277 textureSet.SetSampler(0u, sampler);
282 TextureManager::TextureId RollingAnimatedImageCache::GetCachedTextureId(int index) const
284 return mTextureIds[mQueue[index].mFrameNumber];
287 void RollingAnimatedImageCache::PopFrontCache()
289 ImageFrame imageFrame = mQueue.PopFront();
290 mTextureManager.Remove(mTextureIds[imageFrame.mFrameNumber], this);
291 mTextureIds[imageFrame.mFrameNumber] = TextureManager::INVALID_TEXTURE_ID;
293 if(mMaskingData && mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID)
297 mMaskingData->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
302 void RollingAnimatedImageCache::ClearCache()
304 while(mTextureManagerAlive && !mQueue.IsEmpty())
308 mLoadWaitingQueue.clear();
309 mLoadState = TextureManager::LoadState::NOT_STARTED;
312 void RollingAnimatedImageCache::MakeFrameReady(bool loadSuccess, TextureSet textureSet, uint32_t interval)
316 mLoadState = TextureManager::LoadState::LOAD_FAILED;
317 mObserver.FrameReady(TextureSet(), 0);
321 mLoadState = TextureManager::LoadState::LOAD_FINISHED;
323 // Reset size of Queue according to the real frame count.
324 if(mFrameCount != mAnimatedImageLoading.GetImageCount())
326 mFrameCount = mAnimatedImageLoading.GetImageCount();
327 mTextureIds.resize(mFrameCount);
328 mIntervals.assign(mFrameCount, 0u);
331 bool frontFrameReady = IsFrontReady();
332 // Because only one frame is on loading and the others are in mLoadWaitingQueue,
333 // mQueue.Back() is always the frame currently loaded.
334 mQueue.Back().mReady = true;
335 mIntervals[mQueue.Back().mFrameNumber] = interval;
336 // Check whether currently loaded frame is front of queue or not.
337 // If it is, notify frame ready to observer.
338 if(frontFrameReady == false && IsFrontReady())
340 mObserver.FrameReady(textureSet, interval);
345 void RollingAnimatedImageCache::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
347 DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "AnimatedImageVisual::LoadComplete(textureId:%d) start\n", textureInformation.textureId);
350 if(textureInformation.textureSet)
352 Sampler sampler = Sampler::New();
353 sampler.SetWrapMode(mWrapModeU, mWrapModeV);
354 textureInformation.textureSet.SetSampler(0u, sampler);
357 MakeFrameReady(loadSuccess, textureInformation.textureSet, textureInformation.interval);
361 // The frames of a single animated image can not be loaded parallelly.
362 // Therefore, a frame is now loading, other orders are waiting.
363 // And, after the frame is loaded, requests load of next order.
364 if(!mLoadWaitingQueue.empty())
366 uint32_t loadingIndex = mLoadWaitingQueue.front();
367 mLoadWaitingQueue.erase(mLoadWaitingQueue.begin());
368 RequestFrameLoading(loadingIndex, false);
370 else if(mQueue.Count() == 1u && textureInformation.frameCount > SINGLE_IMAGE_COUNT)
372 // There is only an image in queue and no waiting queue.
373 // Request to load batch once again.
374 uint32_t batchFrameIndex = 0u;
375 if(!mLoadWaitingQueue.empty())
377 batchFrameIndex = (mLoadWaitingQueue.back() + 1) % mFrameCount;
381 batchFrameIndex = (mQueue.Back().mFrameNumber + 1) % mFrameCount;
383 LoadBatch(batchFrameIndex);
390 } //namespace Internal
391 } //namespace Toolkit