background-repeat: space drops background image tiles after zooming in

#589817018 New / P3 / S3 / Blink>Paint

What

At default zoom a 96x96 box tiled with a 32x32 image at background-repeat: space paints a tidy 3x3 grid with no gap. Pressing Ctrl + once (110%) drops a row and a column: Chromium paints only 2x2 tiles and leaves a wide white gap in the middle, even though the box-to-image ratio has not changed.

Root cause

BackgroundImageGeometry::GetSpaceBetweenImageTiles() counts tiles with (area_size / tile_size).ToInt() where area_size is the device-pixel snapped positioning area and tile_size is the unsnapped, zoom-scaled image size. At 110% a 32px image becomes 35.2px, which LayoutUnit::FromFloatFloor stores as 35.1875 (2253/64), and a 96px box becomes 105.6px which snaps to either 105 or 106 depending on sub-pixel offset. When snapping lands on 105, 105 / 35.1875 = 2.98 and ToInt() truncates to 2 - one tile short of what actually fits. The caller only distinguishes > 1 from ≤ 1, so this silently drops the third tile.

Before / after

Chromium at 110% browser zoom, same page both captures. Before uses main at 816b52acbd236; after is the same build with the patch below.

before2x2 with white gap
Before: 2x2 aqua tiles with a wide white cross-shaped gap.
after3x3, no gap
After: 3x3 aqua tiles filling the box with no gap.

Cross-browser

Same page, 110% zoom. Chromium pre-fix and WebKit2GTK 2.50.6 both drop a row and a column; Firefox 153 is clean. Firefox's ComputeSpacedRepeatSize divides as float (not fixed-point LayoutUnit), so 105.6 / 35.2 lands on a clean 3.0.

Firefox 153.5 esr3x3, no gap
Firefox 110% zoom: 3x3 aqua tiles.
WebKit2GTK 2.50.62x2 with gap
WebKit 110% zoom: 2x2 aqua tiles with a wide white cross-shaped gap.

Live sampler

Each card renders live in your browser (Rendered, requires Ctrl + once to simulate the reporter's step) next to the pre-fix (Before, main at 816b52acbd236) and post-fix (After) captures. On a build with the fix, Rendered at 110% matches After; on pre-fix Chrome the aqua grid loses a row and a column.

Reporter's repro (96x96 box, 32x32 image, press Ctrl + once)
before: 2x2 with white gap
after: 3x3 no gap

Red / green

TestBeforeAfter
wpt_internal/css/css-backgrounds/background-repeat-space-zoomed.html (new reftest) fail (reference mismatch) pass
web tests: external/wpt/css/css-backgrounds + fast/backgrounds + fast/borders + wpt_internal/css/css-backgrounds (984) all ran as expected

The fix

// third_party/blink/renderer/core/paint/background_image_geometry.cc
inline LayoutUnit GetSpaceBetweenImageTiles(LayoutUnit area_size,
                                            LayoutUnit tile_size,
                                            LayoutUnit count_area_size) {
  int number_of_tiles = (count_area_size / tile_size).ToInt();
  LayoutUnit space(-1);
  if (number_of_tiles > 1) {
    space = (area_size - number_of_tiles * tile_size).ClampNegativeToZero() /
            (number_of_tiles - 1);
  }
  return space;
}

// Caller now passes the unsnapped positioning area for counting so a
// sub-pixel of snapping loss cannot drop a whole tile:
LayoutUnit space = GetSpaceBetweenImageTiles(
    snapped_positioning_area_size.width, tile_size_.width,
    unsnapped_positioning_area_size.width);

Links

Issue 589817018 · full repro · CSS Backgrounds 3 - the background-repeat property