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
31 const bool ENABLE_ORIENTATION_CORRECTION(true);
34 FixedImageCache::FixedImageCache(
35 TextureManager& textureManager, UrlList& urlList, ImageCache::FrameReadyObserver& observer, unsigned int batchSize)
36 : ImageCache(textureManager, observer, batchSize),
40 mReadyFlags.reserve(mImageUrls.size());
44 FixedImageCache::~FixedImageCache()
46 if(mTextureManagerAlive)
48 for(std::size_t i = 0; i < mImageUrls.size(); ++i)
50 mTextureManager.Remove(mImageUrls[i].mTextureId, this);
55 TextureSet FixedImageCache::Frame(uint32_t frameIndex)
57 while(frameIndex > mFront)
60 if(mFront >= mImageUrls.size())
69 TextureSet textureSet;
70 if(IsFrontReady() == true)
72 textureSet = GetFrontTextureSet();
76 mWaitingForReadyFrame = true;
82 TextureSet FixedImageCache::FirstFrame()
84 TextureSet textureSet = GetFrontTextureSet();
88 mWaitingForReadyFrame = true;
94 TextureSet FixedImageCache::NextFrame()
96 TextureSet textureSet = Frame((mFront + 1) % mImageUrls.size());
101 uint32_t FixedImageCache::GetFrameInterval(uint32_t frameIndex) const
106 int32_t FixedImageCache::GetCurrentFrameIndex() const
108 return static_cast<int32_t>(mFront);
111 int32_t FixedImageCache::GetTotalFrameCount() const
113 return mImageUrls.size();
116 bool FixedImageCache::IsFrontReady() const
118 return (mReadyFlags.size() > 0 && mReadyFlags[mFront] == true);
121 void FixedImageCache::LoadBatch()
123 // Try and load up to mBatchSize images, until the cache is filled.
124 // Once the cache is filled, mUrlIndex exceeds mImageUrls size and
125 // no more images are loaded.
126 bool frontFrameReady = IsFrontReady();
128 for(unsigned int i = 0; i < mBatchSize && mUrlIndex < mImageUrls.size(); ++i)
130 std::string& url = mImageUrls[mUrlIndex].mUrl;
132 mReadyFlags.push_back(false);
134 // Note, if the image is already loaded, then UploadComplete will get called
135 // from within this method. This means it won't yet have a texture id, so we
136 // need to account for this inside the UploadComplete method using mRequestingLoad.
137 mRequestingLoad = true;
139 bool synchronousLoading = false;
140 bool atlasingStatus = false;
141 bool loadingStatus = false;
142 TextureManager::MaskingDataPointer maskInfo = nullptr;
143 AtlasUploadObserver* atlasObserver = nullptr;
144 ImageAtlasManagerPtr imageAtlasManager = nullptr;
146 Dali::ImageDimensions textureRectSize;
147 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
149 mTextureManager.LoadTexture(
150 url, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::BOX_THEN_LINEAR, maskInfo, synchronousLoading, mImageUrls[mUrlIndex].mTextureId, textureRect, textureRectSize, atlasingStatus, loadingStatus, Dali::WrapMode::Type::DEFAULT, Dali::WrapMode::Type::DEFAULT, this, atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED, preMultiply);
152 if(loadingStatus == false) // not loading, means it's already ready.
154 SetImageFrameReady(mImageUrls[mUrlIndex].mTextureId);
156 mRequestingLoad = false;
160 CheckFrontFrame(frontFrameReady);
163 void FixedImageCache::SetImageFrameReady(TextureManager::TextureId textureId)
165 for(std::size_t i = 0; i < mImageUrls.size(); ++i)
167 if(mImageUrls[i].mTextureId == textureId)
169 mReadyFlags[i] = true;
175 TextureSet FixedImageCache::GetFrontTextureSet() const
177 return mTextureManager.GetTextureSet(mImageUrls[mFront].mTextureId);
180 void FixedImageCache::CheckFrontFrame(bool wasReady)
182 if(mWaitingForReadyFrame && wasReady == false && IsFrontReady())
184 mWaitingForReadyFrame = false;
185 mObserver.FrameReady(GetFrontTextureSet());
189 void FixedImageCache::UploadComplete(
192 TextureSet textureSet,
194 const Vector4& atlasRect,
197 bool frontFrameReady = IsFrontReady();
201 SetImageFrameReady(textureId);
203 CheckFrontFrame(frontFrameReady);
207 // UploadComplete has been called from within RequestLoad. TextureManager must
208 // therefore already have the texture cached, so make the texture ready.
209 // (Use the last texture, as the texture id hasn't been assigned yet)
210 mReadyFlags.back() = true;
214 void FixedImageCache::LoadComplete(
216 Devel::PixelBuffer pixelBuffer,
217 const VisualUrl& url,
220 // LoadComplete is called if this TextureUploadObserver requested to load
221 // an image that will be returned as a type of PixelBuffer by using a method
222 // TextureManager::LoadPixelBuffer.
225 } //namespace Internal
226 } //namespace Toolkit