dee6149c16216cea1ba056366d24df1aaf022767
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-image / rolling-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/rolling-image-cache.h>
19
20 // INTERNAL HEADERS
21 #include <dali-toolkit/internal/visuals/image-atlas-manager.h> // For ImageAtlasManagerPtr
22 #include <dali/integration-api/debug.h>
23
24 // EXTERNAL HEADERS
25
26 namespace
27 {
28 #if defined(DEBUG_ENABLED)
29 Debug::Filter* gAnimImgLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_ANIMATED_IMAGE");
30
31 #define LOG_CACHE                                                       \
32   {                                                                     \
33     std::ostringstream oss;                                             \
34     oss<<"Size:"<<mQueue.Count()<<" [ ";                                \
35     for(std::size_t _i=0; _i<mQueue.Count(); ++_i)                      \
36     {                                                                   \
37       oss<<_i<<                                                         \
38         "={ tex:"<<mImageUrls[mQueue[_i].mUrlIndex].mTextureId<<        \
39         " urlId:"<<mQueue[_i].mUrlIndex<<                               \
40         " rdy:"<<(mQueue[_i].mReady?"T":"F")<< "}, ";                   \
41     }                                                                   \
42     oss<<" ]"<<std::endl;                                               \
43     DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"%s",oss.str().c_str()); \
44   }
45
46 #else
47   #define LOG_CACHE
48 #endif
49
50 const bool ENABLE_ORIENTATION_CORRECTION( true );
51
52 }
53
54 namespace Dali
55 {
56 namespace Toolkit
57 {
58 namespace Internal
59 {
60
61 RollingImageCache::RollingImageCache(
62   TextureManager& textureManager, UrlList& urlList, ImageCache::FrameReadyObserver& observer,
63   uint16_t cacheSize, uint16_t batchSize )
64 : ImageCache( textureManager, observer, batchSize ),
65   mImageUrls( urlList ),
66   mQueue( cacheSize )
67 {
68   LoadBatch();
69 }
70
71 RollingImageCache::~RollingImageCache()
72 {
73   if( mTextureManagerAlive )
74   {
75     while( !mQueue.IsEmpty() )
76     {
77       ImageFrame imageFrame = mQueue.PopFront();
78       mTextureManager.Remove( mImageUrls[ imageFrame.mUrlIndex ].mTextureId, this );
79     }
80   }
81 }
82
83 TextureSet RollingImageCache::Frame( uint32_t frameIndex )
84 {
85   // If a frame of frameIndex is not loaded, clear the queue and remove all loaded textures.
86   if( mImageUrls[ frameIndex ].mTextureId == TextureManager::INVALID_TEXTURE_ID )
87   {
88     mUrlIndex = frameIndex;
89     while( !mQueue.IsEmpty() )
90     {
91       ImageFrame imageFrame = mQueue.PopFront();
92       mTextureManager.Remove( mImageUrls[ imageFrame.mUrlIndex ].mTextureId, this );
93       mImageUrls[ imageFrame.mUrlIndex ].mTextureId = TextureManager::INVALID_TEXTURE_ID;
94     }
95     LoadBatch();
96   }
97   // If the frame is already loaded, remove previous frames of the frame in the queue
98   // and load new frames amount of removed frames.
99   else
100   {
101     bool popExist = false;
102     while( !mQueue.IsEmpty() && mQueue.Front().mUrlIndex != frameIndex )
103     {
104       ImageFrame imageFrame = mQueue.PopFront();
105       mTextureManager.Remove( mImageUrls[ imageFrame.mUrlIndex ].mTextureId, this );
106       mImageUrls[ imageFrame.mUrlIndex ].mTextureId = TextureManager::INVALID_TEXTURE_ID;
107       popExist = true;
108     }
109     if( popExist )
110     {
111       mUrlIndex = ( mQueue.Back().mUrlIndex + 1 ) % mImageUrls.size();
112       LoadBatch();
113     }
114   }
115
116   TextureSet textureSet;
117   if( IsFrontReady() == true )
118   {
119     textureSet = GetFrontTextureSet();
120   }
121   else
122   {
123     mWaitingForReadyFrame = true;
124   }
125
126   return textureSet;
127 }
128
129 TextureSet RollingImageCache::FirstFrame()
130 {
131   return Frame( 0u );
132 }
133
134 TextureSet RollingImageCache::NextFrame()
135 {
136   TextureSet textureSet;
137   if(!mQueue.IsEmpty())
138   {
139     uint32_t frameIndex = mQueue.Front().mUrlIndex;
140     if(IsFrontReady())
141     {
142       frameIndex = (frameIndex + 1) % mImageUrls.size();
143     }
144     textureSet = Frame(frameIndex);
145   }
146   else
147   {
148     DALI_LOG_ERROR("Cache is empty.");
149   }
150
151   return textureSet;
152 }
153
154 uint32_t RollingImageCache::GetFrameInterval( uint32_t frameIndex ) const
155 {
156   return 0u;
157 }
158
159 int32_t RollingImageCache::GetCurrentFrameIndex() const
160 {
161   if(mQueue.IsEmpty())
162   {
163     return -1;
164   }
165   return mQueue.Front().mUrlIndex;
166 }
167
168 int32_t RollingImageCache::GetTotalFrameCount() const
169 {
170   return mImageUrls.size();
171 }
172
173 bool RollingImageCache::IsFrontReady() const
174 {
175   return ( !mQueue.IsEmpty() && mQueue.Front().mReady );
176 }
177
178 void RollingImageCache::LoadBatch()
179 {
180   // Try and load up to mBatchSize images, until the cache is filled.
181   // Once the cache is filled, as frames progress, the old frame is
182   // cleared, but not erased, and another image is loaded
183   bool frontFrameReady = IsFrontReady();
184
185   for( unsigned int i=0; i< mBatchSize && !mQueue.IsFull(); ++i )
186   {
187     ImageFrame imageFrame;
188
189     std::string& url = mImageUrls[ mUrlIndex ].mUrl;
190     imageFrame.mUrlIndex = mUrlIndex;
191     imageFrame.mReady = false;
192
193     ++mUrlIndex;
194     mUrlIndex %= mImageUrls.size();
195
196     mQueue.PushBack( imageFrame );
197
198     // Note, if the image is already loaded, then UploadComplete will get called
199     // from within this method. This means it won't yet have a texture id, so we
200     // need to account for this inside the UploadComplete method using mRequestingLoad.
201     mRequestingLoad = true;
202
203     bool synchronousLoading = false;
204     bool atlasingStatus = false;
205     bool loadingStatus = false;
206     TextureManager::MaskingDataPointer maskInfo = nullptr;
207     AtlasUploadObserver* atlasObserver = nullptr;
208     ImageAtlasManagerPtr imageAtlasManager = nullptr;
209     Vector4 textureRect;
210     Dali::ImageDimensions textureRectSize;
211     auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
212
213     mTextureManager.LoadTexture(
214       url, ImageDimensions(), FittingMode::SCALE_TO_FILL,
215       SamplingMode::BOX_THEN_LINEAR, maskInfo,
216       synchronousLoading, mImageUrls[ imageFrame.mUrlIndex ].mTextureId, textureRect, textureRectSize,
217       atlasingStatus, loadingStatus, Dali::WrapMode::Type::DEFAULT,
218       Dali::WrapMode::Type::DEFAULT, this,
219       atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED,
220       preMultiply );
221
222     mRequestingLoad = false;
223   }
224
225   CheckFrontFrame( frontFrameReady );
226 }
227
228 void RollingImageCache::SetImageFrameReady( TextureManager::TextureId textureId )
229 {
230   for( std::size_t i = 0; i < mQueue.Count() ; ++i )
231   {
232     if( GetCachedTextureId(i) == textureId )
233     {
234       mQueue[i].mReady = true;
235       break;
236     }
237   }
238 }
239
240 TextureSet RollingImageCache::GetFrontTextureSet() const
241 {
242   TextureManager::TextureId textureId = GetCachedTextureId( 0 );
243   return mTextureManager.GetTextureSet( textureId );
244 }
245
246 TextureManager::TextureId RollingImageCache::GetCachedTextureId( int index ) const
247 {
248   return mImageUrls[ mQueue[ index ].mUrlIndex ].mTextureId;
249 }
250
251 void RollingImageCache::CheckFrontFrame( bool wasReady )
252 {
253   if( mWaitingForReadyFrame && wasReady == false && IsFrontReady() )
254   {
255     mWaitingForReadyFrame = false;
256     mObserver.FrameReady( GetFrontTextureSet() );
257   }
258 }
259
260 void RollingImageCache::UploadComplete(
261   bool           loadSuccess,
262   int32_t        textureId,
263   TextureSet     textureSet,
264   bool           useAtlasing,
265   const Vector4& atlasRect,
266   bool           preMultiplied )
267 {
268   DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"AnimatedImageVisual::UploadComplete(textureId:%d) start\n", textureId);
269   LOG_CACHE;
270
271   bool frontFrameReady = IsFrontReady();
272
273   if( ! mRequestingLoad )
274   {
275     SetImageFrameReady( textureId );
276
277     CheckFrontFrame( frontFrameReady );
278   }
279   else
280   {
281     // UploadComplete has been called from within RequestLoad. TextureManager must
282     // therefore already have the texture cached, so make the texture ready.
283     // (Use the last texture, as the texture id hasn't been assigned yet)
284     mQueue.Back().mReady = true;
285   }
286
287   LOG_CACHE;
288 }
289
290 void RollingImageCache::LoadComplete(
291   bool loadSuccess,
292   Devel::PixelBuffer pixelBuffer,
293   const VisualUrl& url,
294   bool preMultiplied )
295 {
296   // LoadComplete is called if this TextureUploadObserver requested to load
297   // an image that will be returned as a type of PixelBuffer by using a method
298   // TextureManager::LoadPixelBuffer.
299 }
300
301 } //namespace Internal
302 } //namespace Toolkit
303 } //namespace Dali