From 428ac168e4a736b9a09c6303dd3283cb88afc95e Mon Sep 17 00:00:00 2001 From: Loic Sharma Date: Wed, 30 Aug 2023 17:58:31 -0700 Subject: [PATCH 1/3] [Windows] Update vsync on raster thread --- shell/platform/windows/angle_surface_manager.cc | 7 +++++++ shell/platform/windows/flutter_windows_engine.h | 2 +- shell/platform/windows/flutter_windows_view.cc | 7 ++++++- shell/platform/windows/flutter_windows_view_unittests.cc | 8 ++++++++ 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/shell/platform/windows/angle_surface_manager.cc b/shell/platform/windows/angle_surface_manager.cc index 7deafc2d0bf8c..2165130ba158a 100644 --- a/shell/platform/windows/angle_surface_manager.cc +++ b/shell/platform/windows/angle_surface_manager.cc @@ -343,6 +343,13 @@ void AngleSurfaceManager::SetVSyncEnabled(bool enabled) { LogEglError("Unable to update the swap interval"); return; } + + if (eglMakeCurrent(egl_display_, EGL_NO_SURFACE, EGL_NO_SURFACE, + EGL_NO_CONTEXT) != EGL_TRUE) { + LogEglError( + "Unable to release the context after updating the swap interval"); + return; + } } bool AngleSurfaceManager::GetDevice(ID3D11Device** device) { diff --git a/shell/platform/windows/flutter_windows_engine.h b/shell/platform/windows/flutter_windows_engine.h index cdbcd375fd119..04d33e7583d4b 100644 --- a/shell/platform/windows/flutter_windows_engine.h +++ b/shell/platform/windows/flutter_windows_engine.h @@ -199,7 +199,7 @@ class FlutterWindowsEngine { bool MarkExternalTextureFrameAvailable(int64_t texture_id); // Posts the given callback onto the raster thread. - bool PostRasterThreadTask(fml::closure callback); + virtual bool PostRasterThreadTask(fml::closure callback); // Invoke on the embedder's vsync callback to schedule a frame. void OnVsync(intptr_t baton); diff --git a/shell/platform/windows/flutter_windows_view.cc b/shell/platform/windows/flutter_windows_view.cc index d668d2f447b6c..20ac21558ec86 100644 --- a/shell/platform/windows/flutter_windows_view.cc +++ b/shell/platform/windows/flutter_windows_view.cc @@ -669,7 +669,12 @@ void FlutterWindowsView::UpdateSemanticsEnabled(bool enabled) { void FlutterWindowsView::OnDwmCompositionChanged() { if (engine_->surface_manager()) { - engine_->surface_manager()->SetVSyncEnabled(binding_handler_->NeedsVSync()); + // Update the surface with the new composition state. + // Switch to the raster thread as this requires making the context current. + auto needs_vsync = binding_handler_->NeedsVSync(); + engine_->PostRasterThreadTask([this, needs_vsync]() { + engine_->surface_manager()->SetVSyncEnabled(needs_vsync); + }); } } diff --git a/shell/platform/windows/flutter_windows_view_unittests.cc b/shell/platform/windows/flutter_windows_view_unittests.cc index 850f2708d8dae..52eb1edc4b3cf 100644 --- a/shell/platform/windows/flutter_windows_view_unittests.cc +++ b/shell/platform/windows/flutter_windows_view_unittests.cc @@ -111,6 +111,7 @@ class MockFlutterWindowsEngine : public FlutterWindowsEngine { MockFlutterWindowsEngine() : FlutterWindowsEngine(GetTestProject()) {} MOCK_METHOD0(Stop, bool()); + MOCK_METHOD1(PostRasterThreadTask, bool(fml::closure)); private: FML_DISALLOW_COPY_AND_ASSIGN(MockFlutterWindowsEngine); @@ -1280,6 +1281,13 @@ TEST(FlutterWindowsViewTest, UpdatesVSyncOnDwmUpdates) { std::unique_ptr surface_manager = std::make_unique(); + EXPECT_CALL(*engine.get(), PostRasterThreadTask) + .Times(2) + .WillRepeatedly([](fml::closure callback) { + callback(); + return true; + }); + EXPECT_CALL(*window_binding_handler.get(), NeedsVSync) .WillOnce(Return(true)) .WillOnce(Return(false)); From 47d6350a668cad5c3a5ebdd7a9e24a2f9ca51985 Mon Sep 17 00:00:00 2001 From: Loic Sharma Date: Fri, 1 Sep 2023 13:50:08 -0700 Subject: [PATCH 2/3] Update --- shell/platform/windows/flutter_windows_view_unittests.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell/platform/windows/flutter_windows_view_unittests.cc b/shell/platform/windows/flutter_windows_view_unittests.cc index 52eb1edc4b3cf..18c745c55be93 100644 --- a/shell/platform/windows/flutter_windows_view_unittests.cc +++ b/shell/platform/windows/flutter_windows_view_unittests.cc @@ -111,7 +111,7 @@ class MockFlutterWindowsEngine : public FlutterWindowsEngine { MockFlutterWindowsEngine() : FlutterWindowsEngine(GetTestProject()) {} MOCK_METHOD0(Stop, bool()); - MOCK_METHOD1(PostRasterThreadTask, bool(fml::closure)); + MOCK_METHOD(PostRasterThreadTask, bool(fml::closure)); private: FML_DISALLOW_COPY_AND_ASSIGN(MockFlutterWindowsEngine); From c9c46d1ff3bb730b701f803394c031189cedde7f Mon Sep 17 00:00:00 2001 From: Loic Sharma Date: Fri, 1 Sep 2023 15:05:24 -0700 Subject: [PATCH 3/3] Fix --- shell/platform/windows/flutter_windows_view_unittests.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell/platform/windows/flutter_windows_view_unittests.cc b/shell/platform/windows/flutter_windows_view_unittests.cc index 18c745c55be93..2c759b84a8db6 100644 --- a/shell/platform/windows/flutter_windows_view_unittests.cc +++ b/shell/platform/windows/flutter_windows_view_unittests.cc @@ -111,7 +111,7 @@ class MockFlutterWindowsEngine : public FlutterWindowsEngine { MockFlutterWindowsEngine() : FlutterWindowsEngine(GetTestProject()) {} MOCK_METHOD0(Stop, bool()); - MOCK_METHOD(PostRasterThreadTask, bool(fml::closure)); + MOCK_METHOD(bool, PostRasterThreadTask, (fml::closure)); private: FML_DISALLOW_COPY_AND_ASSIGN(MockFlutterWindowsEngine);