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

source code of qtdatavis3d/src/datavisualization/utils/qutils.h