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