Commit 997cf97c authored by Roman Gilg's avatar Roman Gilg

Atomic Mode Setting / Universal Plane preliminary support

This is Milestone 1 of full support of Atomic Mode Setting (AMS) and
Universal Planes in the KWin DRM backend.

With Milestone 1 we can use the primary plane of a DRM output and do an
AMS commit (this means mode setting aswell as page flipping), if the
driver supports it. Until now the functionality is only tested on Intel
graphics. You need the drm-next kernel for most recent DRM kernel
developments. As boot option set "i915.nuclear_pageflip". Additionally
at the moment AMS is still hidden behind the environment variable
KWIN_DRM_AMS. Set it, if you want to try out AMS.

What needs to be done next: Make it possible to transfer EGL buffers
directly to planes and implement logic for deciding about using a plane
or not for a specific buffer.

You can read more about it on LWN:
https://lwn.net/Articles/653071
And on Martin's blog:
https://blog.martin-graesslin.com/blog/2015/08/layered-compositing/
I used as model previous work by Daniel Stone for Weston:
https://git.collabora.com/cgit/user/daniels/weston.git

Reviewed-by: mgraesslin

Tags: #kwin

Differential Revision: https://phabricator.kde.org/D2370
parent 4c0e33a9
set(DRM_SOURCES
drm_backend.cpp
drm_object.cpp
drm_object_connector.cpp
drm_object_crtc.cpp
drm_object_plane.cpp
drm_output.cpp
drm_buffer.cpp
drm_inputeventfilter.cpp
......
......@@ -19,6 +19,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#include "drm_backend.h"
#include "drm_output.h"
#include "drm_object_connector.h"
#include "drm_object_crtc.h"
#include "drm_object_plane.h"
#include "composite.h"
#include "cursor.h"
#include "logging.h"
......@@ -84,6 +87,7 @@ DrmBackend::~DrmBackend()
while (m_pageFlipsPending != 0) {
QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);
}
qDeleteAll(m_planes);
qDeleteAll(m_outputs);
delete m_cursor[0];
delete m_cursor[1];
......@@ -242,6 +246,46 @@ void DrmBackend::openDrm()
}
);
m_drmId = device->sysNum();
// 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.";
}
}
queryResources();
if (m_outputs.isEmpty()) {
qCWarning(KWIN_DRM) << "No outputs, cannot render, will terminate now";
......@@ -320,12 +364,37 @@ void DrmBackend::queryResources()
DrmOutput *drmOutput = new DrmOutput(this);
connect(drmOutput, &DrmOutput::dpmsChanged, this, &DrmBackend::outputDpmsChanged);
drmOutput->m_crtcId = crtcId;
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;
}
}
if (crtc->mode_valid) {
drmOutput->m_mode = crtc->mode;
} else {
drmOutput->m_mode = connector->modes[0];
}
drmOutput->m_connector = connector->connector_id;
qCDebug(KWIN_DRM) << "For new output use mode " << drmOutput->m_mode.name;
if (!drmOutput->init(connector.data())) {
qCWarning(KWIN_DRM) << "Failed to create output for connector " << connector->connector_id;
delete drmOutput;
......@@ -625,6 +694,7 @@ DrmBuffer *DrmBackend::createBuffer(gbm_surface *surface)
{
#if HAVE_GBM
DrmBuffer *b = new DrmBuffer(this, surface);
b->m_deleteAfterPageFlip = true;
m_buffers << b;
return b;
#else
......
......@@ -54,6 +54,7 @@ class Udev;
class UdevMonitor;
class DrmOutput;
class DrmPlane;
class KWIN_EXPORT DrmBackend : public Platform
......@@ -85,11 +86,19 @@ public:
QVector<DrmBuffer*> buffers() const {
return m_buffers;
}
QVector<DrmPlane*> planes() const {
return m_planes;
}
void bufferDestroyed(DrmBuffer *b);
void outputWentOff();
void checkOutputsAreOn();
// returns use of AMS, default is not/legacy
bool atomicModeSetting() const {
return m_atomicModeSetting;
}
void setGbmDevice(gbm_device *device) {
m_gbmDevice = device;
}
......@@ -100,7 +109,6 @@ public:
public Q_SLOTS:
void turnOutputsOn();
Q_SIGNALS:
void outputRemoved(KWin::DrmOutput *output);
void outputAdded(KWin::DrmOutput *output);
......@@ -130,11 +138,14 @@ private:
int m_drmId = 0;
QVector<DrmOutput*> m_outputs;
DrmBuffer *m_cursor[2];
bool m_atomicModeSetting = false;
bool m_cursorEnabled = false;
int m_cursorIndex = 0;
int m_pageFlipsPending = 0;
bool m_active = false;
QVector<DrmBuffer*> m_buffers;
// all available planes: primarys, cursors and overlays
QVector<DrmPlane*> m_planes;
QScopedPointer<DpmsInputEventFilter> m_dpmsFilter;
KWayland::Server::OutputManagementInterface *m_outputManagement = nullptr;
gbm_device *m_gbmDevice = nullptr;
......
......@@ -58,6 +58,10 @@ public:
bool isGbm() const {
return m_bo != nullptr;
}
bool deleteAfterPageFlip() const {
return m_deleteAfterPageFlip;
}
void releaseGbm();
private:
......@@ -74,6 +78,7 @@ private:
quint64 m_bufferSize = 0;
void *m_memory = nullptr;
QImage *m_image = nullptr;
bool m_deleteAfterPageFlip = false;
};
}
......
/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2016 Roman Gilg <subdiff@gmail.com>
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_object.h"
#include "logging.h"
namespace KWin
{
/*
* Defintions for class DrmObject
*/
DrmObject::DrmObject(uint32_t object_id, int fd)
: m_fd(fd)
, m_id(object_id)
{
}
DrmObject::~DrmObject()
{
foreach(Property* p, m_props)
delete p;
}
void DrmObject::initProp(int n, drmModeObjectProperties *properties, QVector<QByteArray> enumNames)
{
m_props.resize(m_propsNames.size());
for (unsigned int i = 0; i < properties->count_props; ++i) {
drmModePropertyRes *prop = drmModeGetProperty(m_fd, properties->props[i]);
if (!prop) {
continue;
}
if (prop->name == m_propsNames[n]) {
qCDebug(KWIN_DRM).nospace() << m_id << ": " << prop->name << "' (id " << prop->prop_id
<< "): " << properties->prop_values[i];
m_props[n] = new Property(prop, properties->prop_values[i], enumNames);
}
drmModeFreeProperty(prop);
}
}
void DrmObject::setPropValue(int index, uint64_t new_value)
{
Q_ASSERT(index < m_props.size());
m_props[index]->setValue(new_value);
return;
}
bool DrmObject::atomicAddProperty(drmModeAtomicReq *req, int prop, uint64_t value)
{
uint32_t mask = 1U << prop;
if ((m_propsPending | m_propsValid) & mask && value == propValue(prop)) {
// no change necessary, don't add property for next atomic commit
return true;
}
if (drmModeAtomicAddProperty(req, m_id, m_props[prop]->propId(), value) < 0) {
// error when adding property
return false;
}
m_propsPending |= mask;
m_propsValid &= ~mask;
// adding property was successful
return true;
}
/*
* Defintions for struct Prop
*/
DrmObject::Property::Property(drmModePropertyRes *prop, uint64_t val, QVector<QByteArray> enumNames)
: m_propId(prop->prop_id)
, m_propName(prop->name)
, m_value(val)
{
if (!enumNames.isEmpty()) {
qCDebug(KWIN_DRM) << m_propName << " has enums:" << enumNames;
m_enumNames = enumNames;
initEnumMap(prop);
}
}
DrmObject::Property::~Property() = default;
void DrmObject::Property::initEnumMap(drmModePropertyRes *prop)
{
if (!(prop->flags & DRM_MODE_PROP_ENUM) || prop->count_enums < 1) {
qCWarning(KWIN_DRM) << "Property '" << prop->name << "' ( id ="
<< m_propId << ") should be enum valued, but it is not.";
return;
}
int nameCount = m_enumNames.size();
m_enumMap.resize(nameCount);
qCDebug(KWIN_DRM).nospace() << "Test all " << prop->count_enums <<
" possible enums" <<":";
for (int i = 0; i < prop->count_enums; i++) {
struct drm_mode_property_enum *en = &prop->enums[i];
int j = 0;
while (QByteArray(en->name) != m_enumNames[j]) {
j++;
if (j == nameCount) {
break;
}
}
if (j == nameCount) {
qCWarning(KWIN_DRM).nospace() << m_propName << " has unrecognized enum '" << en->name << "'";
} else {
qCDebug(KWIN_DRM).nospace() << "Enum '"
<< en->name << "': runtime-value = " << en->value;
m_enumMap[j] = en->value;
}
}
if (KWIN_DRM().isDebugEnabled()) {
for (int i = 0; i < m_enumMap.size(); i++) {
if (m_value == m_enumMap[i]) {
qCDebug(KWIN_DRM) << "=>" << m_propName << "with mapped enum value" << m_enumNames[i];
}
}
}
}
}
/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2016 Roman Gilg <subdiff@gmail.com>
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/>.
*********************************************************************/
#ifndef KWIN_DRM_OBJECT_H
#define KWIN_DRM_OBJECT_H
#include <QVector>
#include <QByteArray>
// drm
#include <xf86drmMode.h>
namespace KWin
{
class DrmOutput;
class DrmObject
{
public:
// creates drm object by its id delivered by the kernel
DrmObject(uint32_t object_id, int fd);
virtual ~DrmObject() = 0;
enum class AtomicReturn {
NoChange,
Success,
Error
};
virtual bool init() = 0;
uint32_t id() const {
return m_id;
}
DrmOutput* output() const {
return m_output;
}
void setOutput(DrmOutput* output) {
m_output = output;
}
uint32_t propId(int index) {
return m_props[index]->propId();
}
uint64_t propValue(int index) {
return m_props[index]->value();
}
void setPropValue(int index, uint64_t new_value);
uint32_t propsPending() {
return m_propsPending;
}
uint32_t propsValid() {
return m_propsValid;
}
void setPropsPending(uint32_t value) {
m_propsPending = value;
}
void setPropsValid(uint32_t value) {
m_propsValid = value;
}
bool atomicAddProperty(drmModeAtomicReq *req, int prop, uint64_t value);
protected:
const int m_fd = 0;
const uint32_t m_id = 0;
DrmOutput *m_output = nullptr;
QVector<QByteArray> m_propsNames; // for comparision with received name of DRM object
class Property;
QVector<Property*> m_props;
uint32_t m_propsPending = 0;
uint32_t m_propsValid = 0;
virtual bool initProps() = 0; // only derived classes know names and quantity of properties
void initProp(int n, drmModeObjectProperties *properties, QVector<QByteArray> enumNames = QVector<QByteArray>(0));
class Property
{
public:
Property(drmModePropertyRes *prop, uint64_t val, QVector<QByteArray> enumNames);
virtual ~Property();
void initEnumMap(drmModePropertyRes *prop);
uint64_t enumMap(int n) {
return m_enumMap[n]; // TODO: test on index out of bounds?
}
uint32_t propId() {
return m_propId;
}
uint32_t value() {
return m_value;
}
void setValue(uint64_t new_value) {
m_value = new_value;
}
private:
uint32_t m_propId = 0;
QByteArray m_propName;
uint64_t m_value = 0;
QVector<uint64_t> m_enumMap;
QVector<QByteArray> m_enumNames;
};
};
}
#endif
/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2016 Roman Gilg <subdiff@gmail.com>
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_object_connector.h"
#include "logging.h"
namespace KWin
{
DrmConnector::DrmConnector(uint32_t connector_id, int fd)
: DrmObject(connector_id, fd)
{
}
DrmConnector::~DrmConnector() = default;
bool DrmConnector::init()
{
qCDebug(KWIN_DRM) << "Creating connector" << m_id;
if (!initProps()) {
return false;
}
return true;
}
bool DrmConnector::initProps()
{
m_propsNames = {
QByteArrayLiteral("CRTC_ID"),
};
drmModeObjectProperties *properties = drmModeObjectGetProperties(m_fd, m_id, DRM_MODE_OBJECT_CONNECTOR);
if (!properties) {
qCWarning(KWIN_DRM) << "Failed to get properties for connector " << m_id ;
return false;
}
int propCount = int(PropertyIndex::Count);
for (int j = 0; j < propCount; ++j) {
initProp(j, properties);
}
drmModeFreeObjectProperties(properties);
return true;
}
}
/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2016 Roman Gilg <subdiff@gmail.com>
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/>.
*********************************************************************/
#ifndef KWIN_DRM_OBJECT_CONNECTOR_H
#define KWIN_DRM_OBJECT_CONNECTOR_H
#include "drm_object.h"
namespace KWin
{
class DrmConnector : public DrmObject
{
public:
DrmConnector(uint32_t connector_id, int fd);
virtual ~DrmConnector();
bool init();
enum class PropertyIndex {
CrtcId = 0,
Count
};
bool initProps();
};
}
#endif
/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2016 Roman Gilg <subdiff@gmail.com>
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_object_crtc.h"
#include "logging.h"
namespace KWin