2 * Copyright (c) 2022 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 : ImageCache(textureManager, size, fittingMode, samplingMode, maskingData, observer, batchSize, interval),
49 mFront(FIRST_FRAME_INDEX)
51 mReadyFlags.reserve(mImageUrls.size());
54 FixedImageCache::~FixedImageCache()
59 TextureSet FixedImageCache::Frame(uint32_t frameIndex)
61 TextureSet textureSet;
62 if(frameIndex >= mImageUrls.size())
64 DALI_LOG_ERROR("Wrong frameIndex requested.\n");
68 while(mReadyFlags.size() < mImageUrls.size() &&
69 (frameIndex > mFront || mReadyFlags.empty()))
77 if(IsFrontReady() && mLoadState != TextureManager::LoadState::LOAD_FAILED)
79 textureSet = GetFrontTextureSet();
85 TextureSet FixedImageCache::FirstFrame()
87 TextureSet textureSet = Frame(FIRST_FRAME_INDEX);
92 uint32_t FixedImageCache::GetFrameInterval(uint32_t frameIndex) const
97 int32_t FixedImageCache::GetCurrentFrameIndex() const
99 return static_cast<int32_t>(mFront);
102 int32_t FixedImageCache::GetTotalFrameCount() const
104 return mImageUrls.size();
107 bool FixedImageCache::IsFrontReady() const
109 return (mReadyFlags.size() > 0 && mReadyFlags[mFront] == true);
112 void FixedImageCache::LoadBatch()
114 // Try and load up to mBatchSize images, until the cache is filled.
115 // Once the cache is filled, no more images are loaded.
116 for(unsigned int i = 0; i < mBatchSize && mReadyFlags.size() < mImageUrls.size(); ++i)
118 uint32_t frameIndex = mReadyFlags.size();
119 VisualUrl& url = mImageUrls[frameIndex].mUrl;
121 mReadyFlags.push_back(false);
123 // Note, if the image is already loaded, then LoadComplete will get called
124 // from within this method. This means it won't yet have a texture id, so we
125 // need to account for this inside the LoadComplete method using mRequestingLoad.
126 mRequestingLoad = true;
127 mLoadState = TextureManager::LoadState::LOADING;
129 bool synchronousLoading = false;
130 bool atlasingStatus = false;
131 bool loadingStatus = false;
132 AtlasUploadObserver* atlasObserver = nullptr;
133 ImageAtlasManagerPtr imageAtlasManager = nullptr;
135 Dali::ImageDimensions textureRectSize;
136 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
138 mTextureManager.LoadTexture(url, mDesiredSize, mFittingMode, mSamplingMode, mMaskingData, synchronousLoading, mImageUrls[frameIndex].mTextureId, textureRect, textureRectSize, atlasingStatus, loadingStatus, this, atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED, preMultiply);
139 mRequestingLoad = false;
143 TextureSet FixedImageCache::GetFrontTextureSet() const
145 TextureSet textureSet = mTextureManager.GetTextureSet(mImageUrls[mFront].mTextureId);
148 Sampler sampler = Sampler::New();
149 sampler.SetWrapMode(Dali::WrapMode::Type::DEFAULT, Dali::WrapMode::Type::DEFAULT);
150 textureSet.SetSampler(0u, sampler);
155 void FixedImageCache::CheckFrontFrame(bool wasReady)
157 if(wasReady == false && IsFrontReady())
159 mObserver.FrameReady(GetFrontTextureSet(), mInterval);
163 void FixedImageCache::ClearCache()
165 if(mTextureManagerAlive)
167 for(std::size_t i = 0; i < mImageUrls.size(); ++i)
169 mTextureManager.Remove(mImageUrls[i].mTextureId, this);
170 mImageUrls[i].mTextureId = TextureManager::INVALID_TEXTURE_ID;
174 mLoadState = TextureManager::LoadState::NOT_STARTED;
177 mMaskingData->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
181 void FixedImageCache::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
185 mLoadState = TextureManager::LoadState::LOAD_FINISHED;
186 bool frontFrameReady = IsFrontReady();
189 for(std::size_t i = 0; i < mImageUrls.size(); ++i)
191 if(mImageUrls[i].mTextureId == textureInformation.textureId)
193 mReadyFlags[i] = true;
200 mReadyFlags.back() = true;
202 CheckFrontFrame(frontFrameReady);
206 mLoadState = TextureManager::LoadState::LOAD_FAILED;
207 mObserver.FrameReady(TextureSet(), 0);
211 } //namespace Internal
212 } //namespace Toolkit