1use mln_util::size::Size;
6
7#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
9pub enum ExportKind {
10 IoSurface,
12 DmaBuf,
14 AndroidHardwareBuffer,
16 DxgiSharedHandle,
18 OpaqueFd,
21}
22
23impl ExportKind {
24 pub const BY_PREFERENCE: [ExportKind; 5] = [
28 ExportKind::IoSurface,
29 ExportKind::AndroidHardwareBuffer,
30 ExportKind::DxgiSharedHandle,
31 ExportKind::DmaBuf,
32 ExportKind::OpaqueFd,
33 ];
34
35 fn bit(self) -> u8 {
36 1 << self as u8
37 }
38}
39
40#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
42pub struct ExportKinds(u8);
43
44impl ExportKinds {
45 pub const NONE: ExportKinds = ExportKinds(0);
46
47 #[must_use]
48 pub fn of(kinds: &[ExportKind]) -> ExportKinds {
49 ExportKinds(kinds.iter().fold(0, |bits, kind| bits | kind.bit()))
50 }
51
52 #[must_use]
53 pub fn contains(self, kind: ExportKind) -> bool {
54 self.0 & kind.bit() != 0
55 }
56
57 #[must_use]
58 pub fn is_empty(self) -> bool {
59 self.0 == 0
60 }
61
62 #[must_use]
63 pub fn with(self, kind: ExportKind) -> ExportKinds {
64 ExportKinds(self.0 | kind.bit())
65 }
66
67 #[must_use]
68 pub fn intersection(self, other: ExportKinds) -> ExportKinds {
69 ExportKinds(self.0 & other.0)
70 }
71
72 pub fn iter(self) -> impl Iterator<Item = ExportKind> {
74 ExportKind::BY_PREFERENCE.into_iter().filter(move |kind| self.contains(*kind))
75 }
76}
77
78pub const DRM_FORMAT_ABGR8888: u32 = u32::from_le_bytes(*b"AB24");
81pub const DRM_FORMAT_ARGB8888: u32 = u32::from_le_bytes(*b"AR24");
84pub const DRM_FORMAT_MOD_LINEAR: u64 = 0;
86pub const DMA_BUF_MAX_PLANES: usize = 4;
88
89#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
91pub struct DmaBufFormat {
92 pub fourcc: u32,
93 pub modifier: u64,
94}
95
96#[derive(Clone, Debug, PartialEq, Eq)]
99pub struct DmaBufChoice {
100 pub fourcc: u32,
101 pub modifiers: Vec<u64>,
102}
103
104pub fn choose_dma_buf_format(
108 formats: &[DmaBufFormat],
109 supported: impl Fn(&DmaBufFormat) -> bool,
110) -> Option<DmaBufChoice> {
111 formats.iter().filter(|candidate| matches!(candidate.fourcc, DRM_FORMAT_ABGR8888 | DRM_FORMAT_ARGB8888)).find_map(
112 |candidate| {
113 let modifiers: Vec<u64> = formats
114 .iter()
115 .filter(|format| format.fourcc == candidate.fourcc && supported(format))
116 .map(|format| format.modifier)
117 .collect();
118 (!modifiers.is_empty()).then_some(DmaBufChoice { fourcc: candidate.fourcc, modifiers })
119 },
120 )
121}
122
123#[derive(Clone, Debug, PartialEq, Eq)]
126pub struct ExportedRing {
127 pub depth: usize,
128 pub kinds: ExportKinds,
130 pub dma_buf_formats: Vec<DmaBufFormat>,
133 pub android_hardware_buffer_usage: u64,
135 pub keep_depth: bool,
138}
139
140#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
142pub struct DmaBufPlane {
143 pub fd: i32,
145 pub offset: u32,
146 pub stride: u32,
147}
148
149#[derive(Clone, Debug, PartialEq, Eq, Hash)]
152#[non_exhaustive]
153pub enum ExportedImage {
154 IoSurface { surface: u64 },
156 DmaBuf { fourcc: u32, modifier: u64, planes: Vec<DmaBufPlane> },
158 AndroidHardwareBuffer { buffer: u64 },
161 OpaqueFd { fd: i32, allocation_size: u64, dedicated: bool, format: u32 },
165 DxgiSharedTexture { handle: u64, format: u32 },
168}
169
170#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
172pub enum DepthStencilFormat {
173 Depth24Stencil8,
175 Depth32FloatStencil8,
177}
178
179impl DepthStencilFormat {
180 pub const BY_PREFERENCE: [DepthStencilFormat; 2] =
182 [DepthStencilFormat::Depth24Stencil8, DepthStencilFormat::Depth32FloatStencil8];
183
184 fn bit(self) -> u8 {
185 1 << self as u8
186 }
187}
188
189#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
191pub struct DepthStencilFormats(u8);
192
193impl DepthStencilFormats {
194 pub const NONE: DepthStencilFormats = DepthStencilFormats(0);
195
196 #[must_use]
197 pub fn of(formats: &[DepthStencilFormat]) -> DepthStencilFormats {
198 DepthStencilFormats(formats.iter().fold(0, |bits, format| bits | format.bit()))
199 }
200
201 #[must_use]
202 pub fn contains(self, format: DepthStencilFormat) -> bool {
203 self.0 & format.bit() != 0
204 }
205
206 #[must_use]
207 pub fn with(self, format: DepthStencilFormat) -> DepthStencilFormats {
208 DepthStencilFormats(self.0 | format.bit())
209 }
210
211 pub fn iter(self) -> impl Iterator<Item = DepthStencilFormat> {
213 DepthStencilFormat::BY_PREFERENCE.into_iter().filter(move |format| self.contains(*format))
214 }
215}
216
217#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
219#[non_exhaustive]
220pub enum NativeTexture {
221 Vulkan { image: u64, view: u64, format: i32, layout: i32 },
224 Metal { texture: u64, pixel_format: u64 },
226 OpenGl { texture: u32, target: u32, internal_format: u32 },
229 WebGpu,
231 D3d12 { resource: u64, format: u32, state: u32 },
234}
235
236#[derive(Clone, Debug, PartialEq, Eq, Hash)]
240pub struct ExportedFrame {
241 pub size: Size,
242 pub image: ExportedImage,
243 pub texture: NativeTexture,
245 pub depth: Option<FrameDepth>,
247}
248
249#[derive(Clone, Debug, PartialEq, Eq, Hash)]
251pub struct FrameDepth {
252 pub texture: NativeTexture,
254 pub export: Option<ExportedImage>,
257}
258
259pub struct Generations<G> {
264 current: G,
265 retired: Vec<(G, Vec<usize>)>,
266}
267
268impl<G> Generations<G> {
269 pub fn new(current: G) -> Self {
270 Generations { current, retired: Vec::new() }
271 }
272
273 pub fn current(&self) -> &G {
274 &self.current
275 }
276
277 pub fn current_mut(&mut self) -> &mut G {
278 &mut self.current
279 }
280
281 pub fn replace(&mut self, next: G, held: &[usize], release: impl FnOnce(G)) {
285 let previous = std::mem::replace(&mut self.current, next);
286 let held: Vec<usize> = held.iter().copied().filter(|slot| self.retired_holding(*slot).is_none()).collect();
289 if held.is_empty() {
290 release(previous);
291 } else {
292 self.retired.push((previous, held));
293 }
294 }
295
296 fn retired_holding(&self, slot: usize) -> Option<usize> {
297 self.retired.iter().position(|(_, held)| held.contains(&slot))
298 }
299
300 pub fn retired(&self, slot: usize) -> Option<&G> {
302 self.retired_holding(slot).map(|index| &self.retired[index].0)
303 }
304
305 pub fn give_back(&mut self, slot: usize, release: impl FnOnce(G)) -> bool {
309 let Some(index) = self.retired_holding(slot) else { return false };
310 let held = &mut self.retired[index].1;
311 held.retain(|held| *held != slot);
312 if held.is_empty() {
313 release(self.retired.remove(index).0);
314 }
315 true
316 }
317
318 pub fn clear(&mut self, mut release: impl FnMut(&mut G)) {
321 for (generation, _) in &mut self.retired {
322 release(generation);
323 }
324 self.retired.clear();
325 release(&mut self.current);
326 }
327}
328
329#[cfg(test)]
330mod tests {
331 use super::*;
332
333 #[test]
334 fn kinds_iterate_by_preference() {
335 let kinds = ExportKinds::of(&[ExportKind::OpaqueFd, ExportKind::DmaBuf, ExportKind::IoSurface]);
336 assert_eq!(kinds.iter().collect::<Vec<_>>(), [ExportKind::IoSurface, ExportKind::DmaBuf, ExportKind::OpaqueFd]);
337 assert_eq!(
338 kinds.intersection(ExportKinds::of(&[ExportKind::OpaqueFd])).iter().next(),
339 Some(ExportKind::OpaqueFd)
340 );
341 assert!(ExportKinds::NONE.iter().next().is_none());
342 }
343
344 #[test]
345 fn a_dma_buf_format_follows_the_hosts_order() {
346 let format = |fourcc, modifier| DmaBufFormat { fourcc, modifier };
347 let nv12 = u32::from_le_bytes(*b"NV12");
349 let formats = [
350 format(nv12, 0),
351 format(DRM_FORMAT_ARGB8888, 7),
352 format(DRM_FORMAT_ABGR8888, 0),
353 format(DRM_FORMAT_ARGB8888, 0),
354 ];
355 assert_eq!(
356 choose_dma_buf_format(&formats, |_| true),
357 Some(DmaBufChoice { fourcc: DRM_FORMAT_ARGB8888, modifiers: vec![7, 0] })
358 );
359 assert_eq!(
361 choose_dma_buf_format(&formats, |format| format.fourcc == DRM_FORMAT_ABGR8888),
362 Some(DmaBufChoice { fourcc: DRM_FORMAT_ABGR8888, modifiers: vec![0] })
363 );
364 assert_eq!(choose_dma_buf_format(&formats, |_| false), None);
365 assert_eq!(choose_dma_buf_format(&[format(nv12, 0)], |_| true), None);
366 }
367
368 #[test]
369 fn a_generation_stays_until_its_held_slots_come_back() {
370 let mut generations = Generations::new("first");
371 let gone = std::cell::RefCell::new(Vec::new());
372 let release = |generation| gone.borrow_mut().push(generation);
373 generations.replace("second", &[0, 2], release);
374 assert_eq!(*generations.current(), "second");
375 assert_eq!(generations.retired(0), Some(&"first"));
376 assert_eq!(generations.retired(1), None);
377 generations.replace("third", &[0, 1], release);
379 assert_eq!(generations.retired(0), Some(&"first"));
380 assert_eq!(generations.retired(1), Some(&"second"));
381 assert!(generations.give_back(0, release));
382 assert_eq!(generations.retired(2), Some(&"first"));
383 assert!(gone.borrow().is_empty());
384 assert!(generations.give_back(2, release));
385 assert_eq!(generations.retired(2), None);
386 assert_eq!(*gone.borrow(), ["first"]);
387 assert!(!generations.give_back(2, release));
388 assert!(generations.give_back(1, release));
389 generations.replace("fourth", &[], release);
391 assert_eq!(generations.retired(0), None);
392 assert_eq!(*gone.borrow(), ["first", "second", "third"]);
393 }
394}