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(
60 TextureManager& textureManager, UrlList& urlList, ImageCache::FrameReadyObserver& observer, uint16_t cacheSize, uint16_t batchSize, uint32_t interval)
61 : ImageCache(textureManager, observer, batchSize, interval),
67 RollingImageCache::~RollingImageCache()
72 TextureSet RollingImageCache::Frame(uint32_t frameIndex)
74 // Pop frames until the frame of frameIndex become front frame.
75 bool popExist = false;
76 while(!mQueue.IsEmpty() && mQueue.Front().mUrlIndex != frameIndex)
78 ImageFrame imageFrame = mQueue.PopFront();
79 mTextureManager.Remove(mImageUrls[imageFrame.mUrlIndex].mTextureId, this);
80 mImageUrls[imageFrame.mUrlIndex].mTextureId = TextureManager::INVALID_TEXTURE_ID;
84 // TODO: synchronous loading of first frame.
85 if(popExist || mQueue.IsEmpty())
87 uint32_t batchFrameIndex = frameIndex;
88 // If the frame of frameIndex was already loaded, load batch from the last frame of queue
91 batchFrameIndex = (mQueue.Back().mUrlIndex + 1) % mImageUrls.size();
93 LoadBatch(batchFrameIndex);
96 TextureSet textureSet;
97 if(IsFrontReady() == true && mLoadState != TextureManager::LoadState::LOAD_FAILED)
99 textureSet = GetFrontTextureSet();
105 TextureSet RollingImageCache::FirstFrame()
107 TextureSet textureSet = Frame(FIRST_FRAME_INDEX);
111 uint32_t RollingImageCache::GetFrameInterval(uint32_t frameIndex) const
116 int32_t RollingImageCache::GetCurrentFrameIndex() const
122 return mQueue.Front().mUrlIndex;
125 int32_t RollingImageCache::GetTotalFrameCount() const
127 return mImageUrls.size();
130 bool RollingImageCache::IsFrontReady() const
132 return (!mQueue.IsEmpty() && mQueue.Front().mReady);
135 void RollingImageCache::LoadBatch(uint32_t frameIndex)
137 // Try and load up to mBatchSize images, until the cache is filled.
138 // Once the cache is filled, as frames progress, the old frame is
139 // cleared, but not erased, and another image is loaded
140 for(unsigned int i = 0; i < mBatchSize && !mQueue.IsFull(); ++i)
142 ImageFrame imageFrame;
144 std::string& url = mImageUrls[frameIndex].mUrl;
145 imageFrame.mUrlIndex = frameIndex;
146 imageFrame.mReady = false;
148 mQueue.PushBack(imageFrame);
150 // Note, if the image is already loaded, then LoadComplete will get called
151 // from within this method. This means it won't yet have a texture id, so we
152 // need to account for this inside the LoadComplete method using mRequestingLoad.
153 mRequestingLoad = true;
154 mLoadState = TextureManager::LoadState::LOADING;
156 bool synchronousLoading = false;
157 bool atlasingStatus = false;
158 bool loadingStatus = false;
159 TextureManager::MaskingDataPointer maskInfo = nullptr;
160 AtlasUploadObserver* atlasObserver = nullptr;
161 ImageAtlasManagerPtr imageAtlasManager = nullptr;
163 Dali::ImageDimensions textureRectSize;
164 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
166 TextureManager::TextureId loadTextureId = TextureManager::INVALID_TEXTURE_ID;
167 TextureSet textureSet = mTextureManager.LoadTexture(
168 url, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::BOX_THEN_LINEAR, maskInfo, synchronousLoading, loadTextureId, textureRect, textureRectSize, atlasingStatus, loadingStatus, Dali::WrapMode::Type::DEFAULT, Dali::WrapMode::Type::DEFAULT, this, atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED, preMultiply);
169 mImageUrls[imageFrame.mUrlIndex].mTextureId = loadTextureId;
171 mRequestingLoad = false;
174 frameIndex %= mImageUrls.size();
178 TextureSet RollingImageCache::GetFrontTextureSet() const
180 TextureManager::TextureId textureId = GetCachedTextureId(0);
181 return mTextureManager.GetTextureSet(textureId);
184 TextureManager::TextureId RollingImageCache::GetCachedTextureId(int index) const
186 return mImageUrls[mQueue[index].mUrlIndex].mTextureId;
189 void RollingImageCache::ClearCache()
191 while(mTextureManagerAlive && !mQueue.IsEmpty())
193 ImageFrame imageFrame = mQueue.PopFront();
194 mTextureManager.Remove(mImageUrls[imageFrame.mUrlIndex].mTextureId, this);
195 mImageUrls[imageFrame.mUrlIndex].mTextureId = TextureManager::INVALID_TEXTURE_ID;
197 mLoadState = TextureManager::LoadState::NOT_STARTED;
200 void RollingImageCache::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
202 DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "AnimatedImageVisual::LoadComplete(textureId:%d) start\n", textureInformation.textureId);
207 mLoadState = TextureManager::LoadState::LOAD_FINISHED;
208 bool frontFrameReady = IsFrontReady();
211 for(std::size_t i = 0; i < mQueue.Count(); ++i)
213 if(GetCachedTextureId(i) == textureInformation.textureId)
215 mQueue[i].mReady = true;
222 // LoadComplete has been called from within RequestLoad. TextureManager must
223 // therefore already have the texture cached, so make the texture ready.
224 // (Use the last texture, as the texture id hasn't been assigned yet)
225 mQueue.Back().mReady = true;
228 if(!frontFrameReady && IsFrontReady())
230 mObserver.FrameReady(mTextureManager.GetTextureSet(textureInformation.textureId), mInterval);
235 mLoadState = TextureManager::LoadState::LOAD_FAILED;
236 mObserver.FrameReady(TextureSet(), 0);
242 } //namespace Internal
243 } //namespace Toolkit