Skip to main content

alpha2_native_gpu/
lib.rs

1//! Platform GPU interop for native decoded video surfaces.
2//!
3//! `FFmpeg` ownership stays in `alpha2-ffmpeg-bridge`; this crate owns raw GPU handles,
4//! barriers, synchronization, and bounded surface accounting.
5
6#[cfg(all(target_os = "android", feature = "ahb-import"))]
7mod android;
8#[cfg(all(
9    test,
10    feature = "hwdevice",
11    any(target_os = "linux", target_os = "windows", target_os = "macos")
12))]
13mod conformance;
14mod context;
15/// The fixed-width ABI shared with the C helpers, generated from their headers by the build
16/// script with bindgen's layout assertions. Structs only Windows or macOS use are also pinned by
17/// hand below.
18#[allow(
19    dead_code,
20    non_camel_case_types,
21    unreachable_pub,
22    clippy::all,
23    clippy::pedantic,
24    clippy::undocumented_unsafe_blocks
25)]
26mod interop {
27    include!(concat!(env!("OUT_DIR"), "/interop.rs"));
28}
29
30// Pins the layouts of interop structs that only Windows and macOS builds compile, so a header
31// edit to them fails on every target.
32const _: () = {
33    use interop::{
34        Alpha2D3D12CopyInfo as D3D12, Alpha2D3D12EncodeCaps as Caps,
35        Alpha2D3D12EncodeCopy as Encode, Alpha2MetalFrameInfo as Metal,
36    };
37    use std::mem::{align_of, offset_of, size_of};
38    assert!(size_of::<D3D12>() == 40 && align_of::<D3D12>() == 8);
39    assert!(offset_of!(D3D12, resource) == 0);
40    assert!(offset_of!(D3D12, fence) == 8);
41    assert!(offset_of!(D3D12, fence_value) == 16);
42    assert!(offset_of!(D3D12, width) == 24);
43    assert!(offset_of!(D3D12, height) == 28);
44    assert!(offset_of!(D3D12, dxgi_format) == 32);
45    assert!(size_of::<Encode>() == 48 && align_of::<Encode>() == 8);
46    assert!(offset_of!(Encode, luma) == 0);
47    assert!(offset_of!(Encode, chroma) == 8);
48    assert!(offset_of!(Encode, destination) == 16);
49    assert!(offset_of!(Encode, frame_fence) == 24);
50    assert!(offset_of!(Encode, frame_value) == 32);
51    assert!(offset_of!(Encode, width) == 40);
52    assert!(offset_of!(Encode, height) == 44);
53    assert!(size_of::<Caps>() == 76 && align_of::<Caps>() == 4);
54    assert!(offset_of!(Caps, supported) == 0);
55    assert!(offset_of!(Caps, refused_check) == 4);
56    assert!(offset_of!(Caps, refused_status) == 8);
57    assert!(offset_of!(Caps, validation_flags) == 12);
58    assert!(offset_of!(Caps, min_width) == 16);
59    assert!(offset_of!(Caps, min_height) == 20);
60    assert!(offset_of!(Caps, max_width) == 24);
61    assert!(offset_of!(Caps, max_height) == 28);
62    assert!(offset_of!(Caps, width_multiple) == 32);
63    assert!(offset_of!(Caps, height_multiple) == 36);
64    assert!(offset_of!(Caps, max_p_l0_references) == 40);
65    assert!(offset_of!(Caps, max_b_l0_references) == 44);
66    assert!(offset_of!(Caps, max_l1_references) == 48);
67    assert!(offset_of!(Caps, rate_control_modes) == 52);
68    assert!(offset_of!(Caps, refused_rate_control_modes) == 56);
69    assert!(offset_of!(Caps, rate_control_status) == 60);
70    assert!(size_of::<Metal>() == 56 && align_of::<Metal>() == 8);
71    assert!(offset_of!(Metal, width) == 0);
72    assert!(offset_of!(Metal, height) == 4);
73    assert!(offset_of!(Metal, plane_width) == 8);
74    assert!(offset_of!(Metal, plane_height) == 16);
75    assert!(offset_of!(Metal, clean_x) == 24);
76    assert!(offset_of!(Metal, clean_y) == 28);
77    assert!(offset_of!(Metal, clean_width) == 32);
78    assert!(offset_of!(Metal, clean_height) == 36);
79    assert!(offset_of!(Metal, pixel_format) == 40);
80    assert!(offset_of!(Metal, plane_count) == 44);
81    assert!(offset_of!(Metal, io_surface) == 48);
82};
83#[cfg(all(target_os = "windows", feature = "hwdevice"))]
84mod d3d12;
85mod import;
86mod lifetime;
87#[cfg(feature = "hwdevice")]
88mod memory_budget;
89#[cfg(all(target_vendor = "apple", feature = "iosurface-import"))]
90mod metal;
91#[cfg(any(
92    feature = "hwdevice",
93    all(target_vendor = "apple", feature = "iosurface-import")
94))]
95mod status;
96mod surface_budget;
97mod sync;
98#[cfg(all(target_os = "linux", feature = "hwdevice"))]
99mod vulkan;
100
101#[cfg(all(target_os = "android", feature = "ahb-import"))]
102pub use android::{
103    AhbImportError, AhbImporter, AhbPlanes, ImportedAhbFrame, MAX_CONVERSIONS_IN_FLIGHT,
104    reopen_for_ahb_import,
105};
106pub use context::NativeGpuIdentity;
107#[cfg(all(target_os = "windows", feature = "hwdevice"))]
108pub use d3d12::{
109    D3D12AdapterIdentity, D3D12CopyLease, D3D12EncodeCodec, D3D12EncodeCopier, D3D12EncodeExtent,
110    ImportedD3D12Frame, SharedD3D12Context, SharedD3D12Error, d3d12_adapter_identity,
111    gpu_wait_timeout as d3d12_wait_timeout, hresult_text,
112};
113pub use import::{ImportedPlaneDescriptor, NativePlaneLayout};
114pub use lifetime::{NativeSurfaceLease, SurfaceIdentity};
115#[cfg(feature = "hwdevice")]
116pub use memory_budget::adapter_memory_budget;
117#[cfg(all(target_vendor = "apple", feature = "iosurface-import"))]
118pub use metal::{
119    ImportedMetalFrame, MetalEncoderPlanes, MetalImportError, MetalImportLease, SharedMetalContext,
120    SharedMetalError, gpu_wait_timeout as metal_wait_timeout, pixel_buffer_surface_id,
121};
122#[cfg(all(target_os = "macos", feature = "hwdevice"))]
123pub use metal::{VideoToolboxCodec, videotoolbox_hardware_decodes, videotoolbox_surface_id};
124pub use surface_budget::{SurfaceBudget, SurfaceBudgetError, SurfaceReservation, SurfaceUsage};
125pub use sync::{ExternalSyncPoint, NativeImageState};
126#[cfg(all(target_os = "linux", feature = "hwdevice"))]
127pub use vulkan::{
128    ImportTicketStats, ImportedVulkanFrame, SharedVulkanChoice, SharedVulkanContext,
129    SharedVulkanError, VideoEncodeExtent, VulkanCandidate, VulkanEncodeCopier, VulkanImportLease,
130    VulkanLayerVersion, adapter_pinned, choose_shared_vulkan, probe_vulkan_submission,
131    shared_device_index, vulkan_candidates,
132};
133
134/// Sets how long the platform interop helpers wait on GPU work before failing with a timeout.
135///
136/// Defaults to 30 seconds. A helper whose work did not finish in time leaks its GPU objects
137/// rather than freeing memory the device may still use.
138pub fn set_gpu_wait_timeout(timeout: std::time::Duration) {
139    #[cfg(all(target_os = "linux", feature = "hwdevice"))]
140    vulkan::set_wait_timeout(timeout);
141    #[cfg(all(target_os = "windows", feature = "hwdevice"))]
142    d3d12::set_wait_timeout(timeout);
143    #[cfg(all(target_vendor = "apple", feature = "iosurface-import"))]
144    metal::set_wait_timeout(timeout);
145    #[cfg(all(target_os = "android", feature = "ahb-import"))]
146    android::set_wait_timeout(timeout);
147    let _ = timeout;
148}
149
150/// `timeout` in whole milliseconds for a Win32 wait. `0xFFFFFFFF` means `INFINITE`, so longer
151/// values clamp one below it and no setting makes a wait unbounded.
152#[cfg_attr(
153    not(all(target_os = "windows", feature = "hwdevice")),
154    allow(dead_code)
155)]
156fn win32_wait_milliseconds(timeout: std::time::Duration) -> u32 {
157    const INFINITE: u32 = u32::MAX;
158    u32::try_from(timeout.as_millis())
159        .unwrap_or(INFINITE)
160        .min(INFINITE - 1)
161}
162
163#[cfg(test)]
164mod tests {
165    use std::time::Duration;
166
167    #[test]
168    fn no_wait_timeout_reaches_win32_as_infinite() {
169        let milliseconds =
170            |value: u64| super::win32_wait_milliseconds(Duration::from_millis(value));
171        assert_eq!(milliseconds(30_000), 30_000);
172        assert_eq!(milliseconds(u64::from(u32::MAX) - 1), u32::MAX - 1);
173        assert_eq!(milliseconds(u64::from(u32::MAX)), u32::MAX - 1);
174        assert_eq!(milliseconds(u64::from(u32::MAX) + 1), u32::MAX - 1);
175        assert_eq!(milliseconds(u64::MAX), u32::MAX - 1);
176        assert_eq!(super::win32_wait_milliseconds(Duration::MAX), u32::MAX - 1);
177        // Below a millisecond is no wait, as `Duration::as_millis` rounds it.
178        assert_eq!(
179            super::win32_wait_milliseconds(Duration::from_micros(999)),
180            0
181        );
182    }
183}