diff --git a/host/build-win/CMakeFiles/looking-glass-host.dir/platform/Windows/resource.rc.res b/host/build-win/CMakeFiles/looking-glass-host.dir/platform/Windows/resource.rc.res index 81c23877..04b19f09 100644 Binary files a/host/build-win/CMakeFiles/looking-glass-host.dir/platform/Windows/resource.rc.res and b/host/build-win/CMakeFiles/looking-glass-host.dir/platform/Windows/resource.rc.res differ diff --git a/host/build-win/CMakeFiles/looking-glass-host.dir/version.c.obj b/host/build-win/CMakeFiles/looking-glass-host.dir/version.c.obj index 7950528d..a7f6fe76 100644 Binary files a/host/build-win/CMakeFiles/looking-glass-host.dir/version.c.obj and b/host/build-win/CMakeFiles/looking-glass-host.dir/version.c.obj differ diff --git a/host/build-win/VERSION b/host/build-win/VERSION index 4f69cdb3..c4eba6d0 100644 --- a/host/build-win/VERSION +++ b/host/build-win/VERSION @@ -1 +1 @@ -B7-385-gb73ffa1b33+ \ No newline at end of file +B7-386-g76207dce41+ \ No newline at end of file diff --git a/host/build-win/include/version.h b/host/build-win/include/version.h index fd114bce..8c26d237 100644 --- a/host/build-win/include/version.h +++ b/host/build-win/include/version.h @@ -1,2 +1,2 @@ -#define LG_VERSION_STR "B7-385-gb73ffa1b33+" +#define LG_VERSION_STR "B7-386-g76207dce41+" #define LG_CURRENT_YEAR 2026 diff --git a/host/build-win/platform/Windows/capture/DXGI/CMakeFiles/capture_DXGI.dir/compiler_depend.internal b/host/build-win/platform/Windows/capture/DXGI/CMakeFiles/capture_DXGI.dir/compiler_depend.internal index 73fd6949..637c87a5 100644 --- a/host/build-win/platform/Windows/capture/DXGI/CMakeFiles/capture_DXGI.dir/compiler_depend.internal +++ b/host/build-win/platform/Windows/capture/DXGI/CMakeFiles/capture_DXGI.dir/compiler_depend.internal @@ -319,6 +319,7 @@ platform/Windows/capture/DXGI/CMakeFiles/capture_DXGI.dir/src/dxgi.c.obj /home/pop-os/Documents/looking-glass-2/common/include/common/array.h /home/pop-os/Documents/looking-glass-2/common/include/common/debug.h /home/pop-os/Documents/looking-glass-2/common/include/common/display.h + /home/pop-os/Documents/looking-glass-2/common/include/common/displays.h /home/pop-os/Documents/looking-glass-2/common/include/common/event.h /home/pop-os/Documents/looking-glass-2/common/include/common/framebuffer.h /home/pop-os/Documents/looking-glass-2/common/include/common/locking.h diff --git a/host/build-win/platform/Windows/capture/DXGI/CMakeFiles/capture_DXGI.dir/compiler_depend.make b/host/build-win/platform/Windows/capture/DXGI/CMakeFiles/capture_DXGI.dir/compiler_depend.make index b9d9fd86..f74e8812 100644 --- a/host/build-win/platform/Windows/capture/DXGI/CMakeFiles/capture_DXGI.dir/compiler_depend.make +++ b/host/build-win/platform/Windows/capture/DXGI/CMakeFiles/capture_DXGI.dir/compiler_depend.make @@ -317,6 +317,7 @@ platform/Windows/capture/DXGI/CMakeFiles/capture_DXGI.dir/src/dxgi.c.obj: /home/ /home/pop-os/Documents/looking-glass-2/common/include/common/array.h \ /home/pop-os/Documents/looking-glass-2/common/include/common/debug.h \ /home/pop-os/Documents/looking-glass-2/common/include/common/display.h \ + /home/pop-os/Documents/looking-glass-2/common/include/common/displays.h \ /home/pop-os/Documents/looking-glass-2/common/include/common/event.h \ /home/pop-os/Documents/looking-glass-2/common/include/common/framebuffer.h \ /home/pop-os/Documents/looking-glass-2/common/include/common/locking.h \ @@ -2177,6 +2178,8 @@ platform/Windows/capture/DXGI/CMakeFiles/capture_DXGI.dir/src/util.c.obj: /home/ /home/pop-os/Documents/looking-glass-2/common/include/common/event.h: +/home/pop-os/Documents/looking-glass-2/common/include/common/displays.h: + /home/pop-os/Documents/looking-glass-2/common/include/common/display.h: /home/pop-os/Documents/looking-glass-2/common/include/common/array.h: diff --git a/host/build-win/platform/Windows/capture/DXGI/CMakeFiles/capture_DXGI.dir/src/dxgi.c.obj b/host/build-win/platform/Windows/capture/DXGI/CMakeFiles/capture_DXGI.dir/src/dxgi.c.obj index 349c41ff..26636669 100644 Binary files a/host/build-win/platform/Windows/capture/DXGI/CMakeFiles/capture_DXGI.dir/src/dxgi.c.obj and b/host/build-win/platform/Windows/capture/DXGI/CMakeFiles/capture_DXGI.dir/src/dxgi.c.obj differ diff --git a/host/build-win/platform/Windows/capture/DXGI/CMakeFiles/capture_DXGI.dir/src/dxgi.c.obj.d b/host/build-win/platform/Windows/capture/DXGI/CMakeFiles/capture_DXGI.dir/src/dxgi.c.obj.d index 1e2e2212..1601b7ea 100644 --- a/host/build-win/platform/Windows/capture/DXGI/CMakeFiles/capture_DXGI.dir/src/dxgi.c.obj.d +++ b/host/build-win/platform/Windows/capture/DXGI/CMakeFiles/capture_DXGI.dir/src/dxgi.c.obj.d @@ -284,6 +284,7 @@ platform/Windows/capture/DXGI/CMakeFiles/capture_DXGI.dir/src/dxgi.c.obj: \ /usr/share/mingw-w64/include/winsvc.h /usr/share/mingw-w64/include/mcx.h \ /usr/share/mingw-w64/include/imm.h \ /home/pop-os/Documents/looking-glass-2/common/include/common/display.h \ + /home/pop-os/Documents/looking-glass-2/common/include/common/displays.h \ /home/pop-os/Documents/looking-glass-2/common/include/common/windebug.h \ /home/pop-os/Documents/looking-glass-2/common/include/common/debug.h \ /home/pop-os/Documents/looking-glass-2/common/include/common/option.h \ diff --git a/host/build-win/version.c b/host/build-win/version.c index 8f6cfbf1..b7b228c8 100644 --- a/host/build-win/version.c +++ b/host/build-win/version.c @@ -1 +1 @@ -const char * BUILD_VERSION = "B7-385-gb73ffa1b33+"; \ No newline at end of file +const char * BUILD_VERSION = "B7-386-g76207dce41+"; \ No newline at end of file diff --git a/host/platform/Windows/capture/DXGI/src/dxgi.c b/host/platform/Windows/capture/DXGI/src/dxgi.c index f8a99497..20853990 100644 --- a/host/platform/Windows/capture/DXGI/src/dxgi.c +++ b/host/platform/Windows/capture/DXGI/src/dxgi.c @@ -23,6 +23,7 @@ #include "common/array.h" #include "common/debug.h" #include "common/display.h" +#include "common/displays.h" #include "common/windebug.h" #include "common/option.h" #include "common/locking.h" @@ -164,6 +165,26 @@ struct DXGIInterface bool lastPointerVisible; FrameDamage frameDamage[LGMP_Q_FRAME_LEN]; + + /* Multi-monitor capture. + * + * `displayCount` is zero when capturing a single output, in which case + * every field below is unused and this backend behaves exactly as it always + * has. Otherwise each secondary output is duplicated separately and copied + * into `atlasTex`, which is then handed to the post processors in place of + * the acquired texture, so nothing downstream is aware of the difference. + * + * Index 0 is the primary output, which is `this->output` / `this->dup`; the + * secondary entries own their own duplication. */ + bool multiMonitor; + unsigned displayCount; + KVMFRDisplay displays[KVMFR_MAX_DISPLAYS]; + IDXGIOutput ** extraOutput [KVMFR_MAX_DISPLAYS]; + IDXGIOutputDuplication ** extraDup [KVMFR_MAX_DISPLAYS]; + bool extraNeedsRelease[KVMFR_MAX_DISPLAYS]; + ID3D11Texture2D ** atlasTex; + unsigned atlasWidth, atlasHeight; + unsigned desktopWidth, desktopHeight; }; // locals @@ -232,6 +253,7 @@ static struct DXGICopyBackend * backends[] = { static bool dxgi_deinit(void); static CaptureResult dxgi_releaseFrame(void); +static bool dxgi_initMultiMonitor(void); static void ppEarlyInit(void); static bool ppInit(const DXGIPostProcess * pp, bool shareable); @@ -243,6 +265,245 @@ static void ppFreeAll(void); // implementation +/* Bring up capture of the adapter's remaining outputs and pack them all into + * a single atlas frame. + * + * Returning false is not fatal and not an error: the caller carries on + * capturing the primary output alone, exactly as this backend always has. + * That is the deliberate response to anything this cannot handle safely, + * because a wrong atlas is worse than one working display. + * + * Must be called once the primary duplication exists and this->dxgiSrcFormat + * is known, and before the copy backend is created, since it changes the + * frame dimensions to those of the atlas. */ +static bool dxgi_initMultiMonitor(void) +{ + /* This runs again on every reinitialisation, which happens on resolution + * changes and desktop switches, so nothing may be assumed about the + * previous run's state. */ + this->displayCount = 0; + memset(this->extraNeedsRelease, 0, sizeof(this->extraNeedsRelease)); + + if (!this->multiMonitor) + return false; + + if (option_get_string("dxgi", "output")) + { + DEBUG_INFO("A specific output was requested, not capturing all outputs"); + return false; + } + + DXGI_OUTPUT_DESC primaryDesc; + IDXGIOutput_GetDesc(*this->output, &primaryDesc); + + if (primaryDesc.Rotation != DXGI_MODE_ROTATION_IDENTITY && + primaryDesc.Rotation != DXGI_MODE_ROTATION_UNSPECIFIED) + { + DEBUG_INFO("The primary output is rotated, not capturing all outputs"); + return false; + } + + LGDisplayInfo info[KVMFR_MAX_DISPLAYS]; + unsigned count = 0; + + // the primary is always display 0, so its duplication stays this->dup + memset(&info[0], 0, sizeof(info[0])); + info[0].id = 0; + info[0].primary = true; + info[0].x = primaryDesc.DesktopCoordinates.left; + info[0].y = primaryDesc.DesktopCoordinates.top; + info[0].width = primaryDesc.DesktopCoordinates.right - + primaryDesc.DesktopCoordinates.left; + info[0].height = primaryDesc.DesktopCoordinates.bottom - + primaryDesc.DesktopCoordinates.top; + info[0].rotation = FRAME_ROT_0; + wcstombs(info[0].name, primaryDesc.DeviceName, sizeof(info[0].name) - 1); + count = 1; + + comRef_scopePush(4); + comRef_defineLocal(IDXGIOutput, output); + + for(int n = 0; + IDXGIAdapter1_EnumOutputs(*this->adapter, n, output) != DXGI_ERROR_NOT_FOUND; + ++n, comRef_release(output)) + { + DXGI_OUTPUT_DESC desc; + IDXGIOutput_GetDesc(*output, &desc); + + if (!desc.AttachedToDesktop) + continue; + + // skip the primary, which is already display 0 + if (wcscmp(desc.DeviceName, primaryDesc.DeviceName) == 0) + continue; + + if (count == KVMFR_MAX_DISPLAYS) + { + DEBUG_WARN("More than %d outputs attached, ignoring the rest", + KVMFR_MAX_DISPLAYS); + break; + } + + /* A rotated output's duplication delivers pixels in the rotated + * orientation, which the packer does not expect and composition would not + * correct. Refuse rather than produce a sideways display. */ + if (desc.Rotation != DXGI_MODE_ROTATION_IDENTITY && + desc.Rotation != DXGI_MODE_ROTATION_UNSPECIFIED) + { + DEBUG_WARN("Output %ls is rotated, not capturing all outputs", + desc.DeviceName); + goto fail; + } + + comRef_defineLocal(IDXGIOutput5, output5); + if (FAILED(IDXGIOutput_QueryInterface(*output, &IID_IDXGIOutput5, + (void **)output5))) + { + DEBUG_WARN("Output %ls has no IDXGIOutput5, not capturing all outputs", + desc.DeviceName); + goto fail; + } + + static const DXGI_FORMAT supportedFormats[] = + { + DXGI_FORMAT_B8G8R8A8_UNORM, + DXGI_FORMAT_R8G8B8A8_UNORM, + DXGI_FORMAT_R16G16B16A16_FLOAT + }; + + comRef_defineLocal(IDXGIOutputDuplication, dup); + HRESULT status = E_FAIL; + for(int i = 0; i < 2; ++i) + { + status = IDXGIOutput5_DuplicateOutput1(*output5, + (IUnknown *)*this->device, 0, + ARRAY_LENGTH(supportedFormats), supportedFormats, dup); + + if (SUCCEEDED(status)) + break; + + // as with the primary, access is denied until the desktop settles + if (status == E_ACCESSDENIED) + --i; + + Sleep(200); + } + + if (FAILED(status)) + { + DEBUG_WINERROR("Failed to duplicate a secondary output", status); + goto fail; + } + + /* Every display shares one atlas texture, and a plain subresource copy + * cannot convert between formats. Mixed formats occur when one monitor is + * HDR and another is not. */ + DXGI_OUTDUPL_DESC dupDesc; + IDXGIOutputDuplication_GetDesc(*dup, &dupDesc); + if (dupDesc.ModeDesc.Format != this->dxgiSrcFormat) + { + DEBUG_WARN("Output %ls captures as %s but the primary is %s, " + "not capturing all outputs", desc.DeviceName, + getDXGIFormatStr(dupDesc.ModeDesc.Format), + getDXGIFormatStr(this->dxgiSrcFormat)); + goto fail; + } + + memset(&info[count], 0, sizeof(info[count])); + info[count].id = count; + info[count].x = desc.DesktopCoordinates.left; + info[count].y = desc.DesktopCoordinates.top; + info[count].width = desc.DesktopCoordinates.right - + desc.DesktopCoordinates.left; + info[count].height = desc.DesktopCoordinates.bottom - + desc.DesktopCoordinates.top; + info[count].rotation = FRAME_ROT_0; + wcstombs(info[count].name, desc.DeviceName, sizeof(info[count].name) - 1); + + comRef_toGlobal(this->extraDup [count], dup ); + comRef_toGlobal(this->extraOutput[count], output); + ++count; + } + + if (count < 2) + { + DEBUG_INFO("Only one output attached, capturing a single display"); + goto fail; + } + + unsigned atlasWidth, atlasHeight, desktopWidth, desktopHeight; + if (!lgDisplaysPack(info, count, 1, this->displays, + &atlasWidth, &atlasHeight, &desktopWidth, &desktopHeight)) + { + DEBUG_WARN("Failed to pack %u outputs, capturing a single display", count); + goto fail; + } + + // the atlas stands in for the acquired texture, so it needs the same uses + D3D11_TEXTURE2D_DESC atlasDesc = + { + .Width = atlasWidth, + .Height = atlasHeight, + .MipLevels = 1, + .ArraySize = 1, + .Format = this->dxgiSrcFormat, + .SampleDesc.Count = 1, + .SampleDesc.Quality = 0, + .Usage = D3D11_USAGE_DEFAULT, + .BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET, + .CPUAccessFlags = 0, + .MiscFlags = 0 + }; + + comRef_defineLocal(ID3D11Texture2D, atlas); + HRESULT status = ID3D11Device_CreateTexture2D(*this->device, &atlasDesc, + NULL, atlas); + if (FAILED(status)) + { + DEBUG_WINERROR("Failed to create the atlas texture", status); + goto fail; + } + + comRef_toGlobal(this->atlasTex, atlas); + + this->displayCount = count; + this->atlasWidth = atlasWidth; + this->atlasHeight = atlasHeight; + this->desktopWidth = desktopWidth; + this->desktopHeight = desktopHeight; + + /* The rest of the pipeline treats the atlas as the frame, so the capture + * dimensions become the atlas dimensions. The virtual desktop size is + * reported separately as the screen size. */ + this->width = atlasWidth; + this->height = atlasHeight; + + DEBUG_INFO("Multi-monitor : %u outputs, desktop %ux%u, atlas %ux%u", + count, desktopWidth, desktopHeight, atlasWidth, atlasHeight); + for(unsigned i = 0; i < count; ++i) + { + const KVMFRDisplay * d = &this->displays[i]; + DEBUG_INFO(" %-20s %ux%u desktop +%d+%d -> atlas +%u+%u%s", + d->name, d->desktopWidth, d->desktopHeight, + d->desktopX, d->desktopY, d->atlasX, d->atlasY, + (d->flags & KVMFR_DISPLAY_FLAG_PRIMARY) ? " (primary)" : ""); + } + + comRef_scopePop(); + return true; + +fail: + // drop any secondary duplication already created and fall back to one output + for(unsigned i = 1; i < count; ++i) + { + comRef_release(this->extraDup [i]); + comRef_release(this->extraOutput[i]); + } + this->displayCount = 0; + comRef_scopePop(); + return false; +} + static const char * dxgi_getName(void) { if (!this) @@ -265,6 +526,13 @@ static void dxgi_initOptions(void) .type = OPTION_TYPE_STRING, .value.x_string = NULL }, + { + .module = "dxgi", + .name = "multiMonitor", + .description = "Capture all of the adapter's outputs into one frame", + .type = OPTION_TYPE_BOOL, + .value.x_bool = true + }, { .module = "dxgi", .name = "output", @@ -347,6 +615,7 @@ static bool dxgi_create( this->maxTextures = 1; this->debug = option_get_bool("dxgi", "debug"); + this->multiMonitor = option_get_bool("dxgi", "multiMonitor"); this->useAcquireLock = option_get_bool("dxgi", "useAcquireLock"); this->allowRGB24 = option_get_bool("dxgi", "allowRGB24"); this->dwmFlush = option_get_bool("dxgi", "dwmFlush"); @@ -835,6 +1104,8 @@ static bool dxgi_init(void * ivshmemBase, unsigned * alignSize) this->hdr = this->dxgiColorSpace == DXGI_COLOR_SPACE_RGB_FULL_G2084_NONE_P2020; } + dxgi_initMultiMonitor(); + // set the initial format switch(this->dxgiFormat) { @@ -970,6 +1241,12 @@ static bool dxgi_deinit(void) // this MUST run before backend->free() & ppFreeAll. comRef_freeScope(&this->comScope); + /* The scope owned the secondary duplications and the atlas texture, so + * nothing multi-monitor is valid any more. Clear it so a later release can + * not walk pointers the scope has already freed. */ + this->displayCount = 0; + memset(this->extraNeedsRelease, 0, sizeof(this->extraNeedsRelease)); + ppFreeAll(); if (this->backend) { @@ -1115,6 +1392,88 @@ static void computeTexDamage(Texture * tex) } } +/* Copy every display into the atlas texture. + * + * The primary's texture has already been acquired by the caller. Each + * secondary is polled with a zero timeout: outputs present independently, so + * waiting on each in turn would drag the whole frame rate down to that of the + * slowest monitor. A display with nothing new simply keeps the pixels it + * already has in the atlas, which is exactly right since it has not changed. + * + * Acquired frames are deliberately not released here. The copies are recorded + * into the device context and are not necessarily complete on return, and + * releasing a frame invalidates its surface. They are released at the start of + * the next capture, which is the same discipline the primary already uses. */ +static CaptureResult dxgi_compositeDisplays(ID3D11Texture2D * primary) +{ + CaptureResult result = CAPTURE_RESULT_OK; + comRef_scopePush(KVMFR_MAX_DISPLAYS * 2 + 4); + + const KVMFRDisplay * pd = &this->displays[0]; + LG_LOCK(this->deviceContextLock); + ID3D11DeviceContext_CopySubresourceRegion(*this->deviceContext, + (ID3D11Resource *)*this->atlasTex, 0, pd->atlasX, pd->atlasY, 0, + (ID3D11Resource *)primary, 0, NULL); + LG_UNLOCK(this->deviceContextLock); + + for(unsigned i = 1; i < this->displayCount; ++i) + { + DXGI_OUTDUPL_FRAME_INFO info; + comRef_defineLocal(IDXGIResource, res); + HRESULT status; + + if (this->useAcquireLock) + { + LOCKED({ + status = IDXGIOutputDuplication_AcquireNextFrame( + *this->extraDup[i], 0, &info, res); + }); + } + else + status = IDXGIOutputDuplication_AcquireNextFrame( + *this->extraDup[i], 0, &info, res); + + // nothing new on this output, so its atlas region is already correct + if (status == DXGI_ERROR_WAIT_TIMEOUT) + continue; + + result = dxgi_hResultToCaptureResult(status); + if (result != CAPTURE_RESULT_OK) + { + if (result == CAPTURE_RESULT_ERROR) + DEBUG_WINERROR("AcquireNextFrame failed on a secondary output", status); + goto exit; + } + + this->extraNeedsRelease[i] = true; + + // the output reported only a pointer change, not new desktop content + if (info.LastPresentTime.QuadPart == 0) + continue; + + comRef_defineLocal(ID3D11Texture2D, tex); + if (FAILED(status = IDXGIResource_QueryInterface(*res, + &IID_ID3D11Texture2D, (void **)tex))) + { + DEBUG_WINERROR("Failed to get the texture from a secondary output", + status); + result = CAPTURE_RESULT_ERROR; + goto exit; + } + + const KVMFRDisplay * d = &this->displays[i]; + LG_LOCK(this->deviceContextLock); + ID3D11DeviceContext_CopySubresourceRegion(*this->deviceContext, + (ID3D11Resource *)*this->atlasTex, 0, d->atlasX, d->atlasY, 0, + (ID3D11Resource *)*tex, 0, NULL); + LG_UNLOCK(this->deviceContextLock); + } + +exit: + comRef_scopePop(); + return result; +} + static CaptureResult dxgi_capture(unsigned frameBufferIndex, FrameBuffer * frameBuffer) { @@ -1211,6 +1570,18 @@ static CaptureResult dxgi_capture(unsigned frameBufferIndex, { if (copyFrame) { + /* With several displays the frame is the atlas, not the acquired + * texture, so compose first and hand the atlas to the post processors. + * Everything downstream is unaware of the difference. */ + ID3D11Texture2D * source = *src; + if (this->displayCount) + { + result = dxgi_compositeDisplays(*src); + if (result != CAPTURE_RESULT_OK) + goto exit; + source = *this->atlasTex; + } + // run any postprocessors int width = this->width; int height = this->height; @@ -1218,7 +1589,7 @@ static CaptureResult dxgi_capture(unsigned frameBufferIndex, int rows = this->height; CaptureFormat format = this->format; ID3D11Texture2D *dst = ppRun( - tex, *src, &width, &height, &cols, &rows, &format); + tex, source, &width, &height, &cols, &rows, &format); if (!this->backendConfigured) { @@ -1477,8 +1848,12 @@ static CaptureResult dxgi_waitFrame(unsigned frameBufferIndex, const unsigned int maxRows = maxFrameSize / this->pitch; frame->formatVer = tex->formatVer; - frame->screenWidth = this->width; - frame->screenHeight = this->height; + /* With several displays the screen size describes the guest's virtual + * desktop, while the frame itself is the packed atlas. */ + frame->screenWidth = this->displayCount ? + this->desktopWidth : this->width; + frame->screenHeight = this->displayCount ? + this->desktopHeight : this->height; frame->dataWidth = this->dataWidth; frame->dataHeight = min(maxRows, this->dataHeight); frame->frameWidth = this->outputWidth; @@ -1495,9 +1870,23 @@ static CaptureResult dxgi_waitFrame(unsigned frameBufferIndex, dxgi_setHDRMetadata(frame, &this->outputDesc); frame->rotation = this->rotation; - frame->damageRectsCount = tex->damageRectsCount; - memcpy(frame->damageRects, tex->damageRects, - tex->damageRectsCount * sizeof(*tex->damageRects)); + frame->displayCount = this->displayCount; + memcpy(frame->displays, this->displays, + this->displayCount * sizeof(*this->displays)); + + /* Damage rectangles come from the primary output alone and are in its own + * coordinates, so they say nothing about what changed on the other + * displays. Report full damage instead until per output damage is tracked + * and translated into atlas space; an incorrect damage rectangle leaves + * stale pixels on screen, which is far worse than redrawing too much. */ + if (this->displayCount) + frame->damageRectsCount = 0; + else + { + frame->damageRectsCount = tex->damageRectsCount; + memcpy(frame->damageRects, tex->damageRects, + tex->damageRectsCount * sizeof(*tex->damageRects)); + } atomic_fetch_sub_explicit(&this->texReady, 1, memory_order_release); return CAPTURE_RESULT_OK; @@ -1586,6 +1975,37 @@ static CaptureResult dxgi_getFrame(unsigned frameBufferIndex, static CaptureResult dxgi_releaseFrame(void) { DEBUG_ASSERT(this); + + /* Release any secondary output frames held since the last capture. This is + * done first and unconditionally: they are acquired independently of the + * primary, so they can be outstanding even when it is not, and a duplication + * that is never released stops delivering frames. */ + for(unsigned i = 1; i < this->displayCount; ++i) + { + if (!this->extraNeedsRelease[i]) + continue; + + this->extraNeedsRelease[i] = false; + + HRESULT status; + LOCKED({status = IDXGIOutputDuplication_ReleaseFrame(*this->extraDup[i]);}); + + switch(status) + { + case S_OK: + case DXGI_ERROR_INVALID_CALL: // already released, nothing to do + break; + + case WAIT_ABANDONED: + case DXGI_ERROR_ACCESS_LOST: + return CAPTURE_RESULT_REINIT; + + default: + DEBUG_WINERROR("ReleaseFrame failed on a secondary output", status); + return CAPTURE_RESULT_ERROR; + } + } + if (!this->needsRelease) return CAPTURE_RESULT_OK;