2020-10-05 21:05:55 +00:00
|
|
|
/*
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
|
|
|
|
|
|
|
SPDX-FileCopyrightText: 2020 Xaver Hugl <xaver.hugl@gmail.com>
|
|
|
|
|
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "drm_gpu.h"
|
|
|
|
|
|
|
|
#include "drm_backend.h"
|
|
|
|
#include "drm_output.h"
|
|
|
|
#include "drm_object_connector.h"
|
|
|
|
#include "drm_object_crtc.h"
|
|
|
|
#include "abstract_egl_backend.h"
|
|
|
|
#include "logging.h"
|
2021-01-30 13:22:07 +00:00
|
|
|
#include "session.h"
|
2021-03-30 11:15:09 +00:00
|
|
|
#include "renderloop_p.h"
|
2020-10-05 21:05:55 +00:00
|
|
|
|
|
|
|
#if HAVE_GBM
|
|
|
|
#include "egl_gbm_backend.h"
|
|
|
|
#include <gbm.h>
|
|
|
|
#include "gbm_dmabuf.h"
|
|
|
|
#endif
|
|
|
|
// system
|
|
|
|
#include <algorithm>
|
2021-02-23 06:36:49 +00:00
|
|
|
#include <errno.h>
|
2021-03-09 08:09:21 +00:00
|
|
|
#include <poll.h>
|
2020-10-05 21:05:55 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
// drm
|
|
|
|
#include <xf86drm.h>
|
|
|
|
#include <xf86drmMode.h>
|
|
|
|
#include <libdrm/drm_mode.h>
|
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
2021-04-01 11:21:37 +00:00
|
|
|
DrmGpu::DrmGpu(DrmBackend *backend, QByteArray devNode, int fd, int drmId)
|
|
|
|
: m_backend(backend)
|
|
|
|
, m_devNode(devNode)
|
|
|
|
, m_fd(fd)
|
|
|
|
, m_drmId(drmId)
|
|
|
|
, m_atomicModeSetting(false)
|
|
|
|
, m_gbmDevice(nullptr)
|
2020-10-05 21:05:55 +00:00
|
|
|
{
|
2020-10-12 19:27:53 +00:00
|
|
|
uint64_t capability = 0;
|
|
|
|
|
|
|
|
if (drmGetCap(fd, DRM_CAP_CURSOR_WIDTH, &capability) == 0) {
|
|
|
|
m_cursorSize.setWidth(capability);
|
|
|
|
} else {
|
|
|
|
m_cursorSize.setWidth(64);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (drmGetCap(fd, DRM_CAP_CURSOR_HEIGHT, &capability) == 0) {
|
|
|
|
m_cursorSize.setHeight(capability);
|
|
|
|
} else {
|
|
|
|
m_cursorSize.setHeight(64);
|
|
|
|
}
|
2020-11-27 19:57:24 +00:00
|
|
|
|
Introduce RenderLoop
At the moment, our frame scheduling infrastructure is still heavily
based on Xinerama-style rendering. Specifically, we assume that painting
is driven by a single timer, etc.
This change introduces a new type - RenderLoop. Its main purpose is to
drive compositing on a specific output, or in case of X11, on the
overlay window.
With RenderLoop, compositing is synchronized to vblank events. It
exposes the last and the next estimated presentation timestamp. The
expected presentation timestamp can be used by effects to ensure that
animations are synchronized with the upcoming vblank event.
On Wayland, every outputs has its own render loop. On X11, per screen
rendering is not possible, therefore the platform exposes the render
loop for the overlay window. Ideally, the Scene has to expose the
RenderLoop, but as the first step towards better compositing scheduling
it's good as is for the time being.
The RenderLoop tries to minimize the latency by delaying compositing as
close as possible to the next vblank event. One tricky thing about it is
that if compositing is too close to the next vblank event, animations
may become a little bit choppy. However, increasing the latency reduces
the choppiness.
Given that, there is no any "silver bullet" solution for the choppiness
issue, a new option has been added in the Compositing KCM to specify the
amount of latency. By default, it's "Medium," but if a user is not
satisfied with the upstream default, they can tweak it.
2020-11-19 08:52:29 +00:00
|
|
|
int ret = drmGetCap(fd, DRM_CAP_TIMESTAMP_MONOTONIC, &capability);
|
|
|
|
if (ret == 0 && capability == 1) {
|
|
|
|
m_presentationClock = CLOCK_MONOTONIC;
|
|
|
|
} else {
|
|
|
|
m_presentationClock = CLOCK_REALTIME;
|
|
|
|
}
|
|
|
|
|
2021-03-22 10:43:24 +00:00
|
|
|
if (!qEnvironmentVariableIsSet("KWIN_DRM_NO_MODIFIERS")) {
|
|
|
|
m_addFB2ModifiersSupported = drmGetCap(fd, DRM_CAP_ADDFB2_MODIFIERS, &capability) && capability == 1;
|
|
|
|
qCDebug(KWIN_DRM) << "drmModeAddFB2WithModifiers is" << (m_addFB2ModifiersSupported ? "supported" : "not supported");
|
|
|
|
}
|
|
|
|
|
2020-10-25 17:47:58 +00:00
|
|
|
// find out if this GPU is using the NVidia proprietary driver
|
|
|
|
DrmScopedPointer<drmVersion> version(drmGetVersion(fd));
|
|
|
|
m_useEglStreams = strstr(version->name, "nvidia-drm");
|
|
|
|
|
2021-02-19 11:59:43 +00:00
|
|
|
m_socketNotifier = new QSocketNotifier(fd, QSocketNotifier::Read, this);
|
2021-02-19 13:00:49 +00:00
|
|
|
connect(m_socketNotifier, &QSocketNotifier::activated, this, &DrmGpu::dispatchEvents);
|
2020-10-05 21:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DrmGpu::~DrmGpu()
|
|
|
|
{
|
2021-02-22 18:17:23 +00:00
|
|
|
waitIdle();
|
2020-12-02 12:12:56 +00:00
|
|
|
if (m_eglDisplay != EGL_NO_DISPLAY) {
|
|
|
|
eglTerminate(m_eglDisplay);
|
|
|
|
}
|
2020-10-05 21:05:55 +00:00
|
|
|
#if HAVE_GBM
|
2021-02-25 18:13:57 +00:00
|
|
|
if (m_gbmDevice) {
|
|
|
|
gbm_device_destroy(m_gbmDevice);
|
|
|
|
}
|
2020-10-05 21:05:55 +00:00
|
|
|
#endif
|
2021-04-19 16:07:02 +00:00
|
|
|
qDeleteAll(m_outputs);
|
2020-10-05 21:05:55 +00:00
|
|
|
qDeleteAll(m_crtcs);
|
|
|
|
qDeleteAll(m_connectors);
|
|
|
|
qDeleteAll(m_planes);
|
2021-02-19 11:59:43 +00:00
|
|
|
delete m_socketNotifier;
|
2021-01-30 13:22:07 +00:00
|
|
|
m_backend->session()->closeRestricted(m_fd);
|
2020-10-05 21:05:55 +00:00
|
|
|
}
|
|
|
|
|
Introduce RenderLoop
At the moment, our frame scheduling infrastructure is still heavily
based on Xinerama-style rendering. Specifically, we assume that painting
is driven by a single timer, etc.
This change introduces a new type - RenderLoop. Its main purpose is to
drive compositing on a specific output, or in case of X11, on the
overlay window.
With RenderLoop, compositing is synchronized to vblank events. It
exposes the last and the next estimated presentation timestamp. The
expected presentation timestamp can be used by effects to ensure that
animations are synchronized with the upcoming vblank event.
On Wayland, every outputs has its own render loop. On X11, per screen
rendering is not possible, therefore the platform exposes the render
loop for the overlay window. Ideally, the Scene has to expose the
RenderLoop, but as the first step towards better compositing scheduling
it's good as is for the time being.
The RenderLoop tries to minimize the latency by delaying compositing as
close as possible to the next vblank event. One tricky thing about it is
that if compositing is too close to the next vblank event, animations
may become a little bit choppy. However, increasing the latency reduces
the choppiness.
Given that, there is no any "silver bullet" solution for the choppiness
issue, a new option has been added in the Compositing KCM to specify the
amount of latency. By default, it's "Medium," but if a user is not
satisfied with the upstream default, they can tweak it.
2020-11-19 08:52:29 +00:00
|
|
|
clockid_t DrmGpu::presentationClock() const
|
|
|
|
{
|
|
|
|
return m_presentationClock;
|
|
|
|
}
|
|
|
|
|
2020-10-05 21:05:55 +00:00
|
|
|
void DrmGpu::tryAMS()
|
|
|
|
{
|
|
|
|
m_atomicModeSetting = false;
|
|
|
|
if (drmSetClientCap(m_fd, DRM_CLIENT_CAP_ATOMIC, 1) == 0) {
|
2021-02-18 09:25:38 +00:00
|
|
|
m_atomicModeSetting = true;
|
2020-10-05 21:05:55 +00:00
|
|
|
DrmScopedPointer<drmModePlaneRes> planeResources(drmModeGetPlaneResources(m_fd));
|
|
|
|
if (!planeResources) {
|
|
|
|
qCWarning(KWIN_DRM) << "Failed to get plane resources. Falling back to legacy mode on GPU " << m_devNode;
|
2021-02-15 17:28:58 +00:00
|
|
|
m_atomicModeSetting = false;
|
|
|
|
return;
|
2020-10-05 21:05:55 +00:00
|
|
|
}
|
2021-02-15 17:28:58 +00:00
|
|
|
qCDebug(KWIN_DRM) << "Using Atomic Mode Setting on gpu" << m_devNode;
|
|
|
|
qCDebug(KWIN_DRM) << "Number of planes on GPU" << m_devNode << ":" << planeResources->count_planes;
|
|
|
|
// create the plane objects
|
|
|
|
for (unsigned int i = 0; i < planeResources->count_planes; ++i) {
|
|
|
|
DrmScopedPointer<drmModePlane> kplane(drmModeGetPlane(m_fd, planeResources->planes[i]));
|
2021-04-01 11:15:42 +00:00
|
|
|
DrmPlane *p = new DrmPlane(this, kplane->plane_id);
|
2021-02-15 17:28:58 +00:00
|
|
|
if (p->init()) {
|
|
|
|
m_planes << p;
|
|
|
|
} else {
|
2020-10-05 21:05:55 +00:00
|
|
|
delete p;
|
|
|
|
}
|
|
|
|
}
|
2021-02-15 17:28:58 +00:00
|
|
|
if (m_planes.isEmpty()) {
|
|
|
|
qCWarning(KWIN_DRM) << "Failed to create any plane. Falling back to legacy mode on GPU " << m_devNode;
|
|
|
|
m_atomicModeSetting = false;
|
|
|
|
}
|
|
|
|
m_unusedPlanes = m_planes;
|
2020-10-05 21:05:55 +00:00
|
|
|
} else {
|
|
|
|
qCWarning(KWIN_DRM) << "drmSetClientCap for Atomic Mode Setting failed. Using legacy mode on GPU" << m_devNode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DrmGpu::updateOutputs()
|
|
|
|
{
|
|
|
|
auto oldConnectors = m_connectors;
|
|
|
|
auto oldCrtcs = m_crtcs;
|
|
|
|
DrmScopedPointer<drmModeRes> resources(drmModeGetResources(m_fd));
|
|
|
|
if (!resources) {
|
|
|
|
qCWarning(KWIN_DRM) << "drmModeGetResources failed";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < resources->count_connectors; ++i) {
|
|
|
|
const uint32_t currentConnector = resources->connectors[i];
|
|
|
|
auto it = std::find_if(m_connectors.constBegin(), m_connectors.constEnd(), [currentConnector] (DrmConnector *c) { return c->id() == currentConnector; });
|
|
|
|
if (it == m_connectors.constEnd()) {
|
2021-04-01 11:15:42 +00:00
|
|
|
auto c = new DrmConnector(this, currentConnector);
|
2021-01-26 20:23:44 +00:00
|
|
|
if (!c->init()) {
|
|
|
|
delete c;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (c->isNonDesktop()) {
|
|
|
|
delete c;
|
|
|
|
continue;
|
2020-10-25 19:18:06 +00:00
|
|
|
}
|
2021-02-05 18:55:07 +00:00
|
|
|
if (!c->isConnected()) {
|
|
|
|
delete c;
|
|
|
|
continue;
|
|
|
|
}
|
2020-10-05 21:05:55 +00:00
|
|
|
m_connectors << c;
|
|
|
|
} else {
|
|
|
|
oldConnectors.removeOne(*it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < resources->count_crtcs; ++i) {
|
|
|
|
const uint32_t currentCrtc = resources->crtcs[i];
|
|
|
|
auto it = std::find_if(m_crtcs.constBegin(), m_crtcs.constEnd(), [currentCrtc] (DrmCrtc *c) { return c->id() == currentCrtc; });
|
|
|
|
if (it == m_crtcs.constEnd()) {
|
2021-04-01 11:23:02 +00:00
|
|
|
auto c = new DrmCrtc(this, currentCrtc, i);
|
2021-01-26 20:23:44 +00:00
|
|
|
if (!c->init()) {
|
2020-10-05 21:05:55 +00:00
|
|
|
delete c;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
m_crtcs << c;
|
|
|
|
} else {
|
|
|
|
oldCrtcs.removeOne(*it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto c : qAsConst(oldConnectors)) {
|
|
|
|
m_connectors.removeOne(c);
|
|
|
|
}
|
|
|
|
for (auto c : qAsConst(oldCrtcs)) {
|
|
|
|
m_crtcs.removeOne(c);
|
|
|
|
}
|
2020-11-27 19:57:24 +00:00
|
|
|
|
2020-10-05 21:05:55 +00:00
|
|
|
QVector<DrmOutput*> connectedOutputs;
|
|
|
|
QVector<DrmConnector*> pendingConnectors;
|
|
|
|
|
|
|
|
// split up connected connectors in already or not yet assigned ones
|
|
|
|
for (DrmConnector *con : qAsConst(m_connectors)) {
|
|
|
|
if (!con->isConnected()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DrmOutput *o = findOutput(con->id())) {
|
|
|
|
connectedOutputs << o;
|
|
|
|
} else {
|
|
|
|
pendingConnectors << con;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for outputs which got removed
|
|
|
|
QVector<DrmOutput*> removedOutputs;
|
|
|
|
auto it = m_outputs.begin();
|
|
|
|
while (it != m_outputs.end()) {
|
|
|
|
if (connectedOutputs.contains(*it)) {
|
|
|
|
it++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
DrmOutput *removed = *it;
|
|
|
|
it = m_outputs.erase(it);
|
|
|
|
removedOutputs.append(removed);
|
|
|
|
}
|
2020-11-27 19:57:24 +00:00
|
|
|
|
2020-10-05 21:05:55 +00:00
|
|
|
for (DrmConnector *con : qAsConst(pendingConnectors)) {
|
|
|
|
DrmScopedPointer<drmModeConnector> connector(drmModeGetConnector(m_fd, con->id()));
|
|
|
|
if (!connector) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (connector->count_modes == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
bool outputDone = false;
|
|
|
|
|
|
|
|
QVector<uint32_t> encoders = con->encoders();
|
|
|
|
for (auto encId : qAsConst(encoders)) {
|
|
|
|
DrmScopedPointer<drmModeEncoder> encoder(drmModeGetEncoder(m_fd, encId));
|
|
|
|
if (!encoder) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (DrmCrtc *crtc : qAsConst(m_crtcs)) {
|
2021-04-01 11:28:18 +00:00
|
|
|
if (!(encoder->possible_crtcs & (1 << crtc->pipeIndex()))) {
|
2020-10-05 21:05:55 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-11-27 19:57:24 +00:00
|
|
|
|
2020-10-05 21:05:55 +00:00
|
|
|
// check if crtc isn't used yet -- currently we don't allow multiple outputs on one crtc (cloned mode)
|
|
|
|
auto it = std::find_if(connectedOutputs.constBegin(), connectedOutputs.constEnd(),
|
|
|
|
[crtc] (DrmOutput *o) {
|
|
|
|
return o->m_crtc == crtc;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if (it != connectedOutputs.constEnd()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we found a suitable encoder+crtc
|
|
|
|
// TODO: we could avoid these lib drm calls if we store all struct data in DrmCrtc and DrmConnector in the beginning
|
|
|
|
DrmScopedPointer<drmModeCrtc> modeCrtc(drmModeGetCrtc(m_fd, crtc->id()));
|
|
|
|
if (!modeCrtc) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-04-19 10:22:29 +00:00
|
|
|
auto primary = getCompatiblePlane(DrmPlane::TypeIndex::Primary, crtc);
|
|
|
|
if (m_atomicModeSetting && !primary) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-10-05 21:05:55 +00:00
|
|
|
DrmOutput *output = new DrmOutput(this->m_backend, this);
|
|
|
|
output->m_conn = con;
|
|
|
|
output->m_crtc = crtc;
|
2021-04-19 10:22:29 +00:00
|
|
|
output->m_primaryPlane = primary;
|
2020-10-05 21:05:55 +00:00
|
|
|
|
2021-03-27 14:01:34 +00:00
|
|
|
qCDebug(KWIN_DRM) << "For new output use mode" << con->currentMode().mode.name;
|
|
|
|
if (!output->init()) {
|
2020-10-05 21:05:55 +00:00
|
|
|
qCWarning(KWIN_DRM) << "Failed to create output for connector " << con->id();
|
|
|
|
delete output;
|
|
|
|
continue;
|
|
|
|
}
|
2020-10-12 19:27:53 +00:00
|
|
|
if (!output->initCursor(m_cursorSize)) {
|
2020-10-28 07:03:09 +00:00
|
|
|
m_backend->setSoftwareCursorForced(true);
|
2020-10-05 21:05:55 +00:00
|
|
|
}
|
2020-11-28 17:53:41 +00:00
|
|
|
qCDebug(KWIN_DRM) << "Found new output with uuid" << output->uuid() << "on gpu" << m_devNode;
|
2020-10-05 21:05:55 +00:00
|
|
|
|
|
|
|
connectedOutputs << output;
|
|
|
|
emit outputAdded(output);
|
|
|
|
outputDone = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (outputDone) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::sort(connectedOutputs.begin(), connectedOutputs.end(), [] (DrmOutput *a, DrmOutput *b) { return a->m_conn->id() < b->m_conn->id(); });
|
|
|
|
m_outputs = connectedOutputs;
|
2020-11-27 19:57:24 +00:00
|
|
|
|
2020-10-05 21:05:55 +00:00
|
|
|
for(DrmOutput *removedOutput : removedOutputs) {
|
|
|
|
emit outputRemoved(removedOutput);
|
|
|
|
removedOutput->teardown();
|
|
|
|
removedOutput->m_crtc = nullptr;
|
2021-02-05 18:55:07 +00:00
|
|
|
m_connectors.removeOne(removedOutput->m_conn);
|
|
|
|
delete removedOutput->m_conn;
|
2020-10-05 21:05:55 +00:00
|
|
|
removedOutput->m_conn = nullptr;
|
2021-02-15 17:28:58 +00:00
|
|
|
if (removedOutput->m_primaryPlane) {
|
|
|
|
m_unusedPlanes << removedOutput->m_primaryPlane;
|
|
|
|
}
|
2020-10-05 21:05:55 +00:00
|
|
|
}
|
2020-11-27 19:57:24 +00:00
|
|
|
|
2020-10-05 21:05:55 +00:00
|
|
|
qDeleteAll(oldConnectors);
|
|
|
|
qDeleteAll(oldCrtcs);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
DrmOutput *DrmGpu::findOutput(quint32 connector)
|
|
|
|
{
|
|
|
|
auto it = std::find_if(m_outputs.constBegin(), m_outputs.constEnd(), [connector] (DrmOutput *o) {
|
|
|
|
return o->m_conn->id() == connector;
|
|
|
|
});
|
|
|
|
if (it != m_outputs.constEnd()) {
|
|
|
|
return *it;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-02-15 17:28:58 +00:00
|
|
|
DrmPlane *DrmGpu::getCompatiblePlane(DrmPlane::TypeIndex typeIndex, DrmCrtc *crtc)
|
|
|
|
{
|
|
|
|
for (auto plane : m_unusedPlanes) {
|
|
|
|
if (plane->type() != typeIndex) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-04-01 11:28:18 +00:00
|
|
|
if (plane->isCrtcSupported(crtc->pipeIndex())) {
|
2021-02-15 17:28:58 +00:00
|
|
|
m_unusedPlanes.removeOne(plane);
|
|
|
|
return plane;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-02-22 18:17:23 +00:00
|
|
|
void DrmGpu::waitIdle()
|
|
|
|
{
|
|
|
|
m_socketNotifier->setEnabled(false);
|
|
|
|
while (true) {
|
|
|
|
const bool idle = std::all_of(m_outputs.constBegin(), m_outputs.constEnd(), [](DrmOutput *output){
|
|
|
|
return !output->m_pageFlipPending;
|
|
|
|
});
|
|
|
|
if (idle) {
|
|
|
|
break;
|
|
|
|
}
|
2021-03-09 08:09:21 +00:00
|
|
|
pollfd pfds[1];
|
|
|
|
pfds[0].fd = m_fd;
|
|
|
|
pfds[0].events = POLLIN;
|
|
|
|
|
|
|
|
const int ready = poll(pfds, 1, 30000);
|
|
|
|
if (ready < 0) {
|
|
|
|
if (errno != EINTR) {
|
|
|
|
qCWarning(KWIN_DRM) << Q_FUNC_INFO << "poll() failed:" << strerror(errno);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (ready == 0) {
|
2021-02-22 18:17:23 +00:00
|
|
|
qCWarning(KWIN_DRM) << "No drm events for gpu" << m_devNode << "within last 30 seconds";
|
|
|
|
break;
|
2021-03-09 08:09:21 +00:00
|
|
|
} else {
|
|
|
|
dispatchEvents();
|
2021-02-22 18:17:23 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
m_socketNotifier->setEnabled(true);
|
|
|
|
}
|
|
|
|
|
2021-03-30 11:15:09 +00:00
|
|
|
static std::chrono::nanoseconds convertTimestamp(const timespec ×tamp)
|
|
|
|
{
|
|
|
|
return std::chrono::seconds(timestamp.tv_sec) + std::chrono::nanoseconds(timestamp.tv_nsec);
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::chrono::nanoseconds convertTimestamp(clockid_t sourceClock, clockid_t targetClock,
|
|
|
|
const timespec ×tamp)
|
|
|
|
{
|
|
|
|
if (sourceClock == targetClock) {
|
|
|
|
return convertTimestamp(timestamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
timespec sourceCurrentTime = {};
|
|
|
|
timespec targetCurrentTime = {};
|
|
|
|
|
|
|
|
clock_gettime(sourceClock, &sourceCurrentTime);
|
|
|
|
clock_gettime(targetClock, &targetCurrentTime);
|
|
|
|
|
|
|
|
const auto delta = convertTimestamp(sourceCurrentTime) - convertTimestamp(timestamp);
|
|
|
|
return convertTimestamp(targetCurrentTime) - delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pageFlipHandler(int fd, unsigned int frame, unsigned int sec, unsigned int usec, void *data)
|
|
|
|
{
|
|
|
|
Q_UNUSED(fd)
|
|
|
|
Q_UNUSED(frame)
|
|
|
|
|
|
|
|
auto output = static_cast<DrmOutput *>(data);
|
|
|
|
|
|
|
|
std::chrono::nanoseconds timestamp = convertTimestamp(output->gpu()->presentationClock(),
|
|
|
|
CLOCK_MONOTONIC,
|
|
|
|
{ sec, usec * 1000 });
|
|
|
|
if (timestamp == std::chrono::nanoseconds::zero()) {
|
|
|
|
qCDebug(KWIN_DRM, "Got invalid timestamp (sec: %u, usec: %u) on output %s",
|
|
|
|
sec, usec, qPrintable(output->name()));
|
|
|
|
timestamp = std::chrono::steady_clock::now().time_since_epoch();
|
|
|
|
}
|
|
|
|
|
|
|
|
output->pageFlipped();
|
|
|
|
RenderLoopPrivate *renderLoopPrivate = RenderLoopPrivate::get(output->renderLoop());
|
|
|
|
renderLoopPrivate->notifyFrameCompleted(timestamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrmGpu::dispatchEvents()
|
|
|
|
{
|
|
|
|
if (!m_backend->session()->isActive()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
drmEventContext context = {};
|
|
|
|
context.version = 2;
|
|
|
|
context.page_flip_handler = pageFlipHandler;
|
|
|
|
drmHandleEvent(m_fd, &context);
|
|
|
|
}
|
|
|
|
|
2021-03-27 14:01:34 +00:00
|
|
|
QSharedPointer<DrmBuffer> DrmGpu::createTestbuffer(const QSize &size)
|
|
|
|
{
|
|
|
|
#if HAVE_GBM
|
|
|
|
if (m_gbmDevice) {
|
|
|
|
gbm_bo *bo = gbm_bo_create(m_gbmDevice, size.width(), size.height(), GBM_FORMAT_XRGB8888, GBM_BO_USE_SCANOUT);
|
|
|
|
if (bo) {
|
|
|
|
auto buffer = QSharedPointer<DrmGbmBuffer>::create(this, bo, nullptr);
|
|
|
|
if (buffer->bufferId()) {
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return QSharedPointer<DrmDumbBuffer>::create(this, size);
|
|
|
|
}
|
|
|
|
|
2020-10-05 21:05:55 +00:00
|
|
|
}
|