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