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