Skip to content

Commit 1c7c4db

Browse files
[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.md
1 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/canvaskit_api.dart‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2388,14 +2388,12 @@ extension type SkPartialImageInfo._(JSObject _) implements JSObject {
23882388

23892389
@JS('window.flutterCanvasKit.RuntimeEffect')
23902390
extension type SkRuntimeEffect(JSObject _) implements JSObject {
2391-
@JS('makeShader')
2392-
external SkShader? _makeShader(JSAny uniforms);
2393-
SkShader? makeShader(SkFloat32List uniforms) => _makeShader(uniforms.toJSAnyShallow);
2391+
external SkShader? makeShader(JSFloat32Array uniforms);
23942392

23952393
@JS('makeShaderWithChildren')
2396-
external SkShader? _makeShaderWithChildren(JSAny uniforms, JSAny children);
2397-
SkShader? makeShaderWithChildren(SkFloat32List uniforms, List<Object?> children) =>
2398-
_makeShaderWithChildren(uniforms.toJSAnyShallow, children.toJSAnyShallow);
2394+
external SkShader? _makeShaderWithChildren(JSFloat32Array uniforms, JSAny children);
2395+
SkShader? makeShaderWithChildren(JSFloat32Array uniforms, List<Object?> children) =>
2396+
_makeShaderWithChildren(uniforms, children.toJSAnyShallow);
23992397
}
24002398

24012399
@JS('window.flutterCanvasKit.RuntimeEffect.Make')

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

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

5+
import 'dart:js_interop';
56
import 'dart:typed_data';
67

78
import 'package:meta/meta.dart';
@@ -50,14 +51,14 @@ class CkFragmentProgram implements ui.FragmentProgram {
5051

5152
class CkFragmentShader extends BackendFragmentShader implements CkShader {
5253
CkFragmentShader(this.name, this.effect, this._program)
53-
: floats = mallocFloat32List(_program.floatCount + _program.textureCount * 2),
54+
: floats = Float32List(_program.floatCount + _program.textureCount * 2),
5455
samplers = List<SkShader?>.filled(_program.textureCount, null),
5556
lastFloatIndex = _program.floatCount;
5657

5758
final String name;
5859
final SkRuntimeEffect effect;
5960
final int lastFloatIndex;
60-
final SkFloat32List floats;
61+
final Float32List floats;
6162
final List<SkShader?> samplers;
6263
final CkFragmentProgram _program;
6364

@@ -72,9 +73,15 @@ class CkFragmentShader extends BackendFragmentShader implements CkShader {
7273
assert(!_debugDisposed, 'FragmentShader has been disposed of.');
7374
ref?.dispose();
7475

76+
// Pass the uniforms as a plain JS typed array (not CanvasKit-malloc'd
77+
// memory) so that CanvasKit copies them into a buffer owned by the new
78+
// SkShader. Otherwise pictures that recorded this shader would alias the
79+
// mutable staging buffer and observe later `setFloat` calls or `dispose`.
80+
// See https://github.com/flutter/flutter/issues/193221.
81+
final JSFloat32Array uniformData = floats.toJS;
7582
final SkShader? result = samplers.isEmpty
76-
? effect.makeShader(floats)
77-
: effect.makeShaderWithChildren(floats, samplers);
83+
? effect.makeShader(uniformData)
84+
: effect.makeShaderWithChildren(uniformData, samplers);
7885
if (result == null) {
7986
throw Exception(
8087
'Invalid uniform data for shader $name:'
@@ -90,7 +97,7 @@ class CkFragmentShader extends BackendFragmentShader implements CkShader {
9097
@override
9198
void setFloat(int index, double value) {
9299
assert(!_debugDisposed, 'FragmentShader has been disposed of.');
93-
floats.toTypedArray()[index] = value;
100+
floats[index] = value;
94101
}
95102

96103
@override
@@ -110,7 +117,6 @@ class CkFragmentShader extends BackendFragmentShader implements CkShader {
110117
}());
111118
ref?.dispose();
112119
ref = null;
113-
free(floats);
114120
}
115121

116122
bool _debugDisposed = false;

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -377,12 +377,12 @@ half4 main(vec2 fragCoord) {
377377
final SkRuntimeEffect? invalidEffect = MakeRuntimeEffect(kInvalidSkSlProgram);
378378
expect(invalidEffect, isNull);
379379

380-
final SkFloat32List emptyUniforms = mallocFloat32List(0);
380+
final JSFloat32Array emptyUniforms = Float32List(0).toJS;
381381
final SkShader? shader = effect!.makeShader(emptyUniforms);
382382
expect(shader, isNotNull);
383383

384384
// mismatched uniforms returns null.
385-
final SkFloat32List mismatchedUniforms = mallocFloat32List(1);
385+
final JSFloat32Array mismatchedUniforms = Float32List(1).toJS;
386386
final SkShader? invalidShader = effect.makeShader(mismatchedUniforms);
387387

388388
expect(invalidShader, isNull);
@@ -395,16 +395,15 @@ return u_color;
395395
}
396396
''';
397397

398-
final SkFloat32List uniforms = mallocFloat32List(4);
399-
final Float32List uniformData = uniforms.toTypedArray();
398+
final uniformData = Float32List(4);
400399

401400
uniformData[0] = 1.0;
402401
uniformData[1] = 0.0;
403402
uniformData[2] = 0.0;
404403
uniformData[3] = 1.0;
405404

406405
final SkShader? shaderWithUniform = MakeRuntimeEffect(kSkSlProgramWithUniforms)!
407-
.makeShader(uniforms);
406+
.makeShader(uniformData.toJS);
408407

409408
expect(shaderWithUniform, isNotNull);
410409
});

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -362,14 +362,14 @@ void testMain() {
362362
final ui.UniformArray<ui.UniformVec2Slot> vec2Array = shader.getUniformVec2Array('uVectors');
363363
expect(vec2Array.length, 3);
364364
vec2Array[0].set(1.0, 2.0);
365-
expect(shader.floats.toTypedArray()[13], 1.0);
366-
expect(shader.floats.toTypedArray()[14], 2.0);
365+
expect(shader.floats[13], 1.0);
366+
expect(shader.floats[14], 2.0);
367367
vec2Array[1].set(3.0, 4.0);
368-
expect(shader.floats.toTypedArray()[15], 3.0);
369-
expect(shader.floats.toTypedArray()[16], 4.0);
368+
expect(shader.floats[15], 3.0);
369+
expect(shader.floats[16], 4.0);
370370
vec2Array[2].set(5.0, 6.0);
371-
expect(shader.floats.toTypedArray()[17], 5.0);
372-
expect(shader.floats.toTypedArray()[18], 6.0);
371+
expect(shader.floats[17], 5.0);
372+
expect(shader.floats[18], 6.0);
373373

374374
final ui.UniformArray<ui.UniformMat4Slot> mat4Array = shader.getUniformMat4Array('uMatrices');
375375
expect(mat4Array.length, 2);

0 commit comments

Comments
 (0)