Skip to content

Commit c7926ac

Browse files
[CP-beta][windows]: Uses offscreen MSAA when implicit msaa isn't available. (#190374)
This pull request is created by [automatic cherry pick workflow](https://github.com/flutter/flutter/blob/main/docs/releases/Flutter-Cherrypick-Process.md#automatically-creates-a-cherry-pick-request) Please fill in the form below, and a flutter domain expert will evaluate this cherry pick request. ### Issue Link: *What is the link to the issue this cherry-pick is addressing?* #190060 ### Impact Description: *What is the impact (ex. visual jank on Samsung phones, app crash, cannot ship an iOS app)? Does it impact development (ex. flutter doctor crashes when Android Studio is installed), or the shipping of production apps (the app crashes on launch). This information is for domain experts and release engineers to understand the consequences of saying yes or no to the cherry pick.* This PR fixes anti-aliasing on some draw calls in Impeller Windows, specifically clipping and drawing paths. ### Changelog Description: Explain this cherry pick: * In one line that is accessible to most Flutter developers. * That describes the state prior to the fix. * That includes which platforms are impacted. See [best practices](https://github.com/flutter/flutter/blob/main/docs/releases/Hotfix-Documentation-Best-Practices.md) for examples. < Replace with changelog description here > [flutter/190060] When running an app on Windows with Impeller, drawing paths and clipping regions had jagged edges. ### Workaround: *Is there a workaround for this issue?* Yes, downgrading to Skia. ### Risk: *What is the risk level of this cherry-pick?* ### Test Coverage: *Are you confident that your fix is well-tested by automated tests?* ### Validation Steps: *What are the steps to validate that this fix works?* I ran a Flutter app that clips a rectangle into the shape of a circle on a remote vm windows instance. I also ran it on a differ windows machine. I also recorded it with renderdoc and saw the MSAA process working.
1 parent eb61588 commit c7926ac

3 files changed

Lines changed: 207 additions & 26 deletions

File tree

‎engine/src/flutter/shell/platform/windows/compositor_opengl.cc‎

Lines changed: 87 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "flutter/shell/platform/windows/compositor_opengl.h"
66

77
#include "GLES3/gl3.h"
8+
#include "flutter/fml/logging.h"
89
#include "flutter/shell/platform/windows/flutter_windows_engine.h"
910
#include "flutter/shell/platform/windows/flutter_windows_view.h"
1011

@@ -18,6 +19,7 @@ constexpr uint32_t kWindowFrameBufferId = 0;
1819
struct FramebufferBackingStore {
1920
uint32_t framebuffer_id = 0;
2021
uint32_t texture_id = 0;
22+
uint32_t color_renderbuffer_id = 0;
2123
uint32_t depth_stencil_id = 0;
2224
};
2325

@@ -51,48 +53,94 @@ bool CompositorOpenGL::CreateBackingStore(
5153

5254
auto store = std::make_unique<FramebufferBackingStore>();
5355

54-
gl_->GenTextures(1, &store->texture_id);
5556
gl_->GenFramebuffers(1, &store->framebuffer_id);
56-
5757
gl_->BindFramebuffer(GL_FRAMEBUFFER, store->framebuffer_id);
5858

59-
gl_->BindTexture(GL_TEXTURE_2D, store->texture_id);
60-
gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
61-
gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
62-
gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
63-
gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
64-
gl_->TexImage2D(GL_TEXTURE_2D, 0, format_.general_format, config.size.width,
65-
config.size.height, 0, format_.general_format,
66-
GL_UNSIGNED_BYTE, nullptr);
67-
gl_->BindTexture(GL_TEXTURE_2D, 0);
68-
6959
if (enable_impeller_) {
7060
if (supports_implicit_msaa_) {
61+
gl_->GenTextures(1, &store->texture_id);
62+
gl_->BindTexture(GL_TEXTURE_2D, store->texture_id);
63+
gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
64+
gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
65+
gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
66+
gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
67+
gl_->TexImage2D(GL_TEXTURE_2D, 0, format_.sized_format, config.size.width,
68+
config.size.height, 0, format_.general_format,
69+
GL_UNSIGNED_BYTE, nullptr);
70+
gl_->BindTexture(GL_TEXTURE_2D, 0);
71+
7172
// MSAA color attachment
7273
gl_->FramebufferTexture2DMultisampleEXT(
7374
GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
7475
store->texture_id, 0, 4);
75-
} else {
76-
gl_->FramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
77-
GL_TEXTURE_2D, store->texture_id, 0);
78-
}
7976

80-
// Impeller always requires depth/stencil attachment.
81-
gl_->GenRenderbuffers(1, &store->depth_stencil_id);
82-
gl_->BindRenderbuffer(GL_RENDERBUFFER, store->depth_stencil_id);
83-
if (supports_implicit_msaa_) {
77+
// Impeller always requires depth/stencil attachment.
78+
gl_->GenRenderbuffers(1, &store->depth_stencil_id);
79+
gl_->BindRenderbuffer(GL_RENDERBUFFER, store->depth_stencil_id);
8480
gl_->RenderbufferStorageMultisampleEXT(
8581
GL_RENDERBUFFER, 4, GL_DEPTH24_STENCIL8, config.size.width,
8682
config.size.height);
83+
gl_->FramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
84+
GL_RENDERBUFFER, store->depth_stencil_id);
85+
gl_->FramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
86+
GL_RENDERBUFFER, store->depth_stencil_id);
87+
} else if (supports_offscreen_msaa_) {
88+
// MSAA color renderbuffer attachment
89+
gl_->GenRenderbuffers(1, &store->color_renderbuffer_id);
90+
gl_->BindRenderbuffer(GL_RENDERBUFFER, store->color_renderbuffer_id);
91+
gl_->RenderbufferStorageMultisample(
92+
GL_RENDERBUFFER, 4, format_.sized_format, config.size.width,
93+
config.size.height);
94+
gl_->FramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
95+
GL_RENDERBUFFER,
96+
store->color_renderbuffer_id);
97+
98+
// MSAA depth/stencil attachment
99+
gl_->GenRenderbuffers(1, &store->depth_stencil_id);
100+
gl_->BindRenderbuffer(GL_RENDERBUFFER, store->depth_stencil_id);
101+
gl_->RenderbufferStorageMultisample(
102+
GL_RENDERBUFFER, 4, GL_DEPTH24_STENCIL8, config.size.width,
103+
config.size.height);
104+
gl_->FramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
105+
GL_RENDERBUFFER, store->depth_stencil_id);
106+
gl_->FramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
107+
GL_RENDERBUFFER, store->depth_stencil_id);
87108
} else {
109+
gl_->GenTextures(1, &store->texture_id);
110+
gl_->BindTexture(GL_TEXTURE_2D, store->texture_id);
111+
gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
112+
gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
113+
gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
114+
gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
115+
gl_->TexImage2D(GL_TEXTURE_2D, 0, format_.sized_format, config.size.width,
116+
config.size.height, 0, format_.general_format,
117+
GL_UNSIGNED_BYTE, nullptr);
118+
gl_->BindTexture(GL_TEXTURE_2D, 0);
119+
120+
gl_->FramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
121+
GL_TEXTURE_2D, store->texture_id, 0);
122+
123+
gl_->GenRenderbuffers(1, &store->depth_stencil_id);
124+
gl_->BindRenderbuffer(GL_RENDERBUFFER, store->depth_stencil_id);
88125
gl_->RenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8,
89126
config.size.width, config.size.height);
127+
gl_->FramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
128+
GL_RENDERBUFFER, store->depth_stencil_id);
129+
gl_->FramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
130+
GL_RENDERBUFFER, store->depth_stencil_id);
90131
}
91-
gl_->FramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
92-
GL_RENDERBUFFER, store->depth_stencil_id);
93-
gl_->FramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
94-
GL_RENDERBUFFER, store->depth_stencil_id);
95132
} else {
133+
gl_->GenTextures(1, &store->texture_id);
134+
gl_->BindTexture(GL_TEXTURE_2D, store->texture_id);
135+
gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
136+
gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
137+
gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
138+
gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
139+
gl_->TexImage2D(GL_TEXTURE_2D, 0, format_.sized_format, config.size.width,
140+
config.size.height, 0, format_.general_format,
141+
GL_UNSIGNED_BYTE, nullptr);
142+
gl_->BindTexture(GL_TEXTURE_2D, 0);
143+
96144
gl_->FramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
97145
GL_TEXTURE_2D, store->texture_id, 0);
98146
}
@@ -118,8 +166,12 @@ bool CompositorOpenGL::CollectBackingStore(const FlutterBackingStore* store) {
118166
store->open_gl.framebuffer.user_data);
119167

