Skip to main content

IOSurfaceLockGuard

Struct IOSurfaceLockGuard 

Source
pub struct IOSurfaceLockGuard<'a> { /* private fields */ }
Available on crate feature iosurface only.
Expand description

RAII guard for locked IOSurface

Balances a native surface mapping while the guard is held. The surface is automatically unlocked when this guard is dropped.

§Memory Access

The guard establishes native synchronization, not Rust exclusivity.

§Examples

use apple_cf::iosurface::{IOSurface, IOSurfaceLockOptions};

fn access_surface(surface: &IOSurface) -> Result<(), i32> {
    let guard = surface.lock(IOSurfaceLockOptions::READ_ONLY)?;
     
    // SAFETY: no alias can mutate or remap the surface while `data` lives.
    let data = unsafe { guard.as_slice() }.expect("surface base address");
     
    // Access a specific row
    if let Some(row) = unsafe { guard.row(0) } {
        println!("First row: {} bytes", row.len());
    }
     
    // Access a specific plane (for multi-planar formats)
    if let Some(plane_data) = unsafe { guard.plane_data(0) } {
        println!("Plane 0: {} bytes", plane_data.len());
    }
     
    Ok(())
}

Implementations§

Source§

impl IOSurfaceLockGuard<'_>

Source

pub fn width(&self) -> usize

Get the width of the surface in pixels

Source

pub fn height(&self) -> usize

Get the height of the surface in pixels

Source

pub fn bytes_per_row(&self) -> usize

Get the bytes per row of the surface

Source

pub fn alloc_size(&self) -> usize

Get the total allocation size in bytes

Source

pub fn data_size(&self) -> usize

Get the data size of the surface (alias for alloc_size)

Source

pub fn pixel_format(&self) -> u32

Get the pixel format of the surface

Source

pub fn plane_count(&self) -> usize

Get the number of planes in the surface

Source

pub fn base_address(&self) -> *const u8

Get the base address of the locked surface.

Dereferencing the returned pointer is unsafe. The native lock keeps the mapping synchronized but does not guarantee Rust aliasing or immutability.

Source

pub fn base_address_mut(&mut self) -> Option<*mut u8>

Get the mutable base address (only valid for read-write locks).

Returns None if this is a read-only lock.

Dereferencing the returned pointer requires unique access to the bytes across all Rust, native, retained, GPU, and cross-process aliases.

Source

pub fn base_address_of_plane(&self, plane_index: usize) -> Option<*const u8>

Get the base address of a specific plane.

For multi-planar formats like YCbCr 4:2:0:

  • Plane 0: Y (luminance) data
  • Plane 1: CbCr (chrominance) data

Returns None if the plane index is out of bounds.

Dereferencing the returned pointer is unsafe because the lock does not establish Rust immutability.

Source

pub fn base_address_of_plane_mut( &mut self, plane_index: usize, ) -> Option<*mut u8>

Get the mutable base address of a specific plane.

Returns None if this is a read-only lock or the plane index is out of bounds. Dereferencing requires unique access across every alias.

Source

pub unsafe fn as_slice(&self) -> Option<&[u8]>

Get a slice view of the surface allocation.

Returns None for a missing base address or a length that cannot be represented by a Rust slice.

§Safety

For the returned reference’s lifetime, the allocation must remain initialized, immovable, and locked, and no Rust or native alias may mutate or remap any byte in it.

Source

pub unsafe fn as_slice_mut(&mut self) -> Option<&mut [u8]>

Get a mutable slice view of the surface allocation.

Returns None for read-only locks, a missing base address, or a length that cannot be represented by a Rust slice.

§Safety

For the returned reference’s lifetime, this caller must have unique access to the full allocation across every Rust, native, retained, GPU, and cross-process alias. The allocation must remain initialized, immovable, and locked.

Source

pub unsafe fn row(&self, row_index: usize) -> Option<&[u8]>

Get a specific row as a slice.

Returns None if the row index is out of bounds.

§Safety

For the returned reference’s lifetime, the row must remain initialized, immovable, locked, and immutable through every Rust and native alias.

Source

pub unsafe fn plane_data(&self, plane_index: usize) -> Option<&[u8]>

Get a slice of plane data.

Returns the data for a specific plane as a byte slice. The slice size is calculated from the plane’s height and bytes per row.

Returns None if the plane index is out of bounds or its byte length cannot be represented.

§Safety

For the returned reference’s lifetime, the plane must remain initialized, immovable, locked, and immutable through every Rust and native alias.

Source

pub unsafe fn plane_row( &self, plane_index: usize, row_index: usize, ) -> Option<&[u8]>

Get a specific row from a plane as a slice.

Returns None if the plane or row index is out of bounds.

§Safety

For the returned reference’s lifetime, the row must remain initialized, immovable, locked, and immutable through every Rust and native alias.

Source

pub unsafe fn cursor(&self) -> Option<Cursor<&[u8]>>

Access surface with a standard std::io::Cursor

Returns a cursor over the surface data that implements Read and Seek.

§Examples
use std::io::{Read, Seek, SeekFrom};
use apple_cf::iosurface::{IOSurface, IOSurfaceLockOptions};

fn read_surface(surface: &IOSurface) {
    let guard = surface.lock(IOSurfaceLockOptions::READ_ONLY).unwrap();
    // SAFETY: no alias can mutate or remap the surface while the cursor lives.
    let mut cursor = unsafe { guard.cursor() }.unwrap();

    // Read first 4 bytes
    let mut pixel = [0u8; 4];
    cursor.read_exact(&mut pixel).unwrap();

    // Seek to row 10
    let offset = 10 * guard.bytes_per_row();
    cursor.seek(SeekFrom::Start(offset as u64)).unwrap();
}
§Safety

The same immutability and mapping guarantees as Self::as_slice must hold for the cursor’s lifetime.

Source

pub fn as_ptr(&self) -> *const u8

Get raw pointer to surface data

Source

pub fn as_mut_ptr(&mut self) -> Option<*mut u8>

Get mutable raw pointer to surface data (only valid for read-write locks)

Returns None if this is a read-only lock.

Source

pub const fn is_read_only(&self) -> bool

Check if this is a read-only lock

Source

pub const fn options(&self) -> IOSurfaceLockOptions

Get the lock options

Trait Implementations§

Source§

impl Debug for IOSurfaceLockGuard<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for IOSurfaceLockGuard<'_>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.