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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
// +-----------------------------------------------------------------------------------------------+
// | Copyright 2015 Sean Kerr                                                                      |
// |                                                                                               |
// | Licensed under the Apache License, Version 2.0 (the "License");                               |
// | you may not use this file except in compliance with the License.                              |
// | You may obtain a copy of the License Author                                                   |
// |                                                                                               |
// |  http://www.apache.org/licenses/LICENSE-2.0                                                   |
// |                                                                                               |
// | Unless required by applicable law or agreed to in writing, software                           |
// | distributed under the License is distributed on an "AS IS" BASIS,                             |
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.                      |
// | See the License for the specific language governing permissions and                           |
// | limitations under the License.                                                                |
// +-----------------------------------------------------------------------------------------------+
// | Author: Sean Kerr <sean@code-box.org>                                                         |
// +-----------------------------------------------------------------------------------------------+

#![crate_name = "egl"]
#![allow(dead_code)]
#![allow(non_upper_case_globals)]

// -------------------------------------------------------------------------------------------------
// DEPENDENCIES
// -------------------------------------------------------------------------------------------------

extern crate khronos;
extern crate libc;

// rust
use std::ffi::CStr;
use std::ffi::CString;
use std::ptr;

// system
use khronos::khronos_int32_t;

use libc::{ c_void,
            int32_t,
            uint32_t };

// -------------------------------------------------------------------------------------------------
// LINKING
// -------------------------------------------------------------------------------------------------

#[link(name = "EGL")]
extern {}

// -------------------------------------------------------------------------------------------------
// GLOBAL TYPES
// -------------------------------------------------------------------------------------------------

pub type EGLBoolean           = uint32_t;
pub type EGLClientBuffer      = *mut c_void;
pub type EGLConfig            = *mut c_void;
pub type EGLContext           = *mut c_void;
pub type EGLDisplay           = *mut c_void;
pub type EGLenum              = uint32_t;
pub type EGLint               = khronos_int32_t;
pub type EGLNativeDisplayType = *mut c_void;
pub type EGLSurface           = *mut c_void;

// -------------------------------------------------------------------------------------------------
// ANDROID TYPES
// -------------------------------------------------------------------------------------------------

#[repr(C)]
#[cfg(android)]
enum android_native_window_t {}

#[repr(C)]
#[cfg(android)]
enum egl_native_pixmap_t {}

#[cfg(android)]
pub type EGLNativePixmapType = *mut egl_native_pixmap_t;

#[cfg(android)]
pub type EGLNativeWindowType = *mut android_native_window_t;

// -------------------------------------------------------------------------------------------------
// NON-ANDROID TYPES
// -------------------------------------------------------------------------------------------------

#[cfg(not(android))]
pub type EGLNativePixmapType = *mut c_void;

#[cfg(not(android))]
pub type EGLNativeWindowType = *mut c_void;

// -------------------------------------------------------------------------------------------------
// STRUCTS
// -------------------------------------------------------------------------------------------------

pub struct EGLConfigList {
    pub configs: EGLConfig,
    pub count:   int32_t
}

// -------------------------------------------------------------------------------------------------
// CONSTANTS
// -------------------------------------------------------------------------------------------------

// EGL aliases
pub const FALSE: EGLBoolean = 0;
pub const TRUE:  EGLBoolean = 1;

// out-of-band handle values
pub const DEFAULT_DISPLAY: EGLNativeDisplayType = 0 as *mut c_void;
pub const NO_CONTEXT:      EGLContext = 0 as *mut c_void;
pub const NO_DISPLAY:      EGLDisplay = 0 as *mut c_void;
pub const NO_SURFACE:      EGLSurface = 0 as *mut c_void;

// out-of-band attribute value
pub const DONT_CARE: EGLint = -1;

