Commit 0eeb76d869dcd829cf86dc4c062e6fa82f056bae

Gabriel Jacobo 2013-08-19T16:29:46

Fixes bug #2037, common EGL code for Android and X11

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
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
diff --git a/Android.mk b/Android.mk
index 3230f9f..7338233 100755
--- a/Android.mk
+++ b/Android.mk
@@ -44,6 +44,6 @@ LOCAL_SRC_FILES := \
 	$(wildcard $(LOCAL_PATH)/src/video/android/*.c))
 
 LOCAL_CFLAGS += -DGL_GLEXT_PROTOTYPES
-LOCAL_LDLIBS := -ldl -lGLESv1_CM -lGLESv2 -llog
+LOCAL_LDLIBS := -ldl -lGLESv1_CM -lGLESv2 -llog -landroid
 
 include $(BUILD_SHARED_LIBRARY)
diff --git a/android-project/src/org/libsdl/app/SDLActivity.java b/android-project/src/org/libsdl/app/SDLActivity.java
index 7184a32..f173653 100644
--- a/android-project/src/org/libsdl/app/SDLActivity.java
+++ b/android-project/src/org/libsdl/app/SDLActivity.java
@@ -1,11 +1,5 @@
 package org.libsdl.app;
 
-import javax.microedition.khronos.egl.EGL10;
-import javax.microedition.khronos.egl.EGLConfig;
-import javax.microedition.khronos.egl.EGLContext;
-import javax.microedition.khronos.egl.EGLDisplay;
-import javax.microedition.khronos.egl.EGLSurface;
-
 import android.app.*;
 import android.content.*;
 import android.view.*;
@@ -43,13 +37,6 @@ public class SDLActivity extends Activity {
     protected static Thread mAudioThread;
     protected static AudioTrack mAudioTrack;
 
-    // EGL objects
-    protected static EGLContext  mEGLContext;
-    protected static EGLSurface  mEGLSurface;
-    protected static EGLDisplay  mEGLDisplay;
-    protected static EGLConfig   mEGLConfig;
-    protected static int mGLMajor, mGLMinor;
-
     // Load the .so
     static {
         System.loadLibrary("SDL2");
@@ -70,9 +57,7 @@ public class SDLActivity extends Activity {
         mSingleton = this;
 
         // Set up the surface
-        mEGLSurface = EGL10.EGL_NO_SURFACE;
         mSurface = new SDLSurface(getApplication());
-        mEGLContext = EGL10.EGL_NO_CONTEXT;
 
         mLayout = new AbsoluteLayout(this);
         mLayout.addView(mSurface);
@@ -252,29 +237,12 @@ public class SDLActivity extends Activity {
                                             int action, float x, 
                                             float y, float p);
     public static native void onNativeAccel(float x, float y, float z);
-
-    // Java functions called from C
-
-    public static boolean createGLContext(int majorVersion, int minorVersion, int[] attribs) {
-        return initEGL(majorVersion, minorVersion, attribs);
-    }
-    
-    public static void deleteGLContext() {
-        if (SDLActivity.mEGLDisplay != null && SDLActivity.mEGLContext != EGL10.EGL_NO_CONTEXT) {
-            EGL10 egl = (EGL10)EGLContext.getEGL();
-            egl.eglMakeCurrent(SDLActivity.mEGLDisplay, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT);
-            egl.eglDestroyContext(SDLActivity.mEGLDisplay, SDLActivity.mEGLContext);
-            SDLActivity.mEGLContext = EGL10.EGL_NO_CONTEXT;
-
-            if (SDLActivity.mEGLSurface != EGL10.EGL_NO_SURFACE) {
-                egl.eglDestroySurface(SDLActivity.mEGLDisplay, SDLActivity.mEGLSurface);
-                SDLActivity.mEGLSurface = EGL10.EGL_NO_SURFACE;
-            }
-        }
-    }
+    public static native void onNativeSurfaceChanged();
+    public static native void onNativeSurfaceDestroyed();
+    public static native void nativeFlipBuffers();
 
     public static void flipBuffers() {
-        flipEGL();
+        SDLActivity.nativeFlipBuffers();
     }
 
     public static boolean setActivityTitle(String title) {
@@ -332,147 +300,9 @@ public class SDLActivity extends Activity {
         // Transfer the task to the main thread as a Runnable
         return mSingleton.commandHandler.post(new ShowTextInputTask(x, y, w, h));
     }
-
-
-    // EGL functions
-    public static boolean initEGL(int majorVersion, int minorVersion, int[] attribs) {
-        try {
-            EGL10 egl = (EGL10)EGLContext.getEGL();
-            
-            if (SDLActivity.mEGLDisplay == null) {
-                SDLActivity.mEGLDisplay = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
-                int[] version = new int[2];
-                egl.eglInitialize(SDLActivity.mEGLDisplay, version);
-            }
-            
-            if (SDLActivity.mEGLDisplay != null && SDLActivity.mEGLContext == EGL10.EGL_NO_CONTEXT) {
-                // No current GL context exists, we will create a new one.
-                Log.v("SDL", "Starting up OpenGL ES " + majorVersion + "." + minorVersion);
-                EGLConfig[] configs = new EGLConfig[128];
-                int[] num_config = new int[1];
-                if (!egl.eglChooseConfig(SDLActivity.mEGLDisplay, attribs, configs, 1, num_config) || num_config[0] == 0) {
-                    Log.e("SDL", "No EGL config available");
-                    return false;
-                }
-                EGLConfig config = null;
-                int bestdiff = -1, bitdiff;
-                int[] value = new int[1];
-                
-                // eglChooseConfig returns a number of configurations that match or exceed the requested attribs.
-                // From those, we select the one that matches our requirements more closely
-                Log.v("SDL", "Got " + num_config[0] + " valid modes from egl");
-                for(int i = 0; i < num_config[0]; i++) {
-                    bitdiff = 0;
-                    // Go through some of the attributes and compute the bit difference between what we want and what we get.
-                    for (int j = 0; ; j += 2) {
-                        if (attribs[j] == EGL10.EGL_NONE)
-                            break;
-
-                        if (attribs[j+1] != EGL10.EGL_DONT_CARE && (attribs[j] == EGL10.EGL_RED_SIZE ||
-                            attribs[j] == EGL10.EGL_GREEN_SIZE ||
-                            attribs[j] == EGL10.EGL_BLUE_SIZE ||
-                            attribs[j] == EGL10.EGL_ALPHA_SIZE ||
-                            attribs[j] == EGL10.EGL_DEPTH_SIZE ||
-                            attribs[j] == EGL10.EGL_STENCIL_SIZE)) {
-                            egl.eglGetConfigAttrib(SDLActivity.mEGLDisplay, configs[i], attribs[j], value);
-                            bitdiff += value[0] - attribs[j + 1]; // value is always >= attrib
-                        }
-                    }
-                    
-                    if (bitdiff < bestdiff || bestdiff == -1) {
-                        config = configs[i];
-                        bestdiff = bitdiff;
-                    }
-                    
-                    if (bitdiff == 0) break; // we found an exact match!
-                }
-                
-                Log.d("SDL", "Selected mode with a total bit difference of " + bestdiff);
-
-                SDLActivity.mEGLConfig = config;
-                SDLActivity.mGLMajor = majorVersion;
-                SDLActivity.mGLMinor = minorVersion;
-            }
             
-            return SDLActivity.createEGLSurface();
-
-        } catch(Exception e) {
-            Log.v("SDL", e + "");
-            for (StackTraceElement s : e.getStackTrace()) {
-                Log.v("SDL", s.toString());
-            }
-            return false;
-        }
-    }
-
-    public static boolean createEGLContext() {
-        EGL10 egl = (EGL10)EGLContext.getEGL();
-        int EGL_CONTEXT_CLIENT_VERSION=0x3098;
-        int contextAttrs[] = new int[] { EGL_CONTEXT_CLIENT_VERSION, SDLActivity.mGLMajor, EGL10.EGL_NONE };
-        SDLActivity.mEGLContext = egl.eglCreateContext(SDLActivity.mEGLDisplay, SDLActivity.mEGLConfig, EGL10.EGL_NO_CONTEXT, contextAttrs);
-        if (SDLActivity.mEGLContext == EGL10.EGL_NO_CONTEXT) {
-            Log.e("SDL", "Couldn't create context");
-            return false;
-        }
-        return true;
-    }
-
-    public static boolean createEGLSurface() {
-        if (SDLActivity.mEGLDisplay != null && SDLActivity.mEGLConfig != null) {
-            EGL10 egl = (EGL10)EGLContext.getEGL();
-            if (SDLActivity.mEGLContext == EGL10.EGL_NO_CONTEXT) createEGLContext();
-
-            if (SDLActivity.mEGLSurface == EGL10.EGL_NO_SURFACE) {
-                Log.v("SDL", "Creating new EGL Surface");
-                SDLActivity.mEGLSurface = egl.eglCreateWindowSurface(SDLActivity.mEGLDisplay, SDLActivity.mEGLConfig, SDLActivity.mSurface, null);
-                if (SDLActivity.mEGLSurface == EGL10.EGL_NO_SURFACE) {
-                    Log.e("SDL", "Couldn't create surface");
-                    return false;
-                }
-            }
-            else Log.v("SDL", "EGL Surface remains valid");
-
-            if (egl.eglGetCurrentContext() != SDLActivity.mEGLContext) {
-                if (!egl.eglMakeCurrent(SDLActivity.mEGLDisplay, SDLActivity.mEGLSurface, SDLActivity.mEGLSurface, SDLActivity.mEGLContext)) {
-                    Log.e("SDL", "Old EGL Context doesnt work, trying with a new one");
-                    // TODO: Notify the user via a message that the old context could not be restored, and that textures need to be manually restored.
-                    createEGLContext();
-                    if (!egl.eglMakeCurrent(SDLActivity.mEGLDisplay, SDLActivity.mEGLSurface, SDLActivity.mEGLSurface, SDLActivity.mEGLContext)) {
-                        Log.e("SDL", "Failed making EGL Context current");
-                        return false;
-                    }
-                }
-                else Log.v("SDL", "EGL Context made current");
-            }
-            else Log.v("SDL", "EGL Context remains current");
-
-            return true;
-        } else {
-            Log.e("SDL", "Surface creation failed, display = " + SDLActivity.mEGLDisplay + ", config = " + SDLActivity.mEGLConfig);
-            return false;
-        }
-    }
-
-    // EGL buffer flip
-    public static void flipEGL() {
-        try {
-            EGL10 egl = (EGL10)EGLContext.getEGL();
-
-            egl.eglWaitNative(EGL10.EGL_CORE_NATIVE_ENGINE, null);
-
-            // drawing here
-
-            egl.eglWaitGL();
-
-            egl.eglSwapBuffers(SDLActivity.mEGLDisplay, SDLActivity.mEGLSurface);
-
-
-        } catch(Exception e) {
-            Log.v("SDL", "flipEGL(): " + e);
-            for (StackTraceElement s : e.getStackTrace()) {
-                Log.v("SDL", s.toString());
-            }
-        }
+    public static Surface getNativeSurface() {
+        return SDLActivity.mSurface.getNativeSurface();
     }
 
     // Audio
@@ -600,14 +430,16 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
         mWidth = 1.0f;
         mHeight = 1.0f;
     }
+    
+    public Surface getNativeSurface() {
+        return getHolder().getSurface();
+    }
 
     // Called when we have a valid drawing surface
     @Override
     public void surfaceCreated(SurfaceHolder holder) {
         Log.v("SDL", "surfaceCreated()");
         holder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
-        // Set mIsSurfaceReady to 'true' *before* any call to handleResume
-        SDLActivity.mIsSurfaceReady = true;
     }
 
     // Called when we lose the surface
@@ -617,16 +449,7 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
         // Call this *before* setting mIsSurfaceReady to 'false'
         SDLActivity.handlePause();
         SDLActivity.mIsSurfaceReady = false;
-
-        /* We have to clear the current context and destroy the egl surface here
-         * Otherwise there's BAD_NATIVE_WINDOW errors coming from eglCreateWindowSurface on resume
-         * Ref: http://stackoverflow.com/questions/8762589/eglcreatewindowsurface-on-ics-and-switching-from-2d-to-3d
-         */
-        
-        EGL10 egl = (EGL10)EGLContext.getEGL();
-        egl.eglMakeCurrent(SDLActivity.mEGLDisplay, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT);
-        egl.eglDestroySurface(SDLActivity.mEGLDisplay, SDLActivity.mEGLSurface);
-        SDLActivity.mEGLSurface = EGL10.EGL_NO_SURFACE;
+        SDLActivity.onNativeSurfaceDestroyed();
     }
 
     // Called when the surface is resized
