Refactoring TextureUploadObserver.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-image / rolling-animated-image-cache.cpp
1 /*
2  * Copyright (c) 2021 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 // EXTERNAL HEADERS
21
22 // INTERNAL HEADERS
23 #include <dali-toolkit/devel-api/image-loader/texture-manager.h>
24 #include <dali-toolkit/internal/visuals/image-atlas-manager.h> // For ImageAtlasManagerPtr
25 #include <dali/integration-api/debug.h>
26
27 namespace
28 {
29 #if defined(DEBUG_ENABLED)
30 Debug::Filter* gAnimImgLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_ANIMATED_IMAGE");
31
32 #define LOG_CACHE                                                                                                                 \
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: " << mImageUrls[mQueue[_i].mFrameNumber].mTextureId << "}, "; \
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
48 const bool ENABLE_ORIENTATION_CORRECTION(true);
49
50 } // namespace
51
52 namespace Dali
53 {
54 namespace Toolkit
55 {
56 namespace Internal
57 {
58 RollingAnimatedImageCache::RollingAnimatedImageCache(
59   TextureManager& textureManager, AnimatedImageLoading& animatedImageLoading, uint32_t frameCount, ImageCache::FrameReadyObserver& observer, uint16_t cacheSize, uint16_t batchSize, bool isSynchronousLoading)
60 : ImageCache(textureManager, observer, batchSize),
61   mAnimatedImageLoading(animatedImageLoading),
62   mFrameCount(frameCount),
63   mFrameIndex(0),
64   mCacheSize(cacheSize),
65   mQueue(cacheSize),
66   mIsSynchronousLoading(isSynchronousLoading),
67   mOnLoading(false)
68 {
69   mImageUrls.resize(mFrameCount);
70   mIntervals.assign(mFrameCount, 0);
71   LoadBatch();
72 }
73
74 RollingAnimatedImageCache::~RollingAnimatedImageCache()
75 {
76   if(mTextureManagerAlive)
77   {
78     while(!mQueue.IsEmpty())
79     {
80       ImageFrame imageFrame = mQueue.PopFront();
81       mTextureManager.Remove(mImageUrls[imageFrame.mFrameNumber].mTextureId, this);
82     }
83   }
84 }
85
86 TextureSet RollingAnimatedImageCache::Frame(uint32_t frameIndex)
87 {
88   bool popExist = false;
89   while(!mQueue.IsEmpty() && mQueue.Front().mFrameNumber != frameIndex)
90   {
91     ImageFrame imageFrame = mQueue.PopFront();
92     mTextureManager.Remove(mImageUrls[imageFrame.mFrameNumber].mTextureId, this);
93     mImageUrls[imageFrame.mFrameNumber].mTextureId = TextureManager::INVALID_TEXTURE_ID;
94     popExist                                       = true;
95   }
96
97   TextureSet textureSet;
98   // If we need to load new frame that are not stored in queue.
99   // Load the frame synchronously.
100   if(mIsSynchronousLoading && mQueue.IsEmpty())
101   {
102     bool synchronousLoading = true;
103     textureSet              = mTextureManager.LoadAnimatedImageTexture(mAnimatedImageLoading, frameIndex, SamplingMode::BOX_THEN_LINEAR, synchronousLoading, mImageUrls[frameIndex].mTextureId, Dali::WrapMode::Type::DEFAULT, Dali::WrapMode::Type::DEFAULT, this);
104     mFrameIndex             = (frameIndex + 1) % mFrameCount;
105   }
106
107   if(popExist || mQueue.IsEmpty())
108   {
109     // If the frame of frameIndex was already loaded, load batch from the last frame of queue
110     if(!mQueue.IsEmpty())
111     {
112       if(!mLoadWaitingQueue.empty())
113       {
114         mFrameIndex = (mLoadWaitingQueue.back() + 1) % mFrameCount;
115       }
116       else
117       {
118         mFrameIndex = (mQueue.Back().mFrameNumber + 1) % mFrameCount;
119       }
120     }
121     else
122     {
123       mOnLoading = false;
124       // If the request is for the first frame or a jumped frame(JUMP_TO) remove current waiting queue.
125       mLoadWaitingQueue.clear();
126       // If the queue is empty, and the frame of frameIndex is not loaded synchronously. load batch from the frame of frameIndex
127       if(!textureSet)
128       {
129         mFrameIndex = frameIndex;
130       }
131     }
132     LoadBatch();
133   }
134
135   if(!textureSet)
136   {
137     if(IsFrontReady() == true)
138     {
139       textureSet = GetFrontTextureSet();
140     }
141     else
142     {
143       mWaitingForReadyFrame = true;
144     }
145   }
146
147   return textureSet;
148 }
149
150 TextureSet RollingAnimatedImageCache::FirstFrame()
151 {
152   return Frame(0u);
153 }
154
155 TextureSet RollingAnimatedImageCache::NextFrame()
156 {
157   TextureSet textureSet;
158   if(!mQueue.IsEmpty())
159   {
160     uint32_t frameIndex = mQueue.Front().mFrameNumber;
161     if(IsFrontReady())
162     {
163       frameIndex = (frameIndex + 1) % mFrameCount;
164     }
165     textureSet = Frame(frameIndex);
166   }
167   else
168   {
169     DALI_LOG_ERROR("Cache is empty.");
170   }
171
172   return textureSet;
173 }
174
175 uint32_t RollingAnimatedImageCache::GetFrameInterval(uint32_t frameIndex) const
176 {
177   return mAnimatedImageLoading.GetFrameInterval(frameIndex);
178 }
179
180 int32_t RollingAnimatedImageCache::GetCurrentFrameIndex() const
181 {
182   if(mQueue.IsEmpty())
183   {
184     return -1;
185   }
186   return mQueue.Front().mFrameNumber;
187 }
188
189 int32_t RollingAnimatedImageCache::GetTotalFrameCount() const
190 {
191   return mFrameCount;
192 }
193
194 bool RollingAnimatedImageCache::IsFrontReady() const
195 {
196   return (!mQueue.IsEmpty() && mQueue.Front().mReady);
197 }
198
199 void RollingAnimatedImageCache::RequestFrameLoading(uint32_t frameIndex)
200 {
201   ImageFrame imageFrame;
202   imageFrame.mFrameNumber = frameIndex;
203   imageFrame.mReady       = false;
204
205   mQueue.PushBack(imageFrame);
206
207   mRequestingLoad = true;
208
209   bool synchronousLoading = false;
210   mTextureManager.LoadAnimatedImageTexture(mAnimatedImageLoading, frameIndex, SamplingMode::BOX_THEN_LINEAR, synchronousLoading, mImageUrls[frameIndex].mTextureId, Dali::WrapMode::Type::DEFAULT, Dali::WrapMode::Type::DEFAULT, this);
211
212   mRequestingLoad = false;
213 }
214
215 void RollingAnimatedImageCache::LoadBatch()
216 {
217   // Try and load up to mBatchSize images, until the cache is filled.
218   // Once the cache is filled, as frames progress, the old frame is
219   // removed, and another frame is loaded
220
221   bool frontFrameReady = IsFrontReady();
222   for(unsigned int i = 0; i < mBatchSize && mQueue.Count() + mLoadWaitingQueue.size() < static_cast<uint32_t>(mCacheSize) && !mQueue.IsFull(); ++i)
223   {
224     if(!mOnLoading)
225     {
226       mOnLoading = true;
227       RequestFrameLoading(mFrameIndex);
228     }
229     else
230     {
231       mLoadWaitingQueue.push_back(mFrameIndex);
232     }
233
234     mFrameIndex++;
235     mFrameIndex %= mFrameCount;
236   }
237
238   CheckFrontFrame(frontFrameReady);
239
240   LOG_CACHE;
241 }
242
243 void RollingAnimatedImageCache::SetImageFrameReady(TextureManager::TextureId textureId)
244 {
245   for(std::size_t i = 0; i < mQueue.Count(); ++i)
246   {
247     if(GetCachedTextureId(i) == textureId)
248     {
249       mQueue[i].mReady = true;
250       break;
251     }
252   }
253 }
254
255 TextureSet RollingAnimatedImageCache::GetFrontTextureSet() const
256 {
257   DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "RollingAnimatedImageCache::GetFrontTextureSet() FrameNumber:%d\n", mQueue[0].mFrameNumber);
258
259   TextureManager::TextureId textureId = GetCachedTextureId(0);
260   return mTextureManager.GetTextureSet(textureId);
261 }
262
263 TextureManager::TextureId RollingAnimatedImageCache::GetCachedTextureId(int index) const
264 {
265   return mImageUrls[mQueue[index].mFrameNumber].mTextureId;
266 }
267
268 void RollingAnimatedImageCache::CheckFrontFrame(bool wasReady)
269 {
270   if(mWaitingForReadyFrame && wasReady == false && IsFrontReady())
271   {
272     mWaitingForReadyFrame = false;
273     mObserver.FrameReady(GetFrontTextureSet());
274   }
275 }
276
277 void RollingAnimatedImageCache::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
278 {
279   DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "AnimatedImageVisual::LoadComplete(textureId:%d) start\n", textureInformation.textureId);
280   LOG_CACHE;
281
282   bool frontFrameReady = IsFrontReady();
283
284   if(!mRequestingLoad)
285   {
286     SetImageFrameReady(textureInformation.textureId);
287
288     CheckFrontFrame(frontFrameReady);
289   }
290   else
291   {
292     // LoadComplete has been called from within RequestLoad. TextureManager must
293     // therefore already have the texture cached, so make the texture ready.
294     // (Use the last texture, as the texture id hasn't been assigned yet)
295     mQueue.Back().mReady = true;
296   }
297
298   mOnLoading = false;
299   // The frames of a single animated image can not be loaded parallelly.
300   // Therefore, a frame is now loading, other orders are waiting.
301   // And, after the frame is loaded, requests load of next order.
302   if(!mLoadWaitingQueue.empty())
303   {
304     uint32_t loadingIndex = mLoadWaitingQueue.front();
305     mLoadWaitingQueue.erase(mLoadWaitingQueue.begin());
306     mOnLoading = true;
307     RequestFrameLoading(loadingIndex);
308   }
309
310   LOG_CACHE;
311 }
312
313 } //namespace Internal
314 } //namespace Toolkit
315 } //namespace Dali