// errors / GetError return values
pub const SUCCESS:             EGLint = 0x3000;
pub const NOT_INITIALIZED:     EGLint = 0x3001;
pub const BAD_ACCESS:          EGLint = 0x3002;
pub const BAD_ALLOC:           EGLint = 0x3003;
pub const BAD_ATTRIBUTE:       EGLint = 0x3004;
pub const BAD_CONFIG:          EGLint = 0x3005;
pub const BAD_CONTEXT:         EGLint = 0x3006;
pub const BAD_CURRENT_SURFACE: EGLint = 0x3007;
pub const BAD_DISPLAY:         EGLint = 0x3008;
pub const BAD_MATCH:           EGLint = 0x3009;
pub const BAD_NATIVE_PIXMAP:   EGLint = 0x300A;
pub const BAD_NATIVE_WINDOW:   EGLint = 0x300B;
pub const BAD_PARAMETER:       EGLint = 0x300C;
pub const BAD_SURFACE:         EGLint = 0x300D;
pub const CONTEXT_LOST:        EGLint = 0x300E;  // EGL 1.1 - IMG_power_management

// config attributes
pub const BUFFER_SIZE:             EGLint = 0x3020;
pub const ALPHA_SIZE:              EGLint = 0x3021;
pub const BLUE_SIZE:               EGLint = 0x3022;
pub const GREEN_SIZE:              EGLint = 0x3023;
pub const RED_SIZE:                EGLint = 0x3024;
pub const DEPTH_SIZE:              EGLint = 0x3025;
pub const STENCIL_SIZE:            EGLint = 0x3026;
pub const CONFIG_CAVEAT:           EGLint = 0x3027;
pub const CONFIG_ID:               EGLint = 0x3028;
pub const LEVEL:                   EGLint = 0x3029;
pub const MAX_PBUFFER_HEIGHT:      EGLint = 0x302A;
pub const MAX_PBUFFER_PIXELS:      EGLint = 0x302B;
pub const MAX_PBUFFER_WIDTH:       EGLint = 0x302C;
pub const NATIVE_RENDERABLE:       EGLint = 0x302D;
pub const NATIVE_VISUAL_ID:        EGLint = 0x302E;
pub const NATIVE_VISUAL_TYPE:      EGLint = 0x302F;
pub const SAMPLES:                 EGLint = 0x3031;
pub const SAMPLE_BUFFERS:          EGLint = 0x3032;
pub const SURFACE_TYPE:            EGLint = 0x3033;
pub const TRANSPARENT_TYPE:        EGLint = 0x3034;
pub const TRANSPARENT_BLUE_VALUE:  EGLint = 0x3035;
pub const TRANSPARENT_GREEN_VALUE: EGLint = 0x3036;
pub const TRANSPARENT_RED_VALUE:   EGLint = 0x3037;
pub const NONE:                    EGLint = 0x3038; // attrib list terminator
pub const BIND_TO_TEXTURE_RGB:     EGLint = 0x3039;
pub const BIND_TO_TEXTURE_RGBA:    EGLint = 0x303A;
pub const MIN_SWAP_INTERVAL:       EGLint = 0x303B;
pub const MAX_SWAP_INTERVAL:       EGLint = 0x303C;
pub const LUMINANCE_SIZE:          EGLint = 0x303D;
pub const ALPHA_MASK_SIZE:         EGLint = 0x303E;
pub const COLOR_BUFFER_TYPE:       EGLint = 0x303F;
pub const RENDERABLE_TYPE:         EGLint = 0x3040;
pub const MATCH_NATIVE_PIXMAP:     EGLint = 0x3041;  // psseudo-attribute (not queryable)
pub const CONFORMANT:              EGLint = 0x3042;

// config attribute values
pub const SLOW_CONFIG:           EGLint = 0x3050;  // CONFIG_CAVEAT value
pub const NON_CONFORMANT_CONFIG: EGLint = 0x3051;  // CONFIG_CAVEAT value
pub const TRANSPARENT_RGB:       EGLint = 0x3052;  // TRANSPARENT_TYPE value
pub const RGB_BUFFER:            EGLint = 0x308E;  // COLOR_BUFFER_TYPE value
pub const LUMINANCE_BUFFER:      EGLint = 0x308F;  // COLOR_BUFFER_TYPE value

