2 * Copyright (c) 2024 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 <dali-toolkit/internal/visuals/animated-image/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/adaptor-framework/adaptor.h>
24 #include <dali/integration-api/debug.h>
28 #if defined(DEBUG_ENABLED)
29 Debug::Filter* gAnimImgLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_ANIMATED_IMAGE");
32 if(gAnimImgLogFilter->IsEnabledFor(Debug::Concise)) \
34 std::ostringstream oss; \
35 oss << "Size:" << mQueue.Count() << " [ "; \
36 for(std::size_t _i = 0; _i < mQueue.Count(); ++_i) \
38 oss << _i << "={ frm#: " << mQueue[_i].mFrameNumber << " tex: " << mTextureIds[mQueue[_i].mFrameNumber] << "}, "; \
40 oss << " ]" << std::endl; \
41 DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "%s", oss.str().c_str()); \
57 static constexpr uint32_t SINGLE_IMAGE_COUNT = 1u;
58 static constexpr uint32_t FIRST_FRAME_INDEX = 0u;
61 RollingAnimatedImageCache::RollingAnimatedImageCache(TextureManager& textureManager,
63 Dali::FittingMode::Type fittingMode,
64 Dali::SamplingMode::Type samplingMode,
65 AnimatedImageLoading& animatedImageLoading,
66 TextureManager::MaskingDataPointer& maskingData,
67 ImageCache::FrameReadyObserver& observer,
70 const Dali::WrapMode::Type& wrapModeU,
71 const Dali::WrapMode::Type& wrapModeV,
72 bool isSynchronousLoading,
73 bool preMultiplyOnLoad)
74 : ImageCache(textureManager, size, fittingMode, samplingMode, maskingData, observer, batchSize, 0u, preMultiplyOnLoad),
75 mImageUrl(animatedImageLoading.GetUrl()),
76 mAnimatedImageLoading(animatedImageLoading),
77 mFrameCount(SINGLE_IMAGE_COUNT),
78 mFrameIndex(FIRST_FRAME_INDEX),
79 mCacheSize(cacheSize),
81 mWrapModeU(wrapModeU),
82 mWrapModeV(wrapModeV),
83 mIsSynchronousLoading(isSynchronousLoading)
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 auto preMultiplyOnLoading = mPreMultiplyOnLoad ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD
112 : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
114 textureSet = RequestFrameLoading(frameIndex, true, preMultiplyOnLoading);
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, preMultiplyOnLoading == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD);
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)
196 auto preMultiplyOnLoading = mPreMultiplyOnLoad ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD
197 : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
199 return RequestFrameLoading(frameIndex, false, preMultiplyOnLoading);
202 TextureSet RollingAnimatedImageCache::RequestFrameLoading(uint32_t frameIndex, bool synchronousLoading, TextureManager::MultiplyOnLoad& preMultiplyOnLoading)
204 ImageFrame imageFrame;
205 imageFrame.mFrameNumber = frameIndex;
206 imageFrame.mReady = false;
208 mQueue.PushBack(imageFrame);
210 mLoadState = TextureManager::LoadState::LOADING;
212 TextureManager::TextureId loadTextureId = TextureManager::INVALID_TEXTURE_ID;
213 TextureSet textureSet = mTextureManager.LoadAnimatedImageTexture(mImageUrl,
214 mAnimatedImageLoading,
223 preMultiplyOnLoading);
226 Sampler sampler = Sampler::New();
227 sampler.SetWrapMode(mWrapModeU, mWrapModeV);
228 textureSet.SetSampler(0u, sampler);
231 mTextureIds[frameIndex] = loadTextureId;
236 void RollingAnimatedImageCache::LoadBatch(uint32_t frameIndex)
238 // Try and load up to mBatchSize images, until the cache is filled.
239 // Once the cache is filled, as frames progress, the old frame is
240 // removed, and another frame is loaded
241 uint32_t minimumSize = std::min(mCacheSize, mFrameCount);
242 for(uint32_t i = 0; i < mBatchSize && (mQueue.Count() + mLoadWaitingQueue.size()) < minimumSize; ++i)
244 if(mLoadState != TextureManager::LoadState::LOADING)
246 RequestFrameLoading(frameIndex);
250 mLoadWaitingQueue.push_back(frameIndex);
254 frameIndex %= mFrameCount;
260 void RollingAnimatedImageCache::SetImageFrameReady(TextureManager::TextureId textureId)
262 for(std::size_t i = 0; i < mQueue.Count(); ++i)
264 if(GetCachedTextureId(i) == textureId)
266 mQueue[i].mReady = true;
272 TextureSet RollingAnimatedImageCache::GetFrontTextureSet() const
274 DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "RollingAnimatedImageCache::GetFrontTextureSet() FrameNumber:%d\n", mQueue[0].mFrameNumber);
276 TextureManager::TextureId textureId = GetCachedTextureId(0);
277 TextureSet textureSet = mTextureManager.GetTextureSet(textureId);
280 Sampler sampler = Sampler::New();
281 sampler.SetWrapMode(mWrapModeU, mWrapModeV);
282 textureSet.SetSampler(0u, sampler);
287 TextureManager::TextureId RollingAnimatedImageCache::GetCachedTextureId(int index) const
289 return mTextureIds[mQueue[index].mFrameNumber];
292 void RollingAnimatedImageCache::PopFrontCache()
294 ImageFrame imageFrame = mQueue.PopFront();
296 mTextureManager.RequestRemove(mTextureIds[imageFrame.mFrameNumber], this);
297 mTextureIds[imageFrame.mFrameNumber] = TextureManager::INVALID_TEXTURE_ID;
299 if(mMaskingData && mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID)
303 mMaskingData->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
308 void RollingAnimatedImageCache::ClearCache()
310 while(Dali::Adaptor::IsAvailable() && !mQueue.IsEmpty())
314 mLoadWaitingQueue.clear();
315 mLoadState = TextureManager::LoadState::NOT_STARTED;
318 void RollingAnimatedImageCache::MakeFrameReady(bool loadSuccess, TextureSet textureSet, uint32_t interval, bool preMultiplied)
322 mLoadState = TextureManager::LoadState::LOAD_FAILED;
323 // preMultiplied should be false because broken image don't premultiply alpha on load
324 mObserver.FrameReady(TextureSet(), 0, false);
328 mLoadState = TextureManager::LoadState::LOAD_FINISHED;
330 // Reset size of Queue according to the real frame count.
331 if(mFrameCount != mAnimatedImageLoading.GetImageCount())
333 mFrameCount = mAnimatedImageLoading.GetImageCount();
334 mTextureIds.resize(mFrameCount);
335 mIntervals.assign(mFrameCount, 0u);
338 bool frontFrameReady = IsFrontReady();
339 // Because only one frame is on loading and the others are in mLoadWaitingQueue,
340 // mQueue.Back() is always the frame currently loaded.
341 mQueue.Back().mReady = true;
342 mIntervals[mQueue.Back().mFrameNumber] = interval;
343 // Check whether currently loaded frame is front of queue or not.
344 // If it is, notify frame ready to observer.
345 if(frontFrameReady == false && IsFrontReady())
347 mObserver.FrameReady(textureSet, interval, preMultiplied);
352 void RollingAnimatedImageCache::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
354 DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "AnimatedImageVisual::LoadComplete(textureId:%d) start\n", textureInformation.textureId);
357 if(textureInformation.textureSet)
359 Sampler sampler = Sampler::New();
360 sampler.SetWrapMode(mWrapModeU, mWrapModeV);
361 textureInformation.textureSet.SetSampler(0u, sampler);
364 MakeFrameReady(loadSuccess, textureInformation.textureSet, textureInformation.interval, textureInformation.preMultiplied);
366 // TODO : We need to remove some below logics, since user can remove Visual during ResourceReady callback.
370 // The frames of a single animated image can not be loaded parallelly.
371 // Therefore, a frame is now loading, other orders are waiting.
372 // And, after the frame is loaded, requests load of next order.
373 if(!mLoadWaitingQueue.empty())
375 uint32_t loadingIndex = mLoadWaitingQueue.front();
376 mLoadWaitingQueue.erase(mLoadWaitingQueue.begin());
377 RequestFrameLoading(loadingIndex);
379 else if(mQueue.Count() == 1u && textureInformation.frameCount > SINGLE_IMAGE_COUNT)
381 // There is only an image in queue and no waiting queue.
382 // Request to load batch once again.
383 uint32_t batchFrameIndex = 0u;
384 if(!mLoadWaitingQueue.empty())
386 batchFrameIndex = (mLoadWaitingQueue.back() + 1) % mFrameCount;
390 batchFrameIndex = (mQueue.Back().mFrameNumber + 1) % mFrameCount;
392 LoadBatch(batchFrameIndex);
399 } //namespace Internal
400 } //namespace Toolkit