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 acf_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    pub fn cm_metadata_format_description_create_with_keys(
172        metadata_type: u32,
173        keys: *mut c_void,
174        format_description_out: *mut *mut c_void,
175    ) -> i32;
176    pub fn cm_metadata_format_description_create_with_metadata_specifications(
177        metadata_type: u32,
178        metadata_specifications: *mut c_void,
179        format_description_out: *mut *mut c_void,
180    ) -> i32;
181    pub fn cm_metadata_format_description_create_with_description_and_metadata_specifications(
182        source_description: *mut c_void,
183        metadata_specifications: *mut c_void,
184        format_description_out: *mut *mut c_void,
185    ) -> i32;
186    pub fn cm_metadata_format_description_create_by_merging_descriptions(
187        source_description: *mut c_void,
188        other_source_description: *mut c_void,
189        format_description_out: *mut *mut c_void,
190    ) -> i32;
191    pub fn cm_metadata_format_description_get_identifiers(
192        format_description: *mut c_void,
193    ) -> *mut c_void;
194    pub fn cm_metadata_format_description_get_key_with_local_id(
195        format_description: *mut c_void,
196        local_id: u32,
197    ) -> *mut c_void;
198    pub fn cm_metadata_format_description_extension_key_metadata_key_table() -> *mut c_void;
199    pub fn cm_metadata_format_description_key_conforming_data_types() -> *mut c_void;
200    pub fn cm_metadata_format_description_key_data_type() -> *mut c_void;
201    pub fn cm_metadata_format_description_key_data_type_namespace() -> *mut c_void;
202    pub fn cm_metadata_format_description_key_language_tag() -> *mut c_void;
203    pub fn cm_metadata_format_description_key_local_id() -> *mut c_void;
204    pub fn cm_metadata_format_description_key_namespace() -> *mut c_void;
205    pub fn cm_metadata_format_description_key_setup_data() -> *mut c_void;
206    pub fn cm_metadata_format_description_key_structural_dependency() -> *mut c_void;
207    pub fn cm_metadata_format_description_key_value() -> *mut c_void;
208    pub fn cm_metadata_format_description_metadata_specification_key_data_type() -> *mut c_void;
209    pub fn cm_metadata_format_description_metadata_specification_key_extended_language_tag(
210    ) -> *mut c_void;
211    pub fn cm_metadata_format_description_metadata_specification_key_identifier() -> *mut c_void;
212    pub fn cm_metadata_format_description_metadata_specification_key_setup_data() -> *mut c_void;
213    pub fn cm_metadata_format_description_metadata_specification_key_structural_dependency(
214    ) -> *mut c_void;
215    pub fn cm_metadata_format_description_structural_dependency_key_dependency_is_invalid_flag(
216    ) -> *mut c_void;
217
218    // ---- CVPixelBuffer ----
219    pub fn cv_pixel_buffer_release(pixel_buffer: *mut c_void);
220    pub fn cv_pixel_buffer_retain(pixel_buffer: *mut c_void) -> *mut c_void;
221    pub fn cv_pixel_buffer_hash(pixel_buffer: *mut c_void) -> usize;
222    pub fn cv_pixel_buffer_get_type_id() -> usize;
223    pub fn cv_pixel_buffer_get_width(pixel_buffer: *mut c_void) -> usize;
224    pub fn cv_pixel_buffer_get_height(pixel_buffer: *mut c_void) -> usize;
225    pub fn cv_pixel_buffer_get_pixel_format_type(pixel_buffer: *mut c_void) -> u32;
226    pub fn cv_pixel_buffer_get_bytes_per_row(pixel_buffer: *mut c_void) -> usize;
227    pub fn cv_pixel_buffer_get_data_size(pixel_buffer: *mut c_void) -> usize;
228    pub fn cv_pixel_buffer_is_planar(pixel_buffer: *mut c_void) -> bool;
229    pub fn cv_pixel_buffer_get_plane_count(pixel_buffer: *mut c_void) -> usize;
230    pub fn cv_pixel_buffer_get_width_of_plane(
231        pixel_buffer: *mut c_void,
232        plane_index: usize,
233    ) -> usize;
234    pub fn cv_pixel_buffer_get_height_of_plane(
235        pixel_buffer: *mut c_void,
236        plane_index: usize,
237    ) -> usize;
238    pub fn cv_pixel_buffer_get_bytes_per_row_of_plane(
239        pixel_buffer: *mut c_void,
240        plane_index: usize,
241    ) -> usize;
242    pub fn cv_pixel_buffer_get_base_address_of_plane(
243        pixel_buffer: *mut c_void,
244        plane_index: usize,
245    ) -> *mut c_void;
246    pub fn cv_pixel_buffer_get_base_address(pixel_buffer: *mut c_void) -> *mut c_void;
247    pub fn cv_pixel_buffer_lock_base_address(pixel_buffer: *mut c_void, flags: u32) -> i32;
248    pub fn cv_pixel_buffer_unlock_base_address(pixel_buffer: *mut c_void, flags: u32) -> i32;
249    pub fn cv_pixel_buffer_get_io_surface(pixel_buffer: *mut c_void) -> *mut c_void;
250    pub fn cv_pixel_buffer_get_extended_pixels(
251        pixel_buffer: *mut c_void,
252        extra_columns_on_left: *mut usize,
253        extra_columns_on_right: *mut usize,
254        extra_rows_on_top: *mut usize,
255        extra_rows_on_bottom: *mut usize,
256    );
257    pub fn cv_pixel_buffer_fill_extended_pixels(pixel_buffer: *mut c_void) -> i32;
258    pub fn cv_pixel_buffer_create(
259        width: usize,
260        height: usize,
261        pixel_format_type: u32,
262        pixel_buffer_out: *mut *mut c_void,
263    ) -> i32;
264    pub fn cv_pixel_buffer_create_with_bytes(
265        width: usize,
266        height: usize,
267        pixel_format_type: u32,
268        base_address: *mut c_void,
269        bytes_per_row: usize,
270        pixel_buffer_out: *mut *mut c_void,
271    ) -> i32;
272    pub fn cv_pixel_buffer_create_with_planar_bytes(
273        width: usize,
274        height: usize,
275        pixel_format_type: u32,
276        num_planes: usize,
277        plane_base_addresses: *const *mut c_void,
278        plane_widths: *const usize,
279        plane_heights: *const usize,
280        plane_bytes_per_row: *const usize,
281        pixel_buffer_out: *mut *mut c_void,
282    ) -> i32;
283    pub fn cv_pixel_buffer_create_with_io_surface(
284        io_surface: *mut c_void,
285        pixel_buffer_out: *mut *mut c_void,
286    ) -> i32;
287
288    // ---- CVPixelBufferPool ----
289    pub fn cv_pixel_buffer_pool_release(pool: *mut c_void);
290    pub fn cv_pixel_buffer_pool_retain(pool: *mut c_void) -> *mut c_void;
291    pub fn cv_pixel_buffer_pool_hash(pool: *mut c_void) -> usize;
292    pub fn cv_pixel_buffer_pool_get_type_id() -> usize;
293    pub fn cv_pixel_buffer_pool_create(
294        width: usize,
295        height: usize,
296        pixel_format_type: u32,
297        max_buffers: usize,
298        pool_out: *mut *mut c_void,
299    ) -> i32;
300    pub fn cv_pixel_buffer_pool_create_pixel_buffer(
301        pool: *mut c_void,
302        pixel_buffer_out: *mut *mut c_void,
303    ) -> i32;
304    pub fn cv_pixel_buffer_pool_flush(pool: *mut c_void);
305    pub fn cv_pixel_buffer_pool_get_attributes(pool: *mut c_void) -> *const c_void;
306    pub fn cv_pixel_buffer_pool_get_pixel_buffer_attributes(pool: *mut c_void) -> *const c_void;
307}