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