347e9d306b499c45c1ab2490f49d6240bf90726c
[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) 2021 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    */
115   static AnimatedImageVisualPtr New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl);
116
117 public: // from Visual
118   /**
119    * @copydoc Visual::Base::GetNaturalSize
120    */
121   void GetNaturalSize(Vector2& naturalSize) override;
122
123   /**
124    * @copydoc Visual::Base::CreatePropertyMap
125    */
126   void DoCreatePropertyMap(Property::Map& map) const override;
127
128   /**
129    * @copydoc Visual::Base::CreateInstancePropertyMap
130    */
131   void DoCreateInstancePropertyMap(Property::Map& map) const override;
132
133   /**
134    * @copydoc Visual::Base::OnDoAction
135    */
136   void OnDoAction(const Dali::Property::Index actionName, const Dali::Property::Value& attributes) override;
137
138 protected:
139   /**
140    * @brief Constructor.
141    *
142    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
143    * @param[in] shaderFactory The ImageVisualShaderFactory object
144    */
145   AnimatedImageVisual(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory);
146
147   /**
148    * @brief A reference counted object may only be deleted by calling Unreference().
149    */
150   ~AnimatedImageVisual() override;
151
152   /**
153    * @copydoc Visual::Base::OnInitialize
154    */
155   void OnInitialize() override;
156
157   /**
158    * @copydoc Visual::Base::DoSetProperties
159    */
160   void DoSetProperties(const Property::Map& propertyMap) override;
161
162   /**
163    * Helper method to set individual values by index key.
164    * @param[in] index The index key of the value
165    * @param[in] value The value
166    */
167   void DoSetProperty(Property::Index index, const Property::Value& value);
168
169   /**
170    * @copydoc Visual::Base::DoSetOnScene
171    */
172   void DoSetOnScene(Actor& actor) override;
173
174   /**
175    * @copydoc Visual::Base::DoSetOffScene
176    */
177   void DoSetOffScene(Actor& actor) override;
178
179   /**
180    * @copydoc Visual::Base::OnSetTransform
181    */
182   void OnSetTransform() override;
183
184   /**
185    * @copydoc Visual::Base::UpdateShader
186    */
187   void UpdateShader() override;
188
189   /**
190    * @copydoc Visual::Base::GenerateShader
191    */
192   Shader GenerateShader() const override;
193
194 private:
195   /**
196    * Creates the renderer for the animated image
197    */
198   void CreateRenderer();
199
200   /**
201    * Starts the Load of the first batch of URLs
202    */
203   void LoadFirstBatch();
204
205   /**
206    * Adds the texture set to the renderer, and the renderer to the
207    * placement actor, and starts the frame timer
208    * @param[in] textureSet The texture set to apply
209    */
210   void StartFirstFrame(TextureSet& textureSet);
211
212   /**
213    * Prepares the texture set for displaying
214    */
215   TextureSet PrepareTextureSet();
216
217   /**
218    * Set the image size from the texture set
219    * @param[in] textureSet The texture set to get the size from
220    */
221   void SetImageSize(TextureSet& textureSet);
222
223   /**
224    * Called when the next frame is ready.
225    * @param[in] textureSet the texture set to apply
226    */
227   void FrameReady(TextureSet textureSet) override;
228
229   /**
230    * Display the next frame. It is called when the mFrameDelayTimer ticks.
231    * Returns true to ensure the timer continues running.
232    */
233   bool DisplayNextFrame();
234
235   /**
236    * Initialize the animated image variables.
237    * @param[in] imageUrl The url of the animated image
238    */
239   void InitializeAnimatedImage(const VisualUrl& imageUrl);
240
241   /**
242    * Set the state of loading fail of an image or a frame.
243    * Returns TextureSet of broken image.
244    */
245   TextureSet SetLoadingFailed();
246
247   // Undefined
248   AnimatedImageVisual(const AnimatedImageVisual& animatedImageVisual);
249
250   // Undefined
251   AnimatedImageVisual& operator=(const AnimatedImageVisual& animatedImageVisual);
252
253 private:
254   Timer                     mFrameDelayTimer;
255   WeakHandle<Actor>         mPlacementActor;
256   ImageVisualShaderFactory& mImageVisualShaderFactory;
257
258   // Variables for Animated Image player
259   Vector4                    mPixelArea;
260   VisualUrl                  mImageUrl;
261   Dali::AnimatedImageLoading mAnimatedImageLoading; // Only needed for animated image
262   uint32_t                   mFrameIndexForJumpTo;  // Frame index into textureRects
263
264   // Variables for Multi-Image player
265   ImageCache::UrlList* mImageUrls;
266   ImageCache*          mImageCache;
267   uint16_t             mCacheSize;
268   uint16_t             mBatchSize;
269   uint16_t             mFrameDelay;
270   int16_t              mLoopCount;
271   int16_t              mCurrentLoopIndex;
272   uint16_t             mUrlIndex;
273
274   // Shared variables
275   uint32_t        mFrameCount; // Number of frames
276   ImageDimensions mImageSize;
277
278   Dali::WrapMode::Type                   mWrapModeU : 3;
279   Dali::WrapMode::Type                   mWrapModeV : 3;
280   DevelAnimatedImageVisual::Action::Type mActionStatus : 3;
281   DevelImageVisual::StopBehavior::Type   mStopBehavior : 2;
282   bool                                   mStartFirstFrame : 1;
283   bool                                   mIsJumpTo : 1;
284 };
285
286 } // namespace Internal
287
288 } // namespace Toolkit
289
290 } // namespace Dali
291 #endif /* DALI_TOOLKIT_INTERNAL_ANIMATED_IMAGE_VISUAL_H */