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