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()); \
49 const bool ENABLE_ORIENTATION_CORRECTION( true );
60 RollingImageCache::RollingImageCache(
61 TextureManager& textureManager, UrlList& urlList, ImageCache::FrameReadyObserver& observer,
62 uint16_t cacheSize, uint16_t batchSize )
63 : ImageCache( textureManager, urlList, observer, batchSize ),
69 RollingImageCache::~RollingImageCache()
71 while( !mQueue.IsEmpty() )
73 ImageFrame imageFrame = mQueue.PopFront();
74 mTextureManager.Remove( mImageUrls[ imageFrame.mUrlIndex ].mTextureId );
78 TextureSet RollingImageCache::FirstFrame()
80 TextureSet textureSet = GetFrontTextureSet();
84 mWaitingForReadyFrame = true;
90 TextureSet RollingImageCache::NextFrame()
92 TextureSet textureSet;
94 ImageFrame imageFrame = mQueue.PopFront();
95 mTextureManager.Remove( mImageUrls[ imageFrame.mUrlIndex ].mTextureId );
96 mImageUrls[ imageFrame.mUrlIndex ].mTextureId = TextureManager::INVALID_TEXTURE_ID;
98 if( IsFrontReady() == true )
100 textureSet = GetFrontTextureSet();
104 mWaitingForReadyFrame = true;
112 bool RollingImageCache::IsFrontReady() const
114 return ( !mQueue.IsEmpty() && mQueue.Front().mReady );
117 void RollingImageCache::LoadBatch()
119 // Try and load up to mBatchSize images, until the cache is filled.
120 // Once the cache is filled, as frames progress, the old frame is
121 // cleared, but not erased, and another image is loaded
122 bool frontFrameReady = IsFrontReady();;
124 for( unsigned int i=0; i< mBatchSize && !mQueue.IsFull(); ++i )
126 ImageFrame imageFrame;
128 std::string& url = mImageUrls[ mUrlIndex ].mUrl;
129 imageFrame.mUrlIndex = mUrlIndex;
130 imageFrame.mReady = false;
133 mUrlIndex %= mImageUrls.size();
135 mQueue.PushBack( imageFrame );
137 // Note, if the image is already loaded, then UploadComplete will get called
138 // from within this method. This means it won't yet have a texture id, so we
139 // need to account for this inside the UploadComplete method using mRequestingLoad.
140 mRequestingLoad = true;
142 mImageUrls[ imageFrame.mUrlIndex ].mTextureId =
143 mTextureManager.RequestLoad( url, ImageDimensions(), FittingMode::SCALE_TO_FILL,
144 SamplingMode::BOX_THEN_LINEAR, TextureManager::NO_ATLAS,
145 this, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED );
146 mRequestingLoad = false;
149 CheckFrontFrame( frontFrameReady );
152 void RollingImageCache::SetImageFrameReady( TextureManager::TextureId textureId )
154 for( std::size_t i = 0; i < mQueue.Count() ; ++i )
156 if( GetCachedTextureId(i) == textureId )
158 mQueue[i].mReady = true;
164 TextureSet RollingImageCache::GetFrontTextureSet() const
166 TextureManager::TextureId textureId = GetCachedTextureId( 0 );
167 return mTextureManager.GetTextureSet( textureId );
170 TextureManager::TextureId RollingImageCache::GetCachedTextureId( int index ) const
172 return mImageUrls[ mQueue[ index ].mUrlIndex ].mTextureId;
175 void RollingImageCache::CheckFrontFrame( bool wasReady )
177 if( mWaitingForReadyFrame && wasReady == false && IsFrontReady() )
179 mWaitingForReadyFrame = false;
180 mObserver.FrameReady( GetFrontTextureSet() );
184 void RollingImageCache::UploadComplete(
187 TextureSet textureSet,
189 const Vector4& atlasRect )
191 DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"AnimatedImageVisual::UploadComplete(textureId:%d) start\n", textureId);
194 bool frontFrameReady = IsFrontReady();
196 if( ! mRequestingLoad )
198 SetImageFrameReady( textureId );
200 CheckFrontFrame( frontFrameReady );
204 // UploadComplete has been called from within RequestLoad. TextureManager must
205 // therefore already have the texture cached, so make the texture ready.
206 // (Use the last texture, as the texture id hasn't been assigned yet)
207 mQueue.Back().mReady = true;
213 } //namespace Internal
214 } //namespace Toolkit