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 const auto screens = m_integration->screens();
26 for (auto screen : screens) {
27 platformScreens.append(screen);
28 }
29 return platformScreens;
30}
31
33{
34public:
35 QOffscreenCursor() : m_pos(10, 10) {}
36
37 QPoint pos() const override { return m_pos; }
38 void setPos(const QPoint &pos) override
39 {
40 m_pos = pos;
41 const QWindowList wl = QGuiApplication::topLevelWindows();
42 QWindow *containing = nullptr;
43 for (QWindow *w : wl) {
44 if (w->isExposed() && w->geometry().contains(pos)) {
45 containing = w;
46 break;
47 }
48 }
49
50 QPoint local = pos;
51 if (containing)
52 local -= containing->position();
53
54 QWindow *previous = QOffscreenScreen::windowContainingCursor ? QOffscreenScreen::windowContainingCursor->window() : nullptr;
55
56 if (containing != previous)
57 QWindowSystemInterface::handleEnterLeaveEvent(containing, previous, local, pos);
58
59 QWindowSystemInterface::handleMouseEvent(containing, local, pos, QGuiApplication::mouseButtons(), Qt::NoButton,
60 QEvent::MouseMove, QGuiApplication::keyboardModifiers(), Qt::MouseEventSynthesizedByQt);
61
62 QOffscreenScreen::windowContainingCursor = containing ? containing->handle() : nullptr;
63 }
64#ifndef QT_NO_CURSOR
65 void changeCursor(QCursor *windowCursor, QWindow *window) override
66 {
67 Q_UNUSED(windowCursor);
68 Q_UNUSED(window);
69 }
70#endif
71private:
72 QPoint m_pos;
73};
74
76 : m_geometry(0, 0, 800, 600)
78 , m_integration(integration)
79{
80}
81
82QPixmap QOffscreenScreen::grabWindow(WId id, int x, int y, int width, int height) const
83{
84 QRect rect(x, y, width, height);
85
86 // id == 0 -> grab the screen, so all windows intersecting rect
87 if (!id) {
88 if (width == -1)
89 rect.setWidth(m_geometry.width());
90 if (height == -1)
91 rect.setHeight(m_geometry.height());
92 QPixmap screenImage(rect.size());
93 QPainter painter(&screenImage);
94 painter.translate(-x, -y);
95 const QWindowList wl = QGuiApplication::topLevelWindows();
96 for (QWindow *w : wl) {
97 if (w->isExposed() && w->geometry().intersects(rect)) {
98 QOffscreenBackingStore *store = QOffscreenBackingStore::backingStoreForWinId(w->winId());
99 const QImage windowImage = store ? store->toImage() : QImage();
100 if (!windowImage.isNull())
101 painter.drawImage(w->position(), windowImage);
102 }
103 }
104 return screenImage;
105 }
106
107 QOffscreenBackingStore *store = QOffscreenBackingStore::backingStoreForWinId(id);
108 if (store)
109 return store->grabWindow(id, rect);
110 return QPixmap();
111}
112
117
119{
120 clearHash();
121}
122
124{
125 return &m_image;
126}
127
128void QOffscreenBackingStore::flush(QWindow *window, const QRegion &region, const QPoint &offset)
129{
130 Q_UNUSED(region);
131
132 if (m_image.size().isEmpty())
133 return;
134
135 QSize imageSize = m_image.size();
136
137 QRegion clipped = QRect(0, 0, window->width(), window->height());
138 clipped &= QRect(0, 0, imageSize.width(), imageSize.height()).translated(-offset);
139
140 QRect bounds = clipped.boundingRect().translated(offset);
141
142 if (bounds.isNull())
143 return;
144
145 WId id = window->winId();
146
147 m_windowAreaHash[id] = bounds;
148 m_backingStoreForWinIdHash[id] = this;
149}
150
151void QOffscreenBackingStore::resize(const QSize &size, const QRegion &)
152{
153 QImage::Format format = window()->format().hasAlpha()
154 ? QImage::Format_ARGB32_Premultiplied
155 : QGuiApplication::primaryScreen()->handle()->format();
156 if (m_image.size() != size)
157 m_image = QImage(size, format);
158 clearHash();
159}
160
161extern void qt_scrollRectInImage(QImage &img, const QRect &rect, const QPoint &offset);
162
163bool QOffscreenBackingStore::scroll(const QRegion &area, int dx, int dy)
164{
165 if (m_image.isNull())
166 return false;
167
168 const QRect rect = area.boundingRect();
169 qt_scrollRectInImage(m_image, rect, QPoint(dx, dy));
170
171 return true;
172}
173
174void QOffscreenBackingStore::beginPaint(const QRegion &region)
175{
176 if (QImage::toPixelFormat(m_image.format()).alphaUsage() == QPixelFormat::UsesAlpha) {
177 QPainter p(&m_image);
178 p.setCompositionMode(QPainter::CompositionMode_Source);
179 const QColor blank = Qt::transparent;
180 for (const QRect &r : region)
181 p.fillRect(r, blank);
182 }
183}
184
185QPixmap QOffscreenBackingStore::grabWindow(WId window, const QRect &rect) const
186{
187 QRect area = m_windowAreaHash.value(window, QRect());
188 if (area.isNull())
189 return QPixmap();
190
191 QRect adjusted = rect;
192 if (adjusted.width() <= 0)
193 adjusted.setWidth(area.width());
194 if (adjusted.height() <= 0)
195 adjusted.setHeight(area.height());
196
197 adjusted = adjusted.translated(area.topLeft()) & area;
198
199 if (adjusted.isEmpty())
200 return QPixmap();
201
202 return QPixmap::fromImage(m_image.copy(adjusted));
203}
204
205QOffscreenBackingStore *QOffscreenBackingStore::backingStoreForWinId(WId id)
206{
207 return m_backingStoreForWinIdHash.value(id, nullptr);
208}
209
210void QOffscreenBackingStore::clearHash()
211{
212 for (auto it = m_windowAreaHash.cbegin(), end = m_windowAreaHash.cend(); it != end; ++it) {
213 const auto it2 = std::as_const(m_backingStoreForWinIdHash).find(it.key());
214 if (it2.value() == this)
215 m_backingStoreForWinIdHash.erase(it2);
216 }
217 m_windowAreaHash.clear();
218}
219
220QHash<WId, QOffscreenBackingStore *> QOffscreenBackingStore::m_backingStoreForWinIdHash;
221
223 : m_integration(integration)
224{
225
226}
227
229
230/*
231 Set platform configuration, e.g. screen configuration
232*/
233void QOffscreenPlatformNativeInterface::setConfiguration(const QJsonObject &configuration, QOffscreenPlatformNativeInterface *iface)
234{
235 iface->m_integration->setConfiguration(configuration);
236}
237
238/*
239 Get the current platform configuration
240*/
242{
243 return iface->m_integration->configuration();
244}
245
247{
248 if (resource == "setConfiguration")
249 return reinterpret_cast<void*>(&QOffscreenPlatformNativeInterface::setConfiguration);
250 else if (resource == "configuration")
251 return reinterpret_cast<void*>(&QOffscreenPlatformNativeInterface::configuration);
252 else
253 return nullptr;
254}
255
256QT_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)