1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "qoffscreencommon.h" |
5 | #include "qoffscreenintegration.h" |
6 | #include "qoffscreenwindow.h" |
7 | |
8 | |
9 | #include <QtGui/QPainter> |
10 | #include <QtGui/private/qpixmap_raster_p.h> |
11 | #include <QtGui/private/qguiapplication_p.h> |
12 | |
13 | #include <qpa/qplatformcursor.h> |
14 | #include <qpa/qplatformwindow.h> |
15 | |
16 | QT_BEGIN_NAMESPACE |
17 | |
18 | QPlatformWindow *QOffscreenScreen::windowContainingCursor = nullptr; |
19 | |
20 | |
21 | QList<QPlatformScreen *> QOffscreenScreen::virtualSiblings() const |
22 | { |
23 | QList<QPlatformScreen *> platformScreens; |
24 | for (auto screen : m_integration->screens()) { |
25 | platformScreens.append(t: screen); |
26 | } |
27 | return platformScreens; |
28 | } |
29 | |
30 | class QOffscreenCursor : public QPlatformCursor |
31 | { |
32 | public: |
33 | QOffscreenCursor() : m_pos(10, 10) {} |
34 | |
35 | QPoint pos() const override { return m_pos; } |
36 | void setPos(const QPoint &pos) override |
37 | { |
38 | m_pos = pos; |
39 | const QWindowList wl = QGuiApplication::topLevelWindows(); |
40 | QWindow *containing = nullptr; |
41 | for (QWindow *w : wl) { |
42 | if (w->type() != Qt::Desktop && w->isExposed() && w->geometry().contains(p: pos)) { |
43 | containing = w; |
44 | break; |
45 | } |
46 | } |
47 | |
48 | QPoint local = pos; |
49 | if (containing) |
50 | local -= containing->position(); |
51 | |
52 | QWindow *previous = QOffscreenScreen::windowContainingCursor ? QOffscreenScreen::windowContainingCursor->window() : nullptr; |
53 | |
54 | if (containing != previous) |
55 | QWindowSystemInterface::handleEnterLeaveEvent(enter: containing, leave: previous, local, global: pos); |
56 | |
57 | QWindowSystemInterface::handleMouseEvent(window: containing, local, global: pos, state: QGuiApplication::mouseButtons(), button: Qt::NoButton, |
58 | type: QEvent::MouseMove, mods: QGuiApplication::keyboardModifiers(), source: Qt::MouseEventSynthesizedByQt); |
59 | |
60 | QOffscreenScreen::windowContainingCursor = containing ? containing->handle() : nullptr; |
61 | } |
62 | #ifndef QT_NO_CURSOR |
63 | void changeCursor(QCursor *windowCursor, QWindow *window) override |
64 | { |
65 | Q_UNUSED(windowCursor); |
66 | Q_UNUSED(window); |
67 | } |
68 | #endif |
69 | private: |
70 | QPoint m_pos; |
71 | }; |
72 | |
73 | QOffscreenScreen::QOffscreenScreen(const QOffscreenIntegration *integration) |
74 | : m_geometry(0, 0, 800, 600) |
75 | , m_cursor(new QOffscreenCursor) |
76 | , m_integration(integration) |
77 | { |
78 | } |
79 | |
80 | QPixmap QOffscreenScreen::grabWindow(WId id, int x, int y, int width, int height) const |
81 | { |
82 | QRect rect(x, y, width, height); |
83 | |
84 | // id == 0 -> grab the screen, so all windows intersecting rect |
85 | if (!id) { |
86 | if (width == -1) |
87 | rect.setWidth(m_geometry.width()); |
88 | if (height == -1) |
89 | rect.setHeight(m_geometry.height()); |
90 | QPixmap screenImage(rect.size()); |
91 | QPainter painter(&screenImage); |
92 | painter.translate(dx: -x, dy: -y); |
93 | const QWindowList wl = QGuiApplication::topLevelWindows(); |
94 | for (QWindow *w : wl) { |
95 | if (w->isExposed() && w->geometry().intersects(r: rect)) { |
96 | QOffscreenBackingStore *store = QOffscreenBackingStore::backingStoreForWinId(id: w->winId()); |
97 | const QImage windowImage = store ? store->toImage() : QImage(); |
98 | if (!windowImage.isNull()) |
99 | painter.drawImage(p: w->position(), image: windowImage); |
100 | } |
101 | } |
102 | return screenImage; |
103 | } |
104 | |
105 | QOffscreenBackingStore *store = QOffscreenBackingStore::backingStoreForWinId(id); |
106 | if (store) |
107 | return store->grabWindow(window: id, rect); |
108 | return QPixmap(); |
109 | } |
110 | |
111 | QOffscreenBackingStore::QOffscreenBackingStore(QWindow *window) |
112 | : QPlatformBackingStore(window) |
113 | { |
114 | } |
115 | |
116 | QOffscreenBackingStore::~QOffscreenBackingStore() |
117 | { |
118 | clearHash(); |
119 | } |
120 | |
121 | QPaintDevice *QOffscreenBackingStore::paintDevice() |
122 | { |
123 | return &m_image; |
124 | } |
125 | |
126 | void QOffscreenBackingStore::flush(QWindow *window, const QRegion ®ion, const QPoint &offset) |
127 | { |
128 | Q_UNUSED(region); |
129 | |
130 | if (m_image.size().isEmpty()) |
131 | return; |
132 | |
133 | QSize imageSize = m_image.size(); |
134 | |
135 | QRegion clipped = QRect(0, 0, window->width(), window->height()); |
136 | clipped &= QRect(0, 0, imageSize.width(), imageSize.height()).translated(p: -offset); |
137 | |
138 | QRect bounds = clipped.boundingRect().translated(p: offset); |
139 | |
140 | if (bounds.isNull()) |
141 | return; |
142 | |
143 | WId id = window->winId(); |
144 | |
145 | m_windowAreaHash[id] = bounds; |
146 | m_backingStoreForWinIdHash[id] = this; |
147 | } |
148 | |
149 | void QOffscreenBackingStore::resize(const QSize &size, const QRegion &) |
150 | { |
151 | QImage::Format format = QGuiApplication::primaryScreen()->handle()->format(); |
152 | if (m_image.size() != size) |
153 | m_image = QImage(size, format); |
154 | clearHash(); |
155 | } |
156 | |
157 | extern void qt_scrollRectInImage(QImage &img, const QRect &rect, const QPoint &offset); |
158 | |
159 | bool QOffscreenBackingStore::scroll(const QRegion &area, int dx, int dy) |
160 | { |
161 | if (m_image.isNull()) |
162 | return false; |
163 | |
164 | const QRect rect = area.boundingRect(); |
165 | qt_scrollRectInImage(img&: m_image, rect, offset: QPoint(dx, dy)); |
166 | |
167 | return true; |
168 | } |
169 | |
170 | QPixmap QOffscreenBackingStore::grabWindow(WId window, const QRect &rect) const |
171 | { |
172 | QRect area = m_windowAreaHash.value(key: window, defaultValue: QRect()); |
173 | if (area.isNull()) |
174 | return QPixmap(); |
175 | |
176 | QRect adjusted = rect; |
177 | if (adjusted.width() <= 0) |
178 | adjusted.setWidth(area.width()); |
179 | if (adjusted.height() <= 0) |
180 | adjusted.setHeight(area.height()); |
181 | |
182 | adjusted = adjusted.translated(p: area.topLeft()) & area; |
183 | |
184 | if (adjusted.isEmpty()) |
185 | return QPixmap(); |
186 | |
187 | return QPixmap::fromImage(image: m_image.copy(rect: adjusted)); |
188 | } |
189 | |
190 | QOffscreenBackingStore *QOffscreenBackingStore::backingStoreForWinId(WId id) |
191 | { |
192 | return m_backingStoreForWinIdHash.value(key: id, defaultValue: nullptr); |
193 | } |
194 | |
195 | void QOffscreenBackingStore::clearHash() |
196 | { |
197 | for (auto it = m_windowAreaHash.cbegin(), end = m_windowAreaHash.cend(); it != end; ++it) { |
198 | const auto it2 = std::as_const(t&: m_backingStoreForWinIdHash).find(key: it.key()); |
199 | if (it2.value() == this) |
200 | m_backingStoreForWinIdHash.erase(it: it2); |
201 | } |
202 | m_windowAreaHash.clear(); |
203 | } |
204 | |
205 | QHash<WId, QOffscreenBackingStore *> QOffscreenBackingStore::m_backingStoreForWinIdHash; |
206 | |
207 | QOffscreenPlatformNativeInterface::QOffscreenPlatformNativeInterface(QOffscreenIntegration *integration) |
208 | : m_integration(integration) |
209 | { |
210 | |
211 | } |
212 | |
213 | QOffscreenPlatformNativeInterface::~QOffscreenPlatformNativeInterface() = default; |
214 | |
215 | /* |
216 | Set platform configuration, e.g. screen configuration |
217 | */ |
218 | void QOffscreenPlatformNativeInterface::setConfiguration(const QJsonObject &configuration, QOffscreenPlatformNativeInterface *iface) |
219 | { |
220 | iface->m_integration->setConfiguration(configuration); |
221 | } |
222 | |
223 | /* |
224 | Get the current platform configuration |
225 | */ |
226 | QJsonObject QOffscreenPlatformNativeInterface::configuration(QOffscreenPlatformNativeInterface *iface) |
227 | { |
228 | return iface->m_integration->configuration(); |
229 | } |
230 | |
231 | void *QOffscreenPlatformNativeInterface::nativeResourceForIntegration(const QByteArray &resource) |
232 | { |
233 | if (resource == "setConfiguration" ) |
234 | return reinterpret_cast<void*>(&QOffscreenPlatformNativeInterface::setConfiguration); |
235 | else if (resource == "configuration" ) |
236 | return reinterpret_cast<void*>(&QOffscreenPlatformNativeInterface::configuration); |
237 | else |
238 | return nullptr; |
239 | } |
240 | |
241 | QT_END_NAMESPACE |
242 | |