#![cfg(feature = "vulkan")]
use emblema::{
BackendPreference, Canvas, Color, Context, Error, Extent2D, Paint, PixelFormat, Rect,
};
const SIZE: Extent2D = Extent2D {
width: 16,
height: 16,
};
fn context() -> Option<Context> {
match Context::new(BackendPreference::Vulkan) {
Ok(ctx) => Some(ctx),
Err(e) => {
eprintln!("skipping: no Vulkan device ({e})");
None
}
}
}
fn red() -> emblema::Recording {
let mut canvas = Canvas::new(SIZE);
canvas.clear(Color::WHITE);
canvas
.draw_rect(
Rect::new(0.0, 0.0, 16.0, 16.0),
&Paint::fill(Color::rgba8(255, 0, 0, 255)),
)
.expect("rect");
canvas.finish()
}
#[allow(clippy::panic)]
fn is_lost<T>(result: emblema::Result<T>, what: &str) {
match result {
Err(Error::DeviceLost) => {}
Err(e) => panic!("{what}: refused with {e} rather than as lost"),
Ok(_) => panic!("{what}: went ahead on a lost device"),
}
}
#[test]
fn a_context_marked_lost_refuses_and_a_recovered_one_draws() {
let Some(mut ctx) = context() else { return };
let mut before = ctx
.create_surface(SIZE, PixelFormat::Rgba8Unorm)
.expect("surface");
let mut image = ctx
.create_image(SIZE, PixelFormat::Rgba8Unorm)
.expect("image");
ctx.draw(&mut before, &red()).expect("draw before the loss");
assert!(!ctx.is_lost());
ctx.mark_lost();
assert!(ctx.is_lost());
is_lost(ctx.draw(&mut before, &red()), "a draw on the lost context");
is_lost(
ctx.create_surface(SIZE, PixelFormat::Rgba8Unorm),
"a surface from the lost context",
);
ctx.recover().expect("recover");
assert!(!ctx.is_lost());
is_lost(
ctx.draw(&mut before, &red()),
"a draw into a surface from before",
);
is_lost(ctx.read(&mut before), "reading a surface from before");
let pixels = vec![0u8; (SIZE.width * SIZE.height * 4) as usize];
is_lost(
ctx.write_image(&mut image, &pixels),
"writing an image from before",
);
let mut after = ctx
.create_surface(SIZE, PixelFormat::Rgba8Unorm)
.expect("a surface after recovery");
ctx.draw(&mut after, &red()).expect("a draw after recovery");
let read = ctx.read(&mut after).expect("read after recovery");
assert_eq!(&read[..4], &[255, 0, 0, 255], "the recovered device drew");
assert_eq!(ctx.retired_devices(), 1);
ctx.destroy_surface(before);
assert_eq!(ctx.retired_devices(), 1, "the image still holds it");
ctx.destroy_image(image);
assert_eq!(ctx.retired_devices(), 0, "nothing of it is left");
ctx.destroy_surface(after);
}
#[test]
fn a_context_recovered_twice_draws_each_time() {
let Some(mut ctx) = context() else { return };
for round in 0..2 {
ctx.mark_lost();
ctx.recover()
.unwrap_or_else(|e| panic!("recovery {round}: {e}"));
let mut surface = ctx
.create_surface(SIZE, PixelFormat::Rgba8Unorm)
.expect("surface");
ctx.draw(&mut surface, &red()).expect("draw");
let read = ctx.read(&mut surface).expect("read");
assert_eq!(&read[..4], &[255, 0, 0, 255], "round {round}");
ctx.destroy_surface(surface);
}
assert_eq!(ctx.retired_devices(), 0);
}
#[cfg(feature = "gles")]
#[test]
fn a_gles_context_says_it_cannot_recover() {
let Ok(mut ctx) = Context::new(BackendPreference::Gles) else {
eprintln!("skipping: no GLES context");
return;
};
ctx.mark_lost();
assert!(!ctx.is_lost());
assert!(matches!(ctx.recover(), Err(Error::Unsupported(_))));
assert_eq!(ctx.retired_devices(), 0);
}