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 ),
69 mIsSynchronousLoading( isSynchronousLoading ),
72 mImageUrls.resize( mFrameCount );
73 mIntervals.assign( mFrameCount, 0 );
77 RollingAnimatedImageCache::~RollingAnimatedImageCache()
79 if( mTextureManagerAlive )
81 while( !mQueue.IsEmpty() )
83 ImageFrame imageFrame = mQueue.PopFront();
84 mTextureManager.Remove( mImageUrls[ imageFrame.mFrameNumber ].mTextureId, this );
89 TextureSet RollingAnimatedImageCache::Frame( uint32_t frameIndex )
91 bool popExist = false;
92 while( !mQueue.IsEmpty() && mQueue.Front().mFrameNumber != frameIndex )
94 ImageFrame imageFrame = mQueue.PopFront();
95 mTextureManager.Remove( mImageUrls[ imageFrame.mFrameNumber ].mTextureId, this );
96 mImageUrls[ imageFrame.mFrameNumber ].mTextureId = TextureManager::INVALID_TEXTURE_ID;
100 TextureSet textureSet;
101 // If we need to load new frame that are not stored in queue.
102 // Load the frame synchronously.
103 if( mIsSynchronousLoading && mQueue.IsEmpty() )
105 bool synchronousLoading = true;
106 textureSet = mTextureManager.LoadAnimatedImageTexture( mAnimatedImageLoading, frameIndex, SamplingMode::BOX_THEN_LINEAR,
107 synchronousLoading, mImageUrls[ frameIndex ].mTextureId, Dali::WrapMode::Type::DEFAULT,
108 Dali::WrapMode::Type::DEFAULT, this );
109 mFrameIndex = ( frameIndex + 1 ) % mFrameCount;
112 if( popExist || mQueue.IsEmpty() )
114 // If the frame of frameIndex was already loaded, load batch from the last frame of queue
115 if( !mQueue.IsEmpty() )
117 mFrameIndex = ( mQueue.Back().mFrameNumber + 1 ) % mFrameCount;
121 // If the request is for the first frame or a jumped frame(JUMP_TO) remove current waiting queue.
122 mLoadWaitingQueue.clear();
123 // If the queue is empty, and the frame of frameIndex is not loaded synchronously. load batch from the frame of frameIndex
126 mFrameIndex = frameIndex;
134 if( IsFrontReady() == true )
136 textureSet = GetFrontTextureSet();
140 mWaitingForReadyFrame = true;
147 TextureSet RollingAnimatedImageCache::FirstFrame()
152 uint32_t RollingAnimatedImageCache::GetFrameInterval( uint32_t frameIndex )
154 return mAnimatedImageLoading.GetFrameInterval( frameIndex );
157 bool RollingAnimatedImageCache::IsFrontReady() const
159 return ( !mQueue.IsEmpty() && mQueue.Front().mReady );
162 void RollingAnimatedImageCache::RequestFrameLoading( uint32_t frameIndex )
164 mRequestingLoad = true;
166 bool synchronousLoading = false;
167 mTextureManager.LoadAnimatedImageTexture( mAnimatedImageLoading, frameIndex, SamplingMode::BOX_THEN_LINEAR,
168 synchronousLoading, mImageUrls[ frameIndex ].mTextureId, Dali::WrapMode::Type::DEFAULT,
169 Dali::WrapMode::Type::DEFAULT, this );
171 mRequestingLoad = false;
174 void RollingAnimatedImageCache::LoadBatch()
176 // Try and load up to mBatchSize images, until the cache is filled.
177 // Once the cache is filled, as frames progress, the old frame is
178 // removed, and another frame is loaded
180 bool frontFrameReady = IsFrontReady();
181 for( unsigned int i=0; i< mBatchSize && !mQueue.IsFull(); ++i )
183 ImageFrame imageFrame;
184 imageFrame.mFrameNumber = mFrameIndex;
185 imageFrame.mReady = false;
187 mQueue.PushBack( imageFrame );
192 RequestFrameLoading( mFrameIndex );
196 mLoadWaitingQueue.push_back( mFrameIndex );
200 mFrameIndex %= mFrameCount;
203 CheckFrontFrame( frontFrameReady );
208 void RollingAnimatedImageCache::SetImageFrameReady( TextureManager::TextureId textureId )
210 for( std::size_t i = 0; i < mQueue.Count() ; ++i )
212 if( GetCachedTextureId( i ) == textureId )
214 mQueue[i].mReady = true;
220 TextureSet RollingAnimatedImageCache::GetFrontTextureSet() const
222 DALI_LOG_INFO( gAnimImgLogFilter, Debug::Concise, "RollingAnimatedImageCache::GetFrontTextureSet() FrameNumber:%d\n", mQueue[ 0 ].mFrameNumber );
224 TextureManager::TextureId textureId = GetCachedTextureId( 0 );
225 return mTextureManager.GetTextureSet( textureId );
228 TextureManager::TextureId RollingAnimatedImageCache::GetCachedTextureId( int index ) const
230 return mImageUrls[ mQueue[ index ].mFrameNumber ].mTextureId;
233 void RollingAnimatedImageCache::CheckFrontFrame( bool wasReady )
235 if( mWaitingForReadyFrame && wasReady == false && IsFrontReady() )
237 mWaitingForReadyFrame = false;
238 mObserver.FrameReady( GetFrontTextureSet() );
242 void RollingAnimatedImageCache::UploadComplete(
245 TextureSet textureSet,
247 const Vector4& atlasRect,
250 DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"AnimatedImageVisual::UploadComplete(textureId:%d) start\n", textureId);
253 bool frontFrameReady = IsFrontReady();
255 if( !mRequestingLoad )
257 SetImageFrameReady( textureId );
259 CheckFrontFrame( frontFrameReady );
263 // UploadComplete has been called from within RequestLoad. TextureManager must
264 // therefore already have the texture cached, so make the texture ready.
265 // (Use the last texture, as the texture id hasn't been assigned yet)
266 mQueue.Back().mReady = true;
270 // The frames of a single animated image can not be loaded parallelly.
271 // Therefore, a frame is now loading, other orders are waiting.
272 // And, after the frame is loaded, requests load of next order.
273 if( !mLoadWaitingQueue.empty() )
275 uint32_t loadingIndex = mLoadWaitingQueue.front();
276 mLoadWaitingQueue.erase( mLoadWaitingQueue.begin() );
278 RequestFrameLoading( loadingIndex );
284 void RollingAnimatedImageCache::LoadComplete(
286 Devel::PixelBuffer pixelBuffer,
287 const VisualUrl& url,
290 // LoadComplete is called if this TextureUploadObserver requested to load
291 // an image that will be returned as a type of PixelBuffer by using a method
292 // TextureManager::LoadPixelBuffer.
295 } //namespace Internal
296 } //namespace Toolkit