Files
looking-glass-2/common/tests/test_displays.c
T
donny faaf63bc93
build / client (Debug, map[cc:clang cxx:clang++], libdecor) (push) Failing after 20s
build / client (Debug, map[cc:clang cxx:clang++], xdg-shell) (push) Failing after 20s
build / client (Debug, map[cc:gcc cxx:g++], libdecor) (push) Failing after 20s
build / client (Debug, map[cc:gcc cxx:g++], xdg-shell) (push) Failing after 20s
build / client (Release, map[cc:clang cxx:clang++], libdecor) (push) Failing after 19s
build / client (Release, map[cc:clang cxx:clang++], xdg-shell) (push) Failing after 20s
build / module (push) Failing after 16s
build / client (Release, map[cc:gcc cxx:g++], libdecor) (push) Failing after 19s
build / client (Release, map[cc:gcc cxx:g++], xdg-shell) (push) Failing after 19s
build / host-linux (push) Failing after 18s
build / host-windows-cross (push) Failing after 18s
build / obs (clang) (push) Failing after 19s
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: make the atlas repack shared and testable
Moves the per-display copy out of the XCB backend into common so it can
be unit tested directly and reused by other backends. The test uses a
unique value per source pixel, so a copy landing at the wrong offset
cannot pass by coincidence, and checks truncation against guard bytes.

Also adds a scripted end-to-end test and teaches the verification tool
to report a blank capture source as a skip rather than a failure.
2026-07-28 07:31:13 +00:00

271 lines
9.1 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, 1, 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");
}
/* 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);
}
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);
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();
printf("\n%s (%d failure%s)\n", failures ? "FAILED" : "ALL PASSED",
failures, failures == 1 ? "" : "s");
return failures != 0;
}