Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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
19namespace flutter {
20
21typedef 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.
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.
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.
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
106};
107
108} // namespace flutter
109
110#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_TASK_RUNNER_H_
std::chrono::steady_clock::time_point TaskTimePoint
Definition task_runner.h:28
void PostFlutterTask(FlutterTask flutter_task, uint64_t flutter_target_time_nanos)
virtual bool RunsTasksOnCurrentThread() const
std::function< void(const FlutterTask *)> TaskExpiredCallback
Definition task_runner.h:29
virtual void WakeUp()
void RunNowOrPostTask(TaskClosure task)
Definition task_runner.h:51
std::function< void()> TaskClosure
Definition task_runner.h:30
void PostTask(TaskClosure task)
virtual TaskTimePoint GetCurrentTimeForTask() const
Definition task_runner.h:90
std::chrono::nanoseconds ProcessTasks()
static bool b
struct MyStruct a[10]
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition macros.h:27
uint64_t(* CurrentTimeProc)()
Definition task_runner.h:21
Definition DM.cpp:1161
bool operator()(const Task &a, const Task &b)
Definition task_runner.h:71
unsigned long DWORD