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 constexpr bool ENABLE_ORIENTATION_CORRECTION(true);
35 constexpr uint32_t FIRST_FRAME_INDEX = 0u;
38 FixedImageCache::FixedImageCache(TextureManager& textureManager,
40 TextureManager::MaskingDataPointer& maskingData,
41 ImageCache::FrameReadyObserver& observer,
44 : ImageCache(textureManager, maskingData, observer, batchSize, interval),
46 mFront(FIRST_FRAME_INDEX)
48 mReadyFlags.reserve(mImageUrls.size());
51 FixedImageCache::~FixedImageCache()
56 TextureSet FixedImageCache::Frame(uint32_t frameIndex)
58 TextureSet textureSet;
59 if(frameIndex >= mImageUrls.size())
61 DALI_LOG_ERROR("Wrong frameIndex requested.\n");
65 while(mReadyFlags.size() < mImageUrls.size() &&
66 (frameIndex > mFront || mReadyFlags.empty()))
74 if(IsFrontReady() && mLoadState != TextureManager::LoadState::LOAD_FAILED)
76 textureSet = GetFrontTextureSet();
82 TextureSet FixedImageCache::FirstFrame()
84 TextureSet textureSet = Frame(FIRST_FRAME_INDEX);
89 uint32_t FixedImageCache::GetFrameInterval(uint32_t frameIndex) const
94 int32_t FixedImageCache::GetCurrentFrameIndex() const
96 return static_cast<int32_t>(mFront);
99 int32_t FixedImageCache::GetTotalFrameCount() const
101 return mImageUrls.size();
104 bool FixedImageCache::IsFrontReady() const
106 return (mReadyFlags.size() > 0 && mReadyFlags[mFront] == true);
109 void FixedImageCache::LoadBatch()
111 // Try and load up to mBatchSize images, until the cache is filled.
112 // Once the cache is filled, no more images are loaded.
113 for(unsigned int i = 0; i < mBatchSize && mReadyFlags.size() < mImageUrls.size(); ++i)
115 uint32_t frameIndex = mReadyFlags.size();
116 std::string& url = mImageUrls[frameIndex].mUrl;
118 mReadyFlags.push_back(false);
120 // Note, if the image is already loaded, then LoadComplete will get called
121 // from within this method. This means it won't yet have a texture id, so we
122 // need to account for this inside the LoadComplete method using mRequestingLoad.
123 mRequestingLoad = true;
124 mLoadState = TextureManager::LoadState::LOADING;
126 bool synchronousLoading = false;
127 bool atlasingStatus = false;
128 bool loadingStatus = false;
129 AtlasUploadObserver* atlasObserver = nullptr;
130 ImageAtlasManagerPtr imageAtlasManager = nullptr;
132 Dali::ImageDimensions textureRectSize;
133 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
135 mTextureManager.LoadTexture(
136 url, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::BOX_THEN_LINEAR, mMaskingData, synchronousLoading, mImageUrls[frameIndex].mTextureId, textureRect, textureRectSize, atlasingStatus, loadingStatus, Dali::WrapMode::Type::DEFAULT, Dali::WrapMode::Type::DEFAULT, this, atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED, preMultiply);
138 mRequestingLoad = false;
142 TextureSet FixedImageCache::GetFrontTextureSet() const
144 return mTextureManager.GetTextureSet(mImageUrls[mFront].mTextureId);
147 void FixedImageCache::CheckFrontFrame(bool wasReady)
149 if(wasReady == false && IsFrontReady())
151 mObserver.FrameReady(GetFrontTextureSet(), mInterval);
155 void FixedImageCache::ClearCache()
157 if(mTextureManagerAlive)
159 for(std::size_t i = 0; i < mImageUrls.size(); ++i)
161 mTextureManager.Remove(mImageUrls[i].mTextureId, this);
162 mImageUrls[i].mTextureId = TextureManager::INVALID_TEXTURE_ID;
164 if(mMaskingData && mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID)
166 // In the CPU alpha masking, each frame increases reference count of masking texture.
167 // We should call TextureManager::Remove to decrease reference count when each frame is removed.
168 mTextureManager.Remove(mMaskingData->mAlphaMaskId, this);
173 mLoadState = TextureManager::LoadState::NOT_STARTED;
176 mMaskingData->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
180 void FixedImageCache::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
184 mLoadState = TextureManager::LoadState::LOAD_FINISHED;
185 bool frontFrameReady = IsFrontReady();
188 for(std::size_t i = 0; i < mImageUrls.size(); ++i)
190 if(mImageUrls[i].mTextureId == textureInformation.textureId)
192 mReadyFlags[i] = true;
199 mReadyFlags.back() = true;
201 CheckFrontFrame(frontFrameReady);
205 mLoadState = TextureManager::LoadState::LOAD_FAILED;
206 mObserver.FrameReady(TextureSet(), 0);
210 } //namespace Internal
211 } //namespace Toolkit