1 #ifndef DALI_TOOLKIT_INTERNAL_ANIMATED_IMAGE_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_ANIMATED_IMAGE_VISUAL_H
5 * Copyright (c) 2018 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/gif-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>
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
91 * @brief Create the animated image Visual using the image URL.
93 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
94 * @param[in] shaderFactory The ImageVisualShaderFactory object
95 * @param[in] imageUrl The URL to gif resource to use
96 * @param[in] properties A Property::Map containing settings for this visual
97 * @return A smart-pointer to the newly allocated visual.
99 static AnimatedImageVisualPtr New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties );
102 * @brief Create the animated image Visual using image URLs.
104 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
105 * @param[in] shaderFactory The ImageVisualShaderFactory object
106 * @param[in] imageUrls A Property::Array containing the URLs to the image resources
107 * @param[in] properties A Property::Map containing settings for this visual
108 * @return A smart-pointer to the newly allocated visual.
110 static AnimatedImageVisualPtr New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const Property::Array& imageUrls, const Property::Map& properties );
113 * @brief Create the animated image visual using the image URL.
115 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
116 * @param[in] shaderFactory The ImageVisualShaderFactory object
117 * @param[in] imageUrl The URL to animated image resource to use
119 static AnimatedImageVisualPtr New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl );
121 public: // from Visual
124 * @copydoc Visual::Base::GetNaturalSize
126 virtual void GetNaturalSize( Vector2& naturalSize );
129 * @copydoc Visual::Base::CreatePropertyMap
131 virtual void DoCreatePropertyMap( Property::Map& map ) const;
134 * @copydoc Visual::Base::CreateInstancePropertyMap
136 virtual void DoCreateInstancePropertyMap( Property::Map& map ) const;
139 * @copydoc Visual::Base::OnDoAction
141 virtual void OnDoAction( const Dali::Property::Index actionName, const Dali::Property::Value& attributes ) override;
146 * @brief Constructor.
148 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
149 * @param[in] shaderFactory The ImageVisualShaderFactory object
151 AnimatedImageVisual( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory );
154 * @brief A reference counted object may only be deleted by calling Unreference().
156 virtual ~AnimatedImageVisual();
159 * @copydoc Visual::Base::DoSetProperties
161 virtual void DoSetProperties( const Property::Map& propertyMap );
164 * Helper method to set individual values by index key.
165 * @param[in] index The index key of the value
166 * @param[in] value The value
168 void DoSetProperty( Property::Index index, const Property::Value& value );
171 * @copydoc Visual::Base::DoSetOnStage
173 virtual void DoSetOnStage( Actor& actor );
176 * @copydoc Visual::Base::DoSetOffStage
178 virtual void DoSetOffStage( Actor& actor );
181 * @copydoc Visual::Base::OnSetTransform
183 virtual void OnSetTransform();
187 * Creates the renderer for the animated image
189 void CreateRenderer();
192 * Starts the Load of the first batch of URLs
194 void LoadFirstBatch();
197 * Adds the texture set to the renderer, and the renderer to the
198 * placement actor, and starts the frame timer
199 * @param[in] textureSet The texture set to apply
201 void StartFirstFrame( TextureSet& textureSet );
204 * Prepares the texture set for displaying
206 TextureSet PrepareTextureSet();
209 * Set the image size from the texture set
210 * @param[in] textureSet The texture set to get the size from
212 void SetImageSize( TextureSet& textureSet );
215 * Called when the next frame is ready.
216 * @param[in] textureSet the texture set to apply
218 void FrameReady( TextureSet textureSet );
221 * Display the next frame. It is called when the mFrameDelayTimer ticks.
222 * Returns true to ensure the timer continues running.
224 bool DisplayNextFrame();
227 * Initialize the gif variables.
228 * @param[in] imageUrl The url of the animated gif
230 void InitializeGif( const VisualUrl& imageUrl );
233 AnimatedImageVisual( const AnimatedImageVisual& animatedImageVisual );
236 AnimatedImageVisual& operator=( const AnimatedImageVisual& animatedImageVisual );
240 Timer mFrameDelayTimer;
241 WeakHandle<Actor> mPlacementActor;
242 ImageVisualShaderFactory& mImageVisualShaderFactory;
244 // Variables for GIF player
245 Dali::Vector<uint32_t> mFrameDelayContainer;
248 std::unique_ptr<Dali::GifLoading> mGifLoading; // Only needed for animated gifs
249 uint32_t mCurrentFrameIndex; // Frame index into textureRects
251 // Variables for Multi-Image player
252 ImageCache::UrlList* mImageUrls;
253 ImageCache* mImageCache;
256 uint16_t mFrameDelay;
258 int16_t mCurrentLoopIndex;
262 uint32_t mFrameCount; // Number of frames
263 ImageDimensions mImageSize;
265 Dali::WrapMode::Type mWrapModeU:3;
266 Dali::WrapMode::Type mWrapModeV:3;
267 DevelAnimatedImageVisual::Action::Type mActionStatus:3;
268 bool mStartFirstFrame:1;
271 } // namespace Internal
273 } // namespace Toolkit
276 #endif /* DALI_TOOLKIT_INTERNAL_ANIMATED_IMAGE_VISUAL_H */