8#include <QOpenGLContext>
13#include <QtGui/private/qglxconvenience_p.h>
15#include <qpa/qplatformsurface.h>
20class QOffscreenX11Info
23 QOffscreenX11Info(QOffscreenX11Connection *connection)
24 : m_connection(connection)
28 Display *display()
const {
29 return (Display *)m_connection->display();
33 return DefaultRootWindow(display());
36 int screenNumber()
const {
37 return m_connection->screenNumber();
41 QOffscreenX11Connection *m_connection;
55 case OpenGL:
return true;
56 case ThreadedOpenGL:
return true;
57 default:
return QOffscreenIntegration::hasCapability(cap);
61#if !defined(QT_NO_OPENGL) && QT_CONFIG(xcb_glx_plugin)
62QPlatformOpenGLContext *QOffscreenX11Integration::createPlatformOpenGLContext(QOpenGLContext *context)
const
64 auto &connection = nativeInterface()->m_connection;
67 connection.reset(
new QOffscreenX11Connection);
69 if (!connection->display())
72 return new QOffscreenX11GLXContext(connection->x11Info(), context);
78 if (!m_nativeInterface)
79 m_nativeInterface.reset(
new QOffscreenX11PlatformNativeInterface(
const_cast<QOffscreenX11Integration *>(
this)));
80 return static_cast<QOffscreenX11PlatformNativeInterface *>(m_nativeInterface.data());
94 if (resource.toLower() == QByteArrayLiteral(
"display") ) {
96 m_connection.reset(
new QOffscreenX11Connection);
97 return m_connection->display();
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") ) {
106 QOffscreenX11GLXContext *glxPlatformContext =
static_cast<QOffscreenX11GLXContext *>(context->handle());
107 if (glxPlatformContext)
108 return glxPlatformContext->glxConfig();
111 if (resource.toLower() == QByteArrayLiteral(
"glxcontext") ) {
113 QOffscreenX11GLXContext *glxPlatformContext =
static_cast<QOffscreenX11GLXContext *>(context->handle());
114 if (glxPlatformContext)
115 return glxPlatformContext->glxContext();
123Display *QOffscreenX11PlatformNativeInterface::display()
const
125 return m_connection ?
reinterpret_cast<Display *>(m_connection->display()) :
nullptr;
133 QByteArray displayName = qgetenv(
"DISPLAY");
134 Display *display = XOpenDisplay(displayName.constData());
136 m_screenNumber = m_display ? DefaultScreen(m_display) : -1;
142 XCloseDisplay((Display *)m_display);
148 m_x11Info.reset(
new QOffscreenX11Info(
this));
149 return m_x11Info.data();
152#if QT_CONFIG(xcb_glx_plugin)
153class QOffscreenX11GLXContextData
156 QOffscreenX11Info *x11 =
nullptr;
157 QSurfaceFormat format;
158 GLXContext context =
nullptr;
159 GLXContext shareContext =
nullptr;
160 GLXFBConfig config =
nullptr;
164static Window createDummyWindow(QOffscreenX11Info *x11, XVisualInfo *visualInfo)
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());
173 Window window = XCreateWindow(x11->display(), x11->root(),
175 0, visualInfo->depth, InputOutput, visualInfo->visual,
176 CWBackPixel|CWBorderPixel|CWColormap, &a);
177 XFreeColormap(x11->display(), cmap);
181static Window createDummyWindow(QOffscreenX11Info *x11, GLXFBConfig config)
183 XVisualInfo *visualInfo = glXGetVisualFromFBConfig(x11->display(), config);
184 if (Q_UNLIKELY(!visualInfo))
185 qFatal(
"Could not initialize GLX");
186 Window window = createDummyWindow(x11, visualInfo);
191QOffscreenX11GLXContext::QOffscreenX11GLXContext(QOffscreenX11Info *x11, QOpenGLContext *context)
192 : d(
new QOffscreenX11GLXContextData)
196 d->format = context->format();
198 if (d->format.renderableType() == QSurfaceFormat::DefaultRenderableType)
199 d->format.setRenderableType(QSurfaceFormat::OpenGL);
201 if (d->format.renderableType() != QSurfaceFormat::OpenGL)
204 d->shareContext =
nullptr;
205 if (context->shareHandle())
206 d->shareContext =
static_cast<QOffscreenX11GLXContext *>(context->shareHandle())->d->context;
208 GLXFBConfig config = qglx_findConfig(x11->display(), x11->screenNumber(), d->format);
212 d->context = glXCreateNewContext(x11->display(), config, GLX_RGBA_TYPE, d->shareContext,
true);
213 if (!d->context && d->shareContext) {
214 d->shareContext =
nullptr;
216 d->context = glXCreateNewContext(x11->display(), config, GLX_RGBA_TYPE,
nullptr,
true);
221 qglx_surfaceFormatFromGLXFBConfig(&d->format, x11->display(), config);
224 d->window = createDummyWindow(x11, config);
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) {
232 d->shareContext =
nullptr;
233 d->context = glXCreateContext(x11->display(), visualInfo,
nullptr,
true);
236 d->window = createDummyWindow(x11, visualInfo);
241QOffscreenX11GLXContext::~QOffscreenX11GLXContext()
243 glXDestroyContext(d->x11->display(), d->context);
244 XDestroyWindow(d->x11->display(), d->window);
247bool QOffscreenX11GLXContext::makeCurrent(QPlatformSurface *surface)
249 QSize size = surface->surface()->size();
251 XResizeWindow(d->x11->display(), d->window, size.width(), size.height());
252 XSync(d->x11->display(),
true);
254 if (glXMakeCurrent(d->x11->display(), d->window, d->context)) {
255 glViewport(0, 0, size.width(), size.height());
262void QOffscreenX11GLXContext::doneCurrent()
264 glXMakeCurrent(d->x11->display(), 0,
nullptr);
267void QOffscreenX11GLXContext::swapBuffers(QPlatformSurface *)
271QFunctionPointer QOffscreenX11GLXContext::getProcAddress(
const char *procName)
273 return (QFunctionPointer)glXGetProcAddressARB(
reinterpret_cast<
const GLubyte *>(procName));
276QSurfaceFormat QOffscreenX11GLXContext::format()
const
281bool QOffscreenX11GLXContext::isSharing()
const
283 return d->shareContext;
286bool QOffscreenX11GLXContext::isValid()
const
288 return d->context && d->window;
291GLXContext QOffscreenX11GLXContext::glxContext()
const
296void *QOffscreenX11GLXContext::glxConfig()
const
~QOffscreenX11Connection()
QOffscreenX11Connection()
QOffscreenX11Info * x11Info()
bool hasCapability(QPlatformIntegration::Capability cap) const override
QOffscreenX11PlatformNativeInterface * nativeInterface() const override
QOffscreenX11Integration(const QStringList ¶mList)
~QOffscreenX11Integration()