1use crate::{AsRaw, BufferObject, Ptr};
2use std::error;
3use std::fmt;
4use std::marker::PhantomData;
5
6pub struct Surface<T: 'static> {
8 ffi: Ptr<ffi::gbm_surface>,
10 _device: Ptr<ffi::gbm_device>,
11 _bo_userdata: PhantomData<T>,
12}
13
14impl<T: 'static> fmt::Debug for Surface<T> {
15 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
16 f.debug_struct("Surface")
17 .field("ptr", &format_args!("{:p}", &self.ffi))
18 .field("device", &format_args!("{:p}", &self._device))
19 .finish()
20 }
21}
22
23#[derive(Debug, Clone, Copy, PartialEq, Eq)]
25pub struct FrontBufferError;
26
27impl fmt::Display for FrontBufferError {
28 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29 write!(f, "Unknown error")
30 }
31}
32
33impl error::Error for FrontBufferError {}
34
35impl<T: 'static> Surface<T> {
36 pub fn has_free_buffers(&self) -> bool {
44 unsafe { ffi::gbm_surface_has_free_buffers(*self.ffi) != 0 }
45 }
46
47 pub unsafe fn lock_front_buffer(&self) -> Result<BufferObject<T>, FrontBufferError> {
60 let buffer_ptr = ffi::gbm_surface_lock_front_buffer(*self.ffi);
61 if !buffer_ptr.is_null() {
62 let surface_ptr = self.ffi.clone();
63 let buffer = BufferObject {
64 ffi: Ptr::new(buffer_ptr, move |ptr| {
65 ffi::gbm_surface_release_buffer(*surface_ptr, ptr);
66 }),
67 _device: self._device.clone(),
68 _userdata: std::marker::PhantomData,
69 };
70 Ok(buffer)
71 } else {
72 Err(FrontBufferError)
73 }
74 }
75
76 pub(crate) unsafe fn new(
77 ffi: *mut ffi::gbm_surface,
78 device: Ptr<ffi::gbm_device>,
79 ) -> Surface<T> {
80 Surface {
81 ffi: Ptr::new(ffi, |ptr| ffi::gbm_surface_destroy(ptr)),
82 _device: device,
83 _bo_userdata: PhantomData,
84 }
85 }
86}
87
88impl<T: 'static> AsRaw<ffi::gbm_surface> for Surface<T> {
89 fn as_raw(&self) -> *const ffi::gbm_surface {
90 *self.ffi
91 }
92}