Updated all header files to new format
[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] observer FrameReady observer
44    * @param[in] cacheSize The size of the cache
45    * @param[in] batchSize The size of a batch to load
46    *
47    * This will start loading textures immediately, according to the
48    * batch and cache sizes.
49    */
50   RollingImageCache(TextureManager&                 textureManager,
51                     UrlList&                        urlList,
52                     ImageCache::FrameReadyObserver& observer,
53                     uint16_t                        cacheSize,
54                     uint16_t                        batchSize);
55
56   /**
57    * Destructor
58    */
59   ~RollingImageCache() override;
60
61   /**
62    * Get the Nth frame. If it's not ready, this will trigger the
63    * sending of FrameReady() when the image becomes ready.
64    */
65   TextureSet Frame(uint32_t frameIndex) override;
66
67   /**
68    * Get the first frame. If it's not ready, this will trigger the
69    * sending of FrameReady() when the image becomes ready.
70    */
71   TextureSet FirstFrame() override;
72
73   /**
74    * Get the next frame. If it's not ready, this will trigger the
75    * sending of FrameReady() when the image becomes ready.
76    */
77   TextureSet NextFrame() override;
78
79   /**
80    * Get the interval of Nth frame.
81    */
82   uint32_t GetFrameInterval(uint32_t frameIndex) const override;
83
84   /**
85    * Get the current rendered frame index.
86    * If there isn't any loaded frame, returns -1.
87    */
88   int32_t GetCurrentFrameIndex() const override;
89
90   /**
91    * Get total frame count of the animated image file.
92    */
93   int32_t GetTotalFrameCount() const override;
94
95 private:
96   /**
97    * @return true if the front frame is ready
98    */
99   bool IsFrontReady() const;
100
101   /**
102    * Load the next batch of images
103    */
104   void LoadBatch();
105
106   /**
107    * Find the matching image frame, and set it to ready
108    */
109   void SetImageFrameReady(TextureManager::TextureId textureId);
110
111   /**
112    * Get the texture set of the front frame.
113    * @return the texture set
114    */
115   TextureSet GetFrontTextureSet() const;
116
117   /**
118    * Get the texture id of the given index
119    */
120   TextureManager::TextureId GetCachedTextureId(int index) const;
121
122   /**
123    * Check if the front frame has become ready - if so, inform observer
124    * @param[in] wasReady Readiness before call.
125    */
126   void CheckFrontFrame(bool wasReady);
127
128 protected:
129   void UploadComplete(
130     bool           loadSuccess,
131     int32_t        textureId,
132     TextureSet     textureSet,
133     bool           useAtlasing,
134     const Vector4& atlasRect,
135     bool           preMultiplied) override;
136
137   void LoadComplete(
138     bool               loadSuccess,
139     Devel::PixelBuffer pixelBuffer,
140     const VisualUrl&   url,
141     bool               preMultiplied) override;
142
143 private:
144   /**
145    * Secondary class to hold readiness and index into url
146    */
147   struct ImageFrame
148   {
149     unsigned int mUrlIndex = 0u;
150     bool         mReady    = false;
151   };
152
153   std::vector<UrlStore>&    mImageUrls;
154   CircularQueue<ImageFrame> mQueue;
155 };
156
157 } // namespace Internal
158 } // namespace Toolkit
159 } // namespace Dali
160
161 #endif