#![cfg(all(feature = "webgpu", not(target_family = "wasm")))]
use std::any::Any;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::{Duration, Instant};
use mln_engine::RenderMode;
use mln_engine::draw::LendAt;
use mln_engine::map::{MapEvent, MapMode};
use mln_runtime::{
AttachOptions, BackendType, DemandMode, Device, DeviceOptions, ErrorKind, FrameDemand, FrameDisposition,
FrameResult, LogicalExtent, Map, MapOptions, RenderSession, RenderTarget, Runtime, RuntimeEventKind,
};
use mln_style::size::Size;
use crate::support::lends::{self, LendingHost, runtime};
use crate::support::{EventLog, eventually, result_of, wait};
const SIZE: Size = Size { width: 64, height: 64 };
const RED: [u8; 4] = [255, 0, 0, 255];
const STYLE: &str = r##"{"version":8,"transition":{"duration":0,"delay":0},"sources":{},"layers":[{"id":"bg","type":"background","paint":{"background-color":"#ff0000"}}]}"##;
struct Breaking {
broken: Arc<AtomicBool>,
}
impl LendingHost for Breaking {
fn lend(&self, at: LendAt, raw: &mut dyn Any) {
if at == LendAt::Pass
&& self.broken.load(Ordering::SeqCst)
&& let Some(lent) = raw.downcast_mut::<mln_webgpu::CustomLayerPass>()
{
lent.pass.draw(0..3, 0..1);
}
}
}
fn broken_map(runtime: &Runtime, mode: MapMode) -> Option<(Map, RenderSession, Arc<AtomicBool>)> {
let map = red_map(runtime, mode, SIZE);
let broken = Arc::new(AtomicBool::new(false));
wait(lends::add_layer(&map, "breaking", Breaking { broken: Arc::clone(&broken) }));
let session = offscreen_session(&map, SIZE)?;
Some((map, session, broken))
}
fn red_map(runtime: &Runtime, mode: MapMode, size: Size) -> Map {
let options = MapOptions {
map: mln_engine::map::MapOptions { size, map_mode: mode, ..Default::default() },
..Default::default()
};
let (map, created) = runtime.create_map(options).unwrap();
result_of(created).unwrap();
wait(map.set_style_json(STYLE));
map
}
fn offscreen_session(map: &Map, size: Size) -> Option<RenderSession> {
let device = match Device::with_backend(BackendType::WebGPU, DeviceOptions::default()) {
Ok(device) => device,
Err(error) if error.kind == ErrorKind::Unsupported => {
eprintln!("skipped: no WebGPU device: {error}");
return None;
}
Err(error) => panic!("a WebGPU device: {error}"),
};
let target = RenderTarget::Offscreen { size, device: Some(device) };
let (session, attached) = map.attach(target, AttachOptions::default()).unwrap();
result_of(attached).unwrap();
Some(session)
}
fn center_pixel(session: &RenderSession) -> [u8; 4] {
let image = wait(session.read_texture());
let offset = ((SIZE.height / 2 * SIZE.width + SIZE.width / 2) * 4) as usize;
image.data[offset..offset + 4].try_into().unwrap()
}
fn render_errors(log: &EventLog) -> Vec<String> {
let errors = log.events().into_iter().filter_map(|event| match event.kind {
RuntimeEventKind::Map(MapEvent::RenderError(error)) => Some(error.message),
_ => None,
});
errors.collect()
}
#[test]
fn a_still_image_whose_frame_fails_ends_with_the_error() {
let runtime = runtime();
let log = EventLog::install(&runtime);
let Some((map, session, broken)) = broken_map(&runtime, MapMode::Static) else { return };
wait(map.request_still_image());
assert_eq!(RED, center_pixel(&session));
session.drain_frame_results().unwrap();
broken.store(true, Ordering::SeqCst);
let error = result_of(map.request_still_image().unwrap()).expect_err("a still whose frame failed");
assert_eq!(ErrorKind::Failed, error.kind);
assert!(error.message.contains("Submitting the frame failed"), "{error}");
let results = session.drain_frame_results().unwrap();
let still = |result: &FrameResult| result.token.is_none();
assert!(
matches!(&results[..], [result] if still(result) && result.disposition == FrameDisposition::NoUpdate),
"{results:?}"
);
assert_eq!(1, render_errors(&log).len());
broken.store(false, Ordering::SeqCst);
wait(map.request_still_image());
assert_eq!(RED, center_pixel(&session));
}
#[test]
fn a_still_image_whose_frames_always_fail_ends_with_the_error_each_time() {
let runtime = runtime();
let Some((map, session, broken)) = broken_map(&runtime, MapMode::Static) else { return };
broken.store(true, Ordering::SeqCst);
for attempt in 0..2 {
let error = result_of(map.request_still_image().unwrap()).expect_err("a still whose frame failed");
assert_eq!(ErrorKind::Failed, error.kind, "still {attempt}");
assert!(error.message.contains("Submitting the frame failed"), "still {attempt}: {error}");
let results = session.drain_frame_results().unwrap();
assert!(
matches!(&results[..], [result] if result.token.is_none() && result.disposition == FrameDisposition::NoUpdate),
"still {attempt}: {results:?}"
);
}
}
#[test]
fn a_still_image_whose_target_cant_make_its_textures_ends_with_the_error() {
let runtime = runtime();
let size = Size::new(1 << 17, 1);
let map = red_map(&runtime, MapMode::Static, size);
let Some(session) = offscreen_session(&map, size) else { return };
let error = result_of(map.request_still_image().unwrap()).expect_err("a still of a target without textures");
assert_eq!(ErrorKind::InvalidState, error.kind, "{error}");
let results = session.drain_frame_results().unwrap();
assert!(
matches!(&results[..], [result] if result.token.is_none() && result.disposition == FrameDisposition::NoUpdate),
"{results:?}"
);
}
const NO_PIXELS: LogicalExtent = LogicalExtent { size: Size { width: 1, height: 64 }, scale_factor: 0.25 };
#[test]
fn a_still_image_of_a_target_without_pixels_ends_with_an_error() {
let runtime = runtime();
let log = EventLog::install(&runtime);
let map = red_map(&runtime, MapMode::Static, SIZE);
let Some(session) = offscreen_session(&map, SIZE) else { return };
wait(session.resize(NO_PIXELS));
let error = result_of(map.request_still_image().unwrap()).expect_err("a still of a target without pixels");
assert_eq!(ErrorKind::InvalidState, error.kind, "{error}");
assert!(error.message.contains("no pixels"), "{error}");
let results = session.drain_frame_results().unwrap();
assert!(
matches!(&results[..], [result] if result.token.is_none() && result.disposition == FrameDisposition::NoUpdate),
"{results:?}"
);
assert!(render_errors(&log).is_empty(), "{:?}", render_errors(&log));
}
#[test]
fn a_continuous_map_on_a_target_without_pixels_isnt_ready() {
let runtime = runtime();
let log = EventLog::install(&runtime);
let map = red_map(&runtime, MapMode::Continuous, SIZE);
let Some(session) = offscreen_session(&map, SIZE) else { return };
wait(session.resize(NO_PIXELS));
session.drain_frame_results().unwrap();
for _ in 0..3 {
session.demand_frame(FrameDemand::new(DemandMode::Force, 0)).unwrap();
let mut results = Vec::new();
assert!(eventually(|| {
results.extend(session.drain_frame_results().unwrap());
!results.is_empty()
}));
assert!(results.iter().all(|result| result.disposition == FrameDisposition::TargetNotReady), "{results:?}");
}
assert!(render_errors(&log).is_empty(), "{:?}", render_errors(&log));
}
#[test]
fn a_continuous_map_whose_frames_fail_reports_once_and_draws_again_once() {
let runtime = runtime();
let log = EventLog::install(&runtime);
let Some((map, session, broken)) = broken_map(&runtime, MapMode::Continuous) else { return };
let frames = |within: Duration| {
let mut results: Vec<FrameResult> = Vec::new();
let start = Instant::now();
while start.elapsed() < within {
session.demand_frame(FrameDemand::new(DemandMode::WaitForUpdate, 0)).unwrap();
let mut taken = Vec::new();
eventually(|| {
taken.extend(session.drain_frame_results().unwrap());
!taken.is_empty() || start.elapsed() >= within
});
results.extend(taken);
}
results
};
session.demand_frame(FrameDemand::new(DemandMode::Force, 0)).unwrap();
assert!(eventually(|| session.drain_frame_results().unwrap().iter().any(|result| result.mode == RenderMode::Full)));
broken.store(true, Ordering::SeqCst);
wait(map.request_repaint());
let failing = frames(Duration::from_secs(1));
let drawn = failing.iter().filter(|result| result.disposition == FrameDisposition::Rendered).count();
assert_eq!(2, drawn, "the failed frame and the one that drew it again: {failing:?}");
assert_eq!(1, render_errors(&log).len(), "{:?}", render_errors(&log));
assert!(map.snapshot().unwrap().fully_loaded);
broken.store(false, Ordering::SeqCst);
wait(map.request_repaint());
let recovered = frames(Duration::from_millis(500));
assert!(
recovered
.iter()
.any(|result| result.disposition == FrameDisposition::Rendered && result.mode == RenderMode::Full),
"{recovered:?}"
);
assert_eq!(RED, center_pixel(&session));
assert_eq!(1, render_errors(&log).len());
}