Skip to main content

apple_mps/generated/
core.rs

1use crate::ffi;
2use crate::image::ImageRegion;
3use core::ffi::c_void;
4use core::ptr;
5
6macro_rules! opaque_generated_handle {
7    ($name:ident) => {
8        pub struct $name {
9            ptr: *mut c_void,
10        }
11
12        unsafe impl Send for $name {}
13        unsafe impl Sync for $name {}
14
15        impl Drop for $name {
16            fn drop(&mut self) {
17                if !self.ptr.is_null() {
18                    unsafe { ffi::mps_object_release(self.ptr) };
19                    self.ptr = ptr::null_mut();
20                }
21            }
22        }
23
24        impl $name {
25            #[must_use]
26            pub const fn as_ptr(&self) -> *mut c_void {
27                self.ptr
28            }
29
30            /// # Safety
31            ///
32            /// `ptr` must be a valid Objective-C object of the matching MPS type or protocol.
33            #[must_use]
34            pub unsafe fn retained_from_raw(ptr: *mut c_void) -> Option<Self> {
35                let retained = unsafe { ffi::mps_object_retain(ptr) };
36                if retained.is_null() {
37                    None
38                } else {
39                    Some(Self { ptr: retained })
40                }
41            }
42        }
43    };
44}
45
46macro_rules! raw_value_type {
47    ($name:ident, $raw:ty) => {
48        #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
49        #[repr(transparent)]
50        pub struct $name(pub $raw);
51
52        impl $name {
53            #[must_use]
54            pub const fn from_raw(raw: $raw) -> Self {
55                Self(raw)
56            }
57
58            #[must_use]
59            pub const fn as_raw(self) -> $raw {
60                self.0
61            }
62        }
63
64        impl From<$raw> for $name {
65            fn from(value: $raw) -> Self {
66                Self(value)
67            }
68        }
69
70        impl From<$name> for $raw {
71            fn from(value: $name) -> Self {
72                value.0
73            }
74        }
75    };
76}
77
78#[derive(Clone, Copy, Debug, Default, PartialEq)]
79pub struct Origin {
80    pub x: f64,
81    pub y: f64,
82    pub z: f64,
83}
84
85#[derive(Clone, Copy, Debug, Default, PartialEq)]
86pub struct Size {
87    pub width: f64,
88    pub height: f64,
89    pub depth: f64,
90}
91
92#[derive(Clone, Copy, Debug, Default, PartialEq)]
93pub struct Region {
94    pub origin: Origin,
95    pub size: Size,
96}
97
98#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
99pub struct DimensionSlice {
100    pub start: usize,
101    pub length: usize,
102}
103
104#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
105pub struct CustomKernelArgumentCount {
106    pub destination_texture_count: usize,
107    pub source_texture_count: usize,
108    pub broadcast_texture_count: usize,
109}
110
111#[must_use]
112pub fn rect_no_clip() -> ImageRegion {
113    let mut x = 0;
114    let mut y = 0;
115    let mut z = 0;
116    let mut width = 0;
117    let mut height = 0;
118    let mut depth = 0;
119    unsafe { ffi::mps_rect_no_clip(&mut x, &mut y, &mut z, &mut width, &mut height, &mut depth) };
120    ImageRegion::new(x, y, z, width, height, depth)
121}
122
123raw_value_type!(AliasingStrategy, usize);
124raw_value_type!(CustomKernelIndex, usize);
125raw_value_type!(DeviceCapsValues, usize);
126raw_value_type!(FloatDataTypeBit, usize);
127raw_value_type!(FloatDataTypeShift, usize);
128opaque_generated_handle!(Kernel);
129opaque_generated_handle!(KeyedUnarchiver);
130opaque_generated_handle!(DeviceProvider);
131opaque_generated_handle!(HeapProvider);