diff --git a/docs/direct-scanout-followups.md b/docs/direct-scanout-followups.md new file mode 100644 index 00000000..d82f2fb8 --- /dev/null +++ b/docs/direct-scanout-followups.md @@ -0,0 +1,129 @@ +# Follow-ups for the direct-scanout consumer + +Notes from a review of the `multi-monitor` branch at `23b8236e`, written for +whoever picks this branch up next. + +`doc/` is the Sphinx user documentation and `doc/multimonitor.rst` holds the +consumer contract itself. This directory is for development handover notes — +things that are not user-facing but should not live only in chat history. + +Context: the downstream consumer is a Smithay-based Wayland compositor that +imports the shared memory as a dmabuf and page-flips each display's region of +the atlas independently. Both items below only matter because of that +consumer. + +The alignment work in `5e2fd18b` is correct, including the part that is easy +to get wrong: the subset-sum candidate shelf widths pad each display, so +candidates never describe a shelf too narrow to hold its set. Packing degrades +to a slightly roomier atlas rather than failing. No changes needed there. + +--- + +## 1. `LG_DISPLAY_ALIGN_PIXELS` is wrong for non-power-of-two `bpp` + +`common/include/common/displays.h`: + +```c +#define LG_DISPLAY_ALIGN_PIXELS(bpp) (LG_DISPLAY_SCANOUT_ALIGN_BYTES / (bpp)) +``` + +This is only correct when `bpp` is a power of two. For a 3 byte format it +yields 85, and `85 * 3` is **255** bytes — one short of the boundary it is +supposed to guarantee, with no error anywhere. + +Not reachable today: the DXGI call site clamps `bpp` to 4 or 8, and XCB passes +a literal 4. But this tree has RGB24 paths (`pp/rgb24.c`, `effect/rgb24.c`), +so it is one plumbing change away from silently producing misaligned offsets. +`doc/multimonitor.rst` already tells consumers the guarantee does not hold for +`FRAME_TYPE_RGB_24`, so the documentation is right and only the macro can lie. + +Divide by the largest power of two dividing `bpp` instead: + +```c +#define LG_DISPLAY_ALIGN_PIXELS(bpp) \ + (LG_DISPLAY_SCANOUT_ALIGN_BYTES / ((bpp) & (~(bpp) + 1))) +``` + +Which gives 64 for `bpp` 4, 32 for 8, 128 for 2, and 256 for 3 — correct in +every case, at the cost of more padding for formats that cannot be scanned out +anyway. A static assert that `bpp` is a power of two would work too, but it +pushes the problem onto the caller. + +Worth extending `common/tests/test_displays.c` with a 3 byte case asserting +`atlasX * bpp % 256 == 0`, since the existing alignment test passes today for +the wrong reason. + +--- + +## 2. Frame queue depth against a pinning consumer + +`doc/multimonitor.rst` already documents the two-deep queue and argues the +host blocking on a pinning consumer is desirable, "what stops stale frames +queueing up". The goal is right. The mechanism is worth reconsidering, because +queue depth and presentation policy are separate concerns and this conflates +them. + +### What the two-deep queue actually costs + +`LGMP_Q_FRAME_LEN` is 2 (`common/include/common/KVMFR.h:45`), and the host +busy-waits in `sendFrame` (`host/src/app.c:238`) while both slots are pending. + +A scanout consumer holds a buffer from flip submission until the *next* flip +completes, so between submitting a flip and vblank it holds both slots: + +1. Consumer pins A on the CRTC, takes B, submits a flip to B. +2. Both A and B are now held. `lgmpHostQueuePending == 2`, host blocks. +3. Vblank. Flip to B completes, A is released. +4. Only now can the host begin capturing into A. + +So capture cannot start until vblank, and the frame presented at vblank *N* +is one whose capture began after vblank *N-1*. Capture and scanout are +serialised rather than overlapped, which costs up to a full frame of latency +compared to a host free to work ahead. + +### Staleness is better handled at flip time + +The anti-staleness goal does not require starving the queue. A consumer that +always advances to the newest posted frame before flipping never presents a +stale one, however deep the queue is — the client already has +`lgmpClientAdvanceToLast` for exactly this. Depth controls whether the host +can *prepare* the next frame concurrently; the flip policy controls which +frame is *shown*. + +### The argument in the other direction + +Three buffers is not free. With the host able to run ahead, a guest rendering +well above the consumer's refresh rate will have frames captured and then +discarded — real work in the guest for pixels nobody sees. The two-deep queue +throttles capture to refresh as a side effect, which on a busy guest is a +genuine saving. + +So this is a latency-versus-guest-overhead tradeoff, not a defect. It should +be **measured on real hardware before being changed**, which cannot happen +until DXGI multi-monitor capture is running. + +### Suggested shape + +Make `LGMP_Q_FRAME_LEN` a build-time option rather than a fixed constant, +defaulting to 3 when the consumer scans out directly and 2 otherwise. Two +consequences travel with any change: + +* **Shm sizing.** `app.maxFrameSize = lgmpHostMemAvail(...) / LGMP_Q_FRAME_LEN` + (`host/src/app.c:888`), so each slot drops from a half to a third of the + region. The sizing guidance in `CLAUDE.md` and `doc/multimonitor.rst` goes + from `total pixels * 4 * 2` to `* 3`, rounded up to a power of two. On an + atlas holding several displays this is not a rounding error. + +* **Host and client must agree.** `client/src/main.c:646` sizes + `dmaInfo[LGMP_Q_FRAME_LEN]`. Bump `KVMFR_VERSION` (24 -> 25) alongside any + change so a mismatched pair refuses to connect instead of misbehaving + quietly. + +### What to measure + +With multi-monitor capture running on real hardware, compare guest-render to +photon latency at queue depth 2 and 3, holding everything else fixed, on a +guest rendering at and above the consumer's refresh rate. The expected result +is depth 3 winning by up to one refresh interval at the cost of additional +discarded captures on the fast guest. If the measured difference is inside the +noise, keep 2 — it is the simpler and cheaper of the two.