common: make the atlas repack shared and testable
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

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.
This commit is contained in:
donny committed 2026-07-28 07:31:13 +00:00
1 parent ba32d48a6a
commit faaf63bc93
6 files changed
+354 -42

No files matched your search

+25
View File
@@ -78,6 +78,31 @@ bool lgDisplaysPack(
unsigned * desktopWidth,
unsigned * desktopHeight);
/**
* Copy each display out of a captured virtual desktop image and into its slot
* in the packed atlas.
*
* `srcX`/`srcY` give each display's position in the source image, which is not
* generally its position in the atlas, nor its normalised desktop position.
*
* Copies are clipped to both the source image and `dstHeight`, so a
* destination too small for the whole atlas truncates safely rather than
* overrunning. Atlas padding owned by no display is left untouched.
*/
void lgDisplaysRepack(
uint8_t * dst,
unsigned dstPitch,
unsigned dstHeight,
const uint8_t * src,
unsigned srcPitch,
unsigned srcWidth,
unsigned srcHeight,
const KVMFRDisplay * displays,
const uint32_t * srcX,
const uint32_t * srcY,
unsigned count,
unsigned bytesPerPixel);
/**
* Returns the index of the display in `displays` containing the virtual
* desktop point (x, y), or -1 if the point falls in a gap between displays.
+47
View File
@@ -242,6 +242,53 @@ bool lgDisplaysPack(
return true;
}
void lgDisplaysRepack(
uint8_t * dst,
unsigned dstPitch,
unsigned dstHeight,
const uint8_t * src,
unsigned srcPitch,
unsigned srcWidth,
unsigned srcHeight,
const KVMFRDisplay * displays,
const uint32_t * srcX,
const uint32_t * srcY,
unsigned count,
unsigned bytesPerPixel)
{
for(unsigned i = 0; i < count; ++i)
{
const KVMFRDisplay * d = &displays[i];
// a display should always sit inside the source, but never trust it to
if (srcX[i] >= srcWidth || srcY[i] >= srcHeight)
continue;
unsigned width = d->atlasWidth;
unsigned rows = d->atlasHeight;
if (srcX[i] + width > srcWidth)
width = srcWidth - srcX[i];
if (srcY[i] + rows > srcHeight)
rows = srcHeight - srcY[i];
// clip to the destination, so a short buffer truncates rather than overruns
if (d->atlasY >= dstHeight)
continue;
if (d->atlasY + rows > dstHeight)
rows = dstHeight - d->atlasY;
const size_t bytes = (size_t)width * bytesPerPixel;
for(unsigned y = 0; y < rows; ++y)
memcpy(
dst + (size_t)(d->atlasY + y) * dstPitch +
(size_t)d->atlasX * bytesPerPixel,
src + (size_t)(srcY[i] + y) * srcPitch +
(size_t)srcX[i] * bytesPerPixel,
bytes);
}
}
int lgDisplaysFindAtPoint(const KVMFRDisplay * displays, unsigned count,
int32_t x, int32_t y)
{
+120 -2
View File
@@ -1,14 +1,21 @@
#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 * what)
static void check(bool cond, const char * fmt, ...)
{
printf(" [%s] %s\n", cond ? "PASS" : "FAIL", what);
va_list va;
va_start(va, fmt);
printf(" [%s] ", cond ? "PASS" : "FAIL");
vprintf(fmt, va);
printf("\n");
va_end(va);
if (!cond)
++failures;
}
@@ -67,6 +74,115 @@ static void run(const char * title, const LGDisplayInfo * in, unsigned count)
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();
@@ -146,6 +262,8 @@ int main(void)
"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;
+4 -32
View File
@@ -479,38 +479,10 @@ static CaptureResult xcb_waitFrame(
* waiting on the pointer must not be told a region is ready before it is. */
static void xcb_packDisplays(FrameBuffer * frame)
{
uint8_t * dst = framebuffer_get_data(frame);
const uint8_t * src = this->data;
for(unsigned i = 0; i < this->displayCount; ++i)
{
const KVMFRDisplay * d = &this->displays[i];
// clamp to the captured root window, which a monitor should never exceed
unsigned width = d->atlasWidth;
unsigned rows = d->atlasHeight;
if (this->srcX[i] >= this->width || this->srcY[i] >= this->height)
continue;
if (this->srcX[i] + width > this->width)
width = this->width - this->srcX[i];
if (this->srcY[i] + rows > this->height)
rows = this->height - this->srcY[i];
// clamp to what fits in the frame, so a small ivshmem truncates safely
if (d->atlasY >= this->dataHeight)
continue;
if (d->atlasY + rows > this->dataHeight)
rows = this->dataHeight - d->atlasY;
const size_t bytes = (size_t)width * 4;
for(unsigned y = 0; y < rows; ++y)
memcpy(
dst + (size_t)(d->atlasY + y) * this->atlasPitch +
(size_t)d->atlasX * 4,
src + (size_t)(this->srcY[i] + y) * this->pitch +
(size_t)this->srcX[i] * 4,
bytes);
}
lgDisplaysRepack(
framebuffer_get_data(frame), this->atlasPitch, this->dataHeight,
this->data, this->pitch, this->width, this->height,
this->displays, this->srcX, this->srcY, this->displayCount, 4);
framebuffer_set_write_ptr(frame,
(size_t)this->atlasPitch * this->dataHeight);
+30 -8
View File
@@ -241,37 +241,59 @@ int main(int argc, char * argv[])
const uint8_t * data = framebuffer_get_buffer(fb);
/* A display region that is entirely one colour means nothing was copied
* into it. The desktop always has some variation, so this catches a
* packing offset that writes to the wrong place. */
/* A display region that is entirely one colour was probably never written
* to, which catches a packing offset that copies to the wrong place.
*
* A capture source can legitimately be blank though: an X root window under
* a Wayland session holds no image, and a headless server may show nothing.
* If every region is not just flat but flat in the same colour, the source
* itself was empty and this proves nothing either way, so say so rather than
* reporting a failure the layout checks have already contradicted. */
bool anyVaries = false;
bool allSameFlat = true;
uint32_t flatColour = 0;
bool varies[KVMFR_MAX_DISPLAYS] = { false };
for(unsigned i = 0; i < frame->displayCount; ++i)
{
const KVMFRDisplay * d = &frame->displays[i];
uint32_t first = 0;
bool varies = false;
for(unsigned y = 0; y < d->atlasHeight && !varies; y += 7)
for(unsigned y = 0; y < d->atlasHeight && !varies[i]; y += 7)
for(unsigned x = 0; x < d->atlasWidth; x += 7)
{
const uint32_t px = *(const uint32_t *)(data +
(size_t)(d->atlasY + y) * frame->pitch +
(size_t)(d->atlasX + x) * 4);
if (!y && !x)
{
first = px;
if (!i)
flatColour = px;
else if (px != flatColour)
allSameFlat = false;
}
else if (px != first)
{
varies = true;
varies[i] = true;
anyVaries = true;
break;
}
}
check(varies, "%s region contains real image data", d->name);
char path[512];
snprintf(path, sizeof(path), "%s/display-%u-%s.ppm", outDir, i, d->name);
writePPM(path, data, frame->pitch, d, 8);
}
if (!anyVaries && allSameFlat)
printf(" [SKIP] every display is uniform 0x%08x: the capture source is "
"blank, so this run cannot confirm the pixel copy\n", flatColour);
else
for(unsigned i = 0; i < frame->displayCount; ++i)
check(varies[i], "%s region contains real image data",
frame->displays[i].name);
lgmpClientUnsubscribe(&queue);
lgmpClientFree(&client);
+128
View File
@@ -0,0 +1,128 @@
#!/bin/bash
# End to end test of multi-monitor capture and atlas packing.
#
# Runs a virtual X server with three RandR monitors defined over one screen,
# paints a distinct pattern into each monitor's region, then has the host
# capture it and checks that every display's pixels arrive at the right place
# in the packed atlas.
#
# Usage: test-multimon.sh <build-dir> <work-dir>
set -u
HOST_BIN="${1:-host/build/looking-glass-host}"
VERIFY_BIN="${2:-lg-verify-atlas}"
WORK="${3:-/tmp/lg-multimon-test}"
DISP=:99
SHM=/dev/shm/looking-glass-test
SCREEN_W=1920
SCREEN_H=1080
MON_W=640
# each monitor gets its own primary colour so a misplaced copy is obvious
COLOURS=(red green blue)
NAMES=(TEST-1 TEST-2 TEST-3)
cleanup() {
[ -n "${HOST_PID:-}" ] && kill "$HOST_PID" 2>/dev/null
[ -n "${XVFB_PID:-}" ] && kill "$XVFB_PID" 2>/dev/null
rm -f "$SHM"
}
trap cleanup EXIT
mkdir -p "$WORK"
rm -rf "${WORK:?}"/*
echo "== starting virtual X server =="
Xvfb $DISP -screen 0 ${SCREEN_W}x${SCREEN_H}x24 +extension RANDR > "$WORK/xvfb.log" 2>&1 &
XVFB_PID=$!
sleep 2
if ! DISPLAY=$DISP xrandr --query > /dev/null 2>&1; then
echo "FAIL: virtual X server did not start"
cat "$WORK/xvfb.log"
exit 1
fi
echo "== defining 3 monitors =="
for i in 0 1 2; do
X=$((i * MON_W))
DISPLAY=$DISP xrandr --setmonitor "${NAMES[$i]}" \
${MON_W}/170x${SCREEN_H}/280+${X}+0 none || {
echo "FAIL: could not define monitor ${NAMES[$i]}"; exit 1; }
done
DISPLAY=$DISP xrandr --listmonitors
echo "== painting a distinct pattern per monitor =="
# a gradient gives within-region variation, so a region that is left untouched
# (flat colour) is distinguishable from one that was really copied
ARGS=()
for i in 0 1 2; do
convert -size ${MON_W}x${SCREEN_H} \
gradient:"${COLOURS[$i]}"-black "$WORK/mon$i.png"
ARGS+=("$WORK/mon$i.png")
done
convert "${ARGS[@]}" +append "$WORK/desktop.png"
DISPLAY=$DISP display -window root "$WORK/desktop.png" 2>/dev/null &
sleep 2
echo "== starting host =="
rm -f "$SHM"
truncate -s 128M "$SHM"
DISPLAY=$DISP "$HOST_BIN" app:capture=XCB app:shmFile="$SHM" \
> "$WORK/host.log" 2>&1 &
HOST_PID=$!
sleep 3
if ! kill -0 "$HOST_PID" 2>/dev/null; then
echo "FAIL: host exited"
tail -20 "$WORK/host.log"
exit 1
fi
grep -E "Multi-monitor|TEST-" "$WORK/host.log" || true
echo
echo "== verifying the packed atlas =="
"$VERIFY_BIN" "$SHM" "$WORK"
VERIFY_RC=$?
echo
echo "== checking each display carries its own colour =="
RC=0
for i in 0 1 2; do
PPM="$WORK/display-$i-${NAMES[$i]}.ppm"
if [ ! -f "$PPM" ]; then
echo " [FAIL] ${NAMES[$i]}: no image written"
RC=1
continue
fi
# the dominant channel must be the colour painted into that monitor
read -r R G B <<< "$(convert "$PPM" -resize 1x1! -format "%[fx:r] %[fx:g] %[fx:b]" info:)"
case $i in
0) OK=$(awk -v r="$R" -v g="$G" -v b="$B" \
'BEGIN{print (r>g && r>b) ? 1 : 0}') ; WANT=red ;;
1) OK=$(awk -v r="$R" -v g="$G" -v b="$B" \
'BEGIN{print (g>r && g>b) ? 1 : 0}') ; WANT=green ;;
2) OK=$(awk -v r="$R" -v g="$G" -v b="$B" \
'BEGIN{print (b>r && b>g) ? 1 : 0}') ; WANT=blue ;;
esac
if [ "$OK" = "1" ]; then
echo " [PASS] ${NAMES[$i]} is $WANT dominant (r=$R g=$G b=$B)"
else
echo " [FAIL] ${NAMES[$i]} should be $WANT dominant (r=$R g=$G b=$B)"
RC=1
fi
done
echo
if [ $VERIFY_RC -eq 0 ] && [ $RC -eq 0 ]; then
echo "MULTI-MONITOR TEST PASSED"
exit 0
fi
echo "MULTI-MONITOR TEST FAILED"
exit 1