Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qoffscreenintegration_x11.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 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// Qt-Security score:significant reason:default
4
6
7#include <QByteArray>
8#include <QOpenGLContext>
9
10#include <X11/Xlib.h>
11#include <GL/glx.h>
12
13#include <QtGui/private/qglxconvenience_p.h>
14
15#include <qpa/qplatformsurface.h>
16#include <qsurface.h>
17
18QT_BEGIN_NAMESPACE
19
20class QOffscreenX11Info
21{
22public:
23 QOffscreenX11Info(QOffscreenX11Connection *connection)
24 : m_connection(connection)
25 {
26 }
27
28 Display *display() const {
29 return (Display *)m_connection->display();
30 }
31
32 Window root() const {
33 return DefaultRootWindow(display());
34 }
35
36 int screenNumber() const {
37 return m_connection->screenNumber();
38 }
39
40private:
41 QOffscreenX11Connection *m_connection;
42};
43
45: QOffscreenIntegration(paramList)
46{
47
48}
49
51
52bool QOffscreenX11Integration::hasCapability(QPlatformIntegration::Capability cap) const
53{
54 switch (cap) {
55 case OpenGL: return true;
56 case ThreadedOpenGL: return true;
57 default: return QOffscreenIntegration::hasCapability(cap);
58 }
59}
60
61#if !defined(QT_NO_OPENGL) && QT_CONFIG(xcb_glx_plugin)
62QPlatformOpenGLContext *QOffscreenX11Integration::createPlatformOpenGLContext(QOpenGLContext *context) const
63{
64 auto &connection = nativeInterface()->m_connection;
65
66 if (!connection)
67 connection.reset(new QOffscreenX11Connection);
68
69 if (!connection->display())
70 return nullptr;
71
72 return new QOffscreenX11GLXContext(connection->x11Info(), context);
73}
74#endif // !defined(QT_NO_OPENGL) && QT_CONFIG(xcb_glx_plugin)
75
77{
78 if (!m_nativeInterface)
79 m_nativeInterface.reset(new QOffscreenX11PlatformNativeInterface(const_cast<QOffscreenX11Integration *>(this)));
80 return static_cast<QOffscreenX11PlatformNativeInterface *>(m_nativeInterface.data());
81}
82
88
90
91void *QOffscreenX11PlatformNativeInterface::nativeResourceForScreen(const QByteArray &resource, QScreen *screen)
92{
93 Q_UNUSED(screen);
94 if (resource.toLower() == QByteArrayLiteral("display") ) {
95 if (!m_connection)
96 m_connection.reset(new QOffscreenX11Connection);
97 return m_connection->display();
98 }
99 return nullptr;
100}
101
102#if !defined(QT_NO_OPENGL) && QT_CONFIG(xcb_glx_plugin)
103void *QOffscreenX11PlatformNativeInterface::nativeResourceForContext(const QByteArray &resource, QOpenGLContext *context) {
104 if (resource.toLower() == QByteArrayLiteral("glxconfig") ) {
105 if (context) {
106 QOffscreenX11GLXContext *glxPlatformContext = static_cast<QOffscreenX11GLXContext *>(context->handle());
107 if (glxPlatformContext)
108 return glxPlatformContext->glxConfig();
109 }
110 }
111 if (resource.toLower() == QByteArrayLiteral("glxcontext") ) {
112 if (context) {
113 QOffscreenX11GLXContext *glxPlatformContext = static_cast<QOffscreenX11GLXContext *>(context->handle());
114 if (glxPlatformContext)
115 return glxPlatformContext->glxContext();
116 }
117 }
118 return nullptr;
119}
120#endif
121
122#if QT_CONFIG(xcb)
123Display *QOffscreenX11PlatformNativeInterface::display() const
124{
125 return m_connection ? reinterpret_cast<Display *>(m_connection->display()) : nullptr;
126}
127#endif
128
130{
131 XInitThreads();
132
133 QByteArray displayName = qgetenv("DISPLAY");
134 Display *display = XOpenDisplay(displayName.constData());
135 m_display = display;
136 m_screenNumber = m_display ? DefaultScreen(m_display) : -1;
137}
138
140{
141 if (m_display)
142 XCloseDisplay((Display *)m_display);
143}
144
145QOffscreenX11Info *QOffscreenX11Connection::x11Info()
146{
147 if (!m_x11Info)
148 m_x11Info.reset(new QOffscreenX11Info(this));
149 return m_x11Info.data();
150}
151
152#if QT_CONFIG(xcb_glx_plugin)
153class QOffscreenX11GLXContextData
154{
155public:
156 QOffscreenX11Info *x11 = nullptr;
157 QSurfaceFormat format;
158 GLXContext context = nullptr;
159 GLXContext shareContext = nullptr;
160 GLXFBConfig config = nullptr;
161 Window window = 0;
162};
163
164static Window createDummyWindow(QOffscreenX11Info *x11, XVisualInfo *visualInfo)
165{
166 Colormap cmap = XCreateColormap(x11->display(), x11->root(), visualInfo->visual, AllocNone);
167 XSetWindowAttributes a;
168 a.background_pixel = WhitePixel(x11->display(), x11->screenNumber());
169 a.border_pixel = BlackPixel(x11->display(), x11->screenNumber());
170 a.colormap = cmap;
171
172
173 Window window = XCreateWindow(x11->display(), x11->root(),
174 0, 0, 100, 100,
175 0, visualInfo->depth, InputOutput, visualInfo->visual,
176 CWBackPixel|CWBorderPixel|CWColormap, &a);
177 XFreeColormap(x11->display(), cmap);
178 return window;
179}
180
181static Window createDummyWindow(QOffscreenX11Info *x11, GLXFBConfig config)
182{
183 XVisualInfo *visualInfo = glXGetVisualFromFBConfig(x11->display(), config);
184 if (Q_UNLIKELY(!visualInfo))
185 qFatal("Could not initialize GLX");
186 Window window = createDummyWindow(x11, visualInfo);
187 XFree(visualInfo);
188 return window;
189}
190
191#ifndef GL_CONTEXT_FLAG_DEBUG_BIT
192#define GL_CONTEXT_FLAG_DEBUG_BIT 0x00000002
193#endif
194
195// glXCreateNewContext() and glXCreateContext() do not take a version or
196// profile, the implementation typically gives the highest compatibility
197// profile version it supports. Report that, not the requested format.
198static void updateFormatFromContext(QSurfaceFormat &format)
199{
200 int major = 0, minor = 0;
201 const char *versionString = reinterpret_cast<const char *>(glGetString(GL_VERSION));
202 if (versionString && QPlatformOpenGLContext::parseOpenGLVersion(versionString, major, minor)) {
203 format.setMajorVersion(major);
204 format.setMinorVersion(minor);
205 }
206
207 format.setProfile(QSurfaceFormat::NoProfile);
208 const bool isStereo = format.testOption(QSurfaceFormat::StereoBuffers);
209 format.setOptions(QSurfaceFormat::FormatOptions());
210 // Restore flags that come from the VisualInfo/FBConfig.
211 if (isStereo)
212 format.setOption(QSurfaceFormat::StereoBuffers);
213
214 if (format.version() < std::pair(3, 0)) {
215 format.setOption(QSurfaceFormat::DeprecatedFunctions);
216 return;
217 }
218
219 GLint value = 0;
220 glGetIntegerv(GL_CONTEXT_FLAGS, &value);
221 if (!(value & GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT))
222 format.setOption(QSurfaceFormat::DeprecatedFunctions);
223 if (value & GL_CONTEXT_FLAG_DEBUG_BIT)
224 format.setOption(QSurfaceFormat::DebugContext);
225 if (format.version() < std::pair(3, 2))
226 return;
227
228 value = 0;
229 glGetIntegerv(GL_CONTEXT_PROFILE_MASK, &value);
230 if (value & GL_CONTEXT_CORE_PROFILE_BIT)
231 format.setProfile(QSurfaceFormat::CoreProfile);
232 else if (value & GL_CONTEXT_COMPATIBILITY_PROFILE_BIT)
233 format.setProfile(QSurfaceFormat::CompatibilityProfile);
234}
235
236QOffscreenX11GLXContext::QOffscreenX11GLXContext(QOffscreenX11Info *x11, QOpenGLContext *context)
237 : d(new QOffscreenX11GLXContextData)
238{
239
240 d->x11 = x11;
241 d->format = context->format();
242
243 if (d->format.renderableType() == QSurfaceFormat::DefaultRenderableType)
244 d->format.setRenderableType(QSurfaceFormat::OpenGL);
245
246 if (d->format.renderableType() != QSurfaceFormat::OpenGL)
247 return;
248
249 d->shareContext = nullptr;
250 if (context->shareHandle())
251 d->shareContext = static_cast<QOffscreenX11GLXContext *>(context->shareHandle())->d->context;
252
253 GLXFBConfig config = qglx_findConfig(x11->display(), x11->screenNumber(), d->format);
254 d->config = config;
255
256 if (config) {
257 d->context = glXCreateNewContext(x11->display(), config, GLX_RGBA_TYPE, d->shareContext, true);
258 if (!d->context && d->shareContext) {
259 d->shareContext = nullptr;
260 // re-try without a shared glx context
261 d->context = glXCreateNewContext(x11->display(), config, GLX_RGBA_TYPE, nullptr, true);
262 }
263
264 // Get the basic surface format details
265 if (d->context)
266 qglx_surfaceFormatFromGLXFBConfig(&d->format, x11->display(), config);
267
268 // Create a temporary window so that we can make the new context current
269 d->window = createDummyWindow(x11, config);
270 } else {
271 XVisualInfo *visualInfo = qglx_findVisualInfo(x11->display(), 0, &d->format);
272 if (Q_UNLIKELY(!visualInfo))
273 qFatal("Could not initialize GLX");
274 d->context = glXCreateContext(x11->display(), visualInfo, d->shareContext, true);
275 if (!d->context && d->shareContext) {
276 // re-try without a shared glx context
277 d->shareContext = nullptr;
278 d->context = glXCreateContext(x11->display(), visualInfo, nullptr, true);
279 }
280
281 d->window = createDummyWindow(x11, visualInfo);
282 XFree(visualInfo);
283 }
284
285 // Query the OpenGL version and profile
286 if (d->context && d->window) {
287 GLXContext prevContext = glXGetCurrentContext();
288 GLXDrawable prevDrawable = glXGetCurrentDrawable();
289 if (glXMakeCurrent(x11->display(), d->window, d->context)) {
290 updateFormatFromContext(d->format);
291 glXMakeCurrent(x11->display(), prevDrawable, prevContext);
292 }
293 }
294}
295
296QOffscreenX11GLXContext::~QOffscreenX11GLXContext()
297{
298 glXDestroyContext(d->x11->display(), d->context);
299 XDestroyWindow(d->x11->display(), d->window);
300}
301
302bool QOffscreenX11GLXContext::makeCurrent(QPlatformSurface *surface)
303{
304 QSize size = surface->surface()->size();
305
306 XResizeWindow(d->x11->display(), d->window, size.width(), size.height());
307 XSync(d->x11->display(), true);
308
309 if (glXMakeCurrent(d->x11->display(), d->window, d->context)) {
310 glViewport(0, 0, size.width(), size.height());
311 return true;
312 }
313
314 return false;
315}
316
317void QOffscreenX11GLXContext::doneCurrent()
318{
319 glXMakeCurrent(d->x11->display(), 0, nullptr);
320}
321
322void QOffscreenX11GLXContext::swapBuffers(QPlatformSurface *)
323{
324}
325
326QFunctionPointer QOffscreenX11GLXContext::getProcAddress(const char *procName)
327{
328 return (QFunctionPointer)glXGetProcAddressARB(reinterpret_cast<const GLubyte *>(procName));
329}
330
331QSurfaceFormat QOffscreenX11GLXContext::format() const
332{
333 return d->format;
334}
335
336bool QOffscreenX11GLXContext::isSharing() const
337{
338 return d->shareContext;
339}
340
341bool QOffscreenX11GLXContext::isValid() const
342{
343 return d->context && d->window;
344}
345
346GLXContext QOffscreenX11GLXContext::glxContext() const
347{
348 return d->context;
349}
350
351void *QOffscreenX11GLXContext::glxConfig() const
352{
353 return d->config;
354}
355#endif // QT_CONFIG(xcb_glx_plugin)
356QT_END_NAMESPACE
QOffscreenPlatformNativeInterface(QOffscreenIntegration *integration)
bool hasCapability(QPlatformIntegration::Capability cap) const override
QOffscreenX11PlatformNativeInterface * nativeInterface() const override
QOffscreenX11Integration(const QStringList &paramList)
QOffscreenX11PlatformNativeInterface(QOffscreenIntegration *integration)
void * nativeResourceForScreen(const QByteArray &resource, QScreen *screen) override