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 uint32_t FixedImageCache::GetFrameInterval( uint32_t frameIndex )
101 bool FixedImageCache::IsFrontReady() const
103 return ( mReadyFlags.size() > 0 && mReadyFlags[mFront] == true );
106 void FixedImageCache::LoadBatch()
108 // Try and load up to mBatchSize images, until the cache is filled.
109 // Once the cache is filled, mUrlIndex exceeds mImageUrls size and
110 // no more images are loaded.
111 bool frontFrameReady = IsFrontReady();
113 for( unsigned int i=0; i< mBatchSize && mUrlIndex < mImageUrls.size(); ++i )
115 std::string& url = mImageUrls[ mUrlIndex ].mUrl;
117 mReadyFlags.push_back(false);
119 // Note, if the image is already loaded, then UploadComplete will get called
120 // from within this method. This means it won't yet have a texture id, so we
121 // need to account for this inside the UploadComplete method using mRequestingLoad.
122 mRequestingLoad = true;
124 bool synchronousLoading = false;
125 bool atlasingStatus = false;
126 bool loadingStatus = false;
127 TextureManager::MaskingDataPointer maskInfo = nullptr;
128 AtlasUploadObserver* atlasObserver = nullptr;
129 ImageAtlasManagerPtr imageAtlasManager = nullptr;
131 Dali::ImageDimensions textureRectSize;
132 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
134 mTextureManager.LoadTexture(
135 url, ImageDimensions(), FittingMode::SCALE_TO_FILL,
136 SamplingMode::BOX_THEN_LINEAR, maskInfo,
137 synchronousLoading, mImageUrls[ mUrlIndex ].mTextureId, textureRect, textureRectSize,
138 atlasingStatus, loadingStatus, Dali::WrapMode::Type::DEFAULT,
139 Dali::WrapMode::Type::DEFAULT, this,
140 atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED,
143 if( loadingStatus == false ) // not loading, means it's already ready.
145 SetImageFrameReady( mImageUrls[ mUrlIndex ].mTextureId );
147 mRequestingLoad = false;
151 CheckFrontFrame( frontFrameReady );
154 void FixedImageCache::SetImageFrameReady( TextureManager::TextureId textureId )
156 for( std::size_t i = 0; i < mImageUrls.size() ; ++i )
158 if( mImageUrls[i].mTextureId == textureId )
160 mReadyFlags[i] = true;
166 TextureSet FixedImageCache::GetFrontTextureSet() const
168 return mTextureManager.GetTextureSet( mImageUrls[mFront].mTextureId );
171 void FixedImageCache::CheckFrontFrame( bool wasReady )
173 if( mWaitingForReadyFrame && wasReady == false && IsFrontReady() )
175 mWaitingForReadyFrame = false;
176 mObserver.FrameReady( GetFrontTextureSet() );
180 void FixedImageCache::UploadComplete(
183 TextureSet textureSet,
185 const Vector4& atlasRect,
188 bool frontFrameReady = IsFrontReady();
190 if( ! mRequestingLoad )
192 SetImageFrameReady( textureId );
194 CheckFrontFrame( frontFrameReady );
198 // UploadComplete has been called from within RequestLoad. TextureManager must
199 // therefore already have the texture cached, so make the texture ready.
200 // (Use the last texture, as the texture id hasn't been assigned yet)
201 mReadyFlags.back() = true;
205 void FixedImageCache::LoadComplete(
207 Devel::PixelBuffer pixelBuffer,
208 const VisualUrl& url,
211 // LoadComplete is called if this TextureUploadObserver requested to load
212 // an image that will be returned as a type of PixelBuffer by using a method
213 // TextureManager::LoadPixelBuffer.
216 } //namespace Internal
217 } //namespace Toolkit