summaryrefslogtreecommitdiffstats
path: root/third_party/rust/khronos-egl/src/lib.rs
blob: 239ff19194c60c17a96ae090f3a1fe75b6ca24fa (plain)
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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
//! This crate provides a binding for the Khronos EGL 1.5 API.
//! It was originally a fork of the [egl](https://crates.io/crates/egl) crate,
//! which is left unmaintained.
//!
//! ## Usage
//!
//! You can access the EGL API using an [`Instance`]
//! object defined by either statically linking with `libEGL.so.1` at compile time,
//! or dynamically loading the EGL library at runtime.
//!
//! ### Static linking
//!
//! You must enable static linking using the `static` feature in your `Cargo.toml`:
//! ```toml
//! khronos-egl = { version = ..., features = ["static"] }
//! ```
//!
//! This will add a dependency to the [`pkg-config`](https://crates.io/crates/pkg-config) crate,
//! necessary to find the EGL library at compile time.
//! Here is a simple example showing how to use this library to create an EGL context when static linking is enabled.
//!
//! ```rust
//! extern crate khronos_egl as egl;
//!
//! fn main() -> Result<(), egl::Error> {
//! 	// Create an EGL API instance.
//! 	// The `egl::Static` API implementation is only available when the `static` feature is enabled.
//! 	let egl = egl::Instance::new(egl::Static);
//!
//! 	let wayland_display = wayland_client::Display::connect_to_env().expect("unable to connect to the wayland server");
//! 	let display = egl.get_display(wayland_display.get_display_ptr() as *mut std::ffi::c_void).unwrap();
//! 	egl.initialize(display)?;
//!
//! 	let attributes = [
//! 		egl::RED_SIZE, 8,
//! 		egl::GREEN_SIZE, 8,
//! 		egl::BLUE_SIZE, 8,
//! 		egl::NONE
//! 	];
//!
//! 	let config = egl.choose_first_config(display, &attributes)?.expect("unable to find an appropriate ELG configuration");
//!
//! 	let context_attributes = [
//! 		egl::CONTEXT_MAJOR_VERSION, 4,
//! 		egl::CONTEXT_MINOR_VERSION, 0,
//! 		egl::CONTEXT_OPENGL_PROFILE_MASK, egl::CONTEXT_OPENGL_CORE_PROFILE_BIT,
//! 		egl::NONE
//! 	];
//!
//! 	egl.create_context(display, config, None, &context_attributes);
//!
//! 	Ok(())
//! }
//! ```
//!
//! The creation of a `Display` instance is not detailed here since it depends on your display server.
//! It is created using the `get_display` function with a pointer to the display server connection handle.
//! For instance, if you are using the [wayland-client](https://crates.io/crates/wayland-client) crate,
//! you can get this pointer using the `Display::get_display_ptr` method.
//!
//! #### Static API Instance
//!
//! It may be bothering in some applications to pass the `Instance` to every fonction that needs to call the EGL API.
//! One workaround would be to define a static `Instance`,
//! which should be possible to define at compile time using static linking.
//! However this is not yet supported by the stable `rustc` compiler.
//! With the nightly compiler,
//! you can combine the `nightly` and `static` features so that this crate
//! can provide a static `Instance`, called `API` that can then be accessed everywhere.
//!
//! ```
//! # extern crate khronos_egl as egl;
//! use egl::API as egl;
//! ```
//!
//! ### Dynamic Linking
//!
//! Dynamic linking allows your application to accept multiple versions of EGL and be more flexible.
//! You must enable dynamic linking using the `dynamic` feature in your `Cargo.toml`:
//! ```toml
//! khronos-egl = { version = ..., features = ["dynamic"] }
//! ```
//!
//! This will add a dependency to the [`libloading`](https://crates.io/crates/libloading) crate,
//! necessary to find the EGL library at runtime.
//! You can then load the EGL API into a `Instance<Dynamic<libloading::Library>>` as follows:
//!
//! ```
//! # extern crate khronos_egl as egl;
//! let lib = libloading::Library::new("libEGL.so.1").expect("unable to find libEGL.so.1");
//! let egl = unsafe { egl::DynamicInstance::<egl::EGL1_4>::load_required_from(lib).expect("unable to load libEGL.so.1") };
//! ```
//!
//! Here, `egl::EGL1_4` is used to specify what is the minimum required version of EGL that must be provided by `libEGL.so.1`.
//! This will return a `DynamicInstance<egl::EGL1_4>`, however in that case where `libEGL.so.1` provides a more recent version of EGL,
//! you can still upcast ths instance to provide version specific features:
//! ```
//! # extern crate khronos_egl as egl;
//! # let lib = libloading::Library::new("libEGL.so.1").expect("unable to find libEGL.so.1");
//! # let egl = unsafe { egl::DynamicInstance::<egl::EGL1_4>::load_required_from(lib).expect("unable to load libEGL.so.1") };
//! match egl.upcast::<egl::EGL1_5>() {
//! 	Some(egl1_5) => {
//! 		// do something with EGL 1.5
//! 	}
//! 	None => {
//! 		// do something with EGL 1.4 instead.
//! 	}
//! };
//! ```
//!
//! ## Troubleshooting
//!
//! ### Static Linking with OpenGL ES
//!
//! When using OpenGL ES with `khronos-egl` with the `static` feature,
//! it is necessary to place a dummy extern at the top of your application which links libEGL first, then GLESv1/2.
//! This is because libEGL provides symbols required by GLESv1/2.
//! Here's how to work around this:
//!
//! ```
//! ##[link(name = "EGL")]
//! ##[link(name = "GLESv2")]
//! extern {}
//! ```
#![allow(non_upper_case_globals)]
#![allow(non_snake_case)]

extern crate libc;

use std::convert::{TryFrom, TryInto};
use std::ffi::CStr;
use std::ffi::CString;
use std::fmt;
use std::ptr;

use libc::{c_uint, c_char, c_void};

/// EGL API provider.
pub trait Api {
	/// Version of the provided EGL API.
	fn version(&self) -> Version;
}

pub trait Downcast<V> {
	fn downcast(&self) -> &V;
}

impl<T> Downcast<T> for T {
	fn downcast(&self) -> &T {
		self
	}
}

pub trait Upcast<V> {
	fn upcast(&self) -> Option<&V>;
}

impl<T> Upcast<T> for T {
	fn upcast(&self) -> Option<&T> {
		Some(self)
	}
}

/// EGL API instance.
///
/// An instance wraps an interface to the EGL API and provide
/// rust-friendly access to it.
pub struct Instance<T> {
	api: T
}

impl<T> Instance<T> {
	/// Cast the API.
	#[inline(always)]
	pub fn cast_into<U: From<T>>(self) -> Instance<U> {
		Instance {
			api: self.api.into()
		}
	}

	/// Try to cast the API.
	#[inline(always)]
	pub fn try_cast_into<U: TryFrom<T>>(self) -> Result<Instance<U>, Instance<U::Error>> {
		match self.api.try_into() {
			Ok(t) => Ok(Instance {
				api: t
			}),
			Err(e) => Err(Instance {
				api: e
			})
		}
	}

	/// Returns the version of the provided EGL API.
	#[inline(always)]
	pub fn version(&self) -> Version where T: Api {
		self.api.version()
	}
}

impl<T> Instance<T> {
	#[inline(always)]
	pub const fn new(api: T) -> Instance<T> {
		Instance {
			api
		}
	}
}

impl<T: fmt::Debug> fmt::Debug for Instance<T> {
	#[inline(always)]
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		write!(f, "Instance({:?})", self.api)
	}
}

impl<T> From<T> for Instance<T> {
	#[inline(always)]
	fn from(t: T) -> Instance<T> {
		Instance::new(t)
	}
}

// ------------------------------------------------------------------------------------------------
// EGL 1.0
// ------------------------------------------------------------------------------------------------

#[cfg(feature = "1_0")]
mod egl1_0 {
	use super::*;

	pub type Boolean = c_uint;
	pub type Int = i32;
	pub type Attrib = usize;
	pub type EGLDisplay = *mut c_void;
	pub type EGLConfig = *mut c_void;
	pub type EGLContext = *mut c_void;
	pub type EGLSurface = *mut c_void;
	pub type NativeDisplayType = *mut c_void;

	#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
	pub struct Display(pub(crate) EGLDisplay);

	impl Display {
		#[inline]
		pub unsafe fn from_ptr(ptr: EGLDisplay) -> Display {
			Display(ptr)
		}

		#[inline]
		pub fn as_ptr(&self) -> EGLDisplay {
			self.0
		}
	}

	#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
	pub struct Config(pub(crate) EGLConfig);

	impl Config {
		#[inline]
		pub unsafe fn from_ptr(ptr: EGLConfig) -> Config {
			Config(ptr)
		}

		#[inline]
		pub fn as_ptr(&self) -> EGLConfig {
			self.0
		}
	}

	#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
	pub struct Context(pub(crate) EGLContext);

	impl Context {
		#[inline]
		pub unsafe fn from_ptr(ptr: EGLContext) -> Context {
			Context(ptr)
		}

		#[inline]
		pub fn as_ptr(&self) -> EGLContext {
			self.0
		}
	}

	#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
	pub struct Surface(pub(crate) EGLSurface);

	impl Surface {
		#[inline]
		pub unsafe fn from_ptr(ptr: EGLSurface) -> Surface {
			Surface(ptr)
		}

		#[inline]
		pub fn as_ptr(&self) -> EGLSurface {
			self.0
		}
	}

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

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

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

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

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

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

