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>
29 const bool ENABLE_ORIENTATION_CORRECTION( true );
32 FixedImageCache::FixedImageCache(
33 TextureManager& textureManager, UrlList& urlList, ImageCache::FrameReadyObserver& observer,
34 unsigned int batchSize )
35 : ImageCache( textureManager, urlList, observer, batchSize ),
38 mReadyFlags.reserve( mImageUrls.size() );
42 FixedImageCache::~FixedImageCache()
44 for( std::size_t i = 0; i < mImageUrls.size() ; ++i )
46 mTextureManager.Remove( mImageUrls[i].mTextureId );
50 TextureSet FixedImageCache::FirstFrame()
52 TextureSet textureSet = GetFrontTextureSet();
56 mWaitingForReadyFrame = true;
62 TextureSet FixedImageCache::NextFrame()
64 TextureSet textureSet;
66 mFront %= mImageUrls.size();
68 if( IsFrontReady() == true )
70 textureSet = GetFrontTextureSet();
74 mWaitingForReadyFrame = true;
82 bool FixedImageCache::IsFrontReady() const
84 return ( mReadyFlags.size() > 0 && mReadyFlags[mFront] == true );
87 void FixedImageCache::LoadBatch()
89 // Try and load up to mBatchSize images, until the cache is filled.
90 // Once the cache is filled, mUrlIndex exceeds mImageUrls size and
91 // no more images are loaded.
92 bool frontFrameReady = IsFrontReady();;
94 for( unsigned int i=0; i< mBatchSize && mUrlIndex < mImageUrls.size(); ++i )
96 std::string& url = mImageUrls[ mUrlIndex ].mUrl;
98 mReadyFlags.push_back(false);
100 // Note, if the image is already loaded, then UploadComplete will get called
101 // from within this method. This means it won't yet have a texture id, so we
102 // need to account for this inside the UploadComplete method using mRequestingLoad.
103 mRequestingLoad = true;
105 mImageUrls[ mUrlIndex ].mTextureId =
106 mTextureManager.RequestLoad( url, ImageDimensions(), FittingMode::SCALE_TO_FILL,
107 SamplingMode::BOX_THEN_LINEAR, TextureManager::NO_ATLAS,
108 this, ENABLE_ORIENTATION_CORRECTION );
109 mRequestingLoad = false;
113 CheckFrontFrame( frontFrameReady );
116 void FixedImageCache::SetImageFrameReady( TextureManager::TextureId textureId )
118 for( std::size_t i = 0; i < mImageUrls.size() ; ++i )
120 if( mImageUrls[i].mTextureId == textureId )
122 mReadyFlags[i] = true;
128 TextureSet FixedImageCache::GetFrontTextureSet() const
130 return mTextureManager.GetTextureSet( mImageUrls[mFront].mTextureId );
133 void FixedImageCache::CheckFrontFrame( bool wasReady )
135 if( mWaitingForReadyFrame && wasReady == false && IsFrontReady() )
137 mWaitingForReadyFrame = false;
138 mObserver.FrameReady( GetFrontTextureSet() );
142 void FixedImageCache::UploadComplete(
145 TextureSet textureSet,
147 const Vector4& atlasRect )
149 bool frontFrameReady = IsFrontReady();
151 if( ! mRequestingLoad )
153 SetImageFrameReady( textureId );
155 CheckFrontFrame( frontFrameReady );
159 // UploadComplete has been called from within RequestLoad. TextureManager must
160 // therefore already have the texture cached, so make the texture ready.
161 // (Use the last texture, as the texture id hasn't been assigned yet)
162 mReadyFlags.back() = true;
166 } //namespace Internal
167 } //namespace Toolkit