2020-08-02 22:22:19 +00:00
|
|
|
/*
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
2015-04-10 08:44:07 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-FileCopyrightText: 2015 Martin Gräßlin <mgraesslin@kde.org>
|
2015-04-10 08:44:07 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
2015-04-10 08:44:07 +00:00
|
|
|
#include "egl_gbm_backend.h"
|
2021-04-09 07:06:04 +00:00
|
|
|
#include "basiceglsurfacetexture_internal.h"
|
|
|
|
#include "basiceglsurfacetexture_wayland.h"
|
2015-04-10 08:44:07 +00:00
|
|
|
// kwin
|
|
|
|
#include "composite.h"
|
|
|
|
#include "drm_backend.h"
|
2021-04-28 19:12:11 +00:00
|
|
|
#include "drm_buffer_gbm.h"
|
2016-03-21 14:11:17 +00:00
|
|
|
#include "drm_output.h"
|
2017-10-05 16:58:57 +00:00
|
|
|
#include "gbm_surface.h"
|
2015-05-05 15:27:03 +00:00
|
|
|
#include "logging.h"
|
2015-04-10 08:44:07 +00:00
|
|
|
#include "options.h"
|
2020-11-29 11:24:38 +00:00
|
|
|
#include "renderloop_p.h"
|
2015-04-10 08:44:07 +00:00
|
|
|
#include "screens.h"
|
2021-02-04 08:40:20 +00:00
|
|
|
#include "surfaceitem_wayland.h"
|
2020-10-05 21:05:55 +00:00
|
|
|
#include "drm_gpu.h"
|
2021-02-02 13:26:43 +00:00
|
|
|
#include "linux_dmabuf.h"
|
2021-05-04 10:19:31 +00:00
|
|
|
#include "dumb_swapchain.h"
|
2015-04-10 08:44:07 +00:00
|
|
|
// kwin libs
|
|
|
|
#include <kwinglplatform.h>
|
2020-07-22 17:38:57 +00:00
|
|
|
#include <kwineglimagetexture.h>
|
2015-04-10 08:44:07 +00:00
|
|
|
// system
|
|
|
|
#include <gbm.h>
|
2020-11-28 17:53:41 +00:00
|
|
|
#include <unistd.h>
|
2020-11-29 18:31:49 +00:00
|
|
|
#include <errno.h>
|
2021-05-30 23:58:47 +00:00
|
|
|
#include <egl_dmabuf.h>
|
|
|
|
#include <drm_fourcc.h>
|
2021-02-02 13:26:43 +00:00
|
|
|
// kwayland server
|
|
|
|
#include "KWaylandServer/surface_interface.h"
|
|
|
|
#include "KWaylandServer/buffer_interface.h"
|
|
|
|
#include "KWaylandServer/linuxdmabuf_v1_interface.h"
|
2021-05-30 23:58:47 +00:00
|
|
|
#include "KWaylandServer/clientconnection.h"
|
2015-04-10 08:44:07 +00:00
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
2020-10-05 21:05:55 +00:00
|
|
|
EglGbmBackend::EglGbmBackend(DrmBackend *drmBackend, DrmGpu *gpu)
|
2020-11-28 17:53:41 +00:00
|
|
|
: AbstractEglDrmBackend(drmBackend, gpu)
|
2015-04-10 08:44:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-04-19 14:23:46 +00:00
|
|
|
EglGbmBackend::~EglGbmBackend()
|
|
|
|
{
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
|
2015-04-17 13:48:55 +00:00
|
|
|
void EglGbmBackend::cleanupSurfaces()
|
|
|
|
{
|
2020-01-02 14:55:09 +00:00
|
|
|
for (auto it = m_outputs.begin(); it != m_outputs.end(); ++it) {
|
2015-04-24 08:05:38 +00:00
|
|
|
cleanupOutput(*it);
|
|
|
|
}
|
2017-10-05 16:58:57 +00:00
|
|
|
m_outputs.clear();
|
2015-04-24 08:05:38 +00:00
|
|
|
}
|
|
|
|
|
2020-01-02 14:55:09 +00:00
|
|
|
void EglGbmBackend::cleanupFramebuffer(Output &output)
|
2015-04-24 08:05:38 +00:00
|
|
|
{
|
2020-01-02 14:55:09 +00:00
|
|
|
if (!output.render.framebuffer) {
|
|
|
|
return;
|
|
|
|
}
|
2020-11-28 17:53:41 +00:00
|
|
|
makeContextCurrent(output);
|
2020-01-02 14:55:09 +00:00
|
|
|
glDeleteTextures(1, &output.render.texture);
|
|
|
|
output.render.texture = 0;
|
|
|
|
glDeleteFramebuffers(1, &output.render.framebuffer);
|
|
|
|
output.render.framebuffer = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EglGbmBackend::cleanupOutput(Output &output)
|
|
|
|
{
|
|
|
|
cleanupFramebuffer(output);
|
2017-03-17 19:31:06 +00:00
|
|
|
|
2019-12-10 00:24:45 +00:00
|
|
|
if (output.eglSurface != EGL_NO_SURFACE) {
|
2021-05-02 20:38:36 +00:00
|
|
|
// gbm buffers have to be released before destroying the egl surface
|
|
|
|
output.output->releaseGbm();
|
2021-05-04 10:19:31 +00:00
|
|
|
output.secondaryBuffer = nullptr;
|
2019-12-10 00:24:45 +00:00
|
|
|
eglDestroySurface(eglDisplay(), output.eglSurface);
|
2015-04-24 08:05:38 +00:00
|
|
|
}
|
2015-04-17 13:48:55 +00:00
|
|
|
}
|
|
|
|
|
2015-04-10 08:44:07 +00:00
|
|
|
bool EglGbmBackend::initializeEgl()
|
|
|
|
{
|
|
|
|
initClientExtensions();
|
2020-10-16 13:11:23 +00:00
|
|
|
EGLDisplay display = m_gpu->eglDisplay();
|
2015-04-10 08:44:07 +00:00
|
|
|
|
|
|
|
// Use eglGetPlatformDisplayEXT() to get the display pointer
|
|
|
|
// if the implementation supports it.
|
2016-07-18 08:27:56 +00:00
|
|
|
if (display == EGL_NO_DISPLAY) {
|
check for EGL_KHR_platform_gbm extension as well
Summary:
some drivers, like Mali have EGL_KHR_platform_gbm
but not EGL_MESA_platform_gbm
Test Plan: pending a test on rock64 board
Reviewers: #kwin, #plasma, davidedmundson, graesslin
Reviewed By: #kwin, #plasma, graesslin
Subscribers: graesslin, garg, davidedmundson, plasma-devel, kwin
Tags: #kwin
Differential Revision: https://phabricator.kde.org/D10346
2018-02-15 10:41:10 +00:00
|
|
|
const bool hasMesaGBM = hasClientExtension(QByteArrayLiteral("EGL_MESA_platform_gbm"));
|
|
|
|
const bool hasKHRGBM = hasClientExtension(QByteArrayLiteral("EGL_KHR_platform_gbm"));
|
|
|
|
const GLenum platform = hasMesaGBM ? EGL_PLATFORM_GBM_MESA : EGL_PLATFORM_GBM_KHR;
|
|
|
|
|
2016-07-18 08:27:56 +00:00
|
|
|
if (!hasClientExtension(QByteArrayLiteral("EGL_EXT_platform_base")) ||
|
check for EGL_KHR_platform_gbm extension as well
Summary:
some drivers, like Mali have EGL_KHR_platform_gbm
but not EGL_MESA_platform_gbm
Test Plan: pending a test on rock64 board
Reviewers: #kwin, #plasma, davidedmundson, graesslin
Reviewed By: #kwin, #plasma, graesslin
Subscribers: graesslin, garg, davidedmundson, plasma-devel, kwin
Tags: #kwin
Differential Revision: https://phabricator.kde.org/D10346
2018-02-15 10:41:10 +00:00
|
|
|
(!hasMesaGBM && !hasKHRGBM)) {
|
2019-12-10 00:24:45 +00:00
|
|
|
setFailed("Missing one or more extensions between EGL_EXT_platform_base, "
|
|
|
|
"EGL_MESA_platform_gbm, EGL_KHR_platform_gbm");
|
2016-07-18 08:27:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-04-10 08:44:07 +00:00
|
|
|
|
2020-10-05 21:05:55 +00:00
|
|
|
auto device = gbm_create_device(m_gpu->fd());
|
2016-08-19 14:09:18 +00:00
|
|
|
if (!device) {
|
2016-07-18 08:27:56 +00:00
|
|
|
setFailed("Could not create gbm device");
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-05 21:05:55 +00:00
|
|
|
m_gpu->setGbmDevice(device);
|
2015-04-10 08:44:07 +00:00
|
|
|
|
check for EGL_KHR_platform_gbm extension as well
Summary:
some drivers, like Mali have EGL_KHR_platform_gbm
but not EGL_MESA_platform_gbm
Test Plan: pending a test on rock64 board
Reviewers: #kwin, #plasma, davidedmundson, graesslin
Reviewed By: #kwin, #plasma, graesslin
Subscribers: graesslin, garg, davidedmundson, plasma-devel, kwin
Tags: #kwin
Differential Revision: https://phabricator.kde.org/D10346
2018-02-15 10:41:10 +00:00
|
|
|
display = eglGetPlatformDisplayEXT(platform, device, nullptr);
|
2020-10-16 13:11:23 +00:00
|
|
|
m_gpu->setEglDisplay(display);
|
2016-07-18 08:27:56 +00:00
|
|
|
}
|
2015-04-10 08:44:07 +00:00
|
|
|
|
2019-12-10 00:24:45 +00:00
|
|
|
if (display == EGL_NO_DISPLAY) {
|
2015-04-10 08:44:07 +00:00
|
|
|
return false;
|
2019-12-10 00:24:45 +00:00
|
|
|
}
|
2015-04-10 08:44:07 +00:00
|
|
|
setEglDisplay(display);
|
|
|
|
return initEglAPI();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EglGbmBackend::init()
|
|
|
|
{
|
2015-11-25 12:09:28 +00:00
|
|
|
if (!initializeEgl()) {
|
|
|
|
setFailed("Could not initialize egl");
|
|
|
|
return;
|
|
|
|
}
|
2021-04-21 15:23:46 +00:00
|
|
|
|
|
|
|
if (!supportsSurfacelessContext()) {
|
|
|
|
setFailed("EGL_KHR_surfaceless_context extension is unavailable!");
|
2021-04-21 15:42:55 +00:00
|
|
|
return;
|
2021-04-21 15:23:46 +00:00
|
|
|
}
|
|
|
|
|
2015-04-10 08:44:07 +00:00
|
|
|
if (!initRenderingContext()) {
|
|
|
|
setFailed("Could not initialize rendering context");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
initBufferAge();
|
2020-11-28 17:53:41 +00:00
|
|
|
// at the moment: no secondary GPU -> no OpenGL context!
|
|
|
|
if (isPrimary()) {
|
|
|
|
initKWinGL();
|
|
|
|
initWayland();
|
|
|
|
}
|
2015-04-10 08:44:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool EglGbmBackend::initRenderingContext()
|
|
|
|
{
|
|
|
|
initBufferConfigs();
|
2021-04-20 10:13:58 +00:00
|
|
|
if (isPrimary()) {
|
2021-05-31 11:13:59 +00:00
|
|
|
if (!createContext() || !makeCurrent()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const auto outputs = m_gpu->outputs();
|
|
|
|
for (const auto &output : outputs) {
|
|
|
|
addOutput(output);
|
2020-11-28 17:53:41 +00:00
|
|
|
}
|
|
|
|
return true;
|
2015-04-10 08:44:07 +00:00
|
|
|
}
|
|
|
|
|
2021-03-22 14:46:09 +00:00
|
|
|
EGLSurface EglGbmBackend::createEglSurface(QSharedPointer<GbmSurface> gbmSurface) const
|
2019-12-10 00:24:45 +00:00
|
|
|
{
|
|
|
|
auto eglSurface = eglCreatePlatformWindowSurfaceEXT(eglDisplay(), config(),
|
|
|
|
(void *)(gbmSurface->surface()), nullptr);
|
|
|
|
if (eglSurface == EGL_NO_SURFACE) {
|
|
|
|
qCCritical(KWIN_DRM) << "Creating EGL surface failed";
|
|
|
|
return EGL_NO_SURFACE;
|
|
|
|
}
|
|
|
|
return eglSurface;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EglGbmBackend::resetOutput(Output &output, DrmOutput *drmOutput)
|
2015-04-24 08:05:38 +00:00
|
|
|
{
|
2019-12-10 00:24:45 +00:00
|
|
|
output.output = drmOutput;
|
2020-01-02 14:55:09 +00:00
|
|
|
const QSize size = drmOutput->hardwareTransforms() ? drmOutput->pixelSize() :
|
|
|
|
drmOutput->modeSize();
|
2021-01-31 00:31:15 +00:00
|
|
|
int flags = GBM_BO_USE_RENDERING;
|
|
|
|
if (drmOutput->gpu() == m_gpu) {
|
|
|
|
flags |= GBM_BO_USE_SCANOUT;
|
|
|
|
} else {
|
|
|
|
flags |= GBM_BO_USE_LINEAR;
|
|
|
|
}
|
2021-03-22 14:46:09 +00:00
|
|
|
auto gbmSurface = QSharedPointer<GbmSurface>::create(m_gpu->gbmDevice(),
|
|
|
|
size.width(), size.height(),
|
|
|
|
GBM_FORMAT_XRGB8888,
|
|
|
|
flags);
|
2017-10-21 13:16:41 +00:00
|
|
|
if (!gbmSurface) {
|
2021-01-31 00:31:15 +00:00
|
|
|
qCCritical(KWIN_DRM) << "Creating GBM surface failed";
|
2017-10-21 13:16:41 +00:00
|
|
|
return false;
|
2015-04-24 08:05:38 +00:00
|
|
|
}
|
2019-12-10 00:24:45 +00:00
|
|
|
auto eglSurface = createEglSurface(gbmSurface);
|
2017-10-21 13:16:41 +00:00
|
|
|
if (eglSurface == EGL_NO_SURFACE) {
|
|
|
|
return false;
|
2019-12-10 00:24:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// destroy previous surface
|
|
|
|
if (output.eglSurface != EGL_NO_SURFACE) {
|
2021-05-02 20:38:36 +00:00
|
|
|
// gbm buffers have to be released before destroying the egl surface
|
|
|
|
output.output->releaseGbm();
|
2021-05-04 10:19:31 +00:00
|
|
|
output.secondaryBuffer = nullptr;
|
2019-12-10 00:24:45 +00:00
|
|
|
eglDestroySurface(eglDisplay(), output.eglSurface);
|
2017-10-21 13:16:41 +00:00
|
|
|
}
|
2019-12-10 00:24:45 +00:00
|
|
|
output.eglSurface = eglSurface;
|
|
|
|
output.gbmSurface = gbmSurface;
|
2020-01-02 14:55:09 +00:00
|
|
|
|
|
|
|
resetFramebuffer(output);
|
2017-10-21 13:16:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-23 17:45:05 +00:00
|
|
|
bool EglGbmBackend::addOutput(DrmOutput *drmOutput)
|
2017-10-21 13:16:41 +00:00
|
|
|
{
|
2020-11-28 17:53:41 +00:00
|
|
|
if (isPrimary()) {
|
|
|
|
Output newOutput;
|
|
|
|
if (resetOutput(newOutput, drmOutput)) {
|
2020-12-01 16:58:24 +00:00
|
|
|
QVector<Output> &outputs = drmOutput->gpu() == m_gpu ? m_outputs : m_secondaryGpuOutputs;
|
2020-11-28 17:53:41 +00:00
|
|
|
connect(drmOutput, &DrmOutput::modeChanged, this,
|
2020-12-01 16:58:24 +00:00
|
|
|
[drmOutput, &outputs, this] {
|
|
|
|
auto it = std::find_if(outputs.begin(), outputs.end(),
|
2020-11-28 17:53:41 +00:00
|
|
|
[drmOutput] (const auto &output) {
|
|
|
|
return output.output == drmOutput;
|
|
|
|
}
|
|
|
|
);
|
2020-12-01 16:58:24 +00:00
|
|
|
if (it == outputs.end()) {
|
2020-11-28 17:53:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
resetOutput(*it, drmOutput);
|
|
|
|
}
|
|
|
|
);
|
2020-12-01 16:58:24 +00:00
|
|
|
outputs << newOutput;
|
2021-05-23 17:45:05 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
2020-11-28 17:53:41 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Output newOutput;
|
|
|
|
newOutput.output = drmOutput;
|
2021-05-23 17:45:05 +00:00
|
|
|
if (!renderingBackend()->addOutput(drmOutput)) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-11-28 17:53:41 +00:00
|
|
|
m_outputs << newOutput;
|
|
|
|
}
|
2021-05-23 17:45:05 +00:00
|
|
|
return true;
|
2020-11-28 17:53:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EglGbmBackend::removeOutput(DrmOutput *drmOutput)
|
|
|
|
{
|
2020-12-01 16:58:24 +00:00
|
|
|
QVector<Output> &outputs = drmOutput->gpu() == m_gpu ? m_outputs : m_secondaryGpuOutputs;
|
|
|
|
auto it = std::find_if(outputs.begin(), outputs.end(),
|
2020-11-28 17:53:41 +00:00
|
|
|
[drmOutput] (const Output &output) {
|
|
|
|
return output.output == drmOutput;
|
|
|
|
}
|
|
|
|
);
|
2020-12-01 16:58:24 +00:00
|
|
|
if (it == outputs.end()) {
|
2020-11-28 17:53:41 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-12-01 16:58:24 +00:00
|
|
|
if (isPrimary()) {
|
2020-11-28 17:53:41 +00:00
|
|
|
cleanupOutput(*it);
|
2020-12-01 16:58:24 +00:00
|
|
|
} else {
|
|
|
|
renderingBackend()->removeOutput((*it).output);
|
2020-11-28 17:53:41 +00:00
|
|
|
}
|
2020-12-01 16:58:24 +00:00
|
|
|
outputs.erase(it);
|
2015-04-24 08:05:38 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 10:19:31 +00:00
|
|
|
bool EglGbmBackend::swapBuffers(DrmOutput *drmOutput)
|
2019-12-10 00:24:45 +00:00
|
|
|
{
|
2020-11-28 17:53:41 +00:00
|
|
|
auto it = std::find_if(m_secondaryGpuOutputs.begin(), m_secondaryGpuOutputs.end(),
|
2019-12-10 00:24:45 +00:00
|
|
|
[drmOutput] (const Output &output) {
|
|
|
|
return output.output == drmOutput;
|
|
|
|
}
|
|
|
|
);
|
2020-11-28 17:53:41 +00:00
|
|
|
if (it == m_secondaryGpuOutputs.end()) {
|
2021-05-04 10:19:31 +00:00
|
|
|
return false;
|
2020-11-28 17:53:41 +00:00
|
|
|
}
|
2020-12-01 16:58:24 +00:00
|
|
|
renderFramebufferToSurface(*it);
|
2020-11-28 17:53:41 +00:00
|
|
|
auto error = eglSwapBuffers(eglDisplay(), it->eglSurface);
|
|
|
|
if (error != EGL_TRUE) {
|
2021-05-23 17:45:05 +00:00
|
|
|
qCCritical(KWIN_DRM) << "an error occurred while swapping buffers" << error;
|
2021-03-29 11:17:44 +00:00
|
|
|
it->secondaryBuffer = nullptr;
|
2021-05-04 10:19:31 +00:00
|
|
|
return false;
|
2020-11-28 17:53:41 +00:00
|
|
|
}
|
2021-03-29 11:17:44 +00:00
|
|
|
it->secondaryBuffer = QSharedPointer<GbmBuffer>::create(it->gbmSurface);
|
2021-05-04 10:19:31 +00:00
|
|
|
if (it->secondaryBuffer->getBo()) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
it->secondaryBuffer = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EglGbmBackend::exportFramebuffer(DrmOutput *drmOutput, void *data, const QSize &size, uint32_t stride)
|
|
|
|
{
|
|
|
|
auto it = std::find_if(m_secondaryGpuOutputs.begin(), m_secondaryGpuOutputs.end(),
|
|
|
|
[drmOutput] (const Output &output) {
|
|
|
|
return output.output == drmOutput;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if (it == m_secondaryGpuOutputs.end()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!it->secondaryBuffer || !it->secondaryBuffer->map(GBM_BO_TRANSFER_READ)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (stride != it->secondaryBuffer->stride()) {
|
|
|
|
// shouldn't happen if formats are the same
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return memcpy(data, it->secondaryBuffer->mappedData(), size.height() * stride);
|
|
|
|
}
|
|
|
|
|
|
|
|
int EglGbmBackend::exportFramebufferAsDmabuf(DrmOutput *output, uint32_t *format, uint32_t *stride)
|
|
|
|
{
|
|
|
|
DrmOutput *drmOutput = static_cast<DrmOutput*>(output);
|
|
|
|
auto it = std::find_if(m_secondaryGpuOutputs.begin(), m_secondaryGpuOutputs.end(),
|
|
|
|
[drmOutput] (const Output &output) {
|
|
|
|
return output.output == drmOutput;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if (it == m_secondaryGpuOutputs.end()) {
|
|
|
|
return -1;
|
|
|
|
}
|
2021-04-11 18:48:47 +00:00
|
|
|
int fd = gbm_bo_get_fd(it->secondaryBuffer->getBo());
|
2020-11-28 17:53:41 +00:00
|
|
|
if (fd == -1) {
|
|
|
|
qCDebug(KWIN_DRM) << "failed to export gbm_bo as dma-buf!";
|
|
|
|
return -1;
|
|
|
|
}
|
2021-04-11 18:48:47 +00:00
|
|
|
*format = gbm_bo_get_format(it->secondaryBuffer->getBo());
|
|
|
|
*stride = gbm_bo_get_stride(it->secondaryBuffer->getBo());
|
2021-03-29 11:17:44 +00:00
|
|
|
return fd;
|
2020-11-28 17:53:41 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 10:19:31 +00:00
|
|
|
QRegion EglGbmBackend::beginFrameForSecondaryGpu(DrmOutput *drmOutput)
|
2020-11-28 17:53:41 +00:00
|
|
|
{
|
|
|
|
auto it = std::find_if(m_secondaryGpuOutputs.begin(), m_secondaryGpuOutputs.end(),
|
|
|
|
[drmOutput] (const Output &output) {
|
|
|
|
return output.output == drmOutput;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if (it == m_secondaryGpuOutputs.end()) {
|
|
|
|
return QRegion();
|
|
|
|
}
|
|
|
|
return prepareRenderingForOutput(*it);
|
|
|
|
}
|
|
|
|
|
2021-05-04 10:19:31 +00:00
|
|
|
void EglGbmBackend::importFramebuffer(Output &output) const
|
|
|
|
{
|
|
|
|
if (!renderingBackend()->swapBuffers(output.output)) {
|
|
|
|
qCWarning(KWIN_DRM) << "swapping buffers failed on output" << output.output;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
output.buffer = nullptr;
|
|
|
|
const auto size = output.output->modeSize();
|
|
|
|
if (output.importMode == ImportMode::Dmabuf) {
|
|
|
|
uint32_t stride = 0;
|
|
|
|
uint32_t format = 0;
|
|
|
|
int fd = renderingBackend()->exportFramebufferAsDmabuf(output.output, &format, &stride);
|
|
|
|
if (fd != -1) {
|
|
|
|
struct gbm_import_fd_data data = {};
|
|
|
|
data.fd = fd;
|
|
|
|
data.width = (uint32_t) size.width();
|
|
|
|
data.height = (uint32_t) size.height();
|
|
|
|
data.stride = stride;
|
|
|
|
data.format = format;
|
|
|
|
gbm_bo *importedBuffer = gbm_bo_import(m_gpu->gbmDevice(), GBM_BO_IMPORT_FD, &data, GBM_BO_USE_SCANOUT | GBM_BO_USE_LINEAR);
|
|
|
|
close(fd);
|
|
|
|
if (importedBuffer) {
|
|
|
|
auto buffer = QSharedPointer<DrmGbmBuffer>::create(m_gpu, importedBuffer, nullptr);
|
|
|
|
if (buffer->bufferId() > 0) {
|
|
|
|
output.buffer = buffer;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
qCDebug(KWIN_DRM) << "import with dmabuf failed! Switching to CPU import on output" << output.output;
|
|
|
|
output.importMode = ImportMode::DumbBuffer;
|
|
|
|
}
|
|
|
|
// ImportMode::DumbBuffer
|
|
|
|
if (!output.importSwapchain || output.importSwapchain->size() != size) {
|
|
|
|
output.importSwapchain = QSharedPointer<DumbSwapchain>::create(m_gpu, size);
|
|
|
|
if (output.importSwapchain->isEmpty()) {
|
|
|
|
output.importSwapchain = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (output.importSwapchain) {
|
|
|
|
auto buffer = output.importSwapchain->acquireBuffer();
|
|
|
|
if (renderingBackend()->exportFramebuffer(output.output, buffer->data(), size, buffer->stride())) {
|
|
|
|
output.buffer = buffer;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
qCWarning(KWIN_DRM) << "all imports failed on output" << output.output;
|
|
|
|
// TODO turn off output?
|
|
|
|
}
|
|
|
|
|
2020-01-02 14:55:09 +00:00
|
|
|
const float vertices[] = {
|
|
|
|
-1.0f, 1.0f,
|
|
|
|
-1.0f, -1.0f,
|
|
|
|
1.0f, -1.0f,
|
|
|
|
|
|
|
|
-1.0f, 1.0f,
|
|
|
|
1.0f, -1.0f,
|
|
|
|
1.0f, 1.0f,
|
|
|
|
};
|
|
|
|
|
|
|
|
const float texCoords[] = {
|
|
|
|
0.0f, 1.0f,
|
|
|
|
0.0f, 0.0f,
|
|
|
|
1.0f, 0.0f,
|
|
|
|
|
|
|
|
0.0f, 1.0f,
|
|
|
|
1.0f, 0.0f,
|
|
|
|
1.0f, 1.0f
|
|
|
|
};
|
|
|
|
|
|
|
|
bool EglGbmBackend::resetFramebuffer(Output &output)
|
|
|
|
{
|
2020-12-02 15:24:39 +00:00
|
|
|
cleanupFramebuffer(output);
|
|
|
|
|
2021-01-31 00:31:15 +00:00
|
|
|
if (output.output->hardwareTransforms()) {
|
2020-01-02 14:55:09 +00:00
|
|
|
// No need for an extra render target.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
makeContextCurrent(output);
|
|
|
|
|
|
|
|
glGenFramebuffers(1, &output.render.framebuffer);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, output.render.framebuffer);
|
|
|
|
GLRenderTarget::setKWinFramebuffer(output.render.framebuffer);
|
|
|
|
|
|
|
|
glGenTextures(1, &output.render.texture);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, output.render.texture);
|
|
|
|
|
|
|
|
const QSize texSize = output.output->pixelSize();
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texSize.width(), texSize.height(),
|
|
|
|
0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
|
2020-11-28 17:53:41 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
2020-01-02 14:55:09 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
|
|
|
|
output.render.texture, 0);
|
|
|
|
|
|
|
|
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
|
|
|
|
qCWarning(KWIN_DRM) << "Error: framebuffer not complete";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
GLRenderTarget::setKWinFramebuffer(0);
|
2020-11-28 17:53:41 +00:00
|
|
|
|
2020-01-02 14:55:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-02-28 12:22:35 +00:00
|
|
|
void EglGbmBackend::initRenderTarget(Output &output)
|
2020-01-02 14:55:09 +00:00
|
|
|
{
|
2020-02-28 12:22:35 +00:00
|
|
|
if (output.render.vbo) {
|
2020-01-02 14:55:09 +00:00
|
|
|
// Already initialized.
|
2020-02-28 12:22:35 +00:00
|
|
|
return;
|
2020-01-02 14:55:09 +00:00
|
|
|
}
|
2021-03-22 14:46:09 +00:00
|
|
|
QSharedPointer<GLVertexBuffer> vbo(new GLVertexBuffer(KWin::GLVertexBuffer::Static));
|
2020-01-02 14:55:09 +00:00
|
|
|
vbo->setData(6, 2, vertices, texCoords);
|
|
|
|
output.render.vbo = vbo;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EglGbmBackend::renderFramebufferToSurface(Output &output)
|
|
|
|
{
|
2021-05-04 10:19:31 +00:00
|
|
|
if (!output.render.framebuffer) {
|
2020-01-02 14:55:09 +00:00
|
|
|
// No additional render target.
|
|
|
|
return;
|
|
|
|
}
|
2021-05-04 10:19:31 +00:00
|
|
|
makeContextCurrent(output);
|
2020-01-02 14:55:09 +00:00
|
|
|
|
2021-05-04 10:19:31 +00:00
|
|
|
const auto size = output.output->modeSize();
|
|
|
|
glViewport(0, 0, size.width(), size.height());
|
|
|
|
|
|
|
|
auto shader = ShaderManager::instance()->pushShader(ShaderTrait::MapTexture);
|
|
|
|
|
|
|
|
QMatrix4x4 mvpMatrix;
|
|
|
|
|
|
|
|
const DrmOutput *drmOutput = output.output;
|
|
|
|
switch (drmOutput->transform()) {
|
|
|
|
case DrmOutput::Transform::Normal:
|
|
|
|
case DrmOutput::Transform::Flipped:
|
|
|
|
break;
|
|
|
|
case DrmOutput::Transform::Rotated90:
|
|
|
|
case DrmOutput::Transform::Flipped90:
|
|
|
|
mvpMatrix.rotate(90, 0, 0, 1);
|
|
|
|
break;
|
|
|
|
case DrmOutput::Transform::Rotated180:
|
|
|
|
case DrmOutput::Transform::Flipped180:
|
|
|
|
mvpMatrix.rotate(180, 0, 0, 1);
|
|
|
|
break;
|
|
|
|
case DrmOutput::Transform::Rotated270:
|
|
|
|
case DrmOutput::Transform::Flipped270:
|
|
|
|
mvpMatrix.rotate(270, 0, 0, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (drmOutput->transform()) {
|
|
|
|
case DrmOutput::Transform::Flipped:
|
|
|
|
case DrmOutput::Transform::Flipped90:
|
|
|
|
case DrmOutput::Transform::Flipped180:
|
|
|
|
case DrmOutput::Transform::Flipped270:
|
|
|
|
mvpMatrix.scale(-1, 1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
shader->setUniform(GLShader::ModelViewProjectionMatrix, mvpMatrix);
|
|
|
|
|
|
|
|
initRenderTarget(output);
|
2020-01-02 14:55:09 +00:00
|
|
|
|
2021-05-04 10:19:31 +00:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
GLRenderTarget::setKWinFramebuffer(0);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, output.render.texture);
|
|
|
|
output.render.vbo->render(GL_TRIANGLES);
|
|
|
|
ShaderManager::instance()->popShader();
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
2020-01-02 14:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EglGbmBackend::prepareRenderFramebuffer(const Output &output) const
|
|
|
|
{
|
|
|
|
// When render.framebuffer is 0 we may just reset to the screen framebuffer.
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, output.render.framebuffer);
|
|
|
|
GLRenderTarget::setKWinFramebuffer(output.render.framebuffer);
|
|
|
|
}
|
|
|
|
|
2019-12-10 00:24:45 +00:00
|
|
|
bool EglGbmBackend::makeContextCurrent(const Output &output) const
|
2015-04-10 08:44:07 +00:00
|
|
|
{
|
2020-11-28 17:53:41 +00:00
|
|
|
Q_ASSERT(isPrimary());
|
2015-04-17 13:48:55 +00:00
|
|
|
const EGLSurface surface = output.eglSurface;
|
|
|
|
if (surface == EGL_NO_SURFACE) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (eglMakeCurrent(eglDisplay(), surface, surface, context()) == EGL_FALSE) {
|
2020-09-23 00:03:45 +00:00
|
|
|
qCCritical(KWIN_DRM) << "Make Context Current failed" << eglGetError();
|
2015-04-10 08:44:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EglGbmBackend::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,
|
2015-04-10 08:44:07 +00:00
|
|
|
EGL_CONFIG_CAVEAT, EGL_NONE,
|
|
|
|
EGL_NONE,
|
|
|
|
};
|
|
|
|
|
|
|
|
EGLint count;
|
|
|
|
EGLConfig configs[1024];
|
2019-12-10 00:24:45 +00:00
|
|
|
if (!eglChooseConfig(eglDisplay(), config_attribs, configs,
|
|
|
|
sizeof(configs) / sizeof(EGLConfig),
|
|
|
|
&count)) {
|
2015-05-05 15:27:03 +00:00
|
|
|
qCCritical(KWIN_DRM) << "choose config failed";
|
2015-04-10 08:44:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
drm backend: choose correct EGL config with mesa-18
Summary:
Do not blindly select first EGL config from returned list, but choose the one that matches GBM surfaces, that will be created later.
GBM surfaces are created with GBM_FORMAT_XRGB8888 format, so choose the config that matches it.
With wrong format EglGbmBackend::resetOutput() will later fail with error EGL_BAD_MATCH.
Test Plan: Compile, run startplasmacompositor. Verify that OpenGL compositing is used, either by kwin debug console, or by kwin support information.
Reviewers: graesslin, davidedmundson, #kwin, #plasma_on_wayland, bshah
Reviewed By: davidedmundson
Subscribers: zzag, kwin, #kwin
Tags: #kwin, #plasma_on_wayland
Differential Revision: https://phabricator.kde.org/D11758
2018-04-04 22:20:46 +00:00
|
|
|
|
|
|
|
qCDebug(KWIN_DRM) << "EGL buffer configs count:" << count;
|
|
|
|
|
2019-12-10 00:24:45 +00:00
|
|
|
// Loop through all configs, choosing the first one that has suitable format.
|
drm backend: choose correct EGL config with mesa-18
Summary:
Do not blindly select first EGL config from returned list, but choose the one that matches GBM surfaces, that will be created later.
GBM surfaces are created with GBM_FORMAT_XRGB8888 format, so choose the config that matches it.
With wrong format EglGbmBackend::resetOutput() will later fail with error EGL_BAD_MATCH.
Test Plan: Compile, run startplasmacompositor. Verify that OpenGL compositing is used, either by kwin debug console, or by kwin support information.
Reviewers: graesslin, davidedmundson, #kwin, #plasma_on_wayland, bshah
Reviewed By: davidedmundson
Subscribers: zzag, kwin, #kwin
Tags: #kwin, #plasma_on_wayland
Differential Revision: https://phabricator.kde.org/D11758
2018-04-04 22:20:46 +00:00
|
|
|
for (EGLint i = 0; i < count; i++) {
|
|
|
|
EGLint gbmFormat;
|
2019-12-10 00:24:45 +00:00
|
|
|
// Query some configuration parameters, to show in debug log.
|
drm backend: choose correct EGL config with mesa-18
Summary:
Do not blindly select first EGL config from returned list, but choose the one that matches GBM surfaces, that will be created later.
GBM surfaces are created with GBM_FORMAT_XRGB8888 format, so choose the config that matches it.
With wrong format EglGbmBackend::resetOutput() will later fail with error EGL_BAD_MATCH.
Test Plan: Compile, run startplasmacompositor. Verify that OpenGL compositing is used, either by kwin debug console, or by kwin support information.
Reviewers: graesslin, davidedmundson, #kwin, #plasma_on_wayland, bshah
Reviewed By: davidedmundson
Subscribers: zzag, kwin, #kwin
Tags: #kwin, #plasma_on_wayland
Differential Revision: https://phabricator.kde.org/D11758
2018-04-04 22:20:46 +00:00
|
|
|
eglGetConfigAttrib(eglDisplay(), configs[i], EGL_NATIVE_VISUAL_ID, &gbmFormat);
|
|
|
|
|
|
|
|
if (KWIN_DRM().isDebugEnabled()) {
|
2019-12-10 00:24:45 +00:00
|
|
|
// GBM formats are declared as FOURCC code (integer from ASCII chars, so use this fact).
|
drm backend: choose correct EGL config with mesa-18
Summary:
Do not blindly select first EGL config from returned list, but choose the one that matches GBM surfaces, that will be created later.
GBM surfaces are created with GBM_FORMAT_XRGB8888 format, so choose the config that matches it.
With wrong format EglGbmBackend::resetOutput() will later fail with error EGL_BAD_MATCH.
Test Plan: Compile, run startplasmacompositor. Verify that OpenGL compositing is used, either by kwin debug console, or by kwin support information.
Reviewers: graesslin, davidedmundson, #kwin, #plasma_on_wayland, bshah
Reviewed By: davidedmundson
Subscribers: zzag, kwin, #kwin
Tags: #kwin, #plasma_on_wayland
Differential Revision: https://phabricator.kde.org/D11758
2018-04-04 22:20:46 +00:00
|
|
|
char gbmFormatStr[sizeof(EGLint) + 1] = {0};
|
|
|
|
memcpy(gbmFormatStr, &gbmFormat, sizeof(EGLint));
|
2019-12-10 00:24:45 +00:00
|
|
|
|
|
|
|
// Query number of bits for color channel.
|
drm backend: choose correct EGL config with mesa-18
Summary:
Do not blindly select first EGL config from returned list, but choose the one that matches GBM surfaces, that will be created later.
GBM surfaces are created with GBM_FORMAT_XRGB8888 format, so choose the config that matches it.
With wrong format EglGbmBackend::resetOutput() will later fail with error EGL_BAD_MATCH.
Test Plan: Compile, run startplasmacompositor. Verify that OpenGL compositing is used, either by kwin debug console, or by kwin support information.
Reviewers: graesslin, davidedmundson, #kwin, #plasma_on_wayland, bshah
Reviewed By: davidedmundson
Subscribers: zzag, kwin, #kwin
Tags: #kwin, #plasma_on_wayland
Differential Revision: https://phabricator.kde.org/D11758
2018-04-04 22:20:46 +00:00
|
|
|
EGLint blueSize, redSize, greenSize, alphaSize;
|
|
|
|
eglGetConfigAttrib(eglDisplay(), configs[i], EGL_RED_SIZE, &redSize);
|
|
|
|
eglGetConfigAttrib(eglDisplay(), configs[i], EGL_GREEN_SIZE, &greenSize);
|
|
|
|
eglGetConfigAttrib(eglDisplay(), configs[i], EGL_BLUE_SIZE, &blueSize);
|
|
|
|
eglGetConfigAttrib(eglDisplay(), configs[i], EGL_ALPHA_SIZE, &alphaSize);
|
|
|
|
qCDebug(KWIN_DRM) << " EGL config #" << i << " has GBM FOURCC format:" << gbmFormatStr
|
2019-12-10 00:24:45 +00:00
|
|
|
<< "; color sizes (RGBA order):"
|
|
|
|
<< redSize << greenSize << blueSize << alphaSize;
|
drm backend: choose correct EGL config with mesa-18
Summary:
Do not blindly select first EGL config from returned list, but choose the one that matches GBM surfaces, that will be created later.
GBM surfaces are created with GBM_FORMAT_XRGB8888 format, so choose the config that matches it.
With wrong format EglGbmBackend::resetOutput() will later fail with error EGL_BAD_MATCH.
Test Plan: Compile, run startplasmacompositor. Verify that OpenGL compositing is used, either by kwin debug console, or by kwin support information.
Reviewers: graesslin, davidedmundson, #kwin, #plasma_on_wayland, bshah
Reviewed By: davidedmundson
Subscribers: zzag, kwin, #kwin
Tags: #kwin, #plasma_on_wayland
Differential Revision: https://phabricator.kde.org/D11758
2018-04-04 22:20:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((gbmFormat == GBM_FORMAT_XRGB8888) || (gbmFormat == GBM_FORMAT_ARGB8888)) {
|
|
|
|
setConfig(configs[i]);
|
|
|
|
return true;
|
|
|
|
}
|
2015-04-10 08:44:07 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 00:24:45 +00:00
|
|
|
qCCritical(KWIN_DRM) << "Choosing EGL config did not return a suitable config. There were"
|
|
|
|
<< count << "configs.";
|
drm backend: choose correct EGL config with mesa-18
Summary:
Do not blindly select first EGL config from returned list, but choose the one that matches GBM surfaces, that will be created later.
GBM surfaces are created with GBM_FORMAT_XRGB8888 format, so choose the config that matches it.
With wrong format EglGbmBackend::resetOutput() will later fail with error EGL_BAD_MATCH.
Test Plan: Compile, run startplasmacompositor. Verify that OpenGL compositing is used, either by kwin debug console, or by kwin support information.
Reviewers: graesslin, davidedmundson, #kwin, #plasma_on_wayland, bshah
Reviewed By: davidedmundson
Subscribers: zzag, kwin, #kwin
Tags: #kwin, #plasma_on_wayland
Differential Revision: https://phabricator.kde.org/D11758
2018-04-04 22:20:46 +00:00
|
|
|
return false;
|
2015-04-10 08:44:07 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 17:11:41 +00:00
|
|
|
static QVector<EGLint> regionToRects(const QRegion ®ion, AbstractWaylandOutput *output)
|
|
|
|
{
|
|
|
|
const int height = output->modeSize().height();
|
|
|
|
|
2020-10-13 20:57:15 +00:00
|
|
|
const QMatrix4x4 matrix = DrmOutput::logicalToNativeMatrix(output->geometry(),
|
|
|
|
output->scale(),
|
|
|
|
output->transform());
|
2020-04-24 17:11:41 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-10-30 07:39:17 +00:00
|
|
|
void EglGbmBackend::aboutToStartPainting(int screenId, const QRegion &damagedRegion)
|
2020-04-24 17:11:41 +00:00
|
|
|
{
|
2020-10-30 07:39:17 +00:00
|
|
|
Q_ASSERT_X(screenId != -1, "aboutToStartPainting", "not using per screen rendering");
|
|
|
|
const Output &output = m_outputs.at(screenId);
|
2020-04-24 17:11:41 +00:00
|
|
|
if (output.bufferAge > 0 && !damagedRegion.isEmpty() && supportsPartialUpdate()) {
|
|
|
|
const QRegion region = damagedRegion & output.output->geometry();
|
|
|
|
|
|
|
|
QVector<EGLint> rects = regionToRects(region, output.output);
|
|
|
|
const bool correct = eglSetDamageRegionKHR(eglDisplay(), output.eglSurface,
|
|
|
|
rects.data(), rects.count()/4);
|
|
|
|
if (!correct) {
|
|
|
|
qCWarning(KWIN_DRM) << "failed eglSetDamageRegionKHR" << eglGetError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-29 11:24:38 +00:00
|
|
|
bool EglGbmBackend::presentOnOutput(Output &output, const QRegion &damagedRegion)
|
2015-04-23 07:55:49 +00:00
|
|
|
{
|
2021-03-29 11:17:44 +00:00
|
|
|
if (isPrimary() && !directScanoutActive(output)) {
|
2020-11-28 17:53:41 +00:00
|
|
|
if (supportsSwapBuffersWithDamage()) {
|
2021-04-06 08:53:26 +00:00
|
|
|
QVector<EGLint> rects = regionToRects(damagedRegion, output.output);
|
2020-11-29 11:24:38 +00:00
|
|
|
if (!eglSwapBuffersWithDamageEXT(eglDisplay(), output.eglSurface,
|
|
|
|
rects.data(), rects.count() / 4)) {
|
|
|
|
qCCritical(KWIN_DRM, "eglSwapBuffersWithDamageEXT() failed: %x", eglGetError());
|
|
|
|
return false;
|
|
|
|
}
|
2020-11-28 17:53:41 +00:00
|
|
|
} else {
|
2020-11-29 11:24:38 +00:00
|
|
|
if (!eglSwapBuffers(eglDisplay(), output.eglSurface)) {
|
|
|
|
qCCritical(KWIN_DRM, "eglSwapBuffers() failed: %x", eglGetError());
|
|
|
|
return false;
|
|
|
|
}
|
2020-11-28 17:53:41 +00:00
|
|
|
}
|
2021-03-29 11:17:44 +00:00
|
|
|
output.buffer = QSharedPointer<DrmGbmBuffer>::create(m_gpu, output.gbmSurface);
|
|
|
|
} else if (!output.buffer) {
|
2021-05-04 10:19:31 +00:00
|
|
|
qCDebug(KWIN_DRM) << "imported buffer does not exist!";
|
2020-11-29 11:24:38 +00:00
|
|
|
return false;
|
2020-04-24 17:11:41 +00:00
|
|
|
}
|
2019-12-10 00:24:45 +00:00
|
|
|
|
2020-07-22 17:38:57 +00:00
|
|
|
Q_EMIT output.output->outputChange(damagedRegion);
|
2021-03-20 18:18:52 +00:00
|
|
|
if (!output.output->present(output.buffer)) {
|
2020-11-29 11:24:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
GBM remote access support for KWin
Summary:
Implements a KWayland protocol to pass GBM fd from KWin to KRfb and
addictions to relevant projects from both sides.
Note that this patch does not affect default behaviour of mentioned projects. It can be used
only with KWIN_REMOTE=1 in env from KWin side and with preferredFrameBufferPlugin=gbm in krfbrc from
KRfb side. In all other aspects app behaviour remains unchanged.
Test Plan: Launched KWin in Wayland mode, launched KRfb in it, launched KRDC on a laptop, connected in read-only mode, observed a correctly retrieved desktop with Krfb window
Reviewers: graesslin, davidedmundson, romangg, #kwin
Reviewed By: davidedmundson, romangg, #kwin
Subscribers: kossebau, jgrulich, romangg, ngraham, alexeymin, aacid, kwin, #kwin, davidedmundson, plasma-devel
Tags: #plasma_on_wayland, #kwin
Maniphest Tasks: T5653, T7785
Differential Revision: https://phabricator.kde.org/D1230
2016-04-30 23:01:44 +00:00
|
|
|
|
2015-04-23 07:55:49 +00:00
|
|
|
if (supportsBufferAge()) {
|
2019-12-10 00:24:45 +00:00
|
|
|
eglQuerySurface(eglDisplay(), output.eglSurface, EGL_BUFFER_AGE_EXT, &output.bufferAge);
|
2015-04-23 07:55:49 +00:00
|
|
|
}
|
2020-11-29 11:24:38 +00:00
|
|
|
return true;
|
2015-04-23 07:55:49 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 11:17:44 +00:00
|
|
|
bool EglGbmBackend::directScanoutActive(const Output &output)
|
|
|
|
{
|
|
|
|
return output.surfaceInterface != nullptr;
|
|
|
|
}
|
|
|
|
|
2021-04-09 07:06:04 +00:00
|
|
|
PlatformSurfaceTexture *EglGbmBackend::createPlatformSurfaceTextureInternal(SurfacePixmapInternal *pixmap)
|
2015-04-10 08:44:07 +00:00
|
|
|
{
|
2021-04-09 07:06:04 +00:00
|
|
|
return new BasicEGLSurfaceTextureInternal(this, pixmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
PlatformSurfaceTexture *EglGbmBackend::createPlatformSurfaceTextureWayland(SurfacePixmapWayland *pixmap)
|
|
|
|
{
|
|
|
|
return new BasicEGLSurfaceTextureWayland(this, pixmap);
|
2015-04-10 08:44:07 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 00:24:45 +00:00
|
|
|
void EglGbmBackend::setViewport(const Output &output) const
|
|
|
|
{
|
2021-06-08 20:25:40 +00:00
|
|
|
const QSize size = output.output->pixelSize();
|
|
|
|
glViewport(0, 0, size.width(), size.height());
|
2019-12-10 00:24:45 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 14:31:26 +00:00
|
|
|
QRegion EglGbmBackend::beginFrame(int screenId)
|
2015-04-23 07:55:49 +00:00
|
|
|
{
|
2021-03-18 14:22:31 +00:00
|
|
|
Output &output = m_outputs[screenId];
|
2021-05-30 23:58:47 +00:00
|
|
|
if (output.surfaceInterface) {
|
|
|
|
qCDebug(KWIN_DRM) << "Direct scanout stopped on output" << output.output->name();
|
|
|
|
}
|
2021-03-29 11:17:44 +00:00
|
|
|
output.surfaceInterface = nullptr;
|
2020-11-28 17:53:41 +00:00
|
|
|
if (isPrimary()) {
|
2021-02-02 15:03:43 +00:00
|
|
|
return prepareRenderingForOutput(output);
|
2020-11-28 17:53:41 +00:00
|
|
|
} else {
|
2021-02-02 15:03:43 +00:00
|
|
|
return renderingBackend()->beginFrameForSecondaryGpu(output.output);
|
2020-11-28 17:53:41 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-10 00:24:45 +00:00
|
|
|
|
2021-02-02 13:26:43 +00:00
|
|
|
QRegion EglGbmBackend::prepareRenderingForOutput(Output &output) const
|
2020-11-28 17:53:41 +00:00
|
|
|
{
|
2019-12-10 00:24:45 +00:00
|
|
|
makeContextCurrent(output);
|
2020-01-02 14:55:09 +00:00
|
|
|
prepareRenderFramebuffer(output);
|
2019-12-10 00:24:45 +00:00
|
|
|
setViewport(output);
|
|
|
|
|
2015-04-17 13:48:55 +00:00
|
|
|
if (supportsBufferAge()) {
|
2015-04-23 07:55:49 +00:00
|
|
|
QRegion region;
|
|
|
|
|
|
|
|
// Note: An age of zero means the buffer contents are undefined
|
2019-12-10 00:24:45 +00:00
|
|
|
if (output.bufferAge > 0 && output.bufferAge <= output.damageHistory.count()) {
|
|
|
|
for (int i = 0; i < output.bufferAge - 1; i++)
|
|
|
|
region |= output.damageHistory[i];
|
2015-04-23 07:55:49 +00:00
|
|
|
} else {
|
2019-12-10 00:24:45 +00:00
|
|
|
region = output.output->geometry();
|
2015-04-17 13:48:55 +00:00
|
|
|
}
|
2015-04-23 07:55:49 +00:00
|
|
|
|
|
|
|
return region;
|
2015-04-17 13:48:55 +00:00
|
|
|
}
|
2020-06-20 00:42:02 +00:00
|
|
|
return output.output->geometry();
|
2015-04-10 08:44:07 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 14:31:26 +00:00
|
|
|
void EglGbmBackend::endFrame(int screenId, const QRegion &renderedRegion,
|
|
|
|
const QRegion &damagedRegion)
|
2015-04-10 08:44:07 +00:00
|
|
|
{
|
2020-11-29 11:24:38 +00:00
|
|
|
Q_UNUSED(renderedRegion)
|
|
|
|
|
2019-12-10 00:24:45 +00:00
|
|
|
Output &output = m_outputs[screenId];
|
2020-11-29 11:24:38 +00:00
|
|
|
DrmOutput *drmOutput = output.output;
|
|
|
|
|
2021-05-04 10:19:31 +00:00
|
|
|
if (isPrimary()) {
|
|
|
|
renderFramebufferToSurface(output);
|
|
|
|
} else {
|
|
|
|
importFramebuffer(output);
|
|
|
|
}
|
2019-12-10 00:24:45 +00:00
|
|
|
|
2021-04-06 08:53:26 +00:00
|
|
|
const QRegion dirty = damagedRegion.intersected(output.output->geometry());
|
|
|
|
if (!presentOnOutput(output, dirty)) {
|
2020-11-29 11:24:38 +00:00
|
|
|
output.damageHistory.clear();
|
|
|
|
RenderLoopPrivate *renderLoopPrivate = RenderLoopPrivate::get(drmOutput->renderLoop());
|
|
|
|
renderLoopPrivate->notifyFrameFailed();
|
|
|
|
return;
|
|
|
|
}
|
2015-04-10 08:44:07 +00:00
|
|
|
|
2020-10-29 21:33:06 +00:00
|
|
|
if (supportsBufferAge()) {
|
2019-12-10 00:24:45 +00:00
|
|
|
if (output.damageHistory.count() > 10) {
|
|
|
|
output.damageHistory.removeLast();
|
2015-04-23 07:55:49 +00:00
|
|
|
}
|
2020-10-30 07:27:38 +00:00
|
|
|
output.damageHistory.prepend(dirty);
|
2015-04-23 07:55:49 +00:00
|
|
|
}
|
2015-04-10 08:44:07 +00:00
|
|
|
}
|
|
|
|
|
2021-02-04 08:40:20 +00:00
|
|
|
bool EglGbmBackend::scanout(int screenId, SurfaceItem *surfaceItem)
|
2021-02-02 13:26:43 +00:00
|
|
|
{
|
2021-02-04 08:40:20 +00:00
|
|
|
SurfaceItemWayland *item = qobject_cast<SurfaceItemWayland *>(surfaceItem);
|
|
|
|
if (!item) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
KWaylandServer::SurfaceInterface *surface = item->surface();
|
2021-02-02 13:26:43 +00:00
|
|
|
if (!surface || !surface->buffer() || !surface->buffer()->linuxDmabufBuffer()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto buffer = surface->buffer();
|
2021-03-18 14:22:31 +00:00
|
|
|
Output &output = m_outputs[screenId];
|
2021-03-11 02:54:10 +00:00
|
|
|
if (buffer->linuxDmabufBuffer()->size() != output.output->modeSize()) {
|
2021-02-02 13:26:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
EglDmabufBuffer *dmabuf = static_cast<EglDmabufBuffer*>(buffer->linuxDmabufBuffer());
|
|
|
|
if (!dmabuf || !dmabuf->planes().count() ||
|
|
|
|
!gbm_device_is_format_supported(m_gpu->gbmDevice(), dmabuf->format(), GBM_BO_USE_SCANOUT)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
gbm_bo *importedBuffer;
|
|
|
|
if (dmabuf->planes()[0].modifier != DRM_FORMAT_MOD_INVALID
|
|
|
|
|| dmabuf->planes()[0].offset > 0
|
|
|
|
|| dmabuf->planes().size() > 1) {
|
2021-06-08 14:07:57 +00:00
|
|
|
if (!m_gpu->addFB2ModifiersSupported()) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-02-02 13:26:43 +00:00
|
|
|
gbm_import_fd_modifier_data data = {};
|
|
|
|
data.format = dmabuf->format();
|
|
|
|
data.width = (uint32_t) dmabuf->size().width();
|
|
|
|
data.height = (uint32_t) dmabuf->size().height();
|
|
|
|
data.num_fds = dmabuf->planes().count();
|
|
|
|
data.modifier = dmabuf->planes()[0].modifier;
|
|
|
|
for (int i = 0; i < dmabuf->planes().count(); i++) {
|
|
|
|
auto plane = dmabuf->planes()[i];
|
|
|
|
data.fds[i] = plane.fd;
|
|
|
|
data.offsets[i] = plane.offset;
|
|
|
|
data.strides[i] = plane.stride;
|
|
|
|
}
|
|
|
|
importedBuffer = gbm_bo_import(m_gpu->gbmDevice(), GBM_BO_IMPORT_FD_MODIFIER, &data, GBM_BO_USE_SCANOUT);
|
|
|
|
} else {
|
|
|
|
auto plane = dmabuf->planes()[0];
|
|
|
|
gbm_import_fd_data data = {};
|
|
|
|
data.fd = plane.fd;
|
|
|
|
data.width = (uint32_t) dmabuf->size().width();
|
|
|
|
data.height = (uint32_t) dmabuf->size().height();
|
|
|
|
data.stride = plane.stride;
|
|
|
|
data.format = dmabuf->format();
|
|
|
|
importedBuffer = gbm_bo_import(m_gpu->gbmDevice(), GBM_BO_IMPORT_FD, &data, GBM_BO_USE_SCANOUT);
|
|
|
|
}
|
|
|
|
if (!importedBuffer) {
|
2021-04-24 17:46:34 +00:00
|
|
|
if (errno != EINVAL) {
|
|
|
|
qCWarning(KWIN_DRM) << "Importing buffer for direct scanout failed!" << strerror(errno);
|
|
|
|
}
|
2021-02-02 13:26:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// damage tracking for screen casting
|
|
|
|
QRegion damage;
|
2021-02-23 14:25:28 +00:00
|
|
|
if (output.surfaceInterface == surface && buffer->size() == output.output->modeSize()) {
|
2021-02-04 08:40:20 +00:00
|
|
|
QRegion trackedDamage = surfaceItem->damage();
|
|
|
|
surfaceItem->resetDamage();
|
2021-02-02 13:26:43 +00:00
|
|
|
for (const auto &rect : trackedDamage) {
|
|
|
|
auto damageRect = QRect(rect);
|
|
|
|
damageRect.translate(output.output->geometry().topLeft());
|
|
|
|
damage |= damageRect;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
damage = output.output->geometry();
|
|
|
|
}
|
2021-03-29 11:17:44 +00:00
|
|
|
output.buffer = QSharedPointer<DrmGbmBuffer>::create(m_gpu, importedBuffer, buffer);
|
2021-05-30 23:58:47 +00:00
|
|
|
auto oldSurface = output.surfaceInterface;
|
2021-02-02 13:26:43 +00:00
|
|
|
output.surfaceInterface = surface;
|
2021-06-07 11:45:50 +00:00
|
|
|
// ensure that a context is current like with normal presentation
|
|
|
|
makeCurrent();
|
2021-05-30 23:58:47 +00:00
|
|
|
if (presentOnOutput(output, damage)) {
|
|
|
|
if (oldSurface != surface) {
|
|
|
|
auto path = surface->client()->executablePath();
|
|
|
|
qCDebug(KWIN_DRM).nospace() << "Direct scanout starting on output " << output.output->name() << " for application \"" << path << "\"";
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
output.surfaceInterface = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
2021-02-02 13:26:43 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 17:38:57 +00:00
|
|
|
QSharedPointer<GLTexture> EglGbmBackend::textureForOutput(AbstractOutput *abstractOutput) const
|
|
|
|
{
|
2021-05-04 10:19:31 +00:00
|
|
|
auto itOutput = std::find_if(m_outputs.begin(), m_outputs.end(),
|
2020-07-22 17:38:57 +00:00
|
|
|
[abstractOutput] (const auto &output) {
|
|
|
|
return output.output == abstractOutput;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if (itOutput == m_outputs.end()) {
|
2021-05-04 10:19:31 +00:00
|
|
|
itOutput = std::find_if(m_secondaryGpuOutputs.begin(), m_secondaryGpuOutputs.end(),
|
|
|
|
[abstractOutput] (const auto &output) {
|
|
|
|
return output.output == abstractOutput;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if (itOutput == m_secondaryGpuOutputs.end()) {
|
|
|
|
return {};
|
|
|
|
}
|
2020-07-22 17:38:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DrmOutput *drmOutput = itOutput->output;
|
|
|
|
if (!drmOutput->hardwareTransforms()) {
|
|
|
|
const auto glTexture = QSharedPointer<KWin::GLTexture>::create(itOutput->render.texture, GL_RGBA8, drmOutput->pixelSize());
|
|
|
|
glTexture->setYInverted(true);
|
|
|
|
return glTexture;
|
|
|
|
}
|
|
|
|
|
2021-05-04 10:19:31 +00:00
|
|
|
auto gbmBuffer = dynamic_cast<GbmBuffer*>(itOutput->buffer.data());
|
|
|
|
if (!gbmBuffer) {
|
|
|
|
qCWarning(KWIN_DRM) << "Failed to record frame: Dumb buffer used for presentation!";
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
EGLImageKHR image = eglCreateImageKHR(eglDisplay(), nullptr, EGL_NATIVE_PIXMAP_KHR, gbmBuffer->getBo(), nullptr);
|
2020-07-22 17:38:57 +00:00
|
|
|
if (image == EGL_NO_IMAGE_KHR) {
|
|
|
|
qCWarning(KWIN_DRM) << "Failed to record frame: Error creating EGLImageKHR - " << glGetError();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return QSharedPointer<EGLImageTexture>::create(eglDisplay(), image, GL_RGBA8, drmOutput->modeSize());
|
|
|
|
}
|
|
|
|
|
2021-02-02 13:26:43 +00:00
|
|
|
bool EglGbmBackend::directScanoutAllowed(int screen) const
|
|
|
|
{
|
2021-02-02 21:15:50 +00:00
|
|
|
return !m_backend->usesSoftwareCursor() && !m_outputs[screen].output->directScanoutInhibited();
|
2021-02-02 13:26:43 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 00:24:45 +00:00
|
|
|
}
|