Skip to content

Commit

Permalink
Auto merge of #250 - declantsien:conn_from_raw_handle, r=jdm
Browse files Browse the repository at this point in the history
Add support creating connection directly from raw handle

None
  • Loading branch information
bors-servo authored Feb 26, 2023
2 parents 50644f0 + 2fbd424 commit d7ad0ac
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 4 deletions.
6 changes: 6 additions & 0 deletions surfman/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ pub trait Connection: Sized {
#[cfg(feature = "sm-winit")]
fn from_winit_window(window: &Window) -> Result<Self, Error>;

/// Opens the display connection corresponding to the given raw display handle.
#[cfg(feature = "sm-raw-window-handle")]
fn from_raw_display_handle(
raw_handle: raw_window_handle::RawDisplayHandle,
) -> Result<Self, Error>;

/// Creates a native widget type from the given `winit` window.
///
/// This type can be later used to create surfaces that render to the window.
Expand Down
2 changes: 2 additions & 0 deletions surfman/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ pub enum Error {
IncompatibleNativeWidget,
/// The `winit` window is incompatible with this backend.
IncompatibleWinitWindow,
/// The `raw display handle` is incompatible with this backend.
IncompatibleRawDisplayHandle,
/// The native context does not match the supplied device.
IncompatibleNativeContext,
/// The native device does not match the supplied connection.
Expand Down
8 changes: 8 additions & 0 deletions surfman/src/implementation/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ impl ConnectionInterface for Connection {
Connection::from_winit_window(window)
}

#[inline]
#[cfg(feature = "sm-raw-window-handle")]
fn from_raw_display_handle(
raw_handle: raw_window_handle::RawDisplayHandle,
) -> Result<Connection, Error> {
Connection::from_raw_display_handle(raw_handle)
}

#[inline]
#[cfg(feature = "sm-winit")]
fn create_native_widget_from_winit_window(
Expand Down
8 changes: 8 additions & 0 deletions surfman/src/platform/android/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ impl Connection {
Ok(Connection)
}

/// Opens the display connection corresponding to the given raw display handle.
#[cfg(feature = "sm-raw-window-handle")]
pub fn from_raw_display_handle(
_: raw_window_handle::RawDisplayHandle,
) -> Result<Connection, Error> {
Ok(Connection)
}

/// Creates a native widget type from the given `winit` window.
///
/// This type can be later used to create surfaces that render to the window.
Expand Down
20 changes: 20 additions & 0 deletions surfman/src/platform/generic/multi/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,19 @@ where
}
}

/// Opens the display connection corresponding to the given raw display handle.
#[cfg(feature = "sm-raw-window-handle")]
pub fn from_raw_display_handle(
raw_handle: raw_window_handle::RawDisplayHandle,
) -> Result<Connection<Def, Alt>, Error> {
match <Def::Connection>::from_raw_display_handle(raw_handle) {
Ok(connection) => Ok(Connection::Default(connection)),
Err(_) => {
<Alt::Connection>::from_raw_display_handle(raw_handle).map(Connection::Alternate)
}
}
}

/// Creates a native widget type from the given `winit` window.
///
/// This type can be later used to create surfaces that render to the window.
Expand Down Expand Up @@ -309,6 +322,13 @@ where
Connection::from_winit_window(window)
}

#[cfg(feature = "sm-raw-window-handle")]
fn from_raw_display_handle(
raw_handle: raw_window_handle::RawDisplayHandle,
) -> Result<Connection<Def, Alt>, Error> {
Connection::from_raw_display_handle(raw_handle)
}

