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 plugins 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 #ifndef QWINDOWSGLCONTEXT_H
41 #define QWINDOWSGLCONTEXT_H
42 
43 #include <QtCore/qt_windows.h>
44 #include "qwindowsopenglcontext.h"
45 
46 #include <QtGui/qopenglcontext.h>
47 
48 #include <vector>
49 
50 QT_BEGIN_NAMESPACE
51 #ifndef QT_NO_OPENGL
52 
53 class QDebug;
54 
55 enum QWindowsGLFormatFlags
56 {
57     QWindowsGLDirectRendering = 0x1,
58     QWindowsGLOverlay = 0x2,
59     QWindowsGLRenderToPixmap = 0x4,
60     QWindowsGLAccumBuffer = 0x8
61 };
62 
63 // Additional format information for Windows.
64 struct QWindowsOpenGLAdditionalFormat
65 {
66     QWindowsOpenGLAdditionalFormat(unsigned formatFlagsIn = 0, unsigned pixmapDepthIn = 0) :
formatFlagsQWindowsOpenGLAdditionalFormat67         formatFlags(formatFlagsIn), pixmapDepth(pixmapDepthIn) { }
68     unsigned formatFlags; // QWindowsGLFormatFlags.
69     unsigned pixmapDepth; // for QWindowsGLRenderToPixmap
70 };
71 
72 // Per-window data for active OpenGL contexts.
73 struct QOpenGLContextData
74 {
QOpenGLContextDataQOpenGLContextData75     QOpenGLContextData(HGLRC r, HWND h, HDC d) : renderingContext(r), hwnd(h), hdc(d) {}
QOpenGLContextDataQOpenGLContextData76     QOpenGLContextData() {}
77 
78     HGLRC renderingContext = nullptr;
79     HWND hwnd = nullptr;
80     HDC hdc = nullptr;
81 };
82 
83 class QOpenGLStaticContext;
84 
85 struct QWindowsOpenGLContextFormat
86 {
87     static QWindowsOpenGLContextFormat current();
88     void apply(QSurfaceFormat *format) const;
89 
90     QSurfaceFormat::OpenGLContextProfile profile = QSurfaceFormat::NoProfile;
91     int version = 0; //! majorVersion<<8 + minorVersion
92     QSurfaceFormat::FormatOptions options;
93 };
94 
95 #ifndef QT_NO_DEBUG_STREAM
96 QDebug operator<<(QDebug d, const PIXELFORMATDESCRIPTOR &);
97 QDebug operator<<(QDebug d, const QWindowsOpenGLContextFormat &);
98 QDebug operator<<(QDebug d, const QOpenGLStaticContext &s);
99 #endif
100 
101 struct QWindowsOpengl32DLL
102 {
103     bool init(bool softwareRendering);
moduleHandleQWindowsOpengl32DLL104     void *moduleHandle() const { return m_lib; }
moduleIsNotOpengl32QWindowsOpengl32DLL105     bool moduleIsNotOpengl32() const { return m_nonOpengl32; }
106 
107     // Wrappers. Always use these instead of SwapBuffers/wglSwapBuffers/etc.
108     BOOL swapBuffers(HDC dc);
109     BOOL setPixelFormat(HDC dc, int pf, const PIXELFORMATDESCRIPTOR *pfd);
110     int describePixelFormat(HDC dc, int pf, UINT size, PIXELFORMATDESCRIPTOR *pfd);
111 
112     // WGL
113     HGLRC (WINAPI * wglCreateContext)(HDC dc);
114     BOOL (WINAPI * wglDeleteContext)(HGLRC context);
115     HGLRC (WINAPI * wglGetCurrentContext)();
116     HDC (WINAPI * wglGetCurrentDC)();
117     PROC (WINAPI * wglGetProcAddress)(LPCSTR name);
118     BOOL (WINAPI * wglMakeCurrent)(HDC dc, HGLRC context);
119     BOOL (WINAPI * wglShareLists)(HGLRC context1, HGLRC context2);
120 
121     // GL1+GLES2 common
122     GLenum (APIENTRY * glGetError)();
123     void (APIENTRY * glGetIntegerv)(GLenum pname, GLint* params);
124     const GLubyte * (APIENTRY * glGetString)(GLenum name);
125 
126     QFunctionPointer resolve(const char *name);
127 private:
128     HMODULE m_lib;
129     bool m_nonOpengl32;
130 
131     // For Mesa llvmpipe shipped with a name other than opengl32.dll
132     BOOL (WINAPI * wglSwapBuffers)(HDC dc);
133     BOOL (WINAPI * wglSetPixelFormat)(HDC dc, int pf, const PIXELFORMATDESCRIPTOR *pfd);
134     int (WINAPI * wglDescribePixelFormat)(HDC dc, int pf, UINT size, PIXELFORMATDESCRIPTOR *pfd);
135 };
136 
137 class QOpenGLStaticContext : public QWindowsStaticOpenGLContext
138 {
139     Q_DISABLE_COPY_MOVE(QOpenGLStaticContext)
140     QOpenGLStaticContext();
141 public:
142     enum Extensions
143     {
144         SampleBuffers = 0x1,
145         sRGBCapableFramebuffer = 0x2,
146         Robustness = 0x4,
147     };
148 
149     typedef bool
150         (APIENTRY *WglGetPixelFormatAttribIVARB)
151             (HDC hdc, int iPixelFormat, int iLayerPlane,
152              uint nAttributes, const int *piAttributes, int *piValues);
153 
154     typedef bool
155         (APIENTRY *WglChoosePixelFormatARB)(HDC hdc, const int *piAttribList,
156             const float *pfAttribFList, uint nMaxFormats, int *piFormats,
157             UINT *nNumFormats);
158 
159     typedef HGLRC
160         (APIENTRY *WglCreateContextAttribsARB)(HDC, HGLRC, const int *);
161 
162     typedef BOOL
163         (APIENTRY *WglSwapInternalExt)(int interval);
164     typedef int
165         (APIENTRY *WglGetSwapInternalExt)(void);
166 
167     typedef const char *
168         (APIENTRY *WglGetExtensionsStringARB)(HDC);
169 
hasExtensions()170     bool hasExtensions() const
171         { return wglGetPixelFormatAttribIVARB && wglChoosePixelFormatARB && wglCreateContextAttribsARB; }
172 
173     static QOpenGLStaticContext *create(bool softwareRendering = false);
174     static QByteArray getGlString(unsigned int which);
175 
176     QWindowsOpenGLContext *createContext(QOpenGLContext *context) override;
moduleHandle()177     void *moduleHandle() const override { return opengl32.moduleHandle(); }
moduleType()178     QOpenGLContext::OpenGLModuleType moduleType() const override
179     { return QOpenGLContext::LibGL; }
180 
181     // For a regular opengl32.dll report the ThreadedOpenGL capability.
182     // For others, which are likely to be software-only, don't.
supportsThreadedOpenGL()183     bool supportsThreadedOpenGL() const override
184     { return !opengl32.moduleIsNotOpengl32(); }
185 
186     const QByteArray vendor;
187     const QByteArray renderer;
188     const QByteArray extensionNames;
189     unsigned extensions;
190     const QWindowsOpenGLContextFormat defaultFormat;
191 
192     WglGetPixelFormatAttribIVARB wglGetPixelFormatAttribIVARB;
193     WglChoosePixelFormatARB wglChoosePixelFormatARB;
194     WglCreateContextAttribsARB wglCreateContextAttribsARB;
195     WglSwapInternalExt wglSwapInternalExt;
196     WglGetSwapInternalExt wglGetSwapInternalExt;
197     WglGetExtensionsStringARB wglGetExtensionsStringARB;
198 
199     static QWindowsOpengl32DLL opengl32;
200 };
201 
202 class QWindowsGLContext : public QWindowsOpenGLContext
203 {
204 public:
205     explicit QWindowsGLContext(QOpenGLStaticContext *staticContext, QOpenGLContext *context);
206     ~QWindowsGLContext() override;
isSharing()207     bool isSharing() const override { return m_context->shareHandle(); }
isValid()208     bool isValid() const override { return m_renderingContext && !m_lost; }
format()209     QSurfaceFormat format() const override { return m_obtainedFormat; }
210 
211     void swapBuffers(QPlatformSurface *surface) override;
212 
213     bool makeCurrent(QPlatformSurface *surface) override;
214     void doneCurrent() override;
215 
216     typedef void (*GL_Proc) ();
217 
218     QFunctionPointer getProcAddress(const char *procName) override;
219 
renderingContext()220     HGLRC renderingContext() const { return m_renderingContext; }
221 
nativeContext()222     void *nativeContext() const override { return m_renderingContext; }
223 
224 private:
225     typedef GLenum (APIENTRY *GlGetGraphicsResetStatusArbType)();
226 
227     inline void releaseDCs();
228     bool updateObtainedParams(HDC hdc, int *obtainedSwapInterval = nullptr);
229 
230     QOpenGLStaticContext *m_staticContext;
231     QOpenGLContext *m_context;
232     QSurfaceFormat m_obtainedFormat;
233     HGLRC m_renderingContext;
234     std::vector<QOpenGLContextData> m_windowContexts;
235     PIXELFORMATDESCRIPTOR m_obtainedPixelFormatDescriptor;
236     int m_pixelFormat;
237     bool m_extensionsUsed;
238     int m_swapInterval;
239     bool m_ownsContext;
240     GlGetGraphicsResetStatusArbType m_getGraphicsResetStatus;
241     bool m_lost;
242 };
243 #endif
244 QT_END_NAMESPACE
245 
246 #endif // QWINDOWSGLCONTEXT_H
247