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