| Flutter Windows Embedder |

task_runner_window.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_WINDOW_H_
6 #define FLUTTER_SHELL_PLATFORM_WINDOWS_TASK_RUNNER_WINDOW_H_
7
8 #include <windows.h>
9
10 #include <atomic>
11 #include <chrono>
12 #include <functional>
13 #include <memory>
14 #include <mutex>
15 #include <string>
16 #include <vector>
17
18 #include "flutter/fml/macros.h"
19
20 namespace flutter {
21
22 namespace testing {
23 class TestTaskRunnerWindow;
24 }
25
26 // Background timer thread. Necessary because neither SetTimer nor
27 // CreateThreadpoolTimer have good enough accuracy not to affect the
28 // framerate.
29 class TimerThread {
30 public:
31 explicit TimerThread(std::function<void()> callback);
32
33 void Start();
34 void Stop();
35
36 ~TimerThread();
37
38 // Schedules the callback to be called at specified time point. If there is
39 // already a callback scheduled earlier than the specified time point, does
40 // nothing.
41 void ScheduleAt(
42 std::chrono::time_point<std::chrono::high_resolution_clock> time_point);
43
44 private:
45 void TimerThreadMain();
46
47 std::mutex mutex_;
48 std::condition_variable cv_;
49 std::function<void()> callback_;
50 uint64_t schedule_counter_ = 0;
51 std::chrono::time_point<std::chrono::high_resolution_clock> next_fire_time_;
52 std::optional<std::thread> thread_;
53 };
54
55 // Hidden HWND responsible for processing flutter tasks on main thread
56 class TaskRunnerWindow {
57 public:
58 class Delegate {
59 public:
60 // Executes expired task, and returns the duration until the next task
61 // deadline if exists, otherwise returns `std::chrono::nanoseconds::max()`.
62 //
63 // Each platform implementation must call this to schedule the tasks.
64 virtual std::chrono::nanoseconds ProcessTasks() = 0;
65 };
66
67 static std::shared_ptr<TaskRunnerWindow> GetSharedInstance();
68
69 // Triggers processing delegate tasks on main thread
70 void WakeUp();
71
72 void AddDelegate(Delegate* delegate);
73 void RemoveDelegate(Delegate* delegate);
74
75 void PollOnce(std::chrono::milliseconds timeout);
76
77 ~TaskRunnerWindow();
78
79 private:
80 friend class testing::TestTaskRunnerWindow;
81 TaskRunnerWindow();
82
83 void ProcessTasks();
84
85 void SetTimer(std::chrono::nanoseconds when);
86
87 WNDCLASS RegisterWindowClass();
88
89 LRESULT
90 HandleMessage(UINT const message,
91 WPARAM const wparam,
92 LPARAM const lparam) noexcept;
93
94 static LRESULT CALLBACK WndProc(HWND const window,
95 UINT const message,
96 WPARAM const wparam,
97 LPARAM const lparam) noexcept;
98
99 void OnTimer();
100
101 static void TimerProc(PTP_CALLBACK_INSTANCE Instance,
102 PVOID Context,
103 PTP_TIMER Timer);
104
105 HWND window_handle_;
106 std::wstring window_class_name_;
107 std::vector<Delegate*> delegates_;
108 DWORD thread_id_ = 0;
109 TimerThread timer_thread_;
110
111 // Used to prevent posting wake up message when one is already scheduled.
112 std::atomic_bool wake_up_posted_ = false;
113
114 FML_DISALLOW_COPY_AND_ASSIGN(TaskRunnerWindow);
115 };
116 } // namespace flutter
117
118 #endif // FLUTTER_SHELL_PLATFORM_WINDOWS_TASK_RUNNER_WINDOW_H_
flutter::TaskRunnerWindow::Delegate
Definition: task_runner_window.h:58
flutter::TaskRunnerWindow::Delegate::ProcessTasks
virtual std::chrono::nanoseconds ProcessTasks()=0
flutter::TaskRunnerWindow
Definition: task_runner_window.h:56
flutter::TaskRunnerWindow::WakeUp
void WakeUp()
Definition: task_runner_window.cc:140
flutter::TaskRunnerWindow::PollOnce
void PollOnce(std::chrono::milliseconds timeout)
Definition: task_runner_window.cc:164
flutter::TaskRunnerWindow::GetSharedInstance
static std::shared_ptr< TaskRunnerWindow > GetSharedInstance()
Definition: task_runner_window.cc:129
flutter::TaskRunnerWindow::AddDelegate
void AddDelegate(Delegate *delegate)
Definition: task_runner_window.cc:152
flutter::TaskRunnerWindow::RemoveDelegate
void RemoveDelegate(Delegate *delegate)
Definition: task_runner_window.cc:157
flutter::TaskRunnerWindow::~TaskRunnerWindow
~TaskRunnerWindow()
Definition: task_runner_window.cc:109
flutter::TimerThread
Definition: task_runner_window.h:29
flutter::TimerThread::ScheduleAt
void ScheduleAt(std::chrono::time_point< std::chrono::high_resolution_clock > time_point)
Definition: task_runner_window.cc:49
flutter::TimerThread::TimerThread
TimerThread(std::function< void()> callback)
Definition: task_runner_window.cc:18
flutter::TimerThread::Stop
void Stop()
Definition: task_runner_window.cc:29
flutter::TimerThread::~TimerThread
~TimerThread()
Definition: task_runner_window.cc:41
flutter::TimerThread::Start
void Start()
Definition: task_runner_window.cc:23
flutter::testing::TestTaskRunnerWindow
Definition: task_runner_unittests.cc:119
callback
FlutterDesktopBinaryReply callback
Definition: flutter_windows_view_unittests.cc:52
message
Win32Message message
Definition: keyboard_unittests.cc:137
flutter
Definition: accessibility_bridge_windows.cc:11