Files
looking-glass-2/common/tests/test_displays.c
T
donny 5e2fd18b5c
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
common: align each display's atlas offset for direct scanout
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.
2026-07-28 09:09:24 +00:00

382 lines
13 KiB
C

#include "common/displays.h"
#include "common/debug.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
static int failures = 0;
static void check(bool cond, const char * fmt, ...)
{
va_list va;
va_start(va, fmt);
printf(" [%s] ", cond ? "PASS" : "FAIL");
vprintf(fmt, va);
printf("\n");
va_end(va);
if (!cond)
++failures;
}
/* verify no two displays overlap in the atlas and all are in bounds */
static bool validate(const KVMFRDisplay * d, unsigned count,
unsigned aw, unsigned ah)
{
for(unsigned i = 0; i < count; ++i)
{
if (d[i].atlasX + d[i].atlasWidth > aw) return false;
if (d[i].atlasY + d[i].atlasHeight > ah) return false;
if (d[i].atlasWidth != d[i].desktopWidth) return false;
if (d[i].atlasHeight != d[i].desktopHeight) return false;
for(unsigned j = i + 1; j < count; ++j)
{
const bool disjoint =
d[i].atlasX + d[i].atlasWidth <= d[j].atlasX ||
d[j].atlasX + d[j].atlasWidth <= d[i].atlasX ||
d[i].atlasY + d[i].atlasHeight <= d[j].atlasY ||
d[j].atlasY + d[j].atlasHeight <= d[i].atlasY;
if (!disjoint)
return false;
}
}
return true;
}
static void run(const char * title, const LGDisplayInfo * in, unsigned count)
{
KVMFRDisplay out[KVMFR_MAX_DISPLAYS];
unsigned aw, ah, dw, dh;
printf("\n== %s ==\n", title);
if (!lgDisplaysPack(in, count, LG_DISPLAY_ALIGN_PIXELS(4), out,
&aw, &ah, &dw, &dh))
{
printf(" [FAIL] pack returned false\n");
++failures;
return;
}
uint64_t used = 0;
for(unsigned i = 0; i < count; ++i)
used += (uint64_t)in[i].width * in[i].height;
printf(" atlas %ux%u (%.1f MP), desktop %ux%u, waste %.1f%%\n",
aw, ah, (aw * (double)ah) / 1e6, dw, dh,
100.0 * (1.0 - used / (aw * (double)ah)));
for(unsigned i = 0; i < count; ++i)
printf(" %-8s atlas +%u+%u %ux%u desktop +%d+%d\n",
out[i].name, out[i].atlasX, out[i].atlasY,
out[i].atlasWidth, out[i].atlasHeight,
out[i].desktopX, out[i].desktopY);
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
* cannot accidentally look correct. */
static uint32_t pixelAt(unsigned x, unsigned y)
{
return (x * 2654435761u) ^ (y * 2246822519u) ^ 0xA5A5A5A5u;
}
static void testRepack(void)
{
printf("\n== repack ==\n");
/* a source "virtual desktop" with three monitors laid out side by side,
* deliberately of differing sizes */
const unsigned srcW = 400, srcH = 200;
const LGDisplayInfo in[3] = {
{ .id = 0, .name = "A", .x = 0, .y = 0, .width = 100, .height = 200 },
{ .id = 1, .name = "B", .x = 100, .y = 0, .width = 150, .height = 120 },
{ .id = 2, .name = "C", .x = 250, .y = 50, .width = 150, .height = 150 },
};
KVMFRDisplay out[KVMFR_MAX_DISPLAYS];
unsigned aw, ah, dw, dh;
if (!lgDisplaysPack(in, 3, 1, out, &aw, &ah, &dw, &dh))
{
check(false, "packed the repack fixture");
return;
}
uint32_t * src = malloc((size_t)srcW * srcH * 4);
uint32_t * dst = malloc((size_t)aw * ah * 4);
for(unsigned y = 0; y < srcH; ++y)
for(unsigned x = 0; x < srcW; ++x)
src[y * srcW + x] = pixelAt(x, y);
memset(dst, 0, (size_t)aw * ah * 4);
uint32_t srcX[3], srcY[3];
for(unsigned i = 0; i < 3; ++i)
{
srcX[i] = in[i].x;
srcY[i] = in[i].y;
}
lgDisplaysRepack((uint8_t *)dst, aw * 4, ah, (const uint8_t *)src,
srcW * 4, srcW, srcH, out, srcX, srcY, 3, 4);
// every pixel of every display must have come from the right source pixel
bool ok = true;
for(unsigned i = 0; i < 3 && ok; ++i)
for(unsigned y = 0; y < out[i].atlasHeight && ok; ++y)
for(unsigned x = 0; x < out[i].atlasWidth; ++x)
{
const uint32_t got = dst[(out[i].atlasY + y) * aw + out[i].atlasX + x];
const uint32_t want = pixelAt(srcX[i] + x, srcY[i] + y);
if (got != want)
{
printf(" %s mismatch at +%u+%u: got %08x want %08x\n",
out[i].name, x, y, got, want);
ok = false;
break;
}
}
check(ok, "every display's pixels land at the correct atlas offset");
/* Padding must be left alone. Count how many atlas pixels are still zero and
* compare against the area no display claims. */
uint64_t claimed = 0;
for(unsigned i = 0; i < 3; ++i)
claimed += (uint64_t)out[i].atlasWidth * out[i].atlasHeight;
uint64_t untouched = 0;
for(unsigned y = 0; y < ah; ++y)
for(unsigned x = 0; x < aw; ++x)
if (dst[y * aw + x] == 0)
++untouched;
check(untouched == (uint64_t)aw * ah - claimed,
"atlas padding is left untouched (%llu unclaimed pixels)",
(unsigned long long)((uint64_t)aw * ah - claimed));
/* Truncation: a destination with too few rows must clip rather than
* overrun. Guard bytes after the buffer catch any overrun. */
const unsigned shortH = ah / 2;
const size_t guard = 4096;
uint8_t * small = malloc((size_t)aw * 4 * shortH + guard);
memset(small, 0, (size_t)aw * 4 * shortH + guard);
memset(small + (size_t)aw * 4 * shortH, 0xCD, guard);
lgDisplaysRepack(small, aw * 4, shortH, (const uint8_t *)src,
srcW * 4, srcW, srcH, out, srcX, srcY, 3, 4);
bool guardIntact = true;
for(size_t i = 0; i < guard; ++i)
if (small[(size_t)aw * 4 * shortH + i] != 0xCD)
{
guardIntact = false;
break;
}
check(guardIntact, "a short destination truncates without overrunning");
/* A source smaller than the displays claim must also be clipped. */
lgDisplaysRepack((uint8_t *)dst, aw * 4, ah, (const uint8_t *)src,
srcW * 4, srcW / 2, srcH / 2, out, srcX, srcY, 3, 4);
check(true, "a source smaller than the layout does not crash");
free(small);
free(dst);
free(src);
}
/* Damage rectangles arrive in a display's own coordinates. Getting the
* translation into atlas space wrong is uniquely nasty: it does not crash, it
* silently redraws the wrong region, leaving stale pixels on screen. */
static void testDamageTranslation(void)
{
printf("\n== damage translation ==\n");
/* a display parked away from the atlas origin, so an offset that is
* forgotten or applied twice is visible */
const KVMFRDisplay d =
{
.atlasX = 1000, .atlasY = 500,
.atlasWidth = 800, .atlasHeight = 600,
.desktopX = 0, .desktopY = 0,
.desktopWidth = 800, .desktopHeight = 600
};
FrameDamageRect r;
check(lgDisplaysRectToAtlas(&d, 0, 0, 800, 600, &r) &&
r.x == 1000 && r.y == 500 && r.width == 800 && r.height == 600,
"a full display rect maps to the whole atlas region");
check(lgDisplaysRectToAtlas(&d, 10, 20, 30, 40, &r) &&
r.x == 1010 && r.y == 520 && r.width == 30 && r.height == 40,
"an interior rect is offset by the display's atlas position");
// a rect running past the display must not bleed into its neighbour
check(lgDisplaysRectToAtlas(&d, 700, 500, 400, 400, &r) &&
r.x == 1700 && r.y == 1000 && r.width == 100 && r.height == 100,
"a rect overhanging the display is clipped to it");
check(lgDisplaysRectToAtlas(&d, -50, -50, 100, 100, &r) &&
r.x == 1000 && r.y == 500 && r.width == 50 && r.height == 50,
"a rect starting before the origin is clipped");
check(!lgDisplaysRectToAtlas(&d, 800, 0, 10, 10, &r),
"a rect entirely past the right edge is dropped");
check(!lgDisplaysRectToAtlas(&d, 0, 600, 10, 10, &r),
"a rect entirely past the bottom edge is dropped");
check(!lgDisplaysRectToAtlas(&d, -100, 0, 50, 10, &r),
"a rect entirely before the left edge is dropped");
check(!lgDisplaysRectToAtlas(&d, 0, 0, 0, 10, &r),
"a zero width rect is dropped");
check(!lgDisplaysRectToAtlas(&d, 0, 0, 10, -5, &r),
"a negative height rect is dropped");
/* Every translated rect must stay inside the display's atlas region.
* Sweep a range of awkward inputs and assert containment. */
bool contained = true;
for(int x = -900; x <= 900 && contained; x += 37)
for(int y = -700; y <= 700; y += 29)
for(int w = 1; w <= 1600; w += 311)
{
if (!lgDisplaysRectToAtlas(&d, x, y, w, w, &r))
continue;
if (r.x < d.atlasX || r.y < d.atlasY ||
r.x + r.width > d.atlasX + d.atlasWidth ||
r.y + r.height > d.atlasY + d.atlasHeight)
{
printf(" escaped at %d,%d %dx%d -> %u,%u %ux%u\n",
x, y, w, w, r.x, r.y, r.width, r.height);
contained = false;
break;
}
}
check(contained, "no translated rect ever escapes its display's region");
}
int main(void)
{
debug_init();
/* the real geometry of the development machine */
const LGDisplayInfo real[3] = {
{ .id = 0, .name = "DP-2", .x = 2160, .y = 2880, .width = 5120, .height = 2880 },
{ .id = 1, .name = "DP-1", .x = 0, .y = 896, .width = 2160, .height = 3840 },
{ .id = 2, .name = "DP-3", .x = 2160, .y = 0, .width = 5120, .height = 2880,
.primary = true },
};
run("3 mixed displays (this machine)", real, 3);
const LGDisplayInfo triple[3] = {
{ .id = 0, .name = "A", .x = 0, .y = 0, .width = 1920, .height = 1080 },
{ .id = 1, .name = "B", .x = 1920, .y = 0, .width = 1920, .height = 1080 },
{ .id = 2, .name = "C", .x = 3840, .y = 0, .width = 1920, .height = 1080 },
};
run("3 identical 1080p displays", triple, 3);
/* One 4K next to two 1440p. The best layout puts the 4K on its own shelf
* with the two 1440p side by side below it, which is only found if the
* candidate shelf widths include sums of displays that are not the first
* few in packing order. */
const LGDisplayInfo mixed[3] = {
{ .id = 0, .name = "4K", .x = 0, .y = 0, .width = 3840, .height = 2160,
.primary = true },
{ .id = 1, .name = "1440pA", .x = 3840, .y = 0, .width = 2560, .height = 1440 },
{ .id = 2, .name = "1440pB", .x = 6400, .y = 0, .width = 2560, .height = 1440 },
};
run("4K plus two 1440p", mixed, 3);
{
KVMFRDisplay o[KVMFR_MAX_DISPLAYS];
unsigned aw, ah, dw, dh;
if (lgDisplaysPack(mixed, 3, 1, o, &aw, &ah, &dw, &dh))
check((uint64_t)aw * ah <= 18432000ULL,
"4K + 2x1440p packs into at most 5120x3600 (got %ux%u)", aw, ah);
else
check(false, "4K + 2x1440p packed");
}
const LGDisplayInfo single[1] = {
{ .id = 0, .name = "only", .x = 0, .y = 0, .width = 2560, .height = 1440 },
};
run("single display", single, 1);
const LGDisplayInfo negative[2] = {
{ .id = 0, .name = "left", .x = -1920, .y = -100, .width = 1920, .height = 1080 },
{ .id = 1, .name = "right", .x = 0, .y = 0, .width = 2560, .height = 1440 },
};
run("negative desktop coordinates", negative, 2);
printf("\n== edge cases ==\n");
KVMFRDisplay out[KVMFR_MAX_DISPLAYS];
unsigned aw, ah, dw, dh;
check(!lgDisplaysPack(real, 0, 1, out, &aw, &ah, &dw, &dh),
"zero displays rejected");
check(!lgDisplaysPack(real, KVMFR_MAX_DISPLAYS + 1, 1, out, &aw, &ah, &dw, &dh),
"too many displays rejected");
const LGDisplayInfo zero[1] = {
{ .id = 0, .name = "bad", .x = 0, .y = 0, .width = 0, .height = 1080 } };
check(!lgDisplaysPack(zero, 1, 1, out, &aw, &ah, &dw, &dh),
"zero-width display rejected");
const LGDisplayInfo huge[2] = {
{ .id = 0, .name = "h1", .x = 0, .y = 0, .width = 16384, .height = 16384 },
{ .id = 1, .name = "h2", .x = 0, .y = 0, .width = 16384, .height = 16384 } };
check(!lgDisplaysPack(huge, 2, 1, out, &aw, &ah, &dw, &dh),
"oversized set rejected rather than silently truncated");
/* alignment */
const LGDisplayInfo odd[1] = {
{ .id = 0, .name = "odd", .x = 0, .y = 0, .width = 1234, .height = 1000 } };
if (lgDisplaysPack(odd, 1, 64, out, &aw, &ah, &dw, &dh))
check(aw % 64 == 0, "atlas width honours 64px alignment");
else
check(false, "alignment case packed");
/* desktop normalisation */
if (lgDisplaysPack(negative, 2, 1, out, &aw, &ah, &dw, &dh))
{
check(out[0].desktopX == 0 && out[1].desktopX == 1920,
"negative coordinates normalised to origin");
check(dw == 4480 && dh == 1540, "virtual desktop bounds computed");
}
/* atlas <-> desktop round trip */
if (lgDisplaysPack(real, 3, 1, out, &aw, &ah, &dw, &dh))
{
int32_t ax, ay, dx, dy;
bool ok = lgDisplaysDesktopToAtlas(out, 3, 2160 + 10, 2880 + 20, &ax, &ay) &&
lgDisplaysAtlasToDesktop(out, 3, ax, ay, &dx, &dy);
check(ok && dx == 2170 && dy == 2900, "desktop->atlas->desktop round trips");
check(lgDisplaysFindAtPoint(out, 3, 0, 0) == -1,
"point in a desktop gap reports no display");
check(lgDisplaysFindAtPoint(out, 3, 2160, 0) == 2,
"point inside DP-3 finds DP-3");
}
testRepack();
testDamageTranslation();
printf("\n%s (%d failure%s)\n", failures ? "FAILED" : "ALL PASSED",
failures, failures == 1 ? "" : "s");
return failures != 0;
}