summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/xcb/gl_integrations/xcb_glx/qxcbglxintegration.cpp
blob: 6814dbd844812413446a7ba561a47fc38f8e0218 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the plugins of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qxcbglxintegration.h"

#if QT_CONFIG(xcb_glx)
#include <xcb/glx.h>
#endif

#include "qxcbnativeinterface.h"
#include "qxcbglxwindow.h"
#include "qxcbscreen.h"
#include "qglxintegration.h"

#include <QtGui/QOpenGLContext>

#include "qxcbglxnativeinterfacehandler.h"

#define register        /* C++17 deprecated register */
#include <X11/Xlibint.h>
#undef register

QT_BEGIN_NAMESPACE

#if QT_CONFIG(xcb_glx)
  #if XCB_GLX_MAJOR_VERSION == 1 && XCB_GLX_MINOR_VERSION < 4
    #define XCB_GLX_BUFFER_SWAP_COMPLETE 1
    typedef struct xcb_glx_buffer_swap_complete_event_t {
        uint8_t            response_type;
        uint8_t            pad0;
        uint16_t           sequence;
        uint16_t           event_type;
        uint8_t            pad1[2];
        xcb_glx_drawable_t drawable;
        uint32_t           ust_hi;
        uint32_t           ust_lo;
        uint32_t           msc_hi;
        uint32_t           msc_lo;
        uint32_t           sbc;
    } xcb_glx_buffer_swap_complete_event_t;
  #endif
  typedef struct {
      int type;
      unsigned long serial;       /* # of last request processed by server */
      Bool send_event;            /* true if this came from a SendEvent request */
      Display *display;           /* Display the event was read from */
      Drawable drawable;  /* drawable on which event was requested in event mask */
      int event_type;
      int64_t ust;
      int64_t msc;
      int64_t sbc;
  } QGLXBufferSwapComplete;
#endif

QXcbGlxIntegration::QXcbGlxIntegration()
    : m_connection(nullptr)
    , m_glx_first_event(0)
{
    qCDebug(lcQpaGl) << "Xcb GLX gl-integration created";
}

QXcbGlxIntegration::~QXcbGlxIntegration()
{
}

bool QXcbGlxIntegration::initialize(QXcbConnection *connection)
{
    m_connection = connection;
#if QT_CONFIG(xcb_glx)

    const xcb_query_extension_reply_t *reply = xcb_get_extension_data(m_connection->xcb_connection(), &xcb_glx_id);
    if (!reply || !reply->present)
        return false;

    m_glx_first_event = reply->first_event;

    auto xglx_query = Q_XCB_REPLY(xcb_glx_query_version, m_connection->xcb_connection(),
                                  XCB_GLX_MAJOR_VERSION,
                                  XCB_GLX_MINOR_VERSION);
    if (!xglx_query) {
        qCWarning(lcQpaGl) << "QXcbConnection: Failed to initialize GLX";
        return false;
    }
#endif

    m_native_interface_handler.reset(new QXcbGlxNativeInterfaceHandler(connection->nativeInterface()));

    qCDebug(lcQpaGl) << "Xcb GLX gl-integration successfully initialized";
    return true;
}

