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