1 #ifndef DALI_TOOLKIT_INTERNAL_ANIMATED_IMAGE_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_ANIMATED_IMAGE_VISUAL_H
5 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
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>
45 class ImageVisualShaderFactory;
46 class AnimatedImageVisual;
47 typedef IntrusivePtr< AnimatedImageVisual > AnimatedImageVisualPtr;
50 * The visual which renders an animated image
52 * One of the following properties is mandatory
54 * | %Property Name | Type |
55 * |------------------- |-------------------|
57 * | urls | ARRAY of STRING |
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 |
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.
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:
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
84 class AnimatedImageVisual : public Visual::Base,
85 public ConnectionTracker,
86 public ImageCache::FrameReadyObserver
92 * @brief Create the animated image Visual using the image URL.
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.
100 static AnimatedImageVisualPtr New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties );
103 * @brief Create the animated image Visual using image URLs.
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.
111 static AnimatedImageVisualPtr New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const Property::Array& imageUrls, const Property::Map& properties );
114 * @brief Create the animated image visual using the image URL.
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
120 static AnimatedImageVisualPtr New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl );
122 public: // from Visual
125 * @copydoc Visual::Base::GetNaturalSize
127 void GetNaturalSize( Vector2& naturalSize ) override;
130 * @copydoc Visual::Base::CreatePropertyMap
132 void DoCreatePropertyMap( Property::Map& map ) const override;
135 * @copydoc Visual::Base::CreateInstancePropertyMap
137 void DoCreateInstancePropertyMap( Property::Map& map ) const override;
140 * @copydoc Visual::Base::OnDoAction
142 void OnDoAction( const Dali::Property::Index actionName, const Dali::Property::Value& attributes ) override;
147 * @brief Constructor.
149 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
150 * @param[in] shaderFactory The ImageVisualShaderFactory object
152 AnimatedImageVisual( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory );
155 * @brief A reference counted object may only be deleted by calling Unreference().
157 ~AnimatedImageVisual() override;
160 * @copydoc Visual::Base::DoSetProperties
162 void DoSetProperties( const Property::Map& propertyMap ) override;
165 * Helper method to set individual values by index key.
166 * @param[in] index The index key of the value
167 * @param[in] value The value
169 void DoSetProperty( Property::Index index, const Property::Value& value );
172 * @copydoc Visual::Base::DoSetOnScene
174 void DoSetOnScene( Actor& actor ) override;
177 * @copydoc Visual::Base::DoSetOffScene
179 void DoSetOffScene( Actor& actor ) override;
182 * @copydoc Visual::Base::OnSetTransform
184 void OnSetTransform() override;
188 * Creates the renderer for the animated image
190 void CreateRenderer();
193 * Starts the Load of the first batch of URLs
195 void LoadFirstBatch();
198 * Adds the texture set to the renderer, and the renderer to the
199 * placement actor, and starts the frame timer
200 * @param[in] textureSet The texture set to apply
202 void StartFirstFrame( TextureSet& textureSet );
205 * Prepares the texture set for displaying
207 TextureSet PrepareTextureSet();
210 * Set the image size from the texture set
211 * @param[in] textureSet The texture set to get the size from
213 void SetImageSize( TextureSet& textureSet );
216 * Called when the next frame is ready.
217 * @param[in] textureSet the texture set to apply
219 void FrameReady( TextureSet textureSet ) override;
222 * Display the next frame. It is called when the mFrameDelayTimer ticks.
223 * Returns true to ensure the timer continues running.
225 bool DisplayNextFrame();
228 * Initialize the animated image variables.
229 * @param[in] imageUrl The url of the animated image
231 void InitializeAnimatedImage( const VisualUrl& imageUrl );
234 AnimatedImageVisual( const AnimatedImageVisual& animatedImageVisual );
237 AnimatedImageVisual& operator=( const AnimatedImageVisual& animatedImageVisual );
241 Timer mFrameDelayTimer;
242 WeakHandle<Actor> mPlacementActor;
243 ImageVisualShaderFactory& mImageVisualShaderFactory;
245 // Variables for Animated Image player
248 Dali::AnimatedImageLoading mAnimatedImageLoading; // Only needed for animated image
249 uint32_t mCurrentFrameIndex; // Frame index into textureRects
251 // Variables for Multi-Image player
252 ImageCache::UrlList* mImageUrls;
253 ImageCache* mImageCache;
256 uint16_t mFrameDelay;
258 int16_t mCurrentLoopIndex;
262 uint32_t mFrameCount; // Number of frames
263 ImageDimensions mImageSize;
265 Dali::WrapMode::Type mWrapModeU:3;
266 Dali::WrapMode::Type mWrapModeV:3;
267 DevelAnimatedImageVisual::Action::Type mActionStatus:3;
268 DevelImageVisual::StopBehavior::Type mStopBehavior:2;
269 bool mStartFirstFrame:1;
273 } // namespace Internal
275 } // namespace Toolkit
278 #endif /* DALI_TOOLKIT_INTERNAL_ANIMATED_IMAGE_VISUAL_H */