2 * Copyright (c) 2020 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
32 const bool ENABLE_ORIENTATION_CORRECTION( true );
35 FixedImageCache::FixedImageCache(
36 TextureManager& textureManager, UrlList& urlList, ImageCache::FrameReadyObserver& observer,
37 unsigned int batchSize )
38 : ImageCache( textureManager, observer, batchSize ),
39 mImageUrls( urlList ),
42 mReadyFlags.reserve( mImageUrls.size() );
46 FixedImageCache::~FixedImageCache()
48 if( mTextureManagerAlive )
50 for( std::size_t i = 0; i < mImageUrls.size() ; ++i )
52 mTextureManager.Remove( mImageUrls[i].mTextureId, this );
57 TextureSet FixedImageCache::Frame( uint32_t frameIndex )
59 while( frameIndex > mFront )
62 if( mFront >= mImageUrls.size() )
71 TextureSet textureSet;
72 if( IsFrontReady() == true )
74 textureSet = GetFrontTextureSet();
78 mWaitingForReadyFrame = true;
84 TextureSet FixedImageCache::FirstFrame()
86 TextureSet textureSet = GetFrontTextureSet();
90 mWaitingForReadyFrame = true;
96 TextureSet FixedImageCache::NextFrame()
98 TextureSet textureSet = Frame((mFront + 1) % mImageUrls.size());
103 uint32_t FixedImageCache::GetFrameInterval( uint32_t frameIndex ) const
108 int32_t FixedImageCache::GetCurrentFrameIndex() const
110 return static_cast<int32_t>(mFront);
113 bool FixedImageCache::IsFrontReady() const
115 return ( mReadyFlags.size() > 0 && mReadyFlags[mFront] == true );
118 void FixedImageCache::LoadBatch()
120 // Try and load up to mBatchSize images, until the cache is filled.
121 // Once the cache is filled, mUrlIndex exceeds mImageUrls size and
122 // no more images are loaded.
123 bool frontFrameReady = IsFrontReady();
125 for( unsigned int i=0; i< mBatchSize && mUrlIndex < mImageUrls.size(); ++i )
127 std::string& url = mImageUrls[ mUrlIndex ].mUrl;
129 mReadyFlags.push_back(false);
131 // Note, if the image is already loaded, then UploadComplete will get called
132 // from within this method. This means it won't yet have a texture id, so we
133 // need to account for this inside the UploadComplete method using mRequestingLoad.
134 mRequestingLoad = true;
136 bool synchronousLoading = false;
137 bool atlasingStatus = false;
138 bool loadingStatus = false;
139 TextureManager::MaskingDataPointer maskInfo = nullptr;
140 AtlasUploadObserver* atlasObserver = nullptr;
141 ImageAtlasManagerPtr imageAtlasManager = nullptr;
143 Dali::ImageDimensions textureRectSize;
144 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
146 mTextureManager.LoadTexture(
147 url, ImageDimensions(), FittingMode::SCALE_TO_FILL,
148 SamplingMode::BOX_THEN_LINEAR, maskInfo,
149 synchronousLoading, mImageUrls[ mUrlIndex ].mTextureId, textureRect, textureRectSize,
150 atlasingStatus, loadingStatus, Dali::WrapMode::Type::DEFAULT,
151 Dali::WrapMode::Type::DEFAULT, this,
152 atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED,
155 if( loadingStatus == false ) // not loading, means it's already ready.
157 SetImageFrameReady( mImageUrls[ mUrlIndex ].mTextureId );
159 mRequestingLoad = false;
163 CheckFrontFrame( frontFrameReady );
166 void FixedImageCache::SetImageFrameReady( TextureManager::TextureId textureId )
168 for( std::size_t i = 0; i < mImageUrls.size() ; ++i )
170 if( mImageUrls[i].mTextureId == textureId )
172 mReadyFlags[i] = true;
178 TextureSet FixedImageCache::GetFrontTextureSet() const
180 return mTextureManager.GetTextureSet( mImageUrls[mFront].mTextureId );
183 void FixedImageCache::CheckFrontFrame( bool wasReady )
185 if( mWaitingForReadyFrame && wasReady == false && IsFrontReady() )
187 mWaitingForReadyFrame = false;
188 mObserver.FrameReady( GetFrontTextureSet() );
192 void FixedImageCache::UploadComplete(
195 TextureSet textureSet,
197 const Vector4& atlasRect,
200 bool frontFrameReady = IsFrontReady();
202 if( ! mRequestingLoad )
204 SetImageFrameReady( textureId );
206 CheckFrontFrame( frontFrameReady );
210 // UploadComplete has been called from within RequestLoad. TextureManager must
211 // therefore already have the texture cached, so make the texture ready.
212 // (Use the last texture, as the texture id hasn't been assigned yet)
213 mReadyFlags.back() = true;
217 void FixedImageCache::LoadComplete(
219 Devel::PixelBuffer pixelBuffer,
220 const VisualUrl& url,
223 // LoadComplete is called if this TextureUploadObserver requested to load
224 // an image that will be returned as a type of PixelBuffer by using a method
225 // TextureManager::LoadPixelBuffer.
228 } //namespace Internal
229 } //namespace Toolkit