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 Dali::FittingMode::Type fittingMode,
63 Dali::SamplingMode::Type samplingMode,
65 TextureManager::MaskingDataPointer& maskingData,
66 ImageCache::FrameReadyObserver& observer,
70 : ImageCache(textureManager, size, fittingMode, samplingMode, maskingData, observer, batchSize, interval),
76 RollingImageCache::~RollingImageCache()
81 TextureSet RollingImageCache::Frame(uint32_t frameIndex)
83 // Pop frames until the frame of frameIndex become front frame.
84 bool popExist = false;
85 while(!mQueue.IsEmpty() && mQueue.Front().mUrlIndex != frameIndex)
91 // TODO: synchronous loading of first frame.
92 if(popExist || mQueue.IsEmpty())
94 uint32_t batchFrameIndex = frameIndex;
95 // If the frame of frameIndex was already loaded, load batch from the last frame of queue
98 batchFrameIndex = (mQueue.Back().mUrlIndex + 1) % static_cast<uint32_t>(mImageUrls.size());
100 LoadBatch(batchFrameIndex);
103 TextureSet textureSet;
104 if(IsFrontReady() == true && mLoadState != TextureManager::LoadState::LOAD_FAILED)
106 textureSet = GetFrontTextureSet();
112 TextureSet RollingImageCache::FirstFrame()
114 TextureSet textureSet = Frame(FIRST_FRAME_INDEX);
118 uint32_t RollingImageCache::GetFrameInterval(uint32_t frameIndex) const
123 int32_t RollingImageCache::GetCurrentFrameIndex() const
129 return mQueue.Front().mUrlIndex;
132 int32_t RollingImageCache::GetTotalFrameCount() const
134 return mImageUrls.size();
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 VisualUrl& 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 LoadComplete 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 LoadComplete method using mRequestingLoad.
160 mRequestingLoad = true;
161 mLoadState = TextureManager::LoadState::LOADING;
163 bool synchronousLoading = false;
164 bool atlasingStatus = false;
165 bool loadingStatus = false;
166 AtlasUploadObserver* atlasObserver = nullptr;
167 ImageAtlasManagerPtr imageAtlasManager = nullptr;
169 Dali::ImageDimensions textureRectSize;
170 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
172 TextureManager::TextureId loadTextureId = TextureManager::INVALID_TEXTURE_ID;
173 TextureSet textureSet = mTextureManager.LoadTexture(
174 url, mDesiredSize, mFittingMode, mSamplingMode, mMaskingData, synchronousLoading, loadTextureId, textureRect, textureRectSize, atlasingStatus, loadingStatus, this, atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED, preMultiply);
175 mImageUrls[imageFrame.mUrlIndex].mTextureId = loadTextureId;
177 mRequestingLoad = false;
180 frameIndex %= mImageUrls.size();
184 TextureSet RollingImageCache::GetFrontTextureSet() const
186 TextureManager::TextureId textureId = GetCachedTextureId(0);
187 TextureSet textureSet = mTextureManager.GetTextureSet(textureId);
190 Sampler sampler = Sampler::New();
191 sampler.SetWrapMode(Dali::WrapMode::Type::DEFAULT, Dali::WrapMode::Type::DEFAULT);
192 textureSet.SetSampler(0u, sampler);
197 TextureManager::TextureId RollingImageCache::GetCachedTextureId(int index) const
199 return mImageUrls[mQueue[index].mUrlIndex].mTextureId;
202 void RollingImageCache::PopFrontCache()
204 ImageFrame imageFrame = mQueue.PopFront();
205 mTextureManager.Remove(mImageUrls[imageFrame.mUrlIndex].mTextureId, this);
206 mImageUrls[imageFrame.mUrlIndex].mTextureId = TextureManager::INVALID_TEXTURE_ID;
208 if(mMaskingData && mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID)
212 mMaskingData->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
217 void RollingImageCache::ClearCache()
219 while(mTextureManagerAlive && !mQueue.IsEmpty())
223 mLoadState = TextureManager::LoadState::NOT_STARTED;
226 void RollingImageCache::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
228 DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "AnimatedImageVisual::LoadComplete(textureId:%d) start\n", textureInformation.textureId);
233 mLoadState = TextureManager::LoadState::LOAD_FINISHED;
234 bool frontFrameReady = IsFrontReady();
237 for(std::size_t i = 0; i < mQueue.Count(); ++i)
239 if(GetCachedTextureId(i) == textureInformation.textureId)
241 mQueue[i].mReady = true;
248 // LoadComplete has been called from within RequestLoad. TextureManager must
249 // therefore already have the texture cached, so make the texture ready.
250 // (Use the last texture, as the texture id hasn't been assigned yet)
251 mQueue.Back().mReady = true;
254 if(!frontFrameReady && IsFrontReady())
256 if(textureInformation.textureSet)
258 Sampler sampler = Sampler::New();
259 sampler.SetWrapMode(Dali::WrapMode::Type::DEFAULT, Dali::WrapMode::Type::DEFAULT);
260 textureInformation.textureSet.SetSampler(0u, sampler);
262 mObserver.FrameReady(textureInformation.textureSet, mInterval);
267 mLoadState = TextureManager::LoadState::LOAD_FAILED;
268 mObserver.FrameReady(TextureSet(), 0);
274 } //namespace Internal
275 } //namespace Toolkit