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 const bool ENABLE_ORIENTATION_CORRECTION(true);
57 RollingImageCache::RollingImageCache(
58 TextureManager& textureManager, UrlList& urlList, ImageCache::FrameReadyObserver& observer, uint16_t cacheSize, uint16_t batchSize)
59 : ImageCache(textureManager, observer, batchSize),
66 RollingImageCache::~RollingImageCache()
68 if(mTextureManagerAlive)
70 while(!mQueue.IsEmpty())
72 ImageFrame imageFrame = mQueue.PopFront();
73 mTextureManager.Remove(mImageUrls[imageFrame.mUrlIndex].mTextureId, this);
78 TextureSet RollingImageCache::Frame(uint32_t frameIndex)
80 // If a frame of frameIndex is not loaded, clear the queue and remove all loaded textures.
81 if(mImageUrls[frameIndex].mTextureId == TextureManager::INVALID_TEXTURE_ID)
83 mUrlIndex = frameIndex;
84 while(!mQueue.IsEmpty())
86 ImageFrame imageFrame = mQueue.PopFront();
87 mTextureManager.Remove(mImageUrls[imageFrame.mUrlIndex].mTextureId, this);
88 mImageUrls[imageFrame.mUrlIndex].mTextureId = TextureManager::INVALID_TEXTURE_ID;
92 // If the frame is already loaded, remove previous frames of the frame in the queue
93 // and load new frames amount of removed frames.
96 bool popExist = false;
97 while(!mQueue.IsEmpty() && mQueue.Front().mUrlIndex != frameIndex)
99 ImageFrame imageFrame = mQueue.PopFront();
100 mTextureManager.Remove(mImageUrls[imageFrame.mUrlIndex].mTextureId, this);
101 mImageUrls[imageFrame.mUrlIndex].mTextureId = TextureManager::INVALID_TEXTURE_ID;
106 mUrlIndex = (mQueue.Back().mUrlIndex + 1) % mImageUrls.size();
111 TextureSet textureSet;
112 if(IsFrontReady() == true)
114 textureSet = GetFrontTextureSet();
118 mWaitingForReadyFrame = true;
124 TextureSet RollingImageCache::FirstFrame()
129 TextureSet RollingImageCache::NextFrame()
131 TextureSet textureSet;
132 if(!mQueue.IsEmpty())
134 uint32_t frameIndex = mQueue.Front().mUrlIndex;
137 frameIndex = (frameIndex + 1) % mImageUrls.size();
139 textureSet = Frame(frameIndex);
143 DALI_LOG_ERROR("Cache is empty.");
149 uint32_t RollingImageCache::GetFrameInterval(uint32_t frameIndex) const
154 int32_t RollingImageCache::GetCurrentFrameIndex() const
160 return mQueue.Front().mUrlIndex;
163 int32_t RollingImageCache::GetTotalFrameCount() const
165 return mImageUrls.size();
168 bool RollingImageCache::IsFrontReady() const
170 return (!mQueue.IsEmpty() && mQueue.Front().mReady);
173 void RollingImageCache::LoadBatch()
175 // Try and load up to mBatchSize images, until the cache is filled.
176 // Once the cache is filled, as frames progress, the old frame is
177 // cleared, but not erased, and another image is loaded
178 bool frontFrameReady = IsFrontReady();
180 for(unsigned int i = 0; i < mBatchSize && !mQueue.IsFull(); ++i)
182 ImageFrame imageFrame;
184 std::string& url = mImageUrls[mUrlIndex].mUrl;
185 imageFrame.mUrlIndex = mUrlIndex;
186 imageFrame.mReady = false;
189 mUrlIndex %= mImageUrls.size();
191 mQueue.PushBack(imageFrame);
193 // Note, if the image is already loaded, then UploadComplete will get called
194 // from within this method. This means it won't yet have a texture id, so we
195 // need to account for this inside the UploadComplete method using mRequestingLoad.
196 mRequestingLoad = true;
198 bool synchronousLoading = false;
199 bool atlasingStatus = false;
200 bool loadingStatus = false;
201 TextureManager::MaskingDataPointer maskInfo = nullptr;
202 AtlasUploadObserver* atlasObserver = nullptr;
203 ImageAtlasManagerPtr imageAtlasManager = nullptr;
205 Dali::ImageDimensions textureRectSize;
206 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
208 mTextureManager.LoadTexture(
209 url, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::BOX_THEN_LINEAR, maskInfo, 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);
211 mRequestingLoad = false;
214 CheckFrontFrame(frontFrameReady);
217 void RollingImageCache::SetImageFrameReady(TextureManager::TextureId textureId)
219 for(std::size_t i = 0; i < mQueue.Count(); ++i)
221 if(GetCachedTextureId(i) == textureId)
223 mQueue[i].mReady = true;
229 TextureSet RollingImageCache::GetFrontTextureSet() const
231 TextureManager::TextureId textureId = GetCachedTextureId(0);
232 return mTextureManager.GetTextureSet(textureId);
235 TextureManager::TextureId RollingImageCache::GetCachedTextureId(int index) const
237 return mImageUrls[mQueue[index].mUrlIndex].mTextureId;
240 void RollingImageCache::CheckFrontFrame(bool wasReady)
242 if(mWaitingForReadyFrame && wasReady == false && IsFrontReady())
244 mWaitingForReadyFrame = false;
245 mObserver.FrameReady(GetFrontTextureSet());
249 void RollingImageCache::UploadComplete(
252 TextureSet textureSet,
254 const Vector4& atlasRect,
257 DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "AnimatedImageVisual::UploadComplete(textureId:%d) start\n", textureId);
260 bool frontFrameReady = IsFrontReady();
264 SetImageFrameReady(textureId);
266 CheckFrontFrame(frontFrameReady);
270 // UploadComplete has been called from within RequestLoad. TextureManager must
271 // therefore already have the texture cached, so make the texture ready.
272 // (Use the last texture, as the texture id hasn't been assigned yet)
273 mQueue.Back().mReady = true;
279 void RollingImageCache::LoadComplete(
281 Devel::PixelBuffer pixelBuffer,
282 const VisualUrl& url,
285 // LoadComplete is called if this TextureUploadObserver requested to load
286 // an image that will be returned as a type of PixelBuffer by using a method
287 // TextureManager::LoadPixelBuffer.
290 } //namespace Internal
291 } //namespace Toolkit