mtl_gpu/texture/
shared_handle.rs1use std::ffi::c_void;
4use std::ptr::NonNull;
5
6use mtl_foundation::Referencing;
7use mtl_sys::{msg_send_0, sel};
8
9#[repr(transparent)]
13pub struct SharedTextureHandle(pub(crate) NonNull<c_void>);
14
15impl SharedTextureHandle {
16 #[inline]
22 pub unsafe fn from_raw(ptr: *mut c_void) -> Option<Self> {
23 NonNull::new(ptr).map(Self)
24 }
25
26 #[inline]
28 pub fn as_raw(&self) -> *mut c_void {
29 self.0.as_ptr()
30 }
31
32 pub fn device(&self) -> crate::Device {
36 unsafe {
37 let ptr: *mut c_void = msg_send_0(self.as_ptr(), sel!(device));
38 let _: *mut c_void = msg_send_0(ptr, sel!(retain));
39 crate::Device::from_raw(ptr).expect("shared texture handle has no device")
40 }
41 }
42
43 pub fn label(&self) -> Option<String> {
47 unsafe {
48 let ptr: *mut c_void = msg_send_0(self.as_ptr(), sel!(label));
49 if ptr.is_null() {
50 return None;
51 }
52 let utf8_ptr: *const std::ffi::c_char =
53 mtl_sys::msg_send_0(ptr as *const c_void, sel!(UTF8String));
54 if utf8_ptr.is_null() {
55 return None;
56 }
57 let c_str = std::ffi::CStr::from_ptr(utf8_ptr);
58 Some(c_str.to_string_lossy().into_owned())
59 }
60 }
61}
62
63impl Clone for SharedTextureHandle {
64 fn clone(&self) -> Self {
65 unsafe {
66 msg_send_0::<*mut c_void>(self.as_ptr(), sel!(retain));
67 }
68 Self(self.0)
69 }
70}
71
72impl Drop for SharedTextureHandle {
73 fn drop(&mut self) {
74 unsafe {
75 msg_send_0::<()>(self.as_ptr(), sel!(release));
76 }
77 }
78}
79
80impl Referencing for SharedTextureHandle {
81 #[inline]
82 fn as_ptr(&self) -> *const c_void {
83 self.0.as_ptr()
84 }
85}
86
87unsafe impl Send for SharedTextureHandle {}
88unsafe impl Sync for SharedTextureHandle {}