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
qgdiwindowcapture.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
5
6#include "qvideoframe.h"
8#include "private/qcapturablewindow_p.h"
9#include "private/qmemoryvideobuffer_p.h"
10#include "private/qvideoframe_p.h"
11
12#include <qt_windows.h>
13#include <QtCore/qloggingcategory.h>
14
15Q_STATIC_LOGGING_CATEGORY(qLcGdiWindowCapture, "qt.multimedia.ffmpeg.gdiwindowcapture");
16
17QT_BEGIN_NAMESPACE
18
19class QGdiWindowCapture::Grabber : public QFFmpegSurfaceCaptureGrabber
20{
21public:
22 static std::unique_ptr<Grabber> create(QGdiWindowCapture &capture, HWND hWnd)
23 {
24 auto hdcWindow = GetDC(hWnd);
25 if (!hdcWindow) {
26 capture.updateError(QPlatformSurfaceCapture::CaptureFailed,
27 QLatin1String("Cannot create a window drawing context"));
28 return nullptr;
29 }
30
31 auto hdcMem = CreateCompatibleDC(hdcWindow);
32
33 if (!hdcMem) {
34 capture.updateError(QPlatformSurfaceCapture::CaptureFailed,
35 QLatin1String("Cannot create a compatible drawing context"));
36 return nullptr;
37 }
38
39 std::unique_ptr<Grabber> result(new Grabber(capture, hWnd, hdcWindow, hdcMem));
40 if (!result->update())
41 return nullptr;
42
43 result->start();
44 return result;
45 }
46
47 ~Grabber() override
48 {
49 stop();
50
51 if (m_hBitmap)
52 DeleteObject(m_hBitmap);
53
54 if (m_hdcMem)
55 DeleteDC(m_hdcMem);
56
57 if (m_hdcWindow)
58 ReleaseDC(m_hwnd, m_hdcWindow);
59 }
60
61 QVideoFrameFormat format() const { return m_format; }
62
63private:
64 Grabber(QGdiWindowCapture &capture, HWND hWnd, HDC hdcWindow, HDC hdcMem)
65 : m_hwnd(hWnd), m_hdcWindow(hdcWindow), m_hdcMem(hdcMem)
66 {
67 if (auto rate = GetDeviceCaps(hdcWindow, VREFRESH); rate > 0)
68 setFrameRate(rate);
69
70 addFrameCallback(capture, &QGdiWindowCapture::newVideoFrame);
71 connect(this, &Grabber::errorUpdated, &capture, &QGdiWindowCapture::updateError);
72 }
73
74 bool update()
75 {
76 RECT windowRect{};
77 if (!GetWindowRect(m_hwnd, &windowRect)) {
78 updateError(QPlatformSurfaceCapture::CaptureFailed,
79 QLatin1String("Cannot get window size"));
80 return false;
81 }
82
83 const QSize size{ windowRect.right - windowRect.left, windowRect.bottom - windowRect.top };
84
85 if (m_format.isValid() && size == m_format.frameSize() && m_hBitmap)
86 return true;
87
88 if (m_hBitmap)
89 DeleteObject(std::exchange(m_hBitmap, nullptr));
90
91 if (size.isEmpty()) {
92 m_format = {};
93 updateError(QPlatformSurfaceCapture::CaptureFailed,
94 QLatin1String("Invalid window size"));
95 return false;
96 }
97
98 m_hBitmap = CreateCompatibleBitmap(m_hdcWindow, size.width(), size.height());
99
100 if (!m_hBitmap) {
101 m_format = {};
102 updateError(QPlatformSurfaceCapture::CaptureFailed,
103 QLatin1String("Cannot create a compatible bitmap"));
104 return false;
105 }
106
107 QVideoFrameFormat format(size, QVideoFrameFormat::Format_BGRX8888);
108 format.setStreamFrameRate(frameRate());
109 m_format = format;
110 return true;
111 }
112
113 QVideoFrame grabFrame() override
114 {
115 if (!update())
116 return {};
117
118 const auto oldBitmap = SelectObject(m_hdcMem, m_hBitmap);
119 auto deselect = qScopeGuard([&]() { SelectObject(m_hdcMem, oldBitmap); });
120
121 const auto size = m_format.frameSize();
122
123 if (!BitBlt(m_hdcMem, 0, 0, size.width(), size.height(), m_hdcWindow, 0, 0, SRCCOPY)) {
124 updateError(QPlatformSurfaceCapture::CaptureFailed,
125 QLatin1String("Cannot copy image to the compatible DC"));
126 return {};
127 }
128
129 BITMAPINFO info{};
130 auto &header = info.bmiHeader;
131 header.biSize = sizeof(BITMAPINFOHEADER);
132 header.biWidth = size.width();
133 header.biHeight = -size.height(); // negative height to ensure top-down orientation
134 header.biPlanes = 1;
135 header.biBitCount = 32;
136 header.biCompression = BI_RGB;
137
138 const auto bytesPerLine = size.width() * 4;
139
140 QByteArray array(size.height() * bytesPerLine, Qt::Uninitialized);
141
142 const auto copiedHeight = GetDIBits(m_hdcMem, m_hBitmap, 0, size.height(), array.data(), &info, DIB_RGB_COLORS);
143 if (copiedHeight != size.height()) {
144 qCWarning(qLcGdiWindowCapture) << copiedHeight << "lines have been copied, expected:" << size.height();
145 // In practice, it might fail randomly first time after start. So we don't consider it as an error.
146 // TODO: investigate reasons and properly handle the error
147 // updateError(QPlatformSurfaceCapture::CaptureFailed,
148 // QLatin1String("Cannot get raw image data"));
149 return {};
150 }
151
152 if (header.biWidth != size.width() || header.biHeight != -size.height()
153 || header.biPlanes != 1 || header.biBitCount != 32 || header.biCompression != BI_RGB) {
154 updateError(QPlatformSurfaceCapture::CaptureFailed,
155 QLatin1String("Output bitmap info is unexpected"));
156 return {};
157 }
158
159 return QVideoFramePrivate::createFrame(
160 std::make_unique<QMemoryVideoBuffer>(std::move(array), bytesPerLine), m_format);
161 }
162
163private:
164 HWND m_hwnd = {};
165 QVideoFrameFormat m_format;
166 HDC m_hdcWindow = {};
167 HDC m_hdcMem = {};
168 HBITMAP m_hBitmap = {};
169};
170
171QGdiWindowCapture::QGdiWindowCapture() : QPlatformSurfaceCapture(WindowSource{}) { }
172
173QGdiWindowCapture::~QGdiWindowCapture() = default;
174
175QVideoFrameFormat QGdiWindowCapture::frameFormat() const
176{
177 return m_grabber ? m_grabber->format() : QVideoFrameFormat();
178}
179
180bool QGdiWindowCapture::setActiveInternal(bool active)
181{
182 if (active == static_cast<bool>(m_grabber))
183 return true;
184
185 if (m_grabber) {
186 m_grabber.reset();
187 } else {
188 auto window = source<WindowSource>();
189 auto handle = QCapturableWindowPrivate::handle(window);
190
191 m_grabber = Grabber::create(*this, reinterpret_cast<HWND>(handle ? handle->id : 0));
192 }
193
194 return static_cast<bool>(m_grabber) == active;
195}
196
197QT_END_NAMESPACE
Q_STATIC_LOGGING_CATEGORY(lcAccessibilityCore, "qt.accessibility.core")