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