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 <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 TextureManager::MaskingDataPointer& maskingData,
63 ImageCache::FrameReadyObserver& observer,
67 : ImageCache(textureManager, maskingData, observer, batchSize, interval),
73 RollingImageCache::~RollingImageCache()
78 TextureSet RollingImageCache::Frame(uint32_t frameIndex)
80 // Pop frames until the frame of frameIndex become front frame.
81 bool popExist = false;
82 while(!mQueue.IsEmpty() && mQueue.Front().mUrlIndex != frameIndex)
88 // TODO: synchronous loading of first frame.
89 if(popExist || mQueue.IsEmpty())
91 uint32_t batchFrameIndex = frameIndex;
92 // If the frame of frameIndex was already loaded, load batch from the last frame of queue
95 batchFrameIndex = (mQueue.Back().mUrlIndex + 1) % mImageUrls.size();
97 LoadBatch(batchFrameIndex);
100 TextureSet textureSet;
101 if(IsFrontReady() == true && mLoadState != TextureManager::LoadState::LOAD_FAILED)
103 textureSet = GetFrontTextureSet();
109 TextureSet RollingImageCache::FirstFrame()
111 TextureSet textureSet = Frame(FIRST_FRAME_INDEX);
115 uint32_t RollingImageCache::GetFrameInterval(uint32_t frameIndex) const
120 int32_t RollingImageCache::GetCurrentFrameIndex() const
126 return mQueue.Front().mUrlIndex;
129 int32_t RollingImageCache::GetTotalFrameCount() const
131 return mImageUrls.size();
134 bool RollingImageCache::IsFrontReady() const
136 return (!mQueue.IsEmpty() && mQueue.Front().mReady);
139 void RollingImageCache::LoadBatch(uint32_t frameIndex)
141 // Try and load up to mBatchSize images, until the cache is filled.
142 // Once the cache is filled, as frames progress, the old frame is
143 // cleared, but not erased, and another image is loaded
144 for(unsigned int i = 0; i < mBatchSize && !mQueue.IsFull(); ++i)
146 ImageFrame imageFrame;
148 VisualUrl& url = mImageUrls[frameIndex].mUrl;
149 imageFrame.mUrlIndex = frameIndex;
150 imageFrame.mReady = false;
152 mQueue.PushBack(imageFrame);
154 // Note, if the image is already loaded, then LoadComplete will get called
155 // from within this method. This means it won't yet have a texture id, so we
156 // need to account for this inside the LoadComplete method using mRequestingLoad.
157 mRequestingLoad = true;
158 mLoadState = TextureManager::LoadState::LOADING;
160 bool synchronousLoading = false;
161 bool atlasingStatus = false;
162 bool loadingStatus = false;
163 AtlasUploadObserver* atlasObserver = nullptr;
164 ImageAtlasManagerPtr imageAtlasManager = nullptr;
166 Dali::ImageDimensions textureRectSize;
167 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
169 TextureManager::TextureId loadTextureId = TextureManager::INVALID_TEXTURE_ID;
170 TextureSet textureSet = mTextureManager.LoadTexture(
171 url, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::BOX_THEN_LINEAR, mMaskingData, synchronousLoading, loadTextureId, textureRect, textureRectSize, atlasingStatus, loadingStatus, Dali::WrapMode::Type::DEFAULT, Dali::WrapMode::Type::DEFAULT, this, atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED, preMultiply);
172 mImageUrls[imageFrame.mUrlIndex].mTextureId = loadTextureId;
174 mRequestingLoad = false;
177 frameIndex %= mImageUrls.size();
181 TextureSet RollingImageCache::GetFrontTextureSet() const
183 TextureManager::TextureId textureId = GetCachedTextureId(0);
184 return mTextureManager.GetTextureSet(textureId);
187 TextureManager::TextureId RollingImageCache::GetCachedTextureId(int index) const
189 return mImageUrls[mQueue[index].mUrlIndex].mTextureId;
192 void RollingImageCache::PopFrontCache()
194 ImageFrame imageFrame = mQueue.PopFront();
195 mTextureManager.Remove(mImageUrls[imageFrame.mUrlIndex].mTextureId, this);
196 mImageUrls[imageFrame.mUrlIndex].mTextureId = TextureManager::INVALID_TEXTURE_ID;
198 if(mMaskingData && mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID)
200 mTextureManager.Remove(mMaskingData->mAlphaMaskId, this);
203 mMaskingData->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
208 void RollingImageCache::ClearCache()
210 while(mTextureManagerAlive && !mQueue.IsEmpty())
214 mLoadState = TextureManager::LoadState::NOT_STARTED;
217 void RollingImageCache::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
219 DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "AnimatedImageVisual::LoadComplete(textureId:%d) start\n", textureInformation.textureId);
224 mLoadState = TextureManager::LoadState::LOAD_FINISHED;
225 bool frontFrameReady = IsFrontReady();
228 for(std::size_t i = 0; i < mQueue.Count(); ++i)
230 if(GetCachedTextureId(i) == textureInformation.textureId)
232 mQueue[i].mReady = true;
239 // LoadComplete has been called from within RequestLoad. TextureManager must
240 // therefore already have the texture cached, so make the texture ready.
241 // (Use the last texture, as the texture id hasn't been assigned yet)
242 mQueue.Back().mReady = true;
245 if(!frontFrameReady && IsFrontReady())
247 mObserver.FrameReady(mTextureManager.GetTextureSet(textureInformation.textureId), mInterval);
252 mLoadState = TextureManager::LoadState::LOAD_FAILED;
253 mObserver.FrameReady(TextureSet(), 0);
259 } //namespace Internal
260 } //namespace Toolkit