[dali_2.3.19] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-image / image-cache.h
1 #ifndef DALI_TOOLKIT_INTERNAL_IMAGE_CACHE_H
2 #define DALI_TOOLKIT_INTERNAL_IMAGE_CACHE_H
3
4 /*
5  * Copyright (c) 2023 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/texture-manager/texture-upload-observer.h>
23
24 namespace Dali
25 {
26 namespace Toolkit
27 {
28 namespace Internal
29 {
30 class ImageCache : public TextureManager::LifecycleObserver
31 {
32 public:
33   /**
34    * Observer class to inform when the next image is ready
35    */
36   class FrameReadyObserver
37   {
38   public:
39     /**
40      * @brief Informs observer when the next texture set is ready to display
41      * @param[in] textureSet The ready texture set
42      * @param[in] interval interval(ms) for the frame
43      * @param[in] preMultiplied whether the texture is premultied alpha or not.
44      */
45     virtual void FrameReady(TextureSet textureSet, uint32_t interval, bool preMultiplied) = 0;
46   };
47
48   struct UrlStore
49   {
50     TextureManager::TextureId mTextureId = TextureManager::INVALID_TEXTURE_ID;
51     VisualUrl                 mUrl;
52   };
53
54   /**
55    * List of URLs
56    */
57   typedef std::vector<UrlStore> UrlList;
58
59 public:
60   /**
61    * @brief Constructor.
62    * @param[in] textureManager    The texture manager
63    * @param[in] size              The width and height to fit the loaded image to.
64    * @param[in] fittingMode       The FittingMode of the resource to load
65    * @param[in] samplingMode      The SamplingMode of the resource to load
66    * @param[in] observer          FrameReady observer
67    * @param[in] maskingData       Masking data to be applied.
68    * @param[in] batchSize         The size of a batch to load
69    * @param[in] interval          Time interval(ms) between each frame
70    * @param[in] preMultiplyOnLoad The flag if image's color should be multiplied by it's alpha
71    *
72    * This will start loading textures immediately, according to the
73    * batch and cache sizes. The cache is as large as the number of urls.
74    */
75   ImageCache(TextureManager&                     textureManager,
76              ImageDimensions                     size,
77              Dali::FittingMode::Type             fittingMode,
78              Dali::SamplingMode::Type            samplingMode,
79              TextureManager::MaskingDataPointer& maskingData,
80              ImageCache::FrameReadyObserver&     observer,
81              uint32_t                            batchSize,
82              uint32_t                            interval,
83              bool                                preMultiplyOnLoad);
84
85   virtual ~ImageCache();
86
87   /**
88    * @brief Get the first frame. If it's not ready, this will trigger the
89    * sending of FrameReady() when the image becomes ready.
90    *
91    * @return TextureSet of the first frame.
92    */
93   virtual TextureSet FirstFrame() = 0;
94
95   /**
96    * @brief Get the Nth frame. If it's not ready, this will trigger the
97    * sending of FrameReady() when the image becomes ready.
98    *
99    * @param[in] frameIndex required frame index to be returned.
100    * @return TextureSet of the frame index.
101    */
102   virtual TextureSet Frame(uint32_t frameIndex) = 0;
103
104   /**
105    * @brief Get the interval(ms) of Nth frame.
106    *
107    * @param[in] frameIndex frame index to get frame interval.
108    * @return Time interval in millisecond between frames of frameIndex and frameIndex + 1.
109    */
110   virtual uint32_t GetFrameInterval(uint32_t frameIndex) const = 0;
111
112   /**
113    * @brief Get the current rendered frame index.
114    * If there isn't any loaded frame, returns -1.
115    *
116    * @return Frame index of currently showing frame.
117    */
118   virtual int32_t GetCurrentFrameIndex() const = 0;
119
120   /**
121    * @brief Get total frame count of the animated image file.
122    *
123    * @return Total frame count of the animated image file.
124    */
125   virtual int32_t GetTotalFrameCount() const = 0;
126
127   /**
128    * @brief Clears animated image cache and remove loaded textures.
129    */
130   virtual void ClearCache() = 0;
131
132   /**
133    * @brief Set default interval(ms) between each frame.
134    *
135    * @param[in] interval time interval in millisecond to be used as default interval.
136    */
137   virtual void SetInterval(uint32_t interval);
138
139 private:
140   /**
141    * @brief Called before the texture manager is destroyed.
142    */
143   void TextureManagerDestroyed() final;
144
145   void AllocateMaskData();
146
147 protected:
148   TextureManager&                     mTextureManager;
149   FrameReadyObserver&                 mObserver;
150   TextureManager::MaskingDataPointer& mMaskingData;
151   Dali::ImageDimensions               mDesiredSize;
152   Dali::FittingMode::Type             mFittingMode : 3;
153   Dali::SamplingMode::Type            mSamplingMode : 4;
154   uint32_t                            mBatchSize;
155   uint32_t                            mInterval;
156   TextureManager::LoadState           mLoadState;
157   bool                                mRequestingLoad : 1;
158   bool                                mPreMultiplyOnLoad : 1;
159   bool                                mTextureManagerAlive : 1;
160 };
161
162 } // namespace Internal
163 } // namespace Toolkit
164 } // namespace Dali
165
166 #endif // DALI_TOOLKIT_INTERNAL_IMAGE_CACHE_H