1 #ifndef DALI_TOOLKIT_VECTOR_ANIMATION_TASK_H
2 #define DALI_TOOLKIT_VECTOR_ANIMATION_TASK_H
5 * Copyright (c) 2019 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.
21 #include <dali/public-api/object/property-array.h>
22 #include <dali/devel-api/adaptor-framework/event-thread-callback.h>
23 #include <dali/devel-api/adaptor-framework/vector-animation-renderer.h>
24 #include <dali/devel-api/threading/conditional-wait.h>
29 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
40 class VisualFactoryCache;
41 class VectorAnimationThread;
42 class VectorAnimationTask;
43 typedef IntrusivePtr< VectorAnimationTask > VectorAnimationTaskPtr;
46 * The task of the vector animation.
48 class VectorAnimationTask : public RefObject
52 using UploadCompletedSignalType = Dali::VectorAnimationRenderer::UploadCompletedSignalType;
55 * Flags for re-sending data to the vector animation thread
59 RESEND_PLAY_RANGE = 1 << 0,
60 RESEND_LOOP_COUNT = 1 << 1,
61 RESEND_STOP_BEHAVIOR = 1 << 2,
62 RESEND_LOOPING_MODE = 1 << 3,
63 RESEND_CURRENT_FRAME = 1 << 4,
65 RESEND_PLAY_STATE = 1 << 6
69 * @brief Structure used to pass parameters to the vector animation task
77 stopBehavior( DevelImageVisual::StopBehavior::CURRENT_FRAME ),
78 loopingMode( DevelImageVisual::LoopingMode::RESTART ),
87 Property::Array playRange;
88 DevelImageVisual::PlayState::Type playState;
89 DevelImageVisual::StopBehavior::Type stopBehavior;
90 DevelImageVisual::LoopingMode::Type loopingMode;
91 uint32_t currentFrame;
100 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
101 * @param[in] url The url of the vector animation file
103 VectorAnimationTask( VisualFactoryCache& factoryCache, const std::string& url );
108 virtual ~VectorAnimationTask();
111 * @brief Finalizes the task.
116 * @brief Sets the renderer used to display the result image.
118 * @param[in] renderer The renderer used to display the result image
120 void SetRenderer( Renderer renderer );
123 * @brief Sets data to specify animation playback.
124 * @param[in] data The animation data
126 void SetAnimationData( const AnimationData& data );
129 * @brief This callback is called after the animation is finished.
130 * @param[in] callback The animation finished callback
132 void SetAnimationFinishedCallback( EventThreadCallback* callback );
135 * @brief Gets the playing range in frame number.
136 * @param[out] startFrame The frame number to specify minimum progress.
137 * @param[out] endFrame The frame number to specify maximum progress.
139 void GetPlayRange( uint32_t& startFrame, uint32_t& endFrame );
142 * @brief Get the play state
143 * @return The play state
145 DevelImageVisual::PlayState::Type GetPlayState() const;
148 * @brief Retrieves the current frame number of the animation.
149 * @return The current frame number
151 uint32_t GetCurrentFrameNumber() const;
154 * @brief Retrieves the total frame number of the animation.
155 * @return The total frame number
157 uint32_t GetTotalFrameNumber() const;
160 * @brief Gets the default size of the file,.
161 * @return The default size of the file
163 void GetDefaultSize( uint32_t& width, uint32_t& height ) const;
166 * @brief Gets the layer information of all the child layers.
167 * @param[out] map The layer information
169 void GetLayerInfo( Property::Map& map ) const;
172 * @brief Connect to this signal to be notified when the texture upload is completed.
173 * @return The signal to connect to.
175 UploadCompletedSignalType& UploadCompletedSignal();
178 * @brief Rasterizes the current frame.
179 * @return true if the animation is running, false otherwise.
184 * @brief Calculates the time for the next frame rasterization.
185 * @return The time for the next frame rasterization.
187 std::chrono::time_point< std::chrono::system_clock > CalculateNextFrameTime( bool renderNow );
190 * @brief Gets the time for the next frame rasterization.
191 * @return The time for the next frame rasterization.
193 std::chrono::time_point< std::chrono::system_clock > GetNextFrameTime();
198 * @brief Initializes the vector renderer.
203 * @brief Play the vector animation.
205 void PlayAnimation();
208 * @brief Stop the vector animation.
210 void StopAnimation();
213 * @brief Pause the vector animation.
215 void PauseAnimation();
218 * @brief Render one frame. The current frame number will be increased.
223 * @brief Sets the target image size.
225 * @param[in] width The target image width
226 * @param[in] height The target image height
228 void SetSize( uint32_t width, uint32_t height );
231 * @brief Enable looping for 'count' repeats. -1 means to repeat forever.
232 * @param[in] count The number of times to loop
234 void SetLoopCount( int32_t count );
237 * @brief Set the playing range in frame number.
238 * @param[in] playRange The array to specify minimum and maximum progress.
239 * The animation will play between those values.
241 void SetPlayRange( const Property::Array& playRange );
244 * @brief Sets the current frame number of the animation.
245 * @param[in] frameNumber The new frame number between [0, the maximum frame number] or between the play range if specified.
247 void SetCurrentFrameNumber( uint32_t frameNumber );
250 * @brief Sets the stop behavior of the animation. This is performed when the animation is stopped.
251 * @param[in] stopBehavior The stop behavior
253 void SetStopBehavior( DevelImageVisual::StopBehavior::Type stopBehavior );
256 * @brief Sets the looping mode.
257 * Animation plays forwards and then restarts from the beginning or runs backwards again.
258 * @param[in] loopingMode The looping mode
260 void SetLoopingMode( DevelImageVisual::LoopingMode::Type loopingMode );
263 * @brief Gets the frame number when the animation is stopped according to the stop behavior.
265 uint32_t GetStoppedFrame( uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame );
268 VectorAnimationTask( const VectorAnimationTask& task ) = delete;
271 VectorAnimationTask& operator=( const VectorAnimationTask& task ) = delete;
277 STOPPING, ///< The animation is stopping
278 STOPPED, ///< The animation has stopped
279 PLAYING, ///< The animation is playing
280 PAUSED ///< The animation is paused
284 VectorAnimationRenderer mVectorRenderer;
285 VectorAnimationThread& mVectorAnimationThread;
286 ConditionalWait mConditionalWait;
287 std::unique_ptr< EventThreadCallback > mAnimationFinishedTrigger;
288 PlayState mPlayState;
289 DevelImageVisual::StopBehavior::Type mStopBehavior;
290 DevelImageVisual::LoopingMode::Type mLoopingMode;
291 std::chrono::time_point< std::chrono::system_clock > mNextFrameStartTime;
292 int64_t mFrameDurationNanoSeconds;
294 uint32_t mCurrentFrame;
295 uint32_t mTotalFrame;
296 uint32_t mStartFrame;
301 int32_t mCurrentLoop;
303 bool mCurrentFrameUpdated;
304 bool mCurrentLoopUpdated;
306 bool mUpdateFrameNumber;
307 bool mNeedAnimationFinishedTrigger;
312 } // namespace Internal
314 } // namespace Toolkit
318 #endif // DALI_TOOLKIT_VECTOR_ANIMATION_TASK_H