DALi Version 2.2.11
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-image / animated-image-visual.h
1 #ifndef DALI_TOOLKIT_INTERNAL_ANIMATED_IMAGE_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_ANIMATED_IMAGE_VISUAL_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
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/adaptor-framework/animated-image-loading.h>
23 #include <dali/public-api/adaptor-framework/timer.h>
24 #include <dali/public-api/common/dali-vector.h>
25 #include <dali/public-api/common/intrusive-ptr.h>
26 #include <dali/public-api/math/vector4.h>
27 #include <dali/public-api/object/weak-handle.h>
28
29 // INTERNAL INCLUDES
30 #include <dali-toolkit/devel-api/visuals/animated-image-visual-actions-devel.h>
31 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
32 #include <dali-toolkit/internal/visuals/animated-image/image-cache.h>
33 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
34 #include <dali-toolkit/internal/visuals/visual-url.h>
35
36 namespace Dali
37 {
38 namespace Toolkit
39 {
40 namespace Internal
41 {
42 class ImageVisualShaderFactory;
43 class AnimatedImageVisual;
44 typedef IntrusivePtr<AnimatedImageVisual> AnimatedImageVisualPtr;
45
46 /**
47  * The visual which renders an animated image
48  *
49  * One of the following properties is mandatory
50  *
51  * | %Property Name     | Type              |
52  * |------------------- |-------------------|
53  * | url                | STRING            |
54  * | urls               | ARRAY of STRING   |
55  *
56  * The remaining properties are optional
57  * | pixelArea          | VECTOR4           |
58  * | wrapModeU          | INTEGER OR STRING |
59  * | wrapModeV          | INTEGER OR STRING |
60  * | cacheSize          | INTEGER           |
61  * | batchSize          | INTEGER           |
62  * | frameDelay         | INTEGER           |
63  *
64  * pixelArea is a rectangular area.
65  * In its Vector4 value, the first two elements indicate the top-left position of the area,
66  * and the last two elements are the area width and height respectively.
67  * If not specified, the default value is [0.0, 0.0, 1.0, 1.0], i.e. the entire area of the image.
68  *
69  * wrapModeU and wrapModeV separately decide how the texture should be sampled when the u and v coordinate exceeds the range of 0.0 to 1.0.
70  * Its value should be one of the following wrap mode:
71  *   "DEFAULT"
72  *   "CLAMP_TO_EDGE"
73  *   "REPEAT"
74  *   "MIRRORED_REPEAT"
75  *
76  * cacheSize is used with multiple images - it determines how many images are kept pre-loaded
77  * batchSize is used with multiple images - it determines how many images to load on each frame
78  * frameDelay is used with multiple images - it is the number of milliseconds between each frame
79  */
80
81 class AnimatedImageVisual : public Visual::Base,
82                             public ConnectionTracker,
83                             public ImageCache::FrameReadyObserver
84 {
85 public:
86   /**
87    * @brief Create the animated image Visual using the image URL.
88    *
89    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
90    * @param[in] shaderFactory The ImageVisualShaderFactory object
91    * @param[in] imageUrl The URL to animated image resource to use
92    * @param[in] properties A Property::Map containing settings for this visual
93    * @return A smart-pointer to the newly allocated visual.
94    */
95   static AnimatedImageVisualPtr New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties);
96
97   /**
98    * @brief Create the animated image Visual using image URLs.
99    *
100    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
101    * @param[in] shaderFactory The ImageVisualShaderFactory object
102    * @param[in] imageUrls A Property::Array containing the URLs to the image resources
103    * @param[in] properties A Property::Map containing settings for this visual
104    * @return A smart-pointer to the newly allocated visual.
105    */
106   static AnimatedImageVisualPtr New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const Property::Array& imageUrls, const Property::Map& properties);
107
108   /**
109    * @brief Create the animated image visual using the image URL.
110    *
111    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
112    * @param[in] shaderFactory The ImageVisualShaderFactory object
113    * @param[in] imageUrl The URL to animated image resource to use
114    * @param[in] size The width and height of the image. The visual size will be used if these are 0.
115    */
116   static AnimatedImageVisualPtr New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, ImageDimensions size = ImageDimensions());
117
118 public: // from Visual
119   /**
120    * @copydoc Visual::Base::GetNaturalSize
121    */
122   void GetNaturalSize(Vector2& naturalSize) override;
123
124   /**
125    * @copydoc Visual::Base::CreatePropertyMap
126    */
127   void DoCreatePropertyMap(Property::Map& map) const override;
128
129   /**
130    * @copydoc Visual::Base::CreateInstancePropertyMap
131    */
132   void DoCreateInstancePropertyMap(Property::Map& map) const override;
133
134   /**
135    * @copydoc Visual::Base::OnDoAction
136    */
137   void OnDoAction(const Dali::Property::Index actionId, const Dali::Property::Value& attributes) override;
138
139 protected:
140   /**
141    * @brief Constructor.
142    *
143    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
144    * @param[in] shaderFactory The ImageVisualShaderFactory object
145    * @param[in] desiredSize The width and height of the image. The visual size will be used if these are 0.
146    */
147   AnimatedImageVisual(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, ImageDimensions desiredSize);
148
149   /**
150    * @brief A reference counted object may only be deleted by calling Unreference().
151    */
152   ~AnimatedImageVisual() override;
153
154   /**
155    * @copydoc Visual::Base::OnInitialize
156    */
157   void OnInitialize() override;
158
159   /**
160    * @copydoc Visual::Base::DoSetProperties
161    */
162   void DoSetProperties(const Property::Map& propertyMap) override;
163
164   /**
165    * Helper method to set individual values by index key.
166    * @param[in] index The index key of the value
167    * @param[in] value The value
168    */
169   void DoSetProperty(Property::Index index, const Property::Value& value);
170
171   /**
172    * @copydoc Visual::Base::DoSetOnScene
173    */
174   void DoSetOnScene(Actor& actor) override;
175
176   /**
177    * @copydoc Visual::Base::DoSetOffScene
178    */
179   void DoSetOffScene(Actor& actor) override;
180
181   /**
182    * @copydoc Visual::Base::OnSetTransform
183    */
184   void OnSetTransform() override;
185
186   /**
187    * @copydoc Visual::Base::UpdateShader
188    */
189   void UpdateShader() override;
190
191   /**
192    * @copydoc Visual::Base::GenerateShader
193    */
194   Shader GenerateShader() const override;
195
196 private:
197   /**
198    * @brief Initialize the animated image variables.
199    * @param[in] imageUrl The url of the animated image
200    */
201   void InitializeAnimatedImage(const VisualUrl& imageUrl);
202
203   /**
204    * @brief Create image cache for animated image or image array.
205    */
206   void CreateImageCache();
207
208   /**
209    * @brief Adds the texture set to the renderer, and the renderer to the
210    * placement actor, and starts the frame timer
211    * @param[in] textureSet    The texture set to apply
212    * @param[in] firstInterval frame interval(ms) for the first frame.
213    */
214   void StartFirstFrame(TextureSet& textureSet, uint32_t firstInterval);
215
216   /**
217    * @brief Prepares the texture set for displaying
218    */
219   void PrepareTextureSet();
220
221   /**
222    * @brief Set the image size from the texture set
223    * @param[in] textureSet The texture set to get the size from
224    */
225   void SetImageSize(TextureSet& textureSet);
226
227   /**
228    * @brief Called when the next frame is ready.
229    * @param[in] textureSet the texture set to apply
230    * @param[in] interval interval(ms) for the frame
231    * @param[in] preMultiplied whether the texture is premultied alpha or not.
232    */
233   void FrameReady(TextureSet textureSet, uint32_t interval, bool preMultiplied) override;
234
235   /**
236    * @brief Display the next frame. It is called when the mFrameDelayTimer ticks.
237    * @return true to ensure the timer continues running.
238    */
239   bool DisplayNextFrame();
240
241   /**
242    * @brief Set the state of loading fail of an image or a frame.
243    * @return TextureSet of broken image.
244    */
245   TextureSet SetLoadingFailed();
246
247   /**
248    * @brief Allocate mask data.
249    * This is allocated only once.
250    */
251   void AllocateMaskData();
252
253   /**
254    * @brief Check whether the mask texture is loaded or not.
255    * If MaskingType is MASKING_ON_LOADING and mask texture is failed to load, update shader.
256    */
257   void CheckMaskTexture();
258
259   // Undefined
260   AnimatedImageVisual(const AnimatedImageVisual& animatedImageVisual);
261
262   // Undefined
263   AnimatedImageVisual& operator=(const AnimatedImageVisual& animatedImageVisual);
264
265 private:
266   Timer                     mFrameDelayTimer;
267   WeakHandle<Actor>         mPlacementActor;
268   ImageVisualShaderFactory& mImageVisualShaderFactory;
269
270   // Variables for Animated Image player
271   Vector4                    mPixelArea;
272   VisualUrl                  mImageUrl;
273   Dali::AnimatedImageLoading mAnimatedImageLoading; // Only needed for animated image
274   uint32_t                   mFrameIndexForJumpTo;  // Frame index into textureRects
275   uint32_t                   mCurrentFrameIndex;
276
277   // Variables for Multi-Image player
278   ImageCache::UrlList* mImageUrls;
279   ImageCache*          mImageCache;
280   uint16_t             mCacheSize;
281   uint16_t             mBatchSize;
282   uint16_t             mFrameDelay;
283   int16_t              mLoopCount;
284   int16_t              mCurrentLoopIndex;
285
286   // Variables for image visual properties.
287   Dali::Toolkit::ImageVisual::LoadPolicy::Type    mLoadPolicy;
288   Dali::Toolkit::ImageVisual::ReleasePolicy::Type mReleasePolicy;
289   TextureManager::MaskingDataPointer              mMaskingData;
290   Dali::ImageDimensions                           mDesiredSize;
291
292   // Shared variables
293   uint32_t        mFrameCount; // Number of frames
294   ImageDimensions mImageSize;
295
296   DevelAnimatedImageVisual::Action::Type mActionStatus;
297
298   Dali::WrapMode::Type                 mWrapModeU : 3;
299   Dali::WrapMode::Type                 mWrapModeV : 3;
300   Dali::FittingMode::Type              mFittingMode : 3;
301   Dali::SamplingMode::Type             mSamplingMode : 4;
302   DevelImageVisual::StopBehavior::Type mStopBehavior : 2;
303   bool                                 mStartFirstFrame : 1;
304   bool                                 mIsJumpTo : 1;
305 };
306
307 } // namespace Internal
308
309 } // namespace Toolkit
310
311 } // namespace Dali
312 #endif /* DALI_TOOLKIT_INTERNAL_ANIMATED_IMAGE_VISUAL_H */