[Tizen] CPU Alpha Masking for 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(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   mLoadStates.assign(mImageUrls.size(), TextureManager::LoadState::NOT_STARTED);
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   if(popExist || mQueue.IsEmpty())
88   {
89     uint32_t batchFrameIndex = frameIndex;
90     // If the frame of frameIndex was already loaded, load batch from the last frame of queue
91     if(!mQueue.IsEmpty())
92     {
93       batchFrameIndex = (mQueue.Back().mUrlIndex + 1) % mImageUrls.size();
94     }
95     LoadBatch(batchFrameIndex);
96   }
97
98   TextureSet textureSet;
99   if(IsFrontReady() == true)
100   {
101     textureSet = GetFrontTextureSet();
102   }
103
104   return textureSet;
105 }
106
107 TextureSet RollingImageCache::FirstFrame()
108 {
109   TextureSet textureSet = Frame(FIRST_FRAME_INDEX);
110   return textureSet;
111 }
112
113 uint32_t RollingImageCache::GetFrameInterval(uint32_t frameIndex) const
114 {
115   return mInterval;
116 }
117
118 int32_t RollingImageCache::GetCurrentFrameIndex() const
119 {
120   if(mQueue.IsEmpty())
121   {
122     return -1;
123   }
124   return mQueue.Front().mUrlIndex;
125 }
126
127 int32_t RollingImageCache::GetTotalFrameCount() const
128 {
129   return mImageUrls.size();
130 }
131
132 TextureManager::LoadState RollingImageCache::GetLoadState() const
133 {
134   return mLoadStates[mQueue.Front().mUrlIndex];
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     std::string& 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 UploadComplete 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 UploadComplete method using mRequestingLoad.
160     mRequestingLoad = true;
161
162     bool                               synchronousLoading = false;
163     bool                               atlasingStatus     = false;
164     bool                               loadingStatus      = false;
165     AtlasUploadObserver*               atlasObserver      = nullptr;
166     ImageAtlasManagerPtr               imageAtlasManager  = nullptr;
167     Vector4                            textureRect;
168     Dali::ImageDimensions              textureRectSize;
169     auto                               preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
170
171     TextureSet textureSet = mTextureManager.LoadTexture(
172       url, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::BOX_THEN_LINEAR, mMaskingData, synchronousLoading, mImageUrls[imageFrame.mUrlIndex].mTextureId, textureRect, textureRectSize, atlasingStatus, loadingStatus, Dali::WrapMode::Type::DEFAULT, Dali::WrapMode::Type::DEFAULT, this, atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED, preMultiply);
173
174     // If textureSet is returned but loadingState is false than load state is LOAD_FINISHED. (Notification is not comming yet.)
175     // If textureSet is null and the request is synchronous, load state is LOAD_FAILED.
176     // If textureSet is null but the request is asynchronous, the frame is still loading so load state is LOADING.
177     mLoadStates[frameIndex] = TextureManager::LoadState::LOADING;
178     if(textureSet)
179     {
180       if(!loadingStatus)
181       {
182         mLoadStates[frameIndex] = TextureManager::LoadState::LOAD_FINISHED;
183       }
184     }
185     else if(synchronousLoading)
186     {
187       // Synchronous loading is failed
188       mLoadStates[frameIndex] = TextureManager::LoadState::LOAD_FAILED;
189     }
190
191     mRequestingLoad = false;
192
193     ++frameIndex;
194     frameIndex %= mImageUrls.size();
195   }
196 }
197
198 void RollingImageCache::SetImageFrameReady(TextureManager::TextureId textureId, bool loadSuccess)
199 {
200   for(std::size_t i = 0; i < mQueue.Count(); ++i)
201   {
202     if(GetCachedTextureId(i) == textureId)
203     {
204       if(loadSuccess)
205       {
206         mLoadStates[i] = TextureManager::LoadState::LOAD_FINISHED;
207       }
208       else
209       {
210         mLoadStates[i] = TextureManager::LoadState::LOAD_FAILED;
211       }
212       mQueue[i].mReady = true;
213       break;
214     }
215   }
216 }
217
218 TextureSet RollingImageCache::GetFrontTextureSet() const
219 {
220   TextureManager::TextureId textureId = GetCachedTextureId(0);
221   return mTextureManager.GetTextureSet(textureId);
222 }
223
224 TextureManager::TextureId RollingImageCache::GetCachedTextureId(int index) const
225 {
226   return mImageUrls[mQueue[index].mUrlIndex].mTextureId;
227 }
228
229 void RollingImageCache::CheckFrontFrame(bool wasReady)
230 {
231   if(wasReady == false && IsFrontReady())
232   {
233     mObserver.FrameReady(GetFrontTextureSet(), mInterval);
234   }
235 }
236
237 void RollingImageCache::PopFrontCache()
238 {
239   ImageFrame imageFrame = mQueue.PopFront();
240   mTextureManager.Remove(mImageUrls[imageFrame.mUrlIndex].mTextureId, this);
241   mImageUrls[imageFrame.mUrlIndex].mTextureId = TextureManager::INVALID_TEXTURE_ID;
242
243   if(mMaskingData && mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID)
244   {
245     mTextureManager.Remove(mMaskingData->mAlphaMaskId, this);
246     if(mQueue.IsEmpty())
247     {
248       mMaskingData->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
249     }
250   }
251 }
252
253 void RollingImageCache::ClearCache()
254 {
255   while(mTextureManagerAlive && !mQueue.IsEmpty())
256   {
257     PopFrontCache();
258   }
259   mLoadStates.assign(mImageUrls.size(), TextureManager::LoadState::NOT_STARTED);
260 }
261
262 void RollingImageCache::UploadComplete(
263   bool           loadSuccess,
264   int32_t        textureId,
265   TextureSet     textureSet,
266   bool           useAtlasing,
267   const Vector4& atlasRect,
268   bool           preMultiplied)
269 {
270   DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "AnimatedImageVisual::UploadComplete(textureId:%d) start\n", textureId);
271   LOG_CACHE;
272
273   bool frontFrameReady = IsFrontReady();
274   SetImageFrameReady(textureId, loadSuccess);
275   if(!mRequestingLoad)
276   {
277     CheckFrontFrame(frontFrameReady);
278   }
279
280   LOG_CACHE;
281 }
282
283 } //namespace Internal
284 } //namespace Toolkit
285 } //namespace Dali