@@ -687,6 +510,8 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
 
         // Set mIsSurfaceReady to 'true' *before* making a call to handleResume
         SDLActivity.mIsSurfaceReady = true;
+        SDLActivity.onNativeSurfaceChanged();
+
 
         if (SDLActivity.mSDLThread == null) {
             // This is the entry point to the C app.
diff --git a/configure b/configure
index 38084c2..41adac4 100755
--- a/configure
+++ b/configure
@@ -20547,9 +20547,9 @@ fi
 CheckOpenGLESX11()
 {
     if test x$enable_video = xyes -a x$enable_video_opengles = xyes; then
-        { $as_echo "$as_me:${as_lineno-$LINENO}: checking for OpenGL ES (EGL) support" >&5
-$as_echo_n "checking for OpenGL ES (EGL) support... " >&6; }
-        video_opengles=no
+        { $as_echo "$as_me:${as_lineno-$LINENO}: checking for EGL support" >&5
+$as_echo_n "checking for EGL support... " >&6; }
+        video_opengl_egl=no
         cat confdefs.h - <<_ACEOF >conftest.$ac_ext
 /* end confdefs.h.  */
 
@@ -20566,21 +20566,26 @@ main ()
 _ACEOF
 if ac_fn_c_try_compile "$LINENO"; then :
 
-        video_opengles=yes
+        video_opengl_egl=yes
 
 fi
 rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
-        { $as_echo "$as_me:${as_lineno-$LINENO}: result: $video_opengles" >&5
-$as_echo "$video_opengles" >&6; }
-        if test x$video_opengles = xyes; then
-            { $as_echo "$as_me:${as_lineno-$LINENO}: checking for OpenGL ES v1 headers" >&5
+        { $as_echo "$as_me:${as_lineno-$LINENO}: result: $video_opengl_egl" >&5
+$as_echo "$video_opengl_egl" >&6; }
+        if test x$video_opengl_egl = xyes; then
+
+$as_echo "#define SDL_VIDEO_OPENGL_EGL 1" >>confdefs.h
+
+        fi
+
+        { $as_echo "$as_me:${as_lineno-$LINENO}: checking for OpenGL ES v1 headers" >&5
 $as_echo_n "checking for OpenGL ES v1 headers... " >&6; }
-            video_opengles_v1=no
-            cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+        video_opengles_v1=no
+        cat confdefs.h - <<_ACEOF >conftest.$ac_ext
 /* end confdefs.h.  */
 
-             #include <GLES/gl.h>
-             #include <GLES/glext.h>
+         #include <GLES/gl.h>
+         #include <GLES/glext.h>
 
 int
 main ()
@@ -20593,28 +20598,29 @@ main ()
 _ACEOF
 if ac_fn_c_try_compile "$LINENO"; then :
 
-            video_opengles_v1=yes
+        video_opengles_v1=yes
 
 fi
 rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
-            { $as_echo "$as_me:${as_lineno-$LINENO}: result: $video_opengles_v1" >&5
+        { $as_echo "$as_me:${as_lineno-$LINENO}: result: $video_opengles_v1" >&5
 $as_echo "$video_opengles_v1" >&6; }
-            if test x$video_opengles_v1 = xyes; then
+        if test x$video_opengles_v1 = xyes; then
 
 $as_echo "#define SDL_VIDEO_OPENGL_ES 1" >>confdefs.h
 
 
 $as_echo "#define SDL_VIDEO_RENDER_OGL_ES 1" >>confdefs.h
 
-            fi
-            { $as_echo "$as_me:${as_lineno-$LINENO}: checking for OpenGL ES v2 headers" >&5
+        fi
+
+        { $as_echo "$as_me:${as_lineno-$LINENO}: checking for OpenGL ES v2 headers" >&5
 $as_echo_n "checking for OpenGL ES v2 headers... " >&6; }
-            video_opengles_v2=no
-            cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+        video_opengles_v2=no
+        cat confdefs.h - <<_ACEOF >conftest.$ac_ext
 /* end confdefs.h.  */
 
-             #include <GLES2/gl2.h>
-             #include <GLES2/gl2ext.h>
+         #include <GLES2/gl2.h>
+         #include <GLES2/gl2ext.h>
 
 int
 main ()
@@ -20627,20 +20633,19 @@ main ()
 _ACEOF
 if ac_fn_c_try_compile "$LINENO"; then :
 
-            video_opengles_v2=yes
+        video_opengles_v2=yes
 
 fi
 rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
-            { $as_echo "$as_me:${as_lineno-$LINENO}: result: $video_opengles_v2" >&5
+        { $as_echo "$as_me:${as_lineno-$LINENO}: result: $video_opengles_v2" >&5
 $as_echo "$video_opengles_v2" >&6; }
-            if test x$video_opengles_v2 = xyes; then
+        if test x$video_opengles_v2 = xyes; then
 
 $as_echo "#define SDL_VIDEO_OPENGL_ES2 1" >>confdefs.h
 
 
 $as_echo "#define SDL_VIDEO_RENDER_OGL_ES2 1" >>confdefs.h
 
-            fi
         fi
     fi
 }
diff --git a/configure.in b/configure.in
index 252efe4..de32e6a 100644
--- a/configure.in
+++ b/configure.in
@@ -1681,44 +1681,47 @@ dnl Find OpenGL ES
 CheckOpenGLESX11()
 {
     if test x$enable_video = xyes -a x$enable_video_opengles = xyes; then
-        AC_MSG_CHECKING(for OpenGL ES (EGL) support)
-        video_opengles=no
+        AC_MSG_CHECKING(for EGL support)
+        video_opengl_egl=no
         AC_TRY_COMPILE([
          #include <EGL/egl.h>
         ],[
         ],[
-        video_opengles=yes
+        video_opengl_egl=yes
         ])
-        AC_MSG_RESULT($video_opengles)
-        if test x$video_opengles = xyes; then
-            AC_MSG_CHECKING(for OpenGL ES v1 headers)
-            video_opengles_v1=no
-            AC_TRY_COMPILE([
-             #include <GLES/gl.h>
-             #include <GLES/glext.h>
-            ],[
-            ],[
-            video_opengles_v1=yes
-            ])
-            AC_MSG_RESULT($video_opengles_v1)
-            if test x$video_opengles_v1 = xyes; then
-                AC_DEFINE(SDL_VIDEO_OPENGL_ES, 1, [ ])
-                AC_DEFINE(SDL_VIDEO_RENDER_OGL_ES, 1, [ ])
-            fi
-            AC_MSG_CHECKING(for OpenGL ES v2 headers)
-            video_opengles_v2=no
-            AC_TRY_COMPILE([
-             #include <GLES2/gl2.h>
-             #include <GLES2/gl2ext.h>
-            ],[
-            ],[
-            video_opengles_v2=yes
-            ])
-            AC_MSG_RESULT($video_opengles_v2)
-            if test x$video_opengles_v2 = xyes; then
-                AC_DEFINE(SDL_VIDEO_OPENGL_ES2, 1, [ ])
-                AC_DEFINE(SDL_VIDEO_RENDER_OGL_ES2, 1, [ ])
-            fi
+        AC_MSG_RESULT($video_opengl_egl)
+        if test x$video_opengl_egl = xyes; then
+            AC_DEFINE(SDL_VIDEO_OPENGL_EGL, 1, [ ])
+        fi
+            
+        AC_MSG_CHECKING(for OpenGL ES v1 headers)
+        video_opengles_v1=no
+        AC_TRY_COMPILE([
+         #include <GLES/gl.h>
+         #include <GLES/glext.h>
+        ],[
+        ],[
+        video_opengles_v1=yes
+        ])
+        AC_MSG_RESULT($video_opengles_v1)
+        if test x$video_opengles_v1 = xyes; then
+            AC_DEFINE(SDL_VIDEO_OPENGL_ES, 1, [ ])
+            AC_DEFINE(SDL_VIDEO_RENDER_OGL_ES, 1, [ ])
+        fi
+        
+        AC_MSG_CHECKING(for OpenGL ES v2 headers)
+        video_opengles_v2=no
+        AC_TRY_COMPILE([
+         #include <GLES2/gl2.h>
+         #include <GLES2/gl2ext.h>
+        ],[
+        ],[
+        video_opengles_v2=yes
+        ])
+        AC_MSG_RESULT($video_opengles_v2)
+        if test x$video_opengles_v2 = xyes; then
+            AC_DEFINE(SDL_VIDEO_OPENGL_ES2, 1, [ ])
+            AC_DEFINE(SDL_VIDEO_RENDER_OGL_ES2, 1, [ ])
         fi
     fi
 }
diff --git a/include/SDL_config.h.in b/include/SDL_config.h.in
index 04ee110..3f92d57 100644
--- a/include/SDL_config.h.in
+++ b/include/SDL_config.h.in
@@ -290,6 +290,7 @@
 #undef SDL_VIDEO_OPENGL_ES
 #undef SDL_VIDEO_OPENGL_BGL
 #undef SDL_VIDEO_OPENGL_CGL
+#undef SDL_VIDEO_OPENGL_EGL
 #undef SDL_VIDEO_OPENGL_GLX
 #undef SDL_VIDEO_OPENGL_WGL
 #undef SDL_VIDEO_OPENGL_OSMESA
diff --git a/include/SDL_config_android.h b/include/SDL_config_android.h
index e0c019f..7c6b6cb 100644
--- a/include/SDL_config_android.h
+++ b/include/SDL_config_android.h
@@ -129,6 +129,7 @@
 
 /* Enable OpenGL ES */
 #define SDL_VIDEO_OPENGL_ES 1
+#define SDL_VIDEO_OPENGL_EGL 1
 #define SDL_VIDEO_RENDER_OGL_ES 1
 #define SDL_VIDEO_RENDER_OGL_ES2    1
 
diff --git a/include/SDL_video.h b/include/SDL_video.h
index f3a9619..30c7393 100644
--- a/include/SDL_video.h
+++ b/include/SDL_video.h
@@ -193,7 +193,7 @@ typedef enum
 {
     SDL_GL_CONTEXT_PROFILE_CORE           = 0x0001,
     SDL_GL_CONTEXT_PROFILE_COMPATIBILITY  = 0x0002,
-    SDL_GL_CONTEXT_PROFILE_ES             = 0x0004
+    SDL_GL_CONTEXT_PROFILE_ES             = 0x0004 /* GLX_CONTEXT_ES2_PROFILE_BIT_EXT */
 } SDL_GLprofile;
 
 typedef enum
diff --git a/src/core/android/SDL_android.c b/src/core/android/SDL_android.c
index b7d1ff6..7606859 100644
--- a/src/core/android/SDL_android.c
+++ b/src/core/android/SDL_android.c
@@ -33,6 +33,7 @@
 #include "../../video/android/SDL_androidkeyboard.h"
 #include "../../video/android/SDL_androidtouch.h"
 #include "../../video/android/SDL_androidvideo.h"
+#include "../../video/android/SDL_androidwindow.h"
 
 #include <android/log.h>
 #include <pthread.h>
@@ -67,8 +68,7 @@ static JavaVM* mJavaVM;
 static jclass mActivityClass;
 
 // method signatures
-static jmethodID midCreateGLContext;
-static jmethodID midDeleteGLContext;
+static jmethodID midGetNativeSurface;
 static jmethodID midFlipBuffers;
 static jmethodID midAudioInit;
 static jmethodID midAudioWriteShortBuffer;
@@ -116,10 +116,8 @@ void SDL_Android_Init(JNIEnv* mEnv, jclass cls)
 
     mActivityClass = (jclass)((*mEnv)->NewGlobalRef(mEnv, cls));
 
-    midCreateGLContext = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
-                                "createGLContext","(II[I)Z");
-    midDeleteGLContext = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
-                                "deleteGLContext","()V");
+    midGetNativeSurface = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
+                                "getNativeSurface","()Landroid/view/Surface;");
     midFlipBuffers = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
                                 "flipBuffers","()V");
     midAudioInit = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
@@ -133,7 +131,7 @@ void SDL_Android_Init(JNIEnv* mEnv, jclass cls)
 
     bHasNewData = false;
 
-    if(!midCreateGLContext || !midFlipBuffers || !midAudioInit ||
+    if(!midGetNativeSurface || !midFlipBuffers || !midAudioInit ||
        !midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit) {
         __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL: Couldn't locate Java callbacks, check that they're named and typed correctly");
     }
@@ -148,6 +146,65 @@ void Java_org_libsdl_app_SDLActivity_onNativeResize(
     Android_SetScreenResolution(width, height, format);
 }
 
+
+// Surface Created
+void Java_org_libsdl_app_SDLActivity_onNativeSurfaceChanged(JNIEnv* env, jclass jcls)
+{
+    SDL_WindowData *data;
+    SDL_VideoDevice *_this;
+
+    if (Android_Window == NULL || Android_Window->driverdata == NULL ) {
+        return;
+    }
+    
+    _this =  SDL_GetVideoDevice();
+    data =  (SDL_WindowData *) Android_Window->driverdata;
+    
+    /* If the surface has been previously destroyed by onNativeSurfaceDestroyed, recreate it here */
+    if (data->egl_surface == EGL_NO_SURFACE) {
+        if(data->native_window) {
+            ANativeWindow_release(data->native_window);
+        }
+        data->native_window = Android_JNI_GetNativeWindow();
+        data->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) data->native_window);
+    }
+    
+    /* GL Context handling is done in the event loop because this function is run from the Java thread */
+    
+}
+
+// Surface Destroyed
+void Java_org_libsdl_app_SDLActivity_onNativeSurfaceDestroyed(JNIEnv* env, jclass jcls)
+{
+    /* We have to clear the current context and destroy the egl surface here
+     * Otherwise there's BAD_NATIVE_WINDOW errors coming from eglCreateWindowSurface on resume
+     * Ref: http://stackoverflow.com/questions/8762589/eglcreatewindowsurface-on-ics-and-switching-from-2d-to-3d
+     */
+    SDL_WindowData *data;
+    SDL_VideoDevice *_this;
+    
+    if (Android_Window == NULL || Android_Window->driverdata == NULL ) {
+        return;
+    }
+    
+    _this =  SDL_GetVideoDevice();
+    data = (SDL_WindowData *) Android_Window->driverdata;
+    
+    if (data->egl_surface != EGL_NO_SURFACE) {
+        SDL_EGL_MakeCurrent(_this, NULL, NULL);
+        SDL_EGL_DestroySurface(_this, data->egl_surface);
+        data->egl_surface = EGL_NO_SURFACE;
+    }
+    
+    /* GL Context handling is done in the event loop because this function is run from the Java thread */
+
+}
+
+void Java_org_libsdl_app_SDLActivity_nativeFlipBuffers(JNIEnv* env, jclass jcls)
+{
+    SDL_GL_SwapWindow(Android_Window);
+}
+
 // Keydown
 void Java_org_libsdl_app_SDLActivity_onNativeKeyDown(
                                     JNIEnv* env, jclass jcls, jint keycode)
