screencapturekit/cm/
iosurface.rs

1//! `IOSurface` - Hardware-accelerated surface
2
3use super::ffi;
4use std::fmt;
5
6pub struct IOSurface(*mut std::ffi::c_void);
7
8impl PartialEq for IOSurface {
9    fn eq(&self, other: &Self) -> bool {
10        self.0 == other.0
11    }
12}
13
14impl Eq for IOSurface {}
15
16impl std::hash::Hash for IOSurface {
17    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
18        unsafe {
19            let hash_value = ffi::io_surface_hash(self.0);
20            hash_value.hash(state);
21        }
22    }
23}
24
25impl IOSurface {
26    pub fn from_raw(ptr: *mut std::ffi::c_void) -> Option<Self> {
27        if ptr.is_null() {
28            None
29        } else {
30            Some(Self(ptr))
31        }
32    }
33
34    /// # Safety
35    /// The caller must ensure the pointer is a valid `IOSurface` pointer.
36    pub unsafe fn from_ptr(ptr: *mut std::ffi::c_void) -> Self {
37        Self(ptr)
38    }
39
40    pub fn as_ptr(&self) -> *mut std::ffi::c_void {
41        self.0
42    }
43
44    pub fn get_width(&self) -> usize {
45        unsafe { ffi::io_surface_get_width(self.0) }
46    }
47
48    pub fn get_height(&self) -> usize {
49        unsafe { ffi::io_surface_get_height(self.0) }
50    }
51
52    pub fn get_bytes_per_row(&self) -> usize {
53        unsafe { ffi::io_surface_get_bytes_per_row(self.0) }
54    }
55}
56
57impl Drop for IOSurface {
58    fn drop(&mut self) {
59        unsafe {
60            ffi::io_surface_release(self.0);
61        }
62    }
63}
64
65impl Clone for IOSurface {
66    fn clone(&self) -> Self {
67        unsafe {
68            let ptr = ffi::io_surface_retain(self.0);
69            Self(ptr)
70        }
71    }
72}
73
74unsafe impl Send for IOSurface {}
75unsafe impl Sync for IOSurface {}
76
77impl fmt::Display for IOSurface {
78    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
79        write!(
80            f,
81            "IOSurface({}x{}, {} bytes/row)",
82            self.get_width(),
83            self.get_height(),
84            self.get_bytes_per_row()
85        )
86    }
87}