File indexing completed on 2024-12-15 03:45:04

0001 /*
0002     SPDX-FileCopyrightText: 2017 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: MIT
0005 */
0006 
0007 #include "openglinfosource.h"
0008 #include "openglinfosource_p.h"
0009 
0010 #include <QVariant>
0011 #ifndef QT_NO_OPENGL
0012 #include <QOpenGLContext>
0013 #include <QOpenGLFunctions>
0014 #include <QSurfaceFormat>
0015 #include <QWindow>
0016 #include "logging_p.h"
0017 #endif
0018 
0019 using namespace KUserFeedback;
0020 
0021 OpenGLInfoSource::OpenGLInfoSource()
0022     : AbstractDataSource(QStringLiteral("opengl"), Provider::DetailedSystemInformation)
0023 {
0024 }
0025 
0026 QString OpenGLInfoSource::description() const
0027 {
0028     return tr("Information about type, version and vendor of the OpenGL stack.");
0029 }
0030 
0031 QVariant OpenGLInfoSource::data()
0032 {
0033     QVariantMap m;
0034 
0035 #ifndef QT_NO_OPENGL
0036     QOpenGLContext context;
0037     if (context.create()) {
0038         QWindow window;
0039         window.setSurfaceType(QSurface::OpenGLSurface);
0040         window.create();
0041         if (!context.makeCurrent(&window)) {
0042             qCWarning(Log) << "Could not make OpenGL context current";
0043             m.insert(QStringLiteral("type"), QStringLiteral("none"));
0044             return m;
0045         }
0046         QOpenGLFunctions functions(&context);
0047         m.insert(QStringLiteral("vendor"), OpenGLInfoSourcePrivate::normalizeVendor(reinterpret_cast<const char*>(functions.glGetString(GL_VENDOR))));
0048         m.insert(QStringLiteral("renderer"), OpenGLInfoSourcePrivate::normalizeRenderer(reinterpret_cast<const char*>(functions.glGetString(GL_RENDERER))));
0049 
0050         switch (context.openGLModuleType()) {
0051             case QOpenGLContext::LibGL:
0052             {
0053                 m.insert(QStringLiteral("type"), QStringLiteral("GL"));
0054 
0055 #if defined(GL_MAJOR_VERSION) && defined(GL_MINOR_VERSION)
0056                 int major = 0, minor = 0;
0057                 functions.glGetIntegerv(GL_MAJOR_VERSION, &major);
0058                 functions.glGetIntegerv(GL_MINOR_VERSION, &minor);
0059                 // e.g. macOS legacy profiles return 0.0 here...
0060                 if (major > 0)
0061                     m.insert(QStringLiteral("version"), QString(QString::number(major) + QLatin1Char('.') + QString::number(minor)));
0062 #endif
0063 
0064                 OpenGLInfoSourcePrivate::parseGLVersion(reinterpret_cast<const char*>(functions.glGetString(GL_VERSION)), m);
0065                 OpenGLInfoSourcePrivate::parseGLSLVersion(reinterpret_cast<const char*>(functions.glGetString(GL_SHADING_LANGUAGE_VERSION)), m);
0066                 break;
0067             }
0068             case QOpenGLContext::LibGLES:
0069             {
0070                 m.insert(QStringLiteral("type"), QStringLiteral("GLES"));
0071                 OpenGLInfoSourcePrivate::parseGLESVersion(reinterpret_cast<const char*>(functions.glGetString(GL_VERSION)), m);
0072                 OpenGLInfoSourcePrivate::parseESGLSLVersion(reinterpret_cast<const char*>(functions.glGetString(GL_SHADING_LANGUAGE_VERSION)), m);
0073                 break;
0074             }
0075         }
0076 
0077         switch (context.format().profile()) {
0078             case QSurfaceFormat::NoProfile:
0079                 break;
0080             case QSurfaceFormat::CoreProfile:
0081                 m.insert(QStringLiteral("profile"), QStringLiteral("core"));
0082                 break;
0083             case QSurfaceFormat::CompatibilityProfile:
0084                 m.insert(QStringLiteral("profile"), QStringLiteral("compat"));
0085                 break;
0086         }
0087 
0088         return m;
0089     }
0090 #endif
0091 
0092     m.insert(QStringLiteral("type"), QStringLiteral("none"));
0093     return m;
0094 }
0095 
0096 QString OpenGLInfoSource::name() const
0097 {
0098     return tr("OpenGL information");
0099 }