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