ec-hw: dmabuf_modifiers(order) lists the DRM modifiers encode_dmabuf imports for BGRx/RGBx (single memory plane, importable, LINEAR included); dup_fd(RawFd) gives an OwnedFd for callers that only hold a raw descriptor
Browse files at this commit7836ccd5a932ee466db5189a2f17c4aab51f3edcParent: c4111896 changed files +88 -6
- crates/ec-hw/src/hw.rs +14 -0
- crates/ec-hw/src/lib.rs +1 -1
- crates/ec-vk/src/lib.rs +1 -1
- shims/vk-video-ffi/examples/dmabuf_smoke.rs +9 -2
- shims/vk-video-ffi/src/dmabuf.rs +62 -1
- shims/vk-video-ffi/src/lib.rs +1 -1
crates/ec-hw/src/hw.rsmodified+14 -0
| @@ -327,6 +327,20 @@ pub struct DmaBufFrame<'a> { | ||
| 327 | 327 | pub order: RgbOrder, |
| 328 | 328 | } |
| 329 | 329 | |
| 330 | +/// DRM format modifiers [`HwEncoder::encode_dmabuf`] imports for `order`, empty when Vulkan or its DMA-BUF import is unavailable. | |
| 331 | +#[cfg(target_os = "linux")] | |
| 332 | +pub fn dmabuf_modifiers(order: RgbOrder) -> Vec<u64> { | |
| 333 | + vk_gpu().map_or_else(Vec::new, |gpu| { | |
| 334 | + ec_vk::rgb_import_modifiers(&gpu, order == RgbOrder::Bgra) | |
| 335 | + }) | |
| 336 | +} | |
| 337 | + | |
| 338 | +/// Duplicates raw descriptor `fd`, which the caller keeps open for this call, so it can back a [`DmaBufFrame`]; the encoder's import cache keys on the buffer, not the descriptor. | |
| 339 | +#[cfg(target_os = "linux")] | |
| 340 | +pub fn dup_fd(fd: std::os::fd::RawFd) -> Result<std::os::fd::OwnedFd> { | |
| 341 | + Ok(ec_vk::dup_raw_fd(fd)?) | |
| 342 | +} | |
| 343 | + | |
| 330 | 344 | /// One picture for [`HwEncoder::submit`]. |
| 331 | 345 | #[derive(Debug, Clone, Copy)] |
| 332 | 346 | pub enum EncodeInput<'a> { |
crates/ec-hw/src/lib.rsmodified+1 -1
| @@ -91,7 +91,7 @@ pub use error::{Error, Result}; | ||
| 91 | 91 | #[cfg(target_os = "linux")] |
| 92 | 92 | pub use ec_vk::{DmaBufFormat, DmaBufPlane}; |
| 93 | 93 | #[cfg(target_os = "linux")] |
| 94 | -pub use hw::{DmaBufFrame, DmaBufYuvFrame}; | |
| 94 | +pub use hw::{DmaBufFrame, DmaBufYuvFrame, dmabuf_modifiers, dup_fd}; | |
| 95 | 95 | pub use hw::{ |
| 96 | 96 | Backend, Capability, CodecProfile, Direction, EncodeInput, HwDecoder, HwEncoder, RgbFrame, |
| 97 | 97 | RgbOrder, RgbOutput, capabilities, |
crates/ec-vk/src/lib.rsmodified+1 -1
| @@ -14,7 +14,7 @@ mod history; | ||
| 14 | 14 | pub use av1::{Av1Encoder, Av1EncoderConfig}; |
| 15 | 15 | pub use h265::{H265Encoder, H265EncoderConfig}; |
| 16 | 16 | #[cfg(target_os = "linux")] |
| 17 | -pub use vk_video_ffi::{DmaBuf, DmaBufExport, DmaBufFormat, DmaBufPlane, read_dmabuf}; | |
| 17 | +pub use vk_video_ffi::{DmaBuf, DmaBufExport, DmaBufFormat, DmaBufPlane, dup_raw_fd, read_dmabuf, rgb_import_modifiers}; | |
| 18 | 18 | pub use vk_video_ffi::{ |
| 19 | 19 | Av1Cdef, Av1ColorConfig, Av1DecodeConfig, Av1DecodePicture, Av1DecodeSession, Av1DeltaParams, |
| 20 | 20 | Av1FrameHeader, Av1FrameType, Av1GlobalMotion, Av1InterpolationFilter, Av1LoopFilter, |
shims/vk-video-ffi/examples/dmabuf_smoke.rsmodified+9 -2
| @@ -1,4 +1,4 @@ | ||
| 1 | -use std::os::fd::{AsFd, FromRawFd, OwnedFd}; | |
| 1 | +use std::os::fd::{AsFd, AsRawFd, FromRawFd, OwnedFd}; | |
| 2 | 2 | |
| 3 | 3 | use vk_video_ffi::{ |
| 4 | 4 | Av1EncodeConfig, Av1EncodeSession, Av1Picture, Av1Ref, DmaBuf, DmaBufFormat, DmaBufPlane, |
| @@ -513,11 +513,18 @@ fn export_planar( | ||
| 513 | 513 | } |
| 514 | 514 | |
| 515 | 515 | fn run<S: Encode>(gpu: &Gpu, name: &str, host: &[u8], linear: &Export, tiled: &Export) { |
| 516 | + let modifiers = vk_video_ffi::rgb_import_modifiers(gpu, true); | |
| 517 | + println!( | |
| 518 | + "{name} bgrx import modifiers {modifiers:x?}, linear {} tiled {}", | |
| 519 | + modifiers.contains(&linear.modifier), | |
| 520 | + modifiers.contains(&tiled.modifier) | |
| 521 | + ); | |
| 516 | 522 | let cases = [(linear, (W, H)), (tiled, (W, H)), (linear, (W / 2, H / 2))]; |
| 517 | 523 | for (export, size) in cases { |
| 518 | 524 | let mut a = S::open(gpu, size, 8).expect("encoder"); |
| 519 | 525 | let mut b = S::open(gpu, size, 8).expect("encoder"); |
| 520 | - let fds = [export.fd.as_fd()]; | |
| 526 | + let owned = vk_video_ffi::dup_raw_fd(export.fd.as_raw_fd()).expect("dup"); | |
| 527 | + let fds = [owned.as_fd()]; | |
| 521 | 528 | let src = DmaBuf { |
| 522 | 529 | fds: &fds, |
| 523 | 530 | planes: &export.planes, |
shims/vk-video-ffi/src/dmabuf.rsmodified+62 -1
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | use std::ffi::c_int; |
| 2 | -use std::os::fd::{AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd}; | |
| 2 | +use std::os::fd::{AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd}; | |
| 3 | 3 | use std::os::unix::fs::MetadataExt; |
| 4 | 4 | |
| 5 | 5 | use vulkanalia::prelude::v1_3::*; |
| @@ -505,6 +505,67 @@ fn export_modifiers(gpu: &Gpu, format: vk::Format) -> Vec<u64> { | ||
| 505 | 505 | .collect() |
| 506 | 506 | } |
| 507 | 507 | |
| 508 | +/// DRM format modifiers a one-object `Bgrx` (`bgr`) or `Rgbx` DMA-BUF can be imported with, empty without DMA-BUF import. | |
| 509 | +pub fn rgb_import_modifiers(gpu: &Gpu, bgr: bool) -> Vec<u64> { | |
| 510 | + if check(gpu).is_err() { | |
| 511 | + return Vec::new(); | |
| 512 | + } | |
| 513 | + let format = if bgr { | |
| 514 | + vk::Format::B8G8R8A8_UNORM | |
| 515 | + } else { | |
| 516 | + vk::Format::R8G8B8A8_UNORM | |
| 517 | + }; | |
| 518 | + modifier_properties(gpu, format) | |
| 519 | + .iter() | |
| 520 | + .filter(|m| { | |
| 521 | + m.drm_format_modifier_plane_count == 1 | |
| 522 | + && m.drm_format_modifier_tiling_features | |
| 523 | + .contains(vk::FormatFeatureFlags::TRANSFER_SRC) | |
| 524 | + && importable(gpu, format, m.drm_format_modifier) | |
| 525 | + }) | |
| 526 | + .map(|m| m.drm_format_modifier) | |
| 527 | + .collect() | |
| 528 | +} | |
| 529 | + | |
| 530 | +/// Duplicates descriptor `fd`, which the caller keeps open for this call, into an owned one; for callers that only hold a raw descriptor. | |
| 531 | +pub fn dup_raw_fd(fd: RawFd) -> Result<OwnedFd, Error> { | |
| 532 | + if fd < 0 { | |
| 533 | + return Err(Error::Invalid("DMA-BUF descriptor".into())); | |
| 534 | + } | |
| 535 | + unsafe { BorrowedFd::borrow_raw(fd) } | |
| 536 | + .try_clone_to_owned() | |
| 537 | + .map_err(|_| Error::Invalid("DMA-BUF descriptor".into())) | |
| 538 | +} | |
| 539 | + | |
| 540 | +fn importable(gpu: &Gpu, format: vk::Format, modifier: u64) -> bool { | |
| 541 | + let mut external_info = vk::PhysicalDeviceExternalImageFormatInfo::builder() | |
| 542 | + .handle_type(vk::ExternalMemoryHandleTypeFlags::DMA_BUF_EXT) | |
| 543 | + .build(); | |
| 544 | + let mut modifier_info = vk::PhysicalDeviceImageDrmFormatModifierInfoEXT::builder() | |
| 545 | + .drm_format_modifier(modifier) | |
| 546 | + .sharing_mode(vk::SharingMode::EXCLUSIVE) | |
| 547 | + .build(); | |
| 548 | + let info = vk::PhysicalDeviceImageFormatInfo2::builder() | |
| 549 | + .format(format) | |
| 550 | + .type_(vk::ImageType::_2D) | |
| 551 | + .tiling(vk::ImageTiling::DRM_FORMAT_MODIFIER_EXT) | |
| 552 | + .usage(vk::ImageUsageFlags::TRANSFER_SRC) | |
| 553 | + .push_next(&mut external_info) | |
| 554 | + .push_next(&mut modifier_info) | |
| 555 | + .build(); | |
| 556 | + let mut external = vk::ExternalImageFormatProperties::builder().build(); | |
| 557 | + let mut props = vk::ImageFormatProperties2::builder().push_next(&mut external).build(); | |
| 558 | + let ok = unsafe { | |
| 559 | + gpu.instance() | |
| 560 | + .get_physical_device_image_format_properties2(gpu.physical(), &info, &mut props) | |
| 561 | + .is_ok() | |
| 562 | + }; | |
| 563 | + ok && external | |
| 564 | + .external_memory_properties | |
| 565 | + .external_memory_features | |
| 566 | + .contains(vk::ExternalMemoryFeatureFlags::IMPORTABLE) | |
| 567 | +} | |
| 568 | + | |
| 508 | 569 | fn single_plane(gpu: &Gpu, format: vk::Format, modifier: u64) -> bool { |
| 509 | 570 | modifier_properties(gpu, format).iter().any(|m| { |
| 510 | 571 | m.drm_format_modifier == modifier |
shims/vk-video-ffi/src/lib.rsmodified+1 -1
| @@ -28,7 +28,7 @@ pub use av1_decode::{ | ||
| 28 | 28 | pub use av1_encode::{Av1EncodeConfig, Av1EncodeSession, Av1Picture, Av1Ref}; |
| 29 | 29 | pub use session::DPB_SLOTS; |
| 30 | 30 | #[cfg(target_os = "linux")] |
| 31 | -pub use dmabuf::{DmaBuf, DmaBufExport, DmaBufFormat, DmaBufPlane, read_dmabuf}; | |
| 31 | +pub use dmabuf::{DmaBuf, DmaBufExport, DmaBufFormat, DmaBufPlane, dup_raw_fd, read_dmabuf, rgb_import_modifiers}; | |
| 32 | 32 | pub use vp9_decode::{ |
| 33 | 33 | Vp9ColorConfig, Vp9DecodeConfig, Vp9DecodePicture, Vp9DecodeSession, Vp9FrameHeader, |
| 34 | 34 | Vp9InterpolationFilter, Vp9LoopFilter, Vp9Segmentation, |