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 Gets the all marker information.
220 * @param[out] map The marker information
222 void GetMarkerInfo(Property::Map& map) const;
225 * @brief Connect to this signal to be notified when the resource is ready.
226 * @return The signal to connect to.
228 ResourceReadySignalType& ResourceReadySignal();
231 * @brief Rasterizes the current frame.
232 * @return true if the rasterization succeeded, false otherwise.
237 * @brief Calculates the time for the next frame rasterization.
238 * @return The time for the next frame rasterization.
240 TimePoint CalculateNextFrameTime(bool renderNow);
243 * @brief Gets the time for the next frame rasterization.
244 * @return The time for the next frame rasterization.
246 TimePoint GetNextFrameTime();
249 * @brief Called when the rasterization is completed from the asyncTaskManager
250 * @param[in] task The completed task
252 void TaskCompleted(VectorAnimationTaskPtr task);
255 * @brief Check the rasterization succeeded
256 * @return true if the rasterization succeeded, false otherwise.
261 * @brief Check the animation is running
262 * @return true if the animation is running, false otherwise.
268 * @brief Loads the animation file.
270 * @param[in] synchronousLoading True if loading is requested synchronously
271 * @return True if loading succeeded, false otherwise.
273 bool Load(bool synchronousLoading);
276 * @brief Play the vector animation.
278 void PlayAnimation();
281 * @brief Stop the vector animation.
283 void StopAnimation();
286 * @brief Pause the vector animation.
288 void PauseAnimation();
291 * @brief Sets the target image size.
293 * @param[in] width The target image width
294 * @param[in] height The target image height
296 void SetSize(uint32_t width, uint32_t height);
299 * @brief Enable looping for 'count' repeats. -1 means to repeat forever.
300 * @param[in] count The number of times to loop
302 void SetLoopCount(int32_t count);
305 * @brief Set the playing range in frame number.
306 * @param[in] playRange The array to specify minimum and maximum progress.
307 * The animation will play between those values.
309 void SetPlayRange(const Property::Array& playRange);
312 * @brief Sets the current frame number of the animation.
313 * @param[in] frameNumber The new frame number between [0, the maximum frame number] or between the play range if specified.
315 void SetCurrentFrameNumber(uint32_t frameNumber);
318 * @brief Sets the stop behavior of the animation. This is performed when the animation is stopped.
319 * @param[in] stopBehavior The stop behavior
321 void SetStopBehavior(DevelImageVisual::StopBehavior::Type stopBehavior);
324 * @brief Sets the looping mode.
325 * Animation plays forwards and then restarts from the beginning or runs backwards again.
326 * @param[in] loopingMode The looping mode
328 void SetLoopingMode(DevelImageVisual::LoopingMode::Type loopingMode);
331 * @brief Gets the frame number when the animation is stopped according to the stop behavior.
333 uint32_t GetStoppedFrame(uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame);
336 * @brief Applies the animation data set by the main thread.
338 void ApplyAnimationData();
341 * @brief Called when the texture upload is completed.
343 void OnUploadCompleted();
346 * @brief Event callback from rasterize thread. This is called when the file loading is completed.
348 void OnLoadCompleted();
351 VectorAnimationTask(const VectorAnimationTask& task) = delete;
354 VectorAnimationTask& operator=(const VectorAnimationTask& task) = delete;
359 STOPPING, ///< The animation is stopping
360 STOPPED, ///< The animation has stopped
361 PLAYING, ///< The animation is playing
362 PAUSED ///< The animation is paused
366 VectorAnimationRenderer mVectorRenderer;
367 std::vector<AnimationData> mAnimationData[2];
368 VectorAnimationThread& mVectorAnimationThread;
369 ConditionalWait mConditionalWait;
370 ResourceReadySignalType mResourceReadySignal;
371 std::unique_ptr<CallbackBase> mAnimationFinishedCallback{};
372 std::unique_ptr<CallbackBase> mLoadCompletedCallback{};
373 PlayState mPlayState;
374 DevelImageVisual::StopBehavior::Type mStopBehavior;
375 DevelImageVisual::LoopingMode::Type mLoopingMode;
376 TimePoint mNextFrameStartTime;
377 int64_t mFrameDurationMicroSeconds;
379 uint32_t mCurrentFrame;
380 uint32_t mTotalFrame;
381 uint32_t mStartFrame;
383 uint32_t mDroppedFrames;
386 uint32_t mAnimationDataIndex;
388 int32_t mCurrentLoop;
390 bool mUpdateFrameNumber;
391 bool mNeedAnimationFinishedTrigger;
392 bool mAnimationDataUpdated;
400 } // namespace Internal
402 } // namespace Toolkit
406 #endif // DALI_TOOLKIT_VECTOR_ANIMATION_TASK_H