[dali_2.3.41] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-image / fixed-image-cache.cpp
1 /*
2  * Copyright (c) 2024 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/image-atlas-manager.h> // For ImageAtlasManagerPtr
22
23 // EXTERNAL HEADERS
24 #include <dali/integration-api/adaptor-framework/adaptor.h>
25 #include <dali/integration-api/debug.h>
26
27 namespace Dali
28 {
29 namespace Toolkit
30 {
31 namespace Internal
32 {
33 namespace
34 {
35 constexpr bool     ENABLE_ORIENTATION_CORRECTION(true);
36 constexpr uint32_t FIRST_FRAME_INDEX = 0u;
37 } // namespace
38
39 FixedImageCache::FixedImageCache(TextureManager&                     textureManager,
40                                  ImageDimensions                     size,
41                                  Dali::FittingMode::Type             fittingMode,
42                                  Dali::SamplingMode::Type            samplingMode,
43                                  UrlList&                            urlList,
44                                  TextureManager::MaskingDataPointer& maskingData,
45                                  ImageCache::FrameReadyObserver&     observer,
46                                  uint32_t                            batchSize,
47                                  uint32_t                            interval,
48                                  bool                                preMultiplyOnLoad)
49 : ImageCache(textureManager, size, fittingMode, samplingMode, maskingData, observer, batchSize, interval, preMultiplyOnLoad),
50   mImageUrls(urlList),
51   mCurrentFrameIndex(FIRST_FRAME_INDEX)
52 {
53   mReadyFlags.reserve(mImageUrls.size());
54 }
55
56 FixedImageCache::~FixedImageCache()
57 {
58   ClearCache();
59 }
60
61 TextureSet FixedImageCache::Frame(uint32_t frameIndex)
62 {
63   TextureSet textureSet;
64   if(frameIndex >= mImageUrls.size())
65   {
66     DALI_LOG_ERROR("Wrong frameIndex requested.\n");
67     return textureSet;
68   }
69
70   mCurrentFrameIndex = frameIndex;
71
72   bool batchRequested = false;
73
74   // Make ensure that current frameIndex load requested.
75   while(mReadyFlags.size() <= frameIndex)
76   {
77     batchRequested = true;
78     LoadBatch();
79   }
80
81   // Request batch only 1 times for this function.
82   if(!batchRequested && mReadyFlags.size() < mImageUrls.size())
83   {
84     LoadBatch();
85   }
86
87   if(IsFrameReady(mCurrentFrameIndex) && mLoadState != TextureManager::LoadState::LOAD_FAILED)
88   {
89     textureSet = GetTextureSet(mCurrentFrameIndex);
90   }
91
92   return textureSet;
93 }
94
95 TextureSet FixedImageCache::FirstFrame()
96 {
97   TextureSet textureSet = Frame(FIRST_FRAME_INDEX);
98
99   return textureSet;
100 }
101
102 uint32_t FixedImageCache::GetFrameInterval(uint32_t frameIndex) const
103 {
104   return mInterval;
105 }
106
107 int32_t FixedImageCache::GetCurrentFrameIndex() const
108 {
109   return static_cast<int32_t>(mCurrentFrameIndex);
110 }
111
112 int32_t FixedImageCache::GetTotalFrameCount() const
113 {
114   return mImageUrls.size();
115 }
116
117 bool FixedImageCache::IsFrameReady(uint32_t frameIndex) const
118 {
119   return ((mReadyFlags.size() > 0) && (mReadyFlags[frameIndex] == true));
120 }
121
122 void FixedImageCache::LoadBatch()
123 {
124   // Try and load up to mBatchSize images, until the cache is filled.
125   // Once the cache is filled, no more images are loaded.
126   for(unsigned int i = 0; i < mBatchSize && mReadyFlags.size() < mImageUrls.size(); ++i)
127   {
128     uint32_t   frameIndex = mReadyFlags.size();
129     VisualUrl& url        = mImageUrls[frameIndex].mUrl;
130
131     mReadyFlags.push_back(false);
132
133     // Note, if the image is already loaded, then LoadComplete will get called
134     // from within this method. This means it won't yet have a texture id, so we
135     // need to account for this inside the LoadComplete method using mRequestingLoad.
136     mRequestingLoad = true;
137     mLoadState      = TextureManager::LoadState::LOADING;
138
139     bool                  synchronousLoading = false;
140     bool                  atlasingStatus     = false;
141     bool                  loadingStatus      = false;
142     AtlasUploadObserver*  atlasObserver      = nullptr;
143     ImageAtlasManagerPtr  imageAtlasManager  = nullptr;
144     Vector4               textureRect;
145     Dali::ImageDimensions textureRectSize;
146
147     auto preMultiplyOnLoading = mPreMultiplyOnLoad ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD
148                                                    : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
149
150     TextureManager::TextureId loadTextureId = TextureManager::INVALID_TEXTURE_ID;
151     mTextureManager.LoadTexture(url, mDesiredSize, mFittingMode, mSamplingMode, mMaskingData, synchronousLoading, loadTextureId, textureRect, textureRectSize, atlasingStatus, loadingStatus, this, atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED, preMultiplyOnLoading);
152     mImageUrls[frameIndex].mTextureId = loadTextureId;
153     mRequestingLoad                   = false;
154   }
155 }
156
157 TextureSet FixedImageCache::GetTextureSet(uint32_t frameIndex) const
158 {
159   TextureSet textureSet = mTextureManager.GetTextureSet(mImageUrls[frameIndex].mTextureId);
160   return textureSet;
161 }
162
163 void FixedImageCache::MakeReady(bool wasReady, uint32_t frameIndex, bool preMultiplied)
164 {
165   if(wasReady == false && IsFrameReady(frameIndex))
166   {
167     mObserver.FrameReady(GetTextureSet(frameIndex), mInterval, preMultiplied);
168   }
169 }
170
171 void FixedImageCache::ClearCache()
172 {
173   if(Dali::Adaptor::IsAvailable())
174   {
175     for(std::size_t i = 0; i < mImageUrls.size(); ++i)
176     {
177       mTextureManager.RequestRemove(mImageUrls[i].mTextureId, this);
178       mImageUrls[i].mTextureId = TextureManager::INVALID_TEXTURE_ID;
179     }
180   }
181   mReadyFlags.clear();
182   mLoadState = TextureManager::LoadState::NOT_STARTED;
183   if(mMaskingData)
184   {
185     mMaskingData->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
186   }
187 }
188
189 void FixedImageCache::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
190 {
191   if(loadSuccess)
192   {
193     mLoadState                = TextureManager::LoadState::LOAD_FINISHED;
194     bool wasCurrentFrameReady = IsFrameReady(mCurrentFrameIndex);
195     if(!mRequestingLoad)
196     {
197       for(std::size_t i = 0; i < mImageUrls.size(); ++i)
198       {
199         if(mImageUrls[i].mTextureId == textureInformation.textureId)
200         {
201           mReadyFlags[i] = true;
202           break;
203         }
204       }
205     }
206     else
207     {
208       DALI_ASSERT_ALWAYS(mReadyFlags.size() > 0u && "Some FixedImageCache::LoadBatch() called mismatched!");
209       size_t i = mReadyFlags.size() - 1u;
210
211       // texture id might not setup yet. Update it now.
212       mImageUrls[i].mTextureId = textureInformation.textureId;
213       mReadyFlags[i]           = true;
214     }
215     MakeReady(wasCurrentFrameReady, mCurrentFrameIndex, textureInformation.preMultiplied);
216   }
217   else
218   {
219     mLoadState = TextureManager::LoadState::LOAD_FAILED;
220     // preMultiplied should be false because broken image don't premultiply alpha on load
221     mObserver.FrameReady(TextureSet(), 0, false);
222   }
223 }
224
225 } //namespace Internal
226 } //namespace Toolkit
227 } //namespace Dali