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;
50 using TimePoint = std::chrono::time_point<std::chrono::system_clock>;
53 * Flags for re-sending data to the vector animation thread
57 RESEND_PLAY_RANGE = 1 << 0,
58 RESEND_LOOP_COUNT = 1 << 1,
59 RESEND_STOP_BEHAVIOR = 1 << 2,
60 RESEND_LOOPING_MODE = 1 << 3,
61 RESEND_CURRENT_FRAME = 1 << 4,
63 RESEND_PLAY_STATE = 1 << 6
67 * @brief Structure used to pass parameters to the vector animation task
75 stopBehavior(DevelImageVisual::StopBehavior::CURRENT_FRAME),
76 loopingMode(DevelImageVisual::LoopingMode::RESTART),
84 AnimationData& operator=(const AnimationData& rhs)
86 resendFlag |= rhs.resendFlag; // OR resend flag
87 playRange = rhs.playRange;
88 playState = rhs.playState;
89 stopBehavior = rhs.stopBehavior;
90 loopingMode = rhs.loopingMode;
91 currentFrame = rhs.currentFrame;
94 loopCount = rhs.loopCount;
99 Property::Array playRange;
100 DevelImageVisual::PlayState::Type playState;
101 DevelImageVisual::StopBehavior::Type stopBehavior;
102 DevelImageVisual::LoopingMode::Type loopingMode;
103 uint32_t currentFrame;
110 * @brief Constructor.
112 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
114 VectorAnimationTask(VisualFactoryCache& factoryCache);
119 ~VectorAnimationTask() override;
122 * @brief Finalizes the task.
127 * @brief Loads the animation file.
129 * @param[in] url The url of the vector animation file
130 * @return True if loading success, false otherwise.
132 bool Load(const std::string& url);
135 * @brief Sets the renderer used to display the result image.
137 * @param[in] renderer The renderer used to display the result image
139 void SetRenderer(Renderer renderer);
142 * @brief Sets data to specify animation playback.
143 * @param[in] data The animation data
145 void SetAnimationData(const AnimationData& data);
148 * @brief This callback is called after the animation is finished.
149 * @param[in] callback The animation finished callback
151 void SetAnimationFinishedCallback(EventThreadCallback* callback);
154 * @brief Gets the playing range in frame number.
155 * @param[out] startFrame The frame number to specify minimum progress.
156 * @param[out] endFrame The frame number to specify maximum progress.
158 void GetPlayRange(uint32_t& startFrame, uint32_t& endFrame);
161 * @brief Retrieves the current frame number of the animation.
162 * @return The current frame number
164 uint32_t GetCurrentFrameNumber() const;
167 * @brief Retrieves the total frame number of the animation.
168 * @return The total frame number
170 uint32_t GetTotalFrameNumber() const;
173 * @brief Gets the default size of the file,.
174 * @return The default size of the file
176 void GetDefaultSize(uint32_t& width, uint32_t& height) const;
179 * @brief Gets the layer information of all the child layers.
180 * @param[out] map The layer information
182 void GetLayerInfo(Property::Map& map) const;
185 * @brief Connect to this signal to be notified when the texture upload is completed.
186 * @return The signal to connect to.
188 UploadCompletedSignalType& UploadCompletedSignal();
191 * @brief Rasterizes the current frame.
192 * @return true if the animation is running, false otherwise.
197 * @brief Calculates the time for the next frame rasterization.
198 * @return The time for the next frame rasterization.
200 TimePoint CalculateNextFrameTime(bool renderNow);
203 * @brief Gets the time for the next frame rasterization.
204 * @return The time for the next frame rasterization.
206 TimePoint GetNextFrameTime();
210 * @brief Play the vector animation.
212 void PlayAnimation();
215 * @brief Stop the vector animation.
217 void StopAnimation();
220 * @brief Pause the vector animation.
222 void PauseAnimation();
225 * @brief Sets the target image size.
227 * @param[in] width The target image width
228 * @param[in] height The target image height
230 void SetSize(uint32_t width, uint32_t height);
233 * @brief Enable looping for 'count' repeats. -1 means to repeat forever.
234 * @param[in] count The number of times to loop
236 void SetLoopCount(int32_t count);
239 * @brief Set the playing range in frame number.
240 * @param[in] playRange The array to specify minimum and maximum progress.
241 * The animation will play between those values.
243 void SetPlayRange(const Property::Array& playRange);
246 * @brief Sets the current frame number of the animation.
247 * @param[in] frameNumber The new frame number between [0, the maximum frame number] or between the play range if specified.
249 void SetCurrentFrameNumber(uint32_t frameNumber);
252 * @brief Sets the stop behavior of the animation. This is performed when the animation is stopped.
253 * @param[in] stopBehavior The stop behavior
255 void SetStopBehavior(DevelImageVisual::StopBehavior::Type stopBehavior);
258 * @brief Sets the looping mode.
259 * Animation plays forwards and then restarts from the beginning or runs backwards again.
260 * @param[in] loopingMode The looping mode
262 void SetLoopingMode(DevelImageVisual::LoopingMode::Type loopingMode);
265 * @brief Gets the frame number when the animation is stopped according to the stop behavior.
267 uint32_t GetStoppedFrame(uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame);
270 * @brief Applies the animation data set by the main thread.
272 void ApplyAnimationData();
275 VectorAnimationTask(const VectorAnimationTask& task) = delete;
278 VectorAnimationTask& operator=(const VectorAnimationTask& task) = delete;
283 STOPPING, ///< The animation is stopping
284 STOPPED, ///< The animation has stopped
285 PLAYING, ///< The animation is playing
286 PAUSED ///< The animation is paused
290 VectorAnimationRenderer mVectorRenderer;
291 AnimationData mAnimationData[2];
292 VectorAnimationThread& mVectorAnimationThread;
293 ConditionalWait mConditionalWait;
294 std::unique_ptr<EventThreadCallback> mAnimationFinishedTrigger;
295 PlayState mPlayState;
296 DevelImageVisual::StopBehavior::Type mStopBehavior;
297 DevelImageVisual::LoopingMode::Type mLoopingMode;
298 TimePoint mNextFrameStartTime;
299 int64_t mFrameDurationMicroSeconds;
301 uint32_t mCurrentFrame;
302 uint32_t mTotalFrame;
303 uint32_t mStartFrame;
305 uint32_t mDroppedFrames;
308 uint32_t mAnimationDataIndex;
310 int32_t mCurrentLoop;
312 bool mUpdateFrameNumber;
313 bool mNeedAnimationFinishedTrigger;
314 bool mAnimationDataUpdated;
318 } // namespace Internal
320 } // namespace Toolkit
324 #endif // DALI_TOOLKIT_VECTOR_ANIMATION_TASK_H