forked from firka/flutter
[Windows] Merge TaskRunnerWin32 into TaskRunner (flutter/engine#35029)
With the removal of the UWP embedder, we can merge Win32-specific implementation classes with their abstract superclasses where those superclasses existed only to support both UWP and Win32. No new tests since this is purely a restructuring of existing code within the Win32 embedder, with no expected change in behaviour. Issue: https://github.com/flutter/flutter/issues/108386
This commit is contained in:
@@ -2428,10 +2428,8 @@ FILE: ../../../flutter/shell/platform/windows/system_utils_unittests.cc
|
||||
FILE: ../../../flutter/shell/platform/windows/task_runner.cc
|
||||
FILE: ../../../flutter/shell/platform/windows/task_runner.h
|
||||
FILE: ../../../flutter/shell/platform/windows/task_runner_unittests.cc
|
||||
FILE: ../../../flutter/shell/platform/windows/task_runner_win32.cc
|
||||
FILE: ../../../flutter/shell/platform/windows/task_runner_win32.h
|
||||
FILE: ../../../flutter/shell/platform/windows/task_runner_win32_window.cc
|
||||
FILE: ../../../flutter/shell/platform/windows/task_runner_win32_window.h
|
||||
FILE: ../../../flutter/shell/platform/windows/task_runner_window.cc
|
||||
FILE: ../../../flutter/shell/platform/windows/task_runner_window.h
|
||||
FILE: ../../../flutter/shell/platform/windows/text_input_manager.cc
|
||||
FILE: ../../../flutter/shell/platform/windows/text_input_manager.h
|
||||
FILE: ../../../flutter/shell/platform/windows/text_input_plugin.cc
|
||||
|
||||
@@ -92,10 +92,8 @@ source_set("flutter_windows_source") {
|
||||
"system_utils.h",
|
||||
"task_runner.cc",
|
||||
"task_runner.h",
|
||||
"task_runner_win32.cc",
|
||||
"task_runner_win32.h",
|
||||
"task_runner_win32_window.cc",
|
||||
"task_runner_win32_window.h",
|
||||
"task_runner_window.cc",
|
||||
"task_runner_window.h",
|
||||
"text_input_manager.cc",
|
||||
"text_input_manager.h",
|
||||
"text_input_plugin.cc",
|
||||
|
||||
@@ -156,7 +156,7 @@ FlutterWindowsEngine::FlutterWindowsEngine(const FlutterProjectBundle& project)
|
||||
embedder_api_.struct_size = sizeof(FlutterEngineProcTable);
|
||||
FlutterEngineGetProcAddresses(&embedder_api_);
|
||||
|
||||
task_runner_ = TaskRunner::Create(
|
||||
task_runner_ = std::make_unique<TaskRunner>(
|
||||
embedder_api_.GetCurrentTime, [this](const auto* task) {
|
||||
if (!engine_) {
|
||||
std::cerr << "Cannot post an engine task when engine is not running."
|
||||
|
||||
@@ -12,7 +12,15 @@ namespace flutter {
|
||||
TaskRunner::TaskRunner(CurrentTimeProc get_current_time,
|
||||
const TaskExpiredCallback& on_task_expired)
|
||||
: get_current_time_(get_current_time),
|
||||
on_task_expired_(std::move(on_task_expired)) {}
|
||||
on_task_expired_(std::move(on_task_expired)) {
|
||||
main_thread_id_ = GetCurrentThreadId();
|
||||
task_runner_window_ = TaskRunnerWindow::GetSharedInstance();
|
||||
task_runner_window_->AddDelegate(this);
|
||||
}
|
||||
|
||||
TaskRunner::~TaskRunner() {
|
||||
task_runner_window_->RemoveDelegate(this);
|
||||
}
|
||||
|
||||
std::chrono::nanoseconds TaskRunner::ProcessTasks() {
|
||||
const TaskTimePoint now = GetCurrentTimeForTask();
|
||||
@@ -101,4 +109,12 @@ void TaskRunner::EnqueueTask(Task task) {
|
||||
WakeUp();
|
||||
}
|
||||
|
||||
bool TaskRunner::RunsTasksOnCurrentThread() const {
|
||||
return GetCurrentThreadId() == main_thread_id_;
|
||||
}
|
||||
|
||||
void TaskRunner::WakeUp() {
|
||||
task_runner_window_->WakeUp();
|
||||
}
|
||||
|
||||
} // namespace flutter
|
||||
|
||||
@@ -14,21 +14,30 @@
|
||||
#include <variant>
|
||||
|
||||
#include "flutter/shell/platform/embedder/embedder.h"
|
||||
#include "flutter/shell/platform/windows/task_runner_window.h"
|
||||
|
||||
namespace flutter {
|
||||
|
||||
typedef uint64_t (*CurrentTimeProc)();
|
||||
|
||||
class TaskRunner {
|
||||
// A custom task runner that integrates with user32 GetMessage semantics so
|
||||
// that host app can own its own message loop and flutter still gets to process
|
||||
// tasks on a timely basis.
|
||||
class TaskRunner : public TaskRunnerWindow::Delegate {
|
||||
public:
|
||||
using TaskTimePoint = std::chrono::steady_clock::time_point;
|
||||
using TaskExpiredCallback = std::function<void(const FlutterTask*)>;
|
||||
using TaskClosure = std::function<void()>;
|
||||
|
||||
virtual ~TaskRunner() = default;
|
||||
// Creates a new task runner with the current thread, current time
|
||||
// provider, and callback for tasks that are ready to be run.
|
||||
TaskRunner(CurrentTimeProc get_current_time,
|
||||
const TaskExpiredCallback& on_task_expired);
|
||||
|
||||
virtual ~TaskRunner();
|
||||
|
||||
// Returns `true` if the current thread is this runner's thread.
|
||||
virtual bool RunsTasksOnCurrentThread() const = 0;
|
||||
virtual bool RunsTasksOnCurrentThread() const;
|
||||
|
||||
// Post a Flutter engine task to the event loop for delayed execution.
|
||||
void PostFlutterTask(FlutterTask flutter_task,
|
||||
@@ -47,33 +56,9 @@ class TaskRunner {
|
||||
}
|
||||
}
|
||||
|
||||
// Creates a new task runner with the current thread, current time
|
||||
// provider, and callback for tasks that are ready to be run.
|
||||
static std::unique_ptr<TaskRunner> Create(
|
||||
CurrentTimeProc get_current_time,
|
||||
const TaskExpiredCallback& on_task_expired);
|
||||
|
||||
protected:
|
||||
TaskRunner(CurrentTimeProc get_current_time,
|
||||
const TaskExpiredCallback& on_task_expired);
|
||||
|
||||
// Schedules timers to call `ProcessTasks()` at the runner's thread.
|
||||
virtual void WakeUp() = 0;
|
||||
|
||||
// Executes expired task, and returns the duration until the next task
|
||||
// deadline if exists, otherwise returns `std::chrono::nanoseconds::max()`.
|
||||
//
|
||||
// Each platform implementations must call this to schedule the tasks.
|
||||
// |TaskRunnerWindow::Delegate|
|
||||
std::chrono::nanoseconds ProcessTasks();
|
||||
|
||||
// Returns the current TaskTimePoint that can be used to determine whether a
|
||||
// task is expired.
|
||||
//
|
||||
// Tests can override this to mock up the time.
|
||||
virtual TaskTimePoint GetCurrentTimeForTask() const {
|
||||
return TaskTimePoint::clock::now();
|
||||
}
|
||||
|
||||
private:
|
||||
typedef std::variant<FlutterTask, TaskClosure> TaskVariant;
|
||||
|
||||
@@ -95,6 +80,17 @@ class TaskRunner {
|
||||
// Enqueues the given task.
|
||||
void EnqueueTask(Task task);
|
||||
|
||||
// Schedules timers to call `ProcessTasks()` at the runner's thread.
|
||||
virtual void WakeUp();
|
||||
|
||||
// Returns the current TaskTimePoint that can be used to determine whether a
|
||||
// task is expired.
|
||||
//
|
||||
// Tests can override this to mock up the time.
|
||||
virtual TaskTimePoint GetCurrentTimeForTask() const {
|
||||
return TaskTimePoint::clock::now();
|
||||
}
|
||||
|
||||
// Returns a TaskTimePoint computed from the given target time from Flutter.
|
||||
TaskTimePoint TimePointFromFlutterTime(
|
||||
uint64_t flutter_target_time_nanos) const;
|
||||
@@ -103,6 +99,8 @@ class TaskRunner {
|
||||
TaskExpiredCallback on_task_expired_;
|
||||
std::mutex task_queue_mutex_;
|
||||
std::priority_queue<Task, std::deque<Task>, Task::Comparer> task_queue_;
|
||||
DWORD main_thread_id_;
|
||||
std::shared_ptr<TaskRunnerWindow> task_runner_window_;
|
||||
|
||||
TaskRunner(const TaskRunner&) = delete;
|
||||
TaskRunner& operator=(const TaskRunner&) = delete;
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
// Copyright 2013 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "flutter/shell/platform/windows/task_runner_win32.h"
|
||||
|
||||
namespace flutter {
|
||||
|
||||
// static
|
||||
std::unique_ptr<TaskRunner> TaskRunner::Create(
|
||||
CurrentTimeProc get_current_time,
|
||||
const TaskExpiredCallback& on_task_expired) {
|
||||
return std::make_unique<TaskRunnerWin32>(get_current_time, on_task_expired);
|
||||
}
|
||||
|
||||
TaskRunnerWin32::TaskRunnerWin32(CurrentTimeProc get_current_time,
|
||||
const TaskExpiredCallback& on_task_expired)
|
||||
: TaskRunner(get_current_time, on_task_expired) {
|
||||
main_thread_id_ = GetCurrentThreadId();
|
||||
task_runner_window_ = TaskRunnerWin32Window::GetSharedInstance();
|
||||
task_runner_window_->AddDelegate(this);
|
||||
}
|
||||
|
||||
TaskRunnerWin32::~TaskRunnerWin32() {
|
||||
task_runner_window_->RemoveDelegate(this);
|
||||
}
|
||||
|
||||
bool TaskRunnerWin32::RunsTasksOnCurrentThread() const {
|
||||
return GetCurrentThreadId() == main_thread_id_;
|
||||
}
|
||||
|
||||
std::chrono::nanoseconds TaskRunnerWin32::ProcessTasks() {
|
||||
return TaskRunner::ProcessTasks();
|
||||
}
|
||||
|
||||
void TaskRunnerWin32::WakeUp() {
|
||||
task_runner_window_->WakeUp();
|
||||
}
|
||||
|
||||
} // namespace flutter
|
||||
@@ -1,46 +0,0 @@
|
||||
// Copyright 2013 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_TASK_RUNNER_WIN32_H_
|
||||
#define FLUTTER_SHELL_PLATFORM_WINDOWS_TASK_RUNNER_WIN32_H_
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#include "flutter/shell/platform/embedder/embedder.h"
|
||||
#include "flutter/shell/platform/windows/task_runner.h"
|
||||
#include "flutter/shell/platform/windows/task_runner_win32_window.h"
|
||||
|
||||
namespace flutter {
|
||||
|
||||
// A custom task runner that integrates with user32 GetMessage semantics so that
|
||||
// host app can own its own message loop and flutter still gets to process
|
||||
// tasks on a timely basis.
|
||||
class TaskRunnerWin32 : public TaskRunner,
|
||||
public TaskRunnerWin32Window::Delegate {
|
||||
public:
|
||||
TaskRunnerWin32(CurrentTimeProc get_current_time,
|
||||
const TaskExpiredCallback& on_task_expired);
|
||||
virtual ~TaskRunnerWin32();
|
||||
|
||||
// |TaskRunner|
|
||||
bool RunsTasksOnCurrentThread() const override;
|
||||
|
||||
// |TaskRunnerWin32Window::Delegate|
|
||||
std::chrono::nanoseconds ProcessTasks() override;
|
||||
|
||||
protected:
|
||||
// |TaskRunner|
|
||||
void WakeUp() override;
|
||||
|
||||
private:
|
||||
DWORD main_thread_id_;
|
||||
std::shared_ptr<TaskRunnerWin32Window> task_runner_window_;
|
||||
|
||||
TaskRunnerWin32(const TaskRunnerWin32&) = delete;
|
||||
TaskRunnerWin32& operator=(const TaskRunnerWin32&) = delete;
|
||||
};
|
||||
|
||||
} // namespace flutter
|
||||
|
||||
#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_TASK_RUNNER_WIN32_H_
|
||||
@@ -2,14 +2,14 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "flutter/shell/platform/windows/task_runner_win32_window.h"
|
||||
#include "flutter/shell/platform/windows/task_runner_window.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
|
||||
namespace flutter {
|
||||
|
||||
TaskRunnerWin32Window::TaskRunnerWin32Window() {
|
||||
TaskRunnerWindow::TaskRunnerWindow() {
|
||||
WNDCLASS window_class = RegisterWindowClass();
|
||||
window_handle_ =
|
||||
CreateWindowEx(0, window_class.lpszClassName, L"", 0, 0, 0, 0, 0,
|
||||
@@ -31,7 +31,7 @@ TaskRunnerWin32Window::TaskRunnerWin32Window() {
|
||||
}
|
||||
}
|
||||
|
||||
TaskRunnerWin32Window::~TaskRunnerWin32Window() {
|
||||
TaskRunnerWindow::~TaskRunnerWindow() {
|
||||
if (window_handle_) {
|
||||
DestroyWindow(window_handle_);
|
||||
window_handle_ = nullptr;
|
||||
@@ -39,37 +39,36 @@ TaskRunnerWin32Window::~TaskRunnerWin32Window() {
|
||||
UnregisterClass(window_class_name_.c_str(), nullptr);
|
||||
}
|
||||
|
||||
std::shared_ptr<TaskRunnerWin32Window>
|
||||
TaskRunnerWin32Window::GetSharedInstance() {
|
||||
static std::weak_ptr<TaskRunnerWin32Window> instance;
|
||||
std::shared_ptr<TaskRunnerWindow> TaskRunnerWindow::GetSharedInstance() {
|
||||
static std::weak_ptr<TaskRunnerWindow> instance;
|
||||
auto res = instance.lock();
|
||||
if (!res) {
|
||||
// can't use make_shared with private contructor
|
||||
res.reset(new TaskRunnerWin32Window());
|
||||
res.reset(new TaskRunnerWindow());
|
||||
instance = res;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
void TaskRunnerWin32Window::WakeUp() {
|
||||
void TaskRunnerWindow::WakeUp() {
|
||||
if (!PostMessage(window_handle_, WM_NULL, 0, 0)) {
|
||||
std::cerr << "Failed to post message to main thread." << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void TaskRunnerWin32Window::AddDelegate(Delegate* delegate) {
|
||||
void TaskRunnerWindow::AddDelegate(Delegate* delegate) {
|
||||
delegates_.push_back(delegate);
|
||||
SetTimer(std::chrono::nanoseconds::zero());
|
||||
}
|
||||
|
||||
void TaskRunnerWin32Window::RemoveDelegate(Delegate* delegate) {
|
||||
void TaskRunnerWindow::RemoveDelegate(Delegate* delegate) {
|
||||
auto i = std::find(delegates_.begin(), delegates_.end(), delegate);
|
||||
if (i != delegates_.end()) {
|
||||
delegates_.erase(i);
|
||||
}
|
||||
}
|
||||
|
||||
void TaskRunnerWin32Window::ProcessTasks() {
|
||||
void TaskRunnerWindow::ProcessTasks() {
|
||||
auto next = std::chrono::nanoseconds::max();
|
||||
auto delegates_copy(delegates_);
|
||||
for (auto delegate : delegates_copy) {
|
||||
@@ -82,7 +81,7 @@ void TaskRunnerWin32Window::ProcessTasks() {
|
||||
SetTimer(next);
|
||||
}
|
||||
|
||||
void TaskRunnerWin32Window::SetTimer(std::chrono::nanoseconds when) {
|
||||
void TaskRunnerWindow::SetTimer(std::chrono::nanoseconds when) {
|
||||
if (when == std::chrono::nanoseconds::max()) {
|
||||
KillTimer(window_handle_, 0);
|
||||
} else {
|
||||
@@ -91,7 +90,7 @@ void TaskRunnerWin32Window::SetTimer(std::chrono::nanoseconds when) {
|
||||
}
|
||||
}
|
||||
|
||||
WNDCLASS TaskRunnerWin32Window::RegisterWindowClass() {
|
||||
WNDCLASS TaskRunnerWindow::RegisterWindowClass() {
|
||||
window_class_name_ = L"FlutterTaskRunnerWindow";
|
||||
|
||||
WNDCLASS window_class{};
|
||||
@@ -110,9 +109,9 @@ WNDCLASS TaskRunnerWin32Window::RegisterWindowClass() {
|
||||
}
|
||||
|
||||
LRESULT
|
||||
TaskRunnerWin32Window::HandleMessage(UINT const message,
|
||||
WPARAM const wparam,
|
||||
LPARAM const lparam) noexcept {
|
||||
TaskRunnerWindow::HandleMessage(UINT const message,
|
||||
WPARAM const wparam,
|
||||
LPARAM const lparam) noexcept {
|
||||
switch (message) {
|
||||
case WM_TIMER:
|
||||
case WM_NULL:
|
||||
@@ -122,11 +121,11 @@ TaskRunnerWin32Window::HandleMessage(UINT const message,
|
||||
return DefWindowProcW(window_handle_, message, wparam, lparam);
|
||||
}
|
||||
|
||||
LRESULT TaskRunnerWin32Window::WndProc(HWND const window,
|
||||
UINT const message,
|
||||
WPARAM const wparam,
|
||||
LPARAM const lparam) noexcept {
|
||||
if (auto* that = reinterpret_cast<TaskRunnerWin32Window*>(
|
||||
LRESULT TaskRunnerWindow::WndProc(HWND const window,
|
||||
UINT const message,
|
||||
WPARAM const wparam,
|
||||
LPARAM const lparam) noexcept {
|
||||
if (auto* that = reinterpret_cast<TaskRunnerWindow*>(
|
||||
GetWindowLongPtr(window, GWLP_USERDATA))) {
|
||||
return that->HandleMessage(message, wparam, lparam);
|
||||
} else {
|
||||
@@ -15,14 +15,18 @@
|
||||
namespace flutter {
|
||||
|
||||
// Hidden HWND responsible for processing flutter tasks on main thread
|
||||
class TaskRunnerWin32Window {
|
||||
class TaskRunnerWindow {
|
||||
public:
|
||||
class Delegate {
|
||||
public:
|
||||
// Executes expired task, and returns the duration until the next task
|
||||
// deadline if exists, otherwise returns `std::chrono::nanoseconds::max()`.
|
||||
//
|
||||
// Each platform implementation must call this to schedule the tasks.
|
||||
virtual std::chrono::nanoseconds ProcessTasks() = 0;
|
||||
};
|
||||
|
||||
static std::shared_ptr<TaskRunnerWin32Window> GetSharedInstance();
|
||||
static std::shared_ptr<TaskRunnerWindow> GetSharedInstance();
|
||||
|
||||
// Triggers processing delegate tasks on main thread
|
||||
void WakeUp();
|
||||
@@ -30,10 +34,10 @@ class TaskRunnerWin32Window {
|
||||
void AddDelegate(Delegate* delegate);
|
||||
void RemoveDelegate(Delegate* delegate);
|
||||
|
||||
~TaskRunnerWin32Window();
|
||||
~TaskRunnerWindow();
|
||||
|
||||
private:
|
||||
TaskRunnerWin32Window();
|
||||
TaskRunnerWindow();
|
||||
|
||||
void ProcessTasks();
|
||||
|
||||
Reference in New Issue
Block a user