File indexing completed on 2024-12-01 11:09:51

0001 /*
0002     SPDX-FileCopyrightText: 2022 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "cursordelegate_qpainter.h"
0008 #include "composite.h"
0009 #include "core/renderlayer.h"
0010 #include "core/rendertarget.h"
0011 #include "cursor.h"
0012 #include "scene/cursorscene.h"
0013 
0014 #include <QPainter>
0015 
0016 namespace KWin
0017 {
0018 
0019 void CursorDelegateQPainter::paint(RenderTarget *renderTarget, const QRegion &region)
0020 {
0021     if (!region.intersects(layer()->mapToGlobal(layer()->rect()))) {
0022         return;
0023     }
0024 
0025     QImage *buffer = std::get<QImage *>(renderTarget->nativeHandle());
0026     if (Q_UNLIKELY(!buffer)) {
0027         return;
0028     }
0029 
0030     const QSize bufferSize = Cursors::self()->currentCursor()->rect().size() * renderTarget->devicePixelRatio();
0031     if (m_buffer.size() != bufferSize) {
0032         m_buffer = QImage(bufferSize, QImage::Format_ARGB32_Premultiplied);
0033     }
0034 
0035     RenderTarget offscreenRenderTarget(&m_buffer);
0036     offscreenRenderTarget.setDevicePixelRatio(renderTarget->devicePixelRatio());
0037 
0038     RenderLayer renderLayer(layer()->loop());
0039     renderLayer.setDelegate(std::make_unique<SceneDelegate>(Compositor::self()->cursorScene()));
0040     renderLayer.delegate()->prePaint();
0041     renderLayer.delegate()->paint(&offscreenRenderTarget, infiniteRegion());
0042     renderLayer.delegate()->postPaint();
0043 
0044     QPainter painter(buffer);
0045     painter.setClipRegion(region);
0046     painter.drawImage(layer()->mapToGlobal(layer()->rect()), m_buffer);
0047 }
0048 
0049 } // namespace KWin