2 * Copyright (c) 2024 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/image-atlas-manager.h> // For ImageAtlasManagerPtr
24 #include <dali/integration-api/adaptor-framework/adaptor.h>
25 #include <dali/integration-api/debug.h>
35 constexpr bool ENABLE_ORIENTATION_CORRECTION(true);
36 constexpr uint32_t FIRST_FRAME_INDEX = 0u;
39 FixedImageCache::FixedImageCache(TextureManager& textureManager,
41 Dali::FittingMode::Type fittingMode,
42 Dali::SamplingMode::Type samplingMode,
44 TextureManager::MaskingDataPointer& maskingData,
45 ImageCache::FrameReadyObserver& observer,
48 bool preMultiplyOnLoad)
49 : ImageCache(textureManager, size, fittingMode, samplingMode, maskingData, observer, batchSize, interval, preMultiplyOnLoad),
51 mCurrentFrameIndex(FIRST_FRAME_INDEX)
53 mReadyFlags.reserve(mImageUrls.size());
56 FixedImageCache::~FixedImageCache()
61 TextureSet FixedImageCache::Frame(uint32_t frameIndex)
63 TextureSet textureSet;
64 if(frameIndex >= mImageUrls.size())
66 DALI_LOG_ERROR("Wrong frameIndex requested.\n");
70 mCurrentFrameIndex = frameIndex;
72 bool batchRequested = false;
74 // Make ensure that current frameIndex load requested.
75 while(mReadyFlags.size() <= frameIndex)
77 batchRequested = true;
81 // Request batch only 1 times for this function.
82 if(!batchRequested && mReadyFlags.size() < mImageUrls.size())
87 if(IsFrameReady(mCurrentFrameIndex) && mLoadState != TextureManager::LoadState::LOAD_FAILED)
89 textureSet = GetTextureSet(mCurrentFrameIndex);
95 TextureSet FixedImageCache::FirstFrame()
97 TextureSet textureSet = Frame(FIRST_FRAME_INDEX);
102 uint32_t FixedImageCache::GetFrameInterval(uint32_t frameIndex) const
107 int32_t FixedImageCache::GetCurrentFrameIndex() const
109 return static_cast<int32_t>(mCurrentFrameIndex);
112 int32_t FixedImageCache::GetTotalFrameCount() const
114 return mImageUrls.size();
117 bool FixedImageCache::IsFrameReady(uint32_t frameIndex) const
119 return ((mReadyFlags.size() > 0) && (mReadyFlags[frameIndex] == true));
122 void FixedImageCache::LoadBatch()
124 // Try and load up to mBatchSize images, until the cache is filled.
125 // Once the cache is filled, no more images are loaded.
126 for(unsigned int i = 0; i < mBatchSize && mReadyFlags.size() < mImageUrls.size(); ++i)
128 uint32_t frameIndex = mReadyFlags.size();
129 VisualUrl& url = mImageUrls[frameIndex].mUrl;
131 mReadyFlags.push_back(false);
133 // Note, if the image is already loaded, then LoadComplete will get called
134 // from within this method. This means it won't yet have a texture id, so we
135 // need to account for this inside the LoadComplete method using mRequestingLoad.
136 mRequestingLoad = true;
137 mLoadState = TextureManager::LoadState::LOADING;
139 bool synchronousLoading = false;
140 bool atlasingStatus = false;
141 bool loadingStatus = false;
142 AtlasUploadObserver* atlasObserver = nullptr;
143 ImageAtlasManagerPtr imageAtlasManager = nullptr;
145 Dali::ImageDimensions textureRectSize;
147 auto preMultiplyOnLoading = mPreMultiplyOnLoad ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD
148 : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
150 TextureManager::TextureId loadTextureId = TextureManager::INVALID_TEXTURE_ID;
151 mTextureManager.LoadTexture(url, mDesiredSize, mFittingMode, mSamplingMode, mMaskingData, synchronousLoading, loadTextureId, textureRect, textureRectSize, atlasingStatus, loadingStatus, this, atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED, preMultiplyOnLoading);
152 mImageUrls[frameIndex].mTextureId = loadTextureId;
153 mRequestingLoad = false;
157 TextureSet FixedImageCache::GetTextureSet(uint32_t frameIndex) const
159 TextureSet textureSet = mTextureManager.GetTextureSet(mImageUrls[frameIndex].mTextureId);
163 void FixedImageCache::MakeReady(bool wasReady, uint32_t frameIndex, bool preMultiplied)
165 if(wasReady == false && IsFrameReady(frameIndex))
167 mObserver.FrameReady(GetTextureSet(frameIndex), mInterval, preMultiplied);
171 void FixedImageCache::ClearCache()
173 if(Dali::Adaptor::IsAvailable())
175 for(std::size_t i = 0; i < mImageUrls.size(); ++i)
177 mTextureManager.RequestRemove(mImageUrls[i].mTextureId, this);
178 mImageUrls[i].mTextureId = TextureManager::INVALID_TEXTURE_ID;
182 mLoadState = TextureManager::LoadState::NOT_STARTED;
185 mMaskingData->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
189 void FixedImageCache::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
193 mLoadState = TextureManager::LoadState::LOAD_FINISHED;
194 bool wasCurrentFrameReady = IsFrameReady(mCurrentFrameIndex);
197 for(std::size_t i = 0; i < mImageUrls.size(); ++i)
199 if(mImageUrls[i].mTextureId == textureInformation.textureId)
201 mReadyFlags[i] = true;
208 DALI_ASSERT_ALWAYS(mReadyFlags.size() > 0u && "Some FixedImageCache::LoadBatch() called mismatched!");
209 size_t i = mReadyFlags.size() - 1u;
211 // texture id might not setup yet. Update it now.
212 mImageUrls[i].mTextureId = textureInformation.textureId;
213 mReadyFlags[i] = true;
215 MakeReady(wasCurrentFrameReady, mCurrentFrameIndex, textureInformation.preMultiplied);
219 mLoadState = TextureManager::LoadState::LOAD_FAILED;
220 // preMultiplied should be false because broken image don't premultiply alpha on load
221 mObserver.FrameReady(TextureSet(), 0, false);
225 } //namespace Internal
226 } //namespace Toolkit