Merge "Divide Render() into small apis" 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   mQueue( cacheSize ),
69   mIsSynchronousLoading( isSynchronousLoading ),
70   mOnLoading( false )
71 {
72   mImageUrls.resize( mFrameCount );
73   mIntervals.assign( mFrameCount, 0 );
74   LoadBatch();
75 }
76
77 RollingAnimatedImageCache::~RollingAnimatedImageCache()
78 {
79   if( mTextureManagerAlive )
80   {
81     while( !mQueue.IsEmpty() )
82     {
83       ImageFrame imageFrame = mQueue.PopFront();
84       mTextureManager.Remove( mImageUrls[ imageFrame.mFrameNumber ].mTextureId, this );
85     }
86   }
87 }
88
89 TextureSet RollingAnimatedImageCache::Frame( uint32_t frameIndex )
90 {
91   bool popExist = false;
92   while( !mQueue.IsEmpty() && mQueue.Front().mFrameNumber != frameIndex )
93   {
94     ImageFrame imageFrame = mQueue.PopFront();
95     mTextureManager.Remove( mImageUrls[ imageFrame.mFrameNumber ].mTextureId, this );
96     mImageUrls[ imageFrame.mFrameNumber ].mTextureId = TextureManager::INVALID_TEXTURE_ID;
97     popExist = true;
98   }
99
100   TextureSet textureSet;
101   // If we need to load new frame that are not stored in queue.
102   // Load the frame synchronously.
103   if( mIsSynchronousLoading && mQueue.IsEmpty() )
104   {
105     bool synchronousLoading = true;
106     textureSet = mTextureManager.LoadAnimatedImageTexture( mAnimatedImageLoading, frameIndex, SamplingMode::BOX_THEN_LINEAR,
107                                                            synchronousLoading, mImageUrls[ frameIndex ].mTextureId, Dali::WrapMode::Type::DEFAULT,
108                                                            Dali::WrapMode::Type::DEFAULT, this );
109     mFrameIndex = ( frameIndex + 1 ) % mFrameCount;
110   }
111
112   if( popExist || mQueue.IsEmpty() )
113   {
114     // If the frame of frameIndex was already loaded, load batch from the last frame of queue
115     if( !mQueue.IsEmpty() )
116     {
117       mFrameIndex = ( mQueue.Back().mFrameNumber + 1 ) % mFrameCount;
118     }
119     else
120     {
121       // If the request is for the first frame or a jumped frame(JUMP_TO) remove current waiting queue.
122       mLoadWaitingQueue.clear();
123       // If the queue is empty, and the frame of frameIndex is not loaded synchronously. load batch from the frame of frameIndex
124       if( !textureSet )
125       {
126         mFrameIndex = frameIndex;
127       }
128     }
129     LoadBatch();
130   }
131
132   if( !textureSet )
133   {
134     if( IsFrontReady() == true )
135     {
136       textureSet = GetFrontTextureSet();
137     }
138     else
139     {
140       mWaitingForReadyFrame = true;
141     }
142   }
143
144   return textureSet;
145 }
146
147 TextureSet RollingAnimatedImageCache::FirstFrame()
148 {
149   return Frame( 0u );
150 }
151
152 uint32_t RollingAnimatedImageCache::GetFrameInterval( uint32_t frameIndex )
153 {
154   return mAnimatedImageLoading.GetFrameInterval( frameIndex );
155 }
156
157 bool RollingAnimatedImageCache::IsFrontReady() const
158 {
159   return ( !mQueue.IsEmpty() && mQueue.Front().mReady );
160 }
161
162 void RollingAnimatedImageCache::RequestFrameLoading( uint32_t frameIndex )
163 {
164   mRequestingLoad = true;
165
166   bool synchronousLoading = false;
167   mTextureManager.LoadAnimatedImageTexture( mAnimatedImageLoading, frameIndex, SamplingMode::BOX_THEN_LINEAR,
168                                             synchronousLoading, mImageUrls[ frameIndex ].mTextureId, Dali::WrapMode::Type::DEFAULT,
169                                             Dali::WrapMode::Type::DEFAULT, this );
170
171   mRequestingLoad = false;
172 }
173
174 void RollingAnimatedImageCache::LoadBatch()
175 {
176   // Try and load up to mBatchSize images, until the cache is filled.
177   // Once the cache is filled, as frames progress, the old frame is
178   // removed, and another frame is loaded
179
180   bool frontFrameReady = IsFrontReady();
181   for( unsigned int i=0; i< mBatchSize && !mQueue.IsFull(); ++i )
182   {
183     ImageFrame imageFrame;
184     imageFrame.mFrameNumber = mFrameIndex;
185     imageFrame.mReady = false;
186
187     mQueue.PushBack( imageFrame );
188
189     if( !mOnLoading )
190     {
191       mOnLoading = true;
192       RequestFrameLoading( mFrameIndex );
193     }
194     else
195     {
196       mLoadWaitingQueue.push_back( mFrameIndex );
197     }
198
199     mFrameIndex++;
200     mFrameIndex %= mFrameCount;
201   }
202
203   CheckFrontFrame( frontFrameReady );
204
205   LOG_CACHE;
206 }
207
208 void RollingAnimatedImageCache::SetImageFrameReady( TextureManager::TextureId textureId )
209 {
210   for( std::size_t i = 0; i < mQueue.Count() ; ++i )
211   {
212     if( GetCachedTextureId( i ) == textureId )
213     {
214       mQueue[i].mReady = true;
215       break;
216     }
217   }
218 }
219
220 TextureSet RollingAnimatedImageCache::GetFrontTextureSet() const
221 {
222   DALI_LOG_INFO( gAnimImgLogFilter, Debug::Concise, "RollingAnimatedImageCache::GetFrontTextureSet() FrameNumber:%d\n", mQueue[ 0 ].mFrameNumber );
223
224   TextureManager::TextureId textureId = GetCachedTextureId( 0 );
225   return mTextureManager.GetTextureSet( textureId );
226 }
227
228 TextureManager::TextureId RollingAnimatedImageCache::GetCachedTextureId( int index ) const
229 {
230   return mImageUrls[ mQueue[ index ].mFrameNumber ].mTextureId;
231 }
232
233 void RollingAnimatedImageCache::CheckFrontFrame( bool wasReady )
234 {
235   if( mWaitingForReadyFrame && wasReady == false && IsFrontReady() )
236   {
237     mWaitingForReadyFrame = false;
238     mObserver.FrameReady( GetFrontTextureSet() );
239   }
240 }
241
242 void RollingAnimatedImageCache::UploadComplete(
243   bool           loadSuccess,
244   int32_t        textureId,
245   TextureSet     textureSet,
246   bool           useAtlasing,
247   const Vector4& atlasRect,
248   bool           preMultiplied )
249 {
250   DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"AnimatedImageVisual::UploadComplete(textureId:%d) start\n", textureId);
251   LOG_CACHE;
252
253   bool frontFrameReady = IsFrontReady();
254
255   if( !mRequestingLoad )
256   {
257     SetImageFrameReady( textureId );
258
259     CheckFrontFrame( frontFrameReady );
260   }
261   else
262   {
263     // UploadComplete has been called from within RequestLoad. TextureManager must
264     // therefore already have the texture cached, so make the texture ready.
265     // (Use the last texture, as the texture id hasn't been assigned yet)
266     mQueue.Back().mReady = true;
267   }
268
269   mOnLoading = false;
270   // The frames of a single animated image can not be loaded parallelly.
271   // Therefore, a frame is now loading, other orders are waiting.
272   // And, after the frame is loaded, requests load of next order.
273   if( !mLoadWaitingQueue.empty() )
274   {
275     uint32_t loadingIndex = mLoadWaitingQueue.front();
276     mLoadWaitingQueue.erase( mLoadWaitingQueue.begin() );
277     mOnLoading = true;
278     RequestFrameLoading( loadingIndex );
279   }
280
281   LOG_CACHE;
282 }
283
284 void RollingAnimatedImageCache::LoadComplete(
285   bool loadSuccess,
286   Devel::PixelBuffer pixelBuffer,
287   const VisualUrl& url,
288   bool preMultiplied )
289 {
290   // LoadComplete is called if this TextureUploadObserver requested to load
291   // an image that will be returned as a type of PixelBuffer by using a method
292   // TextureManager::LoadPixelBuffer.
293 }
294
295 } //namespace Internal
296 } //namespace Toolkit
297 } //namespace Dali