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 Dali::ImageDimensions textureRectSize;
120 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
122 mTextureManager.LoadTexture(
123 url, ImageDimensions(), FittingMode::SCALE_TO_FILL,
124 SamplingMode::BOX_THEN_LINEAR, maskInfo,
125 synchronousLoading, mImageUrls[ mUrlIndex ].mTextureId, textureRect, textureRectSize,
126 atlasingStatus, loadingStatus, Dali::WrapMode::Type::DEFAULT,
127 Dali::WrapMode::Type::DEFAULT, this,
128 atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED,
131 if( loadingStatus == false ) // not loading, means it's already ready.
133 SetImageFrameReady( mImageUrls[ mUrlIndex ].mTextureId );
135 mRequestingLoad = false;
139 CheckFrontFrame( frontFrameReady );
142 void FixedImageCache::SetImageFrameReady( TextureManager::TextureId textureId )
144 for( std::size_t i = 0; i < mImageUrls.size() ; ++i )
146 if( mImageUrls[i].mTextureId == textureId )
148 mReadyFlags[i] = true;
154 TextureSet FixedImageCache::GetFrontTextureSet() const
156 return mTextureManager.GetTextureSet( mImageUrls[mFront].mTextureId );
159 void FixedImageCache::CheckFrontFrame( bool wasReady )
161 if( mWaitingForReadyFrame && wasReady == false && IsFrontReady() )
163 mWaitingForReadyFrame = false;
164 mObserver.FrameReady( GetFrontTextureSet() );
168 void FixedImageCache::UploadComplete(
171 TextureSet textureSet,
173 const Vector4& atlasRect,
176 bool frontFrameReady = IsFrontReady();
178 if( ! mRequestingLoad )
180 SetImageFrameReady( textureId );
182 CheckFrontFrame( frontFrameReady );
186 // UploadComplete has been called from within RequestLoad. TextureManager must
187 // therefore already have the texture cached, so make the texture ready.
188 // (Use the last texture, as the texture id hasn't been assigned yet)
189 mReadyFlags.back() = true;
193 } //namespace Internal
194 } //namespace Toolkit