| Flutter Windows Embedder |

task_runner.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_TASK_RUNNER_H_
6 #define FLUTTER_SHELL_PLATFORM_WINDOWS_TASK_RUNNER_H_
7
8 #include <chrono>
9 #include <deque>
10 #include <functional>
11 #include <memory>
12 #include <mutex>
13 #include <queue>
14 #include <variant>
15
16 #include "flutter/shell/platform/embedder/embedder.h"
17 #include "flutter/shell/platform/windows/task_runner_window.h"
18
19 namespace flutter {
20
21 typedef uint64_t (*CurrentTimeProc)();
22
23 // A custom task runner that integrates with user32 GetMessage semantics so
24 // that host app can own its own message loop and flutter still gets to process
25 // tasks on a timely basis.
26 class TaskRunner : public TaskRunnerWindow::Delegate {
27 public:
28 using TaskTimePoint = std::chrono::steady_clock::time_point;
29 using TaskExpiredCallback = std::function<void(const FlutterTask*)>;
30 using TaskClosure = std::function<void()>;
31
32 // Creates a new task runner with the current thread, current time
33 // provider, and callback for tasks that are ready to be run.
34 TaskRunner(CurrentTimeProc get_current_time,
35 const TaskExpiredCallback& on_task_expired);
36
37 virtual ~TaskRunner();
38
39 // Returns `true` if the current thread is this runner's thread.
40 virtual bool RunsTasksOnCurrentThread() const;
41
42 // Post a Flutter engine task to the event loop for delayed execution.
43 void PostFlutterTask(FlutterTask flutter_task,
44 uint64_t flutter_target_time_nanos);
45
46 // Post a task to the event loop.
47 void PostTask(TaskClosure task);
48
49 // Polls the event loop once. This will only process tasks scheduled through
50 // the task runner. It will not process messages sent to other windows.
51 void PollOnce(std::chrono::milliseconds timeout);
52
53 // Post a task to the event loop or run it immediately if this is being called
54 // from the runner's thread.
55 void RunNowOrPostTask(TaskClosure task) {
56 if (RunsTasksOnCurrentThread()) {
57 task();
58 } else {
59 PostTask(std::move(task));
60 }
61 }
62
63 // |TaskRunnerWindow::Delegate|
64 std::chrono::nanoseconds ProcessTasks();
65
66 private:
67 typedef std::variant<FlutterTask, TaskClosure> TaskVariant;
68
69 struct Task {
70 uint64_t order;
71 TaskTimePoint fire_time;
72 TaskVariant variant;
73
74 struct Comparer {
75 bool operator()(const Task& a, const Task& b) {
76 if (a.fire_time == b.fire_time) {
77 return a.order > b.order;
78 }
79 return a.fire_time > b.fire_time;
80 }
81 };
82 };
83
84 // Enqueues the given task.
85 void EnqueueTask(Task task);
86
87 // Schedules timers to call `ProcessTasks()` at the runner's thread.
88 virtual void WakeUp();
89
90 // Returns the current TaskTimePoint that can be used to determine whether a
91 // task is expired.
92 //
93 // Tests can override this to mock up the time.
94 virtual TaskTimePoint GetCurrentTimeForTask() const {
95 return TaskTimePoint::clock::now();
96 }
97
98 // Returns a TaskTimePoint computed from the given target time from Flutter.
99 TaskTimePoint TimePointFromFlutterTime(
100 uint64_t flutter_target_time_nanos) const;
101
102 CurrentTimeProc get_current_time_;
103 TaskExpiredCallback on_task_expired_;
104 std::mutex task_queue_mutex_;
105 std::priority_queue<Task, std::deque<Task>, Task::Comparer> task_queue_;
106 DWORD main_thread_id_;
107 std::shared_ptr<TaskRunnerWindow> task_runner_window_;
108
109 FML_DISALLOW_COPY_AND_ASSIGN(TaskRunner);
110 };
111
112 } // namespace flutter
113
114 #endif // FLUTTER_SHELL_PLATFORM_WINDOWS_TASK_RUNNER_H_
flutter::TaskRunner
Definition: task_runner.h:26
flutter::TaskRunner::TaskTimePoint
std::chrono::steady_clock::time_point TaskTimePoint
Definition: task_runner.h:28
flutter::TaskRunner::PostFlutterTask
void PostFlutterTask(FlutterTask flutter_task, uint64_t flutter_target_time_nanos)
Definition: task_runner.cc:80
flutter::TaskRunner::TaskRunner
TaskRunner(CurrentTimeProc get_current_time, const TaskExpiredCallback &on_task_expired)
Definition: task_runner.cc:12
flutter::TaskRunner::~TaskRunner
virtual ~TaskRunner()
Definition: task_runner.cc:21
flutter::TaskRunner::RunsTasksOnCurrentThread
virtual bool RunsTasksOnCurrentThread() const
Definition: task_runner.cc:116
flutter::TaskRunner::TaskExpiredCallback
std::function< void(const FlutterTask *)> TaskExpiredCallback
Definition: task_runner.h:29
flutter::TaskRunner::PollOnce
void PollOnce(std::chrono::milliseconds timeout)
Definition: task_runner.cc:95
flutter::TaskRunner::RunNowOrPostTask
void RunNowOrPostTask(TaskClosure task)
Definition: task_runner.h:55
flutter::TaskRunner::TaskClosure
std::function< void()> TaskClosure
Definition: task_runner.h:30
flutter::TaskRunner::PostTask
void PostTask(TaskClosure task)
Definition: task_runner.cc:88
flutter::TaskRunner::ProcessTasks
std::chrono::nanoseconds ProcessTasks()
Definition: task_runner.cc:25
flutter::TaskRunnerWindow::Delegate
Definition: task_runner_window.h:58
flutter
Definition: accessibility_bridge_windows.cc:11
flutter::CurrentTimeProc
uint64_t(* CurrentTimeProc)()
Definition: task_runner.h:21
flutter::TaskRunner::Task::Comparer
Definition: task_runner.h:74
flutter::TaskRunner::Task::Comparer::operator()
bool operator()(const Task &a, const Task &b)
Definition: task_runner.h:75
task_runner_window.h