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