| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2020 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtOpenGL module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 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 Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "qopenglversionprofile.h" |
| 41 | |
| 42 | #include <QtCore/QDebug> |
| 43 | |
| 44 | QT_BEGIN_NAMESPACE |
| 45 | |
| 46 | class QOpenGLVersionProfilePrivate |
| 47 | { |
| 48 | public: |
| 49 | QOpenGLVersionProfilePrivate() |
| 50 | : majorVersion(0), |
| 51 | minorVersion(0), |
| 52 | profile(QSurfaceFormat::NoProfile) |
| 53 | {} |
| 54 | |
| 55 | int majorVersion; |
| 56 | int minorVersion; |
| 57 | QSurfaceFormat::OpenGLContextProfile profile; |
| 58 | }; |
| 59 | |
| 60 | /*! |
| 61 | \class QOpenGLVersionProfile |
| 62 | \inmodule QtOpenGL |
| 63 | \since 5.1 |
| 64 | \brief The QOpenGLVersionProfile class represents the version and if applicable |
| 65 | the profile of an OpenGL context. |
| 66 | |
| 67 | An object of this class can be passed to QOpenGLContext::versionFunctions() to |
| 68 | request a functions object for a specific version and profile of OpenGL. |
| 69 | |
| 70 | It also contains some helper functions to check if a version supports profiles |
| 71 | or is a legacy version. |
| 72 | */ |
| 73 | |
| 74 | /*! |
| 75 | Creates a default invalid QOpenGLVersionProfile object. |
| 76 | */ |
| 77 | QOpenGLVersionProfile::QOpenGLVersionProfile() |
| 78 | : d(new QOpenGLVersionProfilePrivate) |
| 79 | { |
| 80 | } |
| 81 | |
| 82 | /*! |
| 83 | Creates a QOpenGLVersionProfile object initialised with the version and profile |
| 84 | from \a format. |
| 85 | */ |
| 86 | QOpenGLVersionProfile::QOpenGLVersionProfile(const QSurfaceFormat &format) |
| 87 | : d(new QOpenGLVersionProfilePrivate) |
| 88 | { |
| 89 | d->majorVersion = format.majorVersion(); |
| 90 | d->minorVersion = format.minorVersion(); |
| 91 | d->profile = format.profile(); |
| 92 | } |
| 93 | |
| 94 | /*! |
| 95 | Constructs a copy of \a other. |
| 96 | */ |
| 97 | QOpenGLVersionProfile::QOpenGLVersionProfile(const QOpenGLVersionProfile &other) |
| 98 | : d(new QOpenGLVersionProfilePrivate) |
| 99 | { |
| 100 | *d = *(other.d); |
| 101 | } |
| 102 | |
| 103 | /*! |
| 104 | Destroys the QOpenGLVersionProfile object. |
| 105 | */ |
| 106 | QOpenGLVersionProfile::~QOpenGLVersionProfile() |
| 107 | { |
| 108 | delete d; |
| 109 | } |
| 110 | |
| 111 | /*! |
| 112 | Assigns the version and profile of \a rhs to this QOpenGLVersionProfile object. |
| 113 | */ |
| 114 | QOpenGLVersionProfile &QOpenGLVersionProfile::operator=(const QOpenGLVersionProfile &rhs) |
| 115 | { |
| 116 | if (this == &rhs) |
| 117 | return *this; |
| 118 | *d = *(rhs.d); |
| 119 | return *this; |
| 120 | } |
| 121 | |
| 122 | /*! |
| 123 | Returns a QPair<int,int> where the components represent the major and minor OpenGL |
| 124 | version numbers respectively. |
| 125 | |
| 126 | \sa setVersion() |
| 127 | */ |
| 128 | QPair<int, int> QOpenGLVersionProfile::version() const |
| 129 | { |
| 130 | return qMakePair( d->majorVersion, d->minorVersion); |
| 131 | } |
| 132 | |
| 133 | /*! |
| 134 | Sets the major and minor version numbers to \a majorVersion and \a minorVersion respectively. |
| 135 | |
| 136 | \sa version() |
| 137 | */ |
| 138 | void QOpenGLVersionProfile::setVersion(int majorVersion, int minorVersion) |
| 139 | { |
| 140 | d->majorVersion = majorVersion; |
| 141 | d->minorVersion = minorVersion; |
| 142 | } |
| 143 | |
| 144 | /*! |
| 145 | Returns the OpenGL profile. Only makes sense if profiles are supported by this version. |
| 146 | |
| 147 | \sa setProfile() |
| 148 | */ |
| 149 | QSurfaceFormat::OpenGLContextProfile QOpenGLVersionProfile::profile() const |
| 150 | { |
| 151 | return d->profile; |
| 152 | } |
| 153 | |
| 154 | /*! |
| 155 | Sets the OpenGL profile \a profile. Only makes sense if profiles are supported by |
| 156 | this version. |
| 157 | |
| 158 | \sa profile() |
| 159 | */ |
| 160 | void QOpenGLVersionProfile::setProfile(QSurfaceFormat::OpenGLContextProfile profile) |
| 161 | { |
| 162 | d->profile = profile; |
| 163 | } |
| 164 | |
| 165 | /*! |
| 166 | Returns \c true if profiles are supported by the OpenGL version returned by version(). Only |
| 167 | OpenGL versions >= 3.2 support profiles. |
| 168 | |
| 169 | \sa profile(), version() |
| 170 | */ |
| 171 | bool QOpenGLVersionProfile::hasProfiles() const |
| 172 | { |
| 173 | return ( d->majorVersion > 3 |
| 174 | || (d->majorVersion == 3 && d->minorVersion > 1)); |
| 175 | } |
| 176 | |
| 177 | /*! |
| 178 | Returns \c true is the OpenGL version returned by version() contains deprecated functions |
| 179 | and does not support profiles i.e. if the OpenGL version is <= 3.1. |
| 180 | */ |
| 181 | bool QOpenGLVersionProfile::isLegacyVersion() const |
| 182 | { |
| 183 | return (d->majorVersion < 3 || (d->majorVersion == 3 && d->minorVersion == 0)); |
| 184 | } |
| 185 | |
| 186 | /*! |
| 187 | Returns \c true if the version number is valid. Note that for a default constructed |
| 188 | QOpenGLVersionProfile object this function will return \c false. |
| 189 | |
| 190 | \sa setVersion(), version() |
| 191 | */ |
| 192 | bool QOpenGLVersionProfile::isValid() const |
| 193 | { |
| 194 | return d->majorVersion > 0 && d->minorVersion >= 0; |
| 195 | } |
| 196 | |
| 197 | #ifndef QT_NO_DEBUG_STREAM |
| 198 | QDebug operator<<(QDebug debug, const QOpenGLVersionProfile &vp) |
| 199 | { |
| 200 | QDebugStateSaver saver(debug); |
| 201 | debug.nospace(); |
| 202 | debug << "QOpenGLVersionProfile(" ; |
| 203 | if (vp.isValid()) { |
| 204 | debug << vp.version().first << '.' << vp.version().second |
| 205 | << ", profile=" << vp.profile(); |
| 206 | } else { |
| 207 | debug << "invalid" ; |
| 208 | } |
| 209 | debug << ')'; |
| 210 | return debug; |
| 211 | } |
| 212 | |
| 213 | #endif // QT_NO_DEBUG_STREAM |
| 214 | QT_END_NAMESPACE |
| 215 | |