drm/control/
dumbbuffer.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//!
//! # DumbBuffer
//!
//! Memory-supported, slow, but easy & cross-platform buffer implementation
//!

use crate::buffer;

use std::borrow::{Borrow, BorrowMut};
use std::ops::{Deref, DerefMut};

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
/// Slow, but generic [`buffer::Buffer`] implementation
pub struct DumbBuffer {
    pub(crate) size: (u32, u32),
    pub(crate) length: usize,
    pub(crate) format: buffer::DrmFourcc,
    pub(crate) pitch: u32,
    pub(crate) handle: buffer::Handle,
}

/// Mapping of a [`DumbBuffer`]
pub struct DumbMapping<'a> {
    pub(crate) _phantom: core::marker::PhantomData<&'a ()>,
    pub(crate) map: &'a mut [u8],
}

impl AsRef<[u8]> for DumbMapping<'_> {
    fn as_ref(&self) -> &[u8] {
        self.map
    }
}

impl AsMut<[u8]> for DumbMapping<'_> {
    fn as_mut(&mut self) -> &mut [u8] {
        self.map
    }
}

impl Borrow<[u8]> for DumbMapping<'_> {
    fn borrow(&self) -> &[u8] {
        self.map
    }
}

impl BorrowMut<[u8]> for DumbMapping<'_> {
    fn borrow_mut(&mut self) -> &mut [u8] {
        self.map
    }
}

impl Deref for DumbMapping<'_> {
    type Target = [u8];

    fn deref(&self) -> &Self::Target {
        self.map
    }
}

impl DerefMut for DumbMapping<'_> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.map
    }
}

impl<'a> Drop for DumbMapping<'a> {
    fn drop(&mut self) {
        unsafe {
            rustix::mm::munmap(self.map.as_mut_ptr() as *mut _, self.map.len())
                .expect("Unmap failed");
        }
    }
}

impl buffer::Buffer for DumbBuffer {
    fn size(&self) -> (u32, u32) {
        self.size
    }
    fn format(&self) -> buffer::DrmFourcc {
        self.format
    }
    fn pitch(&self) -> u32 {
        self.pitch
    }
    fn handle(&self) -> buffer::Handle {
        self.handle
    }
}