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-image-cache.h>
21 #include <dali-toolkit/internal/visuals/image-atlas-manager.h> // For ImageAtlasManagerPtr
24 #include <dali/integration-api/adaptor-framework/adaptor.h>
25 #include <dali/integration-api/debug.h>
29 #if defined(DEBUG_ENABLED)
30 Debug::Filter* gAnimImgLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_ANIMATED_IMAGE");
33 if(gAnimImgLogFilter->IsEnabledFor(Debug::Concise)) \
35 std::ostringstream oss; \
36 oss << "Size:" << mQueue.Count() << " [ "; \
37 for(std::size_t _i = 0; _i < mQueue.Count(); ++_i) \
39 oss << _i << "={ tex:" << mImageUrls[mQueue[_i].mUrlIndex].mTextureId << " urlId:" << mQueue[_i].mUrlIndex << " rdy:" << (mQueue[_i].mReady ? "T" : "F") << "}, "; \
41 oss << " ]" << std::endl; \
42 DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "%s", oss.str().c_str()); \
49 static constexpr bool ENABLE_ORIENTATION_CORRECTION(true);
51 static constexpr uint32_t FIRST_FRAME_INDEX = 0u;
61 RollingImageCache::RollingImageCache(TextureManager& textureManager,
63 Dali::FittingMode::Type fittingMode,
64 Dali::SamplingMode::Type samplingMode,
66 TextureManager::MaskingDataPointer& maskingData,
67 ImageCache::FrameReadyObserver& observer,
71 bool preMultiplyOnLoad)
72 : ImageCache(textureManager, size, fittingMode, samplingMode, maskingData, observer, batchSize, interval, preMultiplyOnLoad),
78 RollingImageCache::~RollingImageCache()
83 TextureSet RollingImageCache::Frame(uint32_t frameIndex)
85 // Pop frames until the frame of frameIndex become front frame.
86 bool popExist = false;
87 while(!mQueue.IsEmpty() && mQueue.Front().mUrlIndex != frameIndex)
93 // TODO: synchronous loading of first frame.
94 if(popExist || mQueue.IsEmpty())
96 uint32_t batchFrameIndex = frameIndex;
97 // If the frame of frameIndex was already loaded, load batch from the last frame of queue
100 batchFrameIndex = (mQueue.Back().mUrlIndex + 1) % static_cast<uint32_t>(mImageUrls.size());
102 LoadBatch(batchFrameIndex);
105 TextureSet textureSet;
106 if(IsFrontReady() == true && mLoadState != TextureManager::LoadState::LOAD_FAILED)
108 textureSet = GetFrontTextureSet();
114 TextureSet RollingImageCache::FirstFrame()
116 TextureSet textureSet = Frame(FIRST_FRAME_INDEX);
120 uint32_t RollingImageCache::GetFrameInterval(uint32_t frameIndex) const
125 int32_t RollingImageCache::GetCurrentFrameIndex() const
131 return mQueue.Front().mUrlIndex;
134 int32_t RollingImageCache::GetTotalFrameCount() const
136 return mImageUrls.size();
139 bool RollingImageCache::IsFrontReady() const
141 return (!mQueue.IsEmpty() && mQueue.Front().mReady);
144 void RollingImageCache::LoadBatch(uint32_t frameIndex)
146 // Try and load up to mBatchSize images, until the cache is filled.
147 // Once the cache is filled, as frames progress, the old frame is
148 // cleared, but not erased, and another image is loaded
149 for(unsigned int i = 0; i < mBatchSize && !mQueue.IsFull(); ++i)
151 ImageFrame imageFrame;
153 VisualUrl& url = mImageUrls[frameIndex].mUrl;
154 imageFrame.mUrlIndex = frameIndex;
155 imageFrame.mReady = false;
157 mQueue.PushBack(imageFrame);
159 // Note, if the image is already loaded, then LoadComplete will get called
160 // from within this method. This means it won't yet have a texture id, so we
161 // need to account for this inside the LoadComplete method using mRequestingLoad.
162 mRequestingLoad = true;
163 mLoadState = TextureManager::LoadState::LOADING;
165 bool synchronousLoading = false;
166 bool atlasingStatus = false;
167 bool loadingStatus = false;
168 AtlasUploadObserver* atlasObserver = nullptr;
169 ImageAtlasManagerPtr imageAtlasManager = nullptr;
171 Dali::ImageDimensions textureRectSize;
173 auto preMultiplyOnLoading = mPreMultiplyOnLoad ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD
174 : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
176 TextureManager::TextureId loadTextureId = TextureManager::INVALID_TEXTURE_ID;
177 TextureSet textureSet = mTextureManager.LoadTexture(
178 url, mDesiredSize, mFittingMode, mSamplingMode, mMaskingData, synchronousLoading, loadTextureId, textureRect, textureRectSize, atlasingStatus, loadingStatus, this, atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED, preMultiplyOnLoading);
179 mImageUrls[imageFrame.mUrlIndex].mTextureId = loadTextureId;
181 mRequestingLoad = false;
184 frameIndex %= mImageUrls.size();
188 TextureSet RollingImageCache::GetFrontTextureSet() const
190 TextureManager::TextureId textureId = GetCachedTextureId(0);
191 TextureSet textureSet = mTextureManager.GetTextureSet(textureId);
194 Sampler sampler = Sampler::New();
195 sampler.SetWrapMode(Dali::WrapMode::Type::DEFAULT, Dali::WrapMode::Type::DEFAULT);
196 textureSet.SetSampler(0u, sampler);
201 TextureManager::TextureId RollingImageCache::GetCachedTextureId(int index) const
203 return mImageUrls[mQueue[index].mUrlIndex].mTextureId;
206 void RollingImageCache::PopFrontCache()
208 ImageFrame imageFrame = mQueue.PopFront();
210 mTextureManager.RequestRemove(mImageUrls[imageFrame.mUrlIndex].mTextureId, this);
211 mImageUrls[imageFrame.mUrlIndex].mTextureId = TextureManager::INVALID_TEXTURE_ID;
213 if(mMaskingData && mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID)
217 mMaskingData->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
222 void RollingImageCache::ClearCache()
224 while(Dali::Adaptor::IsAvailable() && !mQueue.IsEmpty())
228 mLoadState = TextureManager::LoadState::NOT_STARTED;
231 void RollingImageCache::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
233 DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "AnimatedImageVisual::LoadComplete(textureId:%d) start\n", textureInformation.textureId);
238 mLoadState = TextureManager::LoadState::LOAD_FINISHED;
239 bool frontFrameReady = IsFrontReady();
242 for(std::size_t i = 0; i < mQueue.Count(); ++i)
244 if(GetCachedTextureId(i) == textureInformation.textureId)
246 mQueue[i].mReady = true;
253 // LoadComplete has been called from within RequestLoad. TextureManager must
254 // therefore already have the texture cached, so make the texture ready.
255 // (Use the last texture, as the texture id hasn't been assigned yet)
256 mQueue.Back().mReady = true;
259 if(!frontFrameReady && IsFrontReady())
261 mObserver.FrameReady(textureInformation.textureSet, mInterval, textureInformation.preMultiplied);
266 mLoadState = TextureManager::LoadState::LOAD_FAILED;
267 // preMultiplied should be false because broken image don't premultiply alpha on load
268 mObserver.FrameReady(TextureSet(), 0, false);
274 } //namespace Internal
275 } //namespace Toolkit