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
152 * @param[in] synchronousLoading True if the url should be loaded synchronously
154 void RequestLoad(const std::string& url, bool synchronousLoading);
157 * @brief Queries whether loading is requested.
158 * @return True if loading is requested.
160 bool IsLoadRequested() const;
163 * @brief Sets data to specify animation playback.
164 * @param[in] data The animation data
166 void SetAnimationData(const AnimationData& data);
169 * @brief This callback is called after the animation is finished.
170 * @param[in] callback The animation finished callback
172 void SetAnimationFinishedCallback(EventThreadCallback* callback);
175 * @brief Gets the playing range in frame number.
176 * @param[out] startFrame The frame number to specify minimum progress.
177 * @param[out] endFrame The frame number to specify maximum progress.
179 void GetPlayRange(uint32_t& startFrame, uint32_t& endFrame);
182 * @brief Retrieves the current frame number of the animation.
183 * @return The current frame number
185 uint32_t GetCurrentFrameNumber() const;
188 * @brief Retrieves the total frame number of the animation.
189 * @return The total frame number
191 uint32_t GetTotalFrameNumber() const;
194 * @brief Gets the default size of the file,.
195 * @return The default size of the file
197 void GetDefaultSize(uint32_t& width, uint32_t& height) const;
200 * @brief Gets the layer information of all the child layers.
201 * @param[out] map The layer information
203 void GetLayerInfo(Property::Map& map) const;
206 * @brief Connect to this signal to be notified when the resource is ready.
207 * @return The signal to connect to.
209 ResourceReadySignalType& ResourceReadySignal();
212 * @brief Rasterizes the current frame.
213 * @param[out] keepAnimation true if the animation is running, false otherwise.
214 * @return true if the rasterization succeeded, false otherwise.
216 bool Rasterize(bool& keepAnimation);
219 * @brief Calculates the time for the next frame rasterization.
220 * @return The time for the next frame rasterization.
222 TimePoint CalculateNextFrameTime(bool renderNow);
225 * @brief Gets the time for the next frame rasterization.
226 * @return The time for the next frame rasterization.
228 TimePoint GetNextFrameTime();
232 * @brief Loads the animation file.
234 * @param[in] synchronousLoading True if loading is requested synchronously
235 * @return True if loading succeeded, false otherwise.
237 bool Load(bool synchronousLoading);
240 * @brief Play the vector animation.
242 void PlayAnimation();
245 * @brief Stop the vector animation.
247 void StopAnimation();
250 * @brief Pause the vector animation.
252 void PauseAnimation();
255 * @brief Sets the target image size.
257 * @param[in] width The target image width
258 * @param[in] height The target image height
260 void SetSize(uint32_t width, uint32_t height);
263 * @brief Enable looping for 'count' repeats. -1 means to repeat forever.
264 * @param[in] count The number of times to loop
266 void SetLoopCount(int32_t count);
269 * @brief Set the playing range in frame number.
270 * @param[in] playRange The array to specify minimum and maximum progress.
271 * The animation will play between those values.
273 void SetPlayRange(const Property::Array& playRange);
276 * @brief Sets the current frame number of the animation.
277 * @param[in] frameNumber The new frame number between [0, the maximum frame number] or between the play range if specified.
279 void SetCurrentFrameNumber(uint32_t frameNumber);
282 * @brief Sets the stop behavior of the animation. This is performed when the animation is stopped.
283 * @param[in] stopBehavior The stop behavior
285 void SetStopBehavior(DevelImageVisual::StopBehavior::Type stopBehavior);
288 * @brief Sets the looping mode.
289 * Animation plays forwards and then restarts from the beginning or runs backwards again.
290 * @param[in] loopingMode The looping mode
292 void SetLoopingMode(DevelImageVisual::LoopingMode::Type loopingMode);
295 * @brief Gets the frame number when the animation is stopped according to the stop behavior.
297 uint32_t GetStoppedFrame(uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame);
300 * @brief Applies the animation data set by the main thread.
302 void ApplyAnimationData();
305 * @brief Called when the texture upload is completed.
307 void OnUploadCompleted();
310 * @brief Event callback from rasterize thread. This is called when the file loading is completed.
312 void OnLoadCompleted();
315 VectorAnimationTask(const VectorAnimationTask& task) = delete;
318 VectorAnimationTask& operator=(const VectorAnimationTask& task) = delete;
323 STOPPING, ///< The animation is stopping
324 STOPPED, ///< The animation has stopped
325 PLAYING, ///< The animation is playing
326 PAUSED ///< The animation is paused
330 VectorAnimationRenderer mVectorRenderer;
331 AnimationData mAnimationData[2];
332 VectorAnimationThread& mVectorAnimationThread;
333 ConditionalWait mConditionalWait;
334 ResourceReadySignalType mResourceReadySignal;
335 std::unique_ptr<EventThreadCallback> mAnimationFinishedTrigger;
336 std::unique_ptr<EventThreadCallback> mLoadCompletedTrigger;
337 PlayState mPlayState;
338 DevelImageVisual::StopBehavior::Type mStopBehavior;
339 DevelImageVisual::LoopingMode::Type mLoopingMode;
340 TimePoint mNextFrameStartTime;
341 int64_t mFrameDurationMicroSeconds;
343 uint32_t mCurrentFrame;
344 uint32_t mTotalFrame;
345 uint32_t mStartFrame;
347 uint32_t mDroppedFrames;
350 uint32_t mAnimationDataIndex;
352 int32_t mCurrentLoop;
354 bool mUpdateFrameNumber;
355 bool mNeedAnimationFinishedTrigger;
356 bool mAnimationDataUpdated;
362 } // namespace Internal
364 } // namespace Toolkit
368 #endif // DALI_TOOLKIT_VECTOR_ANIMATION_TASK_H