1 | // Copyright (C) 2016 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #ifndef QUTILS_H |
5 | #define QUTILS_H |
6 | |
7 | #include <QtGui/QSurfaceFormat> |
8 | #include <QtGui/QOpenGLContext> |
9 | #include <QtGui/QOpenGLFunctions> |
10 | #include <QtGui/QOffscreenSurface> |
11 | #include <QtCore/QCoreApplication> |
12 | |
13 | QT_BEGIN_NAMESPACE |
14 | |
15 | [[maybe_unused]] |
16 | static inline QSurfaceFormat qDefaultSurfaceFormat(bool antialias) |
17 | { |
18 | bool isES = false; |
19 | |
20 | QSurfaceFormat surfaceFormat; |
21 | |
22 | // Common attributes |
23 | surfaceFormat.setDepthBufferSize(24); |
24 | surfaceFormat.setStencilBufferSize(8); |
25 | surfaceFormat.setSwapBehavior(QSurfaceFormat::DoubleBuffer); |
26 | surfaceFormat.setRenderableType(QSurfaceFormat::DefaultRenderableType); |
27 | |
28 | QOpenGLContext *ctx = QOpenGLContext::currentContext(); |
29 | QOffscreenSurface *dummySurface = nullptr; |
30 | if (!ctx) { |
31 | dummySurface = new QOffscreenSurface(); |
32 | dummySurface->setFormat(surfaceFormat); |
33 | dummySurface->create(); |
34 | ctx = new QOpenGLContext; |
35 | ctx->setFormat(surfaceFormat); |
36 | ctx->create(); |
37 | ctx->makeCurrent(surface: dummySurface); |
38 | } |
39 | |
40 | #if QT_CONFIG(opengles2) |
41 | isES = true; |
42 | #elif (QT_VERSION < QT_VERSION_CHECK(5, 3, 0)) |
43 | isES = false; |
44 | #else |
45 | isES = ctx->isOpenGLES(); |
46 | #endif |
47 | |
48 | #if (QT_VERSION >= QT_VERSION_CHECK(5, 4, 0)) |
49 | // We support only ES2 emulation with software renderer for now |
50 | QString versionStr; |
51 | #ifdef Q_OS_WIN |
52 | const GLubyte *openGLVersion = ctx->functions()->glGetString(GL_VERSION); |
53 | versionStr = QString::fromLatin1(reinterpret_cast<const char *>(openGLVersion)).toLower(); |
54 | #endif |
55 | if (versionStr.contains(QStringLiteral("mesa")) |
56 | || QCoreApplication::testAttribute(attribute: Qt::AA_UseSoftwareOpenGL)) { |
57 | qWarning(msg: "Only OpenGL ES2 emulation is available for software rendering."); |
58 | isES = true; |
59 | } |
60 | #endif |
61 | |
62 | if (dummySurface) { |
63 | ctx->doneCurrent(); |
64 | delete ctx; |
65 | delete dummySurface; |
66 | } |
67 | |
68 | if (isES) { |
69 | // For ES2 only attributes |
70 | surfaceFormat.setRedBufferSize(8); |
71 | surfaceFormat.setBlueBufferSize(8); |
72 | surfaceFormat.setGreenBufferSize(8); |
73 | } else { |
74 | surfaceFormat.setVersion(major: 2, minor: 1); |
75 | surfaceFormat.setProfile(QSurfaceFormat::CoreProfile); |
76 | // For OpenGL only attributes |
77 | if (antialias) |
78 | surfaceFormat.setSamples(8); |
79 | else |
80 | surfaceFormat.setSamples(0); |
81 | } |
82 | |
83 | return surfaceFormat; |
84 | } |
85 | |
86 | QT_END_NAMESPACE |
87 | |
88 | #endif |
89 |