Repository navigation
Commit 1c7c4db
authored
[web] Fix CanvasKit fragment shader uniforms being freed or mutated after recording (flutter#195718)
On the CanvasKit renderer, a `ui.FragmentShader` disposed after a draw
call (but before the recorded `ui.Picture` is rasterized) corrupted the
picture. `Picture.toImage` sampled black instead of the shader's color,
and a later shader could take over the earlier draw's colors.
The cause: `CkFragmentShader` kept its uniforms in a buffer allocated
with `CanvasKit.Malloc` and passed that buffer straight to
`RuntimeEffect.makeShader`. For malloc'd arrays, CanvasKit wraps the
memory with `SkData::MakeWithoutCopy`, so every recorded `SkShader`
aliased the shader's mutable staging buffer, and `dispose()` then freed
it. The GC finalizer frees it the same way, so apps that never call
`dispose()` could hit this too, depending on GC timing.
This PR stores the uniforms in a plain Dart `Float32List` and passes
`floats.toJS` to `makeShader` / `makeShaderWithChildren`. CanvasKit
copies a non-malloc'd typed array into its own allocation, and the
resulting `SkShader` owns it (`SkData::MakeFromMalloc`). Each recorded
draw therefore gets its own uniform snapshot. Native Flutter's
`ReusableFragmentShader` behaves the same way: it copies the uniforms on
every draw. `dispose()` no longer frees a staging buffer; it only
releases the cached `SkShader` handle.
Fixes flutter#193221
## Behavioral Changes
These apply to the CanvasKit renderer only; Skwasm is unchanged.
- Disposing a fragment shader after recording, before rasterizing:
- Before: `picture.toImage` returned corrupted pixels, e.g. `[0, 0, 0,
255]` for a shader that outputs opaque red.
- After: it returns `[255, 0, 0, 255]`, the color the shader had when it
was drawn.
- Disposing shader A after drawing with it, then creating and drawing
shader B, in the same picture:
- Before: shader B could reuse A's freed uniform memory, so A's box
rendered in B's color.
- After: each box keeps its own color.
- Calling `setFloat` / uniform setters on a shader between draw calls in
the same picture:
- Before: every earlier draw took on the latest uniform values.
- After: each draw keeps the uniforms it was recorded with.
- Image-sampler shaders: the texture-size uniforms recorded with a draw
no longer get corrupted when the shader is disposed before
rasterization.
## Where to focus
- **`lib/src/engine/canvaskit/fragment_shader.dart`** holds the core
change.
- The key point is the comment in `skShader`: correctness depends on
passing a *non*-malloc'd typed array, because CanvasKit only takes
ownership of a copy in that case (it checks the `_ck` marker in
`rt_shader.js`).
- This holds on dart2js, where `toJS` returns the same array, and on
dart2wasm, where `toJS` produces a fresh JS array.
- **`lib/src/engine/canvaskit/canvaskit_api.dart`**:
`SkRuntimeEffect.makeShader` and `makeShaderWithChildren` now accept
`JSAny` uniforms instead of `SkFloat32List`. Existing callers that pass
`SkFloat32List` (the API tests) still compile.
- **Cost:** each `skShader` call now does one small malloc plus a copy
of N floats. This matches native behavior. Caching the `SkShader` across
draws while uniforms are unchanged is intentionally left for a
follow-up.
- **Skwasm:** Skwasm already keeps disposed-shader uniforms alive
through ref-counting, so the disposal tests pass there. It does still
share a single uniform buffer between draws of the same shader. That's
why the per-draw mutation test is skipped on Skwasm, with a comment
explaining it. Fixing that is a separate change.
- **`test/ui/fragment_shader_test.dart`**: pixel-readback regression
tests, one per aliasing route:
- dispose then rasterize, including re-rasterizing and nesting the
picture
- freed-memory reuse by a later shader
- image-sampler dispose
- per-draw uniform mutation
- sampler replacement between draws
- a shader with no uniforms
## 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.
- [x] 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.md1 parent 74d5456 commit 1c7c4db
5 files changed
Lines changed: 311 additions & 23 deletions
File tree
- engine/src/flutter/lib/web_ui
- lib/src/engine/canvaskit
- test
- canvaskit
- ui
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2388 | 2388 | | |
2389 | 2389 | | |
2390 | 2390 | | |
2391 | | - | |
2392 | | - | |
2393 | | - | |
| 2391 | + | |
2394 | 2392 | | |
2395 | 2393 | | |
2396 | | - | |
2397 | | - | |
2398 | | - | |
| 2394 | + | |
| 2395 | + | |
| 2396 | + | |
2399 | 2397 | | |
2400 | 2398 | | |
2401 | 2399 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
50 | 51 | | |
51 | 52 | | |
52 | 53 | | |
53 | | - | |
| 54 | + | |
54 | 55 | | |
55 | 56 | | |
56 | 57 | | |
57 | 58 | | |
58 | 59 | | |
59 | 60 | | |
60 | | - | |
| 61 | + | |
61 | 62 | | |
62 | 63 | | |
63 | 64 | | |
| |||
72 | 73 | | |
73 | 74 | | |
74 | 75 | | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
75 | 82 | | |
76 | | - | |
77 | | - | |
| 83 | + | |
| 84 | + | |
78 | 85 | | |
79 | 86 | | |
80 | 87 | | |
| |||
90 | 97 | | |
91 | 98 | | |
92 | 99 | | |
93 | | - | |
| 100 | + | |
94 | 101 | | |
95 | 102 | | |
96 | 103 | | |
| |||
110 | 117 | | |
111 | 118 | | |
112 | 119 | | |
113 | | - | |
114 | 120 | | |
115 | 121 | | |
116 | 122 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
377 | 377 | | |
378 | 378 | | |
379 | 379 | | |
380 | | - | |
| 380 | + | |
381 | 381 | | |
382 | 382 | | |
383 | 383 | | |
384 | 384 | | |
385 | | - | |
| 385 | + | |
386 | 386 | | |
387 | 387 | | |
388 | 388 | | |
| |||
395 | 395 | | |
396 | 396 | | |
397 | 397 | | |
398 | | - | |
399 | | - | |
| 398 | + | |
400 | 399 | | |
401 | 400 | | |
402 | 401 | | |
403 | 402 | | |
404 | 403 | | |
405 | 404 | | |
406 | 405 | | |
407 | | - | |
| 406 | + | |
408 | 407 | | |
409 | 408 | | |
410 | 409 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
362 | 362 | | |
363 | 363 | | |
364 | 364 | | |
365 | | - | |
366 | | - | |
| 365 | + | |
| 366 | + | |
367 | 367 | | |
368 | | - | |
369 | | - | |
| 368 | + | |
| 369 | + | |
370 | 370 | | |
371 | | - | |
372 | | - | |
| 371 | + | |
| 372 | + | |
373 | 373 | | |
374 | 374 | | |
375 | 375 | | |
| |||
0 commit comments