e5ea68e809329cbe61ad1167c5b96319fbde7503
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-image / rolling-animated-image-cache.cpp
1 /*
2  * Copyright (c) 2021 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 // INTERNAL HEADERS
21 #include <dali-toolkit/devel-api/image-loader/texture-manager.h>
22 #include <dali-toolkit/internal/visuals/image-atlas-manager.h> // For ImageAtlasManagerPtr
23 #include <dali/integration-api/debug.h>
24
25 namespace
26 {
27 #if defined(DEBUG_ENABLED)
28 Debug::Filter* gAnimImgLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_ANIMATED_IMAGE");
29
30 #define LOG_CACHE                                                                                                                 \
31   {                                                                                                                               \
32     std::ostringstream oss;                                                                                                       \
33     oss << "Size:" << mQueue.Count() << " [ ";                                                                                    \
34     for(std::size_t _i = 0; _i < mQueue.Count(); ++_i)                                                                            \
35     {                                                                                                                             \
36       oss << _i << "={ frm#: " << mQueue[_i].mFrameNumber << " tex: " << mImageUrls[mQueue[_i].mFrameNumber].mTextureId << "}, "; \
37     }                                                                                                                             \
38     oss << " ]" << std::endl;                                                                                                     \
39     DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "%s", oss.str().c_str());                                                    \
40   }
41
42 #else
43 #define LOG_CACHE
44 #endif
45
46 static constexpr bool ENABLE_ORIENTATION_CORRECTION(true);
47
48 } // namespace
49
50 namespace Dali
51 {
52 namespace Toolkit
53 {
54 namespace Internal
55 {
56 namespace
57 {
58 static constexpr uint32_t SINGLE_IMAGE_COUNT = 1u;
59 static constexpr uint32_t FIRST_FRAME_INDEX  = 0u;
60 } // namespace
61
62 RollingAnimatedImageCache::RollingAnimatedImageCache(TextureManager&                     textureManager,
63                                                      AnimatedImageLoading&               animatedImageLoading,
64                                                      TextureManager::MaskingDataPointer& maskingData,
65                                                      ImageCache::FrameReadyObserver&     observer,
66                                                      uint16_t                            cacheSize,
67                                                      uint16_t                            batchSize,
68                                                      bool                                isSynchronousLoading)
69 : ImageCache(textureManager, maskingData, observer, batchSize, 0u),
70   mAnimatedImageLoading(animatedImageLoading),
71   mFrameCount(SINGLE_IMAGE_COUNT),
72   mFrameIndex(FIRST_FRAME_INDEX),
73   mCacheSize(cacheSize),
74   mQueue(cacheSize),
75   mIsSynchronousLoading(isSynchronousLoading)
76 {
77   mImageUrls.resize(mFrameCount);
78   mIntervals.assign(mFrameCount, 0);
79 }
80
81 RollingAnimatedImageCache::~RollingAnimatedImageCache()
82 {
83   ClearCache();
84   mAnimatedImageLoading.Reset();
85 }
86
87 TextureSet RollingAnimatedImageCache::Frame(uint32_t frameIndex)
88 {
89   bool popExist = false;
90   while(!mQueue.IsEmpty() && mQueue.Front().mFrameNumber != frameIndex)
91   {
92     PopFrontCache();
93     popExist = true;
94   }
95
96   TextureSet textureSet;
97   uint32_t   batchFrameIndex = frameIndex;
98   // If we need to load new frame that are not stored in queue.
99   // Load the frame synchronously.
100   bool synchronouslyLoaded = false;
101   if(mIsSynchronousLoading && mQueue.IsEmpty())
102   {
103     textureSet          = RequestFrameLoading(frameIndex, frameIndex == FIRST_FRAME_INDEX, true);
104     batchFrameIndex     = (frameIndex + 1) % mFrameCount;
105     uint32_t interval = 0u;
106     if(textureSet)
107     {
108       synchronouslyLoaded = true;
109       interval = mAnimatedImageLoading.GetFrameInterval(mQueue.Back().mFrameNumber);
110     }
111     MakeFrameReady(synchronouslyLoaded, textureSet, interval);
112   }
113
114   if(popExist || mQueue.IsEmpty() || synchronouslyLoaded)
115   {
116     // If the frame of frameIndex was already loaded, load batch from the last frame of queue
117     if(!mQueue.IsEmpty())
118     {
119       if(!mLoadWaitingQueue.empty())
120       {
121         batchFrameIndex = (mLoadWaitingQueue.back() + 1) % mFrameCount;
122       }
123       else
124       {
125         batchFrameIndex = (mQueue.Back().mFrameNumber + 1) % mFrameCount;
126       }
127     }
128     else
129     {
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         batchFrameIndex = frameIndex;
136       }
137     }
138     LoadBatch(batchFrameIndex);
139   }
140
141   if(!textureSet && mLoadState != TextureManager::LoadState::LOAD_FAILED && IsFrontReady() == true)
142   {
143     textureSet = GetFrontTextureSet();
144   }
145
146   return textureSet;
147 }
148
149 TextureSet RollingAnimatedImageCache::FirstFrame()
150 {
151   TextureSet textureSet = Frame(FIRST_FRAME_INDEX);
152   return textureSet;
153 }
154
155 uint32_t RollingAnimatedImageCache::GetFrameInterval(uint32_t frameIndex) const
156 {
157   if(frameIndex >= mIntervals.size())
158   {
159     return 0u;
160   }
161   return mIntervals[frameIndex];
162 }
163
164 int32_t RollingAnimatedImageCache::GetCurrentFrameIndex() const
165 {
166   if(mQueue.IsEmpty())
167   {
168     return -1;
169   }
170   return mQueue.Front().mFrameNumber;
171 }
172
173 int32_t RollingAnimatedImageCache::GetTotalFrameCount() const
174 {
175   return mFrameCount;
176 }
177
178 bool RollingAnimatedImageCache::IsFrontReady() const
179 {
180   return (!mQueue.IsEmpty() && mQueue.Front().mReady);
181 }
182
183 TextureSet RollingAnimatedImageCache::RequestFrameLoading(uint32_t frameIndex, bool useCache, bool synchronousLoading)
184 {
185   ImageFrame imageFrame;
186   imageFrame.mFrameNumber = frameIndex;
187   imageFrame.mReady       = false;
188
189   mQueue.PushBack(imageFrame);
190
191   mLoadState = TextureManager::LoadState::LOADING;
192
193   TextureManager::TextureId loadTextureId = TextureManager::INVALID_TEXTURE_ID;
194   TextureSet                textureSet    = mTextureManager.LoadAnimatedImageTexture(mAnimatedImageLoading,
195                                                                    frameIndex,
196                                                                    loadTextureId,
197                                                                    mMaskingData,
198                                                                    SamplingMode::BOX_THEN_LINEAR,
199                                                                    Dali::WrapMode::Type::DEFAULT,
200                                                                    Dali::WrapMode::Type::DEFAULT,
201                                                                    synchronousLoading,
202                                                                    useCache,
203                                                                    this);
204
205   mImageUrls[frameIndex].mTextureId = loadTextureId;
206
207   return textureSet;
208 }
209
210 void RollingAnimatedImageCache::LoadBatch(uint32_t frameIndex)
211 {
212   // Try and load up to mBatchSize images, until the cache is filled.
213   // Once the cache is filled, as frames progress, the old frame is
214   // removed, and another frame is loaded
215   uint32_t minimumSize = std::min(mCacheSize, mFrameCount);
216   for(uint32_t i = 0; i < mBatchSize && (mQueue.Count() + mLoadWaitingQueue.size()) < minimumSize; ++i)
217   {
218     if(mLoadState != TextureManager::LoadState::LOADING)
219     {
220       RequestFrameLoading(frameIndex, frameIndex == FIRST_FRAME_INDEX, false);
221     }
222     else
223     {
224       mLoadWaitingQueue.push_back(frameIndex);
225     }
226
227     frameIndex++;
228     frameIndex %= mFrameCount;
229   }
230
231   LOG_CACHE;
232 }
233
234 void RollingAnimatedImageCache::SetImageFrameReady(TextureManager::TextureId textureId)
235 {
236   for(std::size_t i = 0; i < mQueue.Count(); ++i)
237   {
238     if(GetCachedTextureId(i) == textureId)
239     {
240       mQueue[i].mReady = true;
241       break;
242     }
243   }
244 }
245
246 TextureSet RollingAnimatedImageCache::GetFrontTextureSet() const
247 {
248   DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "RollingAnimatedImageCache::GetFrontTextureSet() FrameNumber:%d\n", mQueue[0].mFrameNumber);
249
250   TextureManager::TextureId textureId = GetCachedTextureId(0);
251   return mTextureManager.GetTextureSet(textureId);
252 }
253
254 TextureManager::TextureId RollingAnimatedImageCache::GetCachedTextureId(int index) const
255 {
256   return mImageUrls[mQueue[index].mFrameNumber].mTextureId;
257 }
258
259 void RollingAnimatedImageCache::PopFrontCache()
260 {
261   ImageFrame imageFrame = mQueue.PopFront();
262   mTextureManager.Remove(mImageUrls[imageFrame.mFrameNumber].mTextureId, this);
263   mImageUrls[imageFrame.mFrameNumber].mTextureId = TextureManager::INVALID_TEXTURE_ID;
264
265   if(mMaskingData && mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID)
266   {
267     mTextureManager.Remove(mMaskingData->mAlphaMaskId, this);
268     if(mQueue.IsEmpty())
269     {
270       mMaskingData->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
271     }
272   }
273 }
274
275 void RollingAnimatedImageCache::ClearCache()
276 {
277   while(mTextureManagerAlive && !mQueue.IsEmpty())
278   {
279     PopFrontCache();
280   }
281   mLoadWaitingQueue.clear();
282   mLoadState = TextureManager::LoadState::NOT_STARTED;
283 }
284
285 void RollingAnimatedImageCache::MakeFrameReady(bool loadSuccess, TextureSet textureSet, uint32_t interval)
286 {
287   if(!loadSuccess)
288   {
289     mLoadState = TextureManager::LoadState::LOAD_FAILED;
290     mObserver.FrameReady(TextureSet(), 0);
291   }
292   else
293   {
294     mLoadState = TextureManager::LoadState::LOAD_FINISHED;
295
296     // Reset size of Queue according to the real frame count.
297     if(mFrameCount != mAnimatedImageLoading.GetImageCount())
298     {
299       mFrameCount = mAnimatedImageLoading.GetImageCount();
300       mImageUrls.resize(mFrameCount);
301       mIntervals.assign(mFrameCount, 0u);
302     }
303
304     bool frontFrameReady = IsFrontReady();
305     // Because only one frame is on loading and the others are in mLoadWaitingQueue,
306     // mQueue.Back() is always the frame currently loaded.
307     mQueue.Back().mReady                   = true;
308     mIntervals[mQueue.Back().mFrameNumber] = interval;
309     // Check whether currently loaded frame is front of queue or not.
310     // If it is, notify frame ready to observer.
311     if(frontFrameReady == false && IsFrontReady())
312     {
313       mObserver.FrameReady(textureSet, interval);
314     }
315   }
316 }
317
318 void RollingAnimatedImageCache::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
319 {
320   DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "AnimatedImageVisual::LoadComplete(textureId:%d) start\n", textureInformation.textureId);
321   LOG_CACHE;
322
323   MakeFrameReady(loadSuccess, mTextureManager.GetTextureSet(textureInformation.textureId), textureInformation.interval);
324
325   if(loadSuccess)
326   {
327     // The frames of a single animated image can not be loaded parallelly.
328     // Therefore, a frame is now loading, other orders are waiting.
329     // And, after the frame is loaded, requests load of next order.
330     if(!mLoadWaitingQueue.empty())
331     {
332       uint32_t loadingIndex = mLoadWaitingQueue.front();
333       mLoadWaitingQueue.erase(mLoadWaitingQueue.begin());
334       RequestFrameLoading(loadingIndex, loadingIndex == FIRST_FRAME_INDEX, false);
335     }
336     else if(mQueue.Count() == 1u && textureInformation.frameCount > SINGLE_IMAGE_COUNT)
337     {
338       // There is only an image in queue and no waiting queue.
339       // Request to load batch once again.
340       uint32_t batchFrameIndex = 0u;
341       if(!mLoadWaitingQueue.empty())
342       {
343         batchFrameIndex = (mLoadWaitingQueue.back() + 1) % mFrameCount;
344       }
345       else
346       {
347         batchFrameIndex = (mQueue.Back().mFrameNumber + 1) % mFrameCount;
348       }
349       LoadBatch(batchFrameIndex);
350     }
351   }
352
353   LOG_CACHE;
354 }
355
356 } //namespace Internal
357 } //namespace Toolkit
358 } //namespace Dali