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-toolkit/internal/visuals/image-atlas-manager.h> // For ImageAtlasManagerPtr
22 #include <dali/integration-api/debug.h>
28 #if defined(DEBUG_ENABLED)
29 Debug::Filter* gAnimImgLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_ANIMATED_IMAGE");
33 std::ostringstream oss; \
34 oss<<"Size:"<<mQueue.Count()<<" [ "; \
35 for(std::size_t _i=0; _i<mQueue.Count(); ++_i) \
38 "={ tex:"<<mImageUrls[mQueue[_i].mUrlIndex].mTextureId<< \
39 " urlId:"<<mQueue[_i].mUrlIndex<< \
40 " rdy:"<<(mQueue[_i].mReady?"T":"F")<< "}, "; \
42 oss<<" ]"<<std::endl; \
43 DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"%s",oss.str().c_str()); \
50 const bool ENABLE_ORIENTATION_CORRECTION( true );
61 RollingImageCache::RollingImageCache(
62 TextureManager& textureManager, UrlList& urlList, ImageCache::FrameReadyObserver& observer,
63 uint16_t cacheSize, uint16_t batchSize )
64 : ImageCache( textureManager, observer, batchSize ),
65 mImageUrls( urlList ),
71 RollingImageCache::~RollingImageCache()
73 while( !mQueue.IsEmpty() )
75 ImageFrame imageFrame = mQueue.PopFront();
76 mTextureManager.Remove( mImageUrls[ imageFrame.mUrlIndex ].mTextureId );
80 TextureSet RollingImageCache::FirstFrame()
82 TextureSet textureSet = GetFrontTextureSet();
86 mWaitingForReadyFrame = true;
92 TextureSet RollingImageCache::NextFrame()
94 TextureSet textureSet;
96 ImageFrame imageFrame = mQueue.PopFront();
97 mTextureManager.Remove( mImageUrls[ imageFrame.mUrlIndex ].mTextureId );
98 mImageUrls[ imageFrame.mUrlIndex ].mTextureId = TextureManager::INVALID_TEXTURE_ID;
100 if( IsFrontReady() == true )
102 textureSet = GetFrontTextureSet();
106 mWaitingForReadyFrame = true;
114 bool RollingImageCache::IsFrontReady() const
116 return ( !mQueue.IsEmpty() && mQueue.Front().mReady );
119 void RollingImageCache::LoadBatch()
121 // Try and load up to mBatchSize images, until the cache is filled.
122 // Once the cache is filled, as frames progress, the old frame is
123 // cleared, but not erased, and another image is loaded
124 bool frontFrameReady = IsFrontReady();;
126 for( unsigned int i=0; i< mBatchSize && !mQueue.IsFull(); ++i )
128 ImageFrame imageFrame;
130 std::string& url = mImageUrls[ mUrlIndex ].mUrl;
131 imageFrame.mUrlIndex = mUrlIndex;
132 imageFrame.mReady = false;
135 mUrlIndex %= mImageUrls.size();
137 mQueue.PushBack( imageFrame );
139 // Note, if the image is already loaded, then UploadComplete will get called
140 // from within this method. This means it won't yet have a texture id, so we
141 // need to account for this inside the UploadComplete method using mRequestingLoad.
142 mRequestingLoad = true;
144 bool synchronousLoading = false;
145 bool atlasingStatus = false;
146 bool loadingStatus = false;
147 TextureManager::MaskingDataPointer maskInfo = nullptr;
148 AtlasUploadObserver* atlasObserver = nullptr;
149 ImageAtlasManagerPtr imageAtlasManager = nullptr;
152 mTextureManager.LoadTexture(
153 url, ImageDimensions(), FittingMode::SCALE_TO_FILL,
154 SamplingMode::BOX_THEN_LINEAR, maskInfo,
155 synchronousLoading, mImageUrls[ imageFrame.mUrlIndex ].mTextureId, textureRect,
156 atlasingStatus, loadingStatus, Dali::WrapMode::Type::DEFAULT,
157 Dali::WrapMode::Type::DEFAULT, this,
158 atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED,
159 TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY );
161 mRequestingLoad = false;
164 CheckFrontFrame( frontFrameReady );
167 void RollingImageCache::SetImageFrameReady( TextureManager::TextureId textureId )
169 for( std::size_t i = 0; i < mQueue.Count() ; ++i )
171 if( GetCachedTextureId(i) == textureId )
173 mQueue[i].mReady = true;
179 TextureSet RollingImageCache::GetFrontTextureSet() const
181 TextureManager::TextureId textureId = GetCachedTextureId( 0 );
182 return mTextureManager.GetTextureSet( textureId );
185 TextureManager::TextureId RollingImageCache::GetCachedTextureId( int index ) const
187 return mImageUrls[ mQueue[ index ].mUrlIndex ].mTextureId;
190 void RollingImageCache::CheckFrontFrame( bool wasReady )
192 if( mWaitingForReadyFrame && wasReady == false && IsFrontReady() )
194 mWaitingForReadyFrame = false;
195 mObserver.FrameReady( GetFrontTextureSet() );
199 void RollingImageCache::UploadComplete(
202 TextureSet textureSet,
204 const Vector4& atlasRect )
206 DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"AnimatedImageVisual::UploadComplete(textureId:%d) start\n", textureId);
209 bool frontFrameReady = IsFrontReady();
211 if( ! mRequestingLoad )
213 SetImageFrameReady( textureId );
215 CheckFrontFrame( frontFrameReady );
219 // UploadComplete has been called from within RequestLoad. TextureManager must
220 // therefore already have the texture cached, so make the texture ready.
221 // (Use the last texture, as the texture id hasn't been assigned yet)
222 mQueue.Back().mReady = true;
228 } //namespace Internal
229 } //namespace Toolkit