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