glxbackend.cpp 31.5 KB
Newer Older
1 2 3 4 5
/********************************************************************
 KWin - the KDE window manager
 This file is part of the KDE project.

Copyright (C) 2006 Lubos Lunak <l.lunak@kde.org>
6
Copyright (C) 2012 Martin Gräßlin <mgraesslin@kde.org>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

Based on glcompmgr code by Felix Bellaby.
Using code from Compiz and Beryl.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/

25 26
// own
#include "glxbackend.h"
27
#include "logging.h"
28
#include "glx_context_attribute_builder.h"
29 30 31
// kwin
#include "options.h"
#include "overlaywindow.h"
32
#include "composite.h"
33
#include "platform.h"
34
#include "scene.h"
35
#include "screens.h"
36
#include "xcbutils.h"
37
#include "texture.h"
38 39
// kwin libs
#include <kwinglplatform.h>
40
#include <kwinglutils.h>
41
#include <kwineffectquickview.h>
42
#include <kwinxrenderutils.h>
Martin Flöser's avatar
Martin Flöser committed
43
// Qt
44
#include <QDebug>
45
#include <QOpenGLContext>
46
#include <QX11Info>
47
#include <QtPlatformHeaders/QGLXNativeContext>
Thomas Lübking's avatar
Thomas Lübking committed
48 49
// system
#include <unistd.h>
50

51 52
#include <deque>
#include <algorithm>
53
#if HAVE_DL_LIBRARY
54
#include <dlfcn.h>
55
#endif
56

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
#ifndef XCB_GLX_BUFFER_SWAP_COMPLETE
#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

74
#include <tuple>
Martin Flöser's avatar
Martin Flöser committed
75
#include <memory>
76

77 78
namespace KWin
{
79

80
SwapEventFilter::SwapEventFilter(xcb_drawable_t drawable, xcb_glx_drawable_t glxDrawable)
81
    : X11EventFilter(Xcb::Extensions::self()->glxEventBase() + XCB_GLX_BUFFER_SWAP_COMPLETE),
82 83
      m_drawable(drawable),
      m_glxDrawable(glxDrawable)
84 85 86 87 88 89 90 91
{
}

bool SwapEventFilter::event(xcb_generic_event_t *event)
{
    xcb_glx_buffer_swap_complete_event_t *ev =
            reinterpret_cast<xcb_glx_buffer_swap_complete_event_t *>(event);

92 93 94 95
    // The drawable field is the X drawable when the event was synthesized
    // by a WireToEvent handler, and the GLX drawable when the event was
    // received over the wire
    if (ev->drawable == m_drawable || ev->drawable == m_glxDrawable) {
96 97 98 99 100 101 102 103 104 105 106 107
        Compositor::self()->bufferSwapComplete();
        return true;
    }

    return false;
}


// -----------------------------------------------------------------------



108
GlxBackend::GlxBackend(Display *display)
109
    : OpenGLBackend()
110
    , m_overlayWindow(kwinApp()->platform()->createOverlayWindow())
111
    , window(None)
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
112
    , fbconfig(nullptr)
113
    , glxWindow(None)
Martin Flöser's avatar
Martin Flöser committed
114
    , ctx(nullptr)
115
    , m_bufferAge(0)
116
    , haveSwapInterval(false)
117
    , m_x11Display(display)
118
{
119 120 121 122 123
     // Ensures calls to glXSwapBuffers will always block until the next
     // retrace when using the proprietary NVIDIA driver. This must be
     // set before libGL.so is loaded.
     setenv("__GL_MaxFramesAllowed", "1", true);

Alexander Volkov's avatar
Alexander Volkov committed
124 125 126 127
     // Force initialization of GLX integration in the Qt's xcb backend
     // to make it call XESetWireToEvent callbacks, which is required
     // by Mesa when using DRI2.
     QOpenGLContext::supportsThreadedOpenGL();
128 129
}

130 131 132
static bool gs_tripleBufferUndetected = true;
static bool gs_tripleBufferNeedsDetection = false;

133 134
GlxBackend::~GlxBackend()
{
135 136 137
    if (isFailed()) {
        m_overlayWindow->destroy();
    }
138 139 140
    // TODO: cleanup in error case
    // do cleanup after initBuffer()
    cleanupGL();
141
    doneCurrent();
142
    EffectQuickView::setShareContext(nullptr);
143

144 145 146
    gs_tripleBufferUndetected = true;
    gs_tripleBufferNeedsDetection = false;

147 148
    if (ctx)
        glXDestroyContext(display(), ctx);
149 150 151 152 153 154 155

    if (glxWindow)
        glXDestroyWindow(display(), glxWindow);

    if (window)
        XDestroyWindow(display(), window);

156 157 158
    qDeleteAll(m_fbconfigHash);
    m_fbconfigHash.clear();

159
    overlayWindow()->destroy();
160
    delete m_overlayWindow;
161 162
}

163 164 165
typedef void (*glXFuncPtr)();

static glXFuncPtr getProcAddress(const char* name)
166
{
167 168 169 170
    glXFuncPtr ret = nullptr;
#if HAVE_EPOXY_GLX
    ret = glXGetProcAddress((const GLubyte*) name);
#endif
171
#if HAVE_DL_LIBRARY
172 173
    if (ret == nullptr)
        ret = (glXFuncPtr) dlsym(RTLD_DEFAULT, name);
174
#endif
175 176 177
    return ret;
}
glXSwapIntervalMESA_func glXSwapIntervalMESA;
178

179 180
void GlxBackend::init()
{
181
    // Require at least GLX 1.3
182
    if (!checkVersion()) {
183
        setFailed(QStringLiteral("Requires at least GLX 1.3"));
184
        return;
185
    }
186

187 188
    initExtensions();

189 190 191 192 193 194 195
    // resolve glXSwapIntervalMESA if available
    if (hasExtension(QByteArrayLiteral("GLX_MESA_swap_control"))) {
        glXSwapIntervalMESA = (glXSwapIntervalMESA_func) getProcAddress("glXSwapIntervalMESA");
    } else {
        glXSwapIntervalMESA = nullptr;
    }

196 197
    initVisualDepthHashTable();

198
    if (!initBuffer()) {
199
        setFailed(QStringLiteral("Could not initialize the buffer"));
200
        return;
201
    }
202

203
    if (!initRenderingContext()) {
204
        setFailed(QStringLiteral("Could not initialize rendering context"));
205 206
        return;
    }
207

208 209
    // Initialize OpenGL
    GLPlatform *glPlatform = GLPlatform::instance();
Martin Flöser's avatar
Martin Flöser committed
210
    glPlatform->detect(GlxPlatformInterface);
211 212 213
    options->setGlPreferBufferSwap(options->glPreferBufferSwap()); // resolve autosetting
    if (options->glPreferBufferSwap() == Options::AutoSwapStrategy)
        options->setGlPreferBufferSwap('e'); // for unknown drivers - should not happen
214
    glPlatform->printResults();
215
    initGL(&getProcAddress);
216

217
    // Check whether certain features are supported
218 219 220
    m_haveMESACopySubBuffer = hasExtension(QByteArrayLiteral("GLX_MESA_copy_sub_buffer"));
    m_haveMESASwapControl   = hasExtension(QByteArrayLiteral("GLX_MESA_swap_control"));
    m_haveEXTSwapControl    = hasExtension(QByteArrayLiteral("GLX_EXT_swap_control"));
221
    m_haveSGISwapControl    = hasExtension(QByteArrayLiteral("GLX_SGI_swap_control"));
222
    // only enable Intel swap event if env variable is set, see BUG 342582
Roman Gilg's avatar
Roman Gilg committed
223 224 225 226
    m_haveINTELSwapEvent    = hasExtension(QByteArrayLiteral("GLX_INTEL_swap_event"))
                                && qgetenv("KWIN_USE_INTEL_SWAP_EVENT") == QByteArrayLiteral("1");

    if (m_haveINTELSwapEvent) {
227
        m_swapEventFilter = std::make_unique<SwapEventFilter>(window, glxWindow);
228 229
        glXSelectEvent(display(), glxWindow, GLX_BUFFER_SWAP_COMPLETE_INTEL_MASK);
    }
230

231
    haveSwapInterval = m_haveMESASwapControl || m_haveEXTSwapControl || m_haveSGISwapControl;
232

233 234
    setSupportsBufferAge(false);

235
    if (hasExtension(QByteArrayLiteral("GLX_EXT_buffer_age"))) {
236 237 238 239 240 241
        const QByteArray useBufferAge = qgetenv("KWIN_USE_BUFFER_AGE");

        if (useBufferAge != "0")
            setSupportsBufferAge(true);
    }

242
    setSyncsToVBlank(false);
243 244
    setBlocksForRetrace(false);
    haveWaitSync = false;
245 246
    gs_tripleBufferNeedsDetection = false;
    m_swapProfiler.init();
247 248 249 250 251
    const bool wantSync = options->glPreferBufferSwap() != Options::NoSwapEncourage;
    if (wantSync && glXIsDirect(display(), ctx)) {
        if (haveSwapInterval) { // glXSwapInterval is preferred being more reliable
            setSwapInterval(1);
            setSyncsToVBlank(true);
252 253 254 255 256 257
            const QByteArray tripleBuffer = qgetenv("KWIN_TRIPLE_BUFFER");
            if (!tripleBuffer.isEmpty()) {
                setBlocksForRetrace(qstrcmp(tripleBuffer, "0") == 0);
                gs_tripleBufferUndetected = false;
            }
            gs_tripleBufferNeedsDetection = gs_tripleBufferUndetected;
258 259 260 261 262 263 264 265 266 267
        } else if (hasExtension(QByteArrayLiteral("GLX_SGI_video_sync"))) {
            unsigned int sync;
            if (glXGetVideoSyncSGI(&sync) == 0 && glXWaitVideoSyncSGI(1, 0, &sync) == 0) {
                setSyncsToVBlank(true);
                setBlocksForRetrace(true);
                haveWaitSync = true;
            } else
                qCWarning(KWIN_X11STANDALONE) << "NO VSYNC! glXSwapInterval is not supported, glXWaitVideoSync is supported but broken";
        } else
            qCWarning(KWIN_X11STANDALONE) << "NO VSYNC! neither glSwapInterval nor glXWaitVideoSync are supported";
268
    } else {
269 270
        // disable v-sync (if possible)
        setSwapInterval(0);
271
    }
272 273 274 275
    if (glPlatform->isVirtualBox()) {
        // VirtualBox does not support glxQueryDrawable
        // this should actually be in kwinglutils_funcs, but QueryDrawable seems not to be provided by an extension
        // and the GLPlatform has not been initialized at the moment when initGLX() is called.
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
276
        glXQueryDrawable = nullptr;
277
    }
278

279
    setIsDirectRendering(bool(glXIsDirect(display(), ctx)));
280

281
    qCDebug(KWIN_X11STANDALONE) << "Direct rendering:" << isDirectRendering();
282
}
283

284 285 286 287 288 289 290
bool GlxBackend::checkVersion()
{
    int major, minor;
    glXQueryVersion(display(), &major, &minor);
    return kVersionNumber(major, minor) >= kVersionNumber(1, 3);
}

291 292 293 294 295 296
void GlxBackend::initExtensions()
{
    const QByteArray string = (const char *) glXQueryExtensionsString(display(), QX11Info::appScreen());
    setExtensions(string.split(' '));
}

297
bool GlxBackend::initRenderingContext()
298
{
299
    const bool direct = true;
300

301
    // Use glXCreateContextAttribsARB() when it's available
302 303
    if (hasExtension(QByteArrayLiteral("GLX_ARB_create_context"))) {
        const bool have_robustness = hasExtension(QByteArrayLiteral("GLX_ARB_create_context_robustness"));
304
        const bool haveVideoMemoryPurge = hasExtension(QByteArrayLiteral("GLX_NV_robustness_video_memory_purge"));
305

306
        std::vector<GlxContextAttributeBuilder> candidates;
307
        if (options->glCoreProfile()) {
308
            if (have_robustness) {
309
                if (haveVideoMemoryPurge) {
310 311 312 313 314
                    GlxContextAttributeBuilder purgeMemoryCore;
                    purgeMemoryCore.setVersion(3, 1);
                    purgeMemoryCore.setRobust(true);
                    purgeMemoryCore.setResetOnVideoMemoryPurge(true);
                    candidates.emplace_back(std::move(purgeMemoryCore));
315
                }
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
                GlxContextAttributeBuilder robustCore;
                robustCore.setVersion(3, 1);
                robustCore.setRobust(true);
                candidates.emplace_back(std::move(robustCore));
            }
            GlxContextAttributeBuilder core;
            core.setVersion(3, 1);
            candidates.emplace_back(std::move(core));
        } else {
            if (have_robustness) {
                if (haveVideoMemoryPurge) {
                    GlxContextAttributeBuilder purgeMemoryLegacy;
                    purgeMemoryLegacy.setRobust(true);
                    purgeMemoryLegacy.setResetOnVideoMemoryPurge(true);
                    candidates.emplace_back(std::move(purgeMemoryLegacy));
331
                }
332 333 334
                GlxContextAttributeBuilder robustLegacy;
                robustLegacy.setRobust(true);
                candidates.emplace_back(std::move(robustLegacy));
335
            }
336 337 338
            GlxContextAttributeBuilder legacy;
            legacy.setVersion(2, 1);
            candidates.emplace_back(std::move(legacy));
339
        }
340 341
        for (auto it = candidates.begin(); it != candidates.end(); it++) {
            const auto attribs = it->build();
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
342
            ctx = glXCreateContextAttribsARB(display(), fbconfig, nullptr, true, attribs.data());
343 344 345
            if (ctx) {
                qCDebug(KWIN_X11STANDALONE) << "Created GLX context with attributes:" << &(*it);
                break;
346 347
            }
        }
348 349 350
    }

    if (!ctx)
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
351
        ctx = glXCreateNewContext(display(), fbconfig, GLX_RGBA_TYPE, nullptr, direct);
352 353

    if (!ctx) {
354
        qCDebug(KWIN_X11STANDALONE) << "Failed to create an OpenGL context.";
355 356 357
        return false;
    }

358
    if (!glXMakeCurrent(display(), glxWindow, ctx)) {
359
        qCDebug(KWIN_X11STANDALONE) << "Failed to make the OpenGL context current.";
360
        glXDestroyContext(display(), ctx);
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
361
        ctx = nullptr;
362
        return false;
363
    }
364

365 366 367 368 369 370
    auto qtContext = new QOpenGLContext;
    QGLXNativeContext native(ctx, display());
    qtContext->setNativeHandle(QVariant::fromValue(native));
    qtContext->create();
    EffectQuickView::setShareContext(std::unique_ptr<QOpenGLContext>(qtContext));

371 372
    return true;
}
373

374
bool GlxBackend::initBuffer()
375
{
376
    if (!initFbConfig())
377
        return false;
378

379
    if (overlayWindow()->create()) {
380 381
        xcb_connection_t * const c = connection();

382
        // Try to create double-buffered window in the overlay
383 384 385
        xcb_visualid_t visual;
        glXGetFBConfigAttrib(display(), fbconfig, GLX_VISUAL_ID, (int *) &visual);

386
        if (!visual) {
387
           qCCritical(KWIN_X11STANDALONE) << "The GLXFBConfig does not have an associated X visual";
388 389
           return false;
        }
390 391 392 393 394 395 396 397 398 399 400

        xcb_colormap_t colormap = xcb_generate_id(c);
        xcb_create_colormap(c, false, colormap, rootWindow(), visual);

        const QSize size = screens()->size();

        window = xcb_generate_id(c);
        xcb_create_window(c, visualDepth(visual), window, overlayWindow()->window(),
                          0, 0, size.width(), size.height(), 0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
                          visual, XCB_CW_COLORMAP, &colormap);

Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
401
        glxWindow = glXCreateWindow(display(), fbconfig, window, nullptr);
402
        overlayWindow()->setup(window);
403
    } else {
404
        qCCritical(KWIN_X11STANDALONE) << "Failed to create overlay window";
405
        return false;
406
    }
407

408
    return true;
409
}
410

411
bool GlxBackend::initFbConfig()
412
{
413
    const int attribs[] = {
Martin Flöser's avatar
Martin Flöser committed
414
        GLX_RENDER_TYPE,    GLX_RGBA_BIT,
415
        GLX_DRAWABLE_TYPE,  GLX_WINDOW_BIT,
416 417 418 419 420 421 422 423 424 425 426
        GLX_RED_SIZE,       1,
        GLX_GREEN_SIZE,     1,
        GLX_BLUE_SIZE,      1,
        GLX_ALPHA_SIZE,     0,
        GLX_DEPTH_SIZE,     0,
        GLX_STENCIL_SIZE,   0,
        GLX_CONFIG_CAVEAT,  GLX_NONE,
        GLX_DOUBLEBUFFER,   true,
        0
    };

427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
    const int attribs_srgb[] = {
        GLX_RENDER_TYPE,                  GLX_RGBA_BIT,
        GLX_DRAWABLE_TYPE,                GLX_WINDOW_BIT,
        GLX_RED_SIZE,                     1,
        GLX_GREEN_SIZE,                   1,
        GLX_BLUE_SIZE,                    1,
        GLX_ALPHA_SIZE,                   0,
        GLX_DEPTH_SIZE,                   0,
        GLX_STENCIL_SIZE,                 0,
        GLX_CONFIG_CAVEAT,                GLX_NONE,
        GLX_DOUBLEBUFFER,                 true,
        GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB, true,
        0
    };

442 443 444 445 446 447 448 449 450 451
    bool llvmpipe = false;

    // Note that we cannot use GLPlatform::driver() here, because it has not been initialized at this point
    if (hasExtension(QByteArrayLiteral("GLX_MESA_query_renderer"))) {
        const QByteArray device = glXQueryRendererStringMESA(display(), DefaultScreen(display()), 0, GLX_RENDERER_DEVICE_ID_MESA);
        if (device.contains(QByteArrayLiteral("llvmpipe"))) {
            llvmpipe = true;
        }
    }

452
    // Try to find a double buffered sRGB capable configuration
453
    int count = 0;
454 455 456 457 458 459
    GLXFBConfig *configs = nullptr;

    // Don't request an sRGB configuration with LLVMpipe when the default depth is 16. See bug #408594.
    if (!llvmpipe || Xcb::defaultDepth() > 16) {
        configs = glXChooseFBConfig(display(), DefaultScreen(display()), attribs_srgb, &count);
    }
460 461 462 463 464

    if (count == 0) {
        // Try to find a double buffered non-sRGB capable configuration
        configs = glXChooseFBConfig(display(), DefaultScreen(display()), attribs, &count);
    }
465

466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
    struct FBConfig {
        GLXFBConfig config;
        int depth;
        int stencil;
    };

    std::deque<FBConfig> candidates;

    for (int i = 0; i < count; i++) {
        int depth, stencil;
        glXGetFBConfigAttrib(display(), configs[i], GLX_DEPTH_SIZE,   &depth);
        glXGetFBConfigAttrib(display(), configs[i], GLX_STENCIL_SIZE, &stencil);

        candidates.emplace_back(FBConfig{configs[i], depth, stencil});
    }

    if (count > 0)
483
        XFree(configs);
484 485 486 487 488 489 490 491 492 493 494 495 496

    std::stable_sort(candidates.begin(), candidates.end(), [](const FBConfig &left, const FBConfig &right) {
        if (left.depth < right.depth)
            return true;

        if (left.stencil < right.stencil)
            return true;

        return false;
    });

    if (candidates.size() > 0) {
        fbconfig = candidates.front().config;
497

498
        int fbconfig_id, visual_id, red, green, blue, alpha, depth, stencil, srgb;
499 500 501 502 503 504 505 506
        glXGetFBConfigAttrib(display(), fbconfig, GLX_FBCONFIG_ID,  &fbconfig_id);
        glXGetFBConfigAttrib(display(), fbconfig, GLX_VISUAL_ID,    &visual_id);
        glXGetFBConfigAttrib(display(), fbconfig, GLX_RED_SIZE,     &red);
        glXGetFBConfigAttrib(display(), fbconfig, GLX_GREEN_SIZE,   &green);
        glXGetFBConfigAttrib(display(), fbconfig, GLX_BLUE_SIZE,    &blue);
        glXGetFBConfigAttrib(display(), fbconfig, GLX_ALPHA_SIZE,   &alpha);
        glXGetFBConfigAttrib(display(), fbconfig, GLX_DEPTH_SIZE,   &depth);
        glXGetFBConfigAttrib(display(), fbconfig, GLX_STENCIL_SIZE, &stencil);
507
        glXGetFBConfigAttrib(display(), fbconfig, GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB, &srgb);
508

509 510
        qCDebug(KWIN_X11STANDALONE, "Choosing GLXFBConfig %#x X visual %#x depth %d RGBA %d:%d:%d:%d ZS %d:%d sRGB: %d",
                fbconfig_id, visual_id, visualDepth(visual_id), red, green, blue, alpha, depth, stencil, srgb);
511
    }
512

513
    if (fbconfig == nullptr) {
514
        qCCritical(KWIN_X11STANDALONE) << "Failed to find a usable framebuffer configuration";
515
        return false;
516
    }
517

518 519
    return true;
}
520

521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
void GlxBackend::initVisualDepthHashTable()
{
    const xcb_setup_t *setup = xcb_get_setup(connection());

    for (auto screen = xcb_setup_roots_iterator(setup); screen.rem; xcb_screen_next(&screen)) {
        for (auto depth = xcb_screen_allowed_depths_iterator(screen.data); depth.rem; xcb_depth_next(&depth)) {
            const int len = xcb_depth_visuals_length(depth.data);
            const xcb_visualtype_t *visuals = xcb_depth_visuals(depth.data);

            for (int i = 0; i < len; i++)
                m_visualDepthHash.insert(visuals[i].visual_id, depth.data->depth);
        }
    }
}

int GlxBackend::visualDepth(xcb_visualid_t visual) const
{
    return m_visualDepthHash.value(visual);
}

541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
static inline int bitCount(uint32_t mask)
{
#if defined(__GNUC__)
    return __builtin_popcount(mask);
#else
    int count = 0;

    while (mask) {
        count += (mask & 1);
        mask >>= 1;
    }

    return count;
#endif
}

557 558
FBConfigInfo *GlxBackend::infoForVisual(xcb_visualid_t visual)
{
559 560 561 562
    auto it = m_fbconfigHash.constFind(visual);
    if (it != m_fbconfigHash.constEnd()) {
        return it.value();
    }
563

564 565
    FBConfigInfo *info = new FBConfigInfo;
    m_fbconfigHash.insert(visual, info);
566 567 568 569 570 571 572 573 574 575
    info->fbconfig            = nullptr;
    info->bind_texture_format = 0;
    info->texture_targets     = 0;
    info->y_inverted          = 0;
    info->mipmap              = 0;

    const xcb_render_pictformat_t format = XRenderUtils::findPictFormat(visual);
    const xcb_render_directformat_t *direct = XRenderUtils::findPictFormatInfo(format);

    if (!direct) {
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
576
        qCCritical(KWIN_X11STANDALONE).nospace() << "Could not find a picture format for visual 0x" << Qt::hex << visual;
577 578 579 580 581 582 583 584
        return info;
    }

    const int red_bits   = bitCount(direct->red_mask);
    const int green_bits = bitCount(direct->green_mask);
    const int blue_bits  = bitCount(direct->blue_mask);
    const int alpha_bits = bitCount(direct->alpha_mask);

585 586
    const int depth = visualDepth(visual);

587 588 589 590 591 592 593 594
    const auto rgb_sizes = std::tie(red_bits, green_bits, blue_bits);

    const int attribs[] = {
        GLX_RENDER_TYPE,    GLX_RGBA_BIT,
        GLX_DRAWABLE_TYPE,  GLX_WINDOW_BIT | GLX_PIXMAP_BIT,
        GLX_X_VISUAL_TYPE,  GLX_TRUE_COLOR,
        GLX_X_RENDERABLE,   True,
        GLX_CONFIG_CAVEAT,  int(GLX_DONT_CARE), // The ARGB32 visual is marked non-conformant in Catalyst
595
        GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT,  int(GLX_DONT_CARE), // The ARGB32 visual is marked sRGB capable in mesa/i965
596 597 598 599 600 601 602 603 604 605 606 607 608 609
        GLX_BUFFER_SIZE,    red_bits + green_bits + blue_bits + alpha_bits,
        GLX_RED_SIZE,       red_bits,
        GLX_GREEN_SIZE,     green_bits,
        GLX_BLUE_SIZE,      blue_bits,
        GLX_ALPHA_SIZE,     alpha_bits,
        GLX_STENCIL_SIZE,   0,
        GLX_DEPTH_SIZE,     0,
        0
    };

    int count = 0;
    GLXFBConfig *configs = glXChooseFBConfig(display(), DefaultScreen(display()), attribs, &count);

    if (count < 1) {
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
610
        qCCritical(KWIN_X11STANDALONE).nospace() << "Could not find a framebuffer configuration for visual 0x" << Qt::hex << visual;
611 612 613
        return info;
    }

614 615 616 617 618 619 620 621 622
    struct FBConfig {
        GLXFBConfig config;
        int depth;
        int stencil;
        int format;
    };

    std::deque<FBConfig> candidates;

623 624 625 626 627 628 629 630 631
    for (int i = 0; i < count; i++) {
        int red, green, blue;
        glXGetFBConfigAttrib(display(), configs[i], GLX_RED_SIZE,   &red);
        glXGetFBConfigAttrib(display(), configs[i], GLX_GREEN_SIZE, &green);
        glXGetFBConfigAttrib(display(), configs[i], GLX_BLUE_SIZE,  &blue);

        if (std::tie(red, green, blue) != rgb_sizes)
            continue;

632 633 634 635 636 637
        xcb_visualid_t visual;
        glXGetFBConfigAttrib(display(), configs[i], GLX_VISUAL_ID, (int *) &visual);

        if (visualDepth(visual) != depth)
            continue;

638 639 640 641 642 643 644
        int bind_rgb, bind_rgba;
        glXGetFBConfigAttrib(display(), configs[i], GLX_BIND_TO_TEXTURE_RGBA_EXT, &bind_rgba);
        glXGetFBConfigAttrib(display(), configs[i], GLX_BIND_TO_TEXTURE_RGB_EXT,  &bind_rgb);

        if (!bind_rgb && !bind_rgba)
            continue;

645 646 647 648
        int depth, stencil;
        glXGetFBConfigAttrib(display(), configs[i], GLX_DEPTH_SIZE,   &depth);
        glXGetFBConfigAttrib(display(), configs[i], GLX_STENCIL_SIZE, &stencil);

649 650 651 652 653 654
        int texture_format;
        if (alpha_bits)
            texture_format = bind_rgba ? GLX_TEXTURE_FORMAT_RGBA_EXT : GLX_TEXTURE_FORMAT_RGB_EXT;
        else
            texture_format = bind_rgb ? GLX_TEXTURE_FORMAT_RGB_EXT : GLX_TEXTURE_FORMAT_RGBA_EXT;

655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
        candidates.emplace_back(FBConfig{configs[i], depth, stencil, texture_format});
    }

    if (count > 0)
        XFree(configs);

    std::stable_sort(candidates.begin(), candidates.end(), [](const FBConfig &left, const FBConfig &right) {
        if (left.depth < right.depth)
            return true;

        if (left.stencil < right.stencil)
            return true;

        return false;
    });

    if (candidates.size() > 0) {
        const FBConfig &candidate = candidates.front();

674
        int y_inverted, texture_targets;
675 676
        glXGetFBConfigAttrib(display(), candidate.config, GLX_BIND_TO_TEXTURE_TARGETS_EXT, &texture_targets);
        glXGetFBConfigAttrib(display(), candidate.config, GLX_Y_INVERTED_EXT, &y_inverted);
677

678 679
        info->fbconfig            = candidate.config;
        info->bind_texture_format = candidate.format;
680 681 682 683 684 685 686 687 688 689 690 691
        info->texture_targets     = texture_targets;
        info->y_inverted          = y_inverted;
        info->mipmap              = 0;
    }

    if (info->fbconfig) {
        int fbc_id = 0;
        int visual_id = 0;

        glXGetFBConfigAttrib(display(), info->fbconfig, GLX_FBCONFIG_ID, &fbc_id);
        glXGetFBConfigAttrib(display(), info->fbconfig, GLX_VISUAL_ID,   &visual_id);

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
692
        qCDebug(KWIN_X11STANDALONE).nospace() << "Using FBConfig 0x" << Qt::hex << fbc_id << " for visual 0x" << Qt::hex << visual_id;
693 694 695 696 697
    }

    return info;
}

698 699 700 701 702 703
void GlxBackend::setSwapInterval(int interval)
{
    if (m_haveEXTSwapControl)
        glXSwapIntervalEXT(display(), glxWindow, interval);
    else if (m_haveMESASwapControl)
        glXSwapIntervalMESA(interval);
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
    else if (m_haveSGISwapControl)
        glXSwapIntervalSGI(interval);
}

void GlxBackend::waitSync()
{
    // NOTE that vsync has no effect with indirect rendering
    if (haveWaitSync) {
        uint sync;
#if 0
        // TODO: why precisely is this important?
        // the sync counter /can/ perform multiple steps during glXGetVideoSync & glXWaitVideoSync
        // but this only leads to waiting for two frames??!?
        glXGetVideoSync(&sync);
        glXWaitVideoSync(2, (sync + 1) % 2, &sync);
#else
        glXWaitVideoSyncSGI(1, 0, &sync);
#endif
    }
723 724
}

725
void GlxBackend::present()
726
{
Thomas Lübking's avatar
Thomas Lübking committed
727 728 729
    if (lastDamage().isEmpty())
        return;

730 731
    const QSize &screenSize = screens()->size();
    const QRegion displayRegion(0, 0, screenSize.width(), screenSize.height());
732
    const bool fullRepaint = supportsBufferAge() || (lastDamage() == displayRegion);
733

734
    if (fullRepaint) {
Roman Gilg's avatar
Roman Gilg committed
735
        if (m_haveINTELSwapEvent)
736 737
            Compositor::self()->aboutToSwapBuffers();

738
        if (haveSwapInterval) {
739 740 741 742
            if (gs_tripleBufferNeedsDetection) {
                glXWaitGL();
                m_swapProfiler.begin();
            }
743
            glXSwapBuffers(display(), glxWindow);
744 745 746 747 748 749 750
            if (gs_tripleBufferNeedsDetection) {
                glXWaitGL();
                if (char result = m_swapProfiler.end()) {
                    gs_tripleBufferUndetected = gs_tripleBufferNeedsDetection = false;
                    setBlocksForRetrace(result == 'd');
                }
            }
751
        } else {
752
            waitSync();
753 754
            glXSwapBuffers(display(), glxWindow);
        }
755 756 757
        if (supportsBufferAge()) {
            glXQueryDrawable(display(), glxWindow, GLX_BACK_BUFFER_AGE_EXT, (GLuint *) &m_bufferAge);
        }
758
    } else if (m_haveMESACopySubBuffer) {
759
        for (const QRect &r : lastDamage()) {
760
            // convert to OpenGL coordinates
761
            int y = screenSize.height() - r.y() - r.height();
762
            glXCopySubBufferMESA(display(), glxWindow, r.x(), y, r.width(), r.height());
763
        }
764
    } else { // Copy Pixels (horribly slow on Mesa)
765
        glDrawBuffer(GL_FRONT);
766
        copyPixels(lastDamage());
767
        glDrawBuffer(GL_BACK);
768
    }
Ralf Jung's avatar
Ralf Jung committed
769 770

    setLastDamage(QRegion());
771 772 773 774
    if (!supportsBufferAge()) {
        glXWaitGL();
        XFlush(display());
    }
775
}
776

777
void GlxBackend::screenGeometryChanged(const QSize &size)
778
{
779
    doneCurrent();
780 781 782

    XMoveResizeWindow(display(), window, 0, 0, size.width(), size.height());
    overlayWindow()->setup(window);
783
    Xcb::sync();
784

785
    makeCurrent();
786
    glViewport(0, 0, size.width(), size.height());
787 788 789

    // The back buffer contents are now undefined
    m_bufferAge = 0;
790 791
}

792
SceneOpenGLTexturePrivate *GlxBackend::createBackendTexture(SceneOpenGLTexture *texture)
793 794 795
{
    return new GlxTexture(texture, this);
}
796

797
QRegion GlxBackend::prepareRenderingFrame()
798
{
799 800
    QRegion repaint;

801 802 803 804 805 806 807 808 809
    if (gs_tripleBufferNeedsDetection) {
        // the composite timer floors the repaint frequency. This can pollute our triple buffering
        // detection because the glXSwapBuffers call for the new frame has to wait until the pending
        // one scanned out.
        // So we compensate for that by waiting an extra milisecond to give the driver the chance to
        // fllush the buffer queue
        usleep(1000);
    }

810 811
    present();

812 813 814
    if (supportsBufferAge())
        repaint = accumulatedDamageHistory(m_bufferAge);

Thomas Lübking's avatar
Thomas Lübking committed
815
    startRenderTimer();
816
    glXWaitX();
817

818
    return repaint;
819
}
820

821
void GlxBackend::endRenderingFrame(const QRegion &renderedRegion, const QRegion &damagedRegion)
822
{
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
    if (damagedRegion.isEmpty()) {
        setLastDamage(QRegion());

        // If the damaged region of a window is fully occluded, the only
        // rendering done, if any, will have been to repair a reused back
        // buffer, making it identical to the front buffer.
        //
        // In this case we won't post the back buffer. Instead we'll just
        // set the buffer age to 1, so the repaired regions won't be
        // rendered again in the next frame.
        if (!renderedRegion.isEmpty())
            glFlush();

        m_bufferAge = 1;
        return;
    }

840
    setLastDamage(renderedRegion);
841 842 843 844 845 846 847 848 849 850

    if (!blocksForRetrace()) {
        // This also sets lastDamage to empty which prevents the frame from
        // being posted again when prepareRenderingFrame() is called.
        present();
    } else {
        // Make sure that the GPU begins processing the command stream
        // now and not the next time prepareRenderingFrame() is called.
        glFlush();
    }
851 852 853

    if (overlayWindow()->window())  // show the window only after the first pass,
        overlayWindow()->show();   // since that pass may take long
854 855 856 857

    // Save the damaged region to history
    if (supportsBufferAge())
        addToDamageHistory(damagedRegion);
858 859
}

860 861 862 863 864 865 866 867 868 869 870 871 872 873
bool GlxBackend::makeCurrent()
{
    if (QOpenGLContext *context = QOpenGLContext::currentContext()) {
        // Workaround to tell Qt that no QOpenGLContext is current
        context->doneCurrent();
    }
    const bool current = glXMakeCurrent(display(), glxWindow, ctx);
    return current;
}

void GlxBackend::doneCurrent()
{
    glXMakeCurrent(display(), None, nullptr);
}
874

875
OverlayWindow* GlxBackend::overlayWindow() const
876 877 878 879 880 881 882 883 884
{
    return m_overlayWindow;
}

bool GlxBackend::usesOverlayWindow() const
{
    return true;
}

885 886 887
/********************************************************
 * GlxTexture
 *******************************************************/
