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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
// +-----------------------------------------------------------------------------------------------+
// | 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@metatomic.io>                                                         |
// +-----------------------------------------------------------------------------------------------+

#![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_uint,
            c_void,
            int32_t };

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

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

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

pub type EGLBoolean           = c_uint;
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              = c_uint;
pub type EGLint               = khronos_int32_t;
pub type EGLNativeDisplayType = *mut c_void;
pub type EGLSurface           = *mut c_void;

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

#[repr(C)]
#[cfg(android)]
struct android_native_window_t;

#[repr(C)]
#[cfg(android)]
struct 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 EGL_FALSE: EGLBoolean = 0;
pub const EGL_TRUE:  EGLBoolean = 1;

// out-of-band handle values
pub const EGL_DEFAULT_DISPLAY: EGLNativeDisplayType = 0 as *mut c_void;
pub const EGL_NO_CONTEXT:      EGLContext = 0 as *mut c_void;
pub const EGL_NO_DISPLAY:      EGLDisplay = 0 as *mut c_void;
pub const EGL_NO_SURFACE:      EGLSurface = 0 as *mut c_void;

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

// errors / GetError return values
pub const EGL_SUCCESS:             EGLint = 0x3000;
pub const EGL_NOT_INITIALIZED:     EGLint = 0x3001;
pub const EGL_BAD_ACCESS:          EGLint = 0x3002;
pub const EGL_BAD_ALLOC:           EGLint = 0x3003;
pub const EGL_BAD_ATTRIBUTE:       EGLint = 0x3004;
pub const EGL_BAD_CONFIG:          EGLint = 0x3005;
pub const EGL_BAD_CONTEXT:         EGLint = 0x3006;
pub const EGL_BAD_CURRENT_SURFACE: EGLint = 0x3007;
pub const EGL_BAD_DISPLAY:         EGLint = 0x3008;
pub const EGL_BAD_MATCH:           EGLint = 0x3009;
pub const EGL_BAD_NATIVE_PIXMAP:   EGLint = 0x300A;
pub const EGL_BAD_NATIVE_WINDOW:   EGLint = 0x300B;
pub const EGL_BAD_PARAMETER:       EGLint = 0x300C;
pub const EGL_BAD_SURFACE:         EGLint = 0x300D;
pub const EGL_CONTEXT_LOST:        EGLint = 0x300E;  // EGL 1.1 - IMG_power_management

// config attributes
pub const EGL_BUFFER_SIZE:             EGLint = 0x3020;
pub const EGL_ALPHA_SIZE:              EGLint = 0x3021;
pub const EGL_BLUE_SIZE:               EGLint = 0x3022;
pub const EGL_GREEN_SIZE:              EGLint = 0x3023;
pub const EGL_RED_SIZE:                EGLint = 0x3024;
pub const EGL_DEPTH_SIZE:              EGLint = 0x3025;
pub const EGL_STENCIL_SIZE:            EGLint = 0x3026;
pub const EGL_CONFIG_CAVEAT:           EGLint = 0x3027;
pub const EGL_CONFIG_ID:               EGLint = 0x3028;
pub const EGL_LEVEL:                   EGLint = 0x3029;
pub const EGL_MAX_PBUFFER_HEIGHT:      EGLint = 0x302A;
pub const EGL_MAX_PBUFFER_PIXELS:      EGLint = 0x302B;
pub const EGL_MAX_PBUFFER_WIDTH:       EGLint = 0x302C;
pub const EGL_NATIVE_RENDERABLE:       EGLint = 0x302D;
pub const EGL_NATIVE_VISUAL_ID:        EGLint = 0x302E;
pub const EGL_NATIVE_VISUAL_TYPE:      EGLint = 0x302F;
pub const EGL_SAMPLES:                 EGLint = 0x3031;
pub const EGL_SAMPLE_BUFFERS:          EGLint = 0x3032;
pub const EGL_SURFACE_TYPE:            EGLint = 0x3033;
pub const EGL_TRANSPARENT_TYPE:        EGLint = 0x3034;
pub const EGL_TRANSPARENT_BLUE_VALUE:  EGLint = 0x3035;
pub const EGL_TRANSPARENT_GREEN_VALUE: EGLint = 0x3036;
pub const EGL_TRANSPARENT_RED_VALUE:   EGLint = 0x3037;
pub const EGL_NONE:                    EGLint = 0x3038; // attrib list terminator
pub const EGL_BIND_TO_TEXTURE_RGB:     EGLint = 0x3039;
pub const EGL_BIND_TO_TEXTURE_RGBA:    EGLint = 0x303A;
pub const EGL_MIN_SWAP_INTERVAL:       EGLint = 0x303B;
pub const EGL_MAX_SWAP_INTERVAL:       EGLint = 0x303C;
pub const EGL_LUMINANCE_SIZE:          EGLint = 0x303D;
pub const EGL_ALPHA_MASK_SIZE:         EGLint = 0x303E;
pub const EGL_COLOR_BUFFER_TYPE:       EGLint = 0x303F;
pub const EGL_RENDERABLE_TYPE:         EGLint = 0x3040;
pub const EGL_MATCH_NATIVE_PIXMAP:     EGLint = 0x3041;  // psseudo-attribute (not queryable)
pub const EGL_CONFORMANT:              EGLint = 0x3042;

