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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#![allow(non_camel_case_types, non_snake_case, dead_code)]

pub type EGLNativeDisplayType = *mut ();
pub type EGLNativePixmapType = ::std::os::raw::c_ulong;
pub type EGLNativeWindowType = ::std::os::raw::c_ulong;

pub use core::ptr::null_mut;

pub const EGL_NO_CONTEXT: EGLContext = 0 as EGLContext;
pub const EGL_NO_SURFACE: EGLSurface = 0 as EGLSurface;

pub const EGL_WINDOW_BIT: u32 = 4;

pub const EGL_OPENGL_ES2_BIT: u32 = 4;

pub const EGL_SUCCESS: u32 = 12288;
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_SURFACE_TYPE: u32 = 12339;
pub const EGL_NONE: u32 = 12344;
pub const EGL_RENDERABLE_TYPE: u32 = 12352;
pub const EGL_HEIGHT: u32 = 12374;
pub const EGL_WIDTH: u32 = 12375;
pub const EGL_CONTEXT_CLIENT_VERSION: u32 = 12440;
pub const EGL_OPENGL_ES_API: u32 = 12448;

pub const EGL_GL_TEXTURE_2D_KHR: u32 = 12465;

pub const EGL_PLATFORM_X11_EXT: u32 = 12757;
pub const EGL_PLATFORM_GBM_KHR: u32 = 12759;

pub const EGL_LINUX_DMA_BUF_EXT: u32 = 12912;
pub const EGL_LINUX_DRM_FOURCC_EXT: u32 = 12913;
pub const EGL_DMA_BUF_PLANE0_FD_EXT: u32 = 12914;
pub const EGL_DMA_BUF_PLANE0_OFFSET_EXT: u32 = 12915;
pub const EGL_DMA_BUF_PLANE0_PITCH_EXT: u32 = 12916;
pub const EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT: u32 = 13379;
pub const EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT: u32 = 13380;

pub type NativeDisplayType = EGLNativeDisplayType;
pub type NativePixmapType = EGLNativePixmapType;
pub type NativeWindowType = EGLNativeWindowType;
pub type EGLint = i32;
pub type EGLuint64KHR = u64;
pub type EGLenum = ::std::os::raw::c_uint;
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 EGLClientBuffer = *mut ::std::os::raw::c_void;
pub type EGLImageKHR = *mut ::std::os::raw::c_void;
pub type __eglMustCastToProperFunctionPointerType = ::std::option::Option<unsafe extern "C" fn()>;
pub type PFNEGLBINDAPIPROC = ::std::option::Option<unsafe extern "C" fn(api: EGLenum) -> EGLBoolean>;
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 PFNEGLCREATEIMAGEKHRPROC = ::std::option::Option<
unsafe extern "C" fn(
    dpy: EGLDisplay,
    ctx: EGLContext,
    target: EGLenum,
    buffer: EGLClientBuffer,
    attrib_list: *const EGLint,
) -> EGLImageKHR,
>;
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,
) -> *mut ::std::os::raw::c_void,
>;
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 type PFNEGLGETPLATFORMDISPLAYEXTPROC = ::std::option::Option<
unsafe extern "C" fn(
    platform: EGLenum,
    native_display: *mut ::std::os::raw::c_void,
    attrib_list: *const EGLint,
) -> EGLDisplay,
>;

pub type PFNEGLEXPORTDMABUFIMAGEQUERYMESAPROC = ::std::option::Option<
unsafe extern "C" fn(
    dpy: EGLDisplay,
    image: EGLImageKHR,
    fourcc: *mut i32,
    num_planes: *mut i32,
    modifiers: *mut EGLuint64KHR,
) -> EGLBoolean,
>;
pub type PFNEGLEXPORTDMABUFIMAGEMESAPROC = ::std::option::Option<
unsafe extern "C" fn(
    dpy: EGLDisplay,
    image: EGLImageKHR,
    fds: *mut i32,
    strides: *mut EGLint,
    offsets: *mut EGLint,
) -> EGLBoolean,
>;

