2 * Copyright (c) 2021 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");
33 std::ostringstream oss; \
34 oss << "Size:" << mQueue.Count() << " [ "; \
35 for(std::size_t _i = 0; _i < mQueue.Count(); ++_i) \
37 oss << _i << "={ tex:" << mImageUrls[mQueue[_i].mUrlIndex].mTextureId << " urlId:" << mQueue[_i].mUrlIndex << " rdy:" << (mQueue[_i].mReady ? "T" : "F") << "}, "; \
39 oss << " ]" << std::endl; \
40 DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "%s", oss.str().c_str()); \
47 static constexpr bool ENABLE_ORIENTATION_CORRECTION(true);
49 static constexpr uint32_t FIRST_FRAME_INDEX = 0u;
59 RollingImageCache::RollingImageCache(TextureManager& textureManager,
61 TextureManager::MaskingDataPointer& maskingData,
62 ImageCache::FrameReadyObserver& observer,
66 : ImageCache(textureManager, maskingData, observer, batchSize, interval),
70 mLoadStates.assign(mImageUrls.size(), TextureManager::LoadState::NOT_STARTED);
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)
87 if(popExist || mQueue.IsEmpty())
89 uint32_t batchFrameIndex = frameIndex;
90 // If the frame of frameIndex was already loaded, load batch from the last frame of queue
93 batchFrameIndex = (mQueue.Back().mUrlIndex + 1) % mImageUrls.size();
95 LoadBatch(batchFrameIndex);
98 TextureSet textureSet;
99 if(IsFrontReady() == true)
101 textureSet = GetFrontTextureSet();
107 TextureSet RollingImageCache::FirstFrame()
109 TextureSet textureSet = Frame(FIRST_FRAME_INDEX);
113 uint32_t RollingImageCache::GetFrameInterval(uint32_t frameIndex) const
118 int32_t RollingImageCache::GetCurrentFrameIndex() const
124 return mQueue.Front().mUrlIndex;
127 int32_t RollingImageCache::GetTotalFrameCount() const
129 return mImageUrls.size();
132 TextureManager::LoadState RollingImageCache::GetLoadState() const
134 return mLoadStates[mQueue.Front().mUrlIndex];
137 bool RollingImageCache::IsFrontReady() const
139 return (!mQueue.IsEmpty() && mQueue.Front().mReady);
142 void RollingImageCache::LoadBatch(uint32_t frameIndex)
144 // Try and load up to mBatchSize images, until the cache is filled.
145 // Once the cache is filled, as frames progress, the old frame is
146 // cleared, but not erased, and another image is loaded
147 for(unsigned int i = 0; i < mBatchSize && !mQueue.IsFull(); ++i)
149 ImageFrame imageFrame;
151 std::string& url = mImageUrls[frameIndex].mUrl;
152 imageFrame.mUrlIndex = frameIndex;
153 imageFrame.mReady = false;
155 mQueue.PushBack(imageFrame);
157 // Note, if the image is already loaded, then UploadComplete will get called
158 // from within this method. This means it won't yet have a texture id, so we
159 // need to account for this inside the UploadComplete method using mRequestingLoad.
160 mRequestingLoad = true;
162 bool synchronousLoading = false;
163 bool atlasingStatus = false;
164 bool loadingStatus = false;
165 AtlasUploadObserver* atlasObserver = nullptr;
166 ImageAtlasManagerPtr imageAtlasManager = nullptr;
168 Dali::ImageDimensions textureRectSize;
169 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
171 TextureSet textureSet = mTextureManager.LoadTexture(
172 url, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::BOX_THEN_LINEAR, mMaskingData, synchronousLoading, mImageUrls[imageFrame.mUrlIndex].mTextureId, textureRect, textureRectSize, atlasingStatus, loadingStatus, Dali::WrapMode::Type::DEFAULT, Dali::WrapMode::Type::DEFAULT, this, atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED, preMultiply);
174 // If textureSet is returned but loadingState is false than load state is LOAD_FINISHED. (Notification is not comming yet.)
175 // If textureSet is null and the request is synchronous, load state is LOAD_FAILED.
176 // If textureSet is null but the request is asynchronous, the frame is still loading so load state is LOADING.
177 mLoadStates[frameIndex] = TextureManager::LoadState::LOADING;
182 mLoadStates[frameIndex] = TextureManager::LoadState::LOAD_FINISHED;
185 else if(synchronousLoading)
187 // Synchronous loading is failed
188 mLoadStates[frameIndex] = TextureManager::LoadState::LOAD_FAILED;
191 mRequestingLoad = false;
194 frameIndex %= mImageUrls.size();
198 void RollingImageCache::SetImageFrameReady(TextureManager::TextureId textureId, bool loadSuccess)
200 for(std::size_t i = 0; i < mQueue.Count(); ++i)
202 if(GetCachedTextureId(i) == textureId)
206 mLoadStates[i] = TextureManager::LoadState::LOAD_FINISHED;
210 mLoadStates[i] = TextureManager::LoadState::LOAD_FAILED;
212 mQueue[i].mReady = true;
218 TextureSet RollingImageCache::GetFrontTextureSet() const
220 TextureManager::TextureId textureId = GetCachedTextureId(0);
221 return mTextureManager.GetTextureSet(textureId);
224 TextureManager::TextureId RollingImageCache::GetCachedTextureId(int index) const
226 return mImageUrls[mQueue[index].mUrlIndex].mTextureId;
229 void RollingImageCache::CheckFrontFrame(bool wasReady)
231 if(wasReady == false && IsFrontReady())
233 mObserver.FrameReady(GetFrontTextureSet(), mInterval);
237 void RollingImageCache::PopFrontCache()
239 ImageFrame imageFrame = mQueue.PopFront();
240 mTextureManager.Remove(mImageUrls[imageFrame.mUrlIndex].mTextureId, this);
241 mImageUrls[imageFrame.mUrlIndex].mTextureId = TextureManager::INVALID_TEXTURE_ID;
243 if(mMaskingData && mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID)
245 mTextureManager.Remove(mMaskingData->mAlphaMaskId, this);
248 mMaskingData->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
253 void RollingImageCache::ClearCache()
255 while(mTextureManagerAlive && !mQueue.IsEmpty())
259 mLoadStates.assign(mImageUrls.size(), TextureManager::LoadState::NOT_STARTED);
262 void RollingImageCache::UploadComplete(
265 TextureSet textureSet,
267 const Vector4& atlasRect,
270 DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "AnimatedImageVisual::UploadComplete(textureId:%d) start\n", textureId);
273 bool frontFrameReady = IsFrontReady();
274 SetImageFrameReady(textureId, loadSuccess);
277 CheckFrontFrame(frontFrameReady);
283 } //namespace Internal
284 } //namespace Toolkit