Dithering is anchored to the device, not the pixel grid

#356913465 Skia raster + Ganesh + Graphite Skia CL 1405397 In Review

The bug

Every Skia backend indexes its 8x8 ordered dither matrix with device coordinates: the raster pipeline uses dx/dy, the GPU backends use sk_FragCoord. A saveLayer gets its own device whose pixels start at the layer's origin, so the same gradient dithers in a different phase inside a layer than it does drawn straight into the surface. Whether the two agree is purely a function of where the layer happens to land.

// src/opts/SkRasterPipeline_opts.h, before
U32 X = U32_(dx) + sk_unaligned_load<U32>(iota),
    Y = U32_(dy);

dx/dy are device-local, so a layer at an off-grid origin reads the matrix at the wrong offset.

Shape of the defect: every layer origin in an 8x8 window (raster)

Mismatching pixels between a dithered gradient drawn into a saveLayer at origin (x, y) and the same gradient drawn directly into the surface. 600 pixels compared per cell.

Layer origin sweep

Grid of layer origins; only 0,0 shows zero mismatching pixels
Only origin (0, 0) is correct. The other 63 offsets are all wrong, up to 369 of 600 pixels. The checkerboard reflects the matrix's own 2x2 structure.
Grid of layer origins; all cells show zero mismatching pixels
All 64 origins agree with the direct draw.

The raster fix

Carry the device-to-global offset on skcpu::Draw, pass it into blitter creation, and add it to the coordinates that index the matrix, so the pattern is anchored to the global pixel grid rather than to each device.

// after
U32 X = (U32_(dx) + sk_unaligned_load<U32>(iota)) + U32_(ctx->originX),
    Y = U32_(dy) + U32_(ctx->originY);

Three call sites populate it, all guarded by isPixelAlignedToGlobal(): the plain draw path, the tiler (which adds the tile origin so large surfaces stay in phase across tiles), and BDDraw. A device at the origin is unaffected, which is why the (0, 0) cell was already green.

Raster verification

checkresult
Gradient (new subtest, layer at 3,5) fails without the fix at the first compared pixel (0xff0e0e0e vs 0xff0d0d0d), passes with it
dm --src tests --nogpu 2553 run; 4 pre-existing font failures, identical on unpatched origin/main
dm --src gm --config 8888 1082 GMs, no diffs
serialize-8888, pic-8888 vias gradient / dither / savelayer GMs, no diffs

The regression test lives in tests/GradientTest.cpp as test_dither_phase_in_offset_layer, picking a layer origin that is on neither axis of the grid.

The GPU backends have the same bug

Ganesh (make_dither_effect) and Graphite (AddDitherBlock) sample the same 8x8 LUT, but index it with sk_FragCoord, which is render-target-local. A layer is its own render target, so the phase shifts by the layer origin exactly as it did on the CPU. Running the same 8x8 origin sweep through both backends confirms it. Ganesh on --config gl: 63 of 64 origins disagree with the direct draw, worst case 369 of 600 pixels. Graphite on Vulkan (--config grvk): 63 of 64 origins, worst case 345 of 600 pixels. Both report the identical signature to the raster sweep, down to the two colors at the first differing pixel.

Layer origin sweep, Ganesh GL

Ganesh GL sweep; only origin 0,0 shows zero mismatching pixels
Ganesh GL, unpatched. Same shape as the raster sweep: only (0, 0) is correct.
Ganesh GL sweep; all cells show zero mismatching pixels
With the dither origin plumbed into the Ganesh paint conversion, all 64 origins agree.

The GPU fix mirrors the raster one: carry the device's isPixelAlignedToGlobal() origin into SkPaintToGrPaint*, hand it to the dither FP as a uniform, and add it to the LUT coordinate.

// after
"uniform float2 ditherOrigin;"
"half value = table.eval(sk_FragCoord.xy + ditherOrigin).a - 0.5;"

Ganesh GL, gradient in a layer at (3, 5), 8x magnified

Diff mask showing many mismatching pixels inside the layer rect
Mismatches against the direct draw, inside the layer rect only: 361 of 600 pixels. Red is a disagreement, green is a match.
Diff mask showing no mismatching pixels inside the layer rect
0 of 600. The layer now dithers in the phase of the surface it composites into.

GPU verification

