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
qx11surfacecapture.cpp
Go to the documentation of this file.
1// Copyright (C) 2023 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
6
7#include <qvideoframe.h>
8#include <qscreen.h>
9#include <qwindow.h>
10#include <qdebug.h>
11#include <qguiapplication.h>
12#include <qloggingcategory.h>
13#include <qminmax.h>
14
15#include "private/qcapturablewindow_p.h"
16#include "private/qmemoryvideobuffer_p.h"
17#include "private/qvideoframeconversionhelper_p.h"
18#include "private/qvideoframe_p.h"
19
20#include <X11/Xlib.h>
21#include <sys/shm.h>
22#include <X11/extensions/XShm.h>
23#include <X11/Xutil.h>
24#include <X11/extensions/Xrandr.h>
25
26#include <optional>
27#include <utility>
28
29QT_BEGIN_NAMESPACE
30
31Q_STATIC_LOGGING_CATEGORY(qLcX11SurfaceCapture, "qt.multimedia.ffmpeg.qx11surfacecapture");
32
33namespace {
34
35void destroyXImage(XImage* image) {
36 XDestroyImage(image); // macro
37}
38
39template <typename T, typename D>
40std::unique_ptr<T, D> makeXUptr(T* ptr, D deleter) {
41 return std::unique_ptr<T, D>(ptr, deleter);
42}
43
44int screenNumberByName(Display *display, const QString &name)
45{
46 int size = 0;
47 auto monitors = makeXUptr(
48 XRRGetMonitors(display, XDefaultRootWindow(display), true, &size),
49 &XRRFreeMonitors);
50 const auto end = monitors.get() + size;
51 auto found = std::find_if(monitors.get(), end, [&](const XRRMonitorInfo &info) {
52 auto atomName = makeXUptr(XGetAtomName(display, info.name), &XFree);
53 return atomName && name == QString::fromUtf8(atomName.get());
54 });
55
56 return found == end ? -1 : std::distance(monitors.get(), found);
57}
58
59QVideoFrameFormat::PixelFormat xImagePixelFormat(const XImage &image)
60{
61 if (image.bits_per_pixel != 32) return QVideoFrameFormat::Format_Invalid;
62
63 if (image.red_mask == 0xff0000 &&
64 image.green_mask == 0xff00 &&
65 image.blue_mask == 0xff)
66 return QVideoFrameFormat::Format_BGRX8888;
67
68 if (image.red_mask == 0xff00 &&
69 image.green_mask == 0xff0000 &&
70 image.blue_mask == 0xff000000)
71 return QVideoFrameFormat::Format_XBGR8888;
72
73 if (image.blue_mask == 0xff0000 &&
74 image.green_mask == 0xff00 &&
75 image.red_mask == 0xff)
76 return QVideoFrameFormat::Format_RGBX8888;
77
78 if (image.red_mask == 0xff00 &&
79 image.green_mask == 0xff0000 &&
80 image.blue_mask == 0xff000000)
81 return QVideoFrameFormat::Format_XRGB8888;
82
83 return QVideoFrameFormat::Format_Invalid;
84}
85
86} // namespace
87
88class QX11SurfaceCapture::Grabber : private QFFmpegSurfaceCaptureGrabber
89{
90public:
91 static std::unique_ptr<Grabber> create(QX11SurfaceCapture &capture, QScreen *screen)
92 {
93 std::unique_ptr<Grabber> result(new Grabber(capture));
94 return result->init(screen) ? std::move(result) : nullptr;
95 }
96
97 static std::unique_ptr<Grabber> create(QX11SurfaceCapture &capture, WId wid)
98 {
99 std::unique_ptr<Grabber> result(new Grabber(capture));
100 return result->init(wid) ? std::move(result) : nullptr;
101 }
102
104 {
105 stop();
106
107 detachShm();
108 }
109
110 const QVideoFrameFormat &format() const { return m_format; }
111
112private:
113 Grabber(QX11SurfaceCapture &capture)
114 {
115 addFrameCallback(capture, &QX11SurfaceCapture::newVideoFrame);
116 connect(this, &Grabber::errorUpdated, &capture, &QX11SurfaceCapture::updateError);
117 }
118
119 bool createDisplay()
120 {
121 if (!m_display)
122 m_display.reset(XOpenDisplay(nullptr));
123
124 if (!m_display)
125 updateError(QPlatformSurfaceCapture::InternalError,
126 QStringLiteral("Cannot open X11 display"));
127
128 return m_display != nullptr;
129 }
130
131 bool init(WId wid)
132 {
133 if (auto screen = QGuiApplication::primaryScreen())
134 setFrameRate(screen->refreshRate());
135
136 return createDisplay() && initWithXID(static_cast<XID>(wid));
137 }
138
139 bool init(QScreen *screen)
140 {
141 if (!screen) {
142 updateError(QPlatformSurfaceCapture::NotFound, QStringLiteral("Screen Not Found"));
143 return false;
144 }
145
146 if (!createDisplay())
147 return false;
148
149 auto screenNumber = screenNumberByName(m_display.get(), screen->name());
150
151 if (screenNumber < 0)
152 return false;
153
154 setFrameRate(screen->refreshRate());
155
156 return initWithXID(RootWindow(m_display.get(), screenNumber));
157 }
158
159 bool initWithXID(XID xid)
160 {
161 m_xid = xid;
162
163 if (update()) {
164 start();
165 return true;
166 }
167
168 return false;
169 }
170
171 void detachShm()
172 {
173 if (std::exchange(m_attached, false)) {
174 XShmDetach(m_display.get(), &m_shmInfo);
175 shmdt(m_shmInfo.shmaddr);
176 shmctl(m_shmInfo.shmid, IPC_RMID, nullptr);
177 }
178 }
179
180 void attachShm()
181 {
182 Q_ASSERT(!m_attached);
183
184 m_shmInfo.shmid =
185 shmget(IPC_PRIVATE, m_xImage->bytes_per_line * m_xImage->height, IPC_CREAT | 0777);
186
187 if (m_shmInfo.shmid == -1)
188 return;
189
190 m_shmInfo.readOnly = false;
191 m_shmInfo.shmaddr = m_xImage->data = (char *)shmat(m_shmInfo.shmid, nullptr, 0);
192
193 m_attached = XShmAttach(m_display.get(), &m_shmInfo);
194 }
195
196 bool update()
197 {
198 Window win = m_xid;
199 XWindowAttributes wndattr;
200 int xPos = 0;
201 int yPos = 0;
202 int width = 0;
203 int height = 0;
204 int depth = 0;
205 Visual *visual = nullptr;
206 while (true) {
207 if (!XGetWindowAttributes(m_display.get(), win, &wndattr)) {
208 updateError(QPlatformSurfaceCapture::CaptureFailed,
209 QStringLiteral("Cannot get window attributes"));
210 return false;
211 }
212 if (win == m_xid) {
213 if (wndattr.map_state != IsViewable) {
214 updateError(QPlatformSurfaceCapture::CaptureFailed,
215 QStringLiteral("Window is not viewable"));
216 return false;
217 }
218 width = wndattr.width;
219 height = wndattr.height;
220 depth = wndattr.depth;
221 visual = wndattr.visual;
222 }
223 if (win == wndattr.root)
224 break;
225 xPos += wndattr.x;
226 yPos += wndattr.y;
227
228 Window root, *children;
229 unsigned int nchildren;
230 if (!XQueryTree(m_display.get(), win, &root, &win, &children, &nchildren)) {
231 updateError(QPlatformSurfaceCapture::CaptureFailed,
232 QStringLiteral("Cannot get parent window"));
233 return false;
234 }
235 if (children) XFree(children);
236 }
237
238 m_xOffset = qMax(0, -xPos);
239 m_yOffset = qMax(0, -yPos);
240 width = qMin(width, wndattr.width - xPos) - m_xOffset;
241 height = qMin(height, wndattr.height - yPos) - m_yOffset;
242 if (width <= 0 || height <= 0) {
243 updateError(QPlatformSurfaceCapture::CaptureFailed,
244 QStringLiteral("Window is completely out of the screen borders"));
245 return false;
246 }
247
248 // check window params for the root window as well since
249 // it potentially can be changed (e.g. on VM with resizing)
250 if (!m_xImage || width != m_xImage->width || height != m_xImage->height
251 || depth != m_xImage->depth || visual->visualid != m_visualID) {
252
253 qCDebug(qLcX11SurfaceCapture) << "recreate ximage: " << width << height
254 << depth << visual->visualid;
255
256 detachShm();
257 m_xImage.reset();
258
259 m_visualID = wndattr.visual->visualid;
260 m_xImage.reset(XShmCreateImage(m_display.get(), visual, depth, ZPixmap,
261 nullptr, &m_shmInfo, width, height));
262
263 if (!m_xImage) {
264 updateError(QPlatformSurfaceCapture::CaptureFailed,
265 QStringLiteral("Cannot create image"));
266 return false;
267 }
268
269 const auto pixelFormat = xImagePixelFormat(*m_xImage);
270
271 // TODO: probably, add a converter instead
272 if (pixelFormat == QVideoFrameFormat::Format_Invalid) {
273 updateError(QPlatformSurfaceCapture::CaptureFailed,
274 QStringLiteral("Not handled pixel format, bpp=")
275 + QString::number(m_xImage->bits_per_pixel));
276 return false;
277 }
278
279 attachShm();
280
281 if (!m_attached) {
282 updateError(QPlatformSurfaceCapture::CaptureFailed,
283 QStringLiteral("Cannot attach shared memory"));
284 return false;
285 }
286
287 QVideoFrameFormat format(QSize(m_xImage->width, m_xImage->height), pixelFormat);
288 format.setStreamFrameRate(frameRate());
289 m_format = format;
290 }
291
292 return m_attached;
293 }
294
295protected:
297 {
298 if (!grabXImage())
299 return {};
300
301 QByteArray data(m_xImage->bytes_per_line * m_xImage->height, Qt::Uninitialized);
302
303 const auto pixelSrc = reinterpret_cast<const uint32_t *>(m_xImage->data);
304 const auto pixelDst = reinterpret_cast<uint32_t *>(data.data());
305 const auto pixelCount = data.size() / 4;
306 const auto xImageAlphaVaries = false; // In known cases it doesn't vary - it's 0xff or 0xff
307
308 qCopyPixelsWithAlphaMask(pixelDst, pixelSrc, pixelCount, m_format.pixelFormat(),
309 xImageAlphaVaries);
310
311 auto buffer = std::make_unique<QMemoryVideoBuffer>(data, m_xImage->bytes_per_line);
312 return QVideoFramePrivate::createFrame(std::move(buffer), m_format);
313 }
314
315private:
316 bool grabXImage()
317 {
318 for (int i = 0; i < 10; i++) {
319 if (!update())
320 return false;
321 if (XShmGetImage(m_display.get(), m_xid, m_xImage.get(),
322 m_xOffset, m_yOffset, AllPlanes))
323 return true;
324 }
325
326 updateError(QPlatformSurfaceCapture::CaptureFailed,
327 QStringLiteral(
328 "Cannot get ximage; the window geometry may be undergoing change"));
329 return false;
330 }
331
332 std::optional<QPlatformSurfaceCapture::Error> m_prevGrabberError;
333 XID m_xid = None;
334 int m_xOffset = 0;
335 int m_yOffset = 0;
336 std::unique_ptr<Display, decltype(&XCloseDisplay)> m_display{ nullptr, &XCloseDisplay };
337 std::unique_ptr<XImage, decltype(&destroyXImage)> m_xImage{ nullptr, &destroyXImage };
338 XShmSegmentInfo m_shmInfo{};
339 bool m_attached = false;
340 VisualID m_visualID = None;
341 QVideoFrameFormat m_format;
342};
343
344QX11SurfaceCapture::QX11SurfaceCapture(Source initialSource)
345 : QPlatformSurfaceCapture(std::move(initialSource))
346{
347 // For debug
348 // XSetErrorHandler([](Display *, XErrorEvent * e) {
349 // qDebug() << "error handler" << e->error_code;
350 // return 0;
351 // });
352}
353
354QX11SurfaceCapture::~QX11SurfaceCapture() = default;
355
356QVideoFrameFormat QX11SurfaceCapture::frameFormat() const
357{
358 return m_grabber ? m_grabber->format() : QVideoFrameFormat{};
359}
360
361bool QX11SurfaceCapture::setActiveInternal(bool active)
362{
363 qCDebug(qLcX11SurfaceCapture) << "set active" << active;
364
365 if (m_grabber)
366 m_grabber.reset();
367 else
368 std::visit([this](const auto &source) {
369 activate(source);
370 }, source());
371
372 return static_cast<bool>(m_grabber) == active;
373}
374
375void QX11SurfaceCapture::activate(ScreenSource screen)
376{
377 if (checkScreenWithError(screen))
378 m_grabber = Grabber::create(*this, screen);
379}
380
381void QX11SurfaceCapture::activate(const WindowSource &window)
382{
383 auto handle = QCapturableWindowPrivate::handle(window);
384 m_grabber = Grabber::create(*this, handle ? handle->id : 0);
385}
386
387bool QX11SurfaceCapture::isSupported()
388{
389 return qgetenv("XDG_SESSION_TYPE").compare(QLatin1String("x11"), Qt::CaseInsensitive) == 0;
390}
391
392QT_END_NAMESPACE
static std::unique_ptr< Grabber > create(QX11SurfaceCapture &capture, QScreen *screen)
const QVideoFrameFormat & format() const
QVideoFrame grabFrame() override
static std::unique_ptr< Grabber > create(QX11SurfaceCapture &capture, WId wid)
#define qCDebug(category,...)
#define Q_STATIC_LOGGING_CATEGORY(name,...)