From f79695512eff3a353c3fbb4b06b03c442a57f8c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Sharma?= <737941+loic-sharma@users.noreply.github.com> Date: Thu, 20 Apr 2023 18:01:50 -0700 Subject: [PATCH] [Windows] Don't block raster thread until v-blank (flutter/engine#41231) Currently the Windows raster thread blocks until the v-blank. As a result, on Windows a frame can spend 17ms on the raster thread even if rasterization completes in under 1ms. This behavior prevents us from rendering multiple views on the same raster thread on Windows. This change does not make rasterization faster. Instead, it allows the raster thread to do more work. Addresses https://github.com/flutter/flutter/issues/124903 ### Results Here are the performance graphs if I hover on and off the counter app's button repeatedly. #### Before The raster thread spends ~17ms per frame consistently: ![Pasted image 20230414180144](https://user-images.githubusercontent.com/737941/232175444-f4b8eb64-b8a3-47b3-aa85-112c56d596a2.png) #### After After this change, the raster thread spends less than 1ms per frame consistently: ![Pasted image 20230414180011](https://user-images.githubusercontent.com/737941/232176377-a2f26c41-dfd1-4140-a775-59d6f0fac31f.png) ### Background Blocking until the v-blank prevents screen tearing if the OS does not synchronize to the vsync. Windows does synchronize if the Desktop Windows Manager composition is enabled, which is required on Windows 8 and newer. In other words, this blocking behavior is unnecessary for Windows 8 and newer. For Windows 7, screen tearing is possible if DWM composition is disabled (either by the user or by an app). In this scenario, the Flutter app should block until the v-blank to synchronize with the vsync and prevent screen tearing. We can detect if DWM composition is disabled using [`DwmIsCompositionEnabled`](https://learn.microsoft.com/en-us/windows/win32/api/dwmapi/nf-dwmapi-dwmiscompositionenabled). We can detect changes to DWM composition using the top-level [`WM_DWMCOMPOSITIONCHANGED`](https://learn.microsoft.com/en-us/windows/win32/dwm/wm-dwmcompositionchanged) Windows message. Useful resources: 1. https://learn.microsoft.com/en-us/windows/win32/dwm/composition-ovw 2. https://www.khronos.org/opengl/wiki/Swap_Interval 3. https://forums.imgtec.com/t/eglswapinterval-0-tearing-problem/2356/5 4. https://stackoverflow.com/questions/32282252/can-eglswapinterval0-cause-screen-tearing 5. https://bugs.chromium.org/p/chromium/issues/detail?id=480361 --- .../platform/windows/angle_surface_manager.cc | 29 +++- .../platform/windows/angle_surface_manager.h | 20 ++- .../shell/platform/windows/flutter_window.cc | 12 ++ .../shell/platform/windows/flutter_window.h | 3 + .../windows/flutter_windows_engine.cc | 4 + .../platform/windows/flutter_windows_engine.h | 3 + .../platform/windows/flutter_windows_view.cc | 14 +- .../platform/windows/flutter_windows_view.h | 3 + .../windows/flutter_windows_view_unittests.cc | 134 ++++++++++++++++-- .../testing/mock_window_binding_handler.h | 1 + .../platform/windows/window_binding_handler.h | 4 + .../windows/windows_lifecycle_manager.cc | 9 +- .../windows/windows_lifecycle_manager.h | 3 +- 13 files changed, 214 insertions(+), 25 deletions(-) diff --git a/engine/src/flutter/shell/platform/windows/angle_surface_manager.cc b/engine/src/flutter/shell/platform/windows/angle_surface_manager.cc index cd8973dad6..18d21f6b42 100644 --- a/engine/src/flutter/shell/platform/windows/angle_surface_manager.cc +++ b/engine/src/flutter/shell/platform/windows/angle_surface_manager.cc @@ -209,7 +209,8 @@ void AngleSurfaceManager::CleanUp() { bool AngleSurfaceManager::CreateSurface(WindowsRenderTarget* render_target, EGLint width, - EGLint height) { + EGLint height, + bool vsync_enabled) { if (!render_target || !initialize_succeeded_) { return false; } @@ -226,17 +227,21 @@ bool AngleSurfaceManager::CreateSurface(WindowsRenderTarget* render_target, surfaceAttributes); if (surface == EGL_NO_SURFACE) { LogEglError("Surface creation failed."); + return false; } surface_width_ = width; surface_height_ = height; render_surface_ = surface; + + SetVSyncEnabled(vsync_enabled); return true; } void AngleSurfaceManager::ResizeSurface(WindowsRenderTarget* render_target, EGLint width, - EGLint height) { + EGLint height, + bool vsync_enabled) { EGLint existing_width, existing_height; GetSurfaceDimensions(&existing_width, &existing_height); if (width != existing_width || height != existing_height) { @@ -245,7 +250,7 @@ void AngleSurfaceManager::ResizeSurface(WindowsRenderTarget* render_target, ClearContext(); DestroySurface(); - if (!CreateSurface(render_target, width, height)) { + if (!CreateSurface(render_target, width, height, vsync_enabled)) { FML_LOG(ERROR) << "AngleSurfaceManager::ResizeSurface failed to create surface"; } @@ -300,6 +305,24 @@ EGLSurface AngleSurfaceManager::CreateSurfaceFromHandle( egl_config_, attributes); } +void AngleSurfaceManager::SetVSyncEnabled(bool enabled) { + if (eglMakeCurrent(egl_display_, render_surface_, render_surface_, + egl_context_) != EGL_TRUE) { + LogEglError("Unable to make surface current to update the swap interval"); + return; + } + + // OpenGL swap intervals can be used to prevent screen tearing. + // If enabled, the raster thread blocks until the v-blank. + // This is unnecessary if DWM composition is enabled. + // See: https://www.khronos.org/opengl/wiki/Swap_Interval + // See: https://learn.microsoft.com/windows/win32/dwm/composition-ovw + if (eglSwapInterval(egl_display_, enabled ? 1 : 0) != EGL_TRUE) { + LogEglError("Unable to update the swap interval"); + return; + } +} + bool AngleSurfaceManager::GetDevice(ID3D11Device** device) { if (!resolved_device_) { PFNEGLQUERYDISPLAYATTRIBEXTPROC egl_query_display_attrib_EXT = diff --git a/engine/src/flutter/shell/platform/windows/angle_surface_manager.h b/engine/src/flutter/shell/platform/windows/angle_surface_manager.h index ca5ac61f7e..edfe7a8cbc 100644 --- a/engine/src/flutter/shell/platform/windows/angle_surface_manager.h +++ b/engine/src/flutter/shell/platform/windows/angle_surface_manager.h @@ -23,7 +23,7 @@ namespace flutter { -// A manager for inializing ANGLE correctly and using it to create and +// A manager for initializing ANGLE correctly and using it to create and // destroy surfaces class AngleSurfaceManager { public: @@ -34,17 +34,19 @@ class AngleSurfaceManager { // associated with window, in the appropriate format for display. // Target represents the visual entity to bind to. Width and // height represent dimensions surface is created at. - bool CreateSurface(WindowsRenderTarget* render_target, - EGLint width, - EGLint height); + virtual bool CreateSurface(WindowsRenderTarget* render_target, + EGLint width, + EGLint height, + bool enable_vsync); // Resizes backing surface from current size to newly requested size // based on width and height for the specific case when width and height do // not match current surface dimensions. Target represents the visual entity // to bind to. - void ResizeSurface(WindowsRenderTarget* render_target, - EGLint width, - EGLint height); + virtual void ResizeSurface(WindowsRenderTarget* render_target, + EGLint width, + EGLint height, + bool enable_vsync); // queries EGL for the dimensions of surface in physical // pixels returning width and height as out params. @@ -76,6 +78,10 @@ class AngleSurfaceManager { // Gets the |EGLDisplay|. EGLDisplay egl_display() const { return egl_display_; }; + // If enabled, makes the current surface's buffer swaps block until the + // v-blank. + virtual void SetVSyncEnabled(bool enabled); + // Gets the |ID3D11Device| chosen by ANGLE. bool GetDevice(ID3D11Device** device); diff --git a/engine/src/flutter/shell/platform/windows/flutter_window.cc b/engine/src/flutter/shell/platform/windows/flutter_window.cc index a1c84c7969..e5fbc89403 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_window.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_window.cc @@ -312,4 +312,16 @@ ui::AXPlatformNodeWin* FlutterWindow::GetAlert() { return alert_node_.get(); } +bool FlutterWindow::NeedsVSync() { + // If the Desktop Window Manager composition is enabled, + // the system itself synchronizes with v-sync. + // See: https://learn.microsoft.com/windows/win32/dwm/composition-ovw + BOOL composition_enabled; + if (SUCCEEDED(::DwmIsCompositionEnabled(&composition_enabled))) { + return !composition_enabled; + } + + return true; +} + } // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/flutter_window.h b/engine/src/flutter/shell/platform/windows/flutter_window.h index b122f4a8c5..5725af5de1 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_window.h +++ b/engine/src/flutter/shell/platform/windows/flutter_window.h @@ -156,6 +156,9 @@ class FlutterWindow : public Window, public WindowBindingHandler { // |WindowBindingHandler| ui::AXPlatformNodeWin* GetAlert() override; + // |WindowBindingHandler| + bool NeedsVSync() override; + // |Window| ui::AXFragmentRootDelegateWin* GetAxFragmentRootDelegate() override; 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 95d17aa93c..39dbe8ea6b 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc @@ -774,4 +774,8 @@ FlutterWindowsEngine::accessibility_bridge() { return view_->accessibility_bridge(); } +void FlutterWindowsEngine::OnDwmCompositionChanged() { + view_->OnDwmCompositionChanged(); +} + } // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h index 3b73f0a865..797cc2142d 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h @@ -266,6 +266,9 @@ class FlutterWindowsEngine { LPARAM lparam, AppExitType exit_type); + // Called when a WM_DWMCOMPOSITIONCHANGED message is received. + void OnDwmCompositionChanged(); + protected: // Creates the keyboard key handler. // diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_view.cc b/engine/src/flutter/shell/platform/windows/flutter_windows_view.cc index 77288a3991..4107546669 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_view.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_view.cc @@ -81,8 +81,8 @@ uint32_t FlutterWindowsView::GetFrameBufferId(size_t width, size_t height) { if (resize_target_width_ == width && resize_target_height_ == height) { // Platform thread is blocked for the entire duration until the // resize_status_ is set to kDone. - engine_->surface_manager()->ResizeSurface(GetRenderTarget(), width, height); - engine_->surface_manager()->MakeCurrent(); + engine_->surface_manager()->ResizeSurface(GetRenderTarget(), width, height, + binding_handler_->NeedsVSync()); resize_status_ = ResizeState::kFrameGenerated; } @@ -586,8 +586,10 @@ bool FlutterWindowsView::PresentSoftwareBitmap(const void* allocation, void FlutterWindowsView::CreateRenderSurface() { if (engine_ && engine_->surface_manager()) { PhysicalWindowBounds bounds = binding_handler_->GetPhysicalWindowBounds(); + bool enable_vsync = binding_handler_->NeedsVSync(); engine_->surface_manager()->CreateSurface(GetRenderTarget(), bounds.width, - bounds.height); + bounds.height, enable_vsync); + resize_target_width_ = bounds.width; resize_target_height_ = bounds.height; } @@ -661,4 +663,10 @@ void FlutterWindowsView::UpdateSemanticsEnabled(bool enabled) { } } +void FlutterWindowsView::OnDwmCompositionChanged() { + if (engine_->surface_manager()) { + engine_->surface_manager()->SetVSyncEnabled(binding_handler_->NeedsVSync()); + } +} + } // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_view.h b/engine/src/flutter/shell/platform/windows/flutter_windows_view.h index 9160add166..8dc31fafee 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_view.h +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_view.h @@ -195,6 +195,9 @@ class FlutterWindowsView : public WindowBindingHandlerDelegate, // |TextInputPluginDelegate| void OnResetImeComposing() override; + // Called when a WM_ONCOMPOSITIONCHANGED message is received. + void OnDwmCompositionChanged(); + // Get a pointer to the alert node for this view. ui::AXPlatformNodeWin* AlertNode() const; diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_view_unittests.cc b/engine/src/flutter/shell/platform/windows/flutter_windows_view_unittests.cc index b56e51beec..89082dd46c 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_view_unittests.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_view_unittests.cc @@ -12,6 +12,7 @@ #include #include +#include "flutter/fml/synchronization/waitable_event.h" #include "flutter/shell/platform/common/json_message_codec.h" #include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h" #include "flutter/shell/platform/windows/flutter_window.h" @@ -27,8 +28,10 @@ namespace flutter { namespace testing { +using ::testing::_; using ::testing::InSequence; using ::testing::NiceMock; +using ::testing::Return; constexpr uint64_t kScanCodeKeyA = 0x1e; constexpr uint64_t kVirtualKeyA = 0x41; @@ -117,8 +120,12 @@ class MockAngleSurfaceManager : public AngleSurfaceManager { public: MockAngleSurfaceManager() {} + MOCK_METHOD4(CreateSurface, bool(WindowsRenderTarget*, EGLint, EGLint, bool)); + MOCK_METHOD4(ResizeSurface, void(WindowsRenderTarget*, EGLint, EGLint, bool)); MOCK_METHOD0(DestroySurface, void()); + MOCK_METHOD1(SetVSyncEnabled, void(bool)); + private: FML_DISALLOW_COPY_AND_ASSIGN(MockAngleSurfaceManager); }; @@ -714,28 +721,44 @@ TEST(FlutterWindowsViewTest, WindowResizeTests) { auto window_binding_handler = std::make_unique>(); + std::unique_ptr surface_manager = + std::make_unique(); + + EXPECT_CALL(*window_binding_handler.get(), NeedsVSync) + .WillOnce(Return(false)); + EXPECT_CALL( + *surface_manager.get(), + ResizeSurface(_, /*width=*/500, /*height=*/500, /*enable_vsync=*/false)) + .Times(1); + EXPECT_CALL(*surface_manager.get(), DestroySurface).Times(1); FlutterWindowsView view(std::move(window_binding_handler)); + modifier.SetSurfaceManager(surface_manager.release()); view.SetEngine(std::move(engine)); - bool send_window_metrics_event_called = false; + fml::AutoResetWaitableEvent metrics_sent_latch; modifier.embedder_api().SendWindowMetricsEvent = MOCK_ENGINE_PROC( SendWindowMetricsEvent, - ([&send_window_metrics_event_called]( - auto engine, const FlutterWindowMetricsEvent* even) { - send_window_metrics_event_called = true; + ([&metrics_sent_latch](auto engine, + const FlutterWindowMetricsEvent* event) { + metrics_sent_latch.Signal(); return kSuccess; })); - std::promise resize_completed; - std::thread([&resize_completed, &view]() { + fml::AutoResetWaitableEvent resized_latch; + std::thread([&resized_latch, &view]() { + // Start the window resize. This sends the new window metrics + // and then blocks until another thread completes the window resize. view.OnWindowSizeChanged(500, 500); - resize_completed.set_value(true); + resized_latch.Signal(); }).detach(); - auto result = resize_completed.get_future().wait_for(std::chrono::seconds(1)); - EXPECT_EQ(std::future_status::ready, result); - EXPECT_TRUE(send_window_metrics_event_called); + // Wait until the platform thread has started the window resize. + metrics_sent_latch.Wait(); + + // Complete the window resize by requesting a buffer with the new window size. + view.GetFrameBufferId(500, 500); + resized_latch.Wait(); } TEST(FlutterWindowsViewTest, WindowRepaintTests) { @@ -1083,5 +1106,96 @@ TEST(FlutterWindowsViewTest, TooltipNodeData) { EXPECT_EQ(uia_tooltip, "tooltip"); } +// Don't block until the v-blank if it is disabled by the window. +TEST(FlutterWindowsViewTest, DisablesVSync) { + std::unique_ptr engine = + std::make_unique(); + auto window_binding_handler = + std::make_unique>(); + std::unique_ptr surface_manager = + std::make_unique(); + + EXPECT_CALL(*window_binding_handler.get(), NeedsVSync) + .WillOnce(Return(false)); + + EngineModifier modifier(engine.get()); + FlutterWindowsView view(std::move(window_binding_handler)); + + InSequence s; + EXPECT_CALL(*surface_manager.get(), + CreateSurface(_, _, _, /*vsync_enabled=*/false)) + .Times(1) + .WillOnce(Return(true)); + + EXPECT_CALL(*engine.get(), Stop).Times(1); + EXPECT_CALL(*surface_manager.get(), DestroySurface).Times(1); + + modifier.SetSurfaceManager(surface_manager.release()); + view.SetEngine(std::move(engine)); + + view.CreateRenderSurface(); +} + +// Blocks until the v-blank if it is enabled by the window. +TEST(FlutterWindowsViewTest, EnablesVSync) { + std::unique_ptr engine = + std::make_unique(); + auto window_binding_handler = + std::make_unique>(); + std::unique_ptr surface_manager = + std::make_unique(); + + EXPECT_CALL(*window_binding_handler.get(), NeedsVSync).WillOnce(Return(true)); + + EngineModifier modifier(engine.get()); + FlutterWindowsView view(std::move(window_binding_handler)); + + InSequence s; + EXPECT_CALL(*surface_manager.get(), + CreateSurface(_, _, _, /*vsync_enabled=*/true)) + .Times(1) + .WillOnce(Return(true)); + + EXPECT_CALL(*engine.get(), Stop).Times(1); + EXPECT_CALL(*surface_manager.get(), DestroySurface).Times(1); + + modifier.SetSurfaceManager(surface_manager.release()); + view.SetEngine(std::move(engine)); + + view.CreateRenderSurface(); +} + +// Desktop Window Manager composition can be disabled on Windows 7. +// If this happens, the app must synchronize with the vsync to prevent +// screen tearing. +TEST(FlutterWindowsViewTest, UpdatesVSyncOnDwmUpdates) { + std::unique_ptr engine = + std::make_unique(); + auto window_binding_handler = + std::make_unique>(); + std::unique_ptr surface_manager = + std::make_unique(); + + EXPECT_CALL(*window_binding_handler.get(), NeedsVSync) + .WillOnce(Return(true)) + .WillOnce(Return(false)); + + EngineModifier modifier(engine.get()); + FlutterWindowsView view(std::move(window_binding_handler)); + + InSequence s; + EXPECT_CALL(*surface_manager.get(), SetVSyncEnabled(true)).Times(1); + EXPECT_CALL(*surface_manager.get(), SetVSyncEnabled(false)).Times(1); + + EXPECT_CALL(*engine.get(), Stop).Times(1); + EXPECT_CALL(*surface_manager.get(), DestroySurface).Times(1); + + modifier.SetSurfaceManager(surface_manager.release()); + view.SetEngine(std::move(engine)); + + view.GetEngine()->OnDwmCompositionChanged(); + view.GetEngine()->OnDwmCompositionChanged(); +} + } // namespace testing } // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/testing/mock_window_binding_handler.h b/engine/src/flutter/shell/platform/windows/testing/mock_window_binding_handler.h index a2c9145ecc..0f5daadaec 100644 --- a/engine/src/flutter/shell/platform/windows/testing/mock_window_binding_handler.h +++ b/engine/src/flutter/shell/platform/windows/testing/mock_window_binding_handler.h @@ -36,6 +36,7 @@ class MockWindowBindingHandler : public WindowBindingHandler { MOCK_METHOD0(SendInitialAccessibilityFeatures, void()); MOCK_METHOD0(GetAlertDelegate, AlertPlatformNodeDelegate*()); MOCK_METHOD0(GetAlert, ui::AXPlatformNodeWin*()); + MOCK_METHOD0(NeedsVSync, bool()); private: FML_DISALLOW_COPY_AND_ASSIGN(MockWindowBindingHandler); diff --git a/engine/src/flutter/shell/platform/windows/window_binding_handler.h b/engine/src/flutter/shell/platform/windows/window_binding_handler.h index e13d7a138c..2bc28fe284 100644 --- a/engine/src/flutter/shell/platform/windows/window_binding_handler.h +++ b/engine/src/flutter/shell/platform/windows/window_binding_handler.h @@ -106,6 +106,10 @@ class WindowBindingHandler { // Retrieve the alert node. virtual ui::AXPlatformNodeWin* GetAlert() = 0; + + // If true, rendering to the window should synchronize with the vsync + // to prevent screen tearing. + virtual bool NeedsVSync() = 0; }; } // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/windows_lifecycle_manager.cc b/engine/src/flutter/shell/platform/windows/windows_lifecycle_manager.cc index ecb14d4308..c7c46fa1e9 100644 --- a/engine/src/flutter/shell/platform/windows/windows_lifecycle_manager.cc +++ b/engine/src/flutter/shell/platform/windows/windows_lifecycle_manager.cc @@ -48,7 +48,7 @@ bool WindowsLifecycleManager::WindowProc(HWND hwnd, // send a request to the framework to see if the app should exit. If it // is, we re-dispatch a new WM_CLOSE message. In order to allow the new // message to reach other delegates, we ignore it here. - case WM_CLOSE: + case WM_CLOSE: { auto key = std::make_tuple(hwnd, wpar, lpar); auto itr = sent_close_messages_.find(key); if (itr != sent_close_messages_.end()) { @@ -65,6 +65,13 @@ bool WindowsLifecycleManager::WindowProc(HWND hwnd, return true; } break; + } + + // DWM composition can be disabled on Windows 7. + // Notify the engine as this can result in screen tearing. + case WM_DWMCOMPOSITIONCHANGED: + engine_->OnDwmCompositionChanged(); + break; } return false; } diff --git a/engine/src/flutter/shell/platform/windows/windows_lifecycle_manager.h b/engine/src/flutter/shell/platform/windows/windows_lifecycle_manager.h index e2ccfa42fc..e1d1f604c4 100644 --- a/engine/src/flutter/shell/platform/windows/windows_lifecycle_manager.h +++ b/engine/src/flutter/shell/platform/windows/windows_lifecycle_manager.h @@ -18,7 +18,8 @@ class FlutterWindowsEngine; /// A manager for lifecycle events of the top-level window. /// /// Currently handles the following events: -/// WM_CLOSE +/// 1. WM_CLOSE +/// 2. WM_DWMCOMPOSITIONCHANGED class WindowsLifecycleManager { public: WindowsLifecycleManager(FlutterWindowsEngine* engine);