2 * Copyright (c) 2017 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/integration-api/debug.h>
27 #if defined(DEBUG_ENABLED)
28 Debug::Filter* gAnimImgLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_ANIMATED_IMAGE");
32 std::ostringstream oss; \
33 oss<<"Size:"<<mQueue.Count()<<" [ "; \
34 for(std::size_t _i=0; _i<mQueue.Count(); ++_i) \
37 "={ tex:"<<mImageUrls[mQueue[_i].mUrlIndex].mTextureId<< \
38 " urlId:"<<mQueue[_i].mUrlIndex<< \
39 " rdy:"<<(mQueue[_i].mReady?"T":"F")<< "}, "; \
41 oss<<" ]"<<std::endl; \
42 DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"%s",oss.str().c_str()); \
57 RollingImageCache::RollingImageCache(
58 TextureManager& textureManager, UrlList& urlList, ImageCache::FrameReadyObserver& observer,
59 uint16_t cacheSize, uint16_t batchSize )
60 : ImageCache( textureManager, urlList, observer, batchSize ),
66 RollingImageCache::~RollingImageCache()
68 while( !mQueue.IsEmpty() )
70 ImageFrame imageFrame = mQueue.PopFront();
71 mTextureManager.Remove( mImageUrls[ imageFrame.mUrlIndex ].mTextureId );
75 TextureSet RollingImageCache::FirstFrame()
77 TextureSet textureSet = GetFrontTextureSet();
81 mWaitingForReadyFrame = true;
87 TextureSet RollingImageCache::NextFrame()
89 TextureSet textureSet;
91 ImageFrame imageFrame = mQueue.PopFront();
92 mTextureManager.Remove( mImageUrls[ imageFrame.mUrlIndex ].mTextureId );
93 mImageUrls[ imageFrame.mUrlIndex ].mTextureId = TextureManager::INVALID_TEXTURE_ID;
95 if( IsFrontReady() == true )
97 textureSet = GetFrontTextureSet();
101 mWaitingForReadyFrame = true;
109 bool RollingImageCache::IsFrontReady() const
111 return ( !mQueue.IsEmpty() && mQueue.Front().mReady );
114 void RollingImageCache::LoadBatch()
116 // Try and load up to mBatchSize images, until the cache is filled.
117 // Once the cache is filled, as frames progress, the old frame is
118 // cleared, but not erased, and another image is loaded
119 bool frontFrameReady = IsFrontReady();;
121 for( unsigned int i=0; i< mBatchSize && !mQueue.IsFull(); ++i )
123 ImageFrame imageFrame;
125 std::string& url = mImageUrls[ mUrlIndex ].mUrl;
126 imageFrame.mUrlIndex = mUrlIndex;
127 imageFrame.mReady = false;
130 mUrlIndex %= mImageUrls.size();
132 mQueue.PushBack( imageFrame );
134 // Note, if the image is already loaded, then UploadComplete will get called
135 // from within this method. This means it won't yet have a texture id, so we
136 // need to account for this inside the UploadComplete method using mRequestingLoad.
137 mRequestingLoad = true;
139 mImageUrls[ imageFrame.mUrlIndex ].mTextureId =
140 mTextureManager.RequestLoad( url, ImageDimensions(), FittingMode::SCALE_TO_FILL,
141 SamplingMode::BOX_THEN_LINEAR, TextureManager::NO_ATLAS,
143 mRequestingLoad = false;
146 CheckFrontFrame( frontFrameReady );
149 void RollingImageCache::SetImageFrameReady( TextureManager::TextureId textureId )
151 for( std::size_t i = 0; i < mQueue.Count() ; ++i )
153 if( GetCachedTextureId(i) == textureId )
155 mQueue[i].mReady = true;
161 TextureSet RollingImageCache::GetFrontTextureSet() const
163 TextureManager::TextureId textureId = GetCachedTextureId( 0 );
164 return mTextureManager.GetTextureSet( textureId );
167 TextureManager::TextureId RollingImageCache::GetCachedTextureId( int index ) const
169 return mImageUrls[ mQueue[ index ].mUrlIndex ].mTextureId;
172 void RollingImageCache::CheckFrontFrame( bool wasReady )
174 if( mWaitingForReadyFrame && wasReady == false && IsFrontReady() )
176 mWaitingForReadyFrame = false;
177 mObserver.FrameReady( GetFrontTextureSet() );
181 void RollingImageCache::UploadComplete(
184 TextureSet textureSet,
186 const Vector4& atlasRect )
188 DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"AnimatedImageVisual::UploadComplete(textureId:%d) start\n", textureId);
191 bool frontFrameReady = IsFrontReady();
193 if( ! mRequestingLoad )
195 SetImageFrameReady( textureId );
197 CheckFrontFrame( frontFrameReady );
201 // UploadComplete has been called from within RequestLoad. TextureManager must
202 // therefore already have the texture cached, so make the texture ready.
203 // (Use the last texture, as the texture id hasn't been assigned yet)
204 mQueue.Back().mReady = true;
210 } //namespace Internal
211 } //namespace Toolkit