From 0a6a9f29d2c02f3ffaa58ca23e219251b0dcbe1e Mon Sep 17 00:00:00 2001 From: Kaushik Iska Date: Tue, 19 Jan 2021 11:17:41 -0800 Subject: [PATCH] [windows] Enable smooth resizing on windows (flutter/engine#23701) --- .../flutter/shell/platform/windows/BUILD.gn | 2 + .../windows/flutter_windows_engine.cc | 12 +++- .../platform/windows/flutter_windows_view.cc | 59 +++++++++++++++++-- .../platform/windows/flutter_windows_view.h | 42 ++++++++++++- .../platform/windows/win32_flutter_window.cc | 7 +++ .../platform/windows/win32_flutter_window.h | 3 + .../platform/windows/window_binding_handler.h | 3 + .../windows/window_binding_handler_delegate.h | 5 +- 8 files changed, 124 insertions(+), 9 deletions(-) diff --git a/engine/src/flutter/shell/platform/windows/BUILD.gn b/engine/src/flutter/shell/platform/windows/BUILD.gn index 6baf2996ce..e9e33d6efa 100644 --- a/engine/src/flutter/shell/platform/windows/BUILD.gn +++ b/engine/src/flutter/shell/platform/windows/BUILD.gn @@ -102,6 +102,8 @@ source_set("flutter_windows_source") { "win32_window_proc_delegate_manager.cc", "win32_window_proc_delegate_manager.h", ] + + libs = [ "dwmapi.lib" ] } configs += [ 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 8fcedb2a7e..bfe169fc3a 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc @@ -50,7 +50,17 @@ FlutterRendererConfig GetRendererConfig() { } return host->view()->SwapBuffers(); }; - config.open_gl.fbo_callback = [](void* user_data) -> uint32_t { return 0; }; + config.open_gl.fbo_reset_after_present = true; + config.open_gl.fbo_with_frame_info_callback = + [](void* user_data, const FlutterFrameInfo* info) -> uint32_t { + auto host = static_cast(user_data); + if (host->view()) { + return host->view()->GetFrameBufferId(info->size.width, + info->size.height); + } else { + return kWindowFrameBufferID; + } + }; config.open_gl.gl_proc_resolver = [](void* user_data, const char* what) -> void* { return reinterpret_cast(eglGetProcAddress(what)); 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 bcac661aa1..1f3cc8e649 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_view.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_view.cc @@ -49,10 +49,41 @@ void FlutterWindowsView::SetEngine( binding_handler_->GetDpiScale()); } -void FlutterWindowsView::OnWindowSizeChanged(size_t width, - size_t height) const { - surface_manager_->ResizeSurface(GetRenderTarget(), width, height); +uint32_t FlutterWindowsView::GetFrameBufferId(size_t width, size_t height) { + // Called on an engine-controlled (non-platform) thread. + std::unique_lock lock(resize_mutex_); + + if (resize_status_ != ResizeState::kResizeStarted) { + return kWindowFrameBufferID; + } + + 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. + surface_manager_->ResizeSurface(GetRenderTarget(), width, height); + surface_manager_->MakeCurrent(); + resize_status_ = ResizeState::kFrameGenerated; + } + + return kWindowFrameBufferID; +} + +void FlutterWindowsView::OnWindowSizeChanged(size_t width, size_t height) { + // Called on the platform thread. + std::unique_lock lock(resize_mutex_); + resize_status_ = ResizeState::kResizeStarted; + resize_target_width_ = width; + resize_target_height_ = height; SendWindowMetrics(width, height, binding_handler_->GetDpiScale()); + + if (width > 0 && height > 0) { + // Block the platform thread until: + // 1. GetFrameBufferId is called with the right frame size. + // 2. Any pending SwapBuffers calls have been invoked. + resize_cv_.wait(lock, [&resize_status = resize_status_] { + return resize_status == ResizeState::kDone; + }); + } } void FlutterWindowsView::OnPointerMove(double x, double y) { @@ -255,7 +286,27 @@ bool FlutterWindowsView::ClearContext() { } bool FlutterWindowsView::SwapBuffers() { - return surface_manager_->SwapBuffers(); + // Called on an engine-controlled (non-platform) thread. + std::unique_lock lock(resize_mutex_); + + switch (resize_status_) { + // SwapBuffer requests during resize are ignored until the frame with the + // right dimensions has been generated. This is marked with + // kFrameGenerated resize status. + case ResizeState::kResizeStarted: + return false; + case ResizeState::kFrameGenerated: { + bool swap_buffers_result = surface_manager_->SwapBuffers(); + resize_status_ = ResizeState::kDone; + lock.unlock(); + resize_cv_.notify_all(); + binding_handler_->OnWindowResized(); + return swap_buffers_result; + } + case ResizeState::kDone: + default: + return surface_manager_->SwapBuffers(); + } } void FlutterWindowsView::CreateRenderSurface() { 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 4863ef7b1a..6f12d1627f 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_view.h +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_view.h @@ -8,6 +8,7 @@ #include #include +#include #include #include @@ -27,6 +28,9 @@ namespace flutter { +// ID for the window frame buffer. +inline constexpr uint32_t kWindowFrameBufferID = 0; + // An OS-windowing neutral abstration for flutter // view that works with win32 hwnds and Windows::UI::Composition visuals. class FlutterWindowsView : public WindowBindingHandlerDelegate { @@ -57,7 +61,8 @@ class FlutterWindowsView : public WindowBindingHandlerDelegate { // Returns the engine backing this view. FlutterWindowsEngine* GetEngine(); - // Callbacks for clearing context, settings context and swapping buffers. + // Callbacks for clearing context, settings context and swapping buffers, + // these are typically called on an engine-controlled (non-platform) thread. bool ClearContext(); bool MakeCurrent(); bool MakeResourceCurrent(); @@ -66,8 +71,11 @@ class FlutterWindowsView : public WindowBindingHandlerDelegate { // Send initial bounds to embedder. Must occur after engine has initialized. void SendInitialBounds(); + // Returns the frame buffer id for the engine to render to. + uint32_t GetFrameBufferId(size_t width, size_t height); + // |WindowBindingHandlerDelegate| - void OnWindowSizeChanged(size_t width, size_t height) const override; + void OnWindowSizeChanged(size_t width, size_t height) override; // |WindowBindingHandlerDelegate| void OnPointerMove(double x, double y) override; @@ -115,6 +123,19 @@ class FlutterWindowsView : public WindowBindingHandlerDelegate { uint64_t buttons = 0; }; + // States a resize event can be in. + enum class ResizeState { + // When a resize event has started but is in progress. + kResizeStarted, + // After a resize event starts and the framework has been notified to + // generate a frame for the right size. + kFrameGenerated, + // Default state for when no resize is in progress. Also used to indicate + // that during a resize event, a frame with the right size has been rendered + // and the buffers have been swapped. + kDone, + }; + // Sends a window metrics update to the Flutter engine using current window // dimensions in physical void SendWindowMetrics(size_t width, size_t height, double dpiscale) const; @@ -206,6 +227,23 @@ class FlutterWindowsView : public WindowBindingHandlerDelegate { // Currently configured WindowBindingHandler for view. std::unique_ptr binding_handler_; + + // Resize events are synchronized using this mutex and the corresponding + // condition variable. + std::mutex resize_mutex_; + std::condition_variable resize_cv_; + + // Indicates the state of a window resize event. Platform thread will be + // blocked while this is not done. Guarded by resize_mutex_. + ResizeState resize_status_ = ResizeState::kDone; + + // Target for the window width. Valid when resize_pending_ is set. Guarded by + // resize_mutex_. + size_t resize_target_width_ = 0; + + // Target for the window width. Valid when resize_pending_ is set. Guarded by + // resize_mutex_. + size_t resize_target_height_ = 0; }; } // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/win32_flutter_window.cc b/engine/src/flutter/shell/platform/windows/win32_flutter_window.cc index 5f4c92aa98..249bd405f5 100644 --- a/engine/src/flutter/shell/platform/windows/win32_flutter_window.cc +++ b/engine/src/flutter/shell/platform/windows/win32_flutter_window.cc @@ -4,6 +4,7 @@ #include "flutter/shell/platform/windows/win32_flutter_window.h" +#include #include #include @@ -92,6 +93,12 @@ void Win32FlutterWindow::UpdateFlutterCursor(const std::string& cursor_name) { current_cursor_ = GetCursorByName(cursor_name); } +void Win32FlutterWindow::OnWindowResized() { + // Blocking the raster thread until DWM flushes alleviates glitches where + // previous size surface is stretched over current size view. + DwmFlush(); +} + // Translates button codes from Win32 API to FlutterPointerMouseButtons. static uint64_t ConvertWinButtonToFlutterButton(UINT button) { switch (button) { diff --git a/engine/src/flutter/shell/platform/windows/win32_flutter_window.h b/engine/src/flutter/shell/platform/windows/win32_flutter_window.h index 6d26bcf6bf..ca745b7fee 100644 --- a/engine/src/flutter/shell/platform/windows/win32_flutter_window.h +++ b/engine/src/flutter/shell/platform/windows/win32_flutter_window.h @@ -74,6 +74,9 @@ class Win32FlutterWindow : public Win32Window, public WindowBindingHandler { // |FlutterWindowBindingHandler| void UpdateFlutterCursor(const std::string& cursor_name) override; + // |FlutterWindowBindingHandler| + void OnWindowResized() override; + private: // A pointer to a FlutterWindowsView that can be used to update engine // windowing and input state. 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 50eba05877..678bf27466 100644 --- a/engine/src/flutter/shell/platform/windows/window_binding_handler.h +++ b/engine/src/flutter/shell/platform/windows/window_binding_handler.h @@ -45,6 +45,9 @@ class WindowBindingHandler { // Returns the bounds of the backing window in physical pixels. virtual PhysicalWindowBounds GetPhysicalWindowBounds() = 0; + // Invoked after the window has been resized. + virtual void OnWindowResized() = 0; + // Sets the cursor that should be used when the mouse is over the Flutter // content. See mouse_cursor.dart for the values and meanings of cursor_name. virtual void UpdateFlutterCursor(const std::string& cursor_name) = 0; diff --git a/engine/src/flutter/shell/platform/windows/window_binding_handler_delegate.h b/engine/src/flutter/shell/platform/windows/window_binding_handler_delegate.h index 30bc5b3111..595149a2ce 100644 --- a/engine/src/flutter/shell/platform/windows/window_binding_handler_delegate.h +++ b/engine/src/flutter/shell/platform/windows/window_binding_handler_delegate.h @@ -12,8 +12,9 @@ namespace flutter { class WindowBindingHandlerDelegate { public: // Notifies delegate that backing window size has changed. - // Typically called by currently configured WindowBindingHandler - virtual void OnWindowSizeChanged(size_t width, size_t height) const = 0; + // Typically called by currently configured WindowBindingHandler, this is + // called on the platform thread. + virtual void OnWindowSizeChanged(size_t width, size_t height) = 0; // Notifies delegate that backing window mouse has moved. // Typically called by currently configured WindowBindingHandler