888 889
GlxTexture::GlxTexture(SceneOpenGLTexture *texture, GlxBackend *backend)
    : SceneOpenGLTexturePrivate()
890 891 892 893 894 895 896
    , q(texture)
    , m_backend(backend)
    , m_glxpixmap(None)
{
}

GlxTexture::~GlxTexture()
897 898
{
    if (m_glxpixmap != None) {
899
        if (!options->isGlStrictBinding()) {
900
            glXReleaseTexImageEXT(display(), m_glxpixmap, GLX_FRONT_LEFT_EXT);
901
        }
902
        glXDestroyPixmap(display(), m_glxpixmap);
903
        m_glxpixmap = None;
904
    }
905
}
906

907 908 909 910
void GlxTexture::onDamage()
{
    if (options->isGlStrictBinding() && m_glxpixmap) {
        glXReleaseTexImageEXT(display(), m_glxpixmap, GLX_FRONT_LEFT_EXT);
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
911
        glXBindTexImageEXT(display(), m_glxpixmap, GLX_FRONT_LEFT_EXT, nullptr);
912 913 914 915
    }
    GLTexturePrivate::onDamage();
}

916 917 918 919 920 921 922 923 924
bool GlxTexture::loadTexture(xcb_pixmap_t pixmap, const QSize &size, xcb_visualid_t visual)
{
    if (pixmap == XCB_NONE || size.isEmpty() || visual == XCB_NONE)
        return false;

    const FBConfigInfo *info = m_backend->infoForVisual(visual);
    if (!info || info->fbconfig == nullptr)
        return false;

925
    if (info->texture_targets & GLX_TEXTURE_2D_BIT_EXT) {
926 927 928 929
        m_target = GL_TEXTURE_2D;
        m_scale.setWidth(1.0f / m_size.width());
        m_scale.setHeight(1.0f / m_size.height());
    } else {
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
930
        Q_ASSERT(info->texture_targets & GLX_TEXTURE_RECTANGLE_BIT_EXT);
931 932 933 934 935 936 937 938

        m_target = GL_TEXTURE_RECTANGLE;
        m_scale.setWidth(1.0f);
        m_scale.setHeight(1.0f);
    }

    const int attrs[] = {
        GLX_TEXTURE_FORMAT_EXT, info->bind_texture_format,
939
        GLX_MIPMAP_TEXTURE_EXT, false,
940 941 942 943 944 945 946
        GLX_TEXTURE_TARGET_EXT, m_target == GL_TEXTURE_2D ? GLX_TEXTURE_2D_EXT : GLX_TEXTURE_RECTANGLE_EXT,
        0
    };

    m_glxpixmap     = glXCreatePixmap(display(), info->fbconfig, pixmap, attrs);
    m_size          = size;
    m_yInverted     = info->y_inverted ? true : false;
947
    m_canUseMipmaps = false;
948 949 950 951

    glGenTextures(1, &m_texture);

    q->setDirty();
952
    q->setFilter(GL_NEAREST);
953 954 955 956 957 958 959 960

    glBindTexture(m_target, m_texture);
    glXBindTexImageEXT(display(), m_glxpixmap, GLX_FRONT_LEFT_EXT, nullptr);

    updateMatrix();
    return true;
}

961 962 963
bool GlxTexture::loadTexture(WindowPixmap *pixmap)
{
    Toplevel *t = pixmap->toplevel();
964
    return loadTexture(pixmap->pixmap(), t->bufferGeometry().size(), t->visual());
965 966
}

967
OpenGLBackend *GlxTexture::backend()
968
{
969
    return m_backend;
970
}
971 972

} // namespace