Skip to main content

apple_cf/ffi/
mod.rs

1//! Raw FFI declarations for the Swift bridge.
2//!
3//! These are intentionally low-level (`*mut c_void` + scalar arguments) and
4//! `unsafe`; safe wrappers live in [`crate::iosurface`], [`crate::dispatch_queue`],
5//! etc.
6//!
7//! Currently exposes:
8//! * `acf_free_string` — heap-string deallocator used by every bridge fn that
9//!   returns an owned C string back to Rust.
10//! * `dispatch_queue_*` — Grand Central Dispatch queue lifetime + creation.
11//! * `io_surface_*` — IOSurface accessors and lifetime control.
12//!
13//! Future framework additions (CoreMedia, CoreVideo, Metal, ...) extend this
14//! module and gain matching `@_cdecl` exports under
15//! `swift-bridge/Sources/<Framework>Bridge/`.
16
17#![allow(missing_docs)]
18
19use core::ffi::{c_char, c_void};
20
21extern "C" {
22    /// Free a heap-allocated C string previously returned by any bridge fn.
23    /// Centralised in `AppleCFBridge.swift`.
24    pub fn acf_free_string(s: *mut c_char);
25
26    // ---- dispatch ----
27    pub fn dispatch_queue_create(label: *const c_char, qos: i32) -> *const c_void;
28    pub fn dispatch_queue_release(queue: *const c_void);
29    pub fn dispatch_queue_retain(queue: *const c_void) -> *const c_void;
30
31    // ---- IOSurface ----
32    pub fn io_surface_create(
33        width: usize,
34        height: usize,
35        pixel_format: u32,
36        bytes_per_element: usize,
37        out_surface: *mut *mut c_void,
38    ) -> i32;
39    pub fn io_surface_create_with_properties(
40        width: usize,
41        height: usize,
42        pixel_format: u32,
43        bytes_per_element: usize,
44        bytes_per_row: usize,
45        alloc_size: usize,
46        plane_count: usize,
47        plane_widths: *const usize,
48        plane_heights: *const usize,
49        plane_bytes_per_row: *const usize,
50        plane_bytes_per_element: *const usize,
51        plane_offsets: *const usize,
52        plane_sizes: *const usize,
53        surface_out: *mut *mut c_void,
54    ) -> i32;
55    pub fn io_surface_release(surface: *mut c_void);
56    pub fn io_surface_retain(surface: *mut c_void) -> *mut c_void;
57    pub fn io_surface_hash(surface: *mut c_void) -> usize;
58    pub fn io_surface_get_width(surface: *mut c_void) -> usize;
59    pub fn io_surface_get_height(surface: *mut c_void) -> usize;
60    pub fn io_surface_get_bytes_per_row(surface: *mut c_void) -> usize;
61    pub fn io_surface_get_alloc_size(surface: *mut c_void) -> usize;
62    pub fn io_surface_get_pixel_format(surface: *mut c_void) -> u32;
63    pub fn io_surface_get_id(surface: *mut c_void) -> u32;
64    pub fn io_surface_get_seed(surface: *mut c_void) -> u32;
65    pub fn io_surface_get_plane_count(surface: *mut c_void) -> usize;
66    pub fn io_surface_get_width_of_plane(surface: *mut c_void, plane_index: usize) -> usize;
67    pub fn io_surface_get_height_of_plane(surface: *mut c_void, plane_index: usize) -> usize;
68    pub fn io_surface_get_bytes_per_row_of_plane(surface: *mut c_void, plane_index: usize)
69        -> usize;
70    pub fn io_surface_get_base_address_of_plane(
71        surface: *mut c_void,
72        plane_index: usize,
73    ) -> *mut c_void;
74    pub fn io_surface_get_base_address(surface: *mut c_void) -> *mut c_void;
75    pub fn io_surface_get_bytes_per_element(surface: *mut c_void) -> usize;
76    pub fn io_surface_get_element_width(surface: *mut c_void) -> usize;
77    pub fn io_surface_get_element_height(surface: *mut c_void) -> usize;
78    pub fn io_surface_is_in_use(surface: *mut c_void) -> bool;
79    pub fn io_surface_increment_use_count(surface: *mut c_void);
80    pub fn io_surface_decrement_use_count(surface: *mut c_void);
81    pub fn io_surface_lock(surface: *mut c_void, options: u32, seed: *mut u32) -> i32;
82    pub fn io_surface_unlock(surface: *mut c_void, options: u32, seed: *mut u32) -> i32;
83
84    // ---- CMSampleBuffer ----
85    pub fn cm_sample_buffer_release(sample_buffer: *mut c_void);
86    pub fn cm_sample_buffer_retain(sample_buffer: *mut c_void) -> *mut c_void;
87    pub fn cm_sample_buffer_hash(sample_buffer: *mut c_void) -> usize;
88    pub fn cm_sample_buffer_get_data_buffer(sample_buffer: *mut c_void) -> *mut c_void;
89    pub fn cm_sample_buffer_get_format_description(sample_buffer: *mut c_void) -> *mut c_void;
90    pub fn cm_sample_buffer_get_image_buffer(sample_buffer: *mut c_void) -> *mut c_void;
91    pub fn cm_sample_buffer_get_presentation_timestamp(
92        sample_buffer: *mut c_void,
93        out_value: *mut i64,
94        out_timescale: *mut i32,
95        out_flags: *mut u32,
96        out_epoch: *mut i64,
97    );
98    pub fn cm_sample_buffer_get_decode_timestamp(
99        sample_buffer: *mut c_void,
100        out_value: *mut i64,
101        out_timescale: *mut i32,
102        out_flags: *mut u32,
103        out_epoch: *mut i64,
104    );
105    pub fn cm_sample_buffer_get_duration(
106        sample_buffer: *mut c_void,
107        out_value: *mut i64,
108        out_timescale: *mut i32,
109        out_flags: *mut u32,
110        out_epoch: *mut i64,
111    );
112    pub fn cm_sample_buffer_get_num_samples(sample_buffer: *mut c_void) -> i64;
113    pub fn cm_sample_buffer_is_valid(sample_buffer: *mut c_void) -> bool;
114    pub fn cm_sample_buffer_data_is_ready(sample_buffer: *mut c_void) -> bool;
115
116    // ---- CMBlockBuffer ----
117    pub fn cm_block_buffer_release(block_buffer: *mut c_void);
118    pub fn cm_block_buffer_retain(block_buffer: *mut c_void) -> *mut c_void;
119    pub fn cm_block_buffer_hash(block_buffer: *mut c_void) -> usize;
120    pub fn cm_block_buffer_get_data_length(block_buffer: *mut c_void) -> usize;
121    pub fn cm_block_buffer_is_empty(block_buffer: *mut c_void) -> bool;
122    pub fn cm_block_buffer_is_range_contiguous(
123        block_buffer: *mut c_void,
124        offset: usize,
125        length: usize,
126    ) -> bool;
127    pub fn cm_block_buffer_get_data_pointer(
128        block_buffer: *mut c_void,
129        offset: usize,
130        out_length_at_offset: *mut usize,
131        out_total_length: *mut usize,
132        out_data_pointer: *mut *mut c_void,
133    ) -> i32;
134    pub fn cm_block_buffer_copy_data_bytes(
135        block_buffer: *mut c_void,
136        offset_to_data: usize,
137        data_length: usize,
138        destination: *mut c_void,
139    ) -> i32;
140    pub fn cm_block_buffer_create_with_data(
141        data: *const c_void,
142        data_length: usize,
143        block_buffer_out: *mut *mut c_void,
144    ) -> i32;
145    pub fn cm_block_buffer_create_empty(block_buffer_out: *mut *mut c_void) -> i32;
146
147    // ---- CMFormatDescription ----
148    pub fn cm_format_description_release(format_description: *mut c_void);
149    pub fn cm_format_description_retain(format_description: *mut c_void) -> *mut c_void;
150    pub fn cm_format_description_hash(format_description: *mut c_void) -> usize;
151    pub fn cm_format_description_get_media_type(format_description: *mut c_void) -> u32;
152    pub fn cm_format_description_get_media_subtype(format_description: *mut c_void) -> u32;
153    pub fn cm_format_description_get_extensions(format_description: *mut c_void) -> *const c_void;
154    pub fn cm_format_description_get_audio_sample_rate(format_description: *mut c_void) -> f64;
155    pub fn cm_format_description_get_audio_channel_count(format_description: *mut c_void) -> u32;
156    pub fn cm_format_description_get_audio_bits_per_channel(format_description: *mut c_void)
157        -> u32;
158    pub fn cm_format_description_get_audio_bytes_per_frame(format_description: *mut c_void) -> u32;
159    pub fn cm_format_description_get_audio_format_flags(format_description: *mut c_void) -> u32;
160
161    // ---- CVPixelBuffer ----
162    pub fn cv_pixel_buffer_release(pixel_buffer: *mut c_void);
163    pub fn cv_pixel_buffer_retain(pixel_buffer: *mut c_void) -> *mut c_void;
164    pub fn cv_pixel_buffer_hash(pixel_buffer: *mut c_void) -> usize;
165    pub fn cv_pixel_buffer_get_type_id() -> usize;
166    pub fn cv_pixel_buffer_get_width(pixel_buffer: *mut c_void) -> usize;
167    pub fn cv_pixel_buffer_get_height(pixel_buffer: *mut c_void) -> usize;
168    pub fn cv_pixel_buffer_get_pixel_format_type(pixel_buffer: *mut c_void) -> u32;
169    pub fn cv_pixel_buffer_get_bytes_per_row(pixel_buffer: *mut c_void) -> usize;
170    pub fn cv_pixel_buffer_get_data_size(pixel_buffer: *mut c_void) -> usize;
171    pub fn cv_pixel_buffer_is_planar(pixel_buffer: *mut c_void) -> bool;
172    pub fn cv_pixel_buffer_get_plane_count(pixel_buffer: *mut c_void) -> usize;
173    pub fn cv_pixel_buffer_get_width_of_plane(
174        pixel_buffer: *mut c_void,
175        plane_index: usize,
176    ) -> usize;
177    pub fn cv_pixel_buffer_get_height_of_plane(
178        pixel_buffer: *mut c_void,
179        plane_index: usize,
180    ) -> usize;
181    pub fn cv_pixel_buffer_get_bytes_per_row_of_plane(
182        pixel_buffer: *mut c_void,
183        plane_index: usize,
184    ) -> usize;
185    pub fn cv_pixel_buffer_get_base_address_of_plane(
186        pixel_buffer: *mut c_void,
187        plane_index: usize,
188    ) -> *mut c_void;
189    pub fn cv_pixel_buffer_get_base_address(pixel_buffer: *mut c_void) -> *mut c_void;
190    pub fn cv_pixel_buffer_lock_base_address(pixel_buffer: *mut c_void, flags: u32) -> i32;
191    pub fn cv_pixel_buffer_unlock_base_address(pixel_buffer: *mut c_void, flags: u32) -> i32;
192    pub fn cv_pixel_buffer_get_io_surface(pixel_buffer: *mut c_void) -> *mut c_void;
193    pub fn cv_pixel_buffer_get_extended_pixels(
194        pixel_buffer: *mut c_void,
195        extra_columns_on_left: *mut usize,
196        extra_columns_on_right: *mut usize,
197        extra_rows_on_top: *mut usize,
198        extra_rows_on_bottom: *mut usize,
199    );
200    pub fn cv_pixel_buffer_fill_extended_pixels(pixel_buffer: *mut c_void) -> i32;
201    pub fn cv_pixel_buffer_create(
202        width: usize,
203        height: usize,
204        pixel_format_type: u32,
205        pixel_buffer_out: *mut *mut c_void,
206    ) -> i32;
207    pub fn cv_pixel_buffer_create_with_bytes(
208        width: usize,
209        height: usize,
210        pixel_format_type: u32,
211        base_address: *mut c_void,
212        bytes_per_row: usize,
213        pixel_buffer_out: *mut *mut c_void,
214    ) -> i32;
215    pub fn cv_pixel_buffer_create_with_planar_bytes(
216        width: usize,
217        height: usize,
218        pixel_format_type: u32,
219        num_planes: usize,
220        plane_base_addresses: *const *mut c_void,
221        plane_widths: *const usize,
222        plane_heights: *const usize,
223        plane_bytes_per_row: *const usize,
224        pixel_buffer_out: *mut *mut c_void,
225    ) -> i32;
226    pub fn cv_pixel_buffer_create_with_io_surface(
227        io_surface: *mut c_void,
228        pixel_buffer_out: *mut *mut c_void,
229    ) -> i32;
230
231    // ---- CVPixelBufferPool ----
232    pub fn cv_pixel_buffer_pool_release(pool: *mut c_void);
233    pub fn cv_pixel_buffer_pool_retain(pool: *mut c_void) -> *mut c_void;
234    pub fn cv_pixel_buffer_pool_hash(pool: *mut c_void) -> usize;
235    pub fn cv_pixel_buffer_pool_get_type_id() -> usize;
236    pub fn cv_pixel_buffer_pool_create(
237        width: usize,
238        height: usize,
239        pixel_format_type: u32,
240        max_buffers: usize,
241        pool_out: *mut *mut c_void,
242    ) -> i32;
243    pub fn cv_pixel_buffer_pool_create_pixel_buffer(
244        pool: *mut c_void,
245        pixel_buffer_out: *mut *mut c_void,
246    ) -> i32;
247    pub fn cv_pixel_buffer_pool_flush(pool: *mut c_void);
248    pub fn cv_pixel_buffer_pool_get_attributes(pool: *mut c_void) -> *const c_void;
249    pub fn cv_pixel_buffer_pool_get_pixel_buffer_attributes(pool: *mut c_void) -> *const c_void;
250}