[dali_1.9.6] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-image / fixed-image-cache.cpp
1 /*
2  * Copyright (c) 2020 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   if( mTextureManagerAlive )
49   {
50     for( std::size_t i = 0; i < mImageUrls.size() ; ++i )
51     {
52       mTextureManager.Remove( mImageUrls[i].mTextureId, this );
53     }
54   }
55 }
56
57 TextureSet FixedImageCache::Frame( uint32_t frameIndex )
58 {
59   while( frameIndex > mFront )
60   {
61     NextFrame();
62   }
63   mFront = frameIndex;
64
65   TextureSet textureSet;
66   if( IsFrontReady() == true )
67   {
68     textureSet = GetFrontTextureSet();
69   }
70   else
71   {
72     mWaitingForReadyFrame = true;
73   }
74
75   return textureSet;
76 }
77
78 TextureSet FixedImageCache::FirstFrame()
79 {
80   TextureSet textureSet = GetFrontTextureSet();
81
82   if( ! textureSet )
83   {
84     mWaitingForReadyFrame = true;
85   }
86
87   return textureSet;
88 }
89
90 TextureSet FixedImageCache::NextFrame()
91 {
92   TextureSet textureSet;
93   ++mFront;
94   mFront %= mImageUrls.size();
95
96   if( IsFrontReady() == true )
97   {
98     textureSet = GetFrontTextureSet();
99   }
100   else
101   {
102     mWaitingForReadyFrame = true;
103   }
104   LoadBatch();
105
106   return textureSet;
107 }
108
109 bool FixedImageCache::IsFrontReady() const
110 {
111   return ( mReadyFlags.size() > 0 && mReadyFlags[mFront] == true );
112 }
113
114 void FixedImageCache::LoadBatch()
115 {
116   // Try and load up to mBatchSize images, until the cache is filled.
117   // Once the cache is filled, mUrlIndex exceeds mImageUrls size and
118   // no more images are loaded.
119   bool frontFrameReady = IsFrontReady();
120
121   for( unsigned int i=0; i< mBatchSize && mUrlIndex < mImageUrls.size(); ++i )
122   {
123     std::string& url = mImageUrls[ mUrlIndex ].mUrl;
124
125     mReadyFlags.push_back(false);
126
127     // Note, if the image is already loaded, then UploadComplete will get called
128     // from within this method. This means it won't yet have a texture id, so we
129     // need to account for this inside the UploadComplete method using mRequestingLoad.
130     mRequestingLoad = true;
131
132     bool synchronousLoading = false;
133     bool atlasingStatus = false;
134     bool loadingStatus = false;
135     TextureManager::MaskingDataPointer maskInfo = nullptr;
136     AtlasUploadObserver* atlasObserver = nullptr;
137     ImageAtlasManagerPtr imageAtlasManager = nullptr;
138     Vector4 textureRect;
139     Dali::ImageDimensions textureRectSize;
140     auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
141
142     mTextureManager.LoadTexture(
143       url, ImageDimensions(), FittingMode::SCALE_TO_FILL,
144       SamplingMode::BOX_THEN_LINEAR, maskInfo,
145       synchronousLoading, mImageUrls[ mUrlIndex ].mTextureId, textureRect, textureRectSize,
146       atlasingStatus, loadingStatus, Dali::WrapMode::Type::DEFAULT,
147       Dali::WrapMode::Type::DEFAULT, this,
148       atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED,
149       preMultiply );
150
151     if( loadingStatus == false )  // not loading, means it's already ready.
152     {
153       SetImageFrameReady( mImageUrls[ mUrlIndex ].mTextureId );
154     }
155     mRequestingLoad = false;
156     ++mUrlIndex;
157   }
158
159   CheckFrontFrame( frontFrameReady );
160 }
161
162 void FixedImageCache::SetImageFrameReady( TextureManager::TextureId textureId )
163 {
164   for( std::size_t i = 0; i < mImageUrls.size() ; ++i )
165   {
166     if( mImageUrls[i].mTextureId == textureId )
167     {
168       mReadyFlags[i] = true;
169       break;
170     }
171   }
172 }
173
174 TextureSet FixedImageCache::GetFrontTextureSet() const
175 {
176   return mTextureManager.GetTextureSet( mImageUrls[mFront].mTextureId );
177 }
178
179 void FixedImageCache::CheckFrontFrame( bool wasReady )
180 {
181   if( mWaitingForReadyFrame && wasReady == false && IsFrontReady() )
182   {
183     mWaitingForReadyFrame = false;
184     mObserver.FrameReady( GetFrontTextureSet() );
185   }
186 }
187
188 void FixedImageCache::UploadComplete(
189   bool           loadSuccess,
190   int32_t        textureId,
191   TextureSet     textureSet,
192   bool           useAtlasing,
193   const Vector4& atlasRect,
194   bool           preMultiplied)
195 {
196   bool frontFrameReady = IsFrontReady();
197
198   if( ! mRequestingLoad )
199   {
200     SetImageFrameReady( textureId );
201
202     CheckFrontFrame( frontFrameReady );
203   }
204   else
205   {
206     // UploadComplete has been called from within RequestLoad. TextureManager must
207     // therefore already have the texture cached, so make the texture ready.
208     // (Use the last texture, as the texture id hasn't been assigned yet)
209     mReadyFlags.back() = true;
210   }
211 }
212
213 void FixedImageCache::LoadComplete(
214   bool loadSuccess,
215   Devel::PixelBuffer pixelBuffer,
216   const VisualUrl& url,
217   bool preMultiplied )
218 {
219   // LoadComplete is called if this TextureUploadObserver requested to load
220   // an image that will be returned as a type of PixelBuffer by using a method
221   // TextureManager::LoadPixelBuffer.
222 }
223
224 } //namespace Internal
225 } //namespace Toolkit
226 } //namespace Dali