2020-08-02 22:22:19 +00:00
|
|
|
/*
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
2010-11-21 13:01:39 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-FileCopyrightText: 2010, 2012 Martin Gräßlin <mgraesslin@kde.org>
|
2010-11-21 13:01:39 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
2012-08-26 15:14:23 +00:00
|
|
|
#include "eglonxbackend.h"
|
|
|
|
// kwin
|
2015-03-19 10:26:53 +00:00
|
|
|
#include "main.h"
|
2012-08-26 15:14:23 +00:00
|
|
|
#include "options.h"
|
|
|
|
#include "overlaywindow.h"
|
2016-07-18 08:27:56 +00:00
|
|
|
#include "platform.h"
|
2013-03-11 10:01:38 +00:00
|
|
|
#include "xcbutils.h"
|
2013-09-02 11:14:39 +00:00
|
|
|
// Qt
|
2017-09-08 20:30:18 +00:00
|
|
|
#include <QLoggingCategory>
|
2013-09-02 11:14:39 +00:00
|
|
|
#include <QDebug>
|
2012-08-26 15:14:23 +00:00
|
|
|
|
2021-01-25 12:02:48 +00:00
|
|
|
Q_LOGGING_CATEGORY(KWIN_CORE, "kwin_core", QtWarningMsg)
|
2016-04-15 07:11:30 +00:00
|
|
|
|
2012-08-26 15:14:23 +00:00
|
|
|
namespace KWin
|
|
|
|
{
|
2010-11-21 13:01:39 +00:00
|
|
|
|
2016-11-11 08:59:46 +00:00
|
|
|
EglOnXBackend::EglOnXBackend(Display *display)
|
2015-03-19 13:46:39 +00:00
|
|
|
: AbstractEglBackend()
|
2017-08-07 15:54:56 +00:00
|
|
|
, m_overlayWindow(kwinApp()->platform()->createOverlayWindow())
|
2012-08-26 15:14:23 +00:00
|
|
|
, surfaceHasSubPost(0)
|
2015-03-19 10:26:53 +00:00
|
|
|
, m_usesOverlayWindow(true)
|
|
|
|
, m_connection(connection())
|
2016-11-11 08:59:46 +00:00
|
|
|
, m_x11Display(display)
|
2015-03-19 10:26:53 +00:00
|
|
|
, m_rootWindow(rootWindow())
|
|
|
|
, m_x11ScreenNumber(kwinApp()->x11ScreenNumber())
|
2012-08-26 15:14:23 +00:00
|
|
|
{
|
|
|
|
// Egl is always direct rendering
|
|
|
|
setIsDirectRendering(true);
|
|
|
|
}
|
2010-11-28 15:21:12 +00:00
|
|
|
|
2015-05-05 13:35:12 +00:00
|
|
|
EglOnXBackend::EglOnXBackend(xcb_connection_t *connection, Display *display, xcb_window_t rootWindow, int screenNumber, xcb_window_t renderingWindow)
|
2015-03-19 13:46:39 +00:00
|
|
|
: AbstractEglBackend()
|
2015-03-19 10:26:53 +00:00
|
|
|
, m_overlayWindow(nullptr)
|
|
|
|
, surfaceHasSubPost(0)
|
|
|
|
, m_usesOverlayWindow(false)
|
2015-05-05 13:35:12 +00:00
|
|
|
, m_connection(connection)
|
|
|
|
, m_x11Display(display)
|
|
|
|
, m_rootWindow(rootWindow)
|
|
|
|
, m_x11ScreenNumber(screenNumber)
|
|
|
|
, m_renderingWindow(renderingWindow)
|
2015-03-19 10:26:53 +00:00
|
|
|
{
|
|
|
|
// Egl is always direct rendering
|
|
|
|
setIsDirectRendering(true);
|
|
|
|
}
|
|
|
|
|
2012-08-26 15:14:23 +00:00
|
|
|
EglOnXBackend::~EglOnXBackend()
|
|
|
|
{
|
2015-03-19 10:26:53 +00:00
|
|
|
if (isFailed() && m_overlayWindow) {
|
2013-06-19 10:26:34 +00:00
|
|
|
m_overlayWindow->destroy();
|
|
|
|
}
|
2015-03-19 13:46:39 +00:00
|
|
|
cleanup();
|
2015-10-09 16:44:10 +00:00
|
|
|
|
2015-03-19 10:26:53 +00:00
|
|
|
if (m_overlayWindow) {
|
|
|
|
if (overlayWindow()->window()) {
|
|
|
|
overlayWindow()->destroy();
|
|
|
|
}
|
|
|
|
delete m_overlayWindow;
|
2012-08-26 15:14:23 +00:00
|
|
|
}
|
|
|
|
}
|
2010-11-21 13:01:39 +00:00
|
|
|
|
2012-08-26 15:14:23 +00:00
|
|
|
void EglOnXBackend::init()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2016-02-01 21:18:29 +00:00
|
|
|
qputenv("EGL_PLATFORM", "x11");
|
2012-08-26 15:14:23 +00:00
|
|
|
if (!initRenderingContext()) {
|
2013-07-23 05:02:52 +00:00
|
|
|
setFailed(QStringLiteral("Could not initialize rendering context"));
|
2010-11-28 15:21:12 +00:00
|
|
|
return;
|
2012-08-26 15:14:23 +00:00
|
|
|
}
|
2010-11-21 13:01:39 +00:00
|
|
|
|
2015-03-19 13:46:39 +00:00
|
|
|
initKWinGL();
|
2016-11-17 06:51:04 +00:00
|
|
|
if (!hasExtension(QByteArrayLiteral("EGL_KHR_image")) &&
|
|
|
|
(!hasExtension(QByteArrayLiteral("EGL_KHR_image_base")) ||
|
|
|
|
!hasExtension(QByteArrayLiteral("EGL_KHR_image_pixmap")))) {
|
2013-07-23 05:02:52 +00:00
|
|
|
setFailed(QStringLiteral("Required support for binding pixmaps to EGLImages not found, disabling compositing"));
|
2010-12-05 10:55:19 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-02-11 18:35:54 +00:00
|
|
|
if (!hasGLExtension(QByteArrayLiteral("GL_OES_EGL_image"))) {
|
2013-07-23 05:02:52 +00:00
|
|
|
setFailed(QStringLiteral("Required extension GL_OES_EGL_image not found, disabling compositing"));
|
2010-11-21 13:01:39 +00:00
|
|
|
return;
|
2010-12-04 11:31:18 +00:00
|
|
|
}
|
2012-03-29 20:11:28 +00:00
|
|
|
|
2013-03-22 11:18:51 +00:00
|
|
|
// check for EGL_NV_post_sub_buffer and whether it can be used on the surface
|
2016-11-17 06:51:04 +00:00
|
|
|
if (hasExtension(QByteArrayLiteral("EGL_NV_post_sub_buffer"))) {
|
2015-03-19 13:46:39 +00:00
|
|
|
if (eglQuerySurface(eglDisplay(), surface(), EGL_POST_SUB_BUFFER_SUPPORTED_NV, &surfaceHasSubPost) == EGL_FALSE) {
|
2013-03-22 11:18:51 +00:00
|
|
|
EGLint error = eglGetError();
|
|
|
|
if (error != EGL_SUCCESS && error != EGL_BAD_ATTRIBUTE) {
|
2013-07-23 05:02:52 +00:00
|
|
|
setFailed(QStringLiteral("query surface failed"));
|
2013-03-22 11:18:51 +00:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
surfaceHasSubPost = EGL_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-21 09:44:06 +00:00
|
|
|
|
2013-03-22 11:18:51 +00:00
|
|
|
if (surfaceHasSubPost) {
|
2014-12-05 10:42:15 +00:00
|
|
|
qCDebug(KWIN_CORE) << "EGL implementation and surface support eglPostSubBufferNV, let's use it";
|
2013-03-22 11:18:51 +00:00
|
|
|
|
2021-01-07 08:09:47 +00:00
|
|
|
// check if swap interval 1 is supported
|
|
|
|
EGLint val;
|
|
|
|
eglGetConfigAttrib(eglDisplay(), config(), EGL_MAX_SWAP_INTERVAL, &val);
|
|
|
|
if (val >= 1) {
|
|
|
|
if (eglSwapInterval(eglDisplay(), 1)) {
|
|
|
|
qCDebug(KWIN_CORE) << "Enabled v-sync";
|
2013-04-12 12:05:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-01-07 08:09:47 +00:00
|
|
|
qCWarning(KWIN_CORE) << "Cannot enable v-sync as max. swap interval is" << val;
|
2013-03-22 11:18:51 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* In the GLX backend, we fall back to using glCopyPixels if we have no extension providing support for partial screen updates.
|
|
|
|
* However, that does not work in EGL - glCopyPixels with glDrawBuffer(GL_FRONT); does nothing.
|
|
|
|
* Hence we need EGL to preserve the backbuffer for us, so that we can draw the partial updates on it and call
|
|
|
|
* eglSwapBuffers() for each frame. eglSwapBuffers() then does the copy (no page flip possible in this mode),
|
|
|
|
* which means it is slow and not synced to the v-blank. */
|
2014-12-05 10:42:15 +00:00
|
|
|
qCWarning(KWIN_CORE) << "eglPostSubBufferNV not supported, have to enable buffer preservation - which breaks v-sync and performance";
|
2015-03-19 13:46:39 +00:00
|
|
|
eglSurfaceAttrib(eglDisplay(), surface(), EGL_SWAP_BEHAVIOR, EGL_BUFFER_PRESERVED);
|
2012-03-29 20:11:28 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-11-21 13:01:39 +00:00
|
|
|
|
2012-08-26 15:14:23 +00:00
|
|
|
bool EglOnXBackend::initRenderingContext()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2015-03-19 13:46:39 +00:00
|
|
|
initClientExtensions();
|
2016-07-18 08:27:56 +00:00
|
|
|
EGLDisplay dpy = kwinApp()->platform()->sceneEglDisplay();
|
2014-07-22 15:16:29 +00:00
|
|
|
|
|
|
|
// Use eglGetPlatformDisplayEXT() to get the display pointer
|
|
|
|
// if the implementation supports it.
|
2016-07-21 07:42:01 +00:00
|
|
|
if (dpy == EGL_NO_DISPLAY) {
|
2016-07-18 08:27:56 +00:00
|
|
|
const bool havePlatformBase = hasClientExtension(QByteArrayLiteral("EGL_EXT_platform_base"));
|
|
|
|
setHavePlatformBase(havePlatformBase);
|
|
|
|
if (havePlatformBase) {
|
|
|
|
// Make sure that the X11 platform is supported
|
2016-10-04 13:55:26 +00:00
|
|
|
if (!hasClientExtension(QByteArrayLiteral("EGL_EXT_platform_x11")) &&
|
2016-10-04 15:20:41 +00:00
|
|
|
!hasClientExtension(QByteArrayLiteral("EGL_KHR_platform_x11"))) {
|
2016-10-04 13:55:26 +00:00
|
|
|
qCWarning(KWIN_CORE) << "EGL_EXT_platform_base is supported, but neither EGL_EXT_platform_x11 nor EGL_KHR_platform_x11 is supported."
|
|
|
|
<< "Cannot create EGLDisplay on X11";
|
2016-07-18 08:27:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-07-22 15:16:29 +00:00
|
|
|
|
2016-07-18 08:27:56 +00:00
|
|
|
const int attribs[] = {
|
|
|
|
EGL_PLATFORM_X11_SCREEN_EXT, m_x11ScreenNumber,
|
|
|
|
EGL_NONE
|
|
|
|
};
|
2014-07-22 15:16:29 +00:00
|
|
|
|
2016-07-18 08:27:56 +00:00
|
|
|
dpy = eglGetPlatformDisplayEXT(EGL_PLATFORM_X11_EXT, m_x11Display, attribs);
|
|
|
|
} else {
|
|
|
|
dpy = eglGetDisplay(m_x11Display);
|
|
|
|
}
|
2014-07-22 15:16:29 +00:00
|
|
|
}
|
|
|
|
|
2016-07-15 06:44:31 +00:00
|
|
|
if (dpy == EGL_NO_DISPLAY) {
|
|
|
|
qCWarning(KWIN_CORE) << "Failed to get the EGLDisplay";
|
2010-11-28 15:21:12 +00:00
|
|
|
return false;
|
2016-07-15 06:44:31 +00:00
|
|
|
}
|
2015-03-19 13:46:39 +00:00
|
|
|
setEglDisplay(dpy);
|
|
|
|
initEglAPI();
|
2013-03-13 15:28:45 +00:00
|
|
|
|
2010-11-28 15:21:12 +00:00
|
|
|
initBufferConfigs();
|
2013-03-13 15:28:45 +00:00
|
|
|
|
2015-03-19 10:26:53 +00:00
|
|
|
if (m_usesOverlayWindow) {
|
|
|
|
if (!overlayWindow()->create()) {
|
|
|
|
qCCritical(KWIN_CORE) << "Could not get overlay window";
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
overlayWindow()->setup(None);
|
|
|
|
}
|
|
|
|
}
|
2015-11-25 12:32:36 +00:00
|
|
|
if (!createSurfaces()) {
|
|
|
|
qCCritical(KWIN_CORE) << "Creating egl surface failed";
|
2015-03-19 13:46:39 +00:00
|
|
|
return false;
|
2014-07-22 15:16:29 +00:00
|
|
|
}
|
2010-11-28 15:21:12 +00:00
|
|
|
|
2015-11-13 07:29:49 +00:00
|
|
|
if (!createContext()) {
|
2015-11-25 12:32:36 +00:00
|
|
|
qCCritical(KWIN_CORE) << "Create OpenGL context failed";
|
2010-11-28 15:21:12 +00:00
|
|
|
return false;
|
2012-09-29 11:19:35 +00:00
|
|
|
}
|
2013-03-13 15:28:45 +00:00
|
|
|
|
2015-11-25 12:32:36 +00:00
|
|
|
if (!makeContextCurrent(surface())) {
|
2014-12-05 10:42:15 +00:00
|
|
|
qCCritical(KWIN_CORE) << "Make Context Current failed";
|
2010-11-28 15:21:12 +00:00
|
|
|
return false;
|
2012-09-29 11:19:35 +00:00
|
|
|
}
|
2013-03-13 15:28:45 +00:00
|
|
|
|
2010-11-28 15:21:12 +00:00
|
|
|
EGLint error = eglGetError();
|
2011-01-30 14:34:42 +00:00
|
|
|
if (error != EGL_SUCCESS) {
|
2014-12-05 10:42:15 +00:00
|
|
|
qCWarning(KWIN_CORE) << "Error occurred while creating context " << error;
|
2010-11-28 15:21:12 +00:00
|
|
|
return false;
|
2010-11-21 13:01:39 +00:00
|
|
|
}
|
2013-03-13 15:28:45 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
2010-11-21 13:01:39 +00:00
|
|
|
|
2015-11-25 12:32:36 +00:00
|
|
|
bool EglOnXBackend::createSurfaces()
|
|
|
|
{
|
|
|
|
xcb_window_t window = XCB_WINDOW_NONE;
|
|
|
|
if (m_overlayWindow) {
|
|
|
|
window = m_overlayWindow->window();
|
|
|
|
} else if (m_renderingWindow) {
|
|
|
|
window = m_renderingWindow;
|
|
|
|
}
|
|
|
|
|
|
|
|
EGLSurface surface = createSurface(window);
|
|
|
|
|
|
|
|
if (surface == EGL_NO_SURFACE) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
setSurface(surface);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
EGLSurface EglOnXBackend::createSurface(xcb_window_t window)
|
|
|
|
{
|
|
|
|
if (window == XCB_WINDOW_NONE) {
|
|
|
|
return EGL_NO_SURFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
EGLSurface surface = EGL_NO_SURFACE;
|
|
|
|
if (havePlatformBase()) {
|
|
|
|
// Note: Window is 64 bits on a 64-bit architecture whereas xcb_window_t is
|
|
|
|
// always 32 bits. eglCreatePlatformWindowSurfaceEXT() expects the
|
|
|
|
// native_window parameter to be pointer to a Window, so this variable
|
|
|
|
// cannot be an xcb_window_t.
|
|
|
|
surface = eglCreatePlatformWindowSurfaceEXT(eglDisplay(), config(), (void *) &window, nullptr);
|
|
|
|
} else {
|
|
|
|
surface = eglCreateWindowSurface(eglDisplay(), config(), window, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return surface;
|
|
|
|
}
|
|
|
|
|
2012-08-26 15:14:23 +00:00
|
|
|
bool EglOnXBackend::initBufferConfigs()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2016-11-17 06:51:04 +00:00
|
|
|
initBufferAge();
|
2010-11-28 15:21:12 +00:00
|
|
|
const EGLint config_attribs[] = {
|
2016-01-17 21:01:00 +00:00
|
|
|
EGL_SURFACE_TYPE, EGL_WINDOW_BIT | (supportsBufferAge() ? 0 : EGL_SWAP_BEHAVIOR_PRESERVED_BIT),
|
2010-11-28 15:21:12 +00:00
|
|
|
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,
|
2010-11-28 15:21:12 +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, 1024, &count) == EGL_FALSE) {
|
2014-12-05 10:42:15 +00:00
|
|
|
qCCritical(KWIN_CORE) << "choose config failed";
|
2012-09-29 11:19:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-11-28 15:21:12 +00:00
|
|
|
|
2015-03-19 10:26:53 +00:00
|
|
|
ScopedCPointer<xcb_get_window_attributes_reply_t> attribs(xcb_get_window_attributes_reply(m_connection,
|
|
|
|
xcb_get_window_attributes_unchecked(m_connection, m_rootWindow),
|
|
|
|
nullptr));
|
2013-03-11 10:01:38 +00:00
|
|
|
if (!attribs) {
|
2014-12-05 10:42:15 +00:00
|
|
|
qCCritical(KWIN_CORE) << "Failed to get window attributes of root window";
|
2013-03-11 10:01:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-11-28 15:21:12 +00:00
|
|
|
|
2015-03-19 13:46:39 +00:00
|
|
|
setConfig(configs[0]);
|
2011-01-30 14:34:42 +00:00
|
|
|
for (int i = 0; i < count; i++) {
|
2010-11-28 15:21:12 +00:00
|
|
|
EGLint val;
|
2015-03-19 13:46:39 +00:00
|
|
|
if (eglGetConfigAttrib(eglDisplay(), configs[i], EGL_NATIVE_VISUAL_ID, &val) == EGL_FALSE) {
|
2014-12-05 10:42:15 +00:00
|
|
|
qCCritical(KWIN_CORE) << "egl get config attrib failed";
|
2012-09-29 11:19:35 +00:00
|
|
|
}
|
2013-03-11 10:01:38 +00:00
|
|
|
if (uint32_t(val) == attribs->visual) {
|
2015-03-19 13:46:39 +00:00
|
|
|
setConfig(configs[i]);
|
2010-11-28 15:21:12 +00:00
|
|
|
break;
|
|
|
|
}
|
2010-11-21 13:01:39 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
2010-11-21 13:01:39 +00:00
|
|
|
|
2013-06-19 10:26:34 +00:00
|
|
|
bool EglOnXBackend::usesOverlayWindow() const
|
|
|
|
{
|
2015-03-19 10:26:53 +00:00
|
|
|
return m_usesOverlayWindow;
|
2013-06-19 10:26:34 +00:00
|
|
|
}
|
|
|
|
|
2019-08-07 17:33:20 +00:00
|
|
|
OverlayWindow* EglOnXBackend::overlayWindow() const
|
2013-06-19 10:26:34 +00:00
|
|
|
{
|
|
|
|
return m_overlayWindow;
|
|
|
|
}
|
|
|
|
|
2015-11-25 12:32:36 +00:00
|
|
|
bool EglOnXBackend::makeContextCurrent(const EGLSurface &surface)
|
|
|
|
{
|
|
|
|
return eglMakeCurrent(eglDisplay(), surface, surface, context()) == EGL_TRUE;
|
|
|
|
}
|
|
|
|
|
2012-08-26 15:14:23 +00:00
|
|
|
} // namespace
|