diff --git a/host/platform/Linux/capture/XCB/src/xcb.c b/host/platform/Linux/capture/XCB/src/xcb.c index f75bbf27..099d053b 100644 --- a/host/platform/Linux/capture/XCB/src/xcb.c +++ b/host/platform/Linux/capture/XCB/src/xcb.c @@ -558,7 +558,9 @@ static int pointerThread(void * unused) uint32_t size; if (!this->getPointerBufferFn(&data, &size)) { - DEBUG_WARN("failed to get a pointer buffer"); + /* Expected briefly at startup, before the host has allocated the + * pointer buffers. Wait rather than spinning on it. */ + usleep(1000); continue; } diff --git a/host/src/app.c b/host/src/app.c index 42534602..dfcdc1fa 100644 --- a/host/src/app.c +++ b/host/src/app.c @@ -558,6 +558,12 @@ static bool captureStop(void) DEBUG_INFO("==== [ Capture Stop ] ===="); + /* Stop before deinit so a backend that runs its own threads can join them + * while its resources are still valid. Without this a thread can still be + * using the device that deinit is tearing down. */ + if (app.iface->stop) + app.iface->stop(); + if (!app.iface->deinit()) { DEBUG_ERROR("Failed to deinitialize the capture device"); @@ -572,6 +578,14 @@ static bool captureStop(void) bool captureGetPointerBuffer(void ** data, uint32_t * size) { PLGMPMemory mem = app.pointerShapeMemory[app.pointerShapeIndex]; + + /* A backend that captures the pointer on its own thread can reach here + * before lgmpSetup has allocated the pointer buffers, because capture is + * started first. Report that no buffer is available rather than + * dereferencing one that does not exist yet; the backend simply retries. */ + if (!mem) + return false; + *data = (uint8_t*)lgmpHostMemPtr(mem) + sizeof(KVMFRCursor); *size = MAX_POINTER_SIZE - sizeof(KVMFRCursor); return true; diff --git a/host/tests/lg-verify-atlas.c b/host/tests/lg-verify-atlas.c new file mode 100644 index 00000000..8d032e98 --- /dev/null +++ b/host/tests/lg-verify-atlas.c @@ -0,0 +1,281 @@ +/** + * 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 + */ + +/* Subscribes to a running host like a real client would, then checks that the + * multi-display frame it publishes is self consistent: that the layout is + * sane, that every display lands inside the packed frame without overlapping + * another, and that the pixels really were copied. + * + * Writes each display out as a PPM so the packing can also be eyeballed. */ + +#include "common/KVMFR.h" +#include "common/debug.h" +#include "common/displays.h" +#include "common/framebuffer.h" +#include "common/ivshmem.h" + +#include "lgmp/client.h" + +#include +#include +#include +#include +#include + +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; +} + +/* Write one display's region of the atlas as a PPM, downscaled by `step` so + * the files stay small enough to look at. */ +static void writePPM(const char * path, const uint8_t * data, + unsigned pitch, const KVMFRDisplay * d, unsigned step) +{ + const unsigned w = d->atlasWidth / step; + const unsigned h = d->atlasHeight / step; + if (!w || !h) + return; + + FILE * fp = fopen(path, "wb"); + if (!fp) + { + printf(" could not write %s\n", path); + return; + } + + fprintf(fp, "P6\n%u %u\n255\n", w, h); + for(unsigned y = 0; y < h; ++y) + for(unsigned x = 0; x < w; ++x) + { + const uint8_t * px = data + + (size_t)(d->atlasY + y * step) * pitch + + (size_t)(d->atlasX + x * step) * 4; + // the wire format is BGRA + fputc(px[2], fp); + fputc(px[1], fp); + fputc(px[0], fp); + } + + fclose(fp); + printf(" wrote %s (%ux%u)\n", path, w, h); +} + +int main(int argc, char * argv[]) +{ + debug_init(); + + const char * shmPath = argc > 1 ? argv[1] : "/dev/shm/looking-glass"; + const char * outDir = argc > 2 ? argv[2] : "."; + + struct IVSHMEM shm = {0}; + if (!ivshmemOpenDev(&shm, shmPath)) + { + printf("Failed to open %s\n", shmPath); + return 1; + } + + PLGMPClient client; + PLGMPClientQueue queue; + LGMP_STATUS status; + + for(int i = 0; i < 100; ++i) + { + if ((status = lgmpClientInit(shm.mem, shm.size, &client)) == LGMP_OK) + break; + usleep(100000); + } + if (status != LGMP_OK) + { + printf("lgmpClientInit failed: %s\n", lgmpStatusString(status)); + return 1; + } + + uint32_t udataSize; + uint8_t * udata; + uint32_t clientID, remoteVersion; + if ((status = lgmpClientSessionInit(client, &udataSize, &udata, + &clientID, &remoteVersion)) != LGMP_OK) + { + printf("lgmpClientSessionInit failed: %s\n", lgmpStatusString(status)); + return 1; + } + + const KVMFR * kvmfr = (const KVMFR *)udata; + printf("host reports KVMFR version %u\n", kvmfr->version); + check(kvmfr->version == KVMFR_VERSION, + "KVMFR version is %d", KVMFR_VERSION); + + for(int i = 0; i < 100; ++i) + { + if ((status = lgmpClientSubscribe(client, LGMP_Q_FRAME, &queue)) == LGMP_OK) + break; + usleep(100000); + } + if (status != LGMP_OK) + { + printf("lgmpClientSubscribe failed: %s\n", lgmpStatusString(status)); + return 1; + } + + // wait for a frame + LGMPMessage msg; + bool got = false; + for(int i = 0; i < 200; ++i) + { + if ((status = lgmpClientProcess(queue, &msg)) == LGMP_OK) + { + got = true; + break; + } + if (status != LGMP_ERR_QUEUE_EMPTY) + { + printf("lgmpClientProcess failed: %s\n", lgmpStatusString(status)); + return 1; + } + usleep(50000); + } + + if (!got) + { + printf("timed out waiting for a frame\n"); + return 1; + } + + KVMFRFrame * frame = (KVMFRFrame *)msg.mem; + + printf("\nframe: %ux%u packed, screen %ux%u, %u displays\n", + frame->frameWidth, frame->frameHeight, + frame->screenWidth, frame->screenHeight, + frame->displayCount); + + printf("\n== layout ==\n"); + check(frame->displayCount > 0, "frame carries a multi-display layout"); + if (!frame->displayCount) + return 1; + + check(frame->displayCount <= KVMFR_MAX_DISPLAYS, + "display count %u within the maximum", frame->displayCount); + + uint64_t sumArea = 0; + for(unsigned i = 0; i < frame->displayCount; ++i) + { + const KVMFRDisplay * d = &frame->displays[i]; + printf(" %-10s atlas +%u+%u %ux%u desktop +%d+%d %ux%u%s\n", + d->name, d->atlasX, d->atlasY, d->atlasWidth, d->atlasHeight, + d->desktopX, d->desktopY, d->desktopWidth, d->desktopHeight, + (d->flags & KVMFR_DISPLAY_FLAG_PRIMARY) ? " (primary)" : ""); + sumArea += (uint64_t)d->atlasWidth * d->atlasHeight; + } + + for(unsigned i = 0; i < frame->displayCount; ++i) + { + const KVMFRDisplay * d = &frame->displays[i]; + + check(d->atlasX + d->atlasWidth <= frame->frameWidth && + d->atlasY + d->atlasHeight <= frame->frameHeight, + "%s lies inside the packed frame", d->name); + + check(d->atlasWidth == d->desktopWidth && + d->atlasHeight == d->desktopHeight, + "%s is packed without scaling", d->name); + + check(d->desktopX + d->desktopWidth <= frame->screenWidth && + d->desktopY + d->desktopHeight <= frame->screenHeight, + "%s lies inside the virtual desktop", d->name); + + for(unsigned j = i + 1; j < frame->displayCount; ++j) + { + const KVMFRDisplay * o = &frame->displays[j]; + const bool disjoint = + d->atlasX + d->atlasWidth <= o->atlasX || + o->atlasX + o->atlasWidth <= d->atlasX || + d->atlasY + d->atlasHeight <= o->atlasY || + o->atlasY + o->atlasHeight <= d->atlasY; + check(disjoint, "%s and %s do not overlap in the atlas", + d->name, o->name); + } + } + + check((uint64_t)frame->frameWidth * frame->frameHeight >= sumArea, + "atlas is large enough for every display"); + + // the framebuffer follows the header at the recorded offset + FrameBuffer * fb = (FrameBuffer *)((uint8_t *)frame + frame->offset); + const size_t frameSize = (size_t)frame->pitch * frame->frameHeight; + + printf("\n== pixel data ==\n"); + if (!framebuffer_wait(fb, frameSize)) + { + printf(" [FAIL] timed out waiting for the frame data\n"); + ++failures; + return 1; + } + check(true, "host finished writing %zu bytes of frame data", frameSize); + + 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. */ + 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 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; + else if (px != first) + { + varies = 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); + } + + lgmpClientUnsubscribe(&queue); + lgmpClientFree(&client); + + printf("\n%s (%d failure%s)\n", failures ? "FAILED" : "ALL PASSED", + failures, failures == 1 ? "" : "s"); + return failures != 0; +}