@@ -828,6 +828,64 @@ void testMain() {
828828 await matchGoldenFile ('canvaskit_empty_scene.png' ,
829829 region: const ui.Rect .fromLTRB (0 , 0 , 100 , 100 ));
830830 });
831+
832+ // Regression test for https://github.com/flutter/flutter/issues/121758
833+ test ('resources used in temporary surfaces for Image.toByteData can cross to rendering overlays' , () async {
834+ final Rasterizer rasterizer = CanvasKitRenderer .instance.rasterizer;
835+ SurfaceFactory .instance.debugClear ();
836+
837+ ui.platformViewRegistry.registerViewFactory (
838+ 'test-platform-view' ,
839+ (int viewId) => createDomHTMLDivElement ()..id = 'view-0' ,
840+ );
841+ await createPlatformView (0 , 'test-platform-view' );
842+
843+ CkPicture makeTextPicture (String text, ui.Offset offset) {
844+ final CkPictureRecorder recorder = CkPictureRecorder ();
845+ final CkCanvas canvas = recorder.beginRecording (ui.Rect .largest);
846+ final CkParagraphBuilder builder = CkParagraphBuilder (CkParagraphStyle ());
847+ builder.addText (text);
848+ final CkParagraph paragraph = builder.build ();
849+ paragraph.layout (const ui.ParagraphConstraints (width: 100 ));
850+ canvas.drawRect (
851+ ui.Rect .fromLTWH (offset.dx, offset.dy, paragraph.width, paragraph.height).inflate (10 ),
852+ CkPaint ()..color = const ui.Color (0xFF00FF00 )
853+ );
854+ canvas.drawParagraph (paragraph, offset);
855+ return recorder.endRecording ();
856+ }
857+
858+ CkPicture imageToPicture (CkImage image, ui.Offset offset) {
859+ final CkPictureRecorder recorder = CkPictureRecorder ();
860+ final CkCanvas canvas = recorder.beginRecording (ui.Rect .largest);
861+ canvas.drawImage (image, offset, CkPaint ());
862+ return recorder.endRecording ();
863+ }
864+
865+ final CkPicture helloPicture = makeTextPicture ('Hello' , ui.Offset .zero);
866+
867+ final CkImage helloImage = helloPicture.toImageSync (100 , 100 );
868+
869+ // Calling toByteData is essential to hit the bug.
870+ await helloImage.toByteData (format: ui.ImageByteFormat .png);
871+
872+ final LayerSceneBuilder sb = LayerSceneBuilder ();
873+ sb.pushOffset (0 , 0 );
874+ sb.addPicture (ui.Offset .zero, helloPicture);
875+ sb.addPlatformView (0 , width: 10 , height: 10 );
876+
877+ // The image is rendered after the platform view so that it's rendered into
878+ // a separate surface, which is what triggers the bug. If the bug is present
879+ // the image will not appear on the UI.
880+ sb.addPicture (const ui.Offset (0 , 50 ), imageToPicture (helloImage, ui.Offset .zero));
881+ sb.pop ();
882+
883+ // The below line should not throw an error.
884+ rasterizer.draw (sb.build ().layerTree);
885+
886+ await matchGoldenFile ('cross_overlay_resources.png' , region: const ui.Rect .fromLTRB (0 , 0 , 100 , 100 ));
887+ });
888+
831889 // TODO(hterkelsen): https://github.com/flutter/flutter/issues/71520
832890 }, skip: isSafari || isFirefox);
833891}
0 commit comments