1 #ifndef DALI_TOOLKIT_INTERNAL_ANIMATED_IMAGE_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_ANIMATED_IMAGE_VISUAL_H
5 * Copyright (c) 2021 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::OnInitialize
162 void OnInitialize() override;
165 * @copydoc Visual::Base::DoSetProperties
167 void DoSetProperties( const Property::Map& propertyMap ) override;
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
174 void DoSetProperty( Property::Index index, const Property::Value& value );
177 * @copydoc Visual::Base::DoSetOnScene
179 void DoSetOnScene( Actor& actor ) override;
182 * @copydoc Visual::Base::DoSetOffScene
184 void DoSetOffScene( Actor& actor ) override;
187 * @copydoc Visual::Base::OnSetTransform
189 void OnSetTransform() override;
193 * Creates the renderer for the animated image
195 void CreateRenderer();
198 * Starts the Load of the first batch of URLs
200 void LoadFirstBatch();
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
207 void StartFirstFrame( TextureSet& textureSet );
210 * Prepares the texture set for displaying
212 TextureSet PrepareTextureSet();
215 * Set the image size from the texture set
216 * @param[in] textureSet The texture set to get the size from
218 void SetImageSize( TextureSet& textureSet );
221 * Called when the next frame is ready.
222 * @param[in] textureSet the texture set to apply
224 void FrameReady( TextureSet textureSet ) override;
227 * Display the next frame. It is called when the mFrameDelayTimer ticks.
228 * Returns true to ensure the timer continues running.
230 bool DisplayNextFrame();
233 * Initialize the animated image variables.
234 * @param[in] imageUrl The url of the animated image
236 void InitializeAnimatedImage( const VisualUrl& imageUrl );
239 AnimatedImageVisual( const AnimatedImageVisual& animatedImageVisual );
242 AnimatedImageVisual& operator=( const AnimatedImageVisual& animatedImageVisual );
246 Timer mFrameDelayTimer;
247 WeakHandle<Actor> mPlacementActor;
248 ImageVisualShaderFactory& mImageVisualShaderFactory;
250 // Variables for Animated Image player
253 Dali::AnimatedImageLoading mAnimatedImageLoading; // Only needed for animated image
254 uint32_t mFrameIndexForJumpTo; // Frame index into textureRects
256 // Variables for Multi-Image player
257 ImageCache::UrlList* mImageUrls;
258 ImageCache* mImageCache;
261 uint16_t mFrameDelay;
263 int16_t mCurrentLoopIndex;
267 uint32_t mFrameCount; // Number of frames
268 ImageDimensions mImageSize;
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;
278 } // namespace Internal
280 } // namespace Toolkit
283 #endif /* DALI_TOOLKIT_INTERNAL_ANIMATED_IMAGE_VISUAL_H */