1 #ifndef DALI_TOOLKIT_INTERNAL_ANIMATED_IMAGE_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_ANIMATED_IMAGE_VISUAL_H
5 * Copyright (c) 2022 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
114 * @param[in] size The width and height of the image. The visual size will be used if these are 0.
116 static AnimatedImageVisualPtr New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, ImageDimensions size = ImageDimensions());
118 public: // from Visual
120 * @copydoc Visual::Base::GetNaturalSize
122 void GetNaturalSize(Vector2& naturalSize) override;
125 * @copydoc Visual::Base::CreatePropertyMap
127 void DoCreatePropertyMap(Property::Map& map) const override;
130 * @copydoc Visual::Base::CreateInstancePropertyMap
132 void DoCreateInstancePropertyMap(Property::Map& map) const override;
135 * @copydoc Visual::Base::OnDoAction
137 void OnDoAction(const Dali::Property::Index actionId, const Dali::Property::Value& attributes) override;
141 * @brief Constructor.
143 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
144 * @param[in] shaderFactory The ImageVisualShaderFactory object
145 * @param[in] desiredSize The width and height of the image. The visual size will be used if these are 0.
147 AnimatedImageVisual(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, ImageDimensions desiredSize);
150 * @brief A reference counted object may only be deleted by calling Unreference().
152 ~AnimatedImageVisual() override;
155 * @copydoc Visual::Base::OnInitialize
157 void OnInitialize() 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;
187 * @copydoc Visual::Base::UpdateShader
189 void UpdateShader() override;
192 * @copydoc Visual::Base::GenerateShader
194 Shader GenerateShader() const override;
198 * @brief Initialize the animated image variables.
199 * @param[in] imageUrl The url of the animated image
201 void InitializeAnimatedImage(const VisualUrl& imageUrl);
204 * @brief Create image cache for animated image or image array.
206 void CreateImageCache();
209 * @brief Adds the texture set to the renderer, and the renderer to the
210 * placement actor, and starts the frame timer
211 * @param[in] textureSet The texture set to apply
212 * @param[in] firstInterval frame interval(ms) for the first frame.
214 void StartFirstFrame(TextureSet& textureSet, uint32_t firstInterval);
217 * @brief Prepares the texture set for displaying
219 void PrepareTextureSet();
222 * @brief Set the image size from the texture set
223 * @param[in] textureSet The texture set to get the size from
225 void SetImageSize(TextureSet& textureSet);
228 * @brief Called when the next frame is ready.
229 * @param[in] textureSet the texture set to apply
230 * @param[in] interval interval(ms) for the frame
231 * @param[in] preMultiplied whether the texture is premultied alpha or not.
233 void FrameReady(TextureSet textureSet, uint32_t interval, bool preMultiplied) override;
236 * @brief Display the next frame. It is called when the mFrameDelayTimer ticks.
237 * @return true to ensure the timer continues running.
239 bool DisplayNextFrame();
242 * @brief Set the state of loading fail of an image or a frame.
243 * @return TextureSet of broken image.
245 TextureSet SetLoadingFailed();
248 * @brief Allocate mask data.
249 * This is allocated only once.
251 void AllocateMaskData();
254 * @brief Check whether the mask texture is loaded or not.
255 * If MaskingType is MASKING_ON_LOADING and mask texture is failed to load, update shader.
257 void CheckMaskTexture();
260 AnimatedImageVisual(const AnimatedImageVisual& animatedImageVisual);
263 AnimatedImageVisual& operator=(const AnimatedImageVisual& animatedImageVisual);
266 Timer mFrameDelayTimer;
267 WeakHandle<Actor> mPlacementActor;
268 ImageVisualShaderFactory& mImageVisualShaderFactory;
270 // Variables for Animated Image player
273 Dali::AnimatedImageLoading mAnimatedImageLoading; // Only needed for animated image
274 uint32_t mFrameIndexForJumpTo; // Frame index into textureRects
275 uint32_t mCurrentFrameIndex;
277 // Variables for Multi-Image player
278 ImageCache::UrlList* mImageUrls;
279 ImageCache* mImageCache;
282 uint16_t mFrameDelay;
284 int16_t mCurrentLoopIndex;
286 // Variables for image visual properties.
287 Dali::Toolkit::ImageVisual::LoadPolicy::Type mLoadPolicy;
288 Dali::Toolkit::ImageVisual::ReleasePolicy::Type mReleasePolicy;
289 TextureManager::MaskingDataPointer mMaskingData;
290 Dali::ImageDimensions mDesiredSize;
293 uint32_t mFrameCount; // Number of frames
294 ImageDimensions mImageSize;
296 DevelAnimatedImageVisual::Action::Type mActionStatus;
298 Dali::WrapMode::Type mWrapModeU : 3;
299 Dali::WrapMode::Type mWrapModeV : 3;
300 Dali::FittingMode::Type mFittingMode : 3;
301 Dali::SamplingMode::Type mSamplingMode : 4;
302 DevelImageVisual::StopBehavior::Type mStopBehavior : 2;
303 bool mStartFirstFrame : 1;
307 } // namespace Internal
309 } // namespace Toolkit
312 #endif /* DALI_TOOLKIT_INTERNAL_ANIMATED_IMAGE_VISUAL_H */