// more config attribute values, for TEXTURE_FORMAT
pub const NO_TEXTURE:   EGLint = 0x305C;
pub const TEXTURE_RGB:  EGLint = 0x305D;
pub const TEXTURE_RGBA: EGLint = 0x305E;
pub const TEXTURE_2D:   EGLint = 0x305F;

// config attribute mask bits
pub const PBUFFER_BIT:                 EGLint = 0x0001;  // SURFACE_TYPE mask bits
pub const PIXMAP_BIT:                  EGLint = 0x0002;  // SURFACE_TYPE mask bits
pub const WINDOW_BIT:                  EGLint = 0x0004;  // SURFACE_TYPE mask bits
pub const VG_COLORSPACE_LINEAR_BIT:    EGLint = 0x0020;  // SURFACE_TYPE mask bits
pub const VG_ALPHA_FORMAT_PRE_BIT:     EGLint = 0x0040;  // SURFACE_TYPE mask bits
pub const MULTISAMPLE_RESOLVE_BOX_BIT: EGLint = 0x0200;  // SURFACE_TYPE mask bits
pub const SWAP_BEHAVIOR_PRESERVED_BIT: EGLint = 0x0400;  // SURFACE_TYPE mask bits

pub const OPENGL_ES_BIT:  EGLint = 0x0001; // RENDERABLE_TYPE mask bits
pub const OPENVG_BIT:     EGLint = 0x0002; // RENDERABLE_TYPE mask bits
pub const OPENGL_ES2_BIT: EGLint = 0x0004; // RENDERABLE_TYPE mask bits
pub const OPENGL_BIT:     EGLint = 0x0008; // RENDERABLE_TYPE mask bits

// QueryString targets
pub const VENDOR:      EGLint = 0x3053;
pub const VERSION:     EGLint = 0x3054;
pub const EXTENSIONS:  EGLint = 0x3055;
pub const CLIENT_APIS: EGLint = 0x308D;

// QuerySurface / SurfaceAttrib / CreatePbufferSurface targets
pub const HEIGHT:                EGLint = 0x3056;
pub const WIDTH:                 EGLint = 0x3057;
pub const LARGEST_PBUFFER:       EGLint = 0x3058;
pub const TEXTURE_FORMAT:        EGLint = 0x3080;
pub const TEXTURE_TARGET:        EGLint = 0x3081;
pub const MIPMAP_TEXTURE:        EGLint = 0x3082;
pub const MIPMAP_LEVEL:          EGLint = 0x3083;
pub const RENDER_BUFFER:         EGLint = 0x3086;
pub const VG_COLORSPACE:         EGLint = 0x3087;
pub const VG_ALPHA_FORMAT:       EGLint = 0x3088;
pub const HORIZONTAL_RESOLUTION: EGLint = 0x3090;
pub const VERTICAL_RESOLUTION:   EGLint = 0x3091;
pub const PIXEL_ASPECT_RATIO:    EGLint = 0x3092;
pub const SWAP_BEHAVIOR:         EGLint = 0x3093;
pub const MULTISAMPLE_RESOLVE:   EGLint = 0x3099;

// RENDER_BUFFER values / BindTexImage / ReleaseTexImage buffer targets
pub const BACK_BUFFER:   EGLint = 0x3084;
pub const SINGLE_BUFFER: EGLint = 0x3085;

// OpenVG color spaces */
pub const VG_COLORSPACE_sRGB:   EGLint = 0x3089;  // VG_COLORSPACE value
pub const VG_COLORSPACE_LINEAR: EGLint = 0x308A;  // VG_COLORSPACE value

