From bd689ef76ca42ad7eec3f81fd07c1efe6a1985f8 Mon Sep 17 00:00:00 2001 From: Vlad Zahorodnii Date: Tue, 12 Apr 2022 16:14:23 +0300 Subject: [PATCH] Simplify software cursor implementation With RenderTarget, CursorView and CursorDelegate can be merged. --- src/CMakeLists.txt | 5 +-- src/composite.cpp | 15 ++++---- src/composite.h | 1 - ...w_opengl.cpp => cursordelegate_opengl.cpp} | 16 +++++--- ...rview_opengl.h => cursordelegate_opengl.h} | 10 ++--- ...ainter.cpp => cursordelegate_qpainter.cpp} | 14 ++++--- src/cursordelegate_qpainter.h | 24 ++++++++++++ src/cursorview.cpp | 30 --------------- src/cursorview.h | 37 ------------------- src/cursorview_qpainter.h | 24 ------------ 10 files changed, 57 insertions(+), 119 deletions(-) rename src/{cursorview_opengl.cpp => cursordelegate_opengl.cpp} (82%) rename src/{cursorview_opengl.h => cursordelegate_opengl.h} (54%) rename src/{cursorview_qpainter.cpp => cursordelegate_qpainter.cpp} (57%) create mode 100644 src/cursordelegate_qpainter.h delete mode 100644 src/cursorview.cpp delete mode 100644 src/cursorview.h delete mode 100644 src/cursorview_qpainter.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bc7ecf7c53..7178c226e0 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -36,9 +36,8 @@ target_sources(kwin PRIVATE client_machine.cpp composite.cpp cursor.cpp - cursorview.cpp - cursorview_opengl.cpp - cursorview_qpainter.cpp + cursordelegate_opengl.cpp + cursordelegate_qpainter.cpp dbusinterface.cpp debug_console.cpp decorationitem.cpp diff --git a/src/composite.cpp b/src/composite.cpp index 5819395c2c..c77c71b295 100644 --- a/src/composite.cpp +++ b/src/composite.cpp @@ -11,8 +11,8 @@ #include #include "abstract_output.h" -#include "cursorview_opengl.h" -#include "cursorview_qpainter.h" +#include "cursordelegate_opengl.h" +#include "cursordelegate_qpainter.h" #include "dbusinterface.h" #include "decorations/decoratedclient.h" #include "deleted.h" @@ -203,7 +203,6 @@ bool Compositor::attemptOpenGLCompositing() m_backend = backend.take(); m_scene = scene.take(); - m_cursorView = new OpenGLCursorView(); // set strict binding if (options->isGlStrictBindingFollowsDriver()) { @@ -228,7 +227,6 @@ bool Compositor::attemptQPainterCompositing() m_backend = backend.take(); m_scene = scene.take(); - m_cursorView = new QPainterCursorView(); qCDebug(KWIN_CORE) << "QPainter compositing has been successfully initialized"; return true; @@ -441,7 +439,11 @@ void Compositor::addOutput(AbstractOutput *output) auto cursorLayer = new RenderLayer(output->renderLoop()); cursorLayer->setVisible(false); - cursorLayer->setDelegate(new CursorDelegate(m_cursorView)); + if (m_backend->compositingType() == OpenGLCompositing) { + cursorLayer->setDelegate(new CursorDelegateOpenGL()); + } else { + cursorLayer->setDelegate(new CursorDelegateQPainter()); + } cursorLayer->setParent(workspaceLayer); cursorLayer->setSuperlayer(workspaceLayer); @@ -551,9 +553,6 @@ void Compositor::stop() delete m_scene; m_scene = nullptr; - delete m_cursorView; - m_cursorView = nullptr; - delete m_backend; m_backend = nullptr; diff --git a/src/composite.h b/src/composite.h index 96f8173699..114da02972 100644 --- a/src/composite.h +++ b/src/composite.h @@ -153,7 +153,6 @@ private: QList m_unusedSupportProperties; QTimer m_unusedSupportPropertyTimer; Scene *m_scene = nullptr; - CursorView *m_cursorView = nullptr; RenderBackend *m_backend = nullptr; QHash m_superlayers; }; diff --git a/src/cursorview_opengl.cpp b/src/cursordelegate_opengl.cpp similarity index 82% rename from src/cursorview_opengl.cpp rename to src/cursordelegate_opengl.cpp index 654747b402..9dcc4895bb 100644 --- a/src/cursorview_opengl.cpp +++ b/src/cursordelegate_opengl.cpp @@ -4,7 +4,7 @@ SPDX-License-Identifier: GPL-2.0-or-later */ -#include "cursorview_opengl.h" +#include "cursordelegate_opengl.h" #include "cursor.h" #include "kwingltexture.h" #include "kwinglutils.h" @@ -14,17 +14,21 @@ namespace KWin { -OpenGLCursorView::OpenGLCursorView(QObject *parent) - : CursorView(parent) +CursorDelegateOpenGL::CursorDelegateOpenGL(QObject *parent) + : RenderLayerDelegate(parent) { } -OpenGLCursorView::~OpenGLCursorView() +CursorDelegateOpenGL::~CursorDelegateOpenGL() { } -void OpenGLCursorView::paint(RenderLayer *renderLayer, RenderTarget *renderTarget, const QRegion ®ion) +void CursorDelegateOpenGL::paint(RenderTarget *renderTarget, const QRegion ®ion) { + if (!region.intersects(layer()->mapToGlobal(layer()->rect()))) { + return; + } + auto allocateTexture = [this]() { const QImage img = Cursors::self()->currentCursor()->image(); if (img.isNull()) { @@ -54,7 +58,7 @@ void OpenGLCursorView::paint(RenderLayer *renderLayer, RenderTarget *renderTarge } } - const QRect cursorRect = renderLayer->mapToGlobal(renderLayer->rect()); + const QRect cursorRect = layer()->mapToGlobal(layer()->rect()); QMatrix4x4 mvp; mvp.ortho(QRect(QPoint(0, 0), renderTarget->size() / renderTarget->devicePixelRatio())); diff --git a/src/cursorview_opengl.h b/src/cursordelegate_opengl.h similarity index 54% rename from src/cursorview_opengl.h rename to src/cursordelegate_opengl.h index 48b389bc68..d7ab6cc09f 100644 --- a/src/cursorview_opengl.h +++ b/src/cursordelegate_opengl.h @@ -6,22 +6,22 @@ #pragma once -#include "cursorview.h" +#include "renderlayerdelegate.h" namespace KWin { class GLTexture; -class OpenGLCursorView final : public CursorView +class CursorDelegateOpenGL final : public RenderLayerDelegate { Q_OBJECT public: - explicit OpenGLCursorView(QObject *parent = nullptr); - ~OpenGLCursorView() override; + explicit CursorDelegateOpenGL(QObject *parent = nullptr); + ~CursorDelegateOpenGL() override; - void paint(RenderLayer *renderLayer, RenderTarget *renderTarget, const QRegion ®ion) override; + void paint(RenderTarget *renderTarget, const QRegion ®ion) override; private: QScopedPointer m_cursorTexture; diff --git a/src/cursorview_qpainter.cpp b/src/cursordelegate_qpainter.cpp similarity index 57% rename from src/cursorview_qpainter.cpp rename to src/cursordelegate_qpainter.cpp index 04d130e8b1..7e472488ca 100644 --- a/src/cursorview_qpainter.cpp +++ b/src/cursordelegate_qpainter.cpp @@ -4,7 +4,7 @@ SPDX-License-Identifier: GPL-2.0-or-later */ -#include "cursorview_qpainter.h" +#include "cursordelegate_qpainter.h" #include "cursor.h" #include "renderlayer.h" #include "rendertarget.h" @@ -14,13 +14,17 @@ namespace KWin { -QPainterCursorView::QPainterCursorView(QObject *parent) - : CursorView(parent) +CursorDelegateQPainter::CursorDelegateQPainter(QObject *parent) + : RenderLayerDelegate(parent) { } -void QPainterCursorView::paint(RenderLayer *renderLayer, RenderTarget *renderTarget, const QRegion ®ion) +void CursorDelegateQPainter::paint(RenderTarget *renderTarget, const QRegion ®ion) { + if (!region.intersects(layer()->mapToGlobal(layer()->rect()))) { + return; + } + QImage *buffer = std::get(renderTarget->nativeHandle()); if (Q_UNLIKELY(!buffer)) { return; @@ -29,7 +33,7 @@ void QPainterCursorView::paint(RenderLayer *renderLayer, RenderTarget *renderTar const Cursor *cursor = Cursors::self()->currentCursor(); QPainter painter(buffer); painter.setClipRegion(region); - painter.drawImage(renderLayer->mapToGlobal(renderLayer->rect()), cursor->image()); + painter.drawImage(layer()->mapToGlobal(layer()->rect()), cursor->image()); } } // namespace KWin diff --git a/src/cursordelegate_qpainter.h b/src/cursordelegate_qpainter.h new file mode 100644 index 0000000000..47c25ba8d5 --- /dev/null +++ b/src/cursordelegate_qpainter.h @@ -0,0 +1,24 @@ +/* + SPDX-FileCopyrightText: 2022 Vlad Zahorodnii + + SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#pragma once + +#include "renderlayerdelegate.h" + +namespace KWin +{ + +class CursorDelegateQPainter final : public RenderLayerDelegate +{ + Q_OBJECT + +public: + explicit CursorDelegateQPainter(QObject *parent = nullptr); + + void paint(RenderTarget *renderTarget, const QRegion ®ion) override; +}; + +} // namespace KWin diff --git a/src/cursorview.cpp b/src/cursorview.cpp deleted file mode 100644 index 993fc8ee9a..0000000000 --- a/src/cursorview.cpp +++ /dev/null @@ -1,30 +0,0 @@ -/* - SPDX-FileCopyrightText: 2022 Vlad Zahorodnii - - SPDX-License-Identifier: GPL-2.0-or-later -*/ - -#include "cursorview.h" -#include "renderlayer.h" - -namespace KWin -{ - -CursorView::CursorView(QObject *parent) - : QObject(parent) -{ -} - -CursorDelegate::CursorDelegate(CursorView *view) - : m_view(view) -{ -} - -void CursorDelegate::paint(RenderTarget *renderTarget, const QRegion ®ion) -{ - if (region.intersects(layer()->mapToGlobal(layer()->rect()))) { - m_view->paint(layer(), renderTarget, region); - } -} - -} // namespace KWin diff --git a/src/cursorview.h b/src/cursorview.h deleted file mode 100644 index cca198cce5..0000000000 --- a/src/cursorview.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - SPDX-FileCopyrightText: 2022 Vlad Zahorodnii - - SPDX-License-Identifier: GPL-2.0-or-later -*/ - -#pragma once - -#include "renderlayerdelegate.h" - -namespace KWin -{ - -class KWIN_EXPORT CursorView : public QObject -{ - Q_OBJECT - -public: - explicit CursorView(QObject *parent = nullptr); - - virtual void paint(RenderLayer *layer, RenderTarget *renderTarget, const QRegion ®ion) = 0; -}; - -class KWIN_EXPORT CursorDelegate : public RenderLayerDelegate -{ - Q_OBJECT - -public: - CursorDelegate(CursorView *view); - - void paint(RenderTarget *renderTarget, const QRegion ®ion) override; - -private: - CursorView *m_view; -}; - -} // namespace KWin diff --git a/src/cursorview_qpainter.h b/src/cursorview_qpainter.h deleted file mode 100644 index 048ed97b71..0000000000 --- a/src/cursorview_qpainter.h +++ /dev/null @@ -1,24 +0,0 @@ -/* - SPDX-FileCopyrightText: 2022 Vlad Zahorodnii - - SPDX-License-Identifier: GPL-2.0-or-later -*/ - -#pragma once - -#include "cursorview.h" - -namespace KWin -{ - -class QPainterCursorView final : public CursorView -{ - Q_OBJECT - -public: - explicit QPainterCursorView(QObject *parent = nullptr); - - void paint(RenderLayer *renderLayer, RenderTarget *renderTarget, const QRegion ®ion) override; -}; - -} // namespace KWin