@@ -317,47 +374,17 @@ static SDL_bool LocalReferenceHolder_IsActive()
     return s_active > 0;    
 }
 
-
-SDL_bool Android_JNI_CreateContext(int majorVersion, int minorVersion,
-                                int red, int green, int blue, int alpha,
-                                int buffer, int depth, int stencil,
-                                int buffers, int samples)
+ANativeWindow* Android_JNI_GetNativeWindow(void)
 {
+    ANativeWindow* anw;
+    jobject s;
     JNIEnv *env = Android_JNI_GetEnv();
 
-    jint attribs[] = {
-        EGL_RED_SIZE, red,
-        EGL_GREEN_SIZE, green,
-        EGL_BLUE_SIZE, blue,
-        EGL_ALPHA_SIZE, alpha,
-        EGL_BUFFER_SIZE, buffer,
-        EGL_DEPTH_SIZE, depth,
-        EGL_STENCIL_SIZE, stencil,
-        EGL_SAMPLE_BUFFERS, buffers,
-        EGL_SAMPLES, samples,
-        EGL_RENDERABLE_TYPE, (majorVersion == 1 ? EGL_OPENGL_ES_BIT : EGL_OPENGL_ES2_BIT),
-        EGL_NONE
-    };
-    int len = SDL_arraysize(attribs);
-
-    jintArray array;
-
-    array = (*env)->NewIntArray(env, len);
-    (*env)->SetIntArrayRegion(env, array, 0, len, attribs);
-
-    jboolean success = (*env)->CallStaticBooleanMethod(env, mActivityClass, midCreateGLContext, majorVersion, minorVersion, array);
-
-    (*env)->DeleteLocalRef(env, array);
-
-    return success ? SDL_TRUE : SDL_FALSE;
-}
-
-SDL_bool Android_JNI_DeleteContext(void)
-{
-    /* There's only one context, so no parameter for now */
-    JNIEnv *env = Android_JNI_GetEnv();
-    (*env)->CallStaticVoidMethod(env, mActivityClass, midDeleteGLContext);
-    return SDL_TRUE;
+    s = (*env)->CallStaticObjectMethod(env, mActivityClass, midGetNativeSurface);
+    anw = ANativeWindow_fromSurface(env, s);
+    (*env)->DeleteLocalRef(env, s);
+  
+    return anw;
 }
 
 void Android_JNI_SwapWindow()