// config attribute values
pub const EGL_SLOW_CONFIG:           EGLint = 0x3050;  // CONFIG_CAVEAT value
pub const EGL_NON_CONFORMANT_CONFIG: EGLint = 0x3051;  // CONFIG_CAVEAT value
pub const EGL_TRANSPARENT_RGB:       EGLint = 0x3052;  // TRANSPARENT_TYPE value
pub const EGL_RGB_BUFFER:            EGLint = 0x308E;  // COLOR_BUFFER_TYPE value
pub const EGL_LUMINANCE_BUFFER:      EGLint = 0x308F;  // COLOR_BUFFER_TYPE value

// more config attribute values, for TEXTURE_FORMAT
pub const EGL_NO_TEXTURE:   EGLint = 0x305C;
pub const EGL_TEXTURE_RGB:  EGLint = 0x305D;
pub const EGL_TEXTURE_RGBA: EGLint = 0x305E;
pub const EGL_TEXTURE_2D:   EGLint = 0x305F;

// config attribute mask bits
pub const EGL_PBUFFER_BIT:                 EGLint = 0x0001;  // SURFACE_TYPE mask bits
pub const EGL_PIXMAP_BIT:                  EGLint = 0x0002;  // SURFACE_TYPE mask bits
pub const EGL_WINDOW_BIT:                  EGLint = 0x0004;  // SURFACE_TYPE mask bits
pub const EGL_VG_COLORSPACE_LINEAR_BIT:    EGLint = 0x0020;  // SURFACE_TYPE mask bits
pub const EGL_VG_ALPHA_FORMAT_PRE_BIT:     EGLint = 0x0040;  // SURFACE_TYPE mask bits
pub const EGL_MULTISAMPLE_RESOLVE_BOX_BIT: EGLint = 0x0200;  // SURFACE_TYPE mask bits
pub const EGL_SWAP_BEHAVIOR_PRESERVED_BIT: EGLint = 0x0400;  // SURFACE_TYPE mask bits

pub const EGL_OPENGL_ES_BIT:  EGLint = 0x0001; // RENDERABLE_TYPE mask bits
pub const EGL_OPENVG_BIT:     EGLint = 0x0002; // RENDERABLE_TYPE mask bits
pub const EGL_OPENGL_ES2_BIT: EGLint = 0x0004; // RENDERABLE_TYPE mask bits
pub const EGL_OPENGL_BIT:     EGLint = 0x0008; // RENDERABLE_TYPE mask bits

// QueryString targets
pub const EGL_VENDOR:      EGLint = 0x3053;
pub const EGL_VERSION:     EGLint = 0x3054;
pub const EGL_EXTENSIONS:  EGLint = 0x3055;
pub const EGL_CLIENT_APIS: EGLint = 0x308D;

// QuerySurface / SurfaceAttrib / CreatePbufferSurface targets
pub const EGL_HEIGHT:                EGLint = 0x3056;
pub const EGL_WIDTH:                 EGLint = 0x3057;
pub const EGL_LARGEST_PBUFFER:       EGLint = 0x3058;
pub const EGL_TEXTURE_FORMAT:        EGLint = 0x3080;
pub const EGL_TEXTURE_TARGET:        EGLint = 0x3081;
pub const EGL_MIPMAP_TEXTURE:        EGLint = 0x3082;
pub const EGL_MIPMAP_LEVEL:          EGLint = 0x3083;
pub const EGL_RENDER_BUFFER:         EGLint = 0x3086;
pub const EGL_VG_COLORSPACE:         EGLint = 0x3087;
pub const EGL_VG_ALPHA_FORMAT:       EGLint = 0x3088;
pub const EGL_HORIZONTAL_RESOLUTION: EGLint = 0x3090;
pub const EGL_VERTICAL_RESOLUTION:   EGLint = 0x3091;
pub const EGL_PIXEL_ASPECT_RATIO:    EGLint = 0x3092;
pub const EGL_SWAP_BEHAVIOR:         EGLint = 0x3093;
pub const EGL_MULTISAMPLE_RESOLVE:   EGLint = 0x3099;

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

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

