[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
This commit is contained in:
Loïc Sharma
2023-05-16 11:07:15 -07:00
committed by GitHub
parent 61d8ffe27c
commit ac25948dcc
6 changed files with 36 additions and 2 deletions

View File

@@ -29,6 +29,10 @@ FlutterViewController::~FlutterViewController() {
}
}
void FlutterViewController::ForceRedraw() {
FlutterDesktopViewControllerForceRedraw(controller_);
}
std::optional<LRESULT> FlutterViewController::HandleTopLevelWindowProc(
HWND hwnd,
UINT message,

View File

@@ -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<TestWindowsApi>());
auto test_api = static_cast<TestWindowsApi*>(scoped_api_stub.stub());
FlutterViewController controller(100, 100, project);
controller.ForceRedraw();
EXPECT_TRUE(test_api->view_controller_force_redrawed());
}
} // namespace flutter

View File

@@ -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.
//

View File

@@ -65,7 +65,11 @@ FlutterDesktopViewRef FlutterDesktopViewControllerGetView(
}
void FlutterDesktopViewControllerForceRedraw(
FlutterDesktopViewControllerRef controller) {}
FlutterDesktopViewControllerRef controller) {
if (s_stub_implementation) {
s_stub_implementation->ViewControllerForceRedraw();
}
}
bool FlutterDesktopViewControllerHandleTopLevelWindowProc(
FlutterDesktopViewControllerRef controller,

View File

@@ -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,

View File

@@ -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);