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