[Windows] Refactor logic when window resize completes (flutter/engine#49872)
Changes: 1. Moves surface buffer swapping from `FlutterWindowsView` to `CompositorOpenGL` 1. Renames `FlutterWindowsView::SwapBuffers` to `FlutterWindowsView::OnFramePresented` 2. Previously, if a resize was pending and the window was not visible Windows would unblock the platform thread before swapping buffers. This trick aimed to reduce the time the platform thread was blocked as swapping buffers previously waited until the v-blank. This logic was removed as swapping buffers no longer waits until the v-blank. [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
@@ -329,7 +329,7 @@ bool AngleSurfaceManager::MakeResourceCurrent() {
|
||||
egl_resource_context_) == EGL_TRUE);
|
||||
}
|
||||
|
||||
EGLBoolean AngleSurfaceManager::SwapBuffers() {
|
||||
bool AngleSurfaceManager::SwapBuffers() {
|
||||
return (eglSwapBuffers(egl_display_, render_surface_));
|
||||
}
|
||||
|
||||
|
||||
@@ -77,7 +77,7 @@ class AngleSurfaceManager {
|
||||
|
||||
// Swaps the front and back buffers of the DX11 swapchain backing surface if
|
||||
// not null.
|
||||
EGLBoolean SwapBuffers();
|
||||
virtual bool SwapBuffers();
|
||||
|
||||
// Creates a |EGLSurface| from the provided handle.
|
||||
EGLSurface CreateSurfaceFromHandle(EGLenum handle_type,
|
||||
|
||||
@@ -154,7 +154,12 @@ bool CompositorOpenGL::Present(const FlutterLayer** layers,
|
||||
GL_NEAREST // filter
|
||||
);
|
||||
|
||||
return engine_->view()->SwapBuffers();
|
||||
if (!engine_->surface_manager()->SwapBuffers()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
engine_->view()->OnFramePresented();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CompositorOpenGL::Initialize() {
|
||||
@@ -188,7 +193,12 @@ bool CompositorOpenGL::ClearSurface() {
|
||||
gl_->ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
gl_->Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
|
||||
|
||||
return engine_->view()->SwapBuffers();
|
||||
if (!engine_->surface_manager()->SwapBuffers()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
engine_->view()->OnFramePresented();
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace flutter
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "flutter/shell/platform/windows/flutter_windows_view.h"
|
||||
#include "flutter/shell/platform/windows/testing/engine_modifier.h"
|
||||
#include "flutter/shell/platform/windows/testing/flutter_windows_engine_builder.h"
|
||||
#include "flutter/shell/platform/windows/testing/mock_angle_surface_manager.h"
|
||||
#include "flutter/shell/platform/windows/testing/mock_window_binding_handler.h"
|
||||
#include "flutter/shell/platform/windows/testing/windows_test.h"
|
||||
#include "gmock/gmock.h"
|
||||
@@ -56,28 +57,6 @@ const impeller::ProcTableGLES::Resolver kMockResolver = [](const char* name) {
|
||||
}
|
||||
};
|
||||
|
||||
class MockAngleSurfaceManager : public AngleSurfaceManager {
|
||||
public:
|
||||
MockAngleSurfaceManager() : AngleSurfaceManager(false) {}
|
||||
|
||||
MOCK_METHOD(bool, MakeCurrent, (), (override));
|
||||
|
||||
private:
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(MockAngleSurfaceManager);
|
||||
};
|
||||
|
||||
class MockFlutterWindowsView : public FlutterWindowsView {
|
||||
public:
|
||||
MockFlutterWindowsView(std::unique_ptr<WindowBindingHandler> window)
|
||||
: FlutterWindowsView(std::move(window)) {}
|
||||
virtual ~MockFlutterWindowsView() = default;
|
||||
|
||||
MOCK_METHOD(bool, SwapBuffers, (), (override));
|
||||
|
||||
private:
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(MockFlutterWindowsView);
|
||||
};
|
||||
|
||||
class CompositorOpenGLTest : public WindowsTest {
|
||||
public:
|
||||
CompositorOpenGLTest() = default;
|
||||
@@ -85,7 +64,6 @@ class CompositorOpenGLTest : public WindowsTest {
|
||||
|
||||
protected:
|
||||
FlutterWindowsEngine* engine() { return engine_.get(); }
|
||||
MockFlutterWindowsView* view() { return view_.get(); }
|
||||
MockAngleSurfaceManager* surface_manager() { return surface_manager_; }
|
||||
|
||||
void UseHeadlessEngine() {
|
||||
@@ -106,14 +84,14 @@ class CompositorOpenGLTest : public WindowsTest {
|
||||
EXPECT_CALL(*window.get(), SetView).Times(1);
|
||||
EXPECT_CALL(*window.get(), GetWindowHandle).WillRepeatedly(Return(nullptr));
|
||||
|
||||
view_ = std::make_unique<MockFlutterWindowsView>(std::move(window));
|
||||
view_ = std::make_unique<FlutterWindowsView>(std::move(window));
|
||||
|
||||
engine_->SetView(view_.get());
|
||||
}
|
||||
|
||||
private:
|
||||
std::unique_ptr<FlutterWindowsEngine> engine_;
|
||||
std::unique_ptr<MockFlutterWindowsView> view_;
|
||||
std::unique_ptr<FlutterWindowsView> view_;
|
||||
MockAngleSurfaceManager* surface_manager_;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(CompositorOpenGLTest);
|
||||
@@ -163,7 +141,7 @@ TEST_F(CompositorOpenGLTest, Present) {
|
||||
const FlutterLayer* layer_ptr = &layer;
|
||||
|
||||
EXPECT_CALL(*surface_manager(), MakeCurrent).WillOnce(Return(true));
|
||||
EXPECT_CALL(*view(), SwapBuffers).WillOnce(Return(true));
|
||||
EXPECT_CALL(*surface_manager(), SwapBuffers).WillOnce(Return(true));
|
||||
EXPECT_TRUE(compositor.Present(&layer_ptr, 1));
|
||||
|
||||
ASSERT_TRUE(compositor.CollectBackingStore(&backing_store));
|
||||
@@ -179,7 +157,7 @@ TEST_F(CompositorOpenGLTest, PresentEmpty) {
|
||||
EXPECT_CALL(*surface_manager(), MakeCurrent)
|
||||
.Times(2)
|
||||
.WillRepeatedly(Return(true));
|
||||
EXPECT_CALL(*view(), SwapBuffers).WillOnce(Return(true));
|
||||
EXPECT_CALL(*surface_manager(), SwapBuffers).WillOnce(Return(true));
|
||||
EXPECT_TRUE(compositor.Present(nullptr, 0));
|
||||
}
|
||||
|
||||
|
||||
@@ -168,10 +168,6 @@ float FlutterWindow::GetDpiScale() {
|
||||
return static_cast<float>(GetCurrentDPI()) / static_cast<float>(base_dpi);
|
||||
}
|
||||
|
||||
bool FlutterWindow::IsVisible() {
|
||||
return IsWindowVisible(GetWindowHandle());
|
||||
}
|
||||
|
||||
PhysicalWindowBounds FlutterWindow::GetPhysicalWindowBounds() {
|
||||
return {GetCurrentWidth(), GetCurrentHeight()};
|
||||
}
|
||||
|
||||
@@ -158,9 +158,6 @@ class FlutterWindow : public KeyboardManager::WindowDelegate,
|
||||
// |FlutterWindowBindingHandler|
|
||||
virtual float GetDpiScale() override;
|
||||
|
||||
// |FlutterWindowBindingHandler|
|
||||
virtual bool IsVisible() override;
|
||||
|
||||
// |FlutterWindowBindingHandler|
|
||||
virtual PhysicalWindowBounds GetPhysicalWindowBounds() override;
|
||||
|
||||
|
||||
@@ -66,7 +66,6 @@ class MockFlutterWindow : public FlutterWindow {
|
||||
MOCK_METHOD(void, OnSetCursor, (), (override));
|
||||
MOCK_METHOD(float, GetScrollOffsetMultiplier, (), (override));
|
||||
MOCK_METHOD(float, GetDpiScale, (), (override));
|
||||
MOCK_METHOD(bool, IsVisible, (), (override));
|
||||
MOCK_METHOD(void, UpdateCursorRect, (const Rect&), (override));
|
||||
MOCK_METHOD(void, OnResetImeComposing, (), (override));
|
||||
MOCK_METHOD(UINT, Win32DispatchMessage, (UINT, WPARAM, LPARAM), (override));
|
||||
|
||||
@@ -590,26 +590,20 @@ void FlutterWindowsView::SendPointerEventWithData(
|
||||
}
|
||||
}
|
||||
|
||||
bool FlutterWindowsView::SwapBuffers() {
|
||||
// Called on an engine-controlled (non-platform) thread.
|
||||
void FlutterWindowsView::OnFramePresented() {
|
||||
// Called on the engine's raster thread.
|
||||
std::unique_lock<std::mutex> 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;
|
||||
// The caller must first call |OnFrameGenerated| or
|
||||
// |OnEmptyFrameGenerated| before calling this method. This status
|
||||
// indicates the caller did not call these methods or ignored their
|
||||
// result.
|
||||
FML_UNREACHABLE();
|
||||
case ResizeState::kFrameGenerated: {
|
||||
bool visible = binding_handler_->IsVisible();
|
||||
bool swap_buffers_result;
|
||||
// For visible windows swap the buffers while resize handler is waiting.
|
||||
// For invisible windows unblock the handler first and then swap buffers.
|
||||
// SwapBuffers waits for vsync and there's no point doing that for
|
||||
// invisible windows.
|
||||
if (visible) {
|
||||
swap_buffers_result = engine_->surface_manager()->SwapBuffers();
|
||||
}
|
||||
// A frame was generated for a pending resize.
|
||||
// Unblock the platform thread.
|
||||
resize_status_ = ResizeState::kDone;
|
||||
lock.unlock();
|
||||
resize_cv_.notify_all();
|
||||
@@ -617,15 +611,9 @@ bool FlutterWindowsView::SwapBuffers() {
|
||||
// Blocking the raster thread until DWM flushes alleviates glitches where
|
||||
// previous size surface is stretched over current size view.
|
||||
windows_proc_table_->DwmFlush();
|
||||
|
||||
if (!visible) {
|
||||
swap_buffers_result = engine_->surface_manager()->SwapBuffers();
|
||||
}
|
||||
return swap_buffers_result;
|
||||
}
|
||||
case ResizeState::kDone:
|
||||
default:
|
||||
return engine_->surface_manager()->SwapBuffers();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -62,17 +62,6 @@ class FlutterWindowsView : public WindowBindingHandlerDelegate {
|
||||
// Tells the engine to generate a new frame
|
||||
void ForceRedraw();
|
||||
|
||||
// Swap the view's surface buffers. Must be called on the engine's raster
|
||||
// thread. Returns true if the buffers were swapped.
|
||||
//
|
||||
// |OnFrameGenerated| or |OnEmptyFrameGenerated| must be called before this
|
||||
// method.
|
||||
//
|
||||
// If the view is resizing, this returns false if the frame is not the target
|
||||
// size. Otherwise, it unblocks the platform thread and blocks the raster
|
||||
// thread until the v-blank.
|
||||
virtual bool SwapBuffers();
|
||||
|
||||
// Callback to clear a previously presented software bitmap.
|
||||
virtual bool ClearSoftwareBitmap();
|
||||
|
||||
@@ -103,6 +92,11 @@ class FlutterWindowsView : public WindowBindingHandlerDelegate {
|
||||
// |height| match the target size.
|
||||
bool OnFrameGenerated(size_t width, size_t height);
|
||||
|
||||
// Called on the raster thread after |CompositorOpenGL| presents a frame.
|
||||
//
|
||||
// This completes a view resize if one is pending.
|
||||
virtual void OnFramePresented();
|
||||
|
||||
// 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.
|
||||
void UpdateFlutterCursor(const std::string& cursor_name);
|
||||
|
||||
@@ -25,6 +25,8 @@ class MockAngleSurfaceManager : public AngleSurfaceManager {
|
||||
MOCK_METHOD(bool, ClearCurrent, (), (override));
|
||||
MOCK_METHOD(void, SetVSyncEnabled, (bool), (override));
|
||||
|
||||
MOCK_METHOD(bool, SwapBuffers, (), (override));
|
||||
|
||||
private:
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(MockAngleSurfaceManager);
|
||||
};
|
||||
|
||||
@@ -22,7 +22,6 @@ class MockWindowBindingHandler : public WindowBindingHandler {
|
||||
MOCK_METHOD(void, SetView, (WindowBindingHandlerDelegate * view), (override));
|
||||
MOCK_METHOD(HWND, GetWindowHandle, (), (override));
|
||||
MOCK_METHOD(float, GetDpiScale, (), (override));
|
||||
MOCK_METHOD(bool, IsVisible, (), (override));
|
||||
MOCK_METHOD(PhysicalWindowBounds, GetPhysicalWindowBounds, (), (override));
|
||||
MOCK_METHOD(void,
|
||||
UpdateFlutterCursor,
|
||||
|
||||
@@ -51,9 +51,6 @@ class WindowBindingHandler {
|
||||
// Returns the scale factor for the backing window.
|
||||
virtual float GetDpiScale() = 0;
|
||||
|
||||
// Returns whether the HWND is currently visible.
|
||||
virtual bool IsVisible() = 0;
|
||||
|
||||
// Returns the bounds of the backing window in physical pixels.
|
||||
virtual PhysicalWindowBounds GetPhysicalWindowBounds() = 0;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user