diff --git a/doc/multimonitor.rst b/doc/multimonitor.rst index 88aed1dd..5403715c 100644 --- a/doc/multimonitor.rst +++ b/doc/multimonitor.rst @@ -42,13 +42,33 @@ it. To turn it off and capture only as a single display: .. code:: text + dxgi:multiMonitor=no xcb:multiMonitor=no +Supported backends: + +* ``DXGI`` (Windows) duplicates every attached output on the chosen adapter + and composes them on the GPU. It falls back to capturing a single display + if the outputs cannot be combined safely — if their capture formats differ, + which happens when one monitor is HDR and another is not, or if any output + is rotated. The reason is always logged. +* ``XCB`` (Linux) enumerates RandR monitors and packs them. + +Backends that do not implement it capture a single display exactly as before. + +.. warning:: + + The DXGI implementation has not yet been run on real multi-monitor + hardware. If you hit trouble, set ``dxgi:multiMonitor=no`` to confirm the + fault lies there, and see ``MULTIMONITOR.md`` beside the DXGI source for + what each symptom points at. + .. note:: - Support depends on the capture backend. The Linux ``XCB`` backend - enumerates RandR monitors and packs them. Backends that do not implement - it simply capture a single display as before. + All displays are sent as one frame, so they share a single frame rate. + Monitors of differing refresh rates cannot update independently: the + fastest is limited by the frame rate the host achieves for the whole + atlas. .. _multimonitor_shm_size: diff --git a/host/platform/Windows/capture/DXGI/MULTIMONITOR.md b/host/platform/Windows/capture/DXGI/MULTIMONITOR.md index e296d137..fd6c1805 100644 --- a/host/platform/Windows/capture/DXGI/MULTIMONITOR.md +++ b/host/platform/Windows/capture/DXGI/MULTIMONITOR.md @@ -1,100 +1,97 @@ # 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 backend duplicates every attached output on the chosen adapter and +composes them into a single atlas frame. It is on by default and can be +turned off with: -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. +``` +dxgi:multiMonitor=no +``` -## What already exists +**This code has never run on hardware.** It cross compiles cleanly, which +catches type and API errors, but acquire, release and timing behaviour can +only be proven with a real multi-monitor Windows machine. Treat the first run +as a bring-up exercise, and see "If it misbehaves" below. -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`: +## How it works -- `displayCount` — how many displays are packed -- `displays[]` — the layout, produced by `lgDisplaysPack()` -- `screenWidth` / `screenHeight` — the guest's *virtual desktop* size, while - `frameWidth` / `frameHeight` stay the size of the packed frame +`dxgi_initMultiMonitor()` runs once the primary duplication exists and the +capture format is known, but before the copy backend is created, since it +changes the frame dimensions to those of the atlas. -`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. +1. Enumerates the adapter's attached outputs and duplicates each one. The + primary keeps `this->dup`; the rest live in `this->extraDup[]`. +2. Builds an `LGDisplayInfo` per output from `DesktopCoordinates`, which is + already in virtual desktop space, and packs them with `lgDisplaysPack()`. +3. Creates an atlas `ID3D11Texture2D` of the packed size and sets + `this->width` / `this->height` to it. -## Shape of the change +Each capture composes into that atlas with `CopySubresourceRegion` and hands +it to `ppRun()` in place of the acquired texture, so the post processors, the +copy backend and everything past them are unaware of the difference. +`dxgi_waitFrame` then reports the layout, with the virtual desktop as the +screen size and the atlas as the frame. -1. **Enumerate every attached output**, not just the first match. The current - loop in `dxgi_init` breaks out of `IDXGIAdapter1_EnumOutputs` as soon as it - finds an output with `AttachedToDesktop`. Collect them all instead, and - build an `LGDisplayInfo` per output from - `DXGI_OUTPUT_DESC::DesktopCoordinates`, which is already in virtual desktop - space. +## Deliberate choices -2. **Duplicate each output.** `IDXGIOutput5_DuplicateOutput1` is per output, - so this becomes an array of `IDXGIOutputDuplication`. Note the existing - retry loop that treats `E_ACCESSDENIED` as "keep trying" — each output - needs the same treatment, and a secure desktop transition invalidates all - of them at once. +**Secondary outputs are polled, not waited on.** Outputs present +independently, so `AcquireNextFrame` is called with a zero timeout for each +secondary; waiting on each in turn would drag the frame rate down to that of +the slowest monitor. An output with nothing new keeps the pixels already in +its atlas region, which is correct because it has not changed. -3. **Composite into an atlas texture.** Create one `ID3D11Texture2D` of the - packed size and, for each output that produced a frame, - `ID3D11DeviceContext_CopySubresourceRegion` its texture into its atlas - rectangle. Pass that atlas texture to `ppRun()` in place of `*src`. The - post processors and the copy backend then see a single larger texture and - need no changes. +**Frames are released late.** Acquired frames are released at the *start* of +the next capture, not after their copy. `CopySubresourceRegion` only records +into the device context, and releasing a frame invalidates its surface. This +mirrors what the primary output already did. -4. **Report the layout** from `dxgi_waitFrame`. +**Anything unsafe falls back to a single output.** That is not an error path, +it is the design: a wrong atlas is worse than one working display. It happens +when -## Hazards +- capture formats differ between outputs, since a subresource copy cannot + convert, which occurs when one monitor is HDR and another is not +- any output is rotated, since the packer expects pixels in their final + orientation +- `dxgi:output` names a specific output +- fewer than two outputs are attached -These are the parts that make it more than a mechanical change. +## Known gaps -**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. +**Damage is reported as full frame while composing.** `GetFrameDirtyRects` +returns rectangles for the primary alone, in its own coordinates, so they say +nothing about the other displays. Reporting them unchanged would leave stale +pixels on screen. Fixing this means collecting dirty rectangles per output, +translating each into atlas space, and falling back to full damage when the +combined count exceeds `KVMFR_MAX_DAMAGE_RECTS`. This is the single biggest +performance win available here. **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. +`displayPathInfo` describe the primary only, and the frame format carries one +set of HDR static metadata. Mixed HDR and SDR monitors are refused rather than +guessed at. -**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. +**Rotation is refused rather than handled.** `KVMFRDisplay::rotation` exists +for it, but composition would have to rotate during the copy, which +`CopySubresourceRegion` cannot do — it needs a shader pass. -**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. +## If it misbehaves -**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. +Start with `dxgi:multiMonitor=no` to confirm the fault is in this path. -**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. +- **Only one display, and the log says so** — read the reason it logged; it + will name the precondition that failed. +- **One display is frozen while others update** — a `ReleaseFrame` is being + missed for that output, so its duplication stopped delivering. Look at + `extraNeedsRelease` handling in `dxgi_releaseFrame`. +- **Torn or interleaved pixels** — a copy is racing the release. Check that + frames are still held until the next capture. +- **A display shows stale content** — expected for a display that has not + presented; suspicious if it never updates. ## Testing -The Windows host cross-compiles from Linux, which catches type and API errors -without a Windows machine: +Cross compile from Linux to type check without a Windows machine: ```bash cd host && mkdir build-win && cd build-win @@ -102,10 +99,8 @@ 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. +and checks the published layout: every display inside the frame, no overlaps, +packed without scaling. It is backend agnostic, so pointing it at a Windows +host over the same shared memory is the fastest way to confirm this backend's +output is well formed.