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