1 #ifndef DALI_TOOLKIT_VECTOR_ANIMATION_TASK_H
2 #define DALI_TOOLKIT_VECTOR_ANIMATION_TASK_H
5 * Copyright (c) 2020 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 ),
86 AnimationData& operator= ( const AnimationData& rhs )
88 resendFlag |= rhs.resendFlag; // OR resend flag
89 playRange = rhs.playRange;
90 playState = rhs.playState;
91 stopBehavior = rhs.stopBehavior;
92 loopingMode = rhs.loopingMode;
93 currentFrame = rhs.currentFrame;
96 loopCount = rhs.loopCount;
101 Property::Array playRange;
102 DevelImageVisual::PlayState::Type playState;
103 DevelImageVisual::StopBehavior::Type stopBehavior;
104 DevelImageVisual::LoopingMode::Type loopingMode;
105 uint32_t currentFrame;
112 * @brief Constructor.
114 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
115 * @param[in] url The url of the vector animation file
117 VectorAnimationTask( VisualFactoryCache& factoryCache, const std::string& url );
122 ~VectorAnimationTask() override;
125 * @brief Finalizes the task.
130 * @brief Sets the renderer used to display the result image.
132 * @param[in] renderer The renderer used to display the result image
134 void SetRenderer( Renderer renderer );
137 * @brief Sets data to specify animation playback.
138 * @param[in] data The animation data
140 void SetAnimationData( const AnimationData& data );
143 * @brief This callback is called after the animation is finished.
144 * @param[in] callback The animation finished callback
146 void SetAnimationFinishedCallback( EventThreadCallback* callback );
149 * @brief Gets the playing range in frame number.
150 * @param[out] startFrame The frame number to specify minimum progress.
151 * @param[out] endFrame The frame number to specify maximum progress.
153 void GetPlayRange( uint32_t& startFrame, uint32_t& endFrame );
156 * @brief Retrieves the current frame number of the animation.
157 * @return The current frame number
159 uint32_t GetCurrentFrameNumber() const;
162 * @brief Retrieves the total frame number of the animation.
163 * @return The total frame number
165 uint32_t GetTotalFrameNumber() const;
168 * @brief Gets the default size of the file,.
169 * @return The default size of the file
171 void GetDefaultSize( uint32_t& width, uint32_t& height ) const;
174 * @brief Gets the layer information of all the child layers.
175 * @param[out] map The layer information
177 void GetLayerInfo( Property::Map& map ) const;
180 * @brief Connect to this signal to be notified when the texture upload is completed.
181 * @return The signal to connect to.
183 UploadCompletedSignalType& UploadCompletedSignal();
186 * @brief Rasterizes the current frame.
187 * @return true if the animation is running, false otherwise.
192 * @brief Calculates the time for the next frame rasterization.
193 * @return The time for the next frame rasterization.
195 std::chrono::time_point< std::chrono::system_clock > CalculateNextFrameTime( bool renderNow );
198 * @brief Gets the time for the next frame rasterization.
199 * @return The time for the next frame rasterization.
201 std::chrono::time_point< std::chrono::system_clock > GetNextFrameTime();
206 * @brief Initializes the vector renderer.
211 * @brief Play the vector animation.
213 void PlayAnimation();
216 * @brief Stop the vector animation.
218 void StopAnimation();
221 * @brief Pause the vector animation.
223 void PauseAnimation();
226 * @brief Sets the target image size.
228 * @param[in] width The target image width
229 * @param[in] height The target image height
231 void SetSize( uint32_t width, uint32_t height );
234 * @brief Enable looping for 'count' repeats. -1 means to repeat forever.
235 * @param[in] count The number of times to loop
237 void SetLoopCount( int32_t count );
240 * @brief Set the playing range in frame number.
241 * @param[in] playRange The array to specify minimum and maximum progress.
242 * The animation will play between those values.
244 void SetPlayRange( const Property::Array& playRange );
247 * @brief Sets the current frame number of the animation.
248 * @param[in] frameNumber The new frame number between [0, the maximum frame number] or between the play range if specified.
250 void SetCurrentFrameNumber( uint32_t frameNumber );
253 * @brief Sets the stop behavior of the animation. This is performed when the animation is stopped.
254 * @param[in] stopBehavior The stop behavior
256 void SetStopBehavior( DevelImageVisual::StopBehavior::Type stopBehavior );
259 * @brief Sets the looping mode.
260 * Animation plays forwards and then restarts from the beginning or runs backwards again.
261 * @param[in] loopingMode The looping mode
263 void SetLoopingMode( DevelImageVisual::LoopingMode::Type loopingMode );
266 * @brief Gets the frame number when the animation is stopped according to the stop behavior.
268 uint32_t GetStoppedFrame( uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame );
271 * @brief Applies the animation data set by the main thread.
273 void ApplyAnimationData();
276 VectorAnimationTask( const VectorAnimationTask& task ) = delete;
279 VectorAnimationTask& operator=( const VectorAnimationTask& task ) = delete;
285 STOPPING, ///< The animation is stopping
286 STOPPED, ///< The animation has stopped
287 PLAYING, ///< The animation is playing
288 PAUSED ///< The animation is paused
292 VectorAnimationRenderer mVectorRenderer;
293 AnimationData mAnimationData[2];
294 VectorAnimationThread& mVectorAnimationThread;
295 ConditionalWait mConditionalWait;
296 std::unique_ptr< EventThreadCallback > mAnimationFinishedTrigger;
297 PlayState mPlayState;
298 DevelImageVisual::StopBehavior::Type mStopBehavior;
299 DevelImageVisual::LoopingMode::Type mLoopingMode;
300 std::chrono::time_point< std::chrono::system_clock > mNextFrameStartTime;
301 int64_t mFrameDurationNanoSeconds;
303 uint32_t mCurrentFrame;
304 uint32_t mTotalFrame;
305 uint32_t mStartFrame;
309 uint32_t mAnimationDataIndex;
311 int32_t mCurrentLoop;
313 bool mUpdateFrameNumber;
314 bool mNeedAnimationFinishedTrigger;
315 bool mAnimationDataUpdated;
319 } // namespace Internal
321 } // namespace Toolkit
325 #endif // DALI_TOOLKIT_VECTOR_ANIMATION_TASK_H