[dali_1.2.63] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-image / fixed-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/fixed-image-cache.h>
19
20 namespace Dali
21 {
22 namespace Toolkit
23 {
24 namespace Internal
25 {
26
27 namespace
28 {
29 const bool ENABLE_ORIENTATION_CORRECTION( true );
30 } // namespace
31
32 FixedImageCache::FixedImageCache(
33   TextureManager& textureManager, UrlList& urlList, ImageCache::FrameReadyObserver& observer,
34   unsigned int batchSize )
35 : ImageCache( textureManager, urlList, observer, batchSize ),
36   mFront(0u)
37 {
38   mReadyFlags.reserve( mImageUrls.size() );
39   LoadBatch();
40 }
41
42 FixedImageCache::~FixedImageCache()
43 {
44   for( std::size_t i = 0; i < mImageUrls.size() ; ++i )
45   {
46     mTextureManager.Remove( mImageUrls[i].mTextureId );
47   }
48 }
49
50 TextureSet FixedImageCache::FirstFrame()
51 {
52   TextureSet textureSet = GetFrontTextureSet();
53
54   if( ! textureSet )
55   {
56     mWaitingForReadyFrame = true;
57   }
58
59   return textureSet;
60 }
61
62 TextureSet FixedImageCache::NextFrame()
63 {
64   TextureSet textureSet;
65   ++mFront;
66   mFront %= mImageUrls.size();
67
68   if( IsFrontReady() == true )
69   {
70     textureSet = GetFrontTextureSet();
71   }
72   else
73   {
74     mWaitingForReadyFrame = true;
75   }
76
77   LoadBatch();
78
79   return textureSet;
80 }
81
82 bool FixedImageCache::IsFrontReady() const
83 {
84   return ( mReadyFlags.size() > 0 && mReadyFlags[mFront] == true );
85 }
86
87 void FixedImageCache::LoadBatch()
88 {
89   // Try and load up to mBatchSize images, until the cache is filled.
90   // Once the cache is filled, mUrlIndex exceeds mImageUrls size and
91   // no more images are loaded.
92   bool frontFrameReady = IsFrontReady();;
93
94   for( unsigned int i=0; i< mBatchSize && mUrlIndex < mImageUrls.size(); ++i )
95   {
96     std::string& url = mImageUrls[ mUrlIndex ].mUrl;
97
98     mReadyFlags.push_back(false);
99
100     // Note, if the image is already loaded, then UploadComplete will get called
101     // from within this method. This means it won't yet have a texture id, so we
102     // need to account for this inside the UploadComplete method using mRequestingLoad.
103     mRequestingLoad = true;
104
105     mImageUrls[ mUrlIndex ].mTextureId =
106       mTextureManager.RequestLoad( url, ImageDimensions(), FittingMode::SCALE_TO_FILL,
107                                    SamplingMode::BOX_THEN_LINEAR, TextureManager::NO_ATLAS,
108                                    this, ENABLE_ORIENTATION_CORRECTION );
109     mRequestingLoad = false;
110     ++mUrlIndex;
111   }
112
113   CheckFrontFrame( frontFrameReady );
114 }
115
116 void FixedImageCache::SetImageFrameReady( TextureManager::TextureId textureId )
117 {
118   for( std::size_t i = 0; i < mImageUrls.size() ; ++i )
119   {
120     if( mImageUrls[i].mTextureId == textureId )
121     {
122       mReadyFlags[i] = true;
123       break;
124     }
125   }
126 }
127
128 TextureSet FixedImageCache::GetFrontTextureSet() const
129 {
130   return mTextureManager.GetTextureSet( mImageUrls[mFront].mTextureId );
131 }
132
133 void FixedImageCache::CheckFrontFrame( bool wasReady )
134 {
135   if( mWaitingForReadyFrame && wasReady == false && IsFrontReady() )
136   {
137     mWaitingForReadyFrame = false;
138     mObserver.FrameReady( GetFrontTextureSet() );
139   }
140 }
141
142 void FixedImageCache::UploadComplete(
143   bool           loadSuccess,
144   int32_t        textureId,
145   TextureSet     textureSet,
146   bool           useAtlasing,
147   const Vector4& atlasRect )
148 {
149   bool frontFrameReady = IsFrontReady();
150
151   if( ! mRequestingLoad )
152   {
153     SetImageFrameReady( textureId );
154
155     CheckFrontFrame( frontFrameReady );
156   }
157   else
158   {
159     // UploadComplete has been called from within RequestLoad. TextureManager must
160     // therefore already have the texture cached, so make the texture ready.
161     // (Use the last texture, as the texture id hasn't been assigned yet)
162     mReadyFlags.back() = true;
163   }
164 }
165
166 } //namespace Internal
167 } //namespace Toolkit
168 } //namespace Dali