Flutter Engine
win32_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_WIN32_TASK_RUNNER_H_
6 #define FLUTTER_SHELL_PLATFORM_WINDOWS_WIN32_TASK_RUNNER_H_
7 
8 #include <windows.h>
9 
10 #include <chrono>
11 #include <deque>
12 #include <functional>
13 #include <mutex>
14 #include <queue>
15 #include <thread>
16 
17 #include "flutter/shell/platform/embedder/embedder.h"
18 
19 namespace flutter {
20 
21 // A custom task runner that integrates with user32 GetMessage semantics so that
22 // host app can own its own message loop and flutter still gets to process
23 // tasks on a timely basis.
25  public:
26  using TaskExpiredCallback = std::function<void(const FlutterTask*)>;
27  Win32TaskRunner(DWORD main_thread_id,
28  const TaskExpiredCallback& on_task_expired);
29 
31 
32  // Returns if the current thread is the thread used by the win32 event loop.
33  bool RunsTasksOnCurrentThread() const;
34 
35  std::chrono::nanoseconds ProcessTasks();
36 
37  // Post a Flutter engine tasks to the event loop for delayed execution.
38  void PostTask(FlutterTask flutter_task, uint64_t flutter_target_time_nanos);
39 
40  private:
41  using TaskTimePoint = std::chrono::steady_clock::time_point;
42  struct Task {
43  uint64_t order;
44  TaskTimePoint fire_time;
45  FlutterTask task;
46 
47  struct Comparer {
48  bool operator()(const Task& a, const Task& b) {
49  if (a.fire_time == b.fire_time) {
50  return a.order > b.order;
51  }
52  return a.fire_time > b.fire_time;
53  }
54  };
55  };
56  DWORD main_thread_id_;
57  TaskExpiredCallback on_task_expired_;
58  std::mutex task_queue_mutex_;
59  std::priority_queue<Task, std::deque<Task>, Task::Comparer> task_queue_;
60 
61  Win32TaskRunner(const Win32TaskRunner&) = delete;
62 
63  Win32TaskRunner& operator=(const Win32TaskRunner&) = delete;
64 
65  static TaskTimePoint TimePointFromFlutterTime(
66  uint64_t flutter_target_time_nanos);
67 };
68 
69 } // namespace flutter
70 
71 #endif // FLUTTER_SHELL_PLATFORM_WINDOWS_WIN32_TASK_RUNNER_H_
std::function< void(const FlutterTask *)> TaskExpiredCallback
void PostTask(FlutterTask flutter_task, uint64_t flutter_target_time_nanos)
Win32TaskRunner(DWORD main_thread_id, const TaskExpiredCallback &on_task_expired)
bool RunsTasksOnCurrentThread() const
std::chrono::nanoseconds ProcessTasks()
bool operator()(const Task &a, const Task &b)