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