diff --git a/src/core/android/SDL_android.h b/src/core/android/SDL_android.h
index 973f588..e95fd46 100644
--- a/src/core/android/SDL_android.h
+++ b/src/core/android/SDL_android.h
@@ -27,16 +27,20 @@ extern "C" {
 /* *INDENT-ON* */
 #endif
 
+#include <EGL/eglplatform.h>
+#include <android/native_window_jni.h>
+
 #include "SDL_rect.h"
 
 /* Interface from the SDL library into the Android Java activity */
-extern SDL_bool Android_JNI_CreateContext(int majorVersion, int minorVersion, int red, int green, int blue, int alpha, int buffer, int depth, int stencil, int buffers, int samples);
-extern SDL_bool Android_JNI_DeleteContext(void);
+/*extern SDL_bool Android_JNI_CreateContext(int majorVersion, int minorVersion, int red, int green, int blue, int alpha, int buffer, int depth, int stencil, int buffers, int samples);
+extern SDL_bool Android_JNI_DeleteContext(void);*/
 extern void Android_JNI_SwapWindow();
 extern void Android_JNI_SetActivityTitle(const char *title);
 extern SDL_bool Android_JNI_GetAccelerometerValues(float values[3]);
 extern void Android_JNI_ShowTextInput(SDL_Rect *inputRect);
 extern void Android_JNI_HideTextInput();
+extern ANativeWindow* Android_JNI_GetNativeWindow(void);
 
 /* Audio support */
 extern int Android_JNI_OpenAudioDevice(int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames);
diff --git a/src/video/SDL_egl.c b/src/video/SDL_egl.c
new file mode 100644
index 0000000..da29374
--- /dev/null
+++ b/src/video/SDL_egl.c
@@ -0,0 +1,407 @@
+/*
+ *  Simple DirectMedia Layer
+ *  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
+ * 
+ *  This software is provided 'as-is', without any express or implied
+ *  warranty.  In no event will the authors be held liable for any damages
+ *  arising from the use of this software.
+ * 
+ *  Permission is granted to anyone to use this software for any purpose,
+ *  including commercial applications, and to alter it and redistribute it
+ *  freely, subject to the following restrictions:
+ * 
+ *  1. The origin of this software must not be misrepresented; you must not
+ *     claim that you wrote the original software. If you use this software
+ *     in a product, an acknowledgment in the product documentation would be
+ *     appreciated but is not required.
+ *  2. Altered source versions must be plainly marked as such, and must not be
+ *     misrepresented as being the original software.
+ *  3. This notice may not be removed or altered from any source distribution.
+ */
+#include "SDL_config.h"
+
+#if SDL_VIDEO_OPENGL_EGL
+
+#include "SDL_sysvideo.h"
+#include "SDL_egl.h"
+
+#define DEFAULT_EGL "libEGL.so"
+#define DEFAULT_OGL_ES2 "libGLESv2.so"
+#define DEFAULT_OGL_ES_PVR "libGLES_CM.so"
+#define DEFAULT_OGL_ES "libGLESv1_CM.so"
+
+#define LOAD_FUNC(NAME) \
+*((void**)&_this->egl_data->NAME) = dlsym(handle, #NAME); \
+if (!_this->egl_data->NAME) \
+{ \
+    return SDL_SetError("Could not retrieve EGL function " #NAME); \
+}
+    
+/* EGL implementation of SDL OpenGL ES support */
+
+void *
+SDL_EGL_GetProcAddress(_THIS, const char *proc)
+{
+    static char procname[1024];
+    void *handle;
+    void *retval;
+    
+    /* eglGetProcAddress is busted on Android http://code.google.com/p/android/issues/detail?id=7681 */
+#if !defined(SDL_VIDEO_DRIVER_ANDROID)
+    handle = _this->egl_data->egl_dll_handle;
+    if (_this->egl_data->eglGetProcAddress) {
+        retval = _this->egl_data->eglGetProcAddress(proc);
+        if (retval) {
+            return retval;
+        }
+    }
+#endif
+    
+    handle = _this->gl_config.dll_handle;
+    #if defined(__OpenBSD__) && !defined(__ELF__)
+    #undef dlsym(x,y);
+    #endif
+    retval = dlsym(handle, proc);
+    if (!retval && strlen(proc) <= 1022) {
+        procname[0] = '_';
+        strcpy(procname + 1, proc);
+        retval = dlsym(handle, procname);
+    }
+    return retval;
+}
+
+void
+SDL_EGL_UnloadLibrary(_THIS)
+{
+    if ((_this->egl_data) && (_this->gl_config.driver_loaded)) {
+        _this->egl_data->eglTerminate(_this->egl_data->egl_display);
+        
+        dlclose(_this->gl_config.dll_handle);
+        dlclose(_this->egl_data->egl_dll_handle);
+        
+        SDL_free(_this->egl_data);
+        _this->egl_data = NULL;
+        
+        _this->gl_config.dll_handle = NULL;
+        _this->gl_config.driver_loaded = 0;
+    }
+}
+
+int
+SDL_EGL_LoadLibrary(_THIS, const char *path, NativeDisplayType native_display)
+{
+    void *handle;
+    int dlopen_flags;
+    
+    if (_this->egl_data) {
+        return SDL_SetError("OpenGL ES context already created");
+    }
+    
+    /* Unload the old driver and reset the pointers */
+    SDL_EGL_UnloadLibrary(_this);
+    
+    #ifdef RTLD_GLOBAL
+    dlopen_flags = RTLD_LAZY | RTLD_GLOBAL;
+    #else
+    dlopen_flags = RTLD_LAZY;
+    #endif
+    handle = dlopen(path, dlopen_flags);
+    /* Catch the case where the application isn't linked with EGL */
+    if ((dlsym(handle, "eglChooseConfig") == NULL) && (path == NULL)) {
+        
+        dlclose(handle);
+        path = getenv("SDL_VIDEO_EGL_DRIVER");
+        if (path == NULL) {
+            path = DEFAULT_EGL;
+        }
+        handle = dlopen(path, dlopen_flags);
+    }
+
+    if (handle == NULL) {
+        return SDL_SetError("Could not load OpenGL ES/EGL library");
+    }
+    
+    _this->egl_data = (struct SDL_EGL_VideoData *) SDL_calloc(1, sizeof(SDL_EGL_VideoData));
+    if (!_this->egl_data) {
+        return SDL_OutOfMemory();
+    }
+    
+    /* Load new function pointers */
+    LOAD_FUNC(eglGetDisplay);
+    LOAD_FUNC(eglInitialize);
+    LOAD_FUNC(eglTerminate);
+    LOAD_FUNC(eglGetProcAddress);
+    LOAD_FUNC(eglChooseConfig);
+    LOAD_FUNC(eglGetConfigAttrib);
+    LOAD_FUNC(eglCreateContext);
+    LOAD_FUNC(eglDestroyContext);
+    LOAD_FUNC(eglCreateWindowSurface);
+    LOAD_FUNC(eglDestroySurface);
+    LOAD_FUNC(eglMakeCurrent);
+    LOAD_FUNC(eglSwapBuffers);
+    LOAD_FUNC(eglSwapInterval);
+    LOAD_FUNC(eglWaitNative);
+    LOAD_FUNC(eglWaitGL);
+    
+    _this->egl_data->egl_display = _this->egl_data->eglGetDisplay(native_display);
+    
+    if (!_this->egl_data->egl_display) {
+        return SDL_SetError("Could not get EGL display");
+    }
+    
+    if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE) {
+        return SDL_SetError("Could not initialize EGL");
+    }
+
+    _this->egl_data->egl_dll_handle = handle;
+
+    path = getenv("SDL_VIDEO_GL_DRIVER");
+    handle = dlopen(path, dlopen_flags);
+    if ((path == NULL) | (handle == NULL)) {
+      if (_this->gl_config.major_version > 1) {
+          path = DEFAULT_OGL_ES2;
+          handle = dlopen(path, dlopen_flags);
+      } else {
+          path = DEFAULT_OGL_ES;
+          handle = dlopen(path, dlopen_flags);
+          if (handle == NULL) {
+              path = DEFAULT_OGL_ES_PVR;
+              handle = dlopen(path, dlopen_flags);
+          }
+      }
+    }
+
+    if (handle == NULL) {
+      return SDL_SetError("Could not initialize OpenGL ES library");
+    }
+
+    _this->gl_config.dll_handle = handle;
+    _this->gl_config.driver_loaded = 1;
+
+    if (path) {
+      strncpy(_this->gl_config.driver_path, path,
+              sizeof(_this->gl_config.driver_path) - 1);
+    } else {
+      strcpy(_this->gl_config.driver_path, "");
+    }
+    
+    /* We need to select a config here to satisfy some video backends such as X11 */
+    SDL_EGL_ChooseConfig(_this);
+    
+    return 0;
+}
+
+int
+SDL_EGL_ChooseConfig(_THIS) 
+{
+    /* 64 seems nice. */
+    EGLint attribs[64];
+    EGLint found_configs = 0;
+    int i;
+    
+    if (!_this->egl_data) {
+        /* The EGL library wasn't loaded, SDL_GetError() should have info */
+        return -1;
+    }
+  
+    /* Get a valid EGL configuration */
+    i = 0;
+    attribs[i++] = EGL_RED_SIZE;
+    attribs[i++] = _this->gl_config.red_size;
+    attribs[i++] = EGL_GREEN_SIZE;
+    attribs[i++] = _this->gl_config.green_size;
+    attribs[i++] = EGL_BLUE_SIZE;
+    attribs[i++] = _this->gl_config.blue_size;
+    
+    if (_this->gl_config.alpha_size) {
+        attribs[i++] = EGL_ALPHA_SIZE;
+        attribs[i++] = _this->gl_config.alpha_size;
+    }
+    
+    if (_this->gl_config.buffer_size) {
+        attribs[i++] = EGL_BUFFER_SIZE;
+        attribs[i++] = _this->gl_config.buffer_size;
+    }
+    
+    attribs[i++] = EGL_DEPTH_SIZE;
+    attribs[i++] = _this->gl_config.depth_size;
+    
+    if (_this->gl_config.stencil_size) {
+        attribs[i++] = EGL_STENCIL_SIZE;
+        attribs[i++] = _this->gl_config.stencil_size;
+    }
+    
+    if (_this->gl_config.multisamplebuffers) {
+        attribs[i++] = EGL_SAMPLE_BUFFERS;
+        attribs[i++] = _this->gl_config.multisamplebuffers;
+    }
+    
+    if (_this->gl_config.multisamplesamples) {
+        attribs[i++] = EGL_SAMPLES;
+        attribs[i++] = _this->gl_config.multisamplesamples;
+    }
+    
+    attribs[i++] = EGL_RENDERABLE_TYPE;
+    if (_this->gl_config.major_version == 2) {
+        attribs[i++] = EGL_OPENGL_ES2_BIT;
+    } else {
+        attribs[i++] = EGL_OPENGL_ES_BIT;
+    }
+    
+    attribs[i++] = EGL_NONE;
+    
+    if (_this->egl_data->eglChooseConfig(_this->egl_data->egl_display,
+        attribs,
+        &_this->egl_data->egl_config, 1,
+        &found_configs) == EGL_FALSE ||
+        found_configs == 0) {
+        return SDL_SetError("Couldn't find matching EGL config");
+    }
+    
+    return 0;
+}
+
+SDL_GLContext
+SDL_EGL_CreateContext(_THIS, EGLSurface egl_surface)
+{
+    EGLint context_attrib_list[] = {
+        EGL_CONTEXT_CLIENT_VERSION,
+        1,
+        EGL_NONE
+    };
+    
+    EGLContext egl_context;
+    
+    if (!_this->egl_data) {
+        /* The EGL library wasn't loaded, SDL_GetError() should have info */
+        return NULL;
+    }
+    
+    if (_this->gl_config.major_version) {
+        context_attrib_list[1] = _this->gl_config.major_version;
+    }
+
+    egl_context =
+    _this->egl_data->eglCreateContext(_this->egl_data->egl_display,
+                                      _this->egl_data->egl_config,
+                                      EGL_NO_CONTEXT, context_attrib_list);
+    
+    if (egl_context == EGL_NO_CONTEXT) {
+        SDL_SetError("Could not create EGL context");
+        return NULL;
+    }
+    
+    _this->egl_data->egl_swapinterval = 0;
+    
+    if (SDL_EGL_MakeCurrent(_this, egl_surface, egl_context) < 0) {
+        SDL_EGL_DeleteContext(_this, egl_context);
+        SDL_SetError("Could not make EGL context current");
+        return NULL;
+    }
+  
+    return (SDL_GLContext) egl_context;
+}
+
+int
+SDL_EGL_MakeCurrent(_THIS, EGLSurface egl_surface, SDL_GLContext context)
+{
+    if (!_this->egl_data) {
+        return SDL_SetError("OpenGL not initialized");
+    }
+    
+    EGLContext egl_context = (EGLContext) context;
+    
+    /* The android emulator crashes badly if you try to eglMakeCurrent 
+     * with a valid context and invalid surface, so we have to check for both here.
+     */
+    if (!egl_context || !egl_surface) {
+         _this->egl_data->eglMakeCurrent(_this->egl_data->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
+    }
+    else {
+        if (!_this->egl_data->eglMakeCurrent(_this->egl_data->egl_display,
+            egl_surface, egl_surface, egl_context)) {
+            return SDL_SetError("Unable to make EGL context current");
+        }
+    }
+      
+    return 0;
+}
+
+int
+SDL_EGL_SetSwapInterval(_THIS, int interval)
+{
+    if (_this->egl_data) {
+        return SDL_SetError("OpenGL ES context not active");
+    }
+    
+    EGLBoolean status;
+    status = _this->egl_data->eglSwapInterval(_this->egl_data->egl_display, interval);
+    if (status == EGL_TRUE) {
+        _this->egl_data->egl_swapinterval = interval;
+        return 0;
+    }
+    
+    return SDL_SetError("Unable to set the EGL swap interval");
+}
+
+int
+SDL_EGL_GetSwapInterval(_THIS)
+{
+    if (_this->egl_data) {
+        return SDL_SetError("OpenGL ES context not active");
+    }
+    
+    return _this->egl_data->egl_swapinterval;
+}
+
+void
+SDL_EGL_SwapBuffers(_THIS, EGLSurface egl_surface)
+{
+    _this->egl_data->eglSwapBuffers(_this->egl_data->egl_display, egl_surface);
+}
+
+void
+SDL_EGL_DeleteContext(_THIS, SDL_GLContext context)
+{
+    /* Clean up GLES and EGL */
+    if (!_this->egl_data) {
+        return;
+    }
+    
+    EGLContext egl_context = (EGLContext) context;
+    
+    if (!egl_context && egl_context != EGL_NO_CONTEXT) {
+        SDL_EGL_MakeCurrent(_this, NULL, NULL);
+        _this->egl_data->eglDestroyContext(_this->egl_data->egl_display, egl_context);
+    }
+        
+    /* FIXME: This "crappy fix" comes from the X11 code, 
+     * it's required so you can create a GLX context, destroy it and create a EGL one */
+    SDL_EGL_UnloadLibrary(_this);
+}
+
+EGLSurface *
+SDL_EGL_CreateSurface(_THIS, NativeWindowType nw) 
+{
+    return _this->egl_data->eglCreateWindowSurface(
+            _this->egl_data->egl_display,
+            _this->egl_data->egl_config,
+            nw, NULL);
+}
+
+void
+SDL_EGL_DestroySurface(_THIS, EGLSurface egl_surface) 
+{
+    if (!_this->egl_data) {
+        return;
+    }
+    
+    if (egl_surface != EGL_NO_SURFACE) {
+        _this->egl_data->eglDestroySurface(_this->egl_data->egl_display, egl_surface);
+    }
+}
+
+#endif /* SDL_VIDEO_OPENGL_EGL */
+
+/* vi: set ts=4 sw=4 expandtab: */
+    
\ No newline at end of file
diff --git a/src/video/SDL_egl.h b/src/video/SDL_egl.h
new file mode 100644
index 0000000..d613905
--- /dev/null
+++ b/src/video/SDL_egl.h
@@ -0,0 +1,132 @@
+/*
+  Simple DirectMedia Layer
+  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
+
+  This software is provided 'as-is', without any express or implied
+  warranty.  In no event will the authors be held liable for any damages
+  arising from the use of this software.
+
+  Permission is granted to anyone to use this software for any purpose,
+  including commercial applications, and to alter it and redistribute it
+  freely, subject to the following restrictions:
+
+  1. The origin of this software must not be misrepresented; you must not
+     claim that you wrote the original software. If you use this software
+     in a product, an acknowledgment in the product documentation would be
+     appreciated but is not required.
+  2. Altered source versions must be plainly marked as such, and must not be
+     misrepresented as being the original software.
+  3. This notice may not be removed or altered from any source distribution.
+*/
+#include "SDL_config.h"
+
+#ifndef _SDL_egl_h
+#define _SDL_egl_h
+
+#if SDL_VIDEO_OPENGL_EGL
+
+#include <EGL/egl.h>
+
+#include <dlfcn.h>
+#if defined(__OpenBSD__) && !defined(__ELF__)
+#define dlsym(x,y) dlsym(x, "_" y)
+#endif
+
+#include "SDL_sysvideo.h"
+
+typedef struct SDL_EGL_VideoData
+{
+    void *egl_dll_handle;
+    EGLDisplay egl_display;
+    EGLConfig egl_config;
+    int egl_swapinterval;
+    
+    EGLDisplay(*eglGetDisplay) (NativeDisplayType display);
+    EGLBoolean(*eglInitialize) (EGLDisplay dpy, EGLint * major,
+                                EGLint * minor);
+    EGLBoolean(*eglTerminate) (EGLDisplay dpy);
+    
+    void *(*eglGetProcAddress) (const char * procName);
+    
+    EGLBoolean(*eglChooseConfig) (EGLDisplay dpy,
+                                  const EGLint * attrib_list,
+                                  EGLConfig * configs,
+                                  EGLint config_size, EGLint * num_config);
+    
+    EGLContext(*eglCreateContext) (EGLDisplay dpy,
+                                   EGLConfig config,
+                                   EGLContext share_list,
+                                   const EGLint * attrib_list);
+    
+    EGLBoolean(*eglDestroyContext) (EGLDisplay dpy, EGLContext ctx);
+    
+    EGLSurface(*eglCreateWindowSurface) (EGLDisplay dpy,
+                                         EGLConfig config,
+                                         NativeWindowType window,
+                                         const EGLint * attrib_list);
+    EGLBoolean(*eglDestroySurface) (EGLDisplay dpy, EGLSurface surface);
+    
+    EGLBoolean(*eglMakeCurrent) (EGLDisplay dpy, EGLSurface draw,
+                                 EGLSurface read, EGLContext ctx);
+    
+    EGLBoolean(*eglSwapBuffers) (EGLDisplay dpy, EGLSurface draw);
+    
+    EGLBoolean(*eglSwapInterval) (EGLDisplay dpy, EGLint interval);
+    
+    const char *(*eglQueryString) (EGLDisplay dpy, EGLint name);
+    
+    EGLBoolean(*eglGetConfigAttrib) (EGLDisplay dpy, EGLConfig config,
+                                     EGLint attribute, EGLint * value);
+    
+    EGLBoolean(*eglWaitNative) (EGLint  engine);
+
+    EGLBoolean(*eglWaitGL)(void);   
+} SDL_EGL_VideoData;
+
+/* OpenGLES functions */
+extern int SDL_EGL_GetAttribute(_THIS, SDL_GLattr attrib, int *value);
+extern int SDL_EGL_LoadLibrary(_THIS, const char *path, NativeDisplayType native_display);
+extern void *SDL_EGL_GetProcAddress(_THIS, const char *proc);
+extern void SDL_EGL_UnloadLibrary(_THIS);
+extern int SDL_EGL_ChooseConfig(_THIS);
+extern int SDL_EGL_SetSwapInterval(_THIS, int interval);
+extern int SDL_EGL_GetSwapInterval(_THIS);
+extern void SDL_EGL_DeleteContext(_THIS, SDL_GLContext context);
+extern EGLSurface *SDL_EGL_CreateSurface(_THIS, NativeWindowType nw);
+extern void SDL_EGL_DestroySurface(_THIS, EGLSurface egl_surface);
+
+/* These need to be wrapped to get the surface for the window by the platform GLES implementation */
+extern SDL_GLContext SDL_EGL_CreateContext(_THIS, EGLSurface egl_surface);
+extern int SDL_EGL_MakeCurrent(_THIS, EGLSurface egl_surface, SDL_GLContext context);
+extern void SDL_EGL_SwapBuffers(_THIS, EGLSurface egl_surface);
+
+/* A few of useful macros */
+
+#define SDL_EGL_SwapWindow_impl(BACKEND) void \
+BACKEND ## _GLES_SwapWindow(_THIS, SDL_Window * window) \
+{\
+    SDL_EGL_SwapBuffers(_this, ((SDL_WindowData *) window->driverdata)->egl_surface);\
+}
+
+#define SDL_EGL_MakeCurrent_impl(BACKEND) int \
+BACKEND ## _GLES_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context) \
+{\
+    if (window && context) { \
+        return SDL_EGL_MakeCurrent(_this, ((SDL_WindowData *) window->driverdata)->egl_surface, context); \
+    }\
+    else {\
+        return SDL_EGL_MakeCurrent(_this, NULL, NULL);\
+    }\
+}
+
+#define SDL_EGL_CreateContext_impl(BACKEND) SDL_GLContext \
+BACKEND ## _GLES_CreateContext(_THIS, SDL_Window * window) \
+{\
+    return SDL_EGL_CreateContext(_this, ((SDL_WindowData *) window->driverdata)->egl_surface);\
+}
+
+#endif /* SDL_VIDEO_OPENGL_EGL */
+
+#endif /* _SDL_egl_h */
+
+/* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/video/SDL_sysvideo.h b/src/video/SDL_sysvideo.h
index 4973648..084742f 100644
--- a/src/video/SDL_sysvideo.h
+++ b/src/video/SDL_sysvideo.h
@@ -313,7 +313,11 @@ struct SDL_VideoDevice
     /* Data private to this driver */
     void *driverdata;
     struct SDL_GLDriverData *gl_data;
-
+    
+#if SDL_VIDEO_OPENGL_EGL
+    struct SDL_EGL_VideoData *egl_data;
+#endif
+    
 #if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
     struct SDL_PrivateGLESData *gles_data;
 #endif
diff --git a/src/video/SDL_video.c b/src/video/SDL_video.c
index bab6c6c..b5c16e5 100644
--- a/src/video/SDL_video.c
+++ b/src/video/SDL_video.c
@@ -487,12 +487,17 @@ SDL_VideoInit(const char *driver_name)
 #elif SDL_VIDEO_OPENGL_ES
     _this->gl_config.major_version = 1;
     _this->gl_config.minor_version = 1;
+#if SDL_VIDEO_OPENGL_EGL    
     _this->gl_config.use_egl = 1;
+#endif    
 #elif SDL_VIDEO_OPENGL_ES2
     _this->gl_config.major_version = 2;
     _this->gl_config.minor_version = 0;
+#if SDL_VIDEO_OPENGL_EGL    
     _this->gl_config.use_egl = 1;
 #endif
+    
+#endif
     _this->gl_config.flags = 0;
     _this->gl_config.profile_mask = 0;
     _this->gl_config.share_with_current_context = 0;
diff --git a/src/video/android/SDL_androidevents.c b/src/video/android/SDL_androidevents.c
index add86df..d0a16e1 100644
--- a/src/video/android/SDL_androidevents.c
+++ b/src/video/android/SDL_androidevents.c
@@ -27,6 +27,32 @@
 
 #include "SDL_androidevents.h"
 #include "SDL_events.h"
+#include "SDL_androidwindow.h"
+
+void android_egl_context_backup();
+void android_egl_context_restore();
+
+void 
+android_egl_context_restore() 
+{
+    SDL_WindowData *data = (SDL_WindowData *) Android_Window->driverdata;
+    if (SDL_GL_MakeCurrent(Android_Window, (SDL_GLContext) data->egl_context) < 0) {
+        /* The context is no longer valid, create a new one */
+        /* FIXME: Notify the user that the context changed and textures need to be re created*/
+        data->egl_context = (EGLContext) SDL_GL_CreateContext(Android_Window);
+        SDL_GL_MakeCurrent(Android_Window, (SDL_GLContext) data->egl_context);
+    }
+}
+
+void 
+android_egl_context_backup() 
+{
+    /* Keep a copy of the EGL Context so we can try to restore it when we resume */
+    SDL_WindowData *data = (SDL_WindowData *) Android_Window->driverdata;
+    data->egl_context = SDL_GL_GetCurrentContext();
+    /* We need to do this so the EGLSurface can be freed */
+    SDL_GL_MakeCurrent(Android_Window, NULL);
+}
 
 void
 Android_PumpEvents(_THIS)
@@ -52,13 +78,9 @@ Android_PumpEvents(_THIS)
         if(SDL_SemTryWait(Android_ResumeSem) == 0) {
 #endif
             isPaused = 0;
-            /* TODO: Should we double check if we are on the same thread as the one that made the original GL context?
-             * This call will go through the following chain of calls in Java:
-             * SDLActivity::createGLContext -> SDLActivity:: initEGL -> SDLActivity::createEGLSurface -> SDLActivity::createEGLContext
-             * SDLActivity::createEGLContext will attempt to restore the GL context first, and if that fails it will create a new one
-             * If a new GL context is created, the user needs to restore the textures manually (TODO: notify the user that this happened with a message)
-             */
-            SDL_GL_CreateContext(Android_Window);
+            
+            /* Restore the GL Context from here, as this operation is thread dependent */
+            android_egl_context_restore();
         }
     }
     else {
@@ -70,13 +92,14 @@ Android_PumpEvents(_THIS)
                 isPausing = 1;
             }
             else {
+                android_egl_context_backup();
                 isPausing = 0;
                 isPaused = 1;
             }
         }
 #else
         if(SDL_SemTryWait(Android_PauseSem) == 0) {
-            /* If we fall in here, the system is/was paused */
+            android_egl_context_backup();
             isPaused = 1;
         }
 #endif
diff --git a/src/video/android/SDL_androidgl.c b/src/video/android/SDL_androidgl.c
index 6902bc1..1a3eb4c 100644
--- a/src/video/android/SDL_androidgl.c
+++ b/src/video/android/SDL_androidgl.c
@@ -25,6 +25,8 @@
 /* Android SDL video driver implementation */
 
 #include "SDL_video.h"
+#include "../SDL_egl.h"
+#include "SDL_androidwindow.h"
 
 #include "SDL_androidvideo.h"
 #include "../../core/android/SDL_android.h"
@@ -33,95 +35,20 @@
 
 #include <dlfcn.h>
 
-static void* Android_GLHandle = NULL;
+SDL_EGL_CreateContext_impl(Android)
+SDL_EGL_MakeCurrent_impl(Android)
 
-/* GL functions */
-int
-Android_GL_LoadLibrary(_THIS, const char *path)
-{
-    if (!Android_GLHandle) {
-        Android_GLHandle = dlopen("libGLESv1_CM.so",RTLD_GLOBAL);
-        if (!Android_GLHandle) {
-            return SDL_SetError("Could not initialize GL ES library\n");
-        }
-    }
-    return 0;
-}
-
-void *
-Android_GL_GetProcAddress(_THIS, const char *proc)
-{
-    /*
-       !!! FIXME: this _should_ use eglGetProcAddress(), but it appears to be
-       !!! FIXME:  busted on Android at the moment...
-       !!! FIXME:  http://code.google.com/p/android/issues/detail?id=7681
-       !!! FIXME: ...so revisit this later.  --ryan.
-    */
-    return dlsym(Android_GLHandle, proc);
-}
-
-void
-Android_GL_UnloadLibrary(_THIS)
+Android_GLES_SwapWindow(_THIS, SDL_Window * window)
 {
-    if(Android_GLHandle) {
-        dlclose(Android_GLHandle);
-        Android_GLHandle = NULL;
-    }
-}
-
-SDL_GLContext
-Android_GL_CreateContext(_THIS, SDL_Window * window)
-{
-    if (!Android_JNI_CreateContext(_this->gl_config.major_version,
-                                   _this->gl_config.minor_version,
-                                   _this->gl_config.red_size,
-                                   _this->gl_config.green_size,
-                                   _this->gl_config.blue_size,
-                                   _this->gl_config.alpha_size,
-                                   _this->gl_config.buffer_size,
-                                   _this->gl_config.depth_size,
-                                   _this->gl_config.stencil_size,
-                                   _this->gl_config.multisamplebuffers,
-                                   _this->gl_config.multisamplesamples)) {
-        SDL_SetError("Couldn't create OpenGL context - see Android log for details");
-        return NULL;
-    }
-    return (SDL_GLContext)1;
+    /* FIXME: These two functions were in the Java code, do we really need them? */
+    _this->egl_data->eglWaitNative(EGL_CORE_NATIVE_ENGINE);
+    _this->egl_data->eglWaitGL();
+    SDL_EGL_SwapBuffers(_this, ((SDL_WindowData *) window->driverdata)->egl_surface);
 }
 
 int
-Android_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
-{
-    /* There's only one context, nothing to do... */
-    return 0;
-}
-
-int
-Android_GL_SetSwapInterval(_THIS, int interval)
-{
-    __android_log_print(ANDROID_LOG_INFO, "SDL", "[STUB] GL_SetSwapInterval\n");
-    return 0;
-}
-
-int
-Android_GL_GetSwapInterval(_THIS)
-{
-    __android_log_print(ANDROID_LOG_INFO, "SDL", "[STUB] GL_GetSwapInterval\n");
-    return 0;
-}
-
-void
-Android_GL_SwapWindow(_THIS, SDL_Window * window)
-{
-    Android_JNI_SwapWindow();
-}
-
-void
-Android_GL_DeleteContext(_THIS, SDL_GLContext context)
-{
-    if (context) {
-        Android_JNI_DeleteContext();
-    }
+Android_GLES_LoadLibrary(_THIS, const char *path) {
+    return SDL_EGL_LoadLibrary(_this, path, (NativeDisplayType) 0);
 }
 
 #endif /* SDL_VIDEO_DRIVER_ANDROID */
diff --git a/src/video/android/SDL_androidvideo.c b/src/video/android/SDL_androidvideo.c
index 6dcb81e..0bf3a2c 100644
--- a/src/video/android/SDL_androidvideo.c
+++ b/src/video/android/SDL_androidvideo.c
@@ -44,17 +44,17 @@
 static int Android_VideoInit(_THIS);
 static void Android_VideoQuit(_THIS);
 
+#include "../SDL_egl.h"
 /* GL functions (SDL_androidgl.c) */
-extern int Android_GL_LoadLibrary(_THIS, const char *path);
-extern void *Android_GL_GetProcAddress(_THIS, const char *proc);
-extern void Android_GL_UnloadLibrary(_THIS);
-extern SDL_GLContext Android_GL_CreateContext(_THIS, SDL_Window * window);
-extern int Android_GL_MakeCurrent(_THIS, SDL_Window * window,
-                              SDL_GLContext context);
-extern int Android_GL_SetSwapInterval(_THIS, int interval);
-extern int Android_GL_GetSwapInterval(_THIS);
-extern void Android_GL_SwapWindow(_THIS, SDL_Window * window);
-extern void Android_GL_DeleteContext(_THIS, SDL_GLContext context);
+extern SDL_GLContext Android_GLES_CreateContext(_THIS, SDL_Window * window);
+extern int Android_GLES_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context);
+extern void Android_GLES_SwapWindow(_THIS, SDL_Window * window);
+extern int Android_GLES_LoadLibrary(_THIS, const char *path);
+#define Android_GLES_GetProcAddress SDL_EGL_GetProcAddress
+#define Android_GLES_UnloadLibrary SDL_EGL_UnloadLibrary
+#define Android_GLES_SetSwapInterval SDL_EGL_SetSwapInterval
+#define Android_GLES_GetSwapInterval SDL_EGL_GetSwapInterval
+#define Android_GLES_DeleteContext SDL_EGL_DeleteContext
 
 /* Android driver bootstrap functions */
 
@@ -114,15 +114,15 @@ Android_CreateDevice(int devindex)
     device->free = Android_DeleteDevice;
 
     /* GL pointers */
-    device->GL_LoadLibrary = Android_GL_LoadLibrary;
-    device->GL_GetProcAddress = Android_GL_GetProcAddress;
-    device->GL_UnloadLibrary = Android_GL_UnloadLibrary;
-    device->GL_CreateContext = Android_GL_CreateContext;
-    device->GL_MakeCurrent = Android_GL_MakeCurrent;
-    device->GL_SetSwapInterval = Android_GL_SetSwapInterval;
-    device->GL_GetSwapInterval = Android_GL_GetSwapInterval;
-    device->GL_SwapWindow = Android_GL_SwapWindow;
-    device->GL_DeleteContext = Android_GL_DeleteContext;
+    device->GL_LoadLibrary = Android_GLES_LoadLibrary;
+    device->GL_GetProcAddress = Android_GLES_GetProcAddress;
+    device->GL_UnloadLibrary = Android_GLES_UnloadLibrary;
+    device->GL_CreateContext = Android_GLES_CreateContext;
+    device->GL_MakeCurrent = Android_GLES_MakeCurrent;
+    device->GL_SetSwapInterval = Android_GLES_SetSwapInterval;
+    device->GL_GetSwapInterval = Android_GLES_GetSwapInterval;
+    device->GL_SwapWindow = Android_GLES_SwapWindow;
+    device->GL_DeleteContext = Android_GLES_DeleteContext;
 
     /* Text input */
     device->StartTextInput = Android_StartTextInput;
diff --git a/src/video/android/SDL_androidwindow.c b/src/video/android/SDL_androidwindow.c
index 70e244e..acc40db 100644
--- a/src/video/android/SDL_androidwindow.c
+++ b/src/video/android/SDL_androidwindow.c
@@ -29,15 +29,15 @@
 #include "SDL_androidvideo.h"
 #include "SDL_androidwindow.h"
 
-#include "../../core/android/SDL_android.h"
-
 int
 Android_CreateWindow(_THIS, SDL_Window * window)
 {
+    SDL_WindowData *data;
+    
     if (Android_Window) {
         return SDL_SetError("Android only supports one window");
     }
-    Android_Window = window;
+    
     Android_PauseSem = SDL_CreateSemaphore(0);
     Android_ResumeSem = SDL_CreateSemaphore(0);
 
@@ -56,7 +56,29 @@ Android_CreateWindow(_THIS, SDL_Window * window)
     /* One window, it always has focus */
     SDL_SetMouseFocus(window);
     SDL_SetKeyboardFocus(window);
+    
+    data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
+    if (!data) {
+        return SDL_OutOfMemory();
+    }
+    
+    data->native_window = Android_JNI_GetNativeWindow();
+    
+    if (!data->native_window) {
+        SDL_free(data);
+        return SDL_SetError("Could not fetch native window");
+    }
+    
+    data->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) data->native_window);
 
