Skip to main content

moq_video/
output.rs

1//! Where a frame's pixels live once an operation hands it back.
2
3use crate::{Error, Surface};
4
5/// The representation a decoder, capture, or scaler hands frames back in.
6///
7/// A choice, not a promise of GPU residency. [`Native`](Self::Native) lets a
8/// hardware backend keep its picture where it made it, which on a software
9/// backend is CPU memory anyway. [`Cpu`](Self::Cpu) is the portable exit: the
10/// result is always [`Surface::I420`](crate::Surface::I420), downloaded when it
11/// had to be. `#[non_exhaustive]` so a narrower choice (a specific device, a
12/// shareable handle) can be added without breaking a `match`.
13#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
14#[non_exhaustive]
15pub enum Output {
16	/// The backend's natural representation: a GPU surface from a hardware
17	/// decoder or scaler, CPU pixels from a software one. The default, since it
18	/// never pays for a download nobody asked for. Match on each
19	/// [`Surface`](crate::Surface) rather than assuming either.
20	#[default]
21	Native,
22	/// CPU-resident [`I420`](crate::I420), whatever produced it. A hardware
23	/// decode pays a download per picture; a backend that can decode straight
24	/// to system memory does that instead. A surface with no download, such as
25	/// a non-NV12 pixel buffer, is an error (see
26	/// [`Surface::into_i420`](crate::Surface::into_i420)).
27	Cpu,
28	/// What `render::Renderer` draws: a GPU surface it imports on this
29	/// platform stays one, and anything else is converted as [`Cpu`](Self::Cpu)
30	/// converts it, an error included. Without the `render` feature that is
31	/// every frame.
32	///
33	/// The renderer refuses the GPU surfaces it cannot import, so a source
34	/// feeding one asks for this instead of guessing which backend it will get.
35	/// Says nothing about the device, which may be unable to import at all:
36	/// `render::output` picks this only for a device that can, and
37	/// [`Cpu`](Self::Cpu) otherwise.
38	Render,
39}
40
41impl Output {
42	/// What this asks of `surface`: [`Native`](Self::Native) or [`Cpu`](Self::Cpu).
43	pub(crate) fn resolve(self, surface: &Surface) -> Self {
44		match self {
45			Self::Render if renders(surface) => Self::Native,
46			Self::Render => Self::Cpu,
47			other => other,
48		}
49	}
50
51	/// `surface` in this representation, downloaded if it has to be.
52	pub(crate) fn convert(self, surface: Surface) -> Result<Surface, Error> {
53		Ok(match self.resolve(&surface) {
54			Self::Cpu => Surface::I420(surface.into_i420()?),
55			_ => surface,
56		})
57	}
58}
59
60fn renders(surface: &Surface) -> bool {
61	#[cfg(feature = "render")]
62	{
63		crate::render::imports(surface)
64	}
65	#[cfg(not(feature = "render"))]
66	{
67		let _ = surface;
68		false
69	}
70}
71
72#[cfg(all(test, target_os = "linux", feature = "render"))]
73mod tests {
74	use super::*;
75	use crate::Size;
76	use crate::frame::{DmaBuf, DmaBufLayout, DmaBufPlane, DrmFormat};
77
78	fn dmabuf(format: DrmFormat, planes: usize) -> Surface {
79		let planes = (0..planes)
80			.map(|index| DmaBufPlane::new(index as u32 * 64, 8))
81			.collect();
82		let layout = DmaBufLayout::new(format, Size::new(8, 8), planes);
83		let fd = std::fs::File::open("/dev/zero").expect("open /dev/zero").into();
84		Surface::DmaBuf(DmaBuf::new(fd, layout, ()).expect("a well-formed DMA-BUF"))
85	}
86
87	/// `Render` keeps exactly the DMA-BUFs the renderer imports: a format it
88	/// has no shape for would be refused there, so it must arrive on the CPU.
89	#[test]
90	fn render_keeps_only_importable_dma_bufs() {
91		for (format, planes) in [(DrmFormat::NV12, 2), (DrmFormat::YUV420, 3), (DrmFormat::XRGB8888, 1)] {
92			assert_eq!(
93				Output::Render.resolve(&dmabuf(format, planes)),
94				Output::Native,
95				"{format:?}"
96			);
97		}
98		let yuyv = dmabuf(DrmFormat::from_bytes(*b"YUYV"), 1);
99		assert_eq!(Output::Render.resolve(&yuyv), Output::Cpu);
100		assert_eq!(Output::Native.resolve(&yuyv), Output::Native, "Native never downloads");
101	}
102}