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