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 widget type associated with this connection.
17    type NativeWidget;
18
19    /// Connects to the default display.
20    fn new() -> Result<Self, Error>;
21
22    /// Returns the OpenGL API flavor that this connection supports (OpenGL or OpenGL ES).
23    fn gl_api(&self) -> GLApi;
24
25    /// Returns the "best" adapter on this system, preferring high-performance hardware adapters.
26    ///
27    /// This is an alias for `Connection::create_hardware_adapter()`.
28    fn create_adapter(&self) -> Result<Self::Adapter, Error>;
29
30    /// Returns the "best" adapter on this system, preferring high-performance hardware adapters.
31    fn create_hardware_adapter(&self) -> Result<Self::Adapter, Error>;
32
33    /// Returns the "best" adapter on this system, preferring low-power hardware adapters.
34    fn create_low_power_adapter(&self) -> Result<Self::Adapter, Error>;
35
36    /// Returns the "best" adapter on this system, preferring software adapters.
37    fn create_software_adapter(&self) -> Result<Self::Adapter, Error>;
38
39    /// Opens a device.
40    fn create_device(&self, adapter: &Self::Adapter) -> Result<Self::Device, Error>;
41
42    /// Opens the display connection corresponding to the given `DisplayHandle`.
43    fn from_display_handle(handle: raw_window_handle::DisplayHandle) -> Result<Self, Error>;
44
45    /// Creates a native widget from a raw pointer
46    unsafe fn create_native_widget_from_ptr(
47        &self,
48        raw: *mut c_void,
49        size: Size2D<i32>,
50    ) -> Self::NativeWidget;
51
52    /// Create a native widget type from the given `WindowHandle`.
53    fn create_native_widget_from_window_handle(
54        &self,
55        window: raw_window_handle::WindowHandle,
56        size: Size2D<i32>,
57    ) -> Result<Self::NativeWidget, Error>;
58}