605fbb9b10c8c1ba7cf36884706bae10e39efda1
[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) 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/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] animatedImageLoader The loaded animated image
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] isSynchronousLoading The flag to define whether to load first frame synchronously
50    *
51    * This will start loading textures immediately, according to the
52    * batch and cache sizes.
53    */
54   RollingAnimatedImageCache(TextureManager&                 textureManager,
55                             AnimatedImageLoading&           animatedImageLoader,
56                             ImageCache::FrameReadyObserver& observer,
57                             uint16_t                        cacheSize,
58                             uint16_t                        batchSize,
59                             bool                            isSynchronousLoading);
60
61   /**
62    * @brief Destructor
63    */
64   ~RollingAnimatedImageCache() override;
65
66   /**
67    * @copydoc Internal::ImageCache::Frame()
68    */
69   TextureSet Frame(uint32_t frameIndex) override;
70
71   /**
72    * @copydoc Internal::ImageCache::FirstFrame()
73    */
74   TextureSet FirstFrame() override;
75
76   /**
77    * @copydoc Internal::ImageCache::GetFrameInterval()
78    */
79   uint32_t GetFrameInterval(uint32_t frameIndex) const override;
80
81   /**
82    * @copydoc Internal::ImageCache::GetCurrentFrameIndex()
83    */
84   int32_t GetCurrentFrameIndex() const override;
85
86   /**
87    * @copydoc Internal::ImageCache::GetTotalFrameCount()
88    */
89   int32_t GetTotalFrameCount() const override;
90
91   /**
92    * @copydoc Internal::ImageCache::ClearCache()
93    */
94   void ClearCache() override;
95
96 private:
97   /**
98    * @brief Check whether the front frame is ready or not.
99    *
100    * @return true if the front frame is ready
101    */
102   bool IsFrontReady() const;
103
104   /**
105    * @brief Request to Load a frame
106    *
107    * @param[in] frameIndex          index of frame to be loaded.
108    * @param[in] useCache            true if this frame loading uses cache.
109    * @param[in] synchronousLoading  true if the frame should be loaded synchronously
110    *
111    * @return the texture set currently loaded.
112    */
113   TextureSet RequestFrameLoading(uint32_t frameIndex, bool useCache, bool synchronousLoading);
114
115   /**
116    * @brief Load the next batch of images
117    *
118    * @param[in] frameIndex starting frame index of batch to be loaded.
119    */
120   void LoadBatch(uint32_t frameIndex);
121
122   /**
123    * @brief Find the matching image frame, and set it to ready
124    *
125    * @param[in] textureId texture id to be marked as ready.
126    */
127   void SetImageFrameReady(TextureManager::TextureId textureId);
128
129   /**
130    * @brief Get the texture set of the front frame.
131    *
132    * @return the texture set of the front of Cache.
133    */
134   TextureSet GetFrontTextureSet() const;
135
136   /**
137    * @brief Get the texture id of the given index
138    *
139    * @param[in] index index of the queue.
140    */
141   TextureManager::TextureId GetCachedTextureId(int index) const;
142
143   /**
144    * @brief Make the loaded frame ready and notify it to the texture upload observer
145    *
146    * @param[in] loadSuccess whether the loading is succeded or not.
147    * @param[in] textureSet textureSet for this frame.
148    * @param[in] interval interval between this frame and next frame.
149    */
150   void MakeFrameReady(bool loadSuccess, TextureSet textureSet, uint32_t interval);
151
152 protected:
153   /**
154    * @copydoc Toolkit::TextureUploadObserver::LoadComplete()
155    */
156   void LoadComplete(bool loadSuccess, TextureInformation textureInformation) override;
157
158 private:
159
160   /**
161    * Secondary class to hold readiness and index into url
162    */
163   struct ImageFrame
164   {
165     uint32_t mFrameNumber = 0u;
166     bool     mReady       = false;
167   };
168
169   Dali::AnimatedImageLoading mAnimatedImageLoading;
170   uint32_t                   mFrameCount;
171   uint32_t                   mFrameIndex;
172   uint32_t                   mCacheSize;
173   std::vector<UrlStore>      mImageUrls;
174   std::vector<int32_t>       mIntervals;
175   std::vector<uint32_t>      mLoadWaitingQueue;
176   CircularQueue<ImageFrame>  mQueue;
177   bool                       mIsSynchronousLoading;
178 };
179
180 } // namespace Internal
181
182 } // namespace Toolkit
183
184 } // namespace Dali
185
186 #endif //DALI_TOOLKIT_INTERNAL_ROLLING_ANIMATED_IMAGE_CACHE_H