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(
39 TextureManager& textureManager, UrlList& urlList, ImageCache::FrameReadyObserver& observer, uint32_t batchSize, uint32_t interval)
40 : ImageCache(textureManager, observer, batchSize, interval),
42 mFront(FIRST_FRAME_INDEX)
44 mReadyFlags.reserve(mImageUrls.size());
47 FixedImageCache::~FixedImageCache()
52 TextureSet FixedImageCache::Frame(uint32_t frameIndex)
54 TextureSet textureSet;
55 if(frameIndex >= mImageUrls.size())
57 DALI_LOG_ERROR("Wrong frameIndex requested.\n");
61 while(mReadyFlags.size() < mImageUrls.size() &&
62 (frameIndex > mFront || mReadyFlags.empty()))
70 if(IsFrontReady() && mLoadState != TextureManager::LoadState::LOAD_FAILED)
72 textureSet = GetFrontTextureSet();
78 TextureSet FixedImageCache::FirstFrame()
80 TextureSet textureSet = Frame(FIRST_FRAME_INDEX);
85 uint32_t FixedImageCache::GetFrameInterval(uint32_t frameIndex) const
90 int32_t FixedImageCache::GetCurrentFrameIndex() const
92 return static_cast<int32_t>(mFront);
95 int32_t FixedImageCache::GetTotalFrameCount() const
97 return mImageUrls.size();
100 bool FixedImageCache::IsFrontReady() const
102 return (mReadyFlags.size() > 0 && mReadyFlags[mFront] == true);
105 void FixedImageCache::LoadBatch()
107 // Try and load up to mBatchSize images, until the cache is filled.
108 // Once the cache is filled, no more images are loaded.
109 for(unsigned int i = 0; i < mBatchSize && mReadyFlags.size() < mImageUrls.size(); ++i)
111 uint32_t frameIndex = mReadyFlags.size();
112 std::string& url = mImageUrls[frameIndex].mUrl;
114 mReadyFlags.push_back(false);
116 // Note, if the image is already loaded, then LoadComplete will get called
117 // from within this method. This means it won't yet have a texture id, so we
118 // need to account for this inside the LoadComplete method using mRequestingLoad.
119 mRequestingLoad = true;
120 mLoadState = TextureManager::LoadState::LOADING;
122 bool synchronousLoading = false;
123 bool atlasingStatus = false;
124 bool loadingStatus = false;
125 TextureManager::MaskingDataPointer maskInfo = nullptr;
126 AtlasUploadObserver* atlasObserver = nullptr;
127 ImageAtlasManagerPtr imageAtlasManager = nullptr;
129 Dali::ImageDimensions textureRectSize;
130 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
132 mTextureManager.LoadTexture(
133 url, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::BOX_THEN_LINEAR, maskInfo, 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);
135 mRequestingLoad = false;
139 TextureSet FixedImageCache::GetFrontTextureSet() const
141 return mTextureManager.GetTextureSet(mImageUrls[mFront].mTextureId);
144 void FixedImageCache::CheckFrontFrame(bool wasReady)
146 if(wasReady == false && IsFrontReady())
148 mObserver.FrameReady(GetFrontTextureSet(), mInterval);
152 void FixedImageCache::ClearCache()
154 if(mTextureManagerAlive)
156 for(std::size_t i = 0; i < mImageUrls.size(); ++i)
158 mTextureManager.Remove(mImageUrls[i].mTextureId, this);
159 mImageUrls[i].mTextureId = TextureManager::INVALID_TEXTURE_ID;
163 mLoadState = TextureManager::LoadState::NOT_STARTED;
166 void FixedImageCache::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
170 mLoadState = TextureManager::LoadState::LOAD_FINISHED;
171 bool frontFrameReady = IsFrontReady();
174 for(std::size_t i = 0; i < mImageUrls.size(); ++i)
176 if(mImageUrls[i].mTextureId == textureInformation.textureId)
178 mReadyFlags[i] = true;
185 mReadyFlags.back() = true;
187 CheckFrontFrame(frontFrameReady);
191 mLoadState = TextureManager::LoadState::LOAD_FAILED;
192 mObserver.FrameReady(TextureSet(), 0);
196 } //namespace Internal
197 } //namespace Toolkit