| 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 // Post a task to the event loop or run it immediately if this is being called
50 // from the runner's thread.
51 void RunNowOrPostTask(TaskClosure task) {
52 if (RunsTasksOnCurrentThread()) {
53 task();
54 } else {
55 PostTask(std::move(task));
56 }
57 }
58
59 // |TaskRunnerWindow::Delegate|
60 std::chrono::nanoseconds ProcessTasks();
61
62 private:
63 typedef std::variant<FlutterTask, TaskClosure> TaskVariant;
64
65 struct Task {
66 uint64_t order;
67 TaskTimePoint fire_time;
68 TaskVariant variant;
69
70 struct Comparer {
71 bool operator()(const Task& a, const Task& b) {
72 if (a.fire_time == b.fire_time) {
73 return a.order > b.order;
74 }
75 return a.fire_time > b.fire_time;
76 }
77 };
78 };
79
80 // Enqueues the given task.
81 void EnqueueTask(Task task);
82
83 // Schedules timers to call `ProcessTasks()` at the runner's thread.
84 virtual void WakeUp();
85
86 // Returns the current TaskTimePoint that can be used to determine whether a
87 // task is expired.
88 //
89 // Tests can override this to mock up the time.
90 virtual TaskTimePoint GetCurrentTimeForTask() const {
91 return TaskTimePoint::clock::now();
92 }
93
94 // Returns a TaskTimePoint computed from the given target time from Flutter.
95 TaskTimePoint TimePointFromFlutterTime(
96 uint64_t flutter_target_time_nanos) const;
97
98 CurrentTimeProc get_current_time_;
99 TaskExpiredCallback on_task_expired_;
100 std::mutex task_queue_mutex_;
101 std::priority_queue<Task, std::deque<Task>, Task::Comparer> task_queue_;
102 DWORD main_thread_id_;
103 std::shared_ptr<TaskRunnerWindow> task_runner_window_;
104
105 FML_DISALLOW_COPY_AND_ASSIGN(TaskRunner);
106 };
107
108 } // namespace flutter
109
110 #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:112
flutter::TaskRunner::TaskExpiredCallback
std::function< void(const FlutterTask *)> TaskExpiredCallback
Definition: task_runner.h:29
flutter::TaskRunner::RunNowOrPostTask
void RunNowOrPostTask(TaskClosure task)
Definition: task_runner.h:51
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:22
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:70
flutter::TaskRunner::Task::Comparer::operator()
bool operator()(const Task &a, const Task &b)
Definition: task_runner.h:71
task_runner_window.h