1 #ifndef DALI_TOOLKIT_VECTOR_ANIMATION_TASK_H
2 #define DALI_TOOLKIT_VECTOR_ANIMATION_TASK_H
5 * Copyright (c) 2021 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/devel-api/adaptor-framework/event-thread-callback.h>
22 #include <dali/devel-api/adaptor-framework/vector-animation-renderer.h>
23 #include <dali/devel-api/threading/conditional-wait.h>
24 #include <dali/public-api/object/property-array.h>
29 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
37 class VisualFactoryCache;
38 class VectorAnimationThread;
39 class VectorAnimationTask;
40 typedef IntrusivePtr<VectorAnimationTask> VectorAnimationTaskPtr;
43 * The task of the vector animation.
45 class VectorAnimationTask : public RefObject
48 using UploadCompletedSignalType = Dali::VectorAnimationRenderer::UploadCompletedSignalType;
51 * Flags for re-sending data to the vector animation thread
55 RESEND_PLAY_RANGE = 1 << 0,
56 RESEND_LOOP_COUNT = 1 << 1,
57 RESEND_STOP_BEHAVIOR = 1 << 2,
58 RESEND_LOOPING_MODE = 1 << 3,
59 RESEND_CURRENT_FRAME = 1 << 4,
61 RESEND_PLAY_STATE = 1 << 6
65 * @brief Structure used to pass parameters to the vector animation task
73 stopBehavior(DevelImageVisual::StopBehavior::CURRENT_FRAME),
74 loopingMode(DevelImageVisual::LoopingMode::RESTART),
82 AnimationData& operator=(const AnimationData& rhs)
84 resendFlag |= rhs.resendFlag; // OR resend flag
85 playRange = rhs.playRange;
86 playState = rhs.playState;
87 stopBehavior = rhs.stopBehavior;
88 loopingMode = rhs.loopingMode;
89 currentFrame = rhs.currentFrame;
92 loopCount = rhs.loopCount;
97 Property::Array playRange;
98 DevelImageVisual::PlayState::Type playState;
99 DevelImageVisual::StopBehavior::Type stopBehavior;
100 DevelImageVisual::LoopingMode::Type loopingMode;
101 uint32_t currentFrame;
108 * @brief Constructor.
110 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
112 VectorAnimationTask(VisualFactoryCache& factoryCache);
117 ~VectorAnimationTask() override;
120 * @brief Finalizes the task.
125 * @brief Loads the animation file.
127 * @param[in] url The url of the vector animation file
128 * @return True if loading success, false otherwise.
130 bool Load(const std::string& url);
133 * @brief Sets the renderer used to display the result image.
135 * @param[in] renderer The renderer used to display the result image
137 void SetRenderer(Renderer renderer);
140 * @brief Sets data to specify animation playback.
141 * @param[in] data The animation data
143 void SetAnimationData(const AnimationData& data);
146 * @brief This callback is called after the animation is finished.
147 * @param[in] callback The animation finished callback
149 void SetAnimationFinishedCallback(EventThreadCallback* callback);
152 * @brief Gets the playing range in frame number.
153 * @param[out] startFrame The frame number to specify minimum progress.
154 * @param[out] endFrame The frame number to specify maximum progress.
156 void GetPlayRange(uint32_t& startFrame, uint32_t& endFrame);
159 * @brief Retrieves the current frame number of the animation.
160 * @return The current frame number
162 uint32_t GetCurrentFrameNumber() const;
165 * @brief Retrieves the total frame number of the animation.
166 * @return The total frame number
168 uint32_t GetTotalFrameNumber() const;
171 * @brief Gets the default size of the file,.
172 * @return The default size of the file
174 void GetDefaultSize(uint32_t& width, uint32_t& height) const;
177 * @brief Gets the layer information of all the child layers.
178 * @param[out] map The layer information
180 void GetLayerInfo(Property::Map& map) const;
183 * @brief Connect to this signal to be notified when the texture upload is completed.
184 * @return The signal to connect to.
186 UploadCompletedSignalType& UploadCompletedSignal();
189 * @brief Rasterizes the current frame.
190 * @return true if the animation is running, false otherwise.
195 * @brief Calculates the time for the next frame rasterization.
196 * @return The time for the next frame rasterization.
198 std::chrono::time_point<std::chrono::system_clock> CalculateNextFrameTime(bool renderNow);
201 * @brief Gets the time for the next frame rasterization.
202 * @return The time for the next frame rasterization.
204 std::chrono::time_point<std::chrono::system_clock> GetNextFrameTime();
208 * @brief Play the vector animation.
210 void PlayAnimation();
213 * @brief Stop the vector animation.
215 void StopAnimation();
218 * @brief Pause the vector animation.
220 void PauseAnimation();
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 * @brief Applies the animation data set by the main thread.
270 void ApplyAnimationData();
273 VectorAnimationTask(const VectorAnimationTask& task) = delete;
276 VectorAnimationTask& operator=(const VectorAnimationTask& task) = delete;
281 STOPPING, ///< The animation is stopping
282 STOPPED, ///< The animation has stopped
283 PLAYING, ///< The animation is playing
284 PAUSED ///< The animation is paused
288 VectorAnimationRenderer mVectorRenderer;
289 AnimationData mAnimationData[2];
290 VectorAnimationThread& mVectorAnimationThread;
291 ConditionalWait mConditionalWait;
292 std::unique_ptr<EventThreadCallback> mAnimationFinishedTrigger;
293 PlayState mPlayState;
294 DevelImageVisual::StopBehavior::Type mStopBehavior;
295 DevelImageVisual::LoopingMode::Type mLoopingMode;
296 std::chrono::time_point<std::chrono::system_clock> mNextFrameStartTime;
297 int64_t mFrameDurationNanoSeconds;
299 uint32_t mCurrentFrame;
300 uint32_t mTotalFrame;
301 uint32_t mStartFrame;
305 uint32_t mAnimationDataIndex;
307 int32_t mCurrentLoop;
309 bool mUpdateFrameNumber;
310 bool mNeedAnimationFinishedTrigger;
311 bool mAnimationDataUpdated;
315 } // namespace Internal
317 } // namespace Toolkit
321 #endif // DALI_TOOLKIT_VECTOR_ANIMATION_TASK_H