Skip to content

Commit 1b9ee1a

Browse files
[CP-beta][web] Make resize and render atomic on the shared offscreen surface (flutter#195839)
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) Cherry-pick of flutter#194401. ### Issue Link: flutter#185034 ### Impact Description: In multi-view Flutter Web apps, views rendered at the same time race on a shared offscreen surface and end up with the wrong canvas size, clipping or overflowing their host HTML containers. Skwasm's `Image.toByteData` uses the same surface and has the same problem. This is a regression since Flutter 3.41 (flutter#179473) that affects deployed multi-view web apps on both CanvasKit and Skwasm and has been blocking production apps from upgrading past 3.38. ### Changelog Description: [flutter/185034](flutter#185034): When rendering multiple views on the web, canvases may fail to match their host container dimensions. ### Workaround: None. ### 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: Follow the repro steps in flutter#185034, or run the new tests in `engine/src/flutter/lib/web_ui/test/ui/multi_view_sizing_test.dart`.
1 parent e34e4dd commit 1b9ee1a

14 files changed

Lines changed: 459 additions & 46 deletions

File tree

‎engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/surface.dart‎

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -305,10 +305,14 @@ abstract class CkSurface extends Surface {
305305
@override
306306
Future<void> rasterizeToCanvas(ui.Picture picture) async {
307307
await _initialized.future;
308+
_drawPicture(picture as CkPicture);
309+
}
310+
311+
/// Synchronously clears the surface, draws [picture] into it, and flushes.
312+
void _drawPicture(CkPicture picture) {
308313
final canvas = CkCanvas.fromSkCanvas(_skSurface!.getCanvas());
309-
final ckPicture = picture as CkPicture;
310314
canvas.clear(const ui.Color(0x00000000));
311-
canvas.drawPicture(ckPicture);
315+
canvas.drawPicture(picture);
312316
_skSurface!.flush();
313317
}
314318

@@ -334,14 +338,39 @@ class CkOffscreenSurface extends CkSurface implements OffscreenSurface {
334338
}
335339

336340
@override
337-
Future<List<DomImageBitmap>> rasterizeToImageBitmaps(List<ui.Picture> pictures) async {
341+
Future<List<DomImageBitmap>> rasterizeToImageBitmaps(
342+
BitmapSize size,
343+
List<ui.Picture> pictures,
344+
) async {
338345
await _initialized.future;
339-
final bitmaps = <DomImageBitmap>[];
346+
// Everything from here until all bitmaps have been requested must be
347+
// synchronous so that no other operation can resize or draw into the
348+
// canvas in between. `createImageBitmap` copies the canvas contents at the
349+
// time it is called; only the resolution of the returned promise is
350+
// asynchronous.
351+
setSize(size);
352+
final bitmapFutures = <Future<DomImageBitmap>>[];
340353
for (final picture in pictures) {
341-
await rasterizeToCanvas(picture);
342-
bitmaps.add(await createImageBitmap(_canvas));
354+
_drawPicture(picture as CkPicture);
355+
bitmapFutures.add(createImageBitmap(_canvas));
356+
}
357+
return Future.wait(bitmapFutures);
358+
}
359+
360+
@override
361+
Future<void> rasterizeToCanvasSource(
362+
BitmapSize size,
363+
List<ui.Picture> pictures,
364+
void Function(int index, DomCanvasImageSource source) onRendered,
365+
) async {
366+
await _initialized.future;
367+
// Everything from here on must be synchronous so that no other operation
368+
// can resize or draw into the canvas while `onRendered` reads from it.
369+
setSize(size);
370+
for (var i = 0; i < pictures.length; i++) {
371+
_drawPicture(pictures[i] as CkPicture);
372+
onRendered(i, canvasImageSource);
343373
}
344-
return bitmaps;
345374
}
346375

347376
@override

‎engine/src/flutter/lib/web_ui/lib/src/engine/compositing/offscreen_canvas_rasterizer.dart‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,11 @@ class OffscreenCanvasViewRasterizer extends ViewRasterizer {
6464
createCanvas: () => RenderCanvas(),
6565
);
6666

67+
// Nothing to prepare. The shared offscreen surface is resized as part of
68+
// each `rasterize` call. Resizing it here, as a separate step, would let
69+
// another view resize it again before this view renders.
6770
@override
68-
Future<void> prepareToDraw() async {
69-
await rasterizer.offscreenSurface.setSize(currentFrameSize);
70-
}
71+
Future<void> prepareToDraw() => Future<void>.value();
7172

7273
@override
7374
Future<void> rasterize(
@@ -81,19 +82,21 @@ class OffscreenCanvasViewRasterizer extends ViewRasterizer {
8182
recorder?.recordRasterStart();
8283
if (browserSupportsCreateImageBitmap) {
8384
final List<DomImageBitmap> bitmaps = await rasterizer.offscreenSurface
84-
.rasterizeToImageBitmaps(pictures);
85+
.rasterizeToImageBitmaps(currentFrameSize, pictures);
8586
for (var i = 0; i < displayCanvases.length; i++) {
8687
(displayCanvases[i] as RenderCanvas).render(bitmaps[i]);
8788
}
8889
} else {
89-
for (var i = 0; i < displayCanvases.length; i++) {
90-
await rasterizer.offscreenSurface.rasterizeToCanvas(pictures[i]);
91-
(displayCanvases[i] as RenderCanvas).renderWithNoBitmapSupport(
92-
rasterizer.offscreenSurface.canvasImageSource,
90+
await rasterizer.offscreenSurface.rasterizeToCanvasSource(currentFrameSize, pictures, (
91+
int index,
92+
DomCanvasImageSource source,
93+
) {
94+
(displayCanvases[index] as RenderCanvas).renderWithNoBitmapSupport(
95+
source,
9396
currentFrameSize.height,
9497
currentFrameSize,
9598
);
96-
}
99+
});
97100
}
98101
recorder?.recordRasterFinish();
99102
}

‎engine/src/flutter/lib/web_ui/lib/src/engine/compositing/rasterizer.dart‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ abstract class ViewRasterizer {
9292

9393
/// Do some initialization to prepare to draw a frame.
9494
///
95-
/// For example, in the [OffscreenCanvasRasterizer], this ensures the backing
96-
/// [OffscreenCanvas] is the correct size to draw the frame.
95+
/// For example, in the [MultiSurfaceRasterizer], this ensures the base
96+
/// canvas is the correct size to draw the frame.
9797
Future<void> prepareToDraw();
9898

9999
/// Rasterizes the given [pictures] into the [displayCanvases].

‎engine/src/flutter/lib/web_ui/lib/src/engine/compositing/surface.dart‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,34 @@ abstract class Surface {
108108
/// A rendering surface that is optimized for producing `DomImageBitmap` objects.
109109
///
110110
/// This surface is not attached to the DOM and is used for off-screen rendering.
111+
///
112+
/// A single [OffscreenSurface] may be shared by multiple views (and, in some
113+
/// renderers, by image rasterization). Therefore, operations that depend on the
114+
/// size of the surface take the size as an argument and resize the surface as
115+
/// part of the same indivisible operation. This guarantees that no other
116+
/// operation can resize the surface between the resize and the capture.
111117
abstract class OffscreenSurface extends Surface {
112-
/// Rasterizes the given list of [pictures] into a list of `DomImageBitmap`
113-
/// objects.
114-
Future<List<DomImageBitmap>> rasterizeToImageBitmaps(List<ui.Picture> pictures);
118+
/// Resizes this surface to [size] and rasterizes each of the given [pictures]
119+
/// into a `DomImageBitmap` of exactly [size].
120+
///
121+
/// Resizing, rendering, and capturing the bitmaps happen atomically with
122+
/// respect to every other operation on this surface.
123+
Future<List<DomImageBitmap>> rasterizeToImageBitmaps(BitmapSize size, List<ui.Picture> pictures);
124+
125+
/// Resizes this surface to [size] and rasterizes each of the given [pictures]
126+
/// in order, calling [onRendered] synchronously after each picture is
127+
/// rendered, while the surface's [Surface.canvasImageSource] still holds that
128+
/// picture.
129+
///
130+
/// This is used when the browser does not support `createImageBitmap`.
131+
///
132+
/// Resizing, rendering, and every call to [onRendered] happen atomically with
133+
/// respect to every other operation on this surface.
134+
Future<void> rasterizeToCanvasSource(
135+
BitmapSize size,
136+
List<ui.Picture> pictures,
137+
void Function(int index, DomCanvasImageSource source) onRendered,
138+
);
115139
}
116140

117141
/// A rendering surface that is also a `DisplayCanvas`.

‎engine/src/flutter/lib/web_ui/lib/src/engine/primitives/image.dart‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,9 @@ class EngineImage implements ui.Image, StackTraceDebugger {
249249
canvas.drawImage(cloneImage, ui.Offset.zero, ui.Paint());
250250
final ui.Picture picture = recorder.endRecording();
251251

252-
await renderer.pictureToImageSurface.setSize(
253-
BitmapSize(cloneImage.width, cloneImage.height),
254-
);
255-
256252
final List<DomImageBitmap> bitmaps =
257253
await (renderer.pictureToImageSurface as OffscreenSurface).rasterizeToImageBitmaps(
254+
BitmapSize(cloneImage.width, cloneImage.height),
258255
<ui.Picture>[picture],
259256
);
260257
final DomImageBitmap bitmap = bitmaps.single;

‎engine/src/flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/raw/raw_surface.dart‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,16 @@ external void surfaceDestroy(SurfaceHandle surface);
6161
)
6262
external void surfaceSetResourceCacheLimitBytes(SurfaceHandle surface, int bytes);
6363

64-
@Native<Int32 Function(SurfaceHandle, Pointer<PictureHandle>, Int)>(
64+
@Native<Int32 Function(SurfaceHandle, Pointer<PictureHandle>, Int, Int, Int)>(
6565
symbol: 'surface_renderPictures',
6666
isLeaf: true,
6767
)
6868
external CallbackId surfaceRenderPictures(
6969
SurfaceHandle surface,
70-
Pointer<PictureHandle> picture,
70+
Pointer<PictureHandle> pictures,
7171
int count,
72+
int width,
73+
int height,
7274
);
7375

7476
@Native<Int32 Function(SurfaceHandle, ImageHandle, Int)>(

‎engine/src/flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/surface.dart‎

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,9 @@ class SkwasmSurface implements OffscreenSurface {
159159
throw ArgumentError('The image being rasterized must be a Skwasm image.');
160160
}
161161

162-
await setSize(BitmapSize(engineImage.width, engineImage.height));
162+
// The raster thread resizes the surface to fit the image in the same message
163+
// that rasterizes it, so no other operation can resize it in between.
164+
_currentSize = BitmapSize(engineImage.width, engineImage.height);
163165

164166
// Initiate the native rasterization call. This schedules the rasterization on the C++
165167
// side and returns a callback ID that will resolve when the image bytes are ready.
@@ -197,16 +199,29 @@ class SkwasmSurface implements OffscreenSurface {
197199
}
198200

199201
@override
200-
Future<List<DomImageBitmap>> rasterizeToImageBitmaps(List<ui.Picture> pictures) async {
202+
Future<List<DomImageBitmap>> rasterizeToImageBitmaps(
203+
BitmapSize size,
204+
List<ui.Picture> pictures,
205+
) async {
201206
await initialized;
207+
// The size is sent along with the pictures, so the raster thread resizes and
208+
// renders in a single message. Messages are processed in order, so no other
209+
// operation can resize the surface in between.
210+
_currentSize = size;
202211
final int callbackId = withStackScope((StackScope scope) {
203212
final Pointer<PictureHandle> pictureHandles = scope
204213
.allocPointerArray(pictures.length)
205214
.cast<PictureHandle>();
206215
for (var i = 0; i < pictures.length; i++) {
207216
pictureHandles[i] = (pictures[i] as SkwasmPicture).handle;
208217
}
209-
return surfaceRenderPictures(handle, pictureHandles, pictures.length);
218+
return surfaceRenderPictures(
219+
handle,
220+
pictureHandles,
221+
pictures.length,
222+
size.width,
223+
size.height,
224+
);
210225
});
211226
final rasterResult =
212227
(await SkwasmCallbackHandler.instance.registerCallback(callbackId)) as RasterResult;
@@ -269,6 +284,15 @@ class SkwasmSurface implements OffscreenSurface {
269284
// Skwasm.
270285
@override
271286
Future<void> rasterizeToCanvas(ui.Picture picture) {
272-
throw StateError('rasterizeToCanvas is not supported for SkwasmSurface');
287+
throw UnsupportedError('rasterizeToCanvas is not supported for SkwasmSurface');
288+
}
289+
290+
@override
291+
Future<void> rasterizeToCanvasSource(
292+
BitmapSize size,
293+
List<ui.Picture> pictures,
294+
void Function(int index, DomCanvasImageSource source) onRendered,
295+
) {
296+
throw UnsupportedError('rasterizeToCanvasSource is not supported for SkwasmSurface');
273297
}
274298
}

‎engine/src/flutter/lib/web_ui/test/canvaskit/bitmap_less_rendering_test.dart‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ void testMain() {
3232
final pictures = <ui.Picture>[];
3333
pictures.add(_createPicture());
3434

35-
expect(() => surface.rasterizeToImageBitmaps(pictures), throwsUnsupportedError);
35+
expect(
36+
() => surface.rasterizeToImageBitmaps(const BitmapSize(10, 10), pictures),
37+
throwsUnsupportedError,
38+
);
3639
},
3740
);
3841

0 commit comments

Comments
 (0)