+    if (data->egl_surface == EGL_NO_SURFACE) {
+        SDL_free(data);
+        return SDL_SetError("Could not create GLES window surface");
+    }
+
+    window->driverdata = data;
+    Android_Window = window;
+    
     return 0;
 }
 
@@ -69,12 +91,23 @@ Android_SetWindowTitle(_THIS, SDL_Window * window)
 void
 Android_DestroyWindow(_THIS, SDL_Window * window)
 {
+    SDL_WindowData *data;
+    
     if (window == Android_Window) {
         Android_Window = NULL;
         if (Android_PauseSem) SDL_DestroySemaphore(Android_PauseSem);
         if (Android_ResumeSem) SDL_DestroySemaphore(Android_ResumeSem);
         Android_PauseSem = NULL;
         Android_ResumeSem = NULL;
+        
+        if(window->driverdata) {
+            data = (SDL_WindowData *) window->driverdata;
+            if(data->native_window) {
+                ANativeWindow_release(data->native_window);
+            }
+            SDL_free(window->driverdata);
+            window->driverdata = NULL;
+        }
     }
 }
 
diff --git a/src/video/android/SDL_androidwindow.h b/src/video/android/SDL_androidwindow.h
index c0ef93b..4365cd4 100644
--- a/src/video/android/SDL_androidwindow.h
+++ b/src/video/android/SDL_androidwindow.h
@@ -23,10 +23,21 @@
 #ifndef _SDL_androidwindow_h
 #define _SDL_androidwindow_h
 
