diff --git a/crates/ec-hw/src/hw.rs b/crates/ec-hw/src/hw.rs index 42966ef4..49180b56 100644 --- a/crates/ec-hw/src/hw.rs +++ b/crates/ec-hw/src/hw.rs @@ -318,6 +318,32 @@ pub struct DmaBufFrame<'a> { pub order: RgbOrder, } +#[cfg(target_os = "linux")] +impl<'a> DmaBufFrame<'a> { + fn planes(&self) -> [DmaBufPlane; 1] { + [DmaBufPlane { + fd: 0, + offset: self.offset, + pitch: self.stride, + }] + } + + fn as_dmabuf<'b>(&'b self, planes: &'b [DmaBufPlane; 1]) -> DmaBuf<'b> { + DmaBuf { + fds: std::slice::from_ref(&self.fd), + planes, + width: self.width, + height: self.height, + modifier: self.modifier, + format: if self.order == RgbOrder::Bgra { + DmaBufFormat::Bgrx + } else { + DmaBufFormat::Rgbx + }, + } + } +} + /// 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 { @@ -891,29 +917,9 @@ impl HwEncoder { /// Encode one DMA-BUF frame of the source size, box-downscaled to the encoder size. Vulkan only; VA-API has its own PRIME path. #[cfg(target_os = "linux")] - pub fn encode_dmabuf( - &mut self, - frame: &DmaBufFrame, - meta: FrameMetadata, - ) -> Result { - let planes = [DmaBufPlane { - fd: 0, - offset: frame.offset, - pitch: frame.stride, - }]; - let src = DmaBuf { - fds: std::slice::from_ref(&frame.fd), - planes: &planes, - width: frame.width, - height: frame.height, - modifier: frame.modifier, - format: if frame.order == RgbOrder::Bgra { - DmaBufFormat::Bgrx - } else { - DmaBufFormat::Rgbx - }, - }; - self.encode_dma(&src, meta) + pub fn encode_dmabuf(&mut self, frame: &DmaBufFrame, meta: FrameMetadata) -> Result { + let planes = frame.planes(); + self.encode_dma(&frame.as_dmabuf(&planes), meta) } /// Encode one NV12 or P010 DMA-BUF frame of the encoder size. Vulkan only; VA-API has its own PRIME path.