From ac25948dcc0d0b9e5a503c5e481309d4847c763d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Sharma?= <737941+loic-sharma@users.noreply.github.com> Date: Tue, 16 May 2023 11:07:15 -0700 Subject: [PATCH] [Windows] Add force redraw to the C++ client wrapper (flutter/engine#42061) This change adds a C++ client wrapper to Windows's "force redraw" C API. This API can be used to schedule a frame. Part of https://github.com/flutter/flutter/issues/119415 ## Background The Windows runner has a race at startup: 1. **Platform thread**: creates a hidden window 2. **Platform thread**: launches the Flutter engine 3. **UI/Raster threads**: renders the first frame 4. **Platform thread**: Registers a callback to show the window once the next frame has been rendered. Steps 3 and 4 happen in parallel and it is possible for step 3 to complete before step 4 starts. In this scenario, the next frame callback is never called and the window is never shown. The Windows runner will be updated to call the "force redraw" API after it registers the next frame callback. If step 3 hasn't completed yet, the "force redraw" will no-op as a frame is already scheduled. If step 3 has already completed, the "force redraw" will schedule another frame, which will call the next frame callback and show the window. See this discussion below on why we cannot avoid changing the Windows runner to fix this issue: https://github.com/flutter/engine/pull/42061#issuecomment-1550080722 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- .../client_wrapper/flutter_view_controller.cc | 4 ++++ .../flutter_view_controller_unittests.cc | 20 +++++++++++++++++++ .../include/flutter/flutter_view_controller.h | 3 +++ .../testing/stub_flutter_windows_api.cc | 6 +++++- .../testing/stub_flutter_windows_api.h | 3 +++ .../platform/windows/public/flutter_windows.h | 2 +- 6 files changed, 36 insertions(+), 2 deletions(-) diff --git a/engine/src/flutter/shell/platform/windows/client_wrapper/flutter_view_controller.cc b/engine/src/flutter/shell/platform/windows/client_wrapper/flutter_view_controller.cc index 5cb9fe9862..e6f39247cb 100644 --- a/engine/src/flutter/shell/platform/windows/client_wrapper/flutter_view_controller.cc +++ b/engine/src/flutter/shell/platform/windows/client_wrapper/flutter_view_controller.cc @@ -29,6 +29,10 @@ FlutterViewController::~FlutterViewController() { } } +void FlutterViewController::ForceRedraw() { + FlutterDesktopViewControllerForceRedraw(controller_); +} + std::optional FlutterViewController::HandleTopLevelWindowProc( HWND hwnd, UINT message, diff --git a/engine/src/flutter/shell/platform/windows/client_wrapper/flutter_view_controller_unittests.cc b/engine/src/flutter/shell/platform/windows/client_wrapper/flutter_view_controller_unittests.cc index 264a998c29..5b5a019d5f 100644 --- a/engine/src/flutter/shell/platform/windows/client_wrapper/flutter_view_controller_unittests.cc +++ b/engine/src/flutter/shell/platform/windows/client_wrapper/flutter_view_controller_unittests.cc @@ -27,6 +27,11 @@ class TestWindowsApi : public testing::StubFlutterWindowsApi { // |flutter::testing::StubFlutterWindowsApi| void ViewControllerDestroy() override { view_controller_destroyed_ = true; } + // |flutter::testing::StubFlutterWindowsApi| + void ViewControllerForceRedraw() override { + view_controller_force_redrawed_ = true; + } + // |flutter::testing::StubFlutterWindowsApi| FlutterDesktopEngineRef EngineCreate( const FlutterDesktopEngineProperties& engine_properties) override { @@ -41,10 +46,14 @@ class TestWindowsApi : public testing::StubFlutterWindowsApi { bool engine_destroyed() { return engine_destroyed_; } bool view_controller_destroyed() { return view_controller_destroyed_; } + bool view_controller_force_redrawed() { + return view_controller_force_redrawed_; + } private: bool engine_destroyed_ = false; bool view_controller_destroyed_ = false; + bool view_controller_force_redrawed_ = false; }; } // namespace @@ -79,4 +88,15 @@ TEST(FlutterViewControllerTest, GetView) { EXPECT_NE(controller.view(), nullptr); } +TEST(FlutterViewControllerTest, ForceRedraw) { + DartProject project(L"data"); + testing::ScopedStubFlutterWindowsApi scoped_api_stub( + std::make_unique()); + auto test_api = static_cast(scoped_api_stub.stub()); + FlutterViewController controller(100, 100, project); + + controller.ForceRedraw(); + EXPECT_TRUE(test_api->view_controller_force_redrawed()); +} + } // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view_controller.h b/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view_controller.h index 9b08d03f65..7a2ce19197 100644 --- a/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view_controller.h +++ b/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view_controller.h @@ -44,6 +44,9 @@ class FlutterViewController { // Returns the view managed by this controller. FlutterView* view() { return view_.get(); } + // Requests new frame from the engine and repaints the view. + void ForceRedraw(); + // Allows the Flutter engine and any interested plugins an opportunity to // handle the given message. // diff --git a/engine/src/flutter/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.cc b/engine/src/flutter/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.cc index 8028bbb78e..ed9f9d3bcb 100644 --- a/engine/src/flutter/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.cc +++ b/engine/src/flutter/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.cc @@ -65,7 +65,11 @@ FlutterDesktopViewRef FlutterDesktopViewControllerGetView( } void FlutterDesktopViewControllerForceRedraw( - FlutterDesktopViewControllerRef controller) {} + FlutterDesktopViewControllerRef controller) { + if (s_stub_implementation) { + s_stub_implementation->ViewControllerForceRedraw(); + } +} bool FlutterDesktopViewControllerHandleTopLevelWindowProc( FlutterDesktopViewControllerRef controller, diff --git a/engine/src/flutter/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.h b/engine/src/flutter/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.h index 247255c566..a1864d4f07 100644 --- a/engine/src/flutter/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.h +++ b/engine/src/flutter/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.h @@ -38,6 +38,9 @@ class StubFlutterWindowsApi { // Called for FlutterDesktopViewControllerDestroy. virtual void ViewControllerDestroy() {} + // Called for FlutterDesktopViewControllerForceRedraw. + virtual void ViewControllerForceRedraw() {} + // Called for FlutterDesktopViewControllerHandleTopLevelWindowProc. virtual bool ViewControllerHandleTopLevelWindowProc(HWND hwnd, UINT message, diff --git a/engine/src/flutter/shell/platform/windows/public/flutter_windows.h b/engine/src/flutter/shell/platform/windows/public/flutter_windows.h index 1498906fe1..767c3468d4 100644 --- a/engine/src/flutter/shell/platform/windows/public/flutter_windows.h +++ b/engine/src/flutter/shell/platform/windows/public/flutter_windows.h @@ -107,7 +107,7 @@ FLUTTER_EXPORT FlutterDesktopEngineRef FlutterDesktopViewControllerGetEngine( FLUTTER_EXPORT FlutterDesktopViewRef FlutterDesktopViewControllerGetView(FlutterDesktopViewControllerRef controller); -// Requests new frame from engine and repaints the view +// Requests new frame from the engine and repaints the view. FLUTTER_EXPORT void FlutterDesktopViewControllerForceRedraw( FlutterDesktopViewControllerRef controller);