Changeset View
Changeset View
Standalone View
Standalone View
plugins/qpa/backingstore.cpp
1 | /******************************************************************** | 1 | /******************************************************************** | ||
---|---|---|---|---|---|
2 | KWin - the KDE window manager | 2 | KWin - the KDE window manager | ||
3 | This file is part of the KDE project. | 3 | This file is part of the KDE project. | ||
4 | 4 | | |||
5 | Copyright (C) 2015 Martin Gräßlin <mgraesslin@kde.org> | 5 | Copyright (C) 2015 Martin Gräßlin <mgraesslin@kde.org> | ||
6 | Copyright (C) 2019 Vlad Zagorodniy <vladzzag@gmail.com> | ||||
6 | 7 | | |||
7 | This program is free software; you can redistribute it and/or modify | 8 | This program is free software; you can redistribute it and/or modify | ||
8 | it under the terms of the GNU General Public License as published by | 9 | it under the terms of the GNU General Public License as published by | ||
9 | the Free Software Foundation; either version 2 of the License, or | 10 | the Free Software Foundation; either version 2 of the License, or | ||
10 | (at your option) any later version. | 11 | (at your option) any later version. | ||
11 | 12 | | |||
12 | This program is distributed in the hope that it will be useful, | 13 | This program is distributed in the hope that it will be useful, | ||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | GNU General Public License for more details. | 16 | GNU General Public License for more details. | ||
16 | 17 | | |||
17 | You should have received a copy of the GNU General Public License | 18 | You should have received a copy of the GNU General Public License | ||
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
19 | *********************************************************************/ | 20 | *********************************************************************/ | ||
20 | #include "window.h" | | |||
21 | #include "backingstore.h" | 21 | #include "backingstore.h" | ||
22 | #include "../../wayland_server.h" | 22 | #include "window.h" | ||
23 | 23 | | |||
24 | #include <KWayland/Client/buffer.h> | 24 | #include "internal_client.h" | ||
25 | #include <KWayland/Client/connection_thread.h> | | |||
26 | #include <KWayland/Client/shm_pool.h> | | |||
27 | #include <KWayland/Client/surface.h> | | |||
28 | 25 | | |||
29 | namespace KWin | 26 | namespace KWin | ||
30 | { | 27 | { | ||
31 | namespace QPA | 28 | namespace QPA | ||
32 | { | 29 | { | ||
33 | 30 | | |||
34 | BackingStore::BackingStore(QWindow *w, KWayland::Client::ShmPool *shm) | 31 | BackingStore::BackingStore(QWindow *window) | ||
35 | : QPlatformBackingStore(w) | 32 | : QPlatformBackingStore(window) | ||
36 | , m_shm(shm) | 33 | { | ||
37 | , m_backBuffer(QSize(), QImage::Format_ARGB32_Premultiplied) | | |||
38 | { | | |||
39 | QObject::connect(m_shm, &KWayland::Client::ShmPool::poolResized, | | |||
40 | [this] { | | |||
41 | if (!m_buffer) { | | |||
42 | return; | | |||
43 | } | | |||
44 | auto b = m_buffer.toStrongRef(); | | |||
45 | if (!b->isUsed()){ | | |||
46 | return; | | |||
47 | } | | |||
48 | const QSize size = m_backBuffer.size(); | | |||
49 | m_backBuffer = QImage(b->address(), size.width(), size.height(), QImage::Format_ARGB32_Premultiplied); | | |||
50 | m_backBuffer.setDevicePixelRatio(scale()); | | |||
51 | } | | |||
52 | ); | | |||
53 | } | 34 | } | ||
54 | 35 | | |||
55 | BackingStore::~BackingStore() = default; | 36 | BackingStore::~BackingStore() = default; | ||
56 | 37 | | |||
57 | QPaintDevice *BackingStore::paintDevice() | 38 | QPaintDevice *BackingStore::paintDevice() | ||
58 | { | 39 | { | ||
59 | return &m_backBuffer; | 40 | return &m_backBuffer; | ||
60 | } | 41 | } | ||
61 | 42 | | |||
62 | void BackingStore::resize(const QSize &size, const QRegion &staticContents) | 43 | void BackingStore::resize(const QSize &size, const QRegion &staticContents) | ||
63 | { | 44 | { | ||
64 | Q_UNUSED(staticContents) | 45 | Q_UNUSED(staticContents) | ||
65 | m_size = size * scale(); | 46 | | ||
66 | if (!m_buffer) { | 47 | if (m_backBuffer.size() == size) { | ||
67 | return; | 48 | return; | ||
68 | } | 49 | } | ||
69 | m_buffer.toStrongRef()->setUsed(false); | 50 | | ||
70 | m_buffer.clear(); | 51 | const QPlatformWindow *platformWindow = static_cast<QPlatformWindow *>(window()->handle()); | ||
52 | const qreal devicePixelRatio = platformWindow->devicePixelRatio(); | ||||
53 | | ||||
54 | m_backBuffer = QImage(size * devicePixelRatio, QImage::Format_ARGB32_Premultiplied); | ||||
55 | m_backBuffer.setDevicePixelRatio(devicePixelRatio); | ||||
56 | | ||||
57 | m_frontBuffer = QImage(size * devicePixelRatio, QImage::Format_ARGB32_Premultiplied); | ||||
58 | m_frontBuffer.setDevicePixelRatio(devicePixelRatio); | ||||
71 | } | 59 | } | ||
72 | 60 | | |||
73 | void BackingStore::flush(QWindow *window, const QRegion ®ion, const QPoint &offset) | 61 | static void blitImage(const QImage &source, QImage &target, const QRect &rect) | ||
74 | { | 62 | { | ||
75 | Q_UNUSED(region) | 63 | Q_ASSERT(source.format() == QImage::Format_ARGB32_Premultiplied); | ||
76 | Q_UNUSED(offset) | 64 | Q_ASSERT(target.format() == QImage::Format_ARGB32_Premultiplied); | ||
77 | 65 | | |||
78 | auto w = static_cast<Window *>(window->handle()); | 66 | const int devicePixelRatio = target.devicePixelRatio(); | ||
79 | auto s = w->surface(); | 67 | | ||
80 | if (!s) { | 68 | const int x = rect.x() * devicePixelRatio; | ||
81 | return; | 69 | const int y = rect.y() * devicePixelRatio; | ||
70 | const int width = rect.width() * devicePixelRatio; | ||||
71 | const int height = rect.height() * devicePixelRatio; | ||||
72 | | ||||
73 | for (int i = y; i < y + height; ++i) { | ||||
74 | const uint32_t *in = reinterpret_cast<const uint32_t *>(source.scanLine(i)); | ||||
75 | uint32_t *out = reinterpret_cast<uint32_t *>(target.scanLine(i)); | ||||
76 | std::copy(in + x, in + x + width, out + x); | ||||
82 | } | 77 | } | ||
83 | s->attachBuffer(m_buffer); | | |||
84 | // TODO: proper damage region | | |||
85 | s->damage(QRect(QPoint(0, 0), m_backBuffer.size() / scale())); | | |||
86 | s->commit(KWayland::Client::Surface::CommitFlag::None); | | |||
87 | waylandServer()->internalClientConection()->flush(); | | |||
88 | waylandServer()->dispatch(); | | |||
89 | } | 78 | } | ||
90 | 79 | | |||
91 | void BackingStore::beginPaint(const QRegion&) | 80 | static void blitImage(const QImage &source, QImage &target, const QRegion ®ion) | ||
92 | { | 81 | { | ||
93 | if (m_buffer) { | 82 | for (const QRect &rect : region) { | ||
94 | auto b = m_buffer.toStrongRef(); | 83 | blitImage(source, target, rect); | ||
95 | if (b->isReleased()) { | | |||
96 | // we can re-use this buffer | | |||
97 | b->setReleased(false); | | |||
98 | return; | | |||
99 | } else { | | |||
100 | // buffer is still in use, get a new one | | |||
101 | b->setUsed(false); | | |||
102 | } | 84 | } | ||
103 | } | 85 | } | ||
104 | auto oldBuffer = m_buffer.toStrongRef(); | 86 | | ||
105 | m_buffer.clear(); | 87 | void BackingStore::flush(QWindow *window, const QRegion ®ion, const QPoint &offset) | ||
106 | m_buffer = m_shm->getBuffer(m_size, m_size.width() * 4); | 88 | { | ||
107 | if (!m_buffer) { | 89 | Q_UNUSED(offset) | ||
108 | m_backBuffer = QImage(); | 90 | | ||
91 | Window *platformWindow = static_cast<Window *>(window->handle()); | ||||
92 | InternalClient *client = platformWindow->client(); | ||||
93 | if (!client) { | ||||
109 | return; | 94 | return; | ||
110 | } | 95 | } | ||
111 | auto b = m_buffer.toStrongRef(); | | |||
112 | b->setUsed(true); | | |||
113 | m_backBuffer = QImage(b->address(), m_size.width(), m_size.height(), QImage::Format_ARGB32_Premultiplied); | | |||
114 | m_backBuffer.setDevicePixelRatio(scale()); | | |||
115 | if (oldBuffer) { | | |||
116 | b->copy(oldBuffer->address()); | | |||
117 | } else { | | |||
118 | m_backBuffer.fill(Qt::transparent); | | |||
119 | } | | |||
120 | } | | |||
121 | 96 | | |||
122 | int BackingStore::scale() const | 97 | blitImage(m_backBuffer, m_frontBuffer, region); | ||
123 | { | 98 | | ||
124 | return static_cast<Window *>(window()->handle())->scale(); | 99 | client->present(m_frontBuffer, region); | ||
125 | } | 100 | } | ||
126 | 101 | | |||
127 | } | 102 | } | ||
128 | } | 103 | } |