Merge "Extending Style - Adding Strikethrough" into devel/master
[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) 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 #include <dali-toolkit/internal/visuals/animated-image/image-cache.h>
22 #include <dali-toolkit/internal/visuals/texture-manager-impl.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    * Constructor.
44    * @param[in] textureManager The texture manager
45    * @param[in] animatedImageLoader The loaded animated image
46    * @param[in] frameCount The number of frames in the animated image
47    * @param[in] observer FrameReady observer
48    * @param[in] cacheSize The size of the cache
49    * @param[in] batchSize The size of a batch to load
50    * @param[in] isSynchronousLoading The flag to define whether to load first frame synchronously
51    *
52    * This will start loading textures immediately, according to the
53    * batch and cache sizes.
54    */
55   RollingAnimatedImageCache(TextureManager&                 textureManager,
56                             AnimatedImageLoading&           animatedImageLoader,
57                             uint32_t                        frameCount,
58                             ImageCache::FrameReadyObserver& observer,
59                             uint16_t                        cacheSize,
60                             uint16_t                        batchSize,
61                             bool                            isSynchronousLoading);
62
63   /**
64    * Destructor
65    */
66   ~RollingAnimatedImageCache() override;
67
68   /**
69    * Get the Nth frame. If it's not ready, this will trigger the
70    * sending of FrameReady() when the image becomes ready.
71    */
72   TextureSet Frame(uint32_t frameIndex) override;
73
74   /**
75    * Get the first frame. If it's not ready, this will trigger the
76    * sending of FrameReady() when the image becomes ready.
77    */
78   TextureSet FirstFrame() override;
79
80   /**
81    * Get the next frame. If it's not ready, this will trigger the
82    * sending of FrameReady() when the image becomes ready.
83    */
84   TextureSet NextFrame() override;
85
86   /**
87    * Get the interval of Nth frame.
88    */
89   uint32_t GetFrameInterval(uint32_t frameIndex) const override;
90
91   /**
92    * Get the current rendered frame index.
93    * If there isn't any loaded frame, returns -1.
94    */
95   int32_t GetCurrentFrameIndex() const override;
96
97   /**
98    * Get total frame count of the animated image file.
99    */
100   int32_t GetTotalFrameCount() const override;
101
102 private:
103   /**
104    * @return true if the front frame is ready
105    */
106   bool IsFrontReady() const;
107
108   /**
109    * Request to Load a frame
110    */
111   void RequestFrameLoading(uint32_t frameIndex);
112
113   /**
114    * Load the next batch of images
115    */
116   void LoadBatch();
117
118   /**
119    * Find the matching image frame, and set it to ready
120    */
121   void SetImageFrameReady(TextureManager::TextureId textureId);
122
123   /**
124    * Get the texture set of the front frame.
125    * @return the texture set
126    */
127   TextureSet GetFrontTextureSet() const;
128
129   /**
130    * Get the texture id of the given index
131    */
132   TextureManager::TextureId GetCachedTextureId(int index) const;
133
134   /**
135    * Check if the front frame has become ready - if so, inform observer
136    * @param[in] wasReady Readiness before call.
137    */
138   void CheckFrontFrame(bool wasReady);
139
140 protected:
141   void LoadComplete(bool loadSuccess, TextureInformation textureInformation) override;
142
143 private:
144   /**
145    * Secondary class to hold readiness and index into url
146    */
147   struct ImageFrame
148   {
149     unsigned int mFrameNumber = 0u;
150     bool         mReady       = false;
151   };
152
153   Dali::AnimatedImageLoading mAnimatedImageLoading;
154   uint32_t                   mFrameCount;
155   int                        mFrameIndex;
156   int                        mCacheSize;
157   std::vector<UrlStore>      mImageUrls;
158   std::vector<int32_t>       mIntervals;
159   std::vector<uint32_t>      mLoadWaitingQueue;
160   CircularQueue<ImageFrame>  mQueue;
161   bool                       mIsSynchronousLoading;
162   bool                       mOnLoading;
163 };
164
165 } // namespace Internal
166
167 } // namespace Toolkit
168
169 } // namespace Dali
170
171 #endif //DALI_TOOLKIT_INTERNAL_ROLLING_ANIMATED_IMAGE_CACHE_H