The frame format, packer and client compositor are backend agnostic and already work; this backend still reports a single display. Records the shape of the change and the hazards found reading it: mixed formats, per-output HDR and rotation, frame timing, release discipline and COM lifetime.
5.2 KiB
Multi-monitor capture on DXGI
The multi-monitor frame format, the atlas packer and the client side
compositor are all implemented and in use by the Linux XCB backend. This
backend does not yet produce multi-display frames: it reports
displayCount = 0, so the host publishes a conventional single display frame
and everything behaves as it always has.
This note records what adding it involves, and the specific hazards found while reading this backend, so the work does not have to start from scratch.
What already exists
Nothing in the frame format or the client needs changing. A backend opts in
purely by filling in three things on CaptureFrame in its waitFrame:
displayCount— how many displays are packeddisplays[]— the layout, produced bylgDisplaysPack()screenWidth/screenHeight— the guest's virtual desktop size, whileframeWidth/frameHeightstay the size of the packed frame
lgDisplaysPack() (in common/src/displays.c) takes the displays and their
virtual desktop positions and chooses a layout that minimises the packed
area. lgDisplaysRepack() alongside it does the CPU copy, but a D3D backend
should composite on the GPU instead — see below.
Shape of the change
-
Enumerate every attached output, not just the first match. The current loop in
dxgi_initbreaks out ofIDXGIAdapter1_EnumOutputsas soon as it finds an output withAttachedToDesktop. Collect them all instead, and build anLGDisplayInfoper output fromDXGI_OUTPUT_DESC::DesktopCoordinates, which is already in virtual desktop space. -
Duplicate each output.
IDXGIOutput5_DuplicateOutput1is per output, so this becomes an array ofIDXGIOutputDuplication. Note the existing retry loop that treatsE_ACCESSDENIEDas "keep trying" — each output needs the same treatment, and a secure desktop transition invalidates all of them at once. -
Composite into an atlas texture. Create one
ID3D11Texture2Dof the packed size and, for each output that produced a frame,ID3D11DeviceContext_CopySubresourceRegionits texture into its atlas rectangle. Pass that atlas texture toppRun()in place of*src. The post processors and the copy backend then see a single larger texture and need no changes. -
Report the layout from
dxgi_waitFrame.
Hazards
These are the parts that make it more than a mechanical change.
Mixed formats. Each output negotiates its own format, and
CopySubresourceRegion cannot convert. An HDR monitor duplicating as
R16G16B16A16_FLOAT next to an SDR one as B8G8R8A8_UNORM cannot be copied
into a shared atlas directly. Either promote everything to FP16 with a shader
pass, or refuse multi-monitor when formats differ and fall back to a single
output. The latter is a reasonable first version, but detect it explicitly
rather than producing a corrupt atlas.
HDR metadata is per output. this->outputDesc, dxgiColorSpace and
displayPathInfo all describe one monitor, and the frame carries a single set
of HDR static metadata. With mixed monitors there is no single correct answer;
the frame format has no per-display HDR metadata today.
Rotation is per output. DXGI_MODE_ROTATION differs per output, while the
frame carries one rotation. KVMFRDisplay::rotation exists for this, but the
packer assumes pixels are already in their final orientation, so a rotated
output must be rotated during composition.
Frame timing. Outputs present independently. AcquireNextFrame blocks per
duplication, so the current 1000ms wait cannot simply be repeated for each
output or the frame rate collapses to the slowest monitor. Wait on the primary
and poll the others with a zero timeout, reusing the previous contents for any
output with nothing new.
Release discipline. Every successful AcquireNextFrame needs a matching
ReleaseFrame. dxgi_releaseFrame() currently tracks one needsRelease
flag; this has to become per output, and the error paths must release
everything already acquired or the duplication deadlocks.
COM lifetime. This file manages COM references through the comRef
helpers (comRef_defineLocal, comRef_toGlobal, comRef_scopePush). Arrays
of interfaces must be registered the same way or they leak on reinitialisation,
which happens on every resolution change and desktop switch.
Damage rectangles. GetFrameDirtyRects returns rectangles in each
output's own coordinates. They must be translated into atlas space, and the
combined count can exceed KVMFR_MAX_DAMAGE_RECTS, in which case fall back to
full frame damage.
Testing
The Windows host cross-compiles from Linux, which catches type and API errors without a Windows machine:
cd host && mkdir build-win && cd build-win
cmake -DCMAKE_TOOLCHAIN_FILE=../toolchain-mingw64.cmake ..
make
Compiling is not evidence that duplication, timing and release discipline are right; those need real hardware with more than one monitor attached.
host/tests/lg-verify-atlas.c subscribes to a running host as a client would
and checks the published layout for consistency — displays inside the frame,
no overlaps, packed without scaling — and dumps each display to a PPM. It is
backend agnostic and is the fastest way to confirm a new backend's output.