freya_winit/drivers/
mod.rs

1mod gl;
2#[cfg(all(feature = "metal", target_os = "macos"))]
3mod metal;
4#[cfg(feature = "vulkan")]
5mod vulkan;
6
7use freya_engine::prelude::Surface as SkiaSurface;
8use winit::{
9    dpi::PhysicalSize,
10    event_loop::ActiveEventLoop,
11    window::{
12        Window,
13        WindowAttributes,
14    },
15};
16
17use crate::config::WindowConfig;
18
19#[allow(clippy::large_enum_variant)]
20pub enum GraphicsDriver {
21    OpenGl(gl::OpenGLDriver),
22    #[cfg(all(feature = "metal", target_os = "macos"))]
23    Metal(metal::MetalDriver),
24    #[cfg(feature = "vulkan")]
25    Vulkan(vulkan::VulkanDriver),
26}
27
28impl GraphicsDriver {
29    pub fn new(
30        event_loop: &ActiveEventLoop,
31        window_attributes: WindowAttributes,
32        window_config: &WindowConfig,
33    ) -> (Self, Window) {
34        // Metal takes priority on macOS (native, best performance)
35        #[cfg(all(feature = "metal", target_os = "macos"))]
36        {
37            let (driver, window) =
38                metal::MetalDriver::new(event_loop, window_attributes, window_config);
39
40            return (Self::Metal(driver), window);
41        }
42
43        #[cfg(feature = "vulkan")]
44        #[allow(unreachable_code)]
45        {
46            let (driver, window) =
47                vulkan::VulkanDriver::new(event_loop, window_attributes, window_config);
48
49            return (Self::Vulkan(driver), window);
50        }
51
52        #[allow(unreachable_code)]
53        let (driver, window) = gl::OpenGLDriver::new(event_loop, window_attributes, window_config);
54
55        (Self::OpenGl(driver), window)
56    }
57
58    #[allow(unused)]
59    pub fn present(
60        &mut self,
61        size: PhysicalSize<u32>,
62        window: &Window,
63        render: impl FnOnce(&mut SkiaSurface),
64    ) {
65        match self {
66            Self::OpenGl(gl) => gl.present(window, render),
67            #[cfg(all(feature = "metal", target_os = "macos"))]
68            Self::Metal(mtl) => mtl.present(size, window, render),
69            #[cfg(feature = "vulkan")]
70            Self::Vulkan(vk) => vk.present(size, window, render),
71            _ => unimplemented!("Enable `gl` or `vulkan` features."),
72        }
73    }
74
75    #[allow(unused)]
76    pub fn resize(&mut self, size: PhysicalSize<u32>) {
77        match self {
78            Self::OpenGl(gl) => gl.resize(size),
79            #[cfg(all(feature = "metal", target_os = "macos"))]
80            Self::Metal(mtl) => mtl.resize(size),
81            #[cfg(feature = "vulkan")]
82            Self::Vulkan(vk) => vk.resize(size),
83            _ => unimplemented!("Enable `gl` or `vulkan` features."),
84        }
85    }
86}