Fix 2 animatedImageVisual issues
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-image / rolling-image-cache.cpp
1 /*
2  * Copyright (c) 2020 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<<                                                         \
38         "={ tex:"<<mImageUrls[mQueue[_i].mUrlIndex].mTextureId<<        \
39         " urlId:"<<mQueue[_i].mUrlIndex<<                               \
40         " rdy:"<<(mQueue[_i].mReady?"T":"F")<< "}, ";                   \
41     }                                                                   \
42     oss<<" ]"<<std::endl;                                               \
43     DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"%s",oss.str().c_str()); \
44   }
45
46 #else
47   #define LOG_CACHE
48 #endif
49
50 const bool ENABLE_ORIENTATION_CORRECTION( true );
51
52 }
53
54 namespace Dali
55 {
56 namespace Toolkit
57 {
58 namespace Internal
59 {
60
61 RollingImageCache::RollingImageCache(
62   TextureManager& textureManager, UrlList& urlList, ImageCache::FrameReadyObserver& observer,
63   uint16_t cacheSize, uint16_t batchSize )
64 : ImageCache( textureManager, observer, batchSize ),
65   mImageUrls( urlList ),
66   mQueue( cacheSize )
67 {
68   LoadBatch();
69 }
70
71 RollingImageCache::~RollingImageCache()
72 {
73   if( mTextureManagerAlive )
74   {
75     while( !mQueue.IsEmpty() )
76     {
77       ImageFrame imageFrame = mQueue.PopFront();
78       mTextureManager.Remove( mImageUrls[ imageFrame.mUrlIndex ].mTextureId, this );
79     }
80   }
81 }
82
83 TextureSet RollingImageCache::Frame( uint32_t frameIndex )
84 {
85   // If a frame of frameIndex is not loaded, clear the queue and remove all loaded textures.
86   if( mImageUrls[ frameIndex ].mTextureId == TextureManager::INVALID_TEXTURE_ID )
87   {
88     mUrlIndex = frameIndex;
89     while( !mQueue.IsEmpty() )
90     {
91       ImageFrame imageFrame = mQueue.PopFront();
92       mTextureManager.Remove( mImageUrls[ imageFrame.mUrlIndex ].mTextureId, this );
93       mImageUrls[ imageFrame.mUrlIndex ].mTextureId = TextureManager::INVALID_TEXTURE_ID;
94     }
95     LoadBatch();
96   }
97   // If the frame is already loaded, remove previous frames of the frame in the queue
98   // and load new frames amount of removed frames.
99   else
100   {
101     bool popExist = false;
102     while( !mQueue.IsEmpty() && mQueue.Front().mUrlIndex != frameIndex )
103     {
104       ImageFrame imageFrame = mQueue.PopFront();
105       mTextureManager.Remove( mImageUrls[ imageFrame.mUrlIndex ].mTextureId, this );
106       mImageUrls[ imageFrame.mUrlIndex ].mTextureId = TextureManager::INVALID_TEXTURE_ID;
107       popExist = true;
108     }
109     if( popExist )
110     {
111       mUrlIndex = ( mQueue.Back().mUrlIndex + 1 ) % mImageUrls.size();
112       LoadBatch();
113     }
114   }
115
116   TextureSet textureSet;
117   if( IsFrontReady() == true )
118   {
119     textureSet = GetFrontTextureSet();
120   }
121   else
122   {
123     mWaitingForReadyFrame = true;
124   }
125
126   return textureSet;
127 }
128
129 TextureSet RollingImageCache::FirstFrame()
130 {
131   return Frame( 0u );
132 }
133
134 TextureSet RollingImageCache::NextFrame()
135 {
136   TextureSet textureSet;
137   if(!mQueue.IsEmpty())
138   {
139     uint32_t frameIndex = mQueue.Front().mUrlIndex;
140     if(IsFrontReady())
141     {
142       frameIndex = (frameIndex + 1) % mImageUrls.size();
143     }
144     textureSet = Frame(frameIndex);
145   }
146   else
147   {
148     DALI_LOG_ERROR("Cache is empty.");
149   }
150
151   return textureSet;
152 }
153
154 uint32_t RollingImageCache::GetFrameInterval( uint32_t frameIndex ) const
155 {
156   return 0u;
157 }
158
159 int32_t RollingImageCache::GetCurrentFrameIndex() const
160 {
161   if(mQueue.IsEmpty())
162   {
163     return -1;
164   }
165   return mQueue.Front().mUrlIndex;
166 }
167
168 bool RollingImageCache::IsFrontReady() const
169 {
170   return ( !mQueue.IsEmpty() && mQueue.Front().mReady );
171 }
172
173 void RollingImageCache::LoadBatch()
174 {
175   // Try and load up to mBatchSize images, until the cache is filled.
176   // Once the cache is filled, as frames progress, the old frame is
177   // cleared, but not erased, and another image is loaded
178   bool frontFrameReady = IsFrontReady();
179
180   for( unsigned int i=0; i< mBatchSize && !mQueue.IsFull(); ++i )
181   {
182     ImageFrame imageFrame;
183
184     std::string& url = mImageUrls[ mUrlIndex ].mUrl;
185     imageFrame.mUrlIndex = mUrlIndex;
186     imageFrame.mReady = false;
187
188     ++mUrlIndex;
189     mUrlIndex %= mImageUrls.size();
190
191     mQueue.PushBack( imageFrame );
192
193     // Note, if the image is already loaded, then UploadComplete will get called
194     // from within this method. This means it won't yet have a texture id, so we
195     // need to account for this inside the UploadComplete method using mRequestingLoad.
196     mRequestingLoad = true;
197
198     bool synchronousLoading = false;
199     bool atlasingStatus = false;
200     bool loadingStatus = false;
201     TextureManager::MaskingDataPointer maskInfo = nullptr;
202     AtlasUploadObserver* atlasObserver = nullptr;
203     ImageAtlasManagerPtr imageAtlasManager = nullptr;
204     Vector4 textureRect;
205     Dali::ImageDimensions textureRectSize;
206     auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
207
208     mTextureManager.LoadTexture(
209       url, ImageDimensions(), FittingMode::SCALE_TO_FILL,
210       SamplingMode::BOX_THEN_LINEAR, maskInfo,
211       synchronousLoading, mImageUrls[ imageFrame.mUrlIndex ].mTextureId, textureRect, textureRectSize,
212       atlasingStatus, loadingStatus, Dali::WrapMode::Type::DEFAULT,
213       Dali::WrapMode::Type::DEFAULT, this,
214       atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED,
215       preMultiply );
216
217     mRequestingLoad = false;
218   }
219
220   CheckFrontFrame( frontFrameReady );
221 }
222
223 void RollingImageCache::SetImageFrameReady( TextureManager::TextureId textureId )
224 {
225   for( std::size_t i = 0; i < mQueue.Count() ; ++i )
226   {
227     if( GetCachedTextureId(i) == textureId )
228     {
229       mQueue[i].mReady = true;
230       break;
231     }
232   }
233 }
234
235 TextureSet RollingImageCache::GetFrontTextureSet() const
236 {
237   TextureManager::TextureId textureId = GetCachedTextureId( 0 );
238   return mTextureManager.GetTextureSet( textureId );
239 }
240
241 TextureManager::TextureId RollingImageCache::GetCachedTextureId( int index ) const
242 {
243   return mImageUrls[ mQueue[ index ].mUrlIndex ].mTextureId;
244 }
245
246 void RollingImageCache::CheckFrontFrame( bool wasReady )
247 {
248   if( mWaitingForReadyFrame && wasReady == false && IsFrontReady() )
249   {
250     mWaitingForReadyFrame = false;
251     mObserver.FrameReady( GetFrontTextureSet() );
252   }
253 }
254
255 void RollingImageCache::UploadComplete(
256   bool           loadSuccess,
257   int32_t        textureId,
258   TextureSet     textureSet,
259   bool           useAtlasing,
260   const Vector4& atlasRect,
261   bool           preMultiplied )
262 {
263   DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"AnimatedImageVisual::UploadComplete(textureId:%d) start\n", textureId);
264   LOG_CACHE;
265
266   bool frontFrameReady = IsFrontReady();
267
268   if( ! mRequestingLoad )
269   {
270     SetImageFrameReady( textureId );
271
272     CheckFrontFrame( frontFrameReady );
273   }
274   else
275   {
276     // UploadComplete has been called from within RequestLoad. TextureManager must
277     // therefore already have the texture cached, so make the texture ready.
278     // (Use the last texture, as the texture id hasn't been assigned yet)
279     mQueue.Back().mReady = true;
280   }
281
282   LOG_CACHE;
283 }
284
285 void RollingImageCache::LoadComplete(
286   bool loadSuccess,
287   Devel::PixelBuffer pixelBuffer,
288   const VisualUrl& url,
289   bool preMultiplied )
290 {
291   // LoadComplete is called if this TextureUploadObserver requested to load
292   // an image that will be returned as a type of PixelBuffer by using a method
293   // TextureManager::LoadPixelBuffer.
294 }
295
296 } //namespace Internal
297 } //namespace Toolkit
298 } //namespace Dali