fix(🎨): honor the optional paint and blend mode in drawPatch - #4076
Merged
wcandillon merged 3 commits intoSep 23, 2026
Merged
Conversation
SkCanvas.drawPatch declares `mode` and `paint` as optional, but neither the native nor the web implementation accepted their absence. Natively, the paint was read from `arguments[4]` whenever `count >= 4`, so a four-argument call read one slot past the end of the JSI argument array, and the resulting null paint was then dereferenced, crashing the app. The blend mode was read with an unconditional `arguments[3].asNumber()`, which throws for a null or omitted mode. On web the missing paint reached CanvasKit as `undefined` and threw a TypeError. Both layers now fall back to a default-constructed paint and to SkBlendMode::kModulate, matching the four-argument SkCanvas::drawPatch overload and CanvasKit's own default. drawAtlas had the same class of bug: its blend mode lives at index 4 but was guarded by `count > 5`, so a five-argument call silently dropped it. Skia ignores that blend mode when no colors are supplied, so no rendering changes, but the guard was off by one.
1 task
Contributor
Author
|
I have signed the CLA! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
SkCanvas.drawPatchdeclares bothmodeandpaintas optional:Neither implementation accepted their absence. Fixes #4062.
Native (
cpp/api/JsiSkCanvas.h):arguments[4]whenevercount >= 4, so a four-argument call read one slot past the end of the JSI argument array, and the resulting nullshared_ptrwas then dereferenced — a hard crash rather than an exception;arguments[3].asNumber(), which throws for a null or omitted mode (and reads out of bounds for calls with fewer than four arguments).Web (
src/skia/web/JsiSkCanvas.ts): the missing paint was forwarded to CanvasKit asundefined, which throwsTypeError: Cannot read properties of undefined (reading 'Fd').Both layers now fall back to a default-constructed paint and to
SkBlendMode::kModulate, matching the four-argumentSkCanvas::drawPatchoverload and CanvasKit's own shim (t || (t = BlendMode.Modulate)). The web fallback is a rawnew CanvasKit.Paint()rather thanSkia.Paint(), because the latter enables antialiasing and would not match a default-constructedSkPaintnatively; it is released in afinally.drawAtlashas the same class of bug: its blend mode lives at index 4 but was guarded bycount > 5, so a five-argument call silently dropped it. Worth noting for reviewers: this one is not observable today — Skia ignores the atlas blend mode when nocolorsarray is supplied, and a call that supplies colors already hascount >= 6. I verified this against CanvasKit (SrcvsPlusvsClearwith no colors produce byte-identical output; with colors they differ). The guard was still off by one, so it is corrected here.The declarative renderer is unaffected:
sksg/Recorder/commands/Drawing.tsalways passes all five arguments todrawPatchand all seven todrawAtlas.Test plan
Two tests added to
renderer/__tests__/e2e/CoonPatch.spec.tsx, so they run against CanvasKit locally and against the device in E2E:drawPatch(cubics, colors)must render pixel-identically to the explicitdrawPatch(cubics, colors, null, BlendMode.Modulate, paint)with a default (non-antialiased) paint. Fails before the change with theTypeErrorabove.BlendMode.Modulate, whileBlendMode.SrcOvermust differ. The second assertion keeps the first one honest.Full package suite,
tsc --noEmitandeslint --max-warnings 0are clean. Additional verification on the web path: all six previously-throwing argument shapes (2/3/4 args, modenull, paintundefined, paintnull) now match the reference rendering exactly (0 of 16384 bytes differ); the shapes that already worked are byte-for-byte unchanged; 40k calls growCanvasKit.HEAPU8by 0 bytes, and adrawPatchthat throws leaves the canvas usable.I could not build the native side locally, so the C++ change is covered by review and by the added E2E tests rather than by a local device run.