DALi Version 2.2.11
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-image / rolling-image-cache.h
1 #ifndef DALI_TOOLKIT_INTERNAL_ROLLING_IMAGE_CACHE_H
2 #define DALI_TOOLKIT_INTERNAL_ROLLING_IMAGE_CACHE_H
3
4 /*
5  * Copyright (c) 2022 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/common/circular-queue.h>
24
25 namespace Dali
26 {
27 namespace Toolkit
28 {
29 namespace Internal
30 {
31 /**
32  * Class to manage a rolling cache of images, where the cache size
33  * is smaller than the total number of images.
34  */
35 class RollingImageCache : public ImageCache, public TextureUploadObserver
36 {
37 public:
38   /**
39    * Constructor.
40    * @param[in] textureManager    The texture manager
41    * @param[in] size              The width and height to fit the loaded image to.
42    * @param[in] fittingMode       The FittingMode of the resource to load
43    * @param[in] samplingMode      The SamplingMode of the resource to load
44    * @param[in] urlList           List of urls to cache
45    * @param[in] maskingData       Masking data to be applied.
46    * @param[in] observer          FrameReady observer
47    * @param[in] cacheSize         The size of the cache
48    * @param[in] batchSize         The size of a batch to load
49    * @param[in] interval          Time interval between each frame
50    * @param[in] preMultiplyOnLoad The flag if image's color should be multiplied by it's alpha
51    *
52    * This will start loading textures immediately, according to the
53    * batch and cache sizes.
54    */
55   RollingImageCache(TextureManager&                     textureManager,
56                     ImageDimensions                     size,
57                     Dali::FittingMode::Type             fittingMode,
58                     Dali::SamplingMode::Type            samplingMode,
59                     UrlList&                            urlList,
60                     TextureManager::MaskingDataPointer& maskingData,
61                     ImageCache::FrameReadyObserver&     observer,
62                     uint16_t                            cacheSize,
63                     uint16_t                            batchSize,
64                     uint32_t                            interval,
65                     bool                                preMultiplyOnLoad);
66
67   /**
68    * Destructor
69    */
70   ~RollingImageCache() override;
71
72   /**
73    * @copydoc Internal::ImageCache::Frame()
74    */
75   TextureSet Frame(uint32_t frameIndex) override;
76
77   /**
78    * @copydoc Internal::ImageCache::FirstFrame()
79    */
80   TextureSet FirstFrame() override;
81
82   /**
83    * @copydoc Internal::ImageCache::GetFrameInterval()
84    */
85   uint32_t GetFrameInterval(uint32_t frameIndex) const override;
86
87   /**
88    * @copydoc Internal::ImageCache::GetCurrentFrameIndex()
89    */
90   int32_t GetCurrentFrameIndex() const override;
91
92   /**
93    * @copydoc Internal::ImageCache::GetTotalFrameCount()
94    */
95   int32_t GetTotalFrameCount() const override;
96
97   /**
98    * @copydoc Internal::ImageCache::ClearCache()
99    */
100   void ClearCache() override;
101
102 private:
103   /**
104    * @brief Check whether the front frame is ready or not.
105    *
106    * @return true if the front frame is ready
107    */
108   bool IsFrontReady() const;
109
110   /**
111    * @brief Load the next batch of images
112    *
113    * @param[in] frameIndex starting frame index of batch to be loaded.
114    */
115   void LoadBatch(uint32_t frameIndex);
116
117   /**
118    * @brief Get the texture set of the front frame.
119    *
120    * @return the texture set of the front of Cache.
121    */
122   TextureSet GetFrontTextureSet() const;
123
124   /**
125    * @brief Get the texture id of the given index
126    *
127    * @param[in] index index of the queue.
128    */
129   TextureManager::TextureId GetCachedTextureId(int index) const;
130
131   /**
132    * @brief Pop front entity of Cache.
133    */
134   void PopFrontCache();
135
136 protected:
137   /**
138    * @copydoc Toolkit::TextureUploadObserver::LoadComplete()
139    */
140   void LoadComplete(bool loadSuccess, TextureInformation textureInformation) override;
141
142 private:
143   /**
144    * Secondary class to hold readiness and index into url
145    */
146   struct ImageFrame
147   {
148     unsigned int mUrlIndex = 0u;
149     bool         mReady    = false;
150   };
151
152   std::vector<UrlStore>&    mImageUrls;
153   CircularQueue<ImageFrame> mQueue;
154 };
155
156 } // namespace Internal
157 } // namespace Toolkit
158 } // namespace Dali
159
160 #endif