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
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
17
18QPlatformWindow *QOffscreenScreen::windowContainingCursor = nullptr;
19
20
21QList<QPlatformScreen *> QOffscreenScreen::virtualSiblings() const
22{
23 QList<QPlatformScreen *> platformScreens;
24 for (auto screen : m_integration->screens()) {
25 platformScreens.append(screen);
26 }
27 return platformScreens;
28}
29
31{
32public:
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->isExposed() && w->geometry().contains(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(containing, previous, local, pos);
56
57 QWindowSystemInterface::handleMouseEvent(containing, local, pos, QGuiApplication::mouseButtons(), Qt::NoButton,
58 QEvent::MouseMove, QGuiApplication::keyboardModifiers(), 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
69private:
70 QPoint m_pos;
71};
72
74 : m_geometry(0, 0, 800, 600)
76 , m_integration(integration)
77{
78}
79
80QPixmap 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(-x, -y);
93 const QWindowList wl = QGuiApplication::topLevelWindows();
94 for (QWindow *w : wl) {
95 if (w->isExposed() && w->geometry().intersects(rect)) {
96 QOffscreenBackingStore *store = QOffscreenBackingStore::backingStoreForWinId(w->winId());
97 const QImage windowImage = store ? store->toImage() : QImage();
98 if (!windowImage.isNull())
99 painter.drawImage(w->position(), windowImage);
100 }
101 }
102 return screenImage;
103 }
104
105 QOffscreenBackingStore *store = QOffscreenBackingStore::backingStoreForWinId(id);
106 if (store)
107 return store->grabWindow(id, rect);
108 return QPixmap();
109}
110
115
117{
118 clearHash();
119}
120
122{
123 return &m_image;
124}
125
126void QOffscreenBackingStore::flush(QWindow *window, const QRegion &region, 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(-offset);
137
138 QRect bounds = clipped.boundingRect().translated(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
149void 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
159extern void qt_scrollRectInImage(QImage &img, const QRect &rect, const QPoint &offset);
160
161bool 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(m_image, rect, QPoint(dx, dy));
168
169 return true;
170}
171
172void QOffscreenBackingStore::beginPaint(const QRegion &region)
173{
174 if (QImage::toPixelFormat(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, blank);
180 }
181}
182
183QPixmap QOffscreenBackingStore::grabWindow(WId window, const QRect &rect) const
184{
185 QRect area = m_windowAreaHash.value(window, 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(area.topLeft()) & area;
196
197 if (adjusted.isEmpty())
198 return QPixmap();
199
200 return QPixmap::fromImage(m_image.copy(adjusted));
201}
202
203QOffscreenBackingStore *QOffscreenBackingStore::backingStoreForWinId(WId id)
204{
205 return m_backingStoreForWinIdHash.value(id, nullptr);
206}
207
208void QOffscreenBackingStore::clearHash()
209{
210 for (auto it = m_windowAreaHash.cbegin(), end = m_windowAreaHash.cend(); it != end; ++it) {
211 const auto it2 = std::as_const(m_backingStoreForWinIdHash).find(it.key());
212 if (it2.value() == this)
213 m_backingStoreForWinIdHash.erase(it2);
214 }
215 m_windowAreaHash.clear();
216}
217
218QHash<WId, QOffscreenBackingStore *> QOffscreenBackingStore::m_backingStoreForWinIdHash;
219
221 : m_integration(integration)
222{
223
224}
225
227
228/*
229 Set platform configuration, e.g. screen configuration
230*/
231void QOffscreenPlatformNativeInterface::setConfiguration(const QJsonObject &configuration, QOffscreenPlatformNativeInterface *iface)
232{
233 iface->m_integration->setConfiguration(configuration);
234}
235
236/*
237 Get the current platform configuration
238*/
240{
241 return iface->m_integration->configuration();
242}
243
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
254QT_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
void qt_scrollRectInImage(QImage &img, const QRect &rect, const QPoint &offset)