From b62dd292103e00eb2545d6fe762fef1a2c12d64a Mon Sep 17 00:00:00 2001 From: Vlad Zahorodnii Date: Thu, 23 Jun 2022 10:43:57 +0300 Subject: [PATCH] backends/drm: Change the base class of DrmLeaseOutput At the moment, the DrmLeaseOutput class inherits from the KWaylandServer::DrmLeaseConnectionV1Interface class. While this works, it's not a future-proof design. For example, kwin could also lease its "desktop" outputs in order to let another wayland compositor run alongside it. Also, it's a good practice to prefer composition over inheritance. --- src/backends/drm/drm_gpu.cpp | 19 +++++++++++-------- src/backends/drm/drm_lease_output.cpp | 16 +++++++++------- src/backends/drm/drm_lease_output.h | 6 +++--- src/wayland/drmleasedevice_v1_interface.cpp | 5 +++++ src/wayland/drmleasedevice_v1_interface.h | 2 ++ 5 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/backends/drm/drm_gpu.cpp b/src/backends/drm/drm_gpu.cpp index 78cf8b05d5..9879e1d05b 100644 --- a/src/backends/drm/drm_gpu.cpp +++ b/src/backends/drm/drm_gpu.cpp @@ -616,10 +616,13 @@ void DrmGpu::handleLeaseRequest(KWaylandServer::DrmLeaseV1Interface *leaseReques { QVector objects; QVector outputs; - const auto conns = leaseRequest->connectors(); - for (const auto &connector : conns) { - auto output = qobject_cast(connector); - if (m_leaseOutputs.contains(output) && !output->lease()) { + + const auto connectors = leaseRequest->connectors(); + for (KWaylandServer::DrmLeaseConnectorV1Interface *connector : connectors) { + if (DrmLeaseOutput *output = findLeaseOutput(connector->id())) { + if (output->lease()) { + continue; // already leased + } if (!output->addLeaseObjects(objects)) { leaseRequest->deny(); return; @@ -627,6 +630,7 @@ void DrmGpu::handleLeaseRequest(KWaylandServer::DrmLeaseV1Interface *leaseReques outputs << output; } } + uint32_t lesseeId; int fd = drmModeCreateLease(m_fd, objects.constData(), objects.count(), 0, &lesseeId); if (fd < 0) { @@ -650,10 +654,9 @@ void DrmGpu::handleLeaseRequest(KWaylandServer::DrmLeaseV1Interface *leaseReques void DrmGpu::handleLeaseRevoked(KWaylandServer::DrmLeaseV1Interface *lease) { - const auto conns = lease->connectors(); - for (const auto &connector : conns) { - auto output = qobject_cast(connector); - if (m_leaseOutputs.contains(output)) { + const auto connectors = lease->connectors(); + for (KWaylandServer::DrmLeaseConnectorV1Interface *connector : connectors) { + if (DrmLeaseOutput *output = findLeaseOutput(connector->id())) { output->leaseEnded(); } } diff --git a/src/backends/drm/drm_lease_output.cpp b/src/backends/drm/drm_lease_output.cpp index a48dcb35fd..57daadcb2b 100644 --- a/src/backends/drm/drm_lease_output.cpp +++ b/src/backends/drm/drm_lease_output.cpp @@ -24,14 +24,16 @@ namespace KWin { DrmLeaseOutput::DrmLeaseOutput(DrmPipeline *pipeline, KWaylandServer::DrmLeaseDeviceV1Interface *leaseDevice) - : KWaylandServer::DrmLeaseConnectorV1Interface( - leaseDevice, - pipeline->connector()->id(), - pipeline->connector()->modelName(), - QStringLiteral("%1 %2").arg(pipeline->connector()->edid()->manufacturerString(), pipeline->connector()->modelName())) - , m_pipeline(pipeline) + : m_pipeline(pipeline) { - qCDebug(KWIN_DRM) << "offering connector" << m_pipeline->connector()->id() << "for lease"; + const DrmConnector *connector = pipeline->connector(); + qCDebug(KWIN_DRM) << "offering connector" << connector->id() << "for lease"; + + m_offer = std::make_unique( + leaseDevice, + connector->id(), + connector->modelName(), + QStringLiteral("%1 %2").arg(connector->edid()->manufacturerString(), connector->modelName())); } DrmLeaseOutput::~DrmLeaseOutput() diff --git a/src/backends/drm/drm_lease_output.h b/src/backends/drm/drm_lease_output.h index ed02ce0844..3334bdfea2 100644 --- a/src/backends/drm/drm_lease_output.h +++ b/src/backends/drm/drm_lease_output.h @@ -24,12 +24,11 @@ class DrmPipeline; * that is not used directly by the compositor but is instead leased out to * applications (usually VR compositors) that drive the output themselves */ -class DrmLeaseOutput : public KWaylandServer::DrmLeaseConnectorV1Interface +class DrmLeaseOutput { - Q_OBJECT public: DrmLeaseOutput(DrmPipeline *pipeline, KWaylandServer::DrmLeaseDeviceV1Interface *leaseDevice); - ~DrmLeaseOutput() override; + ~DrmLeaseOutput(); bool addLeaseObjects(QVector &objectList); void leased(KWaylandServer::DrmLeaseV1Interface *lease); @@ -40,6 +39,7 @@ public: private: DrmPipeline *m_pipeline; + std::unique_ptr m_offer; KWaylandServer::DrmLeaseV1Interface *m_lease = nullptr; }; diff --git a/src/wayland/drmleasedevice_v1_interface.cpp b/src/wayland/drmleasedevice_v1_interface.cpp index 7431ee5e23..804762ab04 100644 --- a/src/wayland/drmleasedevice_v1_interface.cpp +++ b/src/wayland/drmleasedevice_v1_interface.cpp @@ -182,6 +182,11 @@ DrmLeaseConnectorV1Interface::~DrmLeaseConnectorV1Interface() } } +uint32_t DrmLeaseConnectorV1Interface::id() const +{ + return d->connectorId; +} + DrmLeaseConnectorV1Interface *DrmLeaseConnectorV1Interface::get(wl_resource *resource) { if (auto connectorPrivate = resource_cast(resource)) { diff --git a/src/wayland/drmleasedevice_v1_interface.h b/src/wayland/drmleasedevice_v1_interface.h index d360daaf92..c06423ccd8 100644 --- a/src/wayland/drmleasedevice_v1_interface.h +++ b/src/wayland/drmleasedevice_v1_interface.h @@ -72,6 +72,8 @@ public: explicit DrmLeaseConnectorV1Interface(DrmLeaseDeviceV1Interface *leaseDevice, uint32_t id, const QString &name, const QString &description); ~DrmLeaseConnectorV1Interface() override; + uint32_t id() const; + static DrmLeaseConnectorV1Interface *get(wl_resource *resource); private: