//! Live GPU tests. Not `#[ignore]`d: this machine has the hardware, and a test //! that never runs is not a test. On a machine with no VA-API device every one //! of these fails at `Display::open`, which is the honest outcome — the crate //! has no meaning without a driver. //! //! Everything lives in one test binary on purpose (a file per feature is a //! serial link-and-run tax). Every test opens its own display and takes //! [`serial`], so the two process-wide measurements below stay meaningful. //! //! Reference: `vainfo` on this box (Mesa 26.1.6, radeonsi gfx1200, RX 9060 XT). use std::rc::Rc; use std::sync::{Mutex, MutexGuard, OnceLock}; use ec_va::caps::{Entrypoint, Profile}; use ec_va::va; use ec_va::{CapReport, Display, Image, Surface, SurfaceExt, SurfaceSpec, create_pool}; /// Runs the tests in this binary one at a time. /// /// Two of them measure process-wide state — `VmRSS` for the surface-pool leak /// check and `/proc/self/fd` for the PRIME export ownership check — and a /// sibling test allocating a 1080p pool on another thread would make both /// meaningless. Serialising is cheaper and more honest than loosening the /// thresholds until concurrency stops mattering. fn serial() -> MutexGuard<'static, ()> { static LOCK: OnceLock> = OnceLock::new(); LOCK.get_or_init(|| Mutex::new(())) .lock() // A panicking test poisons the mutex; the rest are still worth running. .unwrap_or_else(|poisoned| poisoned.into_inner()) } fn display() -> Rc { ec_va::open().expect("no VA-API display; this test needs the GPU") } /// The driver's `VAImageFormat` for `fourcc`. fn image_format(display: &Display, fourcc: u32) -> va::VAImageFormat { display .query_image_formats() .expect("vaQueryImageFormats") .into_iter() .find(|f| f.fourcc == fourcc) .expect("driver lacks the image format") } fn pool(display: &Rc, spec: &SurfaceSpec, n: usize) -> Vec { create_pool(display, spec, n).expect("pool") } fn one(display: &Rc, spec: &SurfaceSpec) -> Surface { pool(display, spec, 1).pop().expect("surface") } // -------------------------------------------------------------------------- // Display #[test] fn caps_match_vainfo_on_this_gpu() { let _serial = serial(); let display = display(); let caps = CapReport::probe(&display).expect("probe"); // Decode: every codec the family hands to hardware. for profile in [ Profile::H264ConstrainedBaseline, Profile::H264Main, Profile::H264High, Profile::HEVCMain, Profile::HEVCMain10, Profile::VP9Profile0, Profile::VP9Profile2, Profile::AV1Profile0, Profile::AV1Profile2, ] { assert!( caps.supports(profile, Entrypoint::VLD), "{} VLD missing from the probe but present in vainfo", profile.name() ); } // Encode: H.264 and HEVC are the two the family ships (AV1 encode exists on // this GPU too, but stays opt-in — see the family plan). for profile in [ Profile::H264ConstrainedBaseline, Profile::H264Main, Profile::H264High, Profile::HEVCMain, Profile::HEVCMain10, ] { assert!( caps.supports(profile, Entrypoint::EncSlice), "{} EncSlice missing from the probe but present in vainfo", profile.name() ); } // Post-processing, used later for colour conversion. assert!(caps.supports(Profile::None, Entrypoint::VideoProc)); // A pair this GPU genuinely lacks must be absent, not silently invented: // vainfo lists VP9 profiles 0 and 2 only. assert!(!caps.supports(Profile::VP9Profile1, Entrypoint::VLD)); assert!(!caps.supports(Profile::VP9Profile3, Entrypoint::VLD)); assert!(!caps.supports(Profile::VP9Profile0, Entrypoint::EncSlice)); // 10-bit decode must advertise a 10-bit RT format, or `ec-hw` would derive // an 8-bit context for Main10 — the incumbent's D2 defect. let main10 = caps .entry(Profile::HEVCMain10, Entrypoint::VLD) .expect("HEVC Main10 VLD"); assert!( main10.supports_rt_format(va::VA_RT_FORMAT_YUV420_10), "HEVC Main10 VLD advertised rt_formats 0x{:08x} without a 10-bit format", main10.rt_formats ); let p010_supported = main10 .surfaces .as_ref() .expect("surface caps") .supports_fourcc(va::VA_FOURCC_P010); assert!(p010_supported, "Main10 cannot allocate P010 surfaces"); // 8-bit decode: NV12 everywhere. let h264 = caps .entry(Profile::H264Main, Entrypoint::VLD) .expect("H264 Main VLD"); assert!(h264.supports_rt_format(va::VA_RT_FORMAT_YUV420)); assert!( h264.surfaces .as_ref() .expect("surface caps") .supports_fourcc(va::VA_FOURCC_NV12) ); // Readback formats: `vaGetImage` must be able to produce NV12 and P010. assert!(caps.image_formats.contains(&va::VA_FOURCC_NV12)); assert!(caps.image_formats.contains(&va::VA_FOURCC_P010)); assert!( caps.profiles_for(Entrypoint::VLD).len() >= 9, "expected at least 9 decode profiles, got {:?}", caps.profiles_for(Entrypoint::VLD) ); } /// `Vpp::convert` end to end: a P010 source surface into an NV12 destination, /// no readback — the conversion `ec-hw`'s `encode_frame` relies on for a /// 10-bit source. #[test] fn vpp_converts_p010_to_nv12() { let _serial = serial(); let display = display(); let source = one(&display, &SurfaceSpec::p010(64, 64)); let dest = Rc::new(one(&display, &SurfaceSpec::nv12(64, 64))); let vpp = ec_va::Vpp::new( &display, va::VA_RT_FORMAT_YUV420 | va::VA_RT_FORMAT_YUV420_10, 64, 64, ) .expect("vpp opens"); let out = vpp.convert(&source, Rc::clone(&dest)).expect("convert"); assert_eq!(out.id(), dest.id()); } // -------------------------------------------------------------------------- // helpers fn open_fd_count() -> usize { std::fs::read_dir("/proc/self/fd") .expect("/proc/self/fd") .count() } /// Export a painted NV12 surface as DMA-BUFs, import the fds as a new surface, /// and read both back: the two must be byte-identical plane for plane. The /// import also has to leave no fds behind once it is dropped. #[test] fn dma_buf_import_reads_back_identical_pixels() { let _serial = serial(); let display = display(); let (width, height) = (256u32, 192u32); let surface = one(&display, &SurfaceSpec::nv12(width, height)); let fmt = image_format(&display, va::VA_FOURCC_NV12); // A pattern that varies by row and column, so a shifted or swapped plane // cannot read back equal by accident. The image is written back to the // surface (vaPutImage) when it drops. { let mut source = Image::create_from(&surface, fmt, (width, height), (width, height)).expect("image"); let va_image = *source.image(); let bytes: &mut [u8] = source.as_mut(); for plane in 0..2usize { let pitch = va_image.pitches[plane] as usize; let off = va_image.offsets[plane] as usize; let rows = if plane == 0 { height } else { height / 2 } as usize; for row in 0..rows { for col in 0..pitch { bytes[off + row * pitch + col] = (row * 7 + col * 3 + plane * 101) as u8; } } } } surface.sync().expect("sync"); let baseline = open_fd_count(); let exported = surface.export().expect("export"); let imported = ec_va::surface::import_prime(&display, &exported).expect("import the exported fds"); imported.sync().expect("sync the import"); let from_original = Image::create_from(&surface, fmt, (width, height), (width, height)).expect("read original"); let from_import = Image::create_from(&imported, fmt, (width, height), (width, height)) .expect("read imported"); let (io, ic) = (*from_original.image(), *from_import.image()); let (original, copy): (&[u8], &[u8]) = (from_original.as_ref(), from_import.as_ref()); for plane in 0..2usize { let rows = if plane == 0 { height } else { height / 2 } as usize; let (po, pc) = (io.pitches[plane] as usize, ic.pitches[plane] as usize); let (oo, oc) = (io.offsets[plane] as usize, ic.offsets[plane] as usize); assert_eq!(po, pc, "plane {plane}: pitches differ"); let want = &original[oo..oo + po * rows]; let got = ©[oc..oc + pc * rows]; assert!( want.iter().any(|&b| b != 0), "plane {plane}: the source read back empty, so the comparison proves nothing" ); assert_eq!( want, got, "plane {plane}: the import reads back different pixels" ); } drop(from_original); drop(from_import); // The export owns its fds: they close with the descriptor, not before. assert!( open_fd_count() >= baseline, "the import closed its fds early" ); drop(imported); drop(exported); assert_eq!( open_fd_count(), baseline, "dropping the import and the export leaked file descriptors" ); }