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