e751f902b495c52c3362e484936fcf863062971f
[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-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   while(mReadyFlags.size() < mImageUrls.size() &&
71         (frameIndex > mCurrentFrameIndex || mReadyFlags.empty()))
72   {
73     ++mCurrentFrameIndex;
74     LoadBatch();
75   }
76
77   mCurrentFrameIndex = frameIndex;
78
79   if(IsFrameReady(mCurrentFrameIndex) && mLoadState != TextureManager::LoadState::LOAD_FAILED)
80   {
81     textureSet = GetTextureSet(mCurrentFrameIndex);
82   }
83
84   return textureSet;
85 }
86
87 TextureSet FixedImageCache::FirstFrame()
88 {
89   TextureSet textureSet = Frame(FIRST_FRAME_INDEX);
90
91   return textureSet;
92 }
93
94 uint32_t FixedImageCache::GetFrameInterval(uint32_t frameIndex) const
95 {
96   return mInterval;
97 }
98
99 int32_t FixedImageCache::GetCurrentFrameIndex() const
100 {
101   return static_cast<int32_t>(mCurrentFrameIndex);
102 }
103
104 int32_t FixedImageCache::GetTotalFrameCount() const
105 {
106   return mImageUrls.size();
107 }
108
109 bool FixedImageCache::IsFrameReady(uint32_t frameIndex) const
110 {
111   return (mReadyFlags.size() > 0 && mReadyFlags[frameIndex] == 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, no more images are loaded.
118   for(unsigned int i = 0; i < mBatchSize && mReadyFlags.size() < mImageUrls.size(); ++i)
119   {
120     uint32_t   frameIndex = mReadyFlags.size();
121     VisualUrl& url        = mImageUrls[frameIndex].mUrl;
122
123     mReadyFlags.push_back(false);
124
125     // Note, if the image is already loaded, then LoadComplete will get called
126     // from within this method. This means it won't yet have a texture id, so we
127     // need to account for this inside the LoadComplete method using mRequestingLoad.
128     mRequestingLoad = true;
129     mLoadState      = TextureManager::LoadState::LOADING;
130
131     bool                  synchronousLoading = false;
132     bool                  atlasingStatus     = false;
133     bool                  loadingStatus      = false;
134     AtlasUploadObserver*  atlasObserver      = nullptr;
135     ImageAtlasManagerPtr  imageAtlasManager  = nullptr;
136     Vector4               textureRect;
137     Dali::ImageDimensions textureRectSize;
138
139     auto preMultiplyOnLoading = mPreMultiplyOnLoad ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD
140                                                    : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
141
142     mTextureManager.LoadTexture(url, mDesiredSize, mFittingMode, mSamplingMode, mMaskingData, synchronousLoading, mImageUrls[frameIndex].mTextureId, textureRect, textureRectSize, atlasingStatus, loadingStatus, this, atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED, preMultiplyOnLoading);
143     mRequestingLoad = false;
144   }
145 }
146
147 TextureSet FixedImageCache::GetTextureSet(uint32_t frameIndex) const
148 {
149   TextureSet textureSet = mTextureManager.GetTextureSet(mImageUrls[frameIndex].mTextureId);
150   return textureSet;
151 }
152
153 void FixedImageCache::MakeReady(bool wasReady, uint32_t frameIndex, bool preMultiplied)
154 {
155   if(wasReady == false && IsFrameReady(frameIndex))
156   {
157     mObserver.FrameReady(GetTextureSet(frameIndex), mInterval, preMultiplied);
158   }
159 }
160
161 void FixedImageCache::ClearCache()
162 {
163   if(Dali::Adaptor::IsAvailable())
164   {
165     for(std::size_t i = 0; i < mImageUrls.size(); ++i)
166     {
167       mTextureManager.RequestRemove(mImageUrls[i].mTextureId, this);
168       mImageUrls[i].mTextureId = TextureManager::INVALID_TEXTURE_ID;
169     }
170   }
171   mReadyFlags.clear();
172   mLoadState = TextureManager::LoadState::NOT_STARTED;
173   if(mMaskingData)
174   {
175     mMaskingData->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
176   }
177 }
178
179 void FixedImageCache::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
180 {
181   if(loadSuccess)
182   {
183     mLoadState               = TextureManager::LoadState::LOAD_FINISHED;
184     bool isCurrentFrameReady = IsFrameReady(mCurrentFrameIndex);
185     if(!mRequestingLoad)
186     {
187       for(std::size_t i = 0; i < mImageUrls.size(); ++i)
188       {
189         if(mImageUrls[i].mTextureId == textureInformation.textureId)
190         {
191           mReadyFlags[i] = true;
192           break;
193         }
194       }
195     }
196     else
197     {
198       mReadyFlags.back() = true;
199     }
200     MakeReady(isCurrentFrameReady, mCurrentFrameIndex, textureInformation.preMultiplied);
201   }
202   else
203   {
204     mLoadState = TextureManager::LoadState::LOAD_FAILED;
205     // preMultiplied should be false because broken image don't premultiply alpha on load
206     mObserver.FrameReady(TextureSet(), 0, false);
207   }
208 }
209
210 } //namespace Internal
211 } //namespace Toolkit
212 } //namespace Dali