	pub const ALPHA_SIZE: Int = 0x3021;
	pub const BAD_ACCESS: Int = 0x3002;
	pub const BAD_ALLOC: Int = 0x3003;
	pub const BAD_ATTRIBUTE: Int = 0x3004;
	pub const BAD_CONFIG: Int = 0x3005;
	pub const BAD_CONTEXT: Int = 0x3006;
	pub const BAD_CURRENT_SURFACE: Int = 0x3007;
	pub const BAD_DISPLAY: Int = 0x3008;
	pub const BAD_MATCH: Int = 0x3009;
	pub const BAD_NATIVE_PIXMAP: Int = 0x300A;
	pub const BAD_NATIVE_WINDOW: Int = 0x300B;
	pub const BAD_PARAMETER: Int = 0x300C;
	pub const BAD_SURFACE: Int = 0x300D;
	pub const BLUE_SIZE: Int = 0x3022;
	pub const BUFFER_SIZE: Int = 0x3020;
	pub const CONFIG_CAVEAT: Int = 0x3027;
	pub const CONFIG_ID: Int = 0x3028;
	pub const CORE_NATIVE_ENGINE: Int = 0x305B;
	pub const DEPTH_SIZE: Int = 0x3025;
	pub const DONT_CARE: Int = -1;
	pub const DRAW: Int = 0x3059;
	pub const EXTENSIONS: Int = 0x3055;
	pub const FALSE: Boolean = 0;
	pub const GREEN_SIZE: Int = 0x3023;
	pub const HEIGHT: Int = 0x3056;
	pub const LARGEST_PBUFFER: Int = 0x3058;
	pub const LEVEL: Int = 0x3029;
	pub const MAX_PBUFFER_HEIGHT: Int = 0x302A;
	pub const MAX_PBUFFER_PIXELS: Int = 0x302B;
	pub const MAX_PBUFFER_WIDTH: Int = 0x302C;
	pub const NATIVE_RENDERABLE: Int = 0x302D;
	pub const NATIVE_VISUAL_ID: Int = 0x302E;
	pub const NATIVE_VISUAL_TYPE: Int = 0x302F;
	pub const NONE: Int = 0x3038;
	pub const ATTRIB_NONE: Attrib = 0x3038;
	pub const NON_CONFORMANT_CONFIG: Int = 0x3051;
	pub const NOT_INITIALIZED: Int = 0x3001;
	pub const NO_CONTEXT: EGLContext = 0 as EGLContext;
	pub const NO_DISPLAY: EGLDisplay = 0 as EGLDisplay;
	pub const NO_SURFACE: EGLSurface = 0 as EGLSurface;
	pub const PBUFFER_BIT: Int = 0x0001;
	pub const PIXMAP_BIT: Int = 0x0002;
	pub const READ: Int = 0x305A;
	pub const RED_SIZE: Int = 0x3024;
	pub const SAMPLES: Int = 0x3031;
	pub const SAMPLE_BUFFERS: Int = 0x3032;
	pub const SLOW_CONFIG: Int = 0x3050;
	pub const STENCIL_SIZE: Int = 0x3026;
	pub const SUCCESS: Int = 0x3000;
	pub const SURFACE_TYPE: Int = 0x3033;
	pub const TRANSPARENT_BLUE_VALUE: Int = 0x3035;
	pub const TRANSPARENT_GREEN_VALUE: Int = 0x3036;
	pub const TRANSPARENT_RED_VALUE: Int = 0x3037;
	pub const TRANSPARENT_RGB: Int = 0x3052;
	pub const TRANSPARENT_TYPE: Int = 0x3034;
	pub const TRUE: Boolean = 1;
	pub const VENDOR: Int = 0x3053;
	pub const VERSION: Int = 0x3054;
	pub const WIDTH: Int = 0x3057;
	pub const WINDOW_BIT: Int = 0x0004;

	/// EGL errors.
	#[derive(Clone, Copy, PartialEq, Eq, Debug)]
	pub enum Error {
		/// EGL is not initialized, or could not be initialized, for the specified
		/// EGL display connection.
		NotInitialized,

		/// EGL cannot access a requested resource (for example a context is bound
		/// in another thread).
		BadAccess,

		/// EGL failed to allocate resources for the requested operation.
		BadAlloc,

		/// An unrecognized attribute or attribute value was passed in the attribute
		/// list.
		BadAttribute,

		/// An Context argument does not name a valid EGL rendering context.
		BadContext,

		/// An Config argument does not name a valid EGL frame buffer configuration.
		BadConfig,

		/// The current surface of the calling thread is a window, pixel buffer or
		/// pixmap that is no longer valid.
		BadCurrentSurface,

		/// An Display argument does not name a valid EGL display connection.
		BadDisplay,

		/// An Surface argument does not name a valid surface (window, pixel buffer
		/// or pixmap) configured for GL rendering.
		BadSurface,

		/// Arguments are inconsistent (for example, a valid context requires
		/// buffers not supplied by a valid surface).
		BadMatch,

		/// One or more argument values are invalid.
		BadParameter,

		/// A NativePixmapType argument does not refer to a valid native pixmap.
		BadNativePixmap,

		/// A NativeWindowType argument does not refer to a valid native window.
		BadNativeWindow,

		/// A power management event has occurred. The application must destroy all
		/// contexts and reinitialise OpenGL ES state and objects to continue
		/// rendering.
		ContextLost,
	}

