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