Skip to main content

surfman/
connection.rs

1//! The abstract interface that all connections conform to.
2
3use crate::Error;
4use crate::GLApi;
5
6use euclid::default::Size2D;
7
8use std::os::raw::c_void;
9
10/// Methods relating to display server connections.
11pub trait Connection: Sized {
12    /// The adapter type associated with this connection.
13    type Adapter;
14    /// The device type associated with this connection.
15    type Device;
16    /// The native type associated with this connection.
17    type NativeConnection;
18    /// The native device type associated with this connection.
19    type NativeDevice;
20    /// The native widget type associated with this connection.
21    type NativeWidget;
22
23    /// Connects to the default display.
24    fn new() -> Result<Self, Error>;
25
26    /// Returns the native connection corresponding to this connection.
27    fn native_connection(&self) -> Self::NativeConnection;
28
29    /// Returns the OpenGL API flavor that this connection supports (OpenGL or OpenGL ES).
30    fn gl_api(&self) -> GLApi;
31
32    /// Returns the "best" adapter on this system, preferring high-performance hardware adapters.
33    ///
34    /// This is an alias for `Connection::create_hardware_adapter()`.
35    fn create_adapter(&self) -> Result<Self::Adapter, Error>;
36
37    /// Returns the "best" adapter on this system, preferring high-performance hardware adapters.
38    fn create_hardware_adapter(&self) -> Result<Self::Adapter, Error>;
39
40    /// Returns the "best" adapter on this system, preferring low-power hardware adapters.
41    fn create_low_power_adapter(&self) -> Result<Self::Adapter, Error>;
42
43    /// Returns the "best" adapter on this system, preferring software adapters.
44    fn create_software_adapter(&self) -> Result<Self::Adapter, Error>;
45
46    /// Opens a device.
47    fn create_device(&self, adapter: &Self::Adapter) -> Result<Self::Device, Error>;
48
49    /// Wraps an existing native device type in a device.
50    unsafe fn create_device_from_native_device(
51        &self,
52        native_device: Self::NativeDevice,
53    ) -> Result<Self::Device, Error>;
54
55    /// Opens the display connection corresponding to the given `RawDisplayHandle`.
56    #[cfg(feature = "sm-raw-window-handle-05")]
57    fn from_raw_display_handle(raw_handle: rwh_05::RawDisplayHandle) -> Result<Self, Error>;
58
59    /// Opens the display connection corresponding to the given `DisplayHandle`.
60    #[cfg(feature = "sm-raw-window-handle-06")]
61    fn from_display_handle(handle: rwh_06::DisplayHandle) -> Result<Self, Error>;
62
63    /// Creates a native widget from a raw pointer
64    unsafe fn create_native_widget_from_ptr(
65        &self,
66        raw: *mut c_void,
67        size: Size2D<i32>,
68    ) -> Self::NativeWidget;
69
70    /// Create a native widget type from the given `RawWindowHandle`.
71    #[cfg(feature = "sm-raw-window-handle-05")]
72    fn create_native_widget_from_raw_window_handle(
73        &self,
74        window: rwh_05::RawWindowHandle,
75        size: Size2D<i32>,
76    ) -> Result<Self::NativeWidget, Error>;
77
78    /// Create a native widget type from the given `WindowHandle`.
79    #[cfg(feature = "sm-raw-window-handle-06")]
80    fn create_native_widget_from_window_handle(
81        &self,
82        window: rwh_06::WindowHandle,
83        size: Size2D<i32>,
84    ) -> Result<Self::NativeWidget, Error>;
85}