5
5
#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_TASK_RUNNER_H_
6
6
#define FLUTTER_SHELL_PLATFORM_WINDOWS_TASK_RUNNER_H_
7
7
8
- #include < windows.h>
9
-
10
8
#include < chrono>
9
+ #include < deque>
10
+ #include < functional>
11
11
#include < memory>
12
+ #include < mutex>
13
+ #include < queue>
14
+ #include < variant>
12
15
13
16
#include " flutter/shell/platform/embedder/embedder.h"
14
17
15
18
namespace flutter {
16
19
17
20
typedef uint64_t (*CurrentTimeProc)();
18
21
19
- // Abstract custom task runner for scheduling custom tasks.
20
22
class TaskRunner {
21
23
public:
22
24
using TaskTimePoint = std::chrono::steady_clock::time_point;
@@ -25,18 +27,18 @@ class TaskRunner {
25
27
26
28
virtual ~TaskRunner () = default ;
27
29
28
- // Returns if the current thread is the UI thread.
30
+ // Returns `true` if the current thread is this runner's thread.
29
31
virtual bool RunsTasksOnCurrentThread () const = 0;
30
32
31
33
// Post a Flutter engine task to the event loop for delayed execution.
32
- virtual void PostFlutterTask (FlutterTask flutter_task,
33
- uint64_t flutter_target_time_nanos) = 0 ;
34
+ void PostFlutterTask (FlutterTask flutter_task,
35
+ uint64_t flutter_target_time_nanos);
34
36
35
37
// Post a task to the event loop.
36
- virtual void PostTask (TaskClosure task) = 0 ;
38
+ void PostTask (TaskClosure task);
37
39
38
40
// Post a task to the event loop or run it immediately if this is being called
39
- // from the main thread.
41
+ // from the runner's thread.
40
42
void RunNowOrPostTask (TaskClosure task) {
41
43
if (RunsTasksOnCurrentThread ()) {
42
44
task ();
@@ -45,12 +47,57 @@ class TaskRunner {
45
47
}
46
48
}
47
49
48
- // Creates a new task runner with the given main thread ID , current time
50
+ // Creates a new task runner with the current thread, current time
49
51
// provider, and callback for tasks that are ready to be run.
50
52
static std::unique_ptr<TaskRunner> Create (
51
- DWORD main_thread_id,
52
53
CurrentTimeProc get_current_time,
53
54
const TaskExpiredCallback& on_task_expired);
55
+
56
+ protected:
57
+ TaskRunner (CurrentTimeProc get_current_time,
58
+ const TaskExpiredCallback& on_task_expired);
59
+
60
+ // Schedules timers to call `ProcessTasks()` at the runner's thread.
61
+ virtual void WakeUp () = 0;
62
+
63
+ // Executes expired task, and returns the duration until the next task
64
+ // deadline if exists, otherwise returns `std::chrono::nanoseconds::max()`.
65
+ //
66
+ // Each platform implementations must call this to schedule the tasks.
67
+ std::chrono::nanoseconds ProcessTasks ();
68
+
69
+ private:
70
+ typedef std::variant<FlutterTask, TaskClosure> TaskVariant;
71
+
72
+ struct Task {
73
+ uint64_t order;
74
+ TaskTimePoint fire_time;
75
+ TaskVariant variant;
76
+
77
+ struct Comparer {
78
+ bool operator ()(const Task& a, const Task& b) {
79
+ if (a.fire_time == b.fire_time ) {
80
+ return a.order > b.order ;
81
+ }
82
+ return a.fire_time > b.fire_time ;
83
+ }
84
+ };
85
+ };
86
+
87
+ // Enqueues the given task.
88
+ void EnqueueTask (Task task);
89
+
90
+ // Returns a TaskTimePoint computed from the given target time from Flutter.
91
+ TaskTimePoint TimePointFromFlutterTime (
92
+ uint64_t flutter_target_time_nanos) const ;
93
+
94
+ CurrentTimeProc get_current_time_;
95
+ TaskExpiredCallback on_task_expired_;
96
+ std::mutex task_queue_mutex_;
97
+ std::priority_queue<Task, std::deque<Task>, Task::Comparer> task_queue_;
98
+
99
+ TaskRunner (const TaskRunner&) = delete ;
100
+ TaskRunner& operator =(const TaskRunner&) = delete ;
54
101
};
55
102
56
103
} // namespace flutter
0 commit comments