Skip to content

Visual Studio Code, rebuilt in Rust

Rust

fix(gpui_apple): draw rounded quads the way Chromium rasterizes them

rokartur13e1423f325a
diff --git a/native/vendor/gpui-fast/crates/gpui/src/style.rs b/native/vendor/gpui-fast/crates/gpui/src/style.rs
index 45989123579..49c0e88b02b 100644
--- a/native/vendor/gpui-fast/crates/gpui/src/style.rs
+++ b/native/vendor/gpui-fast/crates/gpui/src/style.rs
@@ -732,9 +732,11 @@ impl Style {
None => Hsla::default(),
};
border_color.a = 0.;
+ let (background_bounds, background_radii) =
+ self.background_shape(bounds, corner_radii, rem_size);
window.paint_quad(quad(
- bounds,
- corner_radii,
+ background_bounds,
+ background_radii,
background_color.unwrap_or_default(),
Edges::default(),
border_color,
@@ -766,6 +768,42 @@ impl Style {
}
}
+ /// Chromium's BackgroundBleedShrinkBackground: behind an opaque solid border on all four sides of a rounded box,
+ /// the background stops half the border widths inside, so the border's anti-aliased outer edge shows no background.
+ fn background_shape(
+ &self,
+ bounds: Bounds<Pixels>,
+ corner_radii: Corners<Pixels>,
+ rem_size: Pixels,
+ ) -> (Bounds<Pixels>, Corners<Pixels>) {
+ let widths = self.border_widths.to_pixels(rem_size);
+ let rounded = [
+ corner_radii.top_left,
+ corner_radii.top_right,
+ corner_radii.bottom_right,
+ corner_radii.bottom_left,
+ ]
+ .iter()
+ .any(|radius| *radius > Pixels::ZERO);
+ let obscured = self.border_style == BorderStyle::Solid
+ && self.border_color.is_some_and(|color| color.is_opaque())
+ && !widths.any(|width| *width <= Pixels::ZERO);
+ if !rounded || !obscured {
+ return (bounds, corner_radii);
+ }
+ let inset = widths.map(|width| *width / 2.);
+ let shrink = |radius: Pixels, x: Pixels, y: Pixels| (radius - x.max(y)).max(Pixels::ZERO);
+ (
+ bounds.extend(inset.map(|width| -*width)),
+ Corners {
+ top_left: shrink(corner_radii.top_left, inset.left, inset.top),
+ top_right: shrink(corner_radii.top_right, inset.right, inset.top),
+ bottom_right: shrink(corner_radii.bottom_right, inset.right, inset.bottom),
+ bottom_left: shrink(corner_radii.bottom_left, inset.left, inset.bottom),
+ },
+ )
+ }
+
fn is_border_visible(&self) -> bool {
self.border_color
.is_some_and(|color| !color.is_transparent())
diff --git a/native/vendor/gpui-fast/crates/gpui_apple/src/shaders.metal b/native/vendor/gpui-fast/crates/gpui_apple/src/shaders.metal
index 2983aca42ad..890d156d30b 100644
--- a/native/vendor/gpui-fast/crates/gpui_apple/src/shaders.metal
+++ b/native/vendor/gpui-fast/crates/gpui_apple/src/shaders.metal
@@ -35,11 +35,18 @@ float corner_dash_velocity(float dv1, float dv2);
float dash_alpha(float t, float period, float length, float dash_velocity,
float antialias_threshold);
float quarter_ellipse_sdf(float2 point, float2 radii);
+bool border_is_path(Edges_ScaledPixels border, float border_alpha);
+float msaa_outside_inner_corner(float2 point, float2 half_size, float2 border, float2 inner_radii);
+uint conic_segments(float2 radii);
+bool inside_conic_chords(float2 q, float2 radii, uint segments);
+float rounded_corner_coverage(float2 pixel, float2 origin, float2 half_size, float radius);
float pick_corner_radius(float2 center_to_point, Corners_ScaledPixels corner_radii);
+float rrect_coverage(float2 pixel, Bounds_ScaledPixels bounds, Corners_ScaledPixels corner_radii);
float quad_sdf(float2 point, Bounds_ScaledPixels bounds,
Corners_ScaledPixels corner_radii);
float quad_sdf_impl(float2 center_to_point, float corner_radius);
float4 over(float4 below, float4 above);
+float4 premultiplied_8bit(float4 color);
float radians(float degrees);
struct PreparedBackground {
@@ -111,7 +118,7 @@ fragment float4 quad_fragment(QuadFragmentInput input [[stage_in]],
constant Quad *quads
[[buffer(QuadInputIndex_Quads)]]) {
if ((input.quad_id & SIMPLE_PRIMITIVE_FLAG) != 0u) {
- return input.background.color0;
+ return premultiplied_8bit(input.background.color0);
}
Quad quad = quads[input.quad_id];
@@ -176,7 +183,7 @@ fragment float4 quad_fragment(QuadFragmentInput input [[stage_in]],
// Fast path for points that must be part of the background
if (is_within_inner_straight_border && !is_near_rounded_corner) {
- return background;
+ return premultiplied_8bit(background);
}
// Signed distance of the point to the outside edge of the quad's border
@@ -198,11 +205,15 @@ fragment float4 quad_fragment(QuadFragmentInput input [[stage_in]],
} else if (is_beyond_inner_straight_border) {
// Fast path for points that must be outside the inner edge
inner_sdf = -1.0;
+ } else if (border_is_path(quad.border_widths, input.border_color.a)) {
+ float2 inner_radii = max(float2(0.0), float2(corner_radius) - border);
+ inner_sdf = antialias_threshold -
+ msaa_outside_inner_corner(point, half_size, border, inner_radii);
} else if (reduced_border.x == reduced_border.y) {
// Fast path for circular inner edge.
inner_sdf = -(outer_sdf + reduced_border.x);
} else {
- float2 ellipse_radii = max(float2(0.0), float2(corner_radius) - reduced_border);
+ float2 ellipse_radii = max(float2(0.0), float2(corner_radius) - border);
inner_sdf = quarter_ellipse_sdf(corner_center_to_point, ellipse_radii);
}
@@ -391,7 +402,15 @@ fragment float4 quad_fragment(QuadFragmentInput input [[stage_in]],
color = over(background, border_color);
}
- return color * float4(1.0, 1.0, 1.0, saturate(antialias_threshold - outer_sdf));
+ if (corner_radius > 0.0 && corner_center_to_point.x > -1.0 && corner_center_to_point.y > -1.0) {
+ color.a *= rounded_corner_coverage(input.position.xy, float2(quad.bounds.origin.x, quad.bounds.origin.y),
+ half_size, corner_radius);
+ } else {
+ color.a *= saturate(antialias_threshold - outer_sdf);
+ }
+ // Chromium rasterizes a translucent fill into an 8-bit premultiplied layer; quantizing a translucent
+ // border too costs the hover and suggest widgets in port/fixtures/ui-pair/corners.
+ return background.a > 0.0 && background.a < 1.0 ? premultiplied_8bit(color) : color;
}
// Returns the dash velocity of a corner given the dash velocity of the two
@@ -428,20 +447,118 @@ float dash_alpha(
return saturate(antialias_threshold - signed_distance / dash_velocity);
}
-// This approximates distance to the nearest point to a quarter ellipse in a way
-// that is sufficient for anti-aliasing when the ellipse is not very eccentric.
-// The components of `point` are expected to be positive.
-//
-// Negative on the outside and positive on the inside.
+// Distance to a quarter ellipse as Skia's EllipticalRRectEffect measures it: the implicit function over its
+// gradient's length. Negative on the outside and positive on the inside.
float quarter_ellipse_sdf(float2 point, float2 radii) {
- // Scale the space to treat the ellipse like a unit circle
- float2 circle_vec = point / radii;
- float unit_circle_sdf = length(circle_vec) - 1.0;
- // Approximate up-scaling of the length by using the average of the radii.
- //
- // TODO: A better solution would be to use the gradient of the implicit
- // function for an ellipse to approximate a scaling factor.
- return unit_circle_sdf * (radii.x + radii.y) * -0.5;
+ float2 z = point / (radii * radii);
+ float implicit = dot(z, point) - 1.0;
+ return -implicit * rsqrt(max(4.0 * dot(z, z), 1e-4));
+}
+
+// Whether Chromium rasterizes this rounded border as a path (4x MSAA), not as an analytic ring: it does for a
+// translucent border and for borders of different widths, as port/fixtures/corners/lab.json measures.
+bool border_is_path(Edges_ScaledPixels border, float border_alpha) {
+ bool uniform = border.top == border.right && border.right == border.bottom && border.bottom == border.left;
+ return border.top + border.right + border.bottom + border.left > 0.0 && (border_alpha < 1.0 || !uniform);
+}
+
+// The share of the pixel's 4x MSAA samples (the standard Metal and D3D positions) outside the inner border rrect's
+// corner: Chromium rasterizes border paths with 4x MSAA. `point` is the pixel center from the quad's origin.
+float msaa_outside_inner_corner(float2 point, float2 half_size, float2 border, float2 inner_radii) {
+ const float2 samples[4] = {float2(0.375, 0.125), float2(0.875, 0.375), float2(0.125, 0.625), float2(0.625, 0.875)};
+ uint segments = conic_segments(inner_radii);
+ uint outside = 0;
+ for (int i = 0; i < 4; i++) {
+ float2 sample_point = floor(point) + samples[i];
+ // Inward distance from the inner border rect's corner, mirrored into the corner like the caller's point.
+ float2 q = half_size - fabs(sample_point - half_size) - border;
+ bool inside = q.x >= 0.0 && q.y >= 0.0 &&
+ (q.x >= inner_radii.x || q.y >= inner_radii.y || inside_conic_chords(q, inner_radii, segments));
+ outside += inside ? 0 : 1;
+ }
+ return float(outside) / 4.0;
+}
+
+// Skia's tessellator splits the quarter ellipse conic (weight sqrt(1/2)) into the power of two segments at or
+// above Wang's formula at precision 4 (wangs_formula::conic, rounded up to a resolve level).
+uint conic_segments(float2 radii) {
+ const float w = M_SQRT1_2_F;
+ const float precision = 4.0;
+ // Control points (0, r.y), (0, 0), (r.x, 0), centered on their bounding box.
+ float2 c = radii * 0.5;
+ float2 p0 = float2(0.0, radii.y) - c, p1 = -c, p2 = float2(radii.x, 0.0) - c;
+ float max_len = sqrt(max(max(length_squared(p0), length_squared(p1)), length_squared(p2)));
+ float2 dp = p0 - 2.0 * w * p1 + p2;
+ float numer = length(dp) * precision + max(0.0, max_len * precision - 1.0) * fabs(2.0 - 2.0 * w);
+ float n = sqrt(numer / (4.0 * w * w));
+ return 1u << uint(ceil(log2(max(n, 1.0))));
+}
+
+// Whether `q` lies inside the chords of the conic (0, r.y), (0, 0), (r.x, 0), split uniformly in t.
+bool inside_conic_chords(float2 q, float2 radii, uint segments) {
+ const float w = M_SQRT1_2_F;
+ float2 previous = float2(0.0, radii.y);
+ for (uint i = 1; i <= segments; i++) {
+ float t = float(i) / float(segments);
+ float a = (1.0 - t) * (1.0 - t), b = 2.0 * w * t * (1.0 - t), c = t * t;
+ float2 next = (a * float2(0.0, radii.y) + c * float2(radii.x, 0.0)) / (a + b + c);
+ float2 edge = next - previous;
+ float2 to_q = q - previous;
+ // The chords run clockwise around the ellipse center at `radii`, so the inside is on the right.
+ if (edge.x * to_q.y - edge.y * to_q.x < 0.0) {
+ return false;
+ }
+ previous = next;
+ }
+ return true;
+}
+
+// Skia Graphite's AnalyticRRect corner distance (first order, not radial) at `p` from the corner circle's center,
+// positive inside. A point outside the corner's quadrant is not affected by the curve.
+float analytic_corner_distance(float2 p, float radius) {
+ if (p.x <= 0.0 || p.y <= 0.0) {
+ return INFINITY;
+ }
+ float rho = length(p);
+ return (radius * radius - rho * rho) / (2.0 * rho);
+}
+
+float analytic_rrect_coverage(float2 local, float radius, float outset) {
+ float distance = min(min(radius - local.x, radius - local.y), analytic_corner_distance(local, radius));
+ return saturate(distance + min(outset, 0.0) + 0.5);
+}
+
+// Coverage of a rounded rect's corner as Chromium draws it outside an MSAA pass: Skia Graphite's AnalyticRRect mesh.
+// Its outer anchors sit on the corner's 45 degree tangent; past it, 1 px outset triangles carry the anchors' edge
+// distances and an outset distance.
+float rounded_corner_coverage(float2 pixel, float2 origin, float2 half_size, float radius) {
+ // Mirrored into the bottom right corner, from the corner circle's center.
+ float2 c = fabs(pixel - origin - half_size) - half_size + radius;
+ float tangent = M_SQRT2_F * radius;
+ float2 a = float2(radius, tangent - radius), b = float2(tangent - radius, radius);
+ float2 normal_a = float2(1.0, M_SQRT2_F - 1.0), normal_b = normal_a.yx;
+ if (c.x <= radius && c.y <= radius && c.x + c.y <= tangent) {
+ return analytic_rrect_coverage(c, radius, 0.0);
+ } else if (c.x > radius && c.y <= a.y) {
+ return analytic_rrect_coverage(float2(radius, c.y), radius, radius - c.x);
+ } else if (c.y > radius && c.x <= b.x) {
+ return analytic_rrect_coverage(float2(c.x, radius), radius, radius - c.y);
+ } else if (c.y - c.x < a.y - a.x) {
+ return analytic_rrect_coverage(a, radius, -dot(c - a, normal_a));
+ } else if (c.y - c.x > b.y - b.x) {
+ return analytic_rrect_coverage(b, radius, -dot(c - b, normal_b));
+ }
+ float beyond = (c.x + c.y - tangent) * 0.5;
+ return analytic_rrect_coverage(c - beyond, radius, -beyond * M_SQRT2_F);
+}
+
+// A rounded rect's coverage of a pixel, as Graphite's AnalyticRRect draws a fill's edge.
+float rrect_coverage(float2 pixel, Bounds_ScaledPixels bounds, Corners_ScaledPixels corner_radii) {
+ float2 half_size = float2(bounds.size.width, bounds.size.height) / 2.0;
+ float2 origin = float2(bounds.origin.x, bounds.origin.y);
+ float radius = pick_corner_radius(pixel - origin - half_size, corner_radii);
+ return radius > 0.0 ? rounded_corner_coverage(pixel, origin, half_size, radius)
+ : saturate(0.5 - quad_sdf(pixel, bounds, corner_radii));
}
struct OpaqueQuadFragmentInput {
@@ -1219,6 +1336,15 @@ float4 distance_from_clip_rect_transformed(float2 unit_vertex, Bounds_ScaledPixe
clip_bounds.origin.y + clip_bounds.size.height - transformed_position.y);
}
+// The straight color whose blend matches an 8-bit premultiplied source: #5a5d5e50 over #181818 gives 44, not 45.
+float4 premultiplied_8bit(float4 color) {
+ float alpha = round(color.a * 255.0) / 255.0;
+ if (alpha == 0.0) {
+ return float4(0.0);
+ }
+ return float4(round(color.rgb * alpha * 255.0) / (255.0 * alpha), alpha);
+}
+
float4 over(float4 below, float4 above) {
float alpha = above.a + below.a * (1.0 - above.a);
float3 premultiplied =