5f4aa4666847f928db9e95460092b28ac4766650
[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) 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
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 images, where the cache size
35  * is smaller than the total number of images.
36  */
37 class RollingImageCache : public ImageCache, public TextureUploadObserver
38 {
39 public:
40   /**
41    * Constructor.
42    * @param[in] textureManager The texture manager
43    * @param[in] urlList List of urls to cache
44    * @param[in] observer FrameReady observer
45    * @param[in] cacheSize The size of the cache
46    * @param[in] batchSize The size of a batch to load
47    *
48    * This will start loading textures immediately, according to the
49    * batch and cache sizes.
50    */
51   RollingImageCache( TextureManager&                 textureManager,
52                      UrlList&                        urlList,
53                      ImageCache::FrameReadyObserver& observer,
54                      uint16_t                        cacheSize,
55                      uint16_t                        batchSize );
56
57   /**
58    * Destructor
59    */
60   ~RollingImageCache() override;
61
62   /**
63    * Get the Nth frame. If it's not ready, this will trigger the
64    * sending of FrameReady() when the image becomes ready.
65    */
66   TextureSet Frame( uint32_t frameIndex ) override;
67
68   /**
69    * Get the first frame. If it's not ready, this will trigger the
70    * sending of FrameReady() when the image becomes ready.
71    */
72   TextureSet FirstFrame() override;
73
74   /**
75    * Get the interval of Nth frame.
76    */
77   uint32_t GetFrameInterval( uint32_t frameIndex ) override;
78
79 private:
80   /**
81    * @return true if the front frame is ready
82    */
83   bool IsFrontReady() const;
84
85   /**
86    * Load the next batch of images
87    */
88   void LoadBatch();
89
90   /**
91    * Find the matching image frame, and set it to ready
92    */
93   void SetImageFrameReady( TextureManager::TextureId textureId );
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   /**
107    * Check if the front frame has become ready - if so, inform observer
108    * @param[in] wasReady Readiness before call.
109    */
110   void CheckFrontFrame( bool wasReady );
111
112 protected:
113   void UploadComplete(
114     bool           loadSuccess,
115     int32_t        textureId,
116     TextureSet     textureSet,
117     bool           useAtlasing,
118     const Vector4& atlasRect,
119     bool           preMultiplied ) override;
120
121   void LoadComplete(
122     bool loadSuccess,
123     Devel::PixelBuffer pixelBuffer,
124     const VisualUrl& url,
125     bool preMultiplied ) override;
126
127 private:
128   /**
129    * Secondary class to hold readiness and index into url
130    */
131   struct ImageFrame
132   {
133     unsigned int mUrlIndex = 0u;
134     bool mReady = false;
135   };
136
137   std::vector<UrlStore>& mImageUrls;
138   CircularQueue<ImageFrame> mQueue;
139 };
140
141 } // namespace Internal
142 } // namespace Toolkit
143 } // namespace Dali
144
145 #endif