// OpenVG alpha formats
pub const VG_ALPHA_FORMAT_NONPRE: EGLint = 0x308B; // ALPHA_FORMAT value
pub const VG_ALPHA_FORMAT_PRE:    EGLint = 0x308C; // ALPHA_FORMAT value

// constant scale factor by which fractional display resolutions & aspect ratio are scaled when
// queried as integer values
pub const DISPLAY_SCALING: EGLint = 10000;

// unknown display resolution/aspect ratio
pub const UNKNOWN: EGLint = -1;

// back buffer swap behaviors
pub const BUFFER_PRESERVED: EGLint = 0x3094; // SWAP_BEHAVIOR value
pub const BUFFER_DESTROYED: EGLint = 0x3095; // SWAP_BEHAVIOR value

// CreatePbufferFromClientBuffer buffer types
pub const OPENVG_IMAGE: EGLint = 0x3096;

// QueryContext targets
pub const CONTEXT_CLIENT_TYPE: EGLint = 0x3097;

// CreateContext attributes
pub const CONTEXT_CLIENT_VERSION: EGLint = 0x3098;

// multisample resolution behaviors
pub const MULTISAMPLE_RESOLVE_DEFAULT: EGLint = 0x309A; // MULTISAMPLE_RESOLVE value
pub const MULTISAMPLE_RESOLVE_BOX:     EGLint = 0x309B; // MULTISAMPLE_RESOLVE value

// BindAPI/QueryAPI targets
pub const OPENGL_ES_API: EGLint = 0x30A0;
pub const OPENVG_API:    EGLint = 0x30A1;
pub const OPENGL_API:    EGLint = 0x30A2;

// GetCurrentSurface targets
pub const DRAW: EGLint = 0x3059;
pub const READ: EGLint = 0x305A;

// WaitNative engines
pub const CORE_NATIVE_ENGINE: EGLint = 0x305B;

// EGL 1.2 tokens renamed for consistency in EGL 1.3
pub const COLORSPACE:          EGLint = VG_COLORSPACE;
pub const ALPHA_FORMAT:        EGLint = VG_ALPHA_FORMAT;
pub const COLORSPACE_sRGB:     EGLint = VG_COLORSPACE_sRGB;
pub const COLORSPACE_LINEAR:   EGLint = VG_COLORSPACE_LINEAR;
pub const ALPHA_FORMAT_NONPRE: EGLint = VG_ALPHA_FORMAT_NONPRE;
pub const ALPHA_FORMAT_PRE:    EGLint = VG_ALPHA_FORMAT_PRE;

// -------------------------------------------------------------------------------------------------
// FUNCTIONS
// -------------------------------------------------------------------------------------------------

pub fn bind_api(api: EGLenum) -> bool {
    unsafe {
        ffi::eglBindAPI(api) == TRUE
    }
}

pub fn bind_tex_image(display: EGLDisplay, surface: EGLSurface, buffer: EGLint) -> bool {
    unsafe {
        ffi::eglBindTexImage(display, surface, buffer) == TRUE
    }
}

pub fn choose_config(display: EGLDisplay, attrib_list: &[EGLint],
                     config_size: EGLint) -> Option<EGLConfig> {
    unsafe {
        let     config: EGLConfig = ptr::null_mut();
        let mut count:  EGLint = 0;

        if ffi::eglChooseConfig(display, attrib_list.as_ptr(), config, config_size,
                                &mut count) == TRUE {
            Some(config)
        } else {
            None
        }
    }
}

pub fn copy_buffers(display: EGLDisplay, surface: EGLSurface,
                    target: EGLNativePixmapType) -> bool {
    unsafe {
        ffi::eglCopyBuffers(display, surface, target) == TRUE
    }
}

pub fn create_context(display: EGLDisplay, config: EGLConfig, share_context: EGLContext,
                      attrib_list: &[EGLint]) -> Option<EGLContext> {
    unsafe {
        let context = ffi::eglCreateContext(display, config, share_context, attrib_list.as_ptr());

        if !context.is_null() {
            Some(context)
        } else {
            None
        }
    }
}