	impl std::error::Error for Error {
		fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
			None
		}
	}

	impl Error {
		pub fn native(&self) -> Int {
			use Error::*;
			match self {
				NotInitialized => NOT_INITIALIZED,
				BadAccess => BAD_ACCESS,
				BadAlloc => BAD_ALLOC,
				BadAttribute => BAD_ATTRIBUTE,
				BadContext => BAD_CONTEXT,
				BadConfig => BAD_CONFIG,
				BadCurrentSurface => BAD_CURRENT_SURFACE,
				BadDisplay => BAD_DISPLAY,
				BadSurface => BAD_SURFACE,
				BadMatch => BAD_MATCH,
				BadParameter => BAD_PARAMETER,
				BadNativePixmap => BAD_NATIVE_PIXMAP,
				BadNativeWindow => BAD_NATIVE_WINDOW,
				ContextLost => CONTEXT_LOST,
			}
		}

		fn message(&self) -> &'static str {
			use Error::*;
			match self {
				NotInitialized => "EGL is not initialized, or could not be initialized, for the specified EGL display connection.",
				BadAccess => "EGL cannot access a requested resource (for example a context is bound in another thread.",
				BadAlloc => "EGL failed to allocate resources for the requested operation.",
				BadAttribute => "An unrecognized attribute or attribute value was passed in the attribute list.",
				BadContext => "An Context argument does not name a valid EGL rendering context.",
				BadConfig => "An Config argument does not name a valid EGL frame buffer configuration.",
				BadCurrentSurface => "The current surface of the calling thread is a window, pixel buffer or pixmap that is no longer valid.",
				BadDisplay => "An Display argument does not name a valid EGL display connection.",
				BadSurface => "An Surface argument does not name a valid surface (window, pixel buffer or pixmap) configured for GL rendering.",
				BadMatch => "Arguments are inconsistent (for example, a valid context requires buffers not supplied by a valid surface.",
				BadParameter => "One or more argument values are invalid.",
				BadNativePixmap => "A NativePixmapType argument does not refer to a valid native pixmap.",
				BadNativeWindow => "A NativeWindowType argument does not refer to a valid native window.",
				ContextLost => "A power management event has occurred. The application must destroy all contexts and reinitialise OpenGL ES state and objects to continue rendering."
			}
		}
	}

	impl From<Error> for Int {
		fn from(e: Error) -> Int {
			e.native()
		}
	}

	impl TryFrom<Int> for Error {
		type Error = Int;

		fn try_from(e: Int) -> Result<Error, Int> {
			use Error::*;
			match e {
				NOT_INITIALIZED => Ok(NotInitialized),
				BAD_ACCESS => Ok(BadAccess),
				BAD_ALLOC => Ok(BadAlloc),
				BAD_ATTRIBUTE => Ok(BadAttribute),
				BAD_CONTEXT => Ok(BadContext),
				BAD_CONFIG => Ok(BadConfig),
				BAD_CURRENT_SURFACE => Ok(BadCurrentSurface),
				BAD_DISPLAY => Ok(BadDisplay),
				BAD_SURFACE => Ok(BadSurface),
				BAD_MATCH => Ok(BadMatch),
				BAD_PARAMETER => Ok(BadParameter),
				BAD_NATIVE_PIXMAP => Ok(BadNativePixmap),
				BAD_NATIVE_WINDOW => Ok(BadNativeWindow),
				CONTEXT_LOST => Ok(ContextLost),
				_ => Err(e),
			}
		}
	}

	impl fmt::Display for Error {
		fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
			self.message().fmt(f)
		}
	}

	pub fn check_int_list(attrib_list: &[Int]) -> Result<(), Error> {
		if attrib_list.last() == Some(&NONE) {
			Ok(())
		} else {
			Err(Error::BadParameter)
		}
	}

	pub fn check_attrib_list(attrib_list: &[Attrib]) -> Result<(), Error> {
		if attrib_list.last() == Some(&ATTRIB_NONE) {
			Ok(())
		} else {
			Err(Error::BadParameter)
		}
	}

	impl<T: api::EGL1_0> Instance<T> {
		/// Return the number of EGL frame buffer configurations that atch specified
		/// attributes.
		///
		/// This will call `eglChooseConfig` without `null` as `configs` to get the
		/// number of matching configurations.
		///
		/// This will return a `BadParameter` error if `attrib_list` is not a valid
		/// attributes list (if it does not terminate with `NONE`).
		pub fn matching_config_count(&self, display: Display, attrib_list: &[Int]) -> Result<usize, Error> {
			check_int_list(attrib_list)?;
			unsafe {
				let mut count = 0;

				if self.api.eglChooseConfig(
					display.as_ptr(),
					attrib_list.as_ptr(),
					ptr::null_mut(),
					0,
					&mut count,
				) == TRUE
				{
					Ok(count as usize)
				} else {
					Err(self.get_error().unwrap())
				}
			}
		}

		/// Return a list of EGL frame buffer configurations that match specified
		/// attributes.
		///
		/// This will write as many matching configurations in `configs` up to its
		/// capacity. You can use the function [`matching_config_count`](Self::matching_config_count) to get the
		/// exact number of configurations matching the specified attributes.
		///
		/// ## Example
		///
		/// ```
		/// # extern crate khronos_egl as egl;
		/// # extern crate wayland_client;
		/// # fn main() -> Result<(), egl::Error> {
		/// # let egl = egl::Instance::new(egl::Static);
		/// # let wayland_display = wayland_client::Display::connect_to_env().expect("unable to connect to the wayland server");
		/// # let display = egl.get_display(wayland_display.get_display_ptr() as *mut std::ffi::c_void).unwrap();
		/// # egl.initialize(display)?;
		/// # let attrib_list = [egl::RED_SIZE, 8, egl::GREEN_SIZE, 8, egl::BLUE_SIZE, 8, egl::NONE];
		/// // Get the number of matching configurations.
		/// let count = egl.matching_config_count(display, &attrib_list)?;
		///
		/// // Get the matching configurations.
		/// let mut configs = Vec::with_capacity(count);
		/// egl.choose_config(display, &attrib_list, &mut configs)?;
		/// # Ok(())
		/// # }
		/// ```
		///
		/// This will return a `BadParameter` error if `attrib_list` is not a valid
		/// attributes list (if it does not terminate with `NONE`).
		pub fn choose_config(&self, display: Display, attrib_list: &[Int], configs: &mut Vec<Config>) -> Result<(), Error> {
			check_int_list(attrib_list)?;
			unsafe {
				let capacity = configs.capacity();
				let mut count = 0;

				if self.api.eglChooseConfig(
					display.as_ptr(),
					attrib_list.as_ptr(),
					configs.as_mut_ptr() as *mut EGLConfig,
					capacity.try_into().unwrap(),
					&mut count,
				) == TRUE
				{
					configs.set_len(count as usize);
					Ok(())
				} else {
					Err(self.get_error().unwrap())
				}
			}
		}

		/// Return the first EGL frame buffer configuration that match specified
		/// attributes.
		///
		/// This is an helper function that will call `choose_config` with a buffer of
		/// size 1, which is equivalent to:
		/// ```
		/// # extern crate khronos_egl as egl;
		/// # extern crate wayland_client;
		/// # fn main() -> Result<(), egl::Error> {
		/// # let egl = egl::Instance::new(egl::Static);
		/// # let wayland_display = wayland_client::Display::connect_to_env().expect("unable to connect to the wayland server");
		/// # let display = egl.get_display(wayland_display.get_display_ptr() as *mut std::ffi::c_void).unwrap();
		/// # egl.initialize(display)?;
		/// # let attrib_list = [egl::RED_SIZE, 8, egl::GREEN_SIZE, 8, egl::BLUE_SIZE, 8, egl::NONE];
		/// let mut configs = Vec::with_capacity(1);
		/// egl.choose_config(display, &attrib_list, &mut configs)?;
		/// configs.first();
		/// # Ok(())
		/// # }
		/// ```
		pub fn choose_first_config(&self, display: Display, attrib_list: &[Int]) -> Result<Option<Config>, Error> {
			let mut configs = Vec::with_capacity(1);
			self.choose_config(display, attrib_list, &mut configs)?;
			Ok(configs.first().map(|config| *config))
		}

		/// Copy EGL surface color buffer to a native pixmap.
		pub fn copy_buffers(&self, display: Display, surface: Surface, target: NativePixmapType) -> Result<(), Error> {
			unsafe {
				if self.api.eglCopyBuffers(display.as_ptr(), surface.as_ptr(), target) == TRUE {
					Ok(())
				} else {
					Err(self.get_error().unwrap())
				}
			}
		}

		/// Create a new EGL rendering context.
		///
		/// This will return a `BadParameter` error if `attrib_list` is not a valid
		/// attributes list (if it does not terminate with `NONE`).
		pub fn create_context(&self, display: Display, config: Config, share_context: Option<Context>, attrib_list: &[Int]) -> Result<Context, Error> {
			check_int_list(attrib_list)?;
			unsafe {
				let share_context = match share_context {
					Some(share_context) => share_context.as_ptr(),
					None => NO_CONTEXT,
				};

				let context = self.api.eglCreateContext(
					display.as_ptr(),
					config.as_ptr(),
					share_context,
					attrib_list.as_ptr(),
				);

				if context != NO_CONTEXT {
					Ok(Context(context))
				} else {
					Err(self.get_error().unwrap())
				}
			}
		}

		/// Create a new EGL pixel buffer surface.
		///
		/// This will return a `BadParameter` error if `attrib_list` is not a valid
		/// attributes list (if it does not terminate with `NONE`).
		pub fn create_pbuffer_surface(&self, display: Display, config: Config, attrib_list: &[Int]) -> Result<Surface, Error> {
			check_int_list(attrib_list)?;
			unsafe {
				let surface =
					self.api.eglCreatePbufferSurface(display.as_ptr(), config.as_ptr(), attrib_list.as_ptr());

				if surface != NO_SURFACE {
					Ok(Surface(surface))
				} else {
					Err(self.get_error().unwrap())
				}
			}
		}

		/// Create a new EGL offscreen surface.
		///
		/// This will return a `BadParameter` error if `attrib_list` is not a valid
		/// attributes list (if it does not terminate with `NONE`).
		///
		/// Since this function may raise undefined behavior if the display and native
		/// pixmap do not belong to the same platform, it is inherently unsafe.
		pub unsafe fn create_pixmap_surface(&self, display: Display, config: Config, pixmap: NativePixmapType, attrib_list: &[Int]) -> Result<Surface, Error> {
			check_int_list(attrib_list)?;
			let surface = self.api.eglCreatePixmapSurface(
				display.as_ptr(),
				config.as_ptr(),
				pixmap,
				attrib_list.as_ptr(),
			);

			if surface != NO_SURFACE {
				Ok(Surface(surface))
			} else {
				Err(self.get_error().unwrap())
			}
		}

		/// Create a new EGL window surface.
		///
		/// This will return a `BadParameter` error if `attrib_list` is not a valid
		/// attributes list (if it does not terminate with `NONE`).
		///
		/// Since this function may raise undefined behavior if the display and native
		/// window do not belong to the same platform, it is inherently unsafe.
		pub unsafe fn create_window_surface(&self, display: Display, config: Config, window: NativeWindowType, attrib_list: Option<&[Int]>) -> Result<Surface, Error> {
			let attrib_list = match attrib_list {
				Some(attrib_list) => {
					check_int_list(attrib_list)?;
					attrib_list.as_ptr()
				}
				None => ptr::null(),
			};

			let surface =
				self.api.eglCreateWindowSurface(display.as_ptr(), config.as_ptr(), window, attrib_list);

			if surface != NO_SURFACE {
				Ok(Surface(surface))
			} else {
				Err(self.get_error().unwrap())
			}
		}

		/// Destroy an EGL rendering context.
		pub fn destroy_context(&self, display: Display, ctx: Context) -> Result<(), Error> {
			unsafe {
				if self.api.eglDestroyContext(display.as_ptr(), ctx.as_ptr()) == TRUE {
					Ok(())
				} else {
					Err(self.get_error().unwrap())
				}
			}
		}

		/// Destroy an EGL surface.
		pub fn destroy_surface(&self, display: Display, surface: Surface) -> Result<(), Error> {
			unsafe {
				if self.api.eglDestroySurface(display.as_ptr(), surface.as_ptr()) == TRUE {
					Ok(())
				} else {
					Err(self.get_error().unwrap())
				}
			}
		}

		/// Return information about an EGL frame buffer configuration.
		pub fn get_config_attrib(&self, display: Display, config: Config, attribute: Int) -> Result<Int, Error> {
			unsafe {
				let mut value: Int = 0;
				if self.api.eglGetConfigAttrib(display.as_ptr(), config.as_ptr(), attribute, &mut value) == TRUE
				{
					Ok(value)
				} else {
					Err(self.get_error().unwrap())
				}
			}
		}

		/// Return the number of all frame buffer configurations.
		///
		/// You can use it to setup the correct capacity for the configurations buffer in [`get_configs`](Self::get_configs).
		///
		/// ## Example
		/// ```
		/// # extern crate khronos_egl as egl;
		/// # extern crate wayland_client;
		/// # fn main() -> Result<(), egl::Error> {
		/// # let egl = egl::Instance::new(egl::Static);
		/// # let wayland_display = wayland_client::Display::connect_to_env().expect("unable to connect to the wayland server");
		/// # let display = egl.get_display(wayland_display.get_display_ptr() as *mut std::ffi::c_void).unwrap();
		/// # egl.initialize(display)?;
		/// let mut configs = Vec::with_capacity(egl.get_config_count(display)?);
		/// egl.get_configs(display, &mut configs);
		/// assert!(configs.len() > 0);
		/// # Ok(())
		/// # }
		/// ```
		pub fn get_config_count(&self, display: Display) -> Result<usize, Error> {
			unsafe {
				let mut count = 0;

				if self.api.eglGetConfigs(display.as_ptr(), std::ptr::null_mut(), 0, &mut count) == TRUE {
					Ok(count as usize)
				} else {
					Err(self.get_error().unwrap())
				}
			}
		}

		/// Get the list of all EGL frame buffer configurations for a display.
		///
		/// The configurations are added to the `configs` buffer, up to the buffer's capacity.
		/// You can use [`get_config_count`](Self::get_config_count) to get the total number of available frame buffer configurations,
		/// and setup the buffer's capacity accordingly.
		///
		/// ## Example
		/// ```
		/// # extern crate khronos_egl as egl;
		/// # extern crate wayland_client;
		/// # fn main() -> Result<(), egl::Error> {
		/// # let egl = egl::Instance::new(egl::Static);
		/// # let wayland_display = wayland_client::Display::connect_to_env().expect("unable to connect to the wayland server");
		/// # let display = egl.get_display(wayland_display.get_display_ptr() as *mut std::ffi::c_void).unwrap();
		/// # egl.initialize(display)?;
		/// let mut configs = Vec::with_capacity(egl.get_config_count(display)?);
		/// egl.get_configs(display, &mut configs);
		/// # Ok(())
		/// # }
		/// ```
		pub fn get_configs(&self, display: Display, configs: &mut Vec<Config>) -> Result<(), Error> {
			unsafe {
				let capacity = configs.capacity();
				let mut count = 0;

				if self.api.eglGetConfigs(
					display.as_ptr(),
					configs.as_mut_ptr() as *mut EGLConfig,
					capacity.try_into().unwrap(),
					&mut count,
				) == TRUE
				{
					configs.set_len(count as usize);
					Ok(())
				} else {
					Err(self.get_error().unwrap())
				}
			}
		}

		/// Return the display for the current EGL rendering context.
		pub fn get_current_display(&self) -> Option<Display> {
			unsafe {
				let display = self.api.eglGetCurrentDisplay();

				if display != NO_DISPLAY {
					Some(Display(display))
				} else {
					None
				}
			}
		}

		/// Return the read or draw surface for the current EGL rendering context.
		pub fn get_current_surface(&self, readdraw: Int) -> Option<Surface> {
			unsafe {
				let surface = self.api.eglGetCurrentSurface(readdraw);

				if surface != NO_SURFACE {
					Some(Surface(surface))
				} else {
					None
				}
			}
		}

		/// Return an EGL display connection.
		pub fn get_display(&self, display_id: NativeDisplayType) -> Option<Display> {
			unsafe {
				let display = self.api.eglGetDisplay(display_id);

				if display != NO_DISPLAY {
					Some(Display(display))
				} else {
					None
				}
			}
		}

		/// Return error information.
		///
		/// Return the error of the last called EGL function in the current thread, or
		/// `None` if the error is set to `SUCCESS`.
		///
		/// Note that since a call to `eglGetError` sets the error to `SUCCESS`, and
		/// since this function is automatically called by any wrapper function
		/// returning a `Result` when necessary, this function may only return `None`
		/// from the point of view of a user.
		pub fn get_error(&self) -> Option<Error> {
			unsafe {
				let e = self.api.eglGetError();
				if e == SUCCESS {
					None
				} else {
					Some(e.try_into().unwrap())
				}
			}
		}

		/// Return a GL or an EGL extension function.
		pub fn get_proc_address(&self, procname: &str) -> Option<extern "C" fn()> {
			unsafe {
				let string = CString::new(procname).unwrap();

				let addr = self.api.eglGetProcAddress(string.as_ptr());
				if !(addr as *const ()).is_null() {
					Some(addr)
				} else {
					None
				}
			}
		}

		/// Initialize an EGL display connection.
		pub fn initialize(&self, display: Display) -> Result<(Int, Int), Error> {
			unsafe {
				let mut major = 0;
				let mut minor = 0;

				if self.api.eglInitialize(display.as_ptr(), &mut major, &mut minor) == TRUE {
					Ok((major, minor))
				} else {
					Err(self.get_error().unwrap())
				}
			}
		}

		/// Attach an EGL rendering context to EGL surfaces.
		pub fn make_current(&self, display: Display, draw: Option<Surface>, read: Option<Surface>, ctx: Option<Context>) -> Result<(), Error> {
			unsafe {
				let draw = match draw {
					Some(draw) => draw.as_ptr(),
					None => NO_SURFACE,
				};
				let read = match read {
					Some(read) => read.as_ptr(),
					None => NO_SURFACE,
				};
				let ctx = match ctx {
					Some(ctx) => ctx.as_ptr(),
					None => NO_CONTEXT,
				};

				if self.api.eglMakeCurrent(display.as_ptr(), draw, read, ctx) == TRUE {
					Ok(())
				} else {
					Err(self.get_error().unwrap())
				}
			}
		}

		/// Return EGL rendering context information.
		pub fn query_context(&self, display: Display, ctx: Context, attribute: Int) -> Result<Int, Error> {
			unsafe {
				let mut value = 0;
				if self.api.eglQueryContext(display.as_ptr(), ctx.as_ptr(), attribute, &mut value) == TRUE {
					Ok(value)
				} else {
					Err(self.get_error().unwrap())
				}
			}
		}

		/// Return a string describing properties of the EGL client or of an EGL display
		/// connection.
		pub fn query_string(&self, display: Option<Display>, name: Int) -> Result<&'static CStr, Error> {
			unsafe {
				let display_ptr = match display {
					Some(display) => display.as_ptr(),
					None => NO_DISPLAY
				};

				let c_str = self.api.eglQueryString(display_ptr, name);

				if !c_str.is_null() {
					Ok(CStr::from_ptr(c_str))
				} else {
					Err(self.get_error().unwrap())
				}
			}
		}

		/// Return EGL surface information.
		pub fn query_surface(&self, display: Display, surface: Surface, attribute: Int) -> Result<Int, Error> {
			unsafe {
				let mut value = 0;
				if self.api.eglQuerySurface(display.as_ptr(), surface.as_ptr(), attribute, &mut value) == TRUE {
					Ok(value)
				} else {
					Err(self.get_error().unwrap())
				}
			}
		}

		/// Post EGL surface color buffer to a native window.
		pub fn swap_buffers(&self, display: Display, surface: Surface) -> Result<(), Error> {
			unsafe {
				if self.api.eglSwapBuffers(display.as_ptr(), surface.as_ptr()) == TRUE {
					Ok(())
				} else {
					Err(self.get_error().unwrap())
				}
			}
		}

		/// Terminate an EGL display connection.
		pub fn terminate(&self, display: Display) -> Result<(), Error> {
			unsafe {
				if self.api.eglTerminate(display.as_ptr()) == TRUE {
					Ok(())
				} else {
					Err(self.get_error().unwrap())
				}
			}
		}

		/// Complete GL execution prior to subsequent native rendering calls.
		pub fn wait_gl(&self) -> Result<(), Error> {
			unsafe {
				if self.api.eglWaitGL() == TRUE {
					Ok(())
				} else {
					Err(self.get_error().unwrap())
				}
			}
		}

		/// Complete native execution prior to subsequent GL rendering calls.
		pub fn wait_native(&self, engine: Int) -> Result<(), Error> {
			unsafe {
				if self.api.eglWaitNative(engine) == TRUE {
					Ok(())
				} else {
					Err(self.get_error().unwrap())
				}
			}
		}
	}
}

