473f276ceb944eeec004f8bf75eb8e0bb2d97bd3
[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(
63   TextureManager& textureManager, AnimatedImageLoading& animatedImageLoading, ImageCache::FrameReadyObserver& observer, uint16_t cacheSize, uint16_t batchSize, bool isSynchronousLoading)
64 : ImageCache(textureManager, observer, batchSize, 0u),
65   mAnimatedImageLoading(animatedImageLoading),
66   mFrameCount(SINGLE_IMAGE_COUNT),
67   mFrameIndex(FIRST_FRAME_INDEX),
68   mCacheSize(cacheSize),
69   mQueue(cacheSize),
70   mIsSynchronousLoading(isSynchronousLoading)
71 {
72   mImageUrls.resize(mFrameCount);
73   mIntervals.assign(mFrameCount, 0);
74 }
75
76 RollingAnimatedImageCache::~RollingAnimatedImageCache()
77 {
78   ClearCache();
79   mAnimatedImageLoading.Reset();
80 }
81
82 TextureSet RollingAnimatedImageCache::Frame(uint32_t frameIndex)
83 {
84   bool popExist = false;
85   while(!mQueue.IsEmpty() && mQueue.Front().mFrameNumber != frameIndex)
86   {
87     ImageFrame imageFrame = mQueue.PopFront();
88     mTextureManager.Remove(mImageUrls[imageFrame.mFrameNumber].mTextureId, this);
89     mImageUrls[imageFrame.mFrameNumber].mTextureId = TextureManager::INVALID_TEXTURE_ID;
90     popExist                                       = true;
91   }
92
93   TextureSet textureSet;
94   uint32_t   batchFrameIndex = frameIndex;
95   // If we need to load new frame that are not stored in queue.
96   // Load the frame synchronously.
97   bool synchronouslyLoaded = false;
98   if(mIsSynchronousLoading && mQueue.IsEmpty())
99   {
100     textureSet          = RequestFrameLoading(frameIndex, frameIndex == FIRST_FRAME_INDEX, true);
101     batchFrameIndex     = (frameIndex + 1) % mFrameCount;
102     uint32_t interval = 0u;
103     if(textureSet)
104     {
105       synchronouslyLoaded = true;
106       interval = mAnimatedImageLoading.GetFrameInterval(mQueue.Back().mFrameNumber);
107     }
108     MakeFrameReady(synchronouslyLoaded, textureSet, interval);
109   }
110
111   if(popExist || mQueue.IsEmpty() || synchronouslyLoaded)
112   {
113     // If the frame of frameIndex was already loaded, load batch from the last frame of queue
114     if(!mQueue.IsEmpty())
115     {
116       if(!mLoadWaitingQueue.empty())
117       {
118         batchFrameIndex = (mLoadWaitingQueue.back() + 1) % mFrameCount;
119       }
120       else
121       {
122         batchFrameIndex = (mQueue.Back().mFrameNumber + 1) % mFrameCount;
123       }
124     }
125     else
126     {
127       // If the request is for the first frame or a jumped frame(JUMP_TO) remove current waiting queue.
128       mLoadWaitingQueue.clear();
129       // If the queue is empty, and the frame of frameIndex is not loaded synchronously. load batch from the frame of frameIndex
130       if(!textureSet)
131       {
132         batchFrameIndex = frameIndex;
133       }
134     }
135     LoadBatch(batchFrameIndex);
136   }
137
138   if(!textureSet && mLoadState != TextureManager::LoadState::LOAD_FAILED && IsFrontReady() == true)
139   {
140     textureSet = GetFrontTextureSet();
141   }
142
143   return textureSet;
144 }
145
146 TextureSet RollingAnimatedImageCache::FirstFrame()
147 {
148   TextureSet textureSet = Frame(FIRST_FRAME_INDEX);
149   return textureSet;
150 }
151
152 uint32_t RollingAnimatedImageCache::GetFrameInterval(uint32_t frameIndex) const
153 {
154   if(frameIndex >= mIntervals.size())
155   {
156     return 0u;
157   }
158   return mIntervals[frameIndex];
159 }
160
161 int32_t RollingAnimatedImageCache::GetCurrentFrameIndex() const
162 {
163   if(mQueue.IsEmpty())
164   {
165     return -1;
166   }
167   return mQueue.Front().mFrameNumber;
168 }
169
170 int32_t RollingAnimatedImageCache::GetTotalFrameCount() const
171 {
172   return mFrameCount;
173 }
174
175 bool RollingAnimatedImageCache::IsFrontReady() const
176 {
177   return (!mQueue.IsEmpty() && mQueue.Front().mReady);
178 }
179
180 TextureSet RollingAnimatedImageCache::RequestFrameLoading(uint32_t frameIndex, bool useCache, bool synchronousLoading)
181 {
182   ImageFrame imageFrame;
183   imageFrame.mFrameNumber = frameIndex;
184   imageFrame.mReady       = false;
185
186   mQueue.PushBack(imageFrame);
187
188   mLoadState = TextureManager::LoadState::LOADING;
189
190   TextureManager::TextureId loadTextureId = TextureManager::INVALID_TEXTURE_ID;
191   TextureSet                textureSet    = mTextureManager.LoadAnimatedImageTexture(mAnimatedImageLoading,
192                                                                    frameIndex,
193                                                                    loadTextureId,
194                                                                    SamplingMode::BOX_THEN_LINEAR,
195                                                                    Dali::WrapMode::Type::DEFAULT,
196                                                                    Dali::WrapMode::Type::DEFAULT,
197                                                                    synchronousLoading,
198                                                                    useCache,
199                                                                    this);
200
201   mImageUrls[frameIndex].mTextureId = loadTextureId;
202
203   return textureSet;
204 }
205
206 void RollingAnimatedImageCache::LoadBatch(uint32_t frameIndex)
207 {
208   // Try and load up to mBatchSize images, until the cache is filled.
209   // Once the cache is filled, as frames progress, the old frame is
210   // removed, and another frame is loaded
211   uint32_t minimumSize = std::min(mCacheSize, mFrameCount);
212   for(uint32_t i = 0; i < mBatchSize && (mQueue.Count() + mLoadWaitingQueue.size()) < minimumSize; ++i)
213   {
214     if(mLoadState != TextureManager::LoadState::LOADING)
215     {
216       RequestFrameLoading(frameIndex, frameIndex == FIRST_FRAME_INDEX, false);
217     }
218     else
219     {
220       mLoadWaitingQueue.push_back(frameIndex);
221     }
222
223     frameIndex++;
224     frameIndex %= mFrameCount;
225   }
226
227   LOG_CACHE;
228 }
229
230 void RollingAnimatedImageCache::SetImageFrameReady(TextureManager::TextureId textureId)
231 {
232   for(std::size_t i = 0; i < mQueue.Count(); ++i)
233   {
234     if(GetCachedTextureId(i) == textureId)
235     {
236       mQueue[i].mReady = true;
237       break;
238     }
239   }
240 }
241
242 TextureSet RollingAnimatedImageCache::GetFrontTextureSet() const
243 {
244   DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "RollingAnimatedImageCache::GetFrontTextureSet() FrameNumber:%d\n", mQueue[0].mFrameNumber);
245
246   TextureManager::TextureId textureId = GetCachedTextureId(0);
247   return mTextureManager.GetTextureSet(textureId);
248 }
249
250 TextureManager::TextureId RollingAnimatedImageCache::GetCachedTextureId(int index) const
251 {
252   return mImageUrls[mQueue[index].mFrameNumber].mTextureId;
253 }
254
255 void RollingAnimatedImageCache::ClearCache()
256 {
257   while(mTextureManagerAlive && !mQueue.IsEmpty())
258   {
259     ImageFrame imageFrame = mQueue.PopFront();
260     mTextureManager.Remove(mImageUrls[imageFrame.mFrameNumber].mTextureId, this);
261     mImageUrls[imageFrame.mFrameNumber].mTextureId = TextureManager::INVALID_TEXTURE_ID;
262   }
263   mLoadWaitingQueue.clear();
264   mLoadState = TextureManager::LoadState::NOT_STARTED;
265 }
266
267 void RollingAnimatedImageCache::MakeFrameReady(bool loadSuccess, TextureSet textureSet, uint32_t interval)
268 {
269   if(!loadSuccess)
270   {
271     mLoadState = TextureManager::LoadState::LOAD_FAILED;
272     mObserver.FrameReady(TextureSet(), 0);
273   }
274   else
275   {
276     mLoadState = TextureManager::LoadState::LOAD_FINISHED;
277
278     // Reset size of Queue according to the real frame count.
279     if(mFrameCount != mAnimatedImageLoading.GetImageCount())
280     {
281       mFrameCount = mAnimatedImageLoading.GetImageCount();
282       mImageUrls.resize(mFrameCount);
283       mIntervals.assign(mFrameCount, 0u);
284     }
285
286     bool frontFrameReady = IsFrontReady();
287     // Because only one frame is on loading and the others are in mLoadWaitingQueue,
288     // mQueue.Back() is always the frame currently loaded.
289     mQueue.Back().mReady                   = true;
290     mIntervals[mQueue.Back().mFrameNumber] = interval;
291     // Check whether currently loaded frame is front of queue or not.
292     // If it is, notify frame ready to observer.
293     if(frontFrameReady == false && IsFrontReady())
294     {
295       mObserver.FrameReady(textureSet, interval);
296     }
297   }
298 }
299
300 void RollingAnimatedImageCache::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
301 {
302   DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "AnimatedImageVisual::LoadComplete(textureId:%d) start\n", textureInformation.textureId);
303   LOG_CACHE;
304
305   MakeFrameReady(loadSuccess, mTextureManager.GetTextureSet(textureInformation.textureId), textureInformation.interval);
306
307   if(loadSuccess)
308   {
309     // The frames of a single animated image can not be loaded parallelly.
310     // Therefore, a frame is now loading, other orders are waiting.
311     // And, after the frame is loaded, requests load of next order.
312     if(!mLoadWaitingQueue.empty())
313     {
314       uint32_t loadingIndex = mLoadWaitingQueue.front();
315       mLoadWaitingQueue.erase(mLoadWaitingQueue.begin());
316       RequestFrameLoading(loadingIndex, loadingIndex == FIRST_FRAME_INDEX, false);
317     }
318     else if(mQueue.Count() == 1u && textureInformation.frameCount > SINGLE_IMAGE_COUNT)
319     {
320       // There is only an image in queue and no waiting queue.
321       // Request to load batch once again.
322       uint32_t batchFrameIndex = 0u;
323       if(!mLoadWaitingQueue.empty())
324       {
325         batchFrameIndex = (mLoadWaitingQueue.back() + 1) % mFrameCount;
326       }
327       else
328       {
329         batchFrameIndex = (mQueue.Back().mFrameNumber + 1) % mFrameCount;
330       }
331       LoadBatch(batchFrameIndex);
332     }
333   }
334
335   LOG_CACHE;
336 }
337
338 } //namespace Internal
339 } //namespace Toolkit
340 } //namespace Dali