0f0d8833276672117cfaa48633c3fe4359616d1d
[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) 2021 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
22 #include <dali-toolkit/internal/visuals/animated-image/image-cache.h>
23 #include <dali-toolkit/internal/visuals/texture-manager-impl.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 images, where the cache size
34  * is smaller than the total number of images.
35  */
36 class RollingImageCache : public ImageCache, public TextureUploadObserver
37 {
38 public:
39   /**
40    * Constructor.
41    * @param[in] textureManager The texture manager
42    * @param[in] urlList        List of urls to cache
43    * @param[in] maskingData    Masking data to be applied.
44    * @param[in] observer       FrameReady observer
45    * @param[in] cacheSize      The size of the cache
46    * @param[in] batchSize      The size of a batch to load
47    * @param[in] interval       Time interval between each frame
48    *
49    * This will start loading textures immediately, according to the
50    * batch and cache sizes.
51    */
52   RollingImageCache(TextureManager&                     textureManager,
53                     UrlList&                            urlList,
54                     TextureManager::MaskingDataPointer& maskingData,
55                     ImageCache::FrameReadyObserver&     observer,
56                     uint16_t                            cacheSize,
57                     uint16_t                            batchSize,
58                     uint32_t                            interval);
59
60   /**
61    * Destructor
62    */
63   ~RollingImageCache() override;
64
65   /**
66    * @copydoc Internal::ImageCache::Frame()
67    */
68   TextureSet Frame(uint32_t frameIndex) override;
69
70   /**
71    * @copydoc Internal::ImageCache::FirstFrame()
72    */
73   TextureSet FirstFrame() override;
74
75   /**
76    * @copydoc Internal::ImageCache::GetFrameInterval()
77    */
78   uint32_t GetFrameInterval(uint32_t frameIndex) const override;
79
80   /**
81    * @copydoc Internal::ImageCache::GetCurrentFrameIndex()
82    */
83   int32_t GetCurrentFrameIndex() const override;
84
85   /**
86    * @copydoc Internal::ImageCache::GetTotalFrameCount()
87    */
88   int32_t GetTotalFrameCount() const override;
89
90   /**
91    * @copydoc Internal::ImageCache::GetLoadState()
92    */
93   TextureManager::LoadState GetLoadState() const override;
94
95   /**
96    * @copydoc Internal::ImageCache::ClearCache()
97    */
98   void ClearCache() override;
99
100 private:
101   /**
102    * @brief Check whether the front frame is ready or not.
103    *
104    * @return true if the front frame is ready
105    */
106   bool IsFrontReady() const;
107
108   /**
109    * @brief Load the next batch of images
110    *
111    * @param[in] frameIndex starting frame index of batch to be loaded.
112    */
113   void LoadBatch(uint32_t frameIndex);
114
115   /**
116    * @brief Find the matching image frame, and set it to ready
117    *
118    * @param[in] textureId texture id to be marked as ready.
119    * @param[in] loadSuccess true if the frame loading is succeeded.
120    */
121   void SetImageFrameReady(TextureManager::TextureId textureId, bool loadSuccess);
122
123   /**
124    * @brief Get the texture set of the front frame.
125    *
126    * @return the texture set of the front of Cache.
127    */
128   TextureSet GetFrontTextureSet() const;
129
130   /**
131    * @brief Get the texture id of the given index
132    *
133    * @param[in] index index of the queue.
134    */
135   TextureManager::TextureId GetCachedTextureId(int index) const;
136
137   /**
138    * @brief Check if the front frame has become ready - if so, inform observer
139    *
140    * @param[in] wasReady Readiness before call.
141    */
142   void CheckFrontFrame(bool wasReady);
143
144   /**
145    * @brief Pop front entity of Cache.
146    */
147   void PopFrontCache();
148
149 protected:
150
151   /**
152    * @copydoc Toolkit::TextureUploadObserver::UploadComplete()
153    */
154   void UploadComplete(
155     bool           loadSuccess,
156     int32_t        textureId,
157     TextureSet     textureSet,
158     bool           useAtlasing,
159     const Vector4& atlasRect,
160     bool           preMultiplied) override;
161
162 private:
163   /**
164    * Secondary class to hold readiness and index into url
165    */
166   struct ImageFrame
167   {
168     unsigned int mUrlIndex = 0u;
169     bool         mReady    = false;
170   };
171
172   std::vector<UrlStore>&                 mImageUrls;
173   std::vector<TextureManager::LoadState> mLoadStates;
174   CircularQueue<ImageFrame>              mQueue;
175 };
176
177 } // namespace Internal
178 } // namespace Toolkit
179 } // namespace Dali
180
181 #endif