diff --git a/CHANGELOG.md b/CHANGELOG.md index b2b29e37f..c96b144ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,33 @@ hashes are given where a change is easiest to understand by reading the commit. ## November 2026 +### Display + +- The O2's display scans out and presents a frame only when the screen may have + changed: the rendering engine wrote memory, a GBE register changed (IRIX's + per-frame rewrites of the same value do not count), a shadow register went live + at vertical blanking, or a snapshot was restored. An idle desktop no longer + decodes 1.3 million pixels sixty times a second. The decode writes the final + pixel format, so the frame goes to the renderer by a row copy. A screenshot, a + frame request or a VNC client still gets a fresh frame, and a window still + has its status bar redrawn about ten times a second. While a window or a VNC + client watches, the screen is scanned at least once a second whatever marked + it, so a guest that draws with CPU stores and no command (a Linux frame buffer + console) is at most a second stale. +- The VPro (Octane2, Tezro) display composes no frames while nothing shows or + reads them: `--ci` with no window, and no screenshot, frame request or VNC + client. With the GPU raster unit a frame was a GPU scan-out, a readback and a + pass over every pixel, sixty times a second, for nobody. The retrace still runs + and the screen's size is still known; a request composes the next frame whole. +- Newport (Indy, Indigo2 XL): the display marks the frame buffer changed after a + GFIFO entry's effect, not before it. A frame that took the mark while a draw + was still landing composed without it, and nothing marked again until the next + command, so the last draw of a burst could stay off the screen while the guest + idled. +- `screen frames [reset]` on the monitor says how many frames a board's display + thread ran, composed and skipped. The O2 and the VPro count them; the other + boards say they do not yet. + ### Bench rig - The bare-metal bench rig (`[testdev]`) takes `cpu = "r14000_500"`, the diff --git a/debug.md b/debug.md index 56c9bce45..9658d7216 100644 --- a/debug.md +++ b/debug.md @@ -537,6 +537,7 @@ to learn the cursor's hotspot exactly. | `screen shot [x y w h]` | The composed screen as PNG (no host status bar), optionally cropped | | `screen pix ` | One pixel, `rrggbb` | | `screen wait [quiet_ms] [timeout_ms]` | Wait until the screen has not changed for `quiet_ms` (default 500, at most 10000 ms) | +| `screen frames [reset]` | How many frames the display thread ran, composed and skipped (O2 and VPro; the other boards say they do not count), and zero them | | `screen cursor` | The cursor glyph as text, with its hotspot | | `ptr pos` | Where the pointer is | | `ptr move ` | Put the pointer on a pixel | diff --git a/src/dev/crime_mem.rs b/src/dev/crime_mem.rs index f8501c5cd..7a6a80b3f 100644 --- a/src/dev/crime_mem.rs +++ b/src/dev/crime_mem.rs @@ -440,6 +440,12 @@ impl O2Memory { .collect() } + /// The scan-out's change flag: marked when the rendering engine wrote + /// memory or memory was restored. + pub fn screen_changed(&self) -> &std::sync::Arc { + &self.shared.changed + } + /// Put back words [`Self::bank_words`] gave, concatenated, with every /// page's generation bumped so no compiled code outlives them. pub fn restore_words(&self, words: &[u32]) -> Result<(), String> { @@ -457,6 +463,7 @@ impl O2Memory { b.bump_gen_all(); at += n; } + self.shared.changed.mark(); Ok(()) } @@ -551,6 +558,9 @@ impl std::ops::IndexMut for O2Memory { pub struct WindowState { mapped: std::sync::atomic::AtomicU32, decode_gen: std::sync::atomic::AtomicU64, + /// Marked when the rendering engine has written memory, and when a snapshot + /// puts memory back: the scan-out reads it. + changed: std::sync::Arc, } /// Memory as a master on its own thread sees it: guest physical addresses @@ -594,6 +604,12 @@ impl RamWindow { // Release, after the run's bytes (`PpMemory::bump_gen`). unsafe { (*self.gens.add(page)).fetch_add(1, std::sync::atomic::Ordering::Release) }; } + // One mark per run that wrote, after its bytes. The engine may have + // drawn into a buffer that is not on screen: that costs a frame, and a + // missed change would cost the picture. + if !self.dirty.is_empty() { + self.shared.changed.mark(); + } self.dirty.clear(); } diff --git a/src/dev/kona/back/display/screen.rs b/src/dev/kona/back/display/screen.rs index c279c4a0b..dbafe5f4a 100644 --- a/src/dev/kona/back/display/screen.rs +++ b/src/dev/kona/back/display/screen.rs @@ -11,9 +11,9 @@ use crate::cpu::mips_core::CyclesPtr; use crate::dev::ng1::rex3::Renderer; use crate::disp::{Rex3Screen, StatusSource, FB_STRIDE}; -use crate::gfx_display::{DisplayStop, GfxDisplay}; +use crate::gfx_display::{DisplayStop, FrameRequests, GfxDisplay}; use parking_lot::Mutex; -use std::sync::atomic::{AtomicU32, AtomicU64, Ordering}; +use std::sync::atomic::{AtomicU32, Ordering}; use std::sync::Arc; @@ -27,13 +27,11 @@ struct Local { pub struct Display { renderer: Mutex>>, screen: Mutex, - /// Requests and what has been presented of them, as counts: a request is - /// pending until a frame composed after it has been presented, and one made - /// while a frame is composed is still pending after that frame. - screenshot_asked: AtomicU64, - screenshot_done: AtomicU64, - frame_asked: AtomicU64, - frame_done: AtomicU64, + /// Requests: one is pending until a frame composed after it has been + /// presented, and one made while a frame is composed is still pending after + /// that frame. + screenshot: FrameRequests, + frame: FrameRequests, screenshots: AtomicU32, display_stop: DisplayStop, local: Mutex>, @@ -55,10 +53,8 @@ impl Display { Display { renderer: Mutex::new(None), screen: Mutex::new(Rex3Screen::new()), - screenshot_asked: AtomicU64::new(0), - screenshot_done: AtomicU64::new(0), - frame_asked: AtomicU64::new(0), - frame_done: AtomicU64::new(0), + screenshot: FrameRequests::default(), + frame: FrameRequests::default(), screenshots: AtomicU32::new(0), display_stop: DisplayStop::default(), local: Mutex::new(None), @@ -87,7 +83,7 @@ impl Display { /// composing and hand it to `present`: a request made while the frame is /// composed is not in it, and stays pending after that frame is presented. pub fn note(&self) -> Due { - Due { screenshot: self.screenshot_asked.load(Ordering::Acquire), frame: self.frame_asked.load(Ordering::Acquire) } + Due { screenshot: self.screenshot.note(), frame: self.frame.note() } } /// Whether a screenshot or a frame has been asked for and not yet presented. @@ -113,7 +109,7 @@ impl Display { } screen.frame_seq = screen.frame_seq.wrapping_add(1); // A requested screenshot is saved as presented. - if due.screenshot != self.screenshot_done.load(Ordering::Acquire) { + if self.screenshot.answers(due.screenshot) { let pixels = screen.rgba.clone(); let n = self.screenshots.fetch_add(1, Ordering::Relaxed); let path = format!("screenshot_{:04}.png", n); @@ -140,8 +136,8 @@ impl Display { } // Mark only the requests this frame answers: one made while it was // composed stays pending for the next. - self.screenshot_done.store(due.screenshot, Ordering::Release); - self.frame_done.store(due.frame, Ordering::Release); + self.screenshot.answer(due.screenshot); + self.frame.answer(due.frame); } /// The display thread's last act: release the renderer, unless the stop came @@ -166,11 +162,11 @@ impl GfxDisplay for Display { } fn request_screenshot(&self) { - self.screenshot_asked.fetch_add(1, Ordering::AcqRel); + self.screenshot.request(); } fn screenshot_pending(&self) -> bool { - self.screenshot_asked.load(Ordering::Acquire) != self.screenshot_done.load(Ordering::Acquire) + self.screenshot.pending() } fn cycles(&self) -> CyclesPtr { @@ -183,11 +179,11 @@ impl GfxDisplay for Display { } fn request_frame(&self) { - self.frame_asked.fetch_add(1, Ordering::AcqRel); + self.frame.request(); } fn frame_pending(&self) -> bool { - self.frame_asked.load(Ordering::Acquire) != self.frame_done.load(Ordering::Acquire) + self.frame.pending() } } diff --git a/src/dev/ng1/rex3.rs b/src/dev/ng1/rex3.rs index 4cd32f152..4919121fe 100644 --- a/src/dev/ng1/rex3.rs +++ b/src/dev/ng1/rex3.rs @@ -1502,6 +1502,12 @@ pub struct Rex3 { #[cfg(feature = "rex-dirty-upload")] fb_damage: DamageTracker, pub screen: Arc>, + /// Test hook: while set the processor parks at `test_hold_point`, and + /// `test_held` says it has. + #[cfg(test)] + pub(super) test_hold: AtomicBool, + #[cfg(test)] + pub(super) test_held: AtomicBool, /// Drives the real REX3 `VV_INT_N` pin (vertical retrace / Kaleidoscope). pub vblank_cb: Mutex>, /// Drives the real REX3 `FIFO_INT_N` pin (GFIFO/BFIFO above/below @@ -1704,6 +1710,10 @@ impl Rex3 { #[cfg(feature = "rex-dirty-upload")] fb_damage: DamageTracker::new_full(), screen, + #[cfg(test)] + test_hold: AtomicBool::new(false), + #[cfg(test)] + test_held: AtomicBool::new(false), vblank_cb: Mutex::new(None), pf_wait: crate::PfLevel::new(), pf_fifo_full: crate::PfLevel::new(), @@ -3272,6 +3282,17 @@ impl Rex3 { self.flush_gpu_replay(FlushReason::WaitIdle); } + #[cfg(test)] + fn test_hold_point(&self) { + if self.test_hold.load(Ordering::SeqCst) { + self.test_held.store(true, Ordering::SeqCst); + while self.test_hold.load(Ordering::SeqCst) { + std::thread::yield_now(); + } + self.test_held.store(false, Ordering::SeqCst); + } + } + fn register_processor(&self) { // Publish our thread handle so gfifo_push can unpark us when we park. let _ = self.processor_unparker.set(thread::current()); @@ -3288,8 +3309,10 @@ impl Rex3 { continue; } - #[cfg(not(feature = "rex-dirty-upload"))] - self.fb_dirty.store(true, Ordering::Relaxed); + // Test hook: parks the processor with the entry peeked and + // nothing applied yet. + #[cfg(test)] + self.test_hold_point(); if !is_busy { self.gfxbusy.store(true, Ordering::Relaxed); @@ -3317,6 +3340,15 @@ impl Rex3 { } } + // Marked after the entry's effect, with release order, so a + // frame that takes the mark reads the pixels it follows. Marked + // before it, a frame could take the mark while a GO was still + // drawing and compose without it; nothing marks again until the + // next entry, and an idle frame does not compose, so the last + // draw of a burst stayed off the screen. + #[cfg(not(feature = "rex-dirty-upload"))] + self.fb_dirty.store(true, Ordering::Release); + // Advance head and release busy atomically — CPU thread sees all writes // (context registers + FB) only after both process_register and execute_go // have completed. @@ -3789,7 +3821,7 @@ impl Rex3 { #[cfg(feature = "rex-dirty-upload")] let fb_was_dirty = self.fb_damage.has_damage(); #[cfg(not(feature = "rex-dirty-upload"))] - let fb_was_dirty = self.fb_dirty.swap(false, Ordering::Acquire); + let fb_was_dirty = self.fb_dirty.swap(false, Ordering::AcqRel); let should_render = fb_was_dirty || palette_dirty || dbg_overlay diff --git a/src/dev/ng1/rex3_tests.rs b/src/dev/ng1/rex3_tests.rs index 6093f2354..b427d2d25 100644 --- a/src/dev/ng1/rex3_tests.rs +++ b/src/dev/ng1/rex3_tests.rs @@ -10847,3 +10847,42 @@ fn rexget_clears_the_sign_extension() { // Writing then reading a negative coordinate leaves its 23-bit form. assert_eq!((-0x80i32 as u32).rexset(9, 7).rexget(9, 7), 0x007F_FF80); } + +/// The display thread reads a frame while the processor holds a GO it has +/// peeked but not applied. The draw lands after that frame, so the mark must +/// be up afterwards: nothing else marks until the next command, and an idle +/// frame does not compose. +#[cfg(not(feature = "rex-dirty-upload"))] +#[test] +fn burst_ordering_the_last_draw_is_marked_after_the_frame_that_missed_it() { + let rex = &make_rex3_unstarted(); + start_rex3_processor_only(rex); + rex3init(rex); + reg(rex, REX3_DRAWMODE1, DM1_CI8_SRC); + reg(rex, REX3_WRMASK, 0xFF); + reg(rex, REX3_COLORI, 0x5a); + reg(rex, REX3_XYENDI, xy(63, 15)); + reg(rex, REX3_XYSTARTI, xy(0, 0)); + wait(rex); + let _ = rex.fb_dirty.swap(false, Ordering::AcqRel); + + let hash = |rex: &Rex3| { + let fb = unsafe { &*rex.fb_rgb.get() }; + fb.iter().take(2048 * 16).fold(0u64, |h, &p| h.wrapping_mul(1099511628211).wrapping_add(p as u64 + 1)) + }; + + rex.test_hold.store(true, Ordering::SeqCst); + w32(rex, go_addr(REX3_DRAWMODE0), DM0_DRAW_BLOCK); + while !rex.test_held.load(Ordering::SeqCst) { + std::thread::yield_now(); + } + // The display's frame: take the mark, then read the pixels. + let during = rex.fb_dirty.swap(false, Ordering::AcqRel); + let seen = hash(rex); + rex.test_hold.store(false, Ordering::SeqCst); + wait(rex); + let after = hash(rex); + let leftover = rex.fb_dirty.swap(false, Ordering::AcqRel); + assert_ne!(seen, after, "the held GO must draw after the frame read"); + assert!(leftover, "the draw landed after the frame (mark taken during the hold: {during}) and nothing marked it afterwards, so the next idle frame leaves it unseen"); +} diff --git a/src/dev/o2gfx.rs b/src/dev/o2gfx.rs index 7024b551f..eebde88a7 100644 --- a/src/dev/o2gfx.rs +++ b/src/dev/o2gfx.rs @@ -126,6 +126,10 @@ pub struct Gbe { irq: std::sync::OnceLock<[crate::irq::IrqLine; 4]>, /// The annotated trace, shared with CRIME and its rendering engine. pub tr: Arc, + /// Marked when anything scan-out reads may have changed: a register write, + /// a shadow copied live at vertical blanking, restored state. The machine + /// shares it with memory, whose marks the rendering engine's drawing makes. + pub changed: Arc, } impl Gbe { @@ -157,7 +161,7 @@ impl Gbe { // GPIO pins unconfigured (inputs), chip id 1. r[(CTRLSTAT / 4) as usize] = 0x03ff_ffc1; r[(VT_XY / 4) as usize] = VT_FREEZE; - Self { s: Mutex::new(GbeState { r, t0: 0, reads: 0, seen_dots: 0, pending: Vec::new() }), hot: Mutex::new(BTreeMap::new()), now, cpu_clock: std::sync::OnceLock::new(), probe: Mutex::new((None, String::new())), irq: std::sync::OnceLock::new(), tr: Arc::new(O2Trace::default()) } + Self { s: Mutex::new(GbeState { r, t0: 0, reads: 0, seen_dots: 0, pending: Vec::new() }), hot: Mutex::new(BTreeMap::new()), now, cpu_clock: std::sync::OnceLock::new(), probe: Mutex::new((None, String::new())), irq: std::sync::OnceLock::new(), tr: Arc::new(O2Trace::default()), changed: Arc::new(crate::gfx_display::ScreenChanged::marked()) } } /// Run the beam on the CPU's instruction count at `mhz` rather than on @@ -240,7 +244,10 @@ impl Gbe { if self.tr.on(trc::TRACE_GBE) { self.tr.event("GBE", format_args!("latched {} = {val:#010x}", trc::gbe_reg_name(live))); } - s.r[(live / 4) as usize] = val; + let old = std::mem::replace(&mut s.r[(live / 4) as usize], val); + if old != val { + self.changed.mark(); + } } else { s.pending.push((live, val, at)); } @@ -333,7 +340,7 @@ impl Gbe { s.reads = 0; s.seen_dots = 0; } - s.r[i] = val; + let old = std::mem::replace(&mut s.r[i], val); // The hardware copies each DMA control's shadow into the live // register at the start of vertical blanking, so until then the live // one still reads what it held. IRIX depends on that: its mode set @@ -355,9 +362,33 @@ impl Gbe { s.pending.retain(|p| p.0 != l); s.pending.push((l, val, f)); } - None => s.r[(l / 4) as usize] = val, + None => { + // No blanking to wait for: live at once, and a live + // register that changes shows even if the shadow held + // this value already. + if std::mem::replace(&mut s.r[(l / 4) as usize], val) != val { + self.changed.mark(); + } + } } } + // After the register is in: a frame that takes the mark reads it. A + // register written with the value it holds changes nothing, and IRIX + // does exactly that every frame: it rewrites the overlay and frame + // control shadows and the vertical interrupt line in the retrace. + if old != val { + self.changed.mark(); + } + } + + /// Whether a shadow register waits for vertical blanking with a value + /// that differs from the live one. The copy to live happens when something + /// next looks, and marks then, but a display that waits for a mark never + /// looks: it asks. A shadow rewritten with the live value waits for + /// nothing (IRIX rewrites them every frame). + pub fn latch_pending(&self) -> bool { + let s = self.s.lock().unwrap(); + s.pending.iter().any(|&(live, val, _)| Self::get(&s.r, live) != val) } /// The registers that decide what is on screen, for a report. @@ -373,7 +404,7 @@ impl Gbe { } /// Render from `ram`: copy the frame's pages, then scan them. - pub fn render_from(&self, ram: &O2Memory, out: &mut [u8]) -> Option<(u32, u32)> { + pub fn render_from(&self, ram: &O2Memory, out: &mut [u32]) -> Option<(u32, u32)> { let snap = { let m = &*ram; self.snapshot(&m) @@ -471,10 +502,12 @@ impl Gbe { Some((x, y, mask)) } - /// Scan the frame out of RAM into `out` (R, G, B, A bytes, row 0 at the - /// top), as GBE would. Returns the size drawn, or None when the display - /// is off. `out` must hold the largest mode (2048 x 2048 pixels). - pub fn render>(&self, mem: &M, out: &mut [u8]) -> Option<(u32, u32)> { + /// Scan the frame out of RAM into `out`, as GBE would: one `0xAABBGGRR` + /// word a pixel with the alpha set, the format the renderers take, row 0 + /// at the top and `w` pixels a row. Returns the size drawn, or None when + /// the display is off. `out` must hold the largest mode (2048 x 2048 + /// pixels). + pub fn render>(&self, mem: &M, out: &mut [u32]) -> Option<(u32, u32)> { let r = { let mut s = self.s.lock().unwrap(); self.latch(&mut s); @@ -489,7 +522,7 @@ impl Gbe { || w == 0 || h == 0 || g(VT_FLAGS) & 0xa == 0xa - || (w as usize * h as usize * 4) > out.len() + || (w as usize * h as usize) > out.len() { return None; } @@ -682,11 +715,7 @@ impl Gbe { } } } - let o = (y * w + x) * 4; - out[o] = rr as u8; - out[o + 1] = gg as u8; - out[o + 2] = bb as u8; - out[o + 3] = 255; + out[y * w + x] = 0xff00_0000 | (bb & 0xff) << 16 | (gg & 0xff) << 8 | (rr & 0xff); } } Some((w as u32, h as u32)) @@ -3043,6 +3072,7 @@ impl Saveable for Gbe { s.reads = u("reads"); s.seen_dots = u("seen_dots"); s.pending.clear(); + self.changed.mark(); if let Some(toml::Value::Array(a)) = get_field(v, "pending") { for e in a { if let Some([off, val, frame]) = e.as_array().map(|x| x.as_slice()) { @@ -4230,6 +4260,179 @@ mod tests { assert_eq!(g.read32((base + OVR_CONTROL) as u64).data, 0x0035_b9c1, "the new value after it"); } + /// The display composes only for a mark. A register write, a shadow going + /// live at blanking and restored state mark it through GBE; a primitive + /// the engine drew marks it through memory, once, after its pixels. + #[test] + fn a_gbe_register_write_marks_the_screen_and_a_read_does_not() { + let g = gbe(); + g.changed.take(); + g.read32(0x1600_0000 + CTRLSTAT as u64); + assert!(!g.changed.take(), "a read changes nothing"); + g.write32((0x1600_0000 + CMAP + 4 * 5) as u64, 0x11223300); + assert!(g.changed.take()); + g.write32((0x1600_0000 + CRS_POS) as u64, 5); + assert!(g.changed.take(), "the cursor moved"); + } + + #[test] + fn a_shadow_register_going_live_at_blanking_marks_the_screen() { + let t = Arc::new(std::sync::atomic::AtomicU64::new(0)); + let tc = t.clone(); + let g = Gbe::new(Arc::new(move || tc.load(std::sync::atomic::Ordering::Relaxed))); + let base = 0x1600_0000; + g.write32((base + DOTCLOCK) as u64, 0x0010_020f); + g.write32((base + VT_XYMAX) as u64, 0x0042_9690); + g.write32((base + VT_XY) as u64, 0); + g.write32((base + OVR_CONTROL_SHADOW) as u64, 0x0035_b9c1); + assert!(g.changed.take(), "the write marks"); + assert!(g.latch_pending(), "and the copy to live is still to come"); + // A look before blanking applies nothing, so marks nothing. + g.read32((base + OVR_CONTROL) as u64); + assert!(!g.changed.take()); + t.store(40_000, std::sync::atomic::Ordering::Relaxed); + g.read32((base + OVR_CONTROL) as u64); + assert!(g.changed.take(), "the copy at blanking changes what scan-out reads"); + assert!(!g.latch_pending()); + } + + /// IRIX's retrace handler writes the same shadow controls and interrupt + /// line every frame. Rewriting a value changes nothing, so it marks nothing + /// and keeps nothing waiting. + #[test] + fn rewriting_a_register_with_its_value_marks_nothing() { + let t = Arc::new(std::sync::atomic::AtomicU64::new(0)); + let tc = t.clone(); + let g = Gbe::new(Arc::new(move || tc.load(std::sync::atomic::Ordering::Relaxed))); + let base = 0x1600_0000; + g.write32((base + DOTCLOCK) as u64, 0x0010_020f); + g.write32((base + VT_XYMAX) as u64, 0x0042_9690); + g.write32((base + VT_XY) as u64, 0); + g.write32((base + OVR_CONTROL_SHADOW) as u64, 0x0035_b9c1); + g.write32((base + VT_INTR01) as u64, 0x4043d8); + t.store(40_000, std::sync::atomic::Ordering::Relaxed); + g.read32((base + OVR_CONTROL) as u64); + assert!(g.changed.take()); + // The next frame's handler writes them again. + t.store(80_000, std::sync::atomic::Ordering::Relaxed); + g.write32((base + OVR_CONTROL_SHADOW) as u64, 0x0035_b9c1); + g.write32((base + VT_INTR01) as u64, 0x4043d8); + assert!(!g.latch_pending(), "the live register already holds it"); + t.store(120_000, std::sync::atomic::Ordering::Relaxed); + g.read32((base + OVR_CONTROL) as u64); + assert!(!g.changed.take(), "nothing changed"); + // A different value does. + g.write32((base + OVR_CONTROL_SHADOW) as u64, 0x0035_b9c0); + assert!(g.changed.take() && g.latch_pending()); + } + + #[test] + fn restored_gbe_state_marks_the_screen() { + let src = gbe(); + src.write32(DOTCLOCK as u64, DOTCLOCK_RUN | 0x0123); + let dst = gbe(); + dst.changed.take(); + dst.load_state(&src.save_state()).unwrap(); + assert!(dst.changed.take()); + } + + #[test] + fn a_primitive_the_engine_draws_marks_the_screen_once_and_register_writes_do_not() { + let ram = Arc::new(O2Memory::single(32 << 20)); + let re = TestRe::new(ram.clone()); + let tile = 0x0010_0000u32; + re.write64(0x1500_0000 + TLB_A, (0x8000u64 | (tile >> 16) as u64) << 48); + re.write32(0x1500_0000 + BUFMODE_DST, 0); + re.write32(0x1500_0000 + DRAWMODE, 0xf << 3); + re.write32(0x1500_0000 + PRIMITIVE, 3 << 24); + re.write32(0x1500_0000 + FG, 0x0505_0505); + re.write32(0x1500_0000 + VERTEX_0, (10 << 16) | 20); + assert!(!ram.screen_changed().take(), "registers alone draw nothing"); + re.write32(0x1500_0000 + (VERTEX_1 | 0x800), (19 << 16) | 29); + assert!(ram.screen_changed().take(), "the rectangle wrote pixels"); + assert!(!ram.screen_changed().take(), "and marked once"); + } + + #[test] + fn restored_memory_marks_the_screen() { + let ram = O2Memory::single(32 << 20); + let words: Vec = ram.bank_words().concat(); + ram.screen_changed().take(); + ram.restore_words(&words).unwrap(); + assert!(ram.screen_changed().take()); + } + + + /// FNV-1a over the frame's bytes, as the frame buffer lays them out. + fn fnv(bytes: impl Iterator) -> u64 { + bytes.fold(0xcbf2_9ce4_8422_2325u64, |h, b| (h ^ b as u64).wrapping_mul(0x100_0000_01b3)) + } + + /// A scene with every stage of scan-out in it (a 32-bit frame buffer through + /// the gamma map, a random colour map, an overlay and the cursor), hashed. The + /// hash was recorded before the decode wrote its final format, so a change + /// to the picture's pixels, not just their layout, fails here. + #[test] + fn a_scanned_out_scene_is_the_frame_recorded_before_the_format_change() { + let ram = Arc::new(O2Memory::single(32 << 20)); + let g = gbe(); + let mut x = 0x2545_f491_4f6c_dd1du64; + let mut next = move || { + x ^= x << 13; + x ^= x >> 7; + x ^= x << 17; + (x >> 16) as u32 + }; + // Four 64 KB tiles of 32-bit pixels, one tile row of a 512 x 128 screen. + let (tiles, list, ovr_tile, ovr_list) = (0x0010_0000u32, 0x0020_0000u32, 0x0030_0000u32, 0x0031_0000u32); + for t in 0..4u32 { + ram.write((list + 2 * t) as usize, &(((tiles >> 16) + t) as u16).to_be_bytes()); + let words: Vec = (0..0x4000).flat_map(|_| next().to_be_bytes()).collect(); + ram.write((tiles + t * 0x10000) as usize, &words); + } + ram.write(ovr_list as usize, &((ovr_tile >> 16) as u16).to_be_bytes()); + let bytes: Vec = (0..0x10000).map(|_| if next() % 3 == 0 { next() as u8 & 0x1f } else { 0 }).collect(); + ram.write(ovr_tile as usize, &bytes); + for (off, v) in [ + (CTRLSTAT, 0x300a_a000u32), + (DOTCLOCK, 0x0010_020f), + (VT_XYMAX, 0x0042_9690), + (FRM_CONTROL_SHADOW, list | 1), + (OVR_CONTROL_SHADOW, ovr_list | 1), + (VT_XY, 0), + (VT_HCMAP, 512 << 12), + (VT_VCMAP, 128 << 12), + (FRM_SIZE_TILE, (4 << 5) | (2 << 13)), + (OVR_WIDTH_TILE, 1 << 5), + (FRM_SIZE_PIXEL, 128 << 16), + // RGB with the gamma map on, then a window type that goes through the colour map. + (MODE, 5 << 2), + (MODE + 4, (5 << 2) | (3 << 5)), + (CRS_CTL, 1), + (CRS_POS, (50 << 16) | 60), + (CRS_CMAP1, 0xff00_0000), + (CRS_CMAP1 + 4, 0x00ff_0000), + (CRS_CMAP1 + 8, 0x0000_ff00), + ] { + g.write32((0x1600_0000 + off) as u64, v); + } + for i in 0..256 { + g.write32((0x1600_0000 + GMAP + 4 * i) as u64, i * 0x0101_0100); + } + for i in 0..6144 { + g.write32((0x1600_0000 + CMAP + 4 * i) as u64, next() & 0xffff_ff00); + } + for i in 0..64 { + g.write32((0x1600_0000 + CRS_GLYPH + 4 * i) as u64, next()); + } + let mut out = vec![0u32; 2048 * 2048]; + let (w, h) = g.render_from(&ram, &mut out).expect("display on"); + assert_eq!((w, h), (512, 128)); + // The words as the frame buffer holds them: little-endian bytes R, G, B, 0xff. + let hash = fnv(out[..(w * h) as usize].iter().flat_map(|p| p.to_le_bytes())); + assert_eq!(hash, 0x4f08_f88f_4b6b_0bc3, "the scene's pixels"); + } + /// Fill a rectangle through TLB A, then scan it out through a tile list. #[test] fn a_filled_rectangle_comes_out_of_scanout_in_its_cmap_colour() { @@ -4272,12 +4475,12 @@ mod tests { ] { g.write32((0x1600_0000 + off) as u64, v); } - let mut out = vec![0u8; 2048 * 2048 * 4]; + let mut out = vec![0u32; 2048 * 2048]; let (w, h) = g.render_from(&ram, &mut out).expect("display on"); assert_eq!((w, h), (512, 128)); - let px = |x: usize, y: usize| &out[(y * 512 + x) * 4..(y * 512 + x) * 4 + 3]; - assert_eq!(px(10, 20), [0x11, 0x22, 0x33]); - assert_eq!(px(9, 20), [0, 0, 0]); + let px = |x: usize, y: usize| out[y * 512 + x]; + assert_eq!(px(10, 20), 0xff33_2211, "opaque, red in the low byte"); + assert_eq!(px(9, 20), 0xff00_0000); } /// IRIX draws menu text into the 8-bit overlay as stippled lines with @@ -4338,10 +4541,10 @@ mod tests { ] { g.write32((0x1600_0000 + off) as u64, v); } - let mut out = vec![0u8; 2048 * 2048 * 4]; + let mut out = vec![0u32; 2048 * 2048]; g.render_from(&ram, &mut out).expect("display on"); - let px = |x: usize, y: usize| &out[(y * 512 + x) * 4..(y * 512 + x) * 4 + 3]; - assert_eq!(px(3, 4), [0xaa, 0xbb, 0xcc], "overlay colour from entry 0x1100 + 7"); - assert_eq!(px(4, 4), [0x11, 0x22, 0x33], "overlay 0 shows the normal planes"); + let px = |x: usize, y: usize| out[y * 512 + x]; + assert_eq!(px(3, 4), 0xffcc_bbaa, "overlay colour from entry 0x1100 + 7"); + assert_eq!(px(4, 4), 0xff33_2211, "overlay 0 shows the normal planes"); } } diff --git a/src/dev/odyssey/display.rs b/src/dev/odyssey/display.rs index eb1565d2a..958141544 100644 --- a/src/dev/odyssey/display.rs +++ b/src/dev/odyssey/display.rs @@ -47,6 +47,8 @@ pub(super) struct Display { /// Consumers (VNC) that read `screen.rgba` every frame /// (`GfxDisplay::set_capture`): while any does, frames are read back. capture: AtomicU32, + /// Frames the display thread composed and skipped, for `screen frames`. + pub(super) counts: crate::gfx_display::FrameCounts, } struct Local { @@ -72,6 +74,33 @@ impl Display { status, shared: OnceLock::new(), capture: AtomicU32::new(0), + counts: Default::default(), + } + } + + /// Whether anything shows or reads the frames: a renderer (the window), a + /// screenshot or frame request, or a client capturing every frame. With + /// none (`--ci` with no window, no screenshot asked for) a frame composed + /// is a frame nobody sees, and with the GPU raster unit it costs a + /// scan-out, a readback and a pass over every pixel. A request composes + /// the next frame whole, since the board's memory marks and register + /// generations are not consumed while frames are skipped. + pub(super) fn consumed(&self) -> bool { + self.screenshot.load(Ordering::Acquire) + || self.frame.load(Ordering::Acquire) + || self.capture.load(Ordering::Acquire) != 0 + || self.renderer.lock().is_some() + } + + /// The screen's size, set without composing a frame: what `screen info` + /// and pointer steering read before anything asks for a picture. + pub(super) fn publish_size(&self, width: usize, height: usize) { + let mut shown = self.shown.lock(); + if *shown != (width, height) && width != 0 && height != 0 { + let mut screen = self.screen.lock(); + screen.width = width.min(FB_STRIDE); + screen.height = height; + *shown = (width, height); } } @@ -356,6 +385,9 @@ impl crate::gfx_display::GfxDisplay for Display { fn screenshot_pending(&self) -> bool { self.screenshot.load(Ordering::Acquire) } + fn frame_counts(&self) -> Option<&crate::gfx_display::FrameCounts> { + Some(&self.counts) + } fn cycles(&self) -> CyclesPtr { self.status.cycles() } diff --git a/src/dev/odyssey/display_bench.rs b/src/dev/odyssey/display_bench.rs index a4f2b679b..c6d6a5c3e 100644 --- a/src/dev/odyssey/display_bench.rs +++ b/src/dev/odyssey/display_bench.rs @@ -238,6 +238,8 @@ fn compose_and_present_timings() { } let locks = BoardLocks::new(scene_board(Scene::Mixed)); let display = Display::new(crate::disp::StatusSource::detached()); + // A client that reads every frame, or the frames are not composed. + crate::gfx_display::GfxDisplay::set_capture(&display, true); let ms = bench(|| Odyssey::frame(&locks, &display)); println!("frame: composed and presented in {ms:.3} ms"); } diff --git a/src/dev/odyssey/mod.rs b/src/dev/odyssey/mod.rs index 06b35c71c..12fea4660 100644 --- a/src/dev/odyssey/mod.rs +++ b/src/dev/odyssey/mod.rs @@ -278,6 +278,14 @@ impl Odyssey { let mut reader = display.reader.lock(); reader.attach(&state.shared); reader.poll(monotonic_ns()); + if !display.consumed() { + // Nothing shows or reads a frame. The retrace above still runs, + // as the guest waits on it; the picture is not composed. + display.publish_size(reader.frame_width as usize, reader.frame_height as usize); + display.counts.count(false); + crate::pf_end!(crate::perfetto::events::ODSY_CAPTURE); + return; + } // On the GPU raster unit with the wgpu renderer on the same device, // the frame is composed and presented there with no readback. #[cfg(feature = "wgpu")] @@ -286,6 +294,7 @@ impl Odyssey { let composed = reader.update_on_gpu(); crate::pf_end!(crate::perfetto::events::ODSY_FRAME_PLANES); if let Some((buffer, w, h)) = composed { + display.counts.count(true); set_display_fifo_pending(state.fifo.level() as usize + state.raster.pending() as usize); crate::pf_end!(crate::perfetto::events::ODSY_CAPTURE); drop(reader); @@ -307,7 +316,9 @@ impl Odyssey { let (w, h) = (reader.frame_width as usize, reader.frame_height as usize); set_display_fifo_pending(state.fifo.level() as usize + state.raster.pending() as usize); crate::pf_end!(crate::perfetto::events::ODSY_CAPTURE); - if w == 0 || h == 0 || !display.due(!matches!(composed, reader::Composed::No)) { + let changed = !matches!(composed, reader::Composed::No); + display.counts.count(changed); + if w == 0 || h == 0 || !display.due(changed) { return; } crate::pf_begin!(crate::perfetto::events::ODSY_PRESENT); diff --git a/src/dev/odyssey/tests/raster.rs b/src/dev/odyssey/tests/raster.rs index 4c8247ec4..6e1a54998 100644 --- a/src/dev/odyssey/tests/raster.rs +++ b/src/dev/odyssey/tests/raster.rs @@ -726,6 +726,9 @@ fn display_rig() -> (BoardLocks, LibState, Arc) { let h = BoardLocks::new(s); h.raster.start(); let display = Arc::new(Display::new(crate::disp::StatusSource::detached())); + // A client that reads every frame, as VNC does: with nobody reading, the + // display composes nothing. + crate::gfx_display::GfxDisplay::set_capture(&*display, true); (h, st, display) } @@ -984,6 +987,7 @@ fn a_frame_waits_for_no_slice_and_takes_no_lock() { } let (h, st) = slow_raster(0); let display = Arc::new(Display::new(crate::disp::StatusSource::detached())); + crate::gfx_display::GfxDisplay::set_capture(&*display, true); let hold = Arc::new(AtomicBool::new(true)); let held = hold.clone(); h.raster.submit(move |_| { @@ -1071,3 +1075,93 @@ fn an_idle_display_presents_only_its_refresh_frames() { assert_eq!(seq(&display), before + 1, "a store shows on the next frame"); h.raster.stop(); } + +/// What the display does when nothing shows or reads its frames, as in `--ci` +/// with no window: no compose and no readback, the retrace still runs and the +/// screen's size is known, and a request, a capturing client or a renderer +/// each bring frames back. +mod unwatched { + use super::*; + use crate::gfx_display::GfxDisplay; + + /// A rig whose display nobody reads. + fn rig() -> (BoardLocks, Arc) { + let (h, _st, display) = display_rig(); + display.set_capture(false); + (h, display) + } + + fn counts(d: &Display) -> (u64, u64, u64) { + d.frame_counts().unwrap().get() + } + + #[test] + fn a_display_nobody_reads_composes_nothing_but_knows_its_size() { + let (h, display) = rig(); + for _ in 0..12 { + Odyssey::frame(&h, &display); + } + assert_eq!(counts(&display), (12, 0, 12), "twelve frames, none composed"); + let screen = display.screen.lock(); + assert_eq!((screen.width, screen.height), (ODSY_WIDTH as usize, ODSY_HEIGHT as usize), "the size is published"); + assert_eq!(screen.frame_seq, 0, "no frame was presented"); + h.raster.stop(); + } + + #[test] + fn the_retrace_still_runs_while_nobody_reads() { + let (h, display) = rig(); + let retraces = |d: &Display| d.reader.lock().clock.frames; + Odyssey::frame(&h, &display); + let before = retraces(&display); + std::thread::sleep(Duration::from_millis(60)); + Odyssey::frame(&h, &display); + assert!(retraces(&display) > before, "the guest's retrace interrupts do not wait for a reader"); + h.raster.stop(); + } + + /// A request composes the next frame whole, including what was drawn while + /// frames were being skipped, and the request is answered after the pixels + /// are in `rgba`. + #[test] + fn a_frame_request_composes_the_next_frame_from_what_was_drawn_meanwhile() { + let (h, display) = rig(); + Odyssey::frame(&h, &display); + h.raster.wait_idle(); + h.test_board().vram.ptr().store(0x1000, 4, 0x55); + Odyssey::frame(&h, &display); + assert_eq!(counts(&display).1, 0); + display.request_frame(); + assert!(display.frame_pending()); + Odyssey::frame(&h, &display); + assert!(!display.frame_pending(), "answered by the next frame"); + assert_eq!(counts(&display).1, 1); + assert_frame_is_whole_resolve(&h, &display, "the requested frame"); + { + let screen = display.screen.lock(); + assert_eq!(screen.frame_seq, 1); + assert_eq!(&screen.rgba[..64], &screen.fb_rgb[..64], "rgba holds the frame the request got"); + } + // And frames are skipped again. + Odyssey::frame(&h, &display); + assert_eq!(counts(&display).1, 1); + h.raster.stop(); + } + + #[test] + fn a_capturing_client_or_a_renderer_brings_frames_back_and_leaving_takes_them_away() { + let (h, display) = rig(); + display.set_capture(true); + Odyssey::frame(&h, &display); + assert_eq!(counts(&display).1, 1, "a capturing client gets a frame"); + display.set_capture(false); + Odyssey::frame(&h, &display); + assert_eq!(counts(&display).1, 1, "and none once it leaves"); + let probe = crate::display_stop_tests::Probe::new(|| {}); + let presents = probe.presents.clone(); + *display.renderer_slot().lock() = Some(Box::new(probe)); + Odyssey::frame(&h, &display); + assert!(presents.load(SeqCst) >= 1, "a renderer is given the frame"); + h.raster.stop(); + } +} diff --git a/src/display_stop_tests.rs b/src/display_stop_tests.rs index caef24f18..48fb24935 100644 --- a/src/display_stop_tests.rs +++ b/src/display_stop_tests.rs @@ -22,7 +22,7 @@ const BOUND: Duration = Duration::from_secs(8); /// A renderer that counts presents and runs `on_stop` when torn down. #[derive(Clone)] pub(crate) struct Probe { - presents: Arc, + pub(crate) presents: Arc, pub(crate) stopped: Arc, on_stop: Arc, } diff --git a/src/gfx_display.rs b/src/gfx_display.rs index 48490a099..fba3a8038 100644 --- a/src/gfx_display.rs +++ b/src/gfx_display.rs @@ -7,7 +7,8 @@ //! last-presented screen (screenshots, VNC), the hardware cursor, and the CPU //! cycle counter. -use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::atomic::{fence, AtomicBool, AtomicU64, Ordering}; +use std::time::{Duration, Instant}; use parking_lot::Mutex; @@ -36,6 +37,10 @@ pub trait GfxDisplay: Send + Sync { /// nothing to wait for. fn screenshot_pending(&self) -> bool { false } + /// Frames the display thread composed and skipped, for `screen frames`, + /// on a board that counts them. + fn frame_counts(&self) -> Option<&FrameCounts> { None } + /// CPU cycle counter the board was wired to (GUI's live MIPS estimate). fn cycles(&self) -> CyclesPtr; @@ -69,6 +74,282 @@ pub trait GfxDisplay: Send + Sync { fn gpu(&self) -> Option> { None } } +/// Set when anything the scan-out reads may have changed; taken by the +/// display before it reads anything. One store per processed command, job or +/// display-register write, never one per pixel or per memory store. +/// +/// The writer marks after its effect is visible and the display takes the flag +/// before it reads, so a frame composed for a mark sees what the mark follows. +/// Every mark stores, even with the flag already up: a frame that takes it +/// between a writer's effect and its mark would otherwise lose the effect. The +/// flag has a cache line to itself, away from the state the writers touch. +/// +/// A display thread that has nothing to do until something changes can park on +/// it ([`FrameWait::park_until_marked`]); a mark then wakes it. +#[repr(align(64))] +#[derive(Default)] +pub struct ScreenChanged { + flag: AtomicBool, + /// The display thread is parked, or on its way to be. + parked: AtomicBool, + /// Someone asked the thread to stop waiting ([`wake`](Self::wake)). + wake: AtomicBool, + /// The display thread, set by [`FrameWait::new`]. + thread: Mutex>, +} + +impl ScreenChanged { + /// A flag that starts up, for a display whose first frame must be composed. + pub fn marked() -> Self { + let c = Self::default(); + c.flag.store(true, Ordering::Relaxed); + c + } + + /// One release store, and a relaxed load of whether the display thread + /// sleeps, which a thread that is not parked leaves false: no call, lock or + /// read-modify-write on the writer's path. + #[inline(always)] + pub fn mark(&self) { + self.flag.store(true, Ordering::Release); + if self.parked.load(Ordering::Relaxed) { + self.unpark(); + } + } + + /// Whether a mark is waiting, leaving it up. + #[inline] + pub fn is_marked(&self) -> bool { + self.flag.load(Ordering::Acquire) + } + + /// Whether a mark came since the last call, clearing it. The display calls + /// it before it reads anything the mark covers. + #[inline] + pub fn take(&self) -> bool { + self.flag.swap(false, Ordering::AcqRel) + } + + /// End the display thread's current or next wait at once: a screenshot + /// request, or a stop. + pub fn wake(&self) { + self.wake.store(true, Ordering::Release); + self.parked.store(false, Ordering::Release); + if let Some(t) = self.thread.lock().as_ref() { + t.unpark(); + } + } + + fn unpark(&self) { + // The swap makes one writer the waker. + if self.parked.swap(false, Ordering::AcqRel) { + if let Some(t) = self.thread.lock().as_ref() { + t.unpark(); + } + } + } +} + +/// What the display thread did, counted by that thread alone with relaxed +/// stores and read by the monitor's `screen frames`. +#[derive(Default)] +pub struct FrameCounts { + frames: AtomicU64, + composed: AtomicU64, + skipped: AtomicU64, +} + +impl FrameCounts { + /// One frame, composed or skipped. + pub fn count(&self, composed: bool) { + // One writer, so a load and a store do. + let add = |a: &AtomicU64| a.store(a.load(Ordering::Relaxed).wrapping_add(1), Ordering::Relaxed); + add(&self.frames); + add(if composed { &self.composed } else { &self.skipped }); + } + + /// Frames, composed, skipped. + pub fn get(&self) -> (u64, u64, u64) { + let g = |a: &AtomicU64| a.load(Ordering::Relaxed); + (g(&self.frames), g(&self.composed), g(&self.skipped)) + } + + pub fn reset(&self) { + for a in [&self.frames, &self.composed, &self.skipped] { + a.store(0, Ordering::Relaxed); + } + } +} + +/// Requests for a fresh frame, counted so that one made while a frame is being +/// composed is still pending after it: asked and answered are counts, not a +/// flag. A screenshot, `request_frame` and a capturing client (VNC) are each +/// one of these on a board. +/// +/// The display thread takes [`note`](Self::note) before it composes and calls +/// [`answer`](Self::answer) with it once the frame is in the screen; a caller +/// waits on [`pending`](Self::pending). A board that parks its display thread +/// wakes it from the request ([`ScreenChanged::wake`]). +#[derive(Default)] +pub struct FrameRequests { + asked: AtomicU64, + done: AtomicU64, + capture: std::sync::atomic::AtomicU32, +} + +impl FrameRequests { + pub fn request(&self) { + self.asked.fetch_add(1, Ordering::AcqRel); + } + + /// Whether a request is waiting for its frame. + pub fn pending(&self) -> bool { + self.asked.load(Ordering::Acquire) != self.done.load(Ordering::Acquire) + } + + /// The requests a frame composed from now on will answer. Take it before + /// composing. + pub fn note(&self) -> u64 { + self.asked.load(Ordering::Acquire) + } + + /// Whether the frame that `noted` was taken for answers a request. + pub fn answers(&self, noted: u64) -> bool { + noted != self.done.load(Ordering::Acquire) + } + + /// The frame is in the screen: everything up to `noted` is answered. + pub fn answer(&self, noted: u64) { + self.done.store(noted, Ordering::Release); + } + + /// One more consumer wants every frame (`on`), or one fewer. Calls nest. + pub fn capture(&self, on: bool) { + if on { + self.capture.fetch_add(1, Ordering::AcqRel); + } else { + let _ = self.capture.fetch_update(Ordering::AcqRel, Ordering::Acquire, |n| n.checked_sub(1)); + } + } + + pub fn capturing(&self) -> bool { + self.capture.load(Ordering::Acquire) != 0 + } +} + +/// The frame period of a 60 Hz mode. +pub const FRAME_PERIOD: Duration = Duration::from_micros(16_667); + +/// Frames between presents of an unchanged screen, about ten a second at +/// 60 Hz: the status bar moves, and a window that missed a frame catches up. +pub const IDLE_REFRESH: u32 = 6; + +/// The longest a parked display thread goes without looking for a mark. A +/// writer that marks while the thread is on its way to sleep may not see that +/// it sleeps, since the mark is a store with no barrier behind it. The thread's +/// own look finds the flag, so the race costs this much latency, not a frame. +const PARK_RECHECK: Duration = Duration::from_millis(100); + +/// Why [`FrameWait::park_until_marked`] returned. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub enum Woke { + /// The display was marked; the mark is still up for the frame to take. + Marked, + /// [`ScreenChanged::wake`] asked for the thread's attention. + Requested, + /// The board's timer came due. + Timer, +} + +/// The display thread's wait between frames, for a loop that already has its +/// frames. Frames are paced from a fixed start, so a slow one does not stretch +/// the rate, and a stall resyncs to now rather than running the missed frames +/// back to back. Call it last in the loop, after the frame. +pub struct FrameWait<'a> { + changed: &'a ScreenChanged, + period: Duration, + /// Where the frame now running started. + next: Instant, +} + +impl<'a> FrameWait<'a> { + /// For the calling thread, which `changed` records to unpark. + pub fn new(changed: &'a ScreenChanged, period: Duration) -> Self { + *changed.thread.lock() = Some(std::thread::current()); + Self { changed, period, next: Instant::now() } + } + + /// Wait out the frame's period; ends early if [`ScreenChanged::wake`] asks. + pub fn wait(&mut self) { + let mut now = Instant::now(); + let end = self.next + self.period; + if now >= end { + self.next = now; + return; + } + loop { + if self.changed.wake.swap(false, Ordering::AcqRel) { + self.next = now; + return; + } + std::thread::park_timeout(end - now); + now = Instant::now(); + if now >= end { + self.next = end; + return; + } + } + } + + /// With nothing marked, park until the display is marked, + /// [`ScreenChanged::wake`] is called, or `timer` comes due, instead of + /// waking every period to find nothing. A board whose retrace or other + /// hardware duty has a time of its own passes it as `timer`. + /// + /// A mark ends the wait at the next vsync, not at once, so marks between + /// two vsyncs make one frame; after a long park that vsync is long past and + /// the frame starts at once. + pub fn park_until_marked(&mut self, timer: Option) -> Woke { + let c = self.changed; + let end = self.next + self.period; + let woke = loop { + if c.is_marked() { + break Woke::Marked; + } + if c.wake.swap(false, Ordering::AcqRel) { + break Woke::Requested; + } + let now = Instant::now(); + let wait = match timer { + Some(t) if t <= now => break Woke::Timer, + Some(t) => (t - now).min(PARK_RECHECK), + None => PARK_RECHECK, + }; + // Say so, then look again: a writer that marked before it could + // see this is found by the look. + c.parked.store(true, Ordering::Relaxed); + fence(Ordering::SeqCst); + if !c.is_marked() && !c.wake.load(Ordering::Acquire) { + std::thread::park_timeout(wait); + } + c.parked.store(false, Ordering::Relaxed); + }; + let now = Instant::now(); + if woke == Woke::Timer { + // The board's own time: leave the slot as it is unless it ran out. + if now >= end { + self.next = now; + } + } else if end > now { + std::thread::sleep(end - now); + self.next = end; + } else { + self.next = now; + } + woke + } +} + /// Whether the display thread tears its renderer down when it ends. /// /// Releasing a renderer's GL context has to happen on the thread that made @@ -189,6 +470,7 @@ pub(crate) mod testing { stop: DisplayStop, renderer: Mutex>>, screen: Mutex, + pub(crate) counts: FrameCounts, stuck: bool, } @@ -197,7 +479,7 @@ pub(crate) mod testing { pub(crate) fn new(w: usize, h: usize, stuck: bool) -> Self { let mut screen = Rex3Screen::new(); (screen.width, screen.height) = (w, h); - Self { stop: DisplayStop::default(), renderer: Mutex::new(None), screen: Mutex::new(screen), stuck } + Self { stop: DisplayStop::default(), renderer: Mutex::new(None), screen: Mutex::new(screen), counts: FrameCounts::default(), stuck } } } @@ -208,6 +490,7 @@ pub(crate) mod testing { fn request_screenshot(&self) {} fn cycles(&self) -> CyclesPtr { CyclesPtr::dangling() } fn frame_pending(&self) -> bool { self.stuck } + fn frame_counts(&self) -> Option<&FrameCounts> { Some(&self.counts) } } } @@ -216,6 +499,7 @@ mod tests { use super::testing::FakeDisplay; use super::*; use std::sync::atomic::AtomicUsize; + use std::sync::Arc; use std::time::Duration; struct ChangingDisplay { @@ -268,4 +552,175 @@ mod tests { assert_eq!(display.checks.load(Ordering::Relaxed), 2); }).unwrap().join().unwrap(); } + + #[test] + fn a_mark_is_taken_once_and_every_mark_stores() { + let c = ScreenChanged::default(); + assert!(!c.take()); + c.mark(); + c.mark(); + assert!(c.is_marked()); + assert!(c.take()); + assert!(!c.take()); + assert!(ScreenChanged::marked().take()); + } + + #[test] + fn a_request_made_while_a_frame_is_composed_is_still_pending_after_it() { + let r = FrameRequests::default(); + assert!(!r.pending()); + r.request(); + let noted = r.note(); + assert!(r.pending() && r.answers(noted)); + // Asked again while that frame is composed. + r.request(); + r.answer(noted); + assert!(r.pending(), "the second request needs a frame composed after it"); + assert!(!r.answers(noted)); + r.answer(r.note()); + assert!(!r.pending()); + } + + #[test] + fn capture_calls_nest() { + let r = FrameRequests::default(); + r.capture(true); + r.capture(true); + r.capture(false); + assert!(r.capturing()); + r.capture(false); + r.capture(false); + assert!(!r.capturing(), "an extra off does not go below none"); + } + + #[test] + fn the_counts_say_what_the_frames_did_and_reset() { + let n = FrameCounts::default(); + for composed in [true, false, false, false] { + n.count(composed); + } + assert_eq!(n.get(), (4, 1, 3)); + n.reset(); + assert_eq!(n.get(), (0, 0, 0)); + } + + #[test] + fn waiting_out_the_frame_takes_the_period_and_does_not_drift() { + let c = ScreenChanged::default(); + let start = Instant::now(); + let mut w = FrameWait::new(&c, Duration::from_millis(20)); + for _ in 0..4 { + // A frame that takes a quarter of the period. + std::thread::sleep(Duration::from_millis(5)); + w.wait(); + } + assert!(start.elapsed() >= Duration::from_millis(80), "four periods, not four frames' work"); + } + + /// Asserts on the deadline the wait computed, not on how long the next + /// wait takes: a thread descheduled after the resync would otherwise find + /// the next slot nearer than a period and fail. + #[test] + fn a_late_frame_resyncs_instead_of_running_the_missed_ones_back_to_back() { + let c = ScreenChanged::default(); + let period = Duration::from_millis(20); + let mut w = FrameWait::new(&c, period); + let first_slot = w.next + period; + std::thread::sleep(Duration::from_millis(70)); + let before = Instant::now(); + w.wait(); + let after = Instant::now(); + assert!(w.next >= before && w.next <= after, "the slot restarts from the moment of the resync"); + assert!(w.next > first_slot + period, "not from one of the slots it missed"); + } + + /// Run `f` on a thread with a wait, and return once it is parked (or done). + fn parked_thread( + c: &Arc, + period: Duration, + f: impl FnOnce(&mut FrameWait<'_>) -> T + Send + 'static, + ) -> std::thread::JoinHandle { + let q = c.clone(); + let t = std::thread::spawn(move || f(&mut FrameWait::new(&q, period))); + while !c.parked.load(Ordering::Relaxed) && !t.is_finished() { + std::thread::yield_now(); + } + t + } + + #[test] + fn a_wake_ends_a_wait_at_once() { + let c = Arc::new(ScreenChanged::default()); + let q = c.clone(); + let t = std::thread::spawn(move || { + let mut w = FrameWait::new(&q, Duration::from_secs(30)); + let t = Instant::now(); + w.wait(); + t.elapsed() + }); + while !t.is_finished() { + c.wake(); + std::thread::sleep(Duration::from_millis(2)); + } + assert!(t.join().unwrap() < Duration::from_secs(10)); + } + + #[test] + fn a_parked_display_wakes_on_a_mark_and_not_before() { + let c = Arc::new(ScreenChanged::default()); + let t = parked_thread(&c, Duration::from_millis(1), |w| w.park_until_marked(None)); + std::thread::sleep(Duration::from_millis(30)); + assert!(!t.is_finished(), "nothing marked, so it stays parked"); + c.mark(); + assert_eq!(t.join().unwrap(), Woke::Marked); + assert!(c.take(), "the mark is still up for the frame to take"); + } + + #[test] + fn a_wake_ends_a_park() { + let c = Arc::new(ScreenChanged::default()); + let t = parked_thread(&c, Duration::from_millis(1), |w| w.park_until_marked(None)); + c.wake(); + assert_eq!(t.join().unwrap(), Woke::Requested); + } + + #[test] + fn marks_between_two_vsyncs_make_one_frame() { + let c = ScreenChanged::default(); + let start = Instant::now(); + let mut w = FrameWait::new(&c, Duration::from_millis(20)); + // Frame one runs at the start; four marks follow within its period. + for _ in 0..4 { + c.mark(); + } + assert_eq!(w.park_until_marked(None), Woke::Marked); + assert!(start.elapsed() >= Duration::from_millis(20), "the frame starts at the vsync, not at the mark"); + assert!(c.take()); + c.mark(); + assert_eq!(w.park_until_marked(None), Woke::Marked); + assert!(start.elapsed() >= Duration::from_millis(40), "and the next one a period after that"); + } + + /// After a long park the vsync is long past: the frame starts at once and + /// the next slot is a period from now, not from where the clock had got to. + #[test] + fn a_mark_after_a_long_park_starts_the_frame_at_once() { + let c = ScreenChanged::default(); + let mut w = FrameWait::new(&c, Duration::from_millis(20)); + w.next -= Duration::from_secs(1); + c.mark(); + let t = Instant::now(); + assert_eq!(w.park_until_marked(None), Woke::Marked); + assert!(w.next >= t, "the slot starts from now"); + } + + #[test] + fn a_board_timer_wakes_a_parked_display_when_it_is_due() { + let c = ScreenChanged::default(); + let mut w = FrameWait::new(&c, Duration::from_millis(20)); + let due = Instant::now() + Duration::from_millis(40); + assert_eq!(w.park_until_marked(Some(due)), Woke::Timer); + assert!(Instant::now() >= due); + assert!(!c.take()); + } } diff --git a/src/hostinput.rs b/src/hostinput.rs index 64e005c6c..22574fe9f 100644 --- a/src/hostinput.rs +++ b/src/hostinput.rs @@ -530,7 +530,7 @@ fn num(args: &[&str], i: usize, what: &str) -> Result { + let d = self.display.as_ref().ok_or("no display")?; + let counts = d.frame_counts().ok_or("this board does not count its display frames")?; + match args.get(1).copied() { + None => { + let (frames, composed, skipped) = counts.get(); + out(&mut w, format!("{frames} frames: {composed} composed, {skipped} skipped")); + } + Some("reset") => { + counts.reset(); + out(&mut w, "frame counts reset".into()); + } + Some(a) => return Err(format!("unknown argument '{a}' (screen frames [reset])")), + } + Ok(()) + } ("screen", "cursor") => { let c = self.cursor().ok_or("cursor off")?; let (hx, hy) = self.hotspot(&c); @@ -733,6 +749,30 @@ mod tests { assert!(hi.wait_still(Duration::from_millis(1), Duration::from_secs(5)).unwrap(), "a composed, unchanging screen is still"); } + #[test] + fn screen_frames_reports_and_resets_a_boards_counts() { + let fake = Arc::new(crate::gfx_display::testing::FakeDisplay::new(4, 2, false)); + let display: Arc = fake.clone(); + let hi = HostInput::new(Some(display), Arc::new(Ps2Controller::new(None))); + let run = |args: &[&str]| { + let out = Arc::new(parking_lot::Mutex::new(Vec::new())); + struct Sink(Arc>>); + impl Write for Sink { + fn write(&mut self, b: &[u8]) -> std::io::Result { self.0.lock().extend_from_slice(b); Ok(b.len()) } + fn flush(&mut self) -> std::io::Result<()> { Ok(()) } + } + use crate::traits::Device; + hi.execute_command("screen", args, Box::new(Sink(out.clone()))).map(|_| String::from_utf8(out.lock().clone()).unwrap()) + }; + for composed in [true, false, false] { + fake.counts.count(composed); + } + assert_eq!(run(&["frames"]).unwrap(), "3 frames: 1 composed, 2 skipped\n"); + run(&["frames", "reset"]).unwrap(); + assert_eq!(run(&["frames"]).unwrap(), "0 frames: 0 composed, 0 skipped\n"); + assert!(run(&["frames", "bogus"]).is_err()); + } + #[test] fn png_crop_bounds() { let px = vec![0u32; 4 * 3]; diff --git a/src/ip32.rs b/src/ip32.rs index 1a303d75f..5343711f6 100644 --- a/src/ip32.rs +++ b/src/ip32.rs @@ -507,6 +507,7 @@ impl Ip32Bus { let ust = ust.clone(); let mut gbe = crate::dev::o2gfx::Gbe::new(Arc::new(move || ust.now())); gbe.tr = crime.tr.clone(); + gbe.changed = ram.screen_changed().clone(); gbe }, mace: Mace::new(ram.clone(), crime, ust, &PCI0), @@ -639,9 +640,9 @@ impl Ip32Bus { self.ram.total() } - /// GBE's picture out of RAM into `out` (R, G, B, A bytes), or None while - /// the display is off. See [`crate::dev::o2gfx::Gbe::render`]. - pub fn render_screen(&self, out: &mut [u8]) -> Option<(u32, u32)> { + /// GBE's picture out of RAM into `out` (`0xAABBGGRR` words), or None + /// while the display is off. See [`crate::dev::o2gfx::Gbe::render`]. + pub fn render_screen(&self, out: &mut [u32]) -> Option<(u32, u32)> { self.gbe.render_from(&self.ram, out) } diff --git a/src/ip32_machine.rs b/src/ip32_machine.rs index c791edc1f..909056ee2 100644 --- a/src/ip32_machine.rs +++ b/src/ip32_machine.rs @@ -805,6 +805,8 @@ impl Drop for O2Machine { crate::cpu::jitv2::Jitv2::stop_for_drop(&self.cpu.jitv2(), || self.cpu.clone().as_device().stop()); self.display.display_stop.request(); self.running.store(false, Ordering::SeqCst); + // A display thread parked with no renderer is woken to see it. + self.bus.gbe.changed.wake(); for t in self.threads.drain(..) { let _ = t.join(); } @@ -907,14 +909,18 @@ impl Workstation for O2Machine { } } -/// GBE's picture, scanned out of RAM about sixty times a second and handed -/// to whatever renderer the front-end installs. +/// GBE's picture, scanned out of RAM when it may have changed and handed to +/// whatever renderer the front-end installs. pub struct O2Display { bus: Arc, renderer: Mutex>>, display_stop: crate::gfx_display::DisplayStop, screen: Mutex, screenshot_pending: AtomicBool, + /// Frames asked for in `screen.rgba` (`request_frame`), and the clients + /// that want every frame there (`set_capture`, VNC). + frames: crate::gfx_display::FrameRequests, + counts: crate::gfx_display::FrameCounts, /// The status bar's inputs; the heartbeat is the MACE ID chip's, and the /// Hz is the CPU's Compare-match count (IRIX's clock tick on the O2, /// which has no 8254). @@ -922,6 +928,9 @@ pub struct O2Display { running: Arc, } +/// How long a watched screen goes without a scan, marks or not. +const RESCAN: Duration = Duration::from_secs(1); + impl O2Display { fn new(bus: Arc, status: Arc, running: Arc) -> Self { Self { @@ -930,53 +939,101 @@ impl O2Display { display_stop: Default::default(), screen: Mutex::new(Rex3Screen::new()), screenshot_pending: AtomicBool::new(false), + frames: Default::default(), + counts: Default::default(), status, running, } } + /// Scan GBE out of RAM when the screen may have changed, and show it. + /// + /// The frame buffer is RAM. What marks the screen as changed: the + /// rendering engine finishing a primitive that wrote memory, a write to a + /// GBE register (the colour maps, the cursor, the tile lists, the timing), + /// a shadow register going live at vertical blanking, and a restored + /// snapshot. A bare store by the CPU into frame buffer memory marks + /// nothing, as it carries no command, so a guest that draws that way (a + /// Linux frame buffer console) shows only when something else marks. While + /// anything watches (a renderer, a capturing client) the screen is scanned + /// at least once a second whatever the marks say, which bounds that + /// staleness; with nothing watching the thread parks. A screenshot or a + /// frame request gets a fresh frame at once, a capturing client one every + /// frame, and a window has the unchanged frame shown again every few + /// frames so its status bar moves. fn display_loop(&self) { - let frame_time = Duration::from_micros(16_667); + let changed = self.bus.gbe.changed.clone(); + let mut wait = crate::gfx_display::FrameWait::new(&changed, crate::gfx_display::FRAME_PERIOD); let mut status_bar = crate::disp::StatusBar::new(); let mut overlay = crate::debug_overlay::DebugOverlay::new(); let mut sbtex = crate::disp::StatusBarTexture::new(); - let mut rgba = vec![0u8; 2048 * 2048 * 4]; + let mut frame = vec![0u32; 2048 * 2048]; let mut size = (0usize, 0usize); + // The size the renderer was last given. + let mut sized = (0usize, 0usize); let mut shots = 0u32; + let mut first = true; + let mut idle = 0u32; + let mut had_renderer = false; + let mut scanned = Instant::now(); while self.running.load(Ordering::Relaxed) { let start = Instant::now(); - let shown = self.bus.render_screen(&mut rgba); - // Off: keep the last size, black. - let (w, h) = shown.map(|(w, h)| (w as usize, h as usize)).unwrap_or(if size.0 == 0 { (1280, 1024) } else { size }); - { + // Taken before the scan reads anything, so a mark made while it + // runs is for the next frame. + let marked = changed.take() | self.bus.gbe.latch_pending(); + let wanted = self.screenshot_pending.load(Ordering::Relaxed) + || self.frames.pending() + || self.frames.capturing(); + let noted = self.frames.note(); + let has_renderer = self.renderer.lock().is_some(); + let watched = has_renderer || self.frames.capturing(); + // A renderer that has just arrived has nothing to show yet. + let compose = first || marked || wanted || (has_renderer && !had_renderer) || (watched && scanned.elapsed() >= RESCAN); + had_renderer = has_renderer; + idle = if compose { 0 } else { idle.saturating_add(1) }; + let refresh = !compose && has_renderer && idle >= crate::gfx_display::IDLE_REFRESH; + if compose { + first = false; + scanned = Instant::now(); + let shown = self.bus.render_screen(&mut frame); + // Off: keep the last size, black. + let (w, h) = shown.map(|(w, h)| (w as usize, h as usize)).unwrap_or(if size.0 == 0 { (1280, 1024) } else { size }); let mut screen = self.screen.lock(); let stride = crate::disp::FB_STRIDE; for y in 0..h { - for x in 0..w { - let v = if shown.is_some() { - let o = (y * w + x) * 4; - u32::from_le_bytes([rgba[o], rgba[o + 1], rgba[o + 2], 0xff]) - } else { - 0xff00_0000 - }; - screen.fb_rgb[y * stride + x] = v; - screen.rgba[y * stride + x] = v; + let row = &mut screen.fb_rgb[y * stride..y * stride + w]; + if shown.is_some() { + row.copy_from_slice(&frame[y * w..(y + 1) * w]); + } else { + row.fill(0xff00_0000); } } + let screen = &mut *screen; + for y in 0..h { + screen.rgba[y * stride..y * stride + w].copy_from_slice(&screen.fb_rgb[y * stride..y * stride + w]); + } screen.frame_seq = screen.frame_seq.wrapping_add(1); - screen.prebuilt = true; screen.width = w; screen.height = h; - screen.status_bar_only = false; + size = (w, h); + // Only the requests made before the scan are answered by it. + self.frames.answer(noted); + } + if compose || refresh { + idle = 0; + let mut screen = self.screen.lock(); + screen.prebuilt = true; + screen.status_bar_only = !compose; + let (w, h) = (screen.width, screen.height); let take_shot = self.screenshot_pending.swap(false, Ordering::Relaxed); let stats = self.status.snapshot(start, 0); if let Some(r) = self.renderer.lock().as_mut() { - if (w, h) != size { + if (w, h) != sized { r.resize(w, h); + sized = (w, h); } r.present(&mut screen, &mut overlay, &mut status_bar, &mut sbtex, &stats, take_shot, None, None); } - size = (w, h); if take_shot { let pixels = screen.rgba.clone(); let path = format!("screenshot_{shots:04}.png"); @@ -987,9 +1044,18 @@ impl O2Display { }); } } - let spent = start.elapsed(); - if spent < frame_time { - std::thread::sleep(frame_time - spent); + // Counted when the frame is in the screen, which is what a reader + // of the counts waits for. + self.counts.count(compose); + // A window or a capturing client is paced by the frame period. A + // shadow control still to go live at blanking is looked at every + // period too: it marks only when something next looks. Otherwise + // nothing needs doing until something changes or someone asks, and + // the timer is only so a renderer installed later is seen. + if watched || self.bus.gbe.latch_pending() { + wait.wait(); + } else { + wait.park_until_marked(Some(Instant::now() + Duration::from_millis(250))); } } self.display_stop.finish(&self.renderer); @@ -1008,6 +1074,23 @@ impl crate::gfx_display::GfxDisplay for O2Display { } fn request_screenshot(&self) { self.screenshot_pending.store(true, Ordering::Relaxed); + self.bus.gbe.changed.wake(); + } + fn request_frame(&self) { + self.frames.request(); + self.bus.gbe.changed.wake(); + } + fn frame_pending(&self) -> bool { + self.frames.pending() + } + fn set_capture(&self, on: bool) { + self.frames.capture(on); + if on { + self.bus.gbe.changed.wake(); + } + } + fn frame_counts(&self) -> Option<&crate::gfx_display::FrameCounts> { + Some(&self.counts) } fn cycles(&self) -> CyclesPtr { self.status.cycles() @@ -1045,12 +1128,12 @@ static LAST_REGS: [std::sync::atomic::AtomicU64; 42] = /// the same commands, and replies, as the control port's. struct O2Control { bus: Arc, - rgba: Mutex>, + rgba: Mutex>, } impl O2Control { fn new(bus: Arc) -> Self { - Self { bus, rgba: Mutex::new(vec![0u8; 2048 * 2048 * 4]) } + Self { bus, rgba: Mutex::new(vec![0u32; 2048 * 2048]) } } } @@ -1088,7 +1171,7 @@ fn control_server(listener: std::net::TcpListener, bus: Arc) { Ok(o) => o, Err(_) => return, }; - let mut rgba = vec![0u8; 2048 * 2048 * 4]; + let mut rgba = vec![0u32; 2048 * 2048]; for line in BufReader::new(conn).lines().map_while(Result::ok) { let (cmd, arg) = line.trim_end().split_once(' ').unwrap_or((line.trim_end(), "")); let reply = control_command(&bus, cmd, arg, &mut rgba); @@ -1100,12 +1183,12 @@ fn control_server(listener: std::net::TcpListener, bus: Arc) { } } -fn control_command(bus: &Ip32Bus, cmd: &str, arg: &str, rgba: &mut [u8]) -> String { +fn control_command(bus: &Ip32Bus, cmd: &str, arg: &str, rgba: &mut [u32]) -> String { let kb = bus.mace.ps2.devices(); match cmd { "shot" => match bus.render_screen(rgba) { - Some((w, h)) => match crate::disp::save_png_rgba8(arg, &rgba[..(w * h * 4) as usize], w, h) { - Ok(()) => format!("ok {w}x{h}"), + Some((w, h)) => match crate::hostinput::save_png(arg, &rgba[..(w * h) as usize], w as usize, h as usize, None) { + Ok(_) => format!("ok {w}x{h}"), Err(e) => format!("err {e}"), }, None => "err display off".into(), @@ -1361,3 +1444,122 @@ mod drop_tests { assert_eq!(phys.strong_count(), 0, "the bus outlives its machine"); } } + +#[cfg(test)] +mod display_tests { + use super::*; + use crate::dev::o2gfx::gbe_reg::*; + use crate::gfx_display::GfxDisplay; + use std::time::Duration; + + /// Poll `f` until it holds, for up to ten seconds. + fn until(what: &str, f: impl Fn() -> bool) { + let t = Instant::now(); + while !f() { + assert!(t.elapsed() < Duration::from_secs(10), "timed out waiting for {what}"); + std::thread::sleep(Duration::from_millis(2)); + } + } + + /// A 512 x 128, 8 bpp display of one tile at 0x100000, entry 5 of the + /// colour map the colour of the tile's first pixel (red low). + fn bus_showing_a_tile() -> Arc { + let bus = Ip32Bus::new(16 << 20, vec![0u8; PROM_SIZE as usize]); + let (tile, list) = (0x0010_0000u32, 0x0020_0000u32); + bus.ram.write(list as usize, &((tile >> 16) as u16).to_be_bytes()); + bus.ram.write(tile as usize, &[5]); + for (off, v) in [ + (CTRLSTAT, 0x300a_a000u32), + (DOTCLOCK, 0x0010_020f), + (VT_XYMAX, 0x0042_9690), + (FRM_CONTROL_SHADOW, list | 1), + (VT_XY, 0), + (VT_HCMAP, 512 << 12), + (VT_VCMAP, 128 << 12), + (FRM_SIZE_TILE, 1 << 5), + (FRM_SIZE_PIXEL, 128 << 16), + (MODE, 0x403), + (CMAP + 4 * 5, 0x1122_3300), + ] { + bus.gbe.write32((0x1600_0000 + off) as u64, v); + } + bus + } + + /// The display scans out when the screen may have changed and parks when + /// not: a headless run idles at no frames, a mark or a request composes + /// one, and a stop ends it. + #[test] + fn the_display_composes_for_a_mark_or_a_request_and_idles_otherwise() { + let bus = bus_showing_a_tile(); + let running = Arc::new(AtomicBool::new(true)); + let status = crate::disp::StatusSource::new(bus.mace.id.heartbeat.clone(), Arc::new(std::sync::atomic::AtomicU64::new(0))); + let display = Arc::new(O2Display::new(bus.clone(), status, running.clone())); + let d = display.clone(); + let thread = std::thread::spawn(move || d.display_loop()); + let seq = || display.screen.lock().frame_seq; + let composed = || display.counts.get().1; + + until("the first frame", || composed() >= 1); + assert_eq!(display.screen.lock().fb_rgb[0], 0xff33_2211, "scan-out, in the renderers' format, opaque"); + assert_eq!(display.screen.lock().rgba[0], 0xff33_2211); + // Nothing marked: the display thread wakes on its timer and composes nothing. + let before = composed(); + until("an idle frame", || display.counts.get().2 >= 1); + assert_eq!(composed(), before, "an idle screen composes nothing"); + + // A write to a GBE register. + bus.gbe.write32((0x1600_0000 + CMAP + 4 * 5) as u64, 0x4455_6600); + until("the register's frame", || composed() == before + 1); + assert_eq!(display.screen.lock().rgba[0], 0xff66_5544, "the new colour"); + // A mark from memory, as the engine makes. + bus.ram.screen_changed().mark(); + until("the memory mark's frame", || composed() == before + 2); + + // A request composes with nothing marked, and is pending until it has. + let seq_before = seq(); + display.request_frame(); + until("the requested frame", || composed() == before + 3 && !display.frame_pending()); + assert_eq!(seq(), seq_before + 1); + // A capturing client gets a frame every period while it is on. + display.set_capture(true); + until("captured frames", || composed() >= before + 6); + display.set_capture(false); + + running.store(false, Ordering::SeqCst); + display.bus.gbe.changed.wake(); + thread.join().unwrap(); + } + + /// A renderer that arrives gets a frame at once, and a watched screen is + /// scanned again about once a second with nothing marked, so a guest that + /// stores into the frame buffer with no command is not stale for long. + #[test] + fn a_watched_screen_is_scanned_again_without_a_mark() { + let bus = bus_showing_a_tile(); + let running = Arc::new(AtomicBool::new(true)); + let status = crate::disp::StatusSource::new(bus.mace.id.heartbeat.clone(), Arc::new(std::sync::atomic::AtomicU64::new(0))); + let display = Arc::new(O2Display::new(bus.clone(), status, running.clone())); + let d = display.clone(); + let thread = std::thread::spawn(move || d.display_loop()); + let composed = || display.counts.get().1; + until("the first frame", || composed() >= 1); + // A store with no command: the picture changes, nothing marks. + bus.ram.write(0x0010_0000, &[6]); + bus.gbe.write32((0x1600_0000 + CMAP + 4 * 6) as u64, 0x7788_9900); + until("the register's frame", || composed() >= 2); + bus.ram.write(0x0010_0000, &[5]); + let before = composed(); + let probe = crate::display_stop_tests::Probe::new(|| {}); + let presents = probe.presents.clone(); + *display.renderer.lock() = Some(Box::new(probe)); + until("the new renderer's frame", || composed() > before && presents.load(Ordering::SeqCst) >= 1); + bus.ram.write(0x0010_0000, &[6]); + let seq = display.screen.lock().frame_seq; + until("a scan with no mark", || display.screen.lock().frame_seq > seq); + assert_eq!(display.screen.lock().rgba[0], 0xff99_8877, "the unmarked store is on the screen"); + running.store(false, Ordering::SeqCst); + display.bus.gbe.changed.wake(); + thread.join().unwrap(); + } +}