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