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