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 AnimatedImageVisual;
45 typedef IntrusivePtr< AnimatedImageVisual > AnimatedImageVisualPtr;
48 * The visual which renders an animated image
50 * One of the following properties is mandatory
52 * | %Property Name | Type |
53 * |------------------- |-------------------|
55 * | urls | ARRAY of STRING |
57 * The remaining properties are optional
58 * | pixelArea | VECTOR4 |
59 * | wrapModeU | INTEGER OR STRING |
60 * | wrapModeV | INTEGER OR STRING |
61 * | cacheSize | INTEGER |
62 * | batchSize | INTEGER |
63 * | frameDelay | INTEGER |
65 * pixelArea is a rectangular area.
66 * In its Vector4 value, the first two elements indicate the top-left position of the area,
67 * and the last two elements are the area width and height respectively.
68 * If not specified, the default value is [0.0, 0.0, 1.0, 1.0], i.e. the entire area of the image.
70 * 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.
71 * Its value should be one of the following wrap mode:
77 * cacheSize is used with multiple images - it determines how many images are kept pre-loaded
78 * batchSize is used with multiple images - it determines how many images to load on each frame
79 * frameDelay is used with multiple images - it is the number of milliseconds between each frame
82 class AnimatedImageVisual : public Visual::Base,
83 public ConnectionTracker,
84 public ImageCache::FrameReadyObserver
90 * @brief Create the animated image Visual using the image URL.
92 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
93 * @param[in] imageUrl The URL to gif 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, 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] imageUrls A Property::Array containing the URLs to the image resources
104 * @param[in] properties A Property::Map containing settings for this visual
105 * @return A smart-pointer to the newly allocated visual.
107 static AnimatedImageVisualPtr New( VisualFactoryCache& factoryCache, const Property::Array& imageUrls, const Property::Map& properties );
110 * @brief Create the animated image visual using the image URL.
112 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
113 * @param[in] imageUrl The URL to animated image resource to use
115 static AnimatedImageVisualPtr New( VisualFactoryCache& factoryCache, const VisualUrl& imageUrl );
117 public: // from Visual
120 * @copydoc Visual::Base::GetNaturalSize
122 virtual void GetNaturalSize( Vector2& naturalSize );
125 * @copydoc Visual::Base::CreatePropertyMap
127 virtual void DoCreatePropertyMap( Property::Map& map ) const;
130 * @copydoc Visual::Base::CreateInstancePropertyMap
132 virtual void DoCreateInstancePropertyMap( Property::Map& map ) const;
135 * @copydoc Visual::Base::OnDoAction
137 virtual void OnDoAction( const Dali::Property::Index actionName, const Dali::Property::Value& attributes ) override;
142 * @brief Constructor.
144 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
146 AnimatedImageVisual( VisualFactoryCache& factoryCache );
149 * @brief A reference counted object may only be deleted by calling Unreference().
151 virtual ~AnimatedImageVisual();
154 * @copydoc Visual::Base::DoSetProperties
156 virtual void DoSetProperties( const Property::Map& propertyMap );
159 * Helper method to set individual values by index key.
160 * @param[in] index The index key of the value
161 * @param[in] value The value
163 void DoSetProperty( Property::Index index, const Property::Value& value );
166 * @copydoc Visual::Base::DoSetOnStage
168 virtual void DoSetOnStage( Actor& actor );
171 * @copydoc Visual::Base::DoSetOffStage
173 virtual void DoSetOffStage( Actor& actor );
176 * @copydoc Visual::Base::OnSetTransform
178 virtual void OnSetTransform();
182 * Creates the renderer for the animated image
184 void CreateRenderer();
187 * Starts the Load of the first batch of URLs
189 void LoadFirstBatch();
192 * Adds the texture set to the renderer, and the renderer to the
193 * placement actor, and starts the frame timer
194 * @param[in] textureSet The texture set to apply
196 void StartFirstFrame( TextureSet& textureSet );
199 * Prepares the texture set for displaying
201 TextureSet PrepareTextureSet();
204 * Set the image size from the texture set
205 * @param[in] textureSet The texture set to get the size from
207 void SetImageSize( TextureSet& textureSet );
210 * Called when the next frame is ready.
211 * @param[in] textureSet the texture set to apply
213 void FrameReady( TextureSet textureSet );
216 * Display the next frame. It is called when the mFrameDelayTimer ticks.
217 * Returns true to ensure the timer continues running.
219 bool DisplayNextFrame();
222 * Initialize the gif variables.
223 * @param[in] imageUrl The url of the animated gif
225 void InitializeGif( const VisualUrl& imageUrl );
228 AnimatedImageVisual( const AnimatedImageVisual& animatedImageVisual );
231 AnimatedImageVisual& operator=( const AnimatedImageVisual& animatedImageVisual );
235 Timer mFrameDelayTimer;
236 WeakHandle<Actor> mPlacementActor;
238 // Variables for GIF player
239 Dali::Vector<uint32_t> mFrameDelayContainer;
242 std::unique_ptr<Dali::GifLoading> mGifLoading; // Only needed for animated gifs
243 uint32_t mCurrentFrameIndex; // Frame index into textureRects
245 // Variables for Multi-Image player
246 ImageCache::UrlList* mImageUrls;
247 ImageCache* mImageCache;
250 uint16_t mFrameDelay;
252 int16_t mCurrentLoopIndex;
256 uint32_t mFrameCount; // Number of frames
257 ImageDimensions mImageSize;
259 Dali::WrapMode::Type mWrapModeU:3;
260 Dali::WrapMode::Type mWrapModeV:3;
261 DevelAnimatedImageVisual::Action::Type mActionStatus:2;
262 bool mStartFirstFrame:1;
265 } // namespace Internal
267 } // namespace Toolkit
270 #endif /* DALI_TOOLKIT_INTERNAL_ANIMATED_IMAGE_VISUAL_H */