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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#![allow(non_camel_case_types, non_snake_case)]
use crate::native::module;
#[cfg(target_os = "linux")]
pub type EGLNativeDisplayType = *mut crate::native::linux_x11::libx11::Display;
#[cfg(target_os = "linux")]
pub type EGLNativePixmapType = crate::native::linux_x11::libx11::Pixmap;
#[cfg(target_os = "linux")]
pub type EGLNativeWindowType = crate::native::linux_x11::libx11::Window;
#[cfg(target_os = "android")]
pub type EGLNativeDisplayType = *mut ();
#[cfg(target_os = "android")]
pub type EGLNativePixmapType = ::std::os::raw::c_ulong;
#[cfg(target_os = "android")]
pub type EGLNativeWindowType = ::std::os::raw::c_ulong;
pub use core::ptr::null_mut;
pub const EGL_SUCCESS: u32 = 12288;
pub const EGL_WINDOW_BIT: u32 = 4;
pub const EGL_ALPHA_SIZE: u32 = 12321;
pub const EGL_BLUE_SIZE: u32 = 12322;
pub const EGL_GREEN_SIZE: u32 = 12323;
pub const EGL_RED_SIZE: u32 = 12324;
pub const EGL_DEPTH_SIZE: u32 = 12325;
pub const EGL_STENCIL_SIZE: u32 = 12326;
pub const EGL_NATIVE_VISUAL_ID: u32 = 12334;
pub const EGL_WIDTH: u32 = 12375;
pub const EGL_HEIGHT: u32 = 12374;
pub const EGL_SURFACE_TYPE: u32 = 12339;
pub const EGL_NONE: u32 = 12344;
pub const EGL_CONTEXT_CLIENT_VERSION: u32 = 12440;
pub type NativeDisplayType = EGLNativeDisplayType;
pub type NativePixmapType = EGLNativePixmapType;
pub type NativeWindowType = EGLNativeWindowType;
pub type EGLint = i32;
pub type EGLBoolean = ::std::os::raw::c_uint;
pub type EGLDisplay = *mut ::std::os::raw::c_void;
pub type EGLConfig = *mut ::std::os::raw::c_void;
pub type EGLSurface = *mut ::std::os::raw::c_void;
pub type EGLContext = *mut ::std::os::raw::c_void;
pub type __eglMustCastToProperFunctionPointerType = ::std::option::Option<unsafe extern "C" fn()>;
pub type PFNEGLCHOOSECONFIGPROC = ::std::option::Option<
unsafe extern "C" fn(
dpy: EGLDisplay,
attrib_list: *const EGLint,
configs: *mut EGLConfig,
config_size: EGLint,
num_config: *mut EGLint,
) -> EGLBoolean,
>;
pub type PFNEGLCOPYBUFFERSPROC = ::std::option::Option<
unsafe extern "C" fn(
dpy: EGLDisplay,
surface: EGLSurface,
target: EGLNativePixmapType,
) -> EGLBoolean,
>;
pub type PFNEGLCREATECONTEXTPROC = ::std::option::Option<
unsafe extern "C" fn(
dpy: EGLDisplay,
config: EGLConfig,
share_context: EGLContext,
attrib_list: *const EGLint,
) -> EGLContext,
>;
pub type PFNEGLCREATEPBUFFERSURFACEPROC = ::std::option::Option<
unsafe extern "C" fn(
dpy: EGLDisplay,
config: EGLConfig,
attrib_list: *const EGLint,
) -> EGLSurface,
>;
pub type PFNEGLCREATEPIXMAPSURFACEPROC = ::std::option::Option<
unsafe extern "C" fn(
dpy: EGLDisplay,
config: EGLConfig,
pixmap: EGLNativePixmapType,
attrib_list: *const EGLint,
) -> EGLSurface,
>;
pub type PFNEGLCREATEWINDOWSURFACEPROC = ::std::option::Option<
unsafe extern "C" fn(
dpy: EGLDisplay,
config: EGLConfig,
win: EGLNativeWindowType,
attrib_list: *const EGLint,
) -> EGLSurface,
>;
pub type PFNEGLDESTROYCONTEXTPROC =
::std::option::Option<unsafe extern "C" fn(dpy: EGLDisplay, ctx: EGLContext) -> EGLBoolean>;
pub type PFNEGLDESTROYSURFACEPROC =
::std::option::Option<unsafe extern "C" fn(dpy: EGLDisplay, surface: EGLSurface) -> EGLBoolean>;
pub type PFNEGLGETCONFIGATTRIBPROC = ::std::option::Option<
unsafe extern "C" fn(
dpy: EGLDisplay,
config: EGLConfig,
attribute: EGLint,
value: *mut EGLint,
) -> EGLBoolean,
>;
pub type PFNEGLGETCONFIGSPROC = ::std::option::Option<
unsafe extern "C" fn(
dpy: EGLDisplay,
configs: *mut EGLConfig,
config_size: EGLint,
num_config: *mut EGLint,
) -> EGLBoolean,
>;
pub type PFNEGLGETCURRENTDISPLAYPROC = ::std::option::Option<unsafe extern "C" fn() -> EGLDisplay>;
pub type PFNEGLGETCURRENTSURFACEPROC =
::std::option::Option<unsafe extern "C" fn(readdraw: EGLint) -> EGLSurface>;
pub type PFNEGLGETDISPLAYPROC =
::std::option::Option<unsafe extern "C" fn(display_id: EGLNativeDisplayType) -> EGLDisplay>;
pub type PFNEGLGETERRORPROC = ::std::option::Option<unsafe extern "C" fn() -> EGLint>;
pub type PFNEGLGETPROCADDRESSPROC = ::std::option::Option<
unsafe extern "C" fn(
procname: *const ::std::os::raw::c_char,
) -> __eglMustCastToProperFunctionPointerType,
>;
pub type PFNEGLINITIALIZEPROC = ::std::option::Option<
unsafe extern "C" fn(dpy: EGLDisplay, major: *mut EGLint, minor: *mut EGLint) -> EGLBoolean,
>;
pub type PFNEGLMAKECURRENTPROC = ::std::option::Option<
unsafe extern "C" fn(
dpy: EGLDisplay,
draw: EGLSurface,
read: EGLSurface,
ctx: EGLContext,
) -> EGLBoolean,
>;
pub type PFNEGLQUERYCONTEXTPROC = ::std::option::Option<
unsafe extern "C" fn(
dpy: EGLDisplay,
ctx: EGLContext,
attribute: EGLint,
value: *mut EGLint,
) -> EGLBoolean,
>;
pub type PFNEGLQUERYSTRINGPROC = ::std::option::Option<
unsafe extern "C" fn(dpy: EGLDisplay, name: EGLint) -> *const ::std::os::raw::c_char,
>;
pub type PFNEGLQUERYSURFACEPROC = ::std::option::Option<
unsafe extern "C" fn(
dpy: EGLDisplay,
surface: EGLSurface,
attribute: EGLint,
value: *mut EGLint,
) -> EGLBoolean,
>;
pub type PFNEGLSWAPBUFFERSPROC =
::std::option::Option<unsafe extern "C" fn(dpy: EGLDisplay, surface: EGLSurface) -> EGLBoolean>;
pub type PFNEGLTERMINATEPROC =
::std::option::Option<unsafe extern "C" fn(dpy: EGLDisplay) -> EGLBoolean>;
pub type PFNEGLWAITGLPROC = ::std::option::Option<unsafe extern "C" fn() -> EGLBoolean>;
pub type PFNEGLWAITNATIVEPROC =
::std::option::Option<unsafe extern "C" fn(engine: EGLint) -> EGLBoolean>;
pub type PFNEGLBINDTEXIMAGEPROC = ::std::option::Option<
unsafe extern "C" fn(dpy: EGLDisplay, surface: EGLSurface, buffer: EGLint) -> EGLBoolean,
>;
pub type PFNEGLRELEASETEXIMAGEPROC = ::std::option::Option<
unsafe extern "C" fn(dpy: EGLDisplay, surface: EGLSurface, buffer: EGLint) -> EGLBoolean,
>;
pub type PFNEGLSURFACEATTRIBPROC = ::std::option::Option<
unsafe extern "C" fn(
dpy: EGLDisplay,
surface: EGLSurface,
attribute: EGLint,
value: EGLint,
) -> EGLBoolean,
>;
pub type PFNEGLSWAPINTERVALPROC =
::std::option::Option<unsafe extern "C" fn(dpy: EGLDisplay, interval: EGLint) -> EGLBoolean>;
pub struct LibEgl {
pub module: crate::native::module::Module,
pub eglChooseConfig: PFNEGLCHOOSECONFIGPROC,
pub eglCopyBuffers: PFNEGLCOPYBUFFERSPROC,
pub eglCreateContext: PFNEGLCREATECONTEXTPROC,
pub eglCreatePbufferSurface: PFNEGLCREATEPBUFFERSURFACEPROC,
pub eglCreatePixmapSurface: PFNEGLCREATEPIXMAPSURFACEPROC,
pub eglCreateWindowSurface: PFNEGLCREATEWINDOWSURFACEPROC,
pub eglDestroyContext: PFNEGLDESTROYCONTEXTPROC,
pub eglDestroySurface: PFNEGLDESTROYSURFACEPROC,
pub eglGetConfigAttrib: PFNEGLGETCONFIGATTRIBPROC,
pub eglGetConfigs: PFNEGLGETCONFIGSPROC,
pub eglGetCurrentDisplay: PFNEGLGETCURRENTDISPLAYPROC,
pub eglGetCurrentSurface: PFNEGLGETCURRENTSURFACEPROC,
pub eglGetDisplay: PFNEGLGETDISPLAYPROC,
pub eglGetError: PFNEGLGETERRORPROC,
pub eglGetProcAddress: PFNEGLGETPROCADDRESSPROC,
pub eglInitialize: PFNEGLINITIALIZEPROC,
pub eglMakeCurrent: PFNEGLMAKECURRENTPROC,
pub eglQueryContext: PFNEGLQUERYCONTEXTPROC,
pub eglQueryString: PFNEGLQUERYSTRINGPROC,
pub eglQuerySurface: PFNEGLQUERYSURFACEPROC,
pub eglSwapBuffers: PFNEGLSWAPBUFFERSPROC,
pub eglTerminate: PFNEGLTERMINATEPROC,
pub eglWaitGL: PFNEGLWAITGLPROC,
pub eglWaitNative: PFNEGLWAITNATIVEPROC,
pub eglBindTexImage: PFNEGLBINDTEXIMAGEPROC,
pub eglReleaseTexImage: PFNEGLRELEASETEXIMAGEPROC,
pub eglSurfaceAttrib: PFNEGLSURFACEATTRIBPROC,
pub eglSwapInterval: PFNEGLSWAPINTERVALPROC,
}
impl LibEgl {
pub fn try_load() -> Option<LibEgl> {
module::Module::load("libEGL.so")
.or_else(|_| module::Module::load("libEGL.so.1"))
.map(|module| LibEgl {
eglChooseConfig: module.get_symbol("eglChooseConfig").ok(),
eglCopyBuffers: module.get_symbol("eglCopyBuffers").ok(),
eglCreateContext: module.get_symbol("eglCreateContext").ok(),
eglCreatePbufferSurface: module.get_symbol("eglCreatePbufferSurface").ok(),
eglCreatePixmapSurface: module.get_symbol("eglCreatePixmapSurface").ok(),
eglCreateWindowSurface: module.get_symbol("eglCreateWindowSurface").ok(),
eglDestroyContext: module.get_symbol("eglDestroyContext").ok(),
eglDestroySurface: module.get_symbol("eglDestroySurface").ok(),
eglGetConfigAttrib: module.get_symbol("eglGetConfigAttrib").ok(),
eglGetConfigs: module.get_symbol("eglGetConfigs").ok(),
eglGetCurrentDisplay: module.get_symbol("eglGetCurrentDisplay").ok(),
eglGetCurrentSurface: module.get_symbol("eglGetCurrentSurface").ok(),
eglGetDisplay: module.get_symbol("eglGetDisplay").ok(),
eglGetError: module.get_symbol("eglGetError").ok(),
eglGetProcAddress: module.get_symbol("eglGetProcAddress").ok(),
eglInitialize: module.get_symbol("eglInitialize").ok(),
eglMakeCurrent: module.get_symbol("eglMakeCurrent").ok(),
eglQueryContext: module.get_symbol("eglQueryContext").ok(),
eglQueryString: module.get_symbol("eglQueryString").ok(),
eglQuerySurface: module.get_symbol("eglQuerySurface").ok(),
eglSwapBuffers: module.get_symbol("eglSwapBuffers").ok(),
eglTerminate: module.get_symbol("eglTerminate").ok(),
eglWaitGL: module.get_symbol("eglWaitGL").ok(),
eglWaitNative: module.get_symbol("eglWaitNative").ok(),
eglBindTexImage: module.get_symbol("eglBindTexImage").ok(),
eglReleaseTexImage: module.get_symbol("eglReleaseTexImage").ok(),
eglSurfaceAttrib: module.get_symbol("eglSurfaceAttrib").ok(),
eglSwapInterval: module.get_symbol("eglSwapInterval").ok(),
module,
})
.ok()
}
}
#[derive(Debug)]
pub enum EglError {
NoDisplay,
InitializeFailed,
CreateContextFailed,
}
pub struct Egl {}
pub unsafe fn create_egl_context(
egl: &mut LibEgl,
display: *mut std::ffi::c_void,
alpha: bool,
) -> Result<(EGLContext, EGLConfig, EGLDisplay), EglError> {
let display = (egl.eglGetDisplay.unwrap())(display as _);
if display == null_mut() {
return Err(EglError::NoDisplay);
}
if (egl.eglInitialize.unwrap())(display, null_mut(), null_mut()) == 0 {
return Err(EglError::InitializeFailed);
}
let alpha_size = if alpha { 8 } else { 0 };
#[rustfmt::skip]
let cfg_attributes = vec![
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_ALPHA_SIZE, alpha_size,
EGL_DEPTH_SIZE, 16,
EGL_STENCIL_SIZE, 0,
EGL_NONE,
];
let mut available_cfgs: Vec<EGLConfig> = vec![null_mut(); 32];
let mut cfg_count = 0;
(egl.eglChooseConfig.unwrap())(
display,
cfg_attributes.as_ptr() as _,
available_cfgs.as_ptr() as _,
32,
&mut cfg_count as *mut _ as *mut _,
);
assert!(cfg_count > 0);
assert!(cfg_count <= 32);
let mut config: EGLConfig = null_mut();
let mut exact_cfg_found = false;
for c in &mut available_cfgs[0..cfg_count] {
let mut r: i32 = 0;
let mut g: i32 = 0;
let mut b: i32 = 0;
let mut a: i32 = 0;
let mut d: i32 = 0;
if (egl.eglGetConfigAttrib.unwrap())(display, *c, EGL_RED_SIZE as _, &mut r) == 1
&& (egl.eglGetConfigAttrib.unwrap())(display, *c, EGL_GREEN_SIZE as _, &mut g) == 1
&& (egl.eglGetConfigAttrib.unwrap())(display, *c, EGL_BLUE_SIZE as _, &mut b) == 1
&& (egl.eglGetConfigAttrib.unwrap())(display, *c, EGL_ALPHA_SIZE as _, &mut a) == 1
&& (egl.eglGetConfigAttrib.unwrap())(display, *c, EGL_DEPTH_SIZE as _, &mut d) == 1
&& r == 8
&& g == 8
&& b == 8
&& (alpha_size == 0 || a == alpha_size as _)
&& d == 16
{
exact_cfg_found = true;
config = *c;
break;
}
}
if !exact_cfg_found {
config = available_cfgs[0];
}
let ctx_attributes = vec![EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE];
let context = (egl.eglCreateContext.unwrap())(
display,
config,
null_mut(),
ctx_attributes.as_ptr() as _,
);
if context.is_null() {
return Err(EglError::CreateContextFailed);
}
return Ok((context, config, display));
}