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