pub fn create_pbuffer_from_client_buffer(display: EGLDisplay, buffer_type: EGLenum,
                                         buffer: EGLClientBuffer, config: EGLConfig,
                                         attrib_list: &[EGLint]) -> Option<EGLSurface> {
    unsafe {
        let surface = ffi::eglCreatePbufferFromClientBuffer(display, buffer_type, buffer, config,
                                                            attrib_list.as_ptr());

        if !surface.is_null() {
            Some(surface)
        } else {
            None
        }
    }
}

pub fn create_pbuffer_surface(display: EGLDisplay, config: EGLConfig,
                              attrib_list: &[EGLint]) -> Option<EGLSurface> {
    unsafe {
        let surface = ffi::eglCreatePbufferSurface(display, config, attrib_list.as_ptr());

        if !surface.is_null() {
            Some(surface)
        } else {
            None
        }
    }
}

pub fn create_pixmap_surface(display: EGLDisplay, config: EGLConfig,
                             pixmap: EGLNativePixmapType,
                             attrib_list: &[EGLint]) -> Option<EGLSurface> {
    unsafe {
        let surface = ffi::eglCreatePixmapSurface(display, config, pixmap, attrib_list.as_ptr());

        if !surface.is_null() {
            Some(surface)
        } else {
            None
        }
    }
}

pub fn create_window_surface(display: EGLDisplay, config: EGLConfig,
                             win: EGLNativeWindowType,
                             attrib_list: &[EGLint]) -> Option<EGLSurface> {
    unsafe {
        let surface = ffi::eglCreateWindowSurface(display, config, win, attrib_list.as_ptr());

        if !surface.is_null() {
            Some(surface)
        } else {
            None
        }
    }
}

pub fn destroy_context(display: EGLDisplay, ctx: EGLContext) -> bool {
    unsafe {
        ffi::eglDestroyContext(display, ctx) == TRUE
    }
}

pub fn destroy_surface(display: EGLDisplay, surface: EGLSurface) -> bool {
    unsafe {
        ffi::eglDestroySurface(display, surface) == TRUE
    }
}

pub fn get_config_attrib(display: EGLDisplay, config: EGLConfig,
                         attribute: EGLint, value: &mut EGLint) -> bool {
    unsafe {
        ffi::eglGetConfigAttrib(display, config, attribute, value) == TRUE
    }
}

pub fn get_configs(display: EGLDisplay, config_size: EGLint) -> EGLConfigList {
    unsafe {
        let list = EGLConfigList{ configs: ptr::null_mut(),
                                  count:   0 };

        ffi::eglGetConfigs(display, list.configs, config_size, list.count as *mut int32_t);

        list
    }
}

pub fn get_current_context() -> Option<EGLContext> {
    unsafe {
        let context = ffi::eglGetCurrentContext();

        if !context.is_null() {
            Some(context)
        } else {
            None
        }
    }
}

pub fn get_current_display() -> Option<EGLDisplay> {
    unsafe {
        let display = ffi::eglGetCurrentDisplay();

        if !display.is_null() {
            Some(display)
        } else {
            None
        }
    }
}

pub fn get_current_surface(readdraw: EGLint) -> Option<EGLSurface> {
    unsafe {
        let surface = ffi::eglGetCurrentSurface(readdraw);

        if !surface.is_null() {
            Some(surface)
        } else {
            None
        }
    }
}

pub fn get_display(display_id: EGLNativeDisplayType) -> Option<EGLDisplay> {
    unsafe {
        let display = ffi::eglGetDisplay(display_id);

        if !display.is_null() {
            Some(display)
        } else {
            None
        }
    }
}

pub fn get_error() -> EGLint {
    unsafe {
        ffi::eglGetError()
    }
}

pub fn get_proc_address(procname: &str) -> extern "C" fn() {
    unsafe {
        let string = CString::new(procname).unwrap();

        ffi::eglGetProcAddress(string.as_ptr())
    }
}

