emblema 0.7.1

A Flutter-class 2D renderer: Vulkan and GLES backends, WSI and DRM/KMS presentation.
Documentation
//! A context through a lost device, from the facade: marked lost, refusing,
//! recovered, drawing again, and refusing what it made before.
//!
//! A driver cannot be made to lose its device on demand, so the loss here is
//! the context's own mark, which every call answers to exactly as it answers
//! to a loss the driver reported. A real one, on a board, is stage 4 of #375.

#![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()
}

// A test helper, and a panic is how a test fails; the lint is for code that
// ships, which it cannot tell this from outside a `#[test]` function.
#[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());

    // What was made before is the lost device's: refused, by the call that
    // would use it, rather than drawn into on a device that never made it.
    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",
    );

    // A surface made now draws, and draws the picture asked for.
    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");

    // The lost device is held for what the caller made from it, and goes
    // once that is destroyed.
    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;
    };
    // GLES does not track a loss, so marking one is ignored and recovering
    // is refused by name rather than pretending to have made anything again.
    ctx.mark_lost();
    assert!(!ctx.is_lost());
    assert!(matches!(ctx.recover(), Err(Error::Unsupported(_))));
    assert_eq!(ctx.retired_devices(), 0);
}