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/rolling-image-cache.h>
21 #include <dali-toolkit/internal/visuals/image-atlas-manager.h> // For ImageAtlasManagerPtr
22 #include <dali/integration-api/debug.h>
28 #if defined(DEBUG_ENABLED)
29 Debug::Filter* gAnimImgLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_ANIMATED_IMAGE");
33 std::ostringstream oss; \
34 oss<<"Size:"<<mQueue.Count()<<" [ "; \
35 for(std::size_t _i=0; _i<mQueue.Count(); ++_i) \
38 "={ tex:"<<mImageUrls[mQueue[_i].mUrlIndex].mTextureId<< \
39 " urlId:"<<mQueue[_i].mUrlIndex<< \
40 " rdy:"<<(mQueue[_i].mReady?"T":"F")<< "}, "; \
42 oss<<" ]"<<std::endl; \
43 DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"%s",oss.str().c_str()); \
50 const bool ENABLE_ORIENTATION_CORRECTION( true );
61 RollingImageCache::RollingImageCache(
62 TextureManager& textureManager, UrlList& urlList, ImageCache::FrameReadyObserver& observer,
63 uint16_t cacheSize, uint16_t batchSize )
64 : ImageCache( textureManager, observer, batchSize ),
65 mImageUrls( urlList ),
71 RollingImageCache::~RollingImageCache()
73 if( mTextureManagerAlive )
75 while( !mQueue.IsEmpty() )
77 ImageFrame imageFrame = mQueue.PopFront();
78 mTextureManager.Remove( mImageUrls[ imageFrame.mUrlIndex ].mTextureId, this );
83 TextureSet RollingImageCache::Frame( uint32_t frameIndex )
85 // If a frame of frameIndex is not loaded, clear the queue and remove all loaded textures.
86 if( mImageUrls[ frameIndex ].mTextureId == TextureManager::INVALID_TEXTURE_ID )
88 mUrlIndex = frameIndex;
89 while( !mQueue.IsEmpty() )
91 ImageFrame imageFrame = mQueue.PopFront();
92 mTextureManager.Remove( mImageUrls[ imageFrame.mUrlIndex ].mTextureId, this );
93 mImageUrls[ imageFrame.mUrlIndex ].mTextureId = TextureManager::INVALID_TEXTURE_ID;
97 // If the frame is already loaded, remove previous frames of the frame in the queue
98 // and load new frames amount of removed frames.
101 bool popExist = false;
102 while( !mQueue.IsEmpty() && mQueue.Front().mUrlIndex != frameIndex )
104 ImageFrame imageFrame = mQueue.PopFront();
105 mTextureManager.Remove( mImageUrls[ imageFrame.mUrlIndex ].mTextureId, this );
106 mImageUrls[ imageFrame.mUrlIndex ].mTextureId = TextureManager::INVALID_TEXTURE_ID;
111 mUrlIndex = ( mQueue.Back().mUrlIndex + 1 ) % mImageUrls.size();
116 TextureSet textureSet;
117 if( IsFrontReady() == true )
119 textureSet = GetFrontTextureSet();
123 mWaitingForReadyFrame = true;
129 TextureSet RollingImageCache::FirstFrame()
134 TextureSet RollingImageCache::NextFrame()
136 TextureSet textureSet;
137 if(!mQueue.IsEmpty())
139 uint32_t frameIndex = mQueue.Front().mUrlIndex;
142 frameIndex = (frameIndex + 1) % mImageUrls.size();
144 textureSet = Frame(frameIndex);
148 DALI_LOG_ERROR("Cache is empty.");
154 uint32_t RollingImageCache::GetFrameInterval( uint32_t frameIndex ) const
159 int32_t RollingImageCache::GetCurrentFrameIndex() const
165 return mQueue.Front().mUrlIndex;
168 bool RollingImageCache::IsFrontReady() const
170 return ( !mQueue.IsEmpty() && mQueue.Front().mReady );
173 void RollingImageCache::LoadBatch()
175 // Try and load up to mBatchSize images, until the cache is filled.
176 // Once the cache is filled, as frames progress, the old frame is
177 // cleared, but not erased, and another image is loaded
178 bool frontFrameReady = IsFrontReady();
180 for( unsigned int i=0; i< mBatchSize && !mQueue.IsFull(); ++i )
182 ImageFrame imageFrame;
184 std::string& url = mImageUrls[ mUrlIndex ].mUrl;
185 imageFrame.mUrlIndex = mUrlIndex;
186 imageFrame.mReady = false;
189 mUrlIndex %= mImageUrls.size();
191 mQueue.PushBack( imageFrame );
193 // Note, if the image is already loaded, then UploadComplete will get called
194 // from within this method. This means it won't yet have a texture id, so we
195 // need to account for this inside the UploadComplete method using mRequestingLoad.
196 mRequestingLoad = true;
198 bool synchronousLoading = false;
199 bool atlasingStatus = false;
200 bool loadingStatus = false;
201 TextureManager::MaskingDataPointer maskInfo = nullptr;
202 AtlasUploadObserver* atlasObserver = nullptr;
203 ImageAtlasManagerPtr imageAtlasManager = nullptr;
205 Dali::ImageDimensions textureRectSize;
206 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
208 mTextureManager.LoadTexture(
209 url, ImageDimensions(), FittingMode::SCALE_TO_FILL,
210 SamplingMode::BOX_THEN_LINEAR, maskInfo,
211 synchronousLoading, mImageUrls[ imageFrame.mUrlIndex ].mTextureId, textureRect, textureRectSize,
212 atlasingStatus, loadingStatus, Dali::WrapMode::Type::DEFAULT,
213 Dali::WrapMode::Type::DEFAULT, this,
214 atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED,
217 mRequestingLoad = false;
220 CheckFrontFrame( frontFrameReady );
223 void RollingImageCache::SetImageFrameReady( TextureManager::TextureId textureId )
225 for( std::size_t i = 0; i < mQueue.Count() ; ++i )
227 if( GetCachedTextureId(i) == textureId )
229 mQueue[i].mReady = true;
235 TextureSet RollingImageCache::GetFrontTextureSet() const
237 TextureManager::TextureId textureId = GetCachedTextureId( 0 );
238 return mTextureManager.GetTextureSet( textureId );
241 TextureManager::TextureId RollingImageCache::GetCachedTextureId( int index ) const
243 return mImageUrls[ mQueue[ index ].mUrlIndex ].mTextureId;
246 void RollingImageCache::CheckFrontFrame( bool wasReady )
248 if( mWaitingForReadyFrame && wasReady == false && IsFrontReady() )
250 mWaitingForReadyFrame = false;
251 mObserver.FrameReady( GetFrontTextureSet() );
255 void RollingImageCache::UploadComplete(
258 TextureSet textureSet,
260 const Vector4& atlasRect,
263 DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"AnimatedImageVisual::UploadComplete(textureId:%d) start\n", textureId);
266 bool frontFrameReady = IsFrontReady();
268 if( ! mRequestingLoad )
270 SetImageFrameReady( textureId );
272 CheckFrontFrame( frontFrameReady );
276 // UploadComplete has been called from within RequestLoad. TextureManager must
277 // therefore already have the texture cached, so make the texture ready.
278 // (Use the last texture, as the texture id hasn't been assigned yet)
279 mQueue.Back().mReady = true;
285 void RollingImageCache::LoadComplete(
287 Devel::PixelBuffer pixelBuffer,
288 const VisualUrl& url,
291 // LoadComplete is called if this TextureUploadObserver requested to load
292 // an image that will be returned as a type of PixelBuffer by using a method
293 // TextureManager::LoadPixelBuffer.
296 } //namespace Internal
297 } //namespace Toolkit