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