1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Data Visualization module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 or (at your option) any later version
20** approved by the KDE Free Qt Foundation. The licenses are as published by
21** the Free Software Foundation and appearing in the file LICENSE.GPL3
22** included in the packaging of this file. Please review the following
23** information to ensure the GNU General Public License requirements will
24** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25**
26** $QT_END_LICENSE$
27**
28****************************************************************************/
29
30#ifndef QUTILS_H
31#define QUTILS_H
32
33#include <QtGui/QSurfaceFormat>
34#include <QtGui/QOpenGLContext>
35#include <QtGui/QOpenGLFunctions>
36#include <QtGui/QOffscreenSurface>
37#include <QtCore/QCoreApplication>
38
39namespace QtDataVisualization {
40
41#ifndef Q_QDOC
42static inline QSurfaceFormat qDefaultSurfaceFormat(bool antialias = true) Q_DECL_UNUSED;
43#endif
44static inline QSurfaceFormat qDefaultSurfaceFormat(bool antialias)
45{
46 bool isES = false;
47
48 QSurfaceFormat surfaceFormat;
49
50 // Common attributes
51 surfaceFormat.setDepthBufferSize(24);
52 surfaceFormat.setStencilBufferSize(8);
53 surfaceFormat.setSwapBehavior(QSurfaceFormat::DoubleBuffer);
54 surfaceFormat.setRenderableType(QSurfaceFormat::DefaultRenderableType);
55
56 QOpenGLContext *ctx = QOpenGLContext::currentContext();
57 QOffscreenSurface *dummySurface = nullptr;
58 if (!ctx) {
59 dummySurface = new QOffscreenSurface();
60 dummySurface->setFormat(surfaceFormat);
61 dummySurface->create();
62 ctx = new QOpenGLContext;
63 ctx->setFormat(surfaceFormat);
64 ctx->create();
65 ctx->makeCurrent(surface: dummySurface);
66 }
67
68#if defined(QT_OPENGL_ES_2)
69 isES = true;
70#elif (QT_VERSION < QT_VERSION_CHECK(5, 3, 0))
71 isES = false;
72#else
73 isES = ctx->isOpenGLES();
74#endif
75
76#if (QT_VERSION >= QT_VERSION_CHECK(5, 4, 0))
77 // We support only ES2 emulation with software renderer for now
78 QString versionStr;
79#ifdef Q_OS_WIN
80 const GLubyte *openGLVersion = ctx->functions()->glGetString(GL_VERSION);
81 versionStr = QString::fromLatin1(reinterpret_cast<const char *>(openGLVersion)).toLower();
82#endif
83 if (versionStr.contains(QStringLiteral("mesa"))
84 || QCoreApplication::testAttribute(attribute: Qt::AA_UseSoftwareOpenGL)) {
85 qWarning(msg: "Only OpenGL ES2 emulation is available for software rendering.");
86 isES = true;
87 }
88#endif
89
90 if (dummySurface) {
91 ctx->doneCurrent();
92 delete ctx;
93 delete dummySurface;
94 }
95
96 if (isES) {
97 // For ES2 only attributes
98 surfaceFormat.setRedBufferSize(8);
99 surfaceFormat.setBlueBufferSize(8);
100 surfaceFormat.setGreenBufferSize(8);
101 } else {
102 surfaceFormat.setVersion(major: 2, minor: 1);
103 surfaceFormat.setProfile(QSurfaceFormat::CoreProfile);
104 // For OpenGL only attributes
105 if (antialias)
106 surfaceFormat.setSamples(8);
107 else
108 surfaceFormat.setSamples(0);
109 }
110
111 return surfaceFormat;
112}
113
114}
115
116#endif
117

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