Merge "Adding support for multiple images in AnimatedImageVisual" 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/integration-api/debug.h>
22
23 // EXTERNAL HEADERS
24
25 namespace
26 {
27 #if defined(DEBUG_ENABLED)
28 Debug::Filter* gAnimImgLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_ANIMATED_IMAGE");
29
30 #define LOG_CACHE                                                       \
31   {                                                                     \
32     std::ostringstream oss;                                             \
33     oss<<"Size:"<<mQueue.Count()<<" [ ";                                \
34     for(std::size_t _i=0; _i<mQueue.Count(); ++_i)                      \
35     {                                                                   \
36       oss<<_i<<                                                         \
37         "={ tex:"<<mImageUrls[mQueue[_i].mUrlIndex].mTextureId<<        \
38         " urlId:"<<mQueue[_i].mUrlIndex<<                               \
39         " rdy:"<<(mQueue[_i].mReady?"T":"F")<< "}, ";                   \
40     }                                                                   \
41     oss<<" ]"<<std::endl;                                               \
42     DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"%s",oss.str().c_str()); \
43   }
44
45 #else
46   #define LOG_CACHE
47 #endif
48 }
49
50 namespace Dali
51 {
52 namespace Toolkit
53 {
54 namespace Internal
55 {
56
57 RollingImageCache::RollingImageCache(
58   TextureManager& textureManager, UrlList& urlList, ImageCache::FrameReadyObserver& observer,
59   uint16_t cacheSize, uint16_t batchSize )
60 : ImageCache( textureManager, urlList, observer, batchSize ),
61   mQueue( cacheSize )
62 {
63   LoadBatch();
64 }
65
66 RollingImageCache::~RollingImageCache()
67 {
68   while( !mQueue.IsEmpty() )
69   {
70     ImageFrame imageFrame = mQueue.PopFront();
71     mTextureManager.Remove( mImageUrls[ imageFrame.mUrlIndex ].mTextureId );
72   }
73 }
74
75 TextureSet RollingImageCache::FirstFrame()
76 {
77   TextureSet textureSet = GetFrontTextureSet();
78
79   if( ! textureSet )
80   {
81     mWaitingForReadyFrame = true;
82   }
83
84   return textureSet;
85 }
86
87 TextureSet RollingImageCache::NextFrame()
88 {
89   TextureSet textureSet;
90
91   ImageFrame imageFrame = mQueue.PopFront();
92   mTextureManager.Remove( mImageUrls[ imageFrame.mUrlIndex ].mTextureId );
93   mImageUrls[ imageFrame.mUrlIndex ].mTextureId = TextureManager::INVALID_TEXTURE_ID;
94
95   if( IsFrontReady() == true )
96   {
97     textureSet = GetFrontTextureSet();
98   }
99   else
100   {
101     mWaitingForReadyFrame = true;
102   }
103
104   LoadBatch();
105
106   return textureSet;
107 }
108
109 bool RollingImageCache::IsFrontReady() const
110 {
111   return ( !mQueue.IsEmpty() && mQueue.Front().mReady );
112 }
113
114 void RollingImageCache::LoadBatch()
115 {
116   // Try and load up to mBatchSize images, until the cache is filled.
117   // Once the cache is filled, as frames progress, the old frame is
118   // cleared, but not erased, and another image is loaded
119   bool frontFrameReady = IsFrontReady();;
120
121   for( unsigned int i=0; i< mBatchSize && !mQueue.IsFull(); ++i )
122   {
123     ImageFrame imageFrame;
124
125     std::string& url = mImageUrls[ mUrlIndex ].mUrl;
126     imageFrame.mUrlIndex = mUrlIndex;
127     imageFrame.mReady = false;
128
129     ++mUrlIndex;
130     mUrlIndex %= mImageUrls.size();
131
132     mQueue.PushBack( imageFrame );
133
134     // Note, if the image is already loaded, then UploadComplete will get called
135     // from within this method. This means it won't yet have a texture id, so we
136     // need to account for this inside the UploadComplete method using mRequestingLoad.
137     mRequestingLoad = true;
138
139     mImageUrls[ imageFrame.mUrlIndex ].mTextureId =
140       mTextureManager.RequestLoad( url, ImageDimensions(), FittingMode::SCALE_TO_FILL,
141                                    SamplingMode::BOX_THEN_LINEAR, TextureManager::NO_ATLAS,
142                                    this );
143     mRequestingLoad = false;
144   }
145
146   CheckFrontFrame( frontFrameReady );
147 }
148
149 void RollingImageCache::SetImageFrameReady( TextureManager::TextureId textureId )
150 {
151   for( std::size_t i = 0; i < mQueue.Count() ; ++i )
152   {
153     if( GetCachedTextureId(i) == textureId )
154     {
155       mQueue[i].mReady = true;
156       break;
157     }
158   }
159 }
160
161 TextureSet RollingImageCache::GetFrontTextureSet() const
162 {
163   TextureManager::TextureId textureId = GetCachedTextureId( 0 );
164   return mTextureManager.GetTextureSet( textureId );
165 }
166
167 TextureManager::TextureId RollingImageCache::GetCachedTextureId( int index ) const
168 {
169   return mImageUrls[ mQueue[ index ].mUrlIndex ].mTextureId;
170 }
171
172 void RollingImageCache::CheckFrontFrame( bool wasReady )
173 {
174   if( mWaitingForReadyFrame && wasReady == false && IsFrontReady() )
175   {
176     mWaitingForReadyFrame = false;
177     mObserver.FrameReady( GetFrontTextureSet() );
178   }
179 }
180
181 void RollingImageCache::UploadComplete(
182   bool           loadSuccess,
183   int32_t        textureId,
184   TextureSet     textureSet,
185   bool           useAtlasing,
186   const Vector4& atlasRect )
187 {
188   DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"AnimatedImageVisual::UploadComplete(textureId:%d) start\n", textureId);
189   LOG_CACHE;
190
191   bool frontFrameReady = IsFrontReady();
192
193   if( ! mRequestingLoad )
194   {
195     SetImageFrameReady( textureId );
196
197     CheckFrontFrame( frontFrameReady );
198   }
199   else
200   {
201     // UploadComplete has been called from within RequestLoad. TextureManager must
202     // therefore already have the texture cached, so make the texture ready.
203     // (Use the last texture, as the texture id hasn't been assigned yet)
204     mQueue.Back().mReady = true;
205   }
206
207   LOG_CACHE;
208 }
209
210 } //namespace Internal
211 } //namespace Toolkit
212 } //namespace Dali