Skip to content

Commit

Permalink
Properly feature gate drm support and add safety docs
Browse files Browse the repository at this point in the history
  • Loading branch information
morr0ne committed Feb 28, 2025
1 parent e4dab9e commit 978f5a5
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 11 deletions.
51 changes: 42 additions & 9 deletions wgpu-core/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,17 @@ impl Instance {
}
}

/// Creates a new surface from the given drm configuration.
///
/// # Safety
///
/// - All parameters must point to valid DRM values.
///
/// # Platform Support
///
/// This function is only available on Unix-like platforms (Linux, FreeBSD) and
/// currently only works with the Vulkan backend.
#[cfg(all(unix, not(target_os = "macos")))]
pub unsafe fn create_surface_from_drm(
&self,
fd: i32,
Expand All @@ -217,16 +228,28 @@ impl Instance {
let mut surface_per_backend: HashMap<Backend, Box<dyn hal::DynSurface>> =
HashMap::default();

let instance = unsafe { self.as_hal::<hal::api::Vulkan>() }
.ok_or(CreateSurfaceError::BackendNotEnabled(Backend::Vulkan))?;

match instance.create_surface_from_drm(fd, plane, connector_id, width, height, refresh_rate)
#[cfg(feature = "vulkan")]
{
Ok(surface) => {
surface_per_backend.insert(Backend::Vulkan, Box::new(surface));
}
Err(err) => {
errors.insert(Backend::Vulkan, err);
let instance = unsafe { self.as_hal::<hal::api::Vulkan>() }
.ok_or(CreateSurfaceError::BackendNotEnabled(Backend::Vulkan))?;

// Safety must be upheld by the caller
match unsafe {
instance.create_surface_from_drm(
fd,
plane,
connector_id,
width,
height,
refresh_rate,
)
} {
Ok(surface) => {
surface_per_backend.insert(Backend::Vulkan, Box::new(surface));
}
Err(err) => {
errors.insert(Backend::Vulkan, err);
}
}
}

Expand Down Expand Up @@ -814,6 +837,16 @@ impl Global {
Ok(id)
}

/// Creates a new surface from the given drm configuration.
///
/// # Safety
///
/// - All parameters must point to valid DRM values.
///
/// # Platform Support
///
/// This function is only available on Unix-like platforms (Linux, FreeBSD) and
/// currently only works with the Vulkan backend.
pub unsafe fn instance_create_surface_from_drm(
&self,
fd: i32,
Expand Down
7 changes: 6 additions & 1 deletion wgpu-hal/src/vulkan/drm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ use std::{string::ToString, vec::Vec};
use ash::{ext, khr, vk};

impl super::Instance {
pub fn create_surface_from_drm(
/// Creates a new surface from the given drm configuration.
///
/// # Safety
///
/// - All parameters must point to valid DRM values.
pub unsafe fn create_surface_from_drm(
&self,
fd: i32,
plane: u32,
Expand Down
4 changes: 3 additions & 1 deletion wgpu-hal/src/vulkan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ mod adapter;
mod command;
mod conv;
mod device;
mod drm;
mod instance;
mod sampler;

#[cfg(all(unix, not(target_os = "macos")))]
mod drm;

use std::{
borrow::Borrow,
boxed::Box,
Expand Down
1 change: 1 addition & 0 deletions wgpu/src/api/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ pub enum SurfaceTargetUnsafe {
///
/// - All parameters must point to valid DRM values and remain valid for as long as the resulting [`Surface`] exists.
/// - The file descriptor (`fd`), plane, connector, and mode configuration must be valid and compatible.
#[cfg(all(unix, not(target_os = "macos")))]
Drm {
/// The file descriptor of the DRM device.
fd: i32,
Expand Down
1 change: 1 addition & 0 deletions wgpu/src/backend/wgpu_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,7 @@ impl dispatch::InstanceInterface for ContextWgpuCore {
.instance_create_surface(raw_display_handle, raw_window_handle, None)
},

#[cfg(all(unix, not(target_os = "macos")))]
SurfaceTargetUnsafe::Drm {
fd,
plane,
Expand Down

0 comments on commit 978f5a5

Please sign in to comment.