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