1 | // Copyright (C) 2020 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "qopenglversionprofile.h" |
5 | |
6 | #include <QtCore/QDebug> |
7 | |
8 | QT_BEGIN_NAMESPACE |
9 | |
10 | class QOpenGLVersionProfilePrivate |
11 | { |
12 | public: |
13 | QOpenGLVersionProfilePrivate() |
14 | : majorVersion(0), |
15 | minorVersion(0), |
16 | profile(QSurfaceFormat::NoProfile) |
17 | {} |
18 | |
19 | int majorVersion; |
20 | int minorVersion; |
21 | QSurfaceFormat::OpenGLContextProfile profile; |
22 | }; |
23 | |
24 | /*! |
25 | \class QOpenGLVersionProfile |
26 | \inmodule QtOpenGL |
27 | \since 5.1 |
28 | \brief The QOpenGLVersionProfile class represents the version and if applicable |
29 | the profile of an OpenGL context. |
30 | |
31 | An object of this class can be passed to QOpenGLContext::versionFunctions() to |
32 | request a functions object for a specific version and profile of OpenGL. |
33 | |
34 | It also contains some helper functions to check if a version supports profiles |
35 | or is a legacy version. |
36 | */ |
37 | |
38 | /*! |
39 | Creates a default invalid QOpenGLVersionProfile object. |
40 | */ |
41 | QOpenGLVersionProfile::QOpenGLVersionProfile() |
42 | : d(new QOpenGLVersionProfilePrivate) |
43 | { |
44 | } |
45 | |
46 | /*! |
47 | Creates a QOpenGLVersionProfile object initialised with the version and profile |
48 | from \a format. |
49 | */ |
50 | QOpenGLVersionProfile::QOpenGLVersionProfile(const QSurfaceFormat &format) |
51 | : d(new QOpenGLVersionProfilePrivate) |
52 | { |
53 | d->majorVersion = format.majorVersion(); |
54 | d->minorVersion = format.minorVersion(); |
55 | d->profile = format.profile(); |
56 | } |
57 | |
58 | /*! |
59 | Constructs a copy of \a other. |
60 | */ |
61 | QOpenGLVersionProfile::QOpenGLVersionProfile(const QOpenGLVersionProfile &other) |
62 | : d(new QOpenGLVersionProfilePrivate) |
63 | { |
64 | *d = *(other.d); |
65 | } |
66 | |
67 | /*! |
68 | Destroys the QOpenGLVersionProfile object. |
69 | */ |
70 | QOpenGLVersionProfile::~QOpenGLVersionProfile() |
71 | { |
72 | delete d; |
73 | } |
74 | |
75 | /*! |
76 | Assigns the version and profile of \a rhs to this QOpenGLVersionProfile object. |
77 | */ |
78 | QOpenGLVersionProfile &QOpenGLVersionProfile::operator=(const QOpenGLVersionProfile &rhs) |
79 | { |
80 | if (this == &rhs) |
81 | return *this; |
82 | *d = *(rhs.d); |
83 | return *this; |
84 | } |
85 | |
86 | /*! |
87 | Returns a QPair<int,int> where the components represent the major and minor OpenGL |
88 | version numbers respectively. |
89 | |
90 | \sa setVersion() |
91 | */ |
92 | QPair<int, int> QOpenGLVersionProfile::version() const |
93 | { |
94 | return qMakePair( value1&: d->majorVersion, value2&: d->minorVersion); |
95 | } |
96 | |
97 | /*! |
98 | Sets the major and minor version numbers to \a majorVersion and \a minorVersion respectively. |
99 | |
100 | \sa version() |
101 | */ |
102 | void QOpenGLVersionProfile::setVersion(int majorVersion, int minorVersion) |
103 | { |
104 | d->majorVersion = majorVersion; |
105 | d->minorVersion = minorVersion; |
106 | } |
107 | |
108 | /*! |
109 | Returns the OpenGL profile. Only makes sense if profiles are supported by this version. |
110 | |
111 | \sa setProfile() |
112 | */ |
113 | QSurfaceFormat::OpenGLContextProfile QOpenGLVersionProfile::profile() const |
114 | { |
115 | return d->profile; |
116 | } |
117 | |
118 | /*! |
119 | Sets the OpenGL profile \a profile. Only makes sense if profiles are supported by |
120 | this version. |
121 | |
122 | \sa profile() |
123 | */ |
124 | void QOpenGLVersionProfile::setProfile(QSurfaceFormat::OpenGLContextProfile profile) |
125 | { |
126 | d->profile = profile; |
127 | } |
128 | |
129 | /*! |
130 | Returns \c true if profiles are supported by the OpenGL version returned by version(). Only |
131 | OpenGL versions >= 3.2 support profiles. |
132 | |
133 | \sa profile(), version() |
134 | */ |
135 | bool QOpenGLVersionProfile::hasProfiles() const |
136 | { |
137 | return ( d->majorVersion > 3 |
138 | || (d->majorVersion == 3 && d->minorVersion > 1)); |
139 | } |
140 | |
141 | /*! |
142 | Returns \c true is the OpenGL version returned by version() contains deprecated functions |
143 | and does not support profiles i.e. if the OpenGL version is <= 3.1. |
144 | */ |
145 | bool QOpenGLVersionProfile::isLegacyVersion() const |
146 | { |
147 | return (d->majorVersion < 3 || (d->majorVersion == 3 && d->minorVersion == 0)); |
148 | } |
149 | |
150 | /*! |
151 | Returns \c true if the version number is valid. Note that for a default constructed |
152 | QOpenGLVersionProfile object this function will return \c false. |
153 | |
154 | \sa setVersion(), version() |
155 | */ |
156 | bool QOpenGLVersionProfile::isValid() const |
157 | { |
158 | return d->majorVersion > 0 && d->minorVersion >= 0; |
159 | } |
160 | |
161 | #ifndef QT_NO_DEBUG_STREAM |
162 | QDebug operator<<(QDebug debug, const QOpenGLVersionProfile &vp) |
163 | { |
164 | QDebugStateSaver saver(debug); |
165 | debug.nospace(); |
166 | debug << "QOpenGLVersionProfile(" ; |
167 | if (vp.isValid()) { |
168 | debug << vp.version().first << '.' << vp.version().second |
169 | << ", profile=" << vp.profile(); |
170 | } else { |
171 | debug << "invalid" ; |
172 | } |
173 | debug << ')'; |
174 | return debug; |
175 | } |
176 | |
177 | #endif // QT_NO_DEBUG_STREAM |
178 | QT_END_NAMESPACE |
179 | |