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 ());
0 commit comments