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
use std::fmt;
use std::sync::{
atomic::{AtomicPtr, Ordering},
Arc,
};
use crate::backend::egl::{
display::{EGLDisplay, EGLDisplayHandle, PixelFormat},
ffi,
native::EGLNativeSurface,
EGLError, SwapBuffersError,
};
use slog::{debug, o};
pub struct EGLSurface {
pub(crate) display: Arc<EGLDisplayHandle>,
native: Box<dyn EGLNativeSurface + Send + 'static>,
pub(crate) surface: AtomicPtr<nix::libc::c_void>,
config_id: ffi::egl::types::EGLConfig,
pixel_format: PixelFormat,
logger: ::slog::Logger,
}
impl fmt::Debug for EGLSurface {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("EGLSurface")
.field("display", &self.display)
.field("surface", &self.surface)
.field("config_id", &self.config_id)
.field("pixel_format", &self.pixel_format)
.field("logger", &self.logger)
.finish()
}
}
unsafe impl Send for EGLSurface {}
impl EGLSurface {
pub fn new<N, L>(
display: &EGLDisplay,
pixel_format: PixelFormat,
config: ffi::egl::types::EGLConfig,
native: N,
log: L,
) -> Result<EGLSurface, EGLError>
where
N: EGLNativeSurface + Send + 'static,
L: Into<Option<::slog::Logger>>,
{
let log = crate::slog_or_fallback(log.into()).new(o!("smithay_module" => "renderer_egl"));
let surface = native.create(&display.display, config)?;
if surface == ffi::egl::NO_SURFACE {
return Err(EGLError::BadSurface);
}
Ok(EGLSurface {
display: display.display.clone(),
native: Box::new(native),
surface: AtomicPtr::new(surface as *mut _),
config_id: config,
pixel_format,
logger: log,
})
}
pub fn swap_buffers(&self) -> ::std::result::Result<(), SwapBuffersError> {
let surface = self.surface.load(Ordering::SeqCst);
let result = if !surface.is_null() {
self.native.swap_buffers(&self.display, surface)
} else {
Err(SwapBuffersError::EGLSwapBuffers(EGLError::BadSurface))
};
let is_bad_surface = matches!(
result,
Err(SwapBuffersError::EGLSwapBuffers(EGLError::BadSurface))
);
if self.native.needs_recreation() || surface.is_null() || is_bad_surface {
let previous = self
.surface
.compare_exchange(
surface,
self.native
.create(&self.display, self.config_id)
.map_err(SwapBuffersError::EGLCreateSurface)? as *mut _,
Ordering::SeqCst,
Ordering::SeqCst,
)
.expect("The surface pointer changed in between?");
if previous == surface && !surface.is_null() {
let _ = unsafe { ffi::egl::DestroySurface(**self.display, surface as *const _) };
}
result.map_err(|err| {
debug!(self.logger, "Hiding page-flip error *before* recreation: {}", err);
SwapBuffersError::EGLSwapBuffers(EGLError::BadSurface)
})
} else {
result
}
}
pub fn is_current(&self) -> bool {
let surface = self.surface.load(Ordering::SeqCst);
unsafe {
ffi::egl::GetCurrentSurface(ffi::egl::DRAW as _) == surface as *const _
&& ffi::egl::GetCurrentSurface(ffi::egl::READ as _) == surface as *const _
}
}
pub fn config_id(&self) -> ffi::egl::types::EGLConfig {
self.config_id
}
pub fn pixel_format(&self) -> PixelFormat {
self.pixel_format
}
pub fn resize(&self, width: i32, height: i32, dx: i32, dy: i32) -> bool {
self.native.resize(width, height, dx, dy)
}
}
impl Drop for EGLSurface {
fn drop(&mut self) {
unsafe {
ffi::egl::DestroySurface(**self.display, *self.surface.get_mut() as *const _);
}
}
}