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/devel-api/adaptor-framework/animated-image-loading.h>
23 #include <dali/public-api/adaptor-framework/timer.h>
24 #include <dali/public-api/common/dali-vector.h>
25 #include <dali/public-api/common/intrusive-ptr.h>
26 #include <dali/public-api/math/vector4.h>
27 #include <dali/public-api/object/weak-handle.h>
30 #include <dali-toolkit/devel-api/visuals/animated-image-visual-actions-devel.h>
31 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
32 #include <dali-toolkit/internal/visuals/animated-image/image-cache.h>
33 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
34 #include <dali-toolkit/internal/visuals/visual-url.h>
42 class ImageVisualShaderFactory;
43 class AnimatedImageVisual;
44 typedef IntrusivePtr<AnimatedImageVisual> AnimatedImageVisualPtr;
47 * The visual which renders an animated image
49 * One of the following properties is mandatory
51 * | %Property Name | Type |
52 * |------------------- |-------------------|
54 * | urls | ARRAY of STRING |
56 * The remaining properties are optional
57 * | pixelArea | VECTOR4 |
58 * | wrapModeU | INTEGER OR STRING |
59 * | wrapModeV | INTEGER OR STRING |
60 * | cacheSize | INTEGER |
61 * | batchSize | INTEGER |
62 * | frameDelay | INTEGER |
64 * pixelArea is a rectangular area.
65 * In its Vector4 value, the first two elements indicate the top-left position of the area,
66 * and the last two elements are the area width and height respectively.
67 * If not specified, the default value is [0.0, 0.0, 1.0, 1.0], i.e. the entire area of the image.
69 * 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.
70 * Its value should be one of the following wrap mode:
76 * cacheSize is used with multiple images - it determines how many images are kept pre-loaded
77 * batchSize is used with multiple images - it determines how many images to load on each frame
78 * frameDelay is used with multiple images - it is the number of milliseconds between each frame
81 class AnimatedImageVisual : public Visual::Base,
82 public ConnectionTracker,
83 public ImageCache::FrameReadyObserver
87 * @brief Create the animated image Visual using the image URL.
89 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
90 * @param[in] shaderFactory The ImageVisualShaderFactory object
91 * @param[in] imageUrl The URL to animated image 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.
95 static AnimatedImageVisualPtr New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties);
98 * @brief Create the animated image Visual using image URLs.
100 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
101 * @param[in] shaderFactory The ImageVisualShaderFactory object
102 * @param[in] imageUrls A Property::Array containing the URLs to the image resources
103 * @param[in] properties A Property::Map containing settings for this visual
104 * @return A smart-pointer to the newly allocated visual.
106 static AnimatedImageVisualPtr New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const Property::Array& imageUrls, const Property::Map& properties);
109 * @brief Create the animated image visual using the image URL.
111 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
112 * @param[in] shaderFactory The ImageVisualShaderFactory object
113 * @param[in] imageUrl The URL to animated image resource to use
115 static AnimatedImageVisualPtr New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl);
117 public: // from Visual
119 * @copydoc Visual::Base::GetNaturalSize
121 void GetNaturalSize(Vector2& naturalSize) override;
124 * @copydoc Visual::Base::CreatePropertyMap
126 void DoCreatePropertyMap(Property::Map& map) const override;
129 * @copydoc Visual::Base::CreateInstancePropertyMap
131 void DoCreateInstancePropertyMap(Property::Map& map) const override;
134 * @copydoc Visual::Base::OnDoAction
136 void OnDoAction(const Dali::Property::Index actionName, const Dali::Property::Value& attributes) override;
140 * @brief Constructor.
142 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
143 * @param[in] shaderFactory The ImageVisualShaderFactory object
145 AnimatedImageVisual(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory);
148 * @brief A reference counted object may only be deleted by calling Unreference().
150 ~AnimatedImageVisual() override;
153 * @copydoc Visual::Base::OnInitialize
155 void OnInitialize() override;
158 * @copydoc Visual::Base::DoSetProperties
160 void DoSetProperties(const Property::Map& propertyMap) override;
163 * Helper method to set individual values by index key.
164 * @param[in] index The index key of the value
165 * @param[in] value The value
167 void DoSetProperty(Property::Index index, const Property::Value& value);
170 * @copydoc Visual::Base::DoSetOnScene
172 void DoSetOnScene(Actor& actor) override;
175 * @copydoc Visual::Base::DoSetOffScene
177 void DoSetOffScene(Actor& actor) override;
180 * @copydoc Visual::Base::OnSetTransform
182 void OnSetTransform() override;
185 * @copydoc Visual::Base::UpdateShader
187 void UpdateShader() override;
190 * @copydoc Visual::Base::GenerateShader
192 Shader GenerateShader() const override;
196 * Creates the renderer for the animated image
198 void CreateRenderer();
201 * Starts the Load of the first batch of URLs
203 void LoadFirstBatch();
206 * Adds the texture set to the renderer, and the renderer to the
207 * placement actor, and starts the frame timer
208 * @param[in] textureSet The texture set to apply
210 void StartFirstFrame(TextureSet& textureSet);
213 * Prepares the texture set for displaying
215 TextureSet PrepareTextureSet();
218 * Set the image size from the texture set
219 * @param[in] textureSet The texture set to get the size from
221 void SetImageSize(TextureSet& textureSet);
224 * Called when the next frame is ready.
225 * @param[in] textureSet the texture set to apply
227 void FrameReady(TextureSet textureSet) override;
230 * Display the next frame. It is called when the mFrameDelayTimer ticks.
231 * Returns true to ensure the timer continues running.
233 bool DisplayNextFrame();
236 * Initialize the animated image variables.
237 * @param[in] imageUrl The url of the animated image
239 void InitializeAnimatedImage(const VisualUrl& imageUrl);
242 * Set the state of loading fail of an image or a frame.
243 * Returns TextureSet of broken image.
245 TextureSet SetLoadingFailed();
248 AnimatedImageVisual(const AnimatedImageVisual& animatedImageVisual);
251 AnimatedImageVisual& operator=(const AnimatedImageVisual& animatedImageVisual);
254 Timer mFrameDelayTimer;
255 WeakHandle<Actor> mPlacementActor;
256 ImageVisualShaderFactory& mImageVisualShaderFactory;
258 // Variables for Animated Image player
261 Dali::AnimatedImageLoading mAnimatedImageLoading; // Only needed for animated image
262 uint32_t mFrameIndexForJumpTo; // Frame index into textureRects
264 // Variables for Multi-Image player
265 ImageCache::UrlList* mImageUrls;
266 ImageCache* mImageCache;
269 uint16_t mFrameDelay;
271 int16_t mCurrentLoopIndex;
275 uint32_t mFrameCount; // Number of frames
276 ImageDimensions mImageSize;
278 Dali::WrapMode::Type mWrapModeU : 3;
279 Dali::WrapMode::Type mWrapModeV : 3;
280 DevelAnimatedImageVisual::Action::Type mActionStatus : 3;
281 DevelImageVisual::StopBehavior::Type mStopBehavior : 2;
282 bool mStartFirstFrame : 1;
286 } // namespace Internal
288 } // namespace Toolkit
291 #endif /* DALI_TOOLKIT_INTERNAL_ANIMATED_IMAGE_VISUAL_H */