Merge "Apply EncodedImageBuffer with Atlas" 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: " << 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, 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 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                                                                    this);
203
204   mImageUrls[frameIndex].mTextureId = loadTextureId;
205
206   return textureSet;
207 }
208
209 void RollingAnimatedImageCache::LoadBatch(uint32_t frameIndex)
210 {
211   // Try and load up to mBatchSize images, until the cache is filled.
212   // Once the cache is filled, as frames progress, the old frame is
213   // removed, and another frame is loaded
214   uint32_t minimumSize = std::min(mCacheSize, mFrameCount);
215   for(uint32_t i = 0; i < mBatchSize && (mQueue.Count() + mLoadWaitingQueue.size()) < minimumSize; ++i)
216   {
217     if(mLoadState != TextureManager::LoadState::LOADING)
218     {
219       RequestFrameLoading(frameIndex, false);
220     }
221     else
222     {
223       mLoadWaitingQueue.push_back(frameIndex);
224     }
225
226     frameIndex++;
227     frameIndex %= mFrameCount;
228   }
229
230   LOG_CACHE;
231 }
232
233 void RollingAnimatedImageCache::SetImageFrameReady(TextureManager::TextureId textureId)
234 {
235   for(std::size_t i = 0; i < mQueue.Count(); ++i)
236   {
237     if(GetCachedTextureId(i) == textureId)
238     {
239       mQueue[i].mReady = true;
240       break;
241     }
242   }
243 }
244
245 TextureSet RollingAnimatedImageCache::GetFrontTextureSet() const
246 {
247   DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "RollingAnimatedImageCache::GetFrontTextureSet() FrameNumber:%d\n", mQueue[0].mFrameNumber);
248
249   TextureManager::TextureId textureId = GetCachedTextureId(0);
250   return mTextureManager.GetTextureSet(textureId);
251 }
252
253 TextureManager::TextureId RollingAnimatedImageCache::GetCachedTextureId(int index) const
254 {
255   return mImageUrls[mQueue[index].mFrameNumber].mTextureId;
256 }
257
258 void RollingAnimatedImageCache::PopFrontCache()
259 {
260   ImageFrame imageFrame = mQueue.PopFront();
261   mTextureManager.Remove(mImageUrls[imageFrame.mFrameNumber].mTextureId, this);
262   mImageUrls[imageFrame.mFrameNumber].mTextureId = TextureManager::INVALID_TEXTURE_ID;
263
264   if(mMaskingData && mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID)
265   {
266     mTextureManager.Remove(mMaskingData->mAlphaMaskId, this);
267     if(mQueue.IsEmpty())
268     {
269       mMaskingData->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
270     }
271   }
272 }
273
274 void RollingAnimatedImageCache::ClearCache()
275 {
276   while(mTextureManagerAlive && !mQueue.IsEmpty())
277   {
278     PopFrontCache();
279   }
280   mLoadWaitingQueue.clear();
281   mLoadState = TextureManager::LoadState::NOT_STARTED;
282 }
283
284 void RollingAnimatedImageCache::MakeFrameReady(bool loadSuccess, TextureSet textureSet, uint32_t interval)
285 {
286   if(!loadSuccess)
287   {
288     mLoadState = TextureManager::LoadState::LOAD_FAILED;
289     mObserver.FrameReady(TextureSet(), 0);
290   }
291   else
292   {
293     mLoadState = TextureManager::LoadState::LOAD_FINISHED;
294
295     // Reset size of Queue according to the real frame count.
296     if(mFrameCount != mAnimatedImageLoading.GetImageCount())
297     {
298       mFrameCount = mAnimatedImageLoading.GetImageCount();
299       mImageUrls.resize(mFrameCount);
300       mIntervals.assign(mFrameCount, 0u);
301     }
302
303     bool frontFrameReady = IsFrontReady();
304     // Because only one frame is on loading and the others are in mLoadWaitingQueue,
305     // mQueue.Back() is always the frame currently loaded.
306     mQueue.Back().mReady                   = true;
307     mIntervals[mQueue.Back().mFrameNumber] = interval;
308     // Check whether currently loaded frame is front of queue or not.
309     // If it is, notify frame ready to observer.
310     if(frontFrameReady == false && IsFrontReady())
311     {
312       mObserver.FrameReady(textureSet, interval);
313     }
314   }
315 }
316
317 void RollingAnimatedImageCache::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
318 {
319   DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "AnimatedImageVisual::LoadComplete(textureId:%d) start\n", textureInformation.textureId);
320   LOG_CACHE;
321
322   MakeFrameReady(loadSuccess, mTextureManager.GetTextureSet(textureInformation.textureId), textureInformation.interval);
323
324   if(loadSuccess)
325   {
326     // The frames of a single animated image can not be loaded parallelly.
327     // Therefore, a frame is now loading, other orders are waiting.
328     // And, after the frame is loaded, requests load of next order.
329     if(!mLoadWaitingQueue.empty())
330     {
331       uint32_t loadingIndex = mLoadWaitingQueue.front();
332       mLoadWaitingQueue.erase(mLoadWaitingQueue.begin());
333       RequestFrameLoading(loadingIndex, false);
334     }
335     else if(mQueue.Count() == 1u && textureInformation.frameCount > SINGLE_IMAGE_COUNT)
336     {
337       // There is only an image in queue and no waiting queue.
338       // Request to load batch once again.
339       uint32_t batchFrameIndex = 0u;
340       if(!mLoadWaitingQueue.empty())
341       {
342         batchFrameIndex = (mLoadWaitingQueue.back() + 1) % mFrameCount;
343       }
344       else
345       {
346         batchFrameIndex = (mQueue.Back().mFrameNumber + 1) % mFrameCount;
347       }
348       LoadBatch(batchFrameIndex);
349     }
350   }
351
352   LOG_CACHE;
353 }
354
355 } //namespace Internal
356 } //namespace Toolkit
357 } //namespace Dali