120168
gl_->DeleteFramebuffers(1, &user_data->framebuffer_id);
121-
gl_->DeleteTextures(1, &user_data->texture_id);
122-
169+
if (user_data->texture_id != 0) {
170+
gl_->DeleteTextures(1, &user_data->texture_id);
171+
}
172+
if (user_data->color_renderbuffer_id != 0) {
173+
gl_->DeleteRenderbuffers(1, &user_data->color_renderbuffer_id);
174+
}
123175
if (user_data->depth_stencil_id != 0) {
124176
gl_->DeleteRenderbuffers(1, &user_data->depth_stencil_id);
125177
}
@@ -237,6 +289,15 @@ bool CompositorOpenGL::Initialize() {
237289

238290
supports_implicit_msaa_ =
239291
gl_->GetCapabilities()->SupportsImplicitResolvingMSAA();
292+
supports_offscreen_msaa_ = gl_->GetCapabilities()->SupportsOffscreenMSAA();
293+
294+
if (enable_impeller_ && !supports_implicit_msaa_ &&
295+
!supports_offscreen_msaa_) {
296+
// TODO(190362): I suspect this branch is never taken since ANGLE will
297+
// support offscreen MSAA. We should investigate removing this branch and
298+
// the implicit MSAA branch in the rendering functions.
299+
FML_LOG(WARNING) << "Rendering without MSAA.";
300+
}
240301

241302
is_initialized_ = true;
242303
return true;

‎engine/src/flutter/shell/platform/windows/compositor_opengl.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ class CompositorOpenGL : public Compositor {
6666
// Whether the OpenGL context supports implicit MSAA.
6767
bool supports_implicit_msaa_ = false;
6868

69+
// Whether the OpenGL context supports offscreen MSAA.
70+
bool supports_offscreen_msaa_ = false;
71+
6972
// Initialize the compositor. This must run on the raster thread.
7073
bool Initialize();
7174

‎engine/src/flutter/shell/platform/windows/compositor_opengl_unittests.cc‎

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,39 @@ const impeller::ProcTableGLES::Resolver kMockResolverWithMSAA =
104104
return kMockResolver(name);
105105
};
106106

107+
void MockGetIntegervWithOffscreenMSAA(GLenum name, int* value) {
108+
if (name == GL_NUM_EXTENSIONS) {
109+
*value = 1;
110+
} else if (name == GL_MAX_SAMPLES) {
111+
*value = 4;
112+
} else {
113+
*value = 0;
114+
}
115+
}
116+
117+
const unsigned char* MockGetStringWithOffscreenMSAA(GLenum name) {
118+
switch (name) {
119+
case GL_VERSION:
120+
return reinterpret_cast<const unsigned char*>("OpenGL ES 3.0");
121+
case GL_SHADING_LANGUAGE_VERSION:
122+
return reinterpret_cast<const unsigned char*>("OpenGL ES GLSL ES 3.0");
123+
default:
124+
return reinterpret_cast<const unsigned char*>("");
125+
}
126+
}
127+
128+
const impeller::ProcTableGLES::Resolver kMockResolverWithOffscreenMSAA =
129+
[](const char* name) -> void* {
130+
std::string_view function_name{name};
131+
132+
if (function_name == "glGetString") {
133+
return reinterpret_cast<void*>(&MockGetStringWithOffscreenMSAA);
134+
} else if (function_name == "glGetIntegerv") {
135+
return reinterpret_cast<void*>(&MockGetIntegervWithOffscreenMSAA);
136+
}
137+
return kMockResolver(name);
138+
};
139+
107140
class CompositorOpenGLTest : public WindowsTest {
108141
public:
109142
CompositorOpenGLTest() = default;
@@ -254,6 +287,90 @@ TEST_F(CompositorOpenGLTest, CreateBackingStoreImpellerMSAA) {
254287
ASSERT_TRUE(compositor.CollectBackingStore(&backing_store));
255288
}
256289

290+
// Verifies that when implicit MSAA is unsupported but offscreen MSAA is
291+
// supported (OpenGL ES 3.0+ with GL_MAX_SAMPLES >= 4), CompositorOpenGL
292+
// creates 4x MSAA color and depth/stencil renderbuffers.
293+
TEST_F(CompositorOpenGLTest, CreateBackingStoreImpellerOffscreenMSAA) {
294+
UseHeadlessEngine();
295+
296+
static int framebuffer_texture2d_calls = 0;
297+
static int framebuffer_texture2d_multisample_calls = 0;
298+
static int renderbuffer_storage_multisample_calls = 0;
299+
static int renderbuffer_storage_multisample_samples = 0;
300+
static int framebuffer_renderbuffer_calls = 0;
301+
static int delete_renderbuffers_calls = 0;
302+
static int delete_textures_calls = 0;
303+
304+
framebuffer_texture2d_calls = 0;
305+
framebuffer_texture2d_multisample_calls = 0;
306+
renderbuffer_storage_multisample_calls = 0;
307+
renderbuffer_storage_multisample_samples = 0;
308+
framebuffer_renderbuffer_calls = 0;
309+
delete_renderbuffers_calls = 0;
310+
delete_textures_calls = 0;
311+
312+
const impeller::ProcTableGLES::Resolver resolver =
313+
[](const char* name) -> void* {
314+
std::string_view function_name{name};
315+
if (function_name == "glFramebufferTexture2D") {
316+
return reinterpret_cast<void*>(
317+
+[](GLenum, GLenum, GLenum, GLuint, GLint) {
318+
framebuffer_texture2d_calls++;
319+
});
320+
} else if (function_name == "glFramebufferTexture2DMultisampleEXT") {
321+
return reinterpret_cast<void*>(
322+
+[](GLenum, GLenum, GLenum, GLuint, GLint, GLsizei) {
323+
framebuffer_texture2d_multisample_calls++;
324+
});
325+
} else if (function_name == "glGenRenderbuffers") {
326+
return reinterpret_cast<void*>(+[](GLsizei n, GLuint* renderbuffers) {
327+
static GLuint next_id = 1;
328+
for (GLsizei i = 0; i < n; i++) {
329+
renderbuffers[i] = next_id++;
330+
}
331+
});
332+
} else if (function_name == "glRenderbufferStorageMultisample") {
333+
return reinterpret_cast<void*>(+[](GLenum target, GLsizei samples,
334+
GLenum internalformat, GLsizei width,
335+
GLsizei height) {
336+
renderbuffer_storage_multisample_calls++;
337+
renderbuffer_storage_multisample_samples = samples;
338+
});
339+
} else if (function_name == "glFramebufferRenderbuffer") {
340+
return reinterpret_cast<void*>(+[](GLenum, GLenum, GLenum, GLuint) {
341+
framebuffer_renderbuffer_calls++;
342+
});
343+
} else if (function_name == "glDeleteRenderbuffers") {
344+
return reinterpret_cast<void*>(
345+
+[](GLsizei n, const GLuint* renderbuffers) {
346+
delete_renderbuffers_calls += n;
347+
});
348+
} else if (function_name == "glDeleteTextures") {
349+
return reinterpret_cast<void*>(+[](GLsizei n, const GLuint* textures) {
350+
delete_textures_calls += n;
351+
});
352+
}
353+
return kMockResolverWithOffscreenMSAA(name);
354+
};
355+
356+
auto compositor =
357+
CompositorOpenGL{engine(), resolver, /*enable_impeller=*/true};
358+
FlutterBackingStoreConfig config = {};
359+
FlutterBackingStore backing_store = {};
360+
361+
EXPECT_CALL(*render_context(), MakeCurrent).WillOnce(Return(true));
362+
ASSERT_TRUE(compositor.CreateBackingStore(config, &backing_store));
363+
EXPECT_EQ(framebuffer_texture2d_calls, 0);
364+
EXPECT_EQ(framebuffer_texture2d_multisample_calls, 0);
365+
EXPECT_EQ(renderbuffer_storage_multisample_calls, 2);
366+
EXPECT_EQ(renderbuffer_storage_multisample_samples, 4);
367+
EXPECT_EQ(framebuffer_renderbuffer_calls, 3);
368+
369+
ASSERT_TRUE(compositor.CollectBackingStore(&backing_store));
370+
EXPECT_EQ(delete_renderbuffers_calls, 2);
371+
EXPECT_EQ(delete_textures_calls, 0);
372+
}
373+
257374
TEST_F(CompositorOpenGLTest, InitializationFailure) {
258375
UseHeadlessEngine();
259376

0 commit comments

Comments
 (0)