use std::ffi::c_void;
use std::ptr::NonNull;
use std::sync::Arc;
use objc2::runtime::ProtocolObject;
use objc2_core_foundation::CFRetained;
use objc2_io_surface::IOSurfaceRef;
use objc2_metal::{
MTLDevice, MTLPixelFormat, MTLStorageMode, MTLTextureDescriptor, MTLTextureType,
MTLTextureUsage,
};
use sync_wrapper::SyncWrapper;
use crate::YcbcrRange;
const PIXEL_FORMAT_420V: u32 = u32::from_be_bytes(*b"420v");
const PIXEL_FORMAT_420F: u32 = u32::from_be_bytes(*b"420f");
const PIXEL_FORMAT_X420: u32 = u32::from_be_bytes(*b"x420");
const PIXEL_FORMAT_XF20: u32 = u32::from_be_bytes(*b"xf20");
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum PackedFormat {
Bgra8,
Rgba8,
}
impl PackedFormat {
#[must_use]
pub const fn texture_format(self) -> wgpu::TextureFormat {
match self {
Self::Bgra8 => wgpu::TextureFormat::Bgra8Unorm,
Self::Rgba8 => wgpu::TextureFormat::Rgba8Unorm,
}
}
const fn metal_format(self) -> MTLPixelFormat {
match self {
Self::Bgra8 => MTLPixelFormat::BGRA8Unorm,
Self::Rgba8 => MTLPixelFormat::RGBA8Unorm,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum YcbcrDepth {
Eight,
Ten,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Ycbcr420Plane {
Luma,
Chroma,
}
impl Ycbcr420Plane {
#[must_use]
pub const fn index(self) -> usize {
match self {
Self::Luma => 0,
Self::Chroma => 1,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Ycbcr420Format {
pub depth: YcbcrDepth,
pub range: YcbcrRange,
}
impl Ycbcr420Format {
#[must_use]
pub const fn from_pixel_format(code: u32) -> Option<Self> {
let (depth, range) = match code {
PIXEL_FORMAT_420V => (YcbcrDepth::Eight, YcbcrRange::Video),
PIXEL_FORMAT_420F => (YcbcrDepth::Eight, YcbcrRange::Full),
PIXEL_FORMAT_X420 => (YcbcrDepth::Ten, YcbcrRange::Video),
PIXEL_FORMAT_XF20 => (YcbcrDepth::Ten, YcbcrRange::Full),
_ => return None,
};
Some(Self { depth, range })
}
#[must_use]
pub const fn pixel_format(self) -> u32 {
match (self.depth, self.range) {
(YcbcrDepth::Eight, YcbcrRange::Video) => PIXEL_FORMAT_420V,
(YcbcrDepth::Eight, YcbcrRange::Full) => PIXEL_FORMAT_420F,
(YcbcrDepth::Ten, YcbcrRange::Video) => PIXEL_FORMAT_X420,
(YcbcrDepth::Ten, YcbcrRange::Full) => PIXEL_FORMAT_XF20,
}
}
#[must_use]
pub const fn texture_format(self, plane: Ycbcr420Plane) -> wgpu::TextureFormat {
match (self.depth, plane) {
(YcbcrDepth::Eight, Ycbcr420Plane::Luma) => wgpu::TextureFormat::R8Unorm,
(YcbcrDepth::Eight, Ycbcr420Plane::Chroma) => wgpu::TextureFormat::Rg8Unorm,
(YcbcrDepth::Ten, Ycbcr420Plane::Luma) => wgpu::TextureFormat::R16Unorm,
(YcbcrDepth::Ten, Ycbcr420Plane::Chroma) => wgpu::TextureFormat::Rg16Unorm,
}
}
#[must_use]
pub const fn required_features(self) -> wgpu::Features {
match self.depth {
YcbcrDepth::Eight => wgpu::Features::empty(),
YcbcrDepth::Ten => wgpu::Features::TEXTURE_FORMAT_16BIT_NORM,
}
}
const fn metal_format(self, plane: Ycbcr420Plane) -> MTLPixelFormat {
match (self.depth, plane) {
(YcbcrDepth::Eight, Ycbcr420Plane::Luma) => MTLPixelFormat::R8Unorm,
(YcbcrDepth::Eight, Ycbcr420Plane::Chroma) => MTLPixelFormat::RG8Unorm,
(YcbcrDepth::Ten, Ycbcr420Plane::Luma) => MTLPixelFormat::R16Unorm,
(YcbcrDepth::Ten, Ycbcr420Plane::Chroma) => MTLPixelFormat::RG16Unorm,
}
}
}
pub struct PackedIoSurfaceFrame {
surface: CFRetained<IOSurfaceRef>,
format: PackedFormat,
width: u32,
height: u32,
owner: Option<SharedOwner>,
}
impl core::fmt::Debug for PackedIoSurfaceFrame {
fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
formatter
.debug_struct("PackedIoSurfaceFrame")
.field("format", &self.format)
.field("width", &self.width)
.field("height", &self.height)
.field("owned", &self.owner.is_some())
.finish_non_exhaustive()
}
}
impl PackedIoSurfaceFrame {
#[must_use]
pub unsafe fn retain(surface: NonNull<c_void>, format: PackedFormat) -> Self {
let surface = unsafe { CFRetained::retain(surface.cast::<IOSurfaceRef>()) };
assert_eq!(
surface.plane_count(),
0,
"a packed IOSurface frame needs a non-planar surface"
);
assert_eq!(
surface.bytes_per_element(),
4,
"a packed IOSurface frame needs 4-byte elements for {format:?}"
);
let (width, height) = extent(surface.width(), surface.height());
Self {
surface,
format,
width,
height,
owner: None,
}
}
#[must_use]
pub fn with_owner(mut self, owner: impl Send + 'static) -> Self {
SharedOwner::attach(&mut self.owner, owner);
self
}
#[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) -> PackedFormat {
self.format
}
#[must_use]
pub fn import(&self, device: &wgpu::Device) -> wgpu::Texture {
import_plane(
device,
&self.surface,
&PlaneImport {
label: "wgpu_external_frame_io_surface",
index: 0,
format: self.format.texture_format(),
metal_format: self.format.metal_format(),
width: self.width,
height: self.height,
},
self.owner.as_ref(),
)
}
}
pub struct Ycbcr420IoSurfaceFrame {
surface: CFRetained<IOSurfaceRef>,
format: Ycbcr420Format,
extents: [(u32, u32); 2],
owner: Option<SharedOwner>,
}
impl core::fmt::Debug for Ycbcr420IoSurfaceFrame {
fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
formatter
.debug_struct("Ycbcr420IoSurfaceFrame")
.field("format", &self.format)
.field("luma", &self.extents[0])
.field("chroma", &self.extents[1])
.field("owned", &self.owner.is_some())
.finish_non_exhaustive()
}
}
impl Ycbcr420IoSurfaceFrame {
#[must_use]
pub unsafe fn retain(surface: NonNull<c_void>) -> Self {
let surface = unsafe { CFRetained::retain(surface.cast::<IOSurfaceRef>()) };
let code = surface.pixel_format();
let format = Ycbcr420Format::from_pixel_format(code).unwrap_or_else(|| {
panic!(
"IOSurface pixel format {:?} is not biplanar 4:2:0 YCbCr",
String::from_utf8_lossy(&code.to_be_bytes())
)
});
assert_eq!(
surface.plane_count(),
2,
"a {format:?} IOSurface must have a luma and a chroma plane"
);
let extents = [Ycbcr420Plane::Luma, Ycbcr420Plane::Chroma].map(|plane| {
extent(
surface.width_of_plane(plane.index()),
surface.height_of_plane(plane.index()),
)
});
Self {
surface,
format,
extents,
owner: None,
}
}
#[must_use]
pub fn with_owner(mut self, owner: impl Send + 'static) -> Self {
SharedOwner::attach(&mut self.owner, owner);
self
}
#[must_use]
pub const fn format(&self) -> Ycbcr420Format {
self.format
}
#[must_use]
pub const fn width(&self, plane: Ycbcr420Plane) -> u32 {
self.extents[plane.index()].0
}
#[must_use]
pub const fn height(&self, plane: Ycbcr420Plane) -> u32 {
self.extents[plane.index()].1
}
#[must_use]
pub fn import(&self, device: &wgpu::Device, plane: Ycbcr420Plane) -> wgpu::Texture {
let required = self.format.required_features();
assert!(
device.features().contains(required),
"importing a {:?} IOSurface needs a device created with {required:?}",
self.format
);
let (width, height) = self.extents[plane.index()];
import_plane(
device,
&self.surface,
&PlaneImport {
label: match plane {
Ycbcr420Plane::Luma => "wgpu_external_frame_io_surface_luma",
Ycbcr420Plane::Chroma => "wgpu_external_frame_io_surface_chroma",
},
index: plane.index(),
format: self.format.texture_format(plane),
metal_format: self.format.metal_format(plane),
width,
height,
},
self.owner.as_ref(),
)
}
}
#[derive(Clone)]
struct SharedOwner {
_owner: Arc<SyncWrapper<Box<dyn Send>>>,
}
impl SharedOwner {
fn attach(slot: &mut Option<Self>, owner: impl Send + 'static) {
assert!(
slot.is_none(),
"an IOSurface frame carries at most one owner"
);
*slot = Some(Self {
_owner: Arc::new(SyncWrapper::new(Box::new(owner))),
});
}
fn drop_callback(&self) -> wgpu::hal::DropCallback {
let share = self.clone();
Box::new(move || drop(share))
}
}
fn extent(width: usize, height: usize) -> (u32, u32) {
(
u32::try_from(width).expect("IOSurface width exceeds u32"),
u32::try_from(height).expect("IOSurface height exceeds u32"),
)
}
#[cfg(target_os = "macos")]
fn storage_mode(device: &ProtocolObject<dyn MTLDevice>) -> MTLStorageMode {
if device.hasUnifiedMemory() {
MTLStorageMode::Shared
} else {
MTLStorageMode::Managed
}
}
#[cfg(target_os = "ios")]
const fn storage_mode(_device: &ProtocolObject<dyn MTLDevice>) -> MTLStorageMode {
MTLStorageMode::Shared
}
struct PlaneImport {
label: &'static str,
index: usize,
format: wgpu::TextureFormat,
metal_format: MTLPixelFormat,
width: u32,
height: u32,
}
fn import_plane(
device: &wgpu::Device,
surface: &IOSurfaceRef,
plane: &PlaneImport,
owner: Option<&SharedOwner>,
) -> wgpu::Texture {
let descriptor = wgpu::TextureDescriptor {
label: Some(plane.label),
size: wgpu::Extent3d {
width: plane.width,
height: plane.height,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: plane.format,
usage: wgpu::TextureUsages::COPY_SRC | wgpu::TextureUsages::TEXTURE_BINDING,
view_formats: &[],
};
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(plane.width).expect("IOSurface width exceeds usize"));
metal_descriptor
.setHeight(usize::try_from(plane.height).expect("IOSurface height exceeds usize"));
}
metal_descriptor.setTextureType(MTLTextureType::Type2D);
metal_descriptor.setPixelFormat(plane.metal_format);
metal_descriptor.setUsage(MTLTextureUsage::ShaderRead);
metal_descriptor.setStorageMode(storage_mode(raw_device));
let texture = raw_device
.newTextureWithDescriptor_iosurface_plane(&metal_descriptor, surface, plane.index)
.unwrap_or_else(|| {
panic!(
"Metal rejected importing IOSurface plane {} as {:?}",
plane.index, plane.format
)
});
unsafe {
<wgpu::hal::api::Metal as wgpu::hal::Api>::Device::texture_from_raw(
texture,
descriptor.format,
MTLTextureType::Type2D,
1,
1,
wgpu::hal::CopyExtent {
width: plane.width,
height: plane.height,
depth: 1,
},
owner.map(SharedOwner::drop_callback),
)
}
});
unsafe {
device.create_texture_from_hal::<wgpu::hal::api::Metal>(
hal_texture,
&descriptor,
wgpu::TextureUses::COPY_SRC,
)
}
}