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