2015-04-09 12:49:32 +00:00
|
|
|
/********************************************************************
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
|
|
|
|
|
|
|
Copyright (C) 2015 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/>.
|
|
|
|
*********************************************************************/
|
|
|
|
#include "drm_backend.h"
|
2016-03-21 14:11:17 +00:00
|
|
|
#include "drm_output.h"
|
2016-08-31 12:00:31 +00:00
|
|
|
#include "drm_object_connector.h"
|
|
|
|
#include "drm_object_crtc.h"
|
|
|
|
#include "drm_object_plane.h"
|
2015-04-13 07:23:03 +00:00
|
|
|
#include "composite.h"
|
2015-04-13 14:25:34 +00:00
|
|
|
#include "cursor.h"
|
2015-05-05 15:27:03 +00:00
|
|
|
#include "logging.h"
|
2015-04-09 12:49:32 +00:00
|
|
|
#include "logind.h"
|
2016-04-13 12:08:38 +00:00
|
|
|
#include "main.h"
|
2015-05-05 11:32:33 +00:00
|
|
|
#include "scene_qpainter_drm_backend.h"
|
2015-04-09 12:49:32 +00:00
|
|
|
#include "screens_drm.h"
|
|
|
|
#include "udev.h"
|
2015-04-24 10:01:39 +00:00
|
|
|
#include "wayland_server.h"
|
2015-04-10 08:44:07 +00:00
|
|
|
#if HAVE_GBM
|
|
|
|
#include "egl_gbm_backend.h"
|
2016-08-19 14:09:18 +00:00
|
|
|
#include <gbm.h>
|
2015-04-10 08:44:07 +00:00
|
|
|
#endif
|
2015-04-24 10:01:39 +00:00
|
|
|
// KWayland
|
2016-05-25 09:24:45 +00:00
|
|
|
#include <KWayland/Server/seat_interface.h>
|
2016-03-10 18:57:07 +00:00
|
|
|
#include <KWayland/Server/outputconfiguration_interface.h>
|
2015-04-27 09:06:04 +00:00
|
|
|
// KF5
|
|
|
|
#include <KConfigGroup>
|
|
|
|
#include <KLocalizedString>
|
|
|
|
#include <KSharedConfig>
|
2015-04-13 07:23:03 +00:00
|
|
|
// Qt
|
2015-11-02 08:34:32 +00:00
|
|
|
#include <QCryptographicHash>
|
2015-04-13 07:23:03 +00:00
|
|
|
#include <QSocketNotifier>
|
2015-04-13 14:25:34 +00:00
|
|
|
#include <QPainter>
|
2015-04-09 12:49:32 +00:00
|
|
|
// system
|
|
|
|
#include <unistd.h>
|
|
|
|
// drm
|
|
|
|
#include <xf86drm.h>
|
|
|
|
#include <xf86drmMode.h>
|
|
|
|
#include <libdrm/drm_mode.h>
|
|
|
|
|
2015-04-13 14:25:34 +00:00
|
|
|
#ifndef DRM_CAP_CURSOR_WIDTH
|
|
|
|
#define DRM_CAP_CURSOR_WIDTH 0x8
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef DRM_CAP_CURSOR_HEIGHT
|
|
|
|
#define DRM_CAP_CURSOR_HEIGHT 0x9
|
|
|
|
#endif
|
|
|
|
|
2017-04-10 04:52:44 +00:00
|
|
|
#define KWIN_DRM_EVENT_CONTEXT_VERSION 2
|
|
|
|
|
2015-04-09 12:49:32 +00:00
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
|
|
|
DrmBackend::DrmBackend(QObject *parent)
|
2016-04-07 07:18:10 +00:00
|
|
|
: Platform(parent)
|
2015-04-09 12:49:32 +00:00
|
|
|
, m_udev(new Udev)
|
2015-04-24 06:15:55 +00:00
|
|
|
, m_udevMonitor(m_udev->monitor())
|
2016-02-15 14:40:25 +00:00
|
|
|
, m_dpmsFilter()
|
2015-04-09 12:49:32 +00:00
|
|
|
{
|
2015-04-24 10:01:39 +00:00
|
|
|
handleOutputs();
|
2015-04-13 14:25:34 +00:00
|
|
|
m_cursor[0] = nullptr;
|
|
|
|
m_cursor[1] = nullptr;
|
2015-04-09 12:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DrmBackend::~DrmBackend()
|
|
|
|
{
|
2016-08-19 14:09:18 +00:00
|
|
|
#if HAVE_GBM
|
|
|
|
if (m_gbmDevice) {
|
|
|
|
gbm_device_destroy(m_gbmDevice);
|
|
|
|
}
|
|
|
|
#endif
|
2015-04-09 12:49:32 +00:00
|
|
|
if (m_fd >= 0) {
|
2015-04-14 09:25:16 +00:00
|
|
|
// wait for pageflips
|
|
|
|
while (m_pageFlipsPending != 0) {
|
|
|
|
QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);
|
|
|
|
}
|
2016-08-31 12:00:31 +00:00
|
|
|
qDeleteAll(m_planes);
|
2015-04-14 09:25:16 +00:00
|
|
|
qDeleteAll(m_outputs);
|
2015-04-13 14:25:34 +00:00
|
|
|
delete m_cursor[0];
|
|
|
|
delete m_cursor[1];
|
2015-04-09 12:49:32 +00:00
|
|
|
close(m_fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrmBackend::init()
|
|
|
|
{
|
|
|
|
LogindIntegration *logind = LogindIntegration::self();
|
|
|
|
auto takeControl = [logind, this]() {
|
|
|
|
if (logind->hasSessionControl()) {
|
|
|
|
openDrm();
|
|
|
|
} else {
|
|
|
|
logind->takeControl();
|
|
|
|
connect(logind, &LogindIntegration::hasSessionControlChanged, this, &DrmBackend::openDrm);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if (logind->isConnected()) {
|
|
|
|
takeControl();
|
|
|
|
} else {
|
|
|
|
connect(logind, &LogindIntegration::connectedChanged, this, takeControl);
|
|
|
|
}
|
2015-04-15 16:26:25 +00:00
|
|
|
}
|
|
|
|
|
2016-02-15 14:40:25 +00:00
|
|
|
void DrmBackend::outputWentOff()
|
|
|
|
{
|
|
|
|
if (!m_dpmsFilter.isNull()) {
|
|
|
|
// already another output is off
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_dpmsFilter.reset(new DpmsInputEventFilter(this));
|
|
|
|
input()->prepandInputEventFilter(m_dpmsFilter.data());
|
|
|
|
}
|
|
|
|
|
2016-02-15 15:30:22 +00:00
|
|
|
void DrmBackend::turnOutputsOn()
|
2016-02-15 14:40:25 +00:00
|
|
|
{
|
|
|
|
m_dpmsFilter.reset();
|
|
|
|
for (auto it = m_outputs.constBegin(), end = m_outputs.constEnd(); it != end; it++) {
|
|
|
|
(*it)->setDpms(DrmOutput::DpmsMode::On);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrmBackend::checkOutputsAreOn()
|
|
|
|
{
|
|
|
|
if (m_dpmsFilter.isNull()) {
|
|
|
|
// already disabled, all outputs are on
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (auto it = m_outputs.constBegin(), end = m_outputs.constEnd(); it != end; it++) {
|
|
|
|
if (!(*it)->isDpmsEnabled()) {
|
|
|
|
// dpms still disabled, need to keep the filter
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// all outputs are on, disable the filter
|
|
|
|
m_dpmsFilter.reset();
|
|
|
|
}
|
|
|
|
|
2015-04-15 16:26:25 +00:00
|
|
|
void DrmBackend::activate(bool active)
|
|
|
|
{
|
|
|
|
if (active) {
|
|
|
|
reactivate();
|
|
|
|
} else {
|
|
|
|
deactivate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrmBackend::reactivate()
|
|
|
|
{
|
|
|
|
if (m_active) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_active = true;
|
2016-06-28 08:20:27 +00:00
|
|
|
if (!usesSoftwareCursor()) {
|
|
|
|
DrmBuffer *c = m_cursor[(m_cursorIndex + 1) % 2];
|
|
|
|
const QPoint cp = Cursor::pos() - softwareCursorHotspot();
|
|
|
|
for (auto it = m_outputs.constBegin(); it != m_outputs.constEnd(); ++it) {
|
|
|
|
DrmOutput *o = *it;
|
|
|
|
o->pageFlipped();
|
|
|
|
o->blank();
|
|
|
|
o->showCursor(c);
|
|
|
|
o->moveCursor(cp);
|
|
|
|
}
|
2015-04-15 16:26:25 +00:00
|
|
|
}
|
|
|
|
// restart compositor
|
|
|
|
m_pageFlipsPending = 0;
|
|
|
|
if (Compositor *compositor = Compositor::self()) {
|
|
|
|
compositor->bufferSwapComplete();
|
|
|
|
compositor->addRepaintFull();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrmBackend::deactivate()
|
|
|
|
{
|
|
|
|
if (!m_active) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// block compositor
|
|
|
|
if (m_pageFlipsPending == 0 && Compositor::self()) {
|
|
|
|
Compositor::self()->aboutToSwapBuffers();
|
|
|
|
}
|
|
|
|
// hide cursor and disable
|
|
|
|
for (auto it = m_outputs.constBegin(); it != m_outputs.constEnd(); ++it) {
|
|
|
|
DrmOutput *o = *it;
|
|
|
|
o->hideCursor();
|
|
|
|
o->restoreSaved();
|
|
|
|
}
|
|
|
|
m_active = false;
|
2015-04-09 12:49:32 +00:00
|
|
|
}
|
|
|
|
|
2015-04-13 07:23:03 +00:00
|
|
|
void DrmBackend::pageFlipHandler(int fd, unsigned int frame, unsigned int sec, unsigned int usec, void *data)
|
|
|
|
{
|
|
|
|
Q_UNUSED(fd)
|
|
|
|
Q_UNUSED(frame)
|
|
|
|
Q_UNUSED(sec)
|
|
|
|
Q_UNUSED(usec)
|
2015-04-14 09:25:16 +00:00
|
|
|
auto output = reinterpret_cast<DrmOutput*>(data);
|
|
|
|
output->pageFlipped();
|
|
|
|
output->m_backend->m_pageFlipsPending--;
|
|
|
|
if (output->m_backend->m_pageFlipsPending == 0) {
|
|
|
|
// TODO: improve, this currently means we wait for all page flips or all outputs.
|
|
|
|
// It would be better to driver the repaint per output
|
|
|
|
if (Compositor::self()) {
|
|
|
|
Compositor::self()->bufferSwapComplete();
|
|
|
|
}
|
2015-04-10 08:44:07 +00:00
|
|
|
}
|
2015-04-13 07:23:03 +00:00
|
|
|
}
|
|
|
|
|
2015-04-09 12:49:32 +00:00
|
|
|
void DrmBackend::openDrm()
|
|
|
|
{
|
2015-04-15 16:26:25 +00:00
|
|
|
connect(LogindIntegration::self(), &LogindIntegration::sessionActiveChanged, this, &DrmBackend::activate);
|
2015-04-09 12:49:32 +00:00
|
|
|
UdevDevice::Ptr device = m_udev->primaryGpu();
|
|
|
|
if (!device) {
|
2015-05-05 15:27:03 +00:00
|
|
|
qCWarning(KWIN_DRM) << "Did not find a GPU";
|
2015-04-09 12:49:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
int fd = LogindIntegration::self()->takeDevice(device->devNode());
|
|
|
|
if (fd < 0) {
|
2015-05-05 15:27:03 +00:00
|
|
|
qCWarning(KWIN_DRM) << "failed to open drm device at" << device->devNode();
|
2015-04-09 12:49:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_fd = fd;
|
2015-04-15 16:26:25 +00:00
|
|
|
m_active = true;
|
2015-04-13 07:23:03 +00:00
|
|
|
QSocketNotifier *notifier = new QSocketNotifier(m_fd, QSocketNotifier::Read, this);
|
|
|
|
connect(notifier, &QSocketNotifier::activated, this,
|
|
|
|
[this] {
|
2016-04-19 07:46:16 +00:00
|
|
|
if (!LogindIntegration::self()->isActiveSession()) {
|
2015-04-15 16:26:25 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-04-13 07:23:03 +00:00
|
|
|
drmEventContext e;
|
|
|
|
memset(&e, 0, sizeof e);
|
2017-04-10 04:52:44 +00:00
|
|
|
e.version = KWIN_DRM_EVENT_CONTEXT_VERSION;
|
2015-04-13 07:23:03 +00:00
|
|
|
e.page_flip_handler = pageFlipHandler;
|
|
|
|
drmHandleEvent(m_fd, &e);
|
|
|
|
}
|
|
|
|
);
|
2015-04-09 12:49:32 +00:00
|
|
|
m_drmId = device->sysNum();
|
2016-08-31 12:00:31 +00:00
|
|
|
|
|
|
|
// trying to activate Atomic Mode Setting (this means also Universal Planes)
|
|
|
|
if (qEnvironmentVariableIsSet("KWIN_DRM_AMS")) {
|
|
|
|
if (drmSetClientCap(m_fd, DRM_CLIENT_CAP_ATOMIC, 1) == 0) {
|
|
|
|
qCDebug(KWIN_DRM) << "Using Atomic Mode Setting.";
|
|
|
|
m_atomicModeSetting = true;
|
|
|
|
|
|
|
|
ScopedDrmPointer<drmModePlaneRes, &drmModeFreePlaneResources> planeResources(drmModeGetPlaneResources(m_fd));
|
|
|
|
if (!planeResources) {
|
|
|
|
qCWarning(KWIN_DRM) << "Failed to get plane resources. Falling back to legacy mode";
|
|
|
|
m_atomicModeSetting = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_atomicModeSetting) {
|
|
|
|
qCDebug(KWIN_DRM) << "Number of planes:" << planeResources->count_planes;
|
|
|
|
|
|
|
|
// create the plane objects
|
|
|
|
for (unsigned int i = 0; i < planeResources->count_planes; ++i) {
|
|
|
|
drmModePlane *kplane = drmModeGetPlane(m_fd, planeResources->planes[i]);
|
|
|
|
DrmPlane *p = new DrmPlane(kplane->plane_id, m_fd);
|
|
|
|
|
|
|
|
if (p->init()) {
|
|
|
|
p->setPossibleCrtcs(kplane->possible_crtcs);
|
|
|
|
p->setFormats(kplane->formats, kplane->count_formats);
|
|
|
|
m_planes << p;
|
|
|
|
} else {
|
|
|
|
delete p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_planes.isEmpty()) {
|
|
|
|
qCWarning(KWIN_DRM) << "Failed to create any plane. Falling back to legacy mode";
|
|
|
|
m_atomicModeSetting = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
qCWarning(KWIN_DRM) << "drmSetClientCap for Atomic Mode Setting failed. Using legacy mode.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-09 12:49:32 +00:00
|
|
|
queryResources();
|
2016-07-11 09:26:03 +00:00
|
|
|
if (m_outputs.isEmpty()) {
|
|
|
|
qCWarning(KWIN_DRM) << "No outputs, cannot render, will terminate now";
|
|
|
|
emit initFailed();
|
|
|
|
return;
|
|
|
|
}
|
2015-04-24 06:15:55 +00:00
|
|
|
|
|
|
|
// setup udevMonitor
|
|
|
|
if (m_udevMonitor) {
|
|
|
|
m_udevMonitor->filterSubsystemDevType("drm");
|
|
|
|
const int fd = m_udevMonitor->fd();
|
|
|
|
if (fd != -1) {
|
|
|
|
QSocketNotifier *notifier = new QSocketNotifier(fd, QSocketNotifier::Read, this);
|
|
|
|
connect(notifier, &QSocketNotifier::activated, this,
|
|
|
|
[this] {
|
|
|
|
auto device = m_udevMonitor->getDevice();
|
|
|
|
if (!device) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (device->sysNum() != m_drmId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (device->hasProperty("HOTPLUG", "1")) {
|
2015-05-05 15:27:03 +00:00
|
|
|
qCDebug(KWIN_DRM) << "Received hot plug event for monitored drm device";
|
2015-04-24 06:15:55 +00:00
|
|
|
queryResources();
|
|
|
|
m_cursorIndex = (m_cursorIndex + 1) % 2;
|
|
|
|
updateCursor();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
m_udevMonitor->enable();
|
|
|
|
}
|
|
|
|
}
|
2015-05-05 17:02:52 +00:00
|
|
|
setReady(true);
|
2015-04-24 06:15:55 +00:00
|
|
|
|
2015-04-13 14:25:34 +00:00
|
|
|
initCursor();
|
2015-04-09 12:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DrmBackend::queryResources()
|
|
|
|
{
|
|
|
|
if (m_fd < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ScopedDrmPointer<_drmModeRes, &drmModeFreeResources> resources(drmModeGetResources(m_fd));
|
|
|
|
if (!resources) {
|
2015-05-05 15:27:03 +00:00
|
|
|
qCWarning(KWIN_DRM) << "drmModeGetResources failed";
|
2015-04-09 12:49:32 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-04-24 06:15:55 +00:00
|
|
|
|
|
|
|
QVector<DrmOutput*> connectedOutputs;
|
2015-04-09 12:49:32 +00:00
|
|
|
for (int i = 0; i < resources->count_connectors; ++i) {
|
|
|
|
const auto id = resources->connectors[i];
|
|
|
|
ScopedDrmPointer<_drmModeConnector, &drmModeFreeConnector> connector(drmModeGetConnector(m_fd, id));
|
|
|
|
if (!connector) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (connector->connection != DRM_MODE_CONNECTED) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-04-24 06:15:55 +00:00
|
|
|
if (connector->count_modes == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (DrmOutput *o = findOutput(connector->connector_id)) {
|
|
|
|
connectedOutputs << o;
|
|
|
|
continue;
|
|
|
|
}
|
2015-04-17 14:41:18 +00:00
|
|
|
bool crtcFound = false;
|
|
|
|
const quint32 crtcId = findCrtc(resources.data(), connector.data(), &crtcFound);
|
|
|
|
if (!crtcFound) {
|
2015-04-09 12:49:32 +00:00
|
|
|
continue;
|
|
|
|
}
|
2015-04-17 14:41:18 +00:00
|
|
|
ScopedDrmPointer<_drmModeCrtc, &drmModeFreeCrtc> crtc(drmModeGetCrtc(m_fd, crtcId));
|
2015-04-09 12:49:32 +00:00
|
|
|
if (!crtc) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-04-14 09:25:16 +00:00
|
|
|
DrmOutput *drmOutput = new DrmOutput(this);
|
2015-08-31 09:41:19 +00:00
|
|
|
connect(drmOutput, &DrmOutput::dpmsChanged, this, &DrmBackend::outputDpmsChanged);
|
2015-04-17 14:41:18 +00:00
|
|
|
drmOutput->m_crtcId = crtcId;
|
2016-08-31 12:00:31 +00:00
|
|
|
drmOutput->m_connector = connector->connector_id;
|
|
|
|
|
|
|
|
if (m_atomicModeSetting) {
|
|
|
|
drmOutput->m_crtc = new DrmCrtc(crtcId, m_fd);
|
|
|
|
if (drmOutput->m_crtc->init()) {
|
|
|
|
drmOutput->m_crtc->setOutput(drmOutput);
|
|
|
|
} else {
|
|
|
|
qCWarning(KWIN_DRM) << "Crtc object failed, skipping output on connector" << connector->connector_id;
|
|
|
|
delete drmOutput->m_crtc;
|
|
|
|
delete drmOutput;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
drmOutput->m_conn = new DrmConnector(connector->connector_id, m_fd);
|
|
|
|
if (drmOutput->m_conn->init()) {
|
|
|
|
drmOutput->m_conn->setOutput(drmOutput);
|
|
|
|
} else {
|
|
|
|
qCWarning(KWIN_DRM) << "Connector object failed, skipping output on connector" << connector->connector_id;
|
|
|
|
delete drmOutput->m_conn;
|
|
|
|
delete drmOutput;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 06:15:55 +00:00
|
|
|
if (crtc->mode_valid) {
|
|
|
|
drmOutput->m_mode = crtc->mode;
|
|
|
|
} else {
|
|
|
|
drmOutput->m_mode = connector->modes[0];
|
|
|
|
}
|
2016-08-31 12:00:31 +00:00
|
|
|
qCDebug(KWIN_DRM) << "For new output use mode " << drmOutput->m_mode.name;
|
|
|
|
|
2016-07-11 09:26:03 +00:00
|
|
|
if (!drmOutput->init(connector.data())) {
|
|
|
|
qCWarning(KWIN_DRM) << "Failed to create output for connector " << connector->connector_id;
|
|
|
|
delete drmOutput;
|
|
|
|
continue;
|
|
|
|
}
|
2015-11-02 08:34:32 +00:00
|
|
|
qCDebug(KWIN_DRM) << "Found new output with uuid" << drmOutput->uuid();
|
2015-04-24 06:15:55 +00:00
|
|
|
connectedOutputs << drmOutput;
|
|
|
|
}
|
2015-11-02 08:34:32 +00:00
|
|
|
std::sort(connectedOutputs.begin(), connectedOutputs.end(), [] (DrmOutput *a, DrmOutput *b) { return a->m_connector < b->m_connector; });
|
2015-04-24 06:15:55 +00:00
|
|
|
// check for outputs which got removed
|
|
|
|
auto it = m_outputs.begin();
|
|
|
|
while (it != m_outputs.end()) {
|
|
|
|
if (connectedOutputs.contains(*it)) {
|
|
|
|
it++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
DrmOutput *removed = *it;
|
|
|
|
it = m_outputs.erase(it);
|
|
|
|
emit outputRemoved(removed);
|
|
|
|
delete removed;
|
|
|
|
}
|
|
|
|
for (auto it = connectedOutputs.constBegin(); it != connectedOutputs.constEnd(); ++it) {
|
|
|
|
if (!m_outputs.contains(*it)) {
|
|
|
|
emit outputAdded(*it);
|
|
|
|
}
|
2015-04-09 12:49:32 +00:00
|
|
|
}
|
2015-04-24 06:15:55 +00:00
|
|
|
m_outputs = connectedOutputs;
|
2015-11-02 08:34:32 +00:00
|
|
|
readOutputsConfiguration();
|
2016-07-11 09:26:03 +00:00
|
|
|
if (!m_outputs.isEmpty()) {
|
|
|
|
emit screensQueried();
|
|
|
|
}
|
2015-11-02 08:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DrmBackend::readOutputsConfiguration()
|
|
|
|
{
|
|
|
|
if (m_outputs.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const QByteArray uuid = generateOutputConfigurationUuid();
|
2016-01-29 10:24:18 +00:00
|
|
|
const auto outputGroup = kwinApp()->config()->group("DrmOutputs");
|
2015-11-02 08:34:32 +00:00
|
|
|
const auto configGroup = outputGroup.group(uuid);
|
|
|
|
qCDebug(KWIN_DRM) << "Reading output configuration for" << uuid;
|
|
|
|
// default position goes from left to right
|
|
|
|
QPoint pos(0, 0);
|
|
|
|
for (auto it = m_outputs.begin(); it != m_outputs.end(); ++it) {
|
|
|
|
const auto outputConfig = configGroup.group((*it)->uuid());
|
|
|
|
(*it)->setGlobalPos(outputConfig.readEntry<QPoint>("Position", pos));
|
|
|
|
// TODO: add mode
|
|
|
|
pos.setX(pos.x() + (*it)->size().width());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray DrmBackend::generateOutputConfigurationUuid() const
|
|
|
|
{
|
|
|
|
auto it = m_outputs.constBegin();
|
|
|
|
if (m_outputs.size() == 1) {
|
|
|
|
// special case: one output
|
|
|
|
return (*it)->uuid();
|
|
|
|
}
|
|
|
|
QCryptographicHash hash(QCryptographicHash::Md5);
|
|
|
|
for (; it != m_outputs.constEnd(); ++it) {
|
|
|
|
hash.addData((*it)->uuid());
|
|
|
|
}
|
|
|
|
return hash.result().toHex().left(10);
|
2015-04-09 12:49:32 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 18:57:07 +00:00
|
|
|
void DrmBackend::configurationChangeRequested(KWayland::Server::OutputConfigurationInterface *config)
|
|
|
|
{
|
|
|
|
const auto changes = config->changes();
|
|
|
|
for (auto it = changes.begin(); it != changes.end(); it++) {
|
|
|
|
|
|
|
|
KWayland::Server::OutputChangeSet *changeset = it.value();
|
|
|
|
|
|
|
|
auto drmoutput = findOutput(it.key()->uuid());
|
|
|
|
if (drmoutput == nullptr) {
|
|
|
|
qCWarning(KWIN_DRM) << "Could NOT find DrmOutput matching " << it.key()->uuid();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
drmoutput->setChanges(changeset);
|
|
|
|
}
|
2016-06-14 08:53:46 +00:00
|
|
|
emit screens()->changed();
|
2016-03-10 18:57:07 +00:00
|
|
|
}
|
|
|
|
|
2015-04-24 06:15:55 +00:00
|
|
|
DrmOutput *DrmBackend::findOutput(quint32 connector)
|
|
|
|
{
|
|
|
|
auto it = std::find_if(m_outputs.constBegin(), m_outputs.constEnd(), [connector] (DrmOutput *o) {
|
|
|
|
return o->m_connector == connector;
|
|
|
|
});
|
|
|
|
if (it != m_outputs.constEnd()) {
|
|
|
|
return *it;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-03-10 18:57:07 +00:00
|
|
|
DrmOutput *DrmBackend::findOutput(const QByteArray &uuid)
|
|
|
|
{
|
|
|
|
auto it = std::find_if(m_outputs.constBegin(), m_outputs.constEnd(), [uuid] (DrmOutput *o) {
|
|
|
|
return o->m_uuid == uuid;
|
|
|
|
});
|
|
|
|
if (it != m_outputs.constEnd()) {
|
|
|
|
return *it;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-04-17 14:41:18 +00:00
|
|
|
quint32 DrmBackend::findCrtc(drmModeRes *res, drmModeConnector *connector, bool *ok)
|
|
|
|
{
|
|
|
|
if (ok) {
|
|
|
|
*ok = false;
|
|
|
|
}
|
|
|
|
ScopedDrmPointer<_drmModeEncoder, &drmModeFreeEncoder> encoder(drmModeGetEncoder(m_fd, connector->encoder_id));
|
|
|
|
if (encoder) {
|
|
|
|
if (!crtcIsUsed(encoder->crtc_id)) {
|
|
|
|
if (ok) {
|
|
|
|
*ok = true;
|
|
|
|
}
|
|
|
|
return encoder->crtc_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// let's iterate over all encoders to find a suitable crtc
|
|
|
|
for (int i = 0; i < connector->count_encoders; ++i) {
|
|
|
|
ScopedDrmPointer<_drmModeEncoder, &drmModeFreeEncoder> encoder(drmModeGetEncoder(m_fd, connector->encoders[i]));
|
|
|
|
if (!encoder) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (int j = 0; j < res->count_crtcs; ++j) {
|
|
|
|
if (!(encoder->possible_crtcs & (1 << j))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!crtcIsUsed(res->crtcs[j])) {
|
|
|
|
if (ok) {
|
|
|
|
*ok = true;
|
|
|
|
}
|
|
|
|
return res->crtcs[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DrmBackend::crtcIsUsed(quint32 crtc)
|
|
|
|
{
|
|
|
|
auto it = std::find_if(m_outputs.constBegin(), m_outputs.constEnd(),
|
|
|
|
[crtc] (DrmOutput *o) {
|
|
|
|
return o->m_crtcId == crtc;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return it != m_outputs.constEnd();
|
|
|
|
}
|
|
|
|
|
2015-04-17 09:16:00 +00:00
|
|
|
void DrmBackend::present(DrmBuffer *buffer, DrmOutput *output)
|
|
|
|
{
|
|
|
|
if (output->present(buffer)) {
|
2015-04-14 09:25:16 +00:00
|
|
|
m_pageFlipsPending++;
|
|
|
|
if (m_pageFlipsPending == 1 && Compositor::self()) {
|
|
|
|
Compositor::self()->aboutToSwapBuffers();
|
|
|
|
}
|
2015-04-13 07:23:03 +00:00
|
|
|
}
|
2015-04-09 12:49:32 +00:00
|
|
|
}
|
|
|
|
|
2015-04-13 14:25:34 +00:00
|
|
|
void DrmBackend::initCursor()
|
|
|
|
{
|
2016-05-25 09:24:45 +00:00
|
|
|
m_cursorEnabled = waylandServer()->seat()->hasPointer();
|
|
|
|
connect(waylandServer()->seat(), &KWayland::Server::SeatInterface::hasPointerChanged, this,
|
|
|
|
[this] {
|
|
|
|
m_cursorEnabled = waylandServer()->seat()->hasPointer();
|
2016-06-28 08:20:27 +00:00
|
|
|
if (usesSoftwareCursor()) {
|
|
|
|
return;
|
|
|
|
}
|
2016-05-25 09:24:45 +00:00
|
|
|
for (auto it = m_outputs.constBegin(); it != m_outputs.constEnd(); ++it) {
|
|
|
|
if (m_cursorEnabled) {
|
|
|
|
(*it)->showCursor(m_cursor[m_cursorIndex]);
|
|
|
|
} else {
|
|
|
|
(*it)->hideCursor();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2015-04-13 14:25:34 +00:00
|
|
|
uint64_t capability = 0;
|
|
|
|
QSize cursorSize;
|
|
|
|
if (drmGetCap(m_fd, DRM_CAP_CURSOR_WIDTH, &capability) == 0) {
|
|
|
|
cursorSize.setWidth(capability);
|
|
|
|
} else {
|
|
|
|
cursorSize.setWidth(64);
|
|
|
|
}
|
|
|
|
if (drmGetCap(m_fd, DRM_CAP_CURSOR_HEIGHT, &capability) == 0) {
|
|
|
|
cursorSize.setHeight(capability);
|
|
|
|
} else {
|
|
|
|
cursorSize.setHeight(64);
|
|
|
|
}
|
2016-06-28 08:20:27 +00:00
|
|
|
auto createCursor = [this, cursorSize] (int index) {
|
|
|
|
m_cursor[index] = createBuffer(cursorSize);
|
|
|
|
if (!m_cursor[index]->map(QImage::Format_ARGB32_Premultiplied)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
m_cursor[index]->image()->fill(Qt::transparent);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
if (!createCursor(0) || !createCursor(1)) {
|
|
|
|
setSoftWareCursor(true);
|
|
|
|
return;
|
|
|
|
}
|
2015-04-13 14:25:34 +00:00
|
|
|
// now we have screens and can set cursors, so start tracking
|
|
|
|
connect(this, &DrmBackend::cursorChanged, this, &DrmBackend::updateCursor);
|
|
|
|
connect(Cursor::self(), &Cursor::posChanged, this, &DrmBackend::moveCursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrmBackend::setCursor()
|
|
|
|
{
|
|
|
|
DrmBuffer *c = m_cursor[m_cursorIndex];
|
|
|
|
m_cursorIndex = (m_cursorIndex + 1) % 2;
|
2016-05-25 09:24:45 +00:00
|
|
|
if (m_cursorEnabled) {
|
|
|
|
for (auto it = m_outputs.constBegin(); it != m_outputs.constEnd(); ++it) {
|
|
|
|
(*it)->showCursor(c);
|
|
|
|
}
|
2015-04-14 09:25:16 +00:00
|
|
|
}
|
2016-02-23 11:29:05 +00:00
|
|
|
markCursorAsRendered();
|
2015-04-13 14:25:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DrmBackend::updateCursor()
|
|
|
|
{
|
2016-06-28 08:20:27 +00:00
|
|
|
if (usesSoftwareCursor()) {
|
|
|
|
return;
|
|
|
|
}
|
2015-04-13 14:25:34 +00:00
|
|
|
const QImage &cursorImage = softwareCursor();
|
|
|
|
if (cursorImage.isNull()) {
|
|
|
|
hideCursor();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QImage *c = m_cursor[m_cursorIndex]->image();
|
|
|
|
c->fill(Qt::transparent);
|
|
|
|
QPainter p;
|
|
|
|
p.begin(c);
|
|
|
|
p.drawImage(QPoint(0, 0), cursorImage);
|
|
|
|
p.end();
|
|
|
|
|
|
|
|
setCursor();
|
|
|
|
moveCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrmBackend::hideCursor()
|
|
|
|
{
|
2016-05-25 09:24:45 +00:00
|
|
|
if (!m_cursorEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
2015-04-14 09:25:16 +00:00
|
|
|
for (auto it = m_outputs.constBegin(); it != m_outputs.constEnd(); ++it) {
|
|
|
|
(*it)->hideCursor();
|
|
|
|
}
|
2015-04-13 14:25:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DrmBackend::moveCursor()
|
|
|
|
{
|
|
|
|
const QPoint p = Cursor::pos() - softwareCursorHotspot();
|
2016-05-25 09:24:45 +00:00
|
|
|
if (!m_cursorEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
2015-04-14 09:25:16 +00:00
|
|
|
for (auto it = m_outputs.constBegin(); it != m_outputs.constEnd(); ++it) {
|
|
|
|
(*it)->moveCursor(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize DrmBackend::size() const
|
|
|
|
{
|
|
|
|
if (m_outputs.isEmpty()) {
|
|
|
|
return QSize();
|
|
|
|
}
|
|
|
|
return m_outputs.first()->size();
|
2015-04-13 14:25:34 +00:00
|
|
|
}
|
|
|
|
|
2015-04-09 12:49:32 +00:00
|
|
|
Screens *DrmBackend::createScreens(QObject *parent)
|
|
|
|
{
|
|
|
|
return new DrmScreens(this, parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
QPainterBackend *DrmBackend::createQPainterBackend()
|
|
|
|
{
|
|
|
|
return new DrmQPainterBackend(this);
|
|
|
|
}
|
|
|
|
|
2015-04-10 08:44:07 +00:00
|
|
|
OpenGLBackend *DrmBackend::createOpenGLBackend()
|
|
|
|
{
|
|
|
|
#if HAVE_GBM
|
|
|
|
return new EglGbmBackend(this);
|
2015-04-24 13:14:53 +00:00
|
|
|
#else
|
2016-04-07 07:18:10 +00:00
|
|
|
return Platform::createOpenGLBackend();
|
2015-04-24 13:14:53 +00:00
|
|
|
#endif
|
2015-04-10 08:44:07 +00:00
|
|
|
}
|
|
|
|
|
2015-04-09 12:49:32 +00:00
|
|
|
DrmBuffer *DrmBackend::createBuffer(const QSize &size)
|
|
|
|
{
|
2015-04-24 08:23:06 +00:00
|
|
|
DrmBuffer *b = new DrmBuffer(this, size);
|
|
|
|
m_buffers << b;
|
|
|
|
return b;
|
2015-04-09 12:49:32 +00:00
|
|
|
}
|
|
|
|
|
2015-04-10 08:44:07 +00:00
|
|
|
DrmBuffer *DrmBackend::createBuffer(gbm_surface *surface)
|
|
|
|
{
|
|
|
|
#if HAVE_GBM
|
2015-04-24 08:23:06 +00:00
|
|
|
DrmBuffer *b = new DrmBuffer(this, surface);
|
2016-08-31 12:00:31 +00:00
|
|
|
b->m_deleteAfterPageFlip = true;
|
2015-04-24 08:23:06 +00:00
|
|
|
m_buffers << b;
|
|
|
|
return b;
|
2015-04-24 13:14:53 +00:00
|
|
|
#else
|
|
|
|
return nullptr;
|
2015-04-10 08:44:07 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-04-24 08:23:06 +00:00
|
|
|
void DrmBackend::bufferDestroyed(DrmBuffer *b)
|
|
|
|
{
|
|
|
|
m_buffers.removeAll(b);
|
|
|
|
}
|
|
|
|
|
2015-08-31 09:41:19 +00:00
|
|
|
void DrmBackend::outputDpmsChanged()
|
|
|
|
{
|
|
|
|
if (m_outputs.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bool enabled = false;
|
|
|
|
for (auto it = m_outputs.constBegin(); it != m_outputs.constEnd(); ++it) {
|
|
|
|
enabled = enabled || (*it)->isDpmsEnabled();
|
|
|
|
}
|
|
|
|
setOutputsEnabled(enabled);
|
|
|
|
}
|
|
|
|
|
2015-04-14 09:25:16 +00:00
|
|
|
|
2015-04-09 12:49:32 +00:00
|
|
|
}
|