RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process
Speed up titlebar rasters and smooth image draws
Claudef6a0ead79006
diff --git a/src/chrome.zig b/src/chrome.zigindex 89598d5..f1a3b6d 100644--- a/src/chrome.zig+++ b/src/chrome.zig@@ -408,58 +408,76 @@ pub const ChromeBuffer = struct {const x0: usize = if (clip) |box| @intCast(@min(frame.width, @max(0, @as(i32, @intFromFloat(@round(@as(f32, @floatFromInt(box.x)) * scale)))))) else 0;const x1: usize = if (clip) |box| @intCast(@min(frame.width, @max(0, @as(i32, @intFromFloat(@round(@as(f32, @floatFromInt(box.x + box.w)) * scale)))))) else stride;+ const controls = Controls.init(min_btn, max_btn, close_btn, frame.state.chip_pos, frame.state.chip_alpha, diag, scale, m.contentScale(), frame.state.maximized);+ const icon_right = icon_left + @as(f32, @floatFromInt(m.iconSize()));+ // The columns of the control group and of the icon. A column's centre+ // grows with x, so each is one run; the rest of a row is the flat fill.+ var control_run: ColumnRun = .{ .start = x1, .end = x1 };+ var icon_run: ColumnRun = .{ .start = x1, .end = x1 };+ for (x0..x1) |x| {+ const px = (@as(f32, @floatFromInt(x)) + 0.5) / scale;+ if (px >= control_left and px <= control_right) control_run.add(x);+ if (px >= icon_left and px < icon_right) icon_run.add(x);+ }for (0..@intCast(frame.height)) |device_y| {const py = (@as(f32, @floatFromInt(device_y)) + 0.5) / scale;const yi: i32 = @intFromFloat(@floor(py));- const row = device_y * stride;+ const row = frame.pixels[device_y * stride ..][0..stride];var row_color = base;if (yi == m.titlebarHeight() - 1) row_color = Color.divider().over(row_color);const flat_color = row_color.argb();const control_row = py >= control_top and py <= control_bottom;const icon_row = frame.state.tabs.tabs.len == 0 and frame.state.icon != null andpy >= icon_top and py < icon_top + @as(f32, @floatFromInt(m.iconSize()));- for (x0..x1) |x| {- const px = (@as(f32, @floatFromInt(x)) + 0.5) / scale;- const in_controls = control_row and px >= control_left and px <= control_right;- const in_icon = icon_row and px >= icon_left and px < icon_left + @as(f32, @floatFromInt(m.iconSize()));- if (!in_controls and !in_icon) {- frame.pixels[row + x] = flat_color;- continue;- }-- var color = row_color;- if (in_controls) {- color = paintControls(- color,- px,- py,- min_btn,- max_btn,- close_btn,- frame.state.chip_pos,- frame.state.chip_alpha,- diag,- scale,- m.contentScale(),- frame.state.maximized,- );+ // The icon is left of the controls, but a narrow frame can make+ // the two overlap.+ var runs: [2]ColumnRun = undefined;+ var count: usize = 0;+ if (icon_row and icon_run.start < icon_run.end) {+ runs[count] = icon_run;+ count += 1;+ }+ if (control_row and control_run.start < control_run.end) {+ if (count > 0 and control_run.start <= runs[0].end) {+ runs[0] = .{ .start = @min(runs[0].start, control_run.start), .end = @max(runs[0].end, control_run.end) };+ } else {+ runs[count] = control_run;+ count += 1;}+ }+ var x = x0;+ for (runs[0..count]) |run| {+ @memset(row[x..run.start], flat_color);+ for (run.start..run.end) |shaded| {+ const px = (@as(f32, @floatFromInt(shaded)) + 0.5) / scale;+ const in_controls = control_row and px >= control_left and px <= control_right;+ const in_icon = icon_row and px >= icon_left and px < icon_right;+ if (!in_controls and !in_icon) {+ row[shaded] = flat_color;+ continue;+ }- if (if (frame.state.tabs.tabs.len == 0) frame.state.icon else null) |icon| {- if (px >= icon_left and px < icon_left + @as(f32, @floatFromInt(m.iconSize())) and- py >= icon_top and py < icon_top + @as(f32, @floatFromInt(m.iconSize())))- {- const ix: i32 = @intFromFloat(@floor((px - icon_left) * scale));- const iy: i32 = @intFromFloat(@floor((py - icon_top) * scale));- if (ix >= 0 and ix < icon.size and iy >= 0 and iy < icon.size) {- const sample = icon.pixels[@intCast(iy * icon.size + ix)];- color = Color.fromPremultiplied(sample).over(color);+ var color = row_color;+ if (in_controls) color = controls.paint(color, px, py);++ if (if (frame.state.tabs.tabs.len == 0) frame.state.icon else null) |icon| {+ if (px >= icon_left and px < icon_right and+ py >= icon_top and py < icon_top + @as(f32, @floatFromInt(m.iconSize())))+ {+ const ix: i32 = @intFromFloat(@floor((px - icon_left) * scale));+ const iy: i32 = @intFromFloat(@floor((py - icon_top) * scale));+ if (ix >= 0 and ix < icon.size and iy >= 0 and iy < icon.size) {+ const sample = icon.pixels[@intCast(iy * icon.size + ix)];+ color = Color.fromPremultiplied(sample).over(color);+ }}}- }- frame.pixels[row + x] = color.argb();+ row[shaded] = color.argb();+ }+ x = run.end;}+ @memset(row[x..x1], flat_color);}if (frame.state.tabs.tabs.len > 0) {@@ -536,7 +554,18 @@ pub const borderColor = chrome_paint.borderColor;pub const windowBorderColor = chrome_paint.windowBorderColor;pub const fillColor = chrome_paint.fillColor;const Color = chrome_paint.Color;-const paintControls = chrome_paint.paintControls;+const Controls = chrome_paint.Controls;++/// Device columns `start..end` of a raster; empty while `start == end`.+const ColumnRun = struct {+ start: usize,+ end: usize,++ fn add(run: *ColumnRun, x: usize) void {+ if (run.start == run.end) run.start = x;+ run.end = x + 1;+ }+};pub const sdQuad = chrome_paint.sdQuad;pub const sdRoundedBox = chrome_paint.sdRoundedBox;pub const coverage = chrome_paint.coverage;diff --git a/src/chrome_paint.zig b/src/chrome_paint.zigindex 779b56b..28eef0d 100644--- a/src/chrome_paint.zig+++ b/src/chrome_paint.zig@@ -130,56 +130,110 @@ pub const Color = struct {}};-pub fn paintControls(- base: Color,- px: f32,- py: f32,+/// The window controls of one raster. Everything that does not depend on the+/// pixel (the chip's box and fill, the glyph inks) is worked out once, not for+/// every pixel of the control group.+pub const Controls = struct {min_btn: Rect,max_btn: Rect,close_btn: Rect,- chip_pos: f32,- chip_alpha: f32,diag: f32,scale: f32,- density: f32,maximized: bool,-) Color {- var color = base;-- // Window-control hover chip, drawn under the glyph. One chip- // slides between the buttons; its rect and red-ness are interpolated- // from the slot coordinate.- if (chip_alpha > 0) {- const pos = std.math.clamp(chip_pos, 0, 2);- const from_max = pos >= 1;- const a_btn = if (from_max) max_btn else min_btn;- const b_btn = if (from_max) close_btn else max_btn;- const t = if (from_max) pos - 1 else pos;- const chip_width = mix(@floatFromInt(a_btn.w), @floatFromInt(b_btn.w), t);- const chip_height = mix(@floatFromInt(a_btn.h), @floatFromInt(b_btn.h), t);- const fill = Color.lerp(Color.control_hover(), Color.close_hover(), clamp01(pos - 1));- const chip = edgeCoverage(sdRoundedBox(- px,- py,- mix(@floatFromInt(a_btn.x), @floatFromInt(b_btn.x), t),- mix(@floatFromInt(a_btn.y), @floatFromInt(b_btn.y), t),- chip_width,- chip_height,- window_chrome.cornerRadius(theme.global.chrome_round_buttons, chip_width, chip_height, btn_radius * density),- ), scale);- if (chip > 0) color = fill.scaled(chip * clamp01(chip_alpha)).over(color);- }-- // Enlarge glyphs within the existing hover chips and hit targets.- const glyph_scale = density * button_glyph_scale;- const min_cov = minimizeIconCoverage(min_btn.centerX() + (px - min_btn.centerX()) / glyph_scale, min_btn.centerY() + (py - min_btn.centerY()) / glyph_scale, min_btn, scale * glyph_scale);- if (min_cov > 0) color = iconColor(chip_pos, chip_alpha, .minimize).scaled(min_cov).over(color);- const max_cov = maximizeIconCoverage(max_btn.centerX() + (px - max_btn.centerX()) / glyph_scale, max_btn.centerY() + (py - max_btn.centerY()) / glyph_scale, max_btn, scale * glyph_scale, maximized);- if (max_cov > 0) color = iconColor(chip_pos, chip_alpha, .maximize).scaled(max_cov).over(color);- const close_cov = closeIconCoverage(close_btn.centerX() + (px - close_btn.centerX()) / glyph_scale, close_btn.centerY() + (py - close_btn.centerY()) / glyph_scale, close_btn, diag, scale * glyph_scale);- if (close_cov > 0) color = iconColor(chip_pos, chip_alpha, .close).scaled(close_cov).over(color);- return color;-}+ glyph_scale: f32,+ // Every glyph lies within 7.6 glyph units of its button's centre (the+ // close cross's corners), and its ramp reaches 0.5 / (scale * glyph_scale)+ // beyond that: farther away on either axis its coverage is zero. 9 units+ // leave room for rounding.+ glyph_reach: f32,+ show_chip: bool,+ chip_x: f32 = 0,+ chip_y: f32 = 0,+ chip_width: f32 = 0,+ chip_height: f32 = 0,+ chip_radius: f32 = 0,+ chip_fill: Color = undefined,+ chip_alpha: f32 = 0,+ min_ink: Color,+ max_ink: Color,+ close_ink: Color,++ pub fn init(+ min_btn: Rect,+ max_btn: Rect,+ close_btn: Rect,+ chip_pos: f32,+ chip_alpha: f32,+ diag: f32,+ scale: f32,+ density: f32,+ maximized: bool,+ ) Controls {+ // Enlarge glyphs within the existing hover chips and hit targets.+ const glyph_scale = density * button_glyph_scale;+ var controls: Controls = .{+ .min_btn = min_btn,+ .max_btn = max_btn,+ .close_btn = close_btn,+ .diag = diag,+ .scale = scale,+ .maximized = maximized,+ .glyph_scale = glyph_scale,+ .glyph_reach = 9 * glyph_scale + 0.5 / scale,+ .show_chip = chip_alpha > 0,+ .min_ink = iconColor(chip_pos, chip_alpha, .minimize),+ .max_ink = iconColor(chip_pos, chip_alpha, .maximize),+ .close_ink = iconColor(chip_pos, chip_alpha, .close),+ };+ // Window-control hover chip, drawn under the glyph. One chip+ // slides between the buttons; its rect and red-ness are interpolated+ // from the slot coordinate.+ if (controls.show_chip) {+ const pos = std.math.clamp(chip_pos, 0, 2);+ const from_max = pos >= 1;+ const a_btn = if (from_max) max_btn else min_btn;+ const b_btn = if (from_max) close_btn else max_btn;+ const t = if (from_max) pos - 1 else pos;+ controls.chip_width = mix(@floatFromInt(a_btn.w), @floatFromInt(b_btn.w), t);+ controls.chip_height = mix(@floatFromInt(a_btn.h), @floatFromInt(b_btn.h), t);+ controls.chip_x = mix(@floatFromInt(a_btn.x), @floatFromInt(b_btn.x), t);+ controls.chip_y = mix(@floatFromInt(a_btn.y), @floatFromInt(b_btn.y), t);+ controls.chip_radius = window_chrome.cornerRadius(theme.global.chrome_round_buttons, controls.chip_width, controls.chip_height, btn_radius * density);+ controls.chip_fill = Color.lerp(Color.control_hover(), Color.close_hover(), clamp01(pos - 1));+ controls.chip_alpha = clamp01(chip_alpha);+ }+ return controls;+ }++ fn reaches(c: *const Controls, btn: Rect, px: f32, py: f32) bool {+ return @abs(px - btn.centerX()) <= c.glyph_reach and @abs(py - btn.centerY()) <= c.glyph_reach;+ }++ pub fn paint(c: *const Controls, base: Color, px: f32, py: f32) Color {+ var color = base;+ if (c.show_chip) {+ const chip = edgeCoverage(sdRoundedBox(px, py, c.chip_x, c.chip_y, c.chip_width, c.chip_height, c.chip_radius), c.scale);+ if (chip > 0) color = c.chip_fill.scaled(chip * c.chip_alpha).over(color);+ }+ const g = c.glyph_scale;+ if (c.reaches(c.min_btn, px, py)) {+ const btn = c.min_btn;+ const min_cov = minimizeIconCoverage(btn.centerX() + (px - btn.centerX()) / g, btn.centerY() + (py - btn.centerY()) / g, btn, c.scale * g);+ if (min_cov > 0) color = c.min_ink.scaled(min_cov).over(color);+ }+ if (c.reaches(c.max_btn, px, py)) {+ const btn = c.max_btn;+ const max_cov = maximizeIconCoverage(btn.centerX() + (px - btn.centerX()) / g, btn.centerY() + (py - btn.centerY()) / g, btn, c.scale * g, c.maximized);+ if (max_cov > 0) color = c.max_ink.scaled(max_cov).over(color);+ }+ if (c.reaches(c.close_btn, px, py)) {+ const btn = c.close_btn;+ const close_cov = closeIconCoverage(btn.centerX() + (px - btn.centerX()) / g, btn.centerY() + (py - btn.centerY()) / g, btn, c.diag, c.scale * g);+ if (close_cov > 0) color = c.close_ink.scaled(close_cov).over(color);+ }+ return color;+ }+};// Resting glyphs use neutral dim ink, --text on hover, #fff over close red.pub fn iconColor(chip_pos: f32, chip_alpha: f32, kind: ControlKind) Color {diff --git a/src/chrome_tests.zig b/src/chrome_tests.zigindex ce8f90f..59c047c 100644--- a/src/chrome_tests.zig+++ b/src/chrome_tests.zig@@ -257,3 +257,34 @@ test "tab title updates preserve neighbouring pixels and match a full raster" {}}}++test "control glyphs have no coverage beyond the reach the raster gives them" {+ const chrome_paint = @import("chrome_paint.zig");+ for ([_]f32{ 1, 0.75, 1.0 / 1.7, 1.3 }) |density| {+ for ([_]f32{ 1, 1.5, 2, 3 }) |scale| {+ for ([_]bool{ false, true }) |maximized| {+ const m: Metrics = .{ .density = density };+ const btn = [_]window_chrome.Rect{ controlRectScaled(400, .minimize, density), controlRectScaled(400, .maximize, density), controlRectScaled(400, .close, density) };+ const controls = chrome_paint.Controls.init(btn[0], btn[1], btn[2], 0, 0, std.math.sqrt1_2, scale, m.contentScale(), maximized);+ const g = controls.glyph_scale;+ var py: f32 = 0;+ while (py < @as(f32, @floatFromInt(m.titlebarHeight()))) : (py += 0.25 / scale) {+ var px: f32 = @floatFromInt(btn[0].x - 10);+ while (px < @as(f32, @floatFromInt(btn[2].x + btn[2].w + 10))) : (px += 0.25 / scale) {+ for (btn, 0..) |b, kind| {+ if (@abs(px - b.centerX()) <= controls.glyph_reach and @abs(py - b.centerY()) <= controls.glyph_reach) continue;+ const gx = b.centerX() + (px - b.centerX()) / g;+ const gy = b.centerY() + (py - b.centerY()) / g;+ const cov = switch (kind) {+ 0 => chrome_paint.minimizeIconCoverage(gx, gy, b, scale * g),+ 1 => chrome_paint.maximizeIconCoverage(gx, gy, b, scale * g, maximized),+ else => chrome_paint.closeIconCoverage(gx, gy, b, std.math.sqrt1_2, scale * g),+ };+ try std.testing.expect(cov <= 0);+ }+ }+ }+ }+ }+ }+}diff --git a/src/ui/paint.zig b/src/ui/paint.zigindex cdc187b..066c8e3 100644--- a/src/ui/paint.zig+++ b/src/ui/paint.zig@@ -59,6 +59,9 @@ pub const button_widget = @import("widgets/button.zig");pub const checkbox_widget = @import("widgets/checkbox.zig");pub const scrollbar = @import("widgets/scrollbar.zig");+/// Columns whose horizontal taps `drawImageSmooth` keeps at once (on the stack).+const taps_strip = 64;+/// A logical-pixel clip rectangle, intersected as scroll containers nest.pub const ClipRect = struct { x: f32, y: f32, w: f32, h: f32 };@@ -421,25 +424,47 @@ pub const Renderer = struct {// blends neighbours (bilinear) rather than repeating them.const footprint = std.math.clamp(k, 1, max_footprint);const stride: usize = @intCast(image.width);+ if (smooth) {+ // A column shares its horizontal taps: work them out once per strip+ // of columns, not once per pixel.+ var across: [taps_strip]Taps = undefined;+ var strip = x0;+ while (strip < x1) : (strip += taps_strip) {+ const strip_end = @min(x1, strip + taps_strip);+ for (across[0..@intCast(strip_end - strip)], 0..) |*taps, i| {+ const sx = @as(f32, @floatFromInt(strip + @as(i32, @intCast(i)))) + 0.5;+ taps.* = Taps.init(off_x + (sx - x * s) * k, footprint / 2, image.width);+ }+ var py = y0;+ while (py < y1) : (py += 1) {+ const sy = @as(f32, @floatFromInt(py)) + 0.5;+ const down = Taps.init(off_y + (sy - y * s) * k, footprint / 2, image.height);+ var px = strip;+ while (px < strip_end) : (px += 1) {+ const sx = @as(f32, @floatFromInt(px)) + 0.5;+ const cov = sdf.coverage(sdf.sdRoundedBox(sx, sy, x * s, y * s, w * s, h * s, radius * s));+ if (cov <= 0) continue;+ const sample = boxAverage(image, across[@intCast(px - strip)], down);+ blendPremultipliedPixel(&r.pixels[@intCast(py * r.width + px)], sample, cov);+ }+ }+ }+ return;+ }var py = y0;while (py < y1) : (py += 1) {const sy = @as(f32, @floatFromInt(py)) + 0.5;const v = off_y + (sy - y * s) * k;- // A row shares its vertical taps.- const down = if (smooth) Taps.init(v, footprint / 2, image.height) else undefined;var px = x0;while (px < x1) : (px += 1) {const sx = @as(f32, @floatFromInt(px)) + 0.5;const cov = sdf.coverage(sdf.sdRoundedBox(sx, sy, x * s, y * s, w * s, h * s, radius * s));if (cov <= 0) continue;const u = off_x + (sx - x * s) * k;- const sample = if (smooth)- boxAverage(image, Taps.init(u, footprint / 2, image.width), down)- else- image.pixels[- @as(usize, @intFromFloat(std.math.clamp(@floor(v), 0, src_h - 1))) * stride +- @as(usize, @intFromFloat(std.math.clamp(@floor(u), 0, src_w - 1)))- ];+ const sample = image.pixels[+ @as(usize, @intFromFloat(std.math.clamp(@floor(v), 0, src_h - 1))) * stride ++ @as(usize, @intFromFloat(std.math.clamp(@floor(u), 0, src_w - 1)))+ ];blendPremultipliedPixel(&r.pixels[@intCast(py * r.width + px)], sample, cov);}}