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
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
|
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "gpu/command_buffer/service/gles2_cmd_decoder.h"
#include <stdio.h>
#include <algorithm>
#include <list>
#include <map>
#include <string>
#include <vector>
#include "app/gfx/gl/gl_context.h"
#include "app/gfx/gl/gl_implementation.h"
#include "base/callback.h"
#include "base/scoped_ptr.h"
#include "base/weak_ptr.h"
#include "build/build_config.h"
#define GLES2_GPU_SERVICE 1
#include "gpu/command_buffer/common/gles2_cmd_format.h"
#include "gpu/command_buffer/common/gles2_cmd_utils.h"
#include "gpu/command_buffer/common/id_allocator.h"
#include "gpu/command_buffer/service/buffer_manager.h"
#include "gpu/command_buffer/service/cmd_buffer_engine.h"
#include "gpu/command_buffer/service/context_group.h"
#include "gpu/command_buffer/service/framebuffer_manager.h"
#include "gpu/command_buffer/service/gl_utils.h"
#include "gpu/command_buffer/service/gles2_cmd_validation.h"
#include "gpu/command_buffer/service/program_manager.h"
#include "gpu/command_buffer/service/renderbuffer_manager.h"
#include "gpu/command_buffer/service/shader_manager.h"
#include "gpu/command_buffer/service/texture_manager.h"
#include "gpu/GLES2/gles2_command_buffer.h"
#if defined(GLES2_GPU_SERVICE_TRANSLATE_SHADER)
#include "third_party/angle/include/GLSLANG/ShaderLang.h"
#endif // GLES2_GPU_SERVICE_TRANSLATE_SHADER
#if !defined(GL_DEPTH24_STENCIL8)
#define GL_DEPTH24_STENCIL8 0x88F0
#endif
namespace gpu {
namespace gles2 {
class GLES2DecoderImpl;
// Check that certain assumptions the code makes are true. There are places in
// the code where shared memory is passed direclty to GL. Example, glUniformiv,
// glShaderSource. The command buffer code assumes GLint and GLsizei (and maybe
// a few others) are 32bits. If they are not 32bits the code will have to change
// to call those GL functions with service side memory and then copy the results
// to shared memory, converting the sizes.
COMPILE_ASSERT(sizeof(GLint) == sizeof(uint32), // NOLINT
GLint_not_same_size_as_uint32);
COMPILE_ASSERT(sizeof(GLsizei) == sizeof(uint32), // NOLINT
GLint_not_same_size_as_uint32);
COMPILE_ASSERT(sizeof(GLfloat) == sizeof(float), // NOLINT
GLfloat_not_same_size_as_float);
// TODO(kbr): the use of this anonymous namespace core dumps the
// linker on Mac OS X 10.6 when the symbol ordering file is used
// namespace {
// Returns the address of the first byte after a struct.
template <typename T>
const void* AddressAfterStruct(const T& pod) {
return reinterpret_cast<const uint8*>(&pod) + sizeof(pod);
}
// Returns the address of the frst byte after the struct or NULL if size >
// immediate_data_size.
template <typename RETURN_TYPE, typename COMMAND_TYPE>
RETURN_TYPE GetImmediateDataAs(const COMMAND_TYPE& pod,
uint32 size,
uint32 immediate_data_size) {
return (size <= immediate_data_size) ?
static_cast<RETURN_TYPE>(const_cast<void*>(AddressAfterStruct(pod))) :
NULL;
}
// Computes the data size for certain gl commands like glUniform.
bool ComputeDataSize(
GLuint count,
size_t size,
unsigned int elements_per_unit,
uint32* dst) {
uint32 value;
if (!SafeMultiplyUint32(count, size, &value)) {
return false;
}
if (!SafeMultiplyUint32(value, elements_per_unit, &value)) {
return false;
}
*dst = value;
return true;
}
// A struct to hold info about each command.
struct CommandInfo {
int arg_flags; // How to handle the arguments for this command
int arg_count; // How many arguments are expected for this command.
};
// A table of CommandInfo for all the commands.
const CommandInfo g_command_info[] = {
#define GLES2_CMD_OP(name) { \
name::kArgFlags, \
sizeof(name) / sizeof(CommandBufferEntry) - 1, }, /* NOLINT */ \
GLES2_COMMAND_LIST(GLES2_CMD_OP)
#undef GLES2_CMD_OP
};
// This class prevents any GL errors that occur when it is in scope from
// being reported to the client.
class ScopedGLErrorSuppressor {
public:
explicit ScopedGLErrorSuppressor(GLES2DecoderImpl* decoder);
~ScopedGLErrorSuppressor();
private:
GLES2DecoderImpl* decoder_;
DISALLOW_COPY_AND_ASSIGN(ScopedGLErrorSuppressor);
};
// Temporarily changes a decoder's bound 2D texture and restore it when this
// object goes out of scope. Also temporarily switches to using active texture
// unit zero in case the client has changed that to something invalid.
class ScopedTexture2DBinder {
public:
ScopedTexture2DBinder(GLES2DecoderImpl* decoder, GLuint id);
~ScopedTexture2DBinder();
private:
GLES2DecoderImpl* decoder_;
DISALLOW_COPY_AND_ASSIGN(ScopedTexture2DBinder);
};
// Temporarily changes a decoder's bound render buffer and restore it when this
// object goes out of scope.
class ScopedRenderBufferBinder {
public:
ScopedRenderBufferBinder(GLES2DecoderImpl* decoder, GLuint id);
~ScopedRenderBufferBinder();
private:
GLES2DecoderImpl* decoder_;
DISALLOW_COPY_AND_ASSIGN(ScopedRenderBufferBinder);
};
// Temporarily changes a decoder's bound frame buffer and restore it when this
// object goes out of scope.
class ScopedFrameBufferBinder {
public:
ScopedFrameBufferBinder(GLES2DecoderImpl* decoder, GLuint id);
~ScopedFrameBufferBinder();
private:
GLES2DecoderImpl* decoder_;
DISALLOW_COPY_AND_ASSIGN(ScopedFrameBufferBinder);
};
// Temporarily switch to a decoder's default GL context, having known default
// state.
class ScopedDefaultGLContext {
public:
explicit ScopedDefaultGLContext(GLES2DecoderImpl* decoder);
~ScopedDefaultGLContext();
private:
GLES2DecoderImpl* decoder_;
DISALLOW_COPY_AND_ASSIGN(ScopedDefaultGLContext);
};
// Encapsulates an OpenGL texture.
class Texture {
public:
explicit Texture(GLES2DecoderImpl* decoder);
~Texture();
// Create a new render texture.
void Create();
// Set the initial size and format of a render texture or resize it.
bool AllocateStorage(const gfx::Size& size);
// Copy the contents of the currently bound frame buffer.
void Copy(const gfx::Size& size);
// Destroy the render texture. This must be explicitly called before
// destroying this object.
void Destroy();
GLuint id() const {
return id_;
}
gfx::Size size() const {
return size_;
}
private:
GLES2DecoderImpl* decoder_;
GLuint id_;
gfx::Size size_;
DISALLOW_COPY_AND_ASSIGN(Texture);
};
// Encapsulates an OpenGL render buffer of any format.
class RenderBuffer {
public:
explicit RenderBuffer(GLES2DecoderImpl* decoder);
~RenderBuffer();
// Create a new render buffer.
void Create();
// Set the initial size and format of a render buffer or resize it.
bool AllocateStorage(const gfx::Size& size, GLenum format);
// Destroy the render buffer. This must be explicitly called before destroying
// this object.
void Destroy();
GLuint id() const {
return id_;
}
private:
GLES2DecoderImpl* decoder_;
GLuint id_;
DISALLOW_COPY_AND_ASSIGN(RenderBuffer);
};
// Encapsulates an OpenGL frame buffer.
class FrameBuffer {
public:
explicit FrameBuffer(GLES2DecoderImpl* decoder);
~FrameBuffer();
// Create a new frame buffer.
void Create();
// Attach a color render buffer to a frame buffer.
void AttachRenderTexture(Texture* texture);
// Attach a render buffer to a frame buffer. Note that this unbinds any
// currently bound frame buffer.
void AttachRenderBuffer(GLenum target, RenderBuffer* render_buffer);
// Clear the given attached buffers.
void Clear(GLbitfield buffers);
// Destroy the frame buffer. This must be explicitly called before destroying
// this object.
void Destroy();
// See glCheckFramebufferStatusEXT.
GLenum CheckStatus();
GLuint id() const {
return id_;
}
private:
GLES2DecoderImpl* decoder_;
GLuint id_;
DISALLOW_COPY_AND_ASSIGN(FrameBuffer);
};
// } // anonymous namespace.
GLES2Decoder::GLES2Decoder(ContextGroup* group)
: group_(group),
debug_(false) {
}
GLES2Decoder::~GLES2Decoder() {
}
class VertexAttribManager {
public:
// Info about Vertex Attributes. This is used to track what the user currently
// has bound on each Vertex Attribute so that checking can be done at
// glDrawXXX time.
class VertexAttribInfo {
public:
typedef std::list<VertexAttribInfo*> VertexAttribInfoList;
struct Vec4 {
float v[4];
};
VertexAttribInfo()
: index_(0),
enabled_(false),
size_(4),
type_(GL_FLOAT),
offset_(0),
normalized_(GL_FALSE),
gl_stride_(0),
real_stride_(16),
list_(NULL) {
value_.v[0] = 0.0f;
value_.v[1] = 0.0f;
value_.v[2] = 0.0f;
value_.v[3] = 1.0f;
}
// Returns true if this VertexAttrib can access index.
bool CanAccess(GLuint index) const;
BufferManager::BufferInfo* buffer() const {
return buffer_;
}
GLsizei offset() const {
return offset_;
}
GLuint index() const {
return index_;
}
GLint size() const {
return size_;
}
GLenum type() const {
return type_;
}
GLboolean normalized() const {
return normalized_;
}
GLsizei gl_stride() const {
return gl_stride_;
}
void SetInfo(
BufferManager::BufferInfo* buffer,
GLint size,
GLenum type,
GLboolean normalized,
GLsizei gl_stride,
GLsizei real_stride,
GLsizei offset) {
DCHECK_GT(real_stride, 0);
buffer_ = buffer;
size_ = size;
type_ = type;
normalized_ = normalized;
gl_stride_ = gl_stride;
real_stride_ = real_stride;
offset_ = offset;
}
void ClearBuffer() {
buffer_ = NULL;
}
bool enabled() const {
return enabled_;
}
void set_value(const Vec4& value) {
value_ = value;
}
const Vec4& value() const {
return value_;
}
private:
friend class VertexAttribManager;
void set_enabled(bool enabled) {
enabled_ = enabled;
}
void set_index(GLuint index) {
index_ = index;
}
void SetList(VertexAttribInfoList* new_list) {
DCHECK(new_list);
if (list_) {
list_->erase(it_);
}
it_ = new_list->insert(new_list->end(), this);
list_ = new_list;
}
// The index of this attrib.
GLuint index_;
// Whether or not this attribute is enabled.
bool enabled_;
// number of components (1, 2, 3, 4)
GLint size_;
// GL_BYTE, GL_FLOAT, etc. See glVertexAttribPointer.
GLenum type_;
// The offset into the buffer.
GLsizei offset_;
GLboolean normalized_;
// The stride passed to glVertexAttribPointer.
GLsizei gl_stride_;
// The stride that will be used to access the buffer. This is the actual
// stide, NOT the GL bogus stride. In other words there is never a stride
// of 0.
GLsizei real_stride_;
// The current value of the attrib.
Vec4 value_;
// The buffer bound to this attribute.
BufferManager::BufferInfo::Ref buffer_;
// List this info is on.
VertexAttribInfoList* list_;
// Iterator for list this info is on. Enabled/Disabled
VertexAttribInfoList::iterator it_;
};
typedef std::list<VertexAttribInfo*> VertexAttribInfoList;
VertexAttribManager()
: max_vertex_attribs_(0) {
}
void Initialize(uint32 num_vertex_attribs);
bool Enable(GLuint index, bool enable);
const VertexAttribInfoList& GetEnabledVertexAttribInfos() const {
return enabled_vertex_attribs_;
}
VertexAttribInfo* GetVertexAttribInfo(GLuint index) {
if (index < max_vertex_attribs_) {
return &vertex_attrib_infos_[index];
}
return NULL;
}
private:
uint32 max_vertex_attribs_;
// Info for each vertex attribute saved so we can check at glDrawXXX time
// if it is safe to draw.
scoped_array<VertexAttribInfo> vertex_attrib_infos_;
// Lists for which vertex attribs are enabled, disabled.
VertexAttribInfoList enabled_vertex_attribs_;
VertexAttribInfoList disabled_vertex_attribs_;
};
bool VertexAttribManager::VertexAttribInfo::CanAccess(GLuint index) const {
if (!enabled_) {
return true;
}
if (!buffer_ || buffer_->IsDeleted()) {
return false;
}
// The number of elements that can be accessed.
GLsizeiptr buffer_size = buffer_->size();
if (offset_ > buffer_size || real_stride_ == 0) {
return false;
}
uint32 usable_size = buffer_size - offset_;
GLuint num_elements = usable_size / real_stride_ +
((usable_size % real_stride_) >=
(GLES2Util::GetGLTypeSizeForTexturesAndBuffers(type_) * size_) ? 1 : 0);
return index < num_elements;
}
void VertexAttribManager::Initialize(uint32 max_vertex_attribs) {
max_vertex_attribs_ = max_vertex_attribs;
vertex_attrib_infos_.reset(
new VertexAttribInfo[max_vertex_attribs]);
for (uint32 vv = 0; vv < max_vertex_attribs; ++vv) {
vertex_attrib_infos_[vv].set_index(vv);
vertex_attrib_infos_[vv].SetList(&disabled_vertex_attribs_);
}
}
bool VertexAttribManager::Enable(GLuint index, bool enable) {
if (index >= max_vertex_attribs_) {
return false;
}
VertexAttribInfo& info = vertex_attrib_infos_[index];
if (info.enabled() != enable) {
info.set_enabled(enable);
info.SetList(enable ? &enabled_vertex_attribs_ : &disabled_vertex_attribs_);
}
return true;
}
// This class implements GLES2Decoder so we don't have to expose all the GLES2
// cmd stuff to outside this class.
class GLES2DecoderImpl : public base::SupportsWeakPtr<GLES2DecoderImpl>,
public GLES2Decoder {
public:
explicit GLES2DecoderImpl(ContextGroup* group);
// Overridden from AsyncAPIInterface.
virtual Error DoCommand(unsigned int command,
unsigned int arg_count,
const void* args);
// Overridden from AsyncAPIInterface.
virtual const char* GetCommandName(unsigned int command_id) const;
// Overridden from GLES2Decoder.
virtual bool Initialize(gfx::GLContext* context,
const gfx::Size& size,
GLES2Decoder* parent,
uint32 parent_client_texture_id);
virtual void Destroy();
virtual void ResizeOffscreenFrameBuffer(const gfx::Size& size);
virtual bool UpdateOffscreenFrameBufferSize();
virtual bool MakeCurrent();
virtual GLES2Util* GetGLES2Util() { return &util_; }
virtual gfx::GLContext* GetGLContext() { return context_; }
virtual void SetSwapBuffersCallback(Callback0::Type* callback);
private:
friend class ScopedGLErrorSuppressor;
friend class ScopedTexture2DBinder;
friend class ScopedFrameBufferBinder;
friend class ScopedRenderBufferBinder;
friend class ScopedDefaultGLContext;
friend class RenderBuffer;
friend class FrameBuffer;
// State associated with each texture unit.
struct TextureUnit {
TextureUnit() : bind_target(GL_TEXTURE_2D) { }
// The last target that was bound to this texture unit.
GLenum bind_target;
// texture currently bound to this unit's GL_TEXTURE_2D with glBindTexture
TextureManager::TextureInfo::Ref bound_texture_2d;
// texture currently bound to this unit's GL_TEXTURE_CUBE_MAP with
// glBindTexture
TextureManager::TextureInfo::Ref bound_texture_cube_map;
};
// Helpers for the glGen and glDelete functions.
bool GenTexturesHelper(GLsizei n, const GLuint* client_ids);
void DeleteTexturesHelper(GLsizei n, const GLuint* client_ids);
bool GenBuffersHelper(GLsizei n, const GLuint* client_ids);
void DeleteBuffersHelper(GLsizei n, const GLuint* client_ids);
bool GenFramebuffersHelper(GLsizei n, const GLuint* client_ids);
void DeleteFramebuffersHelper(GLsizei n, const GLuint* client_ids);
bool GenRenderbuffersHelper(GLsizei n, const GLuint* client_ids);
void DeleteRenderbuffersHelper(GLsizei n, const GLuint* client_ids);
// TODO(gman): Cache these pointers?
BufferManager* buffer_manager() {
return group_->buffer_manager();
}
RenderbufferManager* renderbuffer_manager() {
return group_->renderbuffer_manager();
}
FramebufferManager* framebuffer_manager() {
return group_->framebuffer_manager();
}
ProgramManager* program_manager() {
return group_->program_manager();
}
ShaderManager* shader_manager() {
return group_->shader_manager();
}
TextureManager* texture_manager() {
return group_->texture_manager();
}
// Creates a TextureInfo for the given texture.
TextureManager::TextureInfo* CreateTextureInfo(
GLuint client_id, GLuint service_id) {
return texture_manager()->CreateTextureInfo(client_id, service_id);
}
// Gets the texture info for the given texture. Returns NULL if none exists.
TextureManager::TextureInfo* GetTextureInfo(GLuint client_id) {
TextureManager::TextureInfo* info =
texture_manager()->GetTextureInfo(client_id);
return (info && !info->IsDeleted()) ? info : NULL;
}
// Deletes the texture info for the given texture.
void RemoveTextureInfo(GLuint client_id) {
texture_manager()->RemoveTextureInfo(client_id);
}
// Get the size (in pixels) of the currently bound frame buffer (either FBO
// or regular back buffer).
gfx::Size GetBoundFrameBufferSize();
// Wrapper for CompressedTexImage2D commands.
error::Error DoCompressedTexImage2D(
GLenum target,
GLint level,
GLenum internal_format,
GLsizei width,
GLsizei height,
GLint border,
GLsizei image_size,
const void* data);
// Wrapper for TexImage2D commands.
error::Error DoTexImage2D(
GLenum target,
GLint level,
GLenum internal_format,
GLsizei width,
GLsizei height,
GLint border,
GLenum format,
GLenum type,
const void* pixels,
uint32 pixels_size);
// Creates a ProgramInfo for the given program.
void CreateProgramInfo(GLuint client_id, GLuint service_id) {
program_manager()->CreateProgramInfo(client_id, service_id);
}
// Gets the program info for the given program. Returns NULL if none exists.
ProgramManager::ProgramInfo* GetProgramInfo(GLuint client_id) {
ProgramManager::ProgramInfo* info =
program_manager()->GetProgramInfo(client_id);
return (info && !info->IsDeleted()) ? info : NULL;
}
// Gets the program info for the given program. If it's not a program
// generates a GL error. Returns NULL if not program.
ProgramManager::ProgramInfo* GetProgramInfoNotShader(
GLuint client_id, const char* function_name) {
ProgramManager::ProgramInfo* info = GetProgramInfo(client_id);
if (!info) {
if (GetShaderInfo(client_id)) {
SetGLError(GL_INVALID_OPERATION,
(std::string(function_name) +
": shader passed for program").c_str());
} else {
SetGLError(GL_INVALID_VALUE,
(std::string(function_name) + ": unknown program").c_str());
}
}
return info;
}
// Deletes the program info for the given program.
void RemoveProgramInfo(GLuint client_id) {
program_manager()->RemoveProgramInfo(client_id);
}
// Creates a ShaderInfo for the given shader.
void CreateShaderInfo(GLuint client_id,
GLuint service_id,
GLenum shader_type) {
shader_manager()->CreateShaderInfo(client_id, service_id, shader_type);
}
// Gets the shader info for the given shader. Returns NULL if none exists.
ShaderManager::ShaderInfo* GetShaderInfo(GLuint client_id) {
ShaderManager::ShaderInfo* info =
shader_manager()->GetShaderInfo(client_id);
return (info && !info->IsDeleted()) ? info : NULL;
}
// Gets the shader info for the given shader. If it's not a shader generates a
// GL error. Returns NULL if not shader.
ShaderManager::ShaderInfo* GetShaderInfoNotProgram(
GLuint client_id, const char* function_name) {
ShaderManager::ShaderInfo* info = GetShaderInfo(client_id);
if (!info) {
if (GetProgramInfo(client_id)) {
SetGLError(
GL_INVALID_OPERATION,
(std::string(function_name) +
": program passed for shader").c_str());
} else {
SetGLError(GL_INVALID_VALUE,
(std::string(function_name) + ": unknown shader").c_str());
}
}
return info;
}
// Deletes the shader info for the given shader.
void RemoveShaderInfo(GLuint client_id) {
shader_manager()->RemoveShaderInfo(client_id);
}
// Creates a buffer info for the given buffer.
void CreateBufferInfo(GLuint client_id, GLuint service_id) {
return buffer_manager()->CreateBufferInfo(client_id, service_id);
}
// Gets the buffer info for the given buffer.
BufferManager::BufferInfo* GetBufferInfo(GLuint client_id) {
BufferManager::BufferInfo* info =
buffer_manager()->GetBufferInfo(client_id);
return (info && !info->IsDeleted()) ? info : NULL;
}
// Removes any buffers in the VertexAtrribInfos and BufferInfos. This is used
// on glDeleteBuffers so we can make sure the user does not try to render
// with deleted buffers.
void RemoveBufferInfo(GLuint client_id);
// Creates a framebuffer info for the given framebuffer.
void CreateFramebufferInfo(GLuint client_id, GLuint service_id) {
return framebuffer_manager()->CreateFramebufferInfo(client_id, service_id);
}
// Gets the framebuffer info for the given framebuffer.
FramebufferManager::FramebufferInfo* GetFramebufferInfo(
GLuint client_id) {
FramebufferManager::FramebufferInfo* info =
framebuffer_manager()->GetFramebufferInfo(client_id);
return (info && !info->IsDeleted()) ? info : NULL;
}
// Removes the framebuffer info for the given framebuffer.
void RemoveFramebufferInfo(GLuint client_id) {
framebuffer_manager()->RemoveFramebufferInfo(client_id);
}
// Creates a renderbuffer info for the given renderbuffer.
void CreateRenderbufferInfo(GLuint client_id, GLuint service_id) {
return renderbuffer_manager()->CreateRenderbufferInfo(
client_id, service_id);
}
// Gets the renderbuffer info for the given renderbuffer.
RenderbufferManager::RenderbufferInfo* GetRenderbufferInfo(
GLuint client_id) {
RenderbufferManager::RenderbufferInfo* info =
renderbuffer_manager()->GetRenderbufferInfo(client_id);
return (info && !info->IsDeleted()) ? info : NULL;
}
// Removes the renderbuffer info for the given renderbuffer.
void RemoveRenderbufferInfo(GLuint client_id) {
renderbuffer_manager()->RemoveRenderbufferInfo(client_id);
}
error::Error GetAttribLocationHelper(
GLuint client_id, uint32 location_shm_id, uint32 location_shm_offset,
const std::string& name_str);
error::Error GetUniformLocationHelper(
GLuint client_id, uint32 location_shm_id, uint32 location_shm_offset,
const std::string& name_str);
// Helper for glShaderSource.
error::Error ShaderSourceHelper(
GLuint client_id, const char* data, uint32 data_size);
// Checks if the current program exists and is valid. If not generates the
// appropriate GL error. Returns true if the current program is in a usable
// state.
bool CheckCurrentProgram(const char* function_name);
// Checks if the current program exists and is valid and that location is not
// -1. If the current program is not valid generates the appropriate GL
// error. Returns true if the current program is in a usable state and
// location is not -1.
bool CheckCurrentProgramForUniform(GLint location, const char* function_name);
// Gets the type of a uniform for a location in the current program. Sets GL
// errors if the current program is not valid. Returns true if the current
// program is valid and the location exists.
bool GetUniformTypeByLocation(
GLint location, const char* function_name, GLenum* type);
// Helper for glGetBooleanv, glGetFloatv and glGetIntegerv
bool GetHelper(GLenum pname, GLint* params, GLsizei* num_written);
// Wrapper for glCreateProgram
bool CreateProgramHelper(GLuint client_id);
// Wrapper for glCreateShader
bool CreateShaderHelper(GLenum type, GLuint client_id);
// Wrapper for glActiveTexture
void DoActiveTexture(GLenum texture_unit);
// Wrapper for glAttachShader
void DoAttachShader(GLuint client_program_id, GLint client_shader_id);
// Wrapper for glBindBuffer since we need to track the current targets.
void DoBindBuffer(GLenum target, GLuint buffer);
// Wrapper for glBindFramebuffer since we need to track the current targets.
void DoBindFramebuffer(GLenum target, GLuint framebuffer);
// Wrapper for glBindRenderbuffer since we need to track the current targets.
void DoBindRenderbuffer(GLenum target, GLuint renderbuffer);
// Wrapper for glBindTexture since we need to track the current targets.
void DoBindTexture(GLenum target, GLuint texture);
// Wrapper for glBufferData.
void DoBufferData(
GLenum target, GLsizeiptr size, const GLvoid * data, GLenum usage);
// Wrapper for glBufferSubData.
void DoBufferSubData(
GLenum target, GLintptr offset, GLsizeiptr size, const GLvoid * data);
// Wrapper for glCheckFramebufferStatus
GLenum DoCheckFramebufferStatus(GLenum target);
// Wrapper for glCompileShader.
void DoCompileShader(GLuint shader);
// Helper for DeleteSharedIds commands.
void DoDeleteSharedIds(GLuint namespace_id, GLsizei n, const GLuint* ids);
// Wrapper for glDetachShader
void DoDetachShader(GLuint client_program_id, GLint client_shader_id);
// Wrapper for glDrawArrays.
void DoDrawArrays(GLenum mode, GLint first, GLsizei count);
// Wrapper for glDisableVertexAttribArray.
void DoDisableVertexAttribArray(GLuint index);
// Wrapper for glEnableVertexAttribArray.
void DoEnableVertexAttribArray(GLuint index);
// Wrapper for glFramebufferRenderbufffer.
void DoFramebufferRenderbuffer(
GLenum target, GLenum attachment, GLenum renderbuffertarget,
GLuint renderbuffer);
// Wrapper for glFramebufferTexture2D.
void DoFramebufferTexture2D(
GLenum target, GLenum attachment, GLenum textarget, GLuint texture,
GLint level);
// Wrapper for glGenerateMipmap
void DoGenerateMipmap(GLenum target);
// Helper for GenSharedIds commands.
void DoGenSharedIds(
GLuint namespace_id, GLuint id_offset, GLsizei n, GLuint* ids);
// Wrapper for DoGetBooleanv.
void DoGetBooleanv(GLenum pname, GLboolean* params);
// Wrapper for DoGetFloatv.
void DoGetFloatv(GLenum pname, GLfloat* params);
// Wrapper for glGetFramebufferAttachmentParameteriv.
void DoGetFramebufferAttachmentParameteriv(
GLenum target, GLenum attachment, GLenum pname, GLint* params);
// Wrapper for glGetIntegerv.
void DoGetIntegerv(GLenum pname, GLint* params);
// Gets the max value in a range in a buffer.
GLuint DoGetMaxValueInBuffer(
GLuint buffer_id, GLsizei count, GLenum type, GLuint offset);
// Wrapper for glGetProgramiv.
void DoGetProgramiv(
GLuint program_id, GLenum pname, GLint* params);
// Wrapper for glRenderbufferParameteriv.
void DoGetRenderbufferParameteriv(
GLenum target, GLenum pname, GLint* params);
// Wrapper for glGetShaderiv
void DoGetShaderiv(GLuint shader, GLenum pname, GLint* params);
// Wrappers for glGetVertexAttrib.
void DoGetVertexAttribfv(GLuint index, GLenum pname, GLfloat *params);
void DoGetVertexAttribiv(GLuint index, GLenum pname, GLint *params);
// Wrappers for glIsXXX functions.
bool DoIsBuffer(GLuint client_id);
bool DoIsFramebuffer(GLuint client_id);
bool DoIsProgram(GLuint client_id);
bool DoIsRenderbuffer(GLuint client_id);
bool DoIsShader(GLuint client_id);
bool DoIsTexture(GLuint client_id);
// Wrapper for glLinkProgram
void DoLinkProgram(GLuint program);
// Helper for RegisterSharedIds.
void DoRegisterSharedIds(GLuint namespace_id, GLsizei n, const GLuint* ids);
// Wrapper for glRenderbufferStorage.
void DoRenderbufferStorage(
GLenum target, GLenum internalformat, GLsizei width, GLsizei height);
// Wrapper for glReleaseShaderCompiler.
void DoReleaseShaderCompiler() { }
// Wrappers for glTexParameter functions.
void DoTexParameterf(GLenum target, GLenum pname, GLfloat param);
void DoTexParameteri(GLenum target, GLenum pname, GLint param);
void DoTexParameterfv(GLenum target, GLenum pname, const GLfloat* params);
void DoTexParameteriv(GLenum target, GLenum pname, const GLint* params);
// Wrappers for glUniform1i and glUniform1iv as according to the GLES2
// spec only these 2 functions can be used to set sampler uniforms.
void DoUniform1i(GLint location, GLint v0);
void DoUniform1iv(GLint location, GLsizei count, const GLint* value);
// Wrappers for glUniformfv because some drivers don't correctly accept
// bool uniforms.
void DoUniform1fv(GLint location, GLsizei count, const GLfloat* value);
void DoUniform2fv(GLint location, GLsizei count, const GLfloat* value);
void DoUniform3fv(GLint location, GLsizei count, const GLfloat* value);
void DoUniform4fv(GLint location, GLsizei count, const GLfloat* value);
// Wrappers for glVertexAttrib??
void DoVertexAttrib1f(GLuint index, GLfloat v0);
void DoVertexAttrib2f(GLuint index, GLfloat v0, GLfloat v1);
void DoVertexAttrib3f(GLuint index, GLfloat v0, GLfloat v1, GLfloat v2);
void DoVertexAttrib4f(
GLuint index, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3);
void DoVertexAttrib1fv(GLuint index, const GLfloat *v);
void DoVertexAttrib2fv(GLuint index, const GLfloat *v);
void DoVertexAttrib3fv(GLuint index, const GLfloat *v);
void DoVertexAttrib4fv(GLuint index, const GLfloat *v);
// Wrapper for glUseProgram
void DoUseProgram(GLuint program);
// Wrapper for glValidateProgram.
void DoValidateProgram(GLuint program_client_id);
// Gets the number of values that will be returned by glGetXXX. Returns
// false if pname is unknown.
bool GetNumValuesReturnedForGLGet(GLenum pname, GLsizei* num_values);
// Gets the GLError through our wrapper.
GLenum GetGLError();
// Sets our wrapper for the GLError.
void SetGLError(GLenum error, const char* msg);
// Copies the real GL errors to the wrapper. This is so we can
// make sure there are no native GL errors before calling some GL function
// so that on return we know any error generated was for that specific
// command.
void CopyRealGLErrorsToWrapper();
// Clear all real GL errors. This is to prevent the client from seeing any
// errors caused by GL calls that it was not responsible for issuing.
void ClearRealGLErrors();
// Checks if the current program and vertex attributes are valid for drawing.
bool IsDrawValid(GLuint max_vertex_accessed);
// Returns true if attrib0 was simulated.
bool SimulateAttrib0(GLuint max_vertex_accessed);
void RestoreStateForSimulatedAttrib0();
// Returns true if textures were set.
bool SetBlackTextureForNonRenderableTextures();
void RestoreStateForNonRenderableTextures();
// Gets the buffer id for a given target.
BufferManager::BufferInfo* GetBufferInfoForTarget(GLenum target) {
DCHECK(target == GL_ARRAY_BUFFER || target == GL_ELEMENT_ARRAY_BUFFER);
BufferManager::BufferInfo* info = target == GL_ARRAY_BUFFER ?
bound_array_buffer_ : bound_element_array_buffer_;
return (info && !info->IsDeleted()) ? info : NULL;
}
// Gets the texture id for a given target.
TextureManager::TextureInfo* GetTextureInfoForTarget(GLenum target) {
TextureUnit& unit = texture_units_[active_texture_unit_];
TextureManager::TextureInfo* info = NULL;
switch (target) {
case GL_TEXTURE_2D:
info = unit.bound_texture_2d;
break;
case GL_TEXTURE_CUBE_MAP:
case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
info = unit.bound_texture_cube_map;
break;
// Note: If we ever support TEXTURE_RECTANGLE as a target, be sure to
// track |texture_| with the currently bound TEXTURE_RECTANGLE texture,
// because |texture_| is used by the FBO rendering mechanism for readback
// to the bits that get sent to the browser.
default:
NOTREACHED();
return NULL;
}
return (info && !info->IsDeleted()) ? info : NULL;
}
// Validates the program and location for a glGetUniform call and returns
// a SizeResult setup to receive the result. Returns true if glGetUniform
// should be called.
bool GetUniformSetup(
GLuint program, GLint location,
uint32 shm_id, uint32 shm_offset,
error::Error* error, GLuint* service_id, void** result,
GLenum* result_type);
// Generate a member function prototype for each command in an automated and
// typesafe way.
#define GLES2_CMD_OP(name) \
Error Handle ## name( \
uint32 immediate_data_size, \
const gles2::name& args); \
GLES2_COMMAND_LIST(GLES2_CMD_OP)
#undef GLES2_CMD_OP
// The GL context this decoder renders to on behalf of the client.
gfx::GLContext* context_;
// A GLContext that is kept in its default state. It is used to perform
// operations that should not be dependent on client set GLContext state, like
// clearing a render buffer when it is created.
// TODO(apatrick): Decoders in the same ContextGroup could potentially share
// the same default GL context.
scoped_ptr<gfx::GLContext> default_context_;
// A parent decoder can access this decoders saved offscreen frame buffer.
// The parent pointer is reset if the parent is destroyed.
base::WeakPtr<GLES2DecoderImpl> parent_;
// Width and height to which an offscreen frame buffer should be resized on
// the next call to SwapBuffers.
gfx::Size pending_offscreen_size_;
// Current GL error bits.
uint32 error_bits_;
// Util to help with GL.
GLES2Util util_;
// pack alignment as last set by glPixelStorei
GLint pack_alignment_;
// unpack alignment as last set by glPixelStorei
GLint unpack_alignment_;
// The currently bound array buffer. If this is 0 it is illegal to call
// glVertexAttribPointer.
BufferManager::BufferInfo::Ref bound_array_buffer_;
// The currently bound element array buffer. If this is 0 it is illegal
// to call glDrawElements.
BufferManager::BufferInfo::Ref bound_element_array_buffer_;
// Class that manages vertex attribs.
VertexAttribManager vertex_attrib_manager_;
// The buffer we bind to attrib 0 since OpenGL requires it (ES does not).
GLuint attrib_0_buffer_id_;
// The value currently in attrib_0.
VertexAttribManager::VertexAttribInfo::Vec4 attrib_0_value_;
// The size of attrib 0.
GLsizei attrib_0_size_;
// Current active texture by 0 - n index.
// In other words, if we call glActiveTexture(GL_TEXTURE2) this value would
// be 2.
GLuint active_texture_unit_;
// Which textures are bound to texture units through glActiveTexture.
scoped_array<TextureUnit> texture_units_;
// Black (0,0,0,1) textures for when non-renderable textures are used.
// NOTE: There is no corresponding TextureInfo for these textures.
// TextureInfos are only for textures the client side can access.
GLuint black_2d_texture_id_;
GLuint black_cube_texture_id_;
// The program in use by glUseProgram
ProgramManager::ProgramInfo::Ref current_program_;
// The currently bound framebuffer
FramebufferManager::FramebufferInfo::Ref bound_framebuffer_;
// The currently bound renderbuffer
RenderbufferManager::RenderbufferInfo::Ref bound_renderbuffer_;
bool anti_aliased_;
// The offscreen frame buffer that the client renders to. With EGL, the
// depth and stencil buffers are separate. With regular GL there is a single
// packed depth stencil buffer in offscreen_target_depth_render_buffer_.
// offscreen_target_stencil_render_buffer_ is unused.
scoped_ptr<FrameBuffer> offscreen_target_frame_buffer_;
scoped_ptr<Texture> offscreen_target_color_texture_;
scoped_ptr<RenderBuffer> offscreen_target_depth_render_buffer_;
scoped_ptr<RenderBuffer> offscreen_target_stencil_render_buffer_;
// The copy that is saved when SwapBuffers is called.
scoped_ptr<Texture> offscreen_saved_color_texture_;
scoped_ptr<Callback0::Type> swap_buffers_callback_;
// The last error message set.
std::string last_error_;
bool use_shader_translator_;
// Cached from the context group.
const Validators* validators_;
DISALLOW_COPY_AND_ASSIGN(GLES2DecoderImpl);
};
ScopedGLErrorSuppressor::ScopedGLErrorSuppressor(GLES2DecoderImpl* decoder)
: decoder_(decoder) {
decoder_->CopyRealGLErrorsToWrapper();
}
ScopedGLErrorSuppressor::~ScopedGLErrorSuppressor() {
decoder_->ClearRealGLErrors();
}
ScopedTexture2DBinder::ScopedTexture2DBinder(GLES2DecoderImpl* decoder,
GLuint id)
: decoder_(decoder) {
ScopedGLErrorSuppressor suppressor(decoder_);
// TODO(apatrick): Check if there are any other states that need to be reset
// before binding a new texture.
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, id);
}
ScopedTexture2DBinder::~ScopedTexture2DBinder() {
ScopedGLErrorSuppressor suppressor(decoder_);
GLES2DecoderImpl::TextureUnit& info = decoder_->texture_units_[0];
GLuint last_id;
if (info.bound_texture_2d)
last_id = info.bound_texture_2d->service_id();
else
last_id = 0;
glBindTexture(GL_TEXTURE_2D, last_id);
glActiveTexture(GL_TEXTURE0 + decoder_->active_texture_unit_);
}
ScopedRenderBufferBinder::ScopedRenderBufferBinder(GLES2DecoderImpl* decoder,
GLuint id)
: decoder_(decoder) {
ScopedGLErrorSuppressor suppressor(decoder_);
glBindRenderbufferEXT(GL_RENDERBUFFER, id);
}
ScopedRenderBufferBinder::~ScopedRenderBufferBinder() {
ScopedGLErrorSuppressor suppressor(decoder_);
glBindRenderbufferEXT(
GL_RENDERBUFFER,
decoder_->bound_renderbuffer_ ?
decoder_->bound_renderbuffer_->service_id() : 0);
}
ScopedFrameBufferBinder::ScopedFrameBufferBinder(GLES2DecoderImpl* decoder,
GLuint id)
: decoder_(decoder) {
ScopedGLErrorSuppressor suppressor(decoder_);
glBindFramebufferEXT(GL_FRAMEBUFFER, id);
}
ScopedFrameBufferBinder::~ScopedFrameBufferBinder() {
ScopedGLErrorSuppressor suppressor(decoder_);
FramebufferManager::FramebufferInfo* info =
decoder_->bound_framebuffer_.get();
GLuint framebuffer_id = info ? info->service_id() : 0;
if (framebuffer_id == 0 &&
decoder_->offscreen_target_frame_buffer_.get()) {
glBindFramebufferEXT(GL_FRAMEBUFFER,
decoder_->offscreen_target_frame_buffer_->id());
} else {
glBindFramebufferEXT(GL_FRAMEBUFFER, framebuffer_id);
}
}
ScopedDefaultGLContext::ScopedDefaultGLContext(GLES2DecoderImpl* decoder)
: decoder_(decoder) {
decoder_->default_context_->MakeCurrent();
}
ScopedDefaultGLContext::~ScopedDefaultGLContext() {
decoder_->context_->MakeCurrent();
}
Texture::Texture(GLES2DecoderImpl* decoder)
: decoder_(decoder),
id_(0) {
}
Texture::~Texture() {
// This does not destroy the render texture because that would require that
// the associated GL context was current. Just check that it was explicitly
// destroyed.
DCHECK_EQ(id_, 0u);
}
void Texture::Create() {
ScopedGLErrorSuppressor suppressor(decoder_);
Destroy();
glGenTextures(1, &id_);
}
bool Texture::AllocateStorage(const gfx::Size& size) {
DCHECK_NE(id_, 0u);
ScopedGLErrorSuppressor suppressor(decoder_);
ScopedTexture2DBinder binder(decoder_, id_);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(
GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D,
0, // mip level
GL_RGBA,
size.width(),
size.height(),
0, // border
GL_RGBA,
GL_UNSIGNED_BYTE,
NULL);
size_ = size;
return glGetError() == GL_NO_ERROR;
}
void Texture::Copy(const gfx::Size& size) {
DCHECK_NE(id_, 0u);
ScopedGLErrorSuppressor suppressor(decoder_);
ScopedTexture2DBinder binder(decoder_, id_);
glCopyTexImage2D(GL_TEXTURE_2D,
0, // level
GL_RGBA,
0, 0,
size.width(),
size.height(),
0); // border
}
void Texture::Destroy() {
if (id_ != 0) {
ScopedGLErrorSuppressor suppressor(decoder_);
glDeleteTextures(1, &id_);
id_ = 0;
}
}
RenderBuffer::RenderBuffer(GLES2DecoderImpl* decoder)
: decoder_(decoder),
id_(0) {
}
RenderBuffer::~RenderBuffer() {
// This does not destroy the render buffer because that would require that
// the associated GL context was current. Just check that it was explicitly
// destroyed.
DCHECK_EQ(id_, 0u);
}
void RenderBuffer::Create() {
ScopedGLErrorSuppressor suppressor(decoder_);
Destroy();
glGenRenderbuffersEXT(1, &id_);
}
bool RenderBuffer::AllocateStorage(const gfx::Size& size, GLenum format) {
ScopedGLErrorSuppressor suppressor(decoder_);
ScopedRenderBufferBinder binder(decoder_, id_);
glRenderbufferStorageEXT(GL_RENDERBUFFER,
format,
size.width(),
size.height());
return glGetError() == GL_NO_ERROR;
}
void RenderBuffer::Destroy() {
if (id_ != 0) {
ScopedGLErrorSuppressor suppressor(decoder_);
glDeleteRenderbuffersEXT(1, &id_);
id_ = 0;
}
}
FrameBuffer::FrameBuffer(GLES2DecoderImpl* decoder)
: decoder_(decoder),
id_(0) {
}
FrameBuffer::~FrameBuffer() {
// This does not destroy the frame buffer because that would require that
// the associated GL context was current. Just check that it was explicitly
// destroyed.
DCHECK_EQ(id_, 0u);
}
void FrameBuffer::Create() {
ScopedGLErrorSuppressor suppressor(decoder_);
Destroy();
glGenFramebuffersEXT(1, &id_);
}
void FrameBuffer::AttachRenderTexture(Texture* texture) {
DCHECK_NE(id_, 0u);
ScopedGLErrorSuppressor suppressor(decoder_);
ScopedFrameBufferBinder binder(decoder_, id_);
GLuint attach_id = texture ? texture->id() : 0;
glFramebufferTexture2DEXT(GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D,
attach_id,
0);
}
void FrameBuffer::AttachRenderBuffer(GLenum target,
RenderBuffer* render_buffer) {
DCHECK_NE(id_, 0u);
ScopedGLErrorSuppressor suppressor(decoder_);
ScopedFrameBufferBinder binder(decoder_, id_);
GLuint attach_id = render_buffer ? render_buffer->id() : 0;
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER,
target,
GL_RENDERBUFFER,
attach_id);
}
void FrameBuffer::Clear(GLbitfield buffers) {
ScopedGLErrorSuppressor suppressor(decoder_);
ScopedFrameBufferBinder binder(decoder_, id_);
glClear(buffers);
}
void FrameBuffer::Destroy() {
if (id_ != 0) {
ScopedGLErrorSuppressor suppressor(decoder_);
glDeleteFramebuffersEXT(1, &id_);
id_ = 0;
}
}
GLenum FrameBuffer::CheckStatus() {
DCHECK_NE(id_, 0u);
ScopedGLErrorSuppressor suppressor(decoder_);
ScopedFrameBufferBinder binder(decoder_, id_);
return glCheckFramebufferStatusEXT(GL_FRAMEBUFFER);
}
GLES2Decoder* GLES2Decoder::Create(ContextGroup* group) {
return new GLES2DecoderImpl(group);
}
GLES2DecoderImpl::GLES2DecoderImpl(ContextGroup* group)
: GLES2Decoder(group),
context_(NULL),
error_bits_(0),
util_(0), // TODO(gman): Set to actual num compress texture formats.
pack_alignment_(4),
unpack_alignment_(4),
attrib_0_buffer_id_(0),
attrib_0_size_(0),
active_texture_unit_(0),
black_2d_texture_id_(0),
black_cube_texture_id_(0),
anti_aliased_(false),
use_shader_translator_(true),
validators_(group->validators()) {
attrib_0_value_.v[0] = 0.0f;
attrib_0_value_.v[1] = 0.0f;
attrib_0_value_.v[2] = 0.0f;
attrib_0_value_.v[3] = 1.0f;
// The shader translator is not needed for EGL because it already uses the
// GLSL ES syntax. It is translated for the unit tests because
// GLES2DecoderWithShaderTest.GetShaderInfoLogValidArgs passes the empty
// string to CompileShader and this is not a valid shader. TODO(apatrick):
// fix this test.
if (gfx::GetGLImplementation() == gfx::kGLImplementationEGLGLES2 ||
gfx::GetGLImplementation() == gfx::kGLImplementationMockGL) {
use_shader_translator_ = false;
}
}
bool GLES2DecoderImpl::Initialize(gfx::GLContext* context,
const gfx::Size& size,
GLES2Decoder* parent,
uint32 parent_client_texture_id) {
DCHECK(context);
DCHECK(!context_);
context_ = context;
// Create a GL context that is kept in a default state and shares a namespace
// with the main GL context.
default_context_.reset(gfx::GLContext::CreateOffscreenGLContext(context_));
if (!default_context_.get()) {
Destroy();
return false;
}
// Keep only a weak pointer to the parent so we don't unmap its client
// frame buffer after it has been destroyed.
if (parent)
parent_ = static_cast<GLES2DecoderImpl*>(parent)->AsWeakPtr();
if (!MakeCurrent()) {
Destroy();
return false;
}
CHECK_GL_ERROR();
if (!group_->Initialize()) {
Destroy();
return false;
}
vertex_attrib_manager_.Initialize(group_->max_vertex_attribs());
// We have to enable vertex array 0 on OpenGL or it won't render. Note that
// OpenGL ES 2.0 does not have this issue.
glEnableVertexAttribArray(0);
glGenBuffersARB(1, &attrib_0_buffer_id_);
glBindBuffer(GL_ARRAY_BUFFER, attrib_0_buffer_id_);
glVertexAttribPointer(0, 1, GL_FLOAT, GL_FALSE, 0, NULL);
glBindBuffer(GL_ARRAY_BUFFER, 0);
texture_units_.reset(
new TextureUnit[group_->max_texture_units()]);
for (uint32 tt = 0; tt < group_->max_texture_units(); ++tt) {
texture_units_[tt].bound_texture_2d =
texture_manager()->GetDefaultTextureInfo(GL_TEXTURE_2D);
texture_units_[tt].bound_texture_cube_map =
texture_manager()->GetDefaultTextureInfo(GL_TEXTURE_CUBE_MAP);
}
GLuint ids[2];
glGenTextures(2, ids);
// Make black textures for replacing non-renderable textures.
black_2d_texture_id_ = ids[0];
black_cube_texture_id_ = ids[1];
static uint8 black[] = {0, 0, 0, 255};
glBindTexture(GL_TEXTURE_2D, black_2d_texture_id_);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA,
GL_UNSIGNED_BYTE, black);
glBindTexture(GL_TEXTURE_2D, 0);
glBindTexture(GL_TEXTURE_CUBE_MAP, black_cube_texture_id_);
for (int ii = 0; ii < GLES2Util::kNumFaces; ++ii) {
glTexImage2D(GLES2Util::IndexToGLFaceTarget(ii), 0, GL_RGBA, 1, 1, 0,
GL_RGBA, GL_UNSIGNED_BYTE, black);
}
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
CHECK_GL_ERROR();
if (context_->IsOffscreen()) {
// Create the target frame buffer. This is the one that the client renders
// directly to.
offscreen_target_frame_buffer_.reset(new FrameBuffer(this));
offscreen_target_frame_buffer_->Create();
offscreen_target_color_texture_.reset(new Texture(this));
offscreen_target_color_texture_->Create();
offscreen_target_depth_render_buffer_.reset(
new RenderBuffer(this));
offscreen_target_depth_render_buffer_->Create();
offscreen_target_stencil_render_buffer_.reset(
new RenderBuffer(this));
offscreen_target_stencil_render_buffer_->Create();
// Create the saved offscreen texture. The target frame buffer is copied
// here when SwapBuffers is called.
offscreen_saved_color_texture_.reset(new Texture(this));
offscreen_saved_color_texture_->Create();
// Map the ID of the saved offscreen texture into the parent so that
// it can reference it.
if (parent_) {
GLuint service_id = offscreen_saved_color_texture_->id();
TextureManager::TextureInfo* info =
parent_->CreateTextureInfo(parent_client_texture_id, service_id);
parent_->texture_manager()->SetInfoTarget(info, GL_TEXTURE_2D);
}
// Allocate the render buffers at their initial size and check the status
// of the frame buffers is okay.
pending_offscreen_size_ = size;
if (!UpdateOffscreenFrameBufferSize()) {
DLOG(ERROR) << "Could not allocate offscreen buffer storage.";
Destroy();
return false;
}
// Bind to the new default frame buffer (the offscreen target frame buffer).
// This should now be associated with ID zero.
DoBindFramebuffer(GL_FRAMEBUFFER, 0);
}
// OpenGL ES 2.0 implicitly enables the desktop GL capability
// VERTEX_PROGRAM_POINT_SIZE and doesn't expose this enum. This fact
// isn't well documented; it was discovered in the Khronos OpenGL ES
// mailing list archives.
if (gfx::GetGLImplementation() != gfx::kGLImplementationEGLGLES2) {
glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
}
#if defined(GLES2_GPU_SERVICE_TRANSLATE_SHADER)
// Initialize GLSL ES to GLSL translator.
static bool glsl_translator_initialized = false;
if (!glsl_translator_initialized) {
if (!ShInitialize()) {
DLOG(ERROR) << "Could not initialize GLSL translator.";
Destroy();
return false;
}
glsl_translator_initialized = true;
}
#endif // GLES2_GPU_SERVICE_TRANSLATE_SHADER
return true;
}
bool GLES2DecoderImpl::GenBuffersHelper(GLsizei n, const GLuint* client_ids) {
for (GLsizei ii = 0; ii < n; ++ii) {
if (GetBufferInfo(client_ids[ii])) {
return false;
}
}
scoped_array<GLuint> service_ids(new GLuint[n]);
glGenBuffersARB(n, service_ids.get());
for (GLsizei ii = 0; ii < n; ++ii) {
CreateBufferInfo(client_ids[ii], service_ids[ii]);
}
return true;
}
bool GLES2DecoderImpl::GenFramebuffersHelper(
GLsizei n, const GLuint* client_ids) {
for (GLsizei ii = 0; ii < n; ++ii) {
if (GetFramebufferInfo(client_ids[ii])) {
return false;
}
}
scoped_array<GLuint> service_ids(new GLuint[n]);
glGenFramebuffersEXT(n, service_ids.get());
for (GLsizei ii = 0; ii < n; ++ii) {
CreateFramebufferInfo(client_ids[ii], service_ids[ii]);
}
return true;
}
bool GLES2DecoderImpl::GenRenderbuffersHelper(
GLsizei n, const GLuint* client_ids) {
for (GLsizei ii = 0; ii < n; ++ii) {
if (GetRenderbufferInfo(client_ids[ii])) {
return false;
}
}
scoped_array<GLuint> service_ids(new GLuint[n]);
glGenRenderbuffersEXT(n, service_ids.get());
for (GLsizei ii = 0; ii < n; ++ii) {
CreateRenderbufferInfo(client_ids[ii], service_ids[ii]);
}
return true;
}
bool GLES2DecoderImpl::GenTexturesHelper(GLsizei n, const GLuint* client_ids) {
for (GLsizei ii = 0; ii < n; ++ii) {
if (GetTextureInfo(client_ids[ii])) {
return false;
}
}
scoped_array<GLuint> service_ids(new GLuint[n]);
glGenTextures(n, service_ids.get());
for (GLsizei ii = 0; ii < n; ++ii) {
CreateTextureInfo(client_ids[ii], service_ids[ii]);
}
return true;
}
void GLES2DecoderImpl::DeleteBuffersHelper(
GLsizei n, const GLuint* client_ids) {
for (GLsizei ii = 0; ii < n; ++ii) {
BufferManager::BufferInfo* info = GetBufferInfo(client_ids[ii]);
if (info) {
GLuint service_id = info->service_id();
glDeleteBuffersARB(1, &service_id);
RemoveBufferInfo(client_ids[ii]);
}
}
}
void GLES2DecoderImpl::DeleteFramebuffersHelper(
GLsizei n, const GLuint* client_ids) {
for (GLsizei ii = 0; ii < n; ++ii) {
FramebufferManager::FramebufferInfo* info =
GetFramebufferInfo(client_ids[ii]);
if (info) {
GLuint service_id = info->service_id();
glDeleteFramebuffersEXT(1, &service_id);
RemoveFramebufferInfo(client_ids[ii]);
}
}
}
void GLES2DecoderImpl::DeleteRenderbuffersHelper(
GLsizei n, const GLuint* client_ids) {
for (GLsizei ii = 0; ii < n; ++ii) {
RenderbufferManager::RenderbufferInfo* info =
GetRenderbufferInfo(client_ids[ii]);
if (info) {
GLuint service_id = info->service_id();
glDeleteRenderbuffersEXT(1, &service_id);
RemoveRenderbufferInfo(client_ids[ii]);
}
}
}
void GLES2DecoderImpl::DeleteTexturesHelper(
GLsizei n, const GLuint* client_ids) {
for (GLsizei ii = 0; ii < n; ++ii) {
TextureManager::TextureInfo* info = GetTextureInfo(client_ids[ii]);
if (info) {
GLuint service_id = info->service_id();
glDeleteTextures(1, &service_id);
RemoveTextureInfo(client_ids[ii]);
}
}
}
// } // anonymous namespace
bool GLES2DecoderImpl::MakeCurrent() {
return context_->MakeCurrent();
}
gfx::Size GLES2DecoderImpl::GetBoundFrameBufferSize() {
if (bound_framebuffer_ != 0) {
int width = 0;
int height = 0;
// Assume we have to have COLOR_ATTACHMENT0. Should we check for depth and
// stencil.
GLint fb_type = 0;
glGetFramebufferAttachmentParameterivEXT(
GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0,
GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
&fb_type);
switch (fb_type) {
case GL_RENDERBUFFER:
{
GLint renderbuffer_id = 0;
glGetFramebufferAttachmentParameterivEXT(
GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0,
GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
&renderbuffer_id);
if (renderbuffer_id != 0) {
glGetRenderbufferParameterivEXT(
GL_RENDERBUFFER,
GL_RENDERBUFFER_WIDTH,
&width);
glGetRenderbufferParameterivEXT(
GL_RENDERBUFFER,
GL_RENDERBUFFER_HEIGHT,
&height);
}
break;
}
case GL_TEXTURE:
{
GLint texture_id = 0;
glGetFramebufferAttachmentParameterivEXT(
GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0,
GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
&texture_id);
if (texture_id != 0) {
GLuint client_id = 0;
if (texture_manager()->GetClientId(texture_id, &client_id)) {
TextureManager::TextureInfo* texture_info =
GetTextureInfo(client_id);
if (texture_info) {
GLint level = 0;
GLint face = 0;
glGetFramebufferAttachmentParameterivEXT(
GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0,
GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
&level);
glGetFramebufferAttachmentParameterivEXT(
GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0,
GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE,
&face);
texture_info->GetLevelSize(
face ? face : GL_TEXTURE_2D, level, &width, &height);
}
}
}
break;
}
default:
// unknown so assume width and height are zero.
break;
}
return gfx::Size(width, height);
} else if (offscreen_target_color_texture_.get()) {
return offscreen_target_color_texture_->size();
} else {
return context_->GetSize();
}
}
bool GLES2DecoderImpl::UpdateOffscreenFrameBufferSize() {
if (offscreen_target_color_texture_->size() == pending_offscreen_size_)
return true;
// Reallocate the offscreen target buffers.
if (!offscreen_target_color_texture_->AllocateStorage(
pending_offscreen_size_)) {
return false;
}
if (gfx::GetGLImplementation() == gfx::kGLImplementationEGLGLES2) {
// ANGLE only allows 16-bit depth buffers to be requested. As it happens,
// it creates a 24-bit depth buffer behind the scenes.
// TODO(apatrick): Attempt to use a packed 24/8 depth stencil buffer here if
// the extension is available.
if (!offscreen_target_depth_render_buffer_->AllocateStorage(
pending_offscreen_size_, GL_DEPTH_COMPONENT16)) {
return false;
}
if (!offscreen_target_stencil_render_buffer_->AllocateStorage(
pending_offscreen_size_, GL_STENCIL_INDEX8)) {
return false;
}
} else {
if (!offscreen_target_depth_render_buffer_->AllocateStorage(
pending_offscreen_size_, GL_DEPTH24_STENCIL8)) {
return false;
}
}
// Attach the offscreen target buffers to the target frame buffer.
offscreen_target_frame_buffer_->AttachRenderTexture(
offscreen_target_color_texture_.get());
offscreen_target_frame_buffer_->AttachRenderBuffer(
GL_DEPTH_ATTACHMENT,
offscreen_target_depth_render_buffer_.get());
if (gfx::GetGLImplementation() == gfx::kGLImplementationEGLGLES2) {
offscreen_target_frame_buffer_->AttachRenderBuffer(
GL_STENCIL_ATTACHMENT,
offscreen_target_stencil_render_buffer_.get());
} else {
offscreen_target_frame_buffer_->AttachRenderBuffer(
GL_STENCIL_ATTACHMENT,
offscreen_target_depth_render_buffer_.get());
}
if (offscreen_target_frame_buffer_->CheckStatus() !=
GL_FRAMEBUFFER_COMPLETE) {
return false;
}
// TODO(apatrick): Fix this once ANGLE supports shared contexts.
// Clear offscreen frame buffer to its initial state. Use default GL context
// to ensure clear is not affected by client set state.
if (gfx::GetGLImplementation() != gfx::kGLImplementationEGLGLES2) {
ScopedDefaultGLContext scoped_context(this);
glBindFramebufferEXT(GL_FRAMEBUFFER,
offscreen_target_frame_buffer_->id());
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
if (glGetError() != GL_NO_ERROR)
return false;
}
if (parent_) {
// Adjust the saved offscreen color texture (only accessible to parent).
offscreen_saved_color_texture_->AllocateStorage(pending_offscreen_size_);
// Update the info about the offscreen saved color texture in the parent.
// The reference to the parent is a weak pointer and will become null if the
// parent is later destroyed.
GLuint service_id = offscreen_saved_color_texture_->id();
GLuint client_id;
CHECK(parent_->texture_manager()->GetClientId(service_id, &client_id));
TextureManager::TextureInfo* info = parent_->GetTextureInfo(client_id);
DCHECK(info);
texture_manager()->SetLevelInfo(
info,
GL_TEXTURE_2D,
0, // level
GL_RGBA,
pending_offscreen_size_.width(),
pending_offscreen_size_.height(),
1, // depth
0, // border
GL_RGBA,
GL_UNSIGNED_BYTE);
// Attach the saved offscreen color texture to a frame buffer so we can
// clear it with glClear.
offscreen_target_frame_buffer_->AttachRenderTexture(
offscreen_saved_color_texture_.get());
if (offscreen_target_frame_buffer_->CheckStatus() !=
GL_FRAMEBUFFER_COMPLETE) {
return false;
}
// TODO(apatrick): Fix this once ANGLE supports shared contexts.
// Clear the saved offscreen color texture. Use default GL context
// to ensure clear is not affected by client set state.
if (gfx::GetGLImplementation() != gfx::kGLImplementationEGLGLES2) {
ScopedDefaultGLContext scoped_context(this);
glBindFramebufferEXT(GL_FRAMEBUFFER,
offscreen_target_frame_buffer_->id());
glClear(GL_COLOR_BUFFER_BIT);
glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
if (glGetError() != GL_NO_ERROR)
return false;
}
// Re-attach the offscreen render texture to the target frame buffer.
offscreen_target_frame_buffer_->AttachRenderTexture(
offscreen_target_color_texture_.get());
}
return true;
}
void GLES2DecoderImpl::SetSwapBuffersCallback(Callback0::Type* callback) {
swap_buffers_callback_.reset(callback);
}
void GLES2DecoderImpl::Destroy() {
if (context_) {
MakeCurrent();
if (black_2d_texture_id_) {
glDeleteTextures(1, &black_2d_texture_id_);
}
if (black_cube_texture_id_) {
glDeleteTextures(1, &black_cube_texture_id_);
}
if (attrib_0_buffer_id_) {
glDeleteBuffersARB(1, &attrib_0_buffer_id_);
}
// Remove the saved frame buffer mapping from the parent decoder. The
// parent pointer is a weak pointer so it will be null if the parent has
// already been destroyed.
if (parent_) {
// First check the texture has been mapped into the parent. This might not
// be the case if initialization failed midway through.
GLuint service_id = offscreen_saved_color_texture_->id();
GLuint client_id = 0;
if (parent_->texture_manager()->GetClientId(service_id, &client_id)) {
parent_->texture_manager()->RemoveTextureInfo(client_id);
}
}
if (offscreen_target_frame_buffer_.get()) {
offscreen_target_frame_buffer_->Destroy();
offscreen_target_frame_buffer_.reset();
}
if (offscreen_target_color_texture_.get()) {
offscreen_target_color_texture_->Destroy();
offscreen_target_color_texture_.reset();
}
if (offscreen_target_depth_render_buffer_.get()) {
offscreen_target_depth_render_buffer_->Destroy();
offscreen_target_depth_render_buffer_.reset();
}
if (offscreen_target_stencil_render_buffer_.get()) {
offscreen_target_stencil_render_buffer_->Destroy();
offscreen_target_stencil_render_buffer_.reset();
}
if (offscreen_saved_color_texture_.get()) {
offscreen_saved_color_texture_->Destroy();
offscreen_saved_color_texture_.reset();
}
}
if (default_context_.get()) {
default_context_->Destroy();
default_context_.reset();
}
}
void GLES2DecoderImpl::ResizeOffscreenFrameBuffer(const gfx::Size& size) {
// We can't resize the render buffers immediately because there might be a
// partial frame rendered into them and we don't want the tail end of that
// rendered into the reallocated storage. Defer until the next SwapBuffers.
pending_offscreen_size_ = size;
}
const char* GLES2DecoderImpl::GetCommandName(unsigned int command_id) const {
if (command_id > kStartPoint && command_id < kNumCommands) {
return gles2::GetCommandName(static_cast<CommandId>(command_id));
}
return GetCommonCommandName(static_cast<cmd::CommandId>(command_id));
}
// Decode command with its arguments, and call the corresponding GL function.
// Note: args is a pointer to the command buffer. As such, it could be changed
// by a (malicious) client at any time, so if validation has to happen, it
// should operate on a copy of them.
error::Error GLES2DecoderImpl::DoCommand(
unsigned int command,
unsigned int arg_count,
const void* cmd_data) {
error::Error result = error::kNoError;
if (debug()) {
// TODO(gman): Change output to something useful for NaCl.
DLOG(INFO) << "cmd: " << GetCommandName(command);
}
unsigned int command_index = command - kStartPoint - 1;
if (command_index < arraysize(g_command_info)) {
const CommandInfo& info = g_command_info[command_index];
unsigned int info_arg_count = static_cast<unsigned int>(info.arg_count);
if ((info.arg_flags == cmd::kFixed && arg_count == info_arg_count) ||
(info.arg_flags == cmd::kAtLeastN && arg_count >= info_arg_count)) {
uint32 immediate_data_size =
(arg_count - info_arg_count) * sizeof(CommandBufferEntry); // NOLINT
switch (command) {
#define GLES2_CMD_OP(name) \
case name::kCmdId: \
result = Handle ## name( \
immediate_data_size, \
*static_cast<const name*>(cmd_data)); \
break; \
GLES2_COMMAND_LIST(GLES2_CMD_OP)
#undef GLES2_CMD_OP
}
if (debug()) {
GLenum error;
while ((error = glGetError()) != GL_NO_ERROR) {
// TODO(gman): Change output to something useful for NaCl.
SetGLError(error, NULL);
DLOG(INFO) << "GL ERROR: " << error
<< " : " << GetCommandName(command);
}
}
} else {
result = error::kInvalidArguments;
}
} else {
result = DoCommonCommand(command, arg_count, cmd_data);
}
return result;
}
void GLES2DecoderImpl::RemoveBufferInfo(GLuint client_id) {
buffer_manager()->RemoveBufferInfo(client_id);
}
bool GLES2DecoderImpl::CreateProgramHelper(GLuint client_id) {
if (GetProgramInfo(client_id)) {
return false;
}
GLuint service_id = glCreateProgram();
if (service_id != 0) {
CreateProgramInfo(client_id, service_id);
}
return true;
}
bool GLES2DecoderImpl::CreateShaderHelper(GLenum type, GLuint client_id) {
if (GetShaderInfo(client_id)) {
return false;
}
GLuint service_id = glCreateShader(type);
if (service_id != 0) {
CreateShaderInfo(client_id, service_id, type);
}
return true;
}
void GLES2DecoderImpl::DoActiveTexture(GLenum texture_unit) {
GLuint texture_index = texture_unit - GL_TEXTURE0;
if (texture_index > group_->max_texture_units()) {
SetGLError(GL_INVALID_ENUM, "glActiveTexture: texture_unit out of range.");
return;
}
active_texture_unit_ = texture_index;
glActiveTexture(texture_unit);
}
void GLES2DecoderImpl::DoBindBuffer(GLenum target, GLuint client_id) {
BufferManager::BufferInfo* info = NULL;
GLuint service_id = 0;
if (client_id != 0) {
info = GetBufferInfo(client_id);
if (!info) {
// It's a new id so make a buffer info for it.
glGenBuffersARB(1, &service_id);
CreateBufferInfo(client_id, service_id);
info = GetBufferInfo(client_id);
IdAllocator* id_allocator =
group_->GetIdAllocator(id_namespaces::kBuffers);
id_allocator->MarkAsUsed(client_id);
}
}
if (info) {
if (!buffer_manager()->SetTarget(info, target)) {
SetGLError(GL_INVALID_OPERATION,
"glBindBuffer: buffer bound to more than 1 target");
return;
}
service_id = info->service_id();
}
switch (target) {
case GL_ARRAY_BUFFER:
bound_array_buffer_ = info;
break;
case GL_ELEMENT_ARRAY_BUFFER:
bound_element_array_buffer_ = info;
break;
default:
NOTREACHED(); // Validation should prevent us getting here.
break;
}
glBindBuffer(target, service_id);
}
void GLES2DecoderImpl::DoBindFramebuffer(GLenum target, GLuint client_id) {
FramebufferManager::FramebufferInfo* info = NULL;
GLuint service_id = 0;
if (client_id != 0) {
info = GetFramebufferInfo(client_id);
if (!info) {
// It's a new id so make a framebuffer info for it.
glGenFramebuffersEXT(1, &service_id);
CreateFramebufferInfo(client_id, service_id);
info = GetFramebufferInfo(client_id);
IdAllocator* id_allocator =
group_->GetIdAllocator(id_namespaces::kFramebuffers);
id_allocator->MarkAsUsed(client_id);
} else {
service_id = info->service_id();
}
}
bound_framebuffer_ = info;
// When rendering to an offscreen frame buffer, instead of unbinding from
// the current frame buffer, bind to the offscreen target frame buffer.
if (info == NULL && offscreen_target_frame_buffer_.get())
service_id = offscreen_target_frame_buffer_->id();
glBindFramebufferEXT(target, service_id);
}
void GLES2DecoderImpl::DoBindRenderbuffer(GLenum target, GLuint client_id) {
RenderbufferManager::RenderbufferInfo* info = NULL;
GLuint service_id = 0;
if (client_id != 0) {
info = GetRenderbufferInfo(client_id);
if (!info) {
// It's a new id so make a renderbuffer info for it.
glGenRenderbuffersEXT(1, &service_id);
CreateRenderbufferInfo(client_id, service_id);
info = GetRenderbufferInfo(client_id);
IdAllocator* id_allocator =
group_->GetIdAllocator(id_namespaces::kRenderbuffers);
id_allocator->MarkAsUsed(client_id);
} else {
service_id = info->service_id();
}
}
bound_renderbuffer_ = info;
glBindRenderbufferEXT(target, service_id);
}
void GLES2DecoderImpl::DoBindTexture(GLenum target, GLuint client_id) {
TextureManager::TextureInfo* info = NULL;
GLuint service_id = 0;
if (client_id != 0) {
info = GetTextureInfo(client_id);
if (!info) {
// It's a new id so make a texture info for it.
glGenTextures(1, &service_id);
CreateTextureInfo(client_id, service_id);
info = GetTextureInfo(client_id);
IdAllocator* id_allocator =
group_->GetIdAllocator(id_namespaces::kTextures);
id_allocator->MarkAsUsed(client_id);
}
} else {
info = texture_manager()->GetDefaultTextureInfo(target);
}
// Check the texture exists
// Check that we are not trying to bind it to a different target.
if (info->target() != 0 && info->target() != target) {
SetGLError(GL_INVALID_OPERATION,
"glBindTexture: texture bound to more than 1 target.");
return;
}
if (info->target() == 0) {
texture_manager()->SetInfoTarget(info, target);
}
glBindTexture(target, info->service_id());
TextureUnit& unit = texture_units_[active_texture_unit_];
unit.bind_target = target;
switch (target) {
case GL_TEXTURE_2D:
unit.bound_texture_2d = info;
break;
case GL_TEXTURE_CUBE_MAP:
unit.bound_texture_cube_map = info;
break;
default:
NOTREACHED(); // Validation should prevent us getting here.
break;
}
}
void GLES2DecoderImpl::DoDisableVertexAttribArray(GLuint index) {
if (vertex_attrib_manager_.Enable(index, false)) {
if (index != 0) {
glDisableVertexAttribArray(index);
}
} else {
SetGLError(GL_INVALID_VALUE,
"glDisableVertexAttribArray: index out of range");
}
}
void GLES2DecoderImpl::DoEnableVertexAttribArray(GLuint index) {
if (vertex_attrib_manager_.Enable(index, true)) {
glEnableVertexAttribArray(index);
} else {
SetGLError(GL_INVALID_VALUE,
"glEnableVertexAttribArray: index out of range");
}
}
void GLES2DecoderImpl::DoGenerateMipmap(GLenum target) {
TextureManager::TextureInfo* info = GetTextureInfoForTarget(target);
if (!info || !texture_manager()->MarkMipmapsGenerated(info)) {
SetGLError(GL_INVALID_OPERATION,
"glGenerateMipmaps: Can not generate mips for npot textures");
return;
}
glGenerateMipmapEXT(target);
}
bool GLES2DecoderImpl::GetHelper(
GLenum pname, GLint* params, GLsizei* num_written) {
DCHECK(num_written);
if (gfx::GetGLImplementation() != gfx::kGLImplementationEGLGLES2) {
switch (pname) {
case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
*num_written = 1;
if (params) {
*params = GL_RGBA; // TODO(gman): get correct format.
}
return true;
case GL_IMPLEMENTATION_COLOR_READ_TYPE:
*num_written = 1;
if (params) {
*params = GL_UNSIGNED_BYTE; // TODO(gman): get correct type.
}
return true;
case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
*num_written = 1;
if (params) {
*params = group_->max_fragment_uniform_vectors();
}
return true;
case GL_MAX_VARYING_VECTORS:
*num_written = 1;
if (params) {
*params = group_->max_varying_vectors();
}
return true;
case GL_MAX_VERTEX_UNIFORM_VECTORS:
*num_written = 1;
if (params) {
*params = group_->max_vertex_uniform_vectors();
}
return true;
}
}
switch (pname) {
case GL_COMPRESSED_TEXTURE_FORMATS:
*num_written = 0;
// We don't support compressed textures.
return true;
case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
*num_written = 1;
if (params) {
*params = 0; // We don't support compressed textures.
}
return true;
case GL_NUM_SHADER_BINARY_FORMATS:
*num_written = 1;
if (params) {
*params = 0; // We don't support binary shader formats.
}
return true;
case GL_SHADER_BINARY_FORMATS:
*num_written = 0;
return true; // We don't support binary shader format.s
case GL_SHADER_COMPILER:
*num_written = 1;
if (params) {
*params = GL_TRUE;
}
return true;
case GL_ARRAY_BUFFER_BINDING:
*num_written = 1;
if (params) {
if (bound_array_buffer_) {
GLuint client_id = 0;
buffer_manager()->GetClientId(bound_array_buffer_->service_id(),
&client_id);
*params = client_id;
} else {
*params = 0;
}
}
return true;
case GL_ELEMENT_ARRAY_BUFFER_BINDING:
*num_written = 1;
if (params) {
if (bound_element_array_buffer_) {
GLuint client_id = 0;
buffer_manager()->GetClientId(
bound_element_array_buffer_->service_id(),
&client_id);
*params = client_id;
} else {
*params = 0;
}
}
return true;
case GL_FRAMEBUFFER_BINDING:
*num_written = 1;
if (params) {
if (bound_framebuffer_) {
GLuint client_id = 0;
framebuffer_manager()->GetClientId(
bound_framebuffer_->service_id(), &client_id);
*params = client_id;
} else {
*params = 0;
}
}
return true;
case GL_RENDERBUFFER_BINDING:
*num_written = 1;
if (params) {
if (bound_renderbuffer_) {
GLuint client_id = 0;
renderbuffer_manager()->GetClientId(
bound_renderbuffer_->service_id(), &client_id);
*params = client_id;
} else {
*params = 0;
}
}
return true;
case GL_CURRENT_PROGRAM:
*num_written = 1;
if (params) {
if (current_program_) {
GLuint client_id = 0;
program_manager()->GetClientId(
current_program_->service_id(), &client_id);
*params = client_id;
} else {
*params = 0;
}
}
return true;
case GL_TEXTURE_BINDING_2D:
*num_written = 1;
if (params) {
TextureUnit& unit = texture_units_[active_texture_unit_];
if (unit.bound_texture_2d) {
GLuint client_id = 0;
texture_manager()->GetClientId(
unit.bound_texture_2d->service_id(), &client_id);
*params = client_id;
} else {
*params = 0;
}
}
return true;
case GL_TEXTURE_BINDING_CUBE_MAP:
*num_written = 1;
if (params) {
TextureUnit& unit = texture_units_[active_texture_unit_];
if (unit.bound_texture_cube_map) {
GLuint client_id = 0;
texture_manager()->GetClientId(
unit.bound_texture_cube_map->service_id(), &client_id);
*params = client_id;
} else {
*params = 0;
}
}
return true;
default:
*num_written = util_.GLGetNumValuesReturned(pname);
if (params) {
glGetIntegerv(pname, params);
}
return true;
}
}
bool GLES2DecoderImpl::GetNumValuesReturnedForGLGet(
GLenum pname, GLsizei* num_values) {
return GetHelper(pname, NULL, num_values);
}
void GLES2DecoderImpl::DoGetBooleanv(GLenum pname, GLboolean* params) {
DCHECK(params);
GLsizei num_written = 0;
if (GetHelper(pname, NULL, &num_written)) {
scoped_array<GLint> values(new GLint[num_written]);
GetHelper(pname, values.get(), &num_written);
for (GLsizei ii = 0; ii < num_written; ++ii) {
params[ii] = static_cast<GLboolean>(values[ii]);
}
} else {
glGetBooleanv(pname, params);
}
}
void GLES2DecoderImpl::DoGetFloatv(GLenum pname, GLfloat* params) {
DCHECK(params);
GLsizei num_written = 0;
if (GetHelper(pname, NULL, &num_written)) {
scoped_array<GLint> values(new GLint[num_written]);
GetHelper(pname, values.get(), &num_written);
for (GLsizei ii = 0; ii < num_written; ++ii) {
params[ii] = static_cast<GLfloat>(values[ii]);
}
} else {
glGetFloatv(pname, params);
}
}
void GLES2DecoderImpl::DoGetIntegerv(GLenum pname, GLint* params) {
DCHECK(params);
GLsizei num_written;
if (!GetHelper(pname, params, &num_written)) {
glGetIntegerv(pname, params);
}
}
void GLES2DecoderImpl::DoGetProgramiv(
GLuint program_id, GLenum pname, GLint* params) {
ProgramManager::ProgramInfo* info = GetProgramInfoNotShader(
program_id, "glGetProgramiv");
if (!info) {
return;
}
info->GetProgramiv(pname, params);
}
error::Error GLES2DecoderImpl::HandleBindAttribLocation(
uint32 immediate_data_size, const gles2::BindAttribLocation& c) {
GLuint program = static_cast<GLuint>(c.program);
ProgramManager::ProgramInfo* info = GetProgramInfoNotShader(
program, "glBindAttribLocation");
if (!info) {
return error::kNoError;
}
GLuint index = static_cast<GLuint>(c.index);
uint32 name_size = c.data_size;
const char* name = GetSharedMemoryAs<const char*>(
c.name_shm_id, c.name_shm_offset, name_size);
if (name == NULL) {
return error::kOutOfBounds;
}
String name_str(name, name_size);
glBindAttribLocation(info->service_id(), index, name_str.c_str());
return error::kNoError;
}
error::Error GLES2DecoderImpl::HandleBindAttribLocationImmediate(
uint32 immediate_data_size, const gles2::BindAttribLocationImmediate& c) {
GLuint program = static_cast<GLuint>(c.program);
ProgramManager::ProgramInfo* info = GetProgramInfoNotShader(
program, "glBindAttribLocation");
if (!info) {
return error::kNoError;
}
GLuint index = static_cast<GLuint>(c.index);
uint32 name_size = c.data_size;
const char* name = GetImmediateDataAs<const char*>(
c, name_size, immediate_data_size);
if (name == NULL) {
return error::kOutOfBounds;
}
String name_str(name, name_size);
glBindAttribLocation(info->service_id(), index, name_str.c_str());
return error::kNoError;
}
error::Error GLES2DecoderImpl::HandleBindAttribLocationBucket(
uint32 immediate_data_size, const gles2::BindAttribLocationBucket& c) {
GLuint program = static_cast<GLuint>(c.program);
ProgramManager::ProgramInfo* info = GetProgramInfoNotShader(
program, "glBindAttribLocation");
if (!info) {
return error::kNoError;
}
GLuint index = static_cast<GLuint>(c.index);
Bucket* bucket = GetBucket(c.name_bucket_id);
if (!bucket || bucket->size() == 0) {
return error::kInvalidArguments;
}
std::string name_str;
if (!bucket->GetAsString(&name_str)) {
return error::kInvalidArguments;
}
glBindAttribLocation(info->service_id(), index, name_str.c_str());
return error::kNoError;
}
error::Error GLES2DecoderImpl::HandleDeleteShader(
uint32 immediate_data_size, const gles2::DeleteShader& c) {
GLuint client_id = c.shader;
if (client_id) {
ShaderManager::ShaderInfo* info = GetShaderInfo(client_id);
if (info) {
glDeleteShader(info->service_id());
RemoveShaderInfo(client_id);
} else {
SetGLError(GL_INVALID_VALUE, "glDeleteShader: unknown shader");
}
}
return error::kNoError;
}
error::Error GLES2DecoderImpl::HandleDeleteProgram(
uint32 immediate_data_size, const gles2::DeleteProgram& c) {
GLuint client_id = c.program;
if (client_id) {
ProgramManager::ProgramInfo* info = GetProgramInfo(client_id);
if (info) {
glDeleteProgram(info->service_id());
RemoveProgramInfo(client_id);
} else {
SetGLError(GL_INVALID_VALUE, "glDeleteProgram: unknown program");
}
}
return error::kNoError;
}
void GLES2DecoderImpl::DoDeleteSharedIds(
GLuint namespace_id, GLsizei n, const GLuint* ids) {
IdAllocator* id_allocator = group_->GetIdAllocator(namespace_id);
for (GLsizei ii = 0; ii < n; ++ii) {
id_allocator->FreeID(ids[ii]);
}
}
error::Error GLES2DecoderImpl::HandleDeleteSharedIds(
uint32 immediate_data_size, const gles2::DeleteSharedIds& c) {
GLuint namespace_id = static_cast<GLuint>(c.namespace_id);
GLsizei n = static_cast<GLsizei>(c.n);
uint32 data_size;
if (!SafeMultiplyUint32(n, sizeof(GLuint), &data_size)) {
return error::kOutOfBounds;
}
const GLuint* ids = GetSharedMemoryAs<const GLuint*>(
c.ids_shm_id, c.ids_shm_offset, data_size);
if (n < 0) {
SetGLError(GL_INVALID_VALUE, "DeleteSharedIds: n < 0");
return error::kNoError;
}
if (ids == NULL) {
return error::kOutOfBounds;
}
DoDeleteSharedIds(namespace_id, n, ids);
return error::kNoError;
}
void GLES2DecoderImpl::DoGenSharedIds(
GLuint namespace_id, GLuint id_offset, GLsizei n, GLuint* ids) {
IdAllocator* id_allocator = group_->GetIdAllocator(namespace_id);
if (id_offset == 0) {
for (GLsizei ii = 0; ii < n; ++ii) {
ids[ii] = id_allocator->AllocateID();
}
} else {
for (GLsizei ii = 0; ii < n; ++ii) {
ids[ii] = id_allocator->AllocateIDAtOrAbove(id_offset);
id_offset = ids[ii] + 1;
}
}
}
error::Error GLES2DecoderImpl::HandleGenSharedIds(
uint32 immediate_data_size, const gles2::GenSharedIds& c) {
GLuint namespace_id = static_cast<GLuint>(c.namespace_id);
GLuint id_offset = static_cast<GLuint>(c.id_offset);
GLsizei n = static_cast<GLsizei>(c.n);
uint32 data_size;
if (!SafeMultiplyUint32(n, sizeof(GLuint), &data_size)) {
return error::kOutOfBounds;
}
GLuint* ids = GetSharedMemoryAs<GLuint*>(
c.ids_shm_id, c.ids_shm_offset, data_size);
if (n < 0) {
SetGLError(GL_INVALID_VALUE, "GenSharedIds: n < 0");
return error::kNoError;
}
if (ids == NULL) {
return error::kOutOfBounds;
}
DoGenSharedIds(namespace_id, id_offset, n, ids);
return error::kNoError;
}
void GLES2DecoderImpl::DoRegisterSharedIds(
GLuint namespace_id, GLsizei n, const GLuint* ids) {
IdAllocator* id_allocator = group_->GetIdAllocator(namespace_id);
for (GLsizei ii = 0; ii < n; ++ii) {
if (!id_allocator->MarkAsUsed(ids[ii])) {
for (GLsizei jj = 0; jj < ii; ++jj) {
id_allocator->FreeID(ids[jj]);
}
SetGLError(
GL_INVALID_VALUE,
"RegisterSharedIds: attempt to register id that already exists");
return;
}
}
}
error::Error GLES2DecoderImpl::HandleRegisterSharedIds(
uint32 immediate_data_size, const gles2::RegisterSharedIds& c) {
GLuint namespace_id = static_cast<GLuint>(c.namespace_id);
GLsizei n = static_cast<GLsizei>(c.n);
uint32 data_size;
if (!SafeMultiplyUint32(n, sizeof(GLuint), &data_size)) {
return error::kOutOfBounds;
}
GLuint* ids = GetSharedMemoryAs<GLuint*>(
c.ids_shm_id, c.ids_shm_offset, data_size);
if (n < 0) {
SetGLError(GL_INVALID_VALUE, "RegisterSharedIds: n < 0");
return error::kNoError;
}
if (ids == NULL) {
return error::kOutOfBounds;
}
DoRegisterSharedIds(namespace_id, n, ids);
return error::kNoError;
}
void GLES2DecoderImpl::DoDrawArrays(
GLenum mode, GLint first, GLsizei count) {
// We have to check this here because the prototype for glDrawArrays
// is GLint not GLsizei.
if (first < 0) {
SetGLError(GL_INVALID_ENUM, "glDrawArrays: first < 0");
return;
}
if (IsDrawValid(first + count - 1)) {
bool simulated_attrib_0 = SimulateAttrib0(first + count - 1);
bool textures_set = SetBlackTextureForNonRenderableTextures();
glDrawArrays(mode, first, count);
if (textures_set) {
RestoreStateForNonRenderableTextures();
}
if (simulated_attrib_0) {
RestoreStateForSimulatedAttrib0();
}
}
}
void GLES2DecoderImpl::DoFramebufferRenderbuffer(
GLenum target, GLenum attachment, GLenum renderbuffertarget,
GLuint client_renderbuffer_id) {
if (!bound_framebuffer_) {
SetGLError(GL_INVALID_OPERATION,
"glFramebufferRenderbuffer: no framebuffer bound");
return;
}
GLuint service_id = 0;
if (client_renderbuffer_id) {
RenderbufferManager::RenderbufferInfo* info =
GetRenderbufferInfo(client_renderbuffer_id);
if (!info) {
SetGLError(GL_INVALID_OPERATION,
"glFramebufferRenderbuffer: unknown renderbuffer");
return;
}
service_id = info->service_id();
}
glFramebufferRenderbufferEXT(
target, attachment, renderbuffertarget, service_id);
}
GLenum GLES2DecoderImpl::DoCheckFramebufferStatus(GLenum target) {
if (!bound_framebuffer_) {
return GL_FRAMEBUFFER_COMPLETE;
}
return glCheckFramebufferStatusEXT(target);
}
void GLES2DecoderImpl::DoFramebufferTexture2D(
GLenum target, GLenum attachment, GLenum textarget,
GLuint client_texture_id, GLint level) {
if (!bound_framebuffer_) {
SetGLError(GL_INVALID_OPERATION,
"glFramebufferTexture2D: no framebuffer bound.");
return;
}
GLuint service_id = 0;
if (client_texture_id) {
TextureManager::TextureInfo* info = GetTextureInfo(client_texture_id);
if (!info) {
SetGLError(GL_INVALID_OPERATION,
"glFramebufferTexture2D: unknown texture");
return;
}
service_id = info->service_id();
}
glFramebufferTexture2DEXT(target, attachment, textarget, service_id, level);
}
void GLES2DecoderImpl::DoGetFramebufferAttachmentParameteriv(
GLenum target, GLenum attachment, GLenum pname, GLint* params) {
if (!bound_framebuffer_) {
SetGLError(GL_INVALID_OPERATION,
"glFramebufferAttachmentParameteriv: no framebuffer bound");
return;
}
glGetFramebufferAttachmentParameterivEXT(target, attachment, pname, params);
}
void GLES2DecoderImpl::DoGetRenderbufferParameteriv(
GLenum target, GLenum pname, GLint* params) {
if (!bound_renderbuffer_) {
SetGLError(GL_INVALID_OPERATION,
"glGetRenderbufferParameteriv: no renderbuffer bound");
return;
}
if (pname == GL_RENDERBUFFER_INTERNAL_FORMAT) {
*params = bound_renderbuffer_->internal_format();
return;
}
glGetRenderbufferParameterivEXT(target, pname, params);
}
void GLES2DecoderImpl::DoRenderbufferStorage(
GLenum target, GLenum internalformat, GLsizei width, GLsizei height) {
if (!bound_renderbuffer_) {
SetGLError(GL_INVALID_OPERATION,
"glGetRenderbufferStorage: no renderbuffer bound");
return;
}
bound_renderbuffer_->set_internal_format(internalformat);
#if !defined(GLES2_GPU_SERVICE_BACKEND_NATIVE_GLES2)
switch (internalformat) {
case GL_DEPTH_COMPONENT16:
internalformat = GL_DEPTH_COMPONENT;
break;
case GL_RGBA4:
case GL_RGB5_A1:
internalformat = GL_RGBA;
break;
case GL_RGB565:
internalformat = GL_RGB;
break;
}
#endif // GLES2_GPU_SERVICE_BACKEND_NATIVE_GLES2
glRenderbufferStorageEXT(target, internalformat, width, height);
}
void GLES2DecoderImpl::DoLinkProgram(GLuint program) {
ProgramManager::ProgramInfo* info = GetProgramInfoNotShader(
program, "glLinkProgram");
if (!info) {
return;
}
if (!info->CanLink()) {
return;
}
glLinkProgram(info->service_id());
GLint success = 0;
glGetProgramiv(info->service_id(), GL_LINK_STATUS, &success);
if (success) {
info->Update();
} else {
info->Reset();
}
};
void GLES2DecoderImpl::DoTexParameterf(
GLenum target, GLenum pname, GLfloat param) {
TextureManager::TextureInfo* info = GetTextureInfoForTarget(target);
if (!info) {
SetGLError(GL_INVALID_VALUE, "glTexParameterf: unknown texture");
} else {
texture_manager()->SetParameter(info, pname, static_cast<GLint>(param));
glTexParameterf(target, pname, param);
}
}
void GLES2DecoderImpl::DoTexParameteri(
GLenum target, GLenum pname, GLint param) {
TextureManager::TextureInfo* info = GetTextureInfoForTarget(target);
if (!info) {
SetGLError(GL_INVALID_VALUE, "glTexParameteri: unknown texture");
} else {
texture_manager()->SetParameter(info, pname, param);
glTexParameteri(target, pname, param);
}
}
void GLES2DecoderImpl::DoTexParameterfv(
GLenum target, GLenum pname, const GLfloat* params) {
TextureManager::TextureInfo* info = GetTextureInfoForTarget(target);
if (!info) {
SetGLError(GL_INVALID_VALUE, "glTexParameterfv: unknown texture");
} else {
texture_manager()->SetParameter(
info, pname, *reinterpret_cast<const GLint*>(params));
glTexParameterfv(target, pname, params);
}
}
void GLES2DecoderImpl::DoTexParameteriv(
GLenum target, GLenum pname, const GLint* params) {
TextureManager::TextureInfo* info = GetTextureInfoForTarget(target);
if (!info) {
SetGLError(GL_INVALID_VALUE, "glTexParameteriv: unknown texture");
} else {
texture_manager()->SetParameter(info, pname, *params);
glTexParameteriv(target, pname, params);
}
}
bool GLES2DecoderImpl::CheckCurrentProgram(const char* function_name) {
if (!current_program_ || current_program_->IsDeleted()) {
// The program does not exist.
SetGLError(GL_INVALID_OPERATION,
(std::string(function_name) + ": no program in use").c_str());
return false;
}
if (!current_program_->IsValid()) {
SetGLError(GL_INVALID_OPERATION,
(std::string(function_name) + ": program not linked").c_str());
return false;
}
return true;
}
bool GLES2DecoderImpl::CheckCurrentProgramForUniform(
GLint location, const char* function_name) {
if (!CheckCurrentProgram(function_name)) {
return false;
}
return location != -1;
}
bool GLES2DecoderImpl::GetUniformTypeByLocation(
GLint location, const char* function_name, GLenum* type) {
if (!CheckCurrentProgramForUniform(location, function_name)) {
return false;
}
if (!current_program_->GetUniformTypeByLocation(location, type)) {
SetGLError(GL_INVALID_OPERATION,
(std::string(function_name) + ": program not linked").c_str());
return false;
}
return true;
}
void GLES2DecoderImpl::DoUniform1i(GLint location, GLint v0) {
if (!CheckCurrentProgramForUniform(location, "glUniform1i")) {
return;
}
current_program_->SetSamplers(location, 1, &v0);
glUniform1i(location, v0);
}
void GLES2DecoderImpl::DoUniform1iv(
GLint location, GLsizei count, const GLint *value) {
if (!CheckCurrentProgramForUniform(location, "glUniform1iv")) {
return;
}
current_program_->SetSamplers(location, count, value);
glUniform1iv(location, count, value);
}
void GLES2DecoderImpl::DoUniform1fv(
GLint location, GLsizei count, const GLfloat* value) {
GLenum type;
if (!GetUniformTypeByLocation(location, "glUniform1fv", &type)) {
return;
}
if (type == GL_BOOL) {
scoped_array<GLint> temp(new GLint[count]);
for (GLsizei ii = 0; ii < count; ++ii) {
temp[ii] = static_cast<GLint>(value[ii]);
}
DoUniform1iv(location, count, temp.get());
} else {
glUniform1fv(location, count, value);
}
}
void GLES2DecoderImpl::DoUniform2fv(
GLint location, GLsizei count, const GLfloat* value) {
GLenum type;
if (!GetUniformTypeByLocation(location, "glUniform2fv", &type)) {
return;
}
if (type == GL_BOOL_VEC2) {
GLsizei num_values = count * 2;
scoped_array<GLint> temp(new GLint[num_values]);
for (GLsizei ii = 0; ii < num_values; ++ii) {
temp[ii] = static_cast<GLint>(value[ii]);
}
glUniform2iv(location, count, temp.get());
} else {
glUniform2fv(location, count, value);
}
}
void GLES2DecoderImpl::DoUniform3fv(
GLint location, GLsizei count, const GLfloat* value) {
GLenum type;
if (!GetUniformTypeByLocation(location, "glUniform3fv", &type)) {
return;
}
if (type == GL_BOOL_VEC3) {
GLsizei num_values = count * 3;
scoped_array<GLint> temp(new GLint[num_values]);
for (GLsizei ii = 0; ii < num_values; ++ii) {
temp[ii] = static_cast<GLint>(value[ii]);
}
glUniform3iv(location, count, temp.get());
} else {
glUniform3fv(location, count, value);
}
}
void GLES2DecoderImpl::DoUniform4fv(
GLint location, GLsizei count, const GLfloat* value) {
GLenum type;
if (!GetUniformTypeByLocation(location, "glUniform4fv", &type)) {
return;
}
if (type == GL_BOOL_VEC4) {
GLsizei num_values = count * 4;
scoped_array<GLint> temp(new GLint[num_values]);
for (GLsizei ii = 0; ii < num_values; ++ii) {
temp[ii] = static_cast<GLint>(value[ii]);
}
glUniform4iv(location, count, temp.get());
} else {
glUniform4fv(location, count, value);
}
}
void GLES2DecoderImpl::DoUseProgram(GLuint program) {
GLuint service_id = 0;
ProgramManager::ProgramInfo* info = NULL;
if (program) {
info = GetProgramInfoNotShader(program, "glUseProgram");
if (!info) {
return;
}
if (!info->IsValid()) {
// Program was not linked successfully. (ie, glLinkProgram)
SetGLError(GL_INVALID_OPERATION, "glUseProgram: program not linked");
return;
}
service_id = info->service_id();
}
current_program_ = info;
glUseProgram(service_id);
}
GLenum GLES2DecoderImpl::GetGLError() {
// Check the GL error first, then our wrapped error.
GLenum error = glGetError();
if (error == GL_NO_ERROR && error_bits_ != 0) {
for (uint32 mask = 1; mask != 0; mask = mask << 1) {
if ((error_bits_ & mask) != 0) {
error = GLES2Util::GLErrorBitToGLError(mask);
break;
}
}
}
if (error != GL_NO_ERROR) {
// There was an error, clear the corresponding wrapped error.
error_bits_ &= ~GLES2Util::GLErrorToErrorBit(error);
}
return error;
}
void GLES2DecoderImpl::SetGLError(GLenum error, const char* msg) {
if (msg) {
last_error_ = msg;
DLOG(ERROR) << last_error_;
}
error_bits_ |= GLES2Util::GLErrorToErrorBit(error);
}
void GLES2DecoderImpl::CopyRealGLErrorsToWrapper() {
GLenum error;
while ((error = glGetError()) != GL_NO_ERROR) {
SetGLError(error, NULL);
}
}
void GLES2DecoderImpl::ClearRealGLErrors() {
GLenum error;
while ((error = glGetError()) != GL_NO_ERROR) {
NOTREACHED() << "GL error " << error << " was unhandled.";
}
}
bool GLES2DecoderImpl::SetBlackTextureForNonRenderableTextures() {
DCHECK(current_program_);
DCHECK(!current_program_->IsDeleted());
// Only check if there are some unrenderable textures.
if (!texture_manager()->HaveUnrenderableTextures()) {
return false;
}
bool textures_set = false;
const ProgramManager::ProgramInfo::SamplerIndices& sampler_indices =
current_program_->sampler_indices();
for (size_t ii = 0; ii < sampler_indices.size(); ++ii) {
const ProgramManager::ProgramInfo::UniformInfo* uniform_info =
current_program_->GetUniformInfo(sampler_indices[ii]);
DCHECK(uniform_info);
for (size_t jj = 0; jj < uniform_info->texture_units.size(); ++jj) {
GLuint texture_unit_index = uniform_info->texture_units[jj];
if (texture_unit_index < group_->max_texture_units()) {
TextureUnit& texture_unit = texture_units_[texture_unit_index];
TextureManager::TextureInfo* texture_info =
uniform_info->type == GL_SAMPLER_2D ?
texture_unit.bound_texture_2d :
texture_unit.bound_texture_cube_map;
if (!texture_info || !texture_info->CanRender()) {
textures_set = true;
glActiveTexture(GL_TEXTURE0 + texture_unit_index);
glBindTexture(
uniform_info->type == GL_SAMPLER_2D ? GL_TEXTURE_2D :
GL_TEXTURE_CUBE_MAP,
uniform_info->type == GL_SAMPLER_2D ? black_2d_texture_id_ :
black_cube_texture_id_);
}
}
// else: should this be an error?
}
}
return textures_set;
}
void GLES2DecoderImpl::RestoreStateForNonRenderableTextures() {
DCHECK(current_program_);
DCHECK(!current_program_->IsDeleted());
const ProgramManager::ProgramInfo::SamplerIndices& sampler_indices =
current_program_->sampler_indices();
for (size_t ii = 0; ii < sampler_indices.size(); ++ii) {
const ProgramManager::ProgramInfo::UniformInfo* uniform_info =
current_program_->GetUniformInfo(sampler_indices[ii]);
DCHECK(uniform_info);
for (size_t jj = 0; jj < uniform_info->texture_units.size(); ++jj) {
GLuint texture_unit_index = uniform_info->texture_units[jj];
if (texture_unit_index < group_->max_texture_units()) {
TextureUnit& texture_unit = texture_units_[texture_unit_index];
TextureManager::TextureInfo* texture_info =
uniform_info->type == GL_SAMPLER_2D ?
texture_unit.bound_texture_2d :
texture_unit.bound_texture_cube_map;
if (!texture_info || !texture_info->CanRender()) {
glActiveTexture(GL_TEXTURE0 + texture_unit_index);
// Get the texture info that was previously bound here.
texture_info = texture_unit.bind_target == GL_TEXTURE_2D ?
texture_unit.bound_texture_2d :
texture_unit.bound_texture_cube_map;
glBindTexture(texture_unit.bind_target,
texture_info ? texture_info->service_id() : 0);
}
}
}
}
// Set the active texture back to whatever the user had it as.
glActiveTexture(GL_TEXTURE0 + active_texture_unit_);
}
bool GLES2DecoderImpl::IsDrawValid(GLuint max_vertex_accessed) {
// NOTE: We specifically do not check current_program->IsValid() because
// it could never be invalid since glUseProgram would have failed. While
// glLinkProgram could later mark the program as invalid the previous
// valid program will still function if it is still the current program.
if (!current_program_ || current_program_->IsDeleted()) {
// The program does not exist.
// But GL says no ERROR.
return false;
}
// Validate all attribs currently enabled. If they are used by the current
// program then check that they have enough elements to handle the draw call.
// If they are not used by the current program check that they have a buffer
// assigned.
const VertexAttribManager::VertexAttribInfoList& infos =
vertex_attrib_manager_.GetEnabledVertexAttribInfos();
for (VertexAttribManager::VertexAttribInfoList::const_iterator it =
infos.begin(); it != infos.end(); ++it) {
const VertexAttribManager::VertexAttribInfo* info = *it;
const ProgramManager::ProgramInfo::VertexAttribInfo* attrib_info =
current_program_->GetAttribInfoByLocation(info->index());
if (attrib_info) {
// This attrib is used in the current program.
if (!info->CanAccess(max_vertex_accessed)) {
SetGLError(GL_INVALID_OPERATION,
"glDrawXXX: attempt to access out of range vertices");
return false;
}
} else {
// This attrib is not used in the current program.
if (!info->buffer() || info->buffer()->IsDeleted()) {
SetGLError(
GL_INVALID_OPERATION,
"glDrawXXX: attempt to render with no buffer attached to enabled "
"attrib");
return false;
}
}
}
return true;
}
bool GLES2DecoderImpl::SimulateAttrib0(GLuint max_vertex_accessed) {
#if !defined(GLES2_GPU_SERVICE_BACKEND_NATIVE_GLES2)
const VertexAttribManager::VertexAttribInfo* info =
vertex_attrib_manager_.GetVertexAttribInfo(0);
// If it's enabled or it's not used then we don't need to do anything.
if (info->enabled() || !current_program_->GetAttribInfoByLocation(0)) {
return false;
}
typedef VertexAttribManager::VertexAttribInfo::Vec4 Vec4;
glBindBuffer(GL_ARRAY_BUFFER, attrib_0_buffer_id_);
// Make a buffer with a single repeated vec4 value enough to
// simulate the constant value that is supposed to be here.
// This is required to emulate GLES2 on GL.
GLsizei num_vertices = max_vertex_accessed + 1;
GLsizei size_needed = num_vertices * sizeof(Vec4); // NOLINT
if (size_needed > attrib_0_size_ ||
info->value().v[0] != attrib_0_value_.v[0] ||
info->value().v[1] != attrib_0_value_.v[1] ||
info->value().v[2] != attrib_0_value_.v[2] ||
info->value().v[3] != attrib_0_value_.v[3]) {
scoped_array<Vec4> temp(new Vec4[num_vertices]);
for (GLsizei ii = 0; ii < num_vertices; ++ii) {
temp[ii] = info->value();
}
glBufferData(
GL_ARRAY_BUFFER,
size_needed,
&temp[0].v[0],
GL_DYNAMIC_DRAW);
attrib_0_value_ = info->value();
attrib_0_size_ = size_needed;
}
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, NULL);
return true;
#else // GLES2_GPU_SERVICE_BACKEND_NATIVE_GLES2
return false;
#endif // GLES2_GPU_SERVICE_BACKEND_NATIVE_GLES2
}
void GLES2DecoderImpl::RestoreStateForSimulatedAttrib0() {
const VertexAttribManager::VertexAttribInfo* info =
vertex_attrib_manager_.GetVertexAttribInfo(0);
const void* ptr = reinterpret_cast<const void*>(info->offset());
BufferManager::BufferInfo* buffer_info = info->buffer();
glBindBuffer(GL_ARRAY_BUFFER, buffer_info ? buffer_info->service_id() : 0);
glVertexAttribPointer(
0, info->size(), info->type(), info->normalized(), info->gl_stride(),
ptr);
glBindBuffer(GL_ARRAY_BUFFER,
bound_array_buffer_ ? bound_array_buffer_->service_id() : 0);
}
error::Error GLES2DecoderImpl::HandleDrawElements(
uint32 immediate_data_size, const gles2::DrawElements& c) {
if (!bound_element_array_buffer_ ||
bound_element_array_buffer_->IsDeleted()) {
SetGLError(GL_INVALID_OPERATION,
"glDrawElements: No element array buffer bound");
return error::kNoError;
}
GLenum mode = c.mode;
GLsizei count = c.count;
GLenum type = c.type;
int32 offset = c.index_offset;
if (count < 0) {
SetGLError(GL_INVALID_VALUE, "glDrawElements: count < 0");
return error::kNoError;
}
if (offset < 0) {
SetGLError(GL_INVALID_VALUE, "glDrawElements: offset < 0");
return error::kNoError;
}
if (!validators_->draw_mode.IsValid(mode)) {
SetGLError(GL_INVALID_ENUM, "glDrawElements: mode GL_INVALID_ENUM");
return error::kNoError;
}
if (!validators_->index_type.IsValid(type)) {
SetGLError(GL_INVALID_ENUM, "glDrawElements: type GL_INVALID_ENUM");
return error::kNoError;
}
GLuint max_vertex_accessed;
if (!bound_element_array_buffer_->GetMaxValueForRange(
offset, count, type, &max_vertex_accessed)) {
SetGLError(GL_INVALID_OPERATION,
"glDrawElements: range out of bounds for buffer");
return error::kNoError;
}
if (IsDrawValid(max_vertex_accessed)) {
bool simulated_attrib_0 = SimulateAttrib0(max_vertex_accessed);
bool textures_set = SetBlackTextureForNonRenderableTextures();
const GLvoid* indices = reinterpret_cast<const GLvoid*>(offset);
glDrawElements(mode, count, type, indices);
if (textures_set) {
RestoreStateForNonRenderableTextures();
}
if (simulated_attrib_0) {
RestoreStateForSimulatedAttrib0();
}
}
return error::kNoError;
}
GLuint GLES2DecoderImpl::DoGetMaxValueInBuffer(
GLuint buffer_id, GLsizei count, GLenum type, GLuint offset) {
GLuint max_vertex_accessed = 0;
BufferManager::BufferInfo* info = GetBufferInfo(buffer_id);
if (!info) {
// TODO(gman): Should this be a GL error or a command buffer error?
SetGLError(GL_INVALID_VALUE,
"GetMaxValueInBuffer: unknown buffer");
} else {
if (!info->GetMaxValueForRange(offset, count, type, &max_vertex_accessed)) {
// TODO(gman): Should this be a GL error or a command buffer error?
SetGLError(GL_INVALID_OPERATION,
"GetMaxValueInBuffer: range out of bounds for buffer");
}
}
return max_vertex_accessed;
}
// Calls glShaderSource for the various versions of the ShaderSource command.
// Assumes that data / data_size points to a piece of memory that is in range
// of whatever context it came from (shared memory, immediate memory, bucket
// memory.)
error::Error GLES2DecoderImpl::ShaderSourceHelper(
GLuint client_id, const char* data, uint32 data_size) {
ShaderManager::ShaderInfo* info = GetShaderInfoNotProgram(
client_id, "glShaderSource");
if (!info) {
return error::kNoError;
}
// Note: We don't actually call glShaderSource here. We wait until
// the call to glCompileShader.
info->Update(std::string(data, data + data_size));
return error::kNoError;
}
error::Error GLES2DecoderImpl::HandleShaderSource(
uint32 immediate_data_size, const gles2::ShaderSource& c) {
uint32 data_size = c.data_size;
const char* data = GetSharedMemoryAs<const char*>(
c.data_shm_id, c.data_shm_offset, data_size);
if (!data) {
return error::kOutOfBounds;
}
return ShaderSourceHelper(c.shader, data, data_size);
}
error::Error GLES2DecoderImpl::HandleShaderSourceImmediate(
uint32 immediate_data_size, const gles2::ShaderSourceImmediate& c) {
uint32 data_size = c.data_size;
const char* data = GetImmediateDataAs<const char*>(
c, data_size, immediate_data_size);
if (!data) {
return error::kOutOfBounds;
}
return ShaderSourceHelper(c.shader, data, data_size);
}
error::Error GLES2DecoderImpl::HandleShaderSourceBucket(
uint32 immediate_data_size, const gles2::ShaderSourceBucket& c) {
Bucket* bucket = GetBucket(c.data_bucket_id);
if (!bucket || bucket->size() == 0) {
return error::kInvalidArguments;
}
return ShaderSourceHelper(
c.shader, bucket->GetDataAs<const char*>(0, bucket->size() - 1),
bucket->size() - 1);
}
void GLES2DecoderImpl::DoCompileShader(GLuint client_id) {
ShaderManager::ShaderInfo* info = GetShaderInfoNotProgram(
client_id, "glCompileShader");
if (!info) {
return;
}
// Translate GL ES 2.0 shader to Desktop GL shader and pass that to
// glShaderSource and then glCompileShader.
const char* shader_src = info->source().c_str();
#if defined(GLES2_GPU_SERVICE_TRANSLATE_SHADER)
ShHandle compiler = 0;
if (use_shader_translator_) {
int dbg_options = 0;
EShLanguage language = info->shader_type() == GL_VERTEX_SHADER ?
EShLangVertex : EShLangFragment;
TBuiltInResource resources;
resources.maxVertexAttribs = group_->max_vertex_attribs();
resources.maxVertexUniformVectors =
group_->max_vertex_uniform_vectors();
resources.maxVaryingVectors = group_->max_varying_vectors();
resources.maxVertexTextureImageUnits =
group_->max_vertex_texture_image_units();
resources.maxCombinedTextureImageUnits = group_->max_texture_units();
resources.maxTextureImageUnits = group_->max_texture_image_units();
resources.maxFragmentUniformVectors =
group_->max_fragment_uniform_vectors();
resources.maxDrawBuffers = 1;
compiler = ShConstructCompiler(language, dbg_options);
if (!ShCompile(compiler, &shader_src, 1, EShOptNone, &resources,
dbg_options)) {
info->SetStatus(false, ShGetInfoLog(compiler));
ShDestruct(compiler);
return;
}
shader_src = ShGetObjectCode(compiler);
}
#endif // GLES2_GPU_SERVICE_TRANSLATE_SHADER
glShaderSource(info->service_id(), 1, &shader_src, NULL);
glCompileShader(info->service_id());
GLint status = GL_FALSE;
glGetShaderiv(info->service_id(), GL_COMPILE_STATUS, &status);
if (status) {
info->SetStatus(true, "");
} else {
GLint len = 0;
glGetShaderiv(info->service_id(), GL_INFO_LOG_LENGTH, &len);
scoped_array<char> temp(new char[len]);
glGetShaderInfoLog(info->service_id(), len, &len, temp.get());
info->SetStatus(false, std::string(temp.get(), len));
}
#ifdef GLES2_GPU_SERVICE_TRANSLATE_SHADER
if (use_shader_translator_) {
ShDestruct(compiler);
}
#endif // GLES2_GPU_SERVICE_TRANSLATE_SHADER
};
void GLES2DecoderImpl::DoGetShaderiv(
GLuint shader, GLenum pname, GLint* params) {
ShaderManager::ShaderInfo* info = GetShaderInfoNotProgram(
shader, "glGetShaderiv");
if (!info) {
return;
}
switch (pname) {
case GL_SHADER_SOURCE_LENGTH:
*params = info->source().size();
return;
case GL_COMPILE_STATUS:
*params = info->IsValid();
return;
case GL_INFO_LOG_LENGTH:
*params = info->log_info().size() + 1;
return;
default:
break;
}
glGetShaderiv(info->service_id(), pname, params);
}
error::Error GLES2DecoderImpl::HandleGetShaderSource(
uint32 immediate_data_size, const gles2::GetShaderSource& c) {
GLuint shader = c.shader;
uint32 bucket_id = static_cast<uint32>(c.bucket_id);
Bucket* bucket = CreateBucket(bucket_id);
ShaderManager::ShaderInfo* info = GetShaderInfoNotProgram(
shader, "glGetShaderSource");
if (!info) {
bucket->SetSize(0);
return error::kNoError;
}
bucket->SetFromString(info->source());
return error::kNoError;
}
error::Error GLES2DecoderImpl::HandleGetProgramInfoLog(
uint32 immediate_data_size, const gles2::GetProgramInfoLog& c) {
GLuint program = c.program;
uint32 bucket_id = static_cast<uint32>(c.bucket_id);
Bucket* bucket = CreateBucket(bucket_id);
ProgramManager::ProgramInfo* info = GetProgramInfoNotShader(
program, "glGetProgramInfoLog");
if (!info) {
return error::kNoError;
}
bucket->SetFromString(info->log_info());
return error::kNoError;
}
error::Error GLES2DecoderImpl::HandleGetShaderInfoLog(
uint32 immediate_data_size, const gles2::GetShaderInfoLog& c) {
GLuint shader = c.shader;
uint32 bucket_id = static_cast<uint32>(c.bucket_id);
Bucket* bucket = CreateBucket(bucket_id);
ShaderManager::ShaderInfo* info = GetShaderInfoNotProgram(
shader, "glGetShaderInfoLog");
if (!info) {
bucket->SetSize(0);
return error::kNoError;
}
bucket->SetFromString(info->log_info());
return error::kNoError;
}
bool GLES2DecoderImpl::DoIsBuffer(GLuint client_id) {
return GetBufferInfo(client_id) != NULL;
}
bool GLES2DecoderImpl::DoIsFramebuffer(GLuint client_id) {
return GetFramebufferInfo(client_id) != NULL;
}
bool GLES2DecoderImpl::DoIsProgram(GLuint client_id) {
return GetProgramInfo(client_id) != NULL;
}
bool GLES2DecoderImpl::DoIsRenderbuffer(GLuint client_id) {
return GetRenderbufferInfo(client_id) != NULL;
}
bool GLES2DecoderImpl::DoIsShader(GLuint client_id) {
return GetShaderInfo(client_id) != NULL;
}
bool GLES2DecoderImpl::DoIsTexture(GLuint client_id) {
return GetTextureInfo(client_id) != NULL;
}
void GLES2DecoderImpl::DoAttachShader(
GLuint program_client_id, GLint shader_client_id) {
ProgramManager::ProgramInfo* program_info = GetProgramInfoNotShader(
program_client_id, "glAttachShader");
if (!program_info) {
return;
}
ShaderManager::ShaderInfo* shader_info = GetShaderInfoNotProgram(
shader_client_id, "glAttachShader");
if (!shader_info) {
return;
}
program_info->AttachShader(shader_info);
glAttachShader(program_info->service_id(), shader_info->service_id());
}
void GLES2DecoderImpl::DoDetachShader(
GLuint program_client_id, GLint shader_client_id) {
ProgramManager::ProgramInfo* program_info = GetProgramInfoNotShader(
program_client_id, "glDetachShader");
if (!program_info) {
return;
}
ShaderManager::ShaderInfo* shader_info = GetShaderInfoNotProgram(
shader_client_id, "glDetachShader");
if (!shader_info) {
return;
}
program_info->DetachShader(shader_info);
glDetachShader(program_info->service_id(), shader_info->service_id());
}
void GLES2DecoderImpl::DoValidateProgram(GLuint program_client_id) {
ProgramManager::ProgramInfo* info = GetProgramInfoNotShader(
program_client_id, "glValidateProgram");
if (!info) {
return;
}
if (!info->CanLink()) {
info->set_log_info("Missing Shader");
return;
}
glValidateProgram(info->service_id());
info->UpdateLogInfo();
}
void GLES2DecoderImpl::DoGetVertexAttribfv(
GLuint index, GLenum pname, GLfloat* params) {
VertexAttribManager::VertexAttribInfo* info =
vertex_attrib_manager_.GetVertexAttribInfo(index);
if (!info) {
SetGLError(GL_INVALID_VALUE, "glGetVertexAttribfv: index out of range");
return;
}
switch (pname) {
case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING: {
BufferManager::BufferInfo* buffer = info->buffer();
if (buffer && !buffer->IsDeleted()) {
GLuint client_id;
buffer_manager()->GetClientId(buffer->service_id(), &client_id);
*params = static_cast<GLfloat>(client_id);
}
break;
}
case GL_VERTEX_ATTRIB_ARRAY_ENABLED:
*params = static_cast<GLfloat>(info->enabled());
break;
case GL_VERTEX_ATTRIB_ARRAY_SIZE:
*params = static_cast<GLfloat>(info->size());
break;
case GL_VERTEX_ATTRIB_ARRAY_STRIDE:
*params = static_cast<GLfloat>(info->gl_stride());
break;
case GL_VERTEX_ATTRIB_ARRAY_TYPE:
*params = static_cast<GLfloat>(info->type());
break;
case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED:
*params = static_cast<GLfloat>(info->normalized());
break;
case GL_CURRENT_VERTEX_ATTRIB:
params[0] = info->value().v[0];
params[1] = info->value().v[1];
params[2] = info->value().v[2];
params[3] = info->value().v[3];
break;
default:
NOTREACHED();
break;
}
}
void GLES2DecoderImpl::DoGetVertexAttribiv(
GLuint index, GLenum pname, GLint* params) {
VertexAttribManager::VertexAttribInfo* info =
vertex_attrib_manager_.GetVertexAttribInfo(index);
if (!info) {
SetGLError(GL_INVALID_VALUE, "glGetVertexAttribiv: index out of range");
return;
}
switch (pname) {
case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING: {
BufferManager::BufferInfo* buffer = info->buffer();
if (buffer && !buffer->IsDeleted()) {
GLuint client_id;
buffer_manager()->GetClientId(buffer->service_id(), &client_id);
*params = client_id;
}
break;
}
case GL_VERTEX_ATTRIB_ARRAY_ENABLED:
*params = info->enabled();
break;
case GL_VERTEX_ATTRIB_ARRAY_SIZE:
*params = info->size();
break;
case GL_VERTEX_ATTRIB_ARRAY_STRIDE:
*params = info->gl_stride();
break;
case GL_VERTEX_ATTRIB_ARRAY_TYPE:
*params = info->type();
break;
case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED:
*params = static_cast<GLint>(info->normalized());
break;
case GL_CURRENT_VERTEX_ATTRIB:
params[0] = static_cast<GLint>(info->value().v[0]);
params[1] = static_cast<GLint>(info->value().v[1]);
params[2] = static_cast<GLint>(info->value().v[2]);
params[3] = static_cast<GLint>(info->value().v[3]);
break;
default:
NOTREACHED();
break;
}
}
void GLES2DecoderImpl::DoVertexAttrib1f(GLuint index, GLfloat v0) {
VertexAttribManager::VertexAttribInfo* info =
vertex_attrib_manager_.GetVertexAttribInfo(index);
if (!info) {
SetGLError(GL_INVALID_VALUE, "glVertexAttrib1f: index out of range");
return;
}
VertexAttribManager::VertexAttribInfo::Vec4 value;
value.v[0] = v0;
value.v[1] = 0.0f;
value.v[2] = 0.0f;
value.v[3] = 1.0f;
info->set_value(value);
glVertexAttrib1f(index, v0);
}
void GLES2DecoderImpl::DoVertexAttrib2f(GLuint index, GLfloat v0, GLfloat v1) {
VertexAttribManager::VertexAttribInfo* info =
vertex_attrib_manager_.GetVertexAttribInfo(index);
if (!info) {
SetGLError(GL_INVALID_VALUE, "glVertexAttrib2f: index out of range");
return;
}
VertexAttribManager::VertexAttribInfo::Vec4 value;
value.v[0] = v0;
value.v[1] = v1;
value.v[2] = 0.0f;
value.v[3] = 1.0f;
info->set_value(value);
glVertexAttrib2f(index, v0, v1);
}
void GLES2DecoderImpl::DoVertexAttrib3f(
GLuint index, GLfloat v0, GLfloat v1, GLfloat v2) {
VertexAttribManager::VertexAttribInfo* info =
vertex_attrib_manager_.GetVertexAttribInfo(index);
if (!info) {
SetGLError(GL_INVALID_VALUE, "glVertexAttrib3f: index out of range");
return;
}
VertexAttribManager::VertexAttribInfo::Vec4 value;
value.v[0] = v0;
value.v[1] = v1;
value.v[2] = v2;
value.v[3] = 1.0f;
info->set_value(value);
glVertexAttrib3f(index, v0, v1, v2);
}
void GLES2DecoderImpl::DoVertexAttrib4f(
GLuint index, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3) {
VertexAttribManager::VertexAttribInfo* info =
vertex_attrib_manager_.GetVertexAttribInfo(index);
if (!info) {
SetGLError(GL_INVALID_VALUE, "glVertexAttrib4f: index out of range");
return;
}
VertexAttribManager::VertexAttribInfo::Vec4 value;
value.v[0] = v0;
value.v[1] = v1;
value.v[2] = v2;
value.v[3] = v3;
info->set_value(value);
glVertexAttrib4f(index, v0, v1, v2, v3);
}
void GLES2DecoderImpl::DoVertexAttrib1fv(GLuint index, const GLfloat* v) {
VertexAttribManager::VertexAttribInfo* info =
vertex_attrib_manager_.GetVertexAttribInfo(index);
if (!info) {
SetGLError(GL_INVALID_VALUE, "glVertexAttrib1fv: index out of range");
return;
}
VertexAttribManager::VertexAttribInfo::Vec4 value;
value.v[0] = v[0];
value.v[1] = 0.0f;
value.v[2] = 0.0f;
value.v[3] = 1.0f;
info->set_value(value);
glVertexAttrib1fv(index, v);
}
void GLES2DecoderImpl::DoVertexAttrib2fv(GLuint index, const GLfloat* v) {
VertexAttribManager::VertexAttribInfo* info =
vertex_attrib_manager_.GetVertexAttribInfo(index);
if (!info) {
SetGLError(GL_INVALID_VALUE, "glVertexAttrib2fv: index out of range");
return;
}
VertexAttribManager::VertexAttribInfo::Vec4 value;
value.v[0] = v[0];
value.v[1] = v[1];
value.v[2] = 0.0f;
value.v[3] = 1.0f;
info->set_value(value);
glVertexAttrib2fv(index, v);
}
void GLES2DecoderImpl::DoVertexAttrib3fv(GLuint index, const GLfloat* v) {
VertexAttribManager::VertexAttribInfo* info =
vertex_attrib_manager_.GetVertexAttribInfo(index);
if (!info) {
SetGLError(GL_INVALID_VALUE, "glVertexAttrib3fv: index out of range");
return;
}
VertexAttribManager::VertexAttribInfo::Vec4 value;
value.v[0] = v[0];
value.v[1] = v[1];
value.v[2] = v[2];
value.v[3] = 1.0f;
info->set_value(value);
glVertexAttrib3fv(index, v);
}
void GLES2DecoderImpl::DoVertexAttrib4fv(GLuint index, const GLfloat* v) {
VertexAttribManager::VertexAttribInfo* info =
vertex_attrib_manager_.GetVertexAttribInfo(index);
if (!info) {
SetGLError(GL_INVALID_VALUE, "glVertexAttrib4fv: index out of range");
return;
}
VertexAttribManager::VertexAttribInfo::Vec4 value;
value.v[0] = v[0];
value.v[1] = v[1];
value.v[2] = v[2];
value.v[3] = v[3];
info->set_value(value);
glVertexAttrib4fv(index, v);
}
error::Error GLES2DecoderImpl::HandleVertexAttribPointer(
uint32 immediate_data_size, const gles2::VertexAttribPointer& c) {
if (!bound_array_buffer_ || bound_array_buffer_->IsDeleted()) {
SetGLError(GL_INVALID_VALUE,
"glVertexAttribPointer: no array buffer bound");
return error::kNoError;
}
GLuint indx = c.indx;
GLint size = c.size;
GLenum type = c.type;
GLboolean normalized = c.normalized;
GLsizei stride = c.stride;
GLsizei offset = c.offset;
const void* ptr = reinterpret_cast<const void*>(offset);
if (!validators_->vertex_attrib_type.IsValid(type)) {
SetGLError(GL_INVALID_ENUM,
"glVertexAttribPointer: type GL_INVALID_ENUM");
return error::kNoError;
}
if (!validators_->vertex_attrib_size.IsValid(size)) {
SetGLError(GL_INVALID_ENUM,
"glVertexAttribPointer: size GL_INVALID_VALUE");
return error::kNoError;
}
if (indx >= group_->max_vertex_attribs()) {
SetGLError(GL_INVALID_VALUE, "glVertexAttribPointer: index out of range");
return error::kNoError;
}
if (stride < 0) {
SetGLError(GL_INVALID_VALUE,
"glVertexAttribPointer: stride < 0");
return error::kNoError;
}
if (stride > 255) {
SetGLError(GL_INVALID_VALUE,
"glVertexAttribPointer: stride > 255");
return error::kNoError;
}
if (offset < 0) {
SetGLError(GL_INVALID_VALUE,
"glVertexAttribPointer: offset < 0");
return error::kNoError;
}
GLsizei component_size =
GLES2Util::GetGLTypeSizeForTexturesAndBuffers(type);
if (offset % component_size > 0) {
SetGLError(GL_INVALID_VALUE,
"glVertexAttribPointer: stride not valid for type");
return error::kNoError;
}
vertex_attrib_manager_.GetVertexAttribInfo(indx)->SetInfo(
bound_array_buffer_,
size,
type,
normalized,
stride,
stride != 0 ? stride : component_size * size,
offset);
glVertexAttribPointer(indx, size, type, normalized, stride, ptr);
return error::kNoError;
}
error::Error GLES2DecoderImpl::HandleReadPixels(
uint32 immediate_data_size, const gles2::ReadPixels& c) {
GLint x = c.x;
GLint y = c.y;
GLsizei width = c.width;
GLsizei height = c.height;
GLenum format = c.format;
GLenum type = c.type;
if (width < 0 || height < 0) {
SetGLError(GL_INVALID_VALUE, "glReadPixels: dimensions < 0");
return error::kNoError;
}
typedef gles2::ReadPixels::Result Result;
uint32 pixels_size;
if (!GLES2Util::ComputeImageDataSize(
width, height, format, type, pack_alignment_, &pixels_size)) {
return error::kOutOfBounds;
}
void* pixels = GetSharedMemoryAs<void*>(
c.pixels_shm_id, c.pixels_shm_offset, pixels_size);
Result* result = GetSharedMemoryAs<Result*>(
c.result_shm_id, c.result_shm_offset, sizeof(*result));
if (!pixels || !result) {
return error::kOutOfBounds;
}
if (!validators_->read_pixel_format.IsValid(format)) {
SetGLError(GL_INVALID_ENUM, "glReadPixels: format GL_INVALID_ENUM");
return error::kNoError;
}
if (!validators_->pixel_type.IsValid(type)) {
SetGLError(GL_INVALID_ENUM, "glReadPixels: type GL_INVALID_ENUM");
return error::kNoError;
}
if (width == 0 || height == 0) {
return error::kNoError;
}
CopyRealGLErrorsToWrapper();
// Get the size of the current fbo or backbuffer.
gfx::Size max_size = GetBoundFrameBufferSize();
GLint max_x;
GLint max_y;
if (!SafeAdd(x, width, &max_x) || !SafeAdd(y, height, &max_y)) {
SetGLError(GL_INVALID_VALUE, "glReadPixels: dimensions out of range");
return error::kNoError;
}
if (x < 0 || y < 0 || max_x > max_size.width() || max_y > max_size.height()) {
// The user requested an out of range area. Get the results 1 line
// at a time.
uint32 temp_size;
if (!GLES2Util::ComputeImageDataSize(
width, 1, format, type, pack_alignment_, &temp_size)) {
SetGLError(GL_INVALID_VALUE, "glReadPixels: dimensions out of range");
return error::kNoError;
}
GLsizei unpadded_row_size = temp_size;
if (!GLES2Util::ComputeImageDataSize(
width, 2, format, type, pack_alignment_, &temp_size)) {
SetGLError(GL_INVALID_VALUE, "glReadPixels: dimensions out of range");
return error::kNoError;
}
GLsizei padded_row_size = temp_size - unpadded_row_size;
if (padded_row_size < 0 || unpadded_row_size < 0) {
SetGLError(GL_INVALID_VALUE, "glReadPixels: dimensions out of range");
return error::kNoError;
}
GLint dest_x_offset = std::max(-x, 0);
uint32 dest_row_offset;
if (!GLES2Util::ComputeImageDataSize(
dest_x_offset, 1, format, type, pack_alignment_, &dest_row_offset)) {
SetGLError(GL_INVALID_VALUE, "glReadPixels: dimensions out of range");
return error::kNoError;
}
// Copy each row into the larger dest rect.
int8* dst = static_cast<int8*>(pixels);
GLint read_x = std::max(0, x);
GLint read_end_x = std::max(0, std::min(max_size.width(), max_x));
GLint read_width = read_end_x - read_x;
for (GLint yy = 0; yy < height; ++yy) {
GLint ry = y + yy;
// Clear the row.
memset(dst, 0, unpadded_row_size);
// If the row is in range, copy it.
if (ry >= 0 && ry < max_size.height() && read_width > 0) {
glReadPixels(
read_x, ry, read_width, 1, format, type, dst + dest_row_offset);
}
dst += padded_row_size;
}
} else {
glReadPixels(x, y, width, height, format, type, pixels);
}
GLenum error = glGetError();
if (error == GL_NO_ERROR) {
*result = true;
} else {
SetGLError(error, NULL);
}
return error::kNoError;
}
error::Error GLES2DecoderImpl::HandlePixelStorei(
uint32 immediate_data_size, const gles2::PixelStorei& c) {
GLenum pname = c.pname;
GLenum param = c.param;
if (!validators_->pixel_store.IsValid(pname)) {
SetGLError(GL_INVALID_ENUM, "glPixelStorei: pname GL_INVALID_ENUM");
return error::kNoError;
}
if (!validators_->pixel_store_alignment.IsValid(param)) {
SetGLError(GL_INVALID_VALUE, "glPixelSTore: param GL_INVALID_VALUE");
return error::kNoError;
}
glPixelStorei(pname, param);
switch (pname) {
case GL_PACK_ALIGNMENT:
pack_alignment_ = param;
break;
case GL_UNPACK_ALIGNMENT:
unpack_alignment_ = param;
break;
default:
// Validation should have prevented us from getting here.
NOTREACHED();
break;
}
return error::kNoError;
}
error::Error GLES2DecoderImpl::GetAttribLocationHelper(
GLuint client_id, uint32 location_shm_id, uint32 location_shm_offset,
const std::string& name_str) {
ProgramManager::ProgramInfo* info = GetProgramInfoNotShader(
client_id, "glGetAttribLocation");
if (!info) {
return error::kNoError;
}
if (!info->IsValid()) {
SetGLError(GL_INVALID_OPERATION, "glGetAttribLocation: program not linked");
return error::kNoError;
}
GLint* location = GetSharedMemoryAs<GLint*>(
location_shm_id, location_shm_offset, sizeof(GLint));
if (!location) {
return error::kOutOfBounds;
}
// Require the client to init this incase the context is lost and we are no
// longer executing commands.
if (*location != -1) {
return error::kGenericError;
}
*location = info->GetAttribLocation(name_str);
return error::kNoError;
}
error::Error GLES2DecoderImpl::HandleGetAttribLocation(
uint32 immediate_data_size, const gles2::GetAttribLocation& c) {
uint32 name_size = c.data_size;
const char* name = GetSharedMemoryAs<const char*>(
c.name_shm_id, c.name_shm_offset, name_size);
if (!name) {
return error::kOutOfBounds;
}
String name_str(name, name_size);
return GetAttribLocationHelper(
c.program, c.location_shm_id, c.location_shm_offset, name_str);
}
error::Error GLES2DecoderImpl::HandleGetAttribLocationImmediate(
uint32 immediate_data_size, const gles2::GetAttribLocationImmediate& c) {
uint32 name_size = c.data_size;
const char* name = GetImmediateDataAs<const char*>(
c, name_size, immediate_data_size);
if (!name) {
return error::kOutOfBounds;
}
String name_str(name, name_size);
return GetAttribLocationHelper(
c.program, c.location_shm_id, c.location_shm_offset, name_str);
}
error::Error GLES2DecoderImpl::HandleGetAttribLocationBucket(
uint32 immediate_data_size, const gles2::GetAttribLocationBucket& c) {
Bucket* bucket = GetBucket(c.name_bucket_id);
if (!bucket) {
return error::kInvalidArguments;
}
std::string name_str;
if (!bucket->GetAsString(&name_str)) {
return error::kInvalidArguments;
}
return GetAttribLocationHelper(
c.program, c.location_shm_id, c.location_shm_offset, name_str);
}
error::Error GLES2DecoderImpl::GetUniformLocationHelper(
GLuint client_id, uint32 location_shm_id, uint32 location_shm_offset,
const std::string& name_str) {
ProgramManager::ProgramInfo* info = GetProgramInfoNotShader(
client_id, "glUniformLocation");
if (!info) {
return error::kNoError;
}
if (!info->IsValid()) {
SetGLError(GL_INVALID_OPERATION,
"glGetUniformLocation: program not linked");
return error::kNoError;
}
GLint* location = GetSharedMemoryAs<GLint*>(
location_shm_id, location_shm_offset, sizeof(GLint));
if (!location) {
return error::kOutOfBounds;
}
// Require the client to init this incase the context is lost an we are no
// longer executing commands.
if (*location != -1) {
return error::kGenericError;
}
*location = info->GetUniformLocation(name_str);
return error::kNoError;
}
error::Error GLES2DecoderImpl::HandleGetUniformLocation(
uint32 immediate_data_size, const gles2::GetUniformLocation& c) {
uint32 name_size = c.data_size;
const char* name = GetSharedMemoryAs<const char*>(
c.name_shm_id, c.name_shm_offset, name_size);
if (!name) {
return error::kOutOfBounds;
}
String name_str(name, name_size);
return GetUniformLocationHelper(
c.program, c.location_shm_id, c.location_shm_offset, name_str);
}
error::Error GLES2DecoderImpl::HandleGetUniformLocationImmediate(
uint32 immediate_data_size, const gles2::GetUniformLocationImmediate& c) {
uint32 name_size = c.data_size;
const char* name = GetImmediateDataAs<const char*>(
c, name_size, immediate_data_size);
if (!name) {
return error::kOutOfBounds;
}
String name_str(name, name_size);
return GetUniformLocationHelper(
c.program, c.location_shm_id, c.location_shm_offset, name_str);
}
error::Error GLES2DecoderImpl::HandleGetUniformLocationBucket(
uint32 immediate_data_size, const gles2::GetUniformLocationBucket& c) {
Bucket* bucket = GetBucket(c.name_bucket_id);
if (!bucket) {
return error::kInvalidArguments;
}
std::string name_str;
if (!bucket->GetAsString(&name_str)) {
return error::kInvalidArguments;
}
return GetUniformLocationHelper(
c.program, c.location_shm_id, c.location_shm_offset, name_str);
}
error::Error GLES2DecoderImpl::HandleGetString(
uint32 immediate_data_size, const gles2::GetString& c) {
GLenum name = static_cast<GLenum>(c.name);
if (!validators_->string_type.IsValid(name)) {
SetGLError(GL_INVALID_ENUM, "glGetString: name GL_INVALID_ENUM");
return error::kNoError;
}
const char* gl_str = reinterpret_cast<const char*>(glGetString(name));
const char* str = NULL;
switch (name) {
case GL_VERSION:
str = "OpenGL ES 2.0 Chromium";
break;
case GL_SHADING_LANGUAGE_VERSION:
str = "OpenGL ES GLSL ES 1.0 Chromium";
break;
case GL_EXTENSIONS:
str = "";
break;
default:
str = gl_str;
break;
}
Bucket* bucket = CreateBucket(c.bucket_id);
bucket->SetFromString(str);
return error::kNoError;
}
void GLES2DecoderImpl::DoBufferData(
GLenum target, GLsizeiptr size, const GLvoid * data, GLenum usage) {
if (!validators_->buffer_target.IsValid(target)) {
SetGLError(GL_INVALID_ENUM, "glBufferData: target GL_INVALID_ENUM");
return;
}
if (!validators_->buffer_usage.IsValid(usage)) {
SetGLError(GL_INVALID_ENUM, "glBufferData: usage GL_INVALID_ENUM");
return;
}
if (size < 0) {
SetGLError(GL_INVALID_VALUE, "glBufferData: size < 0");
return;
}
BufferManager::BufferInfo* info = GetBufferInfoForTarget(target);
if (!info) {
SetGLError(GL_INVALID_VALUE, "glBufferData: unknown buffer");
return;
}
// Clear the buffer to 0 if no initial data was passed in.
scoped_array<int8> zero;
if (!data) {
zero.reset(new int8[size]);
memset(zero.get(), 0, size);
data = zero.get();
}
CopyRealGLErrorsToWrapper();
glBufferData(target, size, data, usage);
GLenum error = glGetError();
if (error != GL_NO_ERROR) {
SetGLError(error, NULL);
} else {
buffer_manager()->SetSize(info, size);
info->SetRange(0, size, data);
}
}
error::Error GLES2DecoderImpl::HandleBufferData(
uint32 immediate_data_size, const gles2::BufferData& c) {
GLenum target = static_cast<GLenum>(c.target);
GLsizeiptr size = static_cast<GLsizeiptr>(c.size);
uint32 data_shm_id = static_cast<uint32>(c.data_shm_id);
uint32 data_shm_offset = static_cast<uint32>(c.data_shm_offset);
GLenum usage = static_cast<GLenum>(c.usage);
const void* data = NULL;
if (data_shm_id != 0 || data_shm_offset != 0) {
data = GetSharedMemoryAs<const void*>(data_shm_id, data_shm_offset, size);
if (!data) {
return error::kOutOfBounds;
}
}
DoBufferData(target, size, data, usage);
return error::kNoError;
}
error::Error GLES2DecoderImpl::HandleBufferDataImmediate(
uint32 immediate_data_size, const gles2::BufferDataImmediate& c) {
GLenum target = static_cast<GLenum>(c.target);
GLsizeiptr size = static_cast<GLsizeiptr>(c.size);
const void* data = GetImmediateDataAs<const void*>(
c, size, immediate_data_size);
if (!data) {
return error::kOutOfBounds;
}
GLenum usage = static_cast<GLenum>(c.usage);
DoBufferData(target, size, data, usage);
return error::kNoError;
}
void GLES2DecoderImpl::DoBufferSubData(
GLenum target, GLintptr offset, GLsizeiptr size, const GLvoid * data) {
BufferManager::BufferInfo* info = GetBufferInfoForTarget(target);
if (!info) {
SetGLError(GL_INVALID_VALUE, "glBufferSubData: unknown buffer");
return;
}
if (!info->SetRange(offset, size, data)) {
SetGLError(GL_INVALID_VALUE, "glBufferSubData: out of range");
} else {
glBufferSubData(target, offset, size, data);
}
}
error::Error GLES2DecoderImpl::DoCompressedTexImage2D(
GLenum target,
GLint level,
GLenum internal_format,
GLsizei width,
GLsizei height,
GLint border,
GLsizei image_size,
const void* data) {
// TODO(gman): Validate image_size is correct for width, height and format.
if (!validators_->texture_target.IsValid(target)) {
SetGLError(GL_INVALID_ENUM,
"glCompressedTexImage2D: target GL_INVALID_ENUM");
return error::kNoError;
}
if (!validators_->compressed_texture_format.IsValid(
internal_format)) {
SetGLError(GL_INVALID_ENUM,
"glCompressedTexImage2D: internal_format GL_INVALID_ENUM");
return error::kNoError;
}
if (!texture_manager()->ValidForTarget(target, level, width, height, 1) ||
border != 0) {
SetGLError(GL_INVALID_VALUE,
"glCompressedTexImage2D: dimensions out of range");
return error::kNoError;
}
TextureManager::TextureInfo* info = GetTextureInfoForTarget(target);
if (!info) {
SetGLError(GL_INVALID_VALUE,
"glCompressedTexImage2D: unknown texture target");
return error::kNoError;
}
scoped_array<int8> zero;
if (!data) {
zero.reset(new int8[image_size]);
memset(zero.get(), 0, image_size);
data = zero.get();
}
texture_manager()->SetLevelInfo(
info, target, level, internal_format, width, height, 1, border, 0, 0);
glCompressedTexImage2D(
target, level, internal_format, width, height, border, image_size, data);
return error::kNoError;
}
error::Error GLES2DecoderImpl::HandleCompressedTexImage2D(
uint32 immediate_data_size, const gles2::CompressedTexImage2D& c) {
GLenum target = static_cast<GLenum>(c.target);
GLint level = static_cast<GLint>(c.level);
GLenum internal_format = static_cast<GLenum>(c.internalformat);
GLsizei width = static_cast<GLsizei>(c.width);
GLsizei height = static_cast<GLsizei>(c.height);
GLint border = static_cast<GLint>(c.border);
GLsizei image_size = static_cast<GLsizei>(c.imageSize);
uint32 data_shm_id = static_cast<uint32>(c.data_shm_id);
uint32 data_shm_offset = static_cast<uint32>(c.data_shm_offset);
const void* data = NULL;
if (data_shm_id != 0 || data_shm_offset != 0) {
data = GetSharedMemoryAs<const void*>(
data_shm_id, data_shm_offset, image_size);
if (!data) {
return error::kOutOfBounds;
}
}
return DoCompressedTexImage2D(
target, level, internal_format, width, height, border, image_size, data);
}
error::Error GLES2DecoderImpl::HandleCompressedTexImage2DImmediate(
uint32 immediate_data_size, const gles2::CompressedTexImage2DImmediate& c) {
GLenum target = static_cast<GLenum>(c.target);
GLint level = static_cast<GLint>(c.level);
GLenum internal_format = static_cast<GLenum>(c.internalformat);
GLsizei width = static_cast<GLsizei>(c.width);
GLsizei height = static_cast<GLsizei>(c.height);
GLint border = static_cast<GLint>(c.border);
GLsizei image_size = static_cast<GLsizei>(c.imageSize);
const void* data = GetImmediateDataAs<const void*>(
c, image_size, immediate_data_size);
if (!data) {
return error::kOutOfBounds;
}
return DoCompressedTexImage2D(
target, level, internal_format, width, height, border, image_size, data);
}
error::Error GLES2DecoderImpl::HandleCompressedTexImage2DBucket(
uint32 immediate_data_size, const gles2::CompressedTexImage2DBucket& c) {
GLenum target = static_cast<GLenum>(c.target);
GLint level = static_cast<GLint>(c.level);
GLenum internal_format = static_cast<GLenum>(c.internalformat);
GLsizei width = static_cast<GLsizei>(c.width);
GLsizei height = static_cast<GLsizei>(c.height);
GLint border = static_cast<GLint>(c.border);
Bucket* bucket = GetBucket(c.bucket_id);
return DoCompressedTexImage2D(
target, level, internal_format, width, height, border,
bucket->size(), bucket->GetData(0, bucket->size()));
}
error::Error GLES2DecoderImpl::HandleCompressedTexSubImage2DBucket(
uint32 immediate_data_size,
const gles2::CompressedTexSubImage2DBucket& c) {
GLenum target = static_cast<GLenum>(c.target);
GLint level = static_cast<GLint>(c.level);
GLint xoffset = static_cast<GLint>(c.xoffset);
GLint yoffset = static_cast<GLint>(c.yoffset);
GLsizei width = static_cast<GLsizei>(c.width);
GLsizei height = static_cast<GLsizei>(c.height);
GLenum format = static_cast<GLenum>(c.format);
Bucket* bucket = GetBucket(c.bucket_id);
uint32 data_size = bucket->size();
GLsizei imageSize = data_size;
const void* data = bucket->GetData(0, data_size);
if (!validators_->texture_target.IsValid(target)) {
SetGLError(
GL_INVALID_ENUM, "glCompressedTexSubImage2D: target GL_INVALID_ENUM");
return error::kNoError;
}
if (!validators_->compressed_texture_format.IsValid(format)) {
SetGLError(GL_INVALID_ENUM,
"glCompressedTexSubImage2D: format GL_INVALID_ENUM");
return error::kNoError;
}
if (width < 0) {
SetGLError(GL_INVALID_VALUE, "glCompressedTexSubImage2D: width < 0");
return error::kNoError;
}
if (height < 0) {
SetGLError(GL_INVALID_VALUE, "glCompressedTexSubImage2D: height < 0");
return error::kNoError;
}
if (imageSize < 0) {
SetGLError(GL_INVALID_VALUE, "glCompressedTexSubImage2D: imageSize < 0");
return error::kNoError;
}
glCompressedTexSubImage2D(
target, level, xoffset, yoffset, width, height, format, imageSize, data);
return error::kNoError;
}
error::Error GLES2DecoderImpl::DoTexImage2D(
GLenum target,
GLint level,
GLenum internal_format,
GLsizei width,
GLsizei height,
GLint border,
GLenum format,
GLenum type,
const void* pixels,
uint32 pixels_size) {
if (!validators_->texture_target.IsValid(target)) {
SetGLError(GL_INVALID_ENUM, "glTexImage2D: target GL_INVALID_ENUM");
return error::kNoError;
}
if (!validators_->texture_format.IsValid(internal_format)) {
SetGLError(GL_INVALID_ENUM,
"glTexImage2D: internal_format GL_INVALID_ENUM");
return error::kNoError;
}
if (!validators_->texture_format.IsValid(format)) {
SetGLError(GL_INVALID_ENUM, "glTexImage2D: format GL_INVALID_ENUM");
return error::kNoError;
}
if (!validators_->pixel_type.IsValid(type)) {
SetGLError(GL_INVALID_ENUM, "glTexImage2D: type GL_INVALID_ENUM");
return error::kNoError;
}
if (!texture_manager()->ValidForTarget(target, level, width, height, 1) ||
border != 0) {
SetGLError(GL_INVALID_VALUE, "glTexImage2D: dimensions out of range");
return error::kNoError;
}
TextureManager::TextureInfo* info = GetTextureInfoForTarget(target);
if (!info) {
SetGLError(GL_INVALID_OPERATION,
"glTexImage2D: unknown texture for target");
return error::kNoError;
}
scoped_array<int8> zero;
if (!pixels) {
zero.reset(new int8[pixels_size]);
memset(zero.get(), 0, pixels_size);
pixels = zero.get();
}
texture_manager()->SetLevelInfo(info,
target, level, internal_format, width, height, 1, border, format, type);
glTexImage2D(
target, level, internal_format, width, height, border, format, type,
pixels);
return error::kNoError;
}
error::Error GLES2DecoderImpl::HandleTexImage2D(
uint32 immediate_data_size, const gles2::TexImage2D& c) {
GLenum target = static_cast<GLenum>(c.target);
GLint level = static_cast<GLint>(c.level);
GLint internal_format = static_cast<GLint>(c.internalformat);
GLsizei width = static_cast<GLsizei>(c.width);
GLsizei height = static_cast<GLsizei>(c.height);
GLint border = static_cast<GLint>(c.border);
GLenum format = static_cast<GLenum>(c.format);
GLenum type = static_cast<GLenum>(c.type);
uint32 pixels_shm_id = static_cast<uint32>(c.pixels_shm_id);
uint32 pixels_shm_offset = static_cast<uint32>(c.pixels_shm_offset);
uint32 pixels_size;
if (!GLES2Util::ComputeImageDataSize(
width, height, format, type, unpack_alignment_, &pixels_size)) {
return error::kOutOfBounds;
}
const void* pixels = NULL;
if (pixels_shm_id != 0 || pixels_shm_offset != 0) {
pixels = GetSharedMemoryAs<const void*>(
pixels_shm_id, pixels_shm_offset, pixels_size);
if (!pixels) {
return error::kOutOfBounds;
}
}
return DoTexImage2D(
target, level, internal_format, width, height, border, format, type,
pixels, pixels_size);
}
error::Error GLES2DecoderImpl::HandleTexImage2DImmediate(
uint32 immediate_data_size, const gles2::TexImage2DImmediate& c) {
GLenum target = static_cast<GLenum>(c.target);
GLint level = static_cast<GLint>(c.level);
GLint internal_format = static_cast<GLint>(c.internalformat);
GLsizei width = static_cast<GLsizei>(c.width);
GLsizei height = static_cast<GLsizei>(c.height);
GLint border = static_cast<GLint>(c.border);
GLenum format = static_cast<GLenum>(c.format);
GLenum type = static_cast<GLenum>(c.type);
uint32 size;
if (!GLES2Util::ComputeImageDataSize(
width, height, format, type, unpack_alignment_, &size)) {
return error::kOutOfBounds;
}
const void* pixels = GetImmediateDataAs<const void*>(
c, size, immediate_data_size);
if (!pixels) {
return error::kOutOfBounds;
}
DoTexImage2D(
target, level, internal_format, width, height, border, format, type,
pixels, size);
return error::kNoError;
}
error::Error GLES2DecoderImpl::HandleGetVertexAttribPointerv(
uint32 immediate_data_size, const gles2::GetVertexAttribPointerv& c) {
GLuint index = static_cast<GLuint>(c.index);
GLenum pname = static_cast<GLenum>(c.pname);
typedef gles2::GetVertexAttribPointerv::Result Result;
Result* result = GetSharedMemoryAs<Result*>(
c.pointer_shm_id, c.pointer_shm_offset, Result::ComputeSize(1));
if (!result) {
return error::kOutOfBounds;
}
// Check that the client initialized the result.
if (result->size != 0) {
return error::kInvalidArguments;
}
if (!validators_->vertex_pointer.IsValid(pname)) {
SetGLError(GL_INVALID_ENUM,
"glGetVertexAttribPointerv: pname GL_INVALID_ENUM");
return error::kNoError;
}
if (index >= group_->max_vertex_attribs()) {
SetGLError(GL_INVALID_VALUE,
"glGetVertexAttribPointerv: index out of range.");
return error::kNoError;
}
result->SetNumResults(1);
*result->GetData() =
vertex_attrib_manager_.GetVertexAttribInfo(index)->offset();
return error::kNoError;
}
bool GLES2DecoderImpl::GetUniformSetup(
GLuint program, GLint location,
uint32 shm_id, uint32 shm_offset,
error::Error* error, GLuint* service_id, void** result_pointer,
GLenum* result_type) {
DCHECK(error);
DCHECK(service_id);
DCHECK(result_pointer);
DCHECK(result_type);
*error = error::kNoError;
// Make sure we have enough room for the result on failure.
SizedResult<GLint>* result;
result = GetSharedMemoryAs<SizedResult<GLint>*>(
shm_id, shm_offset, SizedResult<GLint>::ComputeSize(0));
if (!result) {
*error = error::kOutOfBounds;
return false;
}
*result_pointer = result;
// Set the result size to 0 so the client does not have to check for success.
result->SetNumResults(0);
ProgramManager::ProgramInfo* info = GetProgramInfoNotShader(
program, "glGetUniform");
if (!info) {
return false;
}
if (!info->IsValid()) {
// Program was not linked successfully. (ie, glLinkProgram)
SetGLError(GL_INVALID_OPERATION, "glGetUniform: program not linked");
return false;
}
*service_id = info->service_id();
GLenum type;
if (!info->GetUniformTypeByLocation(location, &type)) {
// No such location.
SetGLError(GL_INVALID_OPERATION, "glGetUniform: unknown location");
return false;
}
GLsizei size = GLES2Util::GetGLDataTypeSizeForUniforms(type);
if (size == 0) {
SetGLError(GL_INVALID_OPERATION, "glGetUniform: unknown type");
return false;
}
result = GetSharedMemoryAs<SizedResult<GLint>*>(
shm_id, shm_offset, SizedResult<GLint>::ComputeSizeFromBytes(size));
if (!result) {
*error = error::kOutOfBounds;
return false;
}
result->size = size;
*result_type = type;
return true;
}
error::Error GLES2DecoderImpl::HandleGetUniformiv(
uint32 immediate_data_size, const gles2::GetUniformiv& c) {
GLuint program = c.program;
GLint location = c.location;
GLuint service_id;
GLenum result_type;
Error error;
void* result;
if (GetUniformSetup(
program, location, c.params_shm_id, c.params_shm_offset,
&error, &service_id, &result, &result_type)) {
glGetUniformiv(
service_id, location,
static_cast<gles2::GetUniformiv::Result*>(result)->GetData());
}
return error;
}
error::Error GLES2DecoderImpl::HandleGetUniformfv(
uint32 immediate_data_size, const gles2::GetUniformfv& c) {
GLuint program = c.program;
GLint location = c.location;
GLuint service_id;
Error error;
typedef gles2::GetUniformfv::Result Result;
Result* result;
GLenum result_type;
if (GetUniformSetup(
program, location, c.params_shm_id, c.params_shm_offset,
&error, &service_id, reinterpret_cast<void**>(&result), &result_type)) {
if (result_type == GL_BOOL || result_type == GL_BOOL_VEC2 ||
result_type == GL_BOOL_VEC3 || result_type == GL_BOOL_VEC4) {
GLsizei num_values = result->GetNumResults();
scoped_array<GLint> temp(new GLint[num_values]);
glGetUniformiv(service_id, location, temp.get());
GLfloat* dst = result->GetData();
for (GLsizei ii = 0; ii < num_values; ++ii) {
dst[ii] = (temp[ii] != 0);
}
} else {
glGetUniformfv(service_id, location, result->GetData());
}
}
return error;
}
error::Error GLES2DecoderImpl::HandleGetShaderPrecisionFormat(
uint32 immediate_data_size, const gles2::GetShaderPrecisionFormat& c) {
GLenum shader_type = static_cast<GLenum>(c.shadertype);
GLenum precision_type = static_cast<GLenum>(c.precisiontype);
typedef gles2::GetShaderPrecisionFormat::Result Result;
Result* result = GetSharedMemoryAs<Result*>(
c.result_shm_id, c.result_shm_offset, sizeof(*result));
if (!result) {
return error::kOutOfBounds;
}
// Check that the client initialized the result.
if (result->success != 0) {
return error::kInvalidArguments;
}
if (!validators_->shader_type.IsValid(shader_type)) {
SetGLError(GL_INVALID_ENUM,
"glGetShaderPrecisionFormat: shader_type GL_INVALID_ENUM");
return error::kNoError;
}
if (!validators_->shader_precision.IsValid(precision_type)) {
SetGLError(GL_INVALID_ENUM,
"glGetShaderPrecisionFormat: precision_type GL_INVALID_ENUM");
return error::kNoError;
}
result->success = 1; // true
switch (precision_type) {
case GL_LOW_INT:
case GL_MEDIUM_INT:
case GL_HIGH_INT:
result->min_range = -31;
result->max_range = 31;
result->precision = 0;
break;
case GL_LOW_FLOAT:
case GL_MEDIUM_FLOAT:
case GL_HIGH_FLOAT:
result->min_range = -62;
result->max_range = 62;
result->precision = -16;
break;
default:
NOTREACHED();
break;
}
return error::kNoError;
}
error::Error GLES2DecoderImpl::HandleGetAttachedShaders(
uint32 immediate_data_size, const gles2::GetAttachedShaders& c) {
uint32 result_size = c.result_size;
GLuint program = static_cast<GLuint>(c.program);
ProgramManager::ProgramInfo* info = GetProgramInfoNotShader(
program, "glGetAttachedShaders");
if (!info) {
return error::kNoError;
}
typedef gles2::GetAttachedShaders::Result Result;
uint32 max_count = Result::ComputeMaxResults(result_size);
Result* result = GetSharedMemoryAs<Result*>(
c.result_shm_id, c.result_shm_offset, Result::ComputeSize(max_count));
if (!result) {
return error::kOutOfBounds;
}
// Check that the client initialized the result.
if (result->size != 0) {
return error::kInvalidArguments;
}
GLsizei count = 0;
glGetAttachedShaders(
info->service_id(), max_count, &count, result->GetData());
for (GLsizei ii = 0; ii < count; ++ii) {
if (!shader_manager()->GetClientId(result->GetData()[ii],
&result->GetData()[ii])) {
NOTREACHED();
return error::kGenericError;
}
}
result->SetNumResults(count);
return error::kNoError;
}
error::Error GLES2DecoderImpl::HandleGetActiveUniform(
uint32 immediate_data_size, const gles2::GetActiveUniform& c) {
GLuint program = c.program;
GLuint index = c.index;
uint32 name_bucket_id = c.name_bucket_id;
typedef gles2::GetActiveUniform::Result Result;
Result* result = GetSharedMemoryAs<Result*>(
c.result_shm_id, c.result_shm_offset, sizeof(*result));
if (!result) {
return error::kOutOfBounds;
}
// Check that the client initialized the result.
if (result->success != 0) {
return error::kInvalidArguments;
}
ProgramManager::ProgramInfo* info = GetProgramInfoNotShader(
program, "glGetActiveUniform");
if (!info) {
return error::kNoError;
}
const ProgramManager::ProgramInfo::UniformInfo* uniform_info =
info->GetUniformInfo(index);
if (!uniform_info) {
SetGLError(GL_INVALID_VALUE, "glGetActiveUniform: index out of range");
return error::kNoError;
}
result->success = 1; // true.
result->size = uniform_info->size;
result->type = uniform_info->type;
Bucket* bucket = CreateBucket(name_bucket_id);
bucket->SetFromString(uniform_info->name);
return error::kNoError;
}
error::Error GLES2DecoderImpl::HandleGetActiveAttrib(
uint32 immediate_data_size, const gles2::GetActiveAttrib& c) {
GLuint program = c.program;
GLuint index = c.index;
uint32 name_bucket_id = c.name_bucket_id;
typedef gles2::GetActiveAttrib::Result Result;
Result* result = GetSharedMemoryAs<Result*>(
c.result_shm_id, c.result_shm_offset, sizeof(*result));
if (!result) {
return error::kOutOfBounds;
}
// Check that the client initialized the result.
if (result->success != 0) {
return error::kInvalidArguments;
}
ProgramManager::ProgramInfo* info = GetProgramInfoNotShader(
program, "glGetActiveAttrib");
if (!info) {
return error::kNoError;
}
const ProgramManager::ProgramInfo::VertexAttribInfo* attrib_info =
info->GetAttribInfo(index);
if (!attrib_info) {
SetGLError(GL_INVALID_VALUE, "glGetActiveAttrib: index out of range");
return error::kNoError;
}
result->success = 1; // true.
result->size = attrib_info->size;
result->type = attrib_info->type;
Bucket* bucket = CreateBucket(name_bucket_id);
bucket->SetFromString(attrib_info->name);
return error::kNoError;
}
error::Error GLES2DecoderImpl::HandleShaderBinary(
uint32 immediate_data_size, const gles2::ShaderBinary& c) {
#if 1 // No binary shader support.
SetGLError(GL_INVALID_OPERATION, "glShaderBinary: not supported");
return error::kNoError;
#else
GLsizei n = static_cast<GLsizei>(c.n);
if (n < 0) {
SetGLError(GL_INVALID_VALUE, "glShaderBinary: n < 0");
return error::kNoError;
}
GLsizei length = static_cast<GLsizei>(c.length);
if (length < 0) {
SetGLError(GL_INVALID_VALUE, "glShaderBinary: length < 0");
return error::kNoError;
}
uint32 data_size;
if (!SafeMultiplyUint32(n, sizeof(GLuint), &data_size)) {
return error::kOutOfBounds;
}
const GLuint* shaders = GetSharedMemoryAs<const GLuint*>(
c.shaders_shm_id, c.shaders_shm_offset, data_size);
GLenum binaryformat = static_cast<GLenum>(c.binaryformat);
const void* binary = GetSharedMemoryAs<const void*>(
c.binary_shm_id, c.binary_shm_offset, length);
if (shaders == NULL || binary == NULL) {
return error::kOutOfBounds;
}
scoped_array<GLuint> service_ids(new GLuint[n]);
for (GLsizei ii = 0; ii < n; ++ii) {
ShaderManager::ShaderInfo* info = GetShaderInfo(shaders[ii]);
if (!info) {
SetGLError(GL_INVALID_VALUE, "glShaderBinary: unknown shader");
return error::kNoError;
}
service_ids[ii] = info->service_id();
}
// TODO(gman): call glShaderBinary
return error::kNoError;
#endif
}
error::Error GLES2DecoderImpl::HandleSwapBuffers(
uint32 immediate_data_size, const gles2::SwapBuffers& c) {
// If offscreen then don't actually SwapBuffers to the display. Just copy
// the rendered frame to another frame buffer.
if (offscreen_target_frame_buffer_.get()) {
ScopedGLErrorSuppressor suppressor(this);
// First check to see if a deferred offscreen render buffer resize is
// pending.
if (!UpdateOffscreenFrameBufferSize())
return error::kLostContext;
if (parent_) {
ScopedFrameBufferBinder binder(this,
offscreen_target_frame_buffer_->id());
offscreen_saved_color_texture_->Copy(
offscreen_saved_color_texture_->size());
}
} else {
context_->SwapBuffers();
}
// TODO(kbr): when the back buffer is multisampled, then at least on Mac
// OS X (and probably on all platforms, for best semantics), we will need
// to perform the resolve step and bind the offscreen_saved_color_texture_
// as the color attachment before calling the swap buffers callback, which
// expects a normal (non-multisampled) frame buffer for glCopyTexImage2D /
// glReadPixels. After the callback runs, the multisampled frame buffer
// needs to be bound again.
if (swap_buffers_callback_.get()) {
swap_buffers_callback_->Run();
}
return error::kNoError;
}
error::Error GLES2DecoderImpl::HandleCommandBufferEnable(
uint32 immediate_data_size, const gles2::CommandBufferEnable& c) {
Bucket* bucket = GetBucket(c.bucket_id);
typedef gles2::CommandBufferEnable::Result Result;
Result* result = GetSharedMemoryAs<Result*>(
c.result_shm_id, c.result_shm_offset, sizeof(*result));
if (!result) {
return error::kOutOfBounds;
}
// Check that the client initialized the result.
if (*result != 0) {
return error::kInvalidArguments;
}
std::string feature_str;
if (!bucket->GetAsString(&feature_str)) {
return error::kInvalidArguments;
}
// TODO(gman): make this some kind of table to function pointer thingy.
if (feature_str.compare(PEPPER3D_ALLOW_BUFFERS_ON_MULTIPLE_TARGETS) == 0) {
buffer_manager()->set_allow_buffers_on_multiple_targets(true);
} else if (feature_str.compare(PEPPER3D_SKIP_GLSL_TRANSLATION) == 0) {
use_shader_translator_ = false;
} else {
return error::kNoError;
}
*result = 1; // true.
return error::kNoError;
}
// Include the auto-generated part of this file. We split this because it means
// we can easily edit the non-auto generated parts right here in this file
// instead of having to edit some template or the code generator.
#include "gpu/command_buffer/service/gles2_cmd_decoder_autogen.h"
} // namespace gles2
} // namespace gpu
|