Smoother animations without a GPU: faster rasteriser, frame pacing, software/hardware mode
CI / test (push) Failing after 3m2s
CI / test (push) Failing after 3m2s
Measured headless at 1920x1080 with CHANDRA_FRAME_TRACE (new: one log line per drawn frame): - Overview open: 46 -> 62 fps, worst frame gap 69 -> 36 ms, raster 14 -> 5 ms a frame. - Live Starfield: 19 -> 30 fps (its target), raster 49 -> 13 ms, CPU 97% -> 46%. Rasteriser (MoonBit host; circles and previews also in the Rust host): - Blending works on two channels at a time in 16-bit lanes with an exact /255: same pixels, about 3x quicker on big translucent fills (the overview's dim, the starfield's nebulae). - Filled circles are drawn as row spans, rings skip their empty middle (a sqrt per pixel of the bounding box before). Same pixels; a Rust test checks it against the old loop. - Window previews gliding in the overview are sampled straight from the preview at each frame's size instead of filtered and cached at a size used once; the MoonBit host also gets the one-large-size cache and 4096 px draw limit the Rust host had (it kept every size and didn't draw previews past 1024 px). - Image scaling sums each output row's columns once, with 32-bit sums and division. Frame pacing: frames are drawn only when the output can take them (no page flip in flight on KMS; the previous frame shown nested and on X11), with a frame every 250 ms for an output that never gets ready. The KMS loop runs at 125 Hz, so animations used to rasterise frames that were never shown. Software or hardware mode (compositor/shell/render_mode.mbt, docs/rendering.md): detected from the DRM render nodes' drivers (and NVIDIA's proprietary driver, LIBGL_ALWAYS_SOFTWARE), overridden by CHANDRA_RENDER=software|hardware or Settings > Session "Lighter animations" (state.edn :render, saved only when it differs from what was detected). Software mode remakes changing previews every 200 ms instead of 100 ms and caps desktop modules at ~30 fps. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DPL5hkTQp5uN5FtwpzrXUA
This commit is contained in:
16 files changed
+719
-97
No files matched your search
+143
-9
@@ -185,13 +185,19 @@ fn blend_span(c: &mut Canvas, y: i32, xa: i32, xb: i32, r: i32, g: i32, b: i32,
|
||||
}
|
||||
return;
|
||||
}
|
||||
let inv = 255 - a;
|
||||
let (ra, ga, ba) = (r * a + 127, g * a + 127, b * a + 127);
|
||||
// Two channels at a time in 16-bit lanes with an exact floor(v / 255): the same pixels as
|
||||
// c' = (c * a + d * (255 - a) + 127) / 255 per channel, and alpha a + (d * (255 - a) + 127) / 255
|
||||
// (the alpha lane adds a * 255 before dividing, which is the same).
|
||||
let inv = (255 - a) as u32;
|
||||
let rbadd = (((b * a + 127) as u32) << 16) | (r * a + 127) as u32;
|
||||
let agadd = (((255 * a + 127) as u32) << 16) | (g * a + 127) as u32;
|
||||
for p in row.chunks_exact_mut(4) {
|
||||
p[0] = ((ra + p[0] as i32 * inv) / 255) as u8;
|
||||
p[1] = ((ga + p[1] as i32 * inv) / 255) as u8;
|
||||
p[2] = ((ba + p[2] as i32 * inv) / 255) as u8;
|
||||
p[3] = clamp_u8(a + (p[3] as i32 * inv + 127) / 255);
|
||||
let px = u32::from_le_bytes([p[0], p[1], p[2], p[3]]);
|
||||
let mut rb = (px & 0x00FF_00FF) * inv + rbadd;
|
||||
let mut ag = ((px >> 8) & 0x00FF_00FF) * inv + agadd;
|
||||
rb = ((rb + ((rb >> 8) & 0x00FF_00FF) + 0x0001_0001) >> 8) & 0x00FF_00FF;
|
||||
ag = (ag + ((ag >> 8) & 0x00FF_00FF) + 0x0001_0001) & 0xFF00_FF00;
|
||||
p.copy_from_slice(&(rb | ag).to_le_bytes());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -353,8 +359,46 @@ fn draw_circle(c: &mut Canvas, d: &wt::Disc, ox: i32, oy: i32, clip: Clip) {
|
||||
let (cr, cg, cb, ca) = (d.color.r as i32, d.color.g as i32, d.color.b as i32, d.color.a as f64);
|
||||
let rad = d.radius as f64;
|
||||
let (cx, cy) = (d.cx + ox, d.cy + oy);
|
||||
// Row by row: the antialiased edge pixel by pixel, a filled disc's solid middle (dist <= rad
|
||||
// - 0.5, full coverage) as one span and a ring's empty middle (dist <= rad - 2) skipped. The
|
||||
// same pixels as testing every pixel of the bounding box.
|
||||
let solid = if d.filled {
|
||||
(rad - 0.5) * (rad - 0.5)
|
||||
} else if rad >= 2.0 {
|
||||
(rad - 2.0) * (rad - 2.0)
|
||||
} else {
|
||||
-1.0
|
||||
};
|
||||
for dy in (-d.radius - 1)..=(d.radius + 1) {
|
||||
for dx in (-d.radius - 1)..=(d.radius + 1) {
|
||||
let y = cy + dy;
|
||||
if y < 0 || y >= c.h || clip.is_some_and(|k| y < k.y || y >= k.y + k.h) {
|
||||
continue;
|
||||
}
|
||||
let left = solid - (dy * dy) as f64;
|
||||
let mut m: i32 = -1;
|
||||
if left >= 0.0 {
|
||||
m = left.sqrt() as i32;
|
||||
while ((m + 1) * (m + 1)) as f64 <= left {
|
||||
m += 1;
|
||||
}
|
||||
while m >= 0 && (m * m) as f64 > left {
|
||||
m -= 1;
|
||||
}
|
||||
}
|
||||
let mut dx = -d.radius - 1;
|
||||
while dx <= d.radius + 1 {
|
||||
if m >= 0 && dx == -m {
|
||||
if d.filled {
|
||||
let (mut xa, mut xb) = ((cx - m).max(0), (cx + m + 1).min(c.w));
|
||||
if let Some(k) = clip {
|
||||
xa = xa.max(k.x);
|
||||
xb = xb.min(k.x + k.w);
|
||||
}
|
||||
blend_span(c, y, xa, xb, cr, cg, cb, ca as i32);
|
||||
}
|
||||
dx = m + 1;
|
||||
continue;
|
||||
}
|
||||
let dist = ((dx * dx + dy * dy) as f64).sqrt();
|
||||
let outer = rad + 0.5 - dist;
|
||||
let mut cov = if outer > 1.0 { 1.0 } else { outer };
|
||||
@@ -366,8 +410,9 @@ fn draw_circle(c: &mut Canvas, d: &wt::Disc, ox: i32, oy: i32, clip: Clip) {
|
||||
}
|
||||
}
|
||||
if cov > 0.02 {
|
||||
blend_pixel(c, cx + dx, cy + dy, cr, cg, cb, (ca * cov).round() as i32, clip);
|
||||
blend_pixel(c, cx + dx, y, cr, cg, cb, (ca * cov).round() as i32, clip);
|
||||
}
|
||||
dx += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -433,6 +478,10 @@ pub struct Images {
|
||||
srcs: Vec<ImageSrc>,
|
||||
by_path: HashMap<String, u32>,
|
||||
sized: RefCell<HashMap<(u32, i32, i32), Rc<Vec<u8>>>>,
|
||||
/// Window previews (`Raster::thumbnail`): made at the size they settle at, so at any other
|
||||
/// size (gliding in the overview) they are sampled straight from their pixels, not scaled
|
||||
/// and kept.
|
||||
previews: std::collections::HashSet<u32>,
|
||||
}
|
||||
|
||||
/// Biggest image side accepted from a file, and drawn.
|
||||
@@ -563,12 +612,24 @@ impl Images {
|
||||
if reuse > 0 && (reuse as usize) <= self.srcs.len() && !self.by_path.values().any(|&v| v == reuse) {
|
||||
self.srcs[reuse as usize - 1] = src;
|
||||
self.sized.borrow_mut().retain(|k, _| k.0 != reuse);
|
||||
self.previews.remove(&reuse);
|
||||
return reuse;
|
||||
}
|
||||
self.srcs.push(src);
|
||||
self.srcs.len() as u32
|
||||
}
|
||||
|
||||
/// The pixels of preview `handle` when it is one and isn't `w` x `h`: (its w, h, pixels).
|
||||
fn preview_at_other_size(&self, handle: u32, w: i32, h: i32) -> Option<(i32, i32, &[u8])> {
|
||||
if !self.previews.contains(&handle) {
|
||||
return None;
|
||||
}
|
||||
match self.srcs.get((handle as usize).checked_sub(1)?)? {
|
||||
ImageSrc::Pixels { w: sw, h: sh, rgba } if (*sw, *sh) != (w, h) => Some((*sw, *sh, rgba)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Image `handle` at `w` x `h` (cached), or None for an unknown handle or a silly size.
|
||||
fn sized(&self, handle: u32, w: i32, h: i32) -> Option<Rc<Vec<u8>>> {
|
||||
if w <= 0 || h <= 0 || w > MAX_DRAW || h > MAX_DRAW {
|
||||
@@ -596,6 +657,21 @@ impl Images {
|
||||
|
||||
fn draw_image(c: &mut Canvas, imgs: &Images, p: &wt::Picture, ox: i32, oy: i32, clip: Clip) {
|
||||
let (w, h) = (p.area.w, p.area.h);
|
||||
if w > 0 && h > 0 && w <= MAX_DRAW && h <= MAX_DRAW {
|
||||
if let Some((sw, sh, src)) = imgs.preview_at_other_size(p.image, w, h) {
|
||||
// A moving preview: each pixel from the nearest source pixel, nothing kept.
|
||||
let (x0, y0) = (p.area.x + ox, p.area.y + oy);
|
||||
let sx: Vec<usize> = (0..w).map(|x| ((x as i64 * 2 + 1) * sw as i64 / (2 * w as i64)) as usize).collect();
|
||||
for y in 0..h {
|
||||
let row = ((y as i64 * 2 + 1) * sh as i64 / (2 * h as i64)) as usize * sw as usize;
|
||||
for (x, &cx) in sx.iter().enumerate() {
|
||||
let o = (row + cx) * 4;
|
||||
blend_pixel(c, x0 + x as i32, y0 + y, src[o] as i32, src[o + 1] as i32, src[o + 2] as i32, src[o + 3] as i32, clip);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
let Some(px) = imgs.sized(p.image, w, h) else { return };
|
||||
let (x0, y0) = (p.area.x + ox, p.area.y + oy);
|
||||
for y in 0..h {
|
||||
@@ -831,7 +907,9 @@ impl Raster {
|
||||
}
|
||||
let Some(s) = self.surfaces.get(&id) else { return 0 };
|
||||
let px = scale_rgba(&s.rgba, s.w, s.h, w, h);
|
||||
self.images.put_pixels(reuse, w, h, px)
|
||||
let handle = self.images.put_pixels(reuse, w, h, px);
|
||||
self.images.previews.insert(handle);
|
||||
handle
|
||||
}
|
||||
|
||||
pub fn surface_count(&self) -> usize {
|
||||
@@ -1226,6 +1304,62 @@ mod tests {
|
||||
assert_eq!(px(&r, 0, 0), [0, 0, 255, 255]);
|
||||
}
|
||||
|
||||
/// The old way: every pixel of the bounding box.
|
||||
fn circle_reference(c: &mut Canvas, d: &wt::Disc, clip: Clip) {
|
||||
let (cr, cg, cb, ca) = (d.color.r as i32, d.color.g as i32, d.color.b as i32, d.color.a as f64);
|
||||
let rad = d.radius as f64;
|
||||
for dy in (-d.radius - 1)..=(d.radius + 1) {
|
||||
for dx in (-d.radius - 1)..=(d.radius + 1) {
|
||||
let dist = ((dx * dx + dy * dy) as f64).sqrt();
|
||||
let outer = rad + 0.5 - dist;
|
||||
let mut cov = if outer > 1.0 { 1.0 } else { outer };
|
||||
if !d.filled {
|
||||
let inner = dist - (rad - 1.0 - 0.5);
|
||||
let ic = if inner > 1.0 { 1.0 } else { inner };
|
||||
if ic < cov {
|
||||
cov = ic;
|
||||
}
|
||||
}
|
||||
if cov > 0.02 {
|
||||
blend_pixel(c, d.cx + dx, d.cy + dy, cr, cg, cb, (ca * cov).round() as i32, clip);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn circles_drawn_by_rows_match_the_per_pixel_reference() {
|
||||
let clips = [None, Some(R { x: 13, y: 40, w: 50, h: 31 })];
|
||||
for radius in [1, 2, 3, 5, 17, 40, 70] {
|
||||
for filled in [true, false] {
|
||||
for alpha in [255u8, 200, 6] {
|
||||
for (ci, clip) in clips.iter().enumerate() {
|
||||
let d = wt::Disc { cx: 45, cy: 52, radius, color: color(250, 120, 30, alpha), filled };
|
||||
let mut a: Vec<u8> = (0..100 * 90 * 4).map(|i| (i * 7 % 251) as u8).collect();
|
||||
let mut b = a.clone();
|
||||
draw_circle(&mut Canvas { w: 100, h: 90, data: &mut a }, &d, 0, 0, *clip);
|
||||
circle_reference(&mut Canvas { w: 100, h: 90, data: &mut b }, &d, *clip);
|
||||
assert!(a == b, "radius {radius} filled {filled} alpha {alpha} clip {ci}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_preview_glides_sampled_and_keeps_no_scaled_copy() {
|
||||
let mut r = Raster::default();
|
||||
let src = [199u8, 99, 9, 255].repeat(16); // BGRA
|
||||
let s = r.add_surface(Surface::from_bgra(&src, 16, 4, 4, true).unwrap());
|
||||
let t = r.thumbnail(s, 2, 2, 0);
|
||||
assert_ne!(t, 0);
|
||||
r.submit(&wt::Frame { panel: vec![wt::Op::DrawImage(wt::Picture { area: rect(0, 0, 200, 150), image: t })], ..frame(300, 300) }, &mut Fonts::default());
|
||||
assert_eq!(px(&r, 0, 0), [9, 99, 199, 255]);
|
||||
assert_eq!(px(&r, 199, 149), [9, 99, 199, 255]);
|
||||
assert_eq!(px(&r, 200, 150), [0, 0, 0, 0]);
|
||||
assert!(r.images.sized.borrow().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_large_image_is_cached_at_one_large_size_only() {
|
||||
let mut imgs = Images::default();
|
||||
|
||||
Reference in new issue
Block a user