1 #ifndef DALI_TOOLKIT_INTERNAL_ANIMATED_IMAGE_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_ANIMATED_IMAGE_VISUAL_H
5 * Copyright (c) 2017 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>
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
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] imageUrl The URL to gif resource to use
93 * @param[in] properties A Property::Map containing settings for this visual
94 * @return A smart-pointer to the newly allocated visual.
96 static AnimatedImageVisualPtr New( VisualFactoryCache& factoryCache, const VisualUrl& imageUrl, const Property::Map& properties );
99 * @brief Create the animated image Visual using image URLs.
101 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache 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, 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] imageUrl The URL to animated image resource to use
114 static AnimatedImageVisualPtr New( VisualFactoryCache& factoryCache, const VisualUrl& imageUrl );
116 public: // from Visual
119 * @copydoc Visual::Base::GetNaturalSize
121 virtual void GetNaturalSize( Vector2& naturalSize );
124 * @copydoc Visual::Base::CreatePropertyMap
126 virtual void DoCreatePropertyMap( Property::Map& map ) const;
129 * @copydoc Visual::Base::CreateInstancePropertyMap
131 virtual void DoCreateInstancePropertyMap( Property::Map& map ) const;
136 * @brief Constructor.
138 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
140 AnimatedImageVisual( VisualFactoryCache& factoryCache );
143 * @brief A reference counted object may only be deleted by calling Unreference().
145 virtual ~AnimatedImageVisual();
148 * @copydoc Visual::Base::DoSetProperties
150 virtual void DoSetProperties( const Property::Map& propertyMap );
153 * Helper method to set individual values by index key.
154 * @param[in] index The index key of the value
155 * @param[in] value The value
157 void DoSetProperty( Property::Index index, const Property::Value& value );
160 * @copydoc Visual::Base::DoSetOnStage
162 virtual void DoSetOnStage( Actor& actor );
165 * @copydoc Visual::Base::DoSetOffStage
167 virtual void DoSetOffStage( Actor& actor );
170 * @copydoc Visual::Base::OnSetTransform
172 virtual void OnSetTransform();
176 * Creates the renderer for the animated image
178 void CreateRenderer();
181 * Starts the Load of the first batch of URLs
183 void LoadFirstBatch();
186 * Adds the texture set to the renderer, and the renderer to the
187 * placement actor, and starts the frame timer
188 * @param[in] textureSet The texture set to apply
190 void StartFirstFrame( TextureSet& textureSet );
193 * Prepares the texture set for displaying
195 TextureSet PrepareTextureSet();
198 * Set the image size from the texture set
199 * @param[in] textureSet The texture set to get the size from
201 void SetImageSize( TextureSet& textureSet );
204 * Called when the next frame is ready.
205 * @param[in] textureSet the texture set to apply
207 void FrameReady( TextureSet textureSet );
210 * Display the next frame. It is called when the mFrameDelayTimer ticks.
211 * Returns true to ensure the timer continues running.
213 bool DisplayNextFrame();
216 * Initialize the gif variables.
217 * @param[in] imageUrl The url of the animated gif
219 void InitializeGif( const VisualUrl& imageUrl );
222 AnimatedImageVisual( const AnimatedImageVisual& animatedImageVisual );
225 AnimatedImageVisual& operator=( const AnimatedImageVisual& animatedImageVisual );
229 Timer mFrameDelayTimer;
230 WeakHandle<Actor> mPlacementActor;
232 // Variables for GIF player
233 Dali::Vector<uint32_t> mFrameDelayContainer;
236 std::unique_ptr<Dali::GifLoading> mGifLoading; // Only needed for animated gifs
237 uint32_t mCurrentFrameIndex; // Frame index into textureRects
239 // Variables for Multi-Image player
240 ImageCache::UrlList* mImageUrls;
241 ImageCache* mImageCache;
244 uint16_t mFrameDelay;
248 uint32_t mFrameCount; // Number of frames
249 ImageDimensions mImageSize;
251 Dali::WrapMode::Type mWrapModeU:3;
252 Dali::WrapMode::Type mWrapModeV:3;
253 bool mStartFirstFrame:1;
256 } // namespace Internal
258 } // namespace Toolkit
261 #endif /* DALI_TOOLKIT_INTERNAL_ANIMATED_IMAGE_VISUAL_H */