Merge "Add text selection popup style" into 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(
136       url, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::BOX_THEN_LINEAR, mMaskingData, synchronousLoading, mImageUrls[frameIndex].mTextureId, textureRect, textureRectSize, atlasingStatus, loadingStatus, Dali::WrapMode::Type::DEFAULT, Dali::WrapMode::Type::DEFAULT, this, atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED, preMultiply);
137
138     mRequestingLoad = false;
139   }
140 }
141
142 TextureSet FixedImageCache::GetFrontTextureSet() const
143 {
144   return mTextureManager.GetTextureSet(mImageUrls[mFront].mTextureId);
145 }
146
147 void FixedImageCache::CheckFrontFrame(bool wasReady)
148 {
149   if(wasReady == false && IsFrontReady())
150   {
151     mObserver.FrameReady(GetFrontTextureSet(), mInterval);
152   }
153 }
154
155 void FixedImageCache::ClearCache()
156 {
157   if(mTextureManagerAlive)
158   {
159     for(std::size_t i = 0; i < mImageUrls.size(); ++i)
160     {
161       mTextureManager.Remove(mImageUrls[i].mTextureId, this);
162       mImageUrls[i].mTextureId = TextureManager::INVALID_TEXTURE_ID;
163
164       if(mMaskingData && mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID)
165       {
166         // In the CPU alpha masking, each frame increases reference count of masking texture.
167         // We should call TextureManager::Remove to decrease reference count when each frame is removed.
168         mTextureManager.Remove(mMaskingData->mAlphaMaskId, this);
169       }
170     }
171   }
172   mReadyFlags.clear();
173   mLoadState = TextureManager::LoadState::NOT_STARTED;
174   if(mMaskingData)
175   {
176     mMaskingData->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
177   }
178 }
179
180 void FixedImageCache::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
181 {
182   if(loadSuccess)
183   {
184     mLoadState           = TextureManager::LoadState::LOAD_FINISHED;
185     bool frontFrameReady = IsFrontReady();
186     if(!mRequestingLoad)
187     {
188       for(std::size_t i = 0; i < mImageUrls.size(); ++i)
189       {
190         if(mImageUrls[i].mTextureId == textureInformation.textureId)
191         {
192           mReadyFlags[i] = true;
193           break;
194         }
195       }
196     }
197     else
198     {
199       mReadyFlags.back() = true;
200     }
201     CheckFrontFrame(frontFrameReady);
202   }
203   else
204   {
205     mLoadState = TextureManager::LoadState::LOAD_FAILED;
206     mObserver.FrameReady(TextureSet(), 0);
207   }
208 }
209
210 } //namespace Internal
211 } //namespace Toolkit
212 } //namespace Dali