checkresult
GradientDitherPhaseInOffsetLayer, --config gl fails unpatched at the first compared pixel (0xff0e0e0e vs 0xff0d0d0d), the same values the raster test reports; passes with the fix
8x8 origin sweep, --config gl 63/64 origins bad -> 0/64
GradientDitherPhaseInOffsetLayerGraphite, --config grvk fails with the origin neutralized at the same pixel and the same two colors as raster and Ganesh; passes with the fix
8x8 origin sweep, --config grvk 63/64 origins bad -> 0/64
dm --src gm --config grvk 188 gradient / dither / savelayer / layer / text / glyph / font / blob GMs, no diffs
text, 8x8 origin sweep, both backends Ganesh 63/64 origins bad (worst 176 of 600 px) -> 0/64; Graphite 63/64 (worst 167) -> 0/64; raster 0/64 throughout
dm --src gm --config gl 126 gradient / dither / savelayer / layer / text / blur / image GMs, no diffs
dm --src tests --config gl 182 GPU unit tests across Processor, Device, Surface, Image, Gradient, Blend, Shader, Blur, no failures

The regression test is shared between backends: one comparison helper in tests/GradientTest.cpp, driven by a raster DEF_TEST, a DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS and a DEF_GRAPHITE_TEST_FOR_RENDERING_CONTEXTS. Each one checks two draws through the same dithered gradient paint: a filled rect and a string of glyphs.

Text had it too

Adding a glyph draw to the same test found the gap the first pass left open. Ganesh builds the text paint in AtlasTextOp, which sits below the device and so had no origin to pass; it was handing the dither effect a hard-coded zero. The result is the identical defect, and the test pins it to the same two colors the shape case reports:

text dither phase differs at (5, 5): direct 0xff161616, layer 0xff151515

Both AtlasTextOp::Make call sites originate in Device, which already knows its origin, so the fix is to plumb it through SurfaceDrawContext::drawGlyphRunList and into the paint conversion glyphs already share with every other draw. Graphite needed no second change: its text goes through the same KeyContext, so the origin added for shapes covered glyphs as well, which the sweep confirms rather than assumes.

Raster was correct on text from the start: before the Ganesh fix the one shared test passed on the CPU and failed on the GPU, which is what isolated the backend in the first place.

backendtext, beforetext, after
raster0/64 origins bad 0/64
Ganesh GL63/64, worst 176 of 600 px 0/64
Graphite Vulkan63/64, worst 167 of 600 px 0/64

Notes on this machine

Three failure sets show up here and none of them belong to the change. Long sequential runs segfault in SkRuntimeBlender_Ganesh, a test unrelated to dithering, alongside two SkSLTest cases under llvmpipe; rebuilding the tree one commit back reproduces the same crash, in the same test, with the same four failures. The Android precompile goldens fail identically with and without the change across three runs each, because the canned Android pipeline labels do not match this desktop Vulkan driver. FontMgr_MatchFamilyStyle and TextBlob_builder fail for want of system fonts, including under --nogpu, where none of this code runs. Setting those aside, a 2632-test GPU run is clean.

How Chromium surfaced it

background-clip: text paints the background into a transparency layer and masks it with the text via DstIn. The layer starts at the pixel-snapped background rect, so the clipped text dithered out of phase with an equivalent plain block and background-gradient-interpolation-003.html exceeded its fuzzy allowance.

buildtext clip vs reference blocks, 800x600 capture
before16,120 pixels, each off by one level
with this fix0 (captures byte-identical, matching sha256)

The initial Chromium proposal instead enlarged the layer to start on the dither grid, which leaked an 8x8 Skia detail into Blink paint code and changed the clip. With the phase fixed in Skia that change is unnecessary and the WPT passes at its upstream fuzzy value.

WPT difference masks (magenta = differing pixel)

Before: magenta covers the gradient area of all three samples
16,120 differing pixels.
Blink workaround: only glyph edges remain magenta
1,415 differing pixels with the rejected Blink-side clip change: glyph edges only.
This fix: blank white mask, no differing pixels
Blank: no differing pixels, with Blink's original clip bounds unchanged.
The same gradient as a block and through background-clip: text, rendered by your browser. The error was 1/255 per channel, so it was never visible to the eye - only to a pixel comparison.

Full captures

Unscaled 800x600 content_shell captures behind the masks above.

The clip and reference captures with the fix applied are byte-identical, so one file stands for both (sha256 e551e22cabeaac...).

Links

Skia CL 1405397 Issue 356913465 Chromium CL 8654518 (parked) sampler.html capture_screenshots.py GPU patch (both backends) GPU patch (Graphite)