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
111 * @param[in] url The url of the vector animation file
113 VectorAnimationTask(VisualFactoryCache& factoryCache, const std::string& url);
118 ~VectorAnimationTask() override;
121 * @brief Finalizes the task.
126 * @brief Sets the renderer used to display the result image.
128 * @param[in] renderer The renderer used to display the result image
130 void SetRenderer(Renderer renderer);
133 * @brief Sets data to specify animation playback.
134 * @param[in] data The animation data
136 void SetAnimationData(const AnimationData& data);
139 * @brief This callback is called after the animation is finished.
140 * @param[in] callback The animation finished callback
142 void SetAnimationFinishedCallback(EventThreadCallback* callback);
145 * @brief Gets the playing range in frame number.
146 * @param[out] startFrame The frame number to specify minimum progress.
147 * @param[out] endFrame The frame number to specify maximum progress.
149 void GetPlayRange(uint32_t& startFrame, uint32_t& endFrame);
152 * @brief Retrieves the current frame number of the animation.
153 * @return The current frame number
155 uint32_t GetCurrentFrameNumber() const;
158 * @brief Retrieves the total frame number of the animation.
159 * @return The total frame number
161 uint32_t GetTotalFrameNumber() const;
164 * @brief Gets the default size of the file,.
165 * @return The default size of the file
167 void GetDefaultSize(uint32_t& width, uint32_t& height) const;
170 * @brief Gets the layer information of all the child layers.
171 * @param[out] map The layer information
173 void GetLayerInfo(Property::Map& map) const;
176 * @brief Connect to this signal to be notified when the texture upload is completed.
177 * @return The signal to connect to.
179 UploadCompletedSignalType& UploadCompletedSignal();
182 * @brief Rasterizes the current frame.
183 * @return true if the animation is running, false otherwise.
188 * @brief Calculates the time for the next frame rasterization.
189 * @return The time for the next frame rasterization.
191 std::chrono::time_point<std::chrono::system_clock> CalculateNextFrameTime(bool renderNow);
194 * @brief Gets the time for the next frame rasterization.
195 * @return The time for the next frame rasterization.
197 std::chrono::time_point<std::chrono::system_clock> GetNextFrameTime();
201 * @brief Initializes the vector renderer.
206 * @brief Play the vector animation.
208 void PlayAnimation();
211 * @brief Stop the vector animation.
213 void StopAnimation();
216 * @brief Pause the vector animation.
218 void PauseAnimation();
221 * @brief Sets the target image size.
223 * @param[in] width The target image width
224 * @param[in] height The target image height
226 void SetSize(uint32_t width, uint32_t height);
229 * @brief Enable looping for 'count' repeats. -1 means to repeat forever.
230 * @param[in] count The number of times to loop
232 void SetLoopCount(int32_t count);
235 * @brief Set the playing range in frame number.
236 * @param[in] playRange The array to specify minimum and maximum progress.
237 * The animation will play between those values.
239 void SetPlayRange(const Property::Array& playRange);
242 * @brief Sets the current frame number of the animation.
243 * @param[in] frameNumber The new frame number between [0, the maximum frame number] or between the play range if specified.
245 void SetCurrentFrameNumber(uint32_t frameNumber);
248 * @brief Sets the stop behavior of the animation. This is performed when the animation is stopped.
249 * @param[in] stopBehavior The stop behavior
251 void SetStopBehavior(DevelImageVisual::StopBehavior::Type stopBehavior);
254 * @brief Sets the looping mode.
255 * Animation plays forwards and then restarts from the beginning or runs backwards again.
256 * @param[in] loopingMode The looping mode
258 void SetLoopingMode(DevelImageVisual::LoopingMode::Type loopingMode);
261 * @brief Gets the frame number when the animation is stopped according to the stop behavior.
263 uint32_t GetStoppedFrame(uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame);
266 * @brief Applies the animation data set by the main thread.
268 void ApplyAnimationData();
271 VectorAnimationTask(const VectorAnimationTask& task) = delete;
274 VectorAnimationTask& operator=(const VectorAnimationTask& task) = delete;
279 STOPPING, ///< The animation is stopping
280 STOPPED, ///< The animation has stopped
281 PLAYING, ///< The animation is playing
282 PAUSED ///< The animation is paused
286 VectorAnimationRenderer mVectorRenderer;
287 AnimationData mAnimationData[2];
288 VectorAnimationThread& mVectorAnimationThread;
289 ConditionalWait mConditionalWait;
290 std::unique_ptr<EventThreadCallback> mAnimationFinishedTrigger;
291 PlayState mPlayState;
292 DevelImageVisual::StopBehavior::Type mStopBehavior;
293 DevelImageVisual::LoopingMode::Type mLoopingMode;
294 std::chrono::time_point<std::chrono::system_clock> mNextFrameStartTime;
295 int64_t mFrameDurationNanoSeconds;
297 uint32_t mCurrentFrame;
298 uint32_t mTotalFrame;
299 uint32_t mStartFrame;
303 uint32_t mAnimationDataIndex;
305 int32_t mCurrentLoop;
307 bool mUpdateFrameNumber;
308 bool mNeedAnimationFinishedTrigger;
309 bool mAnimationDataUpdated;
313 } // namespace Internal
315 } // namespace Toolkit
319 #endif // DALI_TOOLKIT_VECTOR_ANIMATION_TASK_H