[CPU raster] SkLine2DPathEffect loses sub-pixel stroke coverage — hairline modulation applied twice; GPU is correct

43 views
Skip to first unread message

L A Romeo (L)

unread,
Aug 11, 2026, 2:38:12 PMAug 11
to skia-discuss
Hi Skia team,

I don't have permission to create on issues.skia.org, so I'm posting here in the hope someone can file it.

SUMMARY

When a paint has a stroke style and a path effect that overrides the stroke rec -- SkLine2DPathEffect is the clear case -- the CPU backend applies its thin-stroke-to-hairline alpha modulation, and the path effect then restores the geometric width. The width change is undone; the alpha change is not. Rendered coverage comes out proportional to width^2 instead of width. The Metal backend renders the same drawing correctly, so the two backends disagree.

REPRO

A line grid on an 8px period. Expected mean alpha over the rect is 255 * w / 8, for any w.

  SkPaint paint;
  paint.setAntiAlias(true);
  paint.setColor(SK_ColorRED);
  paint.setStyle(SkPaint::kStroke_Style);

  SkMatrix lattice;
  lattice.setScale(8, 8);

  const SkScalar w = 0.5f;          // any width below 1 device pixel
  paint.setPathEffect(SkLine2DPathEffect::Make(w, lattice));
  paint.setStrokeWidth(w);

  canvas->drawRect(SkRect::MakeXYWH(10, 10, 40, 40), paint);

Measured mean alpha, sampled over a whole number of periods:

  width    expected    CPU      Metal
  2.0      63.75       63.75    63.75
  1.0      31.88       32.00    32.00
  0.5      15.94        8.00    16.00
  0.25      7.97        1.88     8.00
  0.125     3.98        0.00     0.00

CPU/expected is 0.50 at w=0.5 and 0.24 at w=0.25, i.e. the measured coverage tracks w^2. Metal matches expected to within rounding.

CAUSE

DrawTreatAsHairline() (src/core/SkDrawProcs.h:25) checks style, stroke width and anti-alias, but does not check whether the paint carries a path effect, so it assumes paint.getStrokeWidth() is what will actually be stroked.

modifyPaintForHairlines() (src/core/SkDraw.cpp:979) then acts on that:

  paint.setStrokeWidth(0);                 // 1px hairline
  paint.setAlpha(origAlpha * coverage);    // dimmed by the real width

which is correct in isolation.

SkLine2DPathEffectImpl::onFilterPath() (src/effects/Sk2DPathEffect.cpp:127) subsequently calls rec->setStrokeStyle(fWidth), restoring the geometric width while the alpha reduction stays applied. Net: geometry at width w, alpha scaled by w, so coverage w^2.

modifyPaintForHairlines is CPU-only, which is why the GPU path is unaffected.

CONFIRMING TEST

Raising only the paint's stroke width to 10 -- which the effect overrides, so it cannot change the stroking -- suppresses the hairline substitution and restores exact coverage:

  effect width   expected   setStrokeWidth(w)   setStrokeWidth(10)
  0.5            15.94       8.00               16.00
  0.25            7.97       1.88                8.00

Separately, SkPathUtils::FillPathWithPaint() with the same paint produces correct geometry at every width (bounds exactly centre +/- w/2, same verb count) and filling that path renders correctly. So the effect's output is fine; only the direct drawPath route loses it. Two public entry points that should agree, disagree.

SUGGESTED FIX

An early return in DrawTreatAsHairline when a path effect is present:

  if (paint.getPathEffect()) {
      return false;    // the effect may rewrite the stroke rec
  }

or re-evaluating the hairline decision after filterPath has returned its final SkStrokeRec.

VERSION

Still present on main as of today -- DrawTreatAsHairline has no path-effect check. Observed on SK_MILESTONE 150 (via skia-safe 0.99.0), macOS arm64, CPU raster vs Metal.

LOWER-PRIORITY ASIDE

At w = 0.125 both backends render nothing, including when the geometry is materialised first via FillPathWithPaint -- so this is separate from the above. A plain drawLine at the same width renders correctly (coverage within 6% of ideal), so strokes and fills differ here. Possibly intended, but worth confirming.

Thanks,

Reply all
Reply to author
Forward
0 new messages