Performance tab: GPU uses the same hero card as the CPU; background layer caches wallpaper and clock and fills visualiser bars from a gradient strip (about 4x faster frames); bongo cat colourised like MultiEffect instead of flat

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
Jacob Ward 2026-10-31 20:04:50 -05:00
commit 1ae8ee56ab
4 changed files with 116 additions and 67 deletions

View file

@ -307,6 +307,10 @@ pub struct Bg {
offset: Anim,
opacity: Anim,
seen: String,
/// Wallpaper and clock as last composed, reused while neither changes: they cost far
/// more to draw than a frame's worth of visualiser, lyrics and shapes.
base: Option<(String, Pixmap)>,
strip: Option<((u32, u32, u32, u32, u32, u32, u32, u32), Pixmap)>,
}
#[derive(Clone, PartialEq)]
@ -320,7 +324,7 @@ struct Key {
impl Bg {
pub fn new() -> Self {
Bg { cache: None, prev: None, reveal: Anim::new(1.0), shape: Kind::Circle, offset: Anim::new(0.0), opacity: Anim::new(0.0), seen: String::new() }
Bg { cache: None, prev: None, reveal: Anim::new(1.0), shape: Kind::Circle, offset: Anim::new(0.0), opacity: Anim::new(0.0), seen: String::new(), base: None, strip: None }
}
pub fn animating(&self, shared: &Shared) -> bool {
@ -336,23 +340,63 @@ impl Bg {
return;
}
let anim = shared.anim_scale();
if cfg.wallpaper_enabled {
self.paint_wallpaper(p, shared, w, h, anim);
}
self.paint_visualiser(p, shared, w, h, has_bar, name, anim);
let prof = std::env::var_os("CAELESTIA_PROF").is_some();
let t = std::time::Instant::now();
let g = grid(shared, w, h, scale, has_bar);
let layout_items = shared.bg.layout.as_ref().map(|l| l.items.clone()).unwrap_or_default();
let (pw, ph) = (p.pix.width(), p.pix.height());
let primary = shared.c("primary");
let key = format!(
"{:?}|{pw}x{ph}|{}|{}|{}|{:?}|{:?}|{}|{}|{}",
shared.bg.wall.as_ref().map(|w| &w.0),
cfg.wallpaper_enabled,
cfg.wallpaper_fill_mode,
cfg.wallpaper_recolor,
cfg.wallpaper_recolor_strength.to_bits(),
(primary.r.to_bits(), primary.g.to_bits(), primary.b.to_bits()),
Local::now().format("%H:%M"),
layout_items.iter().filter(|i| i.kind == WidgetKind::Clock).count(),
serde_json::to_string(&cfg.desktop_clock).unwrap_or_default().len(),
);
let settled = !self.reveal.running() && self.prev.is_none();
let reuse = settled && self.base.as_ref().is_some_and(|(k, b)| *k == key && b.width() == pw && b.height() == ph);
if reuse {
p.pix.data_mut().copy_from_slice(self.base.as_ref().unwrap().1.data());
} else {
if cfg.wallpaper_enabled {
self.paint_wallpaper(p, shared, w, h, anim);
}
for it in layout_items.iter().filter(|i| i.kind == WidgetKind::Clock) {
if it.screen.as_ref().is_some_and(|s| s != name) {
continue;
}
paint_clock(p, shared, frame(&g, it), anim);
}
if !self.reveal.running() && self.prev.is_none() {
self.base = Some((key, p.pix.clone()));
}
}
let t_wall = t.elapsed();
self.paint_visualiser(p, shared, w, h, has_bar, name, anim);
let t_vis = t.elapsed();
let hidden = if cfg.visualiser.hide_on_all_monitors { any_covers(shared, (w, h)) } else { shared.niri.window_covers(name, (w, h)) };
let playing = crate::media::active(&shared.players, &shared.cfg.services.default_player).is_some_and(|p| p.playing);
for it in &shared.bg.layout.as_ref().map(|l| l.items.clone()).unwrap_or_default() {
for it in &layout_items {
if it.screen.as_ref().is_some_and(|s| s != name) {
continue;
}
let f = frame(&g, it);
match it.kind {
WidgetKind::Clock => paint_clock(p, shared, f, anim),
WidgetKind::Clock => {}
WidgetKind::Shapes => paint_shapes(p, shared, f, playing && !(cfg.desktop_shapes.auto_hide && hidden), anim),
WidgetKind::Lyrics => paint_lyrics(p, shared, f, playing && shared.bg.lyrics.has_lyrics() && !(cfg.desktop_lyrics.auto_hide && hidden), anim),
}
if prof {
eprintln!(" widget {:?} done at {:?}", it.kind, t.elapsed());
}
}
if prof {
eprintln!(" wall {t_wall:?} vis {:?} all {:?}", t_vis - t_wall, t.elapsed());
}
}
@ -499,19 +543,29 @@ impl Bg {
let Some(path) = pb.finish() else { return };
let (c1, c2) = (shared.c("primary").with_alpha(0.7 * op), shared.c("inversePrimary").with_alpha(0.7 * op));
let col = |c: Rgba| tiny_skia::Color::from_rgba(c.r, c.g, c.b, c.a).unwrap_or(tiny_skia::Color::TRANSPARENT);
let shader = LinearGradient::new(
Point::from_xy(0.0, ay + ah - max_h),
Point::from_xy(0.0, ay + ah),
vec![GradientStop::new(0.0, col(c1)), GradientStop::new(1.0, col(c2))],
SpreadMode::Pad,
Transform::identity(),
);
if let Some(shader) = shader {
// The gradient is a one-pixel-wide strip made once per size and colour; filling bars
// through a pattern of it is several times cheaper than a gradient shader per frame.
let rows = (max_h.ceil().max(1.0) as u32).min(2048);
let key = (rows, c1.r.to_bits(), c1.g.to_bits(), c1.b.to_bits(), c1.a.to_bits(), c2.r.to_bits(), c2.g.to_bits(), c2.b.to_bits());
if self.strip.as_ref().is_none_or(|(k, _)| *k != key) {
let mut strip = Pixmap::new(1, rows).unwrap();
let mut paint = Paint::default();
paint.shader = shader;
paint.anti_alias = true;
p.pix.fill_path(&path, &paint, FillRule::Winding, Transform::from_scale(p.scale, p.scale), None);
paint.shader = LinearGradient::new(
Point::from_xy(0.0, 0.0),
Point::from_xy(0.0, rows as f32),
vec![GradientStop::new(0.0, col(c1)), GradientStop::new(1.0, col(c2))],
SpreadMode::Pad,
Transform::identity(),
)
.unwrap_or(tiny_skia::Shader::SolidColor(col(c1)));
strip.fill_rect(tiny_skia::Rect::from_xywh(0.0, 0.0, 1.0, rows as f32).unwrap(), &paint, Transform::identity(), None);
self.strip = Some((key, strip));
}
let strip = &self.strip.as_ref().unwrap().1;
let mut paint = Paint::default();
paint.shader = tiny_skia::Pattern::new(strip.as_ref(), SpreadMode::Repeat, tiny_skia::FilterQuality::Nearest, 1.0, Transform::from_row(1.0, 0.0, 0.0, max_h / rows as f32, 0.0, ay + ah - max_h));
paint.anti_alias = true;
p.pix.fill_path(&path, &paint, FillRule::Winding, Transform::from_scale(p.scale, p.scale), None);
}
}

View file

@ -113,7 +113,7 @@ fn sizes(p: &mut Painter, shared: &Shared, m: &M) -> Sizes {
let ring_memory = usage_mem + thick + 20.0 * 2.0;
let mem_text = p.measure(&format_usage(1.0, 1.0), m.body).0.max(p.measure("Memory", m.title).0 + m.icon_h + SPACING_SMALL).max(ring_memory);
let memory_w = mem_text + 32.0 * 2.0;
let hero_h = (51.0 + 39.0 + SPACING_LARGE).max(USAGE_SHAPE + m.lh_small).max(if shared.sys.gpu.is_some() { m.icon_h + PADDING_MEDIUM * 2.0 + SPACING_LARGE + 3.0 * (m.icon_h + 8.0) } else { 0.0 }) + PADDING_LARGE * 2.0;
let hero_h = (51.0 + 39.0 + SPACING_LARGE).max(USAGE_SHAPE + m.lh_small) + PADDING_LARGE * 2.0;
let row2 = storage_w + SPACING + NET_W + SPACING + memory_w;
let _ = shared;
Sizes { hero_h, storage_w, memory_w, ring_storage, ring_memory, w: row2.max(HERO_W), h: hero_h + SPACING + NET_H }
@ -125,29 +125,24 @@ pub fn natural(p: &mut Painter, shared: &Shared) -> (f32, f32) {
(s.w, s.h)
}
pub fn paint(p: &mut Painter, shared: &Shared, x: f32, y: f32, op: f32) {
let m = metrics(p);
let s = sizes(p, shared, &m);
#[allow(clippy::too_many_arguments)]
fn hero_card(p: &mut Painter, shared: &Shared, m: &M, x: f32, y: f32, w: f32, h: f32, op: f32, icon_name: &str, label: &str, sub: &str, usage: f32, temp: Option<f32>, accent: Rgba) {
let fade = |c: Rgba| c.with_alpha(c.a * op);
let card = |p: &mut Painter, cx: f32, cy: f32, w: f32, h: f32, r: f32| p.rrect(cx, cy, w, h, r, fade(shared.t("surfaceContainer")));
let sys = &shared.sys;
// ---- CPU -----------------------------------------------------------------
let accent = shared.c("primary");
card(p, x, y, HERO_W, s.hero_h, ROUNDING_XL);
card(p, x, y, w, h, ROUNDING_XL);
let temp_ring = m.icon_h + PADDING_MEDIUM * 2.0;
let (rx, ry) = (x + PADDING_LARGE + temp_ring / 2.0, y + PADDING_LARGE + temp_ring / 2.0);
ring(p, shared, rx, ry, temp_ring, PADDING_XS, -90.0, 360.0, sys.cpu, accent, SPACING_XS, op);
p.text_centered("memory", rx, ry, m.icon, fade(accent));
let shape_x = x + HERO_W - PADDING_MEDIUM - USAGE_SHAPE;
ring(p, shared, rx, ry, temp_ring, PADDING_XS, -90.0, 360.0, usage, accent, SPACING_XS, op);
p.text_centered(icon_name, rx, ry, m.icon, fade(accent));
let shape_x = x + w - PADDING_MEDIUM - USAGE_SHAPE;
let tx = x + PADDING_LARGE + temp_ring + SPACING_LARGE;
let tw = shape_x - SPACING_LARGE - tx;
p.text("CPU", tx, ry - (m.lh_title + SPACING_XS + m.lh_small) / 2.0, m.title, fade(accent));
let name = p.elide(&sys.cpu_name, m.small, tw);
p.text(label, tx, ry - (m.lh_title + SPACING_XS + m.lh_small) / 2.0, m.title, fade(accent));
let name = p.elide(&sub, m.small, tw);
p.text(&name, tx, ry - (m.lh_title + SPACING_XS + m.lh_small) / 2.0 + m.lh_title + SPACING_XS, m.small, fade(shared.c("onSurfaceVariant")));
// Temperature and its bar
let by = y + s.hero_h - 20.0;
let temp = sys.cpu_temp;
let by = y + h - 20.0;
let temp = temp;
let hot = temp.is_some_and(|t| t > 90.0);
let tcol = if hot { shared.c("error") } else { accent };
let bar_y = by - 8.0;
@ -159,42 +154,29 @@ pub fn paint(p: &mut Painter, shared: &Shared, x: f32, y: f32, op: f32) {
p.rrect(x + 20.0, bar_y, bar_w, 8.0, 4.0, fade(shared.c("secondaryContainer")));
p.rrect(x + 20.0, bar_y, (bar_w * temp.unwrap_or(0.0) / 100.0).clamp(0.0, bar_w).max(if temp.is_some() { 8.0 } else { 0.0 }), 8.0, 4.0, fade(tcol));
// The usage shape and percentage
let (scx, scy) = (shape_x + USAGE_SHAPE / 2.0, y + s.hero_h - PADDING_MEDIUM - USAGE_SHAPE / 2.0);
let kind = if sys.cpu >= 0.8 { Kind::SoftBurst } else if sys.cpu >= 0.4 { Kind::Sunny } else { Kind::Cookie4Sided };
let (scx, scy) = (shape_x + USAGE_SHAPE / 2.0, y + h - PADDING_MEDIUM - USAGE_SHAPE / 2.0);
let kind = if usage >= 0.8 { Kind::SoftBurst } else if usage >= 0.4 { Kind::Sunny } else { Kind::Cookie4Sided };
let m2 = crate::shapes::Morph::new(crate::shapes::shape(kind), crate::shapes::shape(kind));
if let Some(path) = crate::shapes::path(&m2.cubics(1.0), scx, scy, USAGE_SHAPE) {
p.fill_path(&path, fade(shared.c("secondaryContainer")), FillRule::Winding);
}
p.text_centered("Usage", scx, scy - USAGE_SHAPE / 2.0 - m.lh_small / 2.0, m.small, fade(shared.c("onSurfaceVariant")));
p.text_centered(&format!("{}%", (sys.cpu * 100.0).round() as u32), scx, scy, TextStyle::sans(BODY_LARGE_PT + 4.0).weight(500), fade(accent));
p.text_centered(&format!("{}%", (usage * 100.0).round() as u32), scx, scy, TextStyle::sans(BODY_LARGE_PT + 4.0).weight(500), fade(accent));
}
// ---- GPU (beside the CPU card, as the original's performance tab) ---------
pub fn paint(p: &mut Painter, shared: &Shared, x: f32, y: f32, op: f32) {
let m = metrics(p);
let s = sizes(p, shared, &m);
let fade = |c: Rgba| c.with_alpha(c.a * op);
let card = |p: &mut Painter, cx: f32, cy: f32, w: f32, h: f32, r: f32| p.rrect(cx, cy, w, h, r, fade(shared.t("surfaceContainer")));
let sys = &shared.sys;
// ---- CPU and GPU: the same hero card (performance/HeroCard.qml), side by side -----
let cpu_temp = sys.cpu_temp;
let hero_w = if sys.gpu.is_some() { (s.w - SPACING) / 2.0 } else { HERO_W };
hero_card(p, shared, &m, x, y, hero_w, s.hero_h, op, "memory", "CPU", &sys.cpu_name, sys.cpu, cpu_temp, shared.c("primary"));
if let Some(g) = &sys.gpu {
let gx = x + HERO_W + SPACING;
let gw = s.w - HERO_W - SPACING;
let gc = shared.c("tertiary");
card(p, gx, y, gw, s.hero_h, ROUNDING_XL);
let ring_d = m.icon_h + PADDING_MEDIUM * 2.0;
let (grx, gry) = (gx + PADDING_LARGE + ring_d / 2.0, y + PADDING_LARGE + ring_d / 2.0);
ring(p, shared, grx, gry, ring_d, PADDING_XS, -90.0, 360.0, g.usage, gc, SPACING_XS, op);
p.text_centered("developer_board", grx, gry, m.icon, fade(gc));
let gtx = gx + PADDING_LARGE + ring_d + SPACING_LARGE;
let top = gry - (m.lh_title + SPACING_XS + m.lh_small) / 2.0;
p.text("GPU", gtx, top, m.title, fade(gc));
let name = p.elide(&g.name, m.small, gx + gw - PADDING_LARGE - gtx);
p.text(&name, gtx, top + m.lh_title + SPACING_XS, m.small, fade(shared.c("onSurfaceVariant")));
let lines = [
("percent", format!("{}% usage", (g.usage * 100.0).round() as u32), g.usage),
("thermostat", g.temp.map_or("--".to_string(), |t| format!("{}°C", t.round() as i32)), g.temp.unwrap_or(0.0) / 100.0),
("memory_alt", if g.vram_total > 0 { format_usage(g.vram_used as f64, g.vram_total as f64) } else { "VRAM n/a".into() }, if g.vram_total > 0 { g.vram_used as f32 / g.vram_total as f32 } else { 0.0 }),
];
let bw = gw - PADDING_LARGE * 2.0;
for (k, (icon, text, frac)) in lines.iter().enumerate() {
let ly = y + PADDING_LARGE + ring_d + SPACING_LARGE + k as f32 * (m.icon_h + 8.0);
p.text(icon, gx + PADDING_LARGE - PADDING_XS, ly, m.icon, fade(gc));
p.text(text, gx + PADDING_LARGE + m.icon_h, ly + (m.icon_h - m.lh_body) / 2.0, m.body, fade(shared.c("onSurface")));
let _ = (bw, frac);
}
hero_card(p, shared, &m, x + hero_w + SPACING, y, hero_w, s.hero_h, op, "desktop_windows", "GPU", &g.name, g.usage, g.temp, shared.c("secondary"));
}
// ---- second row ----------------------------------------------------------

View file

@ -133,12 +133,20 @@ impl Shared {
pub fn tinted(&self, frame: &tiny_skia::Pixmap, tint: Option<Rgba>) -> tiny_skia::Pixmap {
let mut out = frame.clone();
if let Some(t) = tint {
let (r, g, b) = ((t.r * 255.0) as u16, (t.g * 255.0) as u16, (t.b * 255.0) as u16);
// MultiEffect colorization: the picture's lightness is kept, its hue becomes the
// tint (dark stays dark, light goes toward white), so the drawing still shows.
let tint = [t.r, t.g, t.b];
for px in out.data_mut().chunks_exact_mut(4) {
let a = px[3] as u16;
px[0] = (r * a / 255) as u8;
px[1] = (g * a / 255) as u8;
px[2] = (b * a / 255) as u8;
let a = px[3] as f32 / 255.0;
if a <= 0.0 {
continue;
}
let c = [px[0], px[1], px[2]].map(|v| (v as f32 / 255.0 / a).min(1.0));
let l = (c[0].max(c[1]).max(c[2]) + c[0].min(c[1]).min(c[2])) / 2.0;
for k in 0..3 {
let v = if l < 0.5 { tint[k] * 2.0 * l } else { tint[k] + (1.0 - tint[k]) * (2.0 * l - 1.0) };
px[k] = (v.clamp(0.0, 1.0) * a * 255.0).round() as u8;
}
}
}
out

View file

@ -1564,13 +1564,18 @@ impl App {
s.bg.pix = Pixmap::new(pw, ph);
}
let Some(pix) = s.bg.pix.as_mut() else { return };
let t0 = std::time::Instant::now();
let mut painter = Painter::new(pix, scale as f32, text);
s.bg.state.paint(&mut painter, shared, s.width as f32, s.height as f32, s.scale, s.ui.has_bar, &s.ui.output);
let Ok((buffer, canvas)) = pool.create_buffer(pw as i32, ph as i32, pw as i32 * 4, wl_shm::Format::Argb8888) else {
eprintln!("caelestia-shell: out of shm memory");
return;
};
let t1 = t0.elapsed();
to_argb8888(pix.data(), canvas);
if std::env::var_os("CAELESTIA_PROF").is_some() {
eprintln!("bg {}: paint {:?} total {:?}", s.ui.output, t1, t0.elapsed());
}
let surface = s.bg.layer.wl_surface();
surface.set_buffer_scale(scale as i32);
surface.damage_buffer(0, 0, pw as i32, ph as i32);