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 bool RollingAnimatedImageCache::IsFrontReady() const
197 return ( !mQueue.IsEmpty() && mQueue.Front().mReady );
200 void RollingAnimatedImageCache::RequestFrameLoading( uint32_t frameIndex )
202 ImageFrame imageFrame;
203 imageFrame.mFrameNumber = frameIndex;
204 imageFrame.mReady = false;
206 mQueue.PushBack(imageFrame);
208 mRequestingLoad = true;
210 bool synchronousLoading = false;
211 mTextureManager.LoadAnimatedImageTexture( mAnimatedImageLoading, frameIndex, SamplingMode::BOX_THEN_LINEAR,
212 synchronousLoading, mImageUrls[ frameIndex ].mTextureId, Dali::WrapMode::Type::DEFAULT,
213 Dali::WrapMode::Type::DEFAULT, this );
215 mRequestingLoad = false;
218 void RollingAnimatedImageCache::LoadBatch()
220 // Try and load up to mBatchSize images, until the cache is filled.
221 // Once the cache is filled, as frames progress, the old frame is
222 // removed, and another frame is loaded
224 bool frontFrameReady = IsFrontReady();
225 for( unsigned int i=0; i< mBatchSize && mQueue.Count() + mLoadWaitingQueue.size() < static_cast<uint32_t>(mCacheSize) && !mQueue.IsFull(); ++i )
230 RequestFrameLoading( mFrameIndex );
234 mLoadWaitingQueue.push_back( mFrameIndex );
238 mFrameIndex %= mFrameCount;
241 CheckFrontFrame( frontFrameReady );
246 void RollingAnimatedImageCache::SetImageFrameReady( TextureManager::TextureId textureId )
248 for( std::size_t i = 0; i < mQueue.Count() ; ++i )
250 if( GetCachedTextureId( i ) == textureId )
252 mQueue[i].mReady = true;
258 TextureSet RollingAnimatedImageCache::GetFrontTextureSet() const
260 DALI_LOG_INFO( gAnimImgLogFilter, Debug::Concise, "RollingAnimatedImageCache::GetFrontTextureSet() FrameNumber:%d\n", mQueue[ 0 ].mFrameNumber );
262 TextureManager::TextureId textureId = GetCachedTextureId( 0 );
263 return mTextureManager.GetTextureSet( textureId );
266 TextureManager::TextureId RollingAnimatedImageCache::GetCachedTextureId( int index ) const
268 return mImageUrls[ mQueue[ index ].mFrameNumber ].mTextureId;
271 void RollingAnimatedImageCache::CheckFrontFrame( bool wasReady )
273 if( mWaitingForReadyFrame && wasReady == false && IsFrontReady() )
275 mWaitingForReadyFrame = false;
276 mObserver.FrameReady( GetFrontTextureSet() );
280 void RollingAnimatedImageCache::UploadComplete(
283 TextureSet textureSet,
285 const Vector4& atlasRect,
288 DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"AnimatedImageVisual::UploadComplete(textureId:%d) start\n", textureId);
291 bool frontFrameReady = IsFrontReady();
293 if( !mRequestingLoad )
295 SetImageFrameReady( textureId );
297 CheckFrontFrame( frontFrameReady );
301 // UploadComplete has been called from within RequestLoad. TextureManager must
302 // therefore already have the texture cached, so make the texture ready.
303 // (Use the last texture, as the texture id hasn't been assigned yet)
304 mQueue.Back().mReady = true;
308 // The frames of a single animated image can not be loaded parallelly.
309 // Therefore, a frame is now loading, other orders are waiting.
310 // And, after the frame is loaded, requests load of next order.
311 if( !mLoadWaitingQueue.empty() )
313 uint32_t loadingIndex = mLoadWaitingQueue.front();
314 mLoadWaitingQueue.erase( mLoadWaitingQueue.begin() );
316 RequestFrameLoading( loadingIndex );
322 void RollingAnimatedImageCache::LoadComplete(
324 Devel::PixelBuffer pixelBuffer,
325 const VisualUrl& url,
328 // LoadComplete is called if this TextureUploadObserver requested to load
329 // an image that will be returned as a type of PixelBuffer by using a method
330 // TextureManager::LoadPixelBuffer.
333 } //namespace Internal
334 } //namespace Toolkit