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 "rolling-animated-image-cache.h"
23 #include <dali-toolkit/devel-api/image-loader/texture-manager.h>
24 #include <dali-toolkit/internal/visuals/image-atlas-manager.h> // For ImageAtlasManagerPtr
25 #include <dali/integration-api/debug.h>
29 #if defined(DEBUG_ENABLED)
30 Debug::Filter* gAnimImgLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_ANIMATED_IMAGE");
34 std::ostringstream oss; \
35 oss<<"Size:"<<mQueue.Count()<<" [ "; \
36 for(std::size_t _i=0; _i<mQueue.Count(); ++_i) \
39 "={ frm#: " << mQueue[_i].mFrameNumber << \
40 " tex: " << mImageUrls[mQueue[_i].mFrameNumber].mTextureId<<"}, "; \
42 oss<<" ]"<<std::endl; \
43 DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"%s",oss.str().c_str()); \
50 const bool ENABLE_ORIENTATION_CORRECTION( true );
61 RollingAnimatedImageCache::RollingAnimatedImageCache(
62 TextureManager& textureManager, AnimatedImageLoading& animatedImageLoading, uint32_t frameCount, ImageCache::FrameReadyObserver& observer,
63 uint16_t cacheSize, uint16_t batchSize, bool isSynchronousLoading )
64 : ImageCache( textureManager, observer, batchSize ),
65 mAnimatedImageLoading( animatedImageLoading ),
66 mFrameCount( frameCount ),
68 mCacheSize( cacheSize ),
70 mIsSynchronousLoading( isSynchronousLoading ),
73 mImageUrls.resize( mFrameCount );
74 mIntervals.assign( mFrameCount, 0 );
78 RollingAnimatedImageCache::~RollingAnimatedImageCache()
80 if( mTextureManagerAlive )
82 while( !mQueue.IsEmpty() )
84 ImageFrame imageFrame = mQueue.PopFront();
85 mTextureManager.Remove( mImageUrls[ imageFrame.mFrameNumber ].mTextureId, this );
90 TextureSet RollingAnimatedImageCache::Frame( uint32_t frameIndex )
92 bool popExist = false;
93 while( !mQueue.IsEmpty() && mQueue.Front().mFrameNumber != frameIndex )
95 ImageFrame imageFrame = mQueue.PopFront();
96 mTextureManager.Remove( mImageUrls[ imageFrame.mFrameNumber ].mTextureId, this );
97 mImageUrls[ imageFrame.mFrameNumber ].mTextureId = TextureManager::INVALID_TEXTURE_ID;
101 TextureSet textureSet;
102 // If we need to load new frame that are not stored in queue.
103 // Load the frame synchronously.
104 if( mIsSynchronousLoading && mQueue.IsEmpty() )
106 bool synchronousLoading = true;
107 textureSet = mTextureManager.LoadAnimatedImageTexture( mAnimatedImageLoading, frameIndex, SamplingMode::BOX_THEN_LINEAR,
108 synchronousLoading, mImageUrls[ frameIndex ].mTextureId, Dali::WrapMode::Type::DEFAULT,
109 Dali::WrapMode::Type::DEFAULT, this );
110 mFrameIndex = ( frameIndex + 1 ) % mFrameCount;
113 if( popExist || mQueue.IsEmpty() )
115 // If the frame of frameIndex was already loaded, load batch from the last frame of queue
116 if( !mQueue.IsEmpty() )
118 if(!mLoadWaitingQueue.empty())
120 mFrameIndex = ( mLoadWaitingQueue.back() + 1 ) % mFrameCount;
124 mFrameIndex = ( mQueue.Back().mFrameNumber + 1 ) % mFrameCount;
130 // If the request is for the first frame or a jumped frame(JUMP_TO) remove current waiting queue.
131 mLoadWaitingQueue.clear();
132 // If the queue is empty, and the frame of frameIndex is not loaded synchronously. load batch from the frame of frameIndex
135 mFrameIndex = frameIndex;
143 if( IsFrontReady() == true )
145 textureSet = GetFrontTextureSet();
149 mWaitingForReadyFrame = true;
156 TextureSet RollingAnimatedImageCache::FirstFrame()
161 TextureSet RollingAnimatedImageCache::NextFrame()
163 TextureSet textureSet;
164 if(!mQueue.IsEmpty())
166 uint32_t frameIndex = mQueue.Front().mFrameNumber;
169 frameIndex = (frameIndex + 1) % mFrameCount;
171 textureSet = Frame(frameIndex);
175 DALI_LOG_ERROR("Cache is empty.");
181 uint32_t RollingAnimatedImageCache::GetFrameInterval( uint32_t frameIndex ) const
183 return mAnimatedImageLoading.GetFrameInterval( frameIndex );
186 int32_t RollingAnimatedImageCache::GetCurrentFrameIndex() const
192 return mQueue.Front().mFrameNumber;
195 int32_t RollingAnimatedImageCache::GetTotalFrameCount() const
200 bool RollingAnimatedImageCache::IsFrontReady() const
202 return ( !mQueue.IsEmpty() && mQueue.Front().mReady );
205 void RollingAnimatedImageCache::RequestFrameLoading( uint32_t frameIndex )
207 ImageFrame imageFrame;
208 imageFrame.mFrameNumber = frameIndex;
209 imageFrame.mReady = false;
211 mQueue.PushBack(imageFrame);
213 mRequestingLoad = true;
215 bool synchronousLoading = false;
216 mTextureManager.LoadAnimatedImageTexture( mAnimatedImageLoading, frameIndex, SamplingMode::BOX_THEN_LINEAR,
217 synchronousLoading, mImageUrls[ frameIndex ].mTextureId, Dali::WrapMode::Type::DEFAULT,
218 Dali::WrapMode::Type::DEFAULT, this );
220 mRequestingLoad = false;
223 void RollingAnimatedImageCache::LoadBatch()
225 // Try and load up to mBatchSize images, until the cache is filled.
226 // Once the cache is filled, as frames progress, the old frame is
227 // removed, and another frame is loaded
229 bool frontFrameReady = IsFrontReady();
230 for( unsigned int i=0; i< mBatchSize && mQueue.Count() + mLoadWaitingQueue.size() < static_cast<uint32_t>(mCacheSize) && !mQueue.IsFull(); ++i )
235 RequestFrameLoading( mFrameIndex );
239 mLoadWaitingQueue.push_back( mFrameIndex );
243 mFrameIndex %= mFrameCount;
246 CheckFrontFrame( frontFrameReady );
251 void RollingAnimatedImageCache::SetImageFrameReady( TextureManager::TextureId textureId )
253 for( std::size_t i = 0; i < mQueue.Count() ; ++i )
255 if( GetCachedTextureId( i ) == textureId )
257 mQueue[i].mReady = true;
263 TextureSet RollingAnimatedImageCache::GetFrontTextureSet() const
265 DALI_LOG_INFO( gAnimImgLogFilter, Debug::Concise, "RollingAnimatedImageCache::GetFrontTextureSet() FrameNumber:%d\n", mQueue[ 0 ].mFrameNumber );
267 TextureManager::TextureId textureId = GetCachedTextureId( 0 );
268 return mTextureManager.GetTextureSet( textureId );
271 TextureManager::TextureId RollingAnimatedImageCache::GetCachedTextureId( int index ) const
273 return mImageUrls[ mQueue[ index ].mFrameNumber ].mTextureId;
276 void RollingAnimatedImageCache::CheckFrontFrame( bool wasReady )
278 if( mWaitingForReadyFrame && wasReady == false && IsFrontReady() )
280 mWaitingForReadyFrame = false;
281 mObserver.FrameReady( GetFrontTextureSet() );
285 void RollingAnimatedImageCache::UploadComplete(
288 TextureSet textureSet,
290 const Vector4& atlasRect,
293 DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"AnimatedImageVisual::UploadComplete(textureId:%d) start\n", textureId);
296 bool frontFrameReady = IsFrontReady();
298 if( !mRequestingLoad )
300 SetImageFrameReady( textureId );
302 CheckFrontFrame( frontFrameReady );
306 // UploadComplete has been called from within RequestLoad. TextureManager must
307 // therefore already have the texture cached, so make the texture ready.
308 // (Use the last texture, as the texture id hasn't been assigned yet)
309 mQueue.Back().mReady = true;
313 // The frames of a single animated image can not be loaded parallelly.
314 // Therefore, a frame is now loading, other orders are waiting.
315 // And, after the frame is loaded, requests load of next order.
316 if( !mLoadWaitingQueue.empty() )
318 uint32_t loadingIndex = mLoadWaitingQueue.front();
319 mLoadWaitingQueue.erase( mLoadWaitingQueue.begin() );
321 RequestFrameLoading( loadingIndex );
327 void RollingAnimatedImageCache::LoadComplete(
329 Devel::PixelBuffer pixelBuffer,
330 const VisualUrl& url,
333 // LoadComplete is called if this TextureUploadObserver requested to load
334 // an image that will be returned as a type of PixelBuffer by using a method
335 // TextureManager::LoadPixelBuffer.
338 } //namespace Internal
339 } //namespace Toolkit