| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | use std::os::fd::AsRawFd; |
| 13 | use std::rc::Rc; |
| 14 | use std::sync::{Mutex, MutexGuard, OnceLock}; |
| 15 | use std::time::Duration; |
| 16 | |
| 17 | use ec_va::caps::{Entrypoint, Profile}; |
| 18 | use ec_va::va; |
| 19 | use ec_va::{CapReport, Config, Display, Image, Surface, SurfaceExt, SurfaceSpec, create_pool}; |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | fn serial() -> MutexGuard<'static, ()> { |
| 29 | static LOCK: OnceLock<Mutex<()>> = OnceLock::new(); |
| 30 | LOCK.get_or_init(|| Mutex::new(())) |
| 31 | .lock() |
| 32 | |
| 33 | .unwrap_or_else(|poisoned| poisoned.into_inner()) |
| 34 | } |
| 35 | |
| 36 | fn display() -> Rc<Display> { |
| 37 | ec_va::open().expect("no VA-API display; this test needs the GPU") |
| 38 | } |
| 39 | |
| 40 | |
| 41 | fn image_format(display: &Display, fourcc: u32) -> va::VAImageFormat { |
| 42 | display |
| 43 | .query_image_formats() |
| 44 | .expect("vaQueryImageFormats") |
| 45 | .into_iter() |
| 46 | .find(|f| f.fourcc == fourcc) |
| 47 | .expect("driver lacks the image format") |
| 48 | } |
| 49 | |
| 50 | fn pool(display: &Rc<Display>, spec: &SurfaceSpec, n: usize) -> Vec<Surface> { |
| 51 | create_pool(display, spec, n).expect("pool") |
| 52 | } |
| 53 | |
| 54 | fn one(display: &Rc<Display>, spec: &SurfaceSpec) -> Surface { |
| 55 | pool(display, spec, 1).pop().expect("surface") |
| 56 | } |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | #[test] |
| 66 | fn display_opens_on_a_render_node() { |
| 67 | let _serial = serial(); |
| 68 | let display = display(); |
| 69 | let vendor = ec_va::display::vendor(&display).expect("vendor string"); |
| 70 | assert!(!vendor.is_empty(), "driver reported an empty vendor string"); |
| 71 | assert!( |
| 72 | !display |
| 73 | .query_config_profiles() |
| 74 | .expect("profiles") |
| 75 | .is_empty() |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | #[test] |
| 84 | fn caps_match_vainfo_on_this_gpu() { |
| 85 | let _serial = serial(); |
| 86 | let display = display(); |
| 87 | let caps = CapReport::probe(&display).expect("probe"); |
| 88 | |
| 89 | |
| 90 | for profile in [ |
| 91 | Profile::H264ConstrainedBaseline, |
| 92 | Profile::H264Main, |
| 93 | Profile::H264High, |
| 94 | Profile::HEVCMain, |
| 95 | Profile::HEVCMain10, |
| 96 | Profile::VP9Profile0, |
| 97 | Profile::VP9Profile2, |
| 98 | Profile::AV1Profile0, |
| 99 | Profile::AV1Profile2, |
| 100 | ] { |
| 101 | assert!( |
| 102 | caps.supports(profile, Entrypoint::VLD), |
| 103 | "{} VLD missing from the probe but present in vainfo", |
| 104 | profile.name() |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | |
| 110 | for profile in [ |
| 111 | Profile::H264ConstrainedBaseline, |
| 112 | Profile::H264Main, |
| 113 | Profile::H264High, |
| 114 | Profile::HEVCMain, |
| 115 | Profile::HEVCMain10, |
| 116 | ] { |
| 117 | assert!( |
| 118 | caps.supports(profile, Entrypoint::EncSlice), |
| 119 | "{} EncSlice missing from the probe but present in vainfo", |
| 120 | profile.name() |
| 121 | ); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | assert!(caps.supports(Profile::None, Entrypoint::VideoProc)); |
| 126 | |
| 127 | |
| 128 | |
| 129 | assert!(!caps.supports(Profile::VP9Profile1, Entrypoint::VLD)); |
| 130 | assert!(!caps.supports(Profile::VP9Profile3, Entrypoint::VLD)); |
| 131 | assert!(!caps.supports(Profile::VP9Profile0, Entrypoint::EncSlice)); |
| 132 | |
| 133 | |
| 134 | |
| 135 | let main10 = caps |
| 136 | .entry(Profile::HEVCMain10, Entrypoint::VLD) |
| 137 | .expect("HEVC Main10 VLD"); |
| 138 | assert!( |
| 139 | main10.supports_rt_format(va::VA_RT_FORMAT_YUV420_10), |
| 140 | "HEVC Main10 VLD advertised rt_formats 0x{:08x} without a 10-bit format", |
| 141 | main10.rt_formats |
| 142 | ); |
| 143 | let p010_supported = main10 |
| 144 | .surfaces |
| 145 | .as_ref() |
| 146 | .expect("surface caps") |
| 147 | .supports_fourcc(va::VA_FOURCC_P010); |
| 148 | assert!(p010_supported, "Main10 cannot allocate P010 surfaces"); |
| 149 | |
| 150 | |
| 151 | let h264 = caps |
| 152 | .entry(Profile::H264Main, Entrypoint::VLD) |
| 153 | .expect("H264 Main VLD"); |
| 154 | assert!(h264.supports_rt_format(va::VA_RT_FORMAT_YUV420)); |
| 155 | assert!( |
| 156 | h264.surfaces |
| 157 | .as_ref() |
| 158 | .expect("surface caps") |
| 159 | .supports_fourcc(va::VA_FOURCC_NV12) |
| 160 | ); |
| 161 | |
| 162 | |
| 163 | assert!(caps.image_formats.contains(&va::VA_FOURCC_NV12)); |
| 164 | assert!(caps.image_formats.contains(&va::VA_FOURCC_P010)); |
| 165 | |
| 166 | assert!( |
| 167 | caps.profiles_for(Entrypoint::VLD).len() >= 9, |
| 168 | "expected at least 9 decode profiles, got {:?}", |
| 169 | caps.profiles_for(Entrypoint::VLD) |
| 170 | ); |
| 171 | } |
| 172 | |
| 173 | #[test] |
| 174 | fn caps_report_driver_legal_surface_sizes() { |
| 175 | let _serial = serial(); |
| 176 | let display = display(); |
| 177 | let caps = CapReport::probe(&display).expect("probe"); |
| 178 | let entry = caps |
| 179 | .entry(Profile::H264Main, Entrypoint::VLD) |
| 180 | .expect("H264 Main VLD"); |
| 181 | let surfaces = entry.surfaces.as_ref().expect("surface caps"); |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | assert_eq!(surfaces.min_width, Some(64)); |
| 187 | assert_eq!(surfaces.min_height, Some(64)); |
| 188 | assert!(surfaces.max_width.unwrap() >= 4096); |
| 189 | assert!( |
| 190 | !surfaces.allows(16, 16), |
| 191 | "16x16 must be refused by the caps" |
| 192 | ); |
| 193 | assert!(surfaces.allows(1920, 1088)); |
| 194 | |
| 195 | |
| 196 | assert!( |
| 197 | surfaces.memory_types & va::VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2 != 0, |
| 198 | "memory types 0x{:08x} lack DRM_PRIME_2", |
| 199 | surfaces.memory_types |
| 200 | ); |
| 201 | } |
| 202 | |
| 203 | #[test] |
| 204 | fn unsupported_pair_is_refused_by_the_driver() { |
| 205 | let _serial = serial(); |
| 206 | |
| 207 | |
| 208 | let display = display(); |
| 209 | let caps = CapReport::probe(&display).expect("probe"); |
| 210 | assert!(!caps.supports(Profile::VP9Profile1, Entrypoint::VLD)); |
| 211 | |
| 212 | let err = Config::new(&display, Profile::VP9Profile1, Entrypoint::VLD, &[]) |
| 213 | .err() |
| 214 | .expect("VP9 Profile1 decode does not exist on this GPU"); |
| 215 | let status = err.status().expect("a VAStatus-carrying error"); |
| 216 | assert!( |
| 217 | status == va::VA_STATUS_ERROR_UNSUPPORTED_PROFILE as i32 |
| 218 | || status == va::VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT as i32, |
| 219 | "unexpected refusal: {err}" |
| 220 | ); |
| 221 | assert!(err.to_string().contains("vaCreateConfig"), "{err}"); |
| 222 | } |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | #[test] |
| 229 | fn surface_pool_create_and_destroy_is_leak_free() { |
| 230 | let _serial = serial(); |
| 231 | let display = display(); |
| 232 | let spec = SurfaceSpec::nv12(64, 64); |
| 233 | |
| 234 | |
| 235 | for _ in 0..4 { |
| 236 | let p = pool(&display, &spec, 16); |
| 237 | assert_eq!(p.len(), 16); |
| 238 | drop(p); |
| 239 | } |
| 240 | |
| 241 | let before = vm_rss_kb(); |
| 242 | for _ in 0..100 { |
| 243 | let p = pool(&display, &spec, 16); |
| 244 | |
| 245 | let mut ids: Vec<_> = p.iter().map(|s| s.id()).collect(); |
| 246 | ids.sort_unstable(); |
| 247 | ids.dedup(); |
| 248 | assert_eq!(ids.len(), 16, "vaCreateSurfaces returned duplicate ids"); |
| 249 | assert!(ids.iter().all(|&id| id != va::VA_INVALID_SURFACE)); |
| 250 | drop(p); |
| 251 | } |
| 252 | let after = vm_rss_kb(); |
| 253 | |
| 254 | |
| 255 | |
| 256 | let delta_kb = after.saturating_sub(before); |
| 257 | assert!( |
| 258 | delta_kb < 50 * 1024, |
| 259 | "VmRSS grew {delta_kb} kB over 100 create/destroy cycles" |
| 260 | ); |
| 261 | } |
| 262 | |
| 263 | #[test] |
| 264 | fn derive_image_maps_and_reads_back() { |
| 265 | let _serial = serial(); |
| 266 | let display = display(); |
| 267 | let surface = one(&display, &SurfaceSpec::nv12(64, 64)); |
| 268 | let mut image = Image::derive_from(&surface, (64, 64)).expect("vaDeriveImage"); |
| 269 | |
| 270 | let va_image = *image.image(); |
| 271 | assert_eq!(va_image.format.fourcc, va::VA_FOURCC_NV12); |
| 272 | assert_eq!(image.display_resolution(), (64, 64)); |
| 273 | assert!(image.is_derived()); |
| 274 | assert_eq!(va_image.num_planes, 2, "NV12 is luma + interleaved chroma"); |
| 275 | let pitch = va_image.pitches[0] as usize; |
| 276 | assert!(pitch >= 64, "luma pitch {pitch} is narrower than the image"); |
| 277 | assert!(va_image.pitches[1] > 0, "chroma pitch missing"); |
| 278 | let luma_off = va_image.offsets[0] as usize; |
| 279 | let chroma_off = va_image.offsets[1] as usize; |
| 280 | assert!( |
| 281 | chroma_off >= luma_off + pitch * 64, |
| 282 | "luma plane must be at least pitch * height" |
| 283 | ); |
| 284 | |
| 285 | { |
| 286 | let bytes: &mut [u8] = image.as_mut(); |
| 287 | assert!( |
| 288 | bytes.len() > chroma_off, |
| 289 | "mapping shorter than chroma plane" |
| 290 | ); |
| 291 | for (i, byte) in bytes[luma_off..luma_off + pitch * 64] |
| 292 | .iter_mut() |
| 293 | .enumerate() |
| 294 | { |
| 295 | *byte = (i % 251) as u8; |
| 296 | } |
| 297 | let luma = &bytes[luma_off..]; |
| 298 | assert_eq!(luma[0], 0); |
| 299 | assert_eq!(luma[250], 250); |
| 300 | assert_eq!(luma[251], 0, "write did not survive within the mapping"); |
| 301 | } |
| 302 | drop(image); |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | let again = Image::derive_from(&surface, (64, 64)).expect("second vaDeriveImage"); |
| 311 | assert_eq!( |
| 312 | again.image().pitches[0] as usize, |
| 313 | pitch, |
| 314 | "second mapping has a different geometry" |
| 315 | ); |
| 316 | assert!(!AsRef::<[u8]>::as_ref(&again).is_empty()); |
| 317 | } |
| 318 | |
| 319 | #[test] |
| 320 | fn created_image_reads_a_surface_back() { |
| 321 | let _serial = serial(); |
| 322 | let display = display(); |
| 323 | let surface = one(&display, &SurfaceSpec::nv12(64, 64)); |
| 324 | |
| 325 | { |
| 326 | let mut derived = Image::derive_from(&surface, (64, 64)).expect("derive"); |
| 327 | let luma_off = derived.image().offsets[0] as usize; |
| 328 | let pitch = derived.image().pitches[0] as usize; |
| 329 | let bytes: &mut [u8] = derived.as_mut(); |
| 330 | for byte in &mut bytes[luma_off..luma_off + pitch * 64] { |
| 331 | *byte = 0x5a; |
| 332 | } |
| 333 | } |
| 334 | surface.sync().expect("sync"); |
| 335 | |
| 336 | let fmt = image_format(&display, va::VA_FOURCC_NV12); |
| 337 | let image = Image::create_from(&surface, fmt, (64, 64), (64, 64)).expect("vaGetImage"); |
| 338 | let luma_off = image.image().offsets[0] as usize; |
| 339 | let bytes: &[u8] = image.as_ref(); |
| 340 | assert!( |
| 341 | bytes[luma_off..luma_off + 64].iter().all(|&b| b == 0x5a), |
| 342 | "vaGetImage did not return the painted luma" |
| 343 | ); |
| 344 | drop(image); |
| 345 | |
| 346 | |
| 347 | let fmt = image_format(&display, va::VA_FOURCC_NV12); |
| 348 | let refused = Image::create_from(&surface, fmt, (128, 128), (128, 128)); |
| 349 | assert!( |
| 350 | refused.is_err(), |
| 351 | "region larger than the surface must be refused" |
| 352 | ); |
| 353 | } |
| 354 | |
| 355 | #[test] |
| 356 | fn prime_export_owns_and_releases_its_descriptors() { |
| 357 | let _serial = serial(); |
| 358 | let display = display(); |
| 359 | let surface = one(&display, &SurfaceSpec::nv12(64, 64)); |
| 360 | surface |
| 361 | .sync_timeout(Duration::from_secs(5)) |
| 362 | .expect("sync before export"); |
| 363 | |
| 364 | let baseline = open_fd_count(); |
| 365 | let exported = surface.export().expect("vaExportSurfaceHandle"); |
| 366 | |
| 367 | assert_eq!(exported.fourcc, va::VA_FOURCC_NV12); |
| 368 | assert_eq!((exported.width, exported.height), (64, 64)); |
| 369 | assert!(!exported.objects.is_empty(), "no DRM objects exported"); |
| 370 | assert!(!exported.layers.is_empty(), "no layers exported"); |
| 371 | for object in &exported.objects { |
| 372 | assert!(object.fd.as_raw_fd() >= 0); |
| 373 | assert!(object.size > 0); |
| 374 | } |
| 375 | for layer in &exported.layers { |
| 376 | assert!(!layer.planes.is_empty()); |
| 377 | for &(object_index, _offset, pitch) in &layer.planes { |
| 378 | assert!((object_index as usize) < exported.objects.len()); |
| 379 | assert!(pitch > 0); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | let with_export = open_fd_count(); |
| 384 | assert_eq!( |
| 385 | with_export - baseline, |
| 386 | exported.objects.len(), |
| 387 | "exported fd count does not match the descriptor" |
| 388 | ); |
| 389 | drop(exported); |
| 390 | assert_eq!( |
| 391 | open_fd_count(), |
| 392 | baseline, |
| 393 | "dropping the export leaked file descriptors" |
| 394 | ); |
| 395 | } |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | #[test] |
| 402 | fn picture_typestate_walks_the_va_protocol() { |
| 403 | let _serial = serial(); |
| 404 | let display = display(); |
| 405 | let config = Config::new(&display, Profile::H264Main, Entrypoint::VLD, &[]).expect("config"); |
| 406 | let targets = pool(&display, &SurfaceSpec::nv12(1920, 1088), 2); |
| 407 | let context = ec_va::create_context(&display, &config, 1920, 1088, Some(&targets), true) |
| 408 | .expect("context"); |
| 409 | let targets: Vec<Rc<Surface>> = targets.into_iter().map(Rc::new).collect(); |
| 410 | |
| 411 | |
| 412 | |
| 413 | let picture = ec_va::Picture::new(0, Rc::clone(&context), Rc::clone(&targets[0])); |
| 414 | let picture = picture.begin::<()>().expect("vaBeginPicture"); |
| 415 | assert_eq!(picture.surface::<()>().id(), targets[0].id()); |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | let failed = picture.render().and_then(|p| p.end()); |
| 423 | let err = failed |
| 424 | .err() |
| 425 | .expect("empty submission is rejected by radeonsi"); |
| 426 | assert_eq!( |
| 427 | err.va_status(), |
| 428 | va::VA_STATUS_ERROR_INVALID_CONTEXT as i32, |
| 429 | "{err}" |
| 430 | ); |
| 431 | |
| 432 | |
| 433 | |
| 434 | let picture = ec_va::Picture::new(0, Rc::clone(&context), Rc::clone(&targets[1])) |
| 435 | .begin::<()>() |
| 436 | .expect("second vaBeginPicture after a failed end"); |
| 437 | drop(picture); |
| 438 | assert!(display.query_vendor_string().is_ok()); |
| 439 | } |
| 440 | |
| 441 | #[test] |
| 442 | fn buffers_map_unmap_and_destroy() { |
| 443 | let _serial = serial(); |
| 444 | let display = display(); |
| 445 | let config = |
| 446 | Config::new(&display, Profile::H264Main, Entrypoint::EncSlice, &[]).expect("config"); |
| 447 | let targets = pool(&display, &SurfaceSpec::nv12(1920, 1088), 1); |
| 448 | let context = ec_va::create_context(&display, &config, 1920, 1088, Some(&targets), true) |
| 449 | .expect("context"); |
| 450 | |
| 451 | |
| 452 | const CODED_SIZE: usize = 1 << 20; |
| 453 | let coded = context |
| 454 | .create_enc_coded(CODED_SIZE) |
| 455 | .expect("vaCreateBuffer"); |
| 456 | assert_ne!(coded.id(), va::VA_INVALID_ID); |
| 457 | { |
| 458 | let mapped = va::MappedCodedBuffer::new(&coded).expect("vaMapBuffer"); |
| 459 | |
| 460 | assert!(mapped.iter().all(|s| s.buf.is_empty())); |
| 461 | } |
| 462 | |
| 463 | drop(va::MappedCodedBuffer::new(&coded).expect("second vaMapBuffer after unmap")); |
| 464 | |
| 465 | |
| 466 | let buffer = ec_va::picture::buffer( |
| 467 | &context, |
| 468 | va::BufferType::EncMiscParameter(va::EncMiscParameter::FrameRate( |
| 469 | va::EncMiscParameterFrameRate::new(30, 0), |
| 470 | )), |
| 471 | ) |
| 472 | .expect("vaCreateBuffer from a typed parameter"); |
| 473 | drop(buffer); |
| 474 | drop(coded); |
| 475 | } |
| 476 | |
| 477 | #[test] |
| 478 | fn context_keeps_its_dependencies_alive() { |
| 479 | let _serial = serial(); |
| 480 | |
| 481 | |
| 482 | |
| 483 | let display = display(); |
| 484 | let config = Config::new(&display, Profile::H264Main, Entrypoint::VLD, &[]).expect("config"); |
| 485 | let targets = pool(&display, &SurfaceSpec::nv12(1920, 1088), 2); |
| 486 | let context = ec_va::create_context(&display, &config, 1920, 1088, Some(&targets), true) |
| 487 | .expect("context"); |
| 488 | let target = Rc::new(targets.into_iter().next().expect("target")); |
| 489 | drop(config); |
| 490 | |
| 491 | let picture = ec_va::Picture::new(0, Rc::clone(&context), target) |
| 492 | .begin::<()>() |
| 493 | .expect("begin"); |
| 494 | drop(picture); |
| 495 | drop(context); |
| 496 | assert!(display.query_vendor_string().is_ok()); |
| 497 | } |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | #[test] |
| 507 | fn vpp_filters_probe() { |
| 508 | let _serial = serial(); |
| 509 | let display = display(); |
| 510 | let targets = pool(&display, &SurfaceSpec::nv12(1920, 1080), 2); |
| 511 | let vpp = ec_va::Vpp::new(&display, va::VA_RT_FORMAT_YUV420, 1920, 1080).expect("vpp opens"); |
| 512 | let mut it = targets.into_iter(); |
| 513 | let src = it.next().expect("src"); |
| 514 | let dst = Rc::new(it.next().expect("dst")); |
| 515 | vpp.convert(&src, dst).expect("vpp pipeline runs"); |
| 516 | } |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | #[test] |
| 522 | fn vpp_converts_p010_to_nv12() { |
| 523 | let _serial = serial(); |
| 524 | let display = display(); |
| 525 | let source = one(&display, &SurfaceSpec::p010(64, 64)); |
| 526 | let dest = Rc::new(one(&display, &SurfaceSpec::nv12(64, 64))); |
| 527 | let vpp = ec_va::Vpp::new( |
| 528 | &display, |
| 529 | va::VA_RT_FORMAT_YUV420 | va::VA_RT_FORMAT_YUV420_10, |
| 530 | 64, |
| 531 | 64, |
| 532 | ) |
| 533 | .expect("vpp opens"); |
| 534 | let out = vpp.convert(&source, Rc::clone(&dest)).expect("convert"); |
| 535 | assert_eq!(out.id(), dest.id()); |
| 536 | } |
| 537 | |
| 538 | |
| 539 | |
| 540 | |
| 541 | |
| 542 | fn vm_rss_kb() -> u64 { |
| 543 | let status = std::fs::read_to_string("/proc/self/status").expect("/proc/self/status"); |
| 544 | for line in status.lines() { |
| 545 | if let Some(rest) = line.strip_prefix("VmRSS:") { |
| 546 | return rest |
| 547 | .split_whitespace() |
| 548 | .next() |
| 549 | .and_then(|v| v.parse().ok()) |
| 550 | .expect("VmRSS value"); |
| 551 | } |
| 552 | } |
| 553 | panic!("VmRSS not found in /proc/self/status"); |
| 554 | } |
| 555 | |
| 556 | fn open_fd_count() -> usize { |
| 557 | std::fs::read_dir("/proc/self/fd") |
| 558 | .expect("/proc/self/fd") |
| 559 | .count() |
| 560 | } |
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | #[test] |
| 566 | fn dma_buf_import_reads_back_identical_pixels() { |
| 567 | let _serial = serial(); |
| 568 | let display = display(); |
| 569 | let (width, height) = (256u32, 192u32); |
| 570 | let surface = one(&display, &SurfaceSpec::nv12(width, height)); |
| 571 | let fmt = image_format(&display, va::VA_FOURCC_NV12); |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | { |
| 577 | let mut source = |
| 578 | Image::create_from(&surface, fmt, (width, height), (width, height)).expect("image"); |
| 579 | let va_image = *source.image(); |
| 580 | let bytes: &mut [u8] = source.as_mut(); |
| 581 | for plane in 0..2usize { |
| 582 | let pitch = va_image.pitches[plane] as usize; |
| 583 | let off = va_image.offsets[plane] as usize; |
| 584 | let rows = if plane == 0 { height } else { height / 2 } as usize; |
| 585 | for row in 0..rows { |
| 586 | for col in 0..pitch { |
| 587 | bytes[off + row * pitch + col] = (row * 7 + col * 3 + plane * 101) as u8; |
| 588 | } |
| 589 | } |
| 590 | } |
| 591 | } |
| 592 | surface.sync().expect("sync"); |
| 593 | |
| 594 | let baseline = open_fd_count(); |
| 595 | let exported = surface.export().expect("export"); |
| 596 | let imported = |
| 597 | ec_va::surface::import_prime(&display, &exported).expect("import the exported fds"); |
| 598 | imported.sync().expect("sync the import"); |
| 599 | |
| 600 | let from_original = |
| 601 | Image::create_from(&surface, fmt, (width, height), (width, height)).expect("read original"); |
| 602 | let from_import = Image::create_from(&imported, fmt, (width, height), (width, height)) |
| 603 | .expect("read imported"); |
| 604 | |
| 605 | let (io, ic) = (*from_original.image(), *from_import.image()); |
| 606 | let (original, copy): (&[u8], &[u8]) = (from_original.as_ref(), from_import.as_ref()); |
| 607 | for plane in 0..2usize { |
| 608 | let rows = if plane == 0 { height } else { height / 2 } as usize; |
| 609 | let (po, pc) = (io.pitches[plane] as usize, ic.pitches[plane] as usize); |
| 610 | let (oo, oc) = (io.offsets[plane] as usize, ic.offsets[plane] as usize); |
| 611 | assert_eq!(po, pc, "plane {plane}: pitches differ"); |
| 612 | let want = &original[oo..oo + po * rows]; |
| 613 | let got = ©[oc..oc + pc * rows]; |
| 614 | assert!( |
| 615 | want.iter().any(|&b| b != 0), |
| 616 | "plane {plane}: the source read back empty, so the comparison proves nothing" |
| 617 | ); |
| 618 | assert_eq!( |
| 619 | want, got, |
| 620 | "plane {plane}: the import reads back different pixels" |
| 621 | ); |
| 622 | } |
| 623 | drop(from_original); |
| 624 | drop(from_import); |
| 625 | |
| 626 | |
| 627 | assert!( |
| 628 | open_fd_count() >= baseline, |
| 629 | "the import closed its fds early" |
| 630 | ); |
| 631 | drop(imported); |
| 632 | drop(exported); |
| 633 | assert_eq!( |
| 634 | open_fd_count(), |
| 635 | baseline, |
| 636 | "dropping the import and the export leaked file descriptors" |
| 637 | ); |
| 638 | } |