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
|
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtTest/QtTest>
#include <qcoreapplication.h>
#include <qdebug.h>
#include <qgl.h>
#include <qglpixelbuffer.h>
#include <qglframebufferobject.h>
#include <qglcolormap.h>
#include <qpaintengine.h>
#include <QGraphicsView>
#include <QGraphicsProxyWidget>
#include <QVBoxLayout>
#ifdef QT_BUILD_INTERNAL
#include <QtOpenGL/private/qgl_p.h>
#include <QtGui/private/qpixmapdata_p.h>
#include <QtGui/private/qimage_p.h>
#include <QtGui/private/qimagepixmapcleanuphooks_p.h>
#endif
//TESTED_CLASS=
//TESTED_FILES=
class tst_QGL : public QObject
{
Q_OBJECT
public:
tst_QGL();
virtual ~tst_QGL();
private slots:
void getSetCheck();
void openGLVersionCheck();
void graphicsViewClipping();
void partialGLWidgetUpdates_data();
void partialGLWidgetUpdates();
void glWidgetWithAlpha();
void glWidgetRendering();
void glFBOSimpleRendering();
void glFBORendering();
void multipleFBOInterleavedRendering();
void glFBOUseInGLWidget();
void glPBufferRendering();
void glWidgetReparent();
void glWidgetRenderPixmap();
void colormap();
void fboFormat();
void testDontCrashOnDanglingResources();
void replaceClipping();
void clipTest();
void destroyFBOAfterContext();
void shareRegister();
void qglContextDefaultBindTexture();
void textureCleanup();
};
tst_QGL::tst_QGL()
{
}
tst_QGL::~tst_QGL()
{
}
class MyGLContext : public QGLContext
{
public:
MyGLContext(const QGLFormat& format) : QGLContext(format) {}
bool windowCreated() const { return QGLContext::windowCreated(); }
void setWindowCreated(bool on) { QGLContext::setWindowCreated(on); }
bool initialized() const { return QGLContext::initialized(); }
void setInitialized(bool on) { QGLContext::setInitialized(on); }
};
class MyGLWidget : public QGLWidget
{
public:
MyGLWidget() : QGLWidget() {}
bool autoBufferSwap() const { return QGLWidget::autoBufferSwap(); }
void setAutoBufferSwap(bool on) { QGLWidget::setAutoBufferSwap(on); }
};
static int appDefaultDepth()
{
static int depth = 0;
if (depth == 0) {
QPixmap pm(1, 1);
depth = pm.depth();
}
return depth;
}
// Using INT_MIN and INT_MAX will cause failures on systems
// where "int" is 64-bit, so use the explicit values instead.
#define TEST_INT_MIN (-2147483647 - 1)
#define TEST_INT_MAX 2147483647
// Testing get/set functions
void tst_QGL::getSetCheck()
{
if (!QGLFormat::hasOpenGL())
QSKIP("QGL not supported on this platform", SkipAll);
QGLFormat obj1;
// int QGLFormat::depthBufferSize()
// void QGLFormat::setDepthBufferSize(int)
QCOMPARE(-1, obj1.depthBufferSize());
obj1.setDepthBufferSize(0);
QCOMPARE(0, obj1.depthBufferSize());
QTest::ignoreMessage(QtWarningMsg, "QGLFormat::setDepthBufferSize: Cannot set negative depth buffer size -2147483648");
obj1.setDepthBufferSize(TEST_INT_MIN);
QCOMPARE(0, obj1.depthBufferSize()); // Makes no sense with a negative buffer size
obj1.setDepthBufferSize(3);
QTest::ignoreMessage(QtWarningMsg, "QGLFormat::setDepthBufferSize: Cannot set negative depth buffer size -1");
obj1.setDepthBufferSize(-1);
QCOMPARE(3, obj1.depthBufferSize());
obj1.setDepthBufferSize(TEST_INT_MAX);
QCOMPARE(TEST_INT_MAX, obj1.depthBufferSize());
// int QGLFormat::accumBufferSize()
// void QGLFormat::setAccumBufferSize(int)
QCOMPARE(-1, obj1.accumBufferSize());
obj1.setAccumBufferSize(0);
QCOMPARE(0, obj1.accumBufferSize());
QTest::ignoreMessage(QtWarningMsg, "QGLFormat::setAccumBufferSize: Cannot set negative accumulate buffer size -2147483648");
obj1.setAccumBufferSize(TEST_INT_MIN);
QCOMPARE(0, obj1.accumBufferSize()); // Makes no sense with a negative buffer size
obj1.setAccumBufferSize(3);
QTest::ignoreMessage(QtWarningMsg, "QGLFormat::setAccumBufferSize: Cannot set negative accumulate buffer size -1");
obj1.setAccumBufferSize(-1);
QCOMPARE(3, obj1.accumBufferSize());
obj1.setAccumBufferSize(TEST_INT_MAX);
QCOMPARE(TEST_INT_MAX, obj1.accumBufferSize());
// int QGLFormat::redBufferSize()
// void QGLFormat::setRedBufferSize(int)
QCOMPARE(-1, obj1.redBufferSize());
obj1.setRedBufferSize(0);
QCOMPARE(0, obj1.redBufferSize());
QTest::ignoreMessage(QtWarningMsg, "QGLFormat::setRedBufferSize: Cannot set negative red buffer size -2147483648");
obj1.setRedBufferSize(TEST_INT_MIN);
QCOMPARE(0, obj1.redBufferSize()); // Makes no sense with a negative buffer size
obj1.setRedBufferSize(3);
QTest::ignoreMessage(QtWarningMsg, "QGLFormat::setRedBufferSize: Cannot set negative red buffer size -1");
obj1.setRedBufferSize(-1);
QCOMPARE(3, obj1.redBufferSize());
obj1.setRedBufferSize(TEST_INT_MAX);
QCOMPARE(TEST_INT_MAX, obj1.redBufferSize());
// int QGLFormat::greenBufferSize()
// void QGLFormat::setGreenBufferSize(int)
QCOMPARE(-1, obj1.greenBufferSize());
obj1.setGreenBufferSize(0);
QCOMPARE(0, obj1.greenBufferSize());
QTest::ignoreMessage(QtWarningMsg, "QGLFormat::setGreenBufferSize: Cannot set negative green buffer size -2147483648");
obj1.setGreenBufferSize(TEST_INT_MIN);
QCOMPARE(0, obj1.greenBufferSize()); // Makes no sense with a negative buffer size
obj1.setGreenBufferSize(3);
QTest::ignoreMessage(QtWarningMsg, "QGLFormat::setGreenBufferSize: Cannot set negative green buffer size -1");
obj1.setGreenBufferSize(-1);
QCOMPARE(3, obj1.greenBufferSize());
obj1.setGreenBufferSize(TEST_INT_MAX);
QCOMPARE(TEST_INT_MAX, obj1.greenBufferSize());
// int QGLFormat::blueBufferSize()
// void QGLFormat::setBlueBufferSize(int)
QCOMPARE(-1, obj1.blueBufferSize());
obj1.setBlueBufferSize(0);
QCOMPARE(0, obj1.blueBufferSize());
QTest::ignoreMessage(QtWarningMsg, "QGLFormat::setBlueBufferSize: Cannot set negative blue buffer size -2147483648");
obj1.setBlueBufferSize(TEST_INT_MIN);
QCOMPARE(0, obj1.blueBufferSize()); // Makes no sense with a negative buffer size
obj1.setBlueBufferSize(3);
QTest::ignoreMessage(QtWarningMsg, "QGLFormat::setBlueBufferSize: Cannot set negative blue buffer size -1");
obj1.setBlueBufferSize(-1);
QCOMPARE(3, obj1.blueBufferSize());
obj1.setBlueBufferSize(TEST_INT_MAX);
QCOMPARE(TEST_INT_MAX, obj1.blueBufferSize());
// int QGLFormat::alphaBufferSize()
// void QGLFormat::setAlphaBufferSize(int)
QCOMPARE(-1, obj1.alphaBufferSize());
QCOMPARE(false, obj1.alpha());
QVERIFY(!obj1.testOption(QGL::AlphaChannel));
QVERIFY(obj1.testOption(QGL::NoAlphaChannel));
obj1.setAlphaBufferSize(1);
QCOMPARE(true, obj1.alpha()); // setAlphaBufferSize() enables alpha.
QCOMPARE(1, obj1.alphaBufferSize());
QTest::ignoreMessage(QtWarningMsg, "QGLFormat::setAlphaBufferSize: Cannot set negative alpha buffer size -2147483648");
obj1.setAlphaBufferSize(TEST_INT_MIN);
QCOMPARE(1, obj1.alphaBufferSize()); // Makes no sense with a negative buffer size
obj1.setAlphaBufferSize(3);
QTest::ignoreMessage(QtWarningMsg, "QGLFormat::setAlphaBufferSize: Cannot set negative alpha buffer size -1");
obj1.setAlphaBufferSize(-1);
QCOMPARE(3, obj1.alphaBufferSize());
obj1.setAlphaBufferSize(TEST_INT_MAX);
QCOMPARE(TEST_INT_MAX, obj1.alphaBufferSize());
// int QGLFormat::stencilBufferSize()
// void QGLFormat::setStencilBufferSize(int)
QCOMPARE(-1, obj1.stencilBufferSize());
obj1.setStencilBufferSize(1);
QCOMPARE(1, obj1.stencilBufferSize());
QTest::ignoreMessage(QtWarningMsg, "QGLFormat::setStencilBufferSize: Cannot set negative stencil buffer size -2147483648");
obj1.setStencilBufferSize(TEST_INT_MIN);
QCOMPARE(1, obj1.stencilBufferSize()); // Makes no sense with a negative buffer size
obj1.setStencilBufferSize(3);
QTest::ignoreMessage(QtWarningMsg, "QGLFormat::setStencilBufferSize: Cannot set negative stencil buffer size -1");
obj1.setStencilBufferSize(-1);
QCOMPARE(3, obj1.stencilBufferSize());
obj1.setStencilBufferSize(TEST_INT_MAX);
QCOMPARE(TEST_INT_MAX, obj1.stencilBufferSize());
// bool QGLFormat::sampleBuffers()
// void QGLFormat::setSampleBuffers(bool)
QCOMPARE(false, obj1.sampleBuffers());
QVERIFY(!obj1.testOption(QGL::SampleBuffers));
QVERIFY(obj1.testOption(QGL::NoSampleBuffers));
obj1.setSampleBuffers(false);
QCOMPARE(false, obj1.sampleBuffers());
QVERIFY(obj1.testOption(QGL::NoSampleBuffers));
obj1.setSampleBuffers(true);
QCOMPARE(true, obj1.sampleBuffers());
QVERIFY(obj1.testOption(QGL::SampleBuffers));
// int QGLFormat::samples()
// void QGLFormat::setSamples(int)
QCOMPARE(-1, obj1.samples());
obj1.setSamples(0);
QCOMPARE(0, obj1.samples());
QTest::ignoreMessage(QtWarningMsg, "QGLFormat::setSamples: Cannot have negative number of samples per pixel -2147483648");
obj1.setSamples(TEST_INT_MIN);
QCOMPARE(0, obj1.samples()); // Makes no sense with a negative sample size
obj1.setSamples(3);
QTest::ignoreMessage(QtWarningMsg, "QGLFormat::setSamples: Cannot have negative number of samples per pixel -1");
obj1.setSamples(-1);
QCOMPARE(3, obj1.samples());
obj1.setSamples(TEST_INT_MAX);
QCOMPARE(TEST_INT_MAX, obj1.samples());
// int QGLFormat::swapInterval()
// void QGLFormat::setSwapInterval(int)
QCOMPARE(-1, obj1.swapInterval());
obj1.setSwapInterval(0);
QCOMPARE(0, obj1.swapInterval());
obj1.setSwapInterval(TEST_INT_MIN);
QCOMPARE(TEST_INT_MIN, obj1.swapInterval());
obj1.setSwapInterval(-1);
QCOMPARE(-1, obj1.swapInterval());
obj1.setSwapInterval(TEST_INT_MAX);
QCOMPARE(TEST_INT_MAX, obj1.swapInterval());
// bool QGLFormat::doubleBuffer()
// void QGLFormat::setDoubleBuffer(bool)
QCOMPARE(true, obj1.doubleBuffer());
QVERIFY(obj1.testOption(QGL::DoubleBuffer));
QVERIFY(!obj1.testOption(QGL::SingleBuffer));
obj1.setDoubleBuffer(false);
QCOMPARE(false, obj1.doubleBuffer());
QVERIFY(!obj1.testOption(QGL::DoubleBuffer));
QVERIFY(obj1.testOption(QGL::SingleBuffer));
obj1.setDoubleBuffer(true);
QCOMPARE(true, obj1.doubleBuffer());
QVERIFY(obj1.testOption(QGL::DoubleBuffer));
QVERIFY(!obj1.testOption(QGL::SingleBuffer));
// bool QGLFormat::depth()
// void QGLFormat::setDepth(bool)
QCOMPARE(true, obj1.depth());
QVERIFY(obj1.testOption(QGL::DepthBuffer));
QVERIFY(!obj1.testOption(QGL::NoDepthBuffer));
obj1.setDepth(false);
QCOMPARE(false, obj1.depth());
QVERIFY(!obj1.testOption(QGL::DepthBuffer));
QVERIFY(obj1.testOption(QGL::NoDepthBuffer));
obj1.setDepth(true);
QCOMPARE(true, obj1.depth());
QVERIFY(obj1.testOption(QGL::DepthBuffer));
QVERIFY(!obj1.testOption(QGL::NoDepthBuffer));
// bool QGLFormat::rgba()
// void QGLFormat::setRgba(bool)
QCOMPARE(true, obj1.rgba());
QVERIFY(obj1.testOption(QGL::Rgba));
QVERIFY(!obj1.testOption(QGL::ColorIndex));
obj1.setRgba(false);
QCOMPARE(false, obj1.rgba());
QVERIFY(!obj1.testOption(QGL::Rgba));
QVERIFY(obj1.testOption(QGL::ColorIndex));
obj1.setRgba(true);
QCOMPARE(true, obj1.rgba());
QVERIFY(obj1.testOption(QGL::Rgba));
QVERIFY(!obj1.testOption(QGL::ColorIndex));
// bool QGLFormat::alpha()
// void QGLFormat::setAlpha(bool)
QVERIFY(obj1.testOption(QGL::AlphaChannel));
QVERIFY(!obj1.testOption(QGL::NoAlphaChannel));
obj1.setAlpha(false);
QCOMPARE(false, obj1.alpha());
QVERIFY(!obj1.testOption(QGL::AlphaChannel));
QVERIFY(obj1.testOption(QGL::NoAlphaChannel));
obj1.setAlpha(true);
QCOMPARE(true, obj1.alpha());
QVERIFY(obj1.testOption(QGL::AlphaChannel));
QVERIFY(!obj1.testOption(QGL::NoAlphaChannel));
// bool QGLFormat::accum()
// void QGLFormat::setAccum(bool)
obj1.setAccumBufferSize(0);
QCOMPARE(false, obj1.accum());
QVERIFY(!obj1.testOption(QGL::AccumBuffer));
QVERIFY(obj1.testOption(QGL::NoAccumBuffer));
obj1.setAccum(false);
QCOMPARE(false, obj1.accum());
QVERIFY(!obj1.testOption(QGL::AccumBuffer));
QVERIFY(obj1.testOption(QGL::NoAccumBuffer));
obj1.setAccum(true);
QCOMPARE(true, obj1.accum());
QVERIFY(obj1.testOption(QGL::AccumBuffer));
QVERIFY(!obj1.testOption(QGL::NoAccumBuffer));
// bool QGLFormat::stencil()
// void QGLFormat::setStencil(bool)
QCOMPARE(true, obj1.stencil());
QVERIFY(obj1.testOption(QGL::StencilBuffer));
QVERIFY(!obj1.testOption(QGL::NoStencilBuffer));
obj1.setStencil(false);
QCOMPARE(false, obj1.stencil());
QVERIFY(!obj1.testOption(QGL::StencilBuffer));
QVERIFY(obj1.testOption(QGL::NoStencilBuffer));
obj1.setStencil(true);
QCOMPARE(true, obj1.stencil());
QVERIFY(obj1.testOption(QGL::StencilBuffer));
QVERIFY(!obj1.testOption(QGL::NoStencilBuffer));
// bool QGLFormat::stereo()
// void QGLFormat::setStereo(bool)
QCOMPARE(false, obj1.stereo());
QVERIFY(!obj1.testOption(QGL::StereoBuffers));
QVERIFY(obj1.testOption(QGL::NoStereoBuffers));
obj1.setStereo(false);
QCOMPARE(false, obj1.stereo());
QVERIFY(!obj1.testOption(QGL::StereoBuffers));
QVERIFY(obj1.testOption(QGL::NoStereoBuffers));
obj1.setStereo(true);
QCOMPARE(true, obj1.stereo());
QVERIFY(obj1.testOption(QGL::StereoBuffers));
QVERIFY(!obj1.testOption(QGL::NoStereoBuffers));
// bool QGLFormat::directRendering()
// void QGLFormat::setDirectRendering(bool)
QCOMPARE(true, obj1.directRendering());
QVERIFY(obj1.testOption(QGL::DirectRendering));
QVERIFY(!obj1.testOption(QGL::IndirectRendering));
obj1.setDirectRendering(false);
QCOMPARE(false, obj1.directRendering());
QVERIFY(!obj1.testOption(QGL::DirectRendering));
QVERIFY(obj1.testOption(QGL::IndirectRendering));
obj1.setDirectRendering(true);
QCOMPARE(true, obj1.directRendering());
QVERIFY(obj1.testOption(QGL::DirectRendering));
QVERIFY(!obj1.testOption(QGL::IndirectRendering));
// bool QGLFormat::overlay()
// void QGLFormat::setOverlay(bool)
QCOMPARE(false, obj1.hasOverlay());
QVERIFY(!obj1.testOption(QGL::HasOverlay));
QVERIFY(obj1.testOption(QGL::NoOverlay));
obj1.setOverlay(false);
QCOMPARE(false, obj1.hasOverlay());
QVERIFY(!obj1.testOption(QGL::HasOverlay));
QVERIFY(obj1.testOption(QGL::NoOverlay));
obj1.setOverlay(true);
QCOMPARE(true, obj1.hasOverlay());
QVERIFY(obj1.testOption(QGL::HasOverlay));
QVERIFY(!obj1.testOption(QGL::NoOverlay));
// int QGLFormat::plane()
// void QGLFormat::setPlane(int)
QCOMPARE(0, obj1.plane());
obj1.setPlane(0);
QCOMPARE(0, obj1.plane());
obj1.setPlane(TEST_INT_MIN);
QCOMPARE(TEST_INT_MIN, obj1.plane());
obj1.setPlane(TEST_INT_MAX);
QCOMPARE(TEST_INT_MAX, obj1.plane());
// int QGLFormat::major/minorVersion()
// void QGLFormat::setVersion(int, int)
QCOMPARE(obj1.majorVersion(), 1);
QCOMPARE(obj1.minorVersion(), 0);
obj1.setVersion(3, 2);
QCOMPARE(obj1.majorVersion(), 3);
QCOMPARE(obj1.minorVersion(), 2);
QTest::ignoreMessage(QtWarningMsg, "QGLFormat::setVersion: Cannot set zero or negative version number 0.1");
obj1.setVersion(0, 1);
QCOMPARE(obj1.majorVersion(), 3);
QCOMPARE(obj1.minorVersion(), 2);
QTest::ignoreMessage(QtWarningMsg, "QGLFormat::setVersion: Cannot set zero or negative version number 3.-1");
obj1.setVersion(3, -1);
QCOMPARE(obj1.majorVersion(), 3);
QCOMPARE(obj1.minorVersion(), 2);
obj1.setVersion(TEST_INT_MAX, TEST_INT_MAX - 1);
QCOMPARE(obj1.majorVersion(), TEST_INT_MAX);
QCOMPARE(obj1.minorVersion(), TEST_INT_MAX - 1);
// operator== and operator!= for QGLFormat
QGLFormat format1;
QGLFormat format2;
QVERIFY(format1 == format2);
QVERIFY(!(format1 != format2));
format1.setDoubleBuffer(false);
QVERIFY(!(format1 == format2));
QVERIFY(format1 != format2);
format2.setDoubleBuffer(false);
QVERIFY(format1 == format2);
QVERIFY(!(format1 != format2));
format1.setDepthBufferSize(8);
QVERIFY(!(format1 == format2));
QVERIFY(format1 != format2);
format2.setDepthBufferSize(8);
QVERIFY(format1 == format2);
QVERIFY(!(format1 != format2));
format1.setAccumBufferSize(8);
QVERIFY(!(format1 == format2));
QVERIFY(format1 != format2);
format2.setAccumBufferSize(8);
QVERIFY(format1 == format2);
QVERIFY(!(format1 != format2));
format1.setRedBufferSize(8);
QVERIFY(!(format1 == format2));
QVERIFY(format1 != format2);
format2.setRedBufferSize(8);
QVERIFY(format1 == format2);
QVERIFY(!(format1 != format2));
format1.setGreenBufferSize(8);
QVERIFY(!(format1 == format2));
QVERIFY(format1 != format2);
format2.setGreenBufferSize(8);
QVERIFY(format1 == format2);
QVERIFY(!(format1 != format2));
format1.setBlueBufferSize(8);
QVERIFY(!(format1 == format2));
QVERIFY(format1 != format2);
format2.setBlueBufferSize(8);
QVERIFY(format1 == format2);
QVERIFY(!(format1 != format2));
format1.setAlphaBufferSize(8);
QVERIFY(!(format1 == format2));
QVERIFY(format1 != format2);
format2.setAlphaBufferSize(8);
QVERIFY(format1 == format2);
QVERIFY(!(format1 != format2));
format1.setStencilBufferSize(8);
QVERIFY(!(format1 == format2));
QVERIFY(format1 != format2);
format2.setStencilBufferSize(8);
QVERIFY(format1 == format2);
QVERIFY(!(format1 != format2));
format1.setSamples(8);
QVERIFY(!(format1 == format2));
QVERIFY(format1 != format2);
format2.setSamples(8);
QVERIFY(format1 == format2);
QVERIFY(!(format1 != format2));
format1.setSwapInterval(8);
QVERIFY(!(format1 == format2));
QVERIFY(format1 != format2);
format2.setSwapInterval(8);
QVERIFY(format1 == format2);
QVERIFY(!(format1 != format2));
format1.setPlane(8);
QVERIFY(!(format1 == format2));
QVERIFY(format1 != format2);
format2.setPlane(8);
QVERIFY(format1 == format2);
QVERIFY(!(format1 != format2));
format1.setVersion(3, 2);
QVERIFY(!(format1 == format2));
QVERIFY(format1 != format2);
format2.setVersion(3, 2);
QVERIFY(format1 == format2);
QVERIFY(!(format1 != format2));
format1.setProfile(QGLFormat::CoreProfile);
QVERIFY(!(format1 == format2));
QVERIFY(format1 != format2);
format2.setProfile(QGLFormat::CoreProfile);
QVERIFY(format1 == format2);
QVERIFY(!(format1 != format2));
format1.setOption(QGL::NoDeprecatedFunctions);
QVERIFY(!(format1 == format2));
QVERIFY(format1 != format2);
format2.setOption(QGL::NoDeprecatedFunctions);
QVERIFY(format1 == format2);
QVERIFY(!(format1 != format2));
// Copy constructor and assignment for QGLFormat.
QGLFormat format3(format1);
QGLFormat format4;
QVERIFY(format1 == format3);
QVERIFY(format1 != format4);
format4 = format1;
QVERIFY(format1 == format4);
// Check that modifying a copy doesn't affect the original.
format3.setRedBufferSize(16);
format4.setPlane(16);
QCOMPARE(format1.redBufferSize(), 8);
QCOMPARE(format1.plane(), 8);
// Check the QGLFormat constructor that takes an option list.
QGLFormat format5
(QGL::DepthBuffer | QGL::StereoBuffers | QGL::ColorIndex, 3);
QVERIFY(format5.depth());
QVERIFY(format5.stereo());
QVERIFY(format5.doubleBuffer()); // From defaultFormat()
QVERIFY(!format5.hasOverlay()); // From defaultFormat()
QVERIFY(!format5.rgba());
QCOMPARE(format5.plane(), 3);
// The default format should be the same as QGLFormat().
QVERIFY(QGLFormat::defaultFormat() == QGLFormat());
// Modify the default format and check that it was changed.
QGLFormat::setDefaultFormat(format1);
QVERIFY(QGLFormat::defaultFormat() == format1);
// Restore the default format.
QGLFormat::setDefaultFormat(QGLFormat());
QVERIFY(QGLFormat::defaultFormat() == QGLFormat());
// Check the default overlay format's expected values.
QGLFormat overlay(QGLFormat::defaultOverlayFormat());
QCOMPARE(overlay.depthBufferSize(), -1);
QCOMPARE(overlay.accumBufferSize(), -1);
QCOMPARE(overlay.redBufferSize(), -1);
QCOMPARE(overlay.greenBufferSize(), -1);
QCOMPARE(overlay.blueBufferSize(), -1);
QCOMPARE(overlay.alphaBufferSize(), -1);
QCOMPARE(overlay.samples(), -1);
QCOMPARE(overlay.swapInterval(), -1);
QCOMPARE(overlay.plane(), 1);
QVERIFY(!overlay.sampleBuffers());
QVERIFY(!overlay.doubleBuffer());
QVERIFY(!overlay.depth());
QVERIFY(!overlay.rgba());
QVERIFY(!overlay.alpha());
QVERIFY(!overlay.accum());
QVERIFY(!overlay.stencil());
QVERIFY(!overlay.stereo());
QVERIFY(overlay.directRendering()); // Only option that should be on.
QVERIFY(!overlay.hasOverlay()); // Overlay doesn't need an overlay!
// Modify the default overlay format and check that it was changed.
QGLFormat::setDefaultOverlayFormat(format1);
QVERIFY(QGLFormat::defaultOverlayFormat() == format1);
// Restore the default overlay format.
QGLFormat::setDefaultOverlayFormat(overlay);
QVERIFY(QGLFormat::defaultOverlayFormat() == overlay);
MyGLContext obj2(obj1);
// bool QGLContext::windowCreated()
// void QGLContext::setWindowCreated(bool)
obj2.setWindowCreated(false);
QCOMPARE(false, obj2.windowCreated());
obj2.setWindowCreated(true);
QCOMPARE(true, obj2.windowCreated());
// bool QGLContext::initialized()
// void QGLContext::setInitialized(bool)
obj2.setInitialized(false);
QCOMPARE(false, obj2.initialized());
obj2.setInitialized(true);
QCOMPARE(true, obj2.initialized());
MyGLWidget obj3;
// bool QGLWidget::autoBufferSwap()
// void QGLWidget::setAutoBufferSwap(bool)
obj3.setAutoBufferSwap(false);
QCOMPARE(false, obj3.autoBufferSwap());
obj3.setAutoBufferSwap(true);
QCOMPARE(true, obj3.autoBufferSwap());
}
#ifdef QT_BUILD_INTERNAL
QT_BEGIN_NAMESPACE
extern QGLFormat::OpenGLVersionFlags qOpenGLVersionFlagsFromString(const QString &versionString);
QT_END_NAMESPACE
#endif
void tst_QGL::openGLVersionCheck()
{
#ifdef QT_BUILD_INTERNAL
if (!QGLFormat::hasOpenGL())
QSKIP("QGL not supported on this platform", SkipAll);
QString versionString;
QGLFormat::OpenGLVersionFlags expectedFlag;
QGLFormat::OpenGLVersionFlags versionFlag;
versionString = "1.1 Irix 6.5";
expectedFlag = QGLFormat::OpenGL_Version_1_1;
versionFlag = qOpenGLVersionFlagsFromString(versionString);
QCOMPARE(versionFlag, expectedFlag);
versionString = "1.2 Microsoft";
expectedFlag = QGLFormat::OpenGL_Version_1_2 | QGLFormat::OpenGL_Version_1_1;
versionFlag = qOpenGLVersionFlagsFromString(versionString);
QCOMPARE(versionFlag, expectedFlag);
versionString = "1.2.1";
expectedFlag = QGLFormat::OpenGL_Version_1_2 | QGLFormat::OpenGL_Version_1_1;
versionFlag = qOpenGLVersionFlagsFromString(versionString);
QCOMPARE(versionFlag, expectedFlag);
versionString = "1.3 NVIDIA";
expectedFlag = QGLFormat::OpenGL_Version_1_3 | QGLFormat::OpenGL_Version_1_2 | QGLFormat::OpenGL_Version_1_1;
versionFlag = qOpenGLVersionFlagsFromString(versionString);
QCOMPARE(versionFlag, expectedFlag);
versionString = "1.4";
expectedFlag = QGLFormat::OpenGL_Version_1_4 | QGLFormat::OpenGL_Version_1_3 | QGLFormat::OpenGL_Version_1_2 | QGLFormat::OpenGL_Version_1_1;
versionFlag = qOpenGLVersionFlagsFromString(versionString);
QCOMPARE(versionFlag, expectedFlag);
versionString = "1.5 NVIDIA";
expectedFlag = QGLFormat::OpenGL_Version_1_5 | QGLFormat::OpenGL_Version_1_4 | QGLFormat::OpenGL_Version_1_3 | QGLFormat::OpenGL_Version_1_2 | QGLFormat::OpenGL_Version_1_1;
versionFlag = qOpenGLVersionFlagsFromString(versionString);
QCOMPARE(versionFlag, expectedFlag);
versionString = "2.0.2 NVIDIA 87.62";
expectedFlag = QGLFormat::OpenGL_Version_2_0 | QGLFormat::OpenGL_Version_1_5 | QGLFormat::OpenGL_Version_1_4 | QGLFormat::OpenGL_Version_1_3 | QGLFormat::OpenGL_Version_1_2 | QGLFormat::OpenGL_Version_1_1;
versionFlag = qOpenGLVersionFlagsFromString(versionString);
QCOMPARE(versionFlag, expectedFlag);
versionString = "2.1 NVIDIA";
expectedFlag = QGLFormat::OpenGL_Version_2_1 | QGLFormat::OpenGL_Version_2_0 | QGLFormat::OpenGL_Version_1_5 | QGLFormat::OpenGL_Version_1_4 | QGLFormat::OpenGL_Version_1_3 | QGLFormat::OpenGL_Version_1_2 | QGLFormat::OpenGL_Version_1_1;
versionFlag = qOpenGLVersionFlagsFromString(versionString);
QCOMPARE(versionFlag, expectedFlag);
versionString = "2.1";
expectedFlag = QGLFormat::OpenGL_Version_2_1 | QGLFormat::OpenGL_Version_2_0 | QGLFormat::OpenGL_Version_1_5 | QGLFormat::OpenGL_Version_1_4 | QGLFormat::OpenGL_Version_1_3 | QGLFormat::OpenGL_Version_1_2 | QGLFormat::OpenGL_Version_1_1;
versionFlag = qOpenGLVersionFlagsFromString(versionString);
QCOMPARE(versionFlag, expectedFlag);
versionString = "OpenGL ES-CM 1.0 ATI";
expectedFlag = QGLFormat::OpenGL_ES_Common_Version_1_0 | QGLFormat::OpenGL_ES_CommonLite_Version_1_0;
versionFlag = qOpenGLVersionFlagsFromString(versionString);
QCOMPARE(versionFlag, expectedFlag);
versionString = "OpenGL ES-CL 1.0 ATI";
expectedFlag = QGLFormat::OpenGL_ES_CommonLite_Version_1_0;
versionFlag = qOpenGLVersionFlagsFromString(versionString);
QCOMPARE(versionFlag, expectedFlag);
versionString = "OpenGL ES-CM 1.1 ATI";
expectedFlag = QGLFormat::OpenGL_ES_Common_Version_1_1 | QGLFormat::OpenGL_ES_CommonLite_Version_1_1 | QGLFormat::OpenGL_ES_Common_Version_1_0 | QGLFormat::OpenGL_ES_CommonLite_Version_1_0;
versionFlag = qOpenGLVersionFlagsFromString(versionString);
QCOMPARE(versionFlag, expectedFlag);
versionString = "OpenGL ES-CL 1.1 ATI";
expectedFlag = QGLFormat::OpenGL_ES_CommonLite_Version_1_1 | QGLFormat::OpenGL_ES_CommonLite_Version_1_0;
versionFlag = qOpenGLVersionFlagsFromString(versionString);
QCOMPARE(versionFlag, expectedFlag);
versionString = "OpenGL ES 2.0 ATI";
expectedFlag = QGLFormat::OpenGL_ES_Version_2_0;
versionFlag = qOpenGLVersionFlagsFromString(versionString);
QCOMPARE(versionFlag, expectedFlag);
versionString = "3.0";
expectedFlag = QGLFormat::OpenGL_Version_3_0 | QGLFormat::OpenGL_Version_2_1 | QGLFormat::OpenGL_Version_2_0 | QGLFormat::OpenGL_Version_1_5 | QGLFormat::OpenGL_Version_1_4 | QGLFormat::OpenGL_Version_1_3 | QGLFormat::OpenGL_Version_1_2 | QGLFormat::OpenGL_Version_1_1;
versionFlag = qOpenGLVersionFlagsFromString(versionString);
QCOMPARE(versionFlag, expectedFlag);
QGLWidget glWidget;
glWidget.show();
glWidget.makeCurrent();
// This is unfortunately the only test we can make on the actual openGLVersionFlags()
// However, the complicated parts are in openGLVersionFlags(const QString &versionString)
// tested above
#if defined(QT_OPENGL_ES_1)
QVERIFY(QGLFormat::openGLVersionFlags() & QGLFormat::OpenGL_ES_Common_Version_1_0);
#elif defined(QT_OPENGL_ES_2)
QVERIFY(QGLFormat::openGLVersionFlags() & QGLFormat::OpenGL_ES_Version_2_0);
#else
QVERIFY(QGLFormat::openGLVersionFlags() & QGLFormat::OpenGL_Version_1_1);
#endif //defined(QT_OPENGL_ES_1)
#endif //QT_BUILD_INTERNAL
}
static bool fuzzyComparePixels(const QRgb testPixel, const QRgb refPixel, const char* file, int line, int x = -1, int y = -1)
{
static int maxFuzz = 1;
static bool maxFuzzSet = false;
// On 16 bpp systems, we need to allow for more fuzz:
if (!maxFuzzSet) {
maxFuzzSet = true;
if (appDefaultDepth() < 24)
maxFuzz = 32;
}
int redFuzz = qAbs(qRed(testPixel) - qRed(refPixel));
int greenFuzz = qAbs(qGreen(testPixel) - qGreen(refPixel));
int blueFuzz = qAbs(qBlue(testPixel) - qBlue(refPixel));
int alphaFuzz = qAbs(qAlpha(testPixel) - qAlpha(refPixel));
if (refPixel != 0 && testPixel == 0) {
QString msg;
if (x >= 0) {
msg = QString("Test pixel [%1, %2] is null (black) when it should be (%3,%4,%5,%6)")
.arg(x).arg(y)
.arg(qRed(refPixel)).arg(qGreen(refPixel)).arg(qBlue(refPixel)).arg(qAlpha(refPixel));
} else {
msg = QString("Test pixel is null (black) when it should be (%2,%3,%4,%5)")
.arg(qRed(refPixel)).arg(qGreen(refPixel)).arg(qBlue(refPixel)).arg(qAlpha(refPixel));
}
QTest::qFail(msg.toLatin1(), file, line);
return false;
}
if (redFuzz > maxFuzz || greenFuzz > maxFuzz || blueFuzz > maxFuzz || alphaFuzz > maxFuzz) {
QString msg;
if (x >= 0)
msg = QString("Pixel [%1,%2]: ").arg(x).arg(y);
else
msg = QString("Pixel ");
msg += QString("Max fuzz (%1) exceeded: (%2,%3,%4,%5) vs (%6,%7,%8,%9)")
.arg(maxFuzz)
.arg(qRed(testPixel)).arg(qGreen(testPixel)).arg(qBlue(testPixel)).arg(qAlpha(testPixel))
.arg(qRed(refPixel)).arg(qGreen(refPixel)).arg(qBlue(refPixel)).arg(qAlpha(refPixel));
QTest::qFail(msg.toLatin1(), file, line);
return false;
}
return true;
}
static void fuzzyCompareImages(const QImage &testImage, const QImage &referenceImage, const char* file, int line)
{
QCOMPARE(testImage.width(), referenceImage.width());
QCOMPARE(testImage.height(), referenceImage.height());
for (int y = 0; y < testImage.height(); y++) {
for (int x = 0; x < testImage.width(); x++) {
if (!fuzzyComparePixels(testImage.pixel(x, y), referenceImage.pixel(x, y), file, line, x, y)) {
// Might as well save the images for easier debugging:
referenceImage.save("referenceImage.png");
testImage.save("testImage.png");
return;
}
}
}
}
#define QFUZZY_COMPARE_IMAGES(A,B) \
fuzzyCompareImages(A, B, __FILE__, __LINE__)
#define QFUZZY_COMPARE_PIXELS(A,B) \
fuzzyComparePixels(A, B, __FILE__, __LINE__)
class UnclippedWidget : public QWidget
{
public:
void paintEvent(QPaintEvent *)
{
QPainter p(this);
p.fillRect(rect().adjusted(-1000, -1000, 1000, 1000), Qt::black);
}
};
void tst_QGL::graphicsViewClipping()
{
const int size = 64;
UnclippedWidget *widget = new UnclippedWidget;
widget->setFixedSize(size, size);
QGraphicsScene scene;
scene.addWidget(widget)->setPos(0, 0);
QGraphicsView view(&scene);
#ifdef Q_WS_QWS
view.setWindowFlags(Qt::FramelessWindowHint);
#endif
view.resize(2*size, 2*size);
QGLWidget *viewport = new QGLWidget;
view.setViewport(viewport);
view.show();
if (!viewport->isValid())
return;
scene.setSceneRect(view.viewport()->rect());
#ifdef Q_WS_X11
qt_x11_wait_for_window_manager(&view);
#endif
QTest::qWait(500);
QImage image = viewport->grabFrameBuffer();
QImage expected = image;
QPainter p(&expected);
p.fillRect(expected.rect(), Qt::white);
p.fillRect(QRect(0, 0, size, size), Qt::black);
p.end();
QFUZZY_COMPARE_IMAGES(image, expected);
}
void tst_QGL::partialGLWidgetUpdates_data()
{
QTest::addColumn<bool>("doubleBufferedContext");
QTest::addColumn<bool>("autoFillBackground");
QTest::addColumn<bool>("supportsPartialUpdates");
QTest::newRow("Double buffered context") << true << true << false;
QTest::newRow("Double buffered context without auto-fill background") << true << false << false;
QTest::newRow("Single buffered context") << false << true << false;
QTest::newRow("Single buffered context without auto-fill background") << false << false << true;
}
void tst_QGL::partialGLWidgetUpdates()
{
if (!QGLFormat::hasOpenGL())
QSKIP("QGL not supported on this platform", SkipAll);
QFETCH(bool, doubleBufferedContext);
QFETCH(bool, autoFillBackground);
QFETCH(bool, supportsPartialUpdates);
class MyGLWidget : public QGLWidget
{
public:
QRegion paintEventRegion;
void paintEvent(QPaintEvent *e)
{
paintEventRegion = e->region();
}
};
QGLFormat format = QGLFormat::defaultFormat();
format.setDoubleBuffer(doubleBufferedContext);
QGLFormat::setDefaultFormat(format);
MyGLWidget widget;
widget.setFixedSize(150, 150);
widget.setAutoFillBackground(autoFillBackground);
widget.show();
#ifdef Q_WS_X11
qt_x11_wait_for_window_manager(&widget);
#endif
QTest::qWait(200);
if (widget.format().doubleBuffer() != doubleBufferedContext)
QSKIP("Platform does not support requested format", SkipAll);
widget.paintEventRegion = QRegion();
widget.repaint(50, 50, 50, 50);
#ifdef Q_WS_MAC
// repaint() is not immediate on the Mac; it has to go through the event loop.
QTest::qWait(200);
#endif
if (supportsPartialUpdates)
QCOMPARE(widget.paintEventRegion, QRegion(50, 50, 50, 50));
else
QCOMPARE(widget.paintEventRegion, QRegion(widget.rect()));
}
// This tests that rendering to a QGLPBuffer using QPainter works.
void tst_QGL::glPBufferRendering()
{
if (!QGLPixelBuffer::hasOpenGLPbuffers())
QSKIP("QGLPixelBuffer not supported on this platform", SkipSingle);
QGLPixelBuffer* pbuf = new QGLPixelBuffer(128, 128);
QPainter p;
bool begun = p.begin(pbuf);
QVERIFY(begun);
QPaintEngine::Type engineType = p.paintEngine()->type();
QVERIFY(engineType == QPaintEngine::OpenGL || engineType == QPaintEngine::OpenGL2);
p.fillRect(0, 0, 128, 128, Qt::red);
p.fillRect(32, 32, 64, 64, Qt::blue);
p.end();
QImage fb = pbuf->toImage();
delete pbuf;
QImage reference(128, 128, fb.format());
p.begin(&reference);
p.fillRect(0, 0, 128, 128, Qt::red);
p.fillRect(32, 32, 64, 64, Qt::blue);
p.end();
QFUZZY_COMPARE_IMAGES(fb, reference);
}
void tst_QGL::glWidgetWithAlpha()
{
QGLWidget* w = new QGLWidget(QGLFormat(QGL::AlphaChannel));
w->show();
#ifdef Q_WS_X11
qt_x11_wait_for_window_manager(w);
#endif
delete w;
}
void qt_opengl_draw_test_pattern(QPainter* painter, int width, int height)
{
QPainterPath intersectingPath;
intersectingPath.moveTo(0, 0);
intersectingPath.lineTo(100, 0);
intersectingPath.lineTo(0, 100);
intersectingPath.lineTo(100, 100);
intersectingPath.closeSubpath();
QPainterPath trianglePath;
trianglePath.moveTo(50, 0);
trianglePath.lineTo(100, 100);
trianglePath.lineTo(0, 100);
trianglePath.closeSubpath();
painter->setTransform(QTransform()); // reset xform
painter->fillRect(-1, -1, width+2, height+2, Qt::red); // Background
painter->translate(14, 14);
painter->fillPath(intersectingPath, Qt::blue); // Test stencil buffer works
painter->translate(128, 0);
painter->setClipPath(trianglePath); // Test depth buffer works
painter->setTransform(QTransform()); // reset xform ready for fill
painter->fillRect(-1, -1, width+2, height+2, Qt::green);
}
void qt_opengl_check_test_pattern(const QImage& img)
{
// As we're doing more than trivial painting, we can't just compare to
// an image rendered with raster. Instead, we sample at well-defined
// test-points:
QFUZZY_COMPARE_PIXELS(img.pixel(39, 64), QColor(Qt::red).rgb());
QFUZZY_COMPARE_PIXELS(img.pixel(89, 64), QColor(Qt::red).rgb());
QFUZZY_COMPARE_PIXELS(img.pixel(64, 39), QColor(Qt::blue).rgb());
QFUZZY_COMPARE_PIXELS(img.pixel(64, 89), QColor(Qt::blue).rgb());
QFUZZY_COMPARE_PIXELS(img.pixel(167, 39), QColor(Qt::red).rgb());
QFUZZY_COMPARE_PIXELS(img.pixel(217, 39), QColor(Qt::red).rgb());
QFUZZY_COMPARE_PIXELS(img.pixel(192, 64), QColor(Qt::green).rgb());
}
class GLWidget : public QGLWidget
{
public:
GLWidget(QWidget* p = 0)
: QGLWidget(p), beginOk(false), engineType(QPaintEngine::MaxUser) {}
bool beginOk;
QPaintEngine::Type engineType;
void paintGL()
{
QPainter p;
beginOk = p.begin(this);
QPaintEngine* pe = p.paintEngine();
engineType = pe->type();
qt_opengl_draw_test_pattern(&p, width(), height());
// No p.end() or swap buffers, should be done automatically
}
};
void tst_QGL::glWidgetRendering()
{
GLWidget w;
#ifdef Q_WS_QWS
w.setWindowFlags(Qt::FramelessWindowHint);
#endif
w.resize(256, 128);
w.show();
#ifdef Q_WS_X11
qt_x11_wait_for_window_manager(&w);
#endif
QTest::qWait(200);
QVERIFY(w.beginOk);
QVERIFY(w.engineType == QPaintEngine::OpenGL || w.engineType == QPaintEngine::OpenGL2);
QImage fb = w.grabFrameBuffer(false);
qt_opengl_check_test_pattern(fb);
}
void tst_QGL::glFBOSimpleRendering()
{
if (!QGLFramebufferObject::hasOpenGLFramebufferObjects())
QSKIP("QGLFramebufferObject not supported on this platform", SkipSingle);
QGLWidget glw;
glw.makeCurrent();
// No multisample with combined depth/stencil attachment:
QGLFramebufferObjectFormat fboFormat;
fboFormat.setAttachment(QGLFramebufferObject::NoAttachment);
QGLFramebufferObject *fbo = new QGLFramebufferObject(200, 100, fboFormat);
fbo->bind();
glClearColor(1.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glFinish();
QImage fb = fbo->toImage().convertToFormat(QImage::Format_RGB32);
QImage reference(fb.size(), QImage::Format_RGB32);
reference.fill(0xffff0000);
QFUZZY_COMPARE_IMAGES(fb, reference);
delete fbo;
}
// NOTE: This tests that CombinedDepthStencil attachment works by assuming the
// GL2 engine is being used and is implemented the same way as it was when
// this autotest was written. If this is not the case, there may be some
// false-positives: I.e. The test passes when either the depth or stencil
// buffer is actually missing. But that's probably ok anyway.
void tst_QGL::glFBORendering()
{
if (!QGLFramebufferObject::hasOpenGLFramebufferObjects())
QSKIP("QGLFramebufferObject not supported on this platform", SkipSingle);
QGLWidget glw;
glw.makeCurrent();
// No multisample with combined depth/stencil attachment:
QGLFramebufferObjectFormat fboFormat;
fboFormat.setAttachment(QGLFramebufferObject::CombinedDepthStencil);
// Don't complicate things by using NPOT:
QGLFramebufferObject *fbo = new QGLFramebufferObject(256, 128, fboFormat);
if (fbo->attachment() != QGLFramebufferObject::CombinedDepthStencil) {
delete fbo;
QSKIP("FBOs missing combined depth~stencil support", SkipSingle);
}
QPainter fboPainter;
bool painterBegun = fboPainter.begin(fbo);
QVERIFY(painterBegun);
qt_opengl_draw_test_pattern(&fboPainter, fbo->width(), fbo->height());
fboPainter.end();
QImage fb = fbo->toImage().convertToFormat(QImage::Format_RGB32);
delete fbo;
qt_opengl_check_test_pattern(fb);
}
// Tests multiple QPainters active on different FBOs at the same time, with
// interleaving painting. Performance-wise, this is sub-optimal, but it still
// has to work flawlessly
void tst_QGL::multipleFBOInterleavedRendering()
{
if (!QGLFramebufferObject::hasOpenGLFramebufferObjects())
QSKIP("QGLFramebufferObject not supported on this platform", SkipSingle);
QGLWidget glw;
glw.makeCurrent();
// No multisample with combined depth/stencil attachment:
QGLFramebufferObjectFormat fboFormat;
fboFormat.setAttachment(QGLFramebufferObject::CombinedDepthStencil);
QGLFramebufferObject *fbo1 = new QGLFramebufferObject(256, 128, fboFormat);
QGLFramebufferObject *fbo2 = new QGLFramebufferObject(256, 128, fboFormat);
QGLFramebufferObject *fbo3 = new QGLFramebufferObject(256, 128, fboFormat);
if ( (fbo1->attachment() != QGLFramebufferObject::CombinedDepthStencil) ||
(fbo2->attachment() != QGLFramebufferObject::CombinedDepthStencil) ||
(fbo3->attachment() != QGLFramebufferObject::CombinedDepthStencil) )
{
delete fbo1;
delete fbo2;
delete fbo3;
QSKIP("FBOs missing combined depth~stencil support", SkipSingle);
}
QPainter fbo1Painter;
QPainter fbo2Painter;
QPainter fbo3Painter;
QVERIFY(fbo1Painter.begin(fbo1));
QVERIFY(fbo2Painter.begin(fbo2));
QVERIFY(fbo3Painter.begin(fbo3));
// Confirm we're using the GL2 engine, as interleaved rendering isn't supported
// on the GL1 engine:
if (fbo1Painter.paintEngine()->type() != QPaintEngine::OpenGL2)
QSKIP("Interleaved GL rendering requires OpenGL 2.0 or higher", SkipSingle);
QPainterPath intersectingPath;
intersectingPath.moveTo(0, 0);
intersectingPath.lineTo(100, 0);
intersectingPath.lineTo(0, 100);
intersectingPath.lineTo(100, 100);
intersectingPath.closeSubpath();
QPainterPath trianglePath;
trianglePath.moveTo(50, 0);
trianglePath.lineTo(100, 100);
trianglePath.lineTo(0, 100);
trianglePath.closeSubpath();
fbo1Painter.fillRect(0, 0, fbo1->width(), fbo1->height(), Qt::red); // Background
fbo2Painter.fillRect(0, 0, fbo2->width(), fbo2->height(), Qt::green); // Background
fbo3Painter.fillRect(0, 0, fbo3->width(), fbo3->height(), Qt::blue); // Background
fbo1Painter.translate(14, 14);
fbo2Painter.translate(14, 14);
fbo3Painter.translate(14, 14);
fbo1Painter.fillPath(intersectingPath, Qt::blue); // Test stencil buffer works
fbo2Painter.fillPath(intersectingPath, Qt::red); // Test stencil buffer works
fbo3Painter.fillPath(intersectingPath, Qt::green); // Test stencil buffer works
fbo1Painter.translate(128, 0);
fbo2Painter.translate(128, 0);
fbo3Painter.translate(128, 0);
fbo1Painter.setClipPath(trianglePath);
fbo2Painter.setClipPath(trianglePath);
fbo3Painter.setClipPath(trianglePath);
fbo1Painter.setTransform(QTransform()); // reset xform
fbo2Painter.setTransform(QTransform()); // reset xform
fbo3Painter.setTransform(QTransform()); // reset xform
fbo1Painter.fillRect(0, 0, fbo1->width(), fbo1->height(), Qt::green);
fbo2Painter.fillRect(0, 0, fbo2->width(), fbo2->height(), Qt::blue);
fbo3Painter.fillRect(0, 0, fbo3->width(), fbo3->height(), Qt::red);
fbo1Painter.end();
fbo2Painter.end();
fbo3Painter.end();
QImage fb1 = fbo1->toImage().convertToFormat(QImage::Format_RGB32);
QImage fb2 = fbo2->toImage().convertToFormat(QImage::Format_RGB32);
QImage fb3 = fbo3->toImage().convertToFormat(QImage::Format_RGB32);
delete fbo1;
delete fbo2;
delete fbo3;
// As we're doing more than trivial painting, we can't just compare to
// an image rendered with raster. Instead, we sample at well-defined
// test-points:
QFUZZY_COMPARE_PIXELS(fb1.pixel(39, 64), QColor(Qt::red).rgb());
QFUZZY_COMPARE_PIXELS(fb1.pixel(89, 64), QColor(Qt::red).rgb());
QFUZZY_COMPARE_PIXELS(fb1.pixel(64, 39), QColor(Qt::blue).rgb());
QFUZZY_COMPARE_PIXELS(fb1.pixel(64, 89), QColor(Qt::blue).rgb());
QFUZZY_COMPARE_PIXELS(fb1.pixel(167, 39), QColor(Qt::red).rgb());
QFUZZY_COMPARE_PIXELS(fb1.pixel(217, 39), QColor(Qt::red).rgb());
QFUZZY_COMPARE_PIXELS(fb1.pixel(192, 64), QColor(Qt::green).rgb());
QFUZZY_COMPARE_PIXELS(fb2.pixel(39, 64), QColor(Qt::green).rgb());
QFUZZY_COMPARE_PIXELS(fb2.pixel(89, 64), QColor(Qt::green).rgb());
QFUZZY_COMPARE_PIXELS(fb2.pixel(64, 39), QColor(Qt::red).rgb());
QFUZZY_COMPARE_PIXELS(fb2.pixel(64, 89), QColor(Qt::red).rgb());
QFUZZY_COMPARE_PIXELS(fb2.pixel(167, 39), QColor(Qt::green).rgb());
QFUZZY_COMPARE_PIXELS(fb2.pixel(217, 39), QColor(Qt::green).rgb());
QFUZZY_COMPARE_PIXELS(fb2.pixel(192, 64), QColor(Qt::blue).rgb());
QFUZZY_COMPARE_PIXELS(fb3.pixel(39, 64), QColor(Qt::blue).rgb());
QFUZZY_COMPARE_PIXELS(fb3.pixel(89, 64), QColor(Qt::blue).rgb());
QFUZZY_COMPARE_PIXELS(fb3.pixel(64, 39), QColor(Qt::green).rgb());
QFUZZY_COMPARE_PIXELS(fb3.pixel(64, 89), QColor(Qt::green).rgb());
QFUZZY_COMPARE_PIXELS(fb3.pixel(167, 39), QColor(Qt::blue).rgb());
QFUZZY_COMPARE_PIXELS(fb3.pixel(217, 39), QColor(Qt::blue).rgb());
QFUZZY_COMPARE_PIXELS(fb3.pixel(192, 64), QColor(Qt::red).rgb());
}
class FBOUseInGLWidget : public QGLWidget
{
public:
bool widgetPainterBeginOk;
bool fboPainterBeginOk;
QImage fboImage;
protected:
void paintEvent(QPaintEvent*)
{
QPainter widgetPainter;
widgetPainterBeginOk = widgetPainter.begin(this);
QGLFramebufferObjectFormat fboFormat;
fboFormat.setAttachment(QGLFramebufferObject::NoAttachment);
QGLFramebufferObject *fbo = new QGLFramebufferObject(100, 100, fboFormat);
QPainter fboPainter;
fboPainterBeginOk = fboPainter.begin(fbo);
fboPainter.fillRect(-1, -1, 130, 130, Qt::red);
fboPainter.end();
fboImage = fbo->toImage();
widgetPainter.fillRect(-1, -1, width()+2, width()+2, Qt::blue);
delete fbo;
}
};
void tst_QGL::glFBOUseInGLWidget()
{
if (!QGLFramebufferObject::hasOpenGLFramebufferObjects())
QSKIP("QGLFramebufferObject not supported on this platform", SkipSingle);
FBOUseInGLWidget w;
#ifdef Q_WS_QWS
w.setWindowFlags(Qt::FramelessWindowHint);
#endif
w.resize(100, 100);
w.show();
#ifdef Q_WS_X11
qt_x11_wait_for_window_manager(&w);
#endif
QTest::qWait(200);
QVERIFY(w.widgetPainterBeginOk);
QVERIFY(w.fboPainterBeginOk);
QImage widgetFB = w.grabFrameBuffer(false);
QImage widgetReference(widgetFB.size(), widgetFB.format());
widgetReference.fill(0xff0000ff);
QFUZZY_COMPARE_IMAGES(widgetFB, widgetReference);
QImage fboReference(w.fboImage.size(), w.fboImage.format());
fboReference.fill(0xffff0000);
QFUZZY_COMPARE_IMAGES(w.fboImage, fboReference);
}
void tst_QGL::glWidgetReparent()
{
// Try it as a top-level first:
GLWidget *widget = new GLWidget;
widget->setGeometry(0, 0, 200, 30);
widget->show();
QWidget grandParentWidget;
grandParentWidget.setPalette(Qt::blue);
QVBoxLayout grandParentLayout(&grandParentWidget);
QWidget parentWidget(&grandParentWidget);
grandParentLayout.addWidget(&parentWidget);
parentWidget.setPalette(Qt::green);
parentWidget.setAutoFillBackground(true);
QVBoxLayout parentLayout(&parentWidget);
grandParentWidget.setGeometry(0, 100, 200, 200);
grandParentWidget.show();
#ifdef Q_WS_X11
qt_x11_wait_for_window_manager(widget);
qt_x11_wait_for_window_manager(&parentWidget);
#endif
QTest::qWait(200);
QVERIFY(parentWidget.children().count() == 1); // The layout
// Now both widgets should be created & shown, time to re-parent:
parentLayout.addWidget(widget);
#ifdef Q_WS_X11
qt_x11_wait_for_window_manager(&parentWidget);
#endif
QTest::qWait(200);
QVERIFY(parentWidget.children().count() == 2); // Layout & glwidget
QVERIFY(parentWidget.children().contains(widget));
QVERIFY(widget->height() > 30);
delete widget;
#ifdef Q_WS_X11
qt_x11_wait_for_window_manager(&parentWidget);
#endif
QTest::qWait(200);
QVERIFY(parentWidget.children().count() == 1); // The layout
// Now do pretty much the same thing, but don't show the
// widget first:
widget = new GLWidget;
parentLayout.addWidget(widget);
#ifdef Q_WS_X11
qt_x11_wait_for_window_manager(&parentWidget);
#endif
QTest::qWait(200);
QVERIFY(parentWidget.children().count() == 2); // Layout & glwidget
QVERIFY(parentWidget.children().contains(widget));
QVERIFY(widget->height() > 30);
delete widget;
}
class RenderPixmapWidget : public QGLWidget
{
protected:
void initializeGL() {
// Set some gl state:
glClearColor(1.0, 0.0, 0.0, 1.0);
}
void paintGL() {
glClear(GL_COLOR_BUFFER_BIT);
}
};
void tst_QGL::glWidgetRenderPixmap()
{
RenderPixmapWidget *w = new RenderPixmapWidget;
QPixmap pm = w->renderPixmap(100, 100, false);
delete w;
QImage fb = pm.toImage().convertToFormat(QImage::Format_RGB32);
QImage reference(fb.size(), QImage::Format_RGB32);
reference.fill(0xffff0000);
#if defined(QGL_EGL) && !defined(Q_WS_X11)
QSKIP("renderPixmap() not yet supported under EGL on your platform", SkipAll);
#endif
QFUZZY_COMPARE_IMAGES(fb, reference);
}
class ColormapExtended : public QGLColormap
{
public:
ColormapExtended() {}
Qt::HANDLE handle() { return QGLColormap::handle(); }
void setHandle(Qt::HANDLE handle) { QGLColormap::setHandle(handle); }
};
void tst_QGL::colormap()
{
// Check the properties of the default empty colormap.
QGLColormap cmap1;
QVERIFY(cmap1.isEmpty());
QCOMPARE(cmap1.size(), 0);
QVERIFY(cmap1.entryRgb(0) == 0);
QVERIFY(cmap1.entryRgb(-1) == 0);
QVERIFY(cmap1.entryRgb(100) == 0);
QVERIFY(!cmap1.entryColor(0).isValid());
QVERIFY(!cmap1.entryColor(-1).isValid());
QVERIFY(!cmap1.entryColor(100).isValid());
QCOMPARE(cmap1.find(qRgb(255, 0, 0)), -1);
QCOMPARE(cmap1.findNearest(qRgb(255, 0, 0)), -1);
// Set an entry and re-test.
cmap1.setEntry(56, qRgb(255, 0, 0));
// The colormap is still considered "empty" even though it
// has entries in it now. The isEmpty() method is used to
// detect when the colormap is in use by a GL widget,
// not to detect when it is empty!
QVERIFY(cmap1.isEmpty());
QCOMPARE(cmap1.size(), 256);
QVERIFY(cmap1.entryRgb(0) == 0);
QVERIFY(cmap1.entryColor(0) == QColor(0, 0, 0, 255));
QVERIFY(cmap1.entryRgb(56) == qRgb(255, 0, 0));
QVERIFY(cmap1.entryColor(56) == QColor(255, 0, 0, 255));
QCOMPARE(cmap1.find(qRgb(255, 0, 0)), 56);
QCOMPARE(cmap1.findNearest(qRgb(255, 0, 0)), 56);
// Set some more entries.
static QRgb const colors[] = {
qRgb(255, 0, 0),
qRgb(0, 255, 0),
qRgb(255, 255, 255),
qRgb(0, 0, 255),
qRgb(0, 0, 0)
};
cmap1.setEntry(57, QColor(0, 255, 0));
cmap1.setEntries(3, colors + 2, 58);
cmap1.setEntries(5, colors, 251);
int idx;
for (idx = 0; idx < 5; ++idx) {
QVERIFY(cmap1.entryRgb(56 + idx) == colors[idx]);
QVERIFY(cmap1.entryColor(56 + idx) == QColor(colors[idx]));
QVERIFY(cmap1.entryRgb(251 + idx) == colors[idx]);
QVERIFY(cmap1.entryColor(251 + idx) == QColor(colors[idx]));
}
QCOMPARE(cmap1.size(), 256);
// Perform color lookups.
QCOMPARE(cmap1.find(qRgb(255, 0, 0)), 56);
QCOMPARE(cmap1.find(qRgb(0, 0, 0)), 60); // Actually finds 0, 0, 0, 255.
QCOMPARE(cmap1.find(qRgba(0, 0, 0, 0)), 0);
QCOMPARE(cmap1.find(qRgb(0, 255, 0)), 57);
QCOMPARE(cmap1.find(qRgb(255, 255, 255)), 58);
QCOMPARE(cmap1.find(qRgb(0, 0, 255)), 59);
QCOMPARE(cmap1.find(qRgb(140, 0, 0)), -1);
QCOMPARE(cmap1.find(qRgb(0, 140, 0)), -1);
QCOMPARE(cmap1.find(qRgb(0, 0, 140)), -1);
QCOMPARE(cmap1.find(qRgb(64, 0, 0)), -1);
QCOMPARE(cmap1.find(qRgb(0, 64, 0)), -1);
QCOMPARE(cmap1.find(qRgb(0, 0, 64)), -1);
QCOMPARE(cmap1.findNearest(qRgb(255, 0, 0)), 56);
QCOMPARE(cmap1.findNearest(qRgb(0, 0, 0)), 60);
QCOMPARE(cmap1.findNearest(qRgba(0, 0, 0, 0)), 0);
QCOMPARE(cmap1.findNearest(qRgb(0, 255, 0)), 57);
QCOMPARE(cmap1.findNearest(qRgb(255, 255, 255)), 58);
QCOMPARE(cmap1.findNearest(qRgb(0, 0, 255)), 59);
QCOMPARE(cmap1.findNearest(qRgb(140, 0, 0)), 56);
QCOMPARE(cmap1.findNearest(qRgb(0, 140, 0)), 57);
QCOMPARE(cmap1.findNearest(qRgb(0, 0, 140)), 59);
QCOMPARE(cmap1.findNearest(qRgb(64, 0, 0)), 0);
QCOMPARE(cmap1.findNearest(qRgb(0, 64, 0)), 0);
QCOMPARE(cmap1.findNearest(qRgb(0, 0, 64)), 0);
// Make some copies of the colormap and check that they are the same.
QGLColormap cmap2(cmap1);
QGLColormap cmap3;
cmap3 = cmap1;
QVERIFY(cmap2.isEmpty());
QVERIFY(cmap3.isEmpty());
QCOMPARE(cmap2.size(), 256);
QCOMPARE(cmap3.size(), 256);
for (idx = 0; idx < 256; ++idx) {
QCOMPARE(cmap1.entryRgb(idx), cmap2.entryRgb(idx));
QCOMPARE(cmap1.entryRgb(idx), cmap3.entryRgb(idx));
}
// Modify an entry in one of the copies and recheck the original.
cmap2.setEntry(45, qRgb(255, 0, 0));
for (idx = 0; idx < 256; ++idx) {
if (idx != 45)
QCOMPARE(cmap1.entryRgb(idx), cmap2.entryRgb(idx));
else
QCOMPARE(cmap2.entryRgb(45), qRgb(255, 0, 0));
QCOMPARE(cmap1.entryRgb(idx), cmap3.entryRgb(idx));
}
// Check that setting the handle will cause isEmpty() to work right.
ColormapExtended cmap4;
cmap4.setEntry(56, qRgb(255, 0, 0));
QVERIFY(cmap4.isEmpty());
QCOMPARE(cmap4.size(), 256);
cmap4.setHandle(Qt::HANDLE(42));
QVERIFY(cmap4.handle() == Qt::HANDLE(42));
QVERIFY(!cmap4.isEmpty());
QCOMPARE(cmap4.size(), 256);
}
#ifndef QT_OPENGL_ES
#define DEFAULT_FORMAT GL_RGBA8
#else
#define DEFAULT_FORMAT GL_RGBA
#endif
#ifndef GL_TEXTURE_3D
#define GL_TEXTURE_3D 0x806F
#endif
#ifndef GL_RGB16
#define GL_RGB16 0x8054
#endif
void tst_QGL::fboFormat()
{
// Check the initial conditions.
QGLFramebufferObjectFormat format1;
QCOMPARE(format1.samples(), 0);
QVERIFY(format1.attachment() == QGLFramebufferObject::NoAttachment);
QCOMPARE(int(format1.textureTarget()), int(GL_TEXTURE_2D));
QCOMPARE(int(format1.internalTextureFormat()), int(DEFAULT_FORMAT));
// Modify the values and re-check.
format1.setSamples(8);
format1.setAttachment(QGLFramebufferObject::CombinedDepthStencil);
format1.setTextureTarget(GL_TEXTURE_3D);
format1.setInternalTextureFormat(GL_RGB16);
QCOMPARE(format1.samples(), 8);
QVERIFY(format1.attachment() == QGLFramebufferObject::CombinedDepthStencil);
QCOMPARE(int(format1.textureTarget()), int(GL_TEXTURE_3D));
QCOMPARE(int(format1.internalTextureFormat()), int(GL_RGB16));
// Make copies and check that they are the same.
QGLFramebufferObjectFormat format2(format1);
QGLFramebufferObjectFormat format3;
QCOMPARE(format2.samples(), 8);
QVERIFY(format2.attachment() == QGLFramebufferObject::CombinedDepthStencil);
QCOMPARE(int(format2.textureTarget()), int(GL_TEXTURE_3D));
QCOMPARE(int(format2.internalTextureFormat()), int(GL_RGB16));
format3 = format1;
QCOMPARE(format3.samples(), 8);
QVERIFY(format3.attachment() == QGLFramebufferObject::CombinedDepthStencil);
QCOMPARE(int(format3.textureTarget()), int(GL_TEXTURE_3D));
QCOMPARE(int(format3.internalTextureFormat()), int(GL_RGB16));
// Modify the copies and check that the original is unchanged.
format2.setSamples(9);
format3.setTextureTarget(GL_TEXTURE_2D);
QCOMPARE(format1.samples(), 8);
QVERIFY(format1.attachment() == QGLFramebufferObject::CombinedDepthStencil);
QCOMPARE(int(format1.textureTarget()), int(GL_TEXTURE_3D));
QCOMPARE(int(format1.internalTextureFormat()), int(GL_RGB16));
// operator== and operator!= for QGLFramebufferObjectFormat.
QGLFramebufferObjectFormat format1c;
QGLFramebufferObjectFormat format2c;
QVERIFY(format1c == format2c);
QVERIFY(!(format1c != format2c));
format1c.setSamples(8);
QVERIFY(!(format1c == format2c));
QVERIFY(format1c != format2c);
format2c.setSamples(8);
QVERIFY(format1c == format2c);
QVERIFY(!(format1c != format2c));
format1c.setAttachment(QGLFramebufferObject::CombinedDepthStencil);
QVERIFY(!(format1c == format2c));
QVERIFY(format1c != format2c);
format2c.setAttachment(QGLFramebufferObject::CombinedDepthStencil);
QVERIFY(format1c == format2c);
QVERIFY(!(format1c != format2c));
format1c.setTextureTarget(GL_TEXTURE_3D);
QVERIFY(!(format1c == format2c));
QVERIFY(format1c != format2c);
format2c.setTextureTarget(GL_TEXTURE_3D);
QVERIFY(format1c == format2c);
QVERIFY(!(format1c != format2c));
format1c.setInternalTextureFormat(GL_RGB16);
QVERIFY(!(format1c == format2c));
QVERIFY(format1c != format2c);
format2c.setInternalTextureFormat(GL_RGB16);
QVERIFY(format1c == format2c);
QVERIFY(!(format1c != format2c));
QGLFramebufferObjectFormat format3c(format1c);
QGLFramebufferObjectFormat format4c;
QVERIFY(format1c == format3c);
QVERIFY(!(format1c != format3c));
format3c.setInternalTextureFormat(DEFAULT_FORMAT);
QVERIFY(!(format1c == format3c));
QVERIFY(format1c != format3c);
format4c = format1c;
QVERIFY(format1c == format4c);
QVERIFY(!(format1c != format4c));
format4c.setInternalTextureFormat(DEFAULT_FORMAT);
QVERIFY(!(format1c == format4c));
QVERIFY(format1c != format4c);
}
void tst_QGL::testDontCrashOnDanglingResources()
{
// We have a number of Q_GLOBAL_STATICS inside the QtOpenGL
// library. This test is verify that we don't crash as a result of
// them calling into libgl on application shutdown.
QWidget *widget = new UnclippedWidget();
widget->show();
qApp->processEvents();
widget->hide();
}
class ReplaceClippingGLWidget : public QGLWidget
{
public:
void paint(QPainter *painter)
{
painter->fillRect(rect(), Qt::white);
QPainterPath path;
path.addRect(0, 0, 100, 100);
path.addRect(50, 50, 100, 100);
painter->setClipRect(0, 0, 150, 150);
painter->fillPath(path, Qt::red);
painter->translate(150, 150);
painter->setClipRect(0, 0, 150, 150);
painter->fillPath(path, Qt::red);
}
protected:
void paintEvent(QPaintEvent*)
{
// clear the stencil with junk
glStencilMask(0xFFFF);
glClearStencil(0xFFFF);
glDisable(GL_STENCIL_TEST);
glDisable(GL_SCISSOR_TEST);
glClear(GL_STENCIL_BUFFER_BIT);
QPainter painter(this);
paint(&painter);
}
};
void tst_QGL::replaceClipping()
{
ReplaceClippingGLWidget glw;
#ifdef Q_WS_QWS
glw.setWindowFlags(Qt::FramelessWindowHint);
#endif
glw.resize(300, 300);
glw.show();
#ifdef Q_WS_X11
qt_x11_wait_for_window_manager(&glw);
#endif
QTest::qWait(200);
QImage reference(300, 300, QImage::Format_RGB32);
QPainter referencePainter(&reference);
glw.paint(&referencePainter);
referencePainter.end();
const QImage widgetFB = glw.grabFrameBuffer(false).convertToFormat(QImage::Format_RGB32);
// Sample pixels in a grid pattern which avoids false failures due to
// off-by-one pixel errors on some buggy GL implementations
for (int x = 25; x < reference.width(); x += 50) {
for (int y = 25; y < reference.width(); y += 50) {
QFUZZY_COMPARE_PIXELS(widgetFB.pixel(x, y), reference.pixel(x, y));
}
}
}
class ClipTestGLWidget : public QGLWidget
{
public:
void paint(QPainter *painter)
{
painter->fillRect(-1, -1, width()+2, height()+2, Qt::white);
painter->setClipRect(10, 10, width()-20, height()-20);
painter->fillRect(rect(), Qt::cyan);
painter->save();
painter->setClipRect(10, 10, 100, 100, Qt::IntersectClip);
painter->fillRect(rect(), Qt::blue);
painter->save();
painter->setClipRect(10, 10, 50, 50, Qt::IntersectClip);
painter->fillRect(rect(), Qt::red);
painter->restore();
painter->fillRect(0, 0, 40, 40, Qt::white);
painter->save();
painter->setClipRect(0, 0, 35, 35, Qt::IntersectClip);
painter->fillRect(rect(), Qt::black);
painter->restore();
painter->fillRect(0, 0, 30, 30, Qt::magenta);
painter->save();
painter->setClipRect(60, 10, 50, 50, Qt::ReplaceClip);
painter->fillRect(rect(), Qt::green);
painter->restore();
painter->save();
painter->setClipRect(0, 60, 60, 25, Qt::IntersectClip);
painter->setClipRect(60, 60, 50, 25, Qt::UniteClip);
painter->fillRect(rect(), Qt::yellow);
painter->restore();
painter->restore();
painter->translate(100, 100);
{
QPainterPath path;
path.addRect(10, 10, 100, 100);
path.addRect(10, 10, 10, 10);
painter->setClipPath(path, Qt::IntersectClip);
}
painter->fillRect(rect(), Qt::blue);
painter->save();
{
QPainterPath path;
path.addRect(10, 10, 50, 50);
path.addRect(10, 10, 10, 10);
painter->setClipPath(path, Qt::IntersectClip);
}
painter->fillRect(rect(), Qt::red);
painter->restore();
painter->fillRect(0, 0, 40, 40, Qt::white);
painter->save();
{
QPainterPath path;
path.addRect(0, 0, 35, 35);
path.addRect(10, 10, 10, 10);
painter->setClipPath(path, Qt::IntersectClip);
}
painter->fillRect(rect(), Qt::black);
painter->restore();
painter->fillRect(0, 0, 30, 30, Qt::magenta);
painter->save();
{
QPainterPath path;
path.addRect(60, 10, 50, 50);
path.addRect(10, 10, 10, 10);
painter->setClipPath(path, Qt::ReplaceClip);
}
painter->fillRect(rect(), Qt::green);
painter->restore();
painter->save();
{
QPainterPath path;
path.addRect(0, 60, 60, 25);
path.addRect(10, 10, 10, 10);
painter->setClipPath(path, Qt::IntersectClip);
}
painter->setClipRect(60, 60, 50, 25, Qt::UniteClip);
painter->fillRect(rect(), Qt::yellow);
painter->restore();
}
protected:
void paintEvent(QPaintEvent*)
{
QPainter painter(this);
paint(&painter);
}
};
void tst_QGL::clipTest()
{
ClipTestGLWidget glw;
#ifdef Q_WS_QWS
glw.setWindowFlags(Qt::FramelessWindowHint);
#endif
glw.resize(220, 220);
glw.show();
#ifdef Q_WS_X11
qt_x11_wait_for_window_manager(&glw);
#endif
QTest::qWait(200);
QImage reference(glw.size(), QImage::Format_RGB32);
QPainter referencePainter(&reference);
glw.paint(&referencePainter);
referencePainter.end();
const QImage widgetFB = glw.grabFrameBuffer(false).convertToFormat(QImage::Format_RGB32);
// Sample pixels in a grid pattern which avoids false failures due to
// off-by-one pixel errors on some buggy GL implementations
for (int x = 2; x < reference.width(); x += 5) {
for (int y = 2; y < reference.width(); y += 5) {
QFUZZY_COMPARE_PIXELS(widgetFB.pixel(x, y), reference.pixel(x, y));
}
}
}
void tst_QGL::destroyFBOAfterContext()
{
if (!QGLFramebufferObject::hasOpenGLFramebufferObjects())
QSKIP("QGLFramebufferObject not supported on this platform", SkipSingle);
QGLWidget *glw = new QGLWidget();
glw->makeCurrent();
// No multisample with combined depth/stencil attachment:
QGLFramebufferObjectFormat fboFormat;
fboFormat.setAttachment(QGLFramebufferObject::CombinedDepthStencil);
// Don't complicate things by using NPOT:
QGLFramebufferObject *fbo = new QGLFramebufferObject(256, 128, fboFormat);
// The handle should be valid until the context is destroyed.
QVERIFY(fbo->handle() != 0);
QVERIFY(fbo->isValid());
delete glw;
// The handle should now be zero.
QVERIFY(fbo->handle() == 0);
QVERIFY(!fbo->isValid());
delete fbo;
}
#ifdef QT_BUILD_INTERNAL
class tst_QGLResource : public QObject
{
Q_OBJECT
public:
tst_QGLResource(QObject *parent = 0) : QObject(parent) {}
~tst_QGLResource() { ++deletions; }
static int deletions;
};
int tst_QGLResource::deletions = 0;
static void qt_shared_test_free(void *data)
{
delete reinterpret_cast<tst_QGLResource *>(data);
}
Q_GLOBAL_STATIC_WITH_ARGS(QGLContextResource, qt_shared_test, (qt_shared_test_free))
#endif
void tst_QGL::shareRegister()
{
#ifdef QT_BUILD_INTERNAL
// Create a context.
QGLWidget *glw1 = new QGLWidget();
glw1->makeCurrent();
// Nothing should be sharing with glw1's context yet.
QVERIFY(!glw1->isSharing());
// Create a guard for the first context.
QGLSharedResourceGuard guard(glw1->context());
QVERIFY(guard.id() == 0);
guard.setId(3);
QVERIFY(guard.id() == 3);
// Add a resource to the first context.
tst_QGLResource *res1 = new tst_QGLResource();
QVERIFY(!qt_shared_test()->value(glw1->context()));
qt_shared_test()->insert(glw1->context(), res1);
QVERIFY(qt_shared_test()->value(glw1->context()) == res1);
// Create another context that shares with the first.
QVERIFY(!glw1->isSharing());
QGLWidget *glw2 = new QGLWidget(0, glw1);
if (!glw2->isSharing()) {
delete glw2;
delete glw1;
QSKIP("Context sharing is not supported", SkipSingle);
}
QVERIFY(glw1->isSharing());
QVERIFY(glw1->context() != glw2->context());
// Check that the first context's resource is also on the second.
QVERIFY(qt_shared_test()->value(glw1->context()) == res1);
QVERIFY(qt_shared_test()->value(glw2->context()) == res1);
// Guard should still be the same.
QVERIFY(guard.context() == glw1->context());
QVERIFY(guard.id() == 3);
// Check the sharing relationships.
QVERIFY(QGLContext::areSharing(glw1->context(), glw1->context()));
QVERIFY(QGLContext::areSharing(glw2->context(), glw2->context()));
QVERIFY(QGLContext::areSharing(glw1->context(), glw2->context()));
QVERIFY(QGLContext::areSharing(glw2->context(), glw1->context()));
QVERIFY(!QGLContext::areSharing(0, glw2->context()));
QVERIFY(!QGLContext::areSharing(glw1->context(), 0));
QVERIFY(!QGLContext::areSharing(0, 0));
// Create a third context, not sharing with the others.
QGLWidget *glw3 = new QGLWidget();
QVERIFY(!glw3->isSharing());
// Create a guard on the standalone context.
QGLSharedResourceGuard guard3(glw3->context());
guard3.setId(5);
// Add a resource to the third context.
tst_QGLResource *res3 = new tst_QGLResource();
QVERIFY(!qt_shared_test()->value(glw3->context()));
qt_shared_test()->insert(glw3->context(), res3);
QVERIFY(qt_shared_test()->value(glw1->context()) == res1);
QVERIFY(qt_shared_test()->value(glw2->context()) == res1);
QVERIFY(qt_shared_test()->value(glw3->context()) == res3);
// Check the sharing relationships again.
QVERIFY(QGLContext::areSharing(glw1->context(), glw1->context()));
QVERIFY(QGLContext::areSharing(glw2->context(), glw2->context()));
QVERIFY(QGLContext::areSharing(glw1->context(), glw2->context()));
QVERIFY(QGLContext::areSharing(glw2->context(), glw1->context()));
QVERIFY(!QGLContext::areSharing(glw1->context(), glw3->context()));
QVERIFY(!QGLContext::areSharing(glw2->context(), glw3->context()));
QVERIFY(!QGLContext::areSharing(glw3->context(), glw1->context()));
QVERIFY(!QGLContext::areSharing(glw3->context(), glw2->context()));
QVERIFY(QGLContext::areSharing(glw3->context(), glw3->context()));
QVERIFY(!QGLContext::areSharing(0, glw2->context()));
QVERIFY(!QGLContext::areSharing(glw1->context(), 0));
QVERIFY(!QGLContext::areSharing(0, glw3->context()));
QVERIFY(!QGLContext::areSharing(glw3->context(), 0));
QVERIFY(!QGLContext::areSharing(0, 0));
// Shared guard should still be the same.
QVERIFY(guard.context() == glw1->context());
QVERIFY(guard.id() == 3);
// Delete the first context.
delete glw1;
// The second context should no longer register as sharing.
QVERIFY(!glw2->isSharing());
// The first context's resource should transfer to the second context.
QCOMPARE(tst_QGLResource::deletions, 0);
QVERIFY(qt_shared_test()->value(glw2->context()) == res1);
QVERIFY(qt_shared_test()->value(glw3->context()) == res3);
// Shared guard should now be the second context, with the id the same.
QVERIFY(guard.context() == glw2->context());
QVERIFY(guard.id() == 3);
QVERIFY(guard3.context() == glw3->context());
QVERIFY(guard3.id() == 5);
// Clean up and check that the resources are properly deleted.
delete glw2;
QCOMPARE(tst_QGLResource::deletions, 1);
delete glw3;
QCOMPARE(tst_QGLResource::deletions, 2);
// Guards should now be null and the id zero.
QVERIFY(guard.context() == 0);
QVERIFY(guard.id() == 0);
QVERIFY(guard3.context() == 0);
QVERIFY(guard3.id() == 0);
#endif
}
// Tests QGLContext::bindTexture with default options
void tst_QGL::qglContextDefaultBindTexture()
{
#ifdef QT_BUILD_INTERNAL
QGLWidget w;
w.makeCurrent();
QGLContext *ctx = const_cast<QGLContext*>(w.context());
QImage *boundImage = new QImage(256, 256, QImage::Format_RGB32);
boundImage->fill(0xFFFFFFFF);
QPixmap *boundPixmap = new QPixmap(256, 256);
boundPixmap->fill(Qt::red);
int startCacheItemCount = QGLTextureCache::instance()->size();
GLuint boundImageTextureId = ctx->bindTexture(*boundImage);
GLuint boundPixmapTextureId = ctx->bindTexture(*boundPixmap);
// Make sure the image & pixmap have been added to the cache:
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+2);
// Make sure the image & pixmap have the is_cached flag set:
QVERIFY(QImagePixmapCleanupHooks::isImageCached(*boundImage));
QVERIFY(QImagePixmapCleanupHooks::isPixmapCached(*boundPixmap));
// Make sure the texture IDs returned are valid:
QCOMPARE((bool)glIsTexture(boundImageTextureId), GL_TRUE);
QCOMPARE((bool)glIsTexture(boundPixmapTextureId), GL_TRUE);
// Make sure the textures are still valid after we delete the image/pixmap:
// Also check that although the textures are left intact, the cache entries are removed:
delete boundImage;
boundImage = 0;
QCOMPARE((bool)glIsTexture(boundImageTextureId), GL_TRUE);
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+1);
delete boundPixmap;
boundPixmap = 0;
QCOMPARE((bool)glIsTexture(boundPixmapTextureId), GL_TRUE);
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount);
// Finally, make sure QGLContext::deleteTexture deletes the texture IDs:
ctx->deleteTexture(boundImageTextureId);
ctx->deleteTexture(boundPixmapTextureId);
QCOMPARE((bool)glIsTexture(boundImageTextureId), GL_FALSE);
QCOMPARE((bool)glIsTexture(boundPixmapTextureId), GL_FALSE);
#endif
}
void tst_QGL::textureCleanup()
{
#ifdef QT_BUILD_INTERNAL
QGLWidget w;
w.resize(200,200);
w.show();
w.makeCurrent();
// Test pixmaps which have been loaded via QPixmapCache are removed from the texture cache
// when the pixmap cache is cleared
{
int startCacheItemCount = QGLTextureCache::instance()->size();
QPainter p(&w);
QPixmap boundPixmap(":designer.png");
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount);
p.drawPixmap(0, 0, boundPixmap);
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+1);
// Need to call end for the GL2 paint engine to release references to pixmap if using tfp
p.end();
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+1);
// Check that the texture doesn't get removed from the cache when the pixmap is cleared
// as it should still be in the cache:
boundPixmap = QPixmap();
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+1);
QPixmapCache::clear();
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount);
}
// Test pixmaps which have been loaded via QPixmapCache are removed from the texture cache
// when they are explicitly removed from the pixmap cache
{
int startCacheItemCount = QGLTextureCache::instance()->size();
QPainter p(&w);
QPixmap boundPixmap(128, 128);
QString cacheKey = QString::fromLatin1("myPixmap");
QPixmapCache::insert(cacheKey, boundPixmap);
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount);
p.drawPixmap(0, 0, boundPixmap);
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+1);
// Need to call end for the GL2 paint engine to release references to pixmap if using tfp
p.end();
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+1);
// Check that the texture doesn't get removed from the cache when the pixmap is cleared
// as it should still be in the cache:
boundPixmap = QPixmap();
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+1);
// Finally, we check that the texture cache entry is removed when we remove the
// pixmap cache entry, which should hold the last reference:
QPixmapCache::remove(cacheKey);
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount);
}
// Check images & pixmaps are removed from the cache when they are deleted
{
int startCacheItemCount = QGLTextureCache::instance()->size();
QPainter p(&w);
QImage *boundImage = new QImage(256, 256, QImage::Format_RGB32);
boundImage->fill(0xFFFFFFFF);
QPixmap *boundPixmap = new QPixmap(256, 256);
boundPixmap->fill(Qt::red);
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount);
p.drawImage(0, 0, *boundImage);
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+1);
p.drawPixmap(0, 0, *boundPixmap);
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+2);
// Need to call end for the GL2 paint engine to release references to pixmap if using tfp
p.end();
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+2);
delete boundImage;
boundImage = 0;
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+1);
delete boundPixmap;
boundPixmap = 0;
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount);
}
// Check images & pixmaps are removed from the cache when they are assigned to
{
int startCacheItemCount = QGLTextureCache::instance()->size();
QPainter p(&w);
QImage boundImage(256, 256, QImage::Format_RGB32);
boundImage.fill(0xFFFFFFFF);
QPixmap boundPixmap(256, 256);
boundPixmap.fill(Qt::red);
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount);
p.drawImage(0, 0, boundImage);
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+1);
p.drawPixmap(0, 0, boundPixmap);
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+2);
// Need to call end for the GL2 paint engine to release references to pixmap if using tfp
p.end();
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+2);
boundImage = QImage(64, 64, QImage::Format_RGB32);
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+1);
boundPixmap = QPixmap(64, 64);
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount);
}
// Check images & pixmaps are removed from the cache when they are modified (detached)
{
int startCacheItemCount = QGLTextureCache::instance()->size();
QPainter p(&w);
QImage boundImage(256, 256, QImage::Format_RGB32);
boundImage.fill(0xFFFFFFFF);
QPixmap boundPixmap(256, 256);
boundPixmap.fill(Qt::red);
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount);
p.drawImage(0, 0, boundImage);
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+1);
p.drawPixmap(0, 0, boundPixmap);
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+2);
// Need to call end for the GL2 paint engine to release references to pixmap if using tfp
p.end();
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+2);
boundImage.fill(0x00000000);
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+1);
boundPixmap.fill(Qt::blue);
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount);
}
// Check that images/pixmaps aren't removed from the cache if a shallow copy has been made
QImage copyOfImage;
QPixmap copyOfPixmap;
int startCacheItemCount = QGLTextureCache::instance()->size();
{
QPainter p(&w);
QImage boundImage(256, 256, QImage::Format_RGB32);
boundImage.fill(0xFFFFFFFF);
QPixmap boundPixmap(256, 256);
boundPixmap.fill(Qt::red);
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount);
p.drawImage(0, 0, boundImage);
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+1);
p.drawPixmap(0, 0, boundPixmap);
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+2);
// Need to call end for the GL2 paint engine to release references to pixmap if using tfp
p.end();
copyOfImage = boundImage;
copyOfPixmap = boundPixmap;
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+2);
} // boundImage & boundPixmap would have been deleted when they went out of scope
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+2);
copyOfImage = QImage();
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+1);
copyOfPixmap = QPixmap();
QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount);
#endif
}
class tst_QGLDummy : public QObject
{
Q_OBJECT
public:
tst_QGLDummy() {}
private slots:
void qglSkipTests() {
QSKIP("QGL not supported on this system.", SkipAll);
}
};
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QTEST_DISABLE_KEYPAD_NAVIGATION \
QGLWidget glWidget;
if (!glWidget.isValid()) {
tst_QGLDummy tc;
return QTest::qExec(&tc, argc, argv);
}
tst_QGL tc;
return QTest::qExec(&tc, argc, argv);
}
#include "tst_qgl.moc"
|