use std::ffi::c_void;
use std::ptr::NonNull;
use objc2_core_foundation::CFRetained;
use objc2_io_surface::IOSurfaceRef;
use objc2_metal::{
MTLDevice as _, MTLPixelFormat, MTLStorageMode, MTLTextureDescriptor, MTLTextureType,
MTLTextureUsage,
};
pub struct IoSurfaceFrame {
surface: CFRetained<IOSurfaceRef>,
width: u32,
height: u32,
format: wgpu::TextureFormat,
}
impl core::fmt::Debug for IoSurfaceFrame {
fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
formatter
.debug_struct("IoSurfaceFrame")
.field("width", &self.width)
.field("height", &self.height)
.field("format", &self.format)
.finish_non_exhaustive()
}
}
impl IoSurfaceFrame {
#[must_use]
pub unsafe fn retain(
surface: NonNull<c_void>,
width: u32,
height: u32,
format: wgpu::TextureFormat,
) -> Self {
let surface = unsafe { CFRetained::retain(surface.cast::<IOSurfaceRef>()) };
Self {
surface,
width,
height,
format,
}
}
#[must_use]
pub const fn width(&self) -> u32 {
self.width
}
#[must_use]
pub const fn height(&self) -> u32 {
self.height
}
#[must_use]
pub const fn format(&self) -> wgpu::TextureFormat {
self.format
}
#[must_use]
pub fn import(&self, device: &wgpu::Device) -> wgpu::Texture {
let descriptor = wgpu::TextureDescriptor {
label: Some("wgpu_external_frame_io_surface"),
size: wgpu::Extent3d {
width: self.width,
height: self.height,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: self.format,
usage: wgpu::TextureUsages::COPY_SRC,
view_formats: &[],
};
let metal_format = match self.format {
wgpu::TextureFormat::Bgra8Unorm => MTLPixelFormat::BGRA8Unorm,
wgpu::TextureFormat::Rgba8Unorm => MTLPixelFormat::RGBA8Unorm,
format => panic!("an IOSurface cannot carry the wgpu format {format:?}"),
};
let hal_texture = objc2::rc::autoreleasepool(|_| {
let hal_device = unsafe {
device
.as_hal::<wgpu::hal::api::Metal>()
.expect("IOSurface import requires a Metal device")
};
let raw_device = hal_device.raw_device();
let metal_descriptor = MTLTextureDescriptor::new();
unsafe {
metal_descriptor
.setWidth(usize::try_from(self.width).expect("IOSurface width exceeds usize"));
metal_descriptor.setHeight(
usize::try_from(self.height).expect("IOSurface height exceeds usize"),
);
}
metal_descriptor.setTextureType(MTLTextureType::Type2D);
metal_descriptor.setPixelFormat(metal_format);
metal_descriptor.setUsage(MTLTextureUsage::ShaderRead);
metal_descriptor.setStorageMode(if raw_device.hasUnifiedMemory() {
MTLStorageMode::Shared
} else {
MTLStorageMode::Managed
});
let texture = raw_device
.newTextureWithDescriptor_iosurface_plane(&metal_descriptor, &self.surface, 0)
.expect("Metal rejected the IOSurface import");
unsafe {
<wgpu::hal::api::Metal as wgpu::hal::Api>::Device::texture_from_raw(
texture,
descriptor.format,
MTLTextureType::Type2D,
1,
1,
wgpu::hal::CopyExtent {
width: self.width,
height: self.height,
depth: 1,
},
)
}
});
unsafe { device.create_texture_from_hal::<wgpu::hal::api::Metal>(hal_texture, &descriptor) }
}
}