Removed Texture Atlas for the GIF image.
[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
117     mTextureManager.LoadTexture(
118       url, ImageDimensions(), FittingMode::SCALE_TO_FILL,
119       SamplingMode::BOX_THEN_LINEAR, maskInfo,
120       synchronousLoading, mImageUrls[ mUrlIndex ].mTextureId, textureRect,
121       atlasingStatus, loadingStatus, Dali::WrapMode::Type::DEFAULT,
122       Dali::WrapMode::Type::DEFAULT, this,
123       atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED );
124
125     if( loadingStatus == false )  // not loading, means it's already ready.
126     {
127       SetImageFrameReady( mImageUrls[ mUrlIndex ].mTextureId );
128     }
129     mRequestingLoad = false;
130     ++mUrlIndex;
131   }
132
133   CheckFrontFrame( frontFrameReady );
134 }
135
136 void FixedImageCache::SetImageFrameReady( TextureManager::TextureId textureId )
137 {
138   for( std::size_t i = 0; i < mImageUrls.size() ; ++i )
139   {
140     if( mImageUrls[i].mTextureId == textureId )
141     {
142       mReadyFlags[i] = true;
143       break;
144     }
145   }
146 }
147
148 TextureSet FixedImageCache::GetFrontTextureSet() const
149 {
150   return mTextureManager.GetTextureSet( mImageUrls[mFront].mTextureId );
151 }
152
153 void FixedImageCache::CheckFrontFrame( bool wasReady )
154 {
155   if( mWaitingForReadyFrame && wasReady == false && IsFrontReady() )
156   {
157     mWaitingForReadyFrame = false;
158     mObserver.FrameReady( GetFrontTextureSet() );
159   }
160 }
161
162 void FixedImageCache::UploadComplete(
163   bool           loadSuccess,
164   int32_t        textureId,
165   TextureSet     textureSet,
166   bool           useAtlasing,
167   const Vector4& atlasRect )
168 {
169   bool frontFrameReady = IsFrontReady();
170
171   if( ! mRequestingLoad )
172   {
173     SetImageFrameReady( textureId );
174
175     CheckFrontFrame( frontFrameReady );
176   }
177   else
178   {
179     // UploadComplete has been called from within RequestLoad. TextureManager must
180     // therefore already have the texture cached, so make the texture ready.
181     // (Use the last texture, as the texture id hasn't been assigned yet)
182     mReadyFlags.back() = true;
183   }
184 }
185
186 } //namespace Internal
187 } //namespace Toolkit
188 } //namespace Dali