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