Caching texture instead of textureSet in TextureManager
[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, 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   TextureSet textureSet = mTextureManager.GetTextureSet(textureId);
185   if(textureSet)
186   {
187     Sampler sampler = Sampler::New();
188     sampler.SetWrapMode(Dali::WrapMode::Type::DEFAULT, Dali::WrapMode::Type::DEFAULT);
189     textureSet.SetSampler(0u, sampler);
190   }
191   return textureSet;
192 }
193
194 TextureManager::TextureId RollingImageCache::GetCachedTextureId(int index) const
195 {
196   return mImageUrls[mQueue[index].mUrlIndex].mTextureId;
197 }
198
199 void RollingImageCache::PopFrontCache()
200 {
201   ImageFrame imageFrame = mQueue.PopFront();
202   mTextureManager.Remove(mImageUrls[imageFrame.mUrlIndex].mTextureId, this);
203   mImageUrls[imageFrame.mUrlIndex].mTextureId = TextureManager::INVALID_TEXTURE_ID;
204
205   if(mMaskingData && mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID)
206   {
207     mTextureManager.Remove(mMaskingData->mAlphaMaskId, this);
208     if(mQueue.IsEmpty())
209     {
210       mMaskingData->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
211     }
212   }
213 }
214
215 void RollingImageCache::ClearCache()
216 {
217   while(mTextureManagerAlive && !mQueue.IsEmpty())
218   {
219     PopFrontCache();
220   }
221   mLoadState = TextureManager::LoadState::NOT_STARTED;
222 }
223
224 void RollingImageCache::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
225 {
226   DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "AnimatedImageVisual::LoadComplete(textureId:%d) start\n", textureInformation.textureId);
227   LOG_CACHE;
228
229   if(loadSuccess)
230   {
231     mLoadState           = TextureManager::LoadState::LOAD_FINISHED;
232     bool frontFrameReady = IsFrontReady();
233     if(!mRequestingLoad)
234     {
235       for(std::size_t i = 0; i < mQueue.Count(); ++i)
236       {
237         if(GetCachedTextureId(i) == textureInformation.textureId)
238         {
239           mQueue[i].mReady = true;
240           break;
241         }
242       }
243     }
244     else
245     {
246       // LoadComplete has been called from within RequestLoad. TextureManager must
247       // therefore already have the texture cached, so make the texture ready.
248       // (Use the last texture, as the texture id hasn't been assigned yet)
249       mQueue.Back().mReady = true;
250     }
251
252     if(!frontFrameReady && IsFrontReady())
253     {
254       if(textureInformation.textureSet)
255       {
256         Sampler sampler = Sampler::New();
257         sampler.SetWrapMode(Dali::WrapMode::Type::DEFAULT, Dali::WrapMode::Type::DEFAULT);
258         textureInformation.textureSet.SetSampler(0u, sampler);
259       }
260       mObserver.FrameReady(textureInformation.textureSet, mInterval);
261     }
262   }
263   else
264   {
265     mLoadState = TextureManager::LoadState::LOAD_FAILED;
266     mObserver.FrameReady(TextureSet(), 0);
267   }
268
269   LOG_CACHE;
270 }
271
272 } //namespace Internal
273 } //namespace Toolkit
274 } //namespace Dali