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