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/fixed-image-cache.h>
21 #include <dali-toolkit/internal/visuals/image-atlas-manager.h> // For ImageAtlasManagerPtr
24 #include <dali/integration-api/debug.h>
34 static constexpr bool ENABLE_ORIENTATION_CORRECTION(true);
36 static constexpr uint32_t FIRST_FRAME_INDEX = 0u;
39 FixedImageCache::FixedImageCache(TextureManager& textureManager,
41 Dali::SamplingMode::Type samplingMode,
43 TextureManager::MaskingDataPointer& maskingData,
44 ImageCache::FrameReadyObserver& observer,
47 : ImageCache(textureManager, size, samplingMode, maskingData, observer, batchSize, interval),
49 mFront(FIRST_FRAME_INDEX)
51 mLoadStates.assign(mImageUrls.size(), TextureManager::LoadState::NOT_STARTED);
52 mReadyFlags.reserve(mImageUrls.size());
55 FixedImageCache::~FixedImageCache()
60 TextureSet FixedImageCache::Frame(uint32_t frameIndex)
62 while(frameIndex > mFront || mReadyFlags.empty() ||
63 (frameIndex == FIRST_FRAME_INDEX && mFront != FIRST_FRAME_INDEX))
66 if(mFront >= mImageUrls.size())
75 TextureSet textureSet;
78 textureSet = GetFrontTextureSet();
84 TextureSet FixedImageCache::FirstFrame()
86 TextureSet textureSet = Frame(FIRST_FRAME_INDEX);
91 uint32_t FixedImageCache::GetFrameInterval(uint32_t frameIndex) const
96 int32_t FixedImageCache::GetCurrentFrameIndex() const
98 return static_cast<int32_t>(mFront);
101 int32_t FixedImageCache::GetTotalFrameCount() const
103 return mImageUrls.size();
106 TextureManager::LoadState FixedImageCache::GetLoadState() const
108 return mLoadStates[mFront];
111 bool FixedImageCache::IsFrontReady() const
113 return (mReadyFlags.size() > 0 && mReadyFlags[mFront] == true);
116 void FixedImageCache::LoadBatch()
118 // Try and load up to mBatchSize images, until the cache is filled.
119 // Once the cache is filled, no more images are loaded.
120 for(unsigned int i = 0; i < mBatchSize && mReadyFlags.size() < mImageUrls.size(); ++i)
122 uint32_t frameIndex = mReadyFlags.size();
123 std::string& url = mImageUrls[frameIndex].mUrl;
125 mReadyFlags.push_back(false);
127 // Note, if the image is already loaded, then UploadComplete will get called
128 // from within this method. This means it won't yet have a texture id, so we
129 // need to account for this inside the UploadComplete method using mRequestingLoad.
130 mRequestingLoad = true;
132 bool synchronousLoading = false;
133 bool atlasingStatus = false;
134 bool loadingStatus = false;
135 AtlasUploadObserver* atlasObserver = nullptr;
136 ImageAtlasManagerPtr imageAtlasManager = nullptr;
138 Dali::ImageDimensions textureRectSize;
139 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
141 TextureSet textureSet = mTextureManager.LoadTexture(
144 FittingMode::SCALE_TO_FILL,
148 mImageUrls[frameIndex].mTextureId,
153 Dali::WrapMode::Type::DEFAULT,
154 Dali::WrapMode::Type::DEFAULT,
158 ENABLE_ORIENTATION_CORRECTION,
159 TextureManager::ReloadPolicy::CACHED,
162 // If textureSet is returned but loadingState is false than load state is LOAD_FINISHED. (Notification is not comming yet.)
163 // If textureSet is null and the request is synchronous, load state is LOAD_FAILED.
164 // If textureSet is null but the request is asynchronous, the frame is still loading so load state is LOADING.
165 mLoadStates[frameIndex] = TextureManager::LoadState::LOADING;
170 SetImageFrameReady(mImageUrls[frameIndex].mTextureId, true);
173 else if(synchronousLoading)
175 // Synchronous loading is failed
176 mLoadStates[frameIndex] = TextureManager::LoadState::LOAD_FAILED;
179 mRequestingLoad = false;
183 void FixedImageCache::SetImageFrameReady(TextureManager::TextureId textureId, bool loadSuccess)
185 for(std::size_t i = 0; i < mImageUrls.size(); ++i)
187 if(mImageUrls[i].mTextureId == textureId)
191 mLoadStates[i] = TextureManager::LoadState::LOAD_FINISHED;
195 mLoadStates[i] = TextureManager::LoadState::LOAD_FAILED;
197 mReadyFlags[i] = true;
203 TextureSet FixedImageCache::GetFrontTextureSet() const
205 return mTextureManager.GetTextureSet(mImageUrls[mFront].mTextureId);
208 void FixedImageCache::CheckFrontFrame(bool wasReady)
210 if(wasReady == false && IsFrontReady())
212 mObserver.FrameReady(GetFrontTextureSet(), mInterval);
216 void FixedImageCache::ClearCache()
218 if(mTextureManagerAlive)
220 for(std::size_t i = 0; i < mImageUrls.size(); ++i)
222 mTextureManager.Remove(mImageUrls[i].mTextureId, this);
223 mImageUrls[i].mTextureId = TextureManager::INVALID_TEXTURE_ID;
225 if(mMaskingData && mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID)
227 mTextureManager.Remove(mMaskingData->mAlphaMaskId, this);
232 mLoadStates.assign(mImageUrls.size(), TextureManager::LoadState::NOT_STARTED);
233 mMaskingData->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
236 void FixedImageCache::UploadComplete(
239 TextureSet textureSet,
241 const Vector4& atlasRect,
244 bool frontFrameReady = IsFrontReady();
245 SetImageFrameReady(textureId, loadSuccess);
248 CheckFrontFrame(frontFrameReady);
252 } //namespace Internal
253 } //namespace Toolkit