Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add create_surface_from_handle_ref #2156

Merged
merged 2 commits into from
Mar 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 69 additions & 2 deletions vulkano-win/src/raw_window_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use vulkano::{
swapchain::{Surface, SurfaceCreationError},
};

/// Creates a vulkan surface from a generic window
/// which implements HasRawWindowHandle and thus can reveal the os-dependent handle.
/// Creates a Vulkan surface from a generic window which implements `HasRawWindowHandle` and thus
/// can reveal the OS-dependent handle.
pub fn create_surface_from_handle(
window: Arc<impl Any + Send + Sync + HasRawWindowHandle + HasRawDisplayHandle>,
instance: Arc<Instance>,
Expand Down Expand Up @@ -75,3 +75,70 @@ pub fn create_surface_from_handle(
}
}
}

/// Creates a Vulkan surface from a generic window which implements `HasRawWindowHandle` and thus
/// can reveal the OS-dependent handle, without ensuring that the window outlives the surface.
///
/// # Safety
///
/// - The passed-in `window` must outlive the created [`Surface`].
pub unsafe fn create_surface_from_handle_ref(
window: &(impl HasRawWindowHandle + HasRawDisplayHandle),
instance: Arc<Instance>,
) -> Result<Arc<Surface>, SurfaceCreationError> {
unsafe {
match window.raw_window_handle() {
RawWindowHandle::AndroidNdk(h) => {
Surface::from_android(instance, h.a_native_window, None)
}
RawWindowHandle::UiKit(_h) => {
#[cfg(target_os = "ios")]
{
// Ensure the layer is CAMetalLayer
let layer = get_metal_layer_ios(_h.ui_view);
Surface::from_ios(instance, layer, None)
}
#[cfg(not(target_os = "ios"))]
{
panic!("UiKit handle should only be used when target_os == 'ios'");
}
}
RawWindowHandle::AppKit(_h) => {
#[cfg(target_os = "macos")]
{
// Ensure the layer is CAMetalLayer
let layer = get_metal_layer_macos(_h.ns_view);
Surface::from_mac_os(instance, layer as *const (), None)
}
#[cfg(not(target_os = "macos"))]
{
panic!("AppKit handle should only be used when target_os == 'macos'");
}
}
RawWindowHandle::Wayland(h) => {
let d = match window.raw_display_handle() {
RawDisplayHandle::Wayland(d) => d,
_ => panic!("Invalid RawDisplayHandle"),
};
Surface::from_wayland(instance, d.display, h.surface, None)
}
RawWindowHandle::Win32(h) => Surface::from_win32(instance, h.hinstance, h.hwnd, None),
RawWindowHandle::Xcb(h) => {
let d = match window.raw_display_handle() {
RawDisplayHandle::Xcb(d) => d,
_ => panic!("Invalid RawDisplayHandle"),
};
Surface::from_xcb(instance, d.connection, h.window, None)
}
RawWindowHandle::Xlib(h) => {
let d = match window.raw_display_handle() {
RawDisplayHandle::Xlib(d) => d,
_ => panic!("Invalid RawDisplayHandle"),
};
Surface::from_xlib(instance, d.display, h.window, None)
}
RawWindowHandle::Web(_) => unimplemented!(),
_ => unimplemented!(),
}
}
}