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-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 while(mReadyFlags.size() < mImageUrls.size() &&
71 (frameIndex > mCurrentFrameIndex || mReadyFlags.empty()))
77 mCurrentFrameIndex = frameIndex;
79 if(IsFrameReady(mCurrentFrameIndex) && mLoadState != TextureManager::LoadState::LOAD_FAILED)
81 textureSet = GetTextureSet(mCurrentFrameIndex);
87 TextureSet FixedImageCache::FirstFrame()
89 TextureSet textureSet = Frame(FIRST_FRAME_INDEX);
94 uint32_t FixedImageCache::GetFrameInterval(uint32_t frameIndex) const
99 int32_t FixedImageCache::GetCurrentFrameIndex() const
101 return static_cast<int32_t>(mCurrentFrameIndex);
104 int32_t FixedImageCache::GetTotalFrameCount() const
106 return mImageUrls.size();
109 bool FixedImageCache::IsFrameReady(uint32_t frameIndex) const
111 return (mReadyFlags.size() > 0 && mReadyFlags[frameIndex] == true);
114 void FixedImageCache::LoadBatch()
116 // Try and load up to mBatchSize images, until the cache is filled.
117 // Once the cache is filled, no more images are loaded.
118 for(unsigned int i = 0; i < mBatchSize && mReadyFlags.size() < mImageUrls.size(); ++i)
120 uint32_t frameIndex = mReadyFlags.size();
121 VisualUrl& url = mImageUrls[frameIndex].mUrl;
123 mReadyFlags.push_back(false);
125 // Note, if the image is already loaded, then LoadComplete will get called
126 // from within this method. This means it won't yet have a texture id, so we
127 // need to account for this inside the LoadComplete method using mRequestingLoad.
128 mRequestingLoad = true;
129 mLoadState = TextureManager::LoadState::LOADING;
131 bool synchronousLoading = false;
132 bool atlasingStatus = false;
133 bool loadingStatus = false;
134 AtlasUploadObserver* atlasObserver = nullptr;
135 ImageAtlasManagerPtr imageAtlasManager = nullptr;
137 Dali::ImageDimensions textureRectSize;
139 auto preMultiplyOnLoading = mPreMultiplyOnLoad ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD
140 : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
142 mTextureManager.LoadTexture(url, mDesiredSize, mFittingMode, mSamplingMode, mMaskingData, synchronousLoading, mImageUrls[frameIndex].mTextureId, textureRect, textureRectSize, atlasingStatus, loadingStatus, this, atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED, preMultiplyOnLoading);
143 mRequestingLoad = false;
147 TextureSet FixedImageCache::GetTextureSet(uint32_t frameIndex) const
149 TextureSet textureSet = mTextureManager.GetTextureSet(mImageUrls[frameIndex].mTextureId);
153 void FixedImageCache::MakeReady(bool wasReady, uint32_t frameIndex, bool preMultiplied)
155 if(wasReady == false && IsFrameReady(frameIndex))
157 mObserver.FrameReady(GetTextureSet(frameIndex), mInterval, preMultiplied);
161 void FixedImageCache::ClearCache()
163 if(Dali::Adaptor::IsAvailable())
165 for(std::size_t i = 0; i < mImageUrls.size(); ++i)
167 mTextureManager.RequestRemove(mImageUrls[i].mTextureId, this);
168 mImageUrls[i].mTextureId = TextureManager::INVALID_TEXTURE_ID;
172 mLoadState = TextureManager::LoadState::NOT_STARTED;
175 mMaskingData->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
179 void FixedImageCache::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
183 mLoadState = TextureManager::LoadState::LOAD_FINISHED;
184 bool isCurrentFrameReady = IsFrameReady(mCurrentFrameIndex);
187 for(std::size_t i = 0; i < mImageUrls.size(); ++i)
189 if(mImageUrls[i].mTextureId == textureInformation.textureId)
191 mReadyFlags[i] = true;
198 mReadyFlags.back() = true;
200 MakeReady(isCurrentFrameReady, mCurrentFrameIndex, textureInformation.preMultiplied);
204 mLoadState = TextureManager::LoadState::LOAD_FAILED;
205 // preMultiplied should be false because broken image don't premultiply alpha on load
206 mObserver.FrameReady(TextureSet(), 0, false);
210 } //namespace Internal
211 } //namespace Toolkit