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 if( mTextureManagerAlive )
75 while( !mQueue.IsEmpty() )
77 ImageFrame imageFrame = mQueue.PopFront();
78 mTextureManager.Remove( mImageUrls[ imageFrame.mUrlIndex ].mTextureId );
83 TextureSet RollingImageCache::FirstFrame()
85 TextureSet textureSet = GetFrontTextureSet();
89 mWaitingForReadyFrame = true;
95 TextureSet RollingImageCache::NextFrame()
97 TextureSet textureSet;
99 ImageFrame imageFrame = mQueue.PopFront();
100 mTextureManager.Remove( mImageUrls[ imageFrame.mUrlIndex ].mTextureId );
101 mImageUrls[ imageFrame.mUrlIndex ].mTextureId = TextureManager::INVALID_TEXTURE_ID;
103 if( IsFrontReady() == true )
105 textureSet = GetFrontTextureSet();
109 mWaitingForReadyFrame = true;
117 bool RollingImageCache::IsFrontReady() const
119 return ( !mQueue.IsEmpty() && mQueue.Front().mReady );
122 void RollingImageCache::LoadBatch()
124 // Try and load up to mBatchSize images, until the cache is filled.
125 // Once the cache is filled, as frames progress, the old frame is
126 // cleared, but not erased, and another image is loaded
127 bool frontFrameReady = IsFrontReady();;
129 for( unsigned int i=0; i< mBatchSize && !mQueue.IsFull(); ++i )
131 ImageFrame imageFrame;
133 std::string& url = mImageUrls[ mUrlIndex ].mUrl;
134 imageFrame.mUrlIndex = mUrlIndex;
135 imageFrame.mReady = false;
138 mUrlIndex %= mImageUrls.size();
140 mQueue.PushBack( imageFrame );
142 // Note, if the image is already loaded, then UploadComplete will get called
143 // from within this method. This means it won't yet have a texture id, so we
144 // need to account for this inside the UploadComplete method using mRequestingLoad.
145 mRequestingLoad = true;
147 bool synchronousLoading = false;
148 bool atlasingStatus = false;
149 bool loadingStatus = false;
150 TextureManager::MaskingDataPointer maskInfo = nullptr;
151 AtlasUploadObserver* atlasObserver = nullptr;
152 ImageAtlasManagerPtr imageAtlasManager = nullptr;
154 Dali::ImageDimensions textureRectSize;
155 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
157 mTextureManager.LoadTexture(
158 url, ImageDimensions(), FittingMode::SCALE_TO_FILL,
159 SamplingMode::BOX_THEN_LINEAR, maskInfo,
160 synchronousLoading, mImageUrls[ imageFrame.mUrlIndex ].mTextureId, textureRect, textureRectSize,
161 atlasingStatus, loadingStatus, Dali::WrapMode::Type::DEFAULT,
162 Dali::WrapMode::Type::DEFAULT, this,
163 atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED,
166 mRequestingLoad = false;
169 CheckFrontFrame( frontFrameReady );
172 void RollingImageCache::SetImageFrameReady( TextureManager::TextureId textureId )
174 for( std::size_t i = 0; i < mQueue.Count() ; ++i )
176 if( GetCachedTextureId(i) == textureId )
178 mQueue[i].mReady = true;
184 TextureSet RollingImageCache::GetFrontTextureSet() const
186 TextureManager::TextureId textureId = GetCachedTextureId( 0 );
187 return mTextureManager.GetTextureSet( textureId );
190 TextureManager::TextureId RollingImageCache::GetCachedTextureId( int index ) const
192 return mImageUrls[ mQueue[ index ].mUrlIndex ].mTextureId;
195 void RollingImageCache::CheckFrontFrame( bool wasReady )
197 if( mWaitingForReadyFrame && wasReady == false && IsFrontReady() )
199 mWaitingForReadyFrame = false;
200 mObserver.FrameReady( GetFrontTextureSet() );
204 void RollingImageCache::UploadComplete(
207 TextureSet textureSet,
209 const Vector4& atlasRect,
212 DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"AnimatedImageVisual::UploadComplete(textureId:%d) start\n", textureId);
215 bool frontFrameReady = IsFrontReady();
217 if( ! mRequestingLoad )
219 SetImageFrameReady( textureId );
221 CheckFrontFrame( frontFrameReady );
225 // UploadComplete has been called from within RequestLoad. TextureManager must
226 // therefore already have the texture cached, so make the texture ready.
227 // (Use the last texture, as the texture id hasn't been assigned yet)
228 mQueue.Back().mReady = true;
234 } //namespace Internal
235 } //namespace Toolkit