#[cfg(feature = "sm-winit")]
fn create_native_widget_from_winit_window(
&self,
Expand Down
12 changes: 10 additions & 2 deletions surfman/src/platform/macos/cgl/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ impl Connection {
SystemConnection::from_winit_window(window).map(Connection)
}

/// Opens the display connection corresponding to the given raw display handle.
#[cfg(feature = "sm-raw-window-handle")]
pub fn from_raw_display_handle(
raw_handle: raw_window_handle::RawDisplayHandle,
) -> Result<Connection, Error> {
SystemConnection::from_raw_display_handle(raw_handle).map(Connection)
}

/// Creates a native widget type from the given `winit` window.
///
/// This type can be later used to create surfaces that render to the window.
Expand Down Expand Up @@ -136,10 +144,10 @@ impl Connection {
&self,
raw_handle: raw_window_handle::RawWindowHandle,
) -> Result<NativeWidget, Error> {
use raw_window_handle::RawWindowHandle::MacOS;
use raw_window_handle::RawWindowHandle::AppKit;

match raw_handle {
MacOS(handle) => Ok(NativeWidget {
AppKit(handle) => Ok(NativeWidget {
view: NSView(unsafe { msg_send![handle.ns_view as id, retain] }),
}),
_ => Err(Error::IncompatibleNativeWidget),
Expand Down
12 changes: 10 additions & 2 deletions surfman/src/platform/macos/system/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ impl Connection {
Connection::new()
}

/// Opens the display connection corresponding to the given raw display handle.
#[cfg(feature = "sm-raw-window-handle")]
pub fn from_raw_display_handle(
_: raw_window_handle::RawDisplayHandle,
) -> Result<Connection, Error> {
Connection::new()
}

/// Creates a native widget type from the given `winit` window.
///
/// This type can be later used to create surfaces that render to the window.
Expand Down Expand Up @@ -166,10 +174,10 @@ impl Connection {
&self,
raw_handle: raw_window_handle::RawWindowHandle,
) -> Result<NativeWidget, Error> {
use raw_window_handle::RawWindowHandle::MacOS;
use raw_window_handle::RawWindowHandle::AppKit;

match raw_handle {
MacOS(handle) => Ok(NativeWidget {
AppKit(handle) => Ok(NativeWidget {
view: NSView(unsafe { msg_send![handle.ns_view as id, retain] }),
}),
_ => Err(Error::IncompatibleNativeWidget),
Expand Down
8 changes: 8 additions & 0 deletions surfman/src/platform/unix/generic/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ impl Connection {
Err(Error::IncompatibleNativeWidget)
}

/// Opens the display connection corresponding to the given raw display handle.
#[cfg(feature = "sm-raw-window-handle")]
pub fn from_raw_display_handle(
_: raw_window_handle::RawDisplayHandle,
) -> Result<Connection, Error> {
Err(Error::IncompatibleNativeWidget)
}

/// Creates a native widget type from the given `winit` window.
///
/// This type can be later used to create surfaces that render to the window.
Expand Down
17 changes: 17 additions & 0 deletions surfman/src/platform/unix/wayland/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,23 @@ impl Connection {
}
}

/// Opens the display connection corresponding to the given raw display handle.
#[cfg(feature = "sm-raw-window-handle")]
pub fn from_raw_display_handle(
raw_handle: raw_window_handle::RawDisplayHandle,
) -> Result<Connection, Error> {
use raw_window_handle::RawDisplayHandle::Wayland;
use raw_window_handle::WaylandDisplayHandle;
unsafe {
let wayland_display = match raw_handle {
Wayland(WaylandDisplayHandle { display, .. }) => display as *mut wl_display,
_ => return Err(Error::IncompatibleRawDisplayHandle),
};

Connection::from_wayland_display(wayland_display, false)
}
}

/// Creates a native widget type from the given `winit` window.
///
/// This type can be later used to create surfaces that render to the window.
Expand Down
17 changes: 17 additions & 0 deletions surfman/src/platform/unix/x11/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,23 @@ impl Connection {
}
}

/// Opens the display connection corresponding to the given raw display handle.
#[cfg(feature = "sm-raw-window-handle")]
pub fn from_raw_display_handle(
raw_handle: raw_window_handle::RawDisplayHandle,
) -> Result<Connection, Error> {
use raw_window_handle::RawDisplayHandle::Xcb;
use raw_window_handle::RawDisplayHandle::Xlib;
use raw_window_handle::XlibDisplayHandle;
let display = match raw_handle {
Xlib(XlibDisplayHandle { display, .. }) => display as *mut Display,
Xcb(_) => return Err(Error::Unimplemented),
_ => return Err(Error::IncompatibleRawDisplayHandle),
};

Connection::from_x11_display(display, false)
}

/// Creates a native widget type from the given `winit` window.
///
/// This type can be later used to create surfaces that render to the window.
Expand Down
8 changes: 8 additions & 0 deletions surfman/src/platform/windows/angle/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ impl Connection {
Connection::new()
}

/// Opens the display connection corresponding to the given raw display handle.
#[cfg(feature = "sm-raw-window-handle")]
pub fn from_raw_display_handle(
_: raw_window_handle::RawDisplayHandle,
) -> Result<Connection, Error> {
Connection::new()
}

/// Creates a native widget type from the given `winit` window.
///
/// This type can be later used to create surfaces that render to the window.
Expand Down
8 changes: 8 additions & 0 deletions surfman/src/platform/windows/wgl/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ impl Connection {
Connection::new()
}

/// Opens the display connection corresponding to the given raw display handle.
#[cfg(feature = "sm-raw-window-handle")]
pub fn from_raw_display_handle(
_: raw_window_handle::RawDisplayHandle,
) -> Result<Connection, Error> {
Connection::new()
}

/// Creates a native widget type from the given `winit` window.
///
/// This type can be later used to create surfaces that render to the window.
Expand Down

0 comments on commit d7ad0ac

Please sign in to comment.