Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qoffscreencommon.cpp
Go to the documentation of this file.
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// Qt-Security score:significant reason:default
4
8
9
10#include <QtGui/QPainter>
11#include <QtGui/private/qpixmap_raster_p.h>
12#include <QtGui/private/qguiapplication_p.h>
13
14#include <qpa/qplatformcursor.h>
15#include <qpa/qplatformwindow.h>
16
18
19QPlatformWindow *QOffscreenScreen::windowContainingCursor = nullptr;
20
21
22QList<QPlatformScreen *> QOffscreenScreen::virtualSiblings() const
23{
24 QList<QPlatformScreen *> platformScreens;
25 for (auto screen : m_integration->screens()) {
26 platformScreens.append(screen);
27 }
28 return platformScreens;
29}
30
32{
33public:
34 QOffscreenCursor() : m_pos(10, 10) {}
35
36 QPoint pos() const override { return m_pos; }
37 void setPos(const QPoint &pos) override
38 {
39 m_pos = pos;
40 const QWindowList wl = QGuiApplication::topLevelWindows();
41 QWindow *containing = nullptr;
42 for (QWindow *w : wl) {
43 if (w->isExposed() && w->geometry().contains(pos)) {
44 containing = w;
45 break;
46 }
47 }
48
49 QPoint local = pos;
50 if (containing)
51 local -= containing->position();
52
53 QWindow *previous = QOffscreenScreen::windowContainingCursor ? QOffscreenScreen::windowContainingCursor->window() : nullptr;
54
55 if (containing != previous)
56 QWindowSystemInterface::handleEnterLeaveEvent(containing, previous, local, pos);
57
58 QWindowSystemInterface::handleMouseEvent(containing, local, pos, QGuiApplication::mouseButtons(), Qt::NoButton,
59 QEvent::MouseMove, QGuiApplication::keyboardModifiers(), Qt::MouseEventSynthesizedByQt);
60
61 QOffscreenScreen::windowContainingCursor = containing ? containing->handle() : nullptr;
62 }
63#ifndef QT_NO_CURSOR
64 void changeCursor(QCursor *windowCursor, QWindow *window) override
65 {
66 Q_UNUSED(windowCursor);
67 Q_UNUSED(window);
68 }
69#endif
70private:
71 QPoint m_pos;
72};
73
75 : m_geometry(0, 0, 800, 600)
77 , m_integration(integration)
78{
79}
80
81QPixmap QOffscreenScreen::grabWindow(WId id, int x, int y, int width, int height) const
82{
83 QRect rect(x, y, width, height);
84
85 // id == 0 -> grab the screen, so all windows intersecting rect
86 if (!id) {
87 if (width == -1)
88 rect.setWidth(m_geometry.width());
89 if (height == -1)
90 rect.setHeight(m_geometry.height());
91 QPixmap screenImage(rect.size());
92 QPainter painter(&screenImage);
93 painter.translate(-x, -y);
94 const QWindowList wl = QGuiApplication::topLevelWindows();
95 for (QWindow *w : wl) {
96 if (w->isExposed() && w->geometry().intersects(rect)) {
97 QOffscreenBackingStore *store = QOffscreenBackingStore::backingStoreForWinId(w->winId());
98 const QImage windowImage = store ? store->toImage() : QImage();
99 if (!windowImage.isNull())
100 painter.drawImage(w->position(), windowImage);
101 }
102 }
103 return screenImage;
104 }
105
106 QOffscreenBackingStore *store = QOffscreenBackingStore::backingStoreForWinId(id);
107 if (store)
108 return store->grabWindow(id, rect);
109 return QPixmap();
110}
111
116
118{
119 clearHash();
120}
121
123{
124 return &m_image;
125}
126
127void QOffscreenBackingStore::flush(QWindow *window, const QRegion &region, const QPoint &offset)
128{
129 Q_UNUSED(region);
130
131 if (m_image.size().isEmpty())
132 return;
133
134 QSize imageSize = m_image.size();
135
136 QRegion clipped = QRect(0, 0, window->width(), window->height());
137 clipped &= QRect(0, 0, imageSize.width(), imageSize.height()).translated(-offset);
138
139 QRect bounds = clipped.boundingRect().translated(offset);
140
141 if (bounds.isNull())
142 return;
143
144 WId id = window->winId();
145
146 m_windowAreaHash[id] = bounds;
147 m_backingStoreForWinIdHash[id] = this;
148}
149
150void QOffscreenBackingStore::resize(const QSize &size, const QRegion &)
151{
152 QImage::Format format = window()->format().hasAlpha()
153 ? QImage::Format_ARGB32_Premultiplied
154 : QGuiApplication::primaryScreen()->handle()->format();
155 if (m_image.size() != size)
156 m_image = QImage(size, format);
157 clearHash();
158}
159
160extern void qt_scrollRectInImage(QImage &img, const QRect &rect, const QPoint &offset);
161
162bool QOffscreenBackingStore::scroll(const QRegion &area, int dx, int dy)
163{
164 if (m_image.isNull())
165 return false;
166
167 const QRect rect = area.boundingRect();
168 qt_scrollRectInImage(m_image, rect, QPoint(dx, dy));
169
170 return true;
171}
172
173void QOffscreenBackingStore::beginPaint(const QRegion &region)
174{
175 if (QImage::toPixelFormat(m_image.format()).alphaUsage() == QPixelFormat::UsesAlpha) {
176 QPainter p(&m_image);
177 p.setCompositionMode(QPainter::CompositionMode_Source);
178 const QColor blank = Qt::transparent;
179 for (const QRect &r : region)
180 p.fillRect(r, blank);
181 }
182}
183
184QPixmap QOffscreenBackingStore::grabWindow(WId window, const QRect &rect) const
185{
186 QRect area = m_windowAreaHash.value(window, QRect());
187 if (area.isNull())
188 return QPixmap();
189
190 QRect adjusted = rect;
191 if (adjusted.width() <= 0)
192 adjusted.setWidth(area.width());
193 if (adjusted.height() <= 0)
194 adjusted.setHeight(area.height());
195
196 adjusted = adjusted.translated(area.topLeft()) & area;
197
198 if (adjusted.isEmpty())
199 return QPixmap();
200
201 return QPixmap::fromImage(m_image.copy(adjusted));
202}
203
204QOffscreenBackingStore *QOffscreenBackingStore::backingStoreForWinId(WId id)
205{
206 return m_backingStoreForWinIdHash.value(id, nullptr);
207}
208
209void QOffscreenBackingStore::clearHash()
210{
211 for (auto it = m_windowAreaHash.cbegin(), end = m_windowAreaHash.cend(); it != end; ++it) {
212 const auto it2 = std::as_const(m_backingStoreForWinIdHash).find(it.key());
213 if (it2.value() == this)
214 m_backingStoreForWinIdHash.erase(it2);
215 }
216 m_windowAreaHash.clear();
217}
218
219QHash<WId, QOffscreenBackingStore *> QOffscreenBackingStore::m_backingStoreForWinIdHash;
220
222 : m_integration(integration)
223{
224
225}
226
228
229/*
230 Set platform configuration, e.g. screen configuration
231*/
232void QOffscreenPlatformNativeInterface::setConfiguration(const QJsonObject &configuration, QOffscreenPlatformNativeInterface *iface)
233{
234 iface->m_integration->setConfiguration(configuration);
235}
236
237/*
238 Get the current platform configuration
239*/
241{
242 return iface->m_integration->configuration();
243}
244
246{
247 if (resource == "setConfiguration")
248 return reinterpret_cast<void*>(&QOffscreenPlatformNativeInterface::setConfiguration);
249 else if (resource == "configuration")
250 return reinterpret_cast<void*>(&QOffscreenPlatformNativeInterface::configuration);
251 else
252 return nullptr;
253}
254
255QT_END_NAMESPACE
bool scroll(const QRegion &area, int dx, int dy) override
Scrolls the given area dx pixels to the right and dy downward; both dx and dy may be negative.
void beginPaint(const QRegion &) override
This function is called before painting onto the surface begins, with the region in which the paintin...
void flush(QWindow *window, const QRegion &region, const QPoint &offset) override
Flushes the given region from the specified window.
void resize(const QSize &size, const QRegion &staticContents) override
QOffscreenBackingStore(QWindow *window)
QPaintDevice * paintDevice() override
Implement this function to return the appropriate paint device.
QPixmap grabWindow(WId window, const QRect &rect) const
void setPos(const QPoint &pos) override
QPoint pos() const override
void changeCursor(QCursor *windowCursor, QWindow *window) override
This method is called by Qt whenever the cursor graphic should be changed.
void * nativeResourceForIntegration(const QByteArray &resource) override
QOffscreenPlatformNativeInterface(QOffscreenIntegration *integration)
QOffscreenScreen(const QOffscreenIntegration *integration)
QPixmap grabWindow(WId window, int x, int y, int width, int height) const override
This function is called when Qt needs to be able to grab the content of a window.
const QOffscreenIntegration * m_integration
Combined button and popup list for selecting options.
void qt_scrollRectInImage(QImage &img, const QRect &rect, const QPoint &offset)