Skip to content

Commit 94306f7

Browse files
authored
[Windows] Use an unsized texture format for backing stores on OpenGL ES 2 (flutter#192917)
On GPUs limited to D3D11 feature level 10_0, ANGLE only creates an OpenGL ES 2.0 context. `CompositorOpenGL::CreateBackingStore` passes the sized format (`GL_BGRA8_EXT` or `GL_RGBA8`) as the `internalformat` of `glTexImage2D`. ES 2.0 requires `internalformat` to equal `format`, so the call fails with `GL_INVALID_VALUE`, the backing store framebuffer is incomplete and the window stays black with no error logged. Flutter 3.44.2 passed the unsized format there, the sized one came in with the MSAA backing store changes. This change picks the internal format once in `Initialize`: the unsized general format on an ES 2 context, the sized format otherwise. The three `TexImage2D` calls use it. The sized format is still reported to the engine through `FlutterOpenGLFramebuffer.target`, and nothing changes on ES 3 contexts. Tests: - `CreateBackingStoreGLES2Unsized`: ES 2 with Skia, `glTexImage2D` gets `GL_BGRA_EXT`, the engine still gets `GL_BGRA8_EXT`. - `CreateBackingStoreImpellerGLES2Unsized`: ES 2 with Impeller, `GL_BGRA_EXT`. - `CreateBackingStoreGLES3Sized`: ES 3 keeps `GL_RGBA8`. The two ES 2 tests fail without the change. Manually verified with a dxcpl *Feature level limit* of 10_0, where the unpatched engine shows a black window: on 3.47.4 x64 a stock app renders with Skia and Impeller, and so does an app with gradients, shadows, a multiply blend and animation. An animated app runs at 120 FPS at 10_0 with both renderers, the same as the official engine without the limit. With Impeller, content drawn before a `BackdropFilter` is still lost at 10_0; that is a separate Impeller bug. On `main` (arm64) the stock app renders with an ES 2 context, a complete framebuffer and no GL errors. Also verified in a production app on 3.47.4. Fixes flutter#191978 ## Pre-launch Checklist - [ ] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [ ] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [ ] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [ ] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [ ] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant in-code documentation (doc comments with `///`). - [ ] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent b48c396 commit 94306f7

3 files changed

Lines changed: 150 additions & 9 deletions

File tree

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ bool CompositorOpenGL::CreateBackingStore(
6464
gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
6565
gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
6666
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);
67+
gl_->TexImage2D(GL_TEXTURE_2D, 0, format_.internal_format,
68+
config.size.width, config.size.height, 0,
69+
format_.general_format, GL_UNSIGNED_BYTE, nullptr);
7070
gl_->BindTexture(GL_TEXTURE_2D, 0);
7171

7272
// MSAA color attachment
@@ -112,9 +112,9 @@ bool CompositorOpenGL::CreateBackingStore(
112112
gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
113113
gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
114114
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);
115+
gl_->TexImage2D(GL_TEXTURE_2D, 0, format_.internal_format,
116+
config.size.width, config.size.height, 0,
117+
format_.general_format, GL_UNSIGNED_BYTE, nullptr);
118118
gl_->BindTexture(GL_TEXTURE_2D, 0);
119119

120120
gl_->FramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
@@ -136,9 +136,9 @@ bool CompositorOpenGL::CreateBackingStore(
136136
gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
137137
gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
138138
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);
139+
gl_->TexImage2D(GL_TEXTURE_2D, 0, format_.internal_format,
140+
config.size.width, config.size.height, 0,
141+
format_.general_format, GL_UNSIGNED_BYTE, nullptr);
142142
gl_->BindTexture(GL_TEXTURE_2D, 0);
143143

144144
gl_->FramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
@@ -281,6 +281,12 @@ bool CompositorOpenGL::Initialize() {
281281
format_.general_format = GL_RGBA;
282282
}
283283

284+
const impeller::DescriptionGLES* description = gl_->GetDescription();
285+
bool is_gles2 =
286+
description->IsES() && description->GetGlVersion().major_version < 3;
287+
format_.internal_format =
288+
is_gles2 ? format_.general_format : format_.sized_format;
289+
284290
if (!gl_->BlitFramebuffer.IsAvailable() &&
285291
!gl_->BlitFramebufferANGLE.IsAvailable()) {
286292
FML_LOG(ERROR) << "Unable to find OpenGL blit framebuffer procedure.";

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ class CompositorOpenGL : public Compositor {
4444
uint32_t sized_format = 0;
4545
// The format used to create textures. Passed to `glTexImage2D`.
4646
uint32_t general_format = 0;
47+
// The internal format passed to `glTexImage2D`. OpenGL ES 2.0 requires it
48+
// to equal `general_format`, and only OpenGL ES 3.0 accepts `sized_format`.
49+
uint32_t internal_format = 0;
4750
};
4851

4952
// The compositor initializes itself lazily once |CreateBackingStore| is

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

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
#include <memory>
6+
#include <type_traits>
67
#include <vector>
78

89
#include "flutter/impeller/renderer/backend/gles/gles.h"
@@ -76,6 +77,12 @@ const unsigned char* MockGetStringiWithMSAA(GLenum name, int index) {
7677

7778
void DoNothing() {}
7879

80+
template <typename T, typename U>
81+
struct CheckSameSignature : std::false_type {};
82+
83+
template <typename Ret, typename... Args>
84+
struct CheckSameSignature<Ret(Args...), Ret(Args...)> : std::true_type {};
85+
7986
const impeller::ProcTableGLES::Resolver kMockResolver = [](const char* name) {
8087
std::string function_name{name};
8188

@@ -137,12 +144,91 @@ const impeller::ProcTableGLES::Resolver kMockResolverWithOffscreenMSAA =
137144
return kMockResolver(name);
138145
};
139146

147+
// ANGLE returns these strings for an OpenGL ES 2.0 context
148+
const unsigned char* MockGetStringGLES2(GLenum name) {
149+
switch (name) {
150+
case GL_VERSION:
151+
return reinterpret_cast<const unsigned char*>("OpenGL ES 2.0");
152+
case GL_SHADING_LANGUAGE_VERSION:
153+
return reinterpret_cast<const unsigned char*>("OpenGL ES GLSL ES 1.00");
154+
case GL_EXTENSIONS:
155+
return reinterpret_cast<const unsigned char*>(
156+
"GL_ANGLE_framebuffer_blit GL_EXT_texture_format_BGRA8888");
157+
default:
158+
return reinterpret_cast<const unsigned char*>("");
159+
}
160+
}
161+
162+
static_assert(CheckSameSignature<decltype(MockGetStringGLES2), //
163+
decltype(glGetString)>::value);
164+
165+
const unsigned char* MockGetStringGLES3(GLenum name) {
166+
switch (name) {
167+
case GL_VERSION:
168+
return reinterpret_cast<const unsigned char*>("OpenGL ES 3.0");
169+
case GL_SHADING_LANGUAGE_VERSION:
170+
return reinterpret_cast<const unsigned char*>("OpenGL ES GLSL ES 3.00");
171+
default:
172+
return reinterpret_cast<const unsigned char*>("");
173+
}
174+
}
175+
176+
static_assert(CheckSameSignature<decltype(MockGetStringGLES3), //
177+
decltype(glGetString)>::value);
178+
179+
std::vector<GLint>& TexImage2DInternalFormats() {
180+
static std::vector<GLint> internal_formats;
181+
return internal_formats;
182+
}
183+
184+
void RecordTexImage2D(GLenum,
185+
GLint,
186+
GLint internalformat,
187+
GLsizei,
188+
GLsizei,
189+
GLint,
190+
GLenum,
191+
GLenum,
192+
const void*) {
193+
TexImage2DInternalFormats().push_back(internalformat);
194+
}
195+
196+
static_assert(CheckSameSignature<decltype(RecordTexImage2D), //
197+
decltype(glTexImage2D)>::value);
198+
199+
const impeller::ProcTableGLES::Resolver kRecordingResolverGLES2 =
200+
[](const char* name) -> void* {
201+
std::string_view function_name{name};
202+
if (function_name == "glGetString") {
203+
return reinterpret_cast<void*>(&MockGetStringGLES2);
204+
} else if (function_name == "glTexImage2D") {
205+
return reinterpret_cast<void*>(&RecordTexImage2D);
206+
}
207+
return kMockResolver(name);
208+
};
209+
210+
const impeller::ProcTableGLES::Resolver kRecordingResolverGLES3 =
211+
[](const char* name) -> void* {
212+
std::string_view function_name{name};
213+
if (function_name == "glGetString") {
214+
return reinterpret_cast<void*>(&MockGetStringGLES3);
215+
} else if (function_name == "glTexImage2D") {
216+
return reinterpret_cast<void*>(&RecordTexImage2D);
217+
}
218+
return kMockResolver(name);
219+
};
220+
140221
class CompositorOpenGLTest : public WindowsTest {
141222
public:
142223
CompositorOpenGLTest() = default;
143224
virtual ~CompositorOpenGLTest() = default;
144225

145226
protected:
227+
void TearDown() override {
228+
TexImage2DInternalFormats().clear();
229+
WindowsTest::TearDown();
230+
}
231+
146232
FlutterWindowsEngine* engine() { return engine_.get(); }
147233
FlutterWindowsView* view() { return view_.get(); }
148234
egl::MockManager* egl_manager() { return egl_manager_; }
@@ -371,6 +457,52 @@ TEST_F(CompositorOpenGLTest, CreateBackingStoreImpellerOffscreenMSAA) {
371457
EXPECT_EQ(delete_textures_calls, 0);
372458
}
373459

460+
// OpenGL ES 2.0 rejects a sized internal format in glTexImage2D, so the
461+
// texture gets the unsized format while the engine still receives the sized one
462+
TEST_F(CompositorOpenGLTest, CreateBackingStoreGLES2Unsized) {
463+
UseHeadlessEngine();
464+
465+
auto compositor = CompositorOpenGL{engine(), kRecordingResolverGLES2,
466+
/*enable_impeller=*/false};
467+
FlutterBackingStoreConfig config = {};
468+
FlutterBackingStore backing_store = {};
469+
470+
EXPECT_CALL(*render_context(), MakeCurrent).WillOnce(Return(true));
471+
ASSERT_TRUE(compositor.CreateBackingStore(config, &backing_store));
472+
EXPECT_EQ(TexImage2DInternalFormats(), std::vector<GLint>{GL_BGRA_EXT});
473+
EXPECT_EQ(backing_store.open_gl.framebuffer.target,
474+
static_cast<uint32_t>(GL_BGRA8_EXT));
475+
ASSERT_TRUE(compositor.CollectBackingStore(&backing_store));
476+
}
477+
478+
TEST_F(CompositorOpenGLTest, CreateBackingStoreImpellerGLES2Unsized) {
479+
UseHeadlessEngine();
480+
481+
auto compositor = CompositorOpenGL{engine(), kRecordingResolverGLES2,
482+
/*enable_impeller=*/true};
483+
FlutterBackingStoreConfig config = {};
484+
FlutterBackingStore backing_store = {};
485+
486+
EXPECT_CALL(*render_context(), MakeCurrent).WillOnce(Return(true));
487+
ASSERT_TRUE(compositor.CreateBackingStore(config, &backing_store));
488+
EXPECT_EQ(TexImage2DInternalFormats(), std::vector<GLint>{GL_BGRA_EXT});
489+
ASSERT_TRUE(compositor.CollectBackingStore(&backing_store));
490+
}
491+
492+
TEST_F(CompositorOpenGLTest, CreateBackingStoreGLES3Sized) {
493+
UseHeadlessEngine();
494+
495+
auto compositor = CompositorOpenGL{engine(), kRecordingResolverGLES3,
496+
/*enable_impeller=*/false};
497+
FlutterBackingStoreConfig config = {};
498+
FlutterBackingStore backing_store = {};
499+
500+
EXPECT_CALL(*render_context(), MakeCurrent).WillOnce(Return(true));
501+
ASSERT_TRUE(compositor.CreateBackingStore(config, &backing_store));
502+
EXPECT_EQ(TexImage2DInternalFormats(), std::vector<GLint>{GL_RGBA8});
503+
ASSERT_TRUE(compositor.CollectBackingStore(&backing_store));
504+
}
505+
374506
TEST_F(CompositorOpenGLTest, InitializationFailure) {
375507
UseHeadlessEngine();
376508

0 commit comments

Comments
 (0)