Merge "[4.0] Check line is empty or not before getting line of character" into tizen_4.0
[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
152     mTextureManager.LoadTexture(
153       url, ImageDimensions(), FittingMode::SCALE_TO_FILL,
154       SamplingMode::BOX_THEN_LINEAR, maskInfo,
155       synchronousLoading, mImageUrls[ imageFrame.mUrlIndex ].mTextureId, textureRect,
156       atlasingStatus, loadingStatus, Dali::WrapMode::Type::DEFAULT,
157       Dali::WrapMode::Type::DEFAULT, this,
158       atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED );
159
160     mRequestingLoad = false;
161   }
162
163   CheckFrontFrame( frontFrameReady );
164 }
165
166 void RollingImageCache::SetImageFrameReady( TextureManager::TextureId textureId )
167 {
168   for( std::size_t i = 0; i < mQueue.Count() ; ++i )
169   {
170     if( GetCachedTextureId(i) == textureId )
171     {
172       mQueue[i].mReady = true;
173       break;
174     }
175   }
176 }
177
178 TextureSet RollingImageCache::GetFrontTextureSet() const
179 {
180   TextureManager::TextureId textureId = GetCachedTextureId( 0 );
181   return mTextureManager.GetTextureSet( textureId );
182 }
183
184 TextureManager::TextureId RollingImageCache::GetCachedTextureId( int index ) const
185 {
186   return mImageUrls[ mQueue[ index ].mUrlIndex ].mTextureId;
187 }
188
189 void RollingImageCache::CheckFrontFrame( bool wasReady )
190 {
191   if( mWaitingForReadyFrame && wasReady == false && IsFrontReady() )
192   {
193     mWaitingForReadyFrame = false;
194     mObserver.FrameReady( GetFrontTextureSet() );
195   }
196 }
197
198 void RollingImageCache::UploadComplete(
199   bool           loadSuccess,
200   int32_t        textureId,
201   TextureSet     textureSet,
202   bool           useAtlasing,
203   const Vector4& atlasRect )
204 {
205   DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"AnimatedImageVisual::UploadComplete(textureId:%d) start\n", textureId);
206   LOG_CACHE;
207
208   bool frontFrameReady = IsFrontReady();
209
210   if( ! mRequestingLoad )
211   {
212     SetImageFrameReady( textureId );
213
214     CheckFrontFrame( frontFrameReady );
215   }
216   else
217   {
218     // UploadComplete has been called from within RequestLoad. TextureManager must
219     // therefore already have the texture cached, so make the texture ready.
220     // (Use the last texture, as the texture id hasn't been assigned yet)
221     mQueue.Back().mReady = true;
222   }
223
224   LOG_CACHE;
225 }
226
227 } //namespace Internal
228 } //namespace Toolkit
229 } //namespace Dali