Debian Codegraph @

tests/auto/gui/qopenglconfig/tst_qopenglconfig.cpp

raw
1 // Copyright (C) 2016 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 #include <QtGui/QOpenGLFunctions>
5 #include <QtGui/QScreen>
6 #include <QtGui/QWindow>
7 #include <private/qguiapplication_p.h>
8 #include <qpa/qplatformintegration.h>
9 #include <qpa/qplatformnativeinterface.h>
10 #include <private/qopengl_p.h>
11
12 #include <QTest>
13
14 #include <QtCore/QSysInfo>
15 #include <QtCore/QLibraryInfo>
16 #include <QtCore/QScopedPointer>
17 #include <QtCore/QVariant>
18 #include <QtCore/QDebug>
19 #include <QtCore/QTextStream>
20 #include <QtCore/QJsonDocument>
21
22 #include <algorithm>
23
24 #define DUMP_CAPABILITY(str, integration, capability) \
25 if (platformIntegration->hasCapability(QPlatformIntegration::capability)) \
26 str << ' ' << #capability;
27
28 QTextStream &operator<<(QTextStream &str, const QSize &s)
29 {
30 str << s.width() << 'x' << s.height();
31 return str;
32 }
33
34 QTextStream &operator<<(QTextStream &str, const QRect &r)
35 {
36 str << r.size() << '+' << r.x() << '+' << r.y();
37 return str;
38 }
39
40 QTextStream &operator<<(QTextStream &str, const QSizeF &s)
41 {
42 str << s.width() << 'x' << s.height();
43 return str;
44 }
45
46 QTextStream &operator<<(QTextStream &str, const QSurfaceFormat &format)
47 {
48 str << "Version: " << format.majorVersion() << '.'
49 << format.minorVersion() << " Profile: " << format.profile()
50 << " Swap behavior: " << format.swapBehavior()
51 << " Buffer size (RGB";
52 if (format.hasAlpha())
53 str << 'A';
54 str << "): " << format.redBufferSize() << ',' << format.greenBufferSize()
55 << ',' << format.blueBufferSize();
56 if (format.hasAlpha())
57 str << ',' << format.alphaBufferSize();
58 if (const int dbs = format.depthBufferSize())
59 str << " Depth buffer: " << dbs;
60 if (const int sbs = format.stencilBufferSize())
61 str << " Stencil buffer: " << sbs;
62 const int samples = format.samples();
63 if (samples > 0)
64 str << " Samples: " << samples;
65 return str;
66 }
67
68 /* This test contains code from the qtdiag tool. Its purpose is to output the
69 * graphics configuration to the CI log and to verify that Open GL can be
70 * initialized for platforms on which the qopengl test is marked as
71 * insignificant. */
72
73 class tst_QOpenGlConfig : public QObject
74 {
75 Q_OBJECT
76
77 private slots:
78 void initTestCase();
79 void testConfiguration();
80 void testGlConfiguration();
81 void testBugList();
82 void testDefaultWindowsBlacklist();
83 };
84
85 static void dumpConfiguration(QTextStream &str)
86 {
87 const QPlatformIntegration *platformIntegration = QGuiApplicationPrivate::platformIntegration();
88 str << "\nBuild : " << QLibraryInfo::build()
89 << "\nPlatform : " << QGuiApplication::platformName()
90 << "\nOS : " << QSysInfo::prettyProductName() << " ["
91 << QSysInfo::kernelType() << " version " << QSysInfo::kernelVersion() << ']'
92 << "\nArchitecture : " << QSysInfo::currentCpuArchitecture()
93 << "\nCapabilities :";
94 DUMP_CAPABILITY(str, platformIntegration, ThreadedPixmaps)
95 DUMP_CAPABILITY(str, platformIntegration, OpenGL)
96 DUMP_CAPABILITY(str, platformIntegration, ThreadedOpenGL)
97 DUMP_CAPABILITY(str, platformIntegration, SharedGraphicsCache)
98 DUMP_CAPABILITY(str, platformIntegration, BufferQueueingOpenGL)
99 DUMP_CAPABILITY(str, platformIntegration, WindowMasks)
100 DUMP_CAPABILITY(str, platformIntegration, RasterGLSurface)
101 DUMP_CAPABILITY(str, platformIntegration, AllGLFunctionsQueryable)
102 str << '\n';
103
104 const QList<QScreen*> screens = QGuiApplication::screens();
105 const int screenCount = screens.size();
106 str << "\nScreens: " << screenCount << '\n';
107 for (int s = 0; s < screenCount; ++s) {
108 const QScreen *screen = screens.at(s);
109 str << '#' << ' ' << s << " \"" << screen->name() << '"'
110 << " Depth: " << screen->depth()
111 << " Primary: " << (screen == QGuiApplication::primaryScreen() ? "yes" : "no")
112 << "\n Geometry: " << screen->geometry() << " Available: " << screen->availableGeometry();
113 if (screen->geometry() != screen->virtualGeometry())
114 str << "\n Virtual geometry: " << screen->virtualGeometry() << " Available: " << screen->availableVirtualGeometry();
115 if (screen->virtualSiblings().size() > 1)
116 str << "\n " << screen->virtualSiblings().size() << " virtual siblings";
117 str << "\n Physical size: " << screen->physicalSize() << " mm"
118 << " Refresh: " << screen->refreshRate() << " Hz"
119 << "\n Physical DPI: " << screen->physicalDotsPerInchX()
120 << ',' << screen->physicalDotsPerInchY()
121 << " Logical DPI: " << screen->logicalDotsPerInchX()
122 << ',' << screen->logicalDotsPerInchY()
123 << "\n DevicePixelRatio: " << screen->devicePixelRatio()
124 << " Primary orientation: " << screen->primaryOrientation()
125 << "\n Orientation: " << screen->orientation()
126 << " Native orientation: " << screen->nativeOrientation() << '\n';
127 }
128
129 // On Windows, this will provide addition GPU info similar to the output of dxdiag.
130 if (QGuiApplication::platformNativeInterface()) {
131 const QVariant gpuInfoV = QGuiApplication::platformNativeInterface()->property("gpu");
132 if (gpuInfoV.userType() == QMetaType::QVariantMap) {
133 const QString description = gpuInfoV.toMap().value(QStringLiteral("printable")).toString();
134 if (!description.isEmpty())
135 str << "\nGPU:\n" << description << "\n\n";
136 }
137 }
138 }
139
140 void tst_QOpenGlConfig::initTestCase()
141 {
142 #if !QT_CONFIG(run_opengl_tests)
143 QSKIP("Skip test as run-opengl-tests feature is off.");
144 #endif
145 if (!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::OpenGL))
146 QSKIP("OpenGL is not supported on this platform.");
147 }
148
149 void tst_QOpenGlConfig::testConfiguration()
150 {
151 QString result;
152 QTextStream str(&result);
153 dumpConfiguration(str);
154
155 qDebug().noquote() << '\n' << result;
156 }
157
158 static void dumpGlConfiguration(QOpenGLContext &context, QTextStream &str)
159 {
160 str << "Type : ";
161 #ifdef QT_OPENGL_DYNAMIC
162 str << "Dynamic GL ";
163 #endif
164 switch (context.openGLModuleType()) {
165 case QOpenGLContext::LibGL:
166 str << "LibGL";
167 break;
168 case QOpenGLContext::LibGLES:
169 str << "LibGLES";
170 break;
171 }
172 QOpenGLFunctions functions(&context);
173
174 str << "\nVendor : " << reinterpret_cast<const char *>(functions.glGetString(GL_VENDOR))
175 << "\nRenderer : " << reinterpret_cast<const char *>(functions.glGetString(GL_RENDERER))
176 << "\nVersion : " << reinterpret_cast<const char *>(functions.glGetString(GL_VERSION))
177 << "\nShading language : " << reinterpret_cast<const char *>(functions.glGetString(GL_SHADING_LANGUAGE_VERSION))
178 << "\nFormat : " << context.format();
179
180 QList<QByteArray> extensionList = context.extensions().values();
181 std::sort(extensionList.begin(), extensionList.end());
182 const int extensionCount = extensionList.size();
183 str << "\n\nFound " << extensionCount << " extensions:\n";
184 for (int e = 0; e < extensionCount; ++e)
185 str << ((e % 4) ? ' ' : '\n') << extensionList.at(e);
186 }
187
188 void tst_QOpenGlConfig::testGlConfiguration()
189 {
190 QString result;
191 QTextStream str(&result);
192
193 QWindow window;
194 window.setSurfaceType(QSurface::OpenGLSurface);
195 window.create();
196 QOpenGLContext context;
197 QVERIFY(context.create());
198 QVERIFY(context.makeCurrent(&window));
199 dumpGlConfiguration(context, str);
200 context.doneCurrent();
201
202 qDebug().noquote() << '\n' << result;
203
204 // fromContext either uses the current context or creates a temporary dummy one.
205 QOpenGLConfig::Gpu gpu = QOpenGLConfig::Gpu::fromContext();
206 qDebug().noquote() << '\n' << "GL_VENDOR queried by QOpenGLConfig::Gpu:" << gpu.glVendor;
207 QVERIFY(!gpu.glVendor.isEmpty());
208 }
209
210 static inline QByteArray msgSetMismatch(const QSet<QString> &expected,
211 const QSet<QString> &actual)
212 {
213 const QString result = QStringList(expected.values()).join(QLatin1Char(','))
214 + QLatin1String(" != ")
215 + QStringList(actual.values()).join(QLatin1Char(','));
216 return result.toLatin1();
217 }
218
219 void tst_QOpenGlConfig::testBugList()
220 {
221 // Check bug list parsing for some arbitrary NVidia card
222 // faking Windows OS.
223 const QString fileName = QFINDTESTDATA("buglist.json");
224 QVERIFY(!fileName.isEmpty());
225
226 QSet<QString> expectedFeatures;
227 expectedFeatures << "feature1";
228
229 // adapter info
230 QVersionNumber driverVersion(QList<int>() << 9 << 18 << 13 << 4460);
231 QOpenGLConfig::Gpu gpu = QOpenGLConfig::Gpu::fromDevice(0x10DE, 0x0DE9, driverVersion, QByteArrayLiteral("Unknown"));
232
233 QSet<QString> actualFeatures = QOpenGLConfig::gpuFeatures(gpu, QStringLiteral("win"),
234 QVersionNumber(6, 3), QStringLiteral("7"), fileName);
235 QVERIFY2(expectedFeatures == actualFeatures,
236 msgSetMismatch(expectedFeatures, actualFeatures));
237
238 // driver_description
239 gpu = QOpenGLConfig::Gpu::fromDevice(0xDEAD, 0xBEEF, driverVersion, QByteArrayLiteral("Very Long And Special Driver Description"));
240 actualFeatures = QOpenGLConfig::gpuFeatures(gpu, QStringLiteral("win"),
241 QVersionNumber(6, 3), QStringLiteral("8"), fileName);
242 expectedFeatures = QSet<QString>() << "feature2";
243 QVERIFY2(expectedFeatures == actualFeatures,
244 msgSetMismatch(expectedFeatures, actualFeatures));
245
246 // os.release
247 gpu = QOpenGLConfig::Gpu::fromDevice(0xDEAD, 0xBEEF, driverVersion, QByteArrayLiteral("WinVerTest"));
248 actualFeatures = QOpenGLConfig::gpuFeatures(gpu, QStringLiteral("win"),
249 QVersionNumber(12, 34), QStringLiteral("10"), fileName);
250 expectedFeatures = QSet<QString>() << "win10_feature";
251 QVERIFY2(expectedFeatures == actualFeatures,
252 msgSetMismatch(expectedFeatures, actualFeatures));
253
254 // gl_vendor
255 gpu = QOpenGLConfig::Gpu::fromGLVendor(QByteArrayLiteral("Somebody Else"));
256 expectedFeatures.clear();
257 actualFeatures = QOpenGLConfig::gpuFeatures(gpu, QStringLiteral("linux"),
258 QVersionNumber(1, 0), QString(), fileName);
259 QVERIFY2(expectedFeatures == actualFeatures,
260 msgSetMismatch(expectedFeatures, actualFeatures));
261
262 gpu = QOpenGLConfig::Gpu::fromGLVendor(QByteArrayLiteral("The Qt Company"));
263 expectedFeatures = QSet<QString>() << "cool_feature";
264 actualFeatures = QOpenGLConfig::gpuFeatures(gpu, QStringLiteral("linux"),
265 QVersionNumber(1, 0), QString(), fileName);
266 QVERIFY2(expectedFeatures == actualFeatures,
267 msgSetMismatch(expectedFeatures, actualFeatures));
268 }
269
270 void tst_QOpenGlConfig::testDefaultWindowsBlacklist()
271 {
272 if (QGuiApplication::platformName().compare(QLatin1String("windows"), Qt::CaseInsensitive))
273 QSKIP("Only applicable to Windows");
274
275 QFile f(QStringLiteral(":/qt-project.org/windows/openglblacklists/default.json"));
276 QVERIFY(f.open(QIODevice::ReadOnly | QIODevice::Text));
277 QJsonParseError err;
278 QJsonDocument doc = QJsonDocument::fromJson(f.readAll(), &err);
279 QVERIFY2(err.error == 0,
280 QStringLiteral("Failed to parse built-in Windows GPU blacklist. %1 : %2")
281 .arg(err.offset).arg(err.errorString()).toLatin1());
282 }
283
284 QTEST_MAIN(tst_QOpenGlConfig)
285
286 #include "tst_qopenglconfig.moc"