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;
116 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
118 mTextureManager.LoadTexture(
119 url, ImageDimensions(), FittingMode::SCALE_TO_FILL,
120 SamplingMode::BOX_THEN_LINEAR, maskInfo,
121 synchronousLoading, mImageUrls[ mUrlIndex ].mTextureId, textureRect,
122 atlasingStatus, loadingStatus, Dali::WrapMode::Type::DEFAULT,
123 Dali::WrapMode::Type::DEFAULT, this,
124 atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED,
127 if( loadingStatus == false ) // not loading, means it's already ready.
129 SetImageFrameReady( mImageUrls[ mUrlIndex ].mTextureId );
131 mRequestingLoad = false;
135 CheckFrontFrame( frontFrameReady );
138 void FixedImageCache::SetImageFrameReady( TextureManager::TextureId textureId )
140 for( std::size_t i = 0; i < mImageUrls.size() ; ++i )
142 if( mImageUrls[i].mTextureId == textureId )
144 mReadyFlags[i] = true;
150 TextureSet FixedImageCache::GetFrontTextureSet() const
152 return mTextureManager.GetTextureSet( mImageUrls[mFront].mTextureId );
155 void FixedImageCache::CheckFrontFrame( bool wasReady )
157 if( mWaitingForReadyFrame && wasReady == false && IsFrontReady() )
159 mWaitingForReadyFrame = false;
160 mObserver.FrameReady( GetFrontTextureSet() );
164 void FixedImageCache::UploadComplete(
167 TextureSet textureSet,
169 const Vector4& atlasRect,
172 bool frontFrameReady = IsFrontReady();
174 if( ! mRequestingLoad )
176 SetImageFrameReady( textureId );
178 CheckFrontFrame( frontFrameReady );
182 // UploadComplete has been called from within RequestLoad. TextureManager must
183 // therefore already have the texture cached, so make the texture ready.
184 // (Use the last texture, as the texture id hasn't been assigned yet)
185 mReadyFlags.back() = true;
189 } //namespace Internal
190 } //namespace Toolkit