+#include "../../core/android/SDL_android.h"
+#include "../SDL_egl.h"
+
 extern int Android_CreateWindow(_THIS, SDL_Window * window);
 extern void Android_SetWindowTitle(_THIS, SDL_Window * window);
 extern void Android_DestroyWindow(_THIS, SDL_Window * window);
 
+typedef struct
+{
+    EGLSurface egl_surface;
+    EGLContext egl_context; /* We use this to preserve the context when losing focus */
+    ANativeWindow* native_window;
+    
+} SDL_WindowData;
+
 #endif /* _SDL_androidwindow_h */
 
 /* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/video/x11/SDL_x11opengl.c b/src/video/x11/SDL_x11opengl.c
index 2eaed86..6ddc0de 100644
--- a/src/video/x11/SDL_x11opengl.c
+++ b/src/video/x11/SDL_x11opengl.c
@@ -140,25 +140,6 @@ X11_GL_LoadLibrary(_THIS, const char *path)
         return SDL_SetError("OpenGL context already created");
     }
 
-    /* If SDL_GL_CONTEXT_EGL has been changed to 1, switch over to X11_GLES functions  */
-    if (_this->gl_config.use_egl == 1) {
-#if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
-        _this->GL_LoadLibrary = X11_GLES_LoadLibrary;
-        _this->GL_GetProcAddress = X11_GLES_GetProcAddress;
-        _this->GL_UnloadLibrary = X11_GLES_UnloadLibrary;
-        _this->GL_CreateContext = X11_GLES_CreateContext;
-        _this->GL_MakeCurrent = X11_GLES_MakeCurrent;
-        _this->GL_SetSwapInterval = X11_GLES_SetSwapInterval;
-        _this->GL_GetSwapInterval = X11_GLES_GetSwapInterval;
-        _this->GL_SwapWindow = X11_GLES_SwapWindow;
-        _this->GL_DeleteContext = X11_GLES_DeleteContext;
-        return X11_GLES_LoadLibrary(_this, path);
-#else
-        return SDL_SetError("SDL not configured with OpenGL ES/EGL support");
-#endif
-    }
-
-
     /* Load the OpenGL library */
     if (path == NULL) {
         path = SDL_getenv("SDL_OPENGL_LIBRARY");
@@ -228,6 +209,38 @@ X11_GL_LoadLibrary(_THIS, const char *path)
 
     /* Initialize extensions */
     X11_GL_InitExtensions(_this);
+    
+    /* If SDL_GL_CONTEXT_EGL has been changed to 1, and there's 
+     * no GLX_EXT_create_context_es2_profile extension switch over to X11_GLES functions  */
+    if (_this->gl_config.use_egl == 1) {
+        if (_this->gl_data->HAS_GLX_EXT_create_context_es2_profile) {
+            /* We cheat a little bit here by using GLX instead of EGL 
+             * to improve our chances of getting hardware acceleration */
+            _this->gl_config.use_egl = 0;
+            _this->gl_config.profile_mask = SDL_GL_CONTEXT_PROFILE_ES;
+        } else {
+#if SDL_VIDEO_OPENGL_EGL
+            X11_GL_UnloadLibrary(_this);
+            /* Better avoid conflicts! */
+            if (_this->gl_config.dll_handle != NULL ) {
+                GL_UnloadObject(_this->gl_config.dll_handle);
+                _this->gl_config.dll_handle = NULL;
+            }
+            _this->GL_LoadLibrary = X11_GLES_LoadLibrary;
+            _this->GL_GetProcAddress = X11_GLES_GetProcAddress;
+            _this->GL_UnloadLibrary = X11_GLES_UnloadLibrary;
+            _this->GL_CreateContext = X11_GLES_CreateContext;
+            _this->GL_MakeCurrent = X11_GLES_MakeCurrent;
+            _this->GL_SetSwapInterval = X11_GLES_SetSwapInterval;
+            _this->GL_GetSwapInterval = X11_GLES_GetSwapInterval;
+            _this->GL_SwapWindow = X11_GLES_SwapWindow;
+            _this->GL_DeleteContext = X11_GLES_DeleteContext;
+            return X11_GLES_LoadLibrary(_this, NULL);
+#else
+            return SDL_SetError("SDL not configured with EGL support");
+#endif
+        }
+    }
 
     return 0;
 }
@@ -369,6 +382,11 @@ X11_GL_InitExtensions(_THIS)
     if (HasExtension("GLX_EXT_visual_info", extensions)) {
         _this->gl_data->HAS_GLX_EXT_visual_info = SDL_TRUE;
     }
+    
+    /* Check for GLX_EXT_create_context_es2_profile */
+    if (HasExtension("GLX_EXT_create_context_es2_profile", extensions)) {
+        _this->gl_data->HAS_GLX_EXT_create_context_es2_profile = SDL_TRUE;
+    }
 
     if (context) {
         _this->gl_data->glXMakeCurrent(display, None, NULL);
diff --git a/src/video/x11/SDL_x11opengl.h b/src/video/x11/SDL_x11opengl.h
index db1b315..27d7f9a 100644
--- a/src/video/x11/SDL_x11opengl.h
+++ b/src/video/x11/SDL_x11opengl.h
@@ -34,6 +34,7 @@ struct SDL_GLDriverData
     SDL_bool HAS_GLX_EXT_visual_rating;
     SDL_bool HAS_GLX_EXT_visual_info;
     SDL_bool HAS_GLX_EXT_swap_control_tear;
+    SDL_bool HAS_GLX_EXT_create_context_es2_profile;
 
     Bool (*glXQueryExtension) (Display*,int*,int*);
     void *(*glXGetProcAddress) (const GLubyte*);
diff --git a/src/video/x11/SDL_x11opengles.c b/src/video/x11/SDL_x11opengles.c
index ba72aff..2d2054a 100644
--- a/src/video/x11/SDL_x11opengles.c
+++ b/src/video/x11/SDL_x11opengles.c
@@ -20,86 +20,22 @@
 */
 #include "SDL_config.h"
 
-#if SDL_VIDEO_DRIVER_X11 && SDL_VIDEO_OPENGL_ES
+#if SDL_VIDEO_DRIVER_X11 && SDL_VIDEO_OPENGL_EGL
 
 #include "SDL_x11video.h"
 #include "SDL_x11opengles.h"
 #include "SDL_x11opengl.h"
 
-#define DEFAULT_EGL "libEGL.so"
-#define DEFAULT_OGL_ES2 "libGLESv2.so"
-#define DEFAULT_OGL_ES_PVR "libGLES_CM.so"
-#define DEFAULT_OGL_ES "libGLESv1_CM.so"
-
-#define LOAD_FUNC(NAME) \
-    *((void**)&_this->gles_data->NAME) = dlsym(handle, #NAME); \
-    if (!_this->gles_data->NAME) \
-    { \
-        return SDL_SetError("Could not retrieve EGL function " #NAME); \
-    }
-
-/* GLES implementation of SDL OpenGL support */
-
-void *
-X11_GLES_GetProcAddress(_THIS, const char *proc)
-{
-    static char procname[1024];
-    void *handle;
-    void *retval;
-
-    handle = _this->gles_data->egl_dll_handle;
-    if (_this->gles_data->eglGetProcAddress) {
-        retval = _this->gles_data->eglGetProcAddress(proc);
-        if (retval) {
-            return retval;
-        }
-    }
-
-    handle = _this->gl_config.dll_handle;
-#if defined(__OpenBSD__) && !defined(__ELF__)
-#undef dlsym(x,y);
-#endif
-    retval = dlsym(handle, proc);
-    if (!retval && strlen(proc) <= 1022) {
-        procname[0] = '_';
-        strcpy(procname + 1, proc);
-        retval = dlsym(handle, procname);
-    }
-    return retval;
-}
-
-void
-X11_GLES_UnloadLibrary(_THIS)
-{
-    if ((_this->gles_data) && (_this->gl_config.driver_loaded)) {
-        _this->gles_data->eglTerminate(_this->gles_data->egl_display);
-
-        dlclose(_this->gl_config.dll_handle);
-        dlclose(_this->gles_data->egl_dll_handle);
-
-        SDL_free(_this->gles_data);
-        _this->gles_data = NULL;
-
-        _this->gl_config.dll_handle = NULL;
-        _this->gl_config.driver_loaded = 0;
-    }
-}
+/* EGL implementation of SDL OpenGL support */
 
 int
-X11_GLES_LoadLibrary(_THIS, const char *path)
-{
-    void *handle;
-    int dlopen_flags;
-
+X11_GLES_LoadLibrary(_THIS, const char *path) {
+        
     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
 
-    if (_this->gles_data) {
-        return SDL_SetError("OpenGL ES context already created");
-    }
-
     /* If SDL_GL_CONTEXT_EGL has been changed to 0, switch over to X11_GL functions  */
     if (_this->gl_config.use_egl == 0) {
-#if SDL_VIDEO_OPENGL_GLX
+        #if SDL_VIDEO_OPENGL_GLX
         _this->GL_LoadLibrary = X11_GL_LoadLibrary;
         _this->GL_GetProcAddress = X11_GL_GetProcAddress;
         _this->GL_UnloadLibrary = X11_GL_UnloadLibrary;
@@ -110,331 +46,63 @@ X11_GLES_LoadLibrary(_THIS, const char *path)
         _this->GL_SwapWindow = X11_GL_SwapWindow;
         _this->GL_DeleteContext = X11_GL_DeleteContext;
         return X11_GL_LoadLibrary(_this, path);
-#else
+        #else
         return SDL_SetError("SDL not configured with OpenGL/GLX support");
-#endif
+        #endif
     }
-
-#ifdef RTLD_GLOBAL
-    dlopen_flags = RTLD_LAZY | RTLD_GLOBAL;
-#else
-    dlopen_flags = RTLD_LAZY;
-#endif
-    handle = dlopen(path, dlopen_flags);
-    /* Catch the case where the application isn't linked with EGL */
-    if ((dlsym(handle, "eglChooseConfig") == NULL) && (path == NULL)) {
-
-        dlclose(handle);
-        path = getenv("SDL_VIDEO_EGL_DRIVER");
-        if (path == NULL) {
-            path = DEFAULT_EGL;
-        }
-        handle = dlopen(path, dlopen_flags);
-    }
-
-    if (handle == NULL) {
-        return SDL_SetError("Could not load OpenGL ES/EGL library");
-    }
-
-    /* Unload the old driver and reset the pointers */
-    X11_GLES_UnloadLibrary(_this);
-
-    _this->gles_data = (struct SDL_PrivateGLESData *) SDL_calloc(1, sizeof(SDL_PrivateGLESData));
-    if (!_this->gles_data) {
-        return SDL_OutOfMemory();
-    }
-
-    /* Load new function pointers */
-    LOAD_FUNC(eglGetDisplay);
-    LOAD_FUNC(eglInitialize);
-    LOAD_FUNC(eglTerminate);
-    LOAD_FUNC(eglGetProcAddress);
-    LOAD_FUNC(eglChooseConfig);
-    LOAD_FUNC(eglGetConfigAttrib);
-    LOAD_FUNC(eglCreateContext);
-    LOAD_FUNC(eglDestroyContext);
-    LOAD_FUNC(eglCreateWindowSurface);
-    LOAD_FUNC(eglDestroySurface);
-    LOAD_FUNC(eglMakeCurrent);
-    LOAD_FUNC(eglSwapBuffers);
-    LOAD_FUNC(eglSwapInterval);
-
-    _this->gles_data->egl_display =
-        _this->gles_data->eglGetDisplay((NativeDisplayType) data->display);
-
-    if (!_this->gles_data->egl_display) {
-        return SDL_SetError("Could not get EGL display");
-    }
-
-    if (_this->gles_data->
-        eglInitialize(_this->gles_data->egl_display, NULL,
-                      NULL) != EGL_TRUE) {
-        return SDL_SetError("Could not initialize EGL");
-    }
-
-    _this->gles_data->egl_dll_handle = handle;
-
-    path = getenv("SDL_VIDEO_GL_DRIVER");
-    handle = dlopen(path, dlopen_flags);
-    if ((path == NULL) | (handle == NULL)) {
-        if (_this->gl_config.major_version > 1) {
-            path = DEFAULT_OGL_ES2;
-            handle = dlopen(path, dlopen_flags);
-        } else {
-            path = DEFAULT_OGL_ES;
-            handle = dlopen(path, dlopen_flags);
-            if (handle == NULL) {
-                path = DEFAULT_OGL_ES_PVR;
-                handle = dlopen(path, dlopen_flags);
-            }
-        }
-    }
-
-    if (handle == NULL) {
-        return SDL_SetError("Could not initialize OpenGL ES library");
-    }
-
-    _this->gl_config.dll_handle = handle;
-    _this->gl_config.driver_loaded = 1;
-
-    if (path) {
-        strncpy(_this->gl_config.driver_path, path,
-                sizeof(_this->gl_config.driver_path) - 1);
-    } else {
-        strcpy(_this->gl_config.driver_path, "");
-    }
-    return 0;
+    
+    return SDL_EGL_LoadLibrary(_this, path, (NativeDisplayType) data->display);
 }
 
 XVisualInfo *
 X11_GLES_GetVisual(_THIS, Display * display, int screen)
 {
-    /* 64 seems nice. */
-    EGLint attribs[64];
-    EGLint found_configs = 0;
+   
+    XVisualInfo *egl_visualinfo = NULL;
     EGLint visual_id;
-    int i;
+    XVisualInfo vi_in;
+    int out_count;
 
-    if (!_this->gles_data) {
+    if (!_this->egl_data) {
         /* The EGL library wasn't loaded, SDL_GetError() should have info */
         return NULL;
     }
 
-    i = 0;
-    attribs[i++] = EGL_RED_SIZE;
-    attribs[i++] = _this->gl_config.red_size;
-    attribs[i++] = EGL_GREEN_SIZE;
-    attribs[i++] = _this->gl_config.green_size;
-    attribs[i++] = EGL_BLUE_SIZE;
-    attribs[i++] = _this->gl_config.blue_size;
-
-    if (_this->gl_config.alpha_size) {
-        attribs[i++] = EGL_ALPHA_SIZE;
-        attribs[i++] = _this->gl_config.alpha_size;
-    }
-
-    if (_this->gl_config.buffer_size) {
-        attribs[i++] = EGL_BUFFER_SIZE;
-        attribs[i++] = _this->gl_config.buffer_size;
-    }
-
-    attribs[i++] = EGL_DEPTH_SIZE;
-    attribs[i++] = _this->gl_config.depth_size;
-
-    if (_this->gl_config.stencil_size) {
-        attribs[i++] = EGL_STENCIL_SIZE;
-        attribs[i++] = _this->gl_config.stencil_size;
-    }
-
-    if (_this->gl_config.multisamplebuffers) {
-        attribs[i++] = EGL_SAMPLE_BUFFERS;
-        attribs[i++] = _this->gl_config.multisamplebuffers;
-    }
-
-    if (_this->gl_config.multisamplesamples) {
-        attribs[i++] = EGL_SAMPLES;
-        attribs[i++] = _this->gl_config.multisamplesamples;
-    }
-
-    attribs[i++] = EGL_RENDERABLE_TYPE;
-    if (_this->gl_config.major_version == 2) {
-        attribs[i++] = EGL_OPENGL_ES2_BIT;
-    } else {
-        attribs[i++] = EGL_OPENGL_ES_BIT;
-    }
-
-    attribs[i++] = EGL_NONE;
-
-    if (_this->gles_data->eglChooseConfig(_this->gles_data->egl_display,
-                                          attribs,
-                                          &_this->gles_data->egl_config, 1,
-                                          &found_configs) == EGL_FALSE ||
-        found_configs == 0) {
-        SDL_SetError("Couldn't find matching EGL config");
-        return NULL;
-    }
-
-    if (_this->gles_data->eglGetConfigAttrib(_this->gles_data->egl_display,
-                                             _this->gles_data->egl_config,
-                                             EGL_NATIVE_VISUAL_ID,
-                                             &visual_id) ==
-        EGL_FALSE || !visual_id) {
+    if (_this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display,
+                                            _this->egl_data->egl_config,
+                                            EGL_NATIVE_VISUAL_ID,
+                                            &visual_id) == EGL_FALSE || !visual_id) {
         /* Use the default visual when all else fails */
-        XVisualInfo vi_in;
-        int out_count;
         vi_in.screen = screen;
-
-        _this->gles_data->egl_visualinfo = XGetVisualInfo(display,
-                                                          VisualScreenMask,
-                                                          &vi_in, &out_count);
+        egl_visualinfo = XGetVisualInfo(display,
+                                        VisualScreenMask,
+                                        &vi_in, &out_count);
     } else {
-        XVisualInfo vi_in;
-        int out_count;
-
         vi_in.screen = screen;
         vi_in.visualid = visual_id;
-        _this->gles_data->egl_visualinfo = XGetVisualInfo(display,
-                                                          VisualScreenMask |
-                                                          VisualIDMask,
-                                                          &vi_in, &out_count);
+        egl_visualinfo = XGetVisualInfo(display, VisualScreenMask | VisualIDMask, &vi_in, &out_count);
     }
 
-    return _this->gles_data->egl_visualinfo;
+    return egl_visualinfo;
 }
 
 SDL_GLContext
 X11_GLES_CreateContext(_THIS, SDL_Window * window)
 {
-    EGLint context_attrib_list[] = {
-        EGL_CONTEXT_CLIENT_VERSION,
-        1,
-        EGL_NONE
-    };
-
+    SDL_GLContext context;
     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
     Display *display = data->videodata->display;
-    SDL_GLContext context = (SDL_GLContext)1;
 
     XSync(display, False);
-
-    if (_this->gl_config.major_version) {
-        context_attrib_list[1] = _this->gl_config.major_version;
-    }
-
-    _this->gles_data->egl_context =
-        _this->gles_data->eglCreateContext(_this->gles_data->egl_display,
-                                           _this->gles_data->egl_config,
-                                           EGL_NO_CONTEXT, context_attrib_list);
+    context = SDL_EGL_CreateContext(_this, data->egl_surface);
     XSync(display, False);
 
-    if (_this->gles_data->egl_context == EGL_NO_CONTEXT) {
-        SDL_SetError("Could not create EGL context");
-        return NULL;
-    }
-
-    _this->gles_data->egl_swapinterval = 0;
-
-    if (X11_GLES_MakeCurrent(_this, window, context) < 0) {
-        X11_GLES_DeleteContext(_this, context);
-        return NULL;
-    }
-
     return context;
 }
 
-int
-X11_GLES_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
-{
-/*
-    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
-    Display *display = data->videodata->display;
-*/
-
-    if (!_this->gles_data) {
-        return SDL_SetError("OpenGL not initialized");
-    }
-
-    if (!_this->gles_data->eglMakeCurrent(_this->gles_data->egl_display,
-                                          _this->gles_data->egl_surface,
-                                          _this->gles_data->egl_surface,
-                                          _this->gles_data->egl_context)) {
-        return SDL_SetError("Unable to make EGL context current");
-    }
-
-/*
-    XSync(display, False);
-*/
-
-    return 1;
-}
-
-int
-X11_GLES_SetSwapInterval(_THIS, int interval)
-{
-    if (_this->gles_data) {
-        return SDL_SetError("OpenGL ES context not active");
-    }
-
-    EGLBoolean status;
-    status = _this->gles_data->eglSwapInterval(_this->gles_data->egl_display, interval);
-    if (status == EGL_TRUE) {
-        _this->gles_data->egl_swapinterval = interval;
-        return 0;
-    }
-
-    return SDL_SetError("Unable to set the EGL swap interval");
-}
-
-int
-X11_GLES_GetSwapInterval(_THIS)
-{
-    if (_this->gles_data) {
-        return SDL_SetError("OpenGL ES context not active");
-    }
-
-    return _this->gles_data->egl_swapinterval;
-}
-
-void
-X11_GLES_SwapWindow(_THIS, SDL_Window * window)
-{
-    _this->gles_data->eglSwapBuffers(_this->gles_data->egl_display,
-                                     _this->gles_data->egl_surface);
-}
-
-void
-X11_GLES_DeleteContext(_THIS, SDL_GLContext context)
-{
-    /* Clean up GLES and EGL */
-    if (!_this->gles_data) {
-        return;
-    }
-
-    if (_this->gles_data->egl_context != EGL_NO_CONTEXT ||
-        _this->gles_data->egl_surface != EGL_NO_SURFACE) {
-        _this->gles_data->eglMakeCurrent(_this->gles_data->egl_display,
-                                         EGL_NO_SURFACE, EGL_NO_SURFACE,
-                                         EGL_NO_CONTEXT);
-
-        if (_this->gles_data->egl_context != EGL_NO_CONTEXT) {
-            _this->gles_data->eglDestroyContext(_this->gles_data->egl_display,
-                                                _this->gles_data->
-                                                egl_context);
-            _this->gles_data->egl_context = EGL_NO_CONTEXT;
-        }
-
-        if (_this->gles_data->egl_surface != EGL_NO_SURFACE) {
-            _this->gles_data->eglDestroySurface(_this->gles_data->egl_display,
-                                                _this->gles_data->
-                                                egl_surface);
-            _this->gles_data->egl_surface = EGL_NO_SURFACE;
-        }
-    }
-
-    /* crappy fix */
-    X11_GLES_UnloadLibrary(_this);
-}
+SDL_EGL_SwapWindow_impl(X11)
+SDL_EGL_MakeCurrent_impl(X11)
 
-#endif /* SDL_VIDEO_DRIVER_X11 && SDL_VIDEO_OPENGL_ES */
+#endif /* SDL_VIDEO_DRIVER_X11 && SDL_VIDEO_OPENGL_EGL */
 
 /* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/video/x11/SDL_x11opengles.h b/src/video/x11/SDL_x11opengles.h
index fa1506c..978f91f 100644
--- a/src/video/x11/SDL_x11opengles.h
+++ b/src/video/x11/SDL_x11opengles.h
@@ -23,81 +23,30 @@
 #ifndef _SDL_x11opengles_h
 #define _SDL_x11opengles_h
 
-#if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
-#include <GLES/gl.h>
-#include <GLES/egl.h>
-#include <dlfcn.h>
-#if defined(__OpenBSD__) && !defined(__ELF__)
-#define dlsym(x,y) dlsym(x, "_" y)
-#endif
+#if SDL_VIDEO_OPENGL_EGL
 
 #include "../SDL_sysvideo.h"
+#include "../SDL_egl.h"
 
 typedef struct SDL_PrivateGLESData
 {
-    XVisualInfo *egl_visualinfo;
-    void *egl_dll_handle;
-    EGLDisplay egl_display;
-    EGLContext egl_context;     /* Current GLES context */
-    EGLSurface egl_surface;
-    EGLConfig egl_config;
-    int egl_swapinterval;
-
-      EGLDisplay(*eglGetDisplay) (NativeDisplayType display);
-      EGLBoolean(*eglInitialize) (EGLDisplay dpy, EGLint * major,
-                                  EGLint * minor);
-      EGLBoolean(*eglTerminate) (EGLDisplay dpy);
-
-    void *(*eglGetProcAddress) (const char * procName);
-
-      EGLBoolean(*eglChooseConfig) (EGLDisplay dpy,
-                                    const EGLint * attrib_list,
-                                    EGLConfig * configs,
-                                    EGLint config_size, EGLint * num_config);
-
-      EGLContext(*eglCreateContext) (EGLDisplay dpy,
-                                     EGLConfig config,
-                                     EGLContext share_list,
-                                     const EGLint * attrib_list);
-
-      EGLBoolean(*eglDestroyContext) (EGLDisplay dpy, EGLContext ctx);
-
-      EGLSurface(*eglCreateWindowSurface) (EGLDisplay dpy,
-                                           EGLConfig config,
-                                           NativeWindowType window,
-                                           const EGLint * attrib_list);
-      EGLBoolean(*eglDestroySurface) (EGLDisplay dpy, EGLSurface surface);
-
-      EGLBoolean(*eglMakeCurrent) (EGLDisplay dpy, EGLSurface draw,
-                                   EGLSurface read, EGLContext ctx);
-
-      EGLBoolean(*eglSwapBuffers) (EGLDisplay dpy, EGLSurface draw);
-
-      EGLBoolean(*eglSwapInterval) (EGLDisplay dpy, EGLint interval);
-
-    const char *(*eglQueryString) (EGLDisplay dpy, EGLint name);
-
-      EGLBoolean(*eglGetConfigAttrib) (EGLDisplay dpy, EGLConfig config,
-                                       EGLint attribute, EGLint * value);
-
 } SDL_PrivateGLESData;
 
 /* OpenGLES functions */
-extern SDL_GLContext X11_GLES_CreateContext(_THIS, SDL_Window * window);
-extern XVisualInfo *X11_GLES_GetVisual(_THIS, Display * display, int screen);
-extern int X11_GLES_MakeCurrent(_THIS, SDL_Window * window,
-                                SDL_GLContext context);
-extern int X11_GLES_GetAttribute(_THIS, SDL_GLattr attrib, int *value);
-extern int X11_GLES_LoadLibrary(_THIS, const char *path);
-extern void *X11_GLES_GetProcAddress(_THIS, const char *proc);
-extern void X11_GLES_UnloadLibrary(_THIS);
+#define X11_GLES_GetAttribute SDL_EGL_GetAttribute
+#define X11_GLES_GetProcAddress SDL_EGL_GetProcAddress
+#define X11_GLES_UnloadLibrary SDL_EGL_UnloadLibrary
+#define X11_GLES_SetSwapInterval SDL_EGL_SetSwapInterval
+#define X11_GLES_GetSwapInterval SDL_EGL_GetSwapInterval
+#define X11_GLES_DeleteContext SDL_EGL_DeleteContext
 
-extern int X11_GLES_SetSwapInterval(_THIS, int interval);
-extern int X11_GLES_GetSwapInterval(_THIS);
+extern int X11_GLES_LoadLibrary(_THIS, const char *path);
+extern XVisualInfo *X11_GLES_GetVisual(_THIS, Display * display, int screen);
+extern SDL_GLContext X11_GLES_CreateContext(_THIS, SDL_Window * window);
 extern void X11_GLES_SwapWindow(_THIS, SDL_Window * window);
-extern void X11_GLES_DeleteContext(_THIS, SDL_GLContext context);
+extern int X11_GLES_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context);
 
-#endif /* SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2 */
+#endif /* SDL_VIDEO_OPENGL_EGL */
 
 #endif /* _SDL_x11opengles_h */
 
diff --git a/src/video/x11/SDL_x11video.c b/src/video/x11/SDL_x11video.c
index 86597ec..83224bc 100644
--- a/src/video/x11/SDL_x11video.c
+++ b/src/video/x11/SDL_x11video.c
@@ -35,7 +35,7 @@
 #include "SDL_x11touch.h"
 #include "SDL_x11xinput2.h"
 
-#if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
+#if SDL_VIDEO_OPENGL_EGL
 #include "SDL_x11opengles.h"
 #endif
 
@@ -394,7 +394,7 @@ X11_CreateDevice(int devindex)
     device->GL_GetSwapInterval = X11_GL_GetSwapInterval;
     device->GL_SwapWindow = X11_GL_SwapWindow;
     device->GL_DeleteContext = X11_GL_DeleteContext;
-#elif SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
+#elif SDL_VIDEO_OPENGL_EGL
     device->GL_LoadLibrary = X11_GLES_LoadLibrary;
     device->GL_GetProcAddress = X11_GLES_GetProcAddress;
     device->GL_UnloadLibrary = X11_GLES_UnloadLibrary;
diff --git a/src/video/x11/SDL_x11window.c b/src/video/x11/SDL_x11window.c
index 02529e0..60acd70 100644
--- a/src/video/x11/SDL_x11window.c
+++ b/src/video/x11/SDL_x11window.c
@@ -34,7 +34,7 @@
 #include "SDL_x11shape.h"
 #include "SDL_x11xinput2.h"
 
-#if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
+#if SDL_VIDEO_OPENGL_EGL
 #include "SDL_x11opengles.h"
 #endif
 
@@ -363,11 +363,11 @@ X11_CreateWindow(_THIS, SDL_Window * window)
     Atom XdndAware, xdnd_version = 5;
     Uint32 fevent = 0;
 
-#if SDL_VIDEO_OPENGL_GLX || SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
+#if SDL_VIDEO_OPENGL_GLX || SDL_VIDEO_OPENGL_EGL
     if (window->flags & SDL_WINDOW_OPENGL) {
         XVisualInfo *vinfo;
 
-#if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
+#if SDL_VIDEO_OPENGL_EGL
         if (_this->gl_config.use_egl == 1) {
             vinfo = X11_GLES_GetVisual(_this, display, screen);
         } else
@@ -481,26 +481,6 @@ X11_CreateWindow(_THIS, SDL_Window * window)
     if (!w) {
         return SDL_SetError("Couldn't create window");
     }
-#if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
-    if ((window->flags & SDL_WINDOW_OPENGL) && (_this->gl_config.use_egl == 1)) {
-        if (!_this->gles_data) {
-            XDestroyWindow(display, w);
-            return -1;
-        }
-
-        /* Create the GLES window surface */
-        _this->gles_data->egl_surface =
-            _this->gles_data->eglCreateWindowSurface(_this->gles_data->
-                                                 egl_display,
-                                                 _this->gles_data->egl_config,
-                                                 (NativeWindowType) w, NULL);
-
-        if (_this->gles_data->egl_surface == EGL_NO_SURFACE) {
-            XDestroyWindow(display, w);
-            return SDL_SetError("Could not create GLES window surface");
-        }
-    }
-#endif
 
     SetWindowBordered(display, screen, w,
                       (window->flags & SDL_WINDOW_BORDERLESS) == 0);
@@ -568,6 +548,24 @@ X11_CreateWindow(_THIS, SDL_Window * window)
         return -1;
     }
 
+#if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
+    if ((window->flags & SDL_WINDOW_OPENGL) && (_this->gl_config.use_egl == 1)) {
+        if (!_this->egl_data) {
+            XDestroyWindow(display, w);
+            return -1;
+        }
+
+        /* Create the GLES window surface */
+        ((SDL_WindowData *) window->driverdata)->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) w);
+
+        if (((SDL_WindowData *) window->driverdata)->egl_surface == EGL_NO_SURFACE) {
+            XDestroyWindow(display, w);
+            return SDL_SetError("Could not create GLES window surface");
+        }
+    }
+#endif
+    
+
 #ifdef X_HAVE_UTF8_STRING
     if (SDL_X11_HAVE_UTF8) {
         pXGetICValues(((SDL_WindowData *) window->driverdata)->ic,
diff --git a/src/video/x11/SDL_x11window.h b/src/video/x11/SDL_x11window.h
index f53fd8a..b0eff5c 100644
--- a/src/video/x11/SDL_x11window.h
+++ b/src/video/x11/SDL_x11window.h
@@ -30,6 +30,10 @@
 #define PENDING_FOCUS_IN_TIME   200
 #define PENDING_FOCUS_OUT_TIME  200
 
+#if SDL_VIDEO_OPENGL_EGL   
+#include <EGL/egl.h>
+#endif
+
 typedef enum
 {
     PENDING_FOCUS_NONE,
@@ -59,6 +63,9 @@ typedef struct
     struct SDL_VideoData *videodata;
     Atom xdnd_req;
     Window xdnd_source;
+#if SDL_VIDEO_OPENGL_EGL  
+    EGLSurface egl_surface;
+#endif
 } SDL_WindowData;
 
 extern void X11_SetNetWMState(_THIS, Window xwindow, Uint32 flags);