1eea6e687db9b2f074c9a35e81202f55ff6d8d83
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-image / fixed-image-cache.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 // CLASS HEADER
18 #include <dali-toolkit/internal/visuals/animated-image/fixed-image-cache.h>
19
20 // INTERNAL HEADERS
21 #include <dali-toolkit/internal/visuals/image-atlas-manager.h> // For ImageAtlasManagerPtr
22
23 namespace Dali
24 {
25 namespace Toolkit
26 {
27 namespace Internal
28 {
29
30 namespace
31 {
32 const bool ENABLE_ORIENTATION_CORRECTION( true );
33 } // namespace
34
35 FixedImageCache::FixedImageCache(
36   TextureManager& textureManager, UrlList& urlList, ImageCache::FrameReadyObserver& observer,
37   unsigned int batchSize )
38 : ImageCache( textureManager, observer, batchSize ),
39   mImageUrls( urlList ),
40   mFront(0u)
41 {
42   mReadyFlags.reserve( mImageUrls.size() );
43   LoadBatch();
44 }
45
46 FixedImageCache::~FixedImageCache()
47 {
48   if( mTextureManagerAlive )
49   {
50     for( std::size_t i = 0; i < mImageUrls.size() ; ++i )
51     {
52       mTextureManager.Remove( mImageUrls[i].mTextureId, this );
53     }
54   }
55 }
56
57 TextureSet FixedImageCache::Frame( uint32_t frameIndex )
58 {
59   while( frameIndex > mFront )
60   {
61     ++mFront;
62     if( mFront >= mImageUrls.size() )
63     {
64       mFront = 0;
65     }
66     LoadBatch();
67   }
68
69   mFront = frameIndex;
70
71   TextureSet textureSet;
72   if( IsFrontReady() == true )
73   {
74     textureSet = GetFrontTextureSet();
75   }
76   else
77   {
78     mWaitingForReadyFrame = true;
79   }
80
81   return textureSet;
82 }
83
84 TextureSet FixedImageCache::FirstFrame()
85 {
86   TextureSet textureSet = GetFrontTextureSet();
87
88   if( ! textureSet )
89   {
90     mWaitingForReadyFrame = true;
91   }
92
93   return textureSet;
94 }
95
96 TextureSet FixedImageCache::NextFrame()
97 {
98   TextureSet textureSet = Frame((mFront + 1) % mImageUrls.size());
99
100   return textureSet;
101 }
102
103 uint32_t FixedImageCache::GetFrameInterval( uint32_t frameIndex ) const
104 {
105   return 0u;
106 }
107
108 int32_t FixedImageCache::GetCurrentFrameIndex() const
109 {
110   return static_cast<int32_t>(mFront);
111 }
112
113 int32_t FixedImageCache::GetTotalFrameCount() const
114 {
115   return mImageUrls.size();
116 }
117
118 bool FixedImageCache::IsFrontReady() const
119 {
120   return ( mReadyFlags.size() > 0 && mReadyFlags[mFront] == true );
121 }
122
123 void FixedImageCache::LoadBatch()
124 {
125   // Try and load up to mBatchSize images, until the cache is filled.
126   // Once the cache is filled, mUrlIndex exceeds mImageUrls size and
127   // no more images are loaded.
128   bool frontFrameReady = IsFrontReady();
129
130   for( unsigned int i=0; i< mBatchSize && mUrlIndex < mImageUrls.size(); ++i )
131   {
132     std::string& url = mImageUrls[ mUrlIndex ].mUrl;
133
134     mReadyFlags.push_back(false);
135
136     // Note, if the image is already loaded, then UploadComplete will get called
137     // from within this method. This means it won't yet have a texture id, so we
138     // need to account for this inside the UploadComplete method using mRequestingLoad.
139     mRequestingLoad = true;
140
141     bool synchronousLoading = false;
142     bool atlasingStatus = false;
143     bool loadingStatus = false;
144     TextureManager::MaskingDataPointer maskInfo = nullptr;
145     AtlasUploadObserver* atlasObserver = nullptr;
146     ImageAtlasManagerPtr imageAtlasManager = nullptr;
147     Vector4 textureRect;
148     Dali::ImageDimensions textureRectSize;
149     auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
150
151     mTextureManager.LoadTexture(
152       url, ImageDimensions(), FittingMode::SCALE_TO_FILL,
153       SamplingMode::BOX_THEN_LINEAR, maskInfo,
154       synchronousLoading, mImageUrls[ mUrlIndex ].mTextureId, textureRect, textureRectSize,
155       atlasingStatus, loadingStatus, Dali::WrapMode::Type::DEFAULT,
156       Dali::WrapMode::Type::DEFAULT, this,
157       atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED,
158       preMultiply );
159
160     if( loadingStatus == false )  // not loading, means it's already ready.
161     {
162       SetImageFrameReady( mImageUrls[ mUrlIndex ].mTextureId );
163     }
164     mRequestingLoad = false;
165     ++mUrlIndex;
166   }
167
168   CheckFrontFrame( frontFrameReady );
169 }
170
171 void FixedImageCache::SetImageFrameReady( TextureManager::TextureId textureId )
172 {
173   for( std::size_t i = 0; i < mImageUrls.size() ; ++i )
174   {
175     if( mImageUrls[i].mTextureId == textureId )
176     {
177       mReadyFlags[i] = true;
178       break;
179     }
180   }
181 }
182
183 TextureSet FixedImageCache::GetFrontTextureSet() const
184 {
185   return mTextureManager.GetTextureSet( mImageUrls[mFront].mTextureId );
186 }
187
188 void FixedImageCache::CheckFrontFrame( bool wasReady )
189 {
190   if( mWaitingForReadyFrame && wasReady == false && IsFrontReady() )
191   {
192     mWaitingForReadyFrame = false;
193     mObserver.FrameReady( GetFrontTextureSet() );
194   }
195 }
196
197 void FixedImageCache::UploadComplete(
198   bool           loadSuccess,
199   int32_t        textureId,
200   TextureSet     textureSet,
201   bool           useAtlasing,
202   const Vector4& atlasRect,
203   bool           preMultiplied)
204 {
205   bool frontFrameReady = IsFrontReady();
206
207   if( ! mRequestingLoad )
208   {
209     SetImageFrameReady( textureId );
210
211     CheckFrontFrame( frontFrameReady );
212   }
213   else
214   {
215     // UploadComplete has been called from within RequestLoad. TextureManager must
216     // therefore already have the texture cached, so make the texture ready.
217     // (Use the last texture, as the texture id hasn't been assigned yet)
218     mReadyFlags.back() = true;
219   }
220 }
221
222 void FixedImageCache::LoadComplete(
223   bool loadSuccess,
224   Devel::PixelBuffer pixelBuffer,
225   const VisualUrl& url,
226   bool preMultiplied )
227 {
228   // LoadComplete is called if this TextureUploadObserver requested to load
229   // an image that will be returned as a type of PixelBuffer by using a method
230   // TextureManager::LoadPixelBuffer.
231 }
232
233 } //namespace Internal
234 } //namespace Toolkit
235 } //namespace Dali