Support WebP format
[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
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    *
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
62   /**
63    * Destructor
64    */
65   virtual ~RollingAnimatedImageCache();
66
67   /**
68    * Get the Nth frame. If it's not ready, this will trigger the
69    * sending of FrameReady() when the image becomes ready.
70    */
71   TextureSet Frame( uint32_t frameIndex ) override;
72
73   /**
74    * Get the first frame. If it's not ready, this will trigger the
75    * sending of FrameReady() when the image becomes ready.
76    */
77   TextureSet FirstFrame() override;
78
79   /**
80    * Get the interval of Nth frame.
81    */
82   uint32_t GetFrameInterval( uint32_t frameIndex ) override;
83
84 private:
85   /**
86    * @return true if the front frame is ready
87    */
88   bool IsFrontReady() const;
89
90   /**
91    * Load the next batch of images
92    */
93   void LoadBatch();
94
95   /**
96    * Get the texture set of the front frame.
97    * @return the texture set
98    */
99   TextureSet GetFrontTextureSet() const;
100
101   /**
102    * Get the texture id of the given index
103    */
104   TextureManager::TextureId GetCachedTextureId( int index ) const;
105
106 private:
107   /**
108    * Secondary class to hold readiness and index into url
109    */
110   struct ImageFrame
111   {
112     unsigned int mFrameNumber = 0u;
113   };
114
115   Dali::AnimatedImageLoading& mAnimatedImageLoading;
116   uint32_t                   mFrameCount;
117   int                        mFrameIndex;
118   std::vector<UrlStore>      mImageUrls;
119   uint16_t                   mCacheSize;
120   CircularQueue<ImageFrame>  mQueue;
121 };
122
123 } // namespace Internal
124
125 } // namespace Toolkit
126
127 } // namespace Dali
128
129 #endif //DALI_TOOLKIT_INTERNAL_ROLLING_ANIMATED_IMAGE_CACHE_H