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;
186 * Creates the renderer for the animated image
188 void CreateRenderer();
191 * Starts the Load of the first batch of URLs
193 void LoadFirstBatch();
196 * Adds the texture set to the renderer, and the renderer to the
197 * placement actor, and starts the frame timer
198 * @param[in] textureSet The texture set to apply
200 void StartFirstFrame(TextureSet& textureSet);
203 * Prepares the texture set for displaying
205 TextureSet PrepareTextureSet();
208 * Set the image size from the texture set
209 * @param[in] textureSet The texture set to get the size from
211 void SetImageSize(TextureSet& textureSet);
214 * Called when the next frame is ready.
215 * @param[in] textureSet the texture set to apply
217 void FrameReady(TextureSet textureSet) override;
220 * Display the next frame. It is called when the mFrameDelayTimer ticks.
221 * Returns true to ensure the timer continues running.
223 bool DisplayNextFrame();
226 * Initialize the animated image variables.
227 * @param[in] imageUrl The url of the animated image
229 void InitializeAnimatedImage(const VisualUrl& imageUrl);
232 * Set the state of loading fail of an image or a frame.
233 * Returns TextureSet of broken image.
235 TextureSet SetLoadingFailed();
238 AnimatedImageVisual(const AnimatedImageVisual& animatedImageVisual);
241 AnimatedImageVisual& operator=(const AnimatedImageVisual& animatedImageVisual);
244 Timer mFrameDelayTimer;
245 WeakHandle<Actor> mPlacementActor;
246 ImageVisualShaderFactory& mImageVisualShaderFactory;
248 // Variables for Animated Image player
251 Dali::AnimatedImageLoading mAnimatedImageLoading; // Only needed for animated image
252 uint32_t mFrameIndexForJumpTo; // Frame index into textureRects
254 // Variables for Multi-Image player
255 ImageCache::UrlList* mImageUrls;
256 ImageCache* mImageCache;
259 uint16_t mFrameDelay;
261 int16_t mCurrentLoopIndex;
265 uint32_t mFrameCount; // Number of frames
266 ImageDimensions mImageSize;
268 Dali::WrapMode::Type mWrapModeU : 3;
269 Dali::WrapMode::Type mWrapModeV : 3;
270 DevelAnimatedImageVisual::Action::Type mActionStatus : 3;
271 DevelImageVisual::StopBehavior::Type mStopBehavior : 2;
272 bool mStartFirstFrame : 1;
276 } // namespace Internal
278 } // namespace Toolkit
281 #endif /* DALI_TOOLKIT_INTERNAL_ANIMATED_IMAGE_VISUAL_H */