[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-image / rolling-animated-image-cache.h
1 #ifndef DALI_TOOLKIT_INTERNAL_ROLLING_ANIMATED_IMAGE_CACHE_H
2 #define DALI_TOOLKIT_INTERNAL_ROLLING_ANIMATED_IMAGE_CACHE_H
3
4 /*
5  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 // EXTERNAL INCLUDES
21 #include <dali-toolkit/internal/texture-manager/texture-manager-impl.h>
22 #include <dali-toolkit/internal/visuals/animated-image/image-cache.h>
23 #include <dali/devel-api/adaptor-framework/animated-image-loading.h>
24 #include <dali/devel-api/common/circular-queue.h>
25
26 namespace Dali
27 {
28 namespace Toolkit
29 {
30 namespace Internal
31 {
32 /**
33  * Class to manage a rolling cache of Animated images, where the cache size
34  * is smaller than the total number of images.
35  *
36  * Frames are always ready, so the observer.FrameReady callback is never triggered;
37  * the FirstFrame and NextFrame APIs will always return a texture.
38  */
39 class RollingAnimatedImageCache : public ImageCache, public TextureUploadObserver
40 {
41 public:
42   /**
43    * @brief Constructor.
44    * @param[in] textureManager       The texture manager
45    * @param[in] size                 The width and height to fit the loaded image to.
46    * @param[in] fittingMode          The FittingMode of the resource to load
47    * @param[in] samplingMode         The SamplingMode of the resource to load
48    * @param[in] animatedImageLoading The loaded animated image
49    * @param[in] maskingData          Masking data to be applied.
50    * @param[in] observer             FrameReady observer
51    * @param[in] cacheSize            The size of the cache
52    * @param[in] batchSize            The size of a batch to load
53    * @param[in] wrapModeU            Horizontal Wrap mode
54    * @param[in] wrapModeV            Vertical Wrap mode
55    * @param[in] isSynchronousLoading The flag to define whether to load first frame synchronously
56    * @param[in] preMultiplyOnLoad    The flag if image's color should be multiplied by it's alpha
57    *
58    * This will start loading textures immediately, according to the
59    * batch and cache sizes.
60    */
61   RollingAnimatedImageCache(TextureManager&                     textureManager,
62                             ImageDimensions                     size,
63                             Dali::FittingMode::Type             fittingMode,
64                             Dali::SamplingMode::Type            samplingMode,
65                             AnimatedImageLoading&               animatedImageLoading,
66                             TextureManager::MaskingDataPointer& maskingData,
67                             ImageCache::FrameReadyObserver&     observer,
68                             uint16_t                            cacheSize,
69                             uint16_t                            batchSize,
70                             const Dali::WrapMode::Type&         wrapModeU,
71                             const Dali::WrapMode::Type&         wrapModeV,
72                             bool                                isSynchronousLoading,
73                             bool                                preMultiplyOnLoad);
74
75   /**
76    * @brief Destructor
77    */
78   ~RollingAnimatedImageCache() override;
79
80   /**
81    * @copydoc Internal::ImageCache::Frame()
82    */
83   TextureSet Frame(uint32_t frameIndex) override;
84
85   /**
86    * @copydoc Internal::ImageCache::FirstFrame()
87    */
88   TextureSet FirstFrame() override;
89
90   /**
91    * @copydoc Internal::ImageCache::GetFrameInterval()
92    */
93   uint32_t GetFrameInterval(uint32_t frameIndex) const override;
94
95   /**
96    * @copydoc Internal::ImageCache::GetCurrentFrameIndex()
97    */
98   int32_t GetCurrentFrameIndex() const override;
99
100   /**
101    * @copydoc Internal::ImageCache::GetTotalFrameCount()
102    */
103   int32_t GetTotalFrameCount() const override;
104
105   /**
106    * @copydoc Internal::ImageCache::ClearCache()
107    */
108   void ClearCache() override;
109
110 private:
111   /**
112    * @brief Check whether the front frame is ready or not.
113    *
114    * @return true if the front frame is ready
115    */
116   bool IsFrontReady() const;
117
118   /**
119    * @brief Request to Load a frame asynchronously
120    *
121    * @param[in] frameIndex index of frame to be loaded.
122    *
123    * @return the texture set currently loaded.
124    */
125   TextureSet RequestFrameLoading(uint32_t frameIndex);
126
127   /**
128    * @brief Request to Load a frame
129    *
130    * @param[in] frameIndex             Index of frame to be loaded.
131    * @param[in] synchronousLoading     True if the frame should be loaded synchronously
132    * @param[in,out] preMultiplyOnLoad  True if the image color should be multiplied by it's alpha. Set to false if the
133    *                                   image has no alpha channel
134    *
135    * @return the texture set currently loaded.
136    */
137   TextureSet RequestFrameLoading(uint32_t frameIndex, bool synchronousLoading, TextureManager::MultiplyOnLoad& preMultiplyOnLoading);
138
139   /**
140    * @brief Load the next batch of images
141    *
142    * @param[in] frameIndex starting frame index of batch to be loaded.
143    */
144   void LoadBatch(uint32_t frameIndex);
145
146   /**
147    * @brief Find the matching image frame, and set it to ready
148    *
149    * @param[in] textureId texture id to be marked as ready.
150    */
151   void SetImageFrameReady(TextureManager::TextureId textureId);
152
153   /**
154    * @brief Get the texture set of the front frame.
155    *
156    * @return the texture set of the front of Cache.
157    */
158   TextureSet GetFrontTextureSet() const;
159
160   /**
161    * @brief Get the texture id of the given index
162    *
163    * @param[in] index index of the queue.
164    */
165   TextureManager::TextureId GetCachedTextureId(int index) const;
166
167   /**
168    * @brief Make the loaded frame ready and notify it to the texture upload observer
169    *
170    * @param[in] loadSuccess whether the loading is succeded or not.
171    * @param[in] textureSet textureSet for this frame.
172    * @param[in] interval interval between this frame and next frame.
173    * @param[in] preMultiplied whether the texture is premultied alpha or not.
174    */
175   void MakeFrameReady(bool loadSuccess, TextureSet textureSet, uint32_t interval, bool preMultiplied);
176
177   /**
178    * @brief Pop front entity of Cache.
179    */
180   void PopFrontCache();
181
182 protected:
183   /**
184    * @copydoc Toolkit::TextureUploadObserver::LoadComplete()
185    */
186   void LoadComplete(bool loadSuccess, TextureInformation textureInformation) override;
187
188 private:
189   /**
190    * Secondary class to hold readiness and index into url
191    */
192   struct ImageFrame
193   {
194     uint32_t mFrameNumber = 0u;
195     bool     mReady       = false;
196   };
197   std::vector<TextureManager::TextureId> mTextureIds;
198
199   VisualUrl                  mImageUrl;
200   Dali::AnimatedImageLoading mAnimatedImageLoading;
201   uint32_t                   mFrameCount;
202   uint32_t                   mFrameIndex;
203   uint32_t                   mCacheSize;
204   std::vector<int32_t>       mIntervals;
205   std::vector<uint32_t>      mLoadWaitingQueue;
206   CircularQueue<ImageFrame>  mQueue;
207   Dali::WrapMode::Type       mWrapModeU : 3;
208   Dali::WrapMode::Type       mWrapModeV : 3;
209   bool                       mIsSynchronousLoading;
210 };
211
212 } // namespace Internal
213
214 } // namespace Toolkit
215
216 } // namespace Dali
217
218 #endif //DALI_TOOLKIT_INTERNAL_ROLLING_ANIMATED_IMAGE_CACHE_H