Merge "Added automatic premultiplication of image visual images" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-image / rolling-image-cache.cpp
1 /*
2  * Copyright (c) 2017 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   while( !mQueue.IsEmpty() )
74   {
75     ImageFrame imageFrame = mQueue.PopFront();
76     mTextureManager.Remove( mImageUrls[ imageFrame.mUrlIndex ].mTextureId );
77   }
78 }
79
80 TextureSet RollingImageCache::FirstFrame()
81 {
82   TextureSet textureSet = GetFrontTextureSet();
83
84   if( ! textureSet )
85   {
86     mWaitingForReadyFrame = true;
87   }
88
89   return textureSet;
90 }
91
92 TextureSet RollingImageCache::NextFrame()
93 {
94   TextureSet textureSet;
95
96   ImageFrame imageFrame = mQueue.PopFront();
97   mTextureManager.Remove( mImageUrls[ imageFrame.mUrlIndex ].mTextureId );
98   mImageUrls[ imageFrame.mUrlIndex ].mTextureId = TextureManager::INVALID_TEXTURE_ID;
99
100   if( IsFrontReady() == true )
101   {
102     textureSet = GetFrontTextureSet();
103   }
104   else
105   {
106     mWaitingForReadyFrame = true;
107   }
108
109   LoadBatch();
110
111   return textureSet;
112 }
113
114 bool RollingImageCache::IsFrontReady() const
115 {
116   return ( !mQueue.IsEmpty() && mQueue.Front().mReady );
117 }
118
119 void RollingImageCache::LoadBatch()
120 {
121   // Try and load up to mBatchSize images, until the cache is filled.
122   // Once the cache is filled, as frames progress, the old frame is
123   // cleared, but not erased, and another image is loaded
124   bool frontFrameReady = IsFrontReady();;
125
126   for( unsigned int i=0; i< mBatchSize && !mQueue.IsFull(); ++i )
127   {
128     ImageFrame imageFrame;
129
130     std::string& url = mImageUrls[ mUrlIndex ].mUrl;
131     imageFrame.mUrlIndex = mUrlIndex;
132     imageFrame.mReady = false;
133
134     ++mUrlIndex;
135     mUrlIndex %= mImageUrls.size();
136
137     mQueue.PushBack( imageFrame );
138
139     // Note, if the image is already loaded, then UploadComplete will get called
140     // from within this method. This means it won't yet have a texture id, so we
141     // need to account for this inside the UploadComplete method using mRequestingLoad.
142     mRequestingLoad = true;
143
144     bool synchronousLoading = false;
145     bool atlasingStatus = false;
146     bool loadingStatus = false;
147     TextureManager::MaskingDataPointer maskInfo = nullptr;
148     AtlasUploadObserver* atlasObserver = nullptr;
149     ImageAtlasManagerPtr imageAtlasManager = nullptr;
150     Vector4 textureRect;
151     auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
152
153     mTextureManager.LoadTexture(
154       url, ImageDimensions(), FittingMode::SCALE_TO_FILL,
155       SamplingMode::BOX_THEN_LINEAR, maskInfo,
156       synchronousLoading, mImageUrls[ imageFrame.mUrlIndex ].mTextureId, textureRect,
157       atlasingStatus, loadingStatus, Dali::WrapMode::Type::DEFAULT,
158       Dali::WrapMode::Type::DEFAULT, this,
159       atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED,
160       preMultiply );
161
162     mRequestingLoad = false;
163   }
164
165   CheckFrontFrame( frontFrameReady );
166 }
167
168 void RollingImageCache::SetImageFrameReady( TextureManager::TextureId textureId )
169 {
170   for( std::size_t i = 0; i < mQueue.Count() ; ++i )
171   {
172     if( GetCachedTextureId(i) == textureId )
173     {
174       mQueue[i].mReady = true;
175       break;
176     }
177   }
178 }
179
180 TextureSet RollingImageCache::GetFrontTextureSet() const
181 {
182   TextureManager::TextureId textureId = GetCachedTextureId( 0 );
183   return mTextureManager.GetTextureSet( textureId );
184 }
185
186 TextureManager::TextureId RollingImageCache::GetCachedTextureId( int index ) const
187 {
188   return mImageUrls[ mQueue[ index ].mUrlIndex ].mTextureId;
189 }
190
191 void RollingImageCache::CheckFrontFrame( bool wasReady )
192 {
193   if( mWaitingForReadyFrame && wasReady == false && IsFrontReady() )
194   {
195     mWaitingForReadyFrame = false;
196     mObserver.FrameReady( GetFrontTextureSet() );
197   }
198 }
199
200 void RollingImageCache::UploadComplete(
201   bool           loadSuccess,
202   int32_t        textureId,
203   TextureSet     textureSet,
204   bool           useAtlasing,
205   const Vector4& atlasRect,
206   bool           preMultiplied )
207 {
208   DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"AnimatedImageVisual::UploadComplete(textureId:%d) start\n", textureId);
209   LOG_CACHE;
210
211   bool frontFrameReady = IsFrontReady();
212
213   if( ! mRequestingLoad )
214   {
215     SetImageFrameReady( textureId );
216
217     CheckFrontFrame( frontFrameReady );
218   }
219   else
220   {
221     // UploadComplete has been called from within RequestLoad. TextureManager must
222     // therefore already have the texture cached, so make the texture ready.
223     // (Use the last texture, as the texture id hasn't been assigned yet)
224     mQueue.Back().mReady = true;
225   }
226
227   LOG_CACHE;
228 }
229
230 } //namespace Internal
231 } //namespace Toolkit
232 } //namespace Dali