1 #ifndef DALI_TOOLKIT_VECTOR_ANIMATION_TASK_H
2 #define DALI_TOOLKIT_VECTOR_ANIMATION_TASK_H
5 * Copyright (c) 2022 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, public ConnectionTracker
48 enum class ResourceStatus
50 LOADED, /// Resource is loaded
51 READY, /// Resource is ready
52 FAILED /// Resource is fail to load
55 using ResourceReadySignalType = Signal<void(ResourceStatus)>;
57 using TimePoint = std::chrono::time_point<std::chrono::steady_clock>;
60 * Flags for re-sending data to the vector animation thread
64 RESEND_PLAY_RANGE = 1 << 0,
65 RESEND_LOOP_COUNT = 1 << 1,
66 RESEND_STOP_BEHAVIOR = 1 << 2,
67 RESEND_LOOPING_MODE = 1 << 3,
68 RESEND_CURRENT_FRAME = 1 << 4,
70 RESEND_PLAY_STATE = 1 << 6,
71 RESEND_NEED_RESOURCE_READY = 1 << 7
75 * @brief Structure used to pass parameters to the vector animation task
83 stopBehavior(DevelImageVisual::StopBehavior::CURRENT_FRAME),
84 loopingMode(DevelImageVisual::LoopingMode::RESTART),
92 AnimationData& operator=(const AnimationData& rhs)
94 resendFlag |= rhs.resendFlag; // OR resend flag
95 playRange = rhs.playRange;
96 playState = rhs.playState;
97 stopBehavior = rhs.stopBehavior;
98 loopingMode = rhs.loopingMode;
99 currentFrame = rhs.currentFrame;
102 loopCount = rhs.loopCount;
107 Property::Array playRange;
108 DevelImageVisual::PlayState::Type playState;
109 DevelImageVisual::StopBehavior::Type stopBehavior;
110 DevelImageVisual::LoopingMode::Type loopingMode;
111 uint32_t currentFrame;
118 * @brief Constructor.
120 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
122 VectorAnimationTask(VisualFactoryCache& factoryCache);
127 ~VectorAnimationTask() override;
130 * @brief Finalizes the task.
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 Requests to load the animation file.
144 * @param[in] url The url of the vector animation file
146 void RequestLoad(const std::string& url);
149 * @brief Queries whether loading is requested.
150 * @return True if loading is requested.
152 bool IsLoadRequested() const;
155 * @brief Sets data to specify animation playback.
156 * @param[in] data The animation data
158 void SetAnimationData(const AnimationData& data);
161 * @brief This callback is called after the animation is finished.
162 * @param[in] callback The animation finished callback
164 void SetAnimationFinishedCallback(EventThreadCallback* callback);
167 * @brief Gets the playing range in frame number.
168 * @param[out] startFrame The frame number to specify minimum progress.
169 * @param[out] endFrame The frame number to specify maximum progress.
171 void GetPlayRange(uint32_t& startFrame, uint32_t& endFrame);
174 * @brief Retrieves the current frame number of the animation.
175 * @return The current frame number
177 uint32_t GetCurrentFrameNumber() const;
180 * @brief Retrieves the total frame number of the animation.
181 * @return The total frame number
183 uint32_t GetTotalFrameNumber() const;
186 * @brief Gets the default size of the file,.
187 * @return The default size of the file
189 void GetDefaultSize(uint32_t& width, uint32_t& height) const;
192 * @brief Gets the layer information of all the child layers.
193 * @param[out] map The layer information
195 void GetLayerInfo(Property::Map& map) const;
198 * @brief Connect to this signal to be notified when the resource is ready.
199 * @return The signal to connect to.
201 ResourceReadySignalType& ResourceReadySignal();
204 * @brief Rasterizes the current frame.
205 * @param[out] keepAnimation true if the animation is running, false otherwise.
206 * @return true if the rasterization succeeded, false otherwise.
208 bool Rasterize(bool& keepAnimation);
211 * @brief Calculates the time for the next frame rasterization.
212 * @return The time for the next frame rasterization.
214 TimePoint CalculateNextFrameTime(bool renderNow);
217 * @brief Gets the time for the next frame rasterization.
218 * @return The time for the next frame rasterization.
220 TimePoint GetNextFrameTime();
224 * @brief Loads the animation file.
226 * @return True if loading succeeded, false otherwise.
231 * @brief Play the vector animation.
233 void PlayAnimation();
236 * @brief Stop the vector animation.
238 void StopAnimation();
241 * @brief Pause the vector animation.
243 void PauseAnimation();
246 * @brief Sets the target image size.
248 * @param[in] width The target image width
249 * @param[in] height The target image height
251 void SetSize(uint32_t width, uint32_t height);
254 * @brief Enable looping for 'count' repeats. -1 means to repeat forever.
255 * @param[in] count The number of times to loop
257 void SetLoopCount(int32_t count);
260 * @brief Set the playing range in frame number.
261 * @param[in] playRange The array to specify minimum and maximum progress.
262 * The animation will play between those values.
264 void SetPlayRange(const Property::Array& playRange);
267 * @brief Sets the current frame number of the animation.
268 * @param[in] frameNumber The new frame number between [0, the maximum frame number] or between the play range if specified.
270 void SetCurrentFrameNumber(uint32_t frameNumber);
273 * @brief Sets the stop behavior of the animation. This is performed when the animation is stopped.
274 * @param[in] stopBehavior The stop behavior
276 void SetStopBehavior(DevelImageVisual::StopBehavior::Type stopBehavior);
279 * @brief Sets the looping mode.
280 * Animation plays forwards and then restarts from the beginning or runs backwards again.
281 * @param[in] loopingMode The looping mode
283 void SetLoopingMode(DevelImageVisual::LoopingMode::Type loopingMode);
286 * @brief Gets the frame number when the animation is stopped according to the stop behavior.
288 uint32_t GetStoppedFrame(uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame);
291 * @brief Applies the animation data set by the main thread.
293 void ApplyAnimationData();
296 * @brief Called when the texture upload is completed.
298 void OnUploadCompleted();
301 * @brief Event callback from rasterize thread. This is called when the file loading is completed.
303 void OnLoadCompleted();
306 VectorAnimationTask(const VectorAnimationTask& task) = delete;
309 VectorAnimationTask& operator=(const VectorAnimationTask& task) = delete;
314 STOPPING, ///< The animation is stopping
315 STOPPED, ///< The animation has stopped
316 PLAYING, ///< The animation is playing
317 PAUSED ///< The animation is paused
321 VectorAnimationRenderer mVectorRenderer;
322 AnimationData mAnimationData[2];
323 VectorAnimationThread& mVectorAnimationThread;
324 ConditionalWait mConditionalWait;
325 ResourceReadySignalType mResourceReadySignal;
326 std::unique_ptr<EventThreadCallback> mAnimationFinishedTrigger;
327 std::unique_ptr<EventThreadCallback> mLoadCompletedTrigger;
328 PlayState mPlayState;
329 DevelImageVisual::StopBehavior::Type mStopBehavior;
330 DevelImageVisual::LoopingMode::Type mLoopingMode;
331 TimePoint mNextFrameStartTime;
332 int64_t mFrameDurationMicroSeconds;
334 uint32_t mCurrentFrame;
335 uint32_t mTotalFrame;
336 uint32_t mStartFrame;
338 uint32_t mDroppedFrames;
341 uint32_t mAnimationDataIndex;
343 int32_t mCurrentLoop;
345 bool mUpdateFrameNumber;
346 bool mNeedAnimationFinishedTrigger;
347 bool mAnimationDataUpdated;
353 } // namespace Internal
355 } // namespace Toolkit
359 #endif // DALI_TOOLKIT_VECTOR_ANIMATION_TASK_H