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