pub fn initialize(display: EGLDisplay, major: &mut EGLint, minor: &mut EGLint) -> bool {
    unsafe {
        ffi::eglInitialize(display, major, minor) == TRUE
    }
}

pub fn make_current(display: EGLDisplay, draw: EGLSurface,
                    read: EGLSurface, ctx: EGLContext) -> bool {
    unsafe {
        ffi::eglMakeCurrent(display, draw, read, ctx) == TRUE
    }
}

pub fn query_api() -> EGLenum {
    unsafe {
        ffi::eglQueryAPI()
    }
}

pub fn query_context(display: EGLDisplay, ctx: EGLContext,
                     attribute: EGLint, value: &mut EGLint) -> bool {
    unsafe {
        ffi::eglQueryContext(display, ctx, attribute, value) == TRUE
    }
}

pub fn query_string(display: EGLDisplay, name: EGLint) -> Option<&'static CStr> {
    unsafe {
        let c_str = ffi::eglQueryString(display, name);

        if !c_str.is_null() {
            Some(CStr::from_ptr(c_str))
        } else {
            None
        }
    }
}

pub fn query_surface(display: EGLDisplay, surface: EGLSurface,
                     attribute: EGLint, value: *mut EGLint) -> bool {
    unsafe {
        ffi::eglQuerySurface(display, surface, attribute, value) == TRUE
    }
}

pub fn release_tex_image(display: EGLDisplay, surface: EGLSurface, buffer: EGLint) -> bool {
    unsafe {
        ffi::eglReleaseTexImage(display, surface, buffer) == TRUE
    }
}

pub fn release_thread() -> bool {
    unsafe {
        ffi::eglReleaseThread() == TRUE
    }
}

pub fn surface_attrib(display: EGLDisplay, surface: EGLSurface,
                      attribute: EGLint, value: EGLint) -> bool {
    unsafe {
        ffi::eglSurfaceAttrib(display, surface, attribute, value) == TRUE
    }
}

pub fn swap_buffers(display: EGLDisplay, surface: EGLSurface) -> bool {
    unsafe {
        ffi::eglSwapBuffers(display, surface) == TRUE
    }
}

pub fn swap_interface(display: EGLDisplay, interval: EGLint) -> bool {
    unsafe {
        ffi::eglSwapInterface(display, interval) == TRUE
    }
}

pub fn terminate(display: EGLDisplay) -> bool {
    unsafe {
        ffi::eglTerminate(display) == TRUE
    }
}

pub fn wait_client() -> bool {
    unsafe {
        ffi::eglWaitClient() == TRUE
    }
}

pub fn wait_gl() -> bool {
    unsafe {
        ffi::eglWaitGL() == TRUE
    }
}

pub fn wait_native(engine: EGLint) -> bool {
    unsafe {
        ffi::eglWaitNative(engine) == TRUE
    }
}

// -------------------------------------------------------------------------------------------------
// FFI
// -------------------------------------------------------------------------------------------------

mod ffi {
    use libc::c_char;

    use super::{ EGLBoolean,
                 EGLClientBuffer,
                 EGLConfig,
                 EGLContext,
                 EGLDisplay,
                 EGLenum,
                 EGLint,
                 EGLNativeDisplayType,
                 EGLNativePixmapType,
                 EGLNativeWindowType,
                 EGLSurface };

