1 #ifndef DALI_TOOLKIT_VECTOR_ANIMATION_THREAD_H
2 #define DALI_TOOLKIT_VECTOR_ANIMATION_THREAD_H
5 * Copyright (c) 2024 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/threading/conditional-wait.h>
22 #include <dali/devel-api/threading/mutex.h>
23 #include <dali/devel-api/threading/thread.h>
24 #include <dali/integration-api/adaptor-framework/log-factory-interface.h>
25 #include <dali/integration-api/adaptor-framework/trace-factory-interface.h>
26 #include <dali/public-api/adaptor-framework/round-robin-container-view.h>
27 #include <dali/public-api/signals/connection-tracker.h>
31 #include <dali-toolkit/internal/visuals/animated-vector-image/vector-animation-task.h>
40 * The main animation thread for vector animations
42 class VectorAnimationThread : public Thread
48 VectorAnimationThread();
53 ~VectorAnimationThread() override;
56 * @brief Add a animation task into the vector animation thread, called by main thread.
58 * @param[in] task The task added to the thread.
60 void AddTask(VectorAnimationTaskPtr task);
63 * @brief Called when the rasterization is completed from the rasterize thread.
65 * @param[in] task The completed task
66 * @param[in] success true if the task succeeded, false otherwise.
67 * @param[in] keepAnimation true if the animation is running, false otherwise.
69 void OnTaskCompleted(VectorAnimationTaskPtr task, bool success, bool keepAnimation);
72 * @brief Called when the sleep thread is awaken.
74 void OnAwakeFromSleep();
77 * @brief Add an event trigger callback.
79 * @param callback The callback to add
80 * @param argument The argument to pass to the callback
81 * @note Ownership of the callback is NOT passed onto this class.
82 * @note The callback will be excuted in the main thread.
84 void AddEventTriggerCallback(CallbackBase* callback, uint32_t argument);
87 * @brief Remove event trigger callbacks what we added before.
89 * @param callback The callback to remove
91 void RemoveEventTriggerCallbacks(CallbackBase* callback);
94 * @brief Request to event callback from rasterize thread. This is called when we want to ensure rendering next frame.
96 void RequestForceRenderOnce();
100 * @brief The entry function of the animation thread.
106 * @brief Rasterizes the tasks.
111 * @brief Called when the event callback is triggered.
113 void OnEventCallbackTriggered();
116 * @brief Gets next event callback to process.
118 std::pair<CallbackBase*, uint32_t> GetNextEventCallback();
121 * @brief The thread to sleep until the next frame time.
123 class SleepThread : public Thread
127 * @brief Constructor.
129 SleepThread(CallbackBase* callback);
134 ~SleepThread() override;
137 * @brief Sleeps untile the specified time point.
139 void SleepUntil(std::chrono::time_point<std::chrono::steady_clock> timeToSleepUntil);
143 * @brief The entry function of the animation thread.
148 SleepThread(const SleepThread& thread) = delete;
149 SleepThread& operator=(const SleepThread& thread) = delete;
152 ConditionalWait mConditionalWait;
153 std::unique_ptr<CallbackBase> mAwakeCallback;
154 std::chrono::time_point<std::chrono::steady_clock> mSleepTimePoint;
155 const Dali::LogFactoryInterface& mLogFactory;
156 const Dali::TraceFactoryInterface& mTraceFactory;
158 bool mNeedToSleep : 1;
159 bool mDestroyThread : 1;
164 VectorAnimationThread(const VectorAnimationThread& thread) = delete;
167 VectorAnimationThread& operator=(const VectorAnimationThread& thread) = delete;
170 std::vector<VectorAnimationTaskPtr> mAnimationTasks;
171 std::vector<VectorAnimationTaskPtr> mCompletedTasks;
172 std::vector<VectorAnimationTaskPtr> mWorkingTasks;
173 std::vector<std::pair<CallbackBase*, uint32_t>> mTriggerEventCallbacks{}; // Callbacks are not owned
174 SleepThread mSleepThread;
175 ConditionalWait mConditionalWait;
176 Mutex mEventTriggerMutex;
177 std::unique_ptr<EventThreadCallback> mEventTrigger{};
178 const Dali::LogFactoryInterface& mLogFactory;
179 const Dali::TraceFactoryInterface& mTraceFactory;
180 Dali::AsyncTaskManager mAsyncTaskManager;
182 bool mNeedToSleep : 1;
183 bool mDestroyThread : 1;
184 bool mEventTriggered : 1;
185 bool mForceRenderOnce : 1;
188 } // namespace Internal
190 } // namespace Toolkit
194 #endif // #endif // DALI_TOOLKIT_VECTOR_ANIMATION_THREAD_H