kwin/src/plugins/platforms/drm/drm_object_connector.cpp

213 lines
7.8 KiB
C++
Raw Normal View History

2020-08-02 22:22:19 +00:00
/*
KWin - the KDE window manager
This file is part of the KDE project.
2020-08-02 22:22:19 +00:00
SPDX-FileCopyrightText: 2016 Roman Gilg <subdiff@gmail.com>
2020-08-02 22:22:19 +00:00
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "drm_object_connector.h"
#include "drm_gpu.h"
[DRM plugin] Remember static kernel objects, amplify use of DrmCrtc To get an image from KWin to the screen in the DRM pipeline we combine a CRTC, an encoder and a connector. These objects are static in the sense, that they represent real hardware on the graphics card, which doesn't change in a session. See here for more details: https://01.org/linuxgraphics/gfx-docs/drm/gpu/drm-kms.html Until now we used DrmOutput as the main representation for such an active rendering pipeline. I.e. it gets created and destroyed on hot plug events of displays. On the other side we had no fixed representation of the static kernel objects throughout the lifetime of KWin. This has several disadvantages: * We always need to query all available static objects on an hot plug event. * We can't manipulate the frame buffer of a CRTC after an output has been disconnected * Adding functionality for driving multiple displays on a single CRTC (i.e. cloning) would be difficult * We can't destroy the last frame buffer on display disconnect because the CRTC still accesses it and have therefore a memory leak on every display disconnect This patch solves these issues by storing representations of all available CRTC and Connector objects in DrmBackend on init via DrmCrtc and DrmConnector instances. On an hotplug event these vectors are looped for a fitting CRTC and Connector combinations. Buffer handling is moved to the respective CRTC instance. All changes in overview: * Query all available CRTCs and Connectors and save for subsequent hotplug events * Fix logic errors in `queryResources()` * Move framebuffers, buffer flip and blank logic in DrmCrtc * Remove `restoreSaved()`. It isn't necessary and is dangerous if the old framebuffer was deleted in the meantime. Also could reveal sensitive user info from old session. Test Plan: Login, logout, VT switching, connect and disconnect external monitor, energy saving mode. Reviewers: #kwin Subscribers: kwin, #kwin Tags: #kwin Differential Revision: https://phabricator.kde.org/D5118
2017-05-09 18:02:49 +00:00
#include "drm_pointer.h"
#include "logging.h"
2021-03-31 11:22:23 +00:00
#include <main.h>
// frameworks
#include <KConfigGroup>
2021-06-29 08:32:06 +00:00
#include <cerrno>
namespace KWin
{
DrmConnector::DrmConnector(DrmGpu *gpu, uint32_t connectorId)
: DrmObject(gpu, connectorId)
, m_conn(drmModeGetConnector(gpu->fd(), connectorId))
{
if (m_conn) {
for (int i = 0; i < m_conn->count_encoders; ++i) {
m_encoders << m_conn->encoders[i];
}
} else {
qCWarning(KWIN_DRM) << "drmModeGetConnector failed!" << strerror(errno);
[DRM plugin] Remember static kernel objects, amplify use of DrmCrtc To get an image from KWin to the screen in the DRM pipeline we combine a CRTC, an encoder and a connector. These objects are static in the sense, that they represent real hardware on the graphics card, which doesn't change in a session. See here for more details: https://01.org/linuxgraphics/gfx-docs/drm/gpu/drm-kms.html Until now we used DrmOutput as the main representation for such an active rendering pipeline. I.e. it gets created and destroyed on hot plug events of displays. On the other side we had no fixed representation of the static kernel objects throughout the lifetime of KWin. This has several disadvantages: * We always need to query all available static objects on an hot plug event. * We can't manipulate the frame buffer of a CRTC after an output has been disconnected * Adding functionality for driving multiple displays on a single CRTC (i.e. cloning) would be difficult * We can't destroy the last frame buffer on display disconnect because the CRTC still accesses it and have therefore a memory leak on every display disconnect This patch solves these issues by storing representations of all available CRTC and Connector objects in DrmBackend on init via DrmCrtc and DrmConnector instances. On an hotplug event these vectors are looped for a fitting CRTC and Connector combinations. Buffer handling is moved to the respective CRTC instance. All changes in overview: * Query all available CRTCs and Connectors and save for subsequent hotplug events * Fix logic errors in `queryResources()` * Move framebuffers, buffer flip and blank logic in DrmCrtc * Remove `restoreSaved()`. It isn't necessary and is dangerous if the old framebuffer was deleted in the meantime. Also could reveal sensitive user info from old session. Test Plan: Login, logout, VT switching, connect and disconnect external monitor, energy saving mode. Reviewers: #kwin Subscribers: kwin, #kwin Tags: #kwin Differential Revision: https://phabricator.kde.org/D5118
2017-05-09 18:02:49 +00:00
}
}
DrmConnector::~DrmConnector() = default;
bool DrmConnector::init()
{
if (!m_conn || !m_conn->count_modes) {
return false;
}
qCDebug(KWIN_DRM) << "Creating connector" << id();
2021-03-31 11:22:23 +00:00
if (!initProps({
PropertyDefinition(QByteArrayLiteral("CRTC_ID")),
PropertyDefinition(QByteArrayLiteral("non-desktop")),
PropertyDefinition(QByteArrayLiteral("DPMS")),
PropertyDefinition(QByteArrayLiteral("EDID")),
PropertyDefinition(QByteArrayLiteral("overscan")),
PropertyDefinition(QByteArrayLiteral("vrr_capable")),
PropertyDefinition(QByteArrayLiteral("underscan"), {
2021-06-22 21:28:08 +00:00
QByteArrayLiteral("off"),
QByteArrayLiteral("on"),
QByteArrayLiteral("auto")
}),
PropertyDefinition(QByteArrayLiteral("underscan vborder")),
PropertyDefinition(QByteArrayLiteral("underscan hborder")),
2021-03-31 11:22:23 +00:00
}, DRM_MODE_OBJECT_CONNECTOR)) {
return false;
}
if (auto dpmsProp = m_props[static_cast<uint32_t>(PropertyIndex::Dpms)]) {
dpmsProp->setLegacy();
} else {
qCDebug(KWIN_DRM) << "Could not find DPMS property!";
}
2021-06-22 21:28:08 +00:00
auto underscan = m_props[static_cast<uint32_t>(PropertyIndex::Underscan)];
auto vborder = m_props[static_cast<uint32_t>(PropertyIndex::Underscan_vborder)];
auto hborder = m_props[static_cast<uint32_t>(PropertyIndex::Underscan_hborder)];
if (underscan && vborder && hborder) {
underscan->setEnum(vborder->value() > 0 ? UnderscanOptions::On : UnderscanOptions::Off);
} else {
deleteProp(PropertyIndex::Underscan);
deleteProp(PropertyIndex::Underscan_vborder);
deleteProp(PropertyIndex::Underscan_hborder);
}
2021-03-31 11:22:23 +00:00
// parse edid
auto edidProp = m_props[static_cast<uint32_t>(PropertyIndex::Edid)];
if (edidProp && edidProp->blob() && edidProp->blob()->data) {
m_edid = Edid(edidProp->blob()->data, edidProp->blob()->length);
if (!m_edid.isValid()) {
2021-03-31 11:22:23 +00:00
qCWarning(KWIN_DRM, "Couldn't parse EDID for connector with id %d", id());
}
} else {
qCDebug(KWIN_DRM) << "Could not find edid for connector" << this;
2021-03-31 11:22:23 +00:00
}
deleteProp(PropertyIndex::Edid);
2021-03-31 11:22:23 +00:00
// check the physical size
if (m_edid.physicalSize().isEmpty()) {
2021-03-31 11:22:23 +00:00
m_physicalSize = QSize(m_conn->mmWidth, m_conn->mmHeight);
} else {
m_physicalSize = m_edid.physicalSize();
2021-03-31 11:22:23 +00:00
}
// the size might be completely borked. E.g. Samsung SyncMaster 2494HS reports 160x90 while in truth it's 520x292
// as this information is used to calculate DPI info, it's going to result in everything being huge
const QByteArray unknown = QByteArrayLiteral("unknown");
KConfigGroup group = kwinApp()->config()->group("EdidOverwrite").group(m_edid.eisaId().isEmpty() ? unknown : m_edid.eisaId())
.group(m_edid.monitorName().isEmpty() ? unknown : m_edid.monitorName())
.group(m_edid.serialNumber().isEmpty() ? unknown : m_edid.serialNumber());
2021-03-31 11:22:23 +00:00
if (group.hasKey("PhysicalSize")) {
const QSize overwriteSize = group.readEntry("PhysicalSize", m_physicalSize);
qCWarning(KWIN_DRM) << "Overwriting monitor physical size for" << m_edid.eisaId() << "/" << m_edid.monitorName() << "/" << m_edid.serialNumber() << " from " << m_physicalSize << "to " << overwriteSize;
2021-03-31 11:22:23 +00:00
m_physicalSize = overwriteSize;
}
return true;
}
[DRM plugin] Remember static kernel objects, amplify use of DrmCrtc To get an image from KWin to the screen in the DRM pipeline we combine a CRTC, an encoder and a connector. These objects are static in the sense, that they represent real hardware on the graphics card, which doesn't change in a session. See here for more details: https://01.org/linuxgraphics/gfx-docs/drm/gpu/drm-kms.html Until now we used DrmOutput as the main representation for such an active rendering pipeline. I.e. it gets created and destroyed on hot plug events of displays. On the other side we had no fixed representation of the static kernel objects throughout the lifetime of KWin. This has several disadvantages: * We always need to query all available static objects on an hot plug event. * We can't manipulate the frame buffer of a CRTC after an output has been disconnected * Adding functionality for driving multiple displays on a single CRTC (i.e. cloning) would be difficult * We can't destroy the last frame buffer on display disconnect because the CRTC still accesses it and have therefore a memory leak on every display disconnect This patch solves these issues by storing representations of all available CRTC and Connector objects in DrmBackend on init via DrmCrtc and DrmConnector instances. On an hotplug event these vectors are looped for a fitting CRTC and Connector combinations. Buffer handling is moved to the respective CRTC instance. All changes in overview: * Query all available CRTCs and Connectors and save for subsequent hotplug events * Fix logic errors in `queryResources()` * Move framebuffers, buffer flip and blank logic in DrmCrtc * Remove `restoreSaved()`. It isn't necessary and is dangerous if the old framebuffer was deleted in the meantime. Also could reveal sensitive user info from old session. Test Plan: Login, logout, VT switching, connect and disconnect external monitor, energy saving mode. Reviewers: #kwin Subscribers: kwin, #kwin Tags: #kwin Differential Revision: https://phabricator.kde.org/D5118
2017-05-09 18:02:49 +00:00
bool DrmConnector::isConnected()
{
DrmScopedPointer<drmModeConnector> con(drmModeGetConnector(gpu()->fd(), id()));
if (!con) {
[DRM plugin] Remember static kernel objects, amplify use of DrmCrtc To get an image from KWin to the screen in the DRM pipeline we combine a CRTC, an encoder and a connector. These objects are static in the sense, that they represent real hardware on the graphics card, which doesn't change in a session. See here for more details: https://01.org/linuxgraphics/gfx-docs/drm/gpu/drm-kms.html Until now we used DrmOutput as the main representation for such an active rendering pipeline. I.e. it gets created and destroyed on hot plug events of displays. On the other side we had no fixed representation of the static kernel objects throughout the lifetime of KWin. This has several disadvantages: * We always need to query all available static objects on an hot plug event. * We can't manipulate the frame buffer of a CRTC after an output has been disconnected * Adding functionality for driving multiple displays on a single CRTC (i.e. cloning) would be difficult * We can't destroy the last frame buffer on display disconnect because the CRTC still accesses it and have therefore a memory leak on every display disconnect This patch solves these issues by storing representations of all available CRTC and Connector objects in DrmBackend on init via DrmCrtc and DrmConnector instances. On an hotplug event these vectors are looped for a fitting CRTC and Connector combinations. Buffer handling is moved to the respective CRTC instance. All changes in overview: * Query all available CRTCs and Connectors and save for subsequent hotplug events * Fix logic errors in `queryResources()` * Move framebuffers, buffer flip and blank logic in DrmCrtc * Remove `restoreSaved()`. It isn't necessary and is dangerous if the old framebuffer was deleted in the meantime. Also could reveal sensitive user info from old session. Test Plan: Login, logout, VT switching, connect and disconnect external monitor, energy saving mode. Reviewers: #kwin Subscribers: kwin, #kwin Tags: #kwin Differential Revision: https://phabricator.kde.org/D5118
2017-05-09 18:02:49 +00:00
return false;
}
return con->connection == DRM_MODE_CONNECTED;
[DRM plugin] Remember static kernel objects, amplify use of DrmCrtc To get an image from KWin to the screen in the DRM pipeline we combine a CRTC, an encoder and a connector. These objects are static in the sense, that they represent real hardware on the graphics card, which doesn't change in a session. See here for more details: https://01.org/linuxgraphics/gfx-docs/drm/gpu/drm-kms.html Until now we used DrmOutput as the main representation for such an active rendering pipeline. I.e. it gets created and destroyed on hot plug events of displays. On the other side we had no fixed representation of the static kernel objects throughout the lifetime of KWin. This has several disadvantages: * We always need to query all available static objects on an hot plug event. * We can't manipulate the frame buffer of a CRTC after an output has been disconnected * Adding functionality for driving multiple displays on a single CRTC (i.e. cloning) would be difficult * We can't destroy the last frame buffer on display disconnect because the CRTC still accesses it and have therefore a memory leak on every display disconnect This patch solves these issues by storing representations of all available CRTC and Connector objects in DrmBackend on init via DrmCrtc and DrmConnector instances. On an hotplug event these vectors are looped for a fitting CRTC and Connector combinations. Buffer handling is moved to the respective CRTC instance. All changes in overview: * Query all available CRTCs and Connectors and save for subsequent hotplug events * Fix logic errors in `queryResources()` * Move framebuffers, buffer flip and blank logic in DrmCrtc * Remove `restoreSaved()`. It isn't necessary and is dangerous if the old framebuffer was deleted in the meantime. Also could reveal sensitive user info from old session. Test Plan: Login, logout, VT switching, connect and disconnect external monitor, energy saving mode. Reviewers: #kwin Subscribers: kwin, #kwin Tags: #kwin Differential Revision: https://phabricator.kde.org/D5118
2017-05-09 18:02:49 +00:00
}
2021-03-31 11:22:23 +00:00
static QHash<int, QByteArray> s_connectorNames = {
{DRM_MODE_CONNECTOR_Unknown, QByteArrayLiteral("Unknown")},
{DRM_MODE_CONNECTOR_VGA, QByteArrayLiteral("VGA")},
{DRM_MODE_CONNECTOR_DVII, QByteArrayLiteral("DVI-I")},
{DRM_MODE_CONNECTOR_DVID, QByteArrayLiteral("DVI-D")},
{DRM_MODE_CONNECTOR_DVIA, QByteArrayLiteral("DVI-A")},
{DRM_MODE_CONNECTOR_Composite, QByteArrayLiteral("Composite")},
{DRM_MODE_CONNECTOR_SVIDEO, QByteArrayLiteral("SVIDEO")},
{DRM_MODE_CONNECTOR_LVDS, QByteArrayLiteral("LVDS")},
{DRM_MODE_CONNECTOR_Component, QByteArrayLiteral("Component")},
{DRM_MODE_CONNECTOR_9PinDIN, QByteArrayLiteral("DIN")},
{DRM_MODE_CONNECTOR_DisplayPort, QByteArrayLiteral("DP")},
{DRM_MODE_CONNECTOR_HDMIA, QByteArrayLiteral("HDMI-A")},
{DRM_MODE_CONNECTOR_HDMIB, QByteArrayLiteral("HDMI-B")},
{DRM_MODE_CONNECTOR_TV, QByteArrayLiteral("TV")},
{DRM_MODE_CONNECTOR_eDP, QByteArrayLiteral("eDP")},
{DRM_MODE_CONNECTOR_VIRTUAL, QByteArrayLiteral("Virtual")},
{DRM_MODE_CONNECTOR_DSI, QByteArrayLiteral("DSI")},
#ifdef DRM_MODE_CONNECTOR_DPI
{DRM_MODE_CONNECTOR_DPI, QByteArrayLiteral("DPI")},
#endif
};
QString DrmConnector::connectorName() const
{
return s_connectorNames.value(m_conn->connector_type, QByteArrayLiteral("Unknown")) + QStringLiteral("-") + QString::number(m_conn->connector_type_id);
}
QString DrmConnector::modelName() const
{
if (m_edid.serialNumber().isEmpty()) {
return connectorName() + QLatin1Char('-') + m_edid.nameString();
} else {
return m_edid.nameString();
}
2021-03-31 11:22:23 +00:00
}
bool DrmConnector::isInternal() const
{
return m_conn->connector_type == DRM_MODE_CONNECTOR_LVDS || m_conn->connector_type == DRM_MODE_CONNECTOR_eDP
|| m_conn->connector_type == DRM_MODE_CONNECTOR_DSI;
}
QSize DrmConnector::physicalSize() const
{
return m_physicalSize;
}
bool DrmConnector::hasOverscan() const
{
2021-06-22 21:28:08 +00:00
return m_props[static_cast<uint32_t>(PropertyIndex::Overscan)] || m_props[static_cast<uint32_t>(PropertyIndex::Underscan)];
}
uint32_t DrmConnector::overscan() const
{
if (const auto &prop = m_props[static_cast<uint32_t>(PropertyIndex::Overscan)]) {
return prop->value();
2021-06-22 21:28:08 +00:00
} else if (const auto &prop = m_props[static_cast<uint32_t>(PropertyIndex::Underscan_vborder)]) {
return prop->value();
}
return 0;
}
2021-06-22 21:28:08 +00:00
void DrmConnector::setOverscan(uint32_t overscan, const QSize &modeSize)
{
2021-06-22 21:28:08 +00:00
if (auto prop = m_props[static_cast<uint32_t>(PropertyIndex::Overscan)]) {
prop->setValue(overscan);
} else if (auto prop = m_props[static_cast<uint32_t>(PropertyIndex::Underscan)]) {
float aspectRatio = modeSize.width() / static_cast<float>(modeSize.height());
prop->setEnum(overscan > 0 ? UnderscanOptions::On : UnderscanOptions::Off);
uint32_t hborder = overscan * aspectRatio;
2021-06-22 21:28:08 +00:00
if (hborder > 128) {
hborder = 128;
overscan = 128 / aspectRatio;
2021-06-22 21:28:08 +00:00
}
// overscan only goes from 0-100 so we cut off the 101-128 value range of underscan_vborder
setValue(PropertyIndex::Underscan_vborder, overscan);
setValue(PropertyIndex::Underscan_hborder, hborder);
}
}
2021-03-31 11:22:23 +00:00
bool DrmConnector::vrrCapable() const
{
if (const auto &prop = m_props[static_cast<int>(PropertyIndex::VrrCapable)]) {
return prop->value();
}
return false;
}
}