    extern {
        pub fn eglBindAPI(api: EGLenum) -> EGLBoolean;

        pub fn eglBindTexImage(dpy: EGLDisplay, surface: EGLSurface, buffer: EGLint) -> EGLBoolean;

        pub fn eglChooseConfig(dpy: EGLDisplay, attrib_list: *const EGLint,
                               configs: EGLConfig, config_size: EGLint,
                               num_config: *mut EGLint) -> EGLBoolean;

        pub fn eglCopyBuffers(dpy: EGLDisplay, surface: EGLSurface,
                              target: EGLNativePixmapType) -> EGLBoolean;

        pub fn eglCreateContext(dpy: EGLDisplay, config: EGLConfig,
                                share_context: EGLContext,
                                attrib_list: *const EGLint) -> EGLContext;

        pub fn eglCreatePbufferFromClientBuffer(dpy: EGLDisplay, buftype: EGLenum,
                                                buffer: EGLClientBuffer, config: EGLConfig,
                                                attrib_list: *const EGLint) -> EGLSurface;

        pub fn eglCreatePbufferSurface(dpy: EGLDisplay, config: EGLConfig,
                                       attrib_list: *const EGLint) -> EGLSurface;

        pub fn eglCreatePixmapSurface(dpy: EGLDisplay, config: EGLConfig,
                                      pixmap: EGLNativePixmapType,
                                      attrib_list: *const EGLint) -> EGLSurface;

        pub fn eglCreateWindowSurface(dpy: EGLDisplay, config: EGLConfig,
                                      win: EGLNativeWindowType,
                                      attrib_list: *const EGLint) -> EGLSurface;

        pub fn eglDestroyContext(dpy: EGLDisplay, ctx: EGLContext) -> EGLBoolean;

        pub fn eglDestroySurface(dpy: EGLDisplay, surface: EGLSurface) -> EGLBoolean;

        pub fn eglGetConfigAttrib(dpy: EGLDisplay, config: EGLConfig,
                                  attribute: EGLint, value: *mut EGLint) -> EGLBoolean;

        pub fn eglGetConfigs(dpy: EGLDisplay, configs: EGLConfig,
                             config_size: EGLint, num_config: *mut EGLint) -> EGLBoolean;

        pub fn eglGetCurrentContext() -> EGLContext;

        pub fn eglGetCurrentDisplay() -> EGLDisplay;

        pub fn eglGetCurrentSurface(readdraw: EGLint) -> EGLSurface;

        pub fn eglGetDisplay(display_id: EGLNativeDisplayType) -> EGLDisplay;

        pub fn eglGetError() -> EGLint;

        pub fn eglGetProcAddress(procname: *const c_char) -> extern "C" fn();

        pub fn eglInitialize(dpy: EGLDisplay, major: *mut EGLint, minor: *mut EGLint) -> EGLBoolean;

        pub fn eglMakeCurrent(dpy: EGLDisplay, draw: EGLSurface,
                              read: EGLSurface, ctx: EGLContext) -> EGLBoolean;

        pub fn eglQueryAPI() -> EGLenum;

        pub fn eglQueryContext(dpy: EGLDisplay, ctx: EGLContext,
                               attribute: EGLint, value: *mut EGLint) -> EGLBoolean;

        pub fn eglQueryString(dpy: EGLDisplay, name: EGLint) -> *const c_char;

        pub fn eglQuerySurface(dpy: EGLDisplay, surface: EGLSurface,
                               attribute: EGLint, value: *mut EGLint) -> EGLBoolean;

        pub fn eglReleaseTexImage(dpy: EGLDisplay, surface: EGLSurface,
                                  buffer: EGLint) -> EGLBoolean;

        pub fn eglReleaseThread() -> EGLBoolean;

        pub fn eglSurfaceAttrib(dpy: EGLDisplay, surface: EGLSurface,
                                attribute: EGLint, value: EGLint) -> EGLBoolean;

        pub fn eglSwapBuffers(dpy: EGLDisplay, surface: EGLSurface) -> EGLBoolean;

        pub fn eglSwapInterface(dpy: EGLDisplay, interval: EGLint) -> EGLBoolean;

        pub fn eglTerminate(dpy: EGLDisplay) -> EGLBoolean;

        pub fn eglWaitClient() -> EGLBoolean;

        pub fn eglWaitGL() -> EGLBoolean;

        pub fn eglWaitNative(engine: EGLint) -> EGLBoolean;
    }
}