Skip to main content

apple_cf/cv/
metal_texture_cache.rs

1//! `CVMetalTextureCache` wrapper.
2//!
3#![allow(clippy::missing_panics_doc)]
4
5//! ```rust,no_run
6//! use apple_cf::cv::CVMetalTextureCache;
7//!
8//! if let Some(cache) = CVMetalTextureCache::system_default() {
9//!     cache.flush();
10//! }
11//! ```
12
13use crate::ffi;
14use std::ffi::c_void;
15use std::fmt;
16
17#[link(name = "CoreVideo", kind = "framework")]
18extern "C" {
19    fn CVMetalTextureCacheGetTypeID() -> usize;
20}
21
22/// Owned wrapper around `CVMetalTextureCacheRef`.
23pub struct CVMetalTextureCache(*mut c_void);
24
25impl CVMetalTextureCache {
26    /// Create a texture cache using the system-default Metal device.
27    #[must_use]
28    pub fn system_default() -> Option<Self> {
29        let ptr = unsafe { ffi::cv_metal_texture_cache_create_system_default() };
30        Self::from_raw(ptr)
31    }
32
33    #[must_use]
34    pub fn from_raw(ptr: *mut c_void) -> Option<Self> {
35        if ptr.is_null() {
36            None
37        } else {
38            Some(Self(ptr))
39        }
40    }
41
42    /// Borrow the raw cache pointer.
43    #[must_use]
44    pub const fn as_ptr(&self) -> *mut c_void {
45        self.0
46    }
47
48    /// Core Video type identifier.
49    #[must_use]
50    pub fn type_id() -> usize {
51        unsafe { CVMetalTextureCacheGetTypeID() }
52    }
53
54    /// Flush pending cached textures.
55    pub fn flush(&self) {
56        unsafe { ffi::cv_metal_texture_cache_flush(self.0) };
57    }
58}
59
60impl Clone for CVMetalTextureCache {
61    fn clone(&self) -> Self {
62        let retained = unsafe { ffi::cf_type_retain(self.0) };
63        Self(retained)
64    }
65}
66
67impl Drop for CVMetalTextureCache {
68    fn drop(&mut self) {
69        unsafe { ffi::cf_type_release(self.0) };
70    }
71}
72
73impl PartialEq for CVMetalTextureCache {
74    fn eq(&self, other: &Self) -> bool {
75        self.0 == other.0
76    }
77}
78
79impl Eq for CVMetalTextureCache {}
80
81impl std::hash::Hash for CVMetalTextureCache {
82    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
83        unsafe { ffi::cf_type_hash(self.0) }.hash(state);
84    }
85}
86
87impl fmt::Debug for CVMetalTextureCache {
88    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89        f.debug_struct("CVMetalTextureCache")
90            .field("ptr", &self.0)
91            .field("type_id", &Self::type_id())
92            .finish()
93    }
94}