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