diff --git a/client/renderers/EGL/shader/compose.vert b/client/renderers/EGL/shader/compose.vert index e92deec0..1df486c2 100644 --- a/client/renderers/EGL/shader/compose.vert +++ b/client/renderers/EGL/shader/compose.vert @@ -18,9 +18,14 @@ uniform vec2 atlasSize; void main() { - // view pixels -> clip space, with the origin at the top left - vec2 pos = vertex / viewSize * 2.0 - 1.0; - gl_Position = vec4(pos.x, -pos.y, 0.0, 1.0); + /* View pixels to clip space. + * + * Y is deliberately not flipped. This shader renders into a framebuffer + * whose texture is then sampled by the desktop shader, which treats row 0 + * of the image as the top, matching the order rows arrive in the frame. + * Writing clip Y directly keeps the composed texture in that same order; + * flipping here would hand the next stage an upside down desktop. */ + gl_Position = vec4(vertex / viewSize * 2.0 - 1.0, 0.0, 1.0); uv = texture / atlasSize; } diff --git a/host/tests/lg-fake-host.c b/host/tests/lg-fake-host.c new file mode 100644 index 00000000..30f8e631 --- /dev/null +++ b/host/tests/lg-fake-host.c @@ -0,0 +1,248 @@ +/** + * Looking Glass + * Copyright © 2017-2026 The Looking Glass Authors + * https://looking-glass.io + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., 59 + * Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* Publishes a synthetic multi-display frame so the client's composition can be + * tested without a capture device. + * + * Three equally sized displays are laid out in a row on the virtual desktop. + * The packer stacks them vertically in the atlas because that is the squarer + * layout, so the packed order deliberately differs from the desktop order: a + * client that ignored the layout and simply drew the atlas would show them + * stacked, and one that mixed up the mapping would show them in the wrong + * order or the wrong orientation. + * + * Each display is a solid primary colour with a white marker block in its top + * left corner, so both position and orientation are checkable. */ + +#include "common/KVMFR.h" +#include "common/debug.h" +#include "common/displays.h" +#include "common/framebuffer.h" +#include "common/ivshmem.h" + +#include "lgmp/host.h" + +#include +#include +#include +#include +#include + +#define DISPLAY_W 320 +#define DISPLAY_H 240 +#define MARKER 40 + +static const uint32_t COLOURS[3] = +{ + 0x00FF0000, // red (BGRA in memory: B=00 G=00 R=FF) + 0x0000FF00, // green + 0x000000FF // blue +}; + +static volatile sig_atomic_t running = 1; +static void onSignal(int sig) { (void)sig; running = 0; } + +int main(int argc, char * argv[]) +{ + debug_init(); + signal(SIGINT , onSignal); + signal(SIGTERM, onSignal); + + const char * shmPath = argc > 1 ? argv[1] : "/dev/shm/looking-glass"; + + struct IVSHMEM shm = {0}; + if (!ivshmemOpenDev(&shm, shmPath)) + { + printf("failed to open %s\n", shmPath); + return 1; + } + + // describe three displays side by side on the virtual desktop + LGDisplayInfo info[3]; + for(unsigned i = 0; i < 3; ++i) + { + memset(&info[i], 0, sizeof(info[i])); + info[i].id = i; + info[i].primary = (i == 0); + info[i].x = i * DISPLAY_W; + info[i].y = 0; + info[i].width = DISPLAY_W; + info[i].height = DISPLAY_H; + snprintf(info[i].name, sizeof(info[i].name), "FAKE-%u", i); + } + + KVMFRDisplay displays[KVMFR_MAX_DISPLAYS]; + unsigned atlasW, atlasH, deskW, deskH; + if (!lgDisplaysPack(info, 3, 1, displays, &atlasW, &atlasH, &deskW, &deskH)) + { + printf("failed to pack displays\n"); + return 1; + } + + printf("atlas %ux%u, desktop %ux%u\n", atlasW, atlasH, deskW, deskH); + for(unsigned i = 0; i < 3; ++i) + printf(" %s atlas +%u+%u -> desktop +%d+%d\n", displays[i].name, + displays[i].atlasX, displays[i].atlasY, + displays[i].desktopX, displays[i].desktopY); + + // the client validates the KVMFR header before it will subscribe + uint8_t udata[1024]; + memset(udata, 0, sizeof(udata)); + KVMFR * kvmfr = (KVMFR *)udata; + memcpy(kvmfr->magic, KVMFR_MAGIC, sizeof(kvmfr->magic)); + kvmfr->version = KVMFR_VERSION; + kvmfr->features = 0; + strncpy(kvmfr->hostver, "fake-host", sizeof(kvmfr->hostver) - 1); + + PLGMPHost host; + LGMP_STATUS status; + if ((status = lgmpHostInit(shm.mem, shm.size, &host, + sizeof(KVMFR), udata)) != LGMP_OK) + { + printf("lgmpHostInit failed: %s\n", lgmpStatusString(status)); + return 1; + } + + const struct LGMPQueueConfig frameConfig = + { + .queueID = LGMP_Q_FRAME, + .numMessages = LGMP_Q_FRAME_LEN, + .subTimeout = 1000 + }; + const struct LGMPQueueConfig pointerConfig = + { + .queueID = LGMP_Q_POINTER, + .numMessages = LGMP_Q_POINTER_LEN, + .subTimeout = 1000 + }; + + PLGMPHostQueue frameQueue, pointerQueue; + if ((status = lgmpHostQueueNew(host, frameConfig, &frameQueue)) != LGMP_OK || + (status = lgmpHostQueueNew(host, pointerConfig, + &pointerQueue)) != LGMP_OK) + { + printf("lgmpHostQueueNew failed: %s\n", lgmpStatusString(status)); + return 1; + } + + const unsigned pitch = atlasW * 4; + const size_t frameSize = (size_t)pitch * atlasH; + const unsigned alignSize = 4096; + const size_t memSize = frameSize + alignSize + sizeof(KVMFRFrame); + + PLGMPMemory frameMemory[LGMP_Q_FRAME_LEN]; + KVMFRFrame * frames [LGMP_Q_FRAME_LEN]; + FrameBuffer* frameBuffer[LGMP_Q_FRAME_LEN]; + + for(int i = 0; i < LGMP_Q_FRAME_LEN; ++i) + { + if ((status = lgmpHostMemAllocAligned(host, memSize, alignSize, + &frameMemory[i])) != LGMP_OK) + { + printf("lgmpHostMemAllocAligned failed: %s\n", lgmpStatusString(status)); + return 1; + } + frames[i] = lgmpHostMemPtr(frameMemory[i]); + memset(frames[i], 0, sizeof(KVMFRFrame)); + + const unsigned offset = alignSize - sizeof(FrameBuffer); + frames[i]->offset = offset; + frameBuffer[i] = (FrameBuffer *)((uint8_t *)frames[i] + offset); + } + + /* Paint the atlas once: each display solid coloured, with a white marker in + * its top left so a flip or a transpose is visible. + * + * framebuffer_write uses aligned streaming loads, so the source has to be + * at least 32 byte aligned. Real capture buffers always are; a plain malloc + * is not guaranteed to be. */ + const size_t allocSize = (frameSize + 63) & ~(size_t)63; + uint32_t * atlas = aligned_alloc(64, allocSize); + if (!atlas) + { + printf("failed to allocate the atlas\n"); + return 1; + } + memset(atlas, 0, allocSize); + for(unsigned i = 0; i < 3; ++i) + { + const KVMFRDisplay * d = &displays[i]; + for(unsigned y = 0; y < d->atlasHeight; ++y) + for(unsigned x = 0; x < d->atlasWidth; ++x) + { + const bool marker = (x < MARKER && y < MARKER); + atlas[(d->atlasY + y) * atlasW + d->atlasX + x] = + marker ? 0x00FFFFFF : COLOURS[i]; + } + } + + printf("publishing frames, ctrl-c to stop\n"); + + unsigned index = 0, serial = 0; + while(running) + { + lgmpHostProcess(host); + + if (lgmpHostQueuePending(frameQueue) == LGMP_Q_FRAME_LEN) + { + usleep(1000); + continue; + } + + KVMFRFrame * fi = frames[index]; + fi->formatVer = 1; + fi->frameSerial = serial++; + fi->type = FRAME_TYPE_BGRA; + fi->screenWidth = deskW; // the virtual desktop + fi->screenHeight = deskH; + fi->dataWidth = atlasW; // the packed frame + fi->dataHeight = atlasH; + fi->frameWidth = atlasW; + fi->frameHeight = atlasH; + fi->rotation = FRAME_ROT_0; + fi->stride = atlasW; + fi->pitch = pitch; + fi->damageRectsCount = 0; + fi->flags = 0; + fi->sdrWhiteLevel = KVMFR_SDR_WHITE_LEVEL_DEFAULT; + fi->displayCount = 3; + memcpy(fi->displays, displays, sizeof(displays[0]) * 3); + + framebuffer_prepare(frameBuffer[index]); + if ((status = lgmpHostQueuePost(frameQueue, 0, + frameMemory[index])) != LGMP_OK) + { + if (status != LGMP_ERR_QUEUE_FULL) + printf("post failed: %s\n", lgmpStatusString(status)); + usleep(1000); + continue; + } + + framebuffer_write(frameBuffer[index], atlas, frameSize); + + if (++index == LGMP_Q_FRAME_LEN) + index = 0; + usleep(16000); + } + + free(atlas); + printf("stopped\n"); + return 0; +} diff --git a/host/tests/test-compose.sh b/host/tests/test-compose.sh new file mode 100755 index 00000000..ced51a78 --- /dev/null +++ b/host/tests/test-compose.sh @@ -0,0 +1,157 @@ +#!/bin/bash +# Verifies the client's multi-display composition by rendering it for real. +# +# A synthetic host publishes three displays packed as a vertical stack in the +# atlas but laid out as a horizontal row on the virtual desktop. The client is +# rendered into a virtual X server and the result is sampled: getting the +# colours in the right places proves the composition used the layout rather +# than simply drawing the packed frame. +# +# Usage: test-compose.sh [work-dir] + +set -u + +FAKE="${1:?fake host binary required}" +CLIENT="${2:?client binary required}" +WORK="${3:-/tmp/lg-compose-test}" + +DISP=:99 +SHM=/dev/shm/lg-compose-test +VIEW_W=960 +VIEW_H=240 + +cleanup() { + [ -n "${CLIENT_PID:-}" ] && kill "$CLIENT_PID" 2>/dev/null + [ -n "${FAKE_PID:-}" ] && kill "$FAKE_PID" 2>/dev/null + [ -n "${XVFB_PID:-}" ] && kill "$XVFB_PID" 2>/dev/null + rm -f "$SHM" +} +trap cleanup EXIT + +mkdir -p "$WORK" +rm -f "$WORK"/*.png + +echo "== starting virtual X server ==" +Xvfb $DISP -screen 0 1024x320x24 > "$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"; exit 1 +fi + +echo "== starting synthetic host ==" +rm -f "$SHM" +truncate -s 64M "$SHM" +"$FAKE" "$SHM" > "$WORK/fake.log" 2>&1 & +FAKE_PID=$! +sleep 2 +grep -E "^atlas|desktop \+" "$WORK/fake.log" + +echo "== starting client ==" +# force the X11 backend so the output can be captured from the X root +env -u WAYLAND_DISPLAY DISPLAY=$DISP "$CLIENT" \ + -f "$SHM" -s \ + win:size=${VIEW_W}x${VIEW_H} win:autoResize=no win:borderless=yes \ + win:position=0x0 app:display=all \ + > "$WORK/client.log" 2>&1 & +CLIENT_PID=$! +sleep 6 + +if ! kill -0 "$CLIENT_PID" 2>/dev/null; then + echo "FAIL: client exited" + tail -15 "$WORK/client.log" + exit 1 +fi + +grep -E "Composing|Showing" "$WORK/client.log" || true + +echo "== capturing the rendered output ==" +if ! DISPLAY=$DISP import -window root "$WORK/render.png" 2>"$WORK/import.log"; then + echo "FAIL: could not capture the X root" + cat "$WORK/import.log" + exit 1 +fi + +# sample a point and report the dominant channel +sample() { + convert "$WORK/render.png" -format "%[pixel:p{$1,$2}]" info: +} + +dominant() { + local px; px=$(convert "$WORK/render.png" -crop 1x1+$1+$2 +repage \ + -format "%[fx:r] %[fx:g] %[fx:b]" info:) + read -r r g b <<< "$px" + awk -v r="$r" -v g="$g" -v b="$b" 'BEGIN{ + if (r>0.8 && g>0.8 && b>0.8) { print "white"; exit } + if (r>g && r>b) { print "red" ; exit } + if (g>r && g>b) { print "green"; exit } + if (b>r && b>g) { print "blue" ; exit } + print "none" }' +} + +echo +echo "== checking composition ==" +RC=0 + +check() { + local x=$1 y=$2 want=$3 what=$4 + local got; got=$(dominant "$x" "$y") + if [ "$got" = "$want" ]; then + echo " [PASS] $what at ($x,$y) is $got" + else + echo " [FAIL] $what at ($x,$y) is $got, expected $want" + RC=1 + fi +} + +# each display's body, sampled well clear of the corner marker +check 160 150 red "FAKE-0 (leftmost on the desktop)" +check 480 150 green "FAKE-1 (middle on the desktop)" +check 800 150 blue "FAKE-2 (rightmost on the desktop)" + +# the marker sits in each display's top left, proving orientation +check 15 15 white "FAKE-0 corner marker" +check 335 15 white "FAKE-1 corner marker" +check 655 15 white "FAKE-2 corner marker" + +# just right of each marker must be that display's colour, not the next one's +check 60 15 red "FAKE-0 beside its marker" +check 380 15 green "FAKE-1 beside its marker" + +echo +echo "== checking single display selection ==" +kill "$CLIENT_PID" 2>/dev/null +wait "$CLIENT_PID" 2>/dev/null + +# selecting one display must show only that display, filling the view +env -u WAYLAND_DISPLAY DISPLAY=$DISP "$CLIENT" \ + -f "$SHM" -s \ + win:size=320x240 win:autoResize=no win:borderless=yes \ + win:position=0x0 app:display=1 \ + > "$WORK/client-single.log" 2>&1 & +CLIENT_PID=$! +sleep 6 + +if ! kill -0 "$CLIENT_PID" 2>/dev/null; then + echo " [FAIL] client exited when selecting one display" + RC=1 +else + grep -E "Showing guest display" "$WORK/client-single.log" || true + DISPLAY=$DISP import -window root "$WORK/render.png" 2>/dev/null + + # the whole view is now FAKE-1, so it is green with its marker at the origin + check 160 120 green "selected display body" + check 15 15 white "selected display corner marker" + check 60 15 green "selected display beside its marker" + check 300 230 green "selected display far corner" +fi + +echo +echo " rendered output: $WORK/render.png" +echo +if [ $RC -eq 0 ]; then + echo "COMPOSITION TEST PASSED" +else + echo "COMPOSITION TEST FAILED" +fi +exit $RC