#[cfg(feature = "1_0")]
pub use egl1_0::*;

// ------------------------------------------------------------------------------------------------
// EGL 1.1
// ------------------------------------------------------------------------------------------------

#[cfg(feature = "1_1")]
mod egl1_1 {
	use super::*;

	pub const BACK_BUFFER: Int = 0x3084;
	pub const BIND_TO_TEXTURE_RGB: Int = 0x3039;
	pub const BIND_TO_TEXTURE_RGBA: Int = 0x303A;
	pub const CONTEXT_LOST: Int = 0x300E;
	pub const MIN_SWAP_INTERVAL: Int = 0x303B;
	pub const MAX_SWAP_INTERVAL: Int = 0x303C;
	pub const MIPMAP_TEXTURE: Int = 0x3082;
	pub const MIPMAP_LEVEL: Int = 0x3083;
	pub const NO_TEXTURE: Int = 0x305C;
	pub const TEXTURE_2D: Int = 0x305F;
	pub const TEXTURE_FORMAT: Int = 0x3080;
	pub const TEXTURE_RGB: Int = 0x305D;
	pub const TEXTURE_RGBA: Int = 0x305E;
	pub const TEXTURE_TARGET: Int = 0x3081;

	impl<T: api::EGL1_1> Instance<T> {
		/// Defines a two-dimensional texture image.
		pub fn bind_tex_image(&self, display: Display, surface: Surface, buffer: Int) -> Result<(), Error> {
			unsafe {
				if self.api.eglBindTexImage(display.as_ptr(), surface.as_ptr(), buffer) == TRUE {
					Ok(())
				} else {
					Err(self.get_error().unwrap())
				}
			}
		}

		///  Releases a color buffer that is being used as a texture.
		pub fn release_tex_image(&self, display: Display, surface: Surface, buffer: Int) -> Result<(), Error> {
			unsafe {
				if self.api.eglReleaseTexImage(display.as_ptr(), surface.as_ptr(), buffer) == TRUE {
					Ok(())
				} else {
					Err(self.get_error().unwrap())
				}
			}
		}

		/// Set an EGL surface attribute.
		pub fn surface_attrib(&self, display: Display, surface: Surface, attribute: Int, value: Int) -> Result<(), Error> {
			unsafe {
				if self.api.eglSurfaceAttrib(display.as_ptr(), surface.as_ptr(), attribute, value) == TRUE {
					Ok(())
				} else {
					Err(self.get_error().unwrap())
				}
			}
		}

		/// Specifies the minimum number of video frame periods per buffer swap for the
		/// window associated with the current context.
		pub fn swap_interval(&self, display: Display, interval: Int) -> Result<(), Error> {
			unsafe {
				if self.api.eglSwapInterval(display.as_ptr(), interval) == TRUE {
					Ok(())
				} else {
					Err(self.get_error().unwrap())
				}
			}
		}
	}
}

#[cfg(feature = "1_1")]
pub use egl1_1::*;

// ------------------------------------------------------------------------------------------------
// EGL 1.2
// ------------------------------------------------------------------------------------------------

#[cfg(feature = "1_2")]
mod egl1_2 {
	use super::*;

	pub type Enum = c_uint;
	pub type EGLClientBuffer = *mut c_void;

	#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
	pub struct ClientBuffer(EGLClientBuffer);

	impl ClientBuffer {
		#[inline]
		pub unsafe fn from_ptr(ptr: EGLClientBuffer) -> ClientBuffer {
			ClientBuffer(ptr)
		}

		#[inline]
		pub fn as_ptr(&self) -> EGLClientBuffer {
			self.0
		}
	}

	pub const ALPHA_FORMAT: Int = 0x3088;
	pub const ALPHA_FORMAT_NONPRE: Int = 0x308B;
	pub const ALPHA_FORMAT_PRE: Int = 0x308C;
	pub const ALPHA_MASK_SIZE: Int = 0x303E;
	pub const BUFFER_PRESERVED: Int = 0x3094;
	pub const BUFFER_DESTROYED: Int = 0x3095;
	pub const CLIENT_APIS: Int = 0x308D;
	pub const COLORSPACE: Int = 0x3087;
	pub const COLORSPACE_sRGB: Int = 0x3089;
	pub const COLORSPACE_LINEAR: Int = 0x308A;
	pub const COLOR_BUFFER_TYPE: Int = 0x303F;
	pub const CONTEXT_CLIENT_TYPE: Int = 0x3097;
	pub const DISPLAY_SCALING: Int = 10000;
	pub const HORIZONTAL_RESOLUTION: Int = 0x3090;
	pub const LUMINANCE_BUFFER: Int = 0x308F;
	pub const LUMINANCE_SIZE: Int = 0x303D;
	pub const OPENGL_ES_BIT: Int = 0x0001;
	pub const OPENVG_BIT: Int = 0x0002;
	pub const OPENGL_ES_API: Enum = 0x30A0;
	pub const OPENVG_API: Enum = 0x30A1;
	pub const OPENVG_IMAGE: Int = 0x3096;
	pub const PIXEL_ASPECT_RATIO: Int = 0x3092;
	pub const RENDERABLE_TYPE: Int = 0x3040;
	pub const RENDER_BUFFER: Int = 0x3086;
	pub const RGB_BUFFER: Int = 0x308E;
	pub const SINGLE_BUFFER: Int = 0x3085;
	pub const SWAP_BEHAVIOR: Int = 0x3093;
	pub const UNKNOWN: Int = -1;
	pub const VERTICAL_RESOLUTION: Int = 0x3091;

	impl<T: api::EGL1_2> Instance<T> {
		/// Set the current rendering API.
		pub fn bind_api(&self, api: Enum) -> Result<(), Error> {
			unsafe {
				if self.api.eglBindAPI(api) == TRUE {
					Ok(())
				} else {
					Err(self.get_error().unwrap())
				}
			}
		}

		/// Query the current rendering API.
		pub fn query_api(&self) -> Enum {
			unsafe { self.api.eglQueryAPI() }
		}

		/// Create a new EGL pixel buffer surface bound to an OpenVG image.
		///
		/// This will return a `BadParameter` error if `attrib_list` is not a valid
		/// attributes list (if it does not terminate with `NONE`).
		pub fn create_pbuffer_from_client_buffer(&self, display: Display, buffer_type: Enum, buffer: ClientBuffer, config: Config, attrib_list: &[Int]) -> Result<Surface, Error> {
			check_int_list(attrib_list)?;
			unsafe {
				let surface = self.api.eglCreatePbufferFromClientBuffer(
					display.as_ptr(),
					buffer_type,
					buffer.as_ptr(),
					config.as_ptr(),
					attrib_list.as_ptr(),
				);

				if surface != NO_SURFACE {
					Ok(Surface(surface))
				} else {
					Err(self.get_error().unwrap())
				}
			}
		}

		/// Release EGL per-thread state.
		pub fn release_thread(&self) -> Result<(), Error> {
			unsafe {
				if self.api.eglReleaseThread() == TRUE {
					Ok(())
				} else {
					Err(self.get_error().unwrap())
				}
			}
		}

		/// Complete client API execution prior to subsequent native rendering calls.
		pub fn wait_client(&self) -> Result<(), Error> {
			unsafe {
				if self.api.eglWaitClient() == TRUE {
					Ok(())
				} else {
					Err(self.get_error().unwrap())
				}
			}
		}
	}
}

#[cfg(feature = "1_2")]
pub use egl1_2::*;

// ------------------------------------------------------------------------------------------------
// EGL 1.3
// ------------------------------------------------------------------------------------------------

#[cfg(feature = "1_3")]
mod egl1_3 {
	use super::*;

	pub const CONFORMANT: Int = 0x3042;
	pub const CONTEXT_CLIENT_VERSION: Int = 0x3098;
	pub const MATCH_NATIVE_PIXMAP: Int = 0x3041;
	pub const OPENGL_ES2_BIT: Int = 0x0004;
	pub const VG_ALPHA_FORMAT: Int = 0x3088;
	pub const VG_ALPHA_FORMAT_NONPRE: Int = 0x308B;
	pub const VG_ALPHA_FORMAT_PRE: Int = 0x308C;
	pub const VG_ALPHA_FORMAT_PRE_BIT: Int = 0x0040;
	pub const VG_COLORSPACE: Int = 0x3087;
	pub const VG_COLORSPACE_sRGB: Int = 0x3089;
	pub const VG_COLORSPACE_LINEAR: Int = 0x308A;
	pub const VG_COLORSPACE_LINEAR_BIT: Int = 0x0020;
}

