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>
40 class VisualFactoryCache;
41 class VectorAnimationThread;
42 class VectorAnimationTask;
43 typedef IntrusivePtr<VectorAnimationTask> VectorAnimationTaskPtr;
46 * The task of the vector animation.
48 class VectorAnimationTask : public AsyncTask, public ConnectionTracker
51 enum class ResourceStatus
53 LOADED, /// Resource is loaded
54 READY, /// Resource is ready
55 FAILED /// Resource is fail to load
58 using ResourceReadySignalType = Signal<void(ResourceStatus)>;
60 using TimePoint = std::chrono::time_point<std::chrono::steady_clock>;
61 using DynamicPropertyType = std::vector<DevelAnimatedVectorImageVisual::DynamicPropertyInfo>;
64 * Flags for re-sending data to the vector animation thread
68 RESEND_PLAY_RANGE = 1 << 0,
69 RESEND_LOOP_COUNT = 1 << 1,
70 RESEND_STOP_BEHAVIOR = 1 << 2,
71 RESEND_LOOPING_MODE = 1 << 3,
72 RESEND_CURRENT_FRAME = 1 << 4,
74 RESEND_PLAY_STATE = 1 << 6,
75 RESEND_NEED_RESOURCE_READY = 1 << 7,
76 RESEND_DYNAMIC_PROPERTY = 1 << 8
80 * @brief Structure used to pass parameters to the vector animation task
89 stopBehavior(DevelImageVisual::StopBehavior::CURRENT_FRAME),
90 loopingMode(DevelImageVisual::LoopingMode::RESTART),
98 AnimationData& operator=(const AnimationData& rhs)
100 resendFlag |= rhs.resendFlag; // OR resend flag
101 playRange = rhs.playRange;
102 playState = rhs.playState;
103 stopBehavior = rhs.stopBehavior;
104 loopingMode = rhs.loopingMode;
105 currentFrame = rhs.currentFrame;
108 loopCount = rhs.loopCount;
109 dynamicProperties.insert(dynamicProperties.end(), rhs.dynamicProperties.begin(), rhs.dynamicProperties.end());
114 Property::Array playRange;
115 DynamicPropertyType dynamicProperties;
116 DevelImageVisual::PlayState::Type playState;
117 DevelImageVisual::StopBehavior::Type stopBehavior;
118 DevelImageVisual::LoopingMode::Type loopingMode;
119 uint32_t currentFrame;
126 * @brief Constructor.
128 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
130 VectorAnimationTask(VisualFactoryCache& factoryCache);
135 ~VectorAnimationTask() override;
138 * Process the task accodring to the type
140 void Process() override;
143 * Whether the task is ready to process.
144 * @return True if the task is ready to process.
146 bool IsReady() override;
149 * @brief Finalizes the task.
154 * @brief Sets the renderer used to display the result image.
156 * @param[in] renderer The renderer used to display the result image
158 void SetRenderer(Renderer renderer);
161 * @brief Requests to load the animation file.
163 * @param[in] url The url of the vector animation file
164 * @param[in] synchronousLoading True if the url should be loaded synchronously
166 void RequestLoad(const std::string& url, bool synchronousLoading);
169 * @brief Queries whether loading is requested.
170 * @return True if loading is requested.
172 bool IsLoadRequested() const;
175 * @brief Sets data to specify animation playback.
176 * @param[in] data The animation data
178 void SetAnimationData(const AnimationData& data);
181 * @brief This callback is called after the animation is finished.
182 * @param[in] callback The animation finished callback
184 void SetAnimationFinishedCallback(CallbackBase* callback);
187 * @brief Gets the playing range in frame number.
188 * @param[out] startFrame The frame number to specify minimum progress.
189 * @param[out] endFrame The frame number to specify maximum progress.
191 void GetPlayRange(uint32_t& startFrame, uint32_t& endFrame);
194 * @brief Retrieves the current frame number of the animation.
195 * @return The current frame number
197 uint32_t GetCurrentFrameNumber() const;
200 * @brief Retrieves the total frame number of the animation.
201 * @return The total frame number
203 uint32_t GetTotalFrameNumber() const;
206 * @brief Gets the default size of the file,.
207 * @return The default size of the file
209 void GetDefaultSize(uint32_t& width, uint32_t& height) const;
212 * @brief Gets the layer information of all the child layers.
213 * @param[out] map The layer information
215 void GetLayerInfo(Property::Map& map) const;
218 * @brief Connect to this signal to be notified when the resource is ready.
219 * @return The signal to connect to.
221 ResourceReadySignalType& ResourceReadySignal();
224 * @brief Rasterizes the current frame.
225 * @return true if the rasterization succeeded, false otherwise.
230 * @brief Calculates the time for the next frame rasterization.
231 * @return The time for the next frame rasterization.
233 TimePoint CalculateNextFrameTime(bool renderNow);
236 * @brief Gets the time for the next frame rasterization.
237 * @return The time for the next frame rasterization.
239 TimePoint GetNextFrameTime();
242 * @brief Called when the rasterization is completed from the asyncTaskManager
243 * @param[in] task The completed task
245 void TaskCompleted(VectorAnimationTaskPtr task);
248 * @brief Check the rasterization succeeded
249 * @return true if the rasterization succeeded, false otherwise.
254 * @brief Check the animation is running
255 * @return true if the animation is running, false otherwise.
261 * @brief Loads the animation file.
263 * @param[in] synchronousLoading True if loading is requested synchronously
264 * @return True if loading succeeded, false otherwise.
266 bool Load(bool synchronousLoading);
269 * @brief Play the vector animation.
271 void PlayAnimation();
274 * @brief Stop the vector animation.
276 void StopAnimation();
279 * @brief Pause the vector animation.
281 void PauseAnimation();
284 * @brief Sets the target image size.
286 * @param[in] width The target image width
287 * @param[in] height The target image height
289 void SetSize(uint32_t width, uint32_t height);
292 * @brief Enable looping for 'count' repeats. -1 means to repeat forever.
293 * @param[in] count The number of times to loop
295 void SetLoopCount(int32_t count);
298 * @brief Set the playing range in frame number.
299 * @param[in] playRange The array to specify minimum and maximum progress.
300 * The animation will play between those values.
302 void SetPlayRange(const Property::Array& playRange);
305 * @brief Sets the current frame number of the animation.
306 * @param[in] frameNumber The new frame number between [0, the maximum frame number] or between the play range if specified.
308 void SetCurrentFrameNumber(uint32_t frameNumber);
311 * @brief Sets the stop behavior of the animation. This is performed when the animation is stopped.
312 * @param[in] stopBehavior The stop behavior
314 void SetStopBehavior(DevelImageVisual::StopBehavior::Type stopBehavior);
317 * @brief Sets the looping mode.
318 * Animation plays forwards and then restarts from the beginning or runs backwards again.
319 * @param[in] loopingMode The looping mode
321 void SetLoopingMode(DevelImageVisual::LoopingMode::Type loopingMode);
324 * @brief Gets the frame number when the animation is stopped according to the stop behavior.
326 uint32_t GetStoppedFrame(uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame);
329 * @brief Applies the animation data set by the main thread.
331 void ApplyAnimationData();
334 * @brief Called when the texture upload is completed.
336 void OnUploadCompleted();
339 * @brief Event callback from rasterize thread. This is called when the file loading is completed.
341 void OnLoadCompleted();
344 VectorAnimationTask(const VectorAnimationTask& task) = delete;
347 VectorAnimationTask& operator=(const VectorAnimationTask& task) = delete;
352 STOPPING, ///< The animation is stopping
353 STOPPED, ///< The animation has stopped
354 PLAYING, ///< The animation is playing
355 PAUSED ///< The animation is paused
359 VectorAnimationRenderer mVectorRenderer;
360 AnimationData mAnimationData[2];
361 VectorAnimationThread& mVectorAnimationThread;
362 ConditionalWait mConditionalWait;
363 ResourceReadySignalType mResourceReadySignal;
364 std::unique_ptr<CallbackBase> mAnimationFinishedCallback{};
365 std::unique_ptr<CallbackBase> mLoadCompletedCallback{};
366 PlayState mPlayState;
367 DevelImageVisual::StopBehavior::Type mStopBehavior;
368 DevelImageVisual::LoopingMode::Type mLoopingMode;
369 TimePoint mNextFrameStartTime;
370 int64_t mFrameDurationMicroSeconds;
372 uint32_t mCurrentFrame;
373 uint32_t mTotalFrame;
374 uint32_t mStartFrame;
376 uint32_t mDroppedFrames;
379 uint32_t mAnimationDataIndex;
381 int32_t mCurrentLoop;
383 bool mUpdateFrameNumber;
384 bool mNeedAnimationFinishedTrigger;
385 bool mAnimationDataUpdated;
393 } // namespace Internal
395 } // namespace Toolkit
399 #endif // DALI_TOOLKIT_VECTOR_ANIMATION_TASK_H