From 374899e94b09779ae87c2acb5e7681fa755fa770 Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Mon, 1 Aug 2022 20:09:01 +0100 Subject: [PATCH] [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 --- .../ci/licenses_golden/licenses_flutter | 6 +-- .../flutter/shell/platform/windows/BUILD.gn | 6 +-- .../windows/flutter_windows_engine.cc | 2 +- .../shell/platform/windows/task_runner.cc | 18 ++++++- .../shell/platform/windows/task_runner.h | 54 +++++++++---------- .../platform/windows/task_runner_win32.cc | 40 -------------- .../platform/windows/task_runner_win32.h | 46 ---------------- ..._win32_window.cc => task_runner_window.cc} | 41 +++++++------- ...er_win32_window.h => task_runner_window.h} | 12 +++-- 9 files changed, 76 insertions(+), 149 deletions(-) delete mode 100644 engine/src/flutter/shell/platform/windows/task_runner_win32.cc delete mode 100644 engine/src/flutter/shell/platform/windows/task_runner_win32.h rename engine/src/flutter/shell/platform/windows/{task_runner_win32_window.cc => task_runner_window.cc} (72%) rename engine/src/flutter/shell/platform/windows/{task_runner_win32_window.h => task_runner_window.h} (78%) diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 1c8291fb82..6b43cac4a1 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -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 diff --git a/engine/src/flutter/shell/platform/windows/BUILD.gn b/engine/src/flutter/shell/platform/windows/BUILD.gn index 3aa94757e4..d8b39d099b 100644 --- a/engine/src/flutter/shell/platform/windows/BUILD.gn +++ b/engine/src/flutter/shell/platform/windows/BUILD.gn @@ -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", diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc index 20d7318aac..3ed63ae3a3 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.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( embedder_api_.GetCurrentTime, [this](const auto* task) { if (!engine_) { std::cerr << "Cannot post an engine task when engine is not running." diff --git a/engine/src/flutter/shell/platform/windows/task_runner.cc b/engine/src/flutter/shell/platform/windows/task_runner.cc index 464afbc438..10a699403d 100644 --- a/engine/src/flutter/shell/platform/windows/task_runner.cc +++ b/engine/src/flutter/shell/platform/windows/task_runner.cc @@ -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 diff --git a/engine/src/flutter/shell/platform/windows/task_runner.h b/engine/src/flutter/shell/platform/windows/task_runner.h index 685ed77e93..b211dbedcd 100644 --- a/engine/src/flutter/shell/platform/windows/task_runner.h +++ b/engine/src/flutter/shell/platform/windows/task_runner.h @@ -14,21 +14,30 @@ #include #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; using TaskClosure = std::function; - 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 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 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::Comparer> task_queue_; + DWORD main_thread_id_; + std::shared_ptr task_runner_window_; TaskRunner(const TaskRunner&) = delete; TaskRunner& operator=(const TaskRunner&) = delete; diff --git a/engine/src/flutter/shell/platform/windows/task_runner_win32.cc b/engine/src/flutter/shell/platform/windows/task_runner_win32.cc deleted file mode 100644 index 01c7179c79..0000000000 --- a/engine/src/flutter/shell/platform/windows/task_runner_win32.cc +++ /dev/null @@ -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::Create( - CurrentTimeProc get_current_time, - const TaskExpiredCallback& on_task_expired) { - return std::make_unique(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 diff --git a/engine/src/flutter/shell/platform/windows/task_runner_win32.h b/engine/src/flutter/shell/platform/windows/task_runner_win32.h deleted file mode 100644 index 6e3ceb8476..0000000000 --- a/engine/src/flutter/shell/platform/windows/task_runner_win32.h +++ /dev/null @@ -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 - -#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 task_runner_window_; - - TaskRunnerWin32(const TaskRunnerWin32&) = delete; - TaskRunnerWin32& operator=(const TaskRunnerWin32&) = delete; -}; - -} // namespace flutter - -#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_TASK_RUNNER_WIN32_H_ diff --git a/engine/src/flutter/shell/platform/windows/task_runner_win32_window.cc b/engine/src/flutter/shell/platform/windows/task_runner_window.cc similarity index 72% rename from engine/src/flutter/shell/platform/windows/task_runner_win32_window.cc rename to engine/src/flutter/shell/platform/windows/task_runner_window.cc index a08f88cd33..ccdf4b570a 100644 --- a/engine/src/flutter/shell/platform/windows/task_runner_win32_window.cc +++ b/engine/src/flutter/shell/platform/windows/task_runner_window.cc @@ -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 #include 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::GetSharedInstance() { - static std::weak_ptr instance; +std::shared_ptr TaskRunnerWindow::GetSharedInstance() { + static std::weak_ptr 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( +LRESULT TaskRunnerWindow::WndProc(HWND const window, + UINT const message, + WPARAM const wparam, + LPARAM const lparam) noexcept { + if (auto* that = reinterpret_cast( GetWindowLongPtr(window, GWLP_USERDATA))) { return that->HandleMessage(message, wparam, lparam); } else { diff --git a/engine/src/flutter/shell/platform/windows/task_runner_win32_window.h b/engine/src/flutter/shell/platform/windows/task_runner_window.h similarity index 78% rename from engine/src/flutter/shell/platform/windows/task_runner_win32_window.h rename to engine/src/flutter/shell/platform/windows/task_runner_window.h index a11d8ef0dd..ae874f82af 100644 --- a/engine/src/flutter/shell/platform/windows/task_runner_win32_window.h +++ b/engine/src/flutter/shell/platform/windows/task_runner_window.h @@ -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 GetSharedInstance(); + static std::shared_ptr 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();