diff --git a/crates/accshift-egui/src/soft/paint.rs b/crates/accshift-egui/src/soft/paint.rs index db0bfbe8..bcc01557 100644 --- a/crates/accshift-egui/src/soft/paint.rs +++ b/crates/accshift-egui/src/soft/paint.rs @@ -12,7 +12,7 @@ use crate::bench; #[cfg(target_os = "linux")] use super::platform::resize_edge; use super::platform::{os_wants_full_paint, show_window}; -use super::present::{frame_buffer, read_pixels}; +use super::present::{frame_buffer, holds_last_frame, read_pixels}; use super::{Live, Runner, HIDDEN_FRAME_INTERVAL}; impl Runner { @@ -180,7 +180,7 @@ impl Runner { || show || overlay_active || live.overlay_shown - || buffer.age() == 0 + || !holds_last_frame(buffer.age()) || os_wants_full_paint(&live.window); // SAFETY: [u8; 4] has the size of u32 and a weaker alignment. let pixels = unsafe { @@ -193,6 +193,14 @@ impl Runner { window_px }; let mut textures = std::mem::take(&mut live.textures); + // Only an overlay's backdrop blur reads the stage's root + // pixels: without one, a glass layer skips their composite. + let overlay_backdrop = overlay_draw.as_ref().is_some_and(|(prims, _)| { + prims + .iter() + .any(|p| matches!(p.primitive, egui::epaint::Primitive::Callback(_))) + }); + live.render.keep_root_frame(overlay_backdrop); live.render.render_to_canvas( canvas_px[0], canvas_px[1], @@ -227,10 +235,9 @@ impl Runner { }; if let Some((overlay_prims, overlay_ppp)) = &overlay_draw { let mut overlay_textures = std::mem::take(&mut live.overlay_textures); - let has_backdrop = overlay_prims - .iter() - .any(|p| matches!(p.primitive, egui::epaint::Primitive::Callback(_))); - let root = has_backdrop.then(|| live.render.root_pixels()).flatten(); + let root = overlay_backdrop + .then(|| live.render.root_pixels()) + .flatten(); let backdrop_root = match root.as_deref() { Some(root) if zoomed => { // Kept across frames: it only reallocates on a resize. diff --git a/crates/accshift-egui/src/soft/present.rs b/crates/accshift-egui/src/soft/present.rs index 362c7ac6..1c89896f 100644 --- a/crates/accshift-egui/src/soft/present.rs +++ b/crates/accshift-egui/src/soft/present.rs @@ -144,6 +144,13 @@ pub(super) fn backdrop_op( }) } +/// Whether a window buffer of softbuffer age `age` holds the frame +/// presented just before, all a partial copy builds on: 0 is a new buffer, +/// 2 or more one of a swap chain (Wayland) that missed the frames since. +pub(super) fn holds_last_frame(age: u8) -> bool { + age == 1 +} + pub(super) fn read_pixels(bgra: &[[u8; 4]], size: [usize; 2]) -> Arc { let pixels = bgra .iter() diff --git a/crates/accshift-egui/src/soft/tests.rs b/crates/accshift-egui/src/soft/tests.rs index dbc29b30..dae4dbdc 100644 --- a/crates/accshift-egui/src/soft/tests.rs +++ b/crates/accshift-egui/src/soft/tests.rs @@ -2,7 +2,7 @@ use std::num::NonZeroU32; use std::time::Duration; use super::platform::is_shortcut_press; -use super::present::{frame_buffer, Faults, Present, LOGGED_FAULTS}; +use super::present::{frame_buffer, holds_last_frame, Faults, Present, LOGGED_FAULTS}; #[test] fn shortcut_capture_ignores_synthetic_focus_presses() { @@ -73,3 +73,260 @@ fn repeated_failures_back_off_and_stop_logging() { assert!(!faults.logged()); assert_eq!(faults.retry_after(), Duration::from_secs(1)); } + +/// A scene's app in the runner's shell without a window: what `paint` +/// hands the renderer. +struct Stage { + ctx: egui::Context, + shell: crate::Shell, + scale: f32, + /// Frames so far: the clock runs at 60 frames a second whatever the + /// machine does, so two runs draw the same frames. + frames: u32, + /// Deltas for the persistent renderer, cleared once applied. + textures: egui::TexturesDelta, + /// Every delta so far, for a renderer built fresh each frame. + every_texture: egui::TexturesDelta, +} + +impl Stage { + fn new(scene: &str, scale: f32) -> Stage { + let path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("parity/scenes") + .join(format!("{scene}.json")); + let scene = crate::harness::load(&path).unwrap(); + let ctx = egui::Context::default(); + let screen = crate::Screen::for_scene(&ctx, &scene).unwrap(); + let mut stage = Stage { + ctx, + shell: crate::Shell::new(screen, None), + scale, + frames: 0, + textures: Default::default(), + every_texture: Default::default(), + }; + let deadline = std::time::Instant::now() + Duration::from_secs(20); + let mut streak = 0; + while streak < 3 { + assert!(std::time::Instant::now() < deadline, "never settled"); + stage.frame(egui::pos2(250.0, 18.0)); + streak = if stage.shell.screen.ready() { + streak + 1 + } else { + 0 + }; + } + stage + } + + fn pixels(&self) -> [usize; 2] { + [(860.0 * self.scale) as usize, (440.0 * self.scale) as usize] + } + + /// One frame with the pointer at `pointer` (points): its primitives and + /// pixels per point. + fn frame(&mut self, pointer: egui::Pos2) -> (Vec, f32) { + self.frames += 1; + let mut raw = egui::RawInput { + focused: true, + time: Some(f64::from(self.frames) / 60.0), + screen_rect: Some(egui::Rect::from_min_size( + egui::Pos2::ZERO, + egui::vec2(860.0, 440.0), + )), + events: vec![egui::Event::PointerMoved(pointer)], + ..Default::default() + }; + let vp = raw.viewports.entry(egui::ViewportId::ROOT).or_default(); + vp.native_pixels_per_point = Some(self.scale); + vp.focused = Some(true); + let zoom = self.shell.stage_zoom(&self.ctx); + self.shell.input_hook(&self.ctx, &mut raw, zoom); + crate::zoom::begin_frame(&self.ctx, zoom, &raw); + let shell = &mut self.shell; + let mut out = self.ctx.run_ui(raw, |ui| shell.frame(ui)); + let delta = std::mem::take(&mut out.textures_delta); + self.every_texture.append(delta.clone()); + self.textures.append(delta); + ( + self.ctx.tessellate(out.shapes, out.pixels_per_point), + out.pixels_per_point, + ) + } +} + +impl Drop for Stage { + fn drop(&mut self) { + self.textures.clear(); + self.every_texture.clear(); + } +} + +fn renderer() -> egui_software_backend::EguiSoftwareRender { + egui_software_backend::EguiSoftwareRender::new(egui_software_backend::ColorFieldOrder::Bgra) + .with_glyph_coverage(crate::ui::text::coverage_tables()) + .with_backdrop(Box::new(super::present::backdrop_op)) +} + +/// What `paint` does with a frame and a window buffer that holds the last +/// one: the presented regions. +fn present( + render: &mut egui_software_backend::EguiSoftwareRender, + window: &mut [[u8; 4]], + [w, h]: [usize; 2], + (prims, ppp): &(Vec, f32), + textures: &mut egui::TexturesDelta, + full: bool, +) -> Vec<[usize; 4]> { + render.render_to_canvas(w, h, prims, textures, *ppp); + textures.clear(); + let mut buffer = egui_software_backend::BufferMutRef::new(window, w, h); + render.copy_canvas_to_buffer(&mut buffer, full) +} + +/// The cards the `cardext` scenes hover, with the title bar in between. +const HOVERS: [(f32, f32); 5] = [ + (205.0, 150.0), + (425.0, 150.0), + (250.0, 18.0), + (535.0, 150.0), + (755.0, 150.0), +]; + +/// Hovering glass cards, moving between them and leaving them: the window +/// buffer, fed only what each frame presents, holds the frame a renderer +/// built fresh for it draws, and a still hover presents less than the +/// whole window. +#[test] +fn a_hovered_glass_card_presents_only_what_changed() { + for scale in [1.0f32, 1.5] { + let mut stage = Stage::new("cardext-glass-dark-100", scale); + let size = stage.pixels(); + let mut render = renderer(); + let mut window = vec![[0u8; 4]; size[0] * size[1]]; + let mut first = true; + let mut still = usize::MAX; + for (n, &(x, y)) in HOVERS.iter().enumerate() { + for step in 0..4 { + // A pixel of jiggle: the pointer moves, the card stays hovered. + let frame = stage.frame(egui::pos2(x + (step % 2) as f32, y)); + let presented = present( + &mut render, + &mut window, + size, + &frame, + &mut stage.textures, + first, + ); + first = false; + if n == 0 && step == 3 { + still = presented.iter().map(|r| r[2] * r[3]).sum(); + } + let mut fresh = renderer(); + let mut expected = vec![[0u8; 4]; size[0] * size[1]]; + let mut every = stage.every_texture.clone(); + present(&mut fresh, &mut expected, size, &frame, &mut every, true); + // A cached canvas can land a level off a fresh one on a few + // edge pixels (main does it too, about one run in six); a + // stale pixel is a card's glass against the page. + let wrong = window + .iter() + .zip(&expected) + .filter(|(a, b)| a.iter().zip(b.iter()).any(|(a, b)| a.abs_diff(*b) > 2)) + .count(); + assert_eq!( + wrong, 0, + "@{scale} hover {n} step {step}: {wrong} pixels differ" + ); + } + } + let whole = size[0] * size[1]; + assert!( + still < whole / 2, + "@{scale}: a still hover presented {still} of {whole} pixels" + ); + } +} + +/// A glass card hovered for 150 frames, the pointer jiggling a pixel, the +/// first 30 left out: renderer time (`render_to_canvas` and the copy into +/// the window buffer) and presented pixels per frame. `cargo test +/// --release -p accshift-egui --lib hovered_glass_card_frame_cost -- +/// --ignored --nocapture`. +#[test] +#[ignore] +fn hovered_glass_card_frame_cost() { + for scale in [1.0f32, 1.5] { + let mut stage = Stage::new("cardext-glass-dark-100", scale); + let size = stage.pixels(); + let mut render = renderer(); + let mut window = vec![[0u8; 4]; size[0] * size[1]]; + let mut times = Vec::new(); + let mut presented = 0usize; + for i in 0..150 { + let frame = stage.frame(egui::pos2(205.0 + (i % 2) as f32, 150.0)); + let started = std::time::Instant::now(); + let rects = present( + &mut render, + &mut window, + size, + &frame, + &mut stage.textures, + i == 0, + ); + let ms = started.elapsed().as_secs_f64() * 1000.0; + if i >= 30 { + times.push(ms); + presented += rects.iter().map(|r| r[2] * r[3]).sum::(); + } + } + let frames = times.len(); + times.sort_by(|a, b| a.partial_cmp(b).unwrap()); + println!( + "hovered glass card @{scale}: {frames} frames, raster median {:.3} ms, p99 {:.3} ms, presented {} px a frame of {}", + times[frames / 2], + times[(frames * 99).div_ceil(100) - 1], + presented / frames, + size[0] * size[1], + ); + } +} + +/// Two window buffers presented in turn (Wayland's swap): each one holds +/// the frame before last, so it gets every pixel, and the old card leaves +/// no glass behind. +#[test] +fn swapped_window_buffers_keep_no_old_card() { + let mut stage = Stage::new("cardext-glass-dark-100", 1.0); + let size = stage.pixels(); + let mut render = renderer(); + let mut windows = [ + vec![[0u8; 4]; size[0] * size[1]], + vec![[0u8; 4]; size[0] * size[1]], + ]; + let mut frames = 0usize; + for &(x, y) in &HOVERS { + for step in 0..3 { + let frame = stage.frame(egui::pos2(x + (step % 2) as f32, y)); + // softbuffer's age: 0 for a buffer never presented, then 2. + let age = if frames < 2 { 0 } else { 2 }; + let window = &mut windows[frames % 2]; + let full = !holds_last_frame(age); + present(&mut render, window, size, &frame, &mut stage.textures, full); + let mut fresh = renderer(); + let mut expected = vec![[0u8; 4]; size[0] * size[1]]; + let mut every = stage.every_texture.clone(); + present(&mut fresh, &mut expected, size, &frame, &mut every, true); + let wrong = window + .iter() + .zip(&expected) + .filter(|(a, b)| a.iter().zip(b.iter()).any(|(a, b)| a.abs_diff(*b) > 2)) + .count(); + assert_eq!( + wrong, 0, + "frame {frames} at ({x}, {y}): {wrong} pixels differ" + ); + frames += 1; + } + } +} diff --git a/crates/accshift-egui/vendor/egui_software_backend/src/lib.rs b/crates/accshift-egui/vendor/egui_software_backend/src/lib.rs index afe78589..477a6f7e 100644 --- a/crates/accshift-egui/vendor/egui_software_backend/src/lib.rs +++ b/crates/accshift-egui/vendor/egui_software_backend/src/lib.rs @@ -99,6 +99,8 @@ pub struct EguiSoftwareRender { overlay_blurs: Vec>, /// How many layer backdrops `render_overlay` has blurred. overlay_blur_runs: usize, + /// How many first-layer backdrops `render_to_canvas` has blurred. + blur_runs: usize, #[cfg(feature = "raster_stats")] pub stats: RasterStats, } @@ -288,10 +290,105 @@ struct Backdrop { root_frame: Option>, /// The filtered element alone, before it landed (same key as `filtered`). root_filtered: Vec<[u8; 4]>, + /// What `out` holds from the last frame (accshift patch: a hovered + /// glass card copied the whole canvas into `out`, then `out` into the + /// window, on every frame). + out_kind: OutKind, + /// The regions of `out` that differ from what it was copied from (the + /// canvas, or `filtered`): where the layers landed and the jobs after + /// them drew, as `[x, y, width, height]`. + touched: Vec, + /// The regions of `out` this frame changed, `None` for all of it. + damage: Option>, + /// Once the layers are gone, the regions the window buffer takes back + /// from the canvas besides its dirty tiles. + restore: Vec, + /// The cached primitives under the first layer's box when it was + /// blurred ([`EguiSoftwareRender::blur_fingerprint`]). + blur_fingerprint: u64, + /// The caller needs no `root_pixels` this frame: a single layer skips + /// the full composite that keeps them. + skip_root_frame: bool, + /// `root_frame` was skipped though a layer or a filter changed the + /// frame: the cached primitives alone would be wrong. + root_frame_skipped: bool, } type FilterKey = (usize, usize, [u32; 3], Option); +/// `[x, y, width, height]` in device pixels. +type Region = [usize; 4]; + +/// What `Backdrop::out` holds. +#[derive(Clone, Copy, Debug, Default, PartialEq)] +enum OutKind { + /// Nothing worth keeping. + #[default] + None, + /// The canvas with the layers landed ([`EguiSoftwareRender::compose_backdrop`]). + Layers, + /// `filtered` with the jobs after the filter ([`EguiSoftwareRender::compose_filter`]). + Filter, +} + +/// Pixels a drawn mesh may reach past its vertices' box: the rasterizer's +/// rounding, kept generous since a region too small leaves a stale pixel. +const DRAW_MARGIN: usize = 2; + +/// `span` as a region. +fn region([columns, rows]: &[Range; 2]) -> Region { + [columns.start, rows.start, columns.len(), rows.len()] +} + +/// `span` grown by `margin` pixels on each side, inside `width` x `height`. +fn grown( + [columns, rows]: [Range; 2], + margin: usize, + width: usize, + height: usize, +) -> [Range; 2] { + if columns.is_empty() || rows.is_empty() { + return [0..0, 0..0]; + } + [ + columns.start.saturating_sub(margin)..(columns.end + margin).min(width), + rows.start.saturating_sub(margin)..(rows.end + margin).min(height), + ] +} + +/// `regions` without the empty ones and the ones another holds. +fn distinct(mut regions: Vec) -> Vec { + regions.retain(|r| r[2] > 0 && r[3] > 0); + regions.sort_unstable(); + regions.dedup(); + let holds = |a: &Region, b: &Region| { + a[0] <= b[0] && a[1] <= b[1] && a[0] + a[2] >= b[0] + b[2] && a[1] + a[3] >= b[1] + b[3] + }; + let kept: Vec = (0..regions.len()) + .map(|i| { + !regions + .iter() + .enumerate() + .any(|(j, other)| j != i && holds(other, ®ions[i])) + }) + .collect(); + regions + .into_iter() + .zip(kept) + .filter_map(|(r, keep)| keep.then_some(r)) + .collect() +} + +/// Copies `regions` of `src` into `dst`, both `width` pixels wide. +fn copy_regions(dst: &mut [[u8; 4]], src: &[[u8; 4]], width: usize, regions: &[Region]) { + for &[x, y, w, h] in regions { + for row in y..y + h { + let span = row * width + x..row * width + x + w; + dst[span.clone()].copy_from_slice(&src[span]); + } + } +} + impl EguiSoftwareRender { /// Bytes the renderer holds between frames: the primitive cache (its /// count, its bytes, how many of its buffers reach 1 MiB), the @@ -342,6 +439,7 @@ impl EguiSoftwareRender { backdrop: Backdrop::default(), overlay_blurs: Vec::new(), overlay_blur_runs: 0, + blur_runs: 0, #[cfg(feature = "raster_stats")] stats: Default::default(), } @@ -543,7 +641,9 @@ impl EguiSoftwareRender { .map(|&(i, _)| i) .next_back(); self.backdrop.root_frame = None; + self.backdrop.root_frame_skipped = false; let was_shown = self.backdrop.shown; + let was_kind = self.backdrop.out_kind; self.backdrop.active = false; if let Some(( f, @@ -583,7 +683,15 @@ impl EguiSoftwareRender { ); self.backdrop.active = true; } - self.backdrop.force_full = was_shown && !self.backdrop.active; + // The layers left: the window buffer gets back the canvas where they + // were. A filter changed every pixel. + let gone = was_shown && !self.backdrop.active; + self.backdrop.restore = if gone && was_kind == OutKind::Layers { + core::mem::take(&mut self.backdrop.touched) + } else { + Vec::new() + }; + self.backdrop.force_full = gone && was_kind != OutKind::Layers; self.backdrop.shown = self.backdrop.active; if !self.backdrop.active { // Three frames' worth of pixels that only a layer or a filter @@ -594,6 +702,9 @@ impl EguiSoftwareRender { self.backdrop.root_filtered = Vec::new(); self.backdrop.key = None; self.backdrop.filter_key = None; + self.backdrop.out_kind = OutKind::None; + self.backdrop.touched = Vec::new(); + self.backdrop.damage = None; } self.free_textures(textures_delta); @@ -603,13 +714,25 @@ impl EguiSoftwareRender { /// present, as they rendered: filter and layers included (used by an /// unscaled overlay pass so a stand-in desktop stays sharp under the /// overlay's backdrop blur, which must see the shell the stage showed). + /// None after a frame drawn with [`Self::keep_root_frame`] off and a + /// layer or a filter. pub fn root_pixels(&self) -> Option> { + if self.backdrop.root_frame_skipped { + return None; + } match &self.backdrop.root_frame { Some(frame) => Some(frame.clone()), None => self.backdrop.last_root.map(|r| self.composite_after(r)), } } + /// Whether the next frames keep what [`Self::root_pixels`] returns + /// (on by default). Off, a frame with a single layer composites only + /// the pixels that changed. + pub fn keep_root_frame(&mut self, keep: bool) { + self.backdrop.skip_root_frame = !keep; + } + /// Drops the blurs `render_overlay` kept, once no overlay shows. pub fn forget_overlay_backdrops(&mut self) { self.overlay_blurs = Vec::new(); @@ -742,8 +865,13 @@ impl EguiSoftwareRender { /// (the backdrop root's pixels so far: cached primitives after `root`, /// or the whole canvas) blurred, takes its own jobs, and lands on the /// frame; the jobs up to the next layer draw on top. The first layer's - /// blur is kept for the next frame; a later one's backdrop holds the - /// layers before it, so it is blurred again each frame. + /// blur is kept for the next frame while the primitives under its box + /// stay; a later one's backdrop holds the layers before it, so it is + /// blurred again each frame. + /// + /// `out` keeps the last frame: only the canvas's dirty tiles and the + /// regions the layers touched, this frame or the last, are copied from + /// the canvas and drawn again (`damage`). fn compose_backdrop( &mut self, paint_jobs: &[egui::ClippedPrimitive], @@ -753,13 +881,50 @@ impl EguiSoftwareRender { canvas_changed: bool, ) { let (width, height) = (self.canvas.width, self.canvas.height); + // Every pixel the layers and the jobs after them write to. + let mut touched = Vec::with_capacity(layers.len() * 2); + for (n, layer) in layers.iter().enumerate() { + let jobs = paint_jobs.get(layer.begin + 1..layer.end).unwrap_or(&[]); + let span = match layer.op { + BackdropOp::Group { .. } => group_span(jobs, pixels_per_point, width, height), + BackdropOp::Begin { clip, .. } => { + layer_span(clip, jobs, pixels_per_point, width, height) + } + _ => continue, + }; + let next = layers.get(n + 1).map_or(paint_jobs.len(), |l| l.begin); + let after = paint_jobs.get(layer.end + 1..next).unwrap_or(&[]); + let drawn = group_span(after, pixels_per_point, width, height); + touched.push(region(&span)); + touched.push(region(&grown(drawn, DRAW_MARGIN, width, height))); + } + let touched = distinct(touched); + let mut out = core::mem::take(&mut self.backdrop.out); - out.clear(); - out.extend_from_slice(&self.canvas.data); + let size_kept = out.len() == width * height && !self.redraw_everything_this_frame; + let damage = if size_kept && self.backdrop.out_kind == OutKind::Layers { + // Last frame's `out` is the canvas outside its touched regions. + let mut regions = self.dirty_regions(); + regions.extend_from_slice(&self.backdrop.touched); + regions.extend_from_slice(&touched); + let regions = distinct(regions); + copy_regions(&mut out, &self.canvas.data, width, ®ions); + Some(regions) + } else { + out.clear(); + out.extend_from_slice(&self.canvas.data); + // The window buffer holds the last canvas when no layer showed. + (!self.backdrop.shown && !self.redraw_everything_this_frame).then(|| { + let mut regions = self.dirty_regions(); + regions.extend_from_slice(&touched); + distinct(regions) + }) + }; // A later layer's backdrop, and the root's frame for an overlay: // the root's pixels with what came since landed on them. The frame // itself serves when there is no root. - let mut under = root.map(|r| self.composite_after(r)); + let keep_under = layers.len() > 1 || !self.backdrop.skip_root_frame; + let mut under = root.filter(|_| keep_under).map(|r| self.composite_after(r)); for (n, layer) in layers.iter().enumerate() { let jobs = paint_jobs.get(layer.begin + 1..layer.end).unwrap_or(&[]); let (pixels, opacity, span) = match layer.op { @@ -783,7 +948,11 @@ impl EguiSoftwareRender { backdrop::begin_key(sigma, saturate, clip), root, ); - if canvas_changed || self.backdrop.key != Some(key) { + let fingerprint = self.blur_fingerprint(root, clip); + if self.backdrop.key != Some(key) + || self.backdrop.blur_fingerprint != fingerprint + || self.dirty_under(clip, canvas_changed) + { // A clipped backdrop reads its box only: the // root's tiles there are enough. let source: Cow<[[u8; 4]]> = match root { @@ -794,6 +963,8 @@ impl EguiSoftwareRender { &source, width, height, clip, sigma, saturate, ); self.backdrop.key = Some(key); + self.backdrop.blur_fingerprint = fingerprint; + self.blur_runs += 1; } // Only the span lands: the rest of a fresh buffer // stays untouched zero pages. @@ -825,9 +996,106 @@ impl EguiSoftwareRender { ); } self.backdrop.out = out; + self.backdrop.out_kind = OutKind::Layers; + self.backdrop.touched = touched; + self.backdrop.damage = damage; + self.backdrop.root_frame_skipped = root.is_some() && under.is_none(); self.backdrop.root_frame = under; } + /// The canvas tiles the last `render_to_canvas` recomposited, one + /// region per run of them in a tile row. + fn dirty_regions(&self) -> Vec { + let (width, height) = (self.canvas.width, self.canvas.height); + let [tiles_x, tiles_y] = self.tiles_dim; + let dirty = |tx: usize, ty: usize| { + self.dirty_tiles + .get(tx + ty * tiles_x) + .is_some_and(|mask| mask & Self::DIRTY_TILE_MASK != 0) + }; + let mut regions = Vec::new(); + for ty in 0..tiles_y { + let mut tx = 0; + while tx < tiles_x { + if !dirty(tx, ty) { + tx += 1; + continue; + } + let run_start = tx; + while tx < tiles_x && dirty(tx, ty) { + tx += 1; + } + let x0 = run_start * TILE_SIZE; + let x1 = (tx * TILE_SIZE).min(width); + let y0 = ty * TILE_SIZE; + let y1 = (y0 + TILE_SIZE).min(height); + regions.push([x0, y0, x1 - x0, y1 - y0]); + } + } + regions + } + + /// The tiles `clip` reads ([`Self::composite_after_in`]), all of them + /// without a clip. + fn clip_tiles(&self, clip: Option) -> [Range; 2] { + let Some(clip) = clip else { + return [0..self.tiles_dim[0], 0..self.tiles_dim[1]]; + }; + let tiles = |lo: f32, hi: f32, n: usize| { + let lo = (lo.floor().max(0.0) as usize / TILE_SIZE).min(n); + let hi = (hi.ceil().max(0.0) as usize).div_ceil(TILE_SIZE).min(n); + lo..hi + }; + [ + tiles(clip.min[0], clip.max[0], self.tiles_dim[0]), + tiles(clip.min[1], clip.max[1], self.tiles_dim[1]), + ] + } + + /// Whether this frame recomposited a tile the backdrop under `clip` + /// reads. + fn dirty_under(&self, clip: Option, canvas_changed: bool) -> bool { + if clip.is_none() { + return canvas_changed; + } + let [columns, rows] = self.clip_tiles(clip); + rows.into_iter().any(|ty| { + columns.clone().any(|tx| { + self.dirty_tiles + .get(tx + ty * self.tiles_dim[0]) + .is_some_and(|mask| mask & Self::DIRTY_TILE_MASK != 0) + }) + }) + } + + /// The cached primitives after `root` on the tiles `clip` reads, in + /// paint order, as one number: a primitive crossing the root, one that + /// came or went, or two that swapped, without leaving a dirty tile, + /// changes it. + fn blur_fingerprint(&self, root: Option, clip: Option) -> u64 { + let [columns, rows] = self.clip_tiles(clip); + let mut under: Vec<(usize, u32)> = self + .cached_primitives + .iter() + .filter(|(_, prim)| { + !root.is_some_and(|r| prim.z_order <= r) + && prim.occupied_tiles.iter().any(|&[tx, ty]| { + columns.contains(&(tx as usize)) && rows.contains(&(ty as usize)) + }) + }) + .map(|(&hash, prim)| (prim.z_order, hash)) + .collect(); + under.sort_unstable(); + // The order matters, the paint indices themselves do not: a + // primitive added elsewhere shifts them all. + under + .iter() + .fold(0xCBF2_9CE4_8422_2325u64, |acc, &(_, hash)| { + (acc ^ u64::from(hash)).wrapping_mul(0x0000_0100_0000_01B3) + }) + ^ (under.len() as u64).rotate_left(48) + } + /// Draws `jobs` on `buffer`, a layer nested in them as its own: it /// starts as its backdrop in `buffer`, takes its jobs (layers nested in /// it included) and lands at its opacity, like the frame's top-level @@ -890,6 +1158,9 @@ impl EguiSoftwareRender { /// The filter of `paint_jobs[f]` on the element from `root` on, landed /// on what came before `root`, then the jobs after `f` drawn on top. + /// While the filtered pixels stay, `out` keeps the last frame and only + /// the regions the jobs after `f` drew, this frame or the last, are + /// drawn again. fn compose_filter( &mut self, paint_jobs: &[egui::ClippedPrimitive], @@ -902,7 +1173,8 @@ impl EguiSoftwareRender { let (width, height) = (self.canvas.width, self.canvas.height); let bits = [sigma.to_bits(), saturate.to_bits(), scale.to_bits()]; let key = (width, height, bits, root); - if canvas_changed || self.backdrop.filter_key != Some(key) { + let refilter = canvas_changed || self.backdrop.filter_key != Some(key); + if refilter { let filtered = match root { Some(r) => { let element = self.composite_where(|z| z > r); @@ -922,17 +1194,34 @@ impl EguiSoftwareRender { self.backdrop.filtered = filtered; self.backdrop.filter_key = Some(key); } - let mut out = core::mem::take(&mut self.backdrop.out); - out.clear(); - out.extend_from_slice(&self.backdrop.filtered); let after = paint_jobs.get(f + 1..).unwrap_or(&[]); + let drawn = group_span(after, pixels_per_point, width, height); + let touched = distinct(vec![region(&grown(drawn, DRAW_MARGIN, width, height))]); + let mut out = core::mem::take(&mut self.backdrop.out); + let damage = if !refilter + && self.backdrop.out_kind == OutKind::Filter + && out.len() == width * height + { + let mut regions = self.backdrop.touched.clone(); + regions.extend_from_slice(&touched); + let regions = distinct(regions); + copy_regions(&mut out, &self.backdrop.filtered, width, ®ions); + Some(regions) + } else { + out.clear(); + out.extend_from_slice(&self.backdrop.filtered); + None + }; self.draw_jobs_direct( &mut BufferMutRef::new(&mut out, width, height), after, pixels_per_point, ); self.backdrop.out = out; - if root.is_some() { + self.backdrop.out_kind = OutKind::Filter; + self.backdrop.touched = touched; + self.backdrop.damage = damage; + if root.is_some() && !self.backdrop.skip_root_frame { let mut frame = self.backdrop.root_filtered.clone(); self.draw_jobs_direct( &mut BufferMutRef::new(&mut frame, width, height), @@ -941,6 +1230,7 @@ impl EguiSoftwareRender { ); self.backdrop.root_frame = Some(frame); } + self.backdrop.root_frame_skipped = root.is_some() && self.backdrop.skip_root_frame; } /// The cached primitives painted after `after`, over transparent. @@ -1122,49 +1412,36 @@ impl EguiSoftwareRender { /// Added for the accshift spike. Unlike `blit_canvas_to_buffer` this /// overwrites instead of blending, so the buffer needs no clear: the /// canvas already holds the final premultiplied pixels over transparent. + /// Under a backdrop layer or filter the regions are the ones its frame + /// changed, and after one the regions it covered come back too. pub fn copy_canvas_to_buffer(&self, buffer: &mut BufferMutRef, full: bool) -> Vec<[usize; 4]> { let width = self.canvas.width; let height = self.canvas.height; assert_eq!(buffer.data.len(), width * height); assert_eq!(self.canvas.data.len(), width * height); if self.backdrop.active { - buffer.data.copy_from_slice(&self.backdrop.out); - return vec![[0, 0, width, height]]; + return match &self.backdrop.damage { + Some(regions) if !full => { + copy_regions(buffer.data, &self.backdrop.out, width, regions); + regions.clone() + } + _ => { + buffer.data.copy_from_slice(&self.backdrop.out); + vec![[0, 0, width, height]] + } + }; } if full || self.backdrop.force_full { buffer.data.copy_from_slice(&self.canvas.data); return vec![[0, 0, width, height]]; } - let [tiles_x, tiles_y] = self.tiles_dim; - let dirty = |tx: usize, ty: usize| { - self.dirty_tiles - .get(tx + ty * tiles_x) - .is_some_and(|mask| mask & Self::DIRTY_TILE_MASK != 0) - }; - let mut rects = Vec::new(); - for ty in 0..tiles_y { - let mut tx = 0; - while tx < tiles_x { - if !dirty(tx, ty) { - tx += 1; - continue; - } - let run_start = tx; - while tx < tiles_x && dirty(tx, ty) { - tx += 1; - } - let x0 = run_start * TILE_SIZE; - let x1 = (tx * TILE_SIZE).min(width); - let y0 = ty * TILE_SIZE; - let y1 = (y0 + TILE_SIZE).min(height); - for y in y0..y1 { - let row = y * width + x0..y * width + x1; - buffer.data[row.clone()].copy_from_slice(&self.canvas.data[row]); - } - rects.push([x0, y0, x1 - x0, y1 - y0]); - } + let mut regions = self.dirty_regions(); + if !self.backdrop.restore.is_empty() { + regions.extend_from_slice(&self.backdrop.restore); + regions = distinct(regions); } - rects + copy_regions(buffer.data, &self.canvas.data, width, ®ions); + regions } #[allow(clippy::too_many_arguments)] @@ -2346,4 +2623,235 @@ mod layer_tests { frame(&mut render, &striped(1), 3.0); assert_eq!(render.overlay_blur_runs, 3, "so is a changed filter"); } + + #[test] + fn a_still_layer_copies_only_what_changed() { + let classify = || { + alloc::boxed::Box::new(|cb: &egui::PaintCallback| { + cb.callback.downcast_ref::().copied() + }) + }; + let (w, h) = (192, 128); + let rect = |x0: f32, y0: f32, x1: f32, y1: f32| { + egui::Rect::from_min_max(egui::pos2(x0, y0), egui::pos2(x1, y1)) + }; + let whole = rect(0.0, 0.0, w as f32, h as f32); + let cb = |op: BackdropOp, at: egui::Rect| egui::ClippedPrimitive { + clip_rect: whole, + primitive: egui::epaint::Primitive::Callback(egui::PaintCallback { + rect: at, + callback: alloc::sync::Arc::new(op), + }), + }; + let fill = |at: egui::Rect, color: Color32| { + let mut mesh = Mesh::default(); + mesh.add_colored_rect(at, color); + egui::ClippedPrimitive { + clip_rect: whole, + primitive: egui::epaint::Primitive::Mesh(mesh), + } + }; + let clip = backdrop::BackdropClip { + min: [140.0, 70.0], + max: [180.0, 110.0], + radius: 6.0, + }; + // A shell, a dot far from the layer that changes, the layer over + // stripes, and a label drawn after it. + let jobs = |dot: u8| { + vec![ + fill(whole, Color32::from_rgb(30, 30, 60)), + cb(BackdropOp::Root, whole), + fill(rect(0.0, 0.0, 192.0, 128.0), Color32::from_rgb(40, 50, 70)), + fill( + rect(130.0, 60.0, 150.0, 128.0), + Color32::from_rgb(220, 40, 40), + ), + fill(rect(10.0, 10.0, 20.0, 20.0), Color32::from_rgb(dot, 200, 0)), + cb( + BackdropOp::Begin { + sigma: 4.0, + opacity: 0.9, + saturate: 1.15, + clip: Some(clip), + }, + rect(140.0, 70.0, 180.0, 110.0), + ), + fill( + rect(140.0, 70.0, 180.0, 110.0), + Color32::from_rgba_premultiplied(10, 10, 10, 120), + ), + cb(BackdropOp::End, rect(140.0, 70.0, 180.0, 110.0)), + fill(rect(150.0, 80.0, 170.5, 90.5), Color32::WHITE), + ] + }; + let texture = || { + let mut delta = egui::TexturesDelta::default(); + delta.set.insert( + egui::TextureId::Managed(0), + [egui::epaint::ImageDelta::full( + egui::ImageData::Color(alloc::sync::Arc::new(egui::ColorImage::filled( + [1, 1], + Color32::WHITE, + ))), + egui::TextureOptions::NEAREST, + )] + .into_iter() + .collect(), + ); + delta + }; + let mut render = EguiSoftwareRender::new(ColorFieldOrder::Rgba).with_backdrop(classify()); + render.keep_root_frame(false); + let mut window = vec![[0u8; 4]; w * h]; + let mut tex = texture(); + for (n, dot) in [0u8, 0, 90, 90, 180].into_iter().enumerate() { + render.render_to_canvas(w, h, &jobs(dot), &tex, 1.0); + tex.clear(); + let regions = + render.copy_canvas_to_buffer(&mut BufferMutRef::new(&mut window, w, h), n == 0); + let mut fresh = + EguiSoftwareRender::new(ColorFieldOrder::Rgba).with_backdrop(classify()); + let mut fresh_tex = texture(); + fresh.render_to_canvas(w, h, &jobs(dot), &fresh_tex, 1.0); + fresh_tex.clear(); + let mut expected = vec![[0u8; 4]; w * h]; + fresh.copy_canvas_to_buffer(&mut BufferMutRef::new(&mut expected, w, h), true); + assert!(window == expected, "frame {n}: the window holds the frame"); + assert!(render.root_pixels().is_none(), "no root frame was kept"); + let presented: usize = regions.iter().map(|r| r[2] * r[3]).sum(); + if n > 0 { + assert!( + presented < w * h / 2, + "frame {n} presented {presented} of {} pixels", + w * h + ); + } + } + assert_eq!( + render.blur_runs, 1, + "a dot outside the layer's box never blurs it again" + ); + // Asked for, the root frame is there again. + render.keep_root_frame(true); + render.render_to_canvas(w, h, &jobs(180), &tex, 1.0); + assert!(render.root_pixels().is_some()); + // Once the layer goes, the window gets the canvas back where it was. + let plain = &jobs(180)[..5]; + render.render_to_canvas(w, h, plain, &tex, 1.0); + render.copy_canvas_to_buffer(&mut BufferMutRef::new(&mut window, w, h), false); + let mut fresh = EguiSoftwareRender::new(ColorFieldOrder::Rgba).with_backdrop(classify()); + let mut fresh_tex = texture(); + fresh.render_to_canvas(w, h, plain, &fresh_tex, 1.0); + fresh_tex.clear(); + let mut expected = vec![[0u8; 4]; w * h]; + fresh.copy_canvas_to_buffer(&mut BufferMutRef::new(&mut expected, w, h), true); + assert!(window == expected, "the layer's pixels are gone"); + } + + #[test] + fn swapped_shapes_under_a_layer_blur_it_again() { + let classify = || { + alloc::boxed::Box::new(|cb: &egui::PaintCallback| { + cb.callback.downcast_ref::().copied() + }) + }; + let (w, h) = (192, 128); + let rect = |x0: f32, y0: f32, x1: f32, y1: f32| { + egui::Rect::from_min_max(egui::pos2(x0, y0), egui::pos2(x1, y1)) + }; + let whole = rect(0.0, 0.0, w as f32, h as f32); + let cb = |op: BackdropOp, at: egui::Rect| egui::ClippedPrimitive { + clip_rect: whole, + primitive: egui::epaint::Primitive::Callback(egui::PaintCallback { + rect: at, + callback: alloc::sync::Arc::new(op), + }), + }; + let fill = |at: egui::Rect, color: Color32| { + let mut mesh = Mesh::default(); + mesh.add_colored_rect(at, color); + egui::ClippedPrimitive { + clip_rect: whole, + primitive: egui::epaint::Primitive::Mesh(mesh), + } + }; + let clip = backdrop::BackdropClip { + min: [100.0, 40.0], + max: [180.0, 120.0], + radius: 0.0, + }; + // Red and green overlap under the layer; the second frame swaps + // them (the same meshes, so the same cached primitives) and moves + // a dot far away, so the canvas changed elsewhere. + let jobs = |green_on_top: bool, dot: u8| { + let red = fill( + rect(110.0, 50.0, 160.0, 110.0), + Color32::from_rgb(220, 30, 30), + ); + let green = fill( + rect(120.0, 60.0, 170.0, 115.0), + Color32::from_rgb(30, 200, 30), + ); + let (under, over) = if green_on_top { + (red, green) + } else { + (green, red) + }; + vec![ + fill(whole, Color32::from_rgb(30, 30, 60)), + cb(BackdropOp::Root, whole), + fill(whole, Color32::from_rgb(40, 50, 70)), + under, + over, + fill(rect(10.0, 10.0, 20.0, 20.0), Color32::from_rgb(dot, 200, 0)), + cb( + BackdropOp::Begin { + sigma: 2.0, + opacity: 1.0, + saturate: 1.0, + clip: Some(clip), + }, + rect(100.0, 40.0, 180.0, 120.0), + ), + cb(BackdropOp::End, rect(100.0, 40.0, 180.0, 120.0)), + ] + }; + let texture = || { + let mut delta = egui::TexturesDelta::default(); + delta.set.insert( + egui::TextureId::Managed(0), + [egui::epaint::ImageDelta::full( + egui::ImageData::Color(alloc::sync::Arc::new(egui::ColorImage::filled( + [1, 1], + Color32::WHITE, + ))), + egui::TextureOptions::NEAREST, + )] + .into_iter() + .collect(), + ); + delta + }; + let shown = |render: &mut EguiSoftwareRender, jobs: &[egui::ClippedPrimitive]| { + let mut tex = texture(); + render.render_to_canvas(w, h, jobs, &tex, 1.0); + tex.clear(); + let mut window = vec![[0u8; 4]; w * h]; + render.copy_canvas_to_buffer(&mut BufferMutRef::new(&mut window, w, h), true); + window + }; + let mut render = EguiSoftwareRender::new(ColorFieldOrder::Rgba).with_backdrop(classify()); + shown(&mut render, &jobs(true, 0)); + let swapped = shown(&mut render, &jobs(false, 90)); + let mut fresh = EguiSoftwareRender::new(ColorFieldOrder::Rgba).with_backdrop(classify()); + let expected = shown(&mut fresh, &jobs(false, 90)); + // Inside the layer's box, where only its blur shows. + let wrong = (42..118) + .flat_map(|y| (102..178).map(move |x| y * w + x)) + .filter(|&i| swapped[i] != expected[i]) + .count(); + assert_eq!(wrong, 0, "the layer still blurs the old order"); + assert_eq!(render.blur_runs, 2); + } }