Reland "[Windows] Introduce egl::Surface and egl::WindowSurface" (flutter/engine#50148)

# Original pull request description

This introduces the `egl::Surface` and `egl::WindowSurface` types to abstract a raw `EGLSurface`. This also removes some - but not all - EGL surface logic out from `EGLManager`.

Subsequent pull requests will be necessary to:

1. Move ownership of the `egl::WindowSurface` from `egl::Manager` to `FlutterWindowsView`
2. Refactor external texture's off-screen EGL surface to use `egl::Surface`

Part of https://github.com/flutter/flutter/issues/141996

# Reland

https://github.com/flutter/engine/pull/49983 was reverted as it introduced a crash if the render surface fails to be created even though EGL was initialized successfully.

This pull request is split into the following commits:

1. c0b11be79f is the original pull request unchanged
2. 1dc7813845 is the fix: it checks a surface is valid before using it. This also adds several tests to prevent this kind of regression.

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
Loïc Sharma
2024-02-01 09:08:15 -08:00
committed by GitHub
parent 110583e248
commit 5251e0048f
18 changed files with 653 additions and 230 deletions

View File

@@ -7252,6 +7252,10 @@ ORIGIN: ../../../flutter/shell/platform/windows/egl/manager.cc + ../../../flutte
ORIGIN: ../../../flutter/shell/platform/windows/egl/manager.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/windows/egl/proc_table.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/windows/egl/proc_table.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/windows/egl/surface.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/windows/egl/surface.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/windows/egl/window_surface.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/windows/egl/window_surface.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/windows/event_watcher.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/windows/event_watcher.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/windows/external_texture.h + ../../../flutter/LICENSE
@@ -10137,6 +10141,10 @@ FILE: ../../../flutter/shell/platform/windows/egl/manager.cc
FILE: ../../../flutter/shell/platform/windows/egl/manager.h
FILE: ../../../flutter/shell/platform/windows/egl/proc_table.cc
FILE: ../../../flutter/shell/platform/windows/egl/proc_table.h
FILE: ../../../flutter/shell/platform/windows/egl/surface.cc
FILE: ../../../flutter/shell/platform/windows/egl/surface.h
FILE: ../../../flutter/shell/platform/windows/egl/window_surface.cc
FILE: ../../../flutter/shell/platform/windows/egl/window_surface.h
FILE: ../../../flutter/shell/platform/windows/event_watcher.cc
FILE: ../../../flutter/shell/platform/windows/event_watcher.h
FILE: ../../../flutter/shell/platform/windows/external_texture.h

View File

@@ -59,6 +59,10 @@ source_set("flutter_windows_source") {
"egl/manager.h",
"egl/proc_table.cc",
"egl/proc_table.h",
"egl/surface.cc",
"egl/surface.h",
"egl/window_surface.cc",
"egl/window_surface.h",
"event_watcher.cc",
"event_watcher.h",
"external_texture.h",
@@ -209,6 +213,7 @@ executable("flutter_windows_unittests") {
"testing/egl/mock_context.h",
"testing/egl/mock_manager.h",
"testing/egl/mock_proc_table.h",
"testing/egl/mock_window_surface.h",
"testing/engine_modifier.h",
"testing/flutter_window_test.cc",
"testing/flutter_window_test.h",

View File

@@ -97,6 +97,11 @@ bool CompositorOpenGL::Present(const FlutterLayer** layers,
return false;
}
if (!engine_->egl_manager()->surface() ||
!engine_->egl_manager()->surface()->IsValid()) {
return false;
}
// Clear the view if there are no layers to present.
if (layers_count == 0) {
// Normally the compositor is initialized when the first backing store is
@@ -128,7 +133,7 @@ bool CompositorOpenGL::Present(const FlutterLayer** layers,
return false;
}
if (!engine_->egl_manager()->MakeCurrent()) {
if (!engine_->egl_manager()->surface()->MakeCurrent()) {
return false;
}
@@ -154,7 +159,7 @@ bool CompositorOpenGL::Present(const FlutterLayer** layers,
GL_NEAREST // filter
);
if (!engine_->egl_manager()->SwapBuffers()) {
if (!engine_->egl_manager()->surface()->SwapBuffers()) {
return false;
}
@@ -165,7 +170,17 @@ bool CompositorOpenGL::Present(const FlutterLayer** layers,
bool CompositorOpenGL::Initialize() {
FML_DCHECK(!is_initialized_);
if (!engine_->egl_manager()->MakeCurrent()) {
egl::Manager* manager = engine_->egl_manager();
if (!manager) {
return false;
}
egl::Surface* surface = manager->surface();
if (!surface || !surface->IsValid()) {
return false;
}
if (!surface->MakeCurrent()) {
return false;
}
@@ -186,14 +201,14 @@ bool CompositorOpenGL::ClearSurface() {
// Resize the surface if needed.
engine_->view()->OnEmptyFrameGenerated();
if (!engine_->egl_manager()->MakeCurrent()) {
if (!engine_->egl_manager()->surface()->MakeCurrent()) {
return false;
}
gl_->ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl_->Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
if (!engine_->egl_manager()->SwapBuffers()) {
if (!engine_->egl_manager()->surface()->SwapBuffers()) {
return false;
}

View File

@@ -10,6 +10,7 @@
#include "flutter/shell/platform/windows/egl/manager.h"
#include "flutter/shell/platform/windows/flutter_windows_view.h"
#include "flutter/shell/platform/windows/testing/egl/mock_manager.h"
#include "flutter/shell/platform/windows/testing/egl/mock_window_surface.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_window_binding_handler.h"
@@ -65,11 +66,20 @@ class CompositorOpenGLTest : public WindowsTest {
protected:
FlutterWindowsEngine* engine() { return engine_.get(); }
egl::MockManager* egl_manager() { return egl_manager_; }
egl::MockWindowSurface* surface() { return surface_.get(); }
void UseHeadlessEngine() {
void UseHeadlessEngine(bool add_surface = true) {
auto egl_manager = std::make_unique<egl::MockManager>();
egl_manager_ = egl_manager.get();
if (add_surface) {
surface_ = std::make_unique<egl::MockWindowSurface>();
EXPECT_CALL(*egl_manager_, surface)
.WillRepeatedly(Return(surface_.get()));
} else {
EXPECT_CALL(*egl_manager_, surface).WillRepeatedly(Return(nullptr));
}
FlutterWindowsEngineBuilder builder{GetContext()};
engine_ = builder.Build();
@@ -77,8 +87,8 @@ class CompositorOpenGLTest : public WindowsTest {
modifier.SetEGLManager(std::move(egl_manager));
}
void UseEngineWithView() {
UseHeadlessEngine();
void UseEngineWithView(bool add_surface = true) {
UseHeadlessEngine(add_surface);
auto window = std::make_unique<MockWindowBindingHandler>();
EXPECT_CALL(*window.get(), SetView).Times(1);
@@ -92,6 +102,7 @@ class CompositorOpenGLTest : public WindowsTest {
private:
std::unique_ptr<FlutterWindowsEngine> engine_;
std::unique_ptr<FlutterWindowsView> view_;
std::unique_ptr<egl::MockWindowSurface> surface_;
egl::MockManager* egl_manager_;
FML_DISALLOW_COPY_AND_ASSIGN(CompositorOpenGLTest);
@@ -107,7 +118,8 @@ TEST_F(CompositorOpenGLTest, CreateBackingStore) {
FlutterBackingStoreConfig config = {};
FlutterBackingStore backing_store = {};
EXPECT_CALL(*egl_manager(), MakeCurrent).WillOnce(Return(true));
EXPECT_CALL(*surface(), IsValid).WillOnce(Return(true));
EXPECT_CALL(*surface(), MakeCurrent).WillOnce(Return(true));
ASSERT_TRUE(compositor.CreateBackingStore(config, &backing_store));
ASSERT_TRUE(compositor.CollectBackingStore(&backing_store));
}
@@ -120,7 +132,8 @@ TEST_F(CompositorOpenGLTest, InitializationFailure) {
FlutterBackingStoreConfig config = {};
FlutterBackingStore backing_store = {};
EXPECT_CALL(*egl_manager(), MakeCurrent).WillOnce(Return(false));
EXPECT_CALL(*surface(), IsValid).WillOnce(Return(true));
EXPECT_CALL(*surface(), MakeCurrent).WillOnce(Return(false));
EXPECT_FALSE(compositor.CreateBackingStore(config, &backing_store));
}
@@ -132,7 +145,8 @@ TEST_F(CompositorOpenGLTest, Present) {
FlutterBackingStoreConfig config = {};
FlutterBackingStore backing_store = {};
EXPECT_CALL(*egl_manager(), MakeCurrent).WillOnce(Return(true));
EXPECT_CALL(*surface(), IsValid).WillRepeatedly(Return(true));
EXPECT_CALL(*surface(), MakeCurrent).WillOnce(Return(true));
ASSERT_TRUE(compositor.CreateBackingStore(config, &backing_store));
FlutterLayer layer = {};
@@ -140,8 +154,8 @@ TEST_F(CompositorOpenGLTest, Present) {
layer.backing_store = &backing_store;
const FlutterLayer* layer_ptr = &layer;
EXPECT_CALL(*egl_manager(), MakeCurrent).WillOnce(Return(true));
EXPECT_CALL(*egl_manager(), SwapBuffers).WillOnce(Return(true));
EXPECT_CALL(*surface(), MakeCurrent).WillOnce(Return(true));
EXPECT_CALL(*surface(), SwapBuffers).WillOnce(Return(true));
EXPECT_TRUE(compositor.Present(&layer_ptr, 1));
ASSERT_TRUE(compositor.CollectBackingStore(&backing_store));
@@ -154,10 +168,9 @@ TEST_F(CompositorOpenGLTest, PresentEmpty) {
// The context will be bound twice: first to initialize the compositor, second
// to clear the surface.
EXPECT_CALL(*egl_manager(), MakeCurrent)
.Times(2)
.WillRepeatedly(Return(true));
EXPECT_CALL(*egl_manager(), SwapBuffers).WillOnce(Return(true));
EXPECT_CALL(*surface(), IsValid).WillRepeatedly(Return(true));
EXPECT_CALL(*surface(), MakeCurrent).Times(2).WillRepeatedly(Return(true));
EXPECT_CALL(*surface(), SwapBuffers).WillOnce(Return(true));
EXPECT_TRUE(compositor.Present(nullptr, 0));
}
@@ -169,7 +182,8 @@ TEST_F(CompositorOpenGLTest, HeadlessPresentIgnored) {
FlutterBackingStoreConfig config = {};
FlutterBackingStore backing_store = {};
EXPECT_CALL(*egl_manager(), MakeCurrent).WillOnce(Return(true));
EXPECT_CALL(*surface(), IsValid).WillOnce(Return(true));
EXPECT_CALL(*surface(), MakeCurrent).WillOnce(Return(true));
ASSERT_TRUE(compositor.CreateBackingStore(config, &backing_store));
FlutterLayer layer = {};
@@ -182,5 +196,23 @@ TEST_F(CompositorOpenGLTest, HeadlessPresentIgnored) {
ASSERT_TRUE(compositor.CollectBackingStore(&backing_store));
}
TEST_F(CompositorOpenGLTest, NoSurfaceIgnored) {
UseEngineWithView(/*add_surface = */ false);
auto compositor = CompositorOpenGL{engine(), kMockResolver};
FlutterBackingStoreConfig config = {};
FlutterBackingStore backing_store = {};
ASSERT_FALSE(compositor.CreateBackingStore(config, &backing_store));
FlutterLayer layer = {};
layer.type = kFlutterLayerContentTypeBackingStore;
layer.backing_store = nullptr;
const FlutterLayer* layer_ptr = &layer;
EXPECT_FALSE(compositor.Present(&layer_ptr, 1));
}
} // namespace testing
} // namespace flutter

View File

@@ -14,7 +14,7 @@ namespace egl {
// An EGL context to interact with OpenGL.
//
// This enables automatic eror logging and mocking.
// This enables automatic error logging and mocking.
//
// Flutter Windows uses this to create render and resource contexts.
class Context {

View File

@@ -263,92 +263,72 @@ bool Manager::IsValid() const {
return is_valid_;
}
bool Manager::CreateSurface(HWND hwnd, EGLint width, EGLint height) {
bool Manager::CreateWindowSurface(HWND hwnd, size_t width, size_t height) {
FML_DCHECK(surface_ == nullptr || !surface_->IsValid());
if (!hwnd || !is_valid_) {
return false;
}
EGLSurface surface = EGL_NO_SURFACE;
// Disable ANGLE's automatic surface resizing and provide an explicit size.
// The surface will need to be destroyed and re-created if the HWND is
// resized.
const EGLint surface_attributes[] = {
EGL_FIXED_SIZE_ANGLE, EGL_TRUE, EGL_WIDTH, width,
EGL_HEIGHT, height, EGL_NONE};
const EGLint surface_attributes[] = {EGL_FIXED_SIZE_ANGLE,
EGL_TRUE,
EGL_WIDTH,
static_cast<EGLint>(width),
EGL_HEIGHT,
static_cast<EGLint>(height),
EGL_NONE};
surface = ::eglCreateWindowSurface(display_, config_,
static_cast<EGLNativeWindowType>(hwnd),
surface_attributes);
auto const surface = ::eglCreateWindowSurface(
display_, config_, static_cast<EGLNativeWindowType>(hwnd),
surface_attributes);
if (surface == EGL_NO_SURFACE) {
LogEGLError("Surface creation failed.");
return false;
}
surface_width_ = width;
surface_height_ = height;
surface_ = surface;
surface_ = std::make_unique<WindowSurface>(
display_, render_context_->GetHandle(), surface, width, height);
return true;
}
void Manager::ResizeSurface(HWND hwnd,
EGLint width,
EGLint height,
bool vsync_enabled) {
EGLint existing_width, existing_height;
GetSurfaceDimensions(&existing_width, &existing_height);
if (width != existing_width || height != existing_height) {
surface_width_ = width;
surface_height_ = height;
void Manager::ResizeWindowSurface(HWND hwnd, size_t width, size_t height) {
FML_CHECK(surface_ != nullptr);
auto const existing_width = surface_->width();
auto const existing_height = surface_->height();
auto const existing_vsync = surface_->vsync_enabled();
if (width != existing_width || height != existing_height) {
// TODO: Destroying the surface and re-creating it is expensive.
// Ideally this would use ANGLE's automatic surface sizing instead.
// See: https://github.com/flutter/flutter/issues/79427
render_context_->ClearCurrent();
DestroySurface();
if (!CreateSurface(hwnd, width, height)) {
if (!surface_->Destroy()) {
FML_LOG(ERROR) << "Manager::ResizeSurface failed to destroy surface";
return;
}
if (!CreateWindowSurface(hwnd, width, height)) {
FML_LOG(ERROR) << "Manager::ResizeSurface failed to create surface";
return;
}
if (!surface_->MakeCurrent() ||
!surface_->SetVSyncEnabled(existing_vsync)) {
// Surfaces block until the v-blank by default.
// Failing to update the vsync might result in unnecessary blocking.
// This regresses performance but not correctness.
FML_LOG(ERROR) << "Manager::ResizeSurface failed to set vsync";
}
}
SetVSyncEnabled(vsync_enabled);
}
void Manager::GetSurfaceDimensions(EGLint* width, EGLint* height) {
if (surface_ == EGL_NO_SURFACE || !is_valid_) {
*width = 0;
*height = 0;
return;
}
// This avoids eglQuerySurface as ideally surfaces would be automatically
// sized by ANGLE to avoid expensive surface destroy & re-create. With
// automatic sizing, ANGLE could resize the surface before Flutter asks it to,
// which would break resize redraw synchronization.
*width = surface_width_;
*height = surface_height_;
}
void Manager::DestroySurface() {
if (display_ != EGL_NO_DISPLAY && surface_ != EGL_NO_SURFACE) {
::eglDestroySurface(display_, surface_);
}
surface_ = EGL_NO_SURFACE;
}
bool Manager::HasContextCurrent() {
return ::eglGetCurrentContext() != EGL_NO_CONTEXT;
}
bool Manager::MakeCurrent() {
return (::eglMakeCurrent(display_, surface_, surface_,
render_context_->GetHandle()) == EGL_TRUE);
}
bool Manager::SwapBuffers() {
return (::eglSwapBuffers(display_, surface_));
}
EGLSurface Manager::CreateSurfaceFromHandle(EGLenum handle_type,
EGLClientBuffer handle,
const EGLint* attributes) const {
@@ -356,23 +336,6 @@ EGLSurface Manager::CreateSurfaceFromHandle(EGLenum handle_type,
config_, attributes);
}
void Manager::SetVSyncEnabled(bool enabled) {
if (!MakeCurrent()) {
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(display_, enabled ? 1 : 0) != EGL_TRUE) {
LogEGLError("Unable to update the swap interval");
return;
}
}
bool Manager::GetDevice(ID3D11Device** device) {
if (!resolved_device_) {
if (!InitializeDevice()) {
@@ -392,5 +355,9 @@ Context* Manager::resource_context() const {
return resource_context_.get();
}
WindowSurface* Manager::surface() const {
return surface_.get();
}
} // namespace egl
} // namespace flutter

View File

@@ -20,6 +20,8 @@
#include "flutter/fml/macros.h"
#include "flutter/shell/platform/windows/egl/context.h"
#include "flutter/shell/platform/windows/egl/surface.h"
#include "flutter/shell/platform/windows/egl/window_surface.h"
namespace flutter {
namespace egl {
@@ -42,37 +44,19 @@ class Manager {
//
// After the surface is created, |SetVSyncEnabled| should be called on a
// thread that can bind the |render_context_|.
virtual bool CreateSurface(HWND hwnd, EGLint width, EGLint height);
virtual bool CreateWindowSurface(HWND hwnd, size_t width, size_t height);
// 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.
// not match current surface dimensions. HWND is the window backing the
// surface.
//
// This binds |render_context_| to the current thread.
virtual void ResizeSurface(HWND hwnd,
EGLint width,
EGLint height,
bool enable_vsync);
// queries EGL for the dimensions of surface in physical
// pixels returning width and height as out params.
void GetSurfaceDimensions(EGLint* width, EGLint* height);
// Releases the pass-in EGLSurface wrapping and backing resources if not null.
virtual void DestroySurface();
virtual void ResizeWindowSurface(HWND hwnd, size_t width, size_t height);
// Check if the current thread has a context bound.
bool HasContextCurrent();
// Binds |render_context_| to the current rendering thread and to the draw and
// read surfaces returning a boolean result reflecting success.
virtual bool MakeCurrent();
// Swaps the front and back buffers of the DX11 swapchain backing surface if
// not null.
virtual bool SwapBuffers();
// Creates a |EGLSurface| from the provided handle.
EGLSurface CreateSurfaceFromHandle(EGLenum handle_type,
EGLClientBuffer handle,
@@ -81,16 +65,6 @@ class Manager {
// Gets the |EGLDisplay|.
EGLDisplay egl_display() const { return display_; };
// If enabled, makes the current surface's buffer swaps block until the
// v-blank.
//
// If disabled, allows one thread to swap multiple buffers per v-blank
// but can result in screen tearing if the system compositor is disabled.
//
// This binds |render_context_| to the current thread and makes the render
// surface current.
virtual void SetVSyncEnabled(bool enabled);
// Gets the |ID3D11Device| chosen by ANGLE.
bool GetDevice(ID3D11Device** device);
@@ -100,6 +74,9 @@ class Manager {
// Get the EGL context used for async texture uploads.
virtual Context* resource_context() const;
// Get the EGL surface that backs the Flutter view.
virtual WindowSurface* surface() const;
protected:
// Creates a new surface manager retaining reference to the passed-in target
// for the lifetime of the manager.
@@ -138,12 +115,8 @@ class Manager {
// The EGL context used for async texture uploads.
std::unique_ptr<Context> resource_context_;
// Current render_surface that engine will draw into.
EGLSurface surface_ = EGL_NO_SURFACE;
// Requested dimensions for current surface
EGLint surface_width_ = 0;
EGLint surface_height_ = 0;
// Th EGL surface used to render into the Flutter view.
std::unique_ptr<WindowSurface> surface_;
// The current D3D device.
Microsoft::WRL::ComPtr<ID3D11Device> resolved_device_ = nullptr;

View File

@@ -0,0 +1,73 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/shell/platform/windows/egl/surface.h"
#include "flutter/shell/platform/windows/egl/egl.h"
namespace flutter {
namespace egl {
Surface::Surface(EGLDisplay display, EGLContext context, EGLSurface surface)
: display_(display), context_(context), surface_(surface) {}
Surface::~Surface() {
Destroy();
}
bool Surface::IsValid() const {
return is_valid_;
}
bool Surface::Destroy() {
if (surface_ != EGL_NO_SURFACE) {
// Ensure the surface is not current before destroying it.
if (::eglMakeCurrent(display_, EGL_NO_SURFACE, EGL_NO_SURFACE,
EGL_NO_CONTEXT) != EGL_TRUE) {
WINDOWS_LOG_EGL_ERROR;
return false;
}
if (::eglDestroySurface(display_, surface_) != EGL_TRUE) {
WINDOWS_LOG_EGL_ERROR;
return false;
}
}
is_valid_ = false;
surface_ = EGL_NO_SURFACE;
return true;
}
bool Surface::IsCurrent() const {
return display_ == ::eglGetCurrentDisplay() &&
surface_ == ::eglGetCurrentSurface(EGL_DRAW) &&
surface_ == ::eglGetCurrentSurface(EGL_READ) &&
context_ == ::eglGetCurrentContext();
}
bool Surface::MakeCurrent() const {
if (::eglMakeCurrent(display_, surface_, surface_, context_) != EGL_TRUE) {
WINDOWS_LOG_EGL_ERROR;
return false;
}
return true;
}
bool Surface::SwapBuffers() const {
if (::eglSwapBuffers(display_, surface_) != EGL_TRUE) {
WINDOWS_LOG_EGL_ERROR;
return false;
}
return true;
}
const EGLSurface& Surface::GetHandle() const {
return surface_;
}
} // namespace egl
} // namespace flutter

View File

@@ -0,0 +1,60 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_EGL_SURFACE_H_
#define FLUTTER_SHELL_PLATFORM_WINDOWS_EGL_SURFACE_H_
#include <EGL/egl.h>
#include "flutter/fml/macros.h"
namespace flutter {
namespace egl {
// An EGL surface. This can be window surface or an off-screen buffer.
//
// This enables automatic error logging and mocking.
class Surface {
public:
Surface(EGLDisplay display, EGLContext context, EGLSurface surface);
~Surface();
// Destroy the EGL surface and invalidate this object.
//
// This also unbinds the current context from the thread.
virtual bool Destroy();
// Check whether the EGL surface is valid.
virtual bool IsValid() const;
// Check whether the EGL display, context, and surface are bound
// to the current thread.
virtual bool IsCurrent() const;
// Bind the EGL surface's context and read and draw surfaces to the
// current thread. Returns true on success.
virtual bool MakeCurrent() const;
// Swap the surface's front the and back buffers. Used to present content.
// Returns true on success.
virtual bool SwapBuffers() const;
// Get the raw EGL surface.
virtual const EGLSurface& GetHandle() const;
protected:
bool is_valid_ = true;
EGLDisplay display_ = EGL_NO_DISPLAY;
EGLContext context_ = EGL_NO_CONTEXT;
EGLSurface surface_ = EGL_NO_SURFACE;
FML_DISALLOW_COPY_AND_ASSIGN(Surface);
};
} // namespace egl
} // namespace flutter
#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_EGL_SURFACE_H_

View File

@@ -0,0 +1,45 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/shell/platform/windows/egl/window_surface.h"
#include "flutter/fml/logging.h"
#include "flutter/shell/platform/windows/egl/egl.h"
namespace flutter {
namespace egl {
WindowSurface::WindowSurface(EGLDisplay display,
EGLContext context,
EGLSurface surface,
size_t width,
size_t height)
: Surface(display, context, surface), width_(width), height_(height) {}
bool WindowSurface::SetVSyncEnabled(bool enabled) {
FML_DCHECK(IsCurrent());
if (::eglSwapInterval(display_, enabled ? 1 : 0) != EGL_TRUE) {
WINDOWS_LOG_EGL_ERROR;
return false;
}
vsync_enabled_ = enabled;
return true;
}
size_t WindowSurface::width() const {
return width_;
}
size_t WindowSurface::height() const {
return height_;
}
bool WindowSurface::vsync_enabled() const {
return vsync_enabled_;
}
} // namespace egl
} // namespace flutter

View File

@@ -0,0 +1,55 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_EGL_WINDOW_SURFACE_H_
#define FLUTTER_SHELL_PLATFORM_WINDOWS_EGL_WINDOW_SURFACE_H_
#include <EGL/egl.h>
#include "flutter/fml/macros.h"
#include "flutter/shell/platform/windows/egl/surface.h"
namespace flutter {
namespace egl {
// An EGL surface used to render a Flutter view to a win32 HWND.
//
// This enables automatic error logging and mocking.
class WindowSurface : public Surface {
public:
WindowSurface(EGLDisplay display,
EGLContext context,
EGLSurface surface,
size_t width,
size_t height);
// If enabled, makes the surface's buffer swaps block until the v-blank.
//
// If disabled, allows one thread to swap multiple buffers per v-blank
// but can result in screen tearing if the system compositor is disabled.
//
// The surface must be current before calling this.
virtual bool SetVSyncEnabled(bool enabled);
// Get the surface's width in physical pixels.
virtual size_t width() const;
// Get the surface's height in physical pixels.
virtual size_t height() const;
// Get whether the surface's buffer swap blocks until the v-blank.
virtual bool vsync_enabled() const;
private:
size_t width_ = 0;
size_t height_ = 0;
bool vsync_enabled_ = true;
FML_DISALLOW_COPY_AND_ASSIGN(WindowSurface);
};
} // namespace egl
} // namespace flutter
#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_EGL_WINDOW_SURFACE_H_

View File

@@ -58,7 +58,7 @@ FlutterRendererConfig GetOpenGLRendererConfig() {
if (!host->egl_manager()) {
return false;
}
return host->egl_manager()->MakeCurrent();
return host->egl_manager()->render_context()->MakeCurrent();
};
config.open_gl.clear_current = [](void* user_data) -> bool {
auto host = static_cast<FlutterWindowsEngine*>(user_data);

View File

@@ -11,6 +11,7 @@
#include "flutter/fml/synchronization/count_down_latch.h"
#include "flutter/fml/synchronization/waitable_event.h"
#include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h"
#include "flutter/shell/platform/windows/egl/manager.h"
#include "flutter/shell/platform/windows/testing/engine_modifier.h"
#include "flutter/shell/platform/windows/testing/windows_test.h"
#include "flutter/shell/platform/windows/testing/windows_test_config_builder.h"
@@ -22,6 +23,20 @@
namespace flutter {
namespace testing {
namespace {
// An EGL manager that initializes EGL but fails to create surfaces.
class HalfBrokenEGLManager : public egl::Manager {
public:
HalfBrokenEGLManager() : egl::Manager(/*enable_impeller = */ false) {}
bool CreateWindowSurface(HWND hwnd, size_t width, size_t height) override {
return false;
}
};
} // namespace
// Verify that we can fetch a texture registrar.
// Prevent regression: https://github.com/flutter/flutter/issues/86617
TEST(WindowsNoFixtureTest, GetTextureRegistrar) {
@@ -296,5 +311,24 @@ TEST_F(WindowsTest, GetGraphicsAdapter) {
ASSERT_TRUE(SUCCEEDED(dxgi_adapter->GetDesc(&desc)));
}
// Verify the app does not crash if EGL initializes successfully but
// the rendering surface cannot be created.
TEST_F(WindowsTest, SurfaceOptional) {
auto& context = GetContext();
WindowsConfigBuilder builder(context);
EnginePtr engine{builder.InitializeEngine()};
EngineModifier modifier{
reinterpret_cast<FlutterWindowsEngine*>(engine.get())};
auto egl_manager = std::make_unique<HalfBrokenEGLManager>();
ASSERT_TRUE(egl_manager->IsValid());
modifier.SetEGLManager(std::move(egl_manager));
ViewControllerPtr controller{
FlutterDesktopViewControllerCreate(0, 0, engine.release())};
ASSERT_NE(controller, nullptr);
}
} // namespace testing
} // namespace flutter

View File

@@ -46,24 +46,37 @@ void UpdateVsync(const FlutterWindowsEngine& engine, bool needs_vsync) {
return;
}
auto update_vsync = [egl_manager, needs_vsync]() {
egl::WindowSurface* surface = egl_manager->surface();
if (!surface) {
return;
}
if (!surface->MakeCurrent()) {
FML_LOG(ERROR) << "Unable to make the render surface current to update "
"the swap interval";
return;
}
if (!surface->SetVSyncEnabled(needs_vsync)) {
FML_LOG(ERROR) << "Unable to update the render surface's swap interval";
}
if (!egl_manager->render_context()->ClearCurrent()) {
FML_LOG(ERROR) << "Unable to clear current surface after updating "
"the swap interval";
}
};
// Updating the vsync makes the EGL context and render surface current.
// If the engine is running, the render surface should only be made current on
// the raster thread. If the engine is initializing, the raster thread doesn't
// exist yet and the render surface can be made current on the platform
// thread.
if (engine.running()) {
engine.PostRasterThreadTask([egl_manager, needs_vsync]() {
egl_manager->SetVSyncEnabled(needs_vsync);
});
engine.PostRasterThreadTask(update_vsync);
} else {
egl_manager->SetVSyncEnabled(needs_vsync);
// Release the EGL context so that the raster thread can use it.
if (!egl_manager->render_context()->ClearCurrent()) {
FML_LOG(ERROR)
<< "Unable to clear current surface after updating the swap interval";
return;
}
update_vsync();
}
}
@@ -116,8 +129,8 @@ void FlutterWindowsView::OnEmptyFrameGenerated() {
// Platform thread is blocked for the entire duration until the
// resize_status_ is set to kDone.
engine_->egl_manager()->ResizeSurface(GetWindowHandle(), resize_target_width_,
resize_target_height_, NeedsVsync());
engine_->egl_manager()->ResizeWindowSurface(
GetWindowHandle(), resize_target_width_, resize_target_height_);
resize_status_ = ResizeState::kFrameGenerated;
}
@@ -132,8 +145,8 @@ bool FlutterWindowsView::OnFrameGenerated(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_->egl_manager()->ResizeSurface(GetWindowHandle(), width, height,
NeedsVsync());
engine_->egl_manager()->ResizeWindowSurface(GetWindowHandle(), width,
height);
resize_status_ = ResizeState::kFrameGenerated;
return true;
}
@@ -165,13 +178,16 @@ bool FlutterWindowsView::OnWindowSizeChanged(size_t width, size_t height) {
return true;
}
egl::WindowSurface* surface = engine_->egl_manager()->surface();
if (!surface || !surface->IsValid()) {
SendWindowMetrics(width, height, binding_handler_->GetDpiScale());
return true;
}
// We're using OpenGL rendering. Resizing the surface must happen on the
// raster thread.
EGLint surface_width, surface_height;
engine_->egl_manager()->GetSurfaceDimensions(&surface_width, &surface_height);
bool surface_will_update =
SurfaceWillUpdate(surface_width, surface_height, width, height);
SurfaceWillUpdate(surface->width(), surface->height(), width, height);
if (!surface_will_update) {
SendWindowMetrics(width, height, binding_handler_->GetDpiScale());
return true;
@@ -637,8 +653,8 @@ bool FlutterWindowsView::PresentSoftwareBitmap(const void* allocation,
void FlutterWindowsView::CreateRenderSurface() {
if (engine_ && engine_->egl_manager()) {
PhysicalWindowBounds bounds = binding_handler_->GetPhysicalWindowBounds();
engine_->egl_manager()->CreateSurface(GetWindowHandle(), bounds.width,
bounds.height);
engine_->egl_manager()->CreateWindowSurface(GetWindowHandle(), bounds.width,
bounds.height);
UpdateVsync(*engine_, NeedsVsync());
@@ -648,9 +664,21 @@ void FlutterWindowsView::CreateRenderSurface() {
}
void FlutterWindowsView::DestroyRenderSurface() {
if (engine_ && engine_->egl_manager()) {
engine_->egl_manager()->DestroySurface();
if (!engine_) {
return;
}
auto const manager = engine_->egl_manager();
if (!manager) {
return;
}
auto const surface = manager->surface();
if (!surface) {
return;
}
surface->Destroy();
}
void FlutterWindowsView::OnHighContrastChanged() {

View File

@@ -20,6 +20,7 @@
#include "flutter/shell/platform/windows/flutter_windows_texture_registrar.h"
#include "flutter/shell/platform/windows/testing/egl/mock_context.h"
#include "flutter/shell/platform/windows/testing/egl/mock_manager.h"
#include "flutter/shell/platform/windows/testing/egl/mock_window_surface.h"
#include "flutter/shell/platform/windows/testing/engine_modifier.h"
#include "flutter/shell/platform/windows/testing/mock_window_binding_handler.h"
#include "flutter/shell/platform/windows/testing/mock_windows_proc_table.h"
@@ -230,12 +231,13 @@ TEST(FlutterWindowsViewTest, SubMenuExpandedState) {
// The view's surface must be destroyed after the engine is shutdown.
// See: https://github.com/flutter/flutter/issues/124463
TEST(FlutterWindowsViewTest, Shutdown) {
std::unique_ptr<MockFlutterWindowsEngine> engine =
std::make_unique<MockFlutterWindowsEngine>();
auto engine = std::make_unique<MockFlutterWindowsEngine>();
auto window_binding_handler =
std::make_unique<NiceMock<MockWindowBindingHandler>>();
std::unique_ptr<egl::MockManager> egl_manager =
std::make_unique<egl::MockManager>();
auto egl_manager = std::make_unique<egl::MockManager>();
auto surface = std::make_unique<egl::MockWindowSurface>();
EXPECT_CALL(*egl_manager.get(), surface).WillOnce(Return(surface.get()));
EngineModifier modifier(engine.get());
FlutterWindowsView view(std::move(window_binding_handler));
@@ -243,7 +245,7 @@ TEST(FlutterWindowsViewTest, Shutdown) {
// The engine must be stopped before the surface can be destroyed.
InSequence s;
EXPECT_CALL(*engine.get(), Stop).Times(1);
EXPECT_CALL(*egl_manager.get(), DestroySurface).Times(1);
EXPECT_CALL(*surface.get(), Destroy).Times(1);
modifier.SetEGLManager(std::move(egl_manager));
view.SetEngine(engine.get());
@@ -817,16 +819,17 @@ TEST(FlutterWindowsViewTest, WindowResizeTests) {
auto window_binding_handler =
std::make_unique<NiceMock<MockWindowBindingHandler>>();
auto windows_proc_table = std::make_shared<MockWindowsProcTable>();
std::unique_ptr<egl::MockManager> egl_manager =
std::make_unique<egl::MockManager>();
auto egl_manager = std::make_unique<egl::MockManager>();
auto surface = std::make_unique<egl::MockWindowSurface>();
EXPECT_CALL(*windows_proc_table.get(), DwmIsCompositionEnabled)
.WillOnce(Return(true));
EXPECT_CALL(
*egl_manager.get(),
ResizeSurface(_, /*width=*/500, /*height=*/500, /*enable_vsync=*/false))
EXPECT_CALL(*windows_proc_table.get(), DwmFlush).WillOnce(Return(S_OK));
EXPECT_CALL(*egl_manager.get(), surface)
.WillRepeatedly(Return(surface.get()));
EXPECT_CALL(*egl_manager.get(),
ResizeWindowSurface(_, /*width=*/500, /*height=*/500))
.Times(1);
EXPECT_CALL(*egl_manager.get(), DestroySurface).Times(1);
EXPECT_CALL(*surface.get(), IsValid).WillOnce(Return(true));
EXPECT_CALL(*surface.get(), Destroy).Times(1);
FlutterWindowsView view(std::move(window_binding_handler),
std::move(windows_proc_table));
@@ -867,16 +870,17 @@ TEST(FlutterWindowsViewTest, TestEmptyFrameResizes) {
auto window_binding_handler =
std::make_unique<NiceMock<MockWindowBindingHandler>>();
auto windows_proc_table = std::make_shared<MockWindowsProcTable>();
std::unique_ptr<egl::MockManager> egl_manager =
std::make_unique<egl::MockManager>();
auto egl_manager = std::make_unique<egl::MockManager>();
auto surface = std::make_unique<egl::MockWindowSurface>();
EXPECT_CALL(*windows_proc_table.get(), DwmIsCompositionEnabled)
.WillOnce(Return(true));
EXPECT_CALL(
*egl_manager.get(),
ResizeSurface(_, /*width=*/500, /*height=*/500, /*enable_vsync=*/false))
EXPECT_CALL(*windows_proc_table.get(), DwmFlush).WillOnce(Return(S_OK));
EXPECT_CALL(*egl_manager.get(), surface)
.WillRepeatedly(Return(surface.get()));
EXPECT_CALL(*egl_manager.get(),
ResizeWindowSurface(_, /*width=*/500, /*height=*/500))
.Times(1);
EXPECT_CALL(*egl_manager.get(), DestroySurface).Times(1);
EXPECT_CALL(*surface.get(), IsValid).WillOnce(Return(true));
EXPECT_CALL(*surface.get(), Destroy).Times(1);
FlutterWindowsView view(std::move(window_binding_handler),
std::move(windows_proc_table));
@@ -919,10 +923,13 @@ TEST(FlutterWindowsViewTest, WindowResizeRace) {
auto window_binding_handler =
std::make_unique<NiceMock<MockWindowBindingHandler>>();
auto windows_proc_table = std::make_shared<MockWindowsProcTable>();
std::unique_ptr<egl::MockManager> egl_manager =
std::make_unique<egl::MockManager>();
auto egl_manager = std::make_unique<egl::MockManager>();
auto surface = std::make_unique<egl::MockWindowSurface>();
EXPECT_CALL(*egl_manager.get(), DestroySurface).Times(1);
EXPECT_CALL(*egl_manager.get(), surface)
.WillRepeatedly(Return(surface.get()));
EXPECT_CALL(*surface.get(), IsValid).WillOnce(Return(true));
EXPECT_CALL(*surface.get(), Destroy).Times(1);
FlutterWindowsView view(std::move(window_binding_handler),
std::move(windows_proc_table));
@@ -950,6 +957,70 @@ TEST(FlutterWindowsViewTest, WindowResizeRace) {
view.OnFramePresented();
}
// Window resize should succeed even if the render surface could not be created
// even though EGL initialized successfully.
TEST(FlutterWindowsViewTest, WindowResizeInvalidSurface) {
std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
EngineModifier modifier(engine.get());
auto window_binding_handler =
std::make_unique<NiceMock<MockWindowBindingHandler>>();
auto windows_proc_table = std::make_shared<MockWindowsProcTable>();
auto egl_manager = std::make_unique<egl::MockManager>();
auto surface = std::make_unique<egl::MockWindowSurface>();
EXPECT_CALL(*egl_manager.get(), surface)
.WillRepeatedly(Return(surface.get()));
EXPECT_CALL(*egl_manager.get(), ResizeWindowSurface).Times(0);
EXPECT_CALL(*surface.get(), IsValid).WillRepeatedly(Return(false));
EXPECT_CALL(*surface.get(), Destroy).WillOnce(Return(false));
FlutterWindowsView view(std::move(window_binding_handler),
std::move(windows_proc_table));
modifier.SetEGLManager(std::move(egl_manager));
view.SetEngine(engine.get());
auto metrics_sent = false;
modifier.embedder_api().SendWindowMetricsEvent = MOCK_ENGINE_PROC(
SendWindowMetricsEvent,
([&metrics_sent](auto engine, const FlutterWindowMetricsEvent* event) {
metrics_sent = true;
return kSuccess;
}));
view.OnWindowSizeChanged(500, 500);
}
// Window resize should succeed even if EGL initialized successfully
// but the EGL surface could not be created.
TEST(FlutterWindowsViewTest, WindowResizeWithoutSurface) {
std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
EngineModifier modifier(engine.get());
auto window_binding_handler =
std::make_unique<NiceMock<MockWindowBindingHandler>>();
auto windows_proc_table = std::make_shared<MockWindowsProcTable>();
auto egl_manager = std::make_unique<egl::MockManager>();
EXPECT_CALL(*egl_manager.get(), surface).WillRepeatedly(Return(nullptr));
EXPECT_CALL(*egl_manager.get(), ResizeWindowSurface).Times(0);
FlutterWindowsView view(std::move(window_binding_handler),
std::move(windows_proc_table));
modifier.SetEGLManager(std::move(egl_manager));
view.SetEngine(engine.get());
auto metrics_sent = false;
modifier.embedder_api().SendWindowMetricsEvent = MOCK_ENGINE_PROC(
SendWindowMetricsEvent,
([&metrics_sent](auto engine, const FlutterWindowMetricsEvent* event) {
metrics_sent = true;
return kSuccess;
}));
view.OnWindowSizeChanged(500, 500);
}
TEST(FlutterWindowsViewTest, WindowRepaintTests) {
std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
EngineModifier modifier(engine.get());
@@ -1299,14 +1370,13 @@ TEST(FlutterWindowsViewTest, TooltipNodeData) {
// Don't block until the v-blank if it is disabled by the window.
// The surface is updated on the platform thread at startup.
TEST(FlutterWindowsViewTest, DisablesVSyncAtStartup) {
std::unique_ptr<MockFlutterWindowsEngine> engine =
std::make_unique<MockFlutterWindowsEngine>();
auto engine = std::make_unique<MockFlutterWindowsEngine>();
auto window_binding_handler =
std::make_unique<NiceMock<MockWindowBindingHandler>>();
auto windows_proc_table = std::make_shared<MockWindowsProcTable>();
std::unique_ptr<egl::MockManager> egl_manager =
std::make_unique<egl::MockManager>();
auto egl_manager = std::make_unique<egl::MockManager>();
egl::MockContext render_context;
auto surface = std::make_unique<egl::MockWindowSurface>();
EXPECT_CALL(*engine.get(), running).WillRepeatedly(Return(false));
EXPECT_CALL(*engine.get(), PostRasterThreadTask).Times(0);
@@ -1314,21 +1384,25 @@ TEST(FlutterWindowsViewTest, DisablesVSyncAtStartup) {
EXPECT_CALL(*windows_proc_table.get(), DwmIsCompositionEnabled)
.WillOnce(Return(true));
EXPECT_CALL(*egl_manager.get(), render_context)
.WillOnce(Return(&render_context));
EXPECT_CALL(*egl_manager.get(), surface)
.WillRepeatedly(Return(surface.get()));
EngineModifier modifier(engine.get());
FlutterWindowsView view(std::move(window_binding_handler),
std::move(windows_proc_table));
InSequence s;
EXPECT_CALL(*egl_manager.get(), CreateSurface(_, _, _))
EXPECT_CALL(*egl_manager.get(), CreateWindowSurface(_, _, _))
.Times(1)
.WillOnce(Return(true));
EXPECT_CALL(*egl_manager.get(), SetVSyncEnabled(false)).Times(1);
EXPECT_CALL(*egl_manager.get(), render_context)
.WillOnce(Return(&render_context));
EXPECT_CALL(*surface.get(), MakeCurrent).WillOnce(Return(true));
EXPECT_CALL(*surface.get(), SetVSyncEnabled(false)).WillOnce(Return(true));
EXPECT_CALL(render_context, ClearCurrent).WillOnce(Return(true));
EXPECT_CALL(*engine.get(), Stop).Times(1);
EXPECT_CALL(*egl_manager.get(), DestroySurface).Times(1);
EXPECT_CALL(*surface.get(), Destroy).Times(1);
modifier.SetEGLManager(std::move(egl_manager));
view.SetEngine(engine.get());
@@ -1339,35 +1413,38 @@ TEST(FlutterWindowsViewTest, DisablesVSyncAtStartup) {
// Blocks until the v-blank if it is enabled by the window.
// The surface is updated on the platform thread at startup.
TEST(FlutterWindowsViewTest, EnablesVSyncAtStartup) {
std::unique_ptr<MockFlutterWindowsEngine> engine =
std::make_unique<MockFlutterWindowsEngine>();
auto engine = std::make_unique<MockFlutterWindowsEngine>();
auto window_binding_handler =
std::make_unique<NiceMock<MockWindowBindingHandler>>();
auto windows_proc_table = std::make_shared<MockWindowsProcTable>();
std::unique_ptr<egl::MockManager> egl_manager =
std::make_unique<egl::MockManager>();
auto egl_manager = std::make_unique<egl::MockManager>();
egl::MockContext render_context;
auto surface = std::make_unique<egl::MockWindowSurface>();
EXPECT_CALL(*engine.get(), running).WillRepeatedly(Return(false));
EXPECT_CALL(*engine.get(), PostRasterThreadTask).Times(0);
EXPECT_CALL(*windows_proc_table.get(), DwmIsCompositionEnabled)
.WillOnce(Return(false));
EXPECT_CALL(*egl_manager.get(), render_context)
.WillOnce(Return(&render_context));
EXPECT_CALL(*egl_manager.get(), surface)
.WillRepeatedly(Return(surface.get()));
EngineModifier modifier(engine.get());
FlutterWindowsView view(std::move(window_binding_handler),
std::move(windows_proc_table));
InSequence s;
EXPECT_CALL(*egl_manager.get(), CreateSurface(_, _, _))
EXPECT_CALL(*egl_manager.get(), CreateWindowSurface(_, _, _))
.Times(1)
.WillOnce(Return(true));
EXPECT_CALL(*egl_manager.get(), SetVSyncEnabled(true)).Times(1);
EXPECT_CALL(*egl_manager.get(), render_context)
.WillOnce(Return(&render_context));
EXPECT_CALL(*surface.get(), MakeCurrent).WillOnce(Return(true));
EXPECT_CALL(*surface.get(), SetVSyncEnabled(true)).WillOnce(Return(true));
EXPECT_CALL(render_context, ClearCurrent).WillOnce(Return(true));
EXPECT_CALL(*engine.get(), Stop).Times(1);
EXPECT_CALL(*egl_manager.get(), DestroySurface).Times(1);
EXPECT_CALL(*surface.get(), Destroy).Times(1);
modifier.SetEGLManager(std::move(egl_manager));
view.SetEngine(engine.get());
@@ -1378,24 +1455,29 @@ TEST(FlutterWindowsViewTest, EnablesVSyncAtStartup) {
// Don't block until the v-blank if it is disabled by the window.
// The surface is updated on the raster thread if the engine is running.
TEST(FlutterWindowsViewTest, DisablesVSyncAfterStartup) {
std::unique_ptr<MockFlutterWindowsEngine> engine =
std::make_unique<MockFlutterWindowsEngine>();
auto engine = std::make_unique<MockFlutterWindowsEngine>();
auto window_binding_handler =
std::make_unique<NiceMock<MockWindowBindingHandler>>();
auto windows_proc_table = std::make_shared<MockWindowsProcTable>();
std::unique_ptr<egl::MockManager> egl_manager =
std::make_unique<egl::MockManager>();
auto egl_manager = std::make_unique<egl::MockManager>();
egl::MockContext render_context;
auto surface = std::make_unique<egl::MockWindowSurface>();
EXPECT_CALL(*engine.get(), running).WillRepeatedly(Return(true));
EXPECT_CALL(*windows_proc_table.get(), DwmIsCompositionEnabled)
.WillOnce(Return(true));
EXPECT_CALL(*egl_manager.get(), render_context)
.WillOnce(Return(&render_context));
EXPECT_CALL(*egl_manager.get(), surface)
.WillRepeatedly(Return(surface.get()));
EngineModifier modifier(engine.get());
FlutterWindowsView view(std::move(window_binding_handler),
std::move(windows_proc_table));
InSequence s;
EXPECT_CALL(*egl_manager.get(), CreateSurface(_, _, _))
EXPECT_CALL(*egl_manager.get(), CreateWindowSurface(_, _, _))
.Times(1)
.WillOnce(Return(true));
EXPECT_CALL(*engine.get(), PostRasterThreadTask)
@@ -1403,11 +1485,11 @@ TEST(FlutterWindowsViewTest, DisablesVSyncAfterStartup) {
callback();
return true;
});
EXPECT_CALL(*egl_manager.get(), SetVSyncEnabled(false)).Times(1);
EXPECT_CALL(*egl_manager.get(), render_context).Times(0);
EXPECT_CALL(*surface.get(), MakeCurrent).WillOnce(Return(true));
EXPECT_CALL(*surface.get(), SetVSyncEnabled(false)).WillOnce(Return(true));
EXPECT_CALL(render_context, ClearCurrent).WillOnce(Return(true));
EXPECT_CALL(*engine.get(), Stop).Times(1);
EXPECT_CALL(*egl_manager.get(), DestroySurface).Times(1);
EXPECT_CALL(*surface.get(), Destroy).Times(1);
modifier.SetEGLManager(std::move(egl_manager));
view.SetEngine(engine.get());
@@ -1418,25 +1500,30 @@ TEST(FlutterWindowsViewTest, DisablesVSyncAfterStartup) {
// Blocks until the v-blank if it is enabled by the window.
// The surface is updated on the raster thread if the engine is running.
TEST(FlutterWindowsViewTest, EnablesVSyncAfterStartup) {
std::unique_ptr<MockFlutterWindowsEngine> engine =
std::make_unique<MockFlutterWindowsEngine>();
auto engine = std::make_unique<MockFlutterWindowsEngine>();
auto window_binding_handler =
std::make_unique<NiceMock<MockWindowBindingHandler>>();
auto windows_proc_table = std::make_shared<MockWindowsProcTable>();
std::unique_ptr<egl::MockManager> egl_manager =
std::make_unique<egl::MockManager>();
auto egl_manager = std::make_unique<egl::MockManager>();
egl::MockContext render_context;
auto surface = std::make_unique<egl::MockWindowSurface>();
EXPECT_CALL(*engine.get(), running).WillRepeatedly(Return(true));
EXPECT_CALL(*windows_proc_table.get(), DwmIsCompositionEnabled)
.WillOnce(Return(false));
EXPECT_CALL(*egl_manager.get(), render_context)
.WillOnce(Return(&render_context));
EXPECT_CALL(*egl_manager.get(), surface)
.WillRepeatedly(Return(surface.get()));
EngineModifier modifier(engine.get());
FlutterWindowsView view(std::move(window_binding_handler),
std::move(windows_proc_table));
InSequence s;
EXPECT_CALL(*egl_manager.get(), CreateSurface(_, _, _))
EXPECT_CALL(*egl_manager.get(), CreateWindowSurface(_, _, _))
.Times(1)
.WillOnce(Return(true));
EXPECT_CALL(*engine.get(), PostRasterThreadTask)
@@ -1444,11 +1531,12 @@ TEST(FlutterWindowsViewTest, EnablesVSyncAfterStartup) {
callback();
return true;
});
EXPECT_CALL(*egl_manager.get(), SetVSyncEnabled(true)).Times(1);
EXPECT_CALL(*egl_manager.get(), render_context).Times(0);
EXPECT_CALL(*surface.get(), MakeCurrent).WillOnce(Return(true));
EXPECT_CALL(*surface.get(), SetVSyncEnabled(true)).WillOnce(Return(true));
EXPECT_CALL(render_context, ClearCurrent).WillOnce(Return(true));
EXPECT_CALL(*engine.get(), Stop).Times(1);
EXPECT_CALL(*egl_manager.get(), DestroySurface).Times(1);
EXPECT_CALL(*surface.get(), Destroy).Times(1);
modifier.SetEGLManager(std::move(egl_manager));
view.SetEngine(engine.get());
@@ -1460,13 +1548,13 @@ TEST(FlutterWindowsViewTest, EnablesVSyncAfterStartup) {
// If this happens, the app must synchronize with the vsync to prevent
// screen tearing.
TEST(FlutterWindowsViewTest, UpdatesVSyncOnDwmUpdates) {
std::unique_ptr<MockFlutterWindowsEngine> engine =
std::make_unique<MockFlutterWindowsEngine>();
auto engine = std::make_unique<MockFlutterWindowsEngine>();
auto window_binding_handler =
std::make_unique<NiceMock<MockWindowBindingHandler>>();
auto windows_proc_table = std::make_shared<MockWindowsProcTable>();
std::unique_ptr<egl::MockManager> egl_manager =
std::make_unique<egl::MockManager>();
auto egl_manager = std::make_unique<egl::MockManager>();
egl::MockContext render_context;
auto surface = std::make_unique<egl::MockWindowSurface>();
EXPECT_CALL(*engine.get(), running).WillRepeatedly(Return(true));
@@ -1480,18 +1568,26 @@ TEST(FlutterWindowsViewTest, UpdatesVSyncOnDwmUpdates) {
.WillOnce(Return(false))
.WillOnce(Return(true));
EXPECT_CALL(*egl_manager.get(), render_context).Times(0);
EXPECT_CALL(*egl_manager.get(), render_context)
.WillRepeatedly(Return(&render_context));
EXPECT_CALL(*egl_manager.get(), surface)
.WillRepeatedly(Return(surface.get()));
EngineModifier modifier(engine.get());
FlutterWindowsView view(std::move(window_binding_handler),
std::move(windows_proc_table));
InSequence s;
EXPECT_CALL(*egl_manager.get(), SetVSyncEnabled(true)).Times(1);
EXPECT_CALL(*egl_manager.get(), SetVSyncEnabled(false)).Times(1);
EXPECT_CALL(*surface.get(), MakeCurrent).WillOnce(Return(true));
EXPECT_CALL(*surface.get(), SetVSyncEnabled(true)).WillOnce(Return(true));
EXPECT_CALL(render_context, ClearCurrent).WillOnce(Return(true));
EXPECT_CALL(*surface.get(), MakeCurrent).WillOnce(Return(true));
EXPECT_CALL(*surface.get(), SetVSyncEnabled(false)).WillOnce(Return(true));
EXPECT_CALL(render_context, ClearCurrent).WillOnce(Return(true));
EXPECT_CALL(*engine.get(), Stop).Times(1);
EXPECT_CALL(*egl_manager.get(), DestroySurface).Times(1);
EXPECT_CALL(*surface.get(), Destroy).Times(1);
modifier.SetEGLManager(std::move(egl_manager));
view.SetEngine(engine.get());

View File

@@ -18,6 +18,7 @@ class MockContext : public flutter::egl::Context {
public:
MockContext() : Context(EGL_NO_DISPLAY, EGL_NO_CONTEXT) {}
MOCK_METHOD(bool, MakeCurrent, (), (const, override));
MOCK_METHOD(bool, ClearCurrent, (), (const, override));
private:

View File

@@ -18,17 +18,12 @@ class MockManager : public flutter::egl::Manager {
public:
MockManager() : Manager(false) {}
MOCK_METHOD(bool, CreateSurface, (HWND, EGLint, EGLint), (override));
MOCK_METHOD(void, ResizeSurface, (HWND, EGLint, EGLint, bool), (override));
MOCK_METHOD(void, DestroySurface, (), (override));
MOCK_METHOD(bool, MakeCurrent, (), (override));
MOCK_METHOD(void, SetVSyncEnabled, (bool), (override));
MOCK_METHOD(bool, SwapBuffers, (), (override));
MOCK_METHOD(bool, CreateWindowSurface, (HWND, size_t, size_t), (override));
MOCK_METHOD(void, ResizeWindowSurface, (HWND, size_t, size_t), (override));
MOCK_METHOD(flutter::egl::Context*, render_context, (), (const, override));
MOCK_METHOD(flutter::egl::Context*, resource_context, (), (const, override));
MOCK_METHOD(flutter::egl::WindowSurface*, surface, (), (const, override));
private:
FML_DISALLOW_COPY_AND_ASSIGN(MockManager);

View File

@@ -0,0 +1,36 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_TESTING_EGL_MOCK_WINDOW_SURFACE_H_
#define FLUTTER_SHELL_PLATFORM_WINDOWS_TESTING_EGL_MOCK_WINDOW_SURFACE_H_
#include "flutter/fml/macros.h"
#include "flutter/shell/platform/windows/egl/window_surface.h"
#include "gmock/gmock.h"
namespace flutter {
namespace testing {
namespace egl {
/// Mock for the |WindowSurface| base class.
class MockWindowSurface : public flutter::egl::WindowSurface {
public:
MockWindowSurface()
: WindowSurface(EGL_NO_DISPLAY, EGL_NO_CONTEXT, EGL_NO_SURFACE, 0, 0) {}
MOCK_METHOD(bool, IsValid, (), (const, override));
MOCK_METHOD(bool, Destroy, (), (override));
MOCK_METHOD(bool, MakeCurrent, (), (const, override));
MOCK_METHOD(bool, SwapBuffers, (), (const, override));
MOCK_METHOD(bool, SetVSyncEnabled, (bool), (override));
private:
FML_DISALLOW_COPY_AND_ASSIGN(MockWindowSurface);
};
} // namespace egl
} // namespace testing
} // namespace flutter
#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_TESTING_EGL_MOCK_WINDOW_SURFACE_H_