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