2 * Copyright (c) 2023 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
22 #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 << "={ tex:" << mImageUrls[mQueue[_i].mUrlIndex].mTextureId << " urlId:" << mQueue[_i].mUrlIndex << " rdy:" << (mQueue[_i].mReady ? "T" : "F") << "}, "; \
40 oss << " ]" << std::endl; \
41 DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "%s", oss.str().c_str()); \
48 static constexpr bool ENABLE_ORIENTATION_CORRECTION(true);
50 static constexpr uint32_t FIRST_FRAME_INDEX = 0u;
60 RollingImageCache::RollingImageCache(TextureManager& textureManager,
62 Dali::FittingMode::Type fittingMode,
63 Dali::SamplingMode::Type samplingMode,
65 TextureManager::MaskingDataPointer& maskingData,
66 ImageCache::FrameReadyObserver& observer,
70 bool preMultiplyOnLoad)
71 : ImageCache(textureManager, size, fittingMode, samplingMode, maskingData, observer, batchSize, interval, preMultiplyOnLoad),
77 RollingImageCache::~RollingImageCache()
82 TextureSet RollingImageCache::Frame(uint32_t frameIndex)
84 // Pop frames until the frame of frameIndex become front frame.
85 bool popExist = false;
86 while(!mQueue.IsEmpty() && mQueue.Front().mUrlIndex != frameIndex)
92 // TODO: synchronous loading of first frame.
93 if(popExist || mQueue.IsEmpty())
95 uint32_t batchFrameIndex = frameIndex;
96 // If the frame of frameIndex was already loaded, load batch from the last frame of queue
99 batchFrameIndex = (mQueue.Back().mUrlIndex + 1) % static_cast<uint32_t>(mImageUrls.size());
101 LoadBatch(batchFrameIndex);
104 TextureSet textureSet;
105 if(IsFrontReady() == true && mLoadState != TextureManager::LoadState::LOAD_FAILED)
107 textureSet = GetFrontTextureSet();
113 TextureSet RollingImageCache::FirstFrame()
115 TextureSet textureSet = Frame(FIRST_FRAME_INDEX);
119 uint32_t RollingImageCache::GetFrameInterval(uint32_t frameIndex) const
124 int32_t RollingImageCache::GetCurrentFrameIndex() const
130 return mQueue.Front().mUrlIndex;
133 int32_t RollingImageCache::GetTotalFrameCount() const
135 return mImageUrls.size();
138 bool RollingImageCache::IsFrontReady() const
140 return (!mQueue.IsEmpty() && mQueue.Front().mReady);
143 void RollingImageCache::LoadBatch(uint32_t frameIndex)
145 // Try and load up to mBatchSize images, until the cache is filled.
146 // Once the cache is filled, as frames progress, the old frame is
147 // cleared, but not erased, and another image is loaded
148 for(unsigned int i = 0; i < mBatchSize && !mQueue.IsFull(); ++i)
150 ImageFrame imageFrame;
152 VisualUrl& url = mImageUrls[frameIndex].mUrl;
153 imageFrame.mUrlIndex = frameIndex;
154 imageFrame.mReady = false;
156 mQueue.PushBack(imageFrame);
158 // Note, if the image is already loaded, then LoadComplete will get called
159 // from within this method. This means it won't yet have a texture id, so we
160 // need to account for this inside the LoadComplete method using mRequestingLoad.
161 mRequestingLoad = true;
162 mLoadState = TextureManager::LoadState::LOADING;
164 bool synchronousLoading = false;
165 bool atlasingStatus = false;
166 bool loadingStatus = false;
167 AtlasUploadObserver* atlasObserver = nullptr;
168 ImageAtlasManagerPtr imageAtlasManager = nullptr;
170 Dali::ImageDimensions textureRectSize;
172 auto preMultiplyOnLoading = mPreMultiplyOnLoad ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD
173 : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
175 TextureManager::TextureId loadTextureId = TextureManager::INVALID_TEXTURE_ID;
176 TextureSet textureSet = mTextureManager.LoadTexture(
177 url, mDesiredSize, mFittingMode, mSamplingMode, mMaskingData, synchronousLoading, loadTextureId, textureRect, textureRectSize, atlasingStatus, loadingStatus, this, atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED, preMultiplyOnLoading);
178 mImageUrls[imageFrame.mUrlIndex].mTextureId = loadTextureId;
180 mRequestingLoad = false;
183 frameIndex %= mImageUrls.size();
187 TextureSet RollingImageCache::GetFrontTextureSet() const
189 TextureManager::TextureId textureId = GetCachedTextureId(0);
190 TextureSet textureSet = mTextureManager.GetTextureSet(textureId);
193 Sampler sampler = Sampler::New();
194 sampler.SetWrapMode(Dali::WrapMode::Type::DEFAULT, Dali::WrapMode::Type::DEFAULT);
195 textureSet.SetSampler(0u, sampler);
200 TextureManager::TextureId RollingImageCache::GetCachedTextureId(int index) const
202 return mImageUrls[mQueue[index].mUrlIndex].mTextureId;
205 void RollingImageCache::PopFrontCache()
207 ImageFrame imageFrame = mQueue.PopFront();
209 mTextureManager.RequestRemove(mImageUrls[imageFrame.mUrlIndex].mTextureId, this);
210 mImageUrls[imageFrame.mUrlIndex].mTextureId = TextureManager::INVALID_TEXTURE_ID;
212 if(mMaskingData && mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID)
216 mMaskingData->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
221 void RollingImageCache::ClearCache()
223 while(mTextureManagerAlive && !mQueue.IsEmpty())
227 mLoadState = TextureManager::LoadState::NOT_STARTED;
230 void RollingImageCache::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
232 DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "AnimatedImageVisual::LoadComplete(textureId:%d) start\n", textureInformation.textureId);
237 mLoadState = TextureManager::LoadState::LOAD_FINISHED;
238 bool frontFrameReady = IsFrontReady();
241 for(std::size_t i = 0; i < mQueue.Count(); ++i)
243 if(GetCachedTextureId(i) == textureInformation.textureId)
245 mQueue[i].mReady = true;
252 // LoadComplete has been called from within RequestLoad. TextureManager must
253 // therefore already have the texture cached, so make the texture ready.
254 // (Use the last texture, as the texture id hasn't been assigned yet)
255 mQueue.Back().mReady = true;
258 if(!frontFrameReady && IsFrontReady())
260 if(textureInformation.textureSet)
262 Sampler sampler = Sampler::New();
263 sampler.SetWrapMode(Dali::WrapMode::Type::DEFAULT, Dali::WrapMode::Type::DEFAULT);
264 textureInformation.textureSet.SetSampler(0u, sampler);
266 mObserver.FrameReady(textureInformation.textureSet, mInterval, textureInformation.preMultiplied);
271 mLoadState = TextureManager::LoadState::LOAD_FAILED;
272 // preMultiplied should be false because broken image don't premultiply alpha on load
273 mObserver.FrameReady(TextureSet(), 0, false);
279 } //namespace Internal
280 } //namespace Toolkit