1 #ifndef DALI_TOOLKIT_VECTOR_ANIMATION_TASK_H
2 #define DALI_TOOLKIT_VECTOR_ANIMATION_TASK_H
5 * Copyright (c) 2023 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/adaptor-framework/async-task-manager.h>
25 #include <dali/public-api/common/vector-wrapper.h>
26 #include <dali/public-api/object/property-array.h>
31 #include <dali-toolkit/devel-api/visuals/animated-vector-image-visual-actions-devel.h>
32 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
33 #include <dali-toolkit/internal/visuals/visual-url.h>
41 class VisualFactoryCache;
42 class VectorAnimationThread;
43 class VectorAnimationTask;
44 typedef IntrusivePtr<VectorAnimationTask> VectorAnimationTaskPtr;
47 * The task of the vector animation.
49 class VectorAnimationTask : public AsyncTask, public ConnectionTracker
52 enum class ResourceStatus
54 LOADED, /// Resource is loaded
55 READY, /// Resource is ready
56 FAILED /// Resource is fail to load
59 using ResourceReadySignalType = Signal<void(ResourceStatus)>;
61 using TimePoint = std::chrono::time_point<std::chrono::steady_clock>;
62 using DynamicPropertyType = std::vector<DevelAnimatedVectorImageVisual::DynamicPropertyInfo>;
65 * Flags for re-sending data to the vector animation thread
69 RESEND_PLAY_RANGE = 1 << 0,
70 RESEND_LOOP_COUNT = 1 << 1,
71 RESEND_STOP_BEHAVIOR = 1 << 2,
72 RESEND_LOOPING_MODE = 1 << 3,
73 RESEND_CURRENT_FRAME = 1 << 4,
75 RESEND_PLAY_STATE = 1 << 6,
76 RESEND_NEED_RESOURCE_READY = 1 << 7,
77 RESEND_DYNAMIC_PROPERTY = 1 << 8
81 * @brief Structure used to pass parameters to the vector animation task
90 stopBehavior(DevelImageVisual::StopBehavior::CURRENT_FRAME),
91 loopingMode(DevelImageVisual::LoopingMode::RESTART),
99 AnimationData& operator=(const AnimationData& rhs)
101 resendFlag |= rhs.resendFlag; // OR resend flag
102 playRange = rhs.playRange;
103 playState = rhs.playState;
104 stopBehavior = rhs.stopBehavior;
105 loopingMode = rhs.loopingMode;
106 currentFrame = rhs.currentFrame;
109 loopCount = rhs.loopCount;
110 dynamicProperties.insert(dynamicProperties.end(), rhs.dynamicProperties.begin(), rhs.dynamicProperties.end());
115 Property::Array playRange;
116 DynamicPropertyType dynamicProperties;
117 DevelImageVisual::PlayState::Type playState;
118 DevelImageVisual::StopBehavior::Type stopBehavior;
119 DevelImageVisual::LoopingMode::Type loopingMode;
120 uint32_t currentFrame;
127 * @brief Constructor.
129 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
131 VectorAnimationTask(VisualFactoryCache& factoryCache);
136 ~VectorAnimationTask() override;
139 * Process the task accodring to the type
141 void Process() override;
144 * Whether the task is ready to process.
145 * @return True if the task is ready to process.
147 bool IsReady() override;
150 * @brief Finalizes the task.
155 * @brief Sets the renderer used to display the result image.
157 * @param[in] renderer The renderer used to display the result image
159 void SetRenderer(Renderer renderer);
162 * @brief Requests to load the animation file.
164 * @param[in] url The url of the vector animation file
165 * @param[in] synchronousLoading True if the url should be loaded synchronously
167 void RequestLoad(const VisualUrl& url, bool synchronousLoading);
170 * @brief Queries whether loading is requested.
171 * @return True if loading is requested.
173 bool IsLoadRequested() const;
176 * @brief Sets data to specify animation playback.
177 * @param[in] data The animation data
179 void SetAnimationData(const AnimationData& data);
182 * @brief This callback is called after the animation is finished.
183 * @param[in] callback The animation finished callback
185 void SetAnimationFinishedCallback(CallbackBase* callback);
188 * @brief Gets the playing range in frame number.
189 * @param[out] startFrame The frame number to specify minimum progress.
190 * @param[out] endFrame The frame number to specify maximum progress.
192 void GetPlayRange(uint32_t& startFrame, uint32_t& endFrame);
195 * @brief Retrieves the current frame number of the animation.
196 * @return The current frame number
198 uint32_t GetCurrentFrameNumber() const;
201 * @brief Retrieves the total frame number of the animation.
202 * @return The total frame number
204 uint32_t GetTotalFrameNumber() const;
207 * @brief Gets the default size of the file,.
208 * @return The default size of the file
210 void GetDefaultSize(uint32_t& width, uint32_t& height) const;
213 * @brief Gets the layer information of all the child layers.
214 * @param[out] map The layer information
216 void GetLayerInfo(Property::Map& map) const;
219 * @brief Connect to this signal to be notified when the resource is ready.
220 * @return The signal to connect to.
222 ResourceReadySignalType& ResourceReadySignal();
225 * @brief Rasterizes the current frame.
226 * @return true if the rasterization succeeded, false otherwise.
231 * @brief Calculates the time for the next frame rasterization.
232 * @return The time for the next frame rasterization.
234 TimePoint CalculateNextFrameTime(bool renderNow);
237 * @brief Gets the time for the next frame rasterization.
238 * @return The time for the next frame rasterization.
240 TimePoint GetNextFrameTime();
243 * @brief Called when the rasterization is completed from the asyncTaskManager
244 * @param[in] task The completed task
246 void TaskCompleted(VectorAnimationTaskPtr task);
249 * @brief Check the rasterization succeeded
250 * @return true if the rasterization succeeded, false otherwise.
255 * @brief Check the animation is running
256 * @return true if the animation is running, false otherwise.
262 * @brief Loads the animation file.
264 * @param[in] synchronousLoading True if loading is requested synchronously
265 * @return True if loading succeeded, false otherwise.
267 bool Load(bool synchronousLoading);
270 * @brief Play the vector animation.
272 void PlayAnimation();
275 * @brief Stop the vector animation.
277 void StopAnimation();
280 * @brief Pause the vector animation.
282 void PauseAnimation();
285 * @brief Sets the target image size.
287 * @param[in] width The target image width
288 * @param[in] height The target image height
290 void SetSize(uint32_t width, uint32_t height);
293 * @brief Enable looping for 'count' repeats. -1 means to repeat forever.
294 * @param[in] count The number of times to loop
296 void SetLoopCount(int32_t count);
299 * @brief Set the playing range in frame number.
300 * @param[in] playRange The array to specify minimum and maximum progress.
301 * The animation will play between those values.
303 void SetPlayRange(const Property::Array& playRange);
306 * @brief Sets the current frame number of the animation.
307 * @param[in] frameNumber The new frame number between [0, the maximum frame number] or between the play range if specified.
309 void SetCurrentFrameNumber(uint32_t frameNumber);
312 * @brief Sets the stop behavior of the animation. This is performed when the animation is stopped.
313 * @param[in] stopBehavior The stop behavior
315 void SetStopBehavior(DevelImageVisual::StopBehavior::Type stopBehavior);
318 * @brief Sets the looping mode.
319 * Animation plays forwards and then restarts from the beginning or runs backwards again.
320 * @param[in] loopingMode The looping mode
322 void SetLoopingMode(DevelImageVisual::LoopingMode::Type loopingMode);
325 * @brief Gets the frame number when the animation is stopped according to the stop behavior.
327 uint32_t GetStoppedFrame(uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame);
330 * @brief Applies the animation data set by the main thread.
332 void ApplyAnimationData();
335 * @brief Called when the texture upload is completed.
337 void OnUploadCompleted();
340 * @brief Event callback from rasterize thread. This is called when the file loading is completed.
342 void OnLoadCompleted();
345 VectorAnimationTask(const VectorAnimationTask& task) = delete;
348 VectorAnimationTask& operator=(const VectorAnimationTask& task) = delete;
353 STOPPING, ///< The animation is stopping
354 STOPPED, ///< The animation has stopped
355 PLAYING, ///< The animation is playing
356 PAUSED ///< The animation is paused
360 VectorAnimationRenderer mVectorRenderer;
361 std::vector<AnimationData> mAnimationData[2];
362 VectorAnimationThread& mVectorAnimationThread;
363 ConditionalWait mConditionalWait;
364 ResourceReadySignalType mResourceReadySignal;
365 std::unique_ptr<CallbackBase> mAnimationFinishedCallback{};
366 std::unique_ptr<CallbackBase> mLoadCompletedCallback{};
367 PlayState mPlayState;
368 DevelImageVisual::StopBehavior::Type mStopBehavior;
369 DevelImageVisual::LoopingMode::Type mLoopingMode;
370 TimePoint mNextFrameStartTime;
371 int64_t mFrameDurationMicroSeconds;
373 uint32_t mCurrentFrame;
374 uint32_t mTotalFrame;
375 uint32_t mStartFrame;
377 uint32_t mDroppedFrames;
380 uint32_t mAnimationDataIndex;
382 int32_t mCurrentLoop;
384 bool mUpdateFrameNumber;
385 bool mNeedAnimationFinishedTrigger;
386 bool mAnimationDataUpdated;
394 } // namespace Internal
396 } // namespace Toolkit
400 #endif // DALI_TOOLKIT_VECTOR_ANIMATION_TASK_H