-
-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy pathx11.rs
302 lines (266 loc) · 9.89 KB
/
x11.rs
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
pub use winit::os::unix::x11::{XError, XNotSupported, XConnection};
use std::{mem, ptr, fmt, error};
use std::sync::Arc;
use winit;
use winit::os::unix::{EventsLoopExt, WindowExt, WindowBuilderExt};
use {Api, ContextError, CreationError, GlAttributes, GlRequest, PixelFormat, PixelFormatRequirements};
use std::ffi::CString;
use api::glx::{ffi, Context as GlxContext};
use api::{dlopen, egl};
use api::egl::Context as EglContext;
use api::glx::ffi::glx::Glx;
use api::egl::ffi::egl::Egl;
#[derive(Debug)]
struct NoX11Connection;
impl error::Error for NoX11Connection {
fn description(&self) -> &str {
"failed to get x11 connection"
}
}
impl fmt::Display for NoX11Connection {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(error::Error::description(self))
}
}
struct GlxOrEgl {
glx: Option<Glx>,
egl: Option<Egl>,
}
impl GlxOrEgl {
fn new() -> GlxOrEgl {
// TODO: use something safer than raw "dlopen"
let glx = {
let mut libglx = unsafe {
dlopen::dlopen(b"libGL.so.1\0".as_ptr() as *const _, dlopen::RTLD_NOW)
};
if libglx.is_null() {
libglx = unsafe {
dlopen::dlopen(b"libGL.so\0".as_ptr() as *const _, dlopen::RTLD_NOW)
};
}
if libglx.is_null() {
None
} else {
Some(Glx::load_with(|sym| {
let sym = CString::new(sym).unwrap();
unsafe { dlopen::dlsym(libglx, sym.as_ptr()) }
}))
}
};
// TODO: use something safer than raw "dlopen"
let egl = {
let mut libegl = unsafe {
dlopen::dlopen(b"libEGL.so.1\0".as_ptr() as *const _, dlopen::RTLD_NOW)
};
if libegl.is_null() {
libegl = unsafe {
dlopen::dlopen(b"libEGL.so\0".as_ptr() as *const _, dlopen::RTLD_NOW)
};
}
if libegl.is_null() {
None
} else {
Some(Egl::load_with(|sym| {
let sym = CString::new(sym).unwrap();
unsafe { dlopen::dlsym(libegl, sym.as_ptr()) }
}))
}
};
GlxOrEgl {
glx: glx,
egl: egl,
}
}
}
pub enum GlContext {
Glx(GlxContext),
Egl(EglContext),
None,
}
pub struct Context {
display: Arc<XConnection>,
colormap: ffi::Colormap,
context: GlContext,
}
unsafe impl Send for Context {}
unsafe impl Sync for Context {}
impl Drop for Context {
fn drop(&mut self) {
unsafe {
// we don't call MakeCurrent(0, 0) because we are not sure that the context
// is still the current one
self.context = GlContext::None;
(self.display.xlib.XFreeColormap)(self.display.display, self.colormap);
}
}
}
impl Context {
pub fn new(
window_builder: winit::WindowBuilder,
events_loop: &winit::EventsLoop,
pf_reqs: &PixelFormatRequirements,
gl_attr: &GlAttributes<&Context>,
) -> Result<(winit::Window, Self), CreationError>
{
let display = match events_loop.get_xlib_xconnection() {
Some(display) => display,
None => return Err(CreationError::NoBackendAvailable(Box::new(NoX11Connection))),
};
// Get the screen_id for the window being built.
let screen_id = unsafe { (display.xlib.XDefaultScreen)(display.display) };
// start the context building process
enum Prototype<'a> {
Glx(::api::glx::ContextPrototype<'a>),
Egl(::api::egl::ContextPrototype<'a>),
}
let builder_clone_opengl_glx = gl_attr.clone().map_sharing(|_| unimplemented!()); // FIXME:
let builder_clone_opengl_egl = gl_attr.clone().map_sharing(|_| unimplemented!()); // FIXME:
let backend = GlxOrEgl::new();
let context = match gl_attr.version {
GlRequest::Latest |
GlRequest::Specific(Api::OpenGl, _) |
GlRequest::GlThenGles { .. } => {
// GLX should be preferred over EGL, otherwise crashes may occur
// on X11 – issue #314
if let Some(ref glx) = backend.glx {
Prototype::Glx(GlxContext::new(
glx.clone(),
&display.xlib,
pf_reqs,
&builder_clone_opengl_glx,
display.display,
screen_id,
window_builder.window.transparent,
)?)
} else if let Some(ref egl) = backend.egl {
let native_display = egl::NativeDisplay::X11(Some(display.display as *const _));
Prototype::Egl(EglContext::new(
egl.clone(),
pf_reqs,
&builder_clone_opengl_egl,
native_display,
)?)
} else {
return Err(CreationError::NotSupported("both libglx and libEGL not present"));
}
},
GlRequest::Specific(Api::OpenGlEs, _) => {
if let Some(ref egl) = backend.egl {
Prototype::Egl(EglContext::new(
egl.clone(),
pf_reqs,
&builder_clone_opengl_egl,
egl::NativeDisplay::X11(Some(display.display as *const _)),
)?)
} else {
return Err(CreationError::NotSupported("libEGL not present"));
}
},
GlRequest::Specific(_, _) => {
return Err(CreationError::NotSupported("requested specific without gl or gles"));
},
};
// getting the `visual_infos` (a struct that contains information about the visual to use)
let visual_infos = match context {
Prototype::Glx(ref p) => p.get_visual_infos().clone(),
Prototype::Egl(ref p) => {
unsafe {
let mut template: ffi::XVisualInfo = mem::zeroed();
template.visualid = p.get_native_visual_id() as ffi::VisualID;
let mut num_visuals = 0;
let vi = (display.xlib.XGetVisualInfo)(display.display, ffi::VisualIDMask,
&mut template, &mut num_visuals);
display.check_errors().expect("Failed to call XGetVisualInfo");
assert!(!vi.is_null());
assert!(num_visuals == 1);
let vi_copy = ptr::read(vi as *const _);
(display.xlib.XFree)(vi as *mut _);
vi_copy
}
},
};
let window = window_builder
.with_x11_visual(&visual_infos as *const _)
.with_x11_screen(screen_id)
.build(events_loop)?;
let xlib_window = window.get_xlib_window().unwrap();
// finish creating the OpenGL context
let context = match context {
Prototype::Glx(ctxt) => {
GlContext::Glx(ctxt.finish(xlib_window)?)
},
Prototype::Egl(ctxt) => {
GlContext::Egl(ctxt.finish(xlib_window as _)?)
},
};
// getting the root window
let root = unsafe { (display.xlib.XDefaultRootWindow)(display.display) };
display.check_errors().expect("Failed to get root window");
// creating the color map
let cmap = unsafe {
let cmap = (display.xlib.XCreateColormap)(display.display, root,
visual_infos.visual as *mut _,
ffi::AllocNone);
display.check_errors().expect("Failed to call XCreateColormap");
cmap
};
let context = Context {
display: display.clone(),
context: context,
colormap: cmap,
};
Ok((window, context))
}
#[inline]
pub unsafe fn make_current(&self) -> Result<(), ContextError> {
match self.context {
GlContext::Glx(ref ctxt) => ctxt.make_current(),
GlContext::Egl(ref ctxt) => ctxt.make_current(),
GlContext::None => Ok(())
}
}
#[inline]
pub fn is_current(&self) -> bool {
match self.context {
GlContext::Glx(ref ctxt) => ctxt.is_current(),
GlContext::Egl(ref ctxt) => ctxt.is_current(),
GlContext::None => panic!()
}
}
#[inline]
pub fn get_proc_address(&self, addr: &str) -> *const () {
match self.context {
GlContext::Glx(ref ctxt) => ctxt.get_proc_address(addr),
GlContext::Egl(ref ctxt) => ctxt.get_proc_address(addr),
GlContext::None => ptr::null()
}
}
#[inline]
pub fn swap_buffers(&self) -> Result<(), ContextError> {
match self.context {
GlContext::Glx(ref ctxt) => ctxt.swap_buffers(),
GlContext::Egl(ref ctxt) => ctxt.swap_buffers(),
GlContext::None => Ok(())
}
}
#[inline]
pub fn get_api(&self) -> Api {
match self.context {
GlContext::Glx(ref ctxt) => ctxt.get_api(),
GlContext::Egl(ref ctxt) => ctxt.get_api(),
GlContext::None => panic!()
}
}
#[inline]
pub fn get_pixel_format(&self) -> PixelFormat {
match self.context {
GlContext::Glx(ref ctxt) => ctxt.get_pixel_format(),
GlContext::Egl(ref ctxt) => ctxt.get_pixel_format(),
GlContext::None => panic!()
}
}
#[inline]
pub unsafe fn raw_handle(&self) -> &GlContext {
&self.context
}
}