[dali_2.3.19] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-image / rolling-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 <dali-toolkit/internal/visuals/animated-image/rolling-image-cache.h>
19
20 // INTERNAL HEADERS
21 #include <dali-toolkit/internal/visuals/image-atlas-manager.h> // For ImageAtlasManagerPtr
22 #include <dali/integration-api/debug.h>
23
24 // EXTERNAL HEADERS
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 << "={ tex:" << mImageUrls[mQueue[_i].mUrlIndex].mTextureId << " urlId:" << mQueue[_i].mUrlIndex << " rdy:" << (mQueue[_i].mReady ? "T" : "F") << "}, "; \
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 static constexpr bool ENABLE_ORIENTATION_CORRECTION(true);
49
50 static constexpr uint32_t FIRST_FRAME_INDEX = 0u;
51
52 } // namespace
53
54 namespace Dali
55 {
56 namespace Toolkit
57 {
58 namespace Internal
59 {
60 RollingImageCache::RollingImageCache(TextureManager&                     textureManager,
61                                      UrlList&                            urlList,
62                                      TextureManager::MaskingDataPointer& maskingData,
63                                      ImageCache::FrameReadyObserver&     observer,
64                                      uint16_t                            cacheSize,
65                                      uint16_t                            batchSize,
66                                      uint32_t                            interval)
67 : ImageCache(textureManager, maskingData, observer, batchSize, interval),
68   mImageUrls(urlList),
69   mQueue(cacheSize)
70 {
71 }
72
73 RollingImageCache::~RollingImageCache()
74 {
75   ClearCache();
76 }
77
78 TextureSet RollingImageCache::Frame(uint32_t frameIndex)
79 {
80   // Pop frames until the frame of frameIndex become front frame.
81   bool popExist = false;
82   while(!mQueue.IsEmpty() && mQueue.Front().mUrlIndex != frameIndex)
83   {
84     PopFrontCache();
85     popExist = true;
86   }
87
88   // TODO: synchronous loading of first frame.
89   if(popExist || mQueue.IsEmpty())
90   {
91     uint32_t batchFrameIndex = frameIndex;
92     // If the frame of frameIndex was already loaded, load batch from the last frame of queue
93     if(!mQueue.IsEmpty())
94     {
95       batchFrameIndex = (mQueue.Back().mUrlIndex + 1) % mImageUrls.size();
96     }
97     LoadBatch(batchFrameIndex);
98   }
99
100   TextureSet textureSet;
101   if(IsFrontReady() == true && mLoadState != TextureManager::LoadState::LOAD_FAILED)
102   {
103     textureSet = GetFrontTextureSet();
104   }
105
106   return textureSet;
107 }
108
109 TextureSet RollingImageCache::FirstFrame()
110 {
111   TextureSet textureSet = Frame(FIRST_FRAME_INDEX);
112   return textureSet;
113 }
114
115 uint32_t RollingImageCache::GetFrameInterval(uint32_t frameIndex) const
116 {
117   return mInterval;
118 }
119
120 int32_t RollingImageCache::GetCurrentFrameIndex() const
121 {
122   if(mQueue.IsEmpty())
123   {
124     return -1;
125   }
126   return mQueue.Front().mUrlIndex;
127 }
128
129 int32_t RollingImageCache::GetTotalFrameCount() const
130 {
131   return mImageUrls.size();
132 }
133
134 bool RollingImageCache::IsFrontReady() const
135 {
136   return (!mQueue.IsEmpty() && mQueue.Front().mReady);
137 }
138
139 void RollingImageCache::LoadBatch(uint32_t frameIndex)
140 {
141   // Try and load up to mBatchSize images, until the cache is filled.
142   // Once the cache is filled, as frames progress, the old frame is
143   // cleared, but not erased, and another image is loaded
144   for(unsigned int i = 0; i < mBatchSize && !mQueue.IsFull(); ++i)
145   {
146     ImageFrame imageFrame;
147
148     VisualUrl& url       = mImageUrls[frameIndex].mUrl;
149     imageFrame.mUrlIndex = frameIndex;
150     imageFrame.mReady    = false;
151
152     mQueue.PushBack(imageFrame);
153
154     // Note, if the image is already loaded, then LoadComplete will get called
155     // from within this method. This means it won't yet have a texture id, so we
156     // need to account for this inside the LoadComplete method using mRequestingLoad.
157     mRequestingLoad = true;
158     mLoadState      = TextureManager::LoadState::LOADING;
159
160     bool                  synchronousLoading = false;
161     bool                  atlasingStatus     = false;
162     bool                  loadingStatus      = false;
163     AtlasUploadObserver*  atlasObserver      = nullptr;
164     ImageAtlasManagerPtr  imageAtlasManager  = nullptr;
165     Vector4               textureRect;
166     Dali::ImageDimensions textureRectSize;
167     auto                  preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
168
169     TextureManager::TextureId loadTextureId = TextureManager::INVALID_TEXTURE_ID;
170     TextureSet                textureSet    = mTextureManager.LoadTexture(
171       url, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::BOX_THEN_LINEAR, mMaskingData, synchronousLoading, loadTextureId, textureRect, textureRectSize, atlasingStatus, loadingStatus, Dali::WrapMode::Type::DEFAULT, Dali::WrapMode::Type::DEFAULT, this, atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED, preMultiply);
172     mImageUrls[imageFrame.mUrlIndex].mTextureId = loadTextureId;
173
174     mRequestingLoad = false;
175
176     ++frameIndex;
177     frameIndex %= mImageUrls.size();
178   }
179 }
180
181 TextureSet RollingImageCache::GetFrontTextureSet() const
182 {
183   TextureManager::TextureId textureId = GetCachedTextureId(0);
184   return mTextureManager.GetTextureSet(textureId);
185 }
186
187 TextureManager::TextureId RollingImageCache::GetCachedTextureId(int index) const
188 {
189   return mImageUrls[mQueue[index].mUrlIndex].mTextureId;
190 }
191
192 void RollingImageCache::PopFrontCache()
193 {
194   ImageFrame imageFrame = mQueue.PopFront();
195   mTextureManager.Remove(mImageUrls[imageFrame.mUrlIndex].mTextureId, this);
196   mImageUrls[imageFrame.mUrlIndex].mTextureId = TextureManager::INVALID_TEXTURE_ID;
197
198   if(mMaskingData && mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID)
199   {
200     mTextureManager.Remove(mMaskingData->mAlphaMaskId, this);
201     if(mQueue.IsEmpty())
202     {
203       mMaskingData->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
204     }
205   }
206 }
207
208 void RollingImageCache::ClearCache()
209 {
210   while(mTextureManagerAlive && !mQueue.IsEmpty())
211   {
212     PopFrontCache();
213   }
214   mLoadState = TextureManager::LoadState::NOT_STARTED;
215 }
216
217 void RollingImageCache::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
218 {
219   DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "AnimatedImageVisual::LoadComplete(textureId:%d) start\n", textureInformation.textureId);
220   LOG_CACHE;
221
222   if(loadSuccess)
223   {
224     mLoadState           = TextureManager::LoadState::LOAD_FINISHED;
225     bool frontFrameReady = IsFrontReady();
226     if(!mRequestingLoad)
227     {
228       for(std::size_t i = 0; i < mQueue.Count(); ++i)
229       {
230         if(GetCachedTextureId(i) == textureInformation.textureId)
231         {
232           mQueue[i].mReady = true;
233           break;
234         }
235       }
236     }
237     else
238     {
239       // LoadComplete has been called from within RequestLoad. TextureManager must
240       // therefore already have the texture cached, so make the texture ready.
241       // (Use the last texture, as the texture id hasn't been assigned yet)
242       mQueue.Back().mReady = true;
243     }
244
245     if(!frontFrameReady && IsFrontReady())
246     {
247       mObserver.FrameReady(mTextureManager.GetTextureSet(textureInformation.textureId), mInterval);
248     }
249   }
250   else
251   {
252     mLoadState = TextureManager::LoadState::LOAD_FAILED;
253     mObserver.FrameReady(TextureSet(), 0);
254   }
255
256   LOG_CACHE;
257 }
258
259 } //namespace Internal
260 } //namespace Toolkit
261 } //namespace Dali