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 replace(&mut self, next: G, held: &[usize]) {
280 let previous = std::mem::replace(&mut self.current, next);
281 let held: Vec<usize> = held.iter().copied().filter(|slot| self.retired_holding(*slot).is_none()).collect();
284 if !held.is_empty() {
285 self.retired.push((previous, held));
286 }
287 }
288
289 fn retired_holding(&self, slot: usize) -> Option<usize> {
290 self.retired.iter().position(|(_, held)| held.contains(&slot))
291 }
292
293 pub fn retired(&self, slot: usize) -> Option<&G> {
295 self.retired_holding(slot).map(|index| &self.retired[index].0)
296 }
297
298 pub fn give_back(&mut self, slot: usize) -> bool {
302 let Some(index) = self.retired_holding(slot) else { return false };
303 let held = &mut self.retired[index].1;
304 held.retain(|held| *held != slot);
305 if held.is_empty() {
306 drop(self.retired.remove(index));
307 }
308 true
309 }
310
311 pub fn clear_retired(&mut self) {
313 self.retired.clear();
314 }
315}
316
317#[cfg(test)]
318mod tests {
319 use super::*;
320
321 #[test]
322 fn kinds_iterate_by_preference() {
323 let kinds = ExportKinds::of(&[ExportKind::OpaqueFd, ExportKind::DmaBuf, ExportKind::IoSurface]);
324 assert_eq!(kinds.iter().collect::<Vec<_>>(), [ExportKind::IoSurface, ExportKind::DmaBuf, ExportKind::OpaqueFd]);
325 assert_eq!(
326 kinds.intersection(ExportKinds::of(&[ExportKind::OpaqueFd])).iter().next(),
327 Some(ExportKind::OpaqueFd)
328 );
329 assert!(ExportKinds::NONE.iter().next().is_none());
330 }
331
332 #[test]
333 fn a_dma_buf_format_follows_the_hosts_order() {
334 let format = |fourcc, modifier| DmaBufFormat { fourcc, modifier };
335 let nv12 = u32::from_le_bytes(*b"NV12");
337 let formats = [
338 format(nv12, 0),
339 format(DRM_FORMAT_ARGB8888, 7),
340 format(DRM_FORMAT_ABGR8888, 0),
341 format(DRM_FORMAT_ARGB8888, 0),
342 ];
343 assert_eq!(
344 choose_dma_buf_format(&formats, |_| true),
345 Some(DmaBufChoice { fourcc: DRM_FORMAT_ARGB8888, modifiers: vec![7, 0] })
346 );
347 assert_eq!(
349 choose_dma_buf_format(&formats, |format| format.fourcc == DRM_FORMAT_ABGR8888),
350 Some(DmaBufChoice { fourcc: DRM_FORMAT_ABGR8888, modifiers: vec![0] })
351 );
352 assert_eq!(choose_dma_buf_format(&formats, |_| false), None);
353 assert_eq!(choose_dma_buf_format(&[format(nv12, 0)], |_| true), None);
354 }
355
356 #[test]
357 fn a_generation_stays_until_its_held_slots_come_back() {
358 let mut generations = Generations::new("first");
359 generations.replace("second", &[0, 2]);
360 assert_eq!(*generations.current(), "second");
361 assert_eq!(generations.retired(0), Some(&"first"));
362 assert_eq!(generations.retired(1), None);
363 generations.replace("third", &[0, 1]);
365 assert_eq!(generations.retired(0), Some(&"first"));
366 assert_eq!(generations.retired(1), Some(&"second"));
367 assert!(generations.give_back(0));
368 assert_eq!(generations.retired(2), Some(&"first"));
369 assert!(generations.give_back(2));
370 assert_eq!(generations.retired(2), None);
371 assert!(!generations.give_back(2));
372 assert!(generations.give_back(1));
373 generations.replace("fourth", &[]);
375 assert_eq!(generations.retired(0), None);
376 }
377}