summaryrefslogtreecommitdiffstats
path: root/gfx/gl/GLContext.cpp
blob: 93ec040f4c3ced78f5f26e2f13a3a22090a90233 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
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
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim: set ts=8 sts=4 et sw=4 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "GLContext.h"

#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <vector>
#ifdef MOZ_WIDGET_ANDROID
#include <sys/mman.h>
#endif

#include "GLBlitHelper.h"
#include "GLReadTexImageHelper.h"
#include "GLScreenBuffer.h"

#include "gfxEnv.h"
#include "gfxUtils.h"
#include "GLContextProvider.h"
#include "GLTextureImage.h"
#include "nsPrintfCString.h"
#include "nsThreadUtils.h"
#include "prenv.h"
#include "prlink.h"
#include "ScopedGLHelpers.h"
#include "SharedSurfaceGL.h"
#include "GfxTexturesReporter.h"
#include "TextureGarbageBin.h"
#include "gfx2DGlue.h"
#include "gfxPrefs.h"
#include "mozilla/IntegerPrintfMacros.h"
#include "mozilla/gfx/Logging.h"

#include "OGLShaderProgram.h" // for ShaderProgramType

#include "mozilla/DebugOnly.h"

#ifdef XP_MACOSX
#include <CoreServices/CoreServices.h>
#endif

#if defined(MOZ_WIDGET_COCOA)
#include "nsCocoaFeatures.h"
#endif

#ifdef MOZ_WIDGET_ANDROID
#include "AndroidBridge.h"
#endif

