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/actors/actor-devel.h>
23 #include <dali/public-api/adaptor-framework/window.h>
24 #include <dali/devel-api/adaptor-framework/animated-image-loading.h>
25 #include <dali/public-api/adaptor-framework/timer.h>
26 #include <dali/public-api/common/dali-vector.h>
27 #include <dali/public-api/common/intrusive-ptr.h>
28 #include <dali/public-api/math/vector4.h>
29 #include <dali/public-api/object/weak-handle.h>
32 #include <dali-toolkit/devel-api/visuals/animated-image-visual-actions-devel.h>
33 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
34 #include <dali-toolkit/internal/visuals/animated-image/image-cache.h>
35 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
36 #include <dali-toolkit/internal/visuals/visual-url.h>
44 class ImageVisualShaderFactory;
45 class AnimatedImageVisual;
46 typedef IntrusivePtr<AnimatedImageVisual> AnimatedImageVisualPtr;
49 * The visual which renders an animated image
51 * One of the following properties is mandatory
53 * | %Property Name | Type |
54 * |------------------- |-------------------|
56 * | urls | ARRAY of STRING |
58 * The remaining properties are optional
59 * | pixelArea | VECTOR4 |
60 * | wrapModeU | INTEGER OR STRING |
61 * | wrapModeV | INTEGER OR STRING |
62 * | cacheSize | INTEGER |
63 * | batchSize | INTEGER |
64 * | frameDelay | INTEGER |
66 * pixelArea is a rectangular area.
67 * In its Vector4 value, the first two elements indicate the top-left position of the area,
68 * and the last two elements are the area width and height respectively.
69 * If not specified, the default value is [0.0, 0.0, 1.0, 1.0], i.e. the entire area of the image.
71 * 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.
72 * Its value should be one of the following wrap mode:
78 * cacheSize is used with multiple images - it determines how many images are kept pre-loaded
79 * batchSize is used with multiple images - it determines how many images to load on each frame
80 * frameDelay is used with multiple images - it is the number of milliseconds between each frame
83 class AnimatedImageVisual : public Visual::Base,
84 public ConnectionTracker,
85 public ImageCache::FrameReadyObserver
89 * @brief Create the animated image Visual using the image URL.
91 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
92 * @param[in] shaderFactory The ImageVisualShaderFactory object
93 * @param[in] imageUrl The URL to animated image resource to use
94 * @param[in] properties A Property::Map containing settings for this visual
95 * @return A smart-pointer to the newly allocated visual.
97 static AnimatedImageVisualPtr New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties);
100 * @brief Create the animated image Visual using image URLs.
102 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
103 * @param[in] shaderFactory The ImageVisualShaderFactory object
104 * @param[in] imageUrls A Property::Array containing the URLs to the image resources
105 * @param[in] properties A Property::Map containing settings for this visual
106 * @return A smart-pointer to the newly allocated visual.
108 static AnimatedImageVisualPtr New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const Property::Array& imageUrls, const Property::Map& properties);
111 * @brief Create the animated image visual using the image URL.
113 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
114 * @param[in] shaderFactory The ImageVisualShaderFactory object
115 * @param[in] imageUrl The URL to animated image resource to use
116 * @param[in] size The width and height of the image. The visual size will be used if these are 0.
118 static AnimatedImageVisualPtr New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, ImageDimensions size = ImageDimensions());
120 public: // from Visual
122 * @copydoc Visual::Base::GetNaturalSize
124 void GetNaturalSize(Vector2& naturalSize) override;
127 * @copydoc Visual::Base::CreatePropertyMap
129 void DoCreatePropertyMap(Property::Map& map) const override;
132 * @copydoc Visual::Base::CreateInstancePropertyMap
134 void DoCreateInstancePropertyMap(Property::Map& map) const override;
137 * @copydoc Visual::Base::OnDoAction
139 void OnDoAction(const Dali::Property::Index actionId, const Dali::Property::Value& attributes) override;
143 * @brief Constructor.
145 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
146 * @param[in] shaderFactory The ImageVisualShaderFactory object
147 * @param[in] desiredSize The width and height of the image. The visual size will be used if these are 0.
149 AnimatedImageVisual(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, ImageDimensions desiredSize);
152 * @brief A reference counted object may only be deleted by calling Unreference().
154 ~AnimatedImageVisual() override;
157 * @copydoc Visual::Base::OnInitialize
159 void OnInitialize() override;
162 * @copydoc Visual::Base::DoSetProperties
164 void DoSetProperties(const Property::Map& propertyMap) override;
167 * Helper method to set individual values by index key.
168 * @param[in] index The index key of the value
169 * @param[in] value The value
171 void DoSetProperty(Property::Index index, const Property::Value& value);
174 * @copydoc Visual::Base::DoSetOnScene
176 void DoSetOnScene(Actor& actor) override;
179 * @copydoc Visual::Base::DoSetOffScene
181 void DoSetOffScene(Actor& actor) override;
184 * @copydoc Visual::Base::OnSetTransform
186 void OnSetTransform() override;
189 * @copydoc Visual::Base::UpdateShader
191 void UpdateShader() override;
194 * @copydoc Visual::Base::GenerateShader
196 Shader GenerateShader() const override;
200 * @brief Initialize the animated image variables.
201 * @param[in] imageUrl The url of the animated image
203 void InitializeAnimatedImage(const VisualUrl& imageUrl);
206 * @brief Create image cache for animated image or image array.
208 void CreateImageCache();
211 * @brief Adds the texture set to the renderer, and the renderer to the
212 * placement actor, and starts the frame timer
213 * @param[in] textureSet The texture set to apply
214 * @param[in] firstInterval frame interval(ms) for the first frame.
216 void StartFirstFrame(TextureSet& textureSet, uint32_t firstInterval);
219 * @brief Prepares the texture set for displaying
221 void PrepareTextureSet();
224 * @brief Set the image size from the texture set
225 * @param[in] textureSet The texture set to get the size from
227 void SetImageSize(TextureSet& textureSet);
230 * @brief Called when the next frame is ready.
231 * @param[in] textureSet the texture set to apply
232 * @param[in] interval interval(ms) for the frame
233 * @param[in] preMultiplied whether the texture is premultied alpha or not.
235 void FrameReady(TextureSet textureSet, uint32_t interval, bool preMultiplied) override;
238 * @brief Display the next frame. It is called when the mFrameDelayTimer ticks.
239 * @return true to ensure the timer continues running.
241 bool DisplayNextFrame();
244 * @brief Set the state of loading fail of an image or a frame.
245 * @return TextureSet of broken image.
247 TextureSet SetLoadingFailed();
250 * @brief Allocate mask data.
251 * This is allocated only once.
253 void AllocateMaskData();
256 * @brief Check whether the mask texture is loaded or not.
257 * If MaskingType is MASKING_ON_LOADING and mask texture is failed to load, update shader.
259 void CheckMaskTexture();
262 * @brief Callback when the visibility of the actor is changed.
264 void OnControlVisibilityChanged(Actor actor, bool visible, DevelActor::VisibilityChange::Type type);
267 * @brief Callback when the visibility of the window is changed.
269 void OnWindowVisibilityChanged(Window window, bool visible);
272 AnimatedImageVisual(const AnimatedImageVisual& animatedImageVisual);
275 AnimatedImageVisual& operator=(const AnimatedImageVisual& animatedImageVisual);
278 Timer mFrameDelayTimer;
279 WeakHandle<Actor> mPlacementActor;
280 ImageVisualShaderFactory& mImageVisualShaderFactory;
282 // Variables for Animated Image player
285 Dali::AnimatedImageLoading mAnimatedImageLoading; // Only needed for animated image
286 uint32_t mFrameIndexForJumpTo; // Frame index into textureRects
287 uint32_t mCurrentFrameIndex;
289 // Variables for Multi-Image player
290 ImageCache::UrlList* mImageUrls;
291 ImageCache* mImageCache;
294 uint16_t mFrameDelay;
296 int16_t mCurrentLoopIndex;
298 // Variables for image visual properties.
299 Dali::Toolkit::ImageVisual::LoadPolicy::Type mLoadPolicy;
300 Dali::Toolkit::ImageVisual::ReleasePolicy::Type mReleasePolicy;
301 TextureManager::MaskingDataPointer mMaskingData;
302 Dali::ImageDimensions mDesiredSize;
305 uint32_t mFrameCount; // Number of frames
306 ImageDimensions mImageSize;
308 DevelAnimatedImageVisual::Action::Type mActionStatus;
310 Dali::WrapMode::Type mWrapModeU : 3;
311 Dali::WrapMode::Type mWrapModeV : 3;
312 Dali::FittingMode::Type mFittingMode : 3;
313 Dali::SamplingMode::Type mSamplingMode : 4;
314 DevelImageVisual::StopBehavior::Type mStopBehavior : 2;
315 bool mStartFirstFrame : 1;
319 } // namespace Internal
321 } // namespace Toolkit
324 #endif /* DALI_TOOLKIT_INTERNAL_ANIMATED_IMAGE_VISUAL_H */