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