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