Loop count support for animated GIF
[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) 2018 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/gif-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
34 namespace Dali
35 {
36
37 namespace Toolkit
38 {
39
40 namespace Internal
41 {
42
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
86 public:
87
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] imageUrl The URL to gif resource to use
93    * @param[in] properties A Property::Map containing settings for this visual
94    * @return A smart-pointer to the newly allocated visual.
95    */
96   static AnimatedImageVisualPtr New( VisualFactoryCache& factoryCache, const VisualUrl& imageUrl, const Property::Map& properties );
97
98   /**
99    * @brief Create the animated image Visual using image URLs.
100    *
101    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache 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, 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] imageUrl The URL to animated image resource to use
113    */
114   static AnimatedImageVisualPtr New( VisualFactoryCache& factoryCache, const VisualUrl& imageUrl );
115
116 public:  // from Visual
117
118   /**
119    * @copydoc Visual::Base::GetNaturalSize
120    */
121   virtual void GetNaturalSize( Vector2& naturalSize );
122
123   /**
124    * @copydoc Visual::Base::CreatePropertyMap
125    */
126   virtual void DoCreatePropertyMap( Property::Map& map ) const;
127
128   /**
129    * @copydoc Visual::Base::CreateInstancePropertyMap
130    */
131   virtual void DoCreateInstancePropertyMap( Property::Map& map ) const;
132
133 protected:
134
135   /**
136    * @brief Constructor.
137    *
138    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
139    */
140   AnimatedImageVisual( VisualFactoryCache& factoryCache );
141
142   /**
143    * @brief A reference counted object may only be deleted by calling Unreference().
144    */
145   virtual ~AnimatedImageVisual();
146
147   /**
148    * @copydoc Visual::Base::DoSetProperties
149    */
150   virtual void DoSetProperties( const Property::Map& propertyMap );
151
152   /**
153    * Helper method to set individual values by index key.
154    * @param[in] index The index key of the value
155    * @param[in] value The value
156    */
157   void DoSetProperty( Property::Index index, const Property::Value& value );
158
159   /**
160    * @copydoc Visual::Base::DoSetOnStage
161    */
162   virtual void DoSetOnStage( Actor& actor );
163
164   /**
165    * @copydoc Visual::Base::DoSetOffStage
166    */
167   virtual void DoSetOffStage( Actor& actor );
168
169   /**
170    * @copydoc Visual::Base::OnSetTransform
171    */
172   virtual void OnSetTransform();
173
174 private:
175   /**
176    * Creates the renderer for the animated image
177    */
178   void CreateRenderer();
179
180   /**
181    * Starts the Load of the first batch of URLs
182    */
183   void LoadFirstBatch();
184
185   /**
186    * Adds the texture set to the renderer, and the renderer to the
187    * placement actor, and starts the frame timer
188    * @param[in] textureSet The texture set to apply
189    */
190   void StartFirstFrame( TextureSet& textureSet );
191
192   /**
193    * Prepares the texture set for displaying
194    */
195   TextureSet PrepareTextureSet();
196
197   /**
198    * Set the image size from the texture set
199    * @param[in] textureSet The texture set to get the size from
200    */
201   void SetImageSize( TextureSet& textureSet );
202
203   /**
204    * Called when the next frame is ready.
205    * @param[in] textureSet the texture set to apply
206    */
207   void FrameReady( TextureSet textureSet );
208
209   /**
210    * Display the next frame. It is called when the mFrameDelayTimer ticks.
211    * Returns true to ensure the timer continues running.
212    */
213   bool DisplayNextFrame();
214
215   /**
216    * Initialize the gif variables.
217    * @param[in] imageUrl The url of the animated gif
218    */
219   void InitializeGif( const VisualUrl& imageUrl );
220
221   // Undefined
222   AnimatedImageVisual( const AnimatedImageVisual& animatedImageVisual );
223
224   // Undefined
225   AnimatedImageVisual& operator=( const AnimatedImageVisual& animatedImageVisual );
226
227 private:
228
229   Timer mFrameDelayTimer;
230   WeakHandle<Actor> mPlacementActor;
231
232   // Variables for GIF player
233   Dali::Vector<uint32_t> mFrameDelayContainer;
234   Vector4 mPixelArea;
235   VisualUrl mImageUrl;
236   std::unique_ptr<Dali::GifLoading> mGifLoading; // Only needed for animated gifs
237   uint32_t mCurrentFrameIndex; // Frame index into textureRects
238
239   // Variables for Multi-Image player
240   ImageCache::UrlList* mImageUrls;
241   ImageCache* mImageCache;
242   uint16_t mCacheSize;
243   uint16_t mBatchSize;
244   uint16_t mFrameDelay;
245   int16_t mLoopCount;
246   int16_t mCurrentLoopIndex;
247   uint16_t mUrlIndex;
248
249   // Shared variables
250   uint32_t mFrameCount; // Number of frames
251   ImageDimensions mImageSize;
252
253   Dali::WrapMode::Type mWrapModeU:3;
254   Dali::WrapMode::Type mWrapModeV:3;
255   bool mStartFirstFrame:1;
256 };
257
258 } // namespace Internal
259
260 } // namespace Toolkit
261
262 } // namespace Dali
263 #endif /* DALI_TOOLKIT_INTERNAL_ANIMATED_IMAGE_VISUAL_H */