// OpenVG alpha formats
pub const EGL_VG_ALPHA_FORMAT_NONPRE: EGLint = 0x308B; // ALPHA_FORMAT value
pub const EGL_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 EGL_DISPLAY_SCALING: EGLint = 10000;

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

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

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

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

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

// multisample resolution behaviors
pub const EGL_MULTISAMPLE_RESOLVE_DEFAULT: EGLint = 0x309A; // MULTISAMPLE_RESOLVE value
pub const EGL_MULTISAMPLE_RESOLVE_BOX:     EGLint = 0x309B; // MULTISAMPLE_RESOLVE value

// BindAPI/QueryAPI targets
pub const EGL_OPENGL_ES_API: EGLenum = 0x30A0;
pub const EGL_OPENVG_API:    EGLenum = 0x30A1;
pub const EGL_OPENGL_API:    EGLenum = 0x30A2;

// GetCurrentSurface targets
pub const EGL_DRAW: EGLint = 0x3059;
pub const EGL_READ: EGLint = 0x305A;

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

// EGL 1.2 tokens renamed for consistency in EGL 1.3
pub const EGL_COLORSPACE:          EGLint = EGL_VG_COLORSPACE;
pub const EGL_ALPHA_FORMAT:        EGLint = EGL_VG_ALPHA_FORMAT;
pub const EGL_COLORSPACE_sRGB:     EGLint = EGL_VG_COLORSPACE_sRGB;
pub const EGL_COLORSPACE_LINEAR:   EGLint = EGL_VG_COLORSPACE_LINEAR;
pub const EGL_ALPHA_FORMAT_NONPRE: EGLint = EGL_VG_ALPHA_FORMAT_NONPRE;
pub const EGL_ALPHA_FORMAT_PRE:    EGLint = EGL_VG_ALPHA_FORMAT_PRE;

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

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

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

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

        let attribs = if attrib_list.len() > 0 {
            attrib_list.as_ptr()
        } else {
            ptr::null()
        };

        if ffi::eglChooseConfig(display, attribs, &mut config, config_size,
                                &mut count) == EGL_TRUE {
            if count > 0 {
                Some(config)
            } else {
                None
            }
        } else {
            None
        }
    }
}

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

pub fn create_context(display: EGLDisplay, config: EGLConfig, share_context: EGLContext,
                      attrib_list: &[EGLint]) -> Option<EGLContext> {
    unsafe {
        let attribs = if attrib_list.len() > 0 {
            attrib_list.as_ptr()
        } else {
            ptr::null()
        };

        let context = ffi::eglCreateContext(display, config, share_context, attribs);

        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 attribs = if attrib_list.len() > 0 {
            attrib_list.as_ptr()
        } else {
            ptr::null()
        };

        let surface = ffi::eglCreatePbufferFromClientBuffer(display, buffer_type, buffer, config,
                                                            attribs);

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

pub fn create_pbuffer_surface(display: EGLDisplay, config: EGLConfig,
                              attrib_list: &[EGLint]) -> Option<EGLSurface> {
    unsafe {
        let attribs = if attrib_list.len() > 0 {
            attrib_list.as_ptr()
        } else {
            ptr::null()
        };

        let surface = ffi::eglCreatePbufferSurface(display, config, attribs);

        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 attribs = if attrib_list.len() > 0 {
            attrib_list.as_ptr()
        } else {
            ptr::null()
        };

        let surface = ffi::eglCreatePixmapSurface(display, config, pixmap, attribs);

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

pub fn create_window_surface(display: EGLDisplay, config: EGLConfig,
                             window: EGLNativeWindowType,
                             attrib_list: &[EGLint]) -> Option<EGLSurface> {
    unsafe {
        let attribs = if attrib_list.len() > 0 {
            attrib_list.as_ptr()
        } else {
            ptr::null()
        };

        let surface = ffi::eglCreateWindowSurface(display, config, window, attribs);

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

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

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

pub fn get_config_attrib(display: EGLDisplay, config: EGLConfig,
                         attribute: EGLint, value: &mut EGLint) -> bool {
    unsafe {
        ffi::eglGetConfigAttrib(display, config, attribute, value) == EGL_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) == EGL_TRUE
    }
}

pub fn make_current(display: EGLDisplay, draw: EGLSurface,
                    read: EGLSurface, ctx: EGLContext) -> bool {
    unsafe {
        ffi::eglMakeCurrent(display, draw, read, ctx) == EGL_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) == EGL_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) == EGL_TRUE
    }
}

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

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

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

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

pub fn swap_interval(display: EGLDisplay, interval: EGLint) -> bool {
    unsafe {
        ffi::eglSwapInterval(display, interval) == EGL_TRUE
    }
}

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

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

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

pub fn wait_native(engine: EGLint) -> bool {
    unsafe {
        ffi::eglWaitNative(engine) == EGL_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: *mut 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 eglSwapInterval(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;
    }
}