#[cfg(feature = "1_3")]
pub use egl1_3::*;

// ------------------------------------------------------------------------------------------------
// EGL 1.4
// ------------------------------------------------------------------------------------------------

#[cfg(feature = "1_4")]
mod egl1_4 {
	use super::*;

	pub const DEFAULT_DISPLAY: NativeDisplayType = 0 as NativeDisplayType;
	pub const MULTISAMPLE_RESOLVE_BOX_BIT: Int = 0x0200;
	pub const MULTISAMPLE_RESOLVE: Int = 0x3099;
	pub const MULTISAMPLE_RESOLVE_DEFAULT: Int = 0x309A;
	pub const MULTISAMPLE_RESOLVE_BOX: Int = 0x309B;
	pub const OPENGL_API: Enum = 0x30A2;
	pub const OPENGL_BIT: Int = 0x0008;
	pub const SWAP_BEHAVIOR_PRESERVED_BIT: Int = 0x0400;

	impl<T: api::EGL1_4> Instance<T> {
		/// Return the current EGL rendering context.
		pub fn get_current_context(&self) -> Option<Context> {
			unsafe {
				let context = self.api.eglGetCurrentContext();

				if context != NO_CONTEXT {
					Some(Context(context))
				} else {
					None
				}
			}
		}
	}
}

#[cfg(feature = "1_4")]
pub use egl1_4::*;

// ------------------------------------------------------------------------------------------------
// EGL 1.5
// ------------------------------------------------------------------------------------------------

#[cfg(feature = "1_5")]
mod egl1_5 {
	use super::*;

	pub type Time = u64;
	pub type EGLSync = *mut c_void;
	pub type EGLImage = *mut c_void;

	#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
	pub struct Sync(EGLSync);

	impl Sync {
		#[inline]
		pub unsafe fn from_ptr(ptr: EGLSync) -> Sync {
			Sync(ptr)
		}

		#[inline]
		pub fn as_ptr(&self) -> EGLSync {
			self.0
		}
	}

	#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
	pub struct Image(EGLImage);

	impl Image {
		#[inline]
		pub unsafe fn from_ptr(ptr: EGLImage) -> Image {
			Image(ptr)
		}

		#[inline]
		pub fn as_ptr(&self) -> EGLImage {
			self.0
		}
	}

	pub const CONTEXT_MAJOR_VERSION: Int = 0x3098;
	pub const CONTEXT_MINOR_VERSION: Int = 0x30FB;
	pub const CONTEXT_OPENGL_PROFILE_MASK: Int = 0x30FD;
	pub const CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY: Int = 0x31BD;
	pub const NO_RESET_NOTIFICATION: Int = 0x31BE;
	pub const LOSE_CONTEXT_ON_RESET: Int = 0x31BF;
	pub const CONTEXT_OPENGL_CORE_PROFILE_BIT: Int = 0x00000001;
	pub const CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT: Int = 0x00000002;
	pub const CONTEXT_OPENGL_DEBUG: Int = 0x31B0;
	pub const CONTEXT_OPENGL_FORWARD_COMPATIBLE: Int = 0x31B1;
	pub const CONTEXT_OPENGL_ROBUST_ACCESS: Int = 0x31B2;
	pub const OPENGL_ES3_BIT: Int = 0x00000040;
	pub const CL_EVENT_HANDLE: Int = 0x309C;
	pub const SYNC_CL_EVENT: Int = 0x30FE;
	pub const SYNC_CL_EVENT_COMPLETE: Int = 0x30FF;
	pub const SYNC_PRIOR_COMMANDS_COMPLETE: Int = 0x30F0;
	pub const SYNC_TYPE: Int = 0x30F7;
	pub const SYNC_STATUS: Int = 0x30F1;
	pub const SYNC_CONDITION: Int = 0x30F8;
	pub const SIGNALED: Int = 0x30F2;
	pub const UNSIGNALED: Int = 0x30F3;
	pub const SYNC_FLUSH_COMMANDS_BIT: Int = 0x0001;
	pub const FOREVER: u64 = 0xFFFFFFFFFFFFFFFFu64;
	pub const TIMEOUT_EXPIRED: Int = 0x30F5;
	pub const CONDITION_SATISFIED: Int = 0x30F6;
	pub const NO_SYNC: EGLSync = 0 as EGLSync;
	pub const SYNC_FENCE: Int = 0x30F9;
	pub const GL_COLORSPACE: Int = 0x309D;
	pub const GL_COLORSPACE_SRGB: Int = 0x3089;
	pub const GL_COLORSPACE_LINEAR: Int = 0x308A;
	pub const GL_RENDERBUFFER: Int = 0x30B9;
	pub const GL_TEXTURE_2D: Int = 0x30B1;
	pub const GL_TEXTURE_LEVEL: Int = 0x30BC;
	pub const GL_TEXTURE_3D: Int = 0x30B2;
	pub const GL_TEXTURE_ZOFFSET: Int = 0x30BD;
	pub const GL_TEXTURE_CUBE_MAP_POSITIVE_X: Int = 0x30B3;
	pub const GL_TEXTURE_CUBE_MAP_NEGATIVE_X: Int = 0x30B4;
	pub const GL_TEXTURE_CUBE_MAP_POSITIVE_Y: Int = 0x30B5;
	pub const GL_TEXTURE_CUBE_MAP_NEGATIVE_Y: Int = 0x30B6;
	pub const GL_TEXTURE_CUBE_MAP_POSITIVE_Z: Int = 0x30B7;
	pub const GL_TEXTURE_CUBE_MAP_NEGATIVE_Z: Int = 0x30B8;
	pub const IMAGE_PRESERVED: Int = 0x30D2;
	pub const NO_IMAGE: EGLImage = 0 as EGLImage;

	impl<T: api::EGL1_5> Instance<T> {
		/// Create a new EGL sync object.
		///
		/// Note that the constant `ATTRIB_NONE` which has the type `Attrib` can be used
		/// instead of `NONE` to terminate the attribute list.
		///
		/// This will return a `BadParameter` error if `attrib_list` is not a valid
		/// attributes list (if it does not terminate with `ATTRIB_NONE`).
		///
		/// This function is unsafe: when creating an OpenCL Event Sync Object, passing an invalid event
		/// handle in `attrib_list` may result in undefined behavior up to and including program
		/// termination.
		pub unsafe fn create_sync(&self, display: Display, ty: Enum, attrib_list: &[Attrib]) -> Result<Sync, Error> {
			check_attrib_list(attrib_list)?;
			let sync = self.api.eglCreateSync(display.as_ptr(), ty, attrib_list.as_ptr());
			if sync != NO_SYNC {
				Ok(Sync(sync))
			} else {
				Err(self.get_error().unwrap())
			}
		}

		/// Destroy a sync object.
		///
		/// This function is unsafe: if display does not match the display passed to eglCreateSync when
		/// sync was created, the behaviour is undefined.
		pub unsafe fn destroy_sync(&self, display: Display, sync: Sync) -> Result<(), Error> {
			if self.api.eglDestroySync(display.as_ptr(), sync.as_ptr()) == TRUE {
				Ok(())
			} else {
				Err(self.get_error().unwrap())
			}
		}

		/// Wait in the client for a sync object to be signalled.
		///
		/// This function is unsafe: if `display` does not match the [`Display`] passed to [`create_sync`](Self::create_sync)
		/// when `sync` was created, the behaviour is undefined.
		pub unsafe fn client_wait_sync(&self, display: Display, sync: Sync, flags: Int, timeout: Time) -> Result<Int, Error> {
			let status = self.api.eglClientWaitSync(display.as_ptr(), sync.as_ptr(), flags, timeout);
			if status != FALSE as Int {
				Ok(status)
			} else {
				Err(self.get_error().unwrap())
			}
		}

		/// Return an attribute of a sync object.
		///
		/// This function is unsafe: If `display` does not match the [`Display`] passed to [`create_sync`](Self::create_sync)
		/// when `sync` was created, behaviour is undefined.
		pub unsafe fn get_sync_attrib(&self, display: Display, sync: Sync, attribute: Int) -> Result<Attrib, Error> {
			let mut value = 0;
			if self.api.eglGetSyncAttrib(
				display.as_ptr(),
				sync.as_ptr(),
				attribute,
				&mut value as *mut Attrib,
			) == TRUE
			{
				Ok(value)
			} else {
				Err(self.get_error().unwrap())
			}
		}

		/// Create a new Image object.
		///
		/// Note that the constant `ATTRIB_NONE` which has the type `Attrib` can be used
		/// instead of `NONE` to terminate the attribute list.
		///
		/// This will return a `BadParameter` error if `attrib_list` is not a valid
		/// attributes list (if it does not terminate with `ATTRIB_NONE`).
		pub fn create_image(&self, display: Display, ctx: Context, target: Enum, buffer: ClientBuffer, attrib_list: &[Attrib]) -> Result<Image, Error> {
			check_attrib_list(attrib_list)?;
			unsafe {
				let image = self.api.eglCreateImage(
					display.as_ptr(),
					ctx.as_ptr(),
					target,
					buffer.as_ptr(),
					attrib_list.as_ptr(),
				);
				if image != NO_IMAGE {
					Ok(Image(image))
				} else {
					Err(self.get_error().unwrap())
				}
			}
		}

		/// Destroy an Image object.
		pub fn destroy_image(&self, display: Display, image: Image) -> Result<(), Error> {
			unsafe {
				if self.api.eglDestroyImage(display.as_ptr(), image.as_ptr()) == TRUE {
					Ok(())
				} else {
					Err(self.get_error().unwrap())
				}
			}
		}

		/// Return an EGL display connection.
		///
		/// Note that the constant `ATTRIB_NONE` which has the type `Attrib` can be used
		/// instead of `NONE` to terminate the attribute list.
		///
		/// This will return a `BadParameter` error if `attrib_list` is not a valid
		/// attributes list (if it does not terminate with `ATTRIB_NONE`).
		pub fn get_platform_display(&self, platform: Enum, native_display: *mut c_void, attrib_list: &[Attrib]) -> Result<Display, Error> {
			check_attrib_list(attrib_list)?;
			unsafe {
				let display = self.api.eglGetPlatformDisplay(platform, native_display, attrib_list.as_ptr());
				if display != NO_DISPLAY {
					Ok(Display(display))
				} else {
					Err(self.get_error().unwrap())
				}
			}
		}

		/// Create a new EGL on-screen rendering surface.
		///
		/// Note that the constant `ATTRIB_NONE` which has the type `Attrib` can be used
		/// instead of `NONE` to terminate the attribute list.
		///
		/// This will return a `BadParameter` error if `attrib_list` is not a valid
		/// attributes list (if it does not terminate with `ATTRIB_NONE`).
		pub fn create_platform_window_surface(&self, display: Display, config: Config, native_window: *mut c_void, attrib_list: &[Attrib]) -> Result<Surface, Error> {
			check_attrib_list(attrib_list)?;
			unsafe {
				let surface = self.api.eglCreatePlatformWindowSurface(
					display.as_ptr(),
					config.as_ptr(),
					native_window,
					attrib_list.as_ptr(),
				);
				if surface != NO_SURFACE {
					Ok(Surface(surface))
				} else {
					Err(self.get_error().unwrap())
				}
			}
		}

