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