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 if( mTextureManagerAlive )
50 for( std::size_t i = 0; i < mImageUrls.size() ; ++i )
52 mTextureManager.Remove( mImageUrls[i].mTextureId );
57 TextureSet FixedImageCache::FirstFrame()
59 TextureSet textureSet = GetFrontTextureSet();
63 mWaitingForReadyFrame = true;
69 TextureSet FixedImageCache::NextFrame()
71 TextureSet textureSet;
73 mFront %= mImageUrls.size();
75 if( IsFrontReady() == true )
77 textureSet = GetFrontTextureSet();
81 mWaitingForReadyFrame = true;
89 bool FixedImageCache::IsFrontReady() const
91 return ( mReadyFlags.size() > 0 && mReadyFlags[mFront] == true );
94 void FixedImageCache::LoadBatch()
96 // Try and load up to mBatchSize images, until the cache is filled.
97 // Once the cache is filled, mUrlIndex exceeds mImageUrls size and
98 // no more images are loaded.
99 bool frontFrameReady = IsFrontReady();;
101 for( unsigned int i=0; i< mBatchSize && mUrlIndex < mImageUrls.size(); ++i )
103 std::string& url = mImageUrls[ mUrlIndex ].mUrl;
105 mReadyFlags.push_back(false);
107 // Note, if the image is already loaded, then UploadComplete will get called
108 // from within this method. This means it won't yet have a texture id, so we
109 // need to account for this inside the UploadComplete method using mRequestingLoad.
110 mRequestingLoad = true;
112 bool synchronousLoading = false;
113 bool atlasingStatus = false;
114 bool loadingStatus = false;
115 TextureManager::MaskingDataPointer maskInfo = nullptr;
116 AtlasUploadObserver* atlasObserver = nullptr;
117 ImageAtlasManagerPtr imageAtlasManager = nullptr;
119 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
121 mTextureManager.LoadTexture(
122 url, ImageDimensions(), FittingMode::SCALE_TO_FILL,
123 SamplingMode::BOX_THEN_LINEAR, maskInfo,
124 synchronousLoading, mImageUrls[ mUrlIndex ].mTextureId, textureRect,
125 atlasingStatus, loadingStatus, Dali::WrapMode::Type::DEFAULT,
126 Dali::WrapMode::Type::DEFAULT, this,
127 atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED,
130 if( loadingStatus == false ) // not loading, means it's already ready.
132 SetImageFrameReady( mImageUrls[ mUrlIndex ].mTextureId );
134 mRequestingLoad = false;
138 CheckFrontFrame( frontFrameReady );
141 void FixedImageCache::SetImageFrameReady( TextureManager::TextureId textureId )
143 for( std::size_t i = 0; i < mImageUrls.size() ; ++i )
145 if( mImageUrls[i].mTextureId == textureId )
147 mReadyFlags[i] = true;
153 TextureSet FixedImageCache::GetFrontTextureSet() const
155 return mTextureManager.GetTextureSet( mImageUrls[mFront].mTextureId );
158 void FixedImageCache::CheckFrontFrame( bool wasReady )
160 if( mWaitingForReadyFrame && wasReady == false && IsFrontReady() )
162 mWaitingForReadyFrame = false;
163 mObserver.FrameReady( GetFrontTextureSet() );
167 void FixedImageCache::UploadComplete(
170 TextureSet textureSet,
172 const Vector4& atlasRect,
175 bool frontFrameReady = IsFrontReady();
177 if( ! mRequestingLoad )
179 SetImageFrameReady( textureId );
181 CheckFrontFrame( frontFrameReady );
185 // UploadComplete has been called from within RequestLoad. TextureManager must
186 // therefore already have the texture cached, so make the texture ready.
187 // (Use the last texture, as the texture id hasn't been assigned yet)
188 mReadyFlags.back() = true;
192 } //namespace Internal
193 } //namespace Toolkit