		/// Create a new EGL offscreen surface.
		///
		/// Note that the constant `ATTRIB_NONE` which has the type `Attrib` can be used
		/// instead of `NONE` to terminate the attribute list.
		///
		/// This will return a `BadParameter` error if `attrib_list` is not a valid
		/// attributes list (if it does not terminate with `ATTRIB_NONE`).
		pub fn create_platform_pixmap_surface(&self, display: Display, config: Config, native_pixmap: *mut c_void, attrib_list: &[Attrib]) -> Result<Surface, Error> {
			check_attrib_list(attrib_list)?;
			unsafe {
				let surface = self.api.eglCreatePlatformPixmapSurface(
					display.as_ptr(),
					config.as_ptr(),
					native_pixmap,
					attrib_list.as_ptr(),
				);
				if surface != NO_SURFACE {
					Ok(Surface(surface))
				} else {
					Err(self.get_error().unwrap())
				}
			}
		}

		/// Wait in the server for a sync object to be signalled.
		///
		/// This function is unsafe: if `display` does not match the [`Display`] passed to [`create_sync`](Self::create_sync)
		/// when `sync` was created, the behavior is undefined.
		pub fn wait_sync(&self, display: Display, sync: Sync, flags: Int) -> Result<(), Error> {
			unsafe {
				if self.api.eglWaitSync(display.as_ptr(), sync.as_ptr(), flags) == TRUE {
					Ok(())
				} else {
					Err(self.get_error().unwrap())
				}
			}
		}
	}
}

#[cfg(feature = "1_5")]
pub use egl1_5::*;

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

