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::gl::types::{GLenum, GLuint};
use crate::gl;
use crate::platform::generic::egl::context;
use crate::platform::generic::egl::surface::{EGLBackedSurface, EGLSurfaceTexture};
use crate::{Error, SurfaceAccess, SurfaceInfo, SurfaceType};
use super::context::{Context, GL_FUNCTIONS};
use super::device::Device;
use euclid::default::Size2D;
use std::marker::PhantomData;
use std::os::raw::c_void;
use wayland_sys::client::wl_proxy;
use wayland_sys::egl::{WAYLAND_EGL_HANDLE, wl_egl_window};
const SURFACE_GL_TEXTURE_TARGET: GLenum = gl::TEXTURE_2D;
#[derive(Debug)]
pub struct Surface(pub(crate) EGLBackedSurface);
#[derive(Debug)]
pub struct SurfaceTexture(pub(crate) EGLSurfaceTexture);
#[derive(Clone)]
pub struct NativeWidget {
pub(crate) wayland_surface: *mut wl_proxy,
pub(crate) size: Size2D<i32>,
}
unsafe impl Send for Surface {}
impl Device {
pub fn create_surface(&mut self,
context: &Context,
_: SurfaceAccess,
surface_type: SurfaceType<NativeWidget>)
-> Result<Surface, Error> {
match surface_type {
SurfaceType::Generic { size } => self.create_generic_surface(context, &size),
SurfaceType::Widget { native_widget } => {
unsafe {
self.create_window_surface(context,
native_widget.wayland_surface,
&native_widget.size)
}
}
}
}
fn create_generic_surface(&mut self, context: &Context, size: &Size2D<i32>)
-> Result<Surface, Error> {
let _guard = self.temporarily_make_context_current(context)?;
let context_descriptor = self.context_descriptor(context);
let context_attributes = self.context_descriptor_attributes(&context_descriptor);
GL_FUNCTIONS.with(|gl| {
Ok(Surface(EGLBackedSurface::new_generic(gl,
self.native_connection.egl_display,
context.0.egl_context,
context.0.id,
&context_attributes,
size)))
})
}
unsafe fn create_window_surface(&mut self,
context: &Context,
wayland_surface: *mut wl_proxy,
size: &Size2D<i32>)
-> Result<Surface, Error> {
let egl_window = (WAYLAND_EGL_HANDLE.wl_egl_window_create)(wayland_surface,
size.width,
size.height);
assert!(!egl_window.is_null());
let context_descriptor = self.context_descriptor(context);
let egl_config = context::egl_config_from_id(self.native_connection.egl_display,
context_descriptor.egl_config_id);
Ok(Surface(EGLBackedSurface::new_window(self.native_connection.egl_display,
egl_config,
egl_window as *mut c_void,
context.0.id,
size)))
}
pub fn create_surface_texture(&self, context: &mut Context, surface: Surface)
-> Result<SurfaceTexture, (Error, Surface)> {
let _guard = match self.temporarily_make_context_current(context) {
Ok(guard) => guard,
Err(err) => return Err((err, surface)),
};
GL_FUNCTIONS.with(|gl| {
match surface.0.to_surface_texture(gl) {
Ok(surface_texture) => Ok(SurfaceTexture(surface_texture)),
Err((err, surface)) => Err((err, Surface(surface))),
}
})
}
pub fn destroy_surface(&self, context: &mut Context, surface: &mut Surface)
-> Result<(), Error> {
GL_FUNCTIONS.with(|gl| {
let egl_display = self.native_connection.egl_display;
if let Some(wayland_egl_window) = surface.0.destroy(gl, egl_display, context.0.id)? {
unsafe {
let wayland_egl_window = wayland_egl_window as *mut wl_egl_window;
(WAYLAND_EGL_HANDLE.wl_egl_window_destroy)(wayland_egl_window);
}
}
Ok(())
})
}
pub fn destroy_surface_texture(&self, context: &mut Context, surface_texture: SurfaceTexture)
-> Result<Surface, (Error, SurfaceTexture)> {
match self.temporarily_make_context_current(context) {
Ok(_guard) => GL_FUNCTIONS.with(|gl| Ok(Surface(surface_texture.0.destroy(gl)))),
Err(err) => Err((err, surface_texture)),
}
}
pub fn present_surface(&self, context: &Context, surface: &mut Surface) -> Result<(), Error> {
surface.0.present(self.native_connection.egl_display, context.0.egl_context)
}
pub fn resize_surface(&self, _context: &Context, surface: &mut Surface, size: Size2D<i32>) -> Result<(), Error> {
let wayland_egl_window = surface.0.native_window()? as *mut c_void as *mut wl_egl_window;
unsafe { (WAYLAND_EGL_HANDLE.wl_egl_window_resize)(wayland_egl_window, size.width, size.height, 0, 0) };
surface.0.size = size;
Ok(())
}
#[inline]
pub fn lock_surface_data<'s>(&self, _: &'s mut Surface)
-> Result<SurfaceDataGuard<'s>, Error> {
Err(Error::Unimplemented)
}
#[inline]
pub fn surface_gl_texture_target(&self) -> GLenum {
SURFACE_GL_TEXTURE_TARGET
}
pub fn surface_info(&self, surface: &Surface) -> SurfaceInfo {
surface.0.info()
}
#[inline]
pub fn surface_texture_object(&self, surface_texture: &SurfaceTexture) -> GLuint {
surface_texture.0.texture_object
}
}
pub struct SurfaceDataGuard<'a> {
phantom: PhantomData<&'a ()>,
}