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/fixed-image-cache.h>
27 FixedImageCache::FixedImageCache(
28 TextureManager& textureManager, UrlList& urlList, ImageCache::FrameReadyObserver& observer,
29 unsigned int batchSize )
30 : ImageCache( textureManager, urlList, observer, batchSize ),
33 mReadyFlags.reserve( mImageUrls.size() );
37 FixedImageCache::~FixedImageCache()
39 for( std::size_t i = 0; i < mImageUrls.size() ; ++i )
41 mTextureManager.Remove( mImageUrls[i].mTextureId );
45 TextureSet FixedImageCache::FirstFrame()
47 TextureSet textureSet = GetFrontTextureSet();
51 mWaitingForReadyFrame = true;
57 TextureSet FixedImageCache::NextFrame()
59 TextureSet textureSet;
61 mFront %= mImageUrls.size();
63 if( IsFrontReady() == true )
65 textureSet = GetFrontTextureSet();
69 mWaitingForReadyFrame = true;
77 bool FixedImageCache::IsFrontReady() const
79 return ( mReadyFlags.size() > 0 && mReadyFlags[mFront] == true );
82 void FixedImageCache::LoadBatch()
84 // Try and load up to mBatchSize images, until the cache is filled.
85 // Once the cache is filled, mUrlIndex exceeds mImageUrls size and
86 // no more images are loaded.
87 bool frontFrameReady = IsFrontReady();;
89 for( unsigned int i=0; i< mBatchSize && mUrlIndex < mImageUrls.size(); ++i )
91 std::string& url = mImageUrls[ mUrlIndex ].mUrl;
93 mReadyFlags.push_back(false);
95 // Note, if the image is already loaded, then UploadComplete will get called
96 // from within this method. This means it won't yet have a texture id, so we
97 // need to account for this inside the UploadComplete method using mRequestingLoad.
98 mRequestingLoad = true;
100 mImageUrls[ mUrlIndex ].mTextureId =
101 mTextureManager.RequestLoad( url, ImageDimensions(), FittingMode::SCALE_TO_FILL,
102 SamplingMode::BOX_THEN_LINEAR, TextureManager::NO_ATLAS,
104 mRequestingLoad = false;
108 CheckFrontFrame( frontFrameReady );
111 void FixedImageCache::SetImageFrameReady( TextureManager::TextureId textureId )
113 for( std::size_t i = 0; i < mImageUrls.size() ; ++i )
115 if( mImageUrls[i].mTextureId == textureId )
117 mReadyFlags[i] = true;
123 TextureSet FixedImageCache::GetFrontTextureSet() const
125 return mTextureManager.GetTextureSet( mImageUrls[mFront].mTextureId );
128 void FixedImageCache::CheckFrontFrame( bool wasReady )
130 if( mWaitingForReadyFrame && wasReady == false && IsFrontReady() )
132 mWaitingForReadyFrame = false;
133 mObserver.FrameReady( GetFrontTextureSet() );
137 void FixedImageCache::UploadComplete(
140 TextureSet textureSet,
142 const Vector4& atlasRect )
144 bool frontFrameReady = IsFrontReady();
146 if( ! mRequestingLoad )
148 SetImageFrameReady( textureId );
150 CheckFrontFrame( frontFrameReady );
154 // UploadComplete has been called from within RequestLoad. TextureManager must
155 // therefore already have the texture cached, so make the texture ready.
156 // (Use the last texture, as the texture id hasn't been assigned yet)
157 mReadyFlags.back() = true;
161 } //namespace Internal
162 } //namespace Toolkit