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