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
|
/*
Copyright 2010 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "GrGpuGL.h"
#include "GrMemory.h"
#include <stdio.h>
static const GLuint GR_MAX_GLUINT = ~0;
static const GLint GR_INVAL_GLINT = ~0;
#define SKIP_CACHE_CHECK true
static const GLenum gXfermodeCoeff2Blend[] = {
GL_ZERO,
GL_ONE,
GL_SRC_COLOR,
GL_ONE_MINUS_SRC_COLOR,
GL_DST_COLOR,
GL_ONE_MINUS_DST_COLOR,
GL_SRC_ALPHA,
GL_ONE_MINUS_SRC_ALPHA,
GL_DST_ALPHA,
GL_ONE_MINUS_DST_ALPHA,
};
bool has_gl_extension(const char* ext) {
const char* glstr = (const char*) glGetString(GL_EXTENSIONS);
int extLength = strlen(ext);
while (true) {
int n = strcspn(glstr, " ");
if (n == extLength && 0 == strncmp(ext, glstr, n)) {
return true;
}
if (0 == glstr[n]) {
return false;
}
glstr += n+1;
}
}
void gl_version(int* major, int* minor) {
const char* v = (const char*) glGetString(GL_VERSION);
if (NULL == v) {
GrAssert(0);
*major = 0;
*minor = 0;
return;
}
#if GR_GL_DESKTOP
int n = sscanf(v, "%d.%d", major, minor);
if (n != 2) {
GrAssert(0);
*major = 0;
*minor = 0;
return;
}
#else
char profile[2];
int n = sscanf(v, "OpenGL ES-%c%c %d.%d", profile, profile+1, major, minor);
bool ok = 4 == n;
if (!ok) {
int n = sscanf(v, "OpenGL ES %d.%d", major, minor);
ok = 2 == n;
}
if (!ok) {
GrAssert(0);
*major = 0;
*minor = 0;
return;
}
#endif
}
///////////////////////////////////////////////////////////////////////////////
bool fbo_test(GrGLExts exts, int w, int h) {
GLuint testFBO;
GR_GLEXT(exts, GenFramebuffers(1, &testFBO));
GR_GLEXT(exts, BindFramebuffer(GR_FRAMEBUFFER, testFBO));
GLuint testRTTex;
GR_GL(GenTextures(1, &testRTTex));
GR_GL(BindTexture(GL_TEXTURE_2D, testRTTex));
GR_GL(TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h,
0, GL_RGBA, GL_UNSIGNED_BYTE, NULL));
GR_GL(BindTexture(GL_TEXTURE_2D, 0));
GR_GLEXT(exts, FramebufferTexture2D(GR_FRAMEBUFFER, GR_COLOR_ATTACHMENT0,
GL_TEXTURE_2D, testRTTex, 0));
GLenum status = GR_GLEXT(exts, CheckFramebufferStatus(GR_FRAMEBUFFER));
GR_GLEXT(exts, DeleteFramebuffers(1, &testFBO));
GR_GL(DeleteTextures(1, &testRTTex));
return status == GR_FRAMEBUFFER_COMPLETE;
}
///////////////////////////////////////////////////////////////////////////////
GrGpuGL::GrGpuGL() {
GrPrintf("------------------------- create GrGpuGL %p --------------\n",
this);
GrPrintf("------ VENDOR %s\n", glGetString(GL_VENDOR));
GrPrintf("------ RENDERER %s\n", glGetString(GL_RENDERER));
GrPrintf("------ VERSION %s\n", glGetString(GL_VERSION));
GrPrintf("------ EXTENSIONS\n %s \n", glGetString(GL_EXTENSIONS));
GrGLClearErr();
GrGLInitExtensions(&fExts);
resetContextHelper();
GrGLRenderTarget::GLRenderTargetIDs defaultRTIDs;
GR_GL(GetIntegerv(GR_FRAMEBUFFER_BINDING, (GLint*)&defaultRTIDs.fRTFBOID));
defaultRTIDs.fTexFBOID = defaultRTIDs.fRTFBOID;
defaultRTIDs.fMSColorRenderbufferID = 0;
defaultRTIDs.fStencilRenderbufferID = 0;
GLint vp[4];
GR_GL(GetIntegerv(GL_VIEWPORT, vp));
fHWBounds.fViewportRect.setLTRB(vp[0],
vp[1] + vp[3],
vp[0] + vp[2],
vp[1]);
defaultRTIDs.fOwnIDs = false;
fDefaultRenderTarget = new GrGLRenderTarget(defaultRTIDs,
fHWBounds.fViewportRect,
NULL,
this);
fHWDrawState.fRenderTarget = fDefaultRenderTarget;
fRenderTargetChanged = true;
fCurrDrawState = fHWDrawState;
////////////////////////////////////////////////////////////////////////////
// Check for supported features.
int major, minor;
gl_version(&major, &minor);
GLint numFormats;
GR_GL(GetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats));
GrAutoSTMalloc<10, GLint> formats(numFormats);
GR_GL(GetIntegerv(GL_COMPRESSED_TEXTURE_FORMATS, formats));
for (int i = 0; i < numFormats; ++i) {
if (formats[i] == GR_PALETTE8_RGBA8) {
f8bitPaletteSupport = true;
break;
}
}
GrPrintf("Palette8 support: %s\n", (f8bitPaletteSupport ? "YES" : "NO"));
GR_STATIC_ASSERT(0 == kNone_AALevel);
GR_STATIC_ASSERT(1 == kLow_AALevel);
GR_STATIC_ASSERT(2 == kMed_AALevel);
GR_STATIC_ASSERT(3 == kHigh_AALevel);
memset(fAASamples, 0, sizeof(fAASamples));
fMSFBOType = kNone_MSFBO;
if (has_gl_extension("GL_IMG_multisampled_render_to_texture")) {
fMSFBOType = kIMG_MSFBO;
GrPrintf("MSAA Support: IMG ES EXT.\n");
}
else if (has_gl_extension("GL_APPLE_framebuffer_multisample")) {
fMSFBOType = kApple_MSFBO;
GrPrintf("MSAA Support: APPLE ES EXT.\n");
}
#if GR_GL_DESKTOP
else if ((major >= 3) ||
has_gl_extension("GL_ARB_framebuffer_object") ||
(has_gl_extension("GL_EXT_framebuffer_multisample") &&
has_gl_extension("GL_EXT_framebuffer_blit"))) {
fMSFBOType = kDesktop_MSFBO;
GrPrintf("MSAA Support: DESKTOP\n");
}
#endif
else {
GrPrintf("MSAA Support: NONE\n");
}
if (kNone_MSFBO != fMSFBOType) {
GLint maxSamples;
GLenum maxSampleGetter = (kIMG_MSFBO == fMSFBOType) ?
GR_MAX_SAMPLES_IMG :
GR_MAX_SAMPLES;
GR_GL(GetIntegerv(maxSampleGetter, &maxSamples));
if (maxSamples > 1 ) {
fAASamples[kNone_AALevel] = 0;
fAASamples[kLow_AALevel] = GrMax(2,
GrFixedFloorToInt((GR_FixedHalf) *
maxSamples));
fAASamples[kMed_AALevel] = GrMax(2,
GrFixedFloorToInt(((GR_Fixed1*3)/4) *
maxSamples));
fAASamples[kHigh_AALevel] = maxSamples;
}
GrPrintf("\tMax Samples: %d\n", maxSamples);
}
#if GR_GL_DESKTOP
fHasStencilWrap = (major >= 2 || (major == 1 && minor >= 4)) ||
has_gl_extension("GL_EXT_stencil_wrap");
#else
fHasStencilWrap = (major >= 2) || has_gl_extension("GL_OES_stencil_wrap");
#endif
GrPrintf("Stencil Wrap: %s\n", (fHasStencilWrap ? "YES" : "NO"));
#if GR_GL_DESKTOP
// we could also look for GL_ATI_separate_stencil extension or
// GL_EXT_stencil_two_side but they use different function signatures
// than GL2.0+ (and than each other).
fSingleStencilPassForWinding = (major >= 2);
#else
// ES 2 has two sided stencil but 1.1 doesn't. There doesn't seem to be
// an ES1 extension.
fSingleStencilPassForWinding = (major >= 2);
#endif
GrPrintf("Single Stencil Pass For Winding: %s\n", (fSingleStencilPassForWinding ? "YES" : "NO"));
#if GR_GL_DESKTOP
fRGBA8Renderbuffer = true;
#else
fRGBA8Renderbuffer = has_gl_extension("GL_OES_rgb8_rgba8");
#endif
GrPrintf("RGBA Renderbuffer: %s\n", (fRGBA8Renderbuffer ? "YES" : "NO"));
#if GR_GL_DESKTOP
fBufferLockSupport = true; // we require VBO support and the desktop VBO
// extension includes glMapBuffer.
#else
fBufferLockSupport = has_gl_extension("GL_OES_mapbuffer");
#endif
GrPrintf("Map Buffer: %s\n", (fBufferLockSupport ? "YES" : "NO"));
#if GR_GL_DESKTOP
fNPOTTextureSupport =
(major >= 2 || has_gl_extension("GL_ARB_texture_non_power_of_two")) ?
kFull_NPOTTextureType :
kNone_NPOTTextureType;
#else
if (has_gl_extension("GL_OES_texture_npot")) {
fNPOTTextureSupport = kFull_NPOTTextureType;
} else if (major >= 2 ||
has_gl_extension("GL_APPLE_texture_2D_limited_npot")) {
fNPOTTextureSupport = kNoRepeat_NPOTTextureType;
} else {
fNPOTTextureSupport = kNone_NPOTTextureType;
}
#endif
////////////////////////////////////////////////////////////////////////////
// Experiments to determine limitations that can't be queried. TODO: Make
// these a preprocess that generate some compile time constants.
/* Experimentation has found that some GLs that support NPOT textures
do not support FBOs with a NPOT texture. They report "unsupported" FBO
status. I don't know how to explicitly query for this. Do an
experiment. Note they may support NPOT with a renderbuffer but not a
texture. Presumably, the implementation bloats the renderbuffer
internally to the next POT.
*/
if (fNPOTTextureSupport == kFull_NPOTTextureType) {
bool npotFBOSuccess = fbo_test(fExts, 200, 200);
if (!npotFBOSuccess) {
fNPOTTextureSupport = kNonRendertarget_NPOTTextureType;
GrPrintf("NPOT Renderbuffer Test: FAILED\n");
} else {
GrPrintf("NPOT Renderbuffer Test: PASSED\n");
}
}
switch (fNPOTTextureSupport) {
case kNone_NPOTTextureType:
GrPrintf("NPOT Support: NONE\n");
break;
case kNoRepeat_NPOTTextureType:
GrPrintf("NPOT Support: NO REPEAT\n");
break;
case kNonRendertarget_NPOTTextureType:
GrPrintf("NPOT Support: NO FBOTEX\n");
break;
case kFull_NPOTTextureType:
GrPrintf("NPOT Support: FULL\n");
break;
}
// sanity check to make sure we can at least create an FBO from a POT texture
if (fNPOTTextureSupport < kFull_NPOTTextureType) {
bool npotFBOSuccess = fbo_test(fExts, 128, 128);
if (!npotFBOSuccess) {
GrPrintf("FBO Sanity Test: FAILED\n");
} else {
GrPrintf("FBO Sanity Test: PASSED\n");
}
}
/* The iPhone 4 has a restriction that for an FBO with texture color
attachment with height <= 8 then the width must be <= height. Here
we look for such a limitation.
*/
fMinRenderTargetHeight = GR_INVAL_GLINT;
GLint maxRenderSize;
glGetIntegerv(GR_MAX_RENDERBUFFER_SIZE, &maxRenderSize);
GrPrintf("Small height FBO texture experiments\n");
for (GLuint i = 1; i <= 256;
(kFull_NPOTTextureType != fNPOTTextureSupport) ? i *= 2 : ++i) {
GLuint w = maxRenderSize;
GLuint h = i;
if (fbo_test(fExts, w, h)) {
GrPrintf("\t[%d, %d]: PASSED\n", w, h);
fMinRenderTargetHeight = i;
break;
} else {
GrPrintf("\t[%d, %d]: FAILED\n", w, h);
}
}
GrAssert(GR_INVAL_GLINT != fMinRenderTargetHeight);
GrPrintf("Small width FBO texture experiments\n");
fMinRenderTargetWidth = GR_MAX_GLUINT;
for (GLuint i = 1; i <= 256;
(kFull_NPOTTextureType != fNPOTTextureSupport) ? i *= 2 : ++i) {
GLuint w = i;
GLuint h = maxRenderSize;
if (fbo_test(fExts, w, h)) {
GrPrintf("\t[%d, %d]: PASSED\n", w, h);
fMinRenderTargetWidth = i;
break;
} else {
GrPrintf("\t[%d, %d]: FAILED\n", w, h);
}
}
GrAssert(GR_INVAL_GLINT != fMinRenderTargetWidth);
#if GR_IOS_BUILD
/*
The iPad seems to fail, at least sometimes, if the height is < 16,
so we pin the values here for now. A better fix might be to
conditionalize this based on known that its an iPad (or some other
check).
*/
fMinRenderTargetWidth = GrMax<GLuint>(fMinRenderTargetWidth, 16);
fMinRenderTargetHeight = GrMax<GLuint>(fMinRenderTargetHeight, 16);
#endif
// bind back to original FBO
GR_GLEXT(fExts, BindFramebuffer(GR_FRAMEBUFFER, defaultRTIDs.fRTFBOID));
#if GR_COLLECT_STATS
++fStats.fRenderTargetChngCnt;
#endif
eraseStencil(0, ~0);
}
GrGpuGL::~GrGpuGL() {
fDefaultRenderTarget->abandon();
fDefaultRenderTarget->unref();
}
void GrGpuGL::resetContextHelper() {
// We detect cases when blending is effectively off
fHWBlendDisabled = false;
GR_GL(Enable(GL_BLEND));
// this is always disabled
GR_GL(Disable(GL_CULL_FACE));
GR_GL(Disable(GL_DITHER));
#if GR_GL_DESKTOP
GR_GL(Disable(GL_LINE_SMOOTH));
GR_GL(Disable(GL_POINT_SMOOTH));
GR_GL(Disable(GL_MULTISAMPLE));
#endif
// we only ever use lines in hairline mode
GR_GL(LineWidth(1));
GR_GL(ActiveTexture(GL_TEXTURE0));
fHWDrawState.fFlagBits = 0;
// illegal values
fHWDrawState.fSrcBlend = (BlendCoeff)-1;
fHWDrawState.fDstBlend = (BlendCoeff)-1;
fHWDrawState.fColor = GrColor_ILLEGAL;
fHWDrawState.fPointSize = -1;
fHWDrawState.fTexture = NULL;
GR_GL(Scissor(0,0,0,0));
fHWBounds.fScissorRect.setLTRB(0,0,0,0);
fHWBounds.fScissorEnabled = false;
GR_GL(Disable(GL_SCISSOR_TEST));
fHWDrawState.fSamplerState.setRadial2Params(-GR_ScalarMax,
-GR_ScalarMax,
true);
for (int i = 0; i < kMatrixModeCount; i++) {
fHWDrawState.fMatrixModeCache[i].setScale(GR_ScalarMax, GR_ScalarMax); // illegal
}
// disabling the stencil test also disables
// stencil buffer writes
GR_GL(Disable(GL_STENCIL_TEST));
GR_GL(StencilMask(0xffffffff));
GR_GL(ColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE));
fHWDrawState.fReverseFill = false;
fHWDrawState.fStencilPass = kNone_StencilPass;
fHWStencilClip = false;
fHWGeometryState.fIndexBuffer = NULL;
fHWGeometryState.fVertexBuffer = NULL;
GR_GL(BindBuffer(GL_ARRAY_BUFFER, 0));
GR_GL(BindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
fHWDrawState.fRenderTarget = NULL;
}
void GrGpuGL::resetContext() {
INHERITED::resetContext();
resetContextHelper();
}
// defines stencil formats from more to less preferred
#if GR_GL_ES
GLenum GR_GL_STENCIL_FORMAT_ARRAY[] = {
GR_STENCIL_INDEX8,
};
#else
GLenum GR_GL_STENCIL_FORMAT_ARRAY[] = {
GR_STENCIL_INDEX8,
GR_STENCIL_INDEX16,
GR_UNSIGNED_INT_24_8,
GR_DEPTH_STENCIL,
};
#endif
// good to set a break-point here to know when createTexture fails
static GrTexture* return_null_texture() {
// GrAssert(!"null texture");
return NULL;
}
#if GR_DEBUG
static size_t as_size_t(int x) {
return x;
}
#endif
GrRenderTarget* GrGpuGL::createPlatformRenderTarget(
intptr_t platformRenderTarget,
int width, int height) {
GrGLRenderTarget::GLRenderTargetIDs rtIDs;
rtIDs.fStencilRenderbufferID = 0;
rtIDs.fMSColorRenderbufferID = 0;
rtIDs.fTexFBOID = 0;
rtIDs.fOwnIDs = false;
GrIRect viewport;
// viewport is in GL coords (top >= bottom)
viewport.setLTRB(0, height, width, 0);
rtIDs.fRTFBOID = (GLuint)platformRenderTarget;
rtIDs.fTexFBOID = (GLuint)platformRenderTarget;
GrGLRenderTarget* rt = new GrGLRenderTarget(rtIDs, viewport, NULL, this);
return rt;
}
GrTexture* GrGpuGL::createTexture(const TextureDesc& desc,
const void* srcData, size_t rowBytes) {
#if GR_COLLECT_STATS
++fStats.fTextureCreateCnt;
#endif
GrGLTexture::GLTextureDesc glDesc;
GLenum internalFormat;
glDesc.fContentWidth = desc.fWidth;
glDesc.fContentHeight = desc.fHeight;
glDesc.fAllocWidth = desc.fWidth;
glDesc.fAllocHeight = desc.fHeight;
glDesc.fFormat = desc.fFormat;
bool renderTarget = 0 != (desc.fFlags & kRenderTarget_TextureFlag);
if (!canBeTexture(desc.fFormat,
&internalFormat,
&glDesc.fUploadFormat,
&glDesc.fUploadType)) {
return return_null_texture();
}
GrAssert(as_size_t(desc.fAALevel) < GR_ARRAY_COUNT(fAASamples));
GLint samples = fAASamples[desc.fAALevel];
if (kNone_MSFBO == fMSFBOType && desc.fAALevel != kNone_AALevel) {
GrPrintf("AA RT requested but not supported on this platform.");
}
GR_GL(GenTextures(1, &glDesc.fTextureID));
if (!glDesc.fTextureID) {
return return_null_texture();
}
glDesc.fUploadByteCount = GrTexture::BytesPerPixel(desc.fFormat);
/*
* check if our srcData has extra bytes past each row. If so, we need
* to trim those off here, since GL doesn't let us pass the rowBytes as
* a parameter to glTexImage2D
*/
#if GR_GL_DESKTOP
if (srcData) {
GR_GL(PixelStorei(GL_UNPACK_ROW_LENGTH,
rowBytes / glDesc.fUploadByteCount));
}
#else
GrAutoSMalloc<128 * 128> trimStorage;
size_t trimRowBytes = desc.fWidth * glDesc.fUploadByteCount;
if (srcData && (trimRowBytes < rowBytes)) {
size_t trimSize = desc.fHeight * trimRowBytes;
trimStorage.realloc(trimSize);
// now copy the data into our new storage, skipping the trailing bytes
const char* src = (const char*)srcData;
char* dst = (char*)trimStorage.get();
for (uint32_t y = 0; y < desc.fHeight; y++) {
memcpy(dst, src, trimRowBytes);
src += rowBytes;
dst += trimRowBytes;
}
// now point srcData to our trimmed version
srcData = trimStorage.get();
}
#endif
if (fNPOTTextureSupport < kNonRendertarget_NPOTTextureType ||
(fNPOTTextureSupport == kNonRendertarget_NPOTTextureType &&
renderTarget)) {
glDesc.fAllocWidth = GrNextPow2(desc.fWidth);
glDesc.fAllocHeight = GrNextPow2(desc.fHeight);
}
if (renderTarget) {
glDesc.fAllocWidth = GrMax<int>(fMinRenderTargetWidth,
glDesc.fAllocWidth);
glDesc.fAllocHeight = GrMax<int>(fMinRenderTargetHeight,
glDesc.fAllocHeight);
}
GR_GL(BindTexture(GL_TEXTURE_2D, glDesc.fTextureID));
#if GR_COLLECT_STATS
++fStats.fTextureChngCnt;
#endif
GR_GL(PixelStorei(GL_UNPACK_ALIGNMENT, glDesc.fUploadByteCount));
if (GrTexture::kIndex_8_PixelConfig == desc.fFormat &&
supports8BitPalette()) {
// ES only supports CompressedTexImage2D, not CompressedTexSubimage2D
GrAssert(desc.fWidth == glDesc.fAllocWidth);
GrAssert(desc.fHeight == glDesc.fAllocHeight);
GLsizei imageSize = glDesc.fAllocWidth * glDesc.fAllocHeight +
kColorTableSize;
GR_GL(CompressedTexImage2D(GL_TEXTURE_2D, 0, glDesc.fUploadFormat,
glDesc.fAllocWidth, glDesc.fAllocHeight,
0, imageSize, srcData));
GrGL_RestoreResetRowLength();
} else {
if (NULL != srcData && (glDesc.fAllocWidth != desc.fWidth ||
glDesc.fAllocHeight != desc.fHeight)) {
GR_GL(TexImage2D(GL_TEXTURE_2D, 0, internalFormat,
glDesc.fAllocWidth, glDesc.fAllocHeight,
0, glDesc.fUploadFormat, glDesc.fUploadType, NULL));
GR_GL(TexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, desc.fWidth,
desc.fHeight, glDesc.fUploadFormat,
glDesc.fUploadType, srcData));
GrGL_RestoreResetRowLength();
uint32_t extraW = glDesc.fAllocWidth - desc.fWidth;
uint32_t extraH = glDesc.fAllocHeight - desc.fHeight;
uint32_t maxTexels = extraW * extraH;
maxTexels = GrMax(extraW * desc.fHeight, maxTexels);
maxTexels = GrMax(desc.fWidth * extraH, maxTexels);
GrAutoSMalloc<128*128> texels(glDesc.fUploadByteCount * maxTexels);
uint32_t rowSize = desc.fWidth * glDesc.fUploadByteCount;
if (extraH) {
uint8_t* lastRowStart = (uint8_t*) srcData +
(desc.fHeight - 1) * rowSize;
uint8_t* extraRowStart = (uint8_t*)texels.get();
for (uint32_t i = 0; i < extraH; ++i) {
memcpy(extraRowStart, lastRowStart, rowSize);
extraRowStart += rowSize;
}
GR_GL(TexSubImage2D(GL_TEXTURE_2D, 0, 0, desc.fHeight, desc.fWidth,
extraH, glDesc.fUploadFormat, glDesc.fUploadType,
texels.get()));
}
if (extraW) {
uint8_t* edgeTexel = (uint8_t*)srcData + rowSize - glDesc.fUploadByteCount;
uint8_t* extraTexel = (uint8_t*)texels.get();
for (uint32_t j = 0; j < desc.fHeight; ++j) {
for (uint32_t i = 0; i < extraW; ++i) {
memcpy(extraTexel, edgeTexel, glDesc.fUploadByteCount);
extraTexel += glDesc.fUploadByteCount;
}
edgeTexel += rowSize;
}
GR_GL(TexSubImage2D(GL_TEXTURE_2D, 0, desc.fWidth, 0, extraW,
desc.fHeight, glDesc.fUploadFormat,
glDesc.fUploadType, texels.get()));
}
if (extraW && extraH) {
uint8_t* cornerTexel = (uint8_t*)srcData + desc.fHeight * rowSize
- glDesc.fUploadByteCount;
uint8_t* extraTexel = (uint8_t*)texels.get();
for (uint32_t i = 0; i < extraW*extraH; ++i) {
memcpy(extraTexel, cornerTexel, glDesc.fUploadByteCount);
extraTexel += glDesc.fUploadByteCount;
}
GR_GL(TexSubImage2D(GL_TEXTURE_2D, 0, desc.fWidth, desc.fHeight,
extraW, extraH, glDesc.fUploadFormat,
glDesc.fUploadType, texels.get()));
}
} else {
GR_GL(TexImage2D(GL_TEXTURE_2D, 0, internalFormat, glDesc.fAllocWidth,
glDesc.fAllocHeight, 0, glDesc.fUploadFormat,
glDesc.fUploadType, srcData));
GrGL_RestoreResetRowLength();
}
}
glDesc.fOrientation = GrGLTexture::kTopDown_Orientation;
GrGLRenderTarget::GLRenderTargetIDs rtIDs;
rtIDs.fStencilRenderbufferID = 0;
rtIDs.fMSColorRenderbufferID = 0;
rtIDs.fRTFBOID = 0;
rtIDs.fTexFBOID = 0;
rtIDs.fOwnIDs = true;
GLenum msColorRenderbufferFormat = -1;
if (renderTarget) {
#if GR_COLLECT_STATS
++fStats.fRenderTargetCreateCnt;
#endif
bool failed = true;
GLenum status;
GLint err;
// If need have both RT flag and srcData we have
// to invert the data before uploading because FBO
// will be rendered bottom up
GrAssert(NULL == srcData);
glDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
GR_GLEXT(fExts, GenFramebuffers(1, &rtIDs.fTexFBOID));
GrAssert(rtIDs.fTexFBOID);
// If we are using multisampling and any extension other than the IMG
// one we will create two FBOs. We render to one and then resolve to
// the texture bound to the other. The IMG extension does an implicit
// resolve.
if (samples > 1 && kIMG_MSFBO != fMSFBOType && kNone_MSFBO != fMSFBOType) {
GR_GLEXT(fExts, GenFramebuffers(1, &rtIDs.fRTFBOID));
GrAssert(0 != rtIDs.fRTFBOID);
GR_GLEXT(fExts, GenRenderbuffers(1, &rtIDs.fMSColorRenderbufferID));
GrAssert(0 != rtIDs.fMSColorRenderbufferID);
if (!fboInternalFormat(desc.fFormat, &msColorRenderbufferFormat)) {
GR_GLEXT(fExts,
DeleteRenderbuffers(1, &rtIDs.fMSColorRenderbufferID));
GR_GL(DeleteTextures(1, &glDesc.fTextureID));
GR_GLEXT(fExts, DeleteFramebuffers(1, &rtIDs.fTexFBOID));
GR_GLEXT(fExts, DeleteFramebuffers(1, &rtIDs.fRTFBOID));
fHWDrawState.fTexture = NULL;
return return_null_texture();
}
} else {
rtIDs.fRTFBOID = rtIDs.fTexFBOID;
}
int attempts = 1;
if (!(kNoPathRendering_TextureFlag & desc.fFlags)) {
GR_GLEXT(fExts, GenRenderbuffers(1, &rtIDs.fStencilRenderbufferID));
GrAssert(0 != rtIDs.fStencilRenderbufferID);
attempts = GR_ARRAY_COUNT(GR_GL_STENCIL_FORMAT_ARRAY);
}
// need to unbind the texture before we call FramebufferTexture2D
GR_GL(BindTexture(GL_TEXTURE_2D, 0));
#if GR_COLLECT_STATS
++fStats.fTextureChngCnt;
#endif
fHWDrawState.fTexture = NULL;
err = ~GL_NO_ERROR;
for (int i = 0; i < attempts; ++i) {
if (rtIDs.fStencilRenderbufferID) {
GR_GLEXT(fExts, BindRenderbuffer(GR_RENDERBUFFER,
rtIDs.fStencilRenderbufferID));
if (samples > 1) {
GR_GLEXT_NO_ERR(fExts, RenderbufferStorageMultisample(
GR_RENDERBUFFER,
samples,
GR_GL_STENCIL_FORMAT_ARRAY[i],
glDesc.fAllocWidth,
glDesc.fAllocHeight));
} else {
GR_GLEXT_NO_ERR(fExts, RenderbufferStorage(
GR_RENDERBUFFER,
GR_GL_STENCIL_FORMAT_ARRAY[i],
glDesc.fAllocWidth,
glDesc.fAllocHeight));
}
err = glGetError();
if (err != GL_NO_ERROR) {
continue;
}
}
if (rtIDs.fRTFBOID != rtIDs.fTexFBOID) {
GrAssert(samples > 1);
GR_GLEXT(fExts, BindRenderbuffer(GR_RENDERBUFFER,
rtIDs.fMSColorRenderbufferID));
GR_GLEXT_NO_ERR(fExts, RenderbufferStorageMultisample(
GR_RENDERBUFFER,
samples,
msColorRenderbufferFormat,
glDesc.fAllocWidth,
glDesc.fAllocHeight));
err = glGetError();
if (err != GL_NO_ERROR) {
continue;
}
}
GR_GLEXT(fExts, BindFramebuffer(GR_FRAMEBUFFER, rtIDs.fTexFBOID));
#if GR_COLLECT_STATS
++fStats.fRenderTargetChngCnt;
#endif
if (kIMG_MSFBO == fMSFBOType && samples > 1) {
GR_GLEXT(fExts, FramebufferTexture2DMultisample(
GR_FRAMEBUFFER,
GR_COLOR_ATTACHMENT0,
GL_TEXTURE_2D,
glDesc.fTextureID,
0,
samples));
} else {
GR_GLEXT(fExts, FramebufferTexture2D(GR_FRAMEBUFFER,
GR_COLOR_ATTACHMENT0,
GL_TEXTURE_2D,
glDesc.fTextureID, 0));
}
if (rtIDs.fRTFBOID != rtIDs.fTexFBOID) {
GLenum status = GR_GLEXT(fExts,
CheckFramebufferStatus(GR_FRAMEBUFFER));
if (status != GR_FRAMEBUFFER_COMPLETE) {
GrPrintf("-- glCheckFramebufferStatus %x %d %d\n",
status, desc.fWidth, desc.fHeight);
continue;
}
GR_GLEXT(fExts, BindFramebuffer(GR_FRAMEBUFFER, rtIDs.fRTFBOID));
#if GR_COLLECT_STATS
++fStats.fRenderTargetChngCnt;
#endif
GR_GLEXT(fExts, FramebufferRenderbuffer(GR_FRAMEBUFFER,
GR_COLOR_ATTACHMENT0,
GR_RENDERBUFFER,
rtIDs.fMSColorRenderbufferID));
}
if (rtIDs.fStencilRenderbufferID) {
// bind the stencil to rt fbo if present, othewise the tex fbo
GR_GLEXT(fExts, FramebufferRenderbuffer(GR_FRAMEBUFFER,
GR_STENCIL_ATTACHMENT,
GR_RENDERBUFFER,
rtIDs.fStencilRenderbufferID));
}
status = GR_GLEXT(fExts, CheckFramebufferStatus(GR_FRAMEBUFFER));
#if GR_GL_DESKTOP
// On some implementations you have to be bound as DEPTH_STENCIL.
// (Even binding to DEPTH and STENCIL separately with the same
// buffer doesn't work.)
if (rtIDs.fStencilRenderbufferID &&
status != GR_FRAMEBUFFER_COMPLETE) {
GR_GLEXT(fExts, FramebufferRenderbuffer(GR_FRAMEBUFFER,
GR_STENCIL_ATTACHMENT,
GR_RENDERBUFFER,
0));
GR_GLEXT(fExts,
FramebufferRenderbuffer(GR_FRAMEBUFFER,
GR_DEPTH_STENCIL_ATTACHMENT,
GR_RENDERBUFFER,
rtIDs.fStencilRenderbufferID));
status = GR_GLEXT(fExts, CheckFramebufferStatus(GR_FRAMEBUFFER));
}
#endif
if (status != GR_FRAMEBUFFER_COMPLETE) {
GrPrintf("-- glCheckFramebufferStatus %x %d %d\n",
status, desc.fWidth, desc.fHeight);
#if GR_GL_DESKTOP
if (rtIDs.fStencilRenderbufferID) {
GR_GLEXT(fExts, FramebufferRenderbuffer(GR_FRAMEBUFFER,
GR_DEPTH_STENCIL_ATTACHMENT,
GR_RENDERBUFFER,
0));
}
#endif
continue;
}
// we're successful!
failed = false;
break;
}
if (failed) {
if (rtIDs.fStencilRenderbufferID) {
GR_GLEXT(fExts,
DeleteRenderbuffers(1, &rtIDs.fStencilRenderbufferID));
}
if (rtIDs.fMSColorRenderbufferID) {
GR_GLEXT(fExts,
DeleteRenderbuffers(1, &rtIDs.fMSColorRenderbufferID));
}
if (rtIDs.fRTFBOID != rtIDs.fTexFBOID) {
GR_GLEXT(fExts, DeleteFramebuffers(1, &rtIDs.fRTFBOID));
}
if (rtIDs.fTexFBOID) {
GR_GLEXT(fExts, DeleteFramebuffers(1, &rtIDs.fTexFBOID));
}
GR_GL(DeleteTextures(1, &glDesc.fTextureID));
return return_null_texture();
}
}
#ifdef TRACE_TEXTURE_CREATION
GrPrintf("--- new texture [%d] size=(%d %d) bpp=%d\n",
tex->fTextureID, width, height, tex->fUploadByteCount);
#endif
GrGLTexture* tex = new GrGLTexture(glDesc, rtIDs, this);
if (0 != rtIDs.fTexFBOID) {
GrRenderTarget* rt = tex->asRenderTarget();
// We've messed with FBO state but may not have set the correct viewport
// so just dirty the rendertarget state to force a resend.
fHWDrawState.fRenderTarget = NULL;
// clear the new stencil buffer if we have one
if (!(desc.fFlags & kNoPathRendering_TextureFlag)) {
GrRenderTarget* rtSave = fCurrDrawState.fRenderTarget;
fCurrDrawState.fRenderTarget = rt;
eraseStencil(0, ~0);
fCurrDrawState.fRenderTarget = rtSave;
}
}
return tex;
}
GrRenderTarget* GrGpuGL::defaultRenderTarget() {
return fDefaultRenderTarget;
}
GrVertexBuffer* GrGpuGL::createVertexBuffer(uint32_t size, bool dynamic) {
GLuint id;
GR_GL(GenBuffers(1, &id));
if (id) {
GR_GL(BindBuffer(GL_ARRAY_BUFFER, id));
GrGLClearErr();
// make sure driver can allocate memory for this buffer
GR_GL_NO_ERR(BufferData(GL_ARRAY_BUFFER, size, NULL,
dynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW));
if (glGetError() != GL_NO_ERROR) {
GR_GL(DeleteBuffers(1, &id));
// deleting bound buffer does implicit bind to 0
fHWGeometryState.fVertexBuffer = NULL;
return NULL;
}
GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(id, this,
size, dynamic);
fHWGeometryState.fVertexBuffer = vertexBuffer;
return vertexBuffer;
}
return NULL;
}
GrIndexBuffer* GrGpuGL::createIndexBuffer(uint32_t size, bool dynamic) {
GLuint id;
GR_GL(GenBuffers(1, &id));
if (id) {
GR_GL(BindBuffer(GL_ELEMENT_ARRAY_BUFFER, id));
GrGLClearErr();
// make sure driver can allocate memory for this buffer
GR_GL_NO_ERR(BufferData(GL_ELEMENT_ARRAY_BUFFER, size, NULL,
dynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW));
if (glGetError() != GL_NO_ERROR) {
GR_GL(DeleteBuffers(1, &id));
// deleting bound buffer does implicit bind to 0
fHWGeometryState.fIndexBuffer = NULL;
return NULL;
}
GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(id, this,
size, dynamic);
fHWGeometryState.fIndexBuffer = indexBuffer;
return indexBuffer;
}
return NULL;
}
void GrGpuGL::setDefaultRenderTargetSize(uint32_t width, uint32_t height) {
GrIRect viewport(0, height, width, 0);
if (viewport != fDefaultRenderTarget->viewport()) {
fDefaultRenderTarget->setViewport(viewport);
if (fHWDrawState.fRenderTarget == fDefaultRenderTarget) {
fHWDrawState.fRenderTarget = NULL;
}
}
}
void GrGpuGL::flushScissor(const GrIRect* rect) {
GrAssert(NULL != fCurrDrawState.fRenderTarget);
const GrIRect& vp =
((GrGLRenderTarget*)fCurrDrawState.fRenderTarget)->viewport();
if (NULL != rect &&
rect->contains(vp)) {
rect = NULL;
}
if (NULL != rect) {
GrIRect scissor;
// viewport is already in GL coords
// create a scissor in GL coords (top > bottom)
scissor.setLTRB(vp.fLeft + rect->fLeft,
vp.fTop - rect->fTop,
vp.fLeft + rect->fRight,
vp.fTop - rect->fBottom);
if (fHWBounds.fScissorRect != scissor) {
GR_GL(Scissor(scissor.fLeft, scissor.fBottom,
scissor.width(), -scissor.height()));
fHWBounds.fScissorRect = scissor;
}
if (!fHWBounds.fScissorEnabled) {
GR_GL(Enable(GL_SCISSOR_TEST));
fHWBounds.fScissorEnabled = true;
}
} else {
if (fHWBounds.fScissorEnabled) {
GR_GL(Disable(GL_SCISSOR_TEST));
fHWBounds.fScissorEnabled = false;
}
}
}
void GrGpuGL::setSamplerStateImm(const GrSamplerState& state) {
GLenum filter = state.isFilter() ? GL_LINEAR : GL_NEAREST;
GR_GL(TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter));
GR_GL(TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter));
GR_GL(TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
GrGLTexture::gWrapMode2GLWrap[state.getWrapX()]));
GR_GL(TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
GrGLTexture::gWrapMode2GLWrap[state.getWrapY()]));
}
void GrGpuGL::eraseColor(GrColor color) {
flushRenderTarget();
if (fHWBounds.fScissorEnabled) {
GR_GL(Disable(GL_SCISSOR_TEST));
}
GR_GL(ColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE));
GR_GL(ClearColor(GrColorUnpackR(color)/255.f,
GrColorUnpackG(color)/255.f,
GrColorUnpackB(color)/255.f,
GrColorUnpackA(color)/255.f));
GR_GL(Clear(GL_COLOR_BUFFER_BIT));
fHWBounds.fScissorEnabled = false;
fWriteMaskChanged = true;
}
void GrGpuGL::eraseStencil(uint32_t value, uint32_t mask) {
flushRenderTarget();
if (fHWBounds.fScissorEnabled) {
GR_GL(Disable(GL_SCISSOR_TEST));
}
GR_GL(StencilMask(mask));
GR_GL(ClearStencil(value));
GR_GL(Clear(GL_STENCIL_BUFFER_BIT));
fHWBounds.fScissorEnabled = false;
fWriteMaskChanged = true;
}
void GrGpuGL::eraseStencilClip() {
GLint stencilBitCount;
GR_GL(GetIntegerv(GL_STENCIL_BITS, &stencilBitCount));
GrAssert(stencilBitCount > 0);
GLint clipStencilMask = (1 << (stencilBitCount - 1));
eraseStencil(0, clipStencilMask);
}
void GrGpuGL::forceRenderTargetFlush() {
flushRenderTarget();
}
bool GrGpuGL::readPixels(int left, int top, int width, int height,
GrTexture::PixelConfig config, void* buffer) {
GLenum internalFormat; // we don't use this for glReadPixels
GLenum format;
GLenum type;
if (!this->canBeTexture(config, &internalFormat, &format, &type)) {
return false;
}
GrAssert(NULL != fCurrDrawState.fRenderTarget);
const GrIRect& vp = ((GrGLRenderTarget*)fCurrDrawState.fRenderTarget)->viewport();
// Brian says that viewport rects are already upside down (grrrrr)
glReadPixels(left, -vp.height() - top - height, width, height,
format, type, buffer);
// now reverse the order of the rows, since GL's are bottom-to-top, but our
// API presents top-to-bottom
{
size_t stride = width * GrTexture::BytesPerPixel(config);
GrAutoMalloc rowStorage(stride);
void* tmp = rowStorage.get();
const int halfY = height >> 1;
char* top = reinterpret_cast<char*>(buffer);
char* bottom = top + (height - 1) * stride;
for (int y = 0; y < halfY; y++) {
memcpy(tmp, top, stride);
memcpy(top, bottom, stride);
memcpy(bottom, tmp, stride);
top += stride;
bottom -= stride;
}
}
return true;
}
void GrGpuGL::flushRenderTarget() {
if (fHWDrawState.fRenderTarget != fCurrDrawState.fRenderTarget) {
GrGLRenderTarget* rt = (GrGLRenderTarget*)fCurrDrawState.fRenderTarget;
GR_GLEXT(fExts, BindFramebuffer(GR_FRAMEBUFFER, rt->renderFBOID()));
#if GR_COLLECT_STATS
++fStats.fRenderTargetChngCnt;
#endif
rt->setDirty(true);
#if GR_DEBUG
GLenum status = GR_GLEXT(fExts, CheckFramebufferStatus(GR_FRAMEBUFFER));
if (status != GR_FRAMEBUFFER_COMPLETE) {
GrPrintf("-- glCheckFramebufferStatus %x\n", status);
}
#endif
fHWDrawState.fRenderTarget = fCurrDrawState.fRenderTarget;
const GrIRect& vp = rt->viewport();
fRenderTargetChanged = true;
if (fHWBounds.fViewportRect != vp) {
GR_GL(Viewport(vp.fLeft,
vp.fBottom,
vp.width(),
-vp.height()));
fHWBounds.fViewportRect = vp;
}
}
}
GLenum gPrimitiveType2GLMode[] = {
GL_TRIANGLES,
GL_TRIANGLE_STRIP,
GL_TRIANGLE_FAN,
GL_POINTS,
GL_LINES,
GL_LINE_STRIP
};
void GrGpuGL::drawIndexedHelper(PrimitiveType type,
uint32_t startVertex,
uint32_t startIndex,
uint32_t vertexCount,
uint32_t indexCount) {
GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
GLvoid* indices = (GLvoid*)(sizeof(uint16_t) * startIndex);
if (kReserved_GeometrySrcType == fGeometrySrc.fIndexSrc) {
indices = (GLvoid*)((intptr_t)indices + (intptr_t)fIndices.get());
} else if (kArray_GeometrySrcType == fGeometrySrc.fIndexSrc) {
indices = (GLvoid*)((intptr_t)indices +
(intptr_t)fGeometrySrc.fIndexArray);
}
GR_GL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
GL_UNSIGNED_SHORT, indices));
}
void GrGpuGL::drawNonIndexedHelper(PrimitiveType type,
uint32_t startVertex,
uint32_t vertexCount) {
GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
GR_GL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
}
#if !defined(SK_GL_HAS_COLOR4UB)
static inline GrFixed byte2fixed(unsigned value) {
return (value + (value >> 7)) << 8;
}
#endif
void GrGpuGL::resolveTextureRenderTarget(GrGLTexture* texture) {
GrGLRenderTarget* rt = (GrGLRenderTarget*) texture->asRenderTarget();
if (NULL != rt && rt->needsResolve()) {
GrAssert(kNone_MSFBO != fMSFBOType);
GrAssert(rt->textureFBOID() != rt->renderFBOID());
GR_GLEXT(fExts, BindFramebuffer(GR_READ_FRAMEBUFFER,
rt->renderFBOID()));
GR_GLEXT(fExts, BindFramebuffer(GR_DRAW_FRAMEBUFFER,
rt->textureFBOID()));
#if GR_COLLECT_STATS
++fStats.fRenderTargetChngCnt;
#endif
// make sure we go through set render target
fHWDrawState.fRenderTarget = NULL;
GLint left = 0;
GLint right = texture->contentWidth();
// we will have rendered to the top of the FBO.
GLint top = texture->allocHeight();
GLint bottom = texture->allocHeight() - texture->contentHeight();
if (kApple_MSFBO == fMSFBOType) {
GR_GL(Enable(GL_SCISSOR_TEST));
GR_GL(Scissor(left, bottom, right-left, top-bottom));
GR_GLEXT(fExts, ResolveMultisampleFramebuffer());
fHWBounds.fScissorRect.setEmpty();
fHWBounds.fScissorEnabled = true;
} else {
GR_GLEXT(fExts, BlitFramebuffer(left, bottom, right, top,
left, bottom, right, top,
GL_COLOR_BUFFER_BIT, GL_NEAREST));
}
rt->setDirty(false);
}
}
void GrGpuGL::flushStencil() {
// use stencil for clipping if clipping is enabled and the clip
// has been written into the stencil.
bool stencilClip = fClipState.fClipInStencil &&
(kClip_StateBit & fCurrDrawState.fFlagBits);
bool stencilChange =
fWriteMaskChanged ||
fHWStencilClip != stencilClip ||
fHWDrawState.fStencilPass != fCurrDrawState.fStencilPass ||
(kNone_StencilPass != fCurrDrawState.fStencilPass &&
(StencilPass)kSetClip_StencilPass != fCurrDrawState.fStencilPass &&
fHWDrawState.fReverseFill != fCurrDrawState.fReverseFill);
if (stencilChange) {
GLint stencilBitCount;
GLint clipStencilMask;
GLint pathStencilMask;
GR_GL(GetIntegerv(GL_STENCIL_BITS, &stencilBitCount));
GrAssert(stencilBitCount > 0 ||
kNone_StencilPass == fCurrDrawState.fStencilPass);
clipStencilMask = (1 << (stencilBitCount - 1));
pathStencilMask = clipStencilMask - 1;
switch (fCurrDrawState.fStencilPass) {
case kNone_StencilPass:
if (stencilClip) {
GR_GL(Enable(GL_STENCIL_TEST));
GR_GL(StencilFunc(GL_EQUAL,
clipStencilMask,
clipStencilMask));
GR_GL(StencilOp(GL_KEEP, GL_KEEP, GL_KEEP));
} else {
GR_GL(Disable(GL_STENCIL_TEST));
}
GR_GL(ColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE));
if (!fSingleStencilPassForWinding) {
GR_GL(Disable(GL_CULL_FACE));
}
break;
case kEvenOddStencil_StencilPass:
GR_GL(Enable(GL_STENCIL_TEST));
if (stencilClip) {
GR_GL(StencilFunc(GL_EQUAL, clipStencilMask, clipStencilMask));
} else {
GR_GL(StencilFunc(GL_ALWAYS, 0x0, 0x0));
}
GR_GL(StencilMask(pathStencilMask));
GR_GL(ColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE));
GR_GL(StencilOp(GL_KEEP, GL_INVERT, GL_INVERT));
GR_GL(ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE));
if (!fSingleStencilPassForWinding) {
GR_GL(Disable(GL_CULL_FACE));
}
break;
case kEvenOddColor_StencilPass: {
GR_GL(Enable(GL_STENCIL_TEST));
GLint funcRef = 0;
GLuint funcMask = pathStencilMask;
if (stencilClip) {
funcRef |= clipStencilMask;
funcMask |= clipStencilMask;
}
if (!fCurrDrawState.fReverseFill) {
funcRef |= pathStencilMask;
}
glStencilFunc(GL_EQUAL, funcRef, funcMask);
glStencilMask(pathStencilMask);
GR_GL(StencilOp(GL_ZERO, GL_ZERO, GL_ZERO));
GR_GL(ColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE));
if (!fSingleStencilPassForWinding) {
GR_GL(Disable(GL_CULL_FACE));
}
} break;
case kWindingStencil1_StencilPass:
GR_GL(Enable(GL_STENCIL_TEST));
if (fHasStencilWrap) {
if (stencilClip) {
GR_GL(StencilFunc(GL_EQUAL,
clipStencilMask,
clipStencilMask));
} else {
GR_GL(StencilFunc(GL_ALWAYS, 0x0, 0x0));
}
if (fSingleStencilPassForWinding) {
GR_GL(StencilOpSeparate(GL_FRONT, GL_KEEP,
GL_INCR_WRAP, GL_INCR_WRAP));
GR_GL(StencilOpSeparate(GL_BACK, GL_KEEP,
GL_DECR_WRAP, GL_DECR_WRAP));
} else {
GR_GL(StencilOp(GL_KEEP, GL_INCR_WRAP, GL_INCR_WRAP));
GR_GL(Enable(GL_CULL_FACE));
GR_GL(CullFace(GL_BACK));
}
} else {
// If we don't have wrap then we use the Func to detect
// values that would wrap (0 on decr and mask on incr). We
// make the func fail on these values and use the sfail op
// to effectively wrap by inverting.
// This applies whether we are doing a two-pass (front faces
// followed by back faces) or a single pass (separate func/op)
// Note that in the case where we are also using stencil to
// clip this means we will write into the path bits in clipped
// out pixels. We still apply the clip bit in the color pass
// stencil func so we don't draw color outside the clip.
// We also will clear the stencil bits in clipped pixels by
// using zero in the sfail op with write mask set to the
// path mask.
GR_GL(Enable(GL_STENCIL_TEST));
if (fSingleStencilPassForWinding) {
GR_GL(StencilFuncSeparate(GL_FRONT,
GL_NOTEQUAL,
pathStencilMask,
pathStencilMask));
GR_GL(StencilFuncSeparate(GL_BACK,
GL_NOTEQUAL,
0x0,
pathStencilMask));
GR_GL(StencilOpSeparate(GL_FRONT, GL_INVERT,
GL_INCR, GL_INCR));
GR_GL(StencilOpSeparate(GL_BACK, GL_INVERT,
GL_DECR, GL_DECR));
} else {
GR_GL(StencilFunc(GL_NOTEQUAL,
pathStencilMask,
pathStencilMask));
GR_GL(StencilOp(GL_INVERT, GL_INCR, GL_INCR));
GR_GL(Enable(GL_CULL_FACE));
GR_GL(CullFace(GL_BACK));
}
}
GR_GL(StencilMask(pathStencilMask));
GR_GL(ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE));
break;
case kWindingStencil2_StencilPass:
GrAssert(!fSingleStencilPassForWinding);
GR_GL(Enable(GL_STENCIL_TEST));
if (fHasStencilWrap) {
if (stencilClip) {
GR_GL(StencilFunc(GL_EQUAL,
clipStencilMask,
clipStencilMask));
} else {
GR_GL(StencilFunc(GL_ALWAYS, 0x0, 0x0));
}
GR_GL(StencilOp(GL_DECR_WRAP, GL_DECR_WRAP, GL_DECR_WRAP));
} else {
GR_GL(StencilFunc(GL_NOTEQUAL, 0x0, pathStencilMask));
GR_GL(StencilOp(GL_INVERT, GL_DECR, GL_DECR));
}
GR_GL(StencilMask(pathStencilMask));
GR_GL(Enable(GL_CULL_FACE));
GR_GL(CullFace(GL_FRONT));
GR_GL(ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE));
break;
case kWindingColor_StencilPass: {
GR_GL(Enable(GL_STENCIL_TEST));
GLint funcRef = 0;
GLuint funcMask = pathStencilMask;
GLenum funcFunc;
if (stencilClip) {
funcRef |= clipStencilMask;
funcMask |= clipStencilMask;
}
if (fCurrDrawState.fReverseFill) {
funcFunc = GL_EQUAL;
} else {
funcFunc = GL_LESS;
}
GR_GL(StencilFunc(funcFunc, funcRef, funcMask));
GR_GL(StencilMask(pathStencilMask));
// must zero in sfail because winding w/o wrap will write
// path stencil bits in clipped out pixels
GR_GL(StencilOp(GL_ZERO, GL_ZERO, GL_ZERO));
GR_GL(ColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE));
if (!fSingleStencilPassForWinding) {
GR_GL(Disable(GL_CULL_FACE));
}
} break;
case kSetClip_StencilPass:
GR_GL(Enable(GL_STENCIL_TEST));
GR_GL(StencilFunc(GL_ALWAYS, clipStencilMask, clipStencilMask));
GR_GL(StencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE));
GR_GL(StencilMask(clipStencilMask));
GR_GL(ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE));
if (!fSingleStencilPassForWinding) {
GR_GL(Disable(GL_CULL_FACE));
}
break;
default:
GrAssert(!"Unexpected stencil pass.");
break;
}
fHWDrawState.fStencilPass = fCurrDrawState.fStencilPass;
fHWDrawState.fReverseFill = fCurrDrawState.fReverseFill;
fWriteMaskChanged = false;
fHWStencilClip = stencilClip;
}
}
void GrGpuGL::flushGLStateCommon(PrimitiveType type) {
bool usingTexture = VertexHasTexCoords(fGeometrySrc.fVertexLayout);
// bind texture and set sampler state
if (usingTexture) {
GrGLTexture* nextTexture = (GrGLTexture*)fCurrDrawState.fTexture;
if (NULL != nextTexture) {
// if we created a rt/tex and rendered to it without using a texture
// and now we're texuring from the rt it will still be the last bound
// texture, but it needs resolving. So keep this out of the last
// != next check.
resolveTextureRenderTarget(nextTexture);
if (fHWDrawState.fTexture != nextTexture) {
GR_GL(BindTexture(GL_TEXTURE_2D, nextTexture->textureID()));
#if GR_COLLECT_STATS
++fStats.fTextureChngCnt;
#endif
//GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
fHWDrawState.fTexture = nextTexture;
}
const GrSamplerState& lastSampler = nextTexture->samplerState();
if (lastSampler.isFilter() != fCurrDrawState.fSamplerState.isFilter()) {
GLenum filter = fCurrDrawState.fSamplerState.isFilter() ?
GL_LINEAR :
GL_NEAREST;
GR_GL(TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
filter));
GR_GL(TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
filter));
}
if (lastSampler.getWrapX() != fCurrDrawState.fSamplerState.getWrapX()) {
GR_GL(TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
GrGLTexture::gWrapMode2GLWrap[
fCurrDrawState.fSamplerState.getWrapX()]));
}
if (lastSampler.getWrapY() != fCurrDrawState.fSamplerState.getWrapY()) {
GR_GL(TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
GrGLTexture::gWrapMode2GLWrap[
fCurrDrawState.fSamplerState.getWrapY()]));
}
nextTexture->setSamplerState(fCurrDrawState.fSamplerState);
} else {
GrAssert(!"Rendering with texture vert flag set but no texture");
if (NULL != fHWDrawState.fTexture) {
GR_GL(BindTexture(GL_TEXTURE_2D, 0));
// GrPrintf("---- bindtexture 0\n");
#if GR_COLLECT_STATS
++fStats.fTextureChngCnt;
#endif
fHWDrawState.fTexture = NULL;
}
}
}
flushRenderTarget();
if ((fCurrDrawState.fFlagBits & kDither_StateBit) !=
(fHWDrawState.fFlagBits & kDither_StateBit)) {
if (fCurrDrawState.fFlagBits & kDither_StateBit) {
GR_GL(Enable(GL_DITHER));
} else {
GR_GL(Disable(GL_DITHER));
}
}
#if GR_GL_DESKTOP
// ES doesn't support toggling GL_MULTISAMPLE and doesn't have
// smooth lines.
if (fRenderTargetChanged ||
(fCurrDrawState.fFlagBits & kAntialias_StateBit) !=
(fHWDrawState.fFlagBits & kAntialias_StateBit)) {
GLint msaa = 0;
// only perform query if we know MSAA is supported.
// calling on non-MSAA target caused a crash in one environment,
// though I don't think it should.
if (!fAASamples[kHigh_AALevel]) {
GR_GL(GetIntegerv(GL_SAMPLE_BUFFERS, &msaa));
}
if (fCurrDrawState.fFlagBits & kAntialias_StateBit) {
if (msaa) {
GR_GL(Enable(GL_MULTISAMPLE));
} else {
GR_GL(Enable(GL_LINE_SMOOTH));
}
} else {
if (msaa) {
GR_GL(Disable(GL_MULTISAMPLE));
}
GR_GL(Disable(GL_LINE_SMOOTH));
}
}
#endif
bool blendOff = canDisableBlend();
if (fHWBlendDisabled != blendOff) {
if (blendOff) {
GR_GL(Disable(GL_BLEND));
} else {
GR_GL(Enable(GL_BLEND));
}
fHWBlendDisabled = blendOff;
}
if (!blendOff) {
if (fHWDrawState.fSrcBlend != fCurrDrawState.fSrcBlend ||
fHWDrawState.fDstBlend != fCurrDrawState.fDstBlend) {
GR_GL(BlendFunc(gXfermodeCoeff2Blend[fCurrDrawState.fSrcBlend],
gXfermodeCoeff2Blend[fCurrDrawState.fDstBlend]));
fHWDrawState.fSrcBlend = fCurrDrawState.fSrcBlend;
fHWDrawState.fDstBlend = fCurrDrawState.fDstBlend;
}
}
// check for circular rendering
GrAssert(!usingTexture ||
NULL == fCurrDrawState.fRenderTarget ||
NULL == fCurrDrawState.fTexture ||
fCurrDrawState.fTexture->asRenderTarget() != fCurrDrawState.fRenderTarget);
flushStencil();
fHWDrawState.fFlagBits = fCurrDrawState.fFlagBits;
}
void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
fHWGeometryState.fVertexBuffer = buffer;
}
void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
GrAssert(!(kBuffer_GeometrySrcType == fGeometrySrc.fVertexSrc &&
buffer == fGeometrySrc.fVertexBuffer));
if (fHWGeometryState.fVertexBuffer == buffer) {
// deleting bound buffer does implied bind to 0
fHWGeometryState.fVertexBuffer = NULL;
}
}
void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
fGeometrySrc.fIndexBuffer = buffer;
}
void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
GrAssert(!(kBuffer_GeometrySrcType == fGeometrySrc.fIndexSrc &&
buffer == fGeometrySrc.fIndexBuffer));
if (fHWGeometryState.fIndexBuffer == buffer) {
// deleting bound buffer does implied bind to 0
fHWGeometryState.fIndexBuffer = NULL;
}
}
void GrGpuGL::notifyTextureBind(GrGLTexture* texture) {
fHWDrawState.fTexture = texture;
#if GR_COLLECT_STATS
++fStats.fTextureChngCnt;
#endif
}
void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
GrAssert(NULL != renderTarget);
// if the bound FBO is destroyed we can't rely on the implicit bind to 0
// a) we want the default RT which may not be FBO 0
// b) we set more state than just FBO based on the RT
// So trash the HW state to force an RT flush next time
if (fCurrDrawState.fRenderTarget == renderTarget) {
fCurrDrawState.fRenderTarget = (GrRenderTarget*)&fDefaultRenderTarget;
}
if (fHWDrawState.fRenderTarget == renderTarget) {
fHWDrawState.fRenderTarget = NULL;
}
if (fClipState.fStencilClipTarget == renderTarget) {
fClipState.fStencilClipTarget = NULL;
}
}
void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
if (fCurrDrawState.fTexture == texture) {
fCurrDrawState.fTexture = NULL;
}
if (fHWDrawState.fTexture == texture) {
// deleting bound texture does implied bind to 0
fHWDrawState.fTexture = NULL;
}
}
void GrGpuGL::notifyTextureRemoveRenderTarget(GrGLTexture* texture) {
GrAssert(NULL != texture->asRenderTarget());
// if there is a pending resolve, perform it.
resolveTextureRenderTarget(texture);
}
bool GrGpuGL::canBeTexture(GrTexture::PixelConfig config,
GLenum* internalFormat,
GLenum* format,
GLenum* type) {
switch (config) {
case GrTexture::kRGBA_8888_PixelConfig:
case GrTexture::kRGBX_8888_PixelConfig: // todo: can we tell it our X?
*format = SK_GL_32BPP_COLOR_FORMAT;
*internalFormat = GL_RGBA;
*type = GL_UNSIGNED_BYTE;
break;
case GrTexture::kRGB_565_PixelConfig:
*format = GL_RGB;
*internalFormat = GL_RGB;
*type = GL_UNSIGNED_SHORT_5_6_5;
break;
case GrTexture::kRGBA_4444_PixelConfig:
*format = GL_RGBA;
*internalFormat = GL_RGBA;
*type = GL_UNSIGNED_SHORT_4_4_4_4;
break;
case GrTexture::kIndex_8_PixelConfig:
if (this->supports8BitPalette()) {
*format = GR_PALETTE8_RGBA8;
*internalFormat = GR_PALETTE8_RGBA8;
*type = GL_UNSIGNED_BYTE; // unused I think
} else {
return false;
}
break;
case GrTexture::kAlpha_8_PixelConfig:
*format = GL_ALPHA;
*internalFormat = GL_ALPHA;
*type = GL_UNSIGNED_BYTE;
break;
default:
return false;
}
return true;
}
/* On ES the internalFormat and format must match for TexImage and we use
GL_RGB, GL_RGBA for color formats. We also generally like having the driver
decide the internalFormat. However, on ES internalFormat for
RenderBufferStorage* has to be a specific format (not a base format like
GL_RGBA).
*/
bool GrGpuGL::fboInternalFormat(GrTexture::PixelConfig config, GLenum* format) {
switch (config) {
case GrTexture::kRGBA_8888_PixelConfig:
case GrTexture::kRGBX_8888_PixelConfig:
if (fRGBA8Renderbuffer) {
*format = GR_RGBA8;
return true;
} else {
return false;
}
#if GR_GL_ES // ES2 supports 565. ES1 supports it with FBO extension
// desktop GL has no such internal format
case GrTexture::kRGB_565_PixelConfig:
*format = GR_RGB565;
return true;
#endif
case GrTexture::kRGBA_4444_PixelConfig:
*format = GL_RGBA4;
return true;
default:
return false;
}
}
///////////////////////////////////////////////////////////////////////////////
void GrGLCheckErr(const char* location, const char* call) {
uint32_t err = glGetError();
if (GL_NO_ERROR != err) {
GrPrintf("---- glGetError %x", err);
if (NULL != location) {
GrPrintf(" at\n\t%s", location);
}
if (NULL != call) {
GrPrintf("\n\t\t%s", call);
}
GrPrintf("\n");
}
}
///////////////////////////////////////////////////////////////////////////////
typedef void (*glProc)(void);
void get_gl_proc(const char procName[], glProc *address) {
#if GR_WIN32_BUILD
*address = wglGetProcAddress(procName);
GrAssert(NULL != *address);
#elif GR_MAC_BUILD || GR_IOS_BUILD
GrAssert(!"Extensions don't need to be initialized!");
#elif GR_ANDROID_BUILD
*address = eglGetProcAddress(procName);
GrAssert(NULL != *address);
#elif GR_LINUX_BUILD
GR_STATIC_ASSERT(!"Add environment-dependent implementation here");
//*address = glXGetProcAddressARB(procName);
//*address = eglGetProcAddress(procName);
#elif GR_QNX_BUILD
*address = eglGetProcAddress(procName);
GrAssert(NULL != *address);
#else
// hopefully we're on a system with EGL
*address = eglGetProcAddress(procName);
GrAssert(NULL != *address);
#endif
}
#define GET_PROC(EXT_STRUCT, PROC_NAME, EXT_TAG) \
get_gl_proc("gl" #PROC_NAME #EXT_TAG, (glProc*)&EXT_STRUCT-> PROC_NAME);
extern void GrGLInitExtensions(GrGLExts* exts) {
exts->GenFramebuffers = NULL;
exts->BindFramebuffer = NULL;
exts->FramebufferTexture2D = NULL;
exts->CheckFramebufferStatus = NULL;
exts->DeleteFramebuffers = NULL;
exts->RenderbufferStorage = NULL;
exts->GenRenderbuffers = NULL;
exts->DeleteRenderbuffers = NULL;
exts->FramebufferRenderbuffer = NULL;
exts->BindRenderbuffer = NULL;
exts->RenderbufferStorageMultisample = NULL;
exts->BlitFramebuffer = NULL;
exts->ResolveMultisampleFramebuffer = NULL;
exts->FramebufferTexture2DMultisample = NULL;
exts->MapBuffer = NULL;
exts->UnmapBuffer = NULL;
#if GR_MAC_BUILD
exts->GenFramebuffers = glGenFramebuffers;
exts->BindFramebuffer = glBindFramebuffer;
exts->FramebufferTexture2D = glFramebufferTexture2D;
exts->CheckFramebufferStatus = glCheckFramebufferStatus;
exts->DeleteFramebuffers = glDeleteFramebuffers;
exts->RenderbufferStorage = glRenderbufferStorage;
exts->GenRenderbuffers = glGenRenderbuffers;
exts->DeleteRenderbuffers = glDeleteRenderbuffers;
exts->FramebufferRenderbuffer = glFramebufferRenderbuffer;
exts->BindRenderbuffer = glBindRenderbuffer;
exts->RenderbufferStorageMultisample = glRenderbufferStorageMultisample;
exts->BlitFramebuffer = glBlitFramebuffer;
exts->MapBuffer = glMapBuffer;
exts->UnmapBuffer = glUnmapBuffer;
#elif GR_IOS_BUILD
exts->GenFramebuffers = glGenFramebuffers;
exts->BindFramebuffer = glBindFramebuffer;
exts->FramebufferTexture2D = glFramebufferTexture2D;
exts->CheckFramebufferStatus = glCheckFramebufferStatus;
exts->DeleteFramebuffers = glDeleteFramebuffers;
exts->RenderbufferStorage = glRenderbufferStorage;
exts->GenRenderbuffers = glGenRenderbuffers;
exts->DeleteRenderbuffers = glDeleteRenderbuffers;
exts->FramebufferRenderbuffer = glFramebufferRenderbuffer;
exts->BindRenderbuffer = glBindRenderbuffer;
exts->RenderbufferStorageMultisample = glRenderbufferStorageMultisampleAPPLE;
exts->ResolveMultisampleFramebuffer = glResolveMultisampleFramebufferAPPLE;
exts->MapBuffer = glMapBufferOES;
exts->UnmapBuffer = glUnmapBufferOES;
#else
GLint major, minor;
gl_version(&major, &minor);
#if GR_GL_DESKTOP
if (major >= 3) {// FBO, FBOMS, and FBOBLIT part of 3.0
exts->GenFramebuffers = glGenFramebuffers;
exts->BindFramebuffer = glBindFramebuffer;
exts->FramebufferTexture2D = glFramebufferTexture2D;
exts->CheckFramebufferStatus = glCheckFramebufferStatus;
exts->DeleteFramebuffers = glDeleteFramebuffers;
exts->RenderbufferStorage = glRenderbufferStorage;
exts->GenRenderbuffers = glGenRenderbuffers;
exts->DeleteRenderbuffers = glDeleteRenderbuffers;
exts->FramebufferRenderbuffer = glFramebufferRenderbuffer;
exts->BindRenderbuffer = glBindRenderbuffer;
exts->RenderbufferStorageMultisample = glRenderbufferStorageMultisample;
exts->BlitFramebuffer = glBlitFramebuffer;
} else if (has_gl_extension("GL_ARB_framebuffer_object")) {
GET_PROC(exts, GenFramebuffers, ARB);
GET_PROC(exts, BindFramebuffer, ARB);
GET_PROC(exts, FramebufferTexture2D, ARB);
GET_PROC(exts, CheckFramebufferStatus, ARB);
GET_PROC(exts, DeleteFramebuffers, ARB);
GET_PROC(exts, RenderbufferStorage, ARB);
GET_PROC(exts, GenRenderbuffers, ARB);
GET_PROC(exts, DeleteRenderbuffers, ARB);
GET_PROC(exts, FramebufferRenderbuffer, ARB);
GET_PROC(exts, BindRenderbuffer, ARB);
GET_PROC(exts, RenderbufferStorageMultisample, ARB);
GET_PROC(exts, BlitFramebuffer, ARB);
} else {
// we require some form of FBO
GrAssert(has_gl_extension("GL_EXT_framebuffer_object"));
GET_PROC(exts, GenFramebuffers, EXT);
GET_PROC(exts, BindFramebuffer, EXT);
GET_PROC(exts, FramebufferTexture2D, EXT);
GET_PROC(exts, CheckFramebufferStatus, EXT);
GET_PROC(exts, DeleteFramebuffers, EXT);
GET_PROC(exts, RenderbufferStorage, EXT);
GET_PROC(exts, GenRenderbuffers, EXT);
GET_PROC(exts, DeleteRenderbuffers, EXT);
GET_PROC(exts, FramebufferRenderbuffer, EXT);
GET_PROC(exts, BindRenderbuffer, EXT);
if (has_gl_extension("GL_EXT_framebuffer_multisample")) {
GET_PROC(exts, RenderbufferStorageMultisample, EXT);
}
if (has_gl_extension("GL_EXT_framebuffer_blit")) {
GET_PROC(exts, BlitFramebuffer, EXT);
}
}
// we assume we have at least GL 1.5 or higher (VBOs introduced in 1.5)
exts->MapBuffer = glMapBuffer;
exts->UnmapBuffer = glUnmapBuffer;
#else // !GR_GL_DESKTOP
if (major >= 2) {// ES 2.0 supports FBO
exts->GenFramebuffers = glGenFramebuffers;
exts->BindFramebuffer = glBindFramebuffer;
exts->FramebufferTexture2D = glFramebufferTexture2D;
exts->CheckFramebufferStatus = glCheckFramebufferStatus;
exts->DeleteFramebuffers = glDeleteFramebuffers;
exts->RenderbufferStorage = glRenderbufferStorage;
exts->GenRenderbuffers = glGenRenderbuffers;
exts->DeleteRenderbuffers = glDeleteRenderbuffers;
exts->FramebufferRenderbuffer = glFramebufferRenderbuffer;
exts->BindRenderbuffer = glBindRenderbuffer;
} else {
// we require some form of FBO
GrAssert(has_gl_extension("GL_OES_framebuffer_object"));
GET_PROC(exts, GenFramebuffers, OES);
GET_PROC(exts, BindFramebuffer, OES);
GET_PROC(exts, FramebufferTexture2D, OES);
GET_PROC(exts, CheckFramebufferStatus, OES);
GET_PROC(exts, DeleteFramebuffers, OES);
GET_PROC(exts, RenderbufferStorage, OES);
GET_PROC(exts, GenRenderbuffers, OES);
GET_PROC(exts, DeleteRenderbuffers, OES);
GET_PROC(exts, FramebufferRenderbuffer, OES);
GET_PROC(exts, BindRenderbuffer, OES);
}
if (has_gl_extension("GL_APPLE_framebuffer_multisample")) {
GET_PROC(exts, ResolveMultisampleFramebuffer, APPLE);
}
if (has_gl_extension("GL_IMG_multisampled_render_to_texture")) {
GET_PROC(exts, FramebufferTexture2DMultisample, IMG);
}
if (has_gl_extension("GL_OES_mapbuffer")) {
GET_PROC(exts, MapBuffer, OES);
GET_PROC(exts, UnmapBuffer, OES);
}
#endif // !GR_GL_DESKTOP
#endif // BUILD
}
bool gPrintGL = true;
|