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>
29 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
30 #include <dali-toolkit/internal/visuals/visual-url.h>
31 #include <dali-toolkit/internal/visuals/animated-image/image-cache.h>
42 class AnimatedImageVisual;
43 typedef IntrusivePtr< AnimatedImageVisual > AnimatedImageVisualPtr;
46 * The visual which renders an animated image
48 * One of the following properties is mandatory
50 * | %Property Name | Type |
51 * |------------------- |-------------------|
53 * | urls | ARRAY of STRING |
55 * The remaining properties are optional
56 * | pixelArea | VECTOR4 |
57 * | wrapModeU | INTEGER OR STRING |
58 * | wrapModeV | INTEGER OR STRING |
59 * | cacheSize | INTEGER |
60 * | batchSize | INTEGER |
61 * | frameDelay | INTEGER |
63 * pixelArea is a rectangular area.
64 * In its Vector4 value, the first two elements indicate the top-left position of the area,
65 * and the last two elements are the area width and height respectively.
66 * If not specified, the default value is [0.0, 0.0, 1.0, 1.0], i.e. the entire area of the image.
68 * 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.
69 * Its value should be one of the following wrap mode:
75 * cacheSize is used with multiple images - it determines how many images are kept pre-loaded
76 * batchSize is used with multiple images - it determines how many images to load on each frame
77 * frameDelay is used with multiple images - it is the number of milliseconds between each frame
80 class AnimatedImageVisual : public Visual::Base,
81 public ConnectionTracker,
82 public ImageCache::FrameReadyObserver
88 * @brief Create the animated image Visual using the image URL.
90 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
91 * @param[in] imageUrl The URL to gif 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, 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] imageUrls A Property::Array containing the URLs to the image resources
102 * @param[in] properties A Property::Map containing settings for this visual
103 * @return A smart-pointer to the newly allocated visual.
105 static AnimatedImageVisualPtr New( VisualFactoryCache& factoryCache, const Property::Array& imageUrls, const Property::Map& properties );
108 * @brief Create the animated image visual using the image URL.
110 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
111 * @param[in] imageUrl The URL to animated image resource to use
113 static AnimatedImageVisualPtr New( VisualFactoryCache& factoryCache, const VisualUrl& imageUrl );
115 public: // from Visual
118 * @copydoc Visual::Base::GetNaturalSize
120 virtual void GetNaturalSize( Vector2& naturalSize );
123 * @copydoc Visual::Base::CreatePropertyMap
125 virtual void DoCreatePropertyMap( Property::Map& map ) const;
128 * @copydoc Visual::Base::CreateInstancePropertyMap
130 virtual void DoCreateInstancePropertyMap( Property::Map& map ) const;
135 * @brief Constructor.
137 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
139 AnimatedImageVisual( VisualFactoryCache& factoryCache );
142 * @brief A reference counted object may only be deleted by calling Unreference().
144 virtual ~AnimatedImageVisual();
147 * @copydoc Visual::Base::DoSetProperties
149 virtual void DoSetProperties( const Property::Map& propertyMap );
152 * Helper method to set individual values by index key.
153 * @param[in] index The index key of the value
154 * @param[in] value The value
156 void DoSetProperty( Property::Index index, const Property::Value& value );
159 * @copydoc Visual::Base::DoSetOnStage
161 virtual void DoSetOnStage( Actor& actor );
164 * @copydoc Visual::Base::DoSetOffStage
166 virtual void DoSetOffStage( Actor& actor );
169 * @copydoc Visual::Base::OnSetTransform
171 virtual void OnSetTransform();
175 * Creates the renderer for the animated image
177 void CreateRenderer();
180 * Starts the Load of the first batch of URLs
182 void LoadFirstBatch();
185 * Adds the texture set to the renderer, and the renderer to the
186 * placement actor, and starts the frame timer
188 void StartFirstFrame( TextureSet& textureSet );
191 * Prepares the texture set for displaying
193 TextureSet PrepareTextureSet();
196 * Load the gif image and pack the frames into atlas.
197 * @return The atlas texture.
199 TextureSet PrepareAnimatedGifImage();
202 * Set the image size from the texture set
204 void SetImageSize( TextureSet& textureSet );
207 * Called when the next frame is ready.
209 void FrameReady( TextureSet textureSet );
212 * Display the next frame. It is called when the mFrameDelayTimer ticks.
213 * Returns true to ensure the timer continues running.
215 bool DisplayNextFrame();
219 AnimatedImageVisual( const AnimatedImageVisual& animatedImageVisual );
222 AnimatedImageVisual& operator=( const AnimatedImageVisual& animatedImageVisual );
226 Timer mFrameDelayTimer;
227 WeakHandle<Actor> mPlacementActor;
229 // Variables for GIF player
230 Dali::Vector<Vector4> mTextureRectContainer;
231 Dali::Vector<uint32_t> mFrameDelayContainer;
234 uint32_t mCurrentFrameIndex; // Frame index into textureRects
236 // Variables for Multi-Image player
237 ImageCache::UrlList* mImageUrls;
238 ImageCache* mImageCache;
241 uint16_t mFrameDelay;
245 ImageDimensions mImageSize;
247 Dali::WrapMode::Type mWrapModeU:3;
248 Dali::WrapMode::Type mWrapModeV:3;
249 bool mStartFirstFrame:1;
252 } // namespace Internal
254 } // namespace Toolkit
257 #endif /* DALI_TOOLKIT_INTERNAL_ANIMATED_IMAGE_VISUAL_H */