From e262c8df334af9c317d6dc7d0cee8e623343e714 Mon Sep 17 00:00:00 2001 From: Xaver Hugl Date: Tue, 30 Mar 2021 13:15:09 +0200 Subject: [PATCH] Move pageFlipHandler to DrmGpu --- src/plugins/platforms/drm/drm_backend.cpp | 46 +--------------- src/plugins/platforms/drm/drm_backend.h | 1 - src/plugins/platforms/drm/drm_gpu.cpp | 66 +++++++++++++++++++---- 3 files changed, 56 insertions(+), 57 deletions(-) diff --git a/src/plugins/platforms/drm/drm_backend.cpp b/src/plugins/platforms/drm/drm_backend.cpp index 5a6000952e..d99f3567b5 100644 --- a/src/plugins/platforms/drm/drm_backend.cpp +++ b/src/plugins/platforms/drm/drm_backend.cpp @@ -15,7 +15,7 @@ #include "cursor.h" #include "logging.h" #include "main.h" -#include "renderloop_p.h" +#include "renderloop.h" #include "scene_qpainter_drm_backend.h" #include "session.h" #include "udev.h" @@ -185,50 +185,6 @@ void DrmBackend::deactivate() m_active = false; } -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; -} - -void DrmBackend::pageFlipHandler(int fd, unsigned int frame, unsigned int sec, unsigned int usec, void *data) -{ - Q_UNUSED(fd) - Q_UNUSED(frame) - - auto output = static_cast(data); - DrmGpu *gpu = output->gpu(); - - std::chrono::nanoseconds timestamp = convertTimestamp(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); -} - bool DrmBackend::initialize() { connect(session(), &Session::activeChanged, this, &DrmBackend::activate); diff --git a/src/plugins/platforms/drm/drm_backend.h b/src/plugins/platforms/drm/drm_backend.h index 19f72986d8..d25fb1f9d9 100644 --- a/src/plugins/platforms/drm/drm_backend.h +++ b/src/plugins/platforms/drm/drm_backend.h @@ -91,7 +91,6 @@ private: friend class DrmGpu; void addOutput(DrmOutput* output); void removeOutput(DrmOutput* output); - static void pageFlipHandler(int fd, unsigned int frame, unsigned int sec, unsigned int usec, void *data); void activate(bool active); void reactivate(); void deactivate(); diff --git a/src/plugins/platforms/drm/drm_gpu.cpp b/src/plugins/platforms/drm/drm_gpu.cpp index d4ca98b30f..3aebf181b6 100644 --- a/src/plugins/platforms/drm/drm_gpu.cpp +++ b/src/plugins/platforms/drm/drm_gpu.cpp @@ -16,6 +16,7 @@ #include "abstract_egl_backend.h" #include "logging.h" #include "session.h" +#include "renderloop_p.h" #if HAVE_GBM #include "egl_gbm_backend.h" @@ -322,17 +323,6 @@ DrmPlane *DrmGpu::getCompatiblePlane(DrmPlane::TypeIndex typeIndex, DrmCrtc *crt return nullptr; } -void DrmGpu::dispatchEvents() -{ - if (!m_backend->session()->isActive()) { - return; - } - drmEventContext context = {}; - context.version = 2; - context.page_flip_handler = DrmBackend::pageFlipHandler; - drmHandleEvent(m_fd, &context); -} - void DrmGpu::waitIdle() { m_socketNotifier->setEnabled(false); @@ -363,4 +353,58 @@ void DrmGpu::waitIdle() m_socketNotifier->setEnabled(true); } +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(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); +} + }