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>
21 #include <dali-toolkit/internal/visuals/image-atlas-manager.h> // For ImageAtlasManagerPtr
32 const bool ENABLE_ORIENTATION_CORRECTION( true );
35 FixedImageCache::FixedImageCache(
36 TextureManager& textureManager, UrlList& urlList, ImageCache::FrameReadyObserver& observer,
37 unsigned int batchSize )
38 : ImageCache( textureManager, observer, batchSize ),
39 mImageUrls( urlList ),
42 mReadyFlags.reserve( mImageUrls.size() );
46 FixedImageCache::~FixedImageCache()
48 for( std::size_t i = 0; i < mImageUrls.size() ; ++i )
50 mTextureManager.Remove( mImageUrls[i].mTextureId );
54 TextureSet FixedImageCache::FirstFrame()
56 TextureSet textureSet = GetFrontTextureSet();
60 mWaitingForReadyFrame = true;
66 TextureSet FixedImageCache::NextFrame()
68 TextureSet textureSet;
70 mFront %= mImageUrls.size();
72 if( IsFrontReady() == true )
74 textureSet = GetFrontTextureSet();
78 mWaitingForReadyFrame = true;
86 bool FixedImageCache::IsFrontReady() const
88 return ( mReadyFlags.size() > 0 && mReadyFlags[mFront] == true );
91 void FixedImageCache::LoadBatch()
93 // Try and load up to mBatchSize images, until the cache is filled.
94 // Once the cache is filled, mUrlIndex exceeds mImageUrls size and
95 // no more images are loaded.
96 bool frontFrameReady = IsFrontReady();;
98 for( unsigned int i=0; i< mBatchSize && mUrlIndex < mImageUrls.size(); ++i )
100 std::string& url = mImageUrls[ mUrlIndex ].mUrl;
102 mReadyFlags.push_back(false);
104 // Note, if the image is already loaded, then UploadComplete will get called
105 // from within this method. This means it won't yet have a texture id, so we
106 // need to account for this inside the UploadComplete method using mRequestingLoad.
107 mRequestingLoad = true;
109 bool synchronousLoading = false;
110 bool atlasingStatus = false;
111 bool loadingStatus = false;
112 TextureManager::MaskingDataPointer maskInfo = nullptr;
113 AtlasUploadObserver* atlasObserver = nullptr;
114 ImageAtlasManagerPtr imageAtlasManager = nullptr;
117 mTextureManager.LoadTexture(
118 url, ImageDimensions(), FittingMode::SCALE_TO_FILL,
119 SamplingMode::BOX_THEN_LINEAR, maskInfo,
120 synchronousLoading, mImageUrls[ mUrlIndex ].mTextureId, textureRect,
121 atlasingStatus, loadingStatus, Dali::WrapMode::Type::DEFAULT,
122 Dali::WrapMode::Type::DEFAULT, this,
123 atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED,
124 TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY );
126 if( loadingStatus == false ) // not loading, means it's already ready.
128 SetImageFrameReady( mImageUrls[ mUrlIndex ].mTextureId );
130 mRequestingLoad = false;
134 CheckFrontFrame( frontFrameReady );
137 void FixedImageCache::SetImageFrameReady( TextureManager::TextureId textureId )
139 for( std::size_t i = 0; i < mImageUrls.size() ; ++i )
141 if( mImageUrls[i].mTextureId == textureId )
143 mReadyFlags[i] = true;
149 TextureSet FixedImageCache::GetFrontTextureSet() const
151 return mTextureManager.GetTextureSet( mImageUrls[mFront].mTextureId );
154 void FixedImageCache::CheckFrontFrame( bool wasReady )
156 if( mWaitingForReadyFrame && wasReady == false && IsFrontReady() )
158 mWaitingForReadyFrame = false;
159 mObserver.FrameReady( GetFrontTextureSet() );
163 void FixedImageCache::UploadComplete(
166 TextureSet textureSet,
168 const Vector4& atlasRect )
170 bool frontFrameReady = IsFrontReady();
172 if( ! mRequestingLoad )
174 SetImageFrameReady( textureId );
176 CheckFrontFrame( frontFrameReady );
180 // UploadComplete has been called from within RequestLoad. TextureManager must
181 // therefore already have the texture cached, so make the texture ready.
182 // (Use the last texture, as the texture id hasn't been assigned yet)
183 mReadyFlags.back() = true;
187 } //namespace Internal
188 } //namespace Toolkit