[dali_1.9.18] Merge branch 'devel/master'
[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 uint32_t RollingImageCache::GetFrameInterval( uint32_t frameIndex )
135 {
136   return 0u;
137 }
138
139 bool RollingImageCache::IsFrontReady() const
140 {
141   return ( !mQueue.IsEmpty() && mQueue.Front().mReady );
142 }
143
144 void RollingImageCache::LoadBatch()
145 {
146   // Try and load up to mBatchSize images, until the cache is filled.
147   // Once the cache is filled, as frames progress, the old frame is
148   // cleared, but not erased, and another image is loaded
149   bool frontFrameReady = IsFrontReady();
150
151   for( unsigned int i=0; i< mBatchSize && !mQueue.IsFull(); ++i )
152   {
153     ImageFrame imageFrame;
154
155     std::string& url = mImageUrls[ mUrlIndex ].mUrl;
156     imageFrame.mUrlIndex = mUrlIndex;
157     imageFrame.mReady = false;
158
159     ++mUrlIndex;
160     mUrlIndex %= mImageUrls.size();
161
162     mQueue.PushBack( imageFrame );
163
164     // Note, if the image is already loaded, then UploadComplete will get called
165     // from within this method. This means it won't yet have a texture id, so we
166     // need to account for this inside the UploadComplete method using mRequestingLoad.
167     mRequestingLoad = true;
168
169     bool synchronousLoading = false;
170     bool atlasingStatus = false;
171     bool loadingStatus = false;
172     TextureManager::MaskingDataPointer maskInfo = nullptr;
173     AtlasUploadObserver* atlasObserver = nullptr;
174     ImageAtlasManagerPtr imageAtlasManager = nullptr;
175     Vector4 textureRect;
176     Dali::ImageDimensions textureRectSize;
177     auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
178
179     mTextureManager.LoadTexture(
180       url, ImageDimensions(), FittingMode::SCALE_TO_FILL,
181       SamplingMode::BOX_THEN_LINEAR, maskInfo,
182       synchronousLoading, mImageUrls[ imageFrame.mUrlIndex ].mTextureId, textureRect, textureRectSize,
183       atlasingStatus, loadingStatus, Dali::WrapMode::Type::DEFAULT,
184       Dali::WrapMode::Type::DEFAULT, this,
185       atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED,
186       preMultiply );
187
188     mRequestingLoad = false;
189   }
190
191   CheckFrontFrame( frontFrameReady );
192 }
193
194 void RollingImageCache::SetImageFrameReady( TextureManager::TextureId textureId )
195 {
196   for( std::size_t i = 0; i < mQueue.Count() ; ++i )
197   {
198     if( GetCachedTextureId(i) == textureId )
199     {
200       mQueue[i].mReady = true;
201       break;
202     }
203   }
204 }
205
206 TextureSet RollingImageCache::GetFrontTextureSet() const
207 {
208   TextureManager::TextureId textureId = GetCachedTextureId( 0 );
209   return mTextureManager.GetTextureSet( textureId );
210 }
211
212 TextureManager::TextureId RollingImageCache::GetCachedTextureId( int index ) const
213 {
214   return mImageUrls[ mQueue[ index ].mUrlIndex ].mTextureId;
215 }
216
217 void RollingImageCache::CheckFrontFrame( bool wasReady )
218 {
219   if( mWaitingForReadyFrame && wasReady == false && IsFrontReady() )
220   {
221     mWaitingForReadyFrame = false;
222     mObserver.FrameReady( GetFrontTextureSet() );
223   }
224 }
225
226 void RollingImageCache::UploadComplete(
227   bool           loadSuccess,
228   int32_t        textureId,
229   TextureSet     textureSet,
230   bool           useAtlasing,
231   const Vector4& atlasRect,
232   bool           preMultiplied )
233 {
234   DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"AnimatedImageVisual::UploadComplete(textureId:%d) start\n", textureId);
235   LOG_CACHE;
236
237   bool frontFrameReady = IsFrontReady();
238
239   if( ! mRequestingLoad )
240   {
241     SetImageFrameReady( textureId );
242
243     CheckFrontFrame( frontFrameReady );
244   }
245   else
246   {
247     // UploadComplete has been called from within RequestLoad. TextureManager must
248     // therefore already have the texture cached, so make the texture ready.
249     // (Use the last texture, as the texture id hasn't been assigned yet)
250     mQueue.Back().mReady = true;
251   }
252
253   LOG_CACHE;
254 }
255
256 void RollingImageCache::LoadComplete(
257   bool loadSuccess,
258   Devel::PixelBuffer pixelBuffer,
259   const VisualUrl& url,
260   bool preMultiplied )
261 {
262   // LoadComplete is called if this TextureUploadObserver requested to load
263   // an image that will be returned as a type of PixelBuffer by using a method
264   // TextureManager::LoadPixelBuffer.
265 }
266
267 } //namespace Internal
268 } //namespace Toolkit
269 } //namespace Dali