(Vector) Change event processing
[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) 2020 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/public-api/common/dali-vector.h>
23 #include <dali/public-api/common/intrusive-ptr.h>
24 #include <dali/public-api/math/vector4.h>
25 #include <dali/public-api/object/weak-handle.h>
26 #include <dali/public-api/adaptor-framework/timer.h>
27 #include <dali/devel-api/adaptor-framework/animated-image-loading.h>
28
29 // INTERNAL INCLUDES
30 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
31 #include <dali-toolkit/internal/visuals/visual-url.h>
32 #include <dali-toolkit/internal/visuals/animated-image/image-cache.h>
33 #include <dali-toolkit/devel-api/visuals/animated-image-visual-actions-devel.h>
34 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
35
36 namespace Dali
37 {
38
39 namespace Toolkit
40 {
41
42 namespace Internal
43 {
44
45 class ImageVisualShaderFactory;
46 class AnimatedImageVisual;
47 typedef IntrusivePtr< AnimatedImageVisual > AnimatedImageVisualPtr;
48
49 /**
50  * The visual which renders an animated image
51  *
52  * One of the following properties is mandatory
53  *
54  * | %Property Name     | Type              |
55  * |------------------- |-------------------|
56  * | url                | STRING            |
57  * | urls               | ARRAY of STRING   |
58  *
59  * The remaining properties are optional
60  * | pixelArea          | VECTOR4           |
61  * | wrapModeU          | INTEGER OR STRING |
62  * | wrapModeV          | INTEGER OR STRING |
63  * | cacheSize          | INTEGER           |
64  * | batchSize          | INTEGER           |
65  * | frameDelay         | INTEGER           |
66  *
67  * pixelArea is a rectangular area.
68  * In its Vector4 value, the first two elements indicate the top-left position of the area,
69  * and the last two elements are the area width and height respectively.
70  * If not specified, the default value is [0.0, 0.0, 1.0, 1.0], i.e. the entire area of the image.
71  *
72  * 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.
73  * Its value should be one of the following wrap mode:
74  *   "DEFAULT"
75  *   "CLAMP_TO_EDGE"
76  *   "REPEAT"
77  *   "MIRRORED_REPEAT"
78  *
79  * cacheSize is used with multiple images - it determines how many images are kept pre-loaded
80  * batchSize is used with multiple images - it determines how many images to load on each frame
81  * frameDelay is used with multiple images - it is the number of milliseconds between each frame
82  */
83
84 class AnimatedImageVisual : public Visual::Base,
85                             public ConnectionTracker,
86                             public ImageCache::FrameReadyObserver
87 {
88
89 public:
90
91   /**
92    * @brief Create the animated image Visual using the image URL.
93    *
94    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
95    * @param[in] shaderFactory The ImageVisualShaderFactory object
96    * @param[in] imageUrl The URL to animated image resource to use
97    * @param[in] properties A Property::Map containing settings for this visual
98    * @return A smart-pointer to the newly allocated visual.
99    */
100   static AnimatedImageVisualPtr New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties );
101
102   /**
103    * @brief Create the animated image Visual using image URLs.
104    *
105    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
106    * @param[in] shaderFactory The ImageVisualShaderFactory object
107    * @param[in] imageUrls A Property::Array containing the URLs to the image resources
108    * @param[in] properties A Property::Map containing settings for this visual
109    * @return A smart-pointer to the newly allocated visual.
110    */
111   static AnimatedImageVisualPtr New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const Property::Array& imageUrls, const Property::Map& properties );
112
113   /**
114    * @brief Create the animated image visual using the image URL.
115    *
116    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
117    * @param[in] shaderFactory The ImageVisualShaderFactory object
118    * @param[in] imageUrl The URL to animated image resource to use
119    */
120   static AnimatedImageVisualPtr New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl );
121
122 public:  // from Visual
123
124   /**
125    * @copydoc Visual::Base::GetNaturalSize
126    */
127   void GetNaturalSize( Vector2& naturalSize ) override;
128
129   /**
130    * @copydoc Visual::Base::CreatePropertyMap
131    */
132   void DoCreatePropertyMap( Property::Map& map ) const override;
133
134   /**
135    * @copydoc Visual::Base::CreateInstancePropertyMap
136    */
137   void DoCreateInstancePropertyMap( Property::Map& map ) const override;
138
139   /**
140    * @copydoc Visual::Base::OnDoAction
141    */
142   void OnDoAction( const Dali::Property::Index actionName, const Dali::Property::Value& attributes ) override;
143
144 protected:
145
146   /**
147    * @brief Constructor.
148    *
149    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
150    * @param[in] shaderFactory The ImageVisualShaderFactory object
151    */
152   AnimatedImageVisual( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory );
153
154   /**
155    * @brief A reference counted object may only be deleted by calling Unreference().
156    */
157   virtual ~AnimatedImageVisual();
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 private:
187   /**
188    * Creates the renderer for the animated image
189    */
190   void CreateRenderer();
191
192   /**
193    * Starts the Load of the first batch of URLs
194    */
195   void LoadFirstBatch();
196
197   /**
198    * Adds the texture set to the renderer, and the renderer to the
199    * placement actor, and starts the frame timer
200    * @param[in] textureSet The texture set to apply
201    */
202   void StartFirstFrame( TextureSet& textureSet );
203
204   /**
205    * Prepares the texture set for displaying
206    */
207   TextureSet PrepareTextureSet();
208
209   /**
210    * Set the image size from the texture set
211    * @param[in] textureSet The texture set to get the size from
212    */
213   void SetImageSize( TextureSet& textureSet );
214
215   /**
216    * Called when the next frame is ready.
217    * @param[in] textureSet the texture set to apply
218    */
219   void FrameReady( TextureSet textureSet );
220
221   /**
222    * Display the next frame. It is called when the mFrameDelayTimer ticks.
223    * Returns true to ensure the timer continues running.
224    */
225   bool DisplayNextFrame();
226
227   /**
228    * Initialize the animated image variables.
229    * @param[in] imageUrl The url of the animated image
230    */
231   void InitializeAnimatedImage( const VisualUrl& imageUrl );
232
233   // Undefined
234   AnimatedImageVisual( const AnimatedImageVisual& animatedImageVisual );
235
236   // Undefined
237   AnimatedImageVisual& operator=( const AnimatedImageVisual& animatedImageVisual );
238
239 private:
240
241   Timer mFrameDelayTimer;
242   WeakHandle<Actor> mPlacementActor;
243   ImageVisualShaderFactory& mImageVisualShaderFactory;
244
245   // Variables for Animated Image player
246   Vector4 mPixelArea;
247   VisualUrl mImageUrl;
248   Dali::AnimatedImageLoading mAnimatedImageLoading; // Only needed for animated image
249   uint32_t mCurrentFrameIndex; // Frame index into textureRects
250
251   // Variables for Multi-Image player
252   ImageCache::UrlList* mImageUrls;
253   ImageCache* mImageCache;
254   uint16_t mCacheSize;
255   uint16_t mBatchSize;
256   uint16_t mFrameDelay;
257   int16_t mLoopCount;
258   int16_t mCurrentLoopIndex;
259   uint16_t mUrlIndex;
260
261   // Shared variables
262   uint32_t mFrameCount; // Number of frames
263   ImageDimensions mImageSize;
264
265   Dali::WrapMode::Type mWrapModeU:3;
266   Dali::WrapMode::Type mWrapModeV:3;
267   DevelAnimatedImageVisual::Action::Type mActionStatus:3;
268   DevelImageVisual::StopBehavior::Type   mStopBehavior:2;
269   bool mStartFirstFrame:1;
270   bool mIsJumpTo:1;
271 };
272
273 } // namespace Internal
274
275 } // namespace Toolkit
276
277 } // namespace Dali
278 #endif /* DALI_TOOLKIT_INTERNAL_ANIMATED_IMAGE_VISUAL_H */