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
19mod core_foundation;
20mod core_video_extras;
21mod dispatch_extras;
22
23pub use core_foundation::*;
24pub use core_video_extras::*;
25pub use dispatch_extras::*;
26
27use core::ffi::{c_char, c_void};
28
29extern "C" {
30    /// Free a heap-allocated C string previously returned by any bridge fn.
31    /// Centralised in `AppleCFBridge.swift`.
32    pub fn acf_free_string(s: *mut c_char);
33
34    // ---- dispatch ----
35    pub fn dispatch_queue_create(label: *const c_char, qos: i32) -> *const c_void;
36    pub fn dispatch_queue_release(queue: *const c_void);
37    pub fn dispatch_queue_retain(queue: *const c_void) -> *const c_void;
38
39    // ---- CoreGraphics bridge helpers ----
40    pub fn cgimage_save_png(image: *mut c_void, path: *const c_char) -> bool;
41
42    // ---- IOSurface ----
43    pub fn io_surface_create(
44        width: usize,
45        height: usize,
46        pixel_format: u32,
47        bytes_per_element: usize,
48        out_surface: *mut *mut c_void,
49    ) -> i32;
50    pub fn io_surface_create_with_properties(
51        width: usize,
52        height: usize,
53        pixel_format: u32,
54        bytes_per_element: usize,
55        bytes_per_row: usize,
56        alloc_size: usize,
57        plane_count: usize,
58        plane_widths: *const usize,
59        plane_heights: *const usize,
60        plane_bytes_per_row: *const usize,
61        plane_bytes_per_element: *const usize,
62        plane_offsets: *const usize,
63        plane_sizes: *const usize,
64        surface_out: *mut *mut c_void,
65    ) -> i32;
66    pub fn io_surface_release(surface: *mut c_void);
67    pub fn io_surface_retain(surface: *mut c_void) -> *mut c_void;
68    pub fn io_surface_hash(surface: *mut c_void) -> usize;
69    pub fn io_surface_get_width(surface: *mut c_void) -> usize;
70    pub fn io_surface_get_height(surface: *mut c_void) -> usize;
71    pub fn io_surface_get_bytes_per_row(surface: *mut c_void) -> usize;
72    pub fn io_surface_get_alloc_size(surface: *mut c_void) -> usize;
73    pub fn io_surface_get_pixel_format(surface: *mut c_void) -> u32;
74    pub fn io_surface_get_id(surface: *mut c_void) -> u32;
75    pub fn io_surface_get_seed(surface: *mut c_void) -> u32;
76    pub fn io_surface_get_plane_count(surface: *mut c_void) -> usize;
77    pub fn io_surface_get_width_of_plane(surface: *mut c_void, plane_index: usize) -> usize;
78    pub fn io_surface_get_height_of_plane(surface: *mut c_void, plane_index: usize) -> usize;
79    pub fn io_surface_get_bytes_per_row_of_plane(surface: *mut c_void, plane_index: usize)
80        -> usize;
81    pub fn io_surface_get_base_address_of_plane(
82        surface: *mut c_void,
83        plane_index: usize,
84    ) -> *mut c_void;
85    pub fn io_surface_get_base_address(surface: *mut c_void) -> *mut c_void;
86    pub fn io_surface_get_bytes_per_element(surface: *mut c_void) -> usize;
87    pub fn io_surface_get_element_width(surface: *mut c_void) -> usize;
88    pub fn io_surface_get_element_height(surface: *mut c_void) -> usize;
89    pub fn io_surface_is_in_use(surface: *mut c_void) -> bool;
90    pub fn io_surface_increment_use_count(surface: *mut c_void);
91    pub fn io_surface_decrement_use_count(surface: *mut c_void);
92    pub fn io_surface_lock(surface: *mut c_void, options: u32, seed: *mut u32) -> i32;
93    pub fn io_surface_unlock(surface: *mut c_void, options: u32, seed: *mut u32) -> i32;
94
95    // ---- CMSampleBuffer ----
96    pub fn cm_sample_buffer_release(sample_buffer: *mut c_void);
97    pub fn cm_sample_buffer_retain(sample_buffer: *mut c_void) -> *mut c_void;
98    pub fn cm_sample_buffer_hash(sample_buffer: *mut c_void) -> usize;
99    pub fn cm_sample_buffer_get_data_buffer(sample_buffer: *mut c_void) -> *mut c_void;
100    pub fn cm_sample_buffer_get_format_description(sample_buffer: *mut c_void) -> *mut c_void;
101    pub fn cm_sample_buffer_get_image_buffer(sample_buffer: *mut c_void) -> *mut c_void;
102    pub fn cm_sample_buffer_get_presentation_timestamp(
103        sample_buffer: *mut c_void,
104        out_value: *mut i64,
105        out_timescale: *mut i32,
106        out_flags: *mut u32,
107        out_epoch: *mut i64,
108    );
109    pub fn cm_sample_buffer_get_decode_timestamp(
110        sample_buffer: *mut c_void,
111        out_value: *mut i64,
112        out_timescale: *mut i32,
113        out_flags: *mut u32,
114        out_epoch: *mut i64,
115    );
116    pub fn cm_sample_buffer_get_duration(
117        sample_buffer: *mut c_void,
118        out_value: *mut i64,
119        out_timescale: *mut i32,
120        out_flags: *mut u32,
121        out_epoch: *mut i64,
122    );
123    pub fn cm_sample_buffer_get_num_samples(sample_buffer: *mut c_void) -> i64;
124    pub fn cm_sample_buffer_is_valid(sample_buffer: *mut c_void) -> bool;
125    pub fn cm_sample_buffer_data_is_ready(sample_buffer: *mut c_void) -> bool;
126
127    // ---- CMBlockBuffer ----
128    pub fn cm_block_buffer_release(block_buffer: *mut c_void);
129    pub fn cm_block_buffer_retain(block_buffer: *mut c_void) -> *mut c_void;
130    pub fn cm_block_buffer_hash(block_buffer: *mut c_void) -> usize;
131    pub fn cm_block_buffer_get_data_length(block_buffer: *mut c_void) -> usize;
132    pub fn cm_block_buffer_is_empty(block_buffer: *mut c_void) -> bool;
133    pub fn cm_block_buffer_is_range_contiguous(
134        block_buffer: *mut c_void,
135        offset: usize,
136        length: usize,
137    ) -> bool;
138    pub fn cm_block_buffer_get_data_pointer(
139        block_buffer: *mut c_void,
140        offset: usize,
141        out_length_at_offset: *mut usize,
142        out_total_length: *mut usize,
143        out_data_pointer: *mut *mut c_void,
144    ) -> i32;
145    pub fn cm_block_buffer_copy_data_bytes(
146        block_buffer: *mut c_void,
147        offset_to_data: usize,
148        data_length: usize,
149        destination: *mut c_void,
150    ) -> i32;
151    pub fn cm_block_buffer_create_with_data(
152        data: *const c_void,
153        data_length: usize,
154        block_buffer_out: *mut *mut c_void,
155    ) -> i32;
156    pub fn cm_block_buffer_create_empty(block_buffer_out: *mut *mut c_void) -> i32;
157
158    // ---- CMFormatDescription ----
159    pub fn cm_format_description_release(format_description: *mut c_void);
160    pub fn cm_format_description_retain(format_description: *mut c_void) -> *mut c_void;
161    pub fn cm_format_description_hash(format_description: *mut c_void) -> usize;
162    pub fn cm_format_description_get_media_type(format_description: *mut c_void) -> u32;
163    pub fn cm_format_description_get_media_subtype(format_description: *mut c_void) -> u32;
164    pub fn cm_format_description_get_extensions(format_description: *mut c_void) -> *const c_void;
165    pub fn cm_format_description_get_audio_sample_rate(format_description: *mut c_void) -> f64;
166    pub fn cm_format_description_get_audio_channel_count(format_description: *mut c_void) -> u32;
167    pub fn cm_format_description_get_audio_bits_per_channel(format_description: *mut c_void)
168        -> u32;
169    pub fn cm_format_description_get_audio_bytes_per_frame(format_description: *mut c_void) -> u32;
170    pub fn cm_format_description_get_audio_format_flags(format_description: *mut c_void) -> u32;
171
172    // ---- CVPixelBuffer ----
173    pub fn cv_pixel_buffer_release(pixel_buffer: *mut c_void);
174    pub fn cv_pixel_buffer_retain(pixel_buffer: *mut c_void) -> *mut c_void;
175    pub fn cv_pixel_buffer_hash(pixel_buffer: *mut c_void) -> usize;
176    pub fn cv_pixel_buffer_get_type_id() -> usize;
177    pub fn cv_pixel_buffer_get_width(pixel_buffer: *mut c_void) -> usize;
178    pub fn cv_pixel_buffer_get_height(pixel_buffer: *mut c_void) -> usize;
179    pub fn cv_pixel_buffer_get_pixel_format_type(pixel_buffer: *mut c_void) -> u32;
180    pub fn cv_pixel_buffer_get_bytes_per_row(pixel_buffer: *mut c_void) -> usize;
181    pub fn cv_pixel_buffer_get_data_size(pixel_buffer: *mut c_void) -> usize;
182    pub fn cv_pixel_buffer_is_planar(pixel_buffer: *mut c_void) -> bool;
183    pub fn cv_pixel_buffer_get_plane_count(pixel_buffer: *mut c_void) -> usize;
184    pub fn cv_pixel_buffer_get_width_of_plane(
185        pixel_buffer: *mut c_void,
186        plane_index: usize,
187    ) -> usize;
188    pub fn cv_pixel_buffer_get_height_of_plane(
189        pixel_buffer: *mut c_void,
190        plane_index: usize,
191    ) -> usize;
192    pub fn cv_pixel_buffer_get_bytes_per_row_of_plane(
193        pixel_buffer: *mut c_void,
194        plane_index: usize,
195    ) -> usize;
196    pub fn cv_pixel_buffer_get_base_address_of_plane(
197        pixel_buffer: *mut c_void,
198        plane_index: usize,
199    ) -> *mut c_void;
200    pub fn cv_pixel_buffer_get_base_address(pixel_buffer: *mut c_void) -> *mut c_void;
201    pub fn cv_pixel_buffer_lock_base_address(pixel_buffer: *mut c_void, flags: u32) -> i32;
202    pub fn cv_pixel_buffer_unlock_base_address(pixel_buffer: *mut c_void, flags: u32) -> i32;
203    pub fn cv_pixel_buffer_get_io_surface(pixel_buffer: *mut c_void) -> *mut c_void;
204    pub fn cv_pixel_buffer_get_extended_pixels(
205        pixel_buffer: *mut c_void,
206        extra_columns_on_left: *mut usize,
207        extra_columns_on_right: *mut usize,
208        extra_rows_on_top: *mut usize,
209        extra_rows_on_bottom: *mut usize,
210    );
211    pub fn cv_pixel_buffer_fill_extended_pixels(pixel_buffer: *mut c_void) -> i32;
212    pub fn cv_pixel_buffer_create(
213        width: usize,
214        height: usize,
215        pixel_format_type: u32,
216        pixel_buffer_out: *mut *mut c_void,
217    ) -> i32;
218    pub fn cv_pixel_buffer_create_with_bytes(
219        width: usize,
220        height: usize,
221        pixel_format_type: u32,
222        base_address: *mut c_void,
223        bytes_per_row: usize,
224        pixel_buffer_out: *mut *mut c_void,
225    ) -> i32;
226    pub fn cv_pixel_buffer_create_with_planar_bytes(
227        width: usize,
228        height: usize,
229        pixel_format_type: u32,
230        num_planes: usize,
231        plane_base_addresses: *const *mut c_void,
232        plane_widths: *const usize,
233        plane_heights: *const usize,
234        plane_bytes_per_row: *const usize,
235        pixel_buffer_out: *mut *mut c_void,
236    ) -> i32;
237    pub fn cv_pixel_buffer_create_with_io_surface(
238        io_surface: *mut c_void,
239        pixel_buffer_out: *mut *mut c_void,
240    ) -> i32;
241
242    // ---- CVPixelBufferPool ----
243    pub fn cv_pixel_buffer_pool_release(pool: *mut c_void);
244    pub fn cv_pixel_buffer_pool_retain(pool: *mut c_void) -> *mut c_void;
245    pub fn cv_pixel_buffer_pool_hash(pool: *mut c_void) -> usize;
246    pub fn cv_pixel_buffer_pool_get_type_id() -> usize;
247    pub fn cv_pixel_buffer_pool_create(
248        width: usize,
249        height: usize,
250        pixel_format_type: u32,
251        max_buffers: usize,
252        pool_out: *mut *mut c_void,
253    ) -> i32;
254    pub fn cv_pixel_buffer_pool_create_pixel_buffer(
255        pool: *mut c_void,
256        pixel_buffer_out: *mut *mut c_void,
257    ) -> i32;
258    pub fn cv_pixel_buffer_pool_flush(pool: *mut c_void);
259    pub fn cv_pixel_buffer_pool_get_attributes(pool: *mut c_void) -> *const c_void;
260    pub fn cv_pixel_buffer_pool_get_pixel_buffer_attributes(pool: *mut c_void) -> *const c_void;
261}