bool QXcbGlxIntegration::handleXcbEvent(xcb_generic_event_t *event, uint responseType)
{
    bool handled = false;
    // Check if a custom XEvent constructor was registered in xlib for this event type, and call it discarding the constructed XEvent if any.
    // XESetWireToEvent might be used by libraries to intercept messages from the X server e.g. the OpenGL lib waiting for DRI2 events.
    Display *xdisplay = static_cast<Display *>(m_connection->xlib_display());
    XLockDisplay(xdisplay);
    bool locked = true;
    Bool (*proc)(Display*, XEvent*, xEvent*) = XESetWireToEvent(xdisplay, responseType, nullptr);
    if (proc) {
        XESetWireToEvent(xdisplay, responseType, proc);
        XEvent dummy;
        event->sequence = LastKnownRequestProcessed(xdisplay);
        if (proc(xdisplay, &dummy, (xEvent*)event)) {
#if QT_CONFIG(xcb_glx)
            // DRI2 clients don't receive GLXBufferSwapComplete events on the wire.
            // Instead the GLX event is synthesized from the DRI2BufferSwapComplete event
            // by DRI2WireToEvent(). For an application to be able to see the event
            // we have to convert it to an xcb_glx_buffer_swap_complete_event_t and
            // pass it to the native event filter.
            const uint swap_complete = m_glx_first_event + XCB_GLX_BUFFER_SWAP_COMPLETE;
            QAbstractEventDispatcher* dispatcher = QAbstractEventDispatcher::instance();
            if (dispatcher && uint(dummy.type) == swap_complete && responseType != swap_complete) {
                QGLXBufferSwapComplete *xev = reinterpret_cast<QGLXBufferSwapComplete *>(&dummy);
                xcb_glx_buffer_swap_complete_event_t ev;
                memset(&ev, 0, sizeof(xcb_glx_buffer_swap_complete_event_t));
                ev.response_type = xev->type;
                ev.sequence = xev->serial;
                ev.event_type = xev->event_type;
                ev.drawable = xev->drawable;
                ev.ust_hi = xev->ust >> 32;
                ev.ust_lo = xev->ust & 0xffffffff;
                ev.msc_hi = xev->msc >> 32;
                ev.msc_lo = xev->msc & 0xffffffff;
                ev.sbc = xev->sbc & 0xffffffff;
                // Unlock the display before calling the native event filter
                XUnlockDisplay(xdisplay);
                locked = false;
                auto eventType = m_connection->nativeInterface()->nativeEventType();
#  if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
                qintptr result = 0;
#  else
                long result = 0;
#  endif
                handled = dispatcher->filterNativeEvent(eventType, &ev, &result);
            }
#endif
        }
    }
    if (locked)
        XUnlockDisplay(xdisplay);
    return handled;
}

QXcbWindow *QXcbGlxIntegration::createWindow(QWindow *window) const
{
    return new QXcbGlxWindow(window);
}

QPlatformOpenGLContext *QXcbGlxIntegration::createPlatformOpenGLContext(QOpenGLContext *context) const
{
    QXcbScreen *screen = static_cast<QXcbScreen *>(context->screen()->handle());
    QGLXContext *platformContext = new QGLXContext(screen, screen->surfaceFormatFor(context->format()),
                                                   context->shareHandle(), context->nativeHandle());
    context->setNativeHandle(platformContext->nativeHandle());
    return platformContext;
}

QPlatformOffscreenSurface *QXcbGlxIntegration::createPlatformOffscreenSurface(QOffscreenSurface *surface) const
{
    static bool vendorChecked = false;
    static bool glxPbufferUsable = true;
    if (!vendorChecked) {
        vendorChecked = true;
        Display *display = glXGetCurrentDisplay();
        if (!display)
            display = static_cast<Display *>(m_connection->xlib_display());

        const char *glxvendor = glXGetClientString(display, GLX_VENDOR);
        if (glxvendor) {
            if (!strcmp(glxvendor, "ATI") || !strcmp(glxvendor, "Chromium"))
                glxPbufferUsable = false;
        }
    }
    if (glxPbufferUsable)
        return new QGLXPbuffer(surface);
    else
        return nullptr; // trigger fallback to hidden QWindow

}

bool QXcbGlxIntegration::supportsThreadedOpenGL() const
{
    return QGLXContext::supportsThreading();
}

bool QXcbGlxIntegration::supportsSwitchableWidgetComposition() const
{
    static bool vendorChecked = false;
    static bool isSwitchableWidgetCompositionAvailable = true;
    if (!vendorChecked) {
        vendorChecked = true;
        Display *display = glXGetCurrentDisplay();
        if (!display)
            display = static_cast<Display *>(m_connection->xlib_display());

        const char *glxvendor = glXGetClientString(display, GLX_VENDOR);
        if (glxvendor) {
            if (!strcmp(glxvendor, "Parallels Inc"))
                isSwitchableWidgetCompositionAvailable = false;
        }
    }

    return isSwitchableWidgetCompositionAvailable;
}


QT_END_NAMESPACE