tests/auto/qgl/tst_qgl.cpp
branchGCC_SURGE
changeset 31 5daf16870df6
parent 30 5dc02b23752f
child 37 758a864f9613
equal deleted inserted replaced
27:93b982ccede2 31:5daf16870df6
   225     // void QGLFormat::setAlphaBufferSize(int)
   225     // void QGLFormat::setAlphaBufferSize(int)
   226     QCOMPARE(-1, obj1.alphaBufferSize());
   226     QCOMPARE(-1, obj1.alphaBufferSize());
   227     QCOMPARE(false, obj1.alpha());
   227     QCOMPARE(false, obj1.alpha());
   228     QVERIFY(!obj1.testOption(QGL::AlphaChannel));
   228     QVERIFY(!obj1.testOption(QGL::AlphaChannel));
   229     QVERIFY(obj1.testOption(QGL::NoAlphaChannel));
   229     QVERIFY(obj1.testOption(QGL::NoAlphaChannel));
   230     obj1.setAlphaBufferSize(0);
   230     obj1.setAlphaBufferSize(1);
   231     QCOMPARE(true, obj1.alpha());   // setAlphaBufferSize() enables alpha.
   231     QCOMPARE(true, obj1.alpha());   // setAlphaBufferSize() enables alpha.
   232     QCOMPARE(0, obj1.alphaBufferSize());
   232     QCOMPARE(1, obj1.alphaBufferSize());
   233     QTest::ignoreMessage(QtWarningMsg, "QGLFormat::setAlphaBufferSize: Cannot set negative alpha buffer size -2147483648");
   233     QTest::ignoreMessage(QtWarningMsg, "QGLFormat::setAlphaBufferSize: Cannot set negative alpha buffer size -2147483648");
   234     obj1.setAlphaBufferSize(TEST_INT_MIN);
   234     obj1.setAlphaBufferSize(TEST_INT_MIN);
   235     QCOMPARE(0, obj1.alphaBufferSize()); // Makes no sense with a negative buffer size
   235     QCOMPARE(1, obj1.alphaBufferSize()); // Makes no sense with a negative buffer size
   236     obj1.setAlphaBufferSize(3);
   236     obj1.setAlphaBufferSize(3);
   237     QTest::ignoreMessage(QtWarningMsg, "QGLFormat::setAlphaBufferSize: Cannot set negative alpha buffer size -1");
   237     QTest::ignoreMessage(QtWarningMsg, "QGLFormat::setAlphaBufferSize: Cannot set negative alpha buffer size -1");
   238     obj1.setAlphaBufferSize(-1);
   238     obj1.setAlphaBufferSize(-1);
   239     QCOMPARE(3, obj1.alphaBufferSize());
   239     QCOMPARE(3, obj1.alphaBufferSize());
   240     obj1.setAlphaBufferSize(TEST_INT_MAX);
   240     obj1.setAlphaBufferSize(TEST_INT_MAX);
   241     QCOMPARE(TEST_INT_MAX, obj1.alphaBufferSize());
   241     QCOMPARE(TEST_INT_MAX, obj1.alphaBufferSize());
   242 
   242 
   243     // int QGLFormat::stencilBufferSize()
   243     // int QGLFormat::stencilBufferSize()
   244     // void QGLFormat::setStencilBufferSize(int)
   244     // void QGLFormat::setStencilBufferSize(int)
   245     QCOMPARE(-1, obj1.stencilBufferSize());
   245     QCOMPARE(-1, obj1.stencilBufferSize());
   246     obj1.setStencilBufferSize(0);
   246     obj1.setStencilBufferSize(1);
   247     QCOMPARE(0, obj1.stencilBufferSize());
   247     QCOMPARE(1, obj1.stencilBufferSize());
   248     QTest::ignoreMessage(QtWarningMsg, "QGLFormat::setStencilBufferSize: Cannot set negative stencil buffer size -2147483648");
   248     QTest::ignoreMessage(QtWarningMsg, "QGLFormat::setStencilBufferSize: Cannot set negative stencil buffer size -2147483648");
   249     obj1.setStencilBufferSize(TEST_INT_MIN);
   249     obj1.setStencilBufferSize(TEST_INT_MIN);
   250     QCOMPARE(0, obj1.stencilBufferSize()); // Makes no sense with a negative buffer size
   250     QCOMPARE(1, obj1.stencilBufferSize()); // Makes no sense with a negative buffer size
   251     obj1.setStencilBufferSize(3);
   251     obj1.setStencilBufferSize(3);
   252     QTest::ignoreMessage(QtWarningMsg, "QGLFormat::setStencilBufferSize: Cannot set negative stencil buffer size -1");
   252     QTest::ignoreMessage(QtWarningMsg, "QGLFormat::setStencilBufferSize: Cannot set negative stencil buffer size -1");
   253     obj1.setStencilBufferSize(-1);
   253     obj1.setStencilBufferSize(-1);
   254     QCOMPARE(3, obj1.stencilBufferSize());
   254     QCOMPARE(3, obj1.stencilBufferSize());
   255     obj1.setStencilBufferSize(TEST_INT_MAX);
   255     obj1.setStencilBufferSize(TEST_INT_MAX);
   350     QVERIFY(obj1.testOption(QGL::AlphaChannel));
   350     QVERIFY(obj1.testOption(QGL::AlphaChannel));
   351     QVERIFY(!obj1.testOption(QGL::NoAlphaChannel));
   351     QVERIFY(!obj1.testOption(QGL::NoAlphaChannel));
   352 
   352 
   353     // bool QGLFormat::accum()
   353     // bool QGLFormat::accum()
   354     // void QGLFormat::setAccum(bool)
   354     // void QGLFormat::setAccum(bool)
       
   355     obj1.setAccumBufferSize(0);
   355     QCOMPARE(false, obj1.accum());
   356     QCOMPARE(false, obj1.accum());
   356     QVERIFY(!obj1.testOption(QGL::AccumBuffer));
   357     QVERIFY(!obj1.testOption(QGL::AccumBuffer));
   357     QVERIFY(obj1.testOption(QGL::NoAccumBuffer));
   358     QVERIFY(obj1.testOption(QGL::NoAccumBuffer));
   358     obj1.setAccum(false);
   359     obj1.setAccum(false);
   359     QCOMPARE(false, obj1.accum());
   360     QCOMPARE(false, obj1.accum());
   428     obj1.setPlane(TEST_INT_MIN);
   429     obj1.setPlane(TEST_INT_MIN);
   429     QCOMPARE(TEST_INT_MIN, obj1.plane());
   430     QCOMPARE(TEST_INT_MIN, obj1.plane());
   430     obj1.setPlane(TEST_INT_MAX);
   431     obj1.setPlane(TEST_INT_MAX);
   431     QCOMPARE(TEST_INT_MAX, obj1.plane());
   432     QCOMPARE(TEST_INT_MAX, obj1.plane());
   432 
   433 
       
   434     // int QGLFormat::major/minorVersion()
       
   435     // void QGLFormat::setVersion(int, int)
       
   436     QCOMPARE(obj1.majorVersion(), 1);
       
   437     QCOMPARE(obj1.minorVersion(), 0);
       
   438     obj1.setVersion(3, 2);
       
   439     QCOMPARE(obj1.majorVersion(), 3);
       
   440     QCOMPARE(obj1.minorVersion(), 2);
       
   441     QTest::ignoreMessage(QtWarningMsg, "QGLFormat::setVersion: Cannot set zero or negative version number 0.1");
       
   442     obj1.setVersion(0, 1);
       
   443     QCOMPARE(obj1.majorVersion(), 3);
       
   444     QCOMPARE(obj1.minorVersion(), 2);
       
   445     QTest::ignoreMessage(QtWarningMsg, "QGLFormat::setVersion: Cannot set zero or negative version number 3.-1");
       
   446     obj1.setVersion(3, -1);
       
   447     QCOMPARE(obj1.majorVersion(), 3);
       
   448     QCOMPARE(obj1.minorVersion(), 2);
       
   449     obj1.setVersion(TEST_INT_MAX, TEST_INT_MAX - 1);
       
   450     QCOMPARE(obj1.majorVersion(), TEST_INT_MAX);
       
   451     QCOMPARE(obj1.minorVersion(), TEST_INT_MAX - 1);
       
   452 
       
   453 
   433     // operator== and operator!= for QGLFormat
   454     // operator== and operator!= for QGLFormat
   434     QGLFormat format1;
   455     QGLFormat format1;
   435     QGLFormat format2;
   456     QGLFormat format2;
   436 
   457 
   437     QVERIFY(format1 == format2);
   458     QVERIFY(format1 == format2);
   508 
   529 
   509     format1.setPlane(8);
   530     format1.setPlane(8);
   510     QVERIFY(!(format1 == format2));
   531     QVERIFY(!(format1 == format2));
   511     QVERIFY(format1 != format2);
   532     QVERIFY(format1 != format2);
   512     format2.setPlane(8);
   533     format2.setPlane(8);
       
   534     QVERIFY(format1 == format2);
       
   535     QVERIFY(!(format1 != format2));
       
   536 
       
   537     format1.setVersion(3, 2);
       
   538     QVERIFY(!(format1 == format2));
       
   539     QVERIFY(format1 != format2);
       
   540     format2.setVersion(3, 2);
       
   541     QVERIFY(format1 == format2);
       
   542     QVERIFY(!(format1 != format2));
       
   543 
       
   544     format1.setProfile(QGLFormat::CoreProfile);
       
   545     QVERIFY(!(format1 == format2));
       
   546     QVERIFY(format1 != format2);
       
   547     format2.setProfile(QGLFormat::CoreProfile);
       
   548     QVERIFY(format1 == format2);
       
   549     QVERIFY(!(format1 != format2));
       
   550 
       
   551     format1.setOption(QGL::NoDeprecatedFunctions);
       
   552     QVERIFY(!(format1 == format2));
       
   553     QVERIFY(format1 != format2);
       
   554     format2.setOption(QGL::NoDeprecatedFunctions);
   513     QVERIFY(format1 == format2);
   555     QVERIFY(format1 == format2);
   514     QVERIFY(!(format1 != format2));
   556     QVERIFY(!(format1 != format2));
   515 
   557 
   516     // Copy constructor and assignment for QGLFormat.
   558     // Copy constructor and assignment for QGLFormat.
   517     QGLFormat format3(format1);
   559     QGLFormat format3(format1);
   701     // However, the complicated parts are in openGLVersionFlags(const QString &versionString)
   743     // However, the complicated parts are in openGLVersionFlags(const QString &versionString)
   702     // tested above
   744     // tested above
   703 
   745 
   704 #if defined(QT_OPENGL_ES_1)
   746 #if defined(QT_OPENGL_ES_1)
   705     QVERIFY(QGLFormat::openGLVersionFlags() & QGLFormat::OpenGL_ES_Common_Version_1_0);
   747     QVERIFY(QGLFormat::openGLVersionFlags() & QGLFormat::OpenGL_ES_Common_Version_1_0);
   706 #elif defined(QT_OPENGL_ES_1_CL)
       
   707     QVERIFY(QGLFormat::openGLVersionFlags() & QGLFormat::OpenGL_ES_CommonLite_Version_1_0);
       
   708 #elif defined(QT_OPENGL_ES_2)
   748 #elif defined(QT_OPENGL_ES_2)
   709     QVERIFY(QGLFormat::openGLVersionFlags() & QGLFormat::OpenGL_ES_Version_2_0);
   749     QVERIFY(QGLFormat::openGLVersionFlags() & QGLFormat::OpenGL_ES_Version_2_0);
   710 #else
   750 #else
   711     QVERIFY(QGLFormat::openGLVersionFlags() & QGLFormat::OpenGL_Version_1_1);
   751     QVERIFY(QGLFormat::openGLVersionFlags() & QGLFormat::OpenGL_Version_1_1);
   712 #endif //defined(QT_OPENGL_ES_1)
   752 #endif //defined(QT_OPENGL_ES_1)
   935 #ifdef Q_WS_X11
   975 #ifdef Q_WS_X11
   936     qt_x11_wait_for_window_manager(w);
   976     qt_x11_wait_for_window_manager(w);
   937 #endif
   977 #endif
   938 
   978 
   939     delete w;
   979     delete w;
       
   980 }
       
   981 
       
   982 
       
   983 void qt_opengl_draw_test_pattern(QPainter* painter, int width, int height)
       
   984 {
       
   985     QPainterPath intersectingPath;
       
   986     intersectingPath.moveTo(0, 0);
       
   987     intersectingPath.lineTo(100, 0);
       
   988     intersectingPath.lineTo(0, 100);
       
   989     intersectingPath.lineTo(100, 100);
       
   990     intersectingPath.closeSubpath();
       
   991 
       
   992     QPainterPath trianglePath;
       
   993     trianglePath.moveTo(50, 0);
       
   994     trianglePath.lineTo(100, 100);
       
   995     trianglePath.lineTo(0, 100);
       
   996     trianglePath.closeSubpath();
       
   997 
       
   998     painter->setTransform(QTransform()); // reset xform
       
   999     painter->fillRect(-1, -1, width+2, height+2, Qt::red); // Background
       
  1000     painter->translate(14, 14);
       
  1001     painter->fillPath(intersectingPath, Qt::blue); // Test stencil buffer works
       
  1002     painter->translate(128, 0);
       
  1003     painter->setClipPath(trianglePath); // Test depth buffer works
       
  1004     painter->setTransform(QTransform()); // reset xform ready for fill
       
  1005     painter->fillRect(-1, -1, width+2, height+2, Qt::green);
       
  1006 }
       
  1007 
       
  1008 void qt_opengl_check_test_pattern(const QImage& img)
       
  1009 {
       
  1010     // As we're doing more than trivial painting, we can't just compare to
       
  1011     // an image rendered with raster. Instead, we sample at well-defined
       
  1012     // test-points:
       
  1013     QFUZZY_COMPARE_PIXELS(img.pixel(39, 64), QColor(Qt::red).rgb());
       
  1014     QFUZZY_COMPARE_PIXELS(img.pixel(89, 64), QColor(Qt::red).rgb());
       
  1015     QFUZZY_COMPARE_PIXELS(img.pixel(64, 39), QColor(Qt::blue).rgb());
       
  1016     QFUZZY_COMPARE_PIXELS(img.pixel(64, 89), QColor(Qt::blue).rgb());
       
  1017 
       
  1018     QFUZZY_COMPARE_PIXELS(img.pixel(167, 39), QColor(Qt::red).rgb());
       
  1019     QFUZZY_COMPARE_PIXELS(img.pixel(217, 39), QColor(Qt::red).rgb());
       
  1020     QFUZZY_COMPARE_PIXELS(img.pixel(192, 64), QColor(Qt::green).rgb());
   940 }
  1021 }
   941 
  1022 
   942 class GLWidget : public QGLWidget
  1023 class GLWidget : public QGLWidget
   943 {
  1024 {
   944 public:
  1025 public:
   951         QPainter p;
  1032         QPainter p;
   952         beginOk = p.begin(this);
  1033         beginOk = p.begin(this);
   953         QPaintEngine* pe = p.paintEngine();
  1034         QPaintEngine* pe = p.paintEngine();
   954         engineType = pe->type();
  1035         engineType = pe->type();
   955 
  1036 
   956         // This test only ensures it's possible to paint onto a QGLWidget. Full
  1037         qt_opengl_draw_test_pattern(&p, width(), height());
   957         // paint engine feature testing is way out of scope!
       
   958         p.fillRect(-1, -1, width()+2, height()+2, Qt::red);
       
   959 
  1038 
   960         // No p.end() or swap buffers, should be done automatically
  1039         // No p.end() or swap buffers, should be done automatically
   961     }
  1040     }
   962 
  1041 
   963 };
  1042 };
   966 {
  1045 {
   967     GLWidget w;
  1046     GLWidget w;
   968 #ifdef Q_WS_QWS
  1047 #ifdef Q_WS_QWS
   969     w.setWindowFlags(Qt::FramelessWindowHint);
  1048     w.setWindowFlags(Qt::FramelessWindowHint);
   970 #endif
  1049 #endif
   971     w.setGeometry(100, 100, 200, 200);
  1050     w.resize(256, 128);
   972     w.show();
  1051     w.show();
   973 
  1052 
   974 #ifdef Q_WS_X11
  1053 #ifdef Q_WS_X11
   975     qt_x11_wait_for_window_manager(&w);
  1054     qt_x11_wait_for_window_manager(&w);
   976 #endif
  1055 #endif
   977     QTest::qWait(200);
  1056     QTest::qWait(200);
   978 
  1057 
   979     QVERIFY(w.beginOk);
  1058     QVERIFY(w.beginOk);
   980     QVERIFY(w.engineType == QPaintEngine::OpenGL || w.engineType == QPaintEngine::OpenGL2);
  1059     QVERIFY(w.engineType == QPaintEngine::OpenGL || w.engineType == QPaintEngine::OpenGL2);
   981 
  1060 
   982     QImage fb = w.grabFrameBuffer(false).convertToFormat(QImage::Format_RGB32);
  1061     QImage fb = w.grabFrameBuffer(false);
   983     QImage reference(fb.size(), QImage::Format_RGB32);
  1062     qt_opengl_check_test_pattern(fb);
   984     reference.fill(0xffff0000);
       
   985 
       
   986     QFUZZY_COMPARE_IMAGES(fb, reference);
       
   987 }
  1063 }
   988 
  1064 
   989 void tst_QGL::glFBOSimpleRendering()
  1065 void tst_QGL::glFBOSimpleRendering()
   990 {
  1066 {
   991     if (!QGLFramebufferObject::hasOpenGLFramebufferObjects())
  1067     if (!QGLFramebufferObject::hasOpenGLFramebufferObjects())
  1042 
  1118 
  1043     QPainter fboPainter;
  1119     QPainter fboPainter;
  1044     bool painterBegun = fboPainter.begin(fbo);
  1120     bool painterBegun = fboPainter.begin(fbo);
  1045     QVERIFY(painterBegun);
  1121     QVERIFY(painterBegun);
  1046 
  1122 
  1047     QPainterPath intersectingPath;
  1123     qt_opengl_draw_test_pattern(&fboPainter, fbo->width(), fbo->height());
  1048     intersectingPath.moveTo(0, 0);
  1124 
  1049     intersectingPath.lineTo(100, 0);
       
  1050     intersectingPath.lineTo(0, 100);
       
  1051     intersectingPath.lineTo(100, 100);
       
  1052     intersectingPath.closeSubpath();
       
  1053 
       
  1054     QPainterPath trianglePath;
       
  1055     trianglePath.moveTo(50, 0);
       
  1056     trianglePath.lineTo(100, 100);
       
  1057     trianglePath.lineTo(0, 100);
       
  1058     trianglePath.closeSubpath();
       
  1059 
       
  1060     fboPainter.fillRect(0, 0, fbo->width(), fbo->height(), Qt::red); // Background
       
  1061     fboPainter.translate(14, 14);
       
  1062     fboPainter.fillPath(intersectingPath, Qt::blue); // Test stencil buffer works
       
  1063     fboPainter.translate(128, 0);
       
  1064     fboPainter.setClipPath(trianglePath); // Test depth buffer works
       
  1065     fboPainter.setTransform(QTransform()); // reset xform
       
  1066     fboPainter.fillRect(0, 0, fbo->width(), fbo->height(), Qt::green);
       
  1067     fboPainter.end();
  1125     fboPainter.end();
  1068 
  1126 
  1069     QImage fb = fbo->toImage().convertToFormat(QImage::Format_RGB32);
  1127     QImage fb = fbo->toImage().convertToFormat(QImage::Format_RGB32);
  1070     delete fbo;
  1128     delete fbo;
  1071 
  1129 
  1072     // As we're doing more than trivial painting, we can't just compare to
  1130     qt_opengl_check_test_pattern(fb);
  1073     // an image rendered with raster. Instead, we sample at well-defined
       
  1074     // test-points:
       
  1075     QFUZZY_COMPARE_PIXELS(fb.pixel(39, 64), QColor(Qt::red).rgb());
       
  1076     QFUZZY_COMPARE_PIXELS(fb.pixel(89, 64), QColor(Qt::red).rgb());
       
  1077     QFUZZY_COMPARE_PIXELS(fb.pixel(64, 39), QColor(Qt::blue).rgb());
       
  1078     QFUZZY_COMPARE_PIXELS(fb.pixel(64, 89), QColor(Qt::blue).rgb());
       
  1079 
       
  1080     QFUZZY_COMPARE_PIXELS(fb.pixel(167, 39), QColor(Qt::red).rgb());
       
  1081     QFUZZY_COMPARE_PIXELS(fb.pixel(217, 39), QColor(Qt::red).rgb());
       
  1082     QFUZZY_COMPARE_PIXELS(fb.pixel(192, 64), QColor(Qt::green).rgb());
       
  1083 }
  1131 }
  1084 
  1132 
  1085 
  1133 
  1086 // Tests multiple QPainters active on different FBOs at the same time, with
  1134 // Tests multiple QPainters active on different FBOs at the same time, with
  1087 // interleaving painting. Performance-wise, this is sub-optimal, but it still
  1135 // interleaving painting. Performance-wise, this is sub-optimal, but it still
  1352 
  1400 
  1353     QImage fb = pm.toImage().convertToFormat(QImage::Format_RGB32);
  1401     QImage fb = pm.toImage().convertToFormat(QImage::Format_RGB32);
  1354     QImage reference(fb.size(), QImage::Format_RGB32);
  1402     QImage reference(fb.size(), QImage::Format_RGB32);
  1355     reference.fill(0xffff0000);
  1403     reference.fill(0xffff0000);
  1356 
  1404 
  1357 #ifdef QGL_EGL
  1405 #if defined(QGL_EGL) && !defined(Q_WS_X11)
  1358     QSKIP("renderPixmap() not yet supported under EGL", SkipAll);
  1406     QSKIP("renderPixmap() not yet supported under EGL on your platform", SkipAll);
  1359 #endif
  1407 #endif
  1360 
  1408 
  1361     QFUZZY_COMPARE_IMAGES(fb, reference);
  1409     QFUZZY_COMPARE_IMAGES(fb, reference);
  1362 }
  1410 }
  1363 
  1411