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 QtGui 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 "qplatformopenglcontext.h"
41 
42 #include <QOpenGLFunctions>
43 
44 QT_BEGIN_NAMESPACE
45 
46 /*!
47     \class QPlatformOpenGLContext
48     \since 4.8
49     \internal
50     \preliminary
51     \ingroup qpa
52 
53     \brief The QPlatformOpenGLContext class provides an abstraction for native GL contexts.
54 
55     In QPA the way to support OpenGL or OpenVG or other technologies that requires a native GL
56     context is through the QPlatformOpenGLContext wrapper.
57 
58     There is no factory function for QPlatformOpenGLContexts, but rather only one accessor function.
59     The only place to retrieve a QPlatformOpenGLContext from is through a QPlatformWindow.
60 
61     The context which is current for a specific thread can be collected by the currentContext()
62     function. This is how QPlatformOpenGLContext also makes it possible to use the Qt GUI module
63     withhout using QOpenGLWidget. When using QOpenGLContext::currentContext(), it will ask
64     QPlatformOpenGLContext for the currentContext. Then a corresponding QOpenGLContext will be returned,
65     which maps to the QPlatformOpenGLContext.
66 */
67 
68 /*! \fn void QPlatformOpenGLContext::swapBuffers(QPlatformSurface *surface)
69     Reimplement in subclass to native swap buffers calls
70 
71     The implementation must support being called in a thread different than the gui-thread.
72 */
73 
74 /*! \fn QFunctionPointer QPlatformOpenGLContext::getProcAddress(const char *procName)
75 
76     Reimplement in subclass to allow dynamic querying of OpenGL symbols. As opposed to e.g. the wglGetProcAddress
77     function on Windows, Qt expects this methods to be able to return valid function pointers even for standard
78     OpenGL symbols.
79 */
80 
81 class QPlatformOpenGLContextPrivate
82 {
83 public:
QPlatformOpenGLContextPrivate()84     QPlatformOpenGLContextPrivate() : context(nullptr) {}
85 
86     QOpenGLContext *context;
87 };
88 
QPlatformOpenGLContext()89 QPlatformOpenGLContext::QPlatformOpenGLContext()
90     : d_ptr(new QPlatformOpenGLContextPrivate)
91 {
92 }
93 
~QPlatformOpenGLContext()94 QPlatformOpenGLContext::~QPlatformOpenGLContext()
95 {
96 }
97 
98 /*!
99   Called after a new instance is constructed. The default implementation does nothing.
100 
101   Subclasses can use this function to perform additional initialization that relies on
102   virtual functions.
103  */
initialize()104 void QPlatformOpenGLContext::initialize()
105 {
106 }
107 
108 /*!
109     Reimplement in subclass if your platform uses framebuffer objects for surfaces.
110 
111     The default implementation returns 0.
112 */
defaultFramebufferObject(QPlatformSurface *) const113 GLuint QPlatformOpenGLContext::defaultFramebufferObject(QPlatformSurface *) const
114 {
115     return 0;
116 }
117 
context() const118 QOpenGLContext *QPlatformOpenGLContext::context() const
119 {
120     Q_D(const QPlatformOpenGLContext);
121     return d->context;
122 }
123 
setContext(QOpenGLContext * context)124 void QPlatformOpenGLContext::setContext(QOpenGLContext *context)
125 {
126     Q_D(QPlatformOpenGLContext);
127     d->context = context;
128 }
129 
parseOpenGLVersion(const QByteArray & versionString,int & major,int & minor)130 bool QPlatformOpenGLContext::parseOpenGLVersion(const QByteArray &versionString, int &major, int &minor)
131 {
132     bool majorOk = false;
133     bool minorOk = false;
134     QList<QByteArray> parts = versionString.split(' ');
135     if (versionString.startsWith(QByteArrayLiteral("OpenGL ES"))) {
136         if (parts.size() >= 3) {
137             QList<QByteArray> versionParts = parts.at(2).split('.');
138             if (versionParts.size() >= 2) {
139                 major = versionParts.at(0).toInt(&majorOk);
140                 minor = versionParts.at(1).toInt(&minorOk);
141                 // Nexus 6 has "OpenGL ES 3.0V@95.0 (GIT@I86da836d38)"
142                 if (!minorOk)
143                     if (int idx = versionParts.at(1).indexOf('V'))
144                         minor = versionParts.at(1).left(idx).toInt(&minorOk);
145             } else {
146                 qWarning("Unrecognized OpenGL ES version");
147             }
148         } else {
149             // If < 3 parts to the name, it is an unrecognised OpenGL ES
150             qWarning("Unrecognised OpenGL ES version");
151         }
152     } else {
153         // Not OpenGL ES, but regular OpenGL, the version numbers are first in the string
154         QList<QByteArray> versionParts = parts.at(0).split('.');
155         if (versionParts.size() >= 2) {
156             major = versionParts.at(0).toInt(&majorOk);
157             minor = versionParts.at(1).toInt(&minorOk);
158         } else {
159             qWarning("Unrecognized OpenGL version");
160         }
161     }
162 
163     if (!majorOk || !minorOk)
164         qWarning("Unrecognized OpenGL version");
165     return (majorOk && minorOk);
166 }
167 
168 QT_END_NAMESPACE
169