2015-02-17 09:54:25 +00:00
|
|
|
|
2013-05-15 11:47:27 +00:00
|
|
|
/********************************************************************
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
|
|
|
|
|
|
|
Copyright (C) 2013 Martin Gräßlin <mgraesslin@kde.org>
|
|
|
|
|
|
|
|
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/>.
|
|
|
|
*********************************************************************/
|
|
|
|
#define WL_EGL_PLATFORM 1
|
|
|
|
#include "egl_wayland_backend.h"
|
|
|
|
// kwin
|
2013-06-19 09:12:57 +00:00
|
|
|
#include "composite.h"
|
2015-05-05 15:27:03 +00:00
|
|
|
#include "logging.h"
|
2013-05-15 11:47:27 +00:00
|
|
|
#include "options.h"
|
2013-06-17 07:35:45 +00:00
|
|
|
#include "wayland_backend.h"
|
2015-02-17 09:54:25 +00:00
|
|
|
#include "wayland_server.h"
|
2014-09-19 11:59:51 +00:00
|
|
|
#include <KWayland/Client/surface.h>
|
2013-05-15 11:47:27 +00:00
|
|
|
// kwin libs
|
|
|
|
#include <kwinglplatform.h>
|
|
|
|
// KDE
|
2015-02-11 10:00:12 +00:00
|
|
|
#include <KWayland/Server/buffer_interface.h>
|
2015-02-17 09:54:25 +00:00
|
|
|
#include <KWayland/Server/display.h>
|
2013-05-23 06:23:32 +00:00
|
|
|
// Qt
|
2014-01-07 10:57:29 +00:00
|
|
|
#include <QOpenGLContext>
|
2013-05-15 11:47:27 +00:00
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
2015-03-23 13:29:07 +00:00
|
|
|
EglWaylandBackend::EglWaylandBackend(Wayland::WaylandBackend *b)
|
2013-06-17 07:35:45 +00:00
|
|
|
: QObject(NULL)
|
2015-03-19 13:46:39 +00:00
|
|
|
, AbstractEglBackend()
|
2014-01-07 10:57:29 +00:00
|
|
|
, m_bufferAge(0)
|
2015-03-23 13:29:07 +00:00
|
|
|
, m_wayland(b)
|
2013-06-17 07:35:45 +00:00
|
|
|
, m_overlay(NULL)
|
2013-05-15 11:47:27 +00:00
|
|
|
{
|
2013-06-17 08:28:31 +00:00
|
|
|
if (!m_wayland) {
|
|
|
|
setFailed("Wayland Backend has not been created");
|
|
|
|
return;
|
|
|
|
}
|
2015-05-05 15:27:03 +00:00
|
|
|
qCDebug(KWIN_WAYLAND_BACKEND) << "Connected to Wayland display?" << (m_wayland->display() ? "yes" : "no" );
|
2013-05-15 11:47:27 +00:00
|
|
|
if (!m_wayland->display()) {
|
|
|
|
setFailed("Could not connect to Wayland compositor");
|
|
|
|
return;
|
|
|
|
}
|
2013-06-17 08:28:31 +00:00
|
|
|
connect(m_wayland, SIGNAL(shellSurfaceSizeChanged(QSize)), SLOT(overlaySizeChanged(QSize)));
|
2013-05-15 11:47:27 +00:00
|
|
|
initializeEgl();
|
|
|
|
init();
|
|
|
|
// Egl is always direct rendering
|
|
|
|
setIsDirectRendering(true);
|
2013-05-28 07:58:10 +00:00
|
|
|
|
2015-05-05 15:27:03 +00:00
|
|
|
qCWarning(KWIN_WAYLAND_BACKEND) << "Using Wayland rendering backend";
|
|
|
|
qCWarning(KWIN_WAYLAND_BACKEND) << "This is a highly experimental backend, do not use for productive usage!";
|
|
|
|
qCWarning(KWIN_WAYLAND_BACKEND) << "Please do not report any issues you might encounter when using this backend!";
|
2013-05-15 11:47:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EglWaylandBackend::~EglWaylandBackend()
|
|
|
|
{
|
2015-03-19 13:46:39 +00:00
|
|
|
cleanup();
|
2013-06-17 07:35:45 +00:00
|
|
|
if (m_overlay) {
|
|
|
|
wl_egl_window_destroy(m_overlay);
|
|
|
|
}
|
2013-05-15 11:47:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool EglWaylandBackend::initializeEgl()
|
|
|
|
{
|
2015-03-19 13:46:39 +00:00
|
|
|
initClientExtensions();
|
|
|
|
EGLDisplay display = EGL_NO_DISPLAY;
|
2014-07-22 15:16:54 +00:00
|
|
|
|
|
|
|
// Use eglGetPlatformDisplayEXT() to get the display pointer
|
|
|
|
// if the implementation supports it.
|
2015-03-19 13:46:39 +00:00
|
|
|
m_havePlatformBase = hasClientExtension(QByteArrayLiteral("EGL_EXT_platform_base"));
|
2014-07-22 15:16:54 +00:00
|
|
|
if (m_havePlatformBase) {
|
|
|
|
// Make sure that the wayland platform is supported
|
2015-03-19 13:46:39 +00:00
|
|
|
if (!hasClientExtension(QByteArrayLiteral("EGL_EXT_platform_wayland")))
|
2014-07-22 15:16:54 +00:00
|
|
|
return false;
|
|
|
|
|
2015-03-19 13:46:39 +00:00
|
|
|
display = eglGetPlatformDisplayEXT(EGL_PLATFORM_WAYLAND_EXT, m_wayland->display(), nullptr);
|
2014-07-22 15:16:54 +00:00
|
|
|
} else {
|
2015-03-19 13:46:39 +00:00
|
|
|
display = eglGetDisplay(m_wayland->display());
|
2014-07-22 15:16:54 +00:00
|
|
|
}
|
|
|
|
|
2015-03-19 13:46:39 +00:00
|
|
|
if (display == EGL_NO_DISPLAY)
|
2013-05-15 11:47:27 +00:00
|
|
|
return false;
|
2015-03-19 13:46:39 +00:00
|
|
|
setEglDisplay(display);
|
|
|
|
return initEglAPI();
|
2013-05-15 11:47:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EglWaylandBackend::init()
|
|
|
|
{
|
|
|
|
if (!initRenderingContext()) {
|
|
|
|
setFailed("Could not initialize rendering context");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-19 13:46:39 +00:00
|
|
|
initKWinGL();
|
|
|
|
initBufferAge();
|
|
|
|
initWayland();
|
2013-05-15 11:47:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool EglWaylandBackend::initRenderingContext()
|
|
|
|
{
|
|
|
|
initBufferConfigs();
|
|
|
|
|
2015-03-19 13:46:39 +00:00
|
|
|
EGLContext context = EGL_NO_CONTEXT;
|
2015-10-30 11:56:03 +00:00
|
|
|
if (isOpenGLES()) {
|
|
|
|
const EGLint context_attribs[] = {
|
|
|
|
EGL_CONTEXT_CLIENT_VERSION, 2,
|
|
|
|
EGL_NONE
|
|
|
|
};
|
2013-05-15 11:47:27 +00:00
|
|
|
|
2015-10-30 11:56:03 +00:00
|
|
|
context = eglCreateContext(eglDisplay(), config(), EGL_NO_CONTEXT, context_attribs);
|
|
|
|
} else {
|
|
|
|
const EGLint context_attribs_31_core[] = {
|
|
|
|
EGL_CONTEXT_MAJOR_VERSION_KHR, 3,
|
|
|
|
EGL_CONTEXT_MINOR_VERSION_KHR, 1,
|
|
|
|
EGL_CONTEXT_FLAGS_KHR, EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR,
|
|
|
|
EGL_NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
const EGLint context_attribs_legacy[] = {
|
|
|
|
EGL_NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
const char* eglExtensionsCString = eglQueryString(eglDisplay(), EGL_EXTENSIONS);
|
|
|
|
const QList<QByteArray> extensions = QByteArray::fromRawData(eglExtensionsCString, qstrlen(eglExtensionsCString)).split(' ');
|
|
|
|
|
|
|
|
// Try to create a 3.1 core context
|
|
|
|
if (options->glCoreProfile() && extensions.contains(QByteArrayLiteral("EGL_KHR_create_context")))
|
|
|
|
context = eglCreateContext(eglDisplay(), config(), EGL_NO_CONTEXT, context_attribs_31_core);
|
|
|
|
|
|
|
|
if (context == EGL_NO_CONTEXT)
|
|
|
|
context = eglCreateContext(eglDisplay(), config(), EGL_NO_CONTEXT, context_attribs_legacy);
|
|
|
|
}
|
2013-05-15 11:47:27 +00:00
|
|
|
|
2015-03-19 13:46:39 +00:00
|
|
|
if (context == EGL_NO_CONTEXT) {
|
2015-05-05 15:27:03 +00:00
|
|
|
qCCritical(KWIN_WAYLAND_BACKEND) << "Create Context failed";
|
2013-05-15 11:47:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-03-19 13:46:39 +00:00
|
|
|
setContext(context);
|
2013-05-15 11:47:27 +00:00
|
|
|
|
2013-06-17 07:35:45 +00:00
|
|
|
if (!m_wayland->surface()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QSize &size = m_wayland->shellSurfaceSize();
|
2014-09-19 11:59:51 +00:00
|
|
|
auto s = m_wayland->surface();
|
|
|
|
connect(s, &KWayland::Client::Surface::frameRendered, Compositor::self(), &Compositor::bufferSwapComplete);
|
2014-08-20 12:06:58 +00:00
|
|
|
m_overlay = wl_egl_window_create(*s, size.width(), size.height());
|
2013-06-17 07:35:45 +00:00
|
|
|
if (!m_overlay) {
|
2015-05-05 15:27:03 +00:00
|
|
|
qCCritical(KWIN_WAYLAND_BACKEND) << "Creating Wayland Egl window failed";
|
2013-05-15 11:47:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-19 13:46:39 +00:00
|
|
|
EGLSurface surface = EGL_NO_SURFACE;
|
2014-07-22 15:16:54 +00:00
|
|
|
if (m_havePlatformBase)
|
2015-03-19 13:46:39 +00:00
|
|
|
surface = eglCreatePlatformWindowSurfaceEXT(eglDisplay(), config(), (void *) m_overlay, nullptr);
|
2014-07-22 15:16:54 +00:00
|
|
|
else
|
2015-03-19 13:46:39 +00:00
|
|
|
surface = eglCreateWindowSurface(eglDisplay(), config(), m_overlay, nullptr);
|
2014-07-22 15:16:54 +00:00
|
|
|
|
2015-03-19 13:46:39 +00:00
|
|
|
if (surface == EGL_NO_SURFACE) {
|
2015-05-05 15:27:03 +00:00
|
|
|
qCCritical(KWIN_WAYLAND_BACKEND) << "Create Window Surface failed";
|
2013-05-15 11:47:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-03-19 13:46:39 +00:00
|
|
|
setSurface(surface);
|
2013-05-15 11:47:27 +00:00
|
|
|
|
|
|
|
return makeContextCurrent();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EglWaylandBackend::makeContextCurrent()
|
|
|
|
{
|
2015-03-19 13:46:39 +00:00
|
|
|
if (eglMakeCurrent(eglDisplay(), surface(), surface(), context()) == EGL_FALSE) {
|
2015-05-05 15:27:03 +00:00
|
|
|
qCCritical(KWIN_WAYLAND_BACKEND) << "Make Context Current failed";
|
2013-05-15 11:47:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
EGLint error = eglGetError();
|
|
|
|
if (error != EGL_SUCCESS) {
|
2015-05-05 15:27:03 +00:00
|
|
|
qCWarning(KWIN_WAYLAND_BACKEND) << "Error occurred while creating context " << error;
|
2013-05-15 11:47:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
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,
|
2015-10-30 11:56:03 +00:00
|
|
|
EGL_RENDERABLE_TYPE, isOpenGLES() ? EGL_OPENGL_ES2_BIT : EGL_OPENGL_BIT,
|
2013-05-15 11:47:27 +00:00
|
|
|
EGL_CONFIG_CAVEAT, EGL_NONE,
|
|
|
|
EGL_NONE,
|
|
|
|
};
|
|
|
|
|
|
|
|
EGLint count;
|
|
|
|
EGLConfig configs[1024];
|
2015-03-19 13:46:39 +00:00
|
|
|
if (eglChooseConfig(eglDisplay(), config_attribs, configs, 1, &count) == EGL_FALSE) {
|
2015-05-05 15:27:03 +00:00
|
|
|
qCCritical(KWIN_WAYLAND_BACKEND) << "choose config failed";
|
2013-05-15 11:47:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (count != 1) {
|
2015-05-05 15:27:03 +00:00
|
|
|
qCCritical(KWIN_WAYLAND_BACKEND) << "choose config did not return a config" << count;
|
2013-05-15 11:47:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-03-19 13:46:39 +00:00
|
|
|
setConfig(configs[0]);
|
2013-05-15 11:47:27 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EglWaylandBackend::present()
|
|
|
|
{
|
2014-08-20 12:06:58 +00:00
|
|
|
m_wayland->surface()->setupFrameCallback();
|
2014-08-27 16:24:43 +00:00
|
|
|
Compositor::self()->aboutToSwapBuffers();
|
|
|
|
|
2014-01-07 10:57:29 +00:00
|
|
|
if (supportsBufferAge()) {
|
2015-03-19 13:46:39 +00:00
|
|
|
eglSwapBuffers(eglDisplay(), surface());
|
|
|
|
eglQuerySurface(eglDisplay(), surface(), EGL_BUFFER_AGE_EXT, &m_bufferAge);
|
2014-01-07 10:57:29 +00:00
|
|
|
setLastDamage(QRegion());
|
|
|
|
return;
|
|
|
|
} else {
|
2015-03-19 13:46:39 +00:00
|
|
|
eglSwapBuffers(eglDisplay(), surface());
|
2014-01-07 10:57:29 +00:00
|
|
|
setLastDamage(QRegion());
|
|
|
|
}
|
2013-05-15 11:47:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EglWaylandBackend::screenGeometryChanged(const QSize &size)
|
|
|
|
{
|
|
|
|
Q_UNUSED(size)
|
|
|
|
// no backend specific code needed
|
|
|
|
// TODO: base implementation in OpenGLBackend
|
2014-01-07 10:57:29 +00:00
|
|
|
|
|
|
|
// The back buffer contents are now undefined
|
|
|
|
m_bufferAge = 0;
|
2013-05-15 11:47:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SceneOpenGL::TexturePrivate *EglWaylandBackend::createBackendTexture(SceneOpenGL::Texture *texture)
|
|
|
|
{
|
|
|
|
return new EglWaylandTexture(texture, this);
|
|
|
|
}
|
|
|
|
|
2014-01-07 10:57:29 +00:00
|
|
|
QRegion EglWaylandBackend::prepareRenderingFrame()
|
2013-05-15 11:47:27 +00:00
|
|
|
{
|
|
|
|
if (!lastDamage().isEmpty())
|
|
|
|
present();
|
2014-01-07 10:57:29 +00:00
|
|
|
QRegion repaint;
|
|
|
|
if (supportsBufferAge())
|
|
|
|
repaint = accumulatedDamageHistory(m_bufferAge);
|
2013-05-15 11:47:27 +00:00
|
|
|
eglWaitNative(EGL_CORE_NATIVE_ENGINE);
|
|
|
|
startRenderTimer();
|
2014-01-07 10:57:29 +00:00
|
|
|
return repaint;
|
2013-05-15 11:47:27 +00:00
|
|
|
}
|
|
|
|
|
2014-01-07 10:57:29 +00:00
|
|
|
void EglWaylandBackend::endRenderingFrame(const QRegion &renderedRegion, const QRegion &damagedRegion)
|
2013-05-15 11:47:27 +00:00
|
|
|
{
|
2014-01-07 10:57:29 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
setLastDamage(renderedRegion);
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the damaged region to history
|
|
|
|
if (supportsBufferAge())
|
|
|
|
addToDamageHistory(damagedRegion);
|
|
|
|
}
|
|
|
|
|
2013-06-17 07:35:45 +00:00
|
|
|
void EglWaylandBackend::overlaySizeChanged(const QSize &size)
|
|
|
|
{
|
|
|
|
wl_egl_window_resize(m_overlay, size.width(), size.height(), 0, 0);
|
|
|
|
}
|
|
|
|
|
2013-06-19 10:26:34 +00:00
|
|
|
bool EglWaylandBackend::usesOverlayWindow() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-15 11:47:27 +00:00
|
|
|
/************************************************
|
|
|
|
* EglTexture
|
|
|
|
************************************************/
|
|
|
|
|
|
|
|
EglWaylandTexture::EglWaylandTexture(KWin::SceneOpenGL::Texture *texture, KWin::EglWaylandBackend *backend)
|
2015-03-19 13:46:39 +00:00
|
|
|
: AbstractEglTexture(texture, backend)
|
2013-05-15 11:47:27 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-03-19 13:46:39 +00:00
|
|
|
EglWaylandTexture::~EglWaylandTexture() = default;
|
2015-02-24 07:50:03 +00:00
|
|
|
|
2013-05-15 11:47:27 +00:00
|
|
|
} // namespace
|