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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
use crate::Error;
use crate::egl::types::{EGLAttrib, EGLDisplay};
use crate::egl;
use crate::info::GLApi;
use crate::platform::generic::egl::device::EGL_FUNCTIONS;
use crate::platform::generic::egl::ffi::EGL_PLATFORM_WAYLAND_KHR;
use super::device::{Adapter, Device, NativeDevice};
use super::surface::NativeWidget;
use euclid::default::Size2D;
use std::os::raw::c_void;
use std::ptr;
use std::sync::Arc;
use wayland_sys::client::{WAYLAND_CLIENT_HANDLE, wl_display, wl_proxy};
#[cfg(feature = "sm-winit")]
use winit::Window;
#[cfg(feature = "sm-winit")]
use winit::os::unix::WindowExt;
#[derive(Clone)]
pub struct Connection {
pub(crate) native_connection: Arc<NativeConnectionWrapper>,
}
pub(crate) struct NativeConnectionWrapper {
pub(crate) egl_display: EGLDisplay,
wayland_display: Option<*mut wl_display>,
}
pub struct NativeConnection(pub EGLDisplay);
unsafe impl Send for Connection {}
impl Connection {
#[inline]
pub fn new() -> Result<Connection, Error> {
unsafe {
let wayland_display = (WAYLAND_CLIENT_HANDLE.wl_display_connect)(ptr::null());
Connection::from_wayland_display(wayland_display, true)
}
}
pub unsafe fn from_native_connection(native_connection: NativeConnection)
-> Result<Connection, Error> {
Connection::from_egl_display(native_connection.0, None)
}
#[inline]
pub fn native_connection(&self) -> NativeConnection {
NativeConnection(self.native_connection.egl_display)
}
#[inline]
pub fn gl_api(&self) -> GLApi {
GLApi::GL
}
#[inline]
pub fn create_adapter(&self) -> Result<Adapter, Error> {
self.create_hardware_adapter()
}
#[inline]
pub fn create_hardware_adapter(&self) -> Result<Adapter, Error> {
Ok(Adapter::hardware())
}
#[inline]
pub fn create_low_power_adapter(&self) -> Result<Adapter, Error> {
Ok(Adapter::low_power())
}
#[inline]
pub fn create_software_adapter(&self) -> Result<Adapter, Error> {
Ok(Adapter::software())
}
#[inline]
pub fn create_device(&self, adapter: &Adapter) -> Result<Device, Error> {
Device::new(self, adapter)
}
#[inline]
pub unsafe fn create_device_from_native_device(&self, native_device: NativeDevice)
-> Result<Device, Error> {
Device::new(self, &native_device.adapter)
}
unsafe fn from_wayland_display(wayland_display: *mut wl_display, is_owned: bool)
-> Result<Connection, Error> {
if wayland_display.is_null() {
return Err(Error::ConnectionFailed);
}
EGL_FUNCTIONS.with(|egl| {
let display_attributes = [egl::NONE as EGLAttrib];
let egl_display = egl.GetPlatformDisplay(EGL_PLATFORM_WAYLAND_KHR,
wayland_display as *mut c_void,
display_attributes.as_ptr());
if egl_display == egl::NO_DISPLAY {
return Err(Error::DeviceOpenFailed);
}
let (mut egl_major_version, mut egl_minor_version) = (0, 0);
let ok = egl.Initialize(egl_display, &mut egl_major_version, &mut egl_minor_version);
assert_ne!(ok, egl::FALSE);
let owned_display = if is_owned { Some(wayland_display) } else { None };
Connection::from_egl_display(egl_display, owned_display)
})
}
fn from_egl_display(egl_display: EGLDisplay, wayland_display: Option<*mut wl_display>)
-> Result<Connection, Error> {
Ok(Connection {
native_connection: Arc::new(NativeConnectionWrapper { egl_display, wayland_display })
})
}
#[cfg(feature = "sm-winit")]
pub fn from_winit_window(window: &Window) -> Result<Connection, Error> {
unsafe {
let wayland_display = match window.get_wayland_display() {
Some(wayland_display) => wayland_display as *mut wl_display,
None => return Err(Error::IncompatibleWinitWindow),
};
Connection::from_wayland_display(wayland_display, false)
}
}
#[cfg(feature = "sm-winit")]
pub fn create_native_widget_from_winit_window(&self, window: &Window)
-> Result<NativeWidget, Error> {
let wayland_surface = match window.get_wayland_surface() {
Some(wayland_surface) => wayland_surface as *mut wl_proxy,
None => return Err(Error::IncompatibleNativeWidget),
};
let hidpi_factor = window.get_primary_monitor().get_hidpi_factor();
let window_size = window.get_inner_size().unwrap().to_physical(hidpi_factor);
let window_size = Size2D::new(window_size.width as i32, window_size.height as i32);
Ok(NativeWidget { wayland_surface, size: window_size })
}
pub unsafe fn create_native_widget_from_ptr(&self, raw: *mut c_void, size: Size2D<i32>) -> NativeWidget {
NativeWidget {
wayland_surface: raw as *mut wl_proxy,
size,
}
}
#[cfg(feature = "sm-raw-window-handle")]
pub fn create_native_widget_from_rwh(
&self,
raw_handle: raw_window_handle::RawWindowHandle)
-> Result<NativeWidget, Error> {
use raw_window_handle::RawWindowHandle::Wayland;
let wayland_surface = match raw_handle {
Wayland(handle) => handle.surface as *mut wl_proxy,
_ => return Err(Error::IncompatibleNativeWidget),
};
let window_size = Size2D::new(400, 500);
Ok(NativeWidget { wayland_surface, size: window_size })
}
}
impl Drop for NativeConnectionWrapper {
fn drop(&mut self) {
unsafe {
if let Some(wayland_display) = self.wayland_display {
(WAYLAND_CLIENT_HANDLE.wl_display_disconnect)(wayland_display);
}
}
}
}
impl NativeConnection {
#[inline]
pub fn current() -> Result<NativeConnection, Error> {
unsafe {
EGL_FUNCTIONS.with(|egl| {
let display = egl.GetCurrentDisplay();
if display != egl::NO_DISPLAY {
Ok(NativeConnection(display))
} else {
Err(Error::NoCurrentConnection)
}
})
}
}
}