2 * Copyright (c) 2023 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 constexpr bool ENABLE_ORIENTATION_CORRECTION(true);
35 constexpr uint32_t FIRST_FRAME_INDEX = 0u;
38 FixedImageCache::FixedImageCache(TextureManager& textureManager,
40 Dali::FittingMode::Type fittingMode,
41 Dali::SamplingMode::Type samplingMode,
43 TextureManager::MaskingDataPointer& maskingData,
44 ImageCache::FrameReadyObserver& observer,
47 bool preMultiplyOnLoad)
48 : ImageCache(textureManager, size, fittingMode, samplingMode, maskingData, observer, batchSize, interval, preMultiplyOnLoad),
50 mFront(FIRST_FRAME_INDEX)
52 mReadyFlags.reserve(mImageUrls.size());
55 FixedImageCache::~FixedImageCache()
60 TextureSet FixedImageCache::Frame(uint32_t frameIndex)
62 TextureSet textureSet;
63 if(frameIndex >= mImageUrls.size())
65 DALI_LOG_ERROR("Wrong frameIndex requested.\n");
69 while(mReadyFlags.size() < mImageUrls.size() &&
70 (frameIndex > mFront || mReadyFlags.empty()))
78 if(IsFrontReady() && mLoadState != TextureManager::LoadState::LOAD_FAILED)
80 textureSet = GetFrontTextureSet();
86 TextureSet FixedImageCache::FirstFrame()
88 TextureSet textureSet = Frame(FIRST_FRAME_INDEX);
93 uint32_t FixedImageCache::GetFrameInterval(uint32_t frameIndex) const
98 int32_t FixedImageCache::GetCurrentFrameIndex() const
100 return static_cast<int32_t>(mFront);
103 int32_t FixedImageCache::GetTotalFrameCount() const
105 return mImageUrls.size();
108 bool FixedImageCache::IsFrontReady() const
110 return (mReadyFlags.size() > 0 && mReadyFlags[mFront] == true);
113 void FixedImageCache::LoadBatch()
115 // Try and load up to mBatchSize images, until the cache is filled.
116 // Once the cache is filled, no more images are loaded.
117 for(unsigned int i = 0; i < mBatchSize && mReadyFlags.size() < mImageUrls.size(); ++i)
119 uint32_t frameIndex = mReadyFlags.size();
120 VisualUrl& url = mImageUrls[frameIndex].mUrl;
122 mReadyFlags.push_back(false);
124 // Note, if the image is already loaded, then LoadComplete will get called
125 // from within this method. This means it won't yet have a texture id, so we
126 // need to account for this inside the LoadComplete method using mRequestingLoad.
127 mRequestingLoad = true;
128 mLoadState = TextureManager::LoadState::LOADING;
130 bool synchronousLoading = false;
131 bool atlasingStatus = false;
132 bool loadingStatus = false;
133 AtlasUploadObserver* atlasObserver = nullptr;
134 ImageAtlasManagerPtr imageAtlasManager = nullptr;
136 Dali::ImageDimensions textureRectSize;
138 auto preMultiplyOnLoading = mPreMultiplyOnLoad ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD
139 : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
141 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);
142 mRequestingLoad = false;
146 TextureSet FixedImageCache::GetFrontTextureSet() const
148 TextureSet textureSet = mTextureManager.GetTextureSet(mImageUrls[mFront].mTextureId);
151 Sampler sampler = Sampler::New();
152 sampler.SetWrapMode(Dali::WrapMode::Type::DEFAULT, Dali::WrapMode::Type::DEFAULT);
153 textureSet.SetSampler(0u, sampler);
158 void FixedImageCache::CheckFrontFrame(bool wasReady, bool preMultiplied)
160 if(wasReady == false && IsFrontReady())
162 mObserver.FrameReady(GetFrontTextureSet(), mInterval, preMultiplied);
166 void FixedImageCache::ClearCache()
168 if(mTextureManagerAlive)
170 for(std::size_t i = 0; i < mImageUrls.size(); ++i)
172 mTextureManager.RequestRemove(mImageUrls[i].mTextureId, this);
173 mImageUrls[i].mTextureId = TextureManager::INVALID_TEXTURE_ID;
177 mLoadState = TextureManager::LoadState::NOT_STARTED;
180 mMaskingData->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
184 void FixedImageCache::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
188 mLoadState = TextureManager::LoadState::LOAD_FINISHED;
189 bool frontFrameReady = IsFrontReady();
192 for(std::size_t i = 0; i < mImageUrls.size(); ++i)
194 if(mImageUrls[i].mTextureId == textureInformation.textureId)
196 mReadyFlags[i] = true;
203 mReadyFlags.back() = true;
205 CheckFrontFrame(frontFrameReady, textureInformation.preMultiplied);
209 mLoadState = TextureManager::LoadState::LOAD_FAILED;
210 // preMultiplied should be false because broken image don't premultiply alpha on load
211 mObserver.FrameReady(TextureSet(), 0, false);
215 } //namespace Internal
216 } //namespace Toolkit