Remove mask internally in texture manager
[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     if(mQueue.IsEmpty())
280     {
281       mMaskingData->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
282     }
283   }
284 }
285
286 void RollingAnimatedImageCache::ClearCache()
287 {
288   while(mTextureManagerAlive && !mQueue.IsEmpty())
289   {
290     PopFrontCache();
291   }
292   mLoadWaitingQueue.clear();
293   mLoadState = TextureManager::LoadState::NOT_STARTED;
294 }
295
296 void RollingAnimatedImageCache::MakeFrameReady(bool loadSuccess, TextureSet textureSet, uint32_t interval)
297 {
298   if(!loadSuccess)
299   {
300     mLoadState = TextureManager::LoadState::LOAD_FAILED;
301     mObserver.FrameReady(TextureSet(), 0);
302   }
303   else
304   {
305     mLoadState = TextureManager::LoadState::LOAD_FINISHED;
306
307     // Reset size of Queue according to the real frame count.
308     if(mFrameCount != mAnimatedImageLoading.GetImageCount())
309     {
310       mFrameCount = mAnimatedImageLoading.GetImageCount();
311       mTextureIds.resize(mFrameCount);
312       mIntervals.assign(mFrameCount, 0u);
313     }
314
315     bool frontFrameReady = IsFrontReady();
316     // Because only one frame is on loading and the others are in mLoadWaitingQueue,
317     // mQueue.Back() is always the frame currently loaded.
318     mQueue.Back().mReady                   = true;
319     mIntervals[mQueue.Back().mFrameNumber] = interval;
320     // Check whether currently loaded frame is front of queue or not.
321     // If it is, notify frame ready to observer.
322     if(frontFrameReady == false && IsFrontReady())
323     {
324       mObserver.FrameReady(textureSet, interval);
325     }
326   }
327 }
328
329 void RollingAnimatedImageCache::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
330 {
331   DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "AnimatedImageVisual::LoadComplete(textureId:%d) start\n", textureInformation.textureId);
332   LOG_CACHE;
333
334   TextureSet textureSet = mTextureManager.GetTextureSet(textureInformation.textureId);
335   if(textureSet)
336   {
337     Sampler sampler = Sampler::New();
338     sampler.SetWrapMode(mWrapModeU, mWrapModeV);
339     textureSet.SetSampler(0u, sampler);
340   }
341
342   MakeFrameReady(loadSuccess, textureSet, textureInformation.interval);
343
344   if(loadSuccess)
345   {
346     // The frames of a single animated image can not be loaded parallelly.
347     // Therefore, a frame is now loading, other orders are waiting.
348     // And, after the frame is loaded, requests load of next order.
349     if(!mLoadWaitingQueue.empty())
350     {
351       uint32_t loadingIndex = mLoadWaitingQueue.front();
352       mLoadWaitingQueue.erase(mLoadWaitingQueue.begin());
353       RequestFrameLoading(loadingIndex, false);
354     }
355     else if(mQueue.Count() == 1u && textureInformation.frameCount > SINGLE_IMAGE_COUNT)
356     {
357       // There is only an image in queue and no waiting queue.
358       // Request to load batch once again.
359       uint32_t batchFrameIndex = 0u;
360       if(!mLoadWaitingQueue.empty())
361       {
362         batchFrameIndex = (mLoadWaitingQueue.back() + 1) % mFrameCount;
363       }
364       else
365       {
366         batchFrameIndex = (mQueue.Back().mFrameNumber + 1) % mFrameCount;
367       }
368       LoadBatch(batchFrameIndex);
369     }
370   }
371
372   LOG_CACHE;
373 }
374
375 } //namespace Internal
376 } //namespace Toolkit
377 } //namespace Dali