macro_rules! api {
	($($id:ident : $version:literal { $(fn $name:ident ($($arg:ident : $atype:ty ),* ) -> $rtype:ty ;)* }),*) => {
		#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
		pub enum Version {
			$(
				#[cfg(feature=$version)]
				$id,
			)*
		}

		impl std::fmt::Display for Version {
			fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
				match self {
					$(
						#[cfg(feature=$version)]
						Version::$id => write!(f, $version),
					)*
				}
			}
		}

		pub mod api {
			use super::*;

			api!(@api_traits () () $($id : $version { $(fn $name ($($arg : $atype ),* ) -> $rtype ;)* })*);
		}

		#[cfg(feature="static")]
		mod ffi {
			use libc::{c_char, c_void};

			use super::{
				Attrib, Boolean, EGLClientBuffer, EGLConfig, EGLContext, EGLDisplay, EGLImage, EGLSurface,
				EGLSync, Enum, Int, NativeDisplayType, NativePixmapType, NativeWindowType, Time,
			};

			$(
				extern "C" {
					$(
						#[cfg(feature=$version)]
						pub fn $name ($($arg : $atype ),* ) -> $rtype ;
					)*
				}
			)*
		}

		#[cfg(feature="static")]
		/// Static EGL API interface.
		///
		/// This type is only available when the `static` feature is enabled,
		/// by statically linking the EGL library at compile time.
		#[derive(Copy, Clone, Debug)]
		pub struct Static;

		#[cfg(feature="static")]
		impl Api for Static {
			#[inline(always)]
			fn version(&self) -> Version {
				LATEST
			}
		}

		#[cfg(feature="static")]
		pub static API: Instance<Static> = Instance::new(Static);

		#[cfg(feature="dynamic")]
		extern crate libloading;

		api!(@dynamic_struct $($id : $version { $(fn $name ($($arg : $atype ),* ) -> $rtype ;)* })*);
		api!(@api_types () $($id : $version { $(fn $name ($($arg : $atype ),* ) -> $rtype ;)* })*);
	};
	(@dynamic_struct $($id:ident : $version:literal { $(fn $name:ident ($($arg:ident : $atype:ty ),* ) -> $rtype:ty ;)* })*) => {
		#[cfg(feature="dynamic")]
		#[derive(Debug)]
		pub enum LoadError<L> {
			/// Something wrong happend while loading the library.
			Library(L),

			/// The provided version does not meet the requirements.
			InvalidVersion {
				provided: Version,
				required: Version
			}
		}

		#[cfg(feature="dynamic")]
		impl<L: std::error::Error + 'static> std::error::Error for LoadError<L> {
			fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
				match self {
					LoadError::Library(l) => Some(l),
					_ => None
				}
			}
		}

		#[cfg(feature="dynamic")]
		impl<L: std::fmt::Display> std::fmt::Display for LoadError<L> {
			fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
				match self {
					LoadError::Library(l) => write!(f, "Load error: {}", l),
					LoadError::InvalidVersion { provided, required } => write!(f, "Invalid EGL API version (required {}, provided {})", required, provided)
				}
			}
		}

		#[cfg(feature="dynamic")]
		struct RawDynamic<L> {
			lib: L,
			version: Version,
			$(
				$(
					#[cfg(feature=$version)]
					$name : std::mem::MaybeUninit<unsafe extern "C" fn($($atype ),*) -> $rtype>,
				)*
			)*
		}

		#[cfg(feature="dynamic")]
		impl<L> RawDynamic<L> {
			#[inline(always)]
			/// Returns the underlying EGL library.
			pub fn library(&self) -> &L {
				&self.lib
			}

			#[inline(always)]
			/// Returns the EGL version.
			pub fn version(&self) -> Version {
				self.version
			}

			#[inline(always)]
			/// Sets the EGL version.
			pub unsafe fn set_version(&mut self, version: Version) {
				self.version = version
			}

			/// Wraps the given library but does not load the symbols.
			pub unsafe fn unloaded(lib: L, version: Version) -> Self {
				RawDynamic {
					lib,
					version,
					$(
						$(
							#[cfg(feature=$version)]
							$name : std::mem::MaybeUninit::uninit(),
						)*
					)*
				}
			}
		}

		#[cfg(feature="dynamic")]
		/// Dynamic EGL API interface.
		///
		/// The first type parameter is the type of the underlying library handle.
		/// The second `Dynamic` type parameter gives the EGL API version provided by the library.
		///
		/// This type is only available when the `dynamic` feature is enabled.
		/// In most cases, you may prefer to directly use the `DynamicInstance` type.
		pub struct Dynamic<L, A> {
			raw: RawDynamic<L>,
			_api_version: std::marker::PhantomData<A>
		}

		#[cfg(feature="dynamic")]
		impl<L, A> Dynamic<L, A> {
			#[inline(always)]
			/// Return the underlying EGL library.
			pub fn library(&self) -> &L {
				self.raw.library()
			}

			/// Returns the provided EGL version.
			pub fn version(&self) -> Version {
				self.raw.version()
			}

			/// Wraps the given library but does not load the symbols.
			pub(crate) unsafe fn unloaded(lib: L, version: Version) -> Self {
				Dynamic {
					raw: RawDynamic::unloaded(lib, version),
					_api_version: std::marker::PhantomData
				}
			}
		}

		#[cfg(feature="dynamic")]
		impl<L, A> Api for Dynamic<L, A> {
			/// Returns the provided EGL version.
			#[inline(always)]
			fn version(&self) -> Version {
				self.version()
			}
		}

		#[cfg(feature="dynamic")]
		#[cfg(feature="1_0")]
		impl<L: std::borrow::Borrow<libloading::Library>> Dynamic<L, EGL1_0> {
			#[inline]
			/// Load the EGL API symbols from the given library.
			///
			/// This will load the most recent API provided by the library,
			/// which is at least EGL 1.0.
			/// You can check what version has actually been loaded using [`Dynamic::version`],
			/// and/or convert to a more recent version using [`try_into`](TryInto::try_into).
			///
			/// ## Safety
			/// This is fundamentally unsafe since there are no guaranties the input library complies to the EGL API.
			pub unsafe fn load_from(lib: L) -> Result<Dynamic<L, EGL1_0>, libloading::Error> {
				let mut result = Dynamic::unloaded(lib, Version::EGL1_0);

				$(
					match $id::load_from(&mut result.raw) {
						Ok(()) => result.raw.set_version(Version::$id),
						Err(libloading::Error::DlSymUnknown) => {
							if Version::$id == Version::EGL1_0 {
								return Err(libloading::Error::DlSymUnknown) // we require at least EGL 1.0.
							} else {
								return Ok(result)
							}
						},
						Err(libloading::Error::DlSym { desc }) => {
							if Version::$id == Version::EGL1_0 {
								return Err(libloading::Error::DlSym { desc }) // we require at least EGL 1.0.
							} else {
								return Ok(result)
							}
						},
						Err(e) => return Err(e)
					}
				)*

				Ok(result)
			}
		}

		#[cfg(feature="dynamic")]
		#[cfg(feature="1_0")]
		impl<L: std::borrow::Borrow<libloading::Library>> Instance<Dynamic<L, EGL1_0>> {
			#[inline(always)]
			/// Create an EGL instance using the symbols provided by the given library.
			///
			/// The most recent version of EGL provided by the given library is loaded.
			/// You can check what version has actually been loaded using [`Instance::version`],
			/// and/or convert to a more recent version using [`try_into`](TryInto::try_into).
			///
			/// ## Safety
			/// This is fundamentally unsafe since there are no guaranties the input library complies to the EGL API.
			pub unsafe fn load_from(lib: L) -> Result<Instance<Dynamic<L, EGL1_0>>, libloading::Error> {
				Ok(Instance::new(Dynamic::<L, EGL1_0>::load_from(lib)?))
			}
		}

		#[cfg(feature="dynamic")]
		impl<L, V> Instance<Dynamic<L, V>> {
			/// Cast the API.
			#[inline(always)]
			pub fn downcast<W>(&self) -> &Instance<Dynamic<L, W>> where Instance<Dynamic<L, V>>: Downcast<Instance<Dynamic<L, W>>> {
				Downcast::downcast(self)
			}

			/// Cast the API.
			#[inline(always)]
			pub fn upcast<W>(&self) -> Option<&Instance<Dynamic<L, W>>> where Instance<Dynamic<L, V>>: Upcast<Instance<Dynamic<L, W>>> {
				Upcast::upcast(self)
			}
		}

		#[cfg(feature="dynamic")]
		unsafe impl<L: std::borrow::Borrow<libloading::Library> + Send, A: Send> Send for Dynamic<L, A> {}

		#[cfg(feature="dynamic")]
		unsafe impl<L: std::borrow::Borrow<libloading::Library> + std::marker::Sync, A: std::marker::Sync> std::marker::Sync for Dynamic<L, A> {}

		#[cfg(feature="dynamic")]
		impl<L: std::borrow::Borrow<libloading::Library> + fmt::Debug, A> fmt::Debug for Dynamic<L, A> {
			fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
				write!(f, "Dynamic({:?})", self.library())
			}
		}
	};
	(@api_traits ( ) ( ) $id:ident : $version:literal { $(fn $name:ident ($($arg:ident : $atype:ty ),* ) -> $rtype:ty ;)* } $($t_id:ident : $t_version:literal { $(fn $t_name:ident ($($t_arg:ident : $t_atype:ty ),* ) -> $t_rtype:ty ;)* })*) => {
		api!(@api_trait ( ) ( ) $id : $version { $(fn $name ($($arg : $atype ),* ) -> $rtype ;)* });
		api!(@api_traits ( $id : $version ) ( : $id ) $($t_id : $t_version { $(fn $t_name ($($t_arg : $t_atype ),* ) -> $t_rtype ;)* })*);
	};
	(@api_traits ( $($pred:ident : $p_version:literal)+ ) ( $($deps:tt)+ ) $id:ident : $version:literal { $(fn $name:ident ($($arg:ident : $atype:ty ),* ) -> $rtype:ty ;)* } $($t_id:ident : $t_version:literal { $(fn $t_name:ident ($($t_arg:ident : $t_atype:ty ),* ) -> $t_rtype:ty ;)* })*) => {
		api!(@api_trait ( $($pred : $p_version)* ) ( $($deps)* ) $id : $version { $(fn $name ($($arg : $atype ),* ) -> $rtype ;)* });
		api!(@api_traits ( $($pred : $version)* $id : $version ) ( $($deps)* + $id ) $($t_id : $t_version { $(fn $t_name ($($t_arg : $t_atype ),* ) -> $t_rtype ;)* })*);
	};
	(@api_traits ( $($pred:ident : $p_version:literal)* ) ( $($deps:tt)* )) => {
		// nothing
	};
	(@api_trait ( $($pred:ident : $p_version:literal)* ) ( $($deps:tt)* ) $id:ident : $version:literal { $(fn $name:ident ($($arg:ident : $atype:ty ),* ) -> $rtype:ty ;)* }) => {
		/// EGL API interface.
		///
		/// An implementation of this trait can be used to create an [`Instance`].
		///
		/// This crate provides two implemntation of this trait:
		///  - [`Static`] which is available with the `static` feature enabled,
		///    defined by statically linking to the EGL library at compile time.
		///  - [`Dynamic`] which is available with the `dynamic` feature enabled,
		///    defined by dynamically linking to the EGL library at runtime.
		///    In this case, you may prefer to directly use the `DynamicInstance` type.
		#[cfg(feature=$version)]
		pub unsafe trait $id $($deps)* {
			$(
				unsafe fn $name (&self, $($arg : $atype ),* ) -> $rtype ;
			)*
		}
	};
	(@api_types ( ) $id:ident : $version:literal { $(fn $name:ident ($($arg:ident : $atype:ty ),* ) -> $rtype:ty ;)* } $($t_id:ident : $t_version:literal { $(fn $t_name:ident ($($t_arg:ident : $t_atype:ty ),* ) -> $t_rtype:ty ;)* })*) => {
		#[cfg(feature="dynamic")]
		$(
			#[cfg(not(feature=$t_version))]
		)*
		#[cfg(feature=$version)]
		/// Latest available EGL version.
		pub type Latest = $id;

		$(
			#[cfg(not(feature=$t_version))]
		)*
		#[cfg(feature=$version)]
		/// Latest available EGL version.
		pub const LATEST: Version = Version::$id;

		api!(@api_type ( ) $id : $version { $(fn $name ($($arg : $atype ),* ) -> $rtype ;)* });
		api!(@api_types ( $id : $version { $(fn $name ($($arg : $atype ),* ) -> $rtype ;)* } ) $($t_id : $t_version { $(fn $t_name ($($t_arg : $t_atype ),* ) -> $t_rtype ;)* })*);
	};
	(@api_types ( $($pred:ident : $p_version:literal { $(fn $p_name:ident ($($p_arg:ident : $p_atype:ty ),* ) -> $p_rtype:ty ;)* })+ ) $id:ident : $version:literal { $(fn $name:ident ($($arg:ident : $atype:ty ),* ) -> $rtype:ty ;)* } $($t_id:ident : $t_version:literal { $(fn $t_name:ident ($($t_arg:ident : $t_atype:ty ),* ) -> $t_rtype:ty ;)* })*) => {
		#[cfg(feature="dynamic")]
		$(
			#[cfg(not(feature=$t_version))]
		)*
		#[cfg(feature=$version)]
		/// Latest available EGL version.
		pub type Latest = $id;

		$(
			#[cfg(not(feature=$t_version))]
		)*
		#[cfg(feature=$version)]
		/// Latest available EGL version.
		pub const LATEST: Version = Version::$id;

		api!(@api_type ( $($pred : $p_version { $(fn $p_name ($($p_arg : $p_atype ),* ) -> $p_rtype ;)* })* ) $id : $version { $(fn $name ($($arg : $atype ),* ) -> $rtype ;)* });
		api!(@api_types ( $($pred : $p_version { $(fn $p_name ($($p_arg : $p_atype ),* ) -> $p_rtype ;)* })* $id : $version { $(fn $name ($($arg : $atype ),* ) -> $rtype ;)* } ) $($t_id : $t_version { $(fn $t_name ($($t_arg : $t_atype ),* ) -> $t_rtype ;)* })*);
	};
	(@api_types ( $($pred:ident : $p_version:literal { $(fn $p_name:ident ($($p_arg:ident : $p_atype:ty ),* ) -> $p_rtype:ty ;)* })+ ) ) => {
		#[cfg(feature="dynamic")]
		#[cfg(feature="1_0")]
		/// Alias for dynamically linked instances with the latest handled version of EGL.
		pub type DynamicInstance<V = Latest> = Instance<Dynamic<libloading::Library, V>>;

		#[cfg(feature="dynamic")]
		#[cfg(feature="1_0")]
		impl DynamicInstance<EGL1_0> {
			#[inline(always)]
			/// Create an EGL instance by finding and loading a dynamic library with the given filename.
			///
			/// See [`Library::new`](libloading::Library::new)
			/// for more details on how the `filename` argument is used.
			///
			/// On Linux plateforms, the library is loaded with the `RTLD_NODELETE` flag.
			/// See [#14](https://github.com/timothee-haudebourg/khronos-egl/issues/14) for more details.
			///
			/// ## Safety
			/// This is fundamentally unsafe since there are no guaranties the input library complies to the EGL API.
			pub unsafe fn load_from_filename<P: AsRef<std::ffi::OsStr>>(filename: P) -> Result<DynamicInstance<EGL1_0>, libloading::Error> {
				#[cfg(target_os = "linux")]
				let lib: libloading::Library = {
					// On Linux, load library with `RTLD_NOW | RTLD_NODELETE` to fix a SIGSEGV
					// See https://github.com/timothee-haudebourg/khronos-egl/issues/14 for more details.
					libloading::os::unix::Library::open(Some(filename), 0x2 | 0x1000)?.into()
				};
				#[cfg(not(target_os = "linux"))]
				let lib = libloading::Library::new(filename)?;
				Self::load_from(lib)
			}

			#[inline(always)]
			/// Create an EGL instance by finding and loading the `libEGL.so.1` or `libEGL.so` library.
			///
			/// This is equivalent to `DynamicInstance::load_from_filename("libEGL.so.1")`.
			///
			/// ## Safety
			/// This is fundamentally unsafe since there are no guaranties the found library complies to the EGL API.
			pub unsafe fn load() -> Result<DynamicInstance<EGL1_0>, libloading::Error> {
				Self::load_from_filename("libEGL.so.1").or(Self::load_from_filename("libEGL.so"))
			}
		}
	};
	(@api_type ( $($pred:ident : $p_version:literal { $(fn $p_name:ident ($($p_arg:ident : $p_atype:ty ),* ) -> $p_rtype:ty ;)* })* ) $id:ident : $version:literal { $(fn $name:ident ($($arg:ident : $atype:ty ),* ) -> $rtype:ty ;)* }) => {
		#[cfg(feature="static")]
		#[cfg(feature=$version)]
		unsafe impl api::$id for Static {
			$(
				#[inline(always)]
				unsafe fn $name(&self, $($arg : $atype),*) -> $rtype {
					ffi::$name($($arg),*)
				}
			)*
		}

		#[cfg(feature="dynamic")]
		#[cfg(feature=$version)]
		/// EGL version type.
		///
		/// Used by [`Dynamic`] to statically know the EGL API version provided by the library.
		pub struct $id;

		#[cfg(feature="dynamic")]
		#[cfg(feature=$version)]
		impl $id {
			#[allow(unused_variables)]
			unsafe fn load_from<L: std::borrow::Borrow<libloading::Library>>(raw: &mut RawDynamic<L>) -> Result<(), libloading::Error> {
				let lib = raw.lib.borrow();

				$(
					let name = stringify!($name).as_bytes();
					let symbol = lib.get::<unsafe extern "C" fn($($atype ),*) -> $rtype>(name)?;
					let ptr = (&symbol.into_raw().into_raw()) as *const *mut _ as *const unsafe extern "C" fn($($atype ),*) -> $rtype;
					assert!(!ptr.is_null());
					raw.$name = std::mem::MaybeUninit::new(*ptr);
				)*

				Ok(())
			}
		}

		$(
			#[cfg(feature="dynamic")]
			#[cfg(feature=$version)]
			unsafe impl<L: std::borrow::Borrow<libloading::Library>> api::$pred for Dynamic<L, $id> {
				$(
					#[inline(always)]
					unsafe fn $p_name(&self, $($p_arg : $p_atype),*) -> $p_rtype {
						(self.raw.$p_name.assume_init())($($p_arg),*)
					}
				)*
			}
		)*

		#[cfg(feature="dynamic")]
		#[cfg(feature=$version)]
		unsafe impl<L: std::borrow::Borrow<libloading::Library>> api::$id for Dynamic<L, $id> {
			$(
				#[inline(always)]
				unsafe fn $name(&self, $($arg : $atype),*) -> $rtype {
					(self.raw.$name.assume_init())($($arg),*)
				}
			)*
		}

		$(
			#[cfg(feature="dynamic")]
			#[cfg(feature=$version)]
			impl<L: std::borrow::Borrow<libloading::Library>> TryFrom<Dynamic<L, $pred>> for Dynamic<L, $id> {
				type Error = Dynamic<L, $pred>;

				fn try_from(other: Dynamic<L, $pred>) -> Result<Self, Dynamic<L, $pred>> {
					if other.version() >= Version::$id {
						Ok(Dynamic {
							raw: other.raw,
							_api_version: std::marker::PhantomData
						})
					} else {
						Err(other)
					}
				}
			}

			#[cfg(feature="dynamic")]
			#[cfg(feature=$version)]
			impl<L: std::borrow::Borrow<libloading::Library>> From<Dynamic<L, $id>> for Dynamic<L, $pred> {
				fn from(other: Dynamic<L, $id>) -> Self {
					Dynamic {
						raw: other.raw,
						_api_version: std::marker::PhantomData
					}
				}
			}

			#[cfg(feature="dynamic")]
			#[cfg(feature=$version)]
			impl<L: std::borrow::Borrow<libloading::Library>> AsRef<Dynamic<L, $pred>> for Dynamic<L, $id> {
				fn as_ref(&self) -> &Dynamic<L, $pred> {
					unsafe { std::mem::transmute(self) } // this is safe because both types have the same repr.
				}
			}

			#[cfg(feature="dynamic")]
			#[cfg(feature=$version)]
			impl<L: std::borrow::Borrow<libloading::Library>> Downcast<Dynamic<L, $pred>> for Dynamic<L, $id> {
				fn downcast(&self) -> &Dynamic<L, $pred> {
					unsafe { std::mem::transmute(self) } // this is safe because both types have the same repr.
				}
			}

			#[cfg(feature="dynamic")]
			#[cfg(feature=$version)]
			impl<L: std::borrow::Borrow<libloading::Library>> Downcast<Instance<Dynamic<L, $pred>>> for Instance<Dynamic<L, $id>> {
				fn downcast(&self) -> &Instance<Dynamic<L, $pred>> {
					unsafe { std::mem::transmute(self) } // this is safe because both types have the same repr.
				}
			}

			#[cfg(feature="dynamic")]
			#[cfg(feature=$version)]
			impl<L: std::borrow::Borrow<libloading::Library>> Upcast<Dynamic<L, $id>> for Dynamic<L, $pred> {
				fn upcast(&self) -> Option<&Dynamic<L, $id>> {
					if self.version() >= Version::$id {
						Some(unsafe { std::mem::transmute(self) }) // this is safe because both types have the same repr.
					} else {
						None
					}
				}
			}

			#[cfg(feature="dynamic")]
			#[cfg(feature=$version)]
			impl<L: std::borrow::Borrow<libloading::Library>> Upcast<Instance<Dynamic<L, $id>>> for Instance<Dynamic<L, $pred>> {
				fn upcast(&self) -> Option<&Instance<Dynamic<L, $id>>> {
					if self.version() >= Version::$id {
						Some(unsafe { std::mem::transmute(self) }) // this is safe because both types have the same repr.
					} else {
						None
					}
				}
			}
		)*

		#[cfg(feature="dynamic")]
		#[cfg(feature=$version)]
		impl<L: std::borrow::Borrow<libloading::Library>> Dynamic<L, $id> {
			#[inline]
			/// Load the EGL API symbols from the given library.
			///
			/// The second `Dynamic` type parameter gives the EGL API version expected to be provided by the library.
			///
			/// ## Safety
			/// This is fundamentally unsafe since there are no guaranties the input library complies to the EGL API.
			pub unsafe fn load_required(lib: L) -> Result<Dynamic<L, $id>, LoadError<libloading::Error>> {
				match Dynamic::<L, EGL1_0>::load_from(lib) {
					Ok(dynamic) => {
						let provided = dynamic.version();
						match dynamic.try_into() {
							Ok(t) => Ok(t),
							Err(_) => Err(LoadError::InvalidVersion {
								provided,
								required: Version::$id
							})
						}
					},
					Err(e) => Err(LoadError::Library(e))
				}
			}
		}

		#[cfg(feature="dynamic")]
		#[cfg(feature=$version)]
		impl<L: std::borrow::Borrow<libloading::Library>> Instance<Dynamic<L, $id>> {
			#[inline(always)]
			/// Create an EGL instance using the symbols provided by the given library.
			/// This function fails if the EGL library does not provide the minimum required version given by the type parameter.
			///
			/// ## Safety
			/// This is fundamentally unsafe since there are no guaranties the input library complies to the EGL API.
			pub unsafe fn load_required_from(lib: L) -> Result<Instance<Dynamic<L, $id>>, LoadError<libloading::Error>> {
				Ok(Instance::new(Dynamic::<L, $id>::load_required(lib)?))
			}
		}

		#[cfg(feature="dynamic")]
		#[cfg(feature=$version)]
		impl DynamicInstance<$id> {
			#[inline(always)]
			/// Create an EGL instance by finding and loading a dynamic library with the given filename.
			/// This function fails if the EGL library does not provide the minimum required version given by the type parameter.
			///
			/// See [`Library::new`](libloading::Library::new)
			/// for more details on how the `filename` argument is used.
			///
			/// On Linux plateforms, the library is loaded with the `RTLD_NODELETE` flag.
			/// See [#14](https://github.com/timothee-haudebourg/khronos-egl/issues/14) for more details.
			///
			/// ## Safety
			/// This is fundamentally unsafe since there are no guaranties the input library complies to the EGL API.
			pub unsafe fn load_required_from_filename<P: AsRef<std::ffi::OsStr>>(filename: P) -> Result<DynamicInstance<$id>, LoadError<libloading::Error>> {
				#[cfg(target_os = "linux")]
				let lib: libloading::Library = {
					// On Linux, load library with `RTLD_NOW | RTLD_NODELETE` to fix a SIGSEGV
					// See https://github.com/timothee-haudebourg/khronos-egl/issues/14 for more details.
					libloading::os::unix::Library::open(Some(filename), 0x2 | 0x1000).map_err(LoadError::Library)?.into()
				};
				#[cfg(not(target_os = "linux"))]
				let lib = libloading::Library::new(filename).map_err(LoadError::Library)?;
				Self::load_required_from(lib)
			}

			#[inline(always)]
			/// Create an EGL instance by finding and loading the `libEGL.so.1` or `libEGL.so` library.
			/// This function fails if the EGL library does not provide the minimum required version given by the type parameter.
			///
			/// This is equivalent to `DynamicInstance::load_required_from_filename("libEGL.so.1")`.
			///
			/// ## Safety
			/// This is fundamentally unsafe since there are no guaranties the found library complies to the EGL API.
			pub unsafe fn load_required() -> Result<DynamicInstance<$id>, LoadError<libloading::Error>> {
			    Self::load_required_from_filename("libEGL.so.1").or(Self::load_required_from_filename("libEGL.so"))
			}
		}
	}
}

