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/common/vector-wrapper.h>
25 #include <dali/public-api/object/property-array.h>
30 #include <dali-toolkit/devel-api/visuals/animated-vector-image-visual-actions-devel.h>
31 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
39 class VisualFactoryCache;
40 class VectorAnimationThread;
41 class VectorAnimationTask;
42 typedef IntrusivePtr<VectorAnimationTask> VectorAnimationTaskPtr;
45 * The task of the vector animation.
47 class VectorAnimationTask : public RefObject, public ConnectionTracker
50 enum class ResourceStatus
52 LOADED, /// Resource is loaded
53 READY, /// Resource is ready
54 FAILED /// Resource is fail to load
57 using ResourceReadySignalType = Signal<void(ResourceStatus)>;
59 using TimePoint = std::chrono::time_point<std::chrono::steady_clock>;
60 using DynamicPropertyType = std::vector<DevelAnimatedVectorImageVisual::DynamicPropertyInfo>;
63 * Flags for re-sending data to the vector animation thread
67 RESEND_PLAY_RANGE = 1 << 0,
68 RESEND_LOOP_COUNT = 1 << 1,
69 RESEND_STOP_BEHAVIOR = 1 << 2,
70 RESEND_LOOPING_MODE = 1 << 3,
71 RESEND_CURRENT_FRAME = 1 << 4,
73 RESEND_PLAY_STATE = 1 << 6,
74 RESEND_NEED_RESOURCE_READY = 1 << 7,
75 RESEND_DYNAMIC_PROPERTY = 1 << 8
79 * @brief Structure used to pass parameters to the vector animation task
88 stopBehavior(DevelImageVisual::StopBehavior::CURRENT_FRAME),
89 loopingMode(DevelImageVisual::LoopingMode::RESTART),
97 AnimationData& operator=(const AnimationData& rhs)
99 resendFlag |= rhs.resendFlag; // OR resend flag
100 playRange = rhs.playRange;
101 playState = rhs.playState;
102 stopBehavior = rhs.stopBehavior;
103 loopingMode = rhs.loopingMode;
104 currentFrame = rhs.currentFrame;
107 loopCount = rhs.loopCount;
108 dynamicProperties.insert(dynamicProperties.end(), rhs.dynamicProperties.begin(), rhs.dynamicProperties.end());
113 Property::Array playRange;
114 DynamicPropertyType dynamicProperties;
115 DevelImageVisual::PlayState::Type playState;
116 DevelImageVisual::StopBehavior::Type stopBehavior;
117 DevelImageVisual::LoopingMode::Type loopingMode;
118 uint32_t currentFrame;
125 * @brief Constructor.
127 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
129 VectorAnimationTask(VisualFactoryCache& factoryCache);
134 ~VectorAnimationTask() override;
137 * @brief Finalizes the task.
142 * @brief Sets the renderer used to display the result image.
144 * @param[in] renderer The renderer used to display the result image
146 void SetRenderer(Renderer renderer);
149 * @brief Requests to load the animation file.
151 * @param[in] url The url of the vector animation file
153 void RequestLoad(const std::string& url);
156 * @brief Queries whether loading is requested.
157 * @return True if loading is requested.
159 bool IsLoadRequested() const;
162 * @brief Sets data to specify animation playback.
163 * @param[in] data The animation data
165 void SetAnimationData(const AnimationData& data);
168 * @brief This callback is called after the animation is finished.
169 * @param[in] callback The animation finished callback
171 void SetAnimationFinishedCallback(EventThreadCallback* callback);
174 * @brief Gets the playing range in frame number.
175 * @param[out] startFrame The frame number to specify minimum progress.
176 * @param[out] endFrame The frame number to specify maximum progress.
178 void GetPlayRange(uint32_t& startFrame, uint32_t& endFrame);
181 * @brief Retrieves the current frame number of the animation.
182 * @return The current frame number
184 uint32_t GetCurrentFrameNumber() const;
187 * @brief Retrieves the total frame number of the animation.
188 * @return The total frame number
190 uint32_t GetTotalFrameNumber() const;
193 * @brief Gets the default size of the file,.
194 * @return The default size of the file
196 void GetDefaultSize(uint32_t& width, uint32_t& height) const;
199 * @brief Gets the layer information of all the child layers.
200 * @param[out] map The layer information
202 void GetLayerInfo(Property::Map& map) const;
205 * @brief Connect to this signal to be notified when the resource is ready.
206 * @return The signal to connect to.
208 ResourceReadySignalType& ResourceReadySignal();
211 * @brief Rasterizes the current frame.
212 * @param[out] keepAnimation true if the animation is running, false otherwise.
213 * @return true if the rasterization succeeded, false otherwise.
215 bool Rasterize(bool& keepAnimation);
218 * @brief Calculates the time for the next frame rasterization.
219 * @return The time for the next frame rasterization.
221 TimePoint CalculateNextFrameTime(bool renderNow);
224 * @brief Gets the time for the next frame rasterization.
225 * @return The time for the next frame rasterization.
227 TimePoint GetNextFrameTime();
231 * @brief Loads the animation file.
233 * @return True if loading succeeded, false otherwise.
238 * @brief Play the vector animation.
240 void PlayAnimation();
243 * @brief Stop the vector animation.
245 void StopAnimation();
248 * @brief Pause the vector animation.
250 void PauseAnimation();
253 * @brief Sets the target image size.
255 * @param[in] width The target image width
256 * @param[in] height The target image height
258 void SetSize(uint32_t width, uint32_t height);
261 * @brief Enable looping for 'count' repeats. -1 means to repeat forever.
262 * @param[in] count The number of times to loop
264 void SetLoopCount(int32_t count);
267 * @brief Set the playing range in frame number.
268 * @param[in] playRange The array to specify minimum and maximum progress.
269 * The animation will play between those values.
271 void SetPlayRange(const Property::Array& playRange);
274 * @brief Sets the current frame number of the animation.
275 * @param[in] frameNumber The new frame number between [0, the maximum frame number] or between the play range if specified.
277 void SetCurrentFrameNumber(uint32_t frameNumber);
280 * @brief Sets the stop behavior of the animation. This is performed when the animation is stopped.
281 * @param[in] stopBehavior The stop behavior
283 void SetStopBehavior(DevelImageVisual::StopBehavior::Type stopBehavior);
286 * @brief Sets the looping mode.
287 * Animation plays forwards and then restarts from the beginning or runs backwards again.
288 * @param[in] loopingMode The looping mode
290 void SetLoopingMode(DevelImageVisual::LoopingMode::Type loopingMode);
293 * @brief Gets the frame number when the animation is stopped according to the stop behavior.
295 uint32_t GetStoppedFrame(uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame);
298 * @brief Applies the animation data set by the main thread.
300 void ApplyAnimationData();
303 * @brief Called when the texture upload is completed.
305 void OnUploadCompleted();
308 * @brief Event callback from rasterize thread. This is called when the file loading is completed.
310 void OnLoadCompleted();
313 VectorAnimationTask(const VectorAnimationTask& task) = delete;
316 VectorAnimationTask& operator=(const VectorAnimationTask& task) = delete;
321 STOPPING, ///< The animation is stopping
322 STOPPED, ///< The animation has stopped
323 PLAYING, ///< The animation is playing
324 PAUSED ///< The animation is paused
328 VectorAnimationRenderer mVectorRenderer;
329 AnimationData mAnimationData[2];
330 VectorAnimationThread& mVectorAnimationThread;
331 ConditionalWait mConditionalWait;
332 ResourceReadySignalType mResourceReadySignal;
333 std::unique_ptr<EventThreadCallback> mAnimationFinishedTrigger;
334 std::unique_ptr<EventThreadCallback> mLoadCompletedTrigger;
335 PlayState mPlayState;
336 DevelImageVisual::StopBehavior::Type mStopBehavior;
337 DevelImageVisual::LoopingMode::Type mLoopingMode;
338 TimePoint mNextFrameStartTime;
339 int64_t mFrameDurationMicroSeconds;
341 uint32_t mCurrentFrame;
342 uint32_t mTotalFrame;
343 uint32_t mStartFrame;
345 uint32_t mDroppedFrames;
348 uint32_t mAnimationDataIndex;
350 int32_t mCurrentLoop;
352 bool mUpdateFrameNumber;
353 bool mNeedAnimationFinishedTrigger;
354 bool mAnimationDataUpdated;
360 } // namespace Internal
362 } // namespace Toolkit
366 #endif // DALI_TOOLKIT_VECTOR_ANIMATION_TASK_H