Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
5
6#include <QByteArray>
7#include <QOpenGLContext>
8
9#include <X11/Xlib.h>
10#include <GL/glx.h>
11
12#include <QtGui/private/qglxconvenience_p.h>
13
14#include <qpa/qplatformsurface.h>
15#include <qsurface.h>
16
18
20{
21public:
26
27 Display *display() const {
28 return (Display *)m_connection->display();
29 }
30
31 Window root() const {
32 return DefaultRootWindow(display());
33 }
34
35 int screenNumber() const {
36 return m_connection->screenNumber();
37 }
38
39private:
40 QOffscreenX11Connection *m_connection;
41};
42
48
50
52{
53 switch (cap) {
54 case OpenGL: return true;
55 case ThreadedOpenGL: return true;
56 case RasterGLSurface: return true;
58 }
59}
60
61#if !defined(QT_NO_OPENGL) && QT_CONFIG(xcb_glx_plugin)
63{
65
66 if (!connection)
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
82
88
90
92{
94 if (resource.toLower() == QByteArrayLiteral("display") ) {
95 if (!m_connection)
97 return m_connection->display();
98 }
99 return nullptr;
100}
101
102#if !defined(QT_NO_OPENGL) && QT_CONFIG(xcb_glx_plugin)
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
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;
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
191QOffscreenX11GLXContext::QOffscreenX11GLXContext(QOffscreenX11Info *x11, QOpenGLContext *context)
192 : d(new QOffscreenX11GLXContextData)
193{
194
195 d->x11 = x11;
196 d->format = context->format();
197
198 if (d->format.renderableType() == QSurfaceFormat::DefaultRenderableType)
199 d->format.setRenderableType(QSurfaceFormat::OpenGL);
200
201 if (d->format.renderableType() != QSurfaceFormat::OpenGL)
202 return;
203
204 d->shareContext = nullptr;
205 if (context->shareHandle())
206 d->shareContext = static_cast<QOffscreenX11GLXContext *>(context->shareHandle())->d->context;
207
208 GLXFBConfig config = qglx_findConfig(x11->display(), x11->screenNumber(), d->format);
209 d->config = config;
210
211 if (config) {
212 d->context = glXCreateNewContext(x11->display(), config, GLX_RGBA_TYPE, d->shareContext, true);
213 if (!d->context && d->shareContext) {
214 d->shareContext = nullptr;
215 // re-try without a shared glx context
216 d->context = glXCreateNewContext(x11->display(), config, GLX_RGBA_TYPE, nullptr, true);
217 }
218
219 // Get the basic surface format details
220 if (d->context)
221 qglx_surfaceFormatFromGLXFBConfig(&d->format, x11->display(), config);
222
223 // Create a temporary window so that we can make the new context current
224 d->window = createDummyWindow(x11, config);
225 } else {
226 XVisualInfo *visualInfo = qglx_findVisualInfo(x11->display(), 0, &d->format);
227 if (Q_UNLIKELY(!visualInfo))
228 qFatal("Could not initialize GLX");
229 d->context = glXCreateContext(x11->display(), visualInfo, d->shareContext, true);
230 if (!d->context && d->shareContext) {
231 // re-try without a shared glx context
232 d->shareContext = nullptr;
233 d->context = glXCreateContext(x11->display(), visualInfo, nullptr, true);
234 }
235
236 d->window = createDummyWindow(x11, visualInfo);
237 XFree(visualInfo);
238 }
239}
240
241QOffscreenX11GLXContext::~QOffscreenX11GLXContext()
242{
243 glXDestroyContext(d->x11->display(), d->context);
244 XDestroyWindow(d->x11->display(), d->window);
245}
246
247bool QOffscreenX11GLXContext::makeCurrent(QPlatformSurface *surface)
248{
249 QSize size = surface->surface()->size();
250
251 XResizeWindow(d->x11->display(), d->window, size.width(), size.height());
252 XSync(d->x11->display(), true);
253
254 if (glXMakeCurrent(d->x11->display(), d->window, d->context)) {
255 glViewport(0, 0, size.width(), size.height());
256 return true;
257 }
258
259 return false;
260}
261
262void QOffscreenX11GLXContext::doneCurrent()
263{
264 glXMakeCurrent(d->x11->display(), 0, nullptr);
265}
266
267void QOffscreenX11GLXContext::swapBuffers(QPlatformSurface *)
268{
269}
270
271QFunctionPointer QOffscreenX11GLXContext::getProcAddress(const char *procName)
272{
273 return (QFunctionPointer)glXGetProcAddressARB(reinterpret_cast<const GLubyte *>(procName));
274}
275
276QSurfaceFormat QOffscreenX11GLXContext::format() const
277{
278 return d->format;
279}
280
281bool QOffscreenX11GLXContext::isSharing() const
282{
283 return d->shareContext;
284}
285
286bool QOffscreenX11GLXContext::isValid() const
287{
288 return d->context && d->window;
289}
290
291GLXContext QOffscreenX11GLXContext::glxContext() const
292{
293 return d->context;
294}
295
296void *QOffscreenX11GLXContext::glxConfig() const
297{
298 return d->config;
299}
300#endif // QT_CONFIG(xcb_glx_plugin)
\inmodule QtCore
Definition qbytearray.h:57
QByteArray toLower() const &
Definition qbytearray.h:254
QScopedPointer< QPlatformNativeInterface > m_nativeInterface
bool hasCapability(QPlatformIntegration::Capability cap) const override
QOffscreenX11Info(QOffscreenX11Connection *connection)
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
QScopedPointer< QOffscreenX11Connection > m_connection
\inmodule QtGui
virtual QPlatformOpenGLContext * createPlatformOpenGLContext(QOpenGLContext *context) const
Factory function for QPlatformOpenGLContext.
Capability
Capabilities are used to determine specific features of a platform integration.
virtual void * nativeResourceForContext(const QByteArray &resource, QOpenGLContext *context)
The QPlatformOpenGLContext class provides an abstraction for native GL contexts.
The QPlatformSurface class provides an abstraction for a surface.
QSurface * surface() const
T * data() const noexcept
Returns the value of the pointer referenced by this object.
void reset(T *other=nullptr) noexcept(noexcept(Cleanup::cleanup(std::declval< T * >())))
Deletes the existing object it is pointing to (if any), and sets its pointer to other.
The QScreen class is used to query screen properties. \inmodule QtGui.
Definition qscreen.h:32
\inmodule QtCore
Definition qsize.h:25
\inmodule QtCore
The QSurfaceFormat class represents the format of a QSurface. \inmodule QtGui.
virtual QSize size() const =0
Returns the size of the surface in pixels.
[Window class with invokable method]
Definition window.h:11
struct wl_display * display
Definition linuxdmabuf.h:41
Combined button and popup list for selecting options.
static void * context
#define QByteArrayLiteral(str)
Definition qbytearray.h:52
static QString displayName(CGDirectDisplayID displayID)
#define Q_UNLIKELY(x)
DBusConnection * connection
EGLConfig config
void qglx_surfaceFormatFromGLXFBConfig(QSurfaceFormat *format, Display *display, GLXFBConfig config, int flags)
GLXFBConfig qglx_findConfig(Display *display, int screen, QSurfaceFormat format, bool highestPixelFormat, int drawableBit, int flags)
XVisualInfo * qglx_findVisualInfo(Display *display, int screen, QSurfaceFormat *format, int drawableBit, int flags)
static Window createDummyWindow(Display *dpy, XVisualInfo *visualInfo, int screenNumber, Window rootWin)
#define qFatal
Definition qlogging.h:168
GLboolean GLboolean GLboolean GLboolean a
[7]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLint GLsizei GLsizei GLenum format
GLenum cap
QScreen * screen
[1]
Definition main.cpp:29
Q_CORE_EXPORT QByteArray qgetenv(const char *varName)
#define Q_UNUSED(x)
struct _XDisplay Display
XID Colormap
QObject::connect nullptr
aWidget window() -> setWindowTitle("New Window Title")
[2]