use std::{collections::VecDeque, time::Instant};
use sui_core::{AsyncWakeToken, Error, Event, Result, Size, WindowEvent, WindowId};
use sui_render_wgpu::{
DebugCaptureArtifact, DebugCaptureRequest, DisplayCapabilities, RgbaImage,
WgpuExternalTextureRegistry, WgpuRenderer,
};
use sui_runtime::{PresentationLatencyDiagnostics, Runtime};
use crate::{AccessibilityBridge, AccessibilitySnapshot, PresentedFrame, present_window_frame};
#[derive(Debug, Clone)]
pub struct PlatformWindow {
pub id: WindowId,
pub title: String,
pub accessibility: Option<AccessibilitySnapshot>,
}
#[derive(Debug, Default)]
pub struct HeadlessPlatform {
renderer: WgpuRenderer,
windows: Vec<WindowState>,
pending_events: VecDeque<QueuedEvent>,
frame_clock: f64,
display_capabilities: Option<DisplayCapabilities>,
}
impl HeadlessPlatform {
const DEFAULT_WINDOW_SIZE: Size = Size::new(1280.0, 720.0);
pub fn new() -> Self {
Self::default()
}
pub fn with_feather_width(mut self, feather_width: f32) -> Self {
self.set_feather_width(feather_width);
self
}
pub fn with_display_capabilities(mut self, capabilities: DisplayCapabilities) -> Self {
self.display_capabilities = Some(capabilities);
self
}
fn display_capabilities(&self) -> DisplayCapabilities {
self.display_capabilities
.clone()
.unwrap_or_else(|| DisplayCapabilities {
notes: "Headless offscreen renderer.".to_string(),
..DisplayCapabilities::default()
})
}
pub fn with_feathering_enabled(mut self, enabled: bool) -> Self {
self.set_feathering_enabled(enabled);
self
}
pub fn run(&mut self, runtime: &mut Runtime) -> Result<Vec<PlatformWindow>> {
loop {
if self.pump(runtime)? {
continue;
}
if !self.advance_to_next_wakeup(runtime)? {
break;
}
}
Ok(self
.windows
.iter()
.filter(|window| window.open)
.map(WindowState::snapshot)
.collect())
}
pub fn pump(&mut self, runtime: &mut Runtime) -> Result<bool> {
self.sync_windows(runtime)?;
let mut did_work = runtime.has_pending_commands();
runtime.process_commands();
runtime.tick(self.frame_clock);
self.queue_ready_events(runtime);
self.queue_redraw_requests(runtime)?;
while let Some(queued_event) = self.pending_events.pop_front() {
did_work = true;
self.process_event(runtime, queued_event)?;
}
Ok(did_work)
}
pub fn advance_time(&mut self, delta: f64) {
self.frame_clock += delta;
}
pub fn wake_async(
&mut self,
runtime: &mut Runtime,
window_id: WindowId,
token: AsyncWakeToken,
) -> Result<bool> {
self.sync_windows(runtime)?;
runtime.wake_async(window_id, token)
}
pub fn current_time(&self) -> f64 {
self.frame_clock
}
fn advance_to_next_wakeup(&mut self, runtime: &Runtime) -> Result<bool> {
let mut next: Option<f64> = None;
for window_id in runtime.window_ids() {
let candidate = runtime.next_wakeup_time(window_id)?;
next = match (next, candidate) {
(Some(current), Some(candidate)) => Some(current.min(candidate)),
(None, Some(candidate)) => Some(candidate),
(current, None) => current,
};
}
let Some(next) = next else {
return Ok(false);
};
if next <= self.frame_clock {
return Ok(false);
}
self.frame_clock = next;
Ok(true)
}
pub fn pending_event_count(&self) -> usize {
self.pending_events.len()
}
pub fn has_pending_events(&self) -> bool {
!self.pending_events.is_empty()
}
pub fn dispatch_event(
&mut self,
runtime: &Runtime,
window_id: WindowId,
event: Event,
) -> Result<()> {
self.sync_windows(runtime)?;
if !self
.windows
.iter()
.any(|window| window.id == window_id && window.open)
{
return Err(Error::new(format!(
"window {} is not registered with the headless platform",
window_id.get()
)));
}
self.pending_events
.push_back(QueuedEvent { window_id, event });
Ok(())
}
pub fn renderer(&self) -> &WgpuRenderer {
&self.renderer
}
pub fn feather_width(&self) -> f32 {
self.renderer.feather_width()
}
pub fn feathering_enabled(&self) -> bool {
self.renderer.feathering_enabled()
}
pub fn set_feather_width(&mut self, feather_width: f32) {
self.renderer.set_feather_width(feather_width);
}
pub fn set_feathering_enabled(&mut self, enabled: bool) {
self.renderer.set_feathering_enabled(enabled);
}
pub fn set_external_texture_registry(&mut self, registry: WgpuExternalTextureRegistry) {
self.renderer.set_external_texture_registry(registry);
}
pub fn renderer_mut(&mut self) -> &mut WgpuRenderer {
&mut self.renderer
}
pub fn capture_rgba(&self, window_id: WindowId) -> Result<RgbaImage> {
self.renderer.capture_rgba(window_id)
}
pub fn capture_debug_frame(
&mut self,
window_id: WindowId,
request: DebugCaptureRequest,
) -> Result<DebugCaptureArtifact> {
self.renderer.capture_last_frame_debug(window_id, request)
}
pub fn accessibility_snapshot(&self, window_id: WindowId) -> Option<&AccessibilitySnapshot> {
self.windows
.iter()
.find(|window| window.id == window_id && window.open)
.and_then(|window| window.accessibility.snapshot())
}
fn queue_ready_events(&mut self, runtime: &mut Runtime) {
self.pending_events.extend(
runtime
.drain_ready_events()
.into_iter()
.map(|(window_id, event)| QueuedEvent { window_id, event }),
);
}
fn sync_windows(&mut self, runtime: &Runtime) -> Result<()> {
let runtime_window_ids = runtime.window_ids();
let removed_ids: Vec<_> = self
.windows
.iter()
.map(|window| window.id)
.filter(|window_id| !runtime_window_ids.contains(window_id))
.collect();
for window_id in removed_ids {
self.renderer.remove_window(window_id);
crate::clear_window_performance(window_id);
}
self.windows
.retain(|window| runtime_window_ids.contains(&window.id));
self.pending_events
.retain(|queued_event| runtime_window_ids.contains(&queued_event.window_id));
for window_id in runtime_window_ids {
if self.windows.iter().any(|window| window.id == window_id) {
continue;
}
self.windows.push(WindowState {
id: window_id,
title: runtime.window_title(window_id)?.to_string(),
open: true,
awaiting_performance_bootstrap: true,
redraw_requested: false,
redraw_requested_at_ms: None,
frame_index: 0,
pending_event_time_ms: 0.0,
last_non_redraw_event_at_ms: None,
accessibility: AccessibilityBridge::default(),
});
self.pending_events.push_back(QueuedEvent {
window_id,
event: Event::Window(WindowEvent::Resized(Self::DEFAULT_WINDOW_SIZE)),
});
}
Ok(())
}
fn queue_redraw_requests(&mut self, runtime: &Runtime) -> Result<()> {
let now_ms = self.current_time() * 1000.0;
for window in &mut self.windows {
let capturing = crate::has_pending_window_debug_captures(window.id);
if !window.open
|| window.redraw_requested
|| (!runtime.needs_render(window.id)? && !capturing)
{
continue;
}
window.redraw_requested = true;
window.redraw_requested_at_ms = Some(now_ms);
self.pending_events.push_back(QueuedEvent {
window_id: window.id,
event: Event::Window(WindowEvent::RedrawRequested),
});
}
Ok(())
}
fn process_event(&mut self, runtime: &mut Runtime, queued_event: QueuedEvent) -> Result<()> {
let Some(window_index) = self
.windows
.iter()
.position(|window| window.id == queued_event.window_id && window.open)
else {
return Ok(());
};
let is_redraw = matches!(
queued_event.event,
Event::Window(WindowEvent::RedrawRequested)
);
let is_close = matches!(
queued_event.event,
Event::Window(WindowEvent::CloseRequested)
);
let window_id = queued_event.window_id;
let event_arrived_at_ms = self.current_time() * 1000.0;
let event_started = Instant::now();
runtime.handle_event(window_id, queued_event.event)?;
let event_time_ms = event_started.elapsed().as_secs_f64() * 1000.0;
if !is_redraw && !is_close && runtime.needs_render(window_id)? {
self.windows[window_index].pending_event_time_ms += event_time_ms;
self.windows[window_index].last_non_redraw_event_at_ms = Some(event_arrived_at_ms);
}
if is_redraw {
self.windows[window_index].redraw_requested = false;
if runtime.needs_render(window_id)? {
runtime.tick(self.frame_clock);
let render_started_at_ms = self.current_time() * 1000.0;
let mut presentation_latency = PresentationLatencyDiagnostics::new(
self.windows[window_index]
.last_non_redraw_event_at_ms
.map(|timestamp| (render_started_at_ms - timestamp).max(0.0))
.unwrap_or(0.0),
0.0,
self.windows[window_index]
.redraw_requested_at_ms
.map(|timestamp| (render_started_at_ms - timestamp).max(0.0))
.unwrap_or(0.0),
);
self.renderer
.set_window_display_capabilities(window_id, self.display_capabilities())?;
let PresentedFrame {
output,
runtime_time_ms,
renderer_time_ms,
..
} = present_window_frame(runtime, &mut self.renderer, window_id)?;
let presented_at_ms = self.current_time() * 1000.0;
presentation_latency.event_to_present_ms = self.windows[window_index]
.last_non_redraw_event_at_ms
.map(|timestamp| (presented_at_ms - timestamp).max(0.0))
.unwrap_or(0.0);
self.windows[window_index].frame_index += 1;
let frame_index = self.windows[window_index].frame_index;
let pending_event_time_ms =
std::mem::take(&mut self.windows[window_index].pending_event_time_ms);
self.windows[window_index].last_non_redraw_event_at_ms = None;
self.windows[window_index].redraw_requested_at_ms = None;
crate::publish_frame_performance(
window_id,
frame_index,
pending_event_time_ms,
event_time_ms,
runtime_time_ms,
presentation_latency,
&output,
&self.renderer,
renderer_time_ms,
);
if self.windows[window_index].awaiting_performance_bootstrap {
self.windows[window_index].awaiting_performance_bootstrap = false;
if !self.windows[window_index].redraw_requested {
self.windows[window_index].redraw_requested = true;
self.windows[window_index].redraw_requested_at_ms =
Some(self.current_time() * 1000.0);
self.pending_events.push_back(QueuedEvent {
window_id,
event: Event::Window(WindowEvent::RedrawRequested),
});
}
}
self.windows[window_index].title = output.title;
self.windows[window_index]
.accessibility
.update(window_id, output.semantics);
}
for token in crate::service_window_debug_captures(&mut self.renderer, window_id) {
runtime.wake_async(window_id, token)?;
}
}
if is_close {
runtime.remove_window(window_id)?;
self.renderer.remove_window(window_id);
crate::clear_window_performance(window_id);
self.pending_events
.retain(|pending_event| pending_event.window_id != window_id);
self.windows.swap_remove(window_index);
}
Ok(())
}
}
#[derive(Debug, Clone)]
struct WindowState {
id: WindowId,
title: String,
open: bool,
awaiting_performance_bootstrap: bool,
redraw_requested: bool,
redraw_requested_at_ms: Option<f64>,
frame_index: u64,
pending_event_time_ms: f64,
last_non_redraw_event_at_ms: Option<f64>,
accessibility: AccessibilityBridge,
}
impl WindowState {
fn snapshot(&self) -> PlatformWindow {
PlatformWindow {
id: self.id,
title: self.title.clone(),
accessibility: self.accessibility.snapshot().cloned(),
}
}
}
#[derive(Debug, Clone)]
struct QueuedEvent {
window_id: WindowId,
event: Event,
}
#[cfg(test)]
mod tests {
use std::{cell::RefCell, f32::consts::TAU, rc::Rc};
use super::HeadlessPlatform;
use sui_core::{
AsyncWakeToken, Color, CustomEvent, Event, Rect, Result, SemanticsNode, SemanticsRole,
Size, TimerToken, WakeEvent, WindowEvent,
};
use sui_layout::Constraints;
use sui_runtime::{
Application, ArrangeCtx, EventCtx, MeasureCtx, PaintCtx, Runtime,
SceneStatisticsDetailMode, SemanticsCtx, Widget, WindowBuilder, WindowColorManagementMode,
WindowDynamicRangeMode, WindowOutputColorPrimaries, WindowRenderOptions,
WindowToneMappingMode, set_window_render_options, set_window_scene_statistics_detail_mode,
window_performance_snapshot,
};
#[derive(Default)]
struct Counters {
events: usize,
paints: usize,
timer_wakes: usize,
async_wakes: usize,
animation_wakes: usize,
pending_animation_frames: usize,
last_animation_delta: Option<f64>,
timer_token: Option<TimerToken>,
async_token: Option<AsyncWakeToken>,
}
struct TestRoot {
counters: Rc<RefCell<Counters>>,
}
impl Widget for TestRoot {
fn event(&mut self, ctx: &mut EventCtx, event: &Event) {
match event {
Event::Custom(custom) if custom.kind == "repaint" => {
self.counters.borrow_mut().events += 1;
ctx.request_paint();
}
Event::Custom(custom) if custom.kind == "arm-wakeups" => {
let mut counters = self.counters.borrow_mut();
counters.timer_token = Some(ctx.schedule_timer_after(3.0));
counters.async_token = Some(ctx.register_async_wakeup());
}
Event::Custom(custom) if custom.kind == "arm-animation" => {
let mut counters = self.counters.borrow_mut();
counters.pending_animation_frames = 3;
counters.last_animation_delta = None;
ctx.request_animation_frame();
}
Event::Wake(WakeEvent::Timer { token, .. }) => {
let mut counters = self.counters.borrow_mut();
if counters.timer_token == Some(*token) {
counters.timer_wakes += 1;
ctx.request_paint();
ctx.set_handled();
}
}
Event::Wake(WakeEvent::Async { token, .. }) => {
let mut counters = self.counters.borrow_mut();
if counters.async_token == Some(*token) {
counters.async_wakes += 1;
ctx.request_paint();
ctx.set_handled();
}
}
Event::Wake(WakeEvent::AnimationFrame { delta, .. }) => {
let mut counters = self.counters.borrow_mut();
if counters.pending_animation_frames > 0 {
counters.pending_animation_frames -= 1;
counters.animation_wakes += 1;
counters.last_animation_delta = Some(*delta);
ctx.request_paint();
if counters.pending_animation_frames > 0 {
ctx.request_animation_frame();
}
ctx.set_handled();
}
}
_ => {}
}
}
fn paint(&self, ctx: &mut PaintCtx) {
self.counters.borrow_mut().paints += 1;
ctx.clear(Color::rgba(0.08, 0.09, 0.11, 1.0));
ctx.fill_rect(
Rect::new(24.0, 24.0, 120.0, 48.0),
Color::rgba(0.16, 0.19, 0.25, 1.0),
);
}
fn semantics(&self, ctx: &mut SemanticsCtx) {
ctx.push(SemanticsNode::new(
ctx.widget_id(),
SemanticsRole::Window,
ctx.bounds(),
));
}
}
fn build_runtime(counters: Rc<RefCell<Counters>>) -> (Runtime, sui_core::WindowId) {
let runtime = Application::new()
.window(
WindowBuilder::new()
.title("Test")
.root(TestRoot { counters }),
)
.build()
.unwrap();
let window_id = runtime.window_ids()[0];
(runtime, window_id)
}
#[test]
fn input_without_a_redraw_does_not_accumulate_in_the_next_frames_timing() -> Result<()> {
let counters = Rc::new(RefCell::new(Counters::default()));
let (mut runtime, window_id) = build_runtime(counters);
set_window_scene_statistics_detail_mode(window_id, SceneStatisticsDetailMode::Detailed);
let mut platform = HeadlessPlatform::new();
platform.run(&mut runtime)?;
let original_frame = platform.renderer().frames_rendered();
for _ in 0..1000 {
platform.dispatch_event(
&runtime,
window_id,
Event::Custom(CustomEvent::new("idle-input")),
)?;
}
platform.run(&mut runtime)?;
assert_eq!(platform.renderer().frames_rendered(), original_frame);
assert_eq!(
platform.windows[0].pending_event_time_ms, 0.0,
"input processed while no frame is pending must not be charged to a later frame"
);
assert_eq!(platform.windows[0].last_non_redraw_event_at_ms, None);
platform.advance_time(1.0);
platform.process_event(
&mut runtime,
super::QueuedEvent {
window_id,
event: Event::Custom(CustomEvent::new("repaint")),
},
)?;
let repaint_cost = platform.windows[0].pending_event_time_ms;
assert!(repaint_cost > 0.0);
assert_eq!(
platform.windows[0].last_non_redraw_event_at_ms,
Some(1000.0)
);
platform.process_event(
&mut runtime,
super::QueuedEvent {
window_id,
event: Event::Custom(CustomEvent::new("more-input")),
},
)?;
assert!(platform.windows[0].pending_event_time_ms >= repaint_cost);
let expected_event_cost = platform.windows[0].pending_event_time_ms;
platform.run(&mut runtime)?;
let snapshot = window_performance_snapshot(window_id).expect("repaint timing");
let reported_event_cost = snapshot
.phase_timings
.iter()
.find(|sample| sample.phase == sui_runtime::FramePhase::Event)
.expect("events that contribute to a frame must still be reported")
.duration_ms;
assert_eq!(reported_event_cost, expected_event_cost);
assert_eq!(platform.windows[0].pending_event_time_ms, 0.0);
assert_eq!(platform.windows[0].last_non_redraw_event_at_ms, None);
Ok(())
}
#[test]
fn queued_events_reenter_the_platform_render_loop() -> Result<()> {
let counters = Rc::new(RefCell::new(Counters::default()));
let (mut runtime, window_id) = build_runtime(Rc::clone(&counters));
let mut platform = HeadlessPlatform::new();
let windows = platform.run(&mut runtime)?;
let accessibility = platform.accessibility_snapshot(window_id).unwrap();
assert_eq!(windows.len(), 1);
assert_eq!(windows[0].title, "Test");
assert_eq!(windows[0].accessibility.as_ref(), Some(accessibility));
assert_eq!(accessibility.nodes.len(), 1);
assert_eq!(accessibility.nodes[0].role, SemanticsRole::Window);
assert_eq!(platform.renderer().frames_rendered(), 2);
assert_eq!(counters.borrow().paints, 1);
assert!(!platform.has_pending_events());
platform.dispatch_event(
&runtime,
window_id,
Event::Custom(CustomEvent::new("repaint")),
)?;
let _ = platform.run(&mut runtime)?;
assert_eq!(counters.borrow().events, 1);
assert_eq!(counters.borrow().paints, 2);
assert_eq!(platform.renderer().frames_rendered(), 3);
assert!(!platform.has_pending_events());
assert!(!runtime.needs_render(window_id)?);
Ok(())
}
#[test]
fn close_requested_removes_window_from_platform_and_runtime() -> Result<()> {
let counters = Rc::new(RefCell::new(Counters::default()));
let (mut runtime, window_id) = build_runtime(counters);
let mut platform = HeadlessPlatform::new();
let _ = platform.run(&mut runtime)?;
platform.dispatch_event(
&runtime,
window_id,
Event::Window(WindowEvent::CloseRequested),
)?;
let windows = platform.run(&mut runtime)?;
assert!(windows.is_empty());
assert!(runtime.window_ids().is_empty());
assert!(runtime.needs_render(window_id).is_err());
Ok(())
}
#[test]
fn wakeups_reenter_the_platform_pump_and_trigger_repaint() -> Result<()> {
let counters = Rc::new(RefCell::new(Counters::default()));
let (mut runtime, window_id) = build_runtime(Rc::clone(&counters));
let mut platform = HeadlessPlatform::new();
let _ = platform.run(&mut runtime)?;
platform.dispatch_event(
&runtime,
window_id,
Event::Custom(CustomEvent::new("arm-wakeups")),
)?;
assert!(platform.pump(&mut runtime)?);
let async_token = counters.borrow().async_token.unwrap();
assert_eq!(counters.borrow().paints, 1);
assert_eq!(counters.borrow().timer_wakes, 0);
assert!(platform.wake_async(&mut runtime, window_id, async_token)?);
assert!(platform.pump(&mut runtime)?);
assert_eq!(counters.borrow().async_wakes, 1);
assert_eq!(counters.borrow().paints, 1);
assert!(platform.pump(&mut runtime)?);
assert_eq!(counters.borrow().paints, 2);
platform.advance_time(3.0);
assert!(platform.pump(&mut runtime)?);
assert_eq!(counters.borrow().timer_wakes, 1);
assert_eq!(counters.borrow().paints, 2);
assert!(platform.pump(&mut runtime)?);
assert_eq!(counters.borrow().paints, 3);
Ok(())
}
struct CaptureProbe {
ticket: Rc<RefCell<Option<crate::DebugCaptureTicket>>>,
artifact: Rc<RefCell<Option<Result<sui_render_wgpu::DebugCaptureArtifact>>>>,
}
impl Widget for CaptureProbe {
fn event(&mut self, ctx: &mut EventCtx, event: &Event) {
match event {
Event::Custom(custom) if custom.kind == "capture" => {
let wake = ctx.register_async_wakeup();
let request = sui_render_wgpu::DebugCaptureRequest {
stage: sui_render_wgpu::DebugCaptureStage::HdrIntermediate,
encoding: sui_render_wgpu::DebugCaptureEncoding::Exr,
..Default::default()
};
*self.ticket.borrow_mut() = Some(crate::request_window_debug_capture(
ctx.window_id(),
request,
Some(wake),
));
}
Event::Wake(WakeEvent::Async { .. }) => {
if let Some(ticket) = *self.ticket.borrow() {
*self.artifact.borrow_mut() = crate::take_window_debug_capture(ticket);
}
}
_ => {}
}
}
fn measure(&mut self, _ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
constraints.clamp(Size::new(64.0, 32.0))
}
fn paint(&self, ctx: &mut PaintCtx) {
ctx.fill_bounds(Color::linear_rgba(4.0, 2.0, 0.5, 1.0));
}
}
struct DiagnosticsProbe {
seen: Rc<RefCell<Vec<Option<WindowToneMappingMode>>>>,
}
impl Widget for DiagnosticsProbe {
fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
let diagnostics =
ctx.observe(&crate::window_output_diagnostics_signal(ctx.window_id()));
self.seen
.borrow_mut()
.push(diagnostics.map(|diagnostics| diagnostics.requested_tone_mapping_mode));
constraints.clamp(Size::new(32.0, 16.0))
}
}
#[test]
fn widgets_observing_output_diagnostics_follow_presented_frames() -> Result<()> {
let seen = Rc::new(RefCell::new(Vec::new()));
let mut runtime = Application::new()
.window(
WindowBuilder::new()
.title("Diagnostics")
.root(DiagnosticsProbe {
seen: Rc::clone(&seen),
}),
)
.build()?;
let window_id = runtime.window_ids()[0];
set_window_render_options(
window_id,
WindowRenderOptions::new(false, 0.0)
.with_tone_mapping_mode(WindowToneMappingMode::Clamp),
);
let mut platform = HeadlessPlatform::new();
let _ = platform.run(&mut runtime)?;
assert_eq!(
*seen.borrow(),
vec![None, Some(WindowToneMappingMode::Clamp)]
);
assert!(
!runtime.needs_render(window_id)?,
"unchanged diagnostics do not invalidate again"
);
set_window_render_options(
window_id,
WindowRenderOptions::new(false, 0.0)
.with_tone_mapping_mode(WindowToneMappingMode::Reinhard),
);
platform.dispatch_event(
&runtime,
window_id,
Event::Window(WindowEvent::RedrawRequested),
)?;
let _ = platform.run(&mut runtime)?;
assert_eq!(
seen.borrow().last().copied().flatten(),
Some(WindowToneMappingMode::Reinhard)
);
Ok(())
}
struct White;
impl Widget for White {
fn measure(&mut self, _ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
constraints.clamp(Size::new(32.0, 16.0))
}
fn paint(&self, ctx: &mut PaintCtx) {
ctx.fill_bounds(Color::WHITE);
}
}
fn white_window() -> Result<(Runtime, sui_core::WindowId)> {
let runtime = Application::new()
.window(WindowBuilder::new().title("Display").root(White))
.build()?;
let window_id = runtime.window_ids()[0];
Ok((runtime, window_id))
}
#[test]
fn headless_windows_render_for_the_display_they_are_given() -> Result<()> {
use sui_render_wgpu::{
DebugCaptureArtifact, DebugCaptureEncoding, DebugCaptureRequest, DebugCaptureStage,
DisplayCapabilities, OutputGamut, OutputStrategy,
};
use sui_runtime::{OutputColorRange, window_output_color_range};
let (mut runtime, window_id) = white_window()?;
let _ = HeadlessPlatform::new().run(&mut runtime)?;
let diagnostics = crate::window_output_diagnostics(window_id).expect("diagnostics");
assert!(matches!(
diagnostics.active_output_strategy,
OutputStrategy::SdrSurface { .. }
));
assert_eq!(diagnostics.output_gamut, OutputGamut::Srgb);
assert_eq!(
window_output_color_range(window_id),
Some(OutputColorRange::Standard)
);
let (mut runtime, window_id) = white_window()?;
let mut platform = HeadlessPlatform::new()
.with_display_capabilities(DisplayCapabilities::hdr(1000.0, 250.0));
let _ = platform.run(&mut runtime)?;
let diagnostics = crate::window_output_diagnostics(window_id).expect("diagnostics");
assert!(
matches!(
diagnostics.active_output_strategy,
OutputStrategy::HdrNativeSurface { .. }
),
"{:?}",
diagnostics.active_output_strategy
);
assert_eq!(
diagnostics.display_capabilities.max_luminance_nits,
Some(1000.0)
);
assert_eq!(
diagnostics.display_capabilities.preferred_primaries,
sui_render_wgpu::DisplayColorPrimaries::Srgb
);
assert_eq!(diagnostics.output_gamut, OutputGamut::Display);
assert_eq!(
window_output_color_range(window_id),
Some(OutputColorRange::HighDynamicRange)
);
let DebugCaptureArtifact::HdrLinearRgbaF32(image) = platform.capture_debug_frame(
window_id,
DebugCaptureRequest {
stage: DebugCaptureStage::FinalComposed,
encoding: DebugCaptureEncoding::Exr,
..Default::default()
},
)?
else {
panic!("an HDR final output is linear floating point");
};
let white = diagnostics.requested_sdr_content_brightness_nits / 80.0;
assert!(
(image.pixels()[0] - white).abs() < 0.01,
"SDR white is {white} in scRGB, got {}",
image.pixels()[0]
);
Ok(())
}
#[test]
fn apps_can_capture_their_window_after_a_redraw() -> Result<()> {
let ticket = Rc::new(RefCell::new(None));
let artifact = Rc::new(RefCell::new(None));
let mut runtime = Application::new()
.window(WindowBuilder::new().title("Capture").root(CaptureProbe {
ticket: Rc::clone(&ticket),
artifact: Rc::clone(&artifact),
}))
.build()?;
let window_id = runtime.window_ids()[0];
let mut platform = HeadlessPlatform::new();
let _ = platform.run(&mut runtime)?;
platform.dispatch_event(
&runtime,
window_id,
Event::Custom(CustomEvent::new("capture")),
)?;
let _ = platform.run(&mut runtime)?;
let result = artifact
.borrow_mut()
.take()
.expect("the capture was delivered");
let sui_render_wgpu::DebugCaptureArtifact::HdrLinearRgbaF32(image) = result? else {
panic!("an HDR intermediate capture is linear floating point");
};
let brightest = image.pixels().iter().copied().fold(0.0_f32, f32::max);
assert!(
brightest > 3.9,
"the capture keeps extended range: {brightest}"
);
Ok(())
}
#[test]
fn animation_frame_request_keeps_headless_pumping_until_completion() -> Result<()> {
let counters = Rc::new(RefCell::new(Counters::default()));
let (mut runtime, window_id) = build_runtime(Rc::clone(&counters));
let mut platform = HeadlessPlatform::new();
let _ = platform.run(&mut runtime)?;
assert_eq!(counters.borrow().paints, 1);
platform.dispatch_event(
&runtime,
window_id,
Event::Custom(CustomEvent::new("arm-animation")),
)?;
let _ = platform.run(&mut runtime)?;
let counters = counters.borrow();
assert_eq!(counters.animation_wakes, 3);
assert_eq!(counters.pending_animation_frames, 0);
assert_eq!(counters.paints, 4);
assert_eq!(counters.last_animation_delta, Some(1.0 / 120.0));
assert!((platform.current_time() - (2.0 / 120.0)).abs() < 1e-9);
Ok(())
}
#[test]
fn feather_width_is_configurable_without_renderer_access() {
let mut platform = HeadlessPlatform::new()
.with_feathering_enabled(false)
.with_feather_width(2.5);
assert!(!platform.feathering_enabled());
assert_eq!(platform.feather_width(), 2.5);
platform.set_feather_width(-4.0);
platform.set_feathering_enabled(true);
assert!(platform.feathering_enabled());
assert_eq!(platform.feather_width(), 0.0);
}
const HDR_BENCH_STEP_S: f64 = 1.0 / 120.0;
const HDR_BENCH_FRAMES: usize = 120;
const HDR_BENCH_WIDTH: f32 = 1280.0;
const HDR_BENCH_HEIGHT: f32 = 720.0;
const HDR_BENCH_COLS: usize = 48;
const HDR_BENCH_ROWS: usize = 27;
struct AnimatedHdrGrid {
phase: f32,
timer: Option<TimerToken>,
}
impl AnimatedHdrGrid {
fn new() -> Self {
Self {
phase: 0.0,
timer: None,
}
}
fn arm(&mut self, ctx: &mut EventCtx) {
if self.timer.is_none() {
self.timer = Some(ctx.schedule_timer_after(HDR_BENCH_STEP_S));
}
}
}
impl Widget for AnimatedHdrGrid {
fn event(&mut self, ctx: &mut EventCtx, event: &Event) {
match event {
Event::Window(WindowEvent::Resized(_)) => {
self.arm(ctx);
ctx.request_paint();
}
Event::Wake(WakeEvent::Timer { token, .. }) if self.timer == Some(*token) => {
self.phase = (self.phase + 0.035) % TAU;
self.timer = Some(ctx.schedule_timer_after(HDR_BENCH_STEP_S));
ctx.request_paint();
ctx.set_handled();
}
_ => {}
}
}
fn measure(&mut self, _ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
constraints.clamp(Size::new(HDR_BENCH_WIDTH, HDR_BENCH_HEIGHT))
}
fn arrange(&mut self, _ctx: &mut ArrangeCtx, _bounds: Rect) {}
fn paint(&self, ctx: &mut PaintCtx) {
ctx.clear(Color::linear_rgba(0.08, 0.09, 0.11, 1.0));
let cell_w = HDR_BENCH_WIDTH / HDR_BENCH_COLS as f32;
let cell_h = HDR_BENCH_HEIGHT / HDR_BENCH_ROWS as f32;
for row in 0..HDR_BENCH_ROWS {
for col in 0..HDR_BENCH_COLS {
let x = col as f32 * cell_w;
let y = row as f32 * cell_h;
let t = self.phase + row as f32 * 0.19 + col as f32 * 0.11;
let r = 0.5 + 3.5 * (0.5 + 0.5 * t.sin());
let g = 0.25 + 2.25 * (0.5 + 0.5 * (t * 1.3).cos());
let b = 0.15 + 1.85 * (0.5 + 0.5 * (t * 0.7 + 1.2).sin());
ctx.fill_rect(
Rect::new(x, y, cell_w + 1.0, cell_h + 1.0),
Color::linear_rgba(r, g, b, 1.0),
);
}
}
}
fn semantics(&self, ctx: &mut SemanticsCtx) {
let mut node = SemanticsNode::new(ctx.widget_id(), SemanticsRole::Window, ctx.bounds());
node.name = Some("Animated HDR Grid".to_string());
ctx.push(node);
}
}
fn mean(values: &[f64]) -> f64 {
if values.is_empty() {
0.0
} else {
values.iter().sum::<f64>() / values.len() as f64
}
}
fn percentile(sorted: &[f64], p: f64) -> f64 {
if sorted.is_empty() {
return 0.0;
}
let rank = ((sorted.len() - 1) as f64 * p).round() as usize;
sorted[rank]
}
#[test]
#[ignore = "hardware Vulkan benchmark; run explicitly on a machine with a working headless GPU path"]
fn steady_state_headless_hdr_grid_benchmark_emits_advancing_frames() -> Result<()> {
let options = WindowRenderOptions::new(true, 1.0)
.with_color_management_mode(WindowColorManagementMode::PreferHdr)
.with_output_color_primaries(WindowOutputColorPrimaries::DisplayP3)
.with_dynamic_range_mode(WindowDynamicRangeMode::HighDynamicRange)
.with_tone_mapping_mode(WindowToneMappingMode::Automatic);
let runtime = Application::new()
.window(
WindowBuilder::new()
.title("Animated HDR Grid")
.root(AnimatedHdrGrid::new()),
)
.build()
.unwrap();
let window_id = runtime.window_ids()[0];
set_window_render_options(window_id, options);
set_window_scene_statistics_detail_mode(window_id, SceneStatisticsDetailMode::Detailed);
let mut runtime = runtime;
let mut platform = HeadlessPlatform::new();
let mut totals = Vec::with_capacity(HDR_BENCH_FRAMES);
let mut commands = Vec::with_capacity(HDR_BENCH_FRAMES);
let mut dirty = Vec::with_capacity(HDR_BENCH_FRAMES);
let mut last_frame = 0u64;
let mut iterations = 0usize;
let max_iterations = 20_000usize;
while totals.len() < HDR_BENCH_FRAMES && iterations < max_iterations {
let _ = platform.pump(&mut runtime)?;
if let Some(snapshot) = window_performance_snapshot(window_id)
&& snapshot.frame_index != last_frame
{
last_frame = snapshot.frame_index;
totals.push(snapshot.total_time_ms);
commands.push(snapshot.scene.command_count as f64);
dirty.push(snapshot.scene.dirty_coverage as f64);
}
platform.advance_time(HDR_BENCH_STEP_S);
iterations += 1;
}
let mut totals_sorted = totals.clone();
totals_sorted.sort_by(|a, b| a.total_cmp(b));
println!("benchmark=steady-state-headless-hdr-grid");
println!("iterations={iterations}");
println!("frames={}", totals.len());
println!("nominal_fps={:.2}", 1.0 / HDR_BENCH_STEP_S);
println!("frame_total_ms_avg={:.3}", mean(&totals));
println!("frame_total_ms_p50={:.3}", percentile(&totals_sorted, 0.50));
println!("frame_total_ms_p95={:.3}", percentile(&totals_sorted, 0.95));
println!(
"frame_total_ms_max={:.3}",
totals_sorted.last().copied().unwrap_or(0.0)
);
println!("command_count_avg={:.1}", mean(&commands));
println!("dirty_coverage_avg={:.2}", mean(&dirty));
assert_eq!(
totals.len(),
HDR_BENCH_FRAMES,
"expected {} benchmark frames, got {} after {} iterations",
HDR_BENCH_FRAMES,
totals.len(),
iterations
);
assert!(mean(&totals) > 0.0, "expected positive frame timings");
Ok(())
}
}