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"
21 #include <dali/devel-api/rendering/texture-devel.h>
24 #include <dali-toolkit/devel-api/image-loader/texture-manager.h>
25 #include <dali-toolkit/internal/visuals/image-atlas-manager.h> // For ImageAtlasManagerPtr
26 #include <dali/integration-api/debug.h>
30 #if defined(DEBUG_ENABLED)
31 Debug::Filter* gAnimImgLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_ANIMATED_IMAGE");
35 std::ostringstream oss; \
36 oss<<"Size:"<<mQueue.Count()<<" [ "; \
37 for(std::size_t _i=0; _i<mQueue.Count(); ++_i) \
40 "={ frm#: " << mQueue[_i].mFrameNumber << \
41 " tex: " << mImageUrls[mQueue[_i].mFrameNumber].mTextureId<<"}, "; \
43 oss<<" ]"<<std::endl; \
44 DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"%s",oss.str().c_str()); \
51 const bool ENABLE_ORIENTATION_CORRECTION( true );
62 RollingAnimatedImageCache::RollingAnimatedImageCache(
63 TextureManager& textureManager, AnimatedImageLoading& animatedImageLoading, uint32_t frameCount, ImageCache::FrameReadyObserver& observer,
64 uint16_t cacheSize, uint16_t batchSize, bool isSynchronousLoading )
65 : ImageCache( textureManager, observer, batchSize ),
66 mAnimatedImageLoading( animatedImageLoading ),
67 mFrameCount( frameCount ),
69 mCacheSize( cacheSize ),
71 mIsSynchronousLoading( isSynchronousLoading ),
74 mImageUrls.resize( mFrameCount );
75 mIntervals.assign( mFrameCount, 0 );
79 RollingAnimatedImageCache::~RollingAnimatedImageCache()
81 if( mTextureManagerAlive )
83 while( !mQueue.IsEmpty() )
85 ImageFrame imageFrame = mQueue.PopFront();
86 mTextureManager.Remove( mImageUrls[ imageFrame.mFrameNumber ].mTextureId, this );
91 TextureSet RollingAnimatedImageCache::Frame( uint32_t frameIndex )
93 if(mQueue.IsFull() && IsFrontReady() == true)
95 TextureSet textureSet = GetFrontTextureSet();
96 if(!Dali::DevelTexture::IsUploaded(textureSet.GetTexture(0)))
102 bool popExist = false;
103 while( !mQueue.IsEmpty() && mQueue.Front().mFrameNumber != frameIndex )
105 ImageFrame imageFrame = mQueue.PopFront();
106 mTextureManager.Remove( mImageUrls[ imageFrame.mFrameNumber ].mTextureId, this );
107 mImageUrls[ imageFrame.mFrameNumber ].mTextureId = TextureManager::INVALID_TEXTURE_ID;
111 TextureSet textureSet;
112 // If we need to load new frame that are not stored in queue.
113 // Load the frame synchronously.
114 if( mIsSynchronousLoading && mQueue.IsEmpty() )
116 bool synchronousLoading = true;
117 textureSet = mTextureManager.LoadAnimatedImageTexture( mAnimatedImageLoading, frameIndex, SamplingMode::BOX_THEN_LINEAR,
118 synchronousLoading, mImageUrls[ frameIndex ].mTextureId, Dali::WrapMode::Type::DEFAULT,
119 Dali::WrapMode::Type::DEFAULT, this );
120 mFrameIndex = ( frameIndex + 1 ) % mFrameCount;
123 if( popExist || mQueue.IsEmpty() )
125 // If the frame of frameIndex was already loaded, load batch from the last frame of queue
126 if( !mQueue.IsEmpty() )
128 if(!mLoadWaitingQueue.empty())
130 mFrameIndex = ( mLoadWaitingQueue.back() + 1 ) % mFrameCount;
134 mFrameIndex = ( mQueue.Back().mFrameNumber + 1 ) % mFrameCount;
140 // If the request is for the first frame or a jumped frame(JUMP_TO) remove current waiting queue.
141 mLoadWaitingQueue.clear();
142 // If the queue is empty, and the frame of frameIndex is not loaded synchronously. load batch from the frame of frameIndex
145 mFrameIndex = frameIndex;
153 if( IsFrontReady() == true )
155 textureSet = GetFrontTextureSet();
159 mWaitingForReadyFrame = true;
166 TextureSet RollingAnimatedImageCache::FirstFrame()
171 TextureSet RollingAnimatedImageCache::NextFrame()
173 TextureSet textureSet;
174 if(!mQueue.IsEmpty())
176 uint32_t frameIndex = mQueue.Front().mFrameNumber;
179 frameIndex = (frameIndex + 1) % mFrameCount;
181 textureSet = Frame(frameIndex);
185 DALI_LOG_ERROR("Cache is empty.");
191 uint32_t RollingAnimatedImageCache::GetFrameInterval( uint32_t frameIndex )
193 return mAnimatedImageLoading.GetFrameInterval( frameIndex );
196 int32_t RollingAnimatedImageCache::GetCurrentFrameIndex()
202 return mQueue.Front().mFrameNumber;
205 bool RollingAnimatedImageCache::IsFrontReady() const
207 return ( !mQueue.IsEmpty() && mQueue.Front().mReady );
210 void RollingAnimatedImageCache::RequestFrameLoading( uint32_t frameIndex )
212 ImageFrame imageFrame;
213 imageFrame.mFrameNumber = frameIndex;
214 imageFrame.mReady = false;
216 mQueue.PushBack(imageFrame);
218 mRequestingLoad = true;
220 bool synchronousLoading = false;
221 mTextureManager.LoadAnimatedImageTexture( mAnimatedImageLoading, frameIndex, SamplingMode::BOX_THEN_LINEAR,
222 synchronousLoading, mImageUrls[ frameIndex ].mTextureId, Dali::WrapMode::Type::DEFAULT,
223 Dali::WrapMode::Type::DEFAULT, this );
225 mRequestingLoad = false;
228 void RollingAnimatedImageCache::LoadBatch()
230 // Try and load up to mBatchSize images, until the cache is filled.
231 // Once the cache is filled, as frames progress, the old frame is
232 // removed, and another frame is loaded
234 bool frontFrameReady = IsFrontReady();
235 for( unsigned int i=0; i< mBatchSize && mQueue.Count() + mLoadWaitingQueue.size() < static_cast<uint32_t>(mCacheSize) && !mQueue.IsFull(); ++i )
240 RequestFrameLoading( mFrameIndex );
244 mLoadWaitingQueue.push_back( mFrameIndex );
248 mFrameIndex %= mFrameCount;
251 CheckFrontFrame( frontFrameReady );
256 void RollingAnimatedImageCache::SetImageFrameReady( TextureManager::TextureId textureId )
258 for( std::size_t i = 0; i < mQueue.Count() ; ++i )
260 if( GetCachedTextureId( i ) == textureId )
262 mQueue[i].mReady = true;
268 TextureSet RollingAnimatedImageCache::GetFrontTextureSet() const
270 DALI_LOG_INFO( gAnimImgLogFilter, Debug::Concise, "RollingAnimatedImageCache::GetFrontTextureSet() FrameNumber:%d\n", mQueue[ 0 ].mFrameNumber );
272 TextureManager::TextureId textureId = GetCachedTextureId( 0 );
273 return mTextureManager.GetTextureSet( textureId );
276 TextureManager::TextureId RollingAnimatedImageCache::GetCachedTextureId( int index ) const
278 return mImageUrls[ mQueue[ index ].mFrameNumber ].mTextureId;
281 void RollingAnimatedImageCache::CheckFrontFrame( bool wasReady )
283 if( mWaitingForReadyFrame && wasReady == false && IsFrontReady() )
285 mWaitingForReadyFrame = false;
286 mObserver.FrameReady( GetFrontTextureSet() );
290 void RollingAnimatedImageCache::UploadComplete(
293 TextureSet textureSet,
295 const Vector4& atlasRect,
298 DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"AnimatedImageVisual::UploadComplete(textureId:%d) start\n", textureId);
301 bool frontFrameReady = IsFrontReady();
303 if( !mRequestingLoad )
305 SetImageFrameReady( textureId );
307 CheckFrontFrame( frontFrameReady );
311 // UploadComplete has been called from within RequestLoad. TextureManager must
312 // therefore already have the texture cached, so make the texture ready.
313 // (Use the last texture, as the texture id hasn't been assigned yet)
314 mQueue.Back().mReady = true;
318 // The frames of a single animated image can not be loaded parallelly.
319 // Therefore, a frame is now loading, other orders are waiting.
320 // And, after the frame is loaded, requests load of next order.
321 if( !mLoadWaitingQueue.empty() )
323 uint32_t loadingIndex = mLoadWaitingQueue.front();
324 mLoadWaitingQueue.erase( mLoadWaitingQueue.begin() );
326 RequestFrameLoading( loadingIndex );
332 void RollingAnimatedImageCache::LoadComplete(
334 Devel::PixelBuffer pixelBuffer,
335 const VisualUrl& url,
338 // LoadComplete is called if this TextureUploadObserver requested to load
339 // an image that will be returned as a type of PixelBuffer by using a method
340 // TextureManager::LoadPixelBuffer.
343 } //namespace Internal
344 } //namespace Toolkit