api! {
	EGL1_0 : "1_0" {
		fn eglChooseConfig(
			display: EGLDisplay,
			attrib_list: *const Int,
			configs: *mut EGLConfig,
			config_size: Int,
			num_config: *mut Int
		) -> Boolean;
		fn eglCopyBuffers(
			display: EGLDisplay,
			surface: EGLSurface,
			target: NativePixmapType
		) -> Boolean;
		fn eglCreateContext(
			display: EGLDisplay,
			config: EGLConfig,
			share_context: EGLContext,
			attrib_list: *const Int
		) -> EGLContext;
		fn eglCreatePbufferSurface(
			display: EGLDisplay,
			config: EGLConfig,
			attrib_list: *const Int
		) -> EGLSurface;
		fn eglCreatePixmapSurface(
			display: EGLDisplay,
			config: EGLConfig,
			pixmap: NativePixmapType,
			attrib_list: *const Int
		) -> EGLSurface;
		fn eglCreateWindowSurface(
			display: EGLDisplay,
			config: EGLConfig,
			win: NativeWindowType,
			attrib_list: *const Int
		) -> EGLSurface;
		fn eglDestroyContext(display: EGLDisplay, ctx: EGLContext) -> Boolean;
		fn eglDestroySurface(display: EGLDisplay, surface: EGLSurface) -> Boolean;
		fn eglGetConfigAttrib(
			display: EGLDisplay,
			config: EGLConfig,
			attribute: Int,
			value: *mut Int
		) -> Boolean;
		fn eglGetConfigs(
			display: EGLDisplay,
			configs: *mut EGLConfig,
			config_size: Int,
			num_config: *mut Int
		) -> Boolean;
		fn eglGetCurrentDisplay() -> EGLDisplay;
		fn eglGetCurrentSurface(readdraw: Int) -> EGLSurface;
		fn eglGetDisplay(display_id: NativeDisplayType) -> EGLDisplay;
		fn eglGetError() -> Int;
		fn eglGetProcAddress(procname: *const c_char) -> extern "C" fn();
		fn eglInitialize(display: EGLDisplay, major: *mut Int, minor: *mut Int) -> Boolean;
		fn eglMakeCurrent(
			display: EGLDisplay,
			draw: EGLSurface,
			read: EGLSurface,
			ctx: EGLContext
		) -> Boolean;
		fn eglQueryContext(
			display: EGLDisplay,
			ctx: EGLContext,
			attribute: Int,
			value: *mut Int
		) -> Boolean;
		fn eglQueryString(display: EGLDisplay, name: Int) -> *const c_char;
		fn eglQuerySurface(
			display: EGLDisplay,
			surface: EGLSurface,
			attribute: Int,
			value: *mut Int
		) -> Boolean;
		fn eglSwapBuffers(display: EGLDisplay, surface: EGLSurface) -> Boolean;
		fn eglTerminate(display: EGLDisplay) -> Boolean;
		fn eglWaitGL() -> Boolean;
		fn eglWaitNative(engine: Int) -> Boolean;
	},

	EGL1_1 : "1_1" {
		fn eglBindTexImage(display: EGLDisplay, surface: EGLSurface, buffer: Int) -> Boolean;
		fn eglReleaseTexImage(display: EGLDisplay, surface: EGLSurface, buffer: Int) -> Boolean;
		fn eglSurfaceAttrib(
			display: EGLDisplay,
			surface: EGLSurface,
			attribute: Int,
			value: Int
		) -> Boolean;
		fn eglSwapInterval(display: EGLDisplay, interval: Int) -> Boolean;
	},

	EGL1_2 : "1_2" {
		fn eglBindAPI(api: Enum) -> Boolean;
		fn eglQueryAPI() -> Enum;
		fn eglCreatePbufferFromClientBuffer(
			display: EGLDisplay,
			buftype: Enum,
			buffer: EGLClientBuffer,
			config: EGLConfig,
			attrib_list: *const Int
		) -> EGLSurface;
		fn eglReleaseThread() -> Boolean;
		fn eglWaitClient() -> Boolean;
	},

	EGL1_3 : "1_3" {
		// nothing.
	},

	EGL1_4 : "1_4" {
		fn eglGetCurrentContext() -> EGLContext;
	},

	EGL1_5 : "1_5" {
		fn eglCreateSync(display: EGLDisplay, type_: Enum, attrib_list: *const Attrib) -> EGLSync;
		fn eglDestroySync(display: EGLDisplay, sync: EGLSync) -> Boolean;
		fn eglClientWaitSync(display: EGLDisplay, sync: EGLSync, flags: Int, timeout: Time) -> Int;
		fn eglGetSyncAttrib(
			display: EGLDisplay,
			sync: EGLSync,
			attribute: Int,
			value: *mut Attrib
		) -> Boolean;
		fn eglCreateImage(
			display: EGLDisplay,
			ctx: EGLContext,
			target: Enum,
			buffer: EGLClientBuffer,
			attrib_list: *const Attrib
		) -> EGLImage;
		fn eglDestroyImage(display: EGLDisplay, image: EGLImage) -> Boolean;
		fn eglGetPlatformDisplay(
			platform: Enum,
			native_display: *mut c_void,
			attrib_list: *const Attrib
		) -> EGLDisplay;
		fn eglCreatePlatformWindowSurface(
			display: EGLDisplay,
			config: EGLConfig,
			native_window: *mut c_void,
			attrib_list: *const Attrib
		) -> EGLSurface;
		fn eglCreatePlatformPixmapSurface(
			display: EGLDisplay,
			config: EGLConfig,
			native_pixmap: *mut c_void,
			attrib_list: *const Attrib
		) -> EGLSurface;
		fn eglWaitSync(display: EGLDisplay, sync: EGLSync, flags: Int) -> Boolean;
	}
}