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");
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),
72 RollingImageCache::~RollingImageCache()
77 TextureSet RollingImageCache::Frame(uint32_t frameIndex)
79 // Pop frames until the frame of frameIndex become front frame.
80 bool popExist = false;
81 while(!mQueue.IsEmpty() && mQueue.Front().mUrlIndex != frameIndex)
87 // TODO: synchronous loading of first frame.
88 if(popExist || mQueue.IsEmpty())
90 uint32_t batchFrameIndex = frameIndex;
91 // If the frame of frameIndex was already loaded, load batch from the last frame of queue
94 batchFrameIndex = (mQueue.Back().mUrlIndex + 1) % mImageUrls.size();
96 LoadBatch(batchFrameIndex);
99 TextureSet textureSet;
100 if(IsFrontReady() == true && mLoadState != TextureManager::LoadState::LOAD_FAILED)
102 textureSet = GetFrontTextureSet();
108 TextureSet RollingImageCache::FirstFrame()
110 TextureSet textureSet = Frame(FIRST_FRAME_INDEX);
114 uint32_t RollingImageCache::GetFrameInterval(uint32_t frameIndex) const
119 int32_t RollingImageCache::GetCurrentFrameIndex() const
125 return mQueue.Front().mUrlIndex;
128 int32_t RollingImageCache::GetTotalFrameCount() const
130 return mImageUrls.size();
133 bool RollingImageCache::IsFrontReady() const
135 return (!mQueue.IsEmpty() && mQueue.Front().mReady);
138 void RollingImageCache::LoadBatch(uint32_t frameIndex)
140 // Try and load up to mBatchSize images, until the cache is filled.
141 // Once the cache is filled, as frames progress, the old frame is
142 // cleared, but not erased, and another image is loaded
143 for(unsigned int i = 0; i < mBatchSize && !mQueue.IsFull(); ++i)
145 ImageFrame imageFrame;
147 VisualUrl& url = mImageUrls[frameIndex].mUrl;
148 imageFrame.mUrlIndex = frameIndex;
149 imageFrame.mReady = false;
151 mQueue.PushBack(imageFrame);
153 // Note, if the image is already loaded, then LoadComplete will get called
154 // from within this method. This means it won't yet have a texture id, so we
155 // need to account for this inside the LoadComplete method using mRequestingLoad.
156 mRequestingLoad = true;
157 mLoadState = TextureManager::LoadState::LOADING;
159 bool synchronousLoading = false;
160 bool atlasingStatus = false;
161 bool loadingStatus = false;
162 AtlasUploadObserver* atlasObserver = nullptr;
163 ImageAtlasManagerPtr imageAtlasManager = nullptr;
165 Dali::ImageDimensions textureRectSize;
166 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
168 TextureManager::TextureId loadTextureId = TextureManager::INVALID_TEXTURE_ID;
169 TextureSet textureSet = mTextureManager.LoadTexture(
170 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);
171 mImageUrls[imageFrame.mUrlIndex].mTextureId = loadTextureId;
173 mRequestingLoad = false;
176 frameIndex %= mImageUrls.size();
180 TextureSet RollingImageCache::GetFrontTextureSet() const
182 TextureManager::TextureId textureId = GetCachedTextureId(0);
183 return mTextureManager.GetTextureSet(textureId);
186 TextureManager::TextureId RollingImageCache::GetCachedTextureId(int index) const
188 return mImageUrls[mQueue[index].mUrlIndex].mTextureId;
191 void RollingImageCache::PopFrontCache()
193 ImageFrame imageFrame = mQueue.PopFront();
194 mTextureManager.Remove(mImageUrls[imageFrame.mUrlIndex].mTextureId, this);
195 mImageUrls[imageFrame.mUrlIndex].mTextureId = TextureManager::INVALID_TEXTURE_ID;
197 if(mMaskingData && mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID)
199 mTextureManager.Remove(mMaskingData->mAlphaMaskId, this);
202 mMaskingData->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
207 void RollingImageCache::ClearCache()
209 while(mTextureManagerAlive && !mQueue.IsEmpty())
213 mLoadState = TextureManager::LoadState::NOT_STARTED;
216 void RollingImageCache::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
218 DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "AnimatedImageVisual::LoadComplete(textureId:%d) start\n", textureInformation.textureId);
223 mLoadState = TextureManager::LoadState::LOAD_FINISHED;
224 bool frontFrameReady = IsFrontReady();
227 for(std::size_t i = 0; i < mQueue.Count(); ++i)
229 if(GetCachedTextureId(i) == textureInformation.textureId)
231 mQueue[i].mReady = true;
238 // LoadComplete has been called from within RequestLoad. TextureManager must
239 // therefore already have the texture cached, so make the texture ready.
240 // (Use the last texture, as the texture id hasn't been assigned yet)
241 mQueue.Back().mReady = true;
244 if(!frontFrameReady && IsFrontReady())
246 mObserver.FrameReady(mTextureManager.GetTextureSet(textureInformation.textureId), mInterval);
251 mLoadState = TextureManager::LoadState::LOAD_FAILED;
252 mObserver.FrameReady(TextureSet(), 0);
258 } //namespace Internal
259 } //namespace Toolkit