Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(win): harden the DXGI fallback (reacquire, adapter, cursor, rotation, resize)

EtienneLescote7cc7f474dd9
diff --git a/electron/native/wgc-capture/CMakeLists.txt b/electron/native/wgc-capture/CMakeLists.txt
index 3429cff3f..87eaf2af9 100644
--- a/electron/native/wgc-capture/CMakeLists.txt
+++ b/electron/native/wgc-capture/CMakeLists.txt
@@ -81,6 +81,7 @@ target_link_libraries(wgc-capture PRIVATE
avrt
d3d11
dxgi
+ gdi32
mf
mfplat
mfreadwrite
diff --git a/electron/native/wgc-capture/src/dxgi_duplication_session.cpp b/electron/native/wgc-capture/src/dxgi_duplication_session.cpp
index b93202fb9..e4a4b8c07 100644
--- a/electron/native/wgc-capture/src/dxgi_duplication_session.cpp
+++ b/electron/native/wgc-capture/src/dxgi_duplication_session.cpp
@@ -5,9 +5,10 @@
namespace {
-// Same device shape WgcSession creates, so the encoder and the cursor sampler
-// see the same adapter/feature level whichever backend produced the frames.
-bool createDevice(Microsoft::WRL::ComPtr<ID3D11Device>& device,
+// Same device shape WgcSession creates, but on the adapter that owns the output:
+// DuplicateOutput refuses a device from any other adapter, which is the normal
+// case for a display wired to the discrete GPU of a hybrid laptop.
+bool createDevice(IDXGIAdapter1* adapter, Microsoft::WRL::ComPtr<ID3D11Device>& device,
Microsoft::WRL::ComPtr<ID3D11DeviceContext>& context) {
const D3D_FEATURE_LEVEL levels[] = {
D3D_FEATURE_LEVEL_11_1,
@@ -17,11 +18,11 @@ bool createDevice(Microsoft::WRL::ComPtr<ID3D11Device>& device,
};
UINT flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
D3D_FEATURE_LEVEL obtained = D3D_FEATURE_LEVEL_11_0;
- HRESULT hr = D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, flags, levels,
+ HRESULT hr = D3D11CreateDevice(adapter, D3D_DRIVER_TYPE_UNKNOWN, nullptr, flags, levels,
ARRAYSIZE(levels), D3D11_SDK_VERSION, device.GetAddressOf(),
&obtained, context.GetAddressOf());
if (FAILED(hr)) {
- hr = D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr,
+ hr = D3D11CreateDevice(adapter, D3D_DRIVER_TYPE_UNKNOWN, nullptr,
flags & ~D3D11_CREATE_DEVICE_BGRA_SUPPORT, levels,
ARRAYSIZE(levels), D3D11_SDK_VERSION, device.GetAddressOf(),
&obtained, context.GetAddressOf());
@@ -40,48 +41,62 @@ DxgiDuplicationSession::~DxgiDuplicationSession() {
stop();
}
-bool DxgiDuplicationSession::createD3DDevice() {
- if (d3dDevice_) {
- return true;
- }
- if (!createDevice(d3dDevice_, d3dContext_)) {
- return false;
- }
- QueryPerformanceFrequency(&qpcFrequency_);
- return true;
-}
-
-bool DxgiDuplicationSession::createDuplication(HMONITOR monitor) {
- Microsoft::WRL::ComPtr<IDXGIDevice> dxgiDevice;
- if (FAILED(d3dDevice_.As(&dxgiDevice))) {
- return false;
- }
- if (FAILED(dxgiDevice->GetAdapter(adapter_.ReleaseAndGetAddressOf()))) {
+bool DxgiDuplicationSession::findOutput(HMONITOR monitor) {
+ output_.Reset();
+ adapter_.Reset();
+ Microsoft::WRL::ComPtr<IDXGIFactory1> factory;
+ if (FAILED(CreateDXGIFactory1(IID_PPV_ARGS(&factory)))) {
+ std::cerr << "ERROR: DXGI capture could not create a DXGI factory" << std::endl;
return false;
}
// The monitor handle the caller matched with EnumDisplayMonitors is the only
// thing tying the app's chosen source to an output, so match on it rather
// than on index or position: a display can be rearranged between the picker
- // and the take.
- Microsoft::WRL::ComPtr<IDXGIOutput> candidate;
- for (UINT i = 0; adapter_->EnumOutputs(i, candidate.ReleaseAndGetAddressOf()) == S_OK; ++i) {
- DXGI_OUTPUT_DESC desc{};
- if (SUCCEEDED(candidate->GetDesc(&desc)) && desc.Monitor == monitor) {
+ // and the take. Every adapter is searched, not just the default one.
+ Microsoft::WRL::ComPtr<IDXGIAdapter1> adapter;
+ for (UINT a = 0; factory->EnumAdapters1(a, adapter.ReleaseAndGetAddressOf()) == S_OK; ++a) {
+ Microsoft::WRL::ComPtr<IDXGIOutput> candidate;
+ for (UINT i = 0; adapter->EnumOutputs(i, candidate.ReleaseAndGetAddressOf()) == S_OK; ++i) {
+ DXGI_OUTPUT_DESC desc{};
+ if (FAILED(candidate->GetDesc(&desc)) || desc.Monitor != monitor) {
+ continue;
+ }
+ if (desc.Rotation != DXGI_MODE_ROTATION_IDENTITY &&
+ desc.Rotation != DXGI_MODE_ROTATION_UNSPECIFIED) {
+ // Desktop Duplication hands over the unrotated scan-out image, so a
+ // portrait display would record sideways. Refuse rather than do that.
+ std::cerr << "ERROR: DXGI capture does not support rotated displays (rotation="
+ << desc.Rotation << ")" << std::endl;
+ return false;
+ }
if (FAILED(candidate.As(&output_))) {
std::cerr << "ERROR: DXGI capture could not query IDXGIOutput1 for the chosen monitor"
<< std::endl;
return false;
}
- break;
+ adapter_ = adapter;
+ desktopRect_ = desc.DesktopCoordinates;
+ return true;
}
}
- if (!output_) {
- std::cerr << "ERROR: DXGI capture could not find an output for the chosen monitor"
- << std::endl;
+ std::cerr << "ERROR: DXGI capture could not find an output for the chosen monitor"
+ << std::endl;
+ return false;
+}
+
+bool DxgiDuplicationSession::createD3DDevice() {
+ if (d3dDevice_) {
+ return true;
+ }
+ if (!createDevice(adapter_.Get(), d3dDevice_, d3dContext_)) {
return false;
}
+ QueryPerformanceFrequency(&qpcFrequency_);
+ return true;
+}
+bool DxgiDuplicationSession::createDuplication() {
Microsoft::WRL::ComPtr<IDXGIOutputDuplication> duplication;
HRESULT hr = output_->DuplicateOutput(d3dDevice_.Get(), duplication.GetAddressOf());
if (FAILED(hr)) {
@@ -121,6 +136,8 @@ bool DxgiDuplicationSession::ensureCopyTexture() {
// released immediately and the pointer handed to the caller stays valid.
// Flags match what main.cpp already does when it copies a WGC frame out:
// no bind flags, no CPU access -- it is only ever a CopyResource source.
+ // With the system cursor it must also take a GDI DC, which needs a render
+ // target that is GDI-compatible (drawCursor).
D3D11_TEXTURE2D_DESC desc{};
desc.Width = static_cast<UINT>(width_);
desc.Height = static_cast<UINT>(height_);
@@ -129,9 +146,9 @@ bool DxgiDuplicationSession::ensureCopyTexture() {
desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
desc.SampleDesc.Count = 1;
desc.Usage = D3D11_USAGE_DEFAULT;
- desc.BindFlags = 0;
+ desc.BindFlags = captureCursor_ ? D3D11_BIND_RENDER_TARGET : 0;
desc.CPUAccessFlags = 0;
- desc.MiscFlags = 0;
+ desc.MiscFlags = captureCursor_ ? D3D11_RESOURCE_MISC_GDI_COMPATIBLE : 0;
const HRESULT hr = d3dDevice_->CreateTexture2D(&desc, nullptr, copyTexture_.GetAddressOf());
if (FAILED(hr)) {
std::cerr << "ERROR: DXGI capture could not create its copy texture (hr=0x" << std::hex << hr
@@ -145,17 +162,14 @@ bool DxgiDuplicationSession::initialize(HMONITOR monitor, int fps, bool captureC
if (!monitor) {
return false;
}
- if (captureCursor_) {
- // Nothing to do yet; the warning is emitted once on the first frame.
- }
captureCursor_ = captureCursor;
fps_ = std::clamp(fps, 1, 120);
monitor_ = monitor;
- if (!createD3DDevice()) {
+ if (!findOutput(monitor) || !createD3DDevice()) {
return false;
}
- if (!createDuplication(monitor)) {
+ if (!createDuplication()) {
return false;
}
if (!ensureCopyTexture()) {
@@ -185,45 +199,46 @@ int64_t DxgiDuplicationSession::ticksToHns(LONGLONG ticks) const {
return ticks;
}
// 10'000'000 == one second in 100 ns units.
- return static_cast<int64_t>((ticks * 10'000'000LL) / qpcFrequency_.QuadPart);
+ // Split so ticks * 10^7 never overflows (it would after ~25 h of uptime).
+ const LONGLONG f = qpcFrequency_.QuadPart;
+ return static_cast<int64_t>((ticks / f) * 10'000'000LL + ((ticks % f) * 10'000'000LL) / f);
}
bool DxgiDuplicationSession::reacquire() {
// DXGI_ERROR_ACCESS_LOST is normal: a UAC prompt, a desktop switch, a
// resolution or refresh-rate change, or another process taking exclusive
// fullscreen all invalidate the duplication. Rebuild it and let the caller
- // keep polling -- a frame is simply skipped, never a recording ended.
- ++reacquireCount_;
+ // keep polling -- a frame is simply skipped, never a recording ended. While
+ // the secure desktop is up, DuplicateOutput keeps failing, so the caller
+ // retries about once a second until it comes back.
+ lastReacquireAttemptMs_ = GetTickCount64();
duplication_.Reset();
- output_.Reset();
- adapter_.Reset();
copyTexture_.Reset();
- if (!d3dDevice_) {
- return false;
- }
- if (!createDuplication(monitor_)) {
- if (!warnedAccessLost_) {
- warnedAccessLost_ = true;
- std::cerr << "WARNING: DXGI capture lost the desktop and could not reacquire it; "
- "recording continues without new frames"
+ if (!d3dDevice_ || !findOutput(monitor_) || !createDuplication() || !ensureCopyTexture()) {
+ duplication_.Reset();
+ if (!warnedReacquireFailed_) {
+ warnedReacquireFailed_ = true;
+ std::cerr << "WARNING: DXGI capture lost the desktop and could not reacquire it yet; "
+ "retrying every second"
<< std::endl;
}
return false;
}
- if (!ensureCopyTexture()) {
- return false;
- }
- if (!warnedAccessLost_) {
- warnedAccessLost_ = true;
- std::cout << "{\"event\":\"capture-reacquired\",\"backend\":\"dxgi\"}" << std::endl;
- }
+ warnedReacquireFailed_ = false;
+ std::cout << "{\"event\":\"capture-reacquired\",\"backend\":\"dxgi\"}" << std::endl;
return true;
}
bool DxgiDuplicationSession::tryGetNextFrame(ID3D11Texture2D** outTexture, int64_t* outTimestampHns) {
- if (!outTexture || !outTimestampHns || !started_ || !duplication_ || exhausted_) {
+ if (!outTexture || !outTimestampHns || !started_) {
return false;
}
+ if (!duplication_) {
+ // A previous rebuild failed: try again, throttled, rather than give up.
+ if (GetTickCount64() - lastReacquireAttemptMs_ < 1000 || !reacquire()) {
+ return false;
+ }
+ }
*outTexture = nullptr;
// Wait no longer than one frame interval. main.cpp polls this on its own
@@ -239,9 +254,7 @@ bool DxgiDuplicationSession::tryGetNextFrame(ID3D11Texture2D** outTexture, int64
return false;
}
if (hr == DXGI_ERROR_ACCESS_LOST) {
- if (!reacquire()) {
- exhausted_ = true;
- }
+ reacquire();
return false;
}
if (FAILED(hr)) {
@@ -273,16 +286,8 @@ bool DxgiDuplicationSession::tryGetNextFrame(ID3D11Texture2D** outTexture, int64
return false;
}
- if (captureCursor_ && !warnedCursorIgnored_) {
- warnedCursorIgnored_ = true;
- // Desktop Duplication hands over the desktop image without the pointer;
- // drawing it is the caller's job (DXGI_OUTDUPL_FRAME_INFO carries the
- // shape and position). The app's default editable-overlay mode does not
- // ask for it -- it samples the cursor separately and composites its own
- // -- so this only affects a take explicitly set to the system cursor.
- std::cerr << "WARNING: DXGI capture does not composite the system cursor; use the "
- "editable cursor mode for this machine"
- << std::endl;
+ if (captureCursor_) {
+ drawCursor();
}
// LastPresentTime shares the QPC clock. A zero means the frame carried no
@@ -299,6 +304,39 @@ bool DxgiDuplicationSession::tryGetNextFrame(ID3D11Texture2D** outTexture, int64
return true;
}
+void DxgiDuplicationSession::drawCursor() {
+ CURSORINFO ci{};
+ ci.cbSize = sizeof(ci);
+ if (!GetCursorInfo(&ci) || !(ci.flags & CURSOR_SHOWING) || !ci.hCursor) {
+ return;
+ }
+ ICONINFO icon{};
+ if (!GetIconInfo(ci.hCursor, &icon)) {
+ return;
+ }
+ // GetIconInfo hands over copies of the bitmaps, which the caller owns.
+ if (icon.hbmMask) {
+ DeleteObject(icon.hbmMask);
+ }
+ if (icon.hbmColor) {
+ DeleteObject(icon.hbmColor);
+ }
+
+ Microsoft::WRL::ComPtr<IDXGISurface1> surface;
+ HDC dc = nullptr;
+ if (FAILED(copyTexture_.As(&surface)) || FAILED(surface->GetDC(FALSE, &dc))) {
+ return;
+ }
+ // The helper is per-monitor DPI aware, so the cursor position and the
+ // output's desktop rectangle are both in physical pixels. DrawIconEx with
+ // DI_NORMAL applies the AND/XOR masks, so monochrome and inverting cursors
+ // look the way they do on screen.
+ DrawIconEx(dc, ci.ptScreenPos.x - desktopRect_.left - static_cast<int>(icon.xHotspot),
+ ci.ptScreenPos.y - desktopRect_.top - static_cast<int>(icon.yHotspot), ci.hCursor, 0,
+ 0, 0, nullptr, DI_NORMAL | DI_DEFAULTSIZE);
+ surface->ReleaseDC(nullptr);
+}
+
void DxgiDuplicationSession::setFrameCallback(FrameCallback callback) {
(void)callback;
// Deliberately inert: Desktop Duplication is pull-only, and rebuilding the
diff --git a/electron/native/wgc-capture/src/dxgi_duplication_session.h b/electron/native/wgc-capture/src/dxgi_duplication_session.h
index 5c4ed74c2..911a0b6f6 100644
--- a/electron/native/wgc-capture/src/dxgi_duplication_session.h
+++ b/electron/native/wgc-capture/src/dxgi_duplication_session.h
@@ -63,10 +63,14 @@ public:
ID3D11DeviceContext* context() const { return d3dContext_.Get(); }
private:
+ bool findOutput(HMONITOR monitor);
bool createD3DDevice();
- bool createDuplication(HMONITOR monitor);
+ bool createDuplication();
bool ensureCopyTexture();
bool reacquire();
+ // WGC draws the pointer itself; Desktop Duplication never does, so with the
+ // system cursor mode it is drawn here, over the private copy, through GDI.
+ void drawCursor();
// LastPresentTime and QueryPerformanceCounter share the QPC clock, which is
// the same clock WGC's SystemRelativeTime is derived from -- and main.cpp
// only ever uses the value as a delta from the first frame, so a monotonic
@@ -75,7 +79,7 @@ private:
Microsoft::WRL::ComPtr<ID3D11Device> d3dDevice_;
Microsoft::WRL::ComPtr<ID3D11DeviceContext> d3dContext_;
- Microsoft::WRL::ComPtr<IDXGIAdapter> adapter_;
+ Microsoft::WRL::ComPtr<IDXGIAdapter1> adapter_;
Microsoft::WRL::ComPtr<IDXGIOutput1> output_;
Microsoft::WRL::ComPtr<IDXGIOutputDuplication> duplication_;
Microsoft::WRL::ComPtr<ID3D11Texture2D> copyTexture_;
@@ -87,8 +91,7 @@ private:
int fps_ = 60;
bool captureCursor_ = false;
bool started_ = false;
- bool warnedCursorIgnored_ = false;
- bool warnedAccessLost_ = false;
- bool exhausted_ = false;
- uint64_t reacquireCount_ = 0;
+ RECT desktopRect_{};
+ bool warnedReacquireFailed_ = false;
+ ULONGLONG lastReacquireAttemptMs_ = 0;
};
diff --git a/electron/native/wgc-capture/src/main.cpp b/electron/native/wgc-capture/src/main.cpp
index d4bd09d4b..ff369d9cd 100644
--- a/electron/native/wgc-capture/src/main.cpp
+++ b/electron/native/wgc-capture/src/main.cpp
@@ -1166,6 +1166,23 @@ int wmain(int argc, wchar_t* argv[]) {
break;
}
}
+ // A display mode change mid-take (resolution, scale)
+ // hands over frames of a new size. The encoder was set
+ // up for the first one, and CopyResource between
+ // mismatched textures is undefined, so end the take
+ // with a clear error instead of recording garbage.
+ D3D11_TEXTURE2D_DESC frameDesc{};
+ D3D11_TEXTURE2D_DESC latestDesc{};
+ wgcTexture->GetDesc(&frameDesc);
+ latestFrameTexture->GetDesc(&latestDesc);
+ if (frameDesc.Width != latestDesc.Width || frameDesc.Height != latestDesc.Height) {
+ std::cerr << "ERROR: Capture size changed mid-recording (" << latestDesc.Width
+ << "x" << latestDesc.Height << " -> " << frameDesc.Width << "x"
+ << frameDesc.Height << "); ending the take" << std::endl;
+ encodeFailed = true;
+ control.requestStop();
+ break;
+ }
// The wedge risk this class exists to avoid: this call
// can block inside the display driver and never return
// (#252, still true of PR #305's DXGI path on some