diff --git a/crates/ec-hw/src/hw.rs b/crates/ec-hw/src/hw.rs index cc997594..8374e7b5 100644 --- a/crates/ec-hw/src/hw.rs +++ b/crates/ec-hw/src/hw.rs @@ -327,6 +327,20 @@ pub struct DmaBufFrame<'a> { pub order: RgbOrder, } +/// DRM format modifiers [`HwEncoder::encode_dmabuf`] imports for `order`, empty when Vulkan or its DMA-BUF import is unavailable. +#[cfg(target_os = "linux")] +pub fn dmabuf_modifiers(order: RgbOrder) -> Vec { + vk_gpu().map_or_else(Vec::new, |gpu| { + ec_vk::rgb_import_modifiers(&gpu, order == RgbOrder::Bgra) + }) +} + +/// 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. +#[cfg(target_os = "linux")] +pub fn dup_fd(fd: std::os::fd::RawFd) -> Result { + Ok(ec_vk::dup_raw_fd(fd)?) +} + /// One picture for [`HwEncoder::submit`]. #[derive(Debug, Clone, Copy)] pub enum EncodeInput<'a> { diff --git a/crates/ec-hw/src/lib.rs b/crates/ec-hw/src/lib.rs index a6d93c28..67e4737b 100644 --- a/crates/ec-hw/src/lib.rs +++ b/crates/ec-hw/src/lib.rs @@ -91,7 +91,7 @@ pub use error::{Error, Result}; #[cfg(target_os = "linux")] pub use ec_vk::{DmaBufFormat, DmaBufPlane}; #[cfg(target_os = "linux")] -pub use hw::{DmaBufFrame, DmaBufYuvFrame}; +pub use hw::{DmaBufFrame, DmaBufYuvFrame, dmabuf_modifiers, dup_fd}; pub use hw::{ Backend, Capability, CodecProfile, Direction, EncodeInput, HwDecoder, HwEncoder, RgbFrame, RgbOrder, RgbOutput, capabilities, diff --git a/crates/ec-vk/src/lib.rs b/crates/ec-vk/src/lib.rs index 954d609d..da2f0b38 100644 --- a/crates/ec-vk/src/lib.rs +++ b/crates/ec-vk/src/lib.rs @@ -14,7 +14,7 @@ mod history; pub use av1::{Av1Encoder, Av1EncoderConfig}; pub use h265::{H265Encoder, H265EncoderConfig}; #[cfg(target_os = "linux")] -pub use vk_video_ffi::{DmaBuf, DmaBufExport, DmaBufFormat, DmaBufPlane, read_dmabuf}; +pub use vk_video_ffi::{DmaBuf, DmaBufExport, DmaBufFormat, DmaBufPlane, dup_raw_fd, read_dmabuf, rgb_import_modifiers}; pub use vk_video_ffi::{ Av1Cdef, Av1ColorConfig, Av1DecodeConfig, Av1DecodePicture, Av1DecodeSession, Av1DeltaParams, Av1FrameHeader, Av1FrameType, Av1GlobalMotion, Av1InterpolationFilter, Av1LoopFilter, diff --git a/shims/vk-video-ffi/examples/dmabuf_smoke.rs b/shims/vk-video-ffi/examples/dmabuf_smoke.rs index 0973976b..16795092 100644 --- a/shims/vk-video-ffi/examples/dmabuf_smoke.rs +++ b/shims/vk-video-ffi/examples/dmabuf_smoke.rs @@ -1,4 +1,4 @@ -use std::os::fd::{AsFd, FromRawFd, OwnedFd}; +use std::os::fd::{AsFd, AsRawFd, FromRawFd, OwnedFd}; use vk_video_ffi::{ Av1EncodeConfig, Av1EncodeSession, Av1Picture, Av1Ref, DmaBuf, DmaBufFormat, DmaBufPlane, @@ -513,11 +513,18 @@ fn export_planar( } fn run(gpu: &Gpu, name: &str, host: &[u8], linear: &Export, tiled: &Export) { + let modifiers = vk_video_ffi::rgb_import_modifiers(gpu, true); + println!( + "{name} bgrx import modifiers {modifiers:x?}, linear {} tiled {}", + modifiers.contains(&linear.modifier), + modifiers.contains(&tiled.modifier) + ); let cases = [(linear, (W, H)), (tiled, (W, H)), (linear, (W / 2, H / 2))]; for (export, size) in cases { let mut a = S::open(gpu, size, 8).expect("encoder"); let mut b = S::open(gpu, size, 8).expect("encoder"); - let fds = [export.fd.as_fd()]; + let owned = vk_video_ffi::dup_raw_fd(export.fd.as_raw_fd()).expect("dup"); + let fds = [owned.as_fd()]; let src = DmaBuf { fds: &fds, planes: &export.planes, diff --git a/shims/vk-video-ffi/src/dmabuf.rs b/shims/vk-video-ffi/src/dmabuf.rs index 5ce6aa53..94238866 100644 --- a/shims/vk-video-ffi/src/dmabuf.rs +++ b/shims/vk-video-ffi/src/dmabuf.rs @@ -1,5 +1,5 @@ use std::ffi::c_int; -use std::os::fd::{AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd}; +use std::os::fd::{AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd}; use std::os::unix::fs::MetadataExt; use vulkanalia::prelude::v1_3::*; @@ -505,6 +505,67 @@ fn export_modifiers(gpu: &Gpu, format: vk::Format) -> Vec { .collect() } +/// DRM format modifiers a one-object `Bgrx` (`bgr`) or `Rgbx` DMA-BUF can be imported with, empty without DMA-BUF import. +pub fn rgb_import_modifiers(gpu: &Gpu, bgr: bool) -> Vec { + if check(gpu).is_err() { + return Vec::new(); + } + let format = if bgr { + vk::Format::B8G8R8A8_UNORM + } else { + vk::Format::R8G8B8A8_UNORM + }; + modifier_properties(gpu, format) + .iter() + .filter(|m| { + m.drm_format_modifier_plane_count == 1 + && m.drm_format_modifier_tiling_features + .contains(vk::FormatFeatureFlags::TRANSFER_SRC) + && importable(gpu, format, m.drm_format_modifier) + }) + .map(|m| m.drm_format_modifier) + .collect() +} + +/// Duplicates descriptor `fd`, which the caller keeps open for this call, into an owned one; for callers that only hold a raw descriptor. +pub fn dup_raw_fd(fd: RawFd) -> Result { + if fd < 0 { + return Err(Error::Invalid("DMA-BUF descriptor".into())); + } + unsafe { BorrowedFd::borrow_raw(fd) } + .try_clone_to_owned() + .map_err(|_| Error::Invalid("DMA-BUF descriptor".into())) +} + +fn importable(gpu: &Gpu, format: vk::Format, modifier: u64) -> bool { + let mut external_info = vk::PhysicalDeviceExternalImageFormatInfo::builder() + .handle_type(vk::ExternalMemoryHandleTypeFlags::DMA_BUF_EXT) + .build(); + let mut modifier_info = vk::PhysicalDeviceImageDrmFormatModifierInfoEXT::builder() + .drm_format_modifier(modifier) + .sharing_mode(vk::SharingMode::EXCLUSIVE) + .build(); + let info = vk::PhysicalDeviceImageFormatInfo2::builder() + .format(format) + .type_(vk::ImageType::_2D) + .tiling(vk::ImageTiling::DRM_FORMAT_MODIFIER_EXT) + .usage(vk::ImageUsageFlags::TRANSFER_SRC) + .push_next(&mut external_info) + .push_next(&mut modifier_info) + .build(); + let mut external = vk::ExternalImageFormatProperties::builder().build(); + let mut props = vk::ImageFormatProperties2::builder().push_next(&mut external).build(); + let ok = unsafe { + gpu.instance() + .get_physical_device_image_format_properties2(gpu.physical(), &info, &mut props) + .is_ok() + }; + ok && external + .external_memory_properties + .external_memory_features + .contains(vk::ExternalMemoryFeatureFlags::IMPORTABLE) +} + fn single_plane(gpu: &Gpu, format: vk::Format, modifier: u64) -> bool { modifier_properties(gpu, format).iter().any(|m| { m.drm_format_modifier == modifier diff --git a/shims/vk-video-ffi/src/lib.rs b/shims/vk-video-ffi/src/lib.rs index 9ea581ef..341d0962 100644 --- a/shims/vk-video-ffi/src/lib.rs +++ b/shims/vk-video-ffi/src/lib.rs @@ -28,7 +28,7 @@ pub use av1_decode::{ pub use av1_encode::{Av1EncodeConfig, Av1EncodeSession, Av1Picture, Av1Ref}; pub use session::DPB_SLOTS; #[cfg(target_os = "linux")] -pub use dmabuf::{DmaBuf, DmaBufExport, DmaBufFormat, DmaBufPlane, read_dmabuf}; +pub use dmabuf::{DmaBuf, DmaBufExport, DmaBufFormat, DmaBufPlane, dup_raw_fd, read_dmabuf, rgb_import_modifiers}; pub use vp9_decode::{ Vp9ColorConfig, Vp9DecodeConfig, Vp9DecodePicture, Vp9DecodeSession, Vp9FrameHeader, Vp9InterpolationFilter, Vp9LoopFilter, Vp9Segmentation,