// HACK(eddyb) this is actually an OpenGL extension function.
type PFNGLEGLIMAGETARGETTEXTURE2DOESPROC = ::std::option::Option<
unsafe extern "C" fn(
    super::gl_sys::GLenum,
    EGLImageKHR,
),
>;

struct Module(::std::ptr::NonNull<::std::os::raw::c_void>);

pub struct LibEgl {
    pub eglBindAPI: PFNEGLBINDAPIPROC,
    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,

    pub eglCreateImageKHR: PFNEGLCREATEIMAGEKHRPROC,
    pub eglExportDMABUFImageQueryMESA: PFNEGLEXPORTDMABUFIMAGEQUERYMESAPROC,
    pub eglExportDMABUFImageMESA: PFNEGLEXPORTDMABUFIMAGEMESAPROC,
    pub eglGetPlatformDisplayEXT: PFNEGLGETPLATFORMDISPLAYEXTPROC,

    // HACK(eddyb) this is actually an OpenGL extension function.
    pub glEGLImageTargetTexture2DOES: PFNGLEGLIMAGETARGETTEXTURE2DOESPROC,

    _keep_module_alive: Module,
}

impl LibEgl {
    pub fn try_load() -> Option<LibEgl> {
        use self::super::libc_sys::{dlclose, dlopen, dlsym, RTLD_LAZY, RTLD_LOCAL};
        use std::{
            ffi::{CString, CStr},
            ptr::NonNull,
        };
        
        impl Module {
            pub fn load(path: &str) -> Result<Self,()> {
                let path = CString::new(path).unwrap();
                
                let module = unsafe {dlopen(path.as_ptr(), RTLD_LAZY | RTLD_LOCAL)};
                if module.is_null() {
                    Err(())
                } else {
                    Ok(Module(unsafe {NonNull::new_unchecked(module)}))
                }
            }
            
            pub fn get_symbol<F: Sized>(&self, name: &str) -> Result<F, ()> {
                let name = CString::new(name).unwrap();
                
                let symbol = unsafe {dlsym(self.0.as_ptr(), name.as_ptr())};
                
                if symbol.is_null() {
                    return Err(());
                }
                
                Ok(unsafe {std::mem::transmute_copy::<_, F>(&symbol)})
            }
        }
        
        impl Drop for Module {
            fn drop(&mut self) {
                unsafe {dlclose(self.0.as_ptr())};
            }
        }

        let module = Module::load("libEGL.so").or_else(|_| Module::load("libEGL.so.1")).ok()?;

        let eglGetProcAddress: PFNEGLGETPROCADDRESSPROC = module.get_symbol("eglGetProcAddress").ok();
        macro_rules! get_ext_fn {
            ($name:literal) => {
                eglGetProcAddress.and_then(|gpa| unsafe {
                    std::mem::transmute(gpa(CStr::from_bytes_with_nul(concat!($name, "\0").as_bytes()).unwrap().as_ptr()))
                })
            }
        }

        Some(LibEgl {
            eglBindAPI: module.get_symbol("eglBindAPI").ok(),
            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,
            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(),

            eglCreateImageKHR: get_ext_fn!("eglCreateImageKHR"),
            eglExportDMABUFImageQueryMESA: get_ext_fn!("eglExportDMABUFImageQueryMESA"),
            eglExportDMABUFImageMESA: get_ext_fn!("eglExportDMABUFImageMESA"),
            eglGetPlatformDisplayEXT: get_ext_fn!("eglGetPlatformDisplayEXT"),

            glEGLImageTargetTexture2DOES: get_ext_fn!("glEGLImageTargetTexture2DOES"),

            _keep_module_alive: module,
        })
    }
}

#[derive(Debug)]
pub enum EglError {
    NoDisplay,
    InitializeFailed,
    CreateContextFailed,
}

pub struct Egl {}

#[cfg(target_os="android")]
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 == /* EGL_NO_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,
        24,
        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);
    
    // find config with 8-bit rgb buffer if available, ndk sample does not trust egl spec
    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,
        /* EGL_NO_CONTEXT */ null_mut(),
        ctx_attributes.as_ptr() as _,
    );
    if context.is_null() {
        return Err(EglError::CreateContextFailed);
    }
    
    return Ok((context, config, display));
}