egl_wayland_backend.cpp 12.1 KB
Newer Older
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
1
2
3
/*
    KWin - the KDE window manager
    This file is part of the KDE project.
4

Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
5
6
    SPDX-FileCopyrightText: 2019 Roman Gilg <subdiff@gmail.com>
    SPDX-FileCopyrightText: 2013 Martin Gräßlin <mgraesslin@kde.org>
7

Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
8
9
    SPDX-License-Identifier: GPL-2.0-or-later
*/
10
#define WL_EGL_PLATFORM 1
11

12
#include "egl_wayland_backend.h"
13
14
15
16

#include "wayland_backend.h"
#include "wayland_output.h"

17
#include "composite.h"
18
#include "logging.h"
19
#include "options.h"
20

21
#include "wayland_server.h"
22
23
#include "screens.h"

24
25
26
#include <unistd.h>
#include <fcntl.h>

27
28
// kwin libs
#include <kwinglplatform.h>
29

30
// KDE
31
#include <KWayland/Client/surface.h>
32
33
#include <KWaylandServer/buffer_interface.h>
#include <KWaylandServer/display.h>
34

35
// Qt
36
#include <QFile>
37
#include <QOpenGLContext>
38

Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
39
40
#include <cmath>

41
42
namespace KWin
{
43
44
45
46
47
48
49
50
51
52
53
54
namespace Wayland
{

EglWaylandOutput::EglWaylandOutput(WaylandOutput *output, QObject *parent)
    : QObject(parent)
    , m_waylandOutput(output)
{
}

bool EglWaylandOutput::init(EglWaylandBackend *backend)
{
    auto surface = m_waylandOutput->surface();
55
56
    const QSize nativeSize = m_waylandOutput->geometry().size() * m_waylandOutput->scale();
    auto overlay = wl_egl_window_create(*surface, nativeSize.width(), nativeSize.height());
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
    if (!overlay) {
        qCCritical(KWIN_WAYLAND_BACKEND) << "Creating Wayland Egl window failed";
        return false;
    }
    m_overlay = overlay;

    EGLSurface eglSurface = EGL_NO_SURFACE;
    if (backend->havePlatformBase()) {
        eglSurface = eglCreatePlatformWindowSurfaceEXT(backend->eglDisplay(), backend->config(), (void *) overlay, nullptr);
    } else {
        eglSurface = eglCreateWindowSurface(backend->eglDisplay(), backend->config(), overlay, nullptr);
    }
    if (eglSurface == EGL_NO_SURFACE) {
        qCCritical(KWIN_WAYLAND_BACKEND) << "Create Window Surface failed";
        return false;
    }
    m_eglSurface = eglSurface;

    connect(m_waylandOutput, &WaylandOutput::sizeChanged, this, &EglWaylandOutput::updateSize);
76
    connect(m_waylandOutput, &WaylandOutput::modeChanged, this, &EglWaylandOutput::updateSize);
77
78
79
80

    return true;
}

81
void EglWaylandOutput::updateSize()
82
{
83
84
    const QSize nativeSize = m_waylandOutput->geometry().size() * m_waylandOutput->scale();
    wl_egl_window_resize(m_overlay, nativeSize.width(), nativeSize.height(), 0, 0);
85
86
}

87
EglWaylandBackend::EglWaylandBackend(WaylandBackend *b)
88
    : AbstractEglBackend()
89
    , m_backend(b)
90
{
91
    if (!m_backend) {
92
93
94
        setFailed("Wayland Backend has not been created");
        return;
    }
95
96
    qCDebug(KWIN_WAYLAND_BACKEND) << "Connected to Wayland display?" << (m_backend->display() ? "yes" : "no" );
    if (!m_backend->display()) {
97
98
99
        setFailed("Could not connect to Wayland compositor");
        return;
    }
100

101
102
    // Egl is always direct rendering
    setIsDirectRendering(true);
103
104
105

    connect(m_backend, &WaylandBackend::outputAdded, this, &EglWaylandBackend::createEglWaylandOutput);
    connect(m_backend, &WaylandBackend::outputRemoved, this,
106
        [this] (AbstractOutput *output) {
107
108
109
110
111
112
113
114
115
116
117
118
            auto it = std::find_if(m_outputs.begin(), m_outputs.end(),
                [output] (const EglWaylandOutput *o) {
                    return o->m_waylandOutput == output;
                }
            );
            if (it == m_outputs.end()) {
                return;
            }
            cleanupOutput(*it);
            m_outputs.erase(it);
        }
    );
119
120
121
122
}

EglWaylandBackend::~EglWaylandBackend()
{
123
    cleanup();
124
125
126
127
128
129
130
131
132
133
}

void EglWaylandBackend::cleanupSurfaces()
{
    for (auto o : m_outputs) {
        cleanupOutput(o);
    }
    m_outputs.clear();
}

134
bool EglWaylandBackend::createEglWaylandOutput(AbstractOutput *waylandOutput)
135
{
136
    auto *output = new EglWaylandOutput(static_cast<WaylandOutput *>(waylandOutput), this);
137
    if (!output->init(this)) {
138
        return false;
139
    }
140
141
142
143
144
145
146
    m_outputs << output;
    return true;
}

void EglWaylandBackend::cleanupOutput(EglWaylandOutput *output)
{
    wl_egl_window_destroy(output->m_overlay);
147
148
149
150
}

bool EglWaylandBackend::initializeEgl()
{
151
    initClientExtensions();
152
    EGLDisplay display = m_backend->sceneEglDisplay();
153
154
155

    // Use eglGetPlatformDisplayEXT() to get the display pointer
    // if the implementation supports it.
156
157
158
159
160
161
162
    if (display == EGL_NO_DISPLAY) {
        m_havePlatformBase = hasClientExtension(QByteArrayLiteral("EGL_EXT_platform_base"));
        if (m_havePlatformBase) {
            // Make sure that the wayland platform is supported
            if (!hasClientExtension(QByteArrayLiteral("EGL_EXT_platform_wayland")))
                return false;

163
            display = eglGetPlatformDisplayEXT(EGL_PLATFORM_WAYLAND_EXT, m_backend->display(), nullptr);
164
        } else {
165
            display = eglGetDisplay(m_backend->display());
166
        }
167
168
    }

169
    if (display == EGL_NO_DISPLAY)
170
        return false;
171
172
    setEglDisplay(display);
    return initEglAPI();
173
174
175
176
}

void EglWaylandBackend::init()
{
177
178
179
180
    if (!initializeEgl()) {
        setFailed("Could not initialize egl");
        return;
    }
181
182
183
184
185
    if (!initRenderingContext()) {
        setFailed("Could not initialize rendering context");
        return;
    }

186
187
188
    initKWinGL();
    initBufferAge();
    initWayland();
189
190
191
192
193
194
}

bool EglWaylandBackend::initRenderingContext()
{
    initBufferConfigs();

195
    if (!createContext()) {
196
197
198
        return false;
    }

199
    auto waylandOutputs = m_backend->waylandOutputs();
200

201
202
    // we only allow to start with at least one output
    if (waylandOutputs.isEmpty()) {
203
204
205
        return false;
    }

206
207
208
209
210
    for (auto *out : waylandOutputs) {
        if (!createEglWaylandOutput(out)) {
            return false;
        }
    }
211

212
213
    if (m_outputs.isEmpty()) {
        qCCritical(KWIN_WAYLAND_BACKEND) << "Create Window Surfaces failed";
214
215
216
        return false;
    }

217
218
219
220
    auto *firstOutput = m_outputs.first();
    // set our first surface as the one for the abstract backend, just to make it happy
    setSurface(firstOutput->m_eglSurface);
    return makeContextCurrent(firstOutput);
221
222
}

223
bool EglWaylandBackend::makeContextCurrent(EglWaylandOutput *output)
224
{
225
226
227
228
229
    const EGLSurface eglSurface = output->m_eglSurface;
    if (eglSurface == EGL_NO_SURFACE) {
        return false;
    }
    if (eglMakeCurrent(eglDisplay(), eglSurface, eglSurface, context()) == EGL_FALSE) {
230
        qCCritical(KWIN_WAYLAND_BACKEND) << "Make Context Current failed";
231
232
233
234
235
        return false;
    }

    EGLint error = eglGetError();
    if (error != EGL_SUCCESS) {
236
        qCWarning(KWIN_WAYLAND_BACKEND) << "Error occurred while creating context " << error;
237
238
        return false;
    }
239
240

    const QRect &v = output->m_waylandOutput->geometry();
241
    const qreal scale = output->m_waylandOutput->scale();
242
243
244
245

    const QSize overall = screens()->size();
    glViewport(-v.x() * scale, (v.height() - overall.height() + v.y()) * scale,
               overall.width() * scale, overall.height() * scale);
246
247
248
249
250
251
252
253
254
255
256
    return true;
}

bool EglWaylandBackend::initBufferConfigs()
{
    const EGLint config_attribs[] = {
        EGL_SURFACE_TYPE,         EGL_WINDOW_BIT,
        EGL_RED_SIZE,             1,
        EGL_GREEN_SIZE,           1,
        EGL_BLUE_SIZE,            1,
        EGL_ALPHA_SIZE,           0,
257
        EGL_RENDERABLE_TYPE,      isOpenGLES() ? EGL_OPENGL_ES2_BIT : EGL_OPENGL_BIT,
258
259
260
261
262
263
        EGL_CONFIG_CAVEAT,        EGL_NONE,
        EGL_NONE,
    };

    EGLint count;
    EGLConfig configs[1024];
264
    if (eglChooseConfig(eglDisplay(), config_attribs, configs, 1, &count) == EGL_FALSE) {
265
        qCCritical(KWIN_WAYLAND_BACKEND) << "choose config failed";
266
267
268
        return false;
    }
    if (count != 1) {
269
        qCCritical(KWIN_WAYLAND_BACKEND) << "choose config did not return a config" << count;
270
271
        return false;
    }
272
    setConfig(configs[0]);
273
274
275
276

    return true;
}

277
278
279
static QVector<EGLint> regionToRects(const QRegion &region, AbstractWaylandOutput *output)
{
    const int height = output->modeSize().height();
280
281
282
    const QMatrix4x4 matrix = WaylandOutput::logicalToNativeMatrix(output->geometry(),
                                                                   output->scale(),
                                                                   output->transform());
283
284
285
286
287
288
289
290
291
292
293
294
295
296

    QVector<EGLint> rects;
    rects.reserve(region.rectCount() * 4);
    for (const QRect &_rect : region) {
        const QRect rect = matrix.mapRect(_rect);

        rects << rect.left();
        rects << height - (rect.y() + rect.height());
        rects << rect.width();
        rects << rect.height();
    }
    return rects;
}

297
void EglWaylandBackend::aboutToStartPainting(int screenId, const QRegion &damagedRegion)
298
{
299
300
    Q_ASSERT_X(screenId != -1, "aboutToStartPainting", "not using per screen rendering");
    EglWaylandOutput *output = m_outputs.at(screenId);
301
302
303
304
305
306
307
308
309
310
311
312
    if (output->m_bufferAge > 0 && !damagedRegion.isEmpty() && supportsPartialUpdate()) {
        const QRegion region = damagedRegion & output->m_waylandOutput->geometry();

        QVector<EGLint> rects = regionToRects(region, output->m_waylandOutput);
        const bool correct = eglSetDamageRegionKHR(eglDisplay(), output->m_eglSurface,
                                                   rects.data(), rects.count()/4);
        if (!correct) {
            qCWarning(KWIN_WAYLAND_BACKEND) << "failed eglSetDamageRegionKHR" << eglGetError();
        }
    }
}

313
void EglWaylandBackend::presentOnSurface(EglWaylandOutput *output, const QRegion &damage)
314
{
315
    WaylandOutput *waylandOutput = output->m_waylandOutput;
316

317
    waylandOutput->surface()->setupFrameCallback();
318
    waylandOutput->surface()->setScale(std::ceil(waylandOutput->scale()));
319
    Q_EMIT waylandOutput->outputChange(damage);
320

321
    if (supportsSwapBuffersWithDamage() && !output->m_damageHistory.isEmpty()) {
322
        QVector<EGLint> rects = regionToRects(output->m_damageHistory.constFirst(), waylandOutput);
323
324
325
326
        if (!eglSwapBuffersWithDamageEXT(eglDisplay(), output->m_eglSurface,
                                         rects.data(), rects.count() / 4)) {
            qCCritical(KWIN_WAYLAND_BACKEND, "eglSwapBuffersWithDamage() failed: %x", eglGetError());
        }
327
    } else {
328
329
330
        if (!eglSwapBuffers(eglDisplay(), output->m_eglSurface)) {
            qCCritical(KWIN_WAYLAND_BACKEND, "eglSwapBuffers() failed: %x", eglGetError());
        }
331
    }
332

333
334
335
336
    if (supportsBufferAge()) {
        eglQuerySurface(eglDisplay(), output->m_eglSurface, EGL_BUFFER_AGE_EXT, &output->m_bufferAge);
    }

337
338
339
340
341
342
343
}

void EglWaylandBackend::screenGeometryChanged(const QSize &size)
{
    Q_UNUSED(size)
    // no backend specific code needed
    // TODO: base implementation in OpenGLBackend
344
345

    // The back buffer contents are now undefined
346
347
348
    for (auto *output : qAsConst(m_outputs)) {
        output->m_bufferAge = 0;
    }
349
350
}

351
SceneOpenGLTexturePrivate *EglWaylandBackend::createBackendTexture(SceneOpenGLTexture *texture)
352
353
354
355
{
    return new EglWaylandTexture(texture, this);
}

356
QRegion EglWaylandBackend::beginFrame(int screenId)
357
358
{
    eglWaitNative(EGL_CORE_NATIVE_ENGINE);
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375

    auto *output = m_outputs.at(screenId);
    makeContextCurrent(output);
    if (supportsBufferAge()) {
        QRegion region;

        // Note: An age of zero means the buffer contents are undefined
        if (output->m_bufferAge > 0 && output->m_bufferAge <= output->m_damageHistory.count()) {
            for (int i = 0; i < output->m_bufferAge - 1; i++)
                region |= output->m_damageHistory[i];
        } else {
            region = output->m_waylandOutput->geometry();
        }

        return region;
    }
    return QRegion();
376
377
}

378
void EglWaylandBackend::endFrame(int screenId, const QRegion &renderedRegion, const QRegion &damagedRegion)
379
380
{
    EglWaylandOutput *output = m_outputs[screenId];
381
382
    QRegion damage = damagedRegion.intersected(output->m_waylandOutput->geometry());
    presentOnSurface(output, damage);
383

384
    if (supportsBufferAge()) {
385
386
387
        if (output->m_damageHistory.count() > 10) {
            output->m_damageHistory.removeLast();
        }
388

389
        output->m_damageHistory.prepend(damage);
390
391
392
    }
}

393
394
395
396
/************************************************
 * EglTexture
 ************************************************/

397
EglWaylandTexture::EglWaylandTexture(KWin::SceneOpenGLTexture *texture, KWin::Wayland::EglWaylandBackend *backend)
398
    : AbstractEglTexture(texture, backend)
399
400
401
{
}

402
EglWaylandTexture::~EglWaylandTexture() = default;
403

404
405
}
}