File indexing completed on 2024-11-10 04:55:54

0001 /*
0002     KWin - the KDE window manager
0003     This file is part of the KDE project.
0004 
0005     SPDX-FileCopyrightText: 2016 Martin Gräßlin <mgraesslin@kde.org>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 #include "mock_gl.h"
0010 #include "opengl/glplatform.h"
0011 #include <QTest>
0012 
0013 #include <KConfig>
0014 #include <KConfigGroup>
0015 
0016 Q_DECLARE_METATYPE(KWin::Driver)
0017 Q_DECLARE_METATYPE(KWin::ChipClass)
0018 
0019 using namespace KWin;
0020 
0021 void KWin::cleanupGL()
0022 {
0023     GLPlatform::cleanup();
0024 }
0025 
0026 class GLPlatformTest : public QObject
0027 {
0028     Q_OBJECT
0029 private Q_SLOTS:
0030     void cleanup();
0031 
0032     void testDriverToString_data();
0033     void testDriverToString();
0034     void testChipClassToString_data();
0035     void testChipClassToString();
0036     void testPriorDetect();
0037     void testDetect_data();
0038     void testDetect();
0039 };
0040 
0041 void GLPlatformTest::cleanup()
0042 {
0043     cleanupGL();
0044     delete s_gl;
0045     s_gl = nullptr;
0046 }
0047 
0048 void GLPlatformTest::testDriverToString_data()
0049 {
0050     QTest::addColumn<Driver>("driver");
0051     QTest::addColumn<QString>("expected");
0052 
0053     QTest::newRow("R100") << Driver_R100 << QStringLiteral("Radeon");
0054     QTest::newRow("R200") << Driver_R200 << QStringLiteral("R200");
0055     QTest::newRow("R300C") << Driver_R300C << QStringLiteral("R300C");
0056     QTest::newRow("R300G") << Driver_R300G << QStringLiteral("R300G");
0057     QTest::newRow("R600C") << Driver_R600C << QStringLiteral("R600C");
0058     QTest::newRow("R600G") << Driver_R600G << QStringLiteral("R600G");
0059     QTest::newRow("RadeonSI") << Driver_RadeonSI << QStringLiteral("RadeonSI");
0060     QTest::newRow("Nouveau") << Driver_Nouveau << QStringLiteral("Nouveau");
0061     QTest::newRow("Intel") << Driver_Intel << QStringLiteral("Intel");
0062     QTest::newRow("NVidia") << Driver_NVidia << QStringLiteral("NVIDIA");
0063     QTest::newRow("Catalyst") << Driver_Catalyst << QStringLiteral("Catalyst");
0064     QTest::newRow("Swrast") << Driver_Swrast << QStringLiteral("Software rasterizer");
0065     QTest::newRow("Softpipe") << Driver_Softpipe << QStringLiteral("softpipe");
0066     QTest::newRow("Llvmpipe") << Driver_Llvmpipe << QStringLiteral("LLVMpipe");
0067     QTest::newRow("VirtualBox") << Driver_VirtualBox << QStringLiteral("VirtualBox (Chromium)");
0068     QTest::newRow("VMware") << Driver_VMware << QStringLiteral("VMware (SVGA3D)");
0069     QTest::newRow("Qualcomm") << Driver_Qualcomm << QStringLiteral("Qualcomm");
0070     QTest::newRow("Virgl") << Driver_Virgl << QStringLiteral("Virgl (virtio-gpu, Qemu/KVM guest)");
0071     QTest::newRow("Panfrost") << Driver_Panfrost << QStringLiteral("Panfrost");
0072     QTest::newRow("Lima") << Driver_Lima << QStringLiteral("Mali (Lima)");
0073     QTest::newRow("VC4") << Driver_VC4 << QStringLiteral("VideoCore IV");
0074     QTest::newRow("V3D") << Driver_V3D << QStringLiteral("VideoCore 3D");
0075     QTest::newRow("Unknown") << Driver_Unknown << QStringLiteral("Unknown");
0076 }
0077 
0078 void GLPlatformTest::testDriverToString()
0079 {
0080     QFETCH(Driver, driver);
0081     QTEST(GLPlatform::driverToString(driver), "expected");
0082 }
0083 
0084 void GLPlatformTest::testChipClassToString_data()
0085 {
0086     QTest::addColumn<ChipClass>("chipClass");
0087     QTest::addColumn<QString>("expected");
0088 
0089     QTest::newRow("R100") << R100 << QStringLiteral("R100");
0090     QTest::newRow("R200") << R200 << QStringLiteral("R200");
0091     QTest::newRow("R300") << R300 << QStringLiteral("R300");
0092     QTest::newRow("R400") << R400 << QStringLiteral("R400");
0093     QTest::newRow("R500") << R500 << QStringLiteral("R500");
0094     QTest::newRow("R600") << R600 << QStringLiteral("R600");
0095     QTest::newRow("R700") << R700 << QStringLiteral("R700");
0096     QTest::newRow("Evergreen") << Evergreen << QStringLiteral("EVERGREEN");
0097     QTest::newRow("NorthernIslands") << NorthernIslands << QStringLiteral("Northern Islands");
0098     QTest::newRow("SouthernIslands") << SouthernIslands << QStringLiteral("Southern Islands");
0099     QTest::newRow("SeaIslands") << SeaIslands << QStringLiteral("Sea Islands");
0100     QTest::newRow("VolcanicIslands") << VolcanicIslands << QStringLiteral("Volcanic Islands");
0101     QTest::newRow("Arctic Islands") << ArcticIslands << QStringLiteral("Arctic Islands");
0102     QTest::newRow("Vega") << Vega << QStringLiteral("Vega");
0103     QTest::newRow("UnknownRadeon") << UnknownRadeon << QStringLiteral("Unknown");
0104     QTest::newRow("NV10") << NV10 << QStringLiteral("NV10");
0105     QTest::newRow("NV20") << NV20 << QStringLiteral("NV20");
0106     QTest::newRow("NV30") << NV30 << QStringLiteral("NV30");
0107     QTest::newRow("NV40") << NV40 << QStringLiteral("NV40/G70");
0108     QTest::newRow("G80") << G80 << QStringLiteral("G80/G90");
0109     QTest::newRow("GF100") << GF100 << QStringLiteral("GF100");
0110     QTest::newRow("UnknownNVidia") << UnknownNVidia << QStringLiteral("Unknown");
0111     QTest::newRow("I8XX") << I8XX << QStringLiteral("i830/i835");
0112     QTest::newRow("I915") << I915 << QStringLiteral("i915/i945");
0113     QTest::newRow("I965") << I965 << QStringLiteral("i965");
0114     QTest::newRow("SandyBridge") << SandyBridge << QStringLiteral("SandyBridge");
0115     QTest::newRow("IvyBridge") << IvyBridge << QStringLiteral("IvyBridge");
0116     QTest::newRow("Haswell") << Haswell << QStringLiteral("Haswell");
0117     QTest::newRow("UnknownIntel") << UnknownIntel << QStringLiteral("Unknown");
0118     QTest::newRow("Adreno1XX") << Adreno1XX << QStringLiteral("Adreno 1xx series");
0119     QTest::newRow("Adreno2XX") << Adreno2XX << QStringLiteral("Adreno 2xx series");
0120     QTest::newRow("Adreno3XX") << Adreno3XX << QStringLiteral("Adreno 3xx series");
0121     QTest::newRow("Adreno4XX") << Adreno4XX << QStringLiteral("Adreno 4xx series");
0122     QTest::newRow("Adreno5XX") << Adreno5XX << QStringLiteral("Adreno 5xx series");
0123     QTest::newRow("UnknownAdreno") << UnknownAdreno << QStringLiteral("Unknown");
0124     QTest::newRow("MaliT7XX") << MaliT7XX << QStringLiteral("Mali T7xx series");
0125     QTest::newRow("MaliT8XX") << MaliT8XX << QStringLiteral("Mali T8xx series");
0126     QTest::newRow("MaliGXX") << MaliGXX << QStringLiteral("Mali Gxx series");
0127     QTest::newRow("UnknownPanfrost") << UnknownPanfrost << QStringLiteral("Unknown");
0128     QTest::newRow("Mali400") << Mali400 << QStringLiteral("Mali 400 series");
0129     QTest::newRow("Mali450") << Mali450 << QStringLiteral("Mali 450 series");
0130     QTest::newRow("Mali470") << Mali470 << QStringLiteral("Mali 470 series");
0131     QTest::newRow("UnknownLima") << UnknownLima << QStringLiteral("Unknown");
0132     QTest::newRow("VC4_2_1") << VC4_2_1 << QStringLiteral("VideoCore IV");
0133     QTest::newRow("UnknownVideoCore4") << UnknownVideoCore4 << QStringLiteral("Unknown");
0134     QTest::newRow("V3D_4_2") << V3D_4_2 << QStringLiteral("VideoCore 3D");
0135     QTest::newRow("UnknownVideoCore3D") << UnknownVideoCore3D << QStringLiteral("Unknown");
0136     QTest::newRow("UnknownChipClass") << UnknownChipClass << QStringLiteral("Unknown");
0137 }
0138 
0139 void GLPlatformTest::testChipClassToString()
0140 {
0141     QFETCH(ChipClass, chipClass);
0142     QTEST(GLPlatform::chipClassToString(chipClass), "expected");
0143 }
0144 
0145 void GLPlatformTest::testPriorDetect()
0146 {
0147     auto *gl = GLPlatform::instance();
0148     QVERIFY(gl);
0149     QCOMPARE(gl->supports(GLFeature::LooseBinding), false);
0150 
0151     QCOMPARE(gl->glVersion(), Version());
0152     QCOMPARE(gl->glslVersion(), Version());
0153     QCOMPARE(gl->mesaVersion(), Version());
0154     QCOMPARE(gl->driverVersion(), Version());
0155 
0156     QCOMPARE(gl->driver(), Driver_Unknown);
0157     QCOMPARE(gl->chipClass(), UnknownChipClass);
0158 
0159     QCOMPARE(gl->isMesaDriver(), false);
0160     QCOMPARE(gl->isRadeon(), false);
0161     QCOMPARE(gl->isNvidia(), false);
0162     QCOMPARE(gl->isIntel(), false);
0163     QCOMPARE(gl->isPanfrost(), false);
0164     QCOMPARE(gl->isLima(), false);
0165     QCOMPARE(gl->isVideoCore4(), false);
0166     QCOMPARE(gl->isVideoCore3D(), false);
0167 
0168     QCOMPARE(gl->isVirtualBox(), false);
0169     QCOMPARE(gl->isVMware(), false);
0170 
0171     QCOMPARE(gl->isSoftwareEmulation(), false);
0172     QCOMPARE(gl->isVirtualMachine(), false);
0173 
0174     QCOMPARE(gl->glVersionString(), QByteArray());
0175     QCOMPARE(gl->glRendererString(), QByteArray());
0176     QCOMPARE(gl->glVendorString(), QByteArray());
0177     QCOMPARE(gl->glShadingLanguageVersionString(), QByteArray());
0178 
0179     QCOMPARE(gl->isLooseBinding(), false);
0180     QCOMPARE(gl->isGLES(), false);
0181     QCOMPARE(gl->recommendedCompositor(), QPainterCompositing);
0182     QCOMPARE(gl->preferBufferSubData(), false);
0183     QCOMPARE(gl->platformInterface(), NoOpenGLPlatformInterface);
0184 }
0185 
0186 void GLPlatformTest::testDetect_data()
0187 {
0188     QTest::addColumn<QString>("configFile");
0189 
0190     QDir dir(QFINDTESTDATA("data/glplatform"));
0191     const QStringList entries = dir.entryList(QDir::NoDotAndDotDot | QDir::Files);
0192 
0193     for (const QString &file : entries) {
0194         QTest::newRow(file.toUtf8().constData()) << dir.absoluteFilePath(file);
0195     }
0196 }
0197 
0198 static Version readVersion(const KConfigGroup &group, const char *entry)
0199 {
0200     const QStringList parts = group.readEntry(entry, QString()).split(',');
0201     if (parts.count() < 2) {
0202         return Version();
0203     }
0204     QList<qint64> versionParts;
0205     for (int i = 0; i < parts.count(); ++i) {
0206         bool ok = false;
0207         const auto value = parts.at(i).toLongLong(&ok);
0208         if (ok) {
0209             versionParts << value;
0210         } else {
0211             versionParts << 0;
0212         }
0213     }
0214     while (versionParts.count() < 3) {
0215         versionParts << 0;
0216     }
0217     return Version(versionParts.at(0), versionParts.at(1), versionParts.at(2));
0218 }
0219 
0220 void GLPlatformTest::testDetect()
0221 {
0222     QFETCH(QString, configFile);
0223     KConfig config(configFile);
0224     const KConfigGroup driverGroup = config.group(QStringLiteral("Driver"));
0225     s_gl = new MockGL;
0226     s_gl->getString.vendor = driverGroup.readEntry("Vendor").toUtf8();
0227     s_gl->getString.renderer = driverGroup.readEntry("Renderer").toUtf8();
0228     s_gl->getString.version = driverGroup.readEntry("Version").toUtf8();
0229     s_gl->getString.shadingLanguageVersion = driverGroup.readEntry("ShadingLanguageVersion").toUtf8();
0230     s_gl->getString.extensions = QList<QByteArray>{QByteArrayLiteral("GL_ARB_shader_objects"),
0231                                                    QByteArrayLiteral("GL_ARB_fragment_shader"),
0232                                                    QByteArrayLiteral("GL_ARB_vertex_shader"),
0233                                                    QByteArrayLiteral("GL_ARB_texture_non_power_of_two")};
0234     s_gl->getString.extensionsString = QByteArray();
0235 
0236     auto *gl = GLPlatform::instance();
0237     QVERIFY(gl);
0238     gl->detect(EglPlatformInterface);
0239     QCOMPARE(gl->platformInterface(), EglPlatformInterface);
0240 
0241     const KConfigGroup settingsGroup = config.group(QStringLiteral("Settings"));
0242 
0243     QCOMPARE(gl->supports(GLFeature::LooseBinding), settingsGroup.readEntry("LooseBinding", false));
0244 
0245     QCOMPARE(gl->glVersion(), readVersion(settingsGroup, "GLVersion"));
0246     QCOMPARE(gl->glslVersion(), readVersion(settingsGroup, "GLSLVersion"));
0247     QCOMPARE(gl->mesaVersion(), readVersion(settingsGroup, "MesaVersion"));
0248     QEXPECT_FAIL("amd-catalyst-radeonhd-7700M-3.1.13399", "Detects GL version instead of driver version", Continue);
0249     QCOMPARE(gl->driverVersion(), readVersion(settingsGroup, "DriverVersion"));
0250 
0251     QCOMPARE(gl->driver(), Driver(settingsGroup.readEntry("Driver", int(Driver_Unknown))));
0252     QCOMPARE(gl->chipClass(), ChipClass(settingsGroup.readEntry("ChipClass", int(UnknownChipClass))));
0253 
0254     QCOMPARE(gl->isMesaDriver(), settingsGroup.readEntry("Mesa", false));
0255     QCOMPARE(gl->isRadeon(), settingsGroup.readEntry("Radeon", false));
0256     QCOMPARE(gl->isNvidia(), settingsGroup.readEntry("Nvidia", false));
0257     QCOMPARE(gl->isIntel(), settingsGroup.readEntry("Intel", false));
0258     QCOMPARE(gl->isVirtualBox(), settingsGroup.readEntry("VirtualBox", false));
0259     QCOMPARE(gl->isVMware(), settingsGroup.readEntry("VMware", false));
0260     QCOMPARE(gl->isAdreno(), settingsGroup.readEntry("Adreno", false));
0261     QCOMPARE(gl->isPanfrost(), settingsGroup.readEntry("Panfrost", false));
0262     QCOMPARE(gl->isLima(), settingsGroup.readEntry("Lima", false));
0263     QCOMPARE(gl->isVideoCore4(), settingsGroup.readEntry("VC4", false));
0264     QCOMPARE(gl->isVideoCore3D(), settingsGroup.readEntry("V3D", false));
0265     QCOMPARE(gl->isVirgl(), settingsGroup.readEntry("Virgl", false));
0266 
0267     QCOMPARE(gl->isSoftwareEmulation(), settingsGroup.readEntry("SoftwareEmulation", false));
0268     QCOMPARE(gl->isVirtualMachine(), settingsGroup.readEntry("VirtualMachine", false));
0269 
0270     QCOMPARE(gl->glVersionString(), s_gl->getString.version);
0271     QCOMPARE(gl->glRendererString(), s_gl->getString.renderer);
0272     QCOMPARE(gl->glVendorString(), s_gl->getString.vendor);
0273     QCOMPARE(gl->glShadingLanguageVersionString(), s_gl->getString.shadingLanguageVersion);
0274 
0275     QCOMPARE(gl->isLooseBinding(), settingsGroup.readEntry("LooseBinding", false));
0276     QCOMPARE(gl->isGLES(), settingsGroup.readEntry("GLES", false));
0277     QCOMPARE(gl->recommendedCompositor(), CompositingType(settingsGroup.readEntry("Compositor", int(NoCompositing))));
0278     QCOMPARE(gl->preferBufferSubData(), settingsGroup.readEntry("PreferBufferSubData", false));
0279 }
0280 
0281 QTEST_GUILESS_MAIN(GLPlatformTest)
0282 #include "kwinglplatformtest.moc"