| 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 = window()->format().hasAlpha() |
| 152 | ? QImage::Format_ARGB32_Premultiplied |
| 153 | : QGuiApplication::primaryScreen()->handle()->format(); |
| 154 | if (m_image.size() != size) |
| 155 | m_image = QImage(size, format); |
| 156 | clearHash(); |
| 157 | } |
| 158 | |
| 159 | extern void qt_scrollRectInImage(QImage &img, const QRect &rect, const QPoint &offset); |
| 160 | |
| 161 | bool QOffscreenBackingStore::scroll(const QRegion &area, int dx, int dy) |
| 162 | { |
| 163 | if (m_image.isNull()) |
| 164 | return false; |
| 165 | |
| 166 | const QRect rect = area.boundingRect(); |
| 167 | qt_scrollRectInImage(img&: m_image, rect, offset: QPoint(dx, dy)); |
| 168 | |
| 169 | return true; |
| 170 | } |
| 171 | |
| 172 | void QOffscreenBackingStore::beginPaint(const QRegion ®ion) |
| 173 | { |
| 174 | if (QImage::toPixelFormat(format: m_image.format()).alphaUsage() == QPixelFormat::UsesAlpha) { |
| 175 | QPainter p(&m_image); |
| 176 | p.setCompositionMode(QPainter::CompositionMode_Source); |
| 177 | const QColor blank = Qt::transparent; |
| 178 | for (const QRect &r : region) |
| 179 | p.fillRect(r, color: blank); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | QPixmap QOffscreenBackingStore::grabWindow(WId window, const QRect &rect) const |
| 184 | { |
| 185 | QRect area = m_windowAreaHash.value(key: window, defaultValue: QRect()); |
| 186 | if (area.isNull()) |
| 187 | return QPixmap(); |
| 188 | |
| 189 | QRect adjusted = rect; |
| 190 | if (adjusted.width() <= 0) |
| 191 | adjusted.setWidth(area.width()); |
| 192 | if (adjusted.height() <= 0) |
| 193 | adjusted.setHeight(area.height()); |
| 194 | |
| 195 | adjusted = adjusted.translated(p: area.topLeft()) & area; |
| 196 | |
| 197 | if (adjusted.isEmpty()) |
| 198 | return QPixmap(); |
| 199 | |
| 200 | return QPixmap::fromImage(image: m_image.copy(rect: adjusted)); |
| 201 | } |
| 202 | |
| 203 | QOffscreenBackingStore *QOffscreenBackingStore::backingStoreForWinId(WId id) |
| 204 | { |
| 205 | return m_backingStoreForWinIdHash.value(key: id, defaultValue: nullptr); |
| 206 | } |
| 207 | |
| 208 | void QOffscreenBackingStore::clearHash() |
| 209 | { |
| 210 | for (auto it = m_windowAreaHash.cbegin(), end = m_windowAreaHash.cend(); it != end; ++it) { |
| 211 | const auto it2 = std::as_const(t&: m_backingStoreForWinIdHash).find(key: it.key()); |
| 212 | if (it2.value() == this) |
| 213 | m_backingStoreForWinIdHash.erase(it: it2); |
| 214 | } |
| 215 | m_windowAreaHash.clear(); |
| 216 | } |
| 217 | |
| 218 | QHash<WId, QOffscreenBackingStore *> QOffscreenBackingStore::m_backingStoreForWinIdHash; |
| 219 | |
| 220 | QOffscreenPlatformNativeInterface::QOffscreenPlatformNativeInterface(QOffscreenIntegration *integration) |
| 221 | : m_integration(integration) |
| 222 | { |
| 223 | |
| 224 | } |
| 225 | |
| 226 | QOffscreenPlatformNativeInterface::~QOffscreenPlatformNativeInterface() = default; |
| 227 | |
| 228 | /* |
| 229 | Set platform configuration, e.g. screen configuration |
| 230 | */ |
| 231 | void QOffscreenPlatformNativeInterface::setConfiguration(const QJsonObject &configuration, QOffscreenPlatformNativeInterface *iface) |
| 232 | { |
| 233 | iface->m_integration->setConfiguration(configuration); |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | Get the current platform configuration |
| 238 | */ |
| 239 | QJsonObject QOffscreenPlatformNativeInterface::configuration(QOffscreenPlatformNativeInterface *iface) |
| 240 | { |
| 241 | return iface->m_integration->configuration(); |
| 242 | } |
| 243 | |
| 244 | void *QOffscreenPlatformNativeInterface::nativeResourceForIntegration(const QByteArray &resource) |
| 245 | { |
| 246 | if (resource == "setConfiguration" ) |
| 247 | return reinterpret_cast<void*>(&QOffscreenPlatformNativeInterface::setConfiguration); |
| 248 | else if (resource == "configuration" ) |
| 249 | return reinterpret_cast<void*>(&QOffscreenPlatformNativeInterface::configuration); |
| 250 | else |
| 251 | return nullptr; |
| 252 | } |
| 253 | |
| 254 | QT_END_NAMESPACE |
| 255 | |