1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#[cfg(target_os = "ios")]
use crate::get_metal_layer_ios;
#[cfg(target_os = "macos")]
use crate::get_metal_layer_macos;
use raw_window_handle::{
    HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle,
};
use std::{any::Any, sync::Arc};
use vulkano::{
    instance::Instance,
    swapchain::{Surface, SurfaceCreationError},
};

/// 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>,
) -> Result<Arc<Surface>, SurfaceCreationError> {
    unsafe {
        match window.raw_window_handle() {
            RawWindowHandle::AndroidNdk(h) => {
                Surface::from_android(instance, h.a_native_window, Some(window))
            }
            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, Some(window))
                }
                #[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 (), Some(window))
                }
                #[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, Some(window))
            }
            RawWindowHandle::Win32(h) => {
                Surface::from_win32(instance, h.hinstance, h.hwnd, Some(window))
            }
            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, Some(window))
            }
            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, Some(window))
            }
            RawWindowHandle::Web(_) => unimplemented!(),
            _ => unimplemented!(),
        }
    }
}