namespace mozilla {
namespace gl {

using namespace mozilla::gfx;
using namespace mozilla::layers;

#ifdef MOZ_GL_DEBUG
unsigned GLContext::sCurrentGLContextTLS = -1;
#endif

// If adding defines, don't forget to undefine symbols. See #undef block below.
#define CORE_SYMBOL(x) { (PRFuncPtr*) &mSymbols.f##x, { #x, nullptr } }
#define CORE_EXT_SYMBOL2(x,y,z) { (PRFuncPtr*) &mSymbols.f##x, { #x, #x #y, #x #z, nullptr } }
#define EXT_SYMBOL2(x,y,z) { (PRFuncPtr*) &mSymbols.f##x, { #x #y, #x #z, nullptr } }
#define EXT_SYMBOL3(x,y,z,w) { (PRFuncPtr*) &mSymbols.f##x, { #x #y, #x #z, #x #w, nullptr } }
#define END_SYMBOLS { nullptr, { nullptr } }

// should match the order of GLExtensions, and be null-terminated.
static const char* const sExtensionNames[] = {
    "NO_EXTENSION",
    "GL_AMD_compressed_ATC_texture",
    "GL_ANGLE_depth_texture",
    "GL_ANGLE_framebuffer_blit",
    "GL_ANGLE_framebuffer_multisample",
    "GL_ANGLE_instanced_arrays",
    "GL_ANGLE_texture_compression_dxt3",
    "GL_ANGLE_texture_compression_dxt5",
    "GL_ANGLE_timer_query",
    "GL_APPLE_client_storage",
    "GL_APPLE_framebuffer_multisample",
    "GL_APPLE_sync",
    "GL_APPLE_texture_range",
    "GL_APPLE_vertex_array_object",
    "GL_ARB_ES2_compatibility",
    "GL_ARB_ES3_compatibility",
    "GL_ARB_color_buffer_float",
    "GL_ARB_copy_buffer",
    "GL_ARB_depth_texture",
    "GL_ARB_draw_buffers",
    "GL_ARB_draw_instanced",
    "GL_ARB_framebuffer_object",
    "GL_ARB_framebuffer_sRGB",
    "GL_ARB_geometry_shader4",
    "GL_ARB_gpu_shader5",
    "GL_ARB_half_float_pixel",
    "GL_ARB_instanced_arrays",
    "GL_ARB_internalformat_query",
    "GL_ARB_invalidate_subdata",
    "GL_ARB_map_buffer_range",
    "GL_ARB_occlusion_query2",
    "GL_ARB_pixel_buffer_object",
    "GL_ARB_robustness",
    "GL_ARB_sampler_objects",
    "GL_ARB_seamless_cube_map",
    "GL_ARB_shader_texture_lod",
    "GL_ARB_sync",
    "GL_ARB_texture_compression",
    "GL_ARB_texture_float",
    "GL_ARB_texture_non_power_of_two",
    "GL_ARB_texture_rectangle",
    "GL_ARB_texture_rg",
    "GL_ARB_texture_storage",
    "GL_ARB_texture_swizzle",
    "GL_ARB_timer_query",
    "GL_ARB_transform_feedback2",
    "GL_ARB_uniform_buffer_object",
    "GL_ARB_vertex_array_object",
    "GL_EXT_bgra",
    "GL_EXT_blend_minmax",
    "GL_EXT_color_buffer_float",
    "GL_EXT_color_buffer_half_float",
    "GL_EXT_copy_texture",
    "GL_EXT_disjoint_timer_query",
    "GL_EXT_draw_buffers",
    "GL_EXT_draw_buffers2",
    "GL_EXT_draw_instanced",
    "GL_EXT_draw_range_elements",
    "GL_EXT_frag_depth",
    "GL_EXT_framebuffer_blit",
    "GL_EXT_framebuffer_multisample",
    "GL_EXT_framebuffer_object",
    "GL_EXT_framebuffer_sRGB",
    "GL_EXT_gpu_shader4",
    "GL_EXT_gpu_shader5",
    "GL_EXT_multisampled_render_to_texture",
    "GL_EXT_occlusion_query_boolean",
    "GL_EXT_packed_depth_stencil",
    "GL_EXT_read_format_bgra",
    "GL_EXT_robustness",
    "GL_EXT_sRGB",
    "GL_EXT_sRGB_write_control",
    "GL_EXT_shader_texture_lod",
    "GL_EXT_texture3D",
    "GL_EXT_texture_compression_dxt1",
    "GL_EXT_texture_compression_s3tc",
    "GL_EXT_texture_filter_anisotropic",
    "GL_EXT_texture_format_BGRA8888",
    "GL_EXT_texture_sRGB",
    "GL_EXT_texture_storage",
    "GL_EXT_timer_query",
    "GL_EXT_transform_feedback",
    "GL_EXT_unpack_subimage",
    "GL_IMG_read_format",
    "GL_IMG_texture_compression_pvrtc",
    "GL_IMG_texture_npot",
    "GL_KHR_debug",
    "GL_NV_draw_instanced",
    "GL_NV_fence",
    "GL_NV_framebuffer_blit",
    "GL_NV_geometry_program4",
    "GL_NV_gpu_shader5",
    "GL_NV_half_float",
    "GL_NV_instanced_arrays",
    "GL_NV_primitive_restart",
    "GL_NV_texture_barrier",
    "GL_NV_transform_feedback",
    "GL_NV_transform_feedback2",
    "GL_OES_EGL_image",
    "GL_OES_EGL_image_external",
    "GL_OES_EGL_sync",
    "GL_OES_compressed_ETC1_RGB8_texture",
    "GL_OES_depth24",
    "GL_OES_depth32",
    "GL_OES_depth_texture",
    "GL_OES_element_index_uint",
    "GL_OES_framebuffer_object",
    "GL_OES_packed_depth_stencil",
    "GL_OES_rgb8_rgba8",
    "GL_OES_standard_derivatives",
    "GL_OES_stencil8",
    "GL_OES_texture_3D",
    "GL_OES_texture_float",
    "GL_OES_texture_float_linear",
    "GL_OES_texture_half_float",
    "GL_OES_texture_half_float_linear",
    "GL_OES_texture_npot",
    "GL_OES_vertex_array_object"
};

static bool
ParseGLSLVersion(GLContext* gl, uint32_t* out_version)
{
    if (gl->fGetError() != LOCAL_GL_NO_ERROR) {
        MOZ_ASSERT(false, "An OpenGL error has been triggered before.");
        return false;
    }

    /**
     * OpenGL 2.x, 3.x, 4.x specifications:
     *  The VERSION and SHADING_LANGUAGE_VERSION strings are laid out as follows:
     *
     *    <version number><space><vendor-specific information>
     *
     *  The version number is either of the form major_number.minor_number or
     *  major_number.minor_number.release_number, where the numbers all have
     *  one or more digits.
     *
     * SHADING_LANGUAGE_VERSION is *almost* identical to VERSION. The
     * difference is that the minor version always has two digits and the
     * prefix has an additional 'GLSL ES'
     *
     *
     * OpenGL ES 2.0, 3.0 specifications:
     *  The VERSION string is laid out as follows:
     *
     *     "OpenGL ES N.M vendor-specific information"
     *
     *  The version number is either of the form major_number.minor_number or
     *  major_number.minor_number.release_number, where the numbers all have
     *  one or more digits.
     *
     *
     * Note:
     *  We don't care about release_number.
     */
    const char* versionString = (const char*) gl->fGetString(LOCAL_GL_SHADING_LANGUAGE_VERSION);

    if (gl->fGetError() != LOCAL_GL_NO_ERROR) {
        MOZ_ASSERT(false, "glGetString(GL_SHADING_LANGUAGE_VERSION) has generated an error");
        return false;
    }

    if (!versionString) {
        // This happens on the Android emulators. We'll just return 100
        *out_version = 100;
        return true;
    }

    const auto fnSkipPrefix = [&versionString](const char* prefix) {
        const auto len = strlen(prefix);
        if (strncmp(versionString, prefix, len) == 0)
            versionString += len;
    };

    const char kGLESVersionPrefix[] = "OpenGL ES GLSL ES";
    fnSkipPrefix(kGLESVersionPrefix);

    if (gl->WorkAroundDriverBugs()) {
        // Nexus 7 2013 (bug 1234441)
        const char kBadGLESVersionPrefix[] = "OpenGL ES GLSL";
        fnSkipPrefix(kBadGLESVersionPrefix);
    }

    const char* itr = versionString;
    char* end = nullptr;
    auto majorVersion = strtol(itr, &end, 10);

    if (!end) {
        MOZ_ASSERT(false, "Failed to parse the GL major version number.");
        return false;
    }

    if (*end != '.') {
        MOZ_ASSERT(false, "Failed to parse GL's major-minor version number separator.");
        return false;
    }

    // we skip the '.' between the major and the minor version
    itr = end + 1;
    end = nullptr;

    auto minorVersion = strtol(itr, &end, 10);
    if (!end) {
        MOZ_ASSERT(false, "Failed to parse GL's minor version number.");
        return false;
    }

    if (majorVersion <= 0 || majorVersion >= 100) {
        MOZ_ASSERT(false, "Invalid major version.");
        return false;
    }

    if (minorVersion < 0 || minorVersion >= 100) {
        MOZ_ASSERT(false, "Invalid minor version.");
        return false;
    }

    *out_version = (uint32_t) majorVersion * 100 + (uint32_t) minorVersion;
    return true;
}

static bool
ParseGLVersion(GLContext* gl, uint32_t* out_version)
{
    if (gl->fGetError() != LOCAL_GL_NO_ERROR) {
        MOZ_ASSERT(false, "An OpenGL error has been triggered before.");
        return false;
    }

    /**
     * B2G emulator bug work around: The emulator implements OpenGL ES 2.0 on
     * OpenGL 3.2. The bug is that GetIntegerv(LOCAL_GL_{MAJOR,MINOR}_VERSION)
     * returns OpenGL 3.2 instead of generating an error.
     */
    if (!gl->IsGLES()) {
        /**
         * OpenGL 3.1 and OpenGL ES 3.0 both introduce GL_{MAJOR,MINOR}_VERSION
         * with GetIntegerv. So we first try those constants even though we
         * might not have an OpenGL context supporting them, as this is a
         * better way than parsing GL_VERSION.
         */
        GLint majorVersion = 0;
        GLint minorVersion = 0;

        const bool ok = (gl->GetPotentialInteger(LOCAL_GL_MAJOR_VERSION,
                                                 &majorVersion) &&
                         gl->GetPotentialInteger(LOCAL_GL_MINOR_VERSION,
                                                 &minorVersion));

        // If it's not an OpenGL (ES) 3.0 context, we will have an error
        if (ok &&
            majorVersion > 0 &&
            minorVersion >= 0)
        {
            *out_version = majorVersion * 100 + minorVersion * 10;
            return true;
        }
    }

    /**
     * We were not able to use GL_{MAJOR,MINOR}_VERSION, so we parse
     * GL_VERSION.
     *
     *
     * OpenGL 2.x, 3.x, 4.x specifications:
     *  The VERSION and SHADING_LANGUAGE_VERSION strings are laid out as follows:
     *
     *    <version number><space><vendor-specific information>
     *
     *  The version number is either of the form major_number.minor_number or
     *  major_number.minor_number.release_number, where the numbers all have
     *  one or more digits.
     *
     *
     * OpenGL ES 2.0, 3.0 specifications:
     *  The VERSION string is laid out as follows:
     *
     *     "OpenGL ES N.M vendor-specific information"
     *
     *  The version number is either of the form major_number.minor_number or
     *  major_number.minor_number.release_number, where the numbers all have
     *  one or more digits.
     *
     *
     * Note:
     *  We don't care about release_number.
     */
    const char* versionString = (const char*)gl->fGetString(LOCAL_GL_VERSION);

    if (gl->fGetError() != LOCAL_GL_NO_ERROR) {
        MOZ_ASSERT(false, "glGetString(GL_VERSION) has generated an error");
        return false;
    } else if (!versionString) {
        MOZ_ASSERT(false, "glGetString(GL_VERSION) has returned 0");
        return false;
    }

    const char kGLESVersionPrefix[] = "OpenGL ES ";
    if (strncmp(versionString, kGLESVersionPrefix, strlen(kGLESVersionPrefix)) == 0) {
        versionString += strlen(kGLESVersionPrefix);
    }

    const char* itr = versionString;
    char* end = nullptr;
    auto majorVersion = strtol(itr, &end, 10);

    if (!end) {
        MOZ_ASSERT(false, "Failed to parse the GL major version number.");
        return false;
    } else if (*end != '.') {
        MOZ_ASSERT(false, "Failed to parse GL's major-minor version number separator.");
        return false;
    }

    // we skip the '.' between the major and the minor version
    itr = end + 1;

    end = nullptr;

    auto minorVersion = strtol(itr, &end, 10);
    if (!end) {
        MOZ_ASSERT(false, "Failed to parse GL's minor version number.");
        return false;
    }

    if (majorVersion <= 0 || majorVersion >= 100) {
        MOZ_ASSERT(false, "Invalid major version.");
        return false;
    } else if (minorVersion < 0 || minorVersion >= 10) {
        MOZ_ASSERT(false, "Invalid minor version.");
        return false;
    }

    *out_version = (uint32_t)majorVersion * 100 + (uint32_t)minorVersion * 10;
    return true;
}

static uint8_t
ChooseDebugFlags(CreateContextFlags createFlags)
{
    uint8_t debugFlags = 0;

#ifdef MOZ_GL_DEBUG
    if (gfxEnv::GlDebug()) {
        debugFlags |= GLContext::DebugFlagEnabled;
    }

    // Enables extra verbose output, informing of the start and finish of every GL call.
    // Useful e.g. to record information to investigate graphics system crashes/lockups
    if (gfxEnv::GlDebugVerbose()) {
        debugFlags |= GLContext::DebugFlagTrace;
    }

    // Aborts on GL error. Can be useful to debug quicker code that is known not to
    // generate any GL error in principle.
    bool abortOnError = false;

    if (createFlags & CreateContextFlags::NO_VALIDATION) {
        abortOnError = true;

        const auto fnStringsMatch = [](const char* a, const char* b) {
            return strcmp(a, b) == 0;
        };

        const char* envAbortOnError = PR_GetEnv("MOZ_GL_DEBUG_ABORT_ON_ERROR");
        if (envAbortOnError && fnStringsMatch(envAbortOnError, "0")) {
           abortOnError = false;
        }
    }

    if (abortOnError) {
        debugFlags |= GLContext::DebugFlagAbortOnError;
    }
#endif

    return debugFlags;
}

GLContext::GLContext(CreateContextFlags flags, const SurfaceCaps& caps,
                     GLContext* sharedContext, bool isOffscreen)
  : mIsOffscreen(isOffscreen),
    mContextLost(false),
    mVersion(0),
    mProfile(ContextProfile::Unknown),
    mShadingLanguageVersion(0),
    mVendor(GLVendor::Other),
    mRenderer(GLRenderer::Other),
    mTopError(LOCAL_GL_NO_ERROR),
    mDebugFlags(ChooseDebugFlags(flags)),
    mSharedContext(sharedContext),
    mCaps(caps),
    mScreen(nullptr),
    mLockedSurface(nullptr),
    mMaxTextureSize(0),
    mMaxCubeMapTextureSize(0),
    mMaxTextureImageSize(0),
    mMaxRenderbufferSize(0),
    mMaxSamples(0),
    mNeedsTextureSizeChecks(false),
    mNeedsFlushBeforeDeleteFB(false),
    mTextureAllocCrashesOnMapFailure(false),
    mNeedsCheckAfterAttachTextureToFb(false),
    mWorkAroundDriverBugs(true),
    mHeavyGLCallsSinceLastFlush(false)
{
    mMaxViewportDims[0] = 0;
    mMaxViewportDims[1] = 0;
    mOwningThreadId = PlatformThread::CurrentId();
}

GLContext::~GLContext() {
    NS_ASSERTION(IsDestroyed(), "GLContext implementation must call MarkDestroyed in destructor!");
#ifdef MOZ_GL_DEBUG
    if (mSharedContext) {
        GLContext* tip = mSharedContext;
        while (tip->mSharedContext)
            tip = tip->mSharedContext;
        tip->SharedContextDestroyed(this);
        tip->ReportOutstandingNames();
    } else {
        ReportOutstandingNames();
    }
#endif
}

/*static*/ void
GLContext::StaticDebugCallback(GLenum source,
                               GLenum type,
                               GLuint id,
                               GLenum severity,
                               GLsizei length,
                               const GLchar* message,
                               const GLvoid* userParam)
{
    GLContext* gl = (GLContext*)userParam;
    gl->DebugCallback(source, type, id, severity, length, message);
}

static void
ClearSymbols(const GLLibraryLoader::SymLoadStruct* symbols)
{
    while (symbols->symPointer) {
        *symbols->symPointer = nullptr;
        symbols++;
    }
}

bool
GLContext::InitWithPrefix(const char* prefix, bool trygl)
{
    MOZ_RELEASE_ASSERT(!mSymbols.fBindFramebuffer,
                       "GFX: InitWithPrefix should only be called once.");

    if (!InitWithPrefixImpl(prefix, trygl)) {
        // If initialization fails, zero the symbols to avoid hard-to-understand bugs.
        mSymbols.Zero();
        NS_WARNING("GLContext::InitWithPrefix failed!");
        return false;
    }

    return true;
}

static bool
LoadGLSymbols(GLContext* gl, const char* prefix, bool trygl,
              const GLLibraryLoader::SymLoadStruct* list, const char* desc)
{
    if (gl->LoadSymbols(list, trygl, prefix))
        return true;

    ClearSymbols(list);

    if (desc) {
        const nsPrintfCString err("Failed to load symbols for %s.", desc);
        NS_ERROR(err.BeginReading());
    }
    return false;
}

bool
GLContext::LoadExtSymbols(const char* prefix, bool trygl, const SymLoadStruct* list,
                          GLExtensions ext)
{
    const char* extName = sExtensionNames[size_t(ext)];
    if (!LoadGLSymbols(this, prefix, trygl, list, extName)) {
        MarkExtensionUnsupported(ext);
        return false;
    }
    return true;
};

bool
GLContext::LoadFeatureSymbols(const char* prefix, bool trygl, const SymLoadStruct* list,
                              GLFeature feature)
{
    const char* featureName = GetFeatureName(feature);
    if (!LoadGLSymbols(this, prefix, trygl, list, featureName)) {
        MarkUnsupported(feature);
        return false;
    }
    return true;
};

bool
GLContext::InitWithPrefixImpl(const char* prefix, bool trygl)
{
    // wglGetProcAddress requires a current context.
    if (!MakeCurrent(true))
        return false;

    mWorkAroundDriverBugs = gfxPrefs::WorkAroundDriverBugs();

    const SymLoadStruct coreSymbols[] = {
        { (PRFuncPtr*) &mSymbols.fActiveTexture, { "ActiveTexture", "ActiveTextureARB", nullptr } },
        { (PRFuncPtr*) &mSymbols.fAttachShader, { "AttachShader", "AttachShaderARB", nullptr } },
        { (PRFuncPtr*) &mSymbols.fBindAttribLocation, { "BindAttribLocation", "BindAttribLocationARB", nullptr } },
        { (PRFuncPtr*) &mSymbols.fBindBuffer, { "BindBuffer", "BindBufferARB", nullptr } },
        { (PRFuncPtr*) &mSymbols.fBindTexture, { "BindTexture", "BindTextureARB", nullptr } },
        { (PRFuncPtr*) &mSymbols.fBlendColor, { "BlendColor", nullptr } },
        { (PRFuncPtr*) &mSymbols.fBlendEquation, { "BlendEquation", nullptr } },
        { (PRFuncPtr*) &mSymbols.fBlendEquationSeparate, { "BlendEquationSeparate", "BlendEquationSeparateEXT", nullptr } },
        { (PRFuncPtr*) &mSymbols.fBlendFunc, { "BlendFunc", nullptr } },
        { (PRFuncPtr*) &mSymbols.fBlendFuncSeparate, { "BlendFuncSeparate", "BlendFuncSeparateEXT", nullptr } },
        { (PRFuncPtr*) &mSymbols.fBufferData, { "BufferData", nullptr } },
        { (PRFuncPtr*) &mSymbols.fBufferSubData, { "BufferSubData", nullptr } },
        { (PRFuncPtr*) &mSymbols.fClear, { "Clear", nullptr } },
        { (PRFuncPtr*) &mSymbols.fClearColor, { "ClearColor", nullptr } },
        { (PRFuncPtr*) &mSymbols.fClearStencil, { "ClearStencil", nullptr } },
        { (PRFuncPtr*) &mSymbols.fColorMask, { "ColorMask", nullptr } },
        { (PRFuncPtr*) &mSymbols.fCompressedTexImage2D, {"CompressedTexImage2D", nullptr} },
        { (PRFuncPtr*) &mSymbols.fCompressedTexSubImage2D, {"CompressedTexSubImage2D", nullptr} },
        { (PRFuncPtr*) &mSymbols.fCullFace, { "CullFace", nullptr } },
        { (PRFuncPtr*) &mSymbols.fDetachShader, { "DetachShader", "DetachShaderARB", nullptr } },
        { (PRFuncPtr*) &mSymbols.fDepthFunc, { "DepthFunc", nullptr } },
        { (PRFuncPtr*) &mSymbols.fDepthMask, { "DepthMask", nullptr } },
        { (PRFuncPtr*) &mSymbols.fDisable, { "Disable", nullptr } },
        { (PRFuncPtr*) &mSymbols.fDisableVertexAttribArray, { "DisableVertexAttribArray", "DisableVertexAttribArrayARB", nullptr } },
        { (PRFuncPtr*) &mSymbols.fDrawArrays, { "DrawArrays", nullptr } },
        { (PRFuncPtr*) &mSymbols.fDrawElements, { "DrawElements", nullptr } },
        { (PRFuncPtr*) &mSymbols.fEnable, { "Enable", nullptr } },
        { (PRFuncPtr*) &mSymbols.fEnableVertexAttribArray, { "EnableVertexAttribArray", "EnableVertexAttribArrayARB", nullptr } },
        { (PRFuncPtr*) &mSymbols.fFinish, { "Finish", nullptr } },
        { (PRFuncPtr*) &mSymbols.fFlush, { "Flush", nullptr } },
        { (PRFuncPtr*) &mSymbols.fFrontFace, { "FrontFace", nullptr } },
        { (PRFuncPtr*) &mSymbols.fGetActiveAttrib, { "GetActiveAttrib", "GetActiveAttribARB", nullptr } },
        { (PRFuncPtr*) &mSymbols.fGetActiveUniform, { "GetActiveUniform", "GetActiveUniformARB", nullptr } },
        { (PRFuncPtr*) &mSymbols.fGetAttachedShaders, { "GetAttachedShaders", "GetAttachedShadersARB", nullptr } },
        { (PRFuncPtr*) &mSymbols.fGetAttribLocation, { "GetAttribLocation", "GetAttribLocationARB", nullptr } },
        { (PRFuncPtr*) &mSymbols.fGetIntegerv, { "GetIntegerv", nullptr } },
        { (PRFuncPtr*) &mSymbols.fGetFloatv, { "GetFloatv", nullptr } },
        { (PRFuncPtr*) &mSymbols.fGetBooleanv, { "GetBooleanv", nullptr } },
        { (PRFuncPtr*) &mSymbols.fGetBufferParameteriv, { "GetBufferParameteriv", "GetBufferParameterivARB", nullptr } },
        { (PRFuncPtr*) &mSymbols.fGetError, { "GetError", nullptr } },
        { (PRFuncPtr*) &mSymbols.fGetProgramiv, { "GetProgramiv", "GetProgramivARB", nullptr } },
        { (PRFuncPtr*) &mSymbols.fGetProgramInfoLog, { "GetProgramInfoLog", "GetProgramInfoLogARB", nullptr } },
        { (PRFuncPtr*) &mSymbols.fTexParameteri, { "TexParameteri", nullptr } },
        { (PRFuncPtr*) &mSymbols.fTexParameteriv, { "TexParameteriv", nullptr } },
        { (PRFuncPtr*) &mSymbols.fTexParameterf, { "TexParameterf", nullptr } },
        { (PRFuncPtr*) &mSymbols.fGetString, { "GetString", nullptr } },
        { (PRFuncPtr*) &mSymbols.fGetTexParameterfv, { "GetTexParameterfv", nullptr } },
        { (PRFuncPtr*) &mSymbols.fGetTexParameteriv, { "GetTexParameteriv", nullptr } },
        { (PRFuncPtr*) &mSymbols.fGetUniformfv, { "GetUniformfv", "GetUniformfvARB", nullptr } },
        { (PRFuncPtr*) &mSymbols.fGetUniformiv, { "GetUniformiv", "GetUniformivARB", nullptr } },
        { (PRFuncPtr*) &mSymbols.fGetUniformLocation, { "GetUniformLocation", "GetUniformLocationARB", nullptr } },
        { (PRFuncPtr*) &mSymbols.fGetVertexAttribfv, { "GetVertexAttribfv", "GetVertexAttribfvARB", nullptr } },
        { (PRFuncPtr*) &mSymbols.fGetVertexAttribiv, { "GetVertexAttribiv", "GetVertexAttribivARB", nullptr } },
        { (PRFuncPtr*) &mSymbols.fGetVertexAttribPointerv, { "GetVertexAttribPointerv", nullptr } },
        { (PRFuncPtr*) &mSymbols.fHint, { "Hint", nullptr } },
        { (PRFuncPtr*) &mSymbols.fIsBuffer, { "IsBuffer", "IsBufferARB", nullptr } },
        { (PRFuncPtr*) &mSymbols.fIsEnabled, { "IsEnabled", nullptr } },
        { (PRFuncPtr*) &mSymbols.fIsProgram, { "IsProgram", "IsProgramARB", nullptr } },
        { (PRFuncPtr*) &mSymbols.fIsShader, { "IsShader", "IsShaderARB", nullptr } },
        { (PRFuncPtr*) &mSymbols.fIsTexture, { "IsTexture", "IsTextureARB", nullptr } },
        { (PRFuncPtr*) &mSymbols.fLineWidth, { "LineWidth", nullptr } },
        { (PRFuncPtr*) &mSymbols.fLinkProgram, { "LinkProgram", "LinkProgramARB", nullptr } },
        { (PRFuncPtr*) &mSymbols.fPixelStorei, { "PixelStorei", nullptr } },
        { (PRFuncPtr*) &mSymbols.fPolygonOffset, { "PolygonOffset", nullptr } },
        { (PRFuncPtr*) &mSymbols.fReadPixels, { "ReadPixels", nullptr } },
        { (PRFuncPtr*) &mSymbols.fSampleCoverage, { "SampleCoverage", nullptr } },
        { (PRFuncPtr*) &mSymbols.fScissor, { "Scissor", nullptr } },
        { (PRFuncPtr*) &mSymbols.fStencilFunc, { "StencilFunc", nullptr } },
        { (PRFuncPtr*) &mSymbols.fStencilFuncSeparate, { "StencilFuncSeparate", "StencilFuncSeparateEXT", nullptr } },
        { (PRFuncPtr*) &mSymbols.fStencilMask, { "StencilMask", nullptr } },
        { (PRFuncPtr*) &mSymbols.fStencilMaskSeparate, { "StencilMaskSeparate", "StencilMaskSeparateEXT", nullptr } },
        { (PRFuncPtr*) &mSymbols.fStencilOp, { "StencilOp", nullptr } },
        { (PRFuncPtr*) &mSymbols.fStencilOpSeparate, { "StencilOpSeparate", "StencilOpSeparateEXT", nullptr } },
        { (PRFuncPtr*) &mSymbols.fTexImage2D, { "TexImage2D", nullptr } },
        { (PRFuncPtr*) &mSymbols.fTexSubImage2D, { "TexSubImage2D", nullptr } },
        { (PRFuncPtr*) &mSymbols.fUniform1f, { "Uniform1f", nullptr } },
        { (PRFuncPtr*) &mSymbols.fUniform1fv, { "Uniform1fv", nullptr } },
        { (PRFuncPtr*) &mSymbols.fUniform1i, { "Uniform1i", nullptr } },
        { (PRFuncPtr*) &mSymbols.fUniform1iv, { "Uniform1iv", nullptr } },
        { (PRFuncPtr*) &mSymbols.fUniform2f, { "Uniform2f", nullptr } },
        { (PRFuncPtr*) &mSymbols.fUniform2fv, { "Uniform2fv", nullptr } },
        { (PRFuncPtr*) &mSymbols.fUniform2i, { "Uniform2i", nullptr } },
        { (PRFuncPtr*) &mSymbols.fUniform2iv, { "Uniform2iv", nullptr } },
        { (PRFuncPtr*) &mSymbols.fUniform3f, { "Uniform3f", nullptr } },
        { (PRFuncPtr*) &mSymbols.fUniform3fv, { "Uniform3fv", nullptr } },
        { (PRFuncPtr*) &mSymbols.fUniform3i, { "Uniform3i", nullptr } },
        { (PRFuncPtr*) &mSymbols.fUniform3iv, { "Uniform3iv", nullptr } },
        { (PRFuncPtr*) &mSymbols.fUniform4f, { "Uniform4f", nullptr } },
        { (PRFuncPtr*) &mSymbols.fUniform4fv, { "Uniform4fv", nullptr } },
        { (PRFuncPtr*) &mSymbols.fUniform4i, { "Uniform4i", nullptr } },
        { (PRFuncPtr*) &mSymbols.fUniform4iv, { "Uniform4iv", nullptr } },
        { (PRFuncPtr*) &mSymbols.fUniformMatrix2fv, { "UniformMatrix2fv", nullptr } },
        { (PRFuncPtr*) &mSymbols.fUniformMatrix3fv, { "UniformMatrix3fv", nullptr } },
        { (PRFuncPtr*) &mSymbols.fUniformMatrix4fv, { "UniformMatrix4fv", nullptr } },
        { (PRFuncPtr*) &mSymbols.fUseProgram, { "UseProgram", nullptr } },
        { (PRFuncPtr*) &mSymbols.fValidateProgram, { "ValidateProgram", nullptr } },
        { (PRFuncPtr*) &mSymbols.fVertexAttribPointer, { "VertexAttribPointer", nullptr } },
        { (PRFuncPtr*) &mSymbols.fVertexAttrib1f, { "VertexAttrib1f", nullptr } },
        { (PRFuncPtr*) &mSymbols.fVertexAttrib2f, { "VertexAttrib2f", nullptr } },
        { (PRFuncPtr*) &mSymbols.fVertexAttrib3f, { "VertexAttrib3f", nullptr } },
        { (PRFuncPtr*) &mSymbols.fVertexAttrib4f, { "VertexAttrib4f", nullptr } },
        { (PRFuncPtr*) &mSymbols.fVertexAttrib1fv, { "VertexAttrib1fv", nullptr } },
        { (PRFuncPtr*) &mSymbols.fVertexAttrib2fv, { "VertexAttrib2fv", nullptr } },
        { (PRFuncPtr*) &mSymbols.fVertexAttrib3fv, { "VertexAttrib3fv", nullptr } },
        { (PRFuncPtr*) &mSymbols.fVertexAttrib4fv, { "VertexAttrib4fv", nullptr } },
        { (PRFuncPtr*) &mSymbols.fViewport, { "Viewport", nullptr } },
        { (PRFuncPtr*) &mSymbols.fCompileShader, { "CompileShader", nullptr } },
        { (PRFuncPtr*) &mSymbols.fCopyTexImage2D, { "CopyTexImage2D", nullptr } },
        { (PRFuncPtr*) &mSymbols.fCopyTexSubImage2D, { "CopyTexSubImage2D", nullptr } },
        { (PRFuncPtr*) &mSymbols.fGetShaderiv, { "GetShaderiv", nullptr } },
        { (PRFuncPtr*) &mSymbols.fGetShaderInfoLog, { "GetShaderInfoLog", nullptr } },
        { (PRFuncPtr*) &mSymbols.fGetShaderSource, { "GetShaderSource", nullptr } },
        { (PRFuncPtr*) &mSymbols.fShaderSource, { "ShaderSource", nullptr } },
        { (PRFuncPtr*) &mSymbols.fVertexAttribPointer, { "VertexAttribPointer", nullptr } },

        { (PRFuncPtr*) &mSymbols.fGenBuffers, { "GenBuffers", "GenBuffersARB", nullptr } },
        { (PRFuncPtr*) &mSymbols.fGenTextures, { "GenTextures", nullptr } },
        { (PRFuncPtr*) &mSymbols.fCreateProgram, { "CreateProgram", "CreateProgramARB", nullptr } },
        { (PRFuncPtr*) &mSymbols.fCreateShader, { "CreateShader", "CreateShaderARB", nullptr } },

        { (PRFuncPtr*) &mSymbols.fDeleteBuffers, { "DeleteBuffers", "DeleteBuffersARB", nullptr } },
        { (PRFuncPtr*) &mSymbols.fDeleteTextures, { "DeleteTextures", "DeleteTexturesARB", nullptr } },
        { (PRFuncPtr*) &mSymbols.fDeleteProgram, { "DeleteProgram", "DeleteProgramARB", nullptr } },
        { (PRFuncPtr*) &mSymbols.fDeleteShader, { "DeleteShader", "DeleteShaderARB", nullptr } },

        END_SYMBOLS
    };

    if (!LoadGLSymbols(this, prefix, trygl, coreSymbols, "GL"))
        return false;

    ////////////////

    MOZ_ASSERT(mProfile != ContextProfile::Unknown);

    uint32_t version = 0;
    ParseGLVersion(this, &version);

    mShadingLanguageVersion = 100;
    ParseGLSLVersion(this, &mShadingLanguageVersion);

    if (ShouldSpew()) {
        printf_stderr("OpenGL version detected: %u\n", version);
        printf_stderr("OpenGL shading language version detected: %u\n", mShadingLanguageVersion);
        printf_stderr("OpenGL vendor: %s\n", fGetString(LOCAL_GL_VENDOR));
        printf_stderr("OpenGL renderer: %s\n", fGetString(LOCAL_GL_RENDERER));
    }

    if (version >= mVersion) {
        mVersion = version;
    }
    // Don't fail if version < mVersion, see bug 999445,
    // Mac OSX 10.6/10.7 machines with Intel GPUs claim only OpenGL 1.4 but
    // have all the GL2+ extensions that we need.

    ////////////////

    // Load OpenGL ES 2.0 symbols, or desktop if we aren't using ES 2.
    if (IsGLES()) {
        const SymLoadStruct symbols[] = {
            { (PRFuncPtr*) &mSymbols.fGetShaderPrecisionFormat, { "GetShaderPrecisionFormat", nullptr } },
            { (PRFuncPtr*) &mSymbols.fClearDepthf, { "ClearDepthf", nullptr } },
            { (PRFuncPtr*) &mSymbols.fDepthRangef, { "DepthRangef", nullptr } },
            END_SYMBOLS
        };

        if (!LoadGLSymbols(this, prefix, trygl, symbols, "OpenGL ES"))
            return false;
    } else {
        const SymLoadStruct symbols[] = {
            { (PRFuncPtr*) &mSymbols.fClearDepth, { "ClearDepth", nullptr } },
            { (PRFuncPtr*) &mSymbols.fDepthRange, { "DepthRange", nullptr } },
            { (PRFuncPtr*) &mSymbols.fReadBuffer, { "ReadBuffer", nullptr } },
            { (PRFuncPtr*) &mSymbols.fMapBuffer, { "MapBuffer", nullptr } },
            { (PRFuncPtr*) &mSymbols.fUnmapBuffer, { "UnmapBuffer", nullptr } },
            { (PRFuncPtr*) &mSymbols.fPointParameterf, { "PointParameterf", nullptr } },
            { (PRFuncPtr*) &mSymbols.fDrawBuffer, { "DrawBuffer", nullptr } },
            // The following functions are only used by Skia/GL in desktop mode.
            // Other parts of Gecko should avoid using these
            { (PRFuncPtr*) &mSymbols.fDrawBuffers, { "DrawBuffers", nullptr } },
            { (PRFuncPtr*) &mSymbols.fClientActiveTexture, { "ClientActiveTexture", nullptr } },
            { (PRFuncPtr*) &mSymbols.fDisableClientState, { "DisableClientState", nullptr } },
            { (PRFuncPtr*) &mSymbols.fEnableClientState, { "EnableClientState", nullptr } },
            { (PRFuncPtr*) &mSymbols.fLoadIdentity, { "LoadIdentity", nullptr } },
            { (PRFuncPtr*) &mSymbols.fLoadMatrixf, { "LoadMatrixf", nullptr } },
            { (PRFuncPtr*) &mSymbols.fMatrixMode, { "MatrixMode", nullptr } },
            { (PRFuncPtr*) &mSymbols.fTexGeni, { "TexGeni", nullptr } },
            { (PRFuncPtr*) &mSymbols.fTexGenf, { "TexGenf", nullptr } },
            { (PRFuncPtr*) &mSymbols.fTexGenfv, { "TexGenfv", nullptr } },
            { (PRFuncPtr*) &mSymbols.fVertexPointer, { "VertexPointer", nullptr } },
            END_SYMBOLS
        };

        if (!LoadGLSymbols(this, prefix, trygl, symbols, "Desktop OpenGL"))
            return false;
    }

    ////////////////

    const char* glVendorString = (const char*)fGetString(LOCAL_GL_VENDOR);
    const char* glRendererString = (const char*)fGetString(LOCAL_GL_RENDERER);
    if (!glVendorString || !glRendererString)
        return false;

    // The order of these strings must match up with the order of the enum
    // defined in GLContext.h for vendor IDs.
    const char* vendorMatchStrings[size_t(GLVendor::Other) + 1] = {
        "Intel",
        "NVIDIA",
        "ATI",
        "Qualcomm",
        "Imagination",
        "nouveau",
        "Vivante",
        "VMware, Inc.",
        "ARM",
        "Unknown"
    };

    mVendor = GLVendor::Other;
    for (size_t i = 0; i < size_t(GLVendor::Other); ++i) {
        if (DoesStringMatch(glVendorString, vendorMatchStrings[i])) {
            mVendor = GLVendor(i);
            break;
        }
    }

    // The order of these strings must match up with the order of the enum
    // defined in GLContext.h for renderer IDs.
    const char* rendererMatchStrings[size_t(GLRenderer::Other) + 1] = {
        "Adreno 200",
        "Adreno 205",
        "Adreno (TM) 200",
        "Adreno (TM) 205",
        "Adreno (TM) 305",
        "Adreno (TM) 320",
        "Adreno (TM) 330",
        "Adreno (TM) 420",
        "Mali-400 MP",
        "Mali-450 MP",
        "PowerVR SGX 530",
        "PowerVR SGX 540",
        "PowerVR SGX 544MP",
        "NVIDIA Tegra",
        "Android Emulator",
        "Gallium 0.4 on llvmpipe",
        "Intel HD Graphics 3000 OpenGL Engine",
        "Microsoft Basic Render Driver",
        "Unknown"
    };

    mRenderer = GLRenderer::Other;
    for (size_t i = 0; i < size_t(GLRenderer::Other); ++i) {
        if (DoesStringMatch(glRendererString, rendererMatchStrings[i])) {
            mRenderer = GLRenderer(i);
            break;
        }
    }

    if (ShouldSpew()) {
        printf_stderr("GL_VENDOR: %s\n", glVendorString);
        printf_stderr("mVendor: %s\n", vendorMatchStrings[size_t(mVendor)]);
        printf_stderr("GL_RENDERER: %s\n", glRendererString);
        printf_stderr("mRenderer: %s\n", rendererMatchStrings[size_t(mRenderer)]);
    }

    ////////////////

    // We need this for retrieving the list of extensions on Core profiles.
    if (IsFeatureProvidedByCoreSymbols(GLFeature::get_string_indexed)) {
        const SymLoadStruct symbols[] = {
            { (PRFuncPtr*) &mSymbols.fGetStringi, { "GetStringi", nullptr } },
            END_SYMBOLS
        };

        if (!LoadGLSymbols(this, prefix, trygl, symbols, "get_string_indexed")) {
            MOZ_RELEASE_ASSERT(false, "GFX: get_string_indexed is required!");
            return false;
        }
    }

    InitExtensions();
    InitFeatures();

    // Disable extensions with partial or incorrect support.
    if (WorkAroundDriverBugs()) {
        if (Renderer() == GLRenderer::AdrenoTM320) {
            MarkUnsupported(GLFeature::standard_derivatives);
        }

        if (Vendor() == GLVendor::Vivante) {
            // bug 958256
            MarkUnsupported(GLFeature::standard_derivatives);
        }

        if (Renderer() == GLRenderer::MicrosoftBasicRenderDriver) {
            // Bug 978966: on Microsoft's "Basic Render Driver" (software renderer)
            // multisampling hardcodes blending with the default blendfunc, which breaks WebGL.
            MarkUnsupported(GLFeature::framebuffer_multisample);
        }

#ifdef XP_MACOSX
        // The Mac Nvidia driver, for versions up to and including 10.8,
        // don't seem to properly support this.  See 814839
        // this has been fixed in Mac OS X 10.9. See 907946
        // and it also works in 10.8.3 and higher.  See 1094338.
        if (Vendor() == gl::GLVendor::NVIDIA &&
            !nsCocoaFeatures::IsAtLeastVersion(10,8,3))
        {
            MarkUnsupported(GLFeature::depth_texture);
        }
#endif
    }

    if (IsExtensionSupported(GLContext::ARB_pixel_buffer_object)) {
        MOZ_ASSERT((mSymbols.fMapBuffer && mSymbols.fUnmapBuffer),
                   "ARB_pixel_buffer_object supported without glMapBuffer/UnmapBuffer"
                   " being available!");
    }

    ////////////////////////////////////////////////////////////////////////////

    const auto fnLoadForFeature = [this, prefix, trygl](const SymLoadStruct* list,
                                                        GLFeature feature)
    {
        return this->LoadFeatureSymbols(prefix, trygl, list, feature);
    };

    // Check for ARB_framebuffer_objects
    if (IsSupported(GLFeature::framebuffer_object)) {
        // https://www.opengl.org/registry/specs/ARB/framebuffer_object.txt
        const SymLoadStruct symbols[] = {
            CORE_SYMBOL(IsRenderbuffer),
            CORE_SYMBOL(BindRenderbuffer),
            CORE_SYMBOL(DeleteRenderbuffers),
            CORE_SYMBOL(GenRenderbuffers),
            CORE_SYMBOL(RenderbufferStorage),
            CORE_SYMBOL(RenderbufferStorageMultisample),
            CORE_SYMBOL(GetRenderbufferParameteriv),
            CORE_SYMBOL(IsFramebuffer),
            CORE_SYMBOL(BindFramebuffer),
            CORE_SYMBOL(DeleteFramebuffers),
            CORE_SYMBOL(GenFramebuffers),
            CORE_SYMBOL(CheckFramebufferStatus),
            CORE_SYMBOL(FramebufferTexture2D),
            CORE_SYMBOL(FramebufferTextureLayer),
            CORE_SYMBOL(FramebufferRenderbuffer),
            CORE_SYMBOL(GetFramebufferAttachmentParameteriv),
            CORE_SYMBOL(BlitFramebuffer),
            CORE_SYMBOL(GenerateMipmap),
            END_SYMBOLS
        };
        fnLoadForFeature(symbols, GLFeature::framebuffer_object);
    }

    if (!IsSupported(GLFeature::framebuffer_object)) {
        // Check for aux symbols based on extensions
        if (IsSupported(GLFeature::framebuffer_object_EXT_OES)) {
            const SymLoadStruct symbols[] = {
                CORE_EXT_SYMBOL2(IsRenderbuffer, EXT, OES),
                CORE_EXT_SYMBOL2(BindRenderbuffer, EXT, OES),
                CORE_EXT_SYMBOL2(DeleteRenderbuffers, EXT, OES),
                CORE_EXT_SYMBOL2(GenRenderbuffers, EXT, OES),
                CORE_EXT_SYMBOL2(RenderbufferStorage, EXT, OES),
                CORE_EXT_SYMBOL2(GetRenderbufferParameteriv, EXT, OES),
                CORE_EXT_SYMBOL2(IsFramebuffer, EXT, OES),
                CORE_EXT_SYMBOL2(BindFramebuffer, EXT, OES),
                CORE_EXT_SYMBOL2(DeleteFramebuffers, EXT, OES),
                CORE_EXT_SYMBOL2(GenFramebuffers, EXT, OES),
                CORE_EXT_SYMBOL2(CheckFramebufferStatus, EXT, OES),
                CORE_EXT_SYMBOL2(FramebufferTexture2D, EXT, OES),
                CORE_EXT_SYMBOL2(FramebufferRenderbuffer, EXT, OES),
                CORE_EXT_SYMBOL2(GetFramebufferAttachmentParameteriv, EXT, OES),
                CORE_EXT_SYMBOL2(GenerateMipmap, EXT, OES),
                END_SYMBOLS
            };
            fnLoadForFeature(symbols, GLFeature::framebuffer_object_EXT_OES);
        }

        if (IsSupported(GLFeature::framebuffer_blit)) {
            const SymLoadStruct symbols[] = {
                EXT_SYMBOL3(BlitFramebuffer, ANGLE, EXT, NV),
                END_SYMBOLS
            };
            fnLoadForFeature(symbols, GLFeature::framebuffer_blit);
        }

        if (IsSupported(GLFeature::framebuffer_multisample)) {
            const SymLoadStruct symbols[] = {
                EXT_SYMBOL3(RenderbufferStorageMultisample, ANGLE, APPLE, EXT),
                END_SYMBOLS
            };
            fnLoadForFeature(symbols, GLFeature::framebuffer_multisample);
        }

        if (IsExtensionSupported(GLContext::ARB_geometry_shader4) ||
            IsExtensionSupported(GLContext::NV_geometry_program4))
        {
            const SymLoadStruct symbols[] = {
                EXT_SYMBOL2(FramebufferTextureLayer, ARB, EXT),
                END_SYMBOLS
            };
            if (!LoadGLSymbols(this, prefix, trygl, symbols,
                               "ARB_geometry_shader4/NV_geometry_program4"))
            {
                MarkExtensionUnsupported(GLContext::ARB_geometry_shader4);
                MarkExtensionUnsupported(GLContext::NV_geometry_program4);
            }
        }
    }

    if (!IsSupported(GLFeature::framebuffer_object) &&
        !IsSupported(GLFeature::framebuffer_object_EXT_OES))
    {
        NS_ERROR("GLContext requires support for framebuffer objects.");
        return false;
    }
    MOZ_RELEASE_ASSERT(mSymbols.fBindFramebuffer, "GFX: mSymbols.fBindFramebuffer zero or not set.");

    ////////////////

    LoadMoreSymbols(prefix, trygl);

    ////////////////////////////////////////////////////////////////////////////

    raw_fGetIntegerv(LOCAL_GL_VIEWPORT, mViewportRect);
    raw_fGetIntegerv(LOCAL_GL_SCISSOR_BOX, mScissorRect);
    raw_fGetIntegerv(LOCAL_GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
    raw_fGetIntegerv(LOCAL_GL_MAX_CUBE_MAP_TEXTURE_SIZE, &mMaxCubeMapTextureSize);
    raw_fGetIntegerv(LOCAL_GL_MAX_RENDERBUFFER_SIZE, &mMaxRenderbufferSize);
    raw_fGetIntegerv(LOCAL_GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);

#ifdef XP_MACOSX
    if (mWorkAroundDriverBugs) {
        if (mVendor == GLVendor::Intel) {
            // see bug 737182 for 2D textures, bug 684882 for cube map textures.
            mMaxTextureSize        = std::min(mMaxTextureSize,        4096);
            mMaxCubeMapTextureSize = std::min(mMaxCubeMapTextureSize, 512);
            // for good measure, we align renderbuffers on what we do for 2D textures
            mMaxRenderbufferSize   = std::min(mMaxRenderbufferSize,   4096);
            mNeedsTextureSizeChecks = true;
        } else if (mVendor == GLVendor::NVIDIA) {
            // See bug 879656.  8192 fails, 8191 works.
            mMaxTextureSize = std::min(mMaxTextureSize, 8191);
            mMaxRenderbufferSize = std::min(mMaxRenderbufferSize, 8191);

            // Part of the bug 879656, but it also doesn't hurt the 877949
            mNeedsTextureSizeChecks = true;
        }
    }
#endif
#ifdef MOZ_X11
    if (mWorkAroundDriverBugs) {
        if (mVendor == GLVendor::Nouveau) {
            // see bug 814716. Clamp MaxCubeMapTextureSize at 2K for Nouveau.
            mMaxCubeMapTextureSize = std::min(mMaxCubeMapTextureSize, 2048);
            mNeedsTextureSizeChecks = true;
        } else if (mVendor == GLVendor::Intel) {
            // Bug 1199923. Driver seems to report a larger max size than
            // actually supported.
            mMaxTextureSize /= 2;
            mMaxRenderbufferSize /= 2;
            mNeedsTextureSizeChecks = true;
        }
    }
#endif
    if (mWorkAroundDriverBugs &&
        Renderer() == GLRenderer::AdrenoTM420) {
        // see bug 1194923. Calling glFlush before glDeleteFramebuffers
        // prevents occasional driver crash.
        mNeedsFlushBeforeDeleteFB = true;
    }
#ifdef MOZ_WIDGET_ANDROID
    if (mWorkAroundDriverBugs &&
        (Renderer() == GLRenderer::AdrenoTM305 ||
         Renderer() == GLRenderer::AdrenoTM320 ||
         Renderer() == GLRenderer::AdrenoTM330) &&
        AndroidBridge::Bridge()->GetAPIVersion() < 21) {
        // Bug 1164027. Driver crashes when functions such as
        // glTexImage2D fail due to virtual memory exhaustion.
        mTextureAllocCrashesOnMapFailure = true;
    }
#endif
#if MOZ_WIDGET_ANDROID
    if (mWorkAroundDriverBugs &&
        Renderer() == GLRenderer::SGX540 &&
        AndroidBridge::Bridge()->GetAPIVersion() <= 15) {
        // Bug 1288446. Driver sometimes crashes when uploading data to a
        // texture if the render target has changed since the texture was
        // rendered from. Calling glCheckFramebufferStatus after
        // glFramebufferTexture2D prevents the crash.
        mNeedsCheckAfterAttachTextureToFb = true;
    }
#endif

    mMaxTextureImageSize = mMaxTextureSize;

    if (IsSupported(GLFeature::framebuffer_multisample)) {
        fGetIntegerv(LOCAL_GL_MAX_SAMPLES, (GLint*)&mMaxSamples);
    }

    ////////////////////////////////////////////////////////////////////////////

    // We're ready for final setup.
    fBindFramebuffer(LOCAL_GL_FRAMEBUFFER, 0);

    // TODO: Remove SurfaceCaps::any.
    if (mCaps.any) {
        mCaps.any = false;
        mCaps.color = true;
        mCaps.alpha = false;
    }

    mTexGarbageBin = new TextureGarbageBin(this);

    MOZ_ASSERT(IsCurrent());

    if (ShouldSpew() && IsExtensionSupported(KHR_debug)) {
        fEnable(LOCAL_GL_DEBUG_OUTPUT);
        fDisable(LOCAL_GL_DEBUG_OUTPUT_SYNCHRONOUS);
        fDebugMessageCallback(&StaticDebugCallback, (void*)this);
        fDebugMessageControl(LOCAL_GL_DONT_CARE,
                             LOCAL_GL_DONT_CARE,
                             LOCAL_GL_DONT_CARE,
                             0, nullptr,
                             true);
    }

    mVersionString = nsPrintfCString("%u.%u.%u", mVersion / 100, (mVersion / 10) % 10,
                                     mVersion % 10);
    return true;
}

void
GLContext::LoadMoreSymbols(const char* prefix, bool trygl)
{
    const auto fnLoadForExt = [this, prefix, trygl](const SymLoadStruct* list,
                                                    GLExtensions ext)
    {
        return this->LoadExtSymbols(prefix, trygl, list, ext);
    };

    const auto fnLoadForFeature = [this, prefix, trygl](const SymLoadStruct* list,
                                                        GLFeature feature)
    {
        return this->LoadFeatureSymbols(prefix, trygl, list, feature);
    };

    const auto fnLoadFeatureByCore = [this, fnLoadForFeature](const SymLoadStruct* coreList,
                                                              const SymLoadStruct* extList,
                                                              GLFeature feature)
    {
        const bool useCore = this->IsFeatureProvidedByCoreSymbols(feature);
        const auto list = useCore ? coreList : extList;
        return fnLoadForFeature(list, feature);
    };

    if (IsSupported(GLFeature::robustness)) {
        bool hasRobustness = false;

        if (!hasRobustness && IsExtensionSupported(ARB_robustness)) {
            const SymLoadStruct symbols[] = {
                { (PRFuncPtr*) &mSymbols.fGetGraphicsResetStatus, { "GetGraphicsResetStatusARB", nullptr } },
                END_SYMBOLS
            };
            if (fnLoadForExt(symbols, ARB_robustness)) {
                hasRobustness = true;
            }
        }

        if (!hasRobustness && IsExtensionSupported(EXT_robustness)) {
            const SymLoadStruct symbols[] = {
                { (PRFuncPtr*) &mSymbols.fGetGraphicsResetStatus, { "GetGraphicsResetStatusEXT", nullptr } },
                END_SYMBOLS
            };
            if (fnLoadForExt(symbols, EXT_robustness)) {
                hasRobustness = true;
            }
        }

        if (!hasRobustness) {
            MarkUnsupported(GLFeature::robustness);
        }
    }

    if (IsSupported(GLFeature::sync)) {
        const SymLoadStruct symbols[] = {
            { (PRFuncPtr*) &mSymbols.fFenceSync,      { "FenceSync",      nullptr } },
            { (PRFuncPtr*) &mSymbols.fIsSync,         { "IsSync",         nullptr } },
            { (PRFuncPtr*) &mSymbols.fDeleteSync,     { "DeleteSync",     nullptr } },
            { (PRFuncPtr*) &mSymbols.fClientWaitSync, { "ClientWaitSync", nullptr } },
            { (PRFuncPtr*) &mSymbols.fWaitSync,       { "WaitSync",       nullptr } },
            { (PRFuncPtr*) &mSymbols.fGetInteger64v,  { "GetInteger64v",  nullptr } },
            { (PRFuncPtr*) &mSymbols.fGetSynciv,      { "GetSynciv",      nullptr } },
            END_SYMBOLS
        };
        fnLoadForFeature(symbols, GLFeature::sync);
    }

    if (IsExtensionSupported(OES_EGL_image)) {
        const SymLoadStruct symbols[] = {
            { (PRFuncPtr*) &mSymbols.fEGLImageTargetTexture2D, { "EGLImageTargetTexture2DOES", nullptr } },
            { (PRFuncPtr*) &mSymbols.fEGLImageTargetRenderbufferStorage, { "EGLImageTargetRenderbufferStorageOES", nullptr } },
            END_SYMBOLS
        };
        fnLoadForExt(symbols, OES_EGL_image);
    }

    if (IsExtensionSupported(APPLE_texture_range)) {
        const SymLoadStruct symbols[] = {
            { (PRFuncPtr*) &mSymbols.fTextureRangeAPPLE, { "TextureRangeAPPLE", nullptr } },
            END_SYMBOLS
        };
        fnLoadForExt(symbols, APPLE_texture_range);
    }

    if (IsSupported(GLFeature::vertex_array_object)) {
        const SymLoadStruct coreSymbols[] = {
            { (PRFuncPtr*) &mSymbols.fIsVertexArray, { "IsVertexArray", nullptr } },
            { (PRFuncPtr*) &mSymbols.fGenVertexArrays, { "GenVertexArrays", nullptr } },
            { (PRFuncPtr*) &mSymbols.fBindVertexArray, { "BindVertexArray", nullptr } },
            { (PRFuncPtr*) &mSymbols.fDeleteVertexArrays, { "DeleteVertexArrays", nullptr } },
            END_SYMBOLS
        };
        const SymLoadStruct extSymbols[] = {
            { (PRFuncPtr*) &mSymbols.fIsVertexArray, { "IsVertexArrayARB", "IsVertexArrayOES", "IsVertexArrayAPPLE", nullptr } },
            { (PRFuncPtr*) &mSymbols.fGenVertexArrays, { "GenVertexArraysARB", "GenVertexArraysOES", "GenVertexArraysAPPLE", nullptr } },
            { (PRFuncPtr*) &mSymbols.fBindVertexArray, { "BindVertexArrayARB", "BindVertexArrayOES", "BindVertexArrayAPPLE", nullptr } },
            { (PRFuncPtr*) &mSymbols.fDeleteVertexArrays, { "DeleteVertexArraysARB", "DeleteVertexArraysOES", "DeleteVertexArraysAPPLE", nullptr } },
            END_SYMBOLS
        };
        fnLoadFeatureByCore(coreSymbols, extSymbols, GLFeature::vertex_array_object);
    }

    if (IsSupported(GLFeature::draw_instanced)) {
        const SymLoadStruct coreSymbols[] = {
            { (PRFuncPtr*) &mSymbols.fDrawArraysInstanced, { "DrawArraysInstanced", nullptr } },
            { (PRFuncPtr*) &mSymbols.fDrawElementsInstanced, { "DrawElementsInstanced", nullptr } },
            END_SYMBOLS
        };
        const SymLoadStruct extSymbols[] = {
            { (PRFuncPtr*) &mSymbols.fDrawArraysInstanced, { "DrawArraysInstancedARB", "DrawArraysInstancedEXT", "DrawArraysInstancedNV", "DrawArraysInstancedANGLE", nullptr } },
            { (PRFuncPtr*) &mSymbols.fDrawElementsInstanced, { "DrawElementsInstancedARB", "DrawElementsInstancedEXT", "DrawElementsInstancedNV", "DrawElementsInstancedANGLE", nullptr }
            },
            END_SYMBOLS
        };
        fnLoadFeatureByCore(coreSymbols, extSymbols, GLFeature::draw_instanced);
    }

    if (IsSupported(GLFeature::instanced_arrays)) {
        const SymLoadStruct coreSymbols[] = {
            { (PRFuncPtr*) &mSymbols.fVertexAttribDivisor, { "VertexAttribDivisor", nullptr } },
            END_SYMBOLS
        };
        const SymLoadStruct extSymbols[] = {
            { (PRFuncPtr*) &mSymbols.fVertexAttribDivisor, { "VertexAttribDivisorARB", "VertexAttribDivisorNV", "VertexAttribDivisorANGLE", nullptr } },
            END_SYMBOLS
        };
        fnLoadFeatureByCore(coreSymbols, extSymbols, GLFeature::instanced_arrays);
    }

    if (IsSupported(GLFeature::texture_storage)) {
        const SymLoadStruct coreSymbols[] = {
            { (PRFuncPtr*) &mSymbols.fTexStorage2D, { "TexStorage2D", nullptr } },
            { (PRFuncPtr*) &mSymbols.fTexStorage3D, { "TexStorage3D", nullptr } },
            END_SYMBOLS
        };
        const SymLoadStruct extSymbols[] = {
            { (PRFuncPtr*) &mSymbols.fTexStorage2D, { "TexStorage2DEXT", nullptr } },
            { (PRFuncPtr*) &mSymbols.fTexStorage3D, { "TexStorage3DEXT", nullptr } },
            END_SYMBOLS
        };
        fnLoadFeatureByCore(coreSymbols, extSymbols, GLFeature::texture_storage);
    }

    if (IsSupported(GLFeature::sampler_objects)) {
        const SymLoadStruct symbols[] = {
            { (PRFuncPtr*) &mSymbols.fGenSamplers, { "GenSamplers", nullptr } },
            { (PRFuncPtr*) &mSymbols.fDeleteSamplers, { "DeleteSamplers", nullptr } },
            { (PRFuncPtr*) &mSymbols.fIsSampler, { "IsSampler", nullptr } },
            { (PRFuncPtr*) &mSymbols.fBindSampler, { "BindSampler", nullptr } },
            { (PRFuncPtr*) &mSymbols.fSamplerParameteri, { "SamplerParameteri", nullptr } },
            { (PRFuncPtr*) &mSymbols.fSamplerParameteriv, { "SamplerParameteriv", nullptr } },
            { (PRFuncPtr*) &mSymbols.fSamplerParameterf, { "SamplerParameterf", nullptr } },
            { (PRFuncPtr*) &mSymbols.fSamplerParameterfv, { "SamplerParameterfv", nullptr } },
            { (PRFuncPtr*) &mSymbols.fGetSamplerParameteriv, { "GetSamplerParameteriv", nullptr } },
            { (PRFuncPtr*) &mSymbols.fGetSamplerParameterfv, { "GetSamplerParameterfv", nullptr } },
            END_SYMBOLS
        };
        fnLoadForFeature(symbols, GLFeature::sampler_objects);
    }

    // ARB_transform_feedback2/NV_transform_feedback2 is a
    // superset of EXT_transform_feedback/NV_transform_feedback
    // and adds glPauseTransformFeedback &
    // glResumeTransformFeedback, which are required for WebGL2.
    if (IsSupported(GLFeature::transform_feedback2)) {
        const SymLoadStruct coreSymbols[] = {
            { (PRFuncPtr*) &mSymbols.fBindBufferBase, { "BindBufferBase", nullptr } },
            { (PRFuncPtr*) &mSymbols.fBindBufferRange, { "BindBufferRange", nullptr } },
            { (PRFuncPtr*) &mSymbols.fGenTransformFeedbacks, { "GenTransformFeedbacks", nullptr } },
            { (PRFuncPtr*) &mSymbols.fBindTransformFeedback, { "BindTransformFeedback", nullptr } },
            { (PRFuncPtr*) &mSymbols.fDeleteTransformFeedbacks, { "DeleteTransformFeedbacks", nullptr } },
            { (PRFuncPtr*) &mSymbols.fIsTransformFeedback, { "IsTransformFeedback", nullptr } },
            { (PRFuncPtr*) &mSymbols.fBeginTransformFeedback, { "BeginTransformFeedback", nullptr } },
            { (PRFuncPtr*) &mSymbols.fEndTransformFeedback, { "EndTransformFeedback", nullptr } },
            { (PRFuncPtr*) &mSymbols.fTransformFeedbackVaryings, { "TransformFeedbackVaryings", nullptr } },
            { (PRFuncPtr*) &mSymbols.fGetTransformFeedbackVarying, { "GetTransformFeedbackVarying", nullptr } },
            { (PRFuncPtr*) &mSymbols.fPauseTransformFeedback, { "PauseTransformFeedback", nullptr } },
            { (PRFuncPtr*) &mSymbols.fResumeTransformFeedback, { "ResumeTransformFeedback", nullptr } },
            END_SYMBOLS
        };
        const SymLoadStruct extSymbols[] = {
            { (PRFuncPtr*) &mSymbols.fBindBufferBase, { "BindBufferBaseEXT", "BindBufferBaseNV", nullptr } },
            { (PRFuncPtr*) &mSymbols.fBindBufferRange, { "BindBufferRangeEXT", "BindBufferRangeNV", nullptr } },
            { (PRFuncPtr*) &mSymbols.fGenTransformFeedbacks, { "GenTransformFeedbacksNV", nullptr } },
            { (PRFuncPtr*) &mSymbols.fBindTransformFeedback, { "BindTransformFeedbackNV", nullptr } },
            { (PRFuncPtr*) &mSymbols.fDeleteTransformFeedbacks, { "DeleteTransformFeedbacksNV", nullptr } },
            { (PRFuncPtr*) &mSymbols.fIsTransformFeedback, { "IsTransformFeedbackNV", nullptr } },
            { (PRFuncPtr*) &mSymbols.fBeginTransformFeedback, { "BeginTransformFeedbackEXT", "BeginTransformFeedbackNV", nullptr } },
            { (PRFuncPtr*) &mSymbols.fEndTransformFeedback, { "EndTransformFeedbackEXT", "EndTransformFeedbackNV", nullptr } },
            { (PRFuncPtr*) &mSymbols.fTransformFeedbackVaryings, { "TransformFeedbackVaryingsEXT", "TransformFeedbackVaryingsNV", nullptr } },
            { (PRFuncPtr*) &mSymbols.fGetTransformFeedbackVarying, { "GetTransformFeedbackVaryingEXT", "GetTransformFeedbackVaryingNV", nullptr } },
            { (PRFuncPtr*) &mSymbols.fPauseTransformFeedback, { "PauseTransformFeedbackNV", nullptr } },
            { (PRFuncPtr*) &mSymbols.fResumeTransformFeedback, { "ResumeTransformFeedbackNV", nullptr } },
            END_SYMBOLS
        };
        if (!fnLoadFeatureByCore(coreSymbols, extSymbols, GLFeature::texture_storage)) {
            // Also mark bind_buffer_offset as unsupported.
            MarkUnsupported(GLFeature::bind_buffer_offset);
        }
    }

    if (IsSupported(GLFeature::bind_buffer_offset)) {
        const SymLoadStruct coreSymbols[] = {
            { (PRFuncPtr*) &mSymbols.fBindBufferOffset, { "BindBufferOffset", nullptr } },
            END_SYMBOLS
        };
        const SymLoadStruct extSymbols[] = {
            { (PRFuncPtr*) &mSymbols.fBindBufferOffset,
              { "BindBufferOffsetEXT", "BindBufferOffsetNV", nullptr }
            },
            END_SYMBOLS
        };
        fnLoadFeatureByCore(coreSymbols, extSymbols, GLFeature::bind_buffer_offset);
    }

    if (IsSupported(GLFeature::query_counter)) {
        const SymLoadStruct coreSymbols[] = {
            { (PRFuncPtr*) &mSymbols.fQueryCounter, { "QueryCounter", nullptr } },
            END_SYMBOLS
        };
        const SymLoadStruct extSymbols[] = {
            { (PRFuncPtr*) &mSymbols.fQueryCounter, { "QueryCounterEXT", "QueryCounterANGLE", nullptr } },
            END_SYMBOLS
        };
        fnLoadFeatureByCore(coreSymbols, extSymbols, GLFeature::query_counter);
    }

    if (IsSupported(GLFeature::query_objects)) {
        const SymLoadStruct coreSymbols[] = {
            { (PRFuncPtr*) &mSymbols.fBeginQuery, { "BeginQuery", nullptr } },
            { (PRFuncPtr*) &mSymbols.fGenQueries, { "GenQueries", nullptr } },
            { (PRFuncPtr*) &mSymbols.fDeleteQueries, { "DeleteQueries", nullptr } },
            { (PRFuncPtr*) &mSymbols.fEndQuery, { "EndQuery", nullptr } },
            { (PRFuncPtr*) &mSymbols.fGetQueryiv, { "GetQueryiv", nullptr } },
            { (PRFuncPtr*) &mSymbols.fGetQueryObjectuiv, { "GetQueryObjectuiv", nullptr } },
            { (PRFuncPtr*) &mSymbols.fIsQuery, { "IsQuery", nullptr } },
            END_SYMBOLS
        };
        const SymLoadStruct extSymbols[] = {
            { (PRFuncPtr*) &mSymbols.fBeginQuery, { "BeginQueryEXT", "BeginQueryANGLE", nullptr } },
            { (PRFuncPtr*) &mSymbols.fGenQueries, { "GenQueriesEXT", "GenQueriesANGLE", nullptr } },
            { (PRFuncPtr*) &mSymbols.fDeleteQueries, { "DeleteQueriesEXT", "DeleteQueriesANGLE", nullptr } },
            { (PRFuncPtr*) &mSymbols.fEndQuery, { "EndQueryEXT", "EndQueryANGLE", nullptr } },
            { (PRFuncPtr*) &mSymbols.fGetQueryiv, { "GetQueryivEXT", "GetQueryivANGLE", nullptr } },
            { (PRFuncPtr*) &mSymbols.fGetQueryObjectuiv, { "GetQueryObjectuivEXT", "GetQueryObjectuivANGLE", nullptr } },
            { (PRFuncPtr*) &mSymbols.fIsQuery, { "IsQueryEXT", "IsQueryANGLE", nullptr } },
            END_SYMBOLS
        };
        if (!fnLoadFeatureByCore(coreSymbols, extSymbols, GLFeature::query_objects)) {
            MarkUnsupported(GLFeature::get_query_object_i64v);
            MarkUnsupported(GLFeature::get_query_object_iv);
            MarkUnsupported(GLFeature::occlusion_query);
            MarkUnsupported(GLFeature::occlusion_query_boolean);
            MarkUnsupported(GLFeature::occlusion_query2);
        }
    }

    if (IsSupported(GLFeature::get_query_object_i64v)) {
        const SymLoadStruct coreSymbols[] = {
            { (PRFuncPtr*) &mSymbols.fGetQueryObjecti64v, { "GetQueryObjecti64v", nullptr } },
            { (PRFuncPtr*) &mSymbols.fGetQueryObjectui64v, { "GetQueryObjectui64v", nullptr } },
            END_SYMBOLS
        };
        const SymLoadStruct extSymbols[] = {
            { (PRFuncPtr*) &mSymbols.fGetQueryObjecti64v, { "GetQueryObjecti64vEXT", "GetQueryObjecti64vANGLE", nullptr } },
            { (PRFuncPtr*) &mSymbols.fGetQueryObjectui64v, { "GetQueryObjectui64vEXT", "GetQueryObjectui64vANGLE", nullptr } },
            END_SYMBOLS
        };
        if (!fnLoadFeatureByCore(coreSymbols, extSymbols, GLFeature::get_query_object_i64v)) {
            MarkUnsupported(GLFeature::query_counter);
        }
    }

    if (IsSupported(GLFeature::get_query_object_iv)) {
        const SymLoadStruct coreSymbols[] = {
            { (PRFuncPtr*) &mSymbols.fGetQueryObjectiv, { "GetQueryObjectiv", nullptr } },
            END_SYMBOLS
        };
        const SymLoadStruct extSymbols[] = {
            { (PRFuncPtr*) &mSymbols.fGetQueryObjectiv, { "GetQueryObjectivEXT", "GetQueryObjectivANGLE", nullptr } },
            END_SYMBOLS
        };
        fnLoadFeatureByCore(coreSymbols, extSymbols, GLFeature::get_query_object_iv);
    }

    if (IsSupported(GLFeature::clear_buffers)) {
        const SymLoadStruct symbols[] = {
            { (PRFuncPtr*) &mSymbols.fClearBufferfi,  { "ClearBufferfi",  nullptr } },
            { (PRFuncPtr*) &mSymbols.fClearBufferfv,  { "ClearBufferfv",  nullptr } },
            { (PRFuncPtr*) &mSymbols.fClearBufferiv,  { "ClearBufferiv",  nullptr } },
            { (PRFuncPtr*) &mSymbols.fClearBufferuiv, { "ClearBufferuiv", nullptr } },
            END_SYMBOLS
        };
        fnLoadForFeature(symbols, GLFeature::clear_buffers);
    }

    if (IsSupported(GLFeature::copy_buffer)) {
        const SymLoadStruct symbols[] = {
            { (PRFuncPtr*) &mSymbols.fCopyBufferSubData, { "CopyBufferSubData", nullptr } },
            END_SYMBOLS
        };
        fnLoadForFeature(symbols, GLFeature::copy_buffer);
    }

    if (IsSupported(GLFeature::draw_buffers)) {
        const SymLoadStruct coreSymbols[] = {
            { (PRFuncPtr*) &mSymbols.fDrawBuffers, { "DrawBuffers", nullptr } },
            END_SYMBOLS
        };
        const SymLoadStruct extSymbols[] = {
            { (PRFuncPtr*) &mSymbols.fDrawBuffers, { "DrawBuffersARB", "DrawBuffersEXT", nullptr } },
            END_SYMBOLS
        };
        fnLoadFeatureByCore(coreSymbols, extSymbols, GLFeature::draw_buffers);
    }

    if (IsSupported(GLFeature::draw_range_elements)) {
        const SymLoadStruct coreSymbols[] = {
            { (PRFuncPtr*) &mSymbols.fDrawRangeElements, { "DrawRangeElements", nullptr } },
            END_SYMBOLS
        };
        const SymLoadStruct extSymbols[] = {
            { (PRFuncPtr*) &mSymbols.fDrawRangeElements, { "DrawRangeElementsEXT", nullptr } },
            END_SYMBOLS
        };
        fnLoadFeatureByCore(coreSymbols, extSymbols, GLFeature::draw_range_elements);
    }

    if (IsSupported(GLFeature::get_integer_indexed)) {
        const SymLoadStruct coreSymbols[] = {
            { (PRFuncPtr*) &mSymbols.fGetIntegeri_v, { "GetIntegeri_v", nullptr } },
            END_SYMBOLS
        };
        const SymLoadStruct extSymbols[] ={
            { (PRFuncPtr*) &mSymbols.fGetIntegeri_v, { "GetIntegerIndexedvEXT", nullptr } },
            END_SYMBOLS
        };
        fnLoadFeatureByCore(coreSymbols, extSymbols, GLFeature::get_integer_indexed);
    }

    if (IsSupported(GLFeature::get_integer64_indexed)) {
        const SymLoadStruct symbols[] = {
            { (PRFuncPtr*) &mSymbols.fGetInteger64i_v, { "GetInteger64i_v", nullptr } },
            END_SYMBOLS
        };
        fnLoadForFeature(symbols, GLFeature::get_integer64_indexed);
    }

    if (IsSupported(GLFeature::gpu_shader4)) {
        const SymLoadStruct symbols[] = {
            { (PRFuncPtr*) &mSymbols.fGetVertexAttribIiv, { "GetVertexAttribIiv", "GetVertexAttribIivEXT", nullptr } },
            { (PRFuncPtr*) &mSymbols.fGetVertexAttribIuiv, { "GetVertexAttribIuiv", "GetVertexAttribIuivEXT", nullptr } },
            { (PRFuncPtr*) &mSymbols.fVertexAttribI4i, { "VertexAttribI4i", "VertexAttribI4iEXT", nullptr } },
            { (PRFuncPtr*) &mSymbols.fVertexAttribI4iv, { "VertexAttribI4iv","VertexAttribI4ivEXT", nullptr } },
            { (PRFuncPtr*) &mSymbols.fVertexAttribI4ui, { "VertexAttribI4ui", "VertexAttribI4uiEXT", nullptr } },
            { (PRFuncPtr*) &mSymbols.fVertexAttribI4uiv, { "VertexAttribI4uiv", "VertexAttribI4uivEXT", nullptr } },
            { (PRFuncPtr*) &mSymbols.fVertexAttribIPointer, { "VertexAttribIPointer", "VertexAttribIPointerEXT", nullptr } },
            { (PRFuncPtr*) &mSymbols.fUniform1ui,  { "Uniform1ui", "Uniform1uiEXT", nullptr } },
            { (PRFuncPtr*) &mSymbols.fUniform2ui,  { "Uniform2ui", "Uniform2uiEXT", nullptr } },
            { (PRFuncPtr*) &mSymbols.fUniform3ui,  { "Uniform3ui", "Uniform3uiEXT", nullptr } },
            { (PRFuncPtr*) &mSymbols.fUniform4ui,  { "Uniform4ui", "Uniform4uiEXT", nullptr } },
            { (PRFuncPtr*) &mSymbols.fUniform1uiv, { "Uniform1uiv", "Uniform1uivEXT", nullptr } },
            { (PRFuncPtr*) &mSymbols.fUniform2uiv, { "Uniform2uiv", "Uniform2uivEXT", nullptr } },
            { (PRFuncPtr*) &mSymbols.fUniform3uiv, { "Uniform3uiv", "Uniform3uivEXT", nullptr } },
            { (PRFuncPtr*) &mSymbols.fUniform4uiv, { "Uniform4uiv", "Uniform4uivEXT", nullptr } },
            { (PRFuncPtr*) &mSymbols.fGetFragDataLocation, { "GetFragDataLocation", "GetFragDataLocationEXT", nullptr } },
            { (PRFuncPtr*) &mSymbols.fGetUniformuiv, { "GetUniformuiv", "GetUniformuivEXT", nullptr } },
            END_SYMBOLS
        };
        fnLoadForFeature(symbols, GLFeature::gpu_shader4);
    }

    if (IsSupported(GLFeature::map_buffer_range)) {
        const SymLoadStruct symbols[] = {
            { (PRFuncPtr*) &mSymbols.fMapBufferRange, { "MapBufferRange", nullptr } },
            { (PRFuncPtr*) &mSymbols.fFlushMappedBufferRange, { "FlushMappedBufferRange", nullptr } },
            { (PRFuncPtr*) &mSymbols.fUnmapBuffer, { "UnmapBuffer", nullptr } },
            END_SYMBOLS
        };
        fnLoadForFeature(symbols, GLFeature::map_buffer_range);
    }

    if (IsSupported(GLFeature::texture_3D)) {
        const SymLoadStruct coreSymbols[] = {
            { (PRFuncPtr*) &mSymbols.fTexImage3D, { "TexImage3D", nullptr } },
            { (PRFuncPtr*) &mSymbols.fTexSubImage3D, { "TexSubImage3D", nullptr } },
            END_SYMBOLS
        };
        const SymLoadStruct extSymbols[] = {
            { (PRFuncPtr*) &mSymbols.fTexSubImage3D, { "TexSubImage3DEXT", "TexSubImage3DOES", nullptr } },
            END_SYMBOLS
        };
        fnLoadFeatureByCore(coreSymbols, extSymbols, GLFeature::texture_3D);
    }

    if (IsSupported(GLFeature::texture_3D_compressed)) {
        const SymLoadStruct coreSymbols[] = {
            { (PRFuncPtr*) &mSymbols.fCompressedTexImage3D, { "CompressedTexImage3D", nullptr } },
            { (PRFuncPtr*) &mSymbols.fCompressedTexSubImage3D, { "CompressedTexSubImage3D", nullptr } },
            END_SYMBOLS
        };
        const SymLoadStruct extSymbols[] = {
            { (PRFuncPtr*) &mSymbols.fCompressedTexImage3D, { "CompressedTexImage3DARB", "CompressedTexImage3DOES", nullptr } },
            { (PRFuncPtr*) &mSymbols.fCompressedTexSubImage3D, { "CompressedTexSubImage3DARB", "CompressedTexSubImage3DOES", nullptr } },
            END_SYMBOLS
        };
        fnLoadFeatureByCore(coreSymbols, extSymbols, GLFeature::texture_3D_compressed);
    }

    if (IsSupported(GLFeature::texture_3D_copy)) {
        const SymLoadStruct coreSymbols[] = {
            { (PRFuncPtr*) &mSymbols.fCopyTexSubImage3D, { "CopyTexSubImage3D", nullptr } },
            END_SYMBOLS
        };
        const SymLoadStruct extSymbols[] = {
            { (PRFuncPtr*) &mSymbols.fCopyTexSubImage3D, { "CopyTexSubImage3DEXT", "CopyTexSubImage3DOES", nullptr } },
            END_SYMBOLS
        };
        fnLoadFeatureByCore(coreSymbols, extSymbols, GLFeature::texture_3D_copy);
    }

    if (IsSupported(GLFeature::uniform_buffer_object)) {
        // Note: Don't query for glGetActiveUniformName because it is not
        // supported by GL ES 3.
        const SymLoadStruct symbols[] = {
            { (PRFuncPtr*) &mSymbols.fGetUniformIndices, { "GetUniformIndices", nullptr } },
            { (PRFuncPtr*) &mSymbols.fGetActiveUniformsiv, { "GetActiveUniformsiv", nullptr } },
            { (PRFuncPtr*) &mSymbols.fGetUniformBlockIndex, { "GetUniformBlockIndex", nullptr } },
            { (PRFuncPtr*) &mSymbols.fGetActiveUniformBlockiv, { "GetActiveUniformBlockiv", nullptr } },
            { (PRFuncPtr*) &mSymbols.fGetActiveUniformBlockName, { "GetActiveUniformBlockName", nullptr } },
            { (PRFuncPtr*) &mSymbols.fUniformBlockBinding, { "UniformBlockBinding", nullptr } },
            END_SYMBOLS
        };
        fnLoadForFeature(symbols, GLFeature::uniform_buffer_object);
    }

    if (IsSupported(GLFeature::uniform_matrix_nonsquare)) {
        const SymLoadStruct symbols[] = {
            { (PRFuncPtr*) &mSymbols.fUniformMatrix2x3fv, { "UniformMatrix2x3fv", nullptr } },
            { (PRFuncPtr*) &mSymbols.fUniformMatrix2x4fv, { "UniformMatrix2x4fv", nullptr } },
            { (PRFuncPtr*) &mSymbols.fUniformMatrix3x2fv, { "UniformMatrix3x2fv", nullptr } },
            { (PRFuncPtr*) &mSymbols.fUniformMatrix3x4fv, { "UniformMatrix3x4fv", nullptr } },
            { (PRFuncPtr*) &mSymbols.fUniformMatrix4x2fv, { "UniformMatrix4x2fv", nullptr } },
            { (PRFuncPtr*) &mSymbols.fUniformMatrix4x3fv, { "UniformMatrix4x3fv", nullptr } },
            END_SYMBOLS
        };
        fnLoadForFeature(symbols, GLFeature::uniform_matrix_nonsquare);
    }

    if (IsSupported(GLFeature::internalformat_query)) {
        const SymLoadStruct symbols[] = {
            CORE_SYMBOL(GetInternalformativ),
            END_SYMBOLS
        };
        fnLoadForFeature(symbols, GLFeature::internalformat_query);
    }

    if (IsSupported(GLFeature::invalidate_framebuffer)) {
        const SymLoadStruct symbols[] = {
            { (PRFuncPtr*) &mSymbols.fInvalidateFramebuffer,    { "InvalidateFramebuffer", nullptr } },
            { (PRFuncPtr*) &mSymbols.fInvalidateSubFramebuffer, { "InvalidateSubFramebuffer", nullptr } },
            END_SYMBOLS
        };
        fnLoadForFeature(symbols, GLFeature::invalidate_framebuffer);
    }

    if (IsSupported(GLFeature::prim_restart)) {
        const SymLoadStruct symbols[] = {
            { (PRFuncPtr*) &mSymbols.fPrimitiveRestartIndex,    { "PrimitiveRestartIndex", "PrimitiveRestartIndexNV", nullptr } },
            END_SYMBOLS
        };
        fnLoadForFeature(symbols, GLFeature::prim_restart);
    }

    if (IsExtensionSupported(KHR_debug)) {
        const SymLoadStruct symbols[] = {
            { (PRFuncPtr*) &mSymbols.fDebugMessageControl,  { "DebugMessageControl",  "DebugMessageControlKHR",  nullptr } },
            { (PRFuncPtr*) &mSymbols.fDebugMessageInsert,   { "DebugMessageInsert",   "DebugMessageInsertKHR",   nullptr } },
            { (PRFuncPtr*) &mSymbols.fDebugMessageCallback, { "DebugMessageCallback", "DebugMessageCallbackKHR", nullptr } },
            { (PRFuncPtr*) &mSymbols.fGetDebugMessageLog,   { "GetDebugMessageLog",   "GetDebugMessageLogKHR",   nullptr } },
            { (PRFuncPtr*) &mSymbols.fGetPointerv,          { "GetPointerv",          "GetPointervKHR",          nullptr } },
            { (PRFuncPtr*) &mSymbols.fPushDebugGroup,       { "PushDebugGroup",       "PushDebugGroupKHR",       nullptr } },
            { (PRFuncPtr*) &mSymbols.fPopDebugGroup,        { "PopDebugGroup",        "PopDebugGroupKHR",        nullptr } },
            { (PRFuncPtr*) &mSymbols.fObjectLabel,          { "ObjectLabel",          "ObjectLabelKHR",          nullptr } },
            { (PRFuncPtr*) &mSymbols.fGetObjectLabel,       { "GetObjectLabel",       "GetObjectLabelKHR",       nullptr } },
            { (PRFuncPtr*) &mSymbols.fObjectPtrLabel,       { "ObjectPtrLabel",       "ObjectPtrLabelKHR",       nullptr } },
            { (PRFuncPtr*) &mSymbols.fGetObjectPtrLabel,    { "GetObjectPtrLabel",    "GetObjectPtrLabelKHR",    nullptr } },
            END_SYMBOLS
        };
        fnLoadForExt(symbols, KHR_debug);
    }

    if (IsExtensionSupported(NV_fence)) {
        const SymLoadStruct symbols[] = {
            { (PRFuncPtr*) &mSymbols.fGenFences,    { "GenFencesNV",    nullptr } },
            { (PRFuncPtr*) &mSymbols.fDeleteFences, { "DeleteFencesNV", nullptr } },
            { (PRFuncPtr*) &mSymbols.fSetFence,     { "SetFenceNV",     nullptr } },
            { (PRFuncPtr*) &mSymbols.fTestFence,    { "TestFenceNV",    nullptr } },
            { (PRFuncPtr*) &mSymbols.fFinishFence,  { "FinishFenceNV",  nullptr } },
            { (PRFuncPtr*) &mSymbols.fIsFence,      { "IsFenceNV",      nullptr } },
            { (PRFuncPtr*) &mSymbols.fGetFenceiv,   { "GetFenceivNV",   nullptr } },
            END_SYMBOLS
        };
        fnLoadForExt(symbols, NV_fence);
    }

    if (IsExtensionSupported(NV_texture_barrier)) {
        const SymLoadStruct symbols[] = {
            { (PRFuncPtr*) &mSymbols.fTextureBarrier, { "TextureBarrierNV", nullptr } },
            END_SYMBOLS
        };
        fnLoadForExt(symbols, NV_texture_barrier);
    }

    if (IsSupported(GLFeature::read_buffer)) {
        const SymLoadStruct symbols[] = {
            { (PRFuncPtr*) &mSymbols.fReadBuffer, { "ReadBuffer", nullptr } },
            END_SYMBOLS
        };
        fnLoadForFeature(symbols, GLFeature::read_buffer);
    }

    if (IsExtensionSupported(APPLE_framebuffer_multisample)) {
        const SymLoadStruct symbols[] = {
            { (PRFuncPtr*) &mSymbols.fResolveMultisampleFramebufferAPPLE, { "ResolveMultisampleFramebufferAPPLE", nullptr } },
            END_SYMBOLS
        };
        fnLoadForExt(symbols, APPLE_framebuffer_multisample);
    }

    // Load developer symbols, don't fail if we can't find them.
    const SymLoadStruct devSymbols[] = {
            { (PRFuncPtr*) &mSymbols.fGetTexImage, { "GetTexImage", nullptr } },
            { (PRFuncPtr*) &mSymbols.fGetTexLevelParameteriv, { "GetTexLevelParameteriv", nullptr } },
            END_SYMBOLS
    };
    const bool warnOnFailures = ShouldSpew();
    LoadSymbols(devSymbols, trygl, prefix, warnOnFailures);
}

#undef CORE_SYMBOL
#undef CORE_EXT_SYMBOL2
#undef EXT_SYMBOL2
#undef EXT_SYMBOL3
#undef END_SYMBOLS

void
GLContext::DebugCallback(GLenum source,
                         GLenum type,
                         GLuint id,
                         GLenum severity,
                         GLsizei length,
                         const GLchar* message)
{
    nsAutoCString sourceStr;
    switch (source) {
    case LOCAL_GL_DEBUG_SOURCE_API:
        sourceStr = NS_LITERAL_CSTRING("SOURCE_API");
        break;
    case LOCAL_GL_DEBUG_SOURCE_WINDOW_SYSTEM:
        sourceStr = NS_LITERAL_CSTRING("SOURCE_WINDOW_SYSTEM");
        break;
    case LOCAL_GL_DEBUG_SOURCE_SHADER_COMPILER:
        sourceStr = NS_LITERAL_CSTRING("SOURCE_SHADER_COMPILER");
        break;
    case LOCAL_GL_DEBUG_SOURCE_THIRD_PARTY:
        sourceStr = NS_LITERAL_CSTRING("SOURCE_THIRD_PARTY");
        break;
    case LOCAL_GL_DEBUG_SOURCE_APPLICATION:
        sourceStr = NS_LITERAL_CSTRING("SOURCE_APPLICATION");
        break;
    case LOCAL_GL_DEBUG_SOURCE_OTHER:
        sourceStr = NS_LITERAL_CSTRING("SOURCE_OTHER");
        break;
    default:
        sourceStr = nsPrintfCString("<source 0x%04x>", source);
        break;
    }

    nsAutoCString typeStr;
    switch (type) {
    case LOCAL_GL_DEBUG_TYPE_ERROR:
        typeStr = NS_LITERAL_CSTRING("TYPE_ERROR");
        break;
    case LOCAL_GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR:
        typeStr = NS_LITERAL_CSTRING("TYPE_DEPRECATED_BEHAVIOR");
        break;
    case LOCAL_GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR:
        typeStr = NS_LITERAL_CSTRING("TYPE_UNDEFINED_BEHAVIOR");
        break;
    case LOCAL_GL_DEBUG_TYPE_PORTABILITY:
        typeStr = NS_LITERAL_CSTRING("TYPE_PORTABILITY");
        break;
    case LOCAL_GL_DEBUG_TYPE_PERFORMANCE:
        typeStr = NS_LITERAL_CSTRING("TYPE_PERFORMANCE");
        break;
    case LOCAL_GL_DEBUG_TYPE_OTHER:
        typeStr = NS_LITERAL_CSTRING("TYPE_OTHER");
        break;
    case LOCAL_GL_DEBUG_TYPE_MARKER:
        typeStr = NS_LITERAL_CSTRING("TYPE_MARKER");
        break;
    default:
        typeStr = nsPrintfCString("<type 0x%04x>", type);
        break;
    }

    nsAutoCString sevStr;
    switch (severity) {
    case LOCAL_GL_DEBUG_SEVERITY_HIGH:
        sevStr = NS_LITERAL_CSTRING("SEVERITY_HIGH");
        break;
    case LOCAL_GL_DEBUG_SEVERITY_MEDIUM:
        sevStr = NS_LITERAL_CSTRING("SEVERITY_MEDIUM");
        break;
    case LOCAL_GL_DEBUG_SEVERITY_LOW:
        sevStr = NS_LITERAL_CSTRING("SEVERITY_LOW");
        break;
    case LOCAL_GL_DEBUG_SEVERITY_NOTIFICATION:
        sevStr = NS_LITERAL_CSTRING("SEVERITY_NOTIFICATION");
        break;
    default:
        sevStr = nsPrintfCString("<severity 0x%04x>", severity);
        break;
    }

    printf_stderr("[KHR_debug: 0x%" PRIxPTR "] ID %u: %s, %s, %s:\n    %s\n",
                  (uintptr_t)this,
                  id,
                  sourceStr.BeginReading(),
                  typeStr.BeginReading(),
                  sevStr.BeginReading(),
                  message);
}

void
GLContext::InitExtensions()
{
    MOZ_ASSERT(IsCurrent());

    std::vector<nsCString> driverExtensionList;

    if (IsFeatureProvidedByCoreSymbols(GLFeature::get_string_indexed)) {
        GLuint count = 0;
        GetUIntegerv(LOCAL_GL_NUM_EXTENSIONS, &count);
        for (GLuint i = 0; i < count; i++) {
            // This is UTF-8.
            const char* rawExt = (const char*)fGetStringi(LOCAL_GL_EXTENSIONS, i);

            // We CANNOT use nsDependentCString here, because the spec doesn't guarantee
            // that the pointers returned are different, only that their contents are.
            // On Flame, each of these index string queries returns the same address.
            driverExtensionList.push_back(nsCString(rawExt));
        }
    } else {
        MOZ_ALWAYS_TRUE(!fGetError());
        const char* rawExts = (const char*)fGetString(LOCAL_GL_EXTENSIONS);
        MOZ_ALWAYS_TRUE(!fGetError());

        if (rawExts) {
            nsDependentCString exts(rawExts);
            SplitByChar(exts, ' ', &driverExtensionList);
        }
    }

    const bool shouldDumpExts = ShouldDumpExts();
    if (shouldDumpExts) {
        printf_stderr("%i GL driver extensions: (*: recognized)\n",
                      (uint32_t)driverExtensionList.size());
    }

    MarkBitfieldByStrings(driverExtensionList, shouldDumpExts, sExtensionNames,
                          &mAvailableExtensions);

    if (WorkAroundDriverBugs()) {
        if (Vendor() == GLVendor::Qualcomm) {
            // Some Adreno drivers do not report GL_OES_EGL_sync, but they really do support it.
            MarkExtensionSupported(OES_EGL_sync);
        }

        if (Vendor() == GLVendor::ATI) {
            // ATI drivers say this extension exists, but we can't
            // actually find the EGLImageTargetRenderbufferStorageOES
            // extension function pointer in the drivers.
            MarkExtensionUnsupported(OES_EGL_image);
        }

        if (Vendor() == GLVendor::Imagination &&
            Renderer() == GLRenderer::SGX540)
        {
            // Bug 980048
            MarkExtensionUnsupported(OES_EGL_sync);
        }

#ifdef MOZ_WIDGET_ANDROID
        if (Vendor() == GLVendor::Imagination &&
            Renderer() == GLRenderer::SGX544MP &&
            AndroidBridge::Bridge()->GetAPIVersion() < 21)
        {
            // Bug 1026404
            MarkExtensionUnsupported(OES_EGL_image);
            MarkExtensionUnsupported(OES_EGL_image_external);
        }
#endif

        if (Vendor() == GLVendor::ARM &&
            (Renderer() == GLRenderer::Mali400MP ||
             Renderer() == GLRenderer::Mali450MP))
        {
            // Bug 1264505
            MarkExtensionUnsupported(OES_EGL_image_external);
        }

        if (Renderer() == GLRenderer::AndroidEmulator) {
            // the Android emulator, which we use to run B2G reftests on,
            // doesn't expose the OES_rgb8_rgba8 extension, but it seems to
            // support it (tautologically, as it only runs on desktop GL).
            MarkExtensionSupported(OES_rgb8_rgba8);
            // there seems to be a similar issue for EXT_texture_format_BGRA8888
            // on the Android 4.3 emulator
            MarkExtensionSupported(EXT_texture_format_BGRA8888);
        }

        if (Vendor() == GLVendor::VMware &&
            Renderer() == GLRenderer::GalliumLlvmpipe)
        {
            // The llvmpipe driver that is used on linux try servers appears to have
            // buggy support for s3tc/dxt1 compressed textures.
            // See Bug 975824.
            MarkExtensionUnsupported(EXT_texture_compression_s3tc);
            MarkExtensionUnsupported(EXT_texture_compression_dxt1);
            MarkExtensionUnsupported(ANGLE_texture_compression_dxt3);
            MarkExtensionUnsupported(ANGLE_texture_compression_dxt5);
        }

#ifdef XP_MACOSX
        // Bug 1009642: On OSX Mavericks (10.9), the driver for Intel HD
        // 3000 appears to be buggy WRT updating sub-images of S3TC
        // textures with glCompressedTexSubImage2D. Works on Intel HD 4000
        // and Intel HD 5000/Iris that I tested.
        // Bug 1124996: Appears to be the same on OSX Yosemite (10.10)
        if (nsCocoaFeatures::macOSVersionMajor() == 10 &&
            nsCocoaFeatures::macOSVersionMinor() >= 9 &&
            Renderer() == GLRenderer::IntelHD3000)
        {
            MarkExtensionUnsupported(EXT_texture_compression_s3tc);
        }
#endif
    }

    if (shouldDumpExts) {
        printf_stderr("\nActivated extensions:\n");

        for (size_t i = 0; i < mAvailableExtensions.size(); i++) {
            if (!mAvailableExtensions[i])
                continue;

            const char* ext = sExtensionNames[i];
            printf_stderr("[%i] %s\n", (uint32_t)i, ext);
        }
    }
}

void
GLContext::PlatformStartup()
{
    RegisterStrongMemoryReporter(new GfxTexturesReporter());
}

// Common code for checking for both GL extensions and GLX extensions.
bool
GLContext::ListHasExtension(const GLubyte* extensions, const char* extension)
{
    // fix bug 612572 - we were crashing as we were calling this function with extensions==null
    if (extensions == nullptr || extension == nullptr)
        return false;

    const GLubyte* start;
    GLubyte* where;
    GLubyte* terminator;

    /* Extension names should not have spaces. */
    where = (GLubyte*) strchr(extension, ' ');
    if (where || *extension == '\0')
        return false;

    /*
     * It takes a bit of care to be fool-proof about parsing the
     * OpenGL extensions string. Don't be fooled by sub-strings,
     * etc.
     */
    start = extensions;
    for (;;) {
        where = (GLubyte*) strstr((const char*) start, extension);
        if (!where) {
            break;
        }
        terminator = where + strlen(extension);
        if (where == start || *(where - 1) == ' ') {
            if (*terminator == ' ' || *terminator == '\0') {
                return true;
            }
        }
        start = terminator;
    }
    return false;
}

GLFormats
GLContext::ChooseGLFormats(const SurfaceCaps& caps) const
{
    GLFormats formats;

    // If we're on ES2 hardware and we have an explicit request for 16 bits of color or less
    // OR we don't support full 8-bit color, return a 4444 or 565 format.
    bool bpp16 = caps.bpp16;
    if (IsGLES()) {
        if (!IsExtensionSupported(OES_rgb8_rgba8))
            bpp16 = true;
    } else {
        // RGB565 is uncommon on desktop, requiring ARB_ES2_compatibility.
        // Since it's also vanishingly useless there, let's not support it.
        bpp16 = false;
    }

    if (bpp16) {
        MOZ_ASSERT(IsGLES());
        if (caps.alpha) {
            formats.color_texInternalFormat = LOCAL_GL_RGBA;
            formats.color_texFormat = LOCAL_GL_RGBA;
            formats.color_texType   = LOCAL_GL_UNSIGNED_SHORT_4_4_4_4;
            formats.color_rbFormat  = LOCAL_GL_RGBA4;
        } else {
            formats.color_texInternalFormat = LOCAL_GL_RGB;
            formats.color_texFormat = LOCAL_GL_RGB;
            formats.color_texType   = LOCAL_GL_UNSIGNED_SHORT_5_6_5;
            formats.color_rbFormat  = LOCAL_GL_RGB565;
        }
    } else {
        formats.color_texType = LOCAL_GL_UNSIGNED_BYTE;

        if (caps.alpha) {
            formats.color_texInternalFormat = IsGLES() ? LOCAL_GL_RGBA : LOCAL_GL_RGBA8;
            formats.color_texFormat = LOCAL_GL_RGBA;
            formats.color_rbFormat  = LOCAL_GL_RGBA8;
        } else {
            formats.color_texInternalFormat = IsGLES() ? LOCAL_GL_RGB : LOCAL_GL_RGB8;
            formats.color_texFormat = LOCAL_GL_RGB;
            formats.color_rbFormat  = LOCAL_GL_RGB8;
        }
    }

    uint32_t msaaLevel = gfxPrefs::MSAALevel();
    GLsizei samples = msaaLevel * msaaLevel;
    samples = std::min(samples, mMaxSamples);

    // Bug 778765.
    if (WorkAroundDriverBugs() && samples == 1) {
        samples = 0;
    }
    formats.samples = samples;


    // Be clear that these are 0 if unavailable.
    formats.depthStencil = 0;
    if (IsSupported(GLFeature::packed_depth_stencil)) {
        formats.depthStencil = LOCAL_GL_DEPTH24_STENCIL8;
    }

    formats.depth = 0;
    if (IsGLES()) {
        if (IsExtensionSupported(OES_depth24)) {
            formats.depth = LOCAL_GL_DEPTH_COMPONENT24;
        } else {
            formats.depth = LOCAL_GL_DEPTH_COMPONENT16;
        }
    } else {
        formats.depth = LOCAL_GL_DEPTH_COMPONENT24;
    }

    formats.stencil = LOCAL_GL_STENCIL_INDEX8;

    return formats;
}

bool
GLContext::IsFramebufferComplete(GLuint fb, GLenum* pStatus)
{
    MOZ_ASSERT(fb);

    ScopedBindFramebuffer autoFB(this, fb);
    MOZ_ASSERT(fIsFramebuffer(fb));

    GLenum status = fCheckFramebufferStatus(LOCAL_GL_FRAMEBUFFER);
    if (pStatus)
        *pStatus = status;

    return status == LOCAL_GL_FRAMEBUFFER_COMPLETE;
}

void
GLContext::AttachBuffersToFB(GLuint colorTex, GLuint colorRB,
                             GLuint depthRB, GLuint stencilRB,
                             GLuint fb, GLenum target)
{
    MOZ_ASSERT(fb);
    MOZ_ASSERT( !(colorTex && colorRB) );

    ScopedBindFramebuffer autoFB(this, fb);
    MOZ_ASSERT(fIsFramebuffer(fb)); // It only counts after being bound.

    if (colorTex) {
        MOZ_ASSERT(fIsTexture(colorTex));
        MOZ_ASSERT(target == LOCAL_GL_TEXTURE_2D ||
                   target == LOCAL_GL_TEXTURE_RECTANGLE_ARB);
        fFramebufferTexture2D(LOCAL_GL_FRAMEBUFFER,
                              LOCAL_GL_COLOR_ATTACHMENT0,
                              target,
                              colorTex,
                              0);
    } else if (colorRB) {
        // On the Android 4.3 emulator, IsRenderbuffer may return false incorrectly.
        MOZ_ASSERT_IF(Renderer() != GLRenderer::AndroidEmulator, fIsRenderbuffer(colorRB));
        fFramebufferRenderbuffer(LOCAL_GL_FRAMEBUFFER,
                                 LOCAL_GL_COLOR_ATTACHMENT0,
                                 LOCAL_GL_RENDERBUFFER,
                                 colorRB);
    }

    if (depthRB) {
        MOZ_ASSERT_IF(Renderer() != GLRenderer::AndroidEmulator, fIsRenderbuffer(depthRB));
        fFramebufferRenderbuffer(LOCAL_GL_FRAMEBUFFER,
                                 LOCAL_GL_DEPTH_ATTACHMENT,
                                 LOCAL_GL_RENDERBUFFER,
                                 depthRB);
    }

    if (stencilRB) {
        MOZ_ASSERT_IF(Renderer() != GLRenderer::AndroidEmulator, fIsRenderbuffer(stencilRB));
        fFramebufferRenderbuffer(LOCAL_GL_FRAMEBUFFER,
                                 LOCAL_GL_STENCIL_ATTACHMENT,
                                 LOCAL_GL_RENDERBUFFER,
                                 stencilRB);
    }
}

bool
GLContext::AssembleOffscreenFBs(const GLuint colorMSRB,
                                const GLuint depthRB,
                                const GLuint stencilRB,
                                const GLuint texture,
                                GLuint* drawFB_out,
                                GLuint* readFB_out)
{
    if (!colorMSRB && !texture) {
        MOZ_ASSERT(!depthRB && !stencilRB);

        if (drawFB_out)
            *drawFB_out = 0;
        if (readFB_out)
            *readFB_out = 0;

        return true;
    }

    ScopedBindFramebuffer autoFB(this);

    GLuint drawFB = 0;
    GLuint readFB = 0;

    if (texture) {
        readFB = 0;
        fGenFramebuffers(1, &readFB);
        BindFB(readFB);
        fFramebufferTexture2D(LOCAL_GL_FRAMEBUFFER,
                              LOCAL_GL_COLOR_ATTACHMENT0,
                              LOCAL_GL_TEXTURE_2D,
                              texture,
                              0);
    }

    if (colorMSRB) {
        drawFB = 0;
        fGenFramebuffers(1, &drawFB);
        BindFB(drawFB);
        fFramebufferRenderbuffer(LOCAL_GL_FRAMEBUFFER,
                                 LOCAL_GL_COLOR_ATTACHMENT0,
                                 LOCAL_GL_RENDERBUFFER,
                                 colorMSRB);
    } else {
        drawFB = readFB;
    }
    MOZ_ASSERT(GetFB() == drawFB);

    if (depthRB) {
        fFramebufferRenderbuffer(LOCAL_GL_FRAMEBUFFER,
                                 LOCAL_GL_DEPTH_ATTACHMENT,
                                 LOCAL_GL_RENDERBUFFER,
                                 depthRB);
    }

    if (stencilRB) {
        fFramebufferRenderbuffer(LOCAL_GL_FRAMEBUFFER,
                                 LOCAL_GL_STENCIL_ATTACHMENT,
                                 LOCAL_GL_RENDERBUFFER,
                                 stencilRB);
    }

    // We should be all resized.  Check for framebuffer completeness.
    GLenum status;
    bool isComplete = true;

    if (!IsFramebufferComplete(drawFB, &status)) {
        NS_WARNING("DrawFBO: Incomplete");
  #ifdef MOZ_GL_DEBUG
        if (ShouldSpew()) {
            printf_stderr("Framebuffer status: %X\n", status);
        }
  #endif
        isComplete = false;
    }

    if (!IsFramebufferComplete(readFB, &status)) {
        NS_WARNING("ReadFBO: Incomplete");
  #ifdef MOZ_GL_DEBUG
        if (ShouldSpew()) {
            printf_stderr("Framebuffer status: %X\n", status);
        }
  #endif
        isComplete = false;
    }

    if (drawFB_out) {
        *drawFB_out = drawFB;
    } else if (drawFB) {
        NS_RUNTIMEABORT("drawFB created when not requested!");
    }

    if (readFB_out) {
        *readFB_out = readFB;
    } else if (readFB) {
        NS_RUNTIMEABORT("readFB created when not requested!");
    }

    return isComplete;
}


void
GLContext::ClearSafely()
{
    // bug 659349 --- we must be very careful here: clearing a GL framebuffer is nontrivial, relies on a lot of state,
    // and in the case of the backbuffer of a WebGL context, state is exposed to scripts.
    //
    // The code here is taken from WebGLContext::ForceClearFramebufferWithDefaultValues, but I didn't find a good way of
    // sharing code with it. WebGL's code is somewhat performance-critical as it is typically called on every frame, so
    // WebGL keeps track of GL state to avoid having to query it everytime, and also tries to only do work for actually
    // present buffers (e.g. stencil buffer). Doing that here seems like premature optimization,
    // as ClearSafely() is called only when e.g. a canvas is resized, not on every animation frame.

    realGLboolean scissorTestEnabled;
    realGLboolean ditherEnabled;
    realGLboolean colorWriteMask[4];
    realGLboolean depthWriteMask;
    GLint stencilWriteMaskFront, stencilWriteMaskBack;
    GLfloat colorClearValue[4];
    GLfloat depthClearValue;
    GLint stencilClearValue;

    // save current GL state
    fGetBooleanv(LOCAL_GL_SCISSOR_TEST, &scissorTestEnabled);
    fGetBooleanv(LOCAL_GL_DITHER, &ditherEnabled);
    fGetBooleanv(LOCAL_GL_COLOR_WRITEMASK, colorWriteMask);
    fGetBooleanv(LOCAL_GL_DEPTH_WRITEMASK, &depthWriteMask);
    fGetIntegerv(LOCAL_GL_STENCIL_WRITEMASK, &stencilWriteMaskFront);
    fGetIntegerv(LOCAL_GL_STENCIL_BACK_WRITEMASK, &stencilWriteMaskBack);
    fGetFloatv(LOCAL_GL_COLOR_CLEAR_VALUE, colorClearValue);
    fGetFloatv(LOCAL_GL_DEPTH_CLEAR_VALUE, &depthClearValue);
    fGetIntegerv(LOCAL_GL_STENCIL_CLEAR_VALUE, &stencilClearValue);

    // prepare GL state for clearing
    fDisable(LOCAL_GL_SCISSOR_TEST);
    fDisable(LOCAL_GL_DITHER);

    fColorMask(1, 1, 1, 1);
    fClearColor(0.f, 0.f, 0.f, 0.f);

    fDepthMask(1);
    fClearDepth(1.0f);

    fStencilMask(0xffffffff);
    fClearStencil(0);

    // do clear
    fClear(LOCAL_GL_COLOR_BUFFER_BIT |
           LOCAL_GL_DEPTH_BUFFER_BIT |
           LOCAL_GL_STENCIL_BUFFER_BIT);

    // restore GL state after clearing
    fColorMask(colorWriteMask[0],
               colorWriteMask[1],
               colorWriteMask[2],
               colorWriteMask[3]);
    fClearColor(colorClearValue[0],
                colorClearValue[1],
                colorClearValue[2],
                colorClearValue[3]);

    fDepthMask(depthWriteMask);
    fClearDepth(depthClearValue);

    fStencilMaskSeparate(LOCAL_GL_FRONT, stencilWriteMaskFront);
    fStencilMaskSeparate(LOCAL_GL_BACK, stencilWriteMaskBack);
    fClearStencil(stencilClearValue);

    if (ditherEnabled)
        fEnable(LOCAL_GL_DITHER);
    else
        fDisable(LOCAL_GL_DITHER);

    if (scissorTestEnabled)
        fEnable(LOCAL_GL_SCISSOR_TEST);
    else
        fDisable(LOCAL_GL_SCISSOR_TEST);

}

void
GLContext::MarkDestroyed()
{
    if (IsDestroyed())
        return;

    // Null these before they're naturally nulled after dtor, as we want GLContext to
    // still be alive in *their* dtors.
    mScreen = nullptr;
    mBlitHelper = nullptr;
    mReadTexImageHelper = nullptr;

    mIsDestroyed = true;
    mSymbols.Zero();
    if (MakeCurrent(true)) {
        mTexGarbageBin->GLContextTeardown();
    }
}

#ifdef MOZ_GL_DEBUG
/* static */ void
GLContext::AssertNotPassingStackBufferToTheGL(const void* ptr)
{
  int somethingOnTheStack;
  const void* someStackPtr = &somethingOnTheStack;
  const int page_bits = 12;
  intptr_t page = reinterpret_cast<uintptr_t>(ptr) >> page_bits;
  intptr_t someStackPage = reinterpret_cast<uintptr_t>(someStackPtr) >> page_bits;
  uintptr_t pageDistance = std::abs(page - someStackPage);

  // Explanation for the "distance <= 1" check here as opposed to just
  // an equality check.
  //
  // Here we assume that pages immediately adjacent to the someStackAddress page,
  // are also stack pages. That allows to catch the case where the calling frame put
  // a buffer on the stack, and we just crossed the page boundary. That is likely
  // to happen, precisely, when using stack arrays. I hit that specifically
  // with CompositorOGL::Initialize.
  //
  // In theory we could be unlucky and wrongly assert here. If that happens,
  // it will only affect debug builds, and looking at stacks we'll be able to
  // see that this assert is wrong and revert to the conservative and safe
  // approach of only asserting when address and someStackAddress are
  // on the same page.
  bool isStackAddress = pageDistance <= 1;
  MOZ_ASSERT(!isStackAddress,
             "Please don't pass stack arrays to the GL. "
             "Consider using HeapCopyOfStackArray. "
             "See bug 1005658.");
}

void
GLContext::CreatedProgram(GLContext* aOrigin, GLuint aName)
{
    mTrackedPrograms.AppendElement(NamedResource(aOrigin, aName));
}

void
GLContext::CreatedShader(GLContext* aOrigin, GLuint aName)
{
    mTrackedShaders.AppendElement(NamedResource(aOrigin, aName));
}

void
GLContext::CreatedBuffers(GLContext* aOrigin, GLsizei aCount, GLuint* aNames)
{
    for (GLsizei i = 0; i < aCount; ++i) {
        mTrackedBuffers.AppendElement(NamedResource(aOrigin, aNames[i]));
    }
}

void
GLContext::CreatedQueries(GLContext* aOrigin, GLsizei aCount, GLuint* aNames)
{
    for (GLsizei i = 0; i < aCount; ++i) {
        mTrackedQueries.AppendElement(NamedResource(aOrigin, aNames[i]));
    }
}

void
GLContext::CreatedTextures(GLContext* aOrigin, GLsizei aCount, GLuint* aNames)
{
    for (GLsizei i = 0; i < aCount; ++i) {
        mTrackedTextures.AppendElement(NamedResource(aOrigin, aNames[i]));
    }
}

void
GLContext::CreatedFramebuffers(GLContext* aOrigin, GLsizei aCount, GLuint* aNames)
{
    for (GLsizei i = 0; i < aCount; ++i) {
        mTrackedFramebuffers.AppendElement(NamedResource(aOrigin, aNames[i]));
    }
}

void
GLContext::CreatedRenderbuffers(GLContext* aOrigin, GLsizei aCount, GLuint* aNames)
{
    for (GLsizei i = 0; i < aCount; ++i) {
        mTrackedRenderbuffers.AppendElement(NamedResource(aOrigin, aNames[i]));
    }
}

static void
RemoveNamesFromArray(GLContext* aOrigin, GLsizei aCount, const GLuint* aNames, nsTArray<GLContext::NamedResource>& aArray)
{
    for (GLsizei j = 0; j < aCount; ++j) {
        GLuint name = aNames[j];
        // name 0 can be ignored
        if (name == 0)
            continue;

        for (uint32_t i = 0; i < aArray.Length(); ++i) {
            if (aArray[i].name == name) {
                aArray.RemoveElementAt(i);
                break;
            }
        }
    }
}

void
GLContext::DeletedProgram(GLContext* aOrigin, GLuint aName)
{
    RemoveNamesFromArray(aOrigin, 1, &aName, mTrackedPrograms);
}

void
GLContext::DeletedShader(GLContext* aOrigin, GLuint aName)
{
    RemoveNamesFromArray(aOrigin, 1, &aName, mTrackedShaders);
}

void
GLContext::DeletedBuffers(GLContext* aOrigin, GLsizei aCount, const GLuint* aNames)
{
    RemoveNamesFromArray(aOrigin, aCount, aNames, mTrackedBuffers);
}

void
GLContext::DeletedQueries(GLContext* aOrigin, GLsizei aCount, const GLuint* aNames)
{
    RemoveNamesFromArray(aOrigin, aCount, aNames, mTrackedQueries);
}

void
GLContext::DeletedTextures(GLContext* aOrigin, GLsizei aCount, const GLuint* aNames)
{
    RemoveNamesFromArray(aOrigin, aCount, aNames, mTrackedTextures);
}

void
GLContext::DeletedFramebuffers(GLContext* aOrigin, GLsizei aCount, const GLuint* aNames)
{
    RemoveNamesFromArray(aOrigin, aCount, aNames, mTrackedFramebuffers);
}

void
GLContext::DeletedRenderbuffers(GLContext* aOrigin, GLsizei aCount, const GLuint* aNames)
{
    RemoveNamesFromArray(aOrigin, aCount, aNames, mTrackedRenderbuffers);
}

static void
MarkContextDestroyedInArray(GLContext* aContext, nsTArray<GLContext::NamedResource>& aArray)
{
    for (uint32_t i = 0; i < aArray.Length(); ++i) {
        if (aArray[i].origin == aContext)
            aArray[i].originDeleted = true;
    }
}

void
GLContext::SharedContextDestroyed(GLContext* aChild)
{
    MarkContextDestroyedInArray(aChild, mTrackedPrograms);
    MarkContextDestroyedInArray(aChild, mTrackedShaders);
    MarkContextDestroyedInArray(aChild, mTrackedTextures);
    MarkContextDestroyedInArray(aChild, mTrackedFramebuffers);
    MarkContextDestroyedInArray(aChild, mTrackedRenderbuffers);
    MarkContextDestroyedInArray(aChild, mTrackedBuffers);
    MarkContextDestroyedInArray(aChild, mTrackedQueries);
}

static void
ReportArrayContents(const char* title, const nsTArray<GLContext::NamedResource>& aArray)
{
    if (aArray.Length() == 0)
        return;

    printf_stderr("%s:\n", title);

    nsTArray<GLContext::NamedResource> copy(aArray);
    copy.Sort();

    GLContext* lastContext = nullptr;
    for (uint32_t i = 0; i < copy.Length(); ++i) {
        if (lastContext != copy[i].origin) {
            if (lastContext)
                printf_stderr("\n");
            printf_stderr("  [%p - %s] ", copy[i].origin, copy[i].originDeleted ? "deleted" : "live");
            lastContext = copy[i].origin;
        }
        printf_stderr("%d ", copy[i].name);
    }
    printf_stderr("\n");
}

void
GLContext::ReportOutstandingNames()
{
    if (!ShouldSpew())
        return;

    printf_stderr("== GLContext %p Outstanding ==\n", this);

    ReportArrayContents("Outstanding Textures", mTrackedTextures);
    ReportArrayContents("Outstanding Buffers", mTrackedBuffers);
    ReportArrayContents("Outstanding Queries", mTrackedQueries);
    ReportArrayContents("Outstanding Programs", mTrackedPrograms);
    ReportArrayContents("Outstanding Shaders", mTrackedShaders);
    ReportArrayContents("Outstanding Framebuffers", mTrackedFramebuffers);
    ReportArrayContents("Outstanding Renderbuffers", mTrackedRenderbuffers);
}

#endif /* DEBUG */

void
GLContext::GuaranteeResolve()
{
    if (mScreen) {
        mScreen->AssureBlitted();
    }
    fFinish();
}

const gfx::IntSize&
GLContext::OffscreenSize() const
{
    MOZ_ASSERT(IsOffscreen());
    return mScreen->Size();
}

bool
GLContext::CreateScreenBufferImpl(const IntSize& size, const SurfaceCaps& caps)
{
    UniquePtr<GLScreenBuffer> newScreen = GLScreenBuffer::Create(this, size, caps);
    if (!newScreen)
        return false;

    if (!newScreen->Resize(size)) {
        return false;
    }

    // This will rebind to 0 (Screen) if needed when
    // it falls out of scope.
    ScopedBindFramebuffer autoFB(this);

    mScreen = Move(newScreen);

    return true;
}

bool
GLContext::ResizeScreenBuffer(const IntSize& size)
{
    if (!IsOffscreenSizeAllowed(size))
        return false;

    return mScreen->Resize(size);
}

void
GLContext::ForceDirtyScreen()
{
    ScopedBindFramebuffer autoFB(0);

    BeforeGLDrawCall();
    // no-op; just pretend we did something
    AfterGLDrawCall();
}

void
GLContext::CleanDirtyScreen()
{
    ScopedBindFramebuffer autoFB(0);

    BeforeGLReadCall();
    // no-op; we just want to make sure the Read FBO is updated if it needs to be
    AfterGLReadCall();
}

void
GLContext::EmptyTexGarbageBin()
{
    TexGarbageBin()->EmptyGarbage();
}

bool
GLContext::IsOffscreenSizeAllowed(const IntSize& aSize) const
{
    int32_t biggerDimension = std::max(aSize.width, aSize.height);
    int32_t maxAllowed = std::min(mMaxRenderbufferSize, mMaxTextureSize);
    return biggerDimension <= maxAllowed;
}

bool
GLContext::IsOwningThreadCurrent()
{
    return PlatformThread::CurrentId() == mOwningThreadId;
}

GLBlitHelper*
GLContext::BlitHelper()
{
    if (!mBlitHelper) {
        mBlitHelper.reset(new GLBlitHelper(this));
    }

    return mBlitHelper.get();
}

GLReadTexImageHelper*
GLContext::ReadTexImageHelper()
{
    if (!mReadTexImageHelper) {
        mReadTexImageHelper = MakeUnique<GLReadTexImageHelper>(this);
    }

    return mReadTexImageHelper.get();
}

void
GLContext::FlushIfHeavyGLCallsSinceLastFlush()
{
    if (!mHeavyGLCallsSinceLastFlush) {
        return;
    }
    MakeCurrent();
    fFlush();
}

/*static*/ bool
GLContext::ShouldDumpExts()
{
    return gfxEnv::GlDumpExtensions();
}

bool
DoesStringMatch(const char* aString, const char* aWantedString)
{
    if (!aString || !aWantedString)
        return false;

    const char* occurrence = strstr(aString, aWantedString);

    // aWanted not found
    if (!occurrence)
        return false;

    // aWantedString preceded by alpha character
    if (occurrence != aString && isalpha(*(occurrence-1)))
        return false;

    // aWantedVendor followed by alpha character
    const char* afterOccurrence = occurrence + strlen(aWantedString);
    if (isalpha(*afterOccurrence))
        return false;

    return true;
}

/*static*/ bool
GLContext::ShouldSpew()
{
    return gfxEnv::GlSpew();
}

void
SplitByChar(const nsACString& str, const char delim, std::vector<nsCString>* const out)
{
    uint32_t start = 0;
    while (true) {
        int32_t end = str.FindChar(' ', start);
        if (end == -1)
            break;

        uint32_t len = (uint32_t)end - start;
        nsDependentCSubstring substr(str, start, len);
        out->push_back(nsCString(substr));

        start = end + 1;
        continue;
    }

    nsDependentCSubstring substr(str, start);
    out->push_back(nsCString(substr));
}

void
GLContext::Readback(SharedSurface* src, gfx::DataSourceSurface* dest)
{
    MOZ_ASSERT(src && dest);
    MOZ_ASSERT(dest->GetSize() == src->mSize);
    MOZ_ASSERT(dest->GetFormat() == (src->mHasAlpha ? SurfaceFormat::B8G8R8A8
                                                    : SurfaceFormat::B8G8R8X8));

    MakeCurrent();

    SharedSurface* prev = GetLockedSurface();

    const bool needsSwap = src != prev;
    if (needsSwap) {
        if (prev)
            prev->UnlockProd();
        src->LockProd();
    }

    GLuint tempFB = 0;
    GLuint tempTex = 0;

    {
        ScopedBindFramebuffer autoFB(this);

        // We're consuming from the producer side, so which do we use?
        // Really, we just want a read-only lock, so ConsumerAcquire is the best match.
        src->ProducerReadAcquire();

        if (src->mAttachType == AttachmentType::Screen) {
            fBindFramebuffer(LOCAL_GL_FRAMEBUFFER, 0);
        } else {
            fGenFramebuffers(1, &tempFB);
            fBindFramebuffer(LOCAL_GL_FRAMEBUFFER, tempFB);

            switch (src->mAttachType) {
            case AttachmentType::GLTexture:
                fFramebufferTexture2D(LOCAL_GL_FRAMEBUFFER, LOCAL_GL_COLOR_ATTACHMENT0,
                                      src->ProdTextureTarget(), src->ProdTexture(), 0);
                break;
            case AttachmentType::GLRenderbuffer:
                fFramebufferRenderbuffer(LOCAL_GL_FRAMEBUFFER, LOCAL_GL_COLOR_ATTACHMENT0,
                                         LOCAL_GL_RENDERBUFFER, src->ProdRenderbuffer());
                break;
            default:
                MOZ_CRASH("GFX: bad `src->mAttachType`.");
            }

            DebugOnly<GLenum> status = fCheckFramebufferStatus(LOCAL_GL_FRAMEBUFFER);
            MOZ_ASSERT(status == LOCAL_GL_FRAMEBUFFER_COMPLETE);
        }

        if (src->NeedsIndirectReads()) {
            fGenTextures(1, &tempTex);
            {
                ScopedBindTexture autoTex(this, tempTex);

                GLenum format = src->mHasAlpha ? LOCAL_GL_RGBA
                                               : LOCAL_GL_RGB;
                auto width = src->mSize.width;
                auto height = src->mSize.height;
                fCopyTexImage2D(LOCAL_GL_TEXTURE_2D, 0, format, 0, 0, width,
                                height, 0);
            }

            fFramebufferTexture2D(LOCAL_GL_FRAMEBUFFER,
                                  LOCAL_GL_COLOR_ATTACHMENT0,
                                  LOCAL_GL_TEXTURE_2D, tempTex, 0);
        }

        ReadPixelsIntoDataSurface(this, dest);

        src->ProducerReadRelease();
    }

    if (tempFB)
        fDeleteFramebuffers(1, &tempFB);

    if (tempTex) {
        fDeleteTextures(1, &tempTex);
    }

    if (needsSwap) {
        src->UnlockProd();
        if (prev)
            prev->LockProd();
    }
}

// Do whatever tear-down is necessary after drawing to our offscreen FBO,
// if it's bound.
void
GLContext::AfterGLDrawCall()
{
    if (mScreen) {
        mScreen->AfterDrawCall();
    }
    mHeavyGLCallsSinceLastFlush = true;
}

// Do whatever setup is necessary to read from our offscreen FBO, if it's
// bound.
void
GLContext::BeforeGLReadCall()
{
    if (mScreen)
        mScreen->BeforeReadCall();
}

void
GLContext::fBindFramebuffer(GLenum target, GLuint framebuffer)
{
    if (!mScreen) {
        raw_fBindFramebuffer(target, framebuffer);
        return;
    }

    switch (target) {
        case LOCAL_GL_DRAW_FRAMEBUFFER_EXT:
            mScreen->BindDrawFB(framebuffer);
            return;

        case LOCAL_GL_READ_FRAMEBUFFER_EXT:
            mScreen->BindReadFB(framebuffer);
            return;

        case LOCAL_GL_FRAMEBUFFER:
            mScreen->BindFB(framebuffer);
            return;

        default:
            // Nothing we care about, likely an error.
            break;
    }

    raw_fBindFramebuffer(target, framebuffer);
}

void
GLContext::fCopyTexImage2D(GLenum target, GLint level, GLenum internalformat, GLint x,
                           GLint y, GLsizei width, GLsizei height, GLint border)
{
    if (!IsTextureSizeSafeToPassToDriver(target, width, height)) {
        // pass wrong values to cause the GL to generate GL_INVALID_VALUE.
        // See bug 737182 and the comment in IsTextureSizeSafeToPassToDriver.
        level = -1;
        width = -1;
        height = -1;
        border = -1;
    }

    BeforeGLReadCall();
    bool didCopyTexImage2D = false;
    if (mScreen) {
        didCopyTexImage2D = mScreen->CopyTexImage2D(target, level, internalformat, x,
                                                    y, width, height, border);
    }

    if (!didCopyTexImage2D) {
        raw_fCopyTexImage2D(target, level, internalformat, x, y, width, height,
                            border);
    }
    AfterGLReadCall();
}

void
GLContext::fGetIntegerv(GLenum pname, GLint* params)
{
    switch (pname) {
        // LOCAL_GL_FRAMEBUFFER_BINDING is equal to
        // LOCAL_GL_DRAW_FRAMEBUFFER_BINDING_EXT,
        // so we don't need two cases.
        case LOCAL_GL_DRAW_FRAMEBUFFER_BINDING_EXT:
            if (mScreen) {
                *params = mScreen->GetDrawFB();
            } else {
                raw_fGetIntegerv(pname, params);
            }
            break;

        case LOCAL_GL_READ_FRAMEBUFFER_BINDING_EXT:
            if (mScreen) {
                *params = mScreen->GetReadFB();
            } else {
                raw_fGetIntegerv(pname, params);
            }
            break;

        case LOCAL_GL_MAX_TEXTURE_SIZE:
            MOZ_ASSERT(mMaxTextureSize>0);
            *params = mMaxTextureSize;
            break;

        case LOCAL_GL_MAX_CUBE_MAP_TEXTURE_SIZE:
            MOZ_ASSERT(mMaxCubeMapTextureSize>0);
            *params = mMaxCubeMapTextureSize;
            break;

        case LOCAL_GL_MAX_RENDERBUFFER_SIZE:
            MOZ_ASSERT(mMaxRenderbufferSize>0);
            *params = mMaxRenderbufferSize;
            break;

        case LOCAL_GL_VIEWPORT:
            for (size_t i = 0; i < 4; i++) {
                params[i] = mViewportRect[i];
            }
            break;

        case LOCAL_GL_SCISSOR_BOX:
            for (size_t i = 0; i < 4; i++) {
                params[i] = mScissorRect[i];
            }
            break;

        default:
            raw_fGetIntegerv(pname, params);
            break;
    }
}

void
GLContext::fReadPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format,
                       GLenum type, GLvoid* pixels)
{
    BeforeGLReadCall();

    bool didReadPixels = false;
    if (mScreen) {
        didReadPixels = mScreen->ReadPixels(x, y, width, height, format, type, pixels);
    }

    if (!didReadPixels) {
        raw_fReadPixels(x, y, width, height, format, type, pixels);
    }

    AfterGLReadCall();

    // Check if GL is giving back 1.0 alpha for
    // RGBA reads to RGBA images from no-alpha buffers.
#ifdef XP_MACOSX
    if (WorkAroundDriverBugs() &&
        Vendor() == gl::GLVendor::NVIDIA &&
        format == LOCAL_GL_RGBA &&
        type == LOCAL_GL_UNSIGNED_BYTE &&
        !IsCoreProfile() &&
        width && height)
    {
        GLint alphaBits = 0;
        fGetIntegerv(LOCAL_GL_ALPHA_BITS, &alphaBits);
        if (!alphaBits) {
            const uint32_t alphaMask = 0xff000000;

            uint32_t* itr = (uint32_t*)pixels;
            uint32_t testPixel = *itr;
            if ((testPixel & alphaMask) != alphaMask) {
                // We need to set the alpha channel to 1.0 manually.
                uint32_t* itrEnd = itr + width*height;  // Stride is guaranteed to be width*4.

                for (; itr != itrEnd; itr++) {
                    *itr |= alphaMask;
                }
            }
        }
    }
#endif
}

void
GLContext::fDeleteFramebuffers(GLsizei n, const GLuint* names)
{
    if (mScreen) {
        // Notify mScreen which framebuffers we're deleting.
        // Otherwise, we will get framebuffer binding mispredictions.
        for (int i = 0; i < n; i++) {
            mScreen->DeletingFB(names[i]);
        }
    }

    // Avoid crash by flushing before glDeleteFramebuffers. See bug 1194923.
    if (mNeedsFlushBeforeDeleteFB) {
        fFlush();
    }

    if (n == 1 && *names == 0) {
        // Deleting framebuffer 0 causes hangs on the DROID. See bug 623228.
    } else {
        raw_fDeleteFramebuffers(n, names);
    }
    TRACKING_CONTEXT(DeletedFramebuffers(this, n, names));
}

#ifdef MOZ_WIDGET_ANDROID
/**
 * Conservatively estimate whether there is enough available
 * contiguous virtual address space to map a newly allocated texture.
 */
static bool
WillTextureMapSucceed(GLsizei width, GLsizei height, GLenum format, GLenum type)
{
    bool willSucceed = false;
    // Some drivers leave large gaps between textures, so require
    // there to be double the actual size of the texture available.
    size_t size = width * height * GetBytesPerTexel(format, type) * 2;

    void *p = mmap(nullptr, size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    if (p != MAP_FAILED) {
        willSucceed = true;
        munmap(p, size);
    }

    return willSucceed;
}
#endif // MOZ_WIDGET_ANDROID

void
GLContext::fTexImage2D(GLenum target, GLint level, GLint internalformat,
                       GLsizei width, GLsizei height, GLint border,
                       GLenum format, GLenum type, const GLvoid* pixels) {
    if (!IsTextureSizeSafeToPassToDriver(target, width, height)) {
        // pass wrong values to cause the GL to generate GL_INVALID_VALUE.
        // See bug 737182 and the comment in IsTextureSizeSafeToPassToDriver.
        level = -1;
        width = -1;
        height = -1;
        border = -1;
    }
#if MOZ_WIDGET_ANDROID
    if (mTextureAllocCrashesOnMapFailure) {
        // We have no way of knowing whether this texture already has
        // storage allocated for it, and therefore whether this check
        // is necessary. We must therefore assume it does not and
        // always perform the check.
        if (!WillTextureMapSucceed(width, height, internalformat, type)) {
            return;
        }
    }
#endif
    raw_fTexImage2D(target, level, internalformat, width, height, border, format, type, pixels);
}

GLuint
GLContext::GetDrawFB()
{
    if (mScreen)
        return mScreen->GetDrawFB();

    GLuint ret = 0;
    GetUIntegerv(LOCAL_GL_DRAW_FRAMEBUFFER_BINDING_EXT, &ret);
    return ret;
}

GLuint
GLContext::GetReadFB()
{
    if (mScreen)
        return mScreen->GetReadFB();

    GLenum bindEnum = IsSupported(GLFeature::split_framebuffer)
                        ? LOCAL_GL_READ_FRAMEBUFFER_BINDING_EXT
                        : LOCAL_GL_FRAMEBUFFER_BINDING;

    GLuint ret = 0;
    GetUIntegerv(bindEnum, &ret);
    return ret;
}

GLuint
GLContext::GetFB()
{
    if (mScreen) {
        // This has a very important extra assert that checks that we're
        // not accidentally ignoring a situation where the draw and read
        // FBs differ.
        return mScreen->GetFB();
    }

    GLuint ret = 0;
    GetUIntegerv(LOCAL_GL_FRAMEBUFFER_BINDING, &ret);
    return ret;
}

bool
GLContext::InitOffscreen(const gfx::IntSize& size, const SurfaceCaps& caps)
{
    if (!CreateScreenBuffer(size, caps))
        return false;

    MakeCurrent();
    fBindFramebuffer(LOCAL_GL_FRAMEBUFFER, 0);
    fScissor(0, 0, size.width, size.height);
    fViewport(0, 0, size.width, size.height);

    mCaps = mScreen->mCaps;
    MOZ_ASSERT(!mCaps.any);

    return true;
}

bool
GLContext::IsDrawingToDefaultFramebuffer()
{
    return Screen()->IsDrawFramebufferDefault();
}

GLuint
CreateTexture(GLContext* aGL, GLenum aInternalFormat, GLenum aFormat,
              GLenum aType, const gfx::IntSize& aSize, bool linear)
{
    GLuint tex = 0;
    aGL->fGenTextures(1, &tex);
    ScopedBindTexture autoTex(aGL, tex);

    aGL->fTexParameteri(LOCAL_GL_TEXTURE_2D,
                        LOCAL_GL_TEXTURE_MIN_FILTER, linear ? LOCAL_GL_LINEAR
                                                            : LOCAL_GL_NEAREST);
    aGL->fTexParameteri(LOCAL_GL_TEXTURE_2D,
                        LOCAL_GL_TEXTURE_MAG_FILTER, linear ? LOCAL_GL_LINEAR
                                                            : LOCAL_GL_NEAREST);
    aGL->fTexParameteri(LOCAL_GL_TEXTURE_2D, LOCAL_GL_TEXTURE_WRAP_S,
                        LOCAL_GL_CLAMP_TO_EDGE);
    aGL->fTexParameteri(LOCAL_GL_TEXTURE_2D, LOCAL_GL_TEXTURE_WRAP_T,
                        LOCAL_GL_CLAMP_TO_EDGE);

    aGL->fTexImage2D(LOCAL_GL_TEXTURE_2D,
                     0,
                     aInternalFormat,
                     aSize.width, aSize.height,
                     0,
                     aFormat,
                     aType,
                     nullptr);

    return tex;
}

GLuint
CreateTextureForOffscreen(GLContext* aGL, const GLFormats& aFormats,
                          const gfx::IntSize& aSize)
{
    MOZ_ASSERT(aFormats.color_texInternalFormat);
    MOZ_ASSERT(aFormats.color_texFormat);
    MOZ_ASSERT(aFormats.color_texType);

    GLenum internalFormat = aFormats.color_texInternalFormat;
    GLenum unpackFormat = aFormats.color_texFormat;
    GLenum unpackType = aFormats.color_texType;
    if (aGL->IsANGLE()) {
        MOZ_ASSERT(internalFormat == LOCAL_GL_RGBA);
        MOZ_ASSERT(unpackFormat == LOCAL_GL_RGBA);
        MOZ_ASSERT(unpackType == LOCAL_GL_UNSIGNED_BYTE);
        internalFormat = LOCAL_GL_BGRA_EXT;
        unpackFormat = LOCAL_GL_BGRA_EXT;
    }

    return CreateTexture(aGL, internalFormat, unpackFormat, unpackType, aSize);
}

uint32_t
GetBytesPerTexel(GLenum format, GLenum type)
{
    // If there is no defined format or type, we're not taking up any memory
    if (!format || !type) {
        return 0;
    }

    if (format == LOCAL_GL_DEPTH_COMPONENT) {
        if (type == LOCAL_GL_UNSIGNED_SHORT)
            return 2;
        else if (type == LOCAL_GL_UNSIGNED_INT)
            return 4;
    } else if (format == LOCAL_GL_DEPTH_STENCIL) {
        if (type == LOCAL_GL_UNSIGNED_INT_24_8_EXT)
            return 4;
    }

    if (type == LOCAL_GL_UNSIGNED_BYTE || type == LOCAL_GL_FLOAT || type == LOCAL_GL_UNSIGNED_INT_8_8_8_8_REV) {
        uint32_t multiplier = type == LOCAL_GL_UNSIGNED_BYTE ? 1 : 4;
        switch (format) {
            case LOCAL_GL_ALPHA:
            case LOCAL_GL_LUMINANCE:
                return 1 * multiplier;
            case LOCAL_GL_LUMINANCE_ALPHA:
                return 2 * multiplier;
            case LOCAL_GL_RGB:
                return 3 * multiplier;
            case LOCAL_GL_RGBA:
            case LOCAL_GL_BGRA_EXT:
                return 4 * multiplier;
            default:
                break;
        }
    } else if (type == LOCAL_GL_UNSIGNED_SHORT_4_4_4_4 ||
               type == LOCAL_GL_UNSIGNED_SHORT_5_5_5_1 ||
               type == LOCAL_GL_UNSIGNED_SHORT_5_6_5)
    {
        return 2;
    }

    gfxCriticalError() << "Unknown texture type " << type << " or format " << format;
    return 0;
}

} /* namespace gl */
} /* namespace mozilla */