common: align each display's atlas offset for direct scanout
build / client (Debug, map[cc:clang cxx:clang++], libdecor) (push) Failing after 22s
build / client (Debug, map[cc:gcc cxx:g++], libdecor) (push) Failing after 21s
build / client (Debug, map[cc:clang cxx:clang++], xdg-shell) (push) Failing after 21s
build / client (Debug, map[cc:gcc cxx:g++], xdg-shell) (push) Failing after 21s
build / client (Release, map[cc:clang cxx:clang++], libdecor) (push) Failing after 21s
build / client (Release, map[cc:clang cxx:clang++], xdg-shell) (push) Failing after 21s
build / module (push) Failing after 18s
build / client (Release, map[cc:gcc cxx:g++], xdg-shell) (push) Failing after 21s
build / client (Release, map[cc:gcc cxx:g++], libdecor) (push) Failing after 21s
build / host-linux (push) Failing after 21s
build / host-windows-cross (push) Failing after 19s
build / obs (clang) (push) Failing after 20s
build / docs (push) Failing after 3s
build / obs (gcc) (push) Failing after 19s
build / host-windows-native (push) Canceled after 0s
build / idd (push) Canceled after 0s

The alignment argument only rounded up the atlas width, so a display's X
offset was an arbitrary running sum of the widths before it. A consumer
pointing display hardware at one display's region needs that region's base
address 256 byte aligned, and with unaligned offsets some displays would
land on legal addresses and some would not — giving the fast path to only
some monitors, which is worse than uniform slowness because the difference
is hard to diagnose.

Each display's X offset is now aligned as well, and the candidate shelf
widths account for the padding so they still describe shelves that can
hold the set. Both capture backends pass an alignment derived from their
pixel size.

Costs nothing for common resolutions: 3840, 2560 and 1920 are all
multiples of 64. Only unusual widths pad at all — a 2160 wide portrait
display grows the atlas by 16 pixels.

Also documents the consumer contract: the alignment guarantee, frame
lifetime under the two deep queue and one second subscriber timeout, and
that damage is in packed frame coordinates.
This commit is contained in:
donny committed 2026-07-28 09:09:24 +00:00
1 parent fc0c2cfc37
commit 5e2fd18b5c
6 files changed
+149 -7

No files matched your search

+22 -2
View File
@@ -35,6 +35,23 @@
* let the client reject anything its driver cannot honour. */
#define LG_DISPLAY_MAX_ATLAS_EDGE 16384
/* Display controllers generally require a scanout buffer's base address to be
* 256 byte aligned.
*
* Packing every display on a matching boundary lets a consumer point the
* display hardware straight at one display's region of the atlas — a DRM
* framebuffer whose base offset is `atlasY * pitch + atlasX * bpp` — instead
* of copying it out first. Getting this wrong is worse than it sounds: some
* displays land on legal offsets and some do not, so only some monitors take
* the fast path and the difference is awkward to diagnose.
*
* The cost is up to `alignment - 1` pixels of horizontal padding per display,
* which is nothing against what it buys. */
#define LG_DISPLAY_SCANOUT_ALIGN_BYTES 256
/* The pixel alignment satisfying the above for a given pixel size in bytes. */
#define LG_DISPLAY_ALIGN_PIXELS(bpp) (LG_DISPLAY_SCANOUT_ALIGN_BYTES / (bpp))
/* One guest display as reported by a capture backend, before packing. */
typedef struct LGDisplayInfo
{
@@ -62,8 +79,11 @@ LGDisplayInfo;
* KVMFR_MAX_DISPLAYS. Entries in `out` are written in the same order as `in`,
* so a backend can keep its own indexing.
*
* `alignment` rounds the atlas width up to a multiple of that many pixels, so
* a backend can satisfy a row-pitch requirement. Pass 1 for no alignment.
* `alignment` is a horizontal alignment in pixels, applied both to each
* display's X offset within the atlas and to the atlas width. Together those
* make every display's base address in the atlas a multiple of
* `alignment * bpp`, which is what direct scanout needs. Pass
* LG_DISPLAY_ALIGN_PIXELS(bytesPerPixel) for that, or 1 for none.
*
* Returns false if the inputs are invalid or no layout fits within
* LG_DISPLAY_MAX_ATLAS_EDGE.
+17 -2
View File
@@ -37,6 +37,7 @@ static bool packWithWidth(
const unsigned * order,
unsigned count,
unsigned width,
unsigned alignment,
unsigned * outX,
unsigned * outY,
unsigned * outHeight,
@@ -53,6 +54,12 @@ static bool packWithWidth(
if (w > width)
return false;
/* Advance to the next aligned column before testing whether the display
* fits, so a display never starts at an offset a scanout engine cannot
* address. Zero is always aligned, so a fresh shelf needs no padding. */
if (shelfX % alignment)
shelfX += alignment - (shelfX % alignment);
if (shelfX + w > width)
{
// no room left on this shelf, open a new one above it
@@ -149,7 +156,14 @@ bool lgDisplaysPack(
unsigned width = 0;
for(unsigned i = 0; i < count; ++i)
if (mask & (1u << i))
width += in[i].width;
{
// count the padding each display will need, or the candidate widths
// describe shelves that cannot actually hold the set
unsigned w = in[i].width;
if (w % alignment)
w += alignment - (w % alignment);
width += w;
}
if (width < maxWidth)
width = maxWidth;
@@ -180,7 +194,8 @@ bool lgDisplaysPack(
unsigned x[KVMFR_MAX_DISPLAYS], y[KVMFR_MAX_DISPLAYS];
unsigned height, usedWidth;
if (!packWithWidth(in, order, count, width, x, y, &height, &usedWidth))
if (!packWithWidth(in, order, count, width, alignment, x, y, &height,
&usedWidth))
continue;
// align the width only after tightening, so alignment is applied once
+20 -1
View File
@@ -50,7 +50,8 @@ static void run(const char * title, const LGDisplayInfo * in, unsigned count)
unsigned aw, ah, dw, dh;
printf("\n== %s ==\n", title);
if (!lgDisplaysPack(in, count, 1, out, &aw, &ah, &dw, &dh))
if (!lgDisplaysPack(in, count, LG_DISPLAY_ALIGN_PIXELS(4), out,
&aw, &ah, &dw, &dh))
{
printf(" [FAIL] pack returned false\n");
++failures;
@@ -72,6 +73,24 @@ static void run(const char * title, const LGDisplayInfo * in, unsigned count)
check(validate(out, count, aw, ah), "layout is non-overlapping and in bounds");
check((uint64_t)aw * ah >= used, "atlas is at least the sum of display areas");
/* Every display must start on an address the display hardware can scan out
* from, or a consumer gets the fast path on some monitors and not others. */
bool aligned = true;
const unsigned align = LG_DISPLAY_ALIGN_PIXELS(4);
for(unsigned i = 0; i < count; ++i)
if (out[i].atlasX % align)
{
printf(" %s atlasX %u is not a multiple of %u\n",
out[i].name, out[i].atlasX, align);
aligned = false;
}
if (aw % align)
{
printf(" atlas width %u is not a multiple of %u\n", aw, align);
aligned = false;
}
check(aligned, "every display is scanout aligned at 32bpp");
}
/* A value unique to each source pixel, so a copy landing at the wrong offset