Merge "Updated patch coverage script." into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-image / rolling-animated-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 "rolling-animated-image-cache.h"
19
20 // EXTERNAL HEADERS
21
22 // INTERNAL HEADERS
23 #include <dali-toolkit/devel-api/image-loader/texture-manager.h>
24 #include <dali-toolkit/internal/visuals/image-atlas-manager.h> // For ImageAtlasManagerPtr
25 #include <dali/integration-api/debug.h>
26
27 namespace
28 {
29 #if defined(DEBUG_ENABLED)
30 Debug::Filter* gAnimImgLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_ANIMATED_IMAGE");
31
32 #define LOG_CACHE                                                       \
33   {                                                                     \
34     std::ostringstream oss;                                             \
35     oss<<"Size:"<<mQueue.Count()<<" [ ";                                \
36     for(std::size_t _i=0; _i<mQueue.Count(); ++_i)                      \
37     {                                                                   \
38       oss<<_i<<                                                         \
39         "={ frm#: " << mQueue[_i].mFrameNumber <<                        \
40            " tex: " << mImageUrls[mQueue[_i].mFrameNumber].mTextureId<<"}, ";  \
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 RollingAnimatedImageCache::RollingAnimatedImageCache(
62   TextureManager& textureManager, AnimatedImageLoading& animatedImageLoading, uint32_t frameCount, ImageCache::FrameReadyObserver& observer,
63   uint16_t cacheSize, uint16_t batchSize, bool isSynchronousLoading )
64 : ImageCache( textureManager, observer, batchSize ),
65   mAnimatedImageLoading( animatedImageLoading ),
66   mFrameCount( frameCount ),
67   mFrameIndex( 0 ),
68   mCacheSize( cacheSize ),
69   mQueue( cacheSize ),
70   mIsSynchronousLoading( isSynchronousLoading ),
71   mOnLoading( false )
72 {
73   mImageUrls.resize( mFrameCount );
74   mIntervals.assign( mFrameCount, 0 );
75   LoadBatch();
76 }
77
78 RollingAnimatedImageCache::~RollingAnimatedImageCache()
79 {
80   if( mTextureManagerAlive )
81   {
82     while( !mQueue.IsEmpty() )
83     {
84       ImageFrame imageFrame = mQueue.PopFront();
85       mTextureManager.Remove( mImageUrls[ imageFrame.mFrameNumber ].mTextureId, this );
86     }
87   }
88 }
89
90 TextureSet RollingAnimatedImageCache::Frame( uint32_t frameIndex )
91 {
92   bool popExist = false;
93   while( !mQueue.IsEmpty() && mQueue.Front().mFrameNumber != frameIndex )
94   {
95     ImageFrame imageFrame = mQueue.PopFront();
96     mTextureManager.Remove( mImageUrls[ imageFrame.mFrameNumber ].mTextureId, this );
97     mImageUrls[ imageFrame.mFrameNumber ].mTextureId = TextureManager::INVALID_TEXTURE_ID;
98     popExist = true;
99   }
100
101   TextureSet textureSet;
102   // If we need to load new frame that are not stored in queue.
103   // Load the frame synchronously.
104   if( mIsSynchronousLoading && mQueue.IsEmpty() )
105   {
106     bool synchronousLoading = true;
107     textureSet = mTextureManager.LoadAnimatedImageTexture( mAnimatedImageLoading, frameIndex, SamplingMode::BOX_THEN_LINEAR,
108                                                            synchronousLoading, mImageUrls[ frameIndex ].mTextureId, Dali::WrapMode::Type::DEFAULT,
109                                                            Dali::WrapMode::Type::DEFAULT, this );
110     mFrameIndex = ( frameIndex + 1 ) % mFrameCount;
111   }
112
113   if( popExist || mQueue.IsEmpty() )
114   {
115     // If the frame of frameIndex was already loaded, load batch from the last frame of queue
116     if( !mQueue.IsEmpty() )
117     {
118       if(!mLoadWaitingQueue.empty())
119       {
120         mFrameIndex = ( mLoadWaitingQueue.back() + 1 ) % mFrameCount;
121       }
122       else
123       {
124         mFrameIndex = ( mQueue.Back().mFrameNumber + 1 ) % mFrameCount;
125       }
126     }
127     else
128     {
129       mOnLoading = false;
130       // If the request is for the first frame or a jumped frame(JUMP_TO) remove current waiting queue.
131       mLoadWaitingQueue.clear();
132       // If the queue is empty, and the frame of frameIndex is not loaded synchronously. load batch from the frame of frameIndex
133       if( !textureSet )
134       {
135         mFrameIndex = frameIndex;
136       }
137     }
138     LoadBatch();
139   }
140
141   if( !textureSet )
142   {
143     if( IsFrontReady() == true )
144     {
145       textureSet = GetFrontTextureSet();
146     }
147     else
148     {
149       mWaitingForReadyFrame = true;
150     }
151   }
152
153   return textureSet;
154 }
155
156 TextureSet RollingAnimatedImageCache::FirstFrame()
157 {
158   return Frame( 0u );
159 }
160
161 TextureSet RollingAnimatedImageCache::NextFrame()
162 {
163   TextureSet textureSet;
164   if(!mQueue.IsEmpty())
165   {
166     uint32_t frameIndex = mQueue.Front().mFrameNumber;
167     if(IsFrontReady())
168     {
169       frameIndex = (frameIndex + 1) % mFrameCount;
170     }
171     textureSet = Frame(frameIndex);
172   }
173   else
174   {
175     DALI_LOG_ERROR("Cache is empty.");
176   }
177
178   return textureSet;
179 }
180
181 uint32_t RollingAnimatedImageCache::GetFrameInterval( uint32_t frameIndex ) const
182 {
183   return mAnimatedImageLoading.GetFrameInterval( frameIndex );
184 }
185
186 int32_t RollingAnimatedImageCache::GetCurrentFrameIndex() const
187 {
188   if(mQueue.IsEmpty())
189   {
190     return -1;
191   }
192   return mQueue.Front().mFrameNumber;
193 }
194
195 bool RollingAnimatedImageCache::IsFrontReady() const
196 {
197   return ( !mQueue.IsEmpty() && mQueue.Front().mReady );
198 }
199
200 void RollingAnimatedImageCache::RequestFrameLoading( uint32_t frameIndex )
201 {
202   ImageFrame imageFrame;
203   imageFrame.mFrameNumber = frameIndex;
204   imageFrame.mReady       = false;
205
206   mQueue.PushBack(imageFrame);
207
208   mRequestingLoad = true;
209
210   bool synchronousLoading = false;
211   mTextureManager.LoadAnimatedImageTexture( mAnimatedImageLoading, frameIndex, SamplingMode::BOX_THEN_LINEAR,
212                                             synchronousLoading, mImageUrls[ frameIndex ].mTextureId, Dali::WrapMode::Type::DEFAULT,
213                                             Dali::WrapMode::Type::DEFAULT, this );
214
215   mRequestingLoad = false;
216 }
217
218 void RollingAnimatedImageCache::LoadBatch()
219 {
220   // Try and load up to mBatchSize images, until the cache is filled.
221   // Once the cache is filled, as frames progress, the old frame is
222   // removed, and another frame is loaded
223
224   bool frontFrameReady = IsFrontReady();
225   for( unsigned int i=0; i< mBatchSize && mQueue.Count() + mLoadWaitingQueue.size() < static_cast<uint32_t>(mCacheSize) && !mQueue.IsFull(); ++i )
226   {
227     if( !mOnLoading )
228     {
229       mOnLoading = true;
230       RequestFrameLoading( mFrameIndex );
231     }
232     else
233     {
234       mLoadWaitingQueue.push_back( mFrameIndex );
235     }
236
237     mFrameIndex++;
238     mFrameIndex %= mFrameCount;
239   }
240
241   CheckFrontFrame( frontFrameReady );
242
243   LOG_CACHE;
244 }
245
246 void RollingAnimatedImageCache::SetImageFrameReady( TextureManager::TextureId textureId )
247 {
248   for( std::size_t i = 0; i < mQueue.Count() ; ++i )
249   {
250     if( GetCachedTextureId( i ) == textureId )
251     {
252       mQueue[i].mReady = true;
253       break;
254     }
255   }
256 }
257
258 TextureSet RollingAnimatedImageCache::GetFrontTextureSet() const
259 {
260   DALI_LOG_INFO( gAnimImgLogFilter, Debug::Concise, "RollingAnimatedImageCache::GetFrontTextureSet() FrameNumber:%d\n", mQueue[ 0 ].mFrameNumber );
261
262   TextureManager::TextureId textureId = GetCachedTextureId( 0 );
263   return mTextureManager.GetTextureSet( textureId );
264 }
265
266 TextureManager::TextureId RollingAnimatedImageCache::GetCachedTextureId( int index ) const
267 {
268   return mImageUrls[ mQueue[ index ].mFrameNumber ].mTextureId;
269 }
270
271 void RollingAnimatedImageCache::CheckFrontFrame( bool wasReady )
272 {
273   if( mWaitingForReadyFrame && wasReady == false && IsFrontReady() )
274   {
275     mWaitingForReadyFrame = false;
276     mObserver.FrameReady( GetFrontTextureSet() );
277   }
278 }
279
280 void RollingAnimatedImageCache::UploadComplete(
281   bool           loadSuccess,
282   int32_t        textureId,
283   TextureSet     textureSet,
284   bool           useAtlasing,
285   const Vector4& atlasRect,
286   bool           preMultiplied )
287 {
288   DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"AnimatedImageVisual::UploadComplete(textureId:%d) start\n", textureId);
289   LOG_CACHE;
290
291   bool frontFrameReady = IsFrontReady();
292
293   if( !mRequestingLoad )
294   {
295     SetImageFrameReady( textureId );
296
297     CheckFrontFrame( frontFrameReady );
298   }
299   else
300   {
301     // UploadComplete has been called from within RequestLoad. TextureManager must
302     // therefore already have the texture cached, so make the texture ready.
303     // (Use the last texture, as the texture id hasn't been assigned yet)
304     mQueue.Back().mReady = true;
305   }
306
307   mOnLoading = false;
308   // The frames of a single animated image can not be loaded parallelly.
309   // Therefore, a frame is now loading, other orders are waiting.
310   // And, after the frame is loaded, requests load of next order.
311   if( !mLoadWaitingQueue.empty() )
312   {
313     uint32_t loadingIndex = mLoadWaitingQueue.front();
314     mLoadWaitingQueue.erase( mLoadWaitingQueue.begin() );
315     mOnLoading = true;
316     RequestFrameLoading( loadingIndex );
317   }
318
319   LOG_CACHE;
320 }
321
322 void RollingAnimatedImageCache::LoadComplete(
323   bool loadSuccess,
324   Devel::PixelBuffer pixelBuffer,
325   const VisualUrl& url,
326   bool preMultiplied )
327 {
328   // LoadComplete is called if this TextureUploadObserver requested to load
329   // an image that will be returned as a type of PixelBuffer by using a method
330   // TextureManager::LoadPixelBuffer.
331 }
332
333 } //namespace Internal
334 } //namespace Toolkit
335 } //namespace Dali