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
#![cfg(feature = "wayland")]
use crate::api::egl::{Context as EglContext, NativeDisplay, SurfaceType as EglSurfaceType};
use crate::{
ContextError, CreationError, GlAttributes, PixelFormat, PixelFormatRequirements, Rect,
};
use crate::platform::unix::{EventLoopWindowTargetExtUnix, WindowExtUnix};
use glutin_egl_sys as ffi;
pub use wayland_client::sys::client::wl_display;
use winit;
use winit::dpi;
use winit::event_loop::EventLoopWindowTarget;
use winit::window::{Window, WindowBuilder};
use std::ops::Deref;
use std::os::raw;
use std::sync::Arc;
pub struct EglSurface(Arc<wayland_egl::WlEglSurface>);
impl std::fmt::Debug for EglSurface {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "EglSurface(...)")
}
}
#[derive(Debug)]
pub enum Context {
Windowed(EglContext, EglSurface),
PBuffer(EglContext),
Surfaceless(EglContext),
}
impl Deref for Context {
type Target = EglContext;
fn deref(&self) -> &Self::Target {
match self {
Context::Windowed(ctx, _) => ctx,
Context::PBuffer(ctx) => ctx,
Context::Surfaceless(ctx) => ctx,
}
}
}
impl Context {
#[inline]
pub fn new_headless<T>(
el: &EventLoopWindowTarget<T>,
pf_reqs: &PixelFormatRequirements,
gl_attr: &GlAttributes<&Context>,
size: Option<dpi::PhysicalSize<u32>>,
) -> Result<Self, CreationError> {
let gl_attr = gl_attr.clone().map_sharing(|c| &**c);
let display_ptr = el.wayland_display().unwrap() as *const _;
let native_display = NativeDisplay::Wayland(Some(display_ptr as *const _));
if let Some(size) = size {
let context = EglContext::new(
pf_reqs,
&gl_attr,
native_display,
EglSurfaceType::PBuffer,
|c, _| Ok(c[0]),
)
.and_then(|p| p.finish_pbuffer(size))?;
let context = Context::PBuffer(context);
Ok(context)
} else {
let context = EglContext::new(
pf_reqs,
&gl_attr,
native_display,
EglSurfaceType::Surfaceless,
|c, _| Ok(c[0]),
)
.and_then(|p| p.finish_surfaceless())?;
let context = Context::Surfaceless(context);
Ok(context)
}
}
#[inline]
pub fn new<T>(
wb: WindowBuilder,
el: &EventLoopWindowTarget<T>,
pf_reqs: &PixelFormatRequirements,
gl_attr: &GlAttributes<&Context>,
) -> Result<(Window, Self), CreationError> {
let win = wb.build(el)?;
let size = win.inner_size();
let (width, height): (u32, u32) = size.into();
let display_ptr = win.wayland_display().unwrap() as *const _;
let surface = win.wayland_surface();
let surface = match surface {
Some(s) => s,
None => {
return Err(CreationError::NotSupported("Wayland not found".to_string()));
}
};
let context = Self::new_raw_context(display_ptr, surface, width, height, pf_reqs, gl_attr)?;
Ok((win, context))
}
#[inline]
pub fn new_raw_context(
display_ptr: *const wl_display,
surface: *mut raw::c_void,
width: u32,
height: u32,
pf_reqs: &PixelFormatRequirements,
gl_attr: &GlAttributes<&Context>,
) -> Result<Self, CreationError> {
let egl_surface = unsafe {
wayland_egl::WlEglSurface::new_from_raw(surface as *mut _, width as i32, height as i32)
};
let context = {
let gl_attr = gl_attr.clone().map_sharing(|c| &**c);
let native_display = NativeDisplay::Wayland(Some(display_ptr as *const _));
EglContext::new(pf_reqs, &gl_attr, native_display, EglSurfaceType::Window, |c, _| {
Ok(c[0])
})
.and_then(|p| p.finish(egl_surface.ptr() as *const _))?
};
let context = Context::Windowed(context, EglSurface(Arc::new(egl_surface)));
Ok(context)
}
#[inline]
pub unsafe fn make_current(&self) -> Result<(), ContextError> {
(**self).make_current()
}
#[inline]
pub unsafe fn make_not_current(&self) -> Result<(), ContextError> {
(**self).make_not_current()
}
#[inline]
pub fn is_current(&self) -> bool {
(**self).is_current()
}
#[inline]
pub fn get_api(&self) -> crate::Api {
(**self).get_api()
}
#[inline]
pub unsafe fn raw_handle(&self) -> ffi::EGLContext {
(**self).raw_handle()
}
#[inline]
pub unsafe fn get_egl_display(&self) -> Option<*const raw::c_void> {
Some((**self).get_egl_display())
}
#[inline]
pub fn resize(&self, width: u32, height: u32) {
match self {
Context::Windowed(_, surface) => surface.0.resize(width as i32, height as i32, 0, 0),
_ => unreachable!(),
}
}
#[inline]
pub fn get_proc_address(&self, addr: &str) -> *const core::ffi::c_void {
(**self).get_proc_address(addr)
}
#[inline]
pub fn swap_buffers(&self) -> Result<(), ContextError> {
(**self).swap_buffers()
}
#[inline]
pub fn swap_buffers_with_damage(&self, rects: &[Rect]) -> Result<(), ContextError> {
(**self).swap_buffers_with_damage(rects)
}
#[inline]
pub fn swap_buffers_with_damage_supported(&self) -> bool {
(**self).swap_buffers_with_damage_supported()
}
#[inline]
pub fn get_pixel_format(&self) -> PixelFormat {
(**self).get_pixel_format().clone()
}
}