Skip to content

Commit 9f95f27

Browse files
b-lukclaude
authored andcommitted
[Impeller] Make a GL context current before constructing an AiksContext without a surface (flutter#195629)
Fixes flutter#195627. See the issue for context. When there is no surface, `Rasterizer::GetAiksContext()` now tries to get an AiksContext using the snapshot surface producer. This is similar to flutter#194506's fix for `Picture.toImage()` when there is no surface ([here](https://github.com/flutter/flutter/pull/194506/changes#diff-5d98a88b599a9d73af3f6acda580a3f03bce1eab9c7abd1f29f425f7279768c0R99-R110)). Adds tests similar to flutter#194506, but for this issue's `Image.toByteData()` case. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant in-code documentation (doc comments with `///`). - [x] 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]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- 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/CONTRIBUTING.ai.md [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/tests]: https://github.com/flutter/tests [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 [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md (cherry picked from commit 1ea585c) Backport note: ImageToByteDataWithoutSurfaceOnImpellerGLES is dropped on 3.47.9. It guards flutter#195627, a regression from flutter#191747, which 3.47.9 doesn't have: here Image.toByteData doesn't go through Rasterizer::GetAiksContext, so the test reaches the older encode path and waits forever for a current context. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent c19f6ab commit 9f95f27

4 files changed

Lines changed: 80 additions & 20 deletions

File tree

‎engine/src/flutter/lib/ui/BUILD.gn‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ if (enable_unittests) {
310310
}
311311

312312
if (impeller_enable_opengles) {
313-
sources += [ "compositing/scene_impeller_gles_unittests.cc" ]
313+
sources += [ "impeller_gles_without_surface_unittests.cc" ]
314314

315315
deps += [
316316
"//flutter/impeller/entity:entity_shaders",

‎engine/src/flutter/lib/ui/fixtures/ui_test.dart‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,35 @@ Future<void> sceneToImageWithoutSurface() async {
708708
}
709709
}
710710

711+
@pragma('vm:external-name', 'ClearCurrentGLContext')
712+
external void _clearCurrentGLContext();
713+
@pragma('vm:external-name', 'ToByteDataResult')
714+
external void _toByteDataResult(int length, String? error);
715+
716+
/// `Image.toByteData` on a raster-owned image while the view has no window
717+
/// surface (on Android, after `SurfaceView.surfaceDestroyed`).
718+
/// Regression test for https://github.com/flutter/flutter/issues/195627.
719+
@pragma('vm:entry-point')
720+
Future<void> imageToByteDataWithoutSurface() async {
721+
final recorder = PictureRecorder();
722+
Canvas(recorder)
723+
.drawRect(const Rect.fromLTWH(0, 0, 8, 8), Paint()..color = const Color(0xFF2ADEC3));
724+
final Picture picture = recorder.endRecording();
725+
final Image image = await picture.toImage(8, 8);
726+
// `Picture.toImage` leaves its offscreen context current on the raster
727+
// thread. Clear it, like tearing down the window surface does.
728+
_clearCurrentGLContext();
729+
try {
730+
final ByteData? data = await image.toByteData();
731+
_toByteDataResult(data?.lengthInBytes ?? 0, null);
732+
} catch (error) {
733+
_toByteDataResult(0, error.toString());
734+
} finally {
735+
image.dispose();
736+
picture.dispose();
737+
}
738+
}
739+
711740
@pragma('vm:entry-point')
712741
Future<void> toImageRetries() async {
713742
final PictureRecorder pictureRecorder = PictureRecorder();

engine/src/flutter/lib/ui/compositing/scene_impeller_gles_unittests.cc renamed to engine/src/flutter/lib/ui/impeller_gles_without_surface_unittests.cc

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
// Reproduces https://github.com/flutter/flutter/issues/192365 end to end:
6-
// `Scene.toImage` from Dart while the view has no window surface, rendered by
7-
// Impeller's OpenGL ES backend.
5+
// Reproduces, end to end, snapshot and readback work from Dart while the view
6+
// has no window surface, rendered by Impeller's OpenGL ES backend:
7+
// - `Scene.toImage` (https://github.com/flutter/flutter/issues/192365).
8+
// - `Image.toByteData` on a raster-owned image
9+
// (https://github.com/flutter/flutter/issues/195627).
810
//
911
// The OpenGL ES driver is |MockGLES|, so this runs on any host. EGL is faked
1012
// just enough to model what matters on Android:
@@ -217,30 +219,47 @@ void ConfigureMockDriver(NiceMock<impeller::testing::MockGLESImpl>& gl) {
217219
.WillByDefault(Return(GL_FRAMEBUFFER_COMPLETE));
218220
}
219221

220-
} // namespace
222+
/// An Impeller OpenGL ES context on |MockGLES| whose reactor may only react on
223+
/// the thread that has |egl| current.
224+
struct FakeGLESContext {
225+
std::shared_ptr<impeller::testing::MockGLES> mock_gles;
226+
std::shared_ptr<FakeEGL> egl;
227+
std::shared_ptr<impeller::ContextGLES> context;
228+
std::shared_ptr<FakeEGLReactorWorker> worker;
229+
};
221230

222-
// In unoptimized builds this stops first at the FML_DCHECK in
223-
// ReactorGLES::CreateUntrackedHandle, when the throwaway AiksContext is built
224-
// with no context current. In optimized builds it crashes with the
225-
// use-after-free in DeviceBufferGLES::OnGetContents from the issue.
226-
TEST_F(ShellTest, SceneToImageWithoutSurfaceOnImpellerGLES) {
231+
FakeGLESContext CreateFakeGLESContext() {
227232
auto mock_gles_impl =
228233
std::make_unique<NiceMock<impeller::testing::MockGLESImpl>>();
229234
ConfigureMockDriver(*mock_gles_impl);
230-
std::shared_ptr<impeller::testing::MockGLES> mock_gles =
235+
FakeGLESContext result;
236+
result.mock_gles =
231237
impeller::testing::MockGLES::Init(std::move(mock_gles_impl));
232-
233-
auto egl = std::make_shared<FakeEGL>();
234-
std::shared_ptr<impeller::ContextGLES> context =
238+
result.egl = std::make_shared<FakeEGL>();
239+
result.context =
235240
impeller::ContextGLES::Create(impeller::Flags{},
236241
std::make_unique<impeller::ProcTableGLES>(
237242
impeller::testing::kMockResolverGLES),
238243
ShaderLibraryMappings(),
239244
/*enable_gpu_tracing=*/false);
240-
ASSERT_TRUE(context);
241-
ASSERT_TRUE(std::static_pointer_cast<impeller::Context>(context)->IsValid());
242-
auto worker = std::make_shared<FakeEGLReactorWorker>(egl);
243-
context->AddReactorWorker(worker);
245+
if (result.context) {
246+
result.worker = std::make_shared<FakeEGLReactorWorker>(result.egl);
247+
result.context->AddReactorWorker(result.worker);
248+
}
249+
return result;
250+
}
251+
252+
} // namespace
253+
254+
// In unoptimized builds this stops first at the FML_DCHECK in
255+
// ReactorGLES::CreateUntrackedHandle, when the throwaway AiksContext is built
256+
// with no context current. In optimized builds it crashes with the
257+
// use-after-free in DeviceBufferGLES::OnGetContents from the issue.
258+
TEST_F(ShellTest, SceneToImageWithoutSurfaceOnImpellerGLES) {
259+
FakeGLESContext gles = CreateFakeGLESContext();
260+
ASSERT_TRUE(gles.context);
261+
ASSERT_TRUE(
262+
std::static_pointer_cast<impeller::Context>(gles.context)->IsValid());
244263

245264
Settings settings = CreateSettingsForFixture();
246265
settings.enable_impeller = true;
@@ -255,9 +274,9 @@ TEST_F(ShellTest, SceneToImageWithoutSurfaceOnImpellerGLES) {
255274
.settings = settings,
256275
.task_runners = task_runners,
257276
.platform_view_create_callback =
258-
[&context, &egl](Shell& shell) -> std::unique_ptr<PlatformView> {
277+
[&gles](Shell& shell) -> std::unique_ptr<PlatformView> {
259278
return std::make_unique<NoSurfaceGLESPlatformView>(
260-
shell, shell.GetTaskRunners(), context, egl);
279+
shell, shell.GetTaskRunners(), gles.context, gles.egl);
261280
},
262281
});
263282
ASSERT_TRUE(shell->IsSetup());

‎engine/src/flutter/shell/common/rasterizer.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,18 @@ class Rasterizer final : public SnapshotDelegate,
714714
if (surface_) {
715715
return surface_->GetAiksContext();
716716
}
717+
// There is no surface to get an AiksContext from, so construct a new one.
718+
// On OpenGL ES, that requires a current context, so we can't construct one
719+
// directly. Create a snapshot surface to make an offscreen context current,
720+
// then return the snapshot surface's AiksContext. Only OpenGL ES provides a
721+
// snapshot surface, so other backends skip this.
722+
if (snapshot_surface_producer_) {
723+
if (std::unique_ptr<Surface> snapshot_surface =
724+
snapshot_surface_producer_->CreateSnapshotSurface()) {
725+
return snapshot_surface->GetAiksContext();
726+
}
727+
}
728+
// Other backends can construct an AiksContext directly.
717729
if (auto context = impeller_context_->GetContext()) {
718730
return std::make_shared<impeller::AiksContext>(
719731
context, impeller::TypographerContextSkia::Make());

0 commit comments

Comments
 (0)