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);
191#ifndef GL_CONTEXT_FLAG_DEBUG_BIT
192#define GL_CONTEXT_FLAG_DEBUG_BIT 0x00000002
198static void updateFormatFromContext(QSurfaceFormat &format)
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);
207 format.setProfile(QSurfaceFormat::NoProfile);
208 const bool isStereo = format.testOption(QSurfaceFormat::StereoBuffers);
209 format.setOptions(QSurfaceFormat::FormatOptions());
212 format.setOption(QSurfaceFormat::StereoBuffers);
214 if (format.version() < std::pair(3, 0)) {
215 format.setOption(QSurfaceFormat::DeprecatedFunctions);
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))
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);
236QOffscreenX11GLXContext::QOffscreenX11GLXContext(QOffscreenX11Info *x11, QOpenGLContext *context)
237 : d(
new QOffscreenX11GLXContextData)
241 d->format = context->format();
243 if (d->format.renderableType() == QSurfaceFormat::DefaultRenderableType)
244 d->format.setRenderableType(QSurfaceFormat::OpenGL);
246 if (d->format.renderableType() != QSurfaceFormat::OpenGL)
249 d->shareContext =
nullptr;
250 if (context->shareHandle())
251 d->shareContext =
static_cast<QOffscreenX11GLXContext *>(context->shareHandle())->d->context;
253 GLXFBConfig config = qglx_findConfig(x11->display(), x11->screenNumber(), d->format);
257 d->context = glXCreateNewContext(x11->display(), config, GLX_RGBA_TYPE, d->shareContext,
true);
258 if (!d->context && d->shareContext) {
259 d->shareContext =
nullptr;
261 d->context = glXCreateNewContext(x11->display(), config, GLX_RGBA_TYPE,
nullptr,
true);
266 qglx_surfaceFormatFromGLXFBConfig(&d->format, x11->display(), config);
269 d->window = createDummyWindow(x11, config);
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) {
277 d->shareContext =
nullptr;
278 d->context = glXCreateContext(x11->display(), visualInfo,
nullptr,
true);
281 d->window = createDummyWindow(x11, visualInfo);
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);
296QOffscreenX11GLXContext::~QOffscreenX11GLXContext()
298 glXDestroyContext(d->x11->display(), d->context);
299 XDestroyWindow(d->x11->display(), d->window);
302bool QOffscreenX11GLXContext::makeCurrent(QPlatformSurface *surface)
304 QSize size = surface->surface()->size();
306 XResizeWindow(d->x11->display(), d->window, size.width(), size.height());
307 XSync(d->x11->display(),
true);
309 if (glXMakeCurrent(d->x11->display(), d->window, d->context)) {
310 glViewport(0, 0, size.width(), size.height());
317void QOffscreenX11GLXContext::doneCurrent()
319 glXMakeCurrent(d->x11->display(), 0,
nullptr);
322void QOffscreenX11GLXContext::swapBuffers(QPlatformSurface *)
326QFunctionPointer QOffscreenX11GLXContext::getProcAddress(
const char *procName)
328 return (QFunctionPointer)glXGetProcAddressARB(
reinterpret_cast<
const GLubyte *>(procName));
331QSurfaceFormat QOffscreenX11GLXContext::format()
const
336bool QOffscreenX11GLXContext::isSharing()
const
338 return d->shareContext;
341bool QOffscreenX11GLXContext::isValid()
const
343 return d->context && d->window;
346GLXContext QOffscreenX11GLXContext::glxContext()
const
351void *QOffscreenX11GLXContext::glxConfig()
const
~QOffscreenX11Connection()
QOffscreenX11Connection()
QOffscreenX11Info * x11Info()
bool hasCapability(QPlatformIntegration::Capability cap) const override
QOffscreenX11PlatformNativeInterface * nativeInterface() const override
QOffscreenX11Integration(const QStringList ¶mList)
~QOffscreenX11Integration()