diff --git a/common/include/common/displays.h b/common/include/common/displays.h index a5c7abf1..962d443b 100644 --- a/common/include/common/displays.h +++ b/common/include/common/displays.h @@ -35,6 +35,23 @@ * let the client reject anything its driver cannot honour. */ #define LG_DISPLAY_MAX_ATLAS_EDGE 16384 +/* Display controllers generally require a scanout buffer's base address to be + * 256 byte aligned. + * + * Packing every display on a matching boundary lets a consumer point the + * display hardware straight at one display's region of the atlas — a DRM + * framebuffer whose base offset is `atlasY * pitch + atlasX * bpp` — instead + * of copying it out first. Getting this wrong is worse than it sounds: some + * displays land on legal offsets and some do not, so only some monitors take + * the fast path and the difference is awkward to diagnose. + * + * The cost is up to `alignment - 1` pixels of horizontal padding per display, + * which is nothing against what it buys. */ +#define LG_DISPLAY_SCANOUT_ALIGN_BYTES 256 + +/* The pixel alignment satisfying the above for a given pixel size in bytes. */ +#define LG_DISPLAY_ALIGN_PIXELS(bpp) (LG_DISPLAY_SCANOUT_ALIGN_BYTES / (bpp)) + /* One guest display as reported by a capture backend, before packing. */ typedef struct LGDisplayInfo { @@ -62,8 +79,11 @@ LGDisplayInfo; * KVMFR_MAX_DISPLAYS. Entries in `out` are written in the same order as `in`, * so a backend can keep its own indexing. * - * `alignment` rounds the atlas width up to a multiple of that many pixels, so - * a backend can satisfy a row-pitch requirement. Pass 1 for no alignment. + * `alignment` is a horizontal alignment in pixels, applied both to each + * display's X offset within the atlas and to the atlas width. Together those + * make every display's base address in the atlas a multiple of + * `alignment * bpp`, which is what direct scanout needs. Pass + * LG_DISPLAY_ALIGN_PIXELS(bytesPerPixel) for that, or 1 for none. * * Returns false if the inputs are invalid or no layout fits within * LG_DISPLAY_MAX_ATLAS_EDGE. diff --git a/common/src/displays.c b/common/src/displays.c index 9afaac78..b90b739d 100644 --- a/common/src/displays.c +++ b/common/src/displays.c @@ -37,6 +37,7 @@ static bool packWithWidth( const unsigned * order, unsigned count, unsigned width, + unsigned alignment, unsigned * outX, unsigned * outY, unsigned * outHeight, @@ -53,6 +54,12 @@ static bool packWithWidth( if (w > width) return false; + /* Advance to the next aligned column before testing whether the display + * fits, so a display never starts at an offset a scanout engine cannot + * address. Zero is always aligned, so a fresh shelf needs no padding. */ + if (shelfX % alignment) + shelfX += alignment - (shelfX % alignment); + if (shelfX + w > width) { // no room left on this shelf, open a new one above it @@ -149,7 +156,14 @@ bool lgDisplaysPack( unsigned width = 0; for(unsigned i = 0; i < count; ++i) if (mask & (1u << i)) - width += in[i].width; + { + // count the padding each display will need, or the candidate widths + // describe shelves that cannot actually hold the set + unsigned w = in[i].width; + if (w % alignment) + w += alignment - (w % alignment); + width += w; + } if (width < maxWidth) width = maxWidth; @@ -180,7 +194,8 @@ bool lgDisplaysPack( unsigned x[KVMFR_MAX_DISPLAYS], y[KVMFR_MAX_DISPLAYS]; unsigned height, usedWidth; - if (!packWithWidth(in, order, count, width, x, y, &height, &usedWidth)) + if (!packWithWidth(in, order, count, width, alignment, x, y, &height, + &usedWidth)) continue; // align the width only after tightening, so alignment is applied once diff --git a/common/tests/test_displays.c b/common/tests/test_displays.c index 0d5f22fc..675b9f0b 100644 --- a/common/tests/test_displays.c +++ b/common/tests/test_displays.c @@ -50,7 +50,8 @@ static void run(const char * title, const LGDisplayInfo * in, unsigned count) unsigned aw, ah, dw, dh; printf("\n== %s ==\n", title); - if (!lgDisplaysPack(in, count, 1, out, &aw, &ah, &dw, &dh)) + if (!lgDisplaysPack(in, count, LG_DISPLAY_ALIGN_PIXELS(4), out, + &aw, &ah, &dw, &dh)) { printf(" [FAIL] pack returned false\n"); ++failures; @@ -72,6 +73,24 @@ static void run(const char * title, const LGDisplayInfo * in, unsigned count) 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 diff --git a/doc/multimonitor.rst b/doc/multimonitor.rst index 5403715c..c960e886 100644 --- a/doc/multimonitor.rst +++ b/doc/multimonitor.rst @@ -152,6 +152,78 @@ Be aware that when showing one display of several, the guest's cursor can move onto a display you are not watching, and will appear to vanish. Show the whole desktop, or run a client per display, if that is a problem. +.. _multimonitor_consuming: + +Consuming frames directly +------------------------- + +The packed frame is designed so a consumer other than the Looking Glass +client — a custom compositor, for instance — can use each display's region +without copying it out first. This section is the contract such a consumer +can rely on. + +Layout +^^^^^^ + +``KVMFRFrame::displays`` holds ``displayCount`` entries. Each gives the +display's rectangle in the packed frame (``atlasX/Y/Width/Height``) and where +it belongs on the guest's virtual desktop (``desktopX/Y/Width/Height``). The +two rectangles are always the same size: packing relocates a display, it never +scales it. Rotation is already applied to the pixels and is informational. + +A display's base address within the frame is:: + + atlasY * pitch + atlasX * bytesPerPixel + +.. _multimonitor_alignment: + +Alignment guarantee +^^^^^^^^^^^^^^^^^^^ + +Both ``atlasX`` and the frame's width are aligned so that every display's base +address is a multiple of 256 bytes, which is what display controllers +typically require of a scanout buffer. A consumer can therefore create one +framebuffer per display, all pointing into the same underlying memory at +different offsets, and page-flip them independently. + +This holds for the 32 and 64 bit-per-pixel formats. It cannot be relied on for +``FRAME_TYPE_RGB_24``, which most display hardware cannot scan out regardless. + +Frame lifetime +^^^^^^^^^^^^^^ + +A consumer that scans out directly holds a frame for at least a full vblank, +rather than copying it and releasing immediately. That is safe, but the limits +are worth knowing: + +* The frame queue is **two** buffers deep. Pinning one leaves the host a + single spare, so the host blocks waiting for room rather than overwriting a + frame still in use. It will not recycle a buffer out from under a consumer + that is keeping up. +* The subscriber timeout is **one second**. A consumer that holds a frame + longer than that is considered dead and its subscription is dropped. A + vblank is far inside that, but a consumer stalled across a mode set should + expect to reconnect rather than assume its frame is still valid. + +The practical consequence is that the host cannot run ahead of a pinning +consumer. For direct scanout that is the desired behaviour: it is what stops +stale frames queueing up. + +Damage +^^^^^^ + +Damage rectangles are always in packed frame coordinates, so they can be +intersected against each display's atlas rectangle to find which displays +changed, and a display with no intersecting damage can skip its flip +entirely. A ``damageRectsCount`` of zero means the whole frame changed. + +Format changes +^^^^^^^^^^^^^^ + +Any change to the display arrangement bumps ``formatVer``. A consumer must +re-read the layout when it changes rather than caching it for the life of the +session, since both the atlas dimensions and each display's position can move. + .. _multimonitor_limitations: Limitations diff --git a/host/platform/Linux/capture/XCB/src/xcb.c b/host/platform/Linux/capture/XCB/src/xcb.c index 4dc5d49d..e0d2f80e 100644 --- a/host/platform/Linux/capture/XCB/src/xcb.c +++ b/host/platform/Linux/capture/XCB/src/xcb.c @@ -276,7 +276,8 @@ haveDisplays: this->srcY[i] = info[i].y; } - if (!lgDisplaysPack(info, count, 1, this->displays, + // BGRA is 4 bytes per pixel; align so a consumer can scan out from the atlas + if (!lgDisplaysPack(info, count, LG_DISPLAY_ALIGN_PIXELS(4), this->displays, &this->atlasWidth, &this->atlasHeight, &this->desktopWidth, &this->desktopHeight)) { diff --git a/host/platform/Windows/capture/DXGI/src/dxgi.c b/host/platform/Windows/capture/DXGI/src/dxgi.c index b65da413..3c92d88a 100644 --- a/host/platform/Windows/capture/DXGI/src/dxgi.c +++ b/host/platform/Windows/capture/DXGI/src/dxgi.c @@ -434,8 +434,23 @@ static bool dxgi_initMultiMonitor(void) goto fail; } + /* Align each display so a consumer can point display hardware straight at + * its region of the atlas rather than copying it out. */ + unsigned bpp; + switch(this->dxgiSrcFormat) + { + case DXGI_FORMAT_R16G16B16A16_FLOAT: + bpp = 8; + break; + + default: + bpp = 4; + break; + } + unsigned atlasWidth, atlasHeight, desktopWidth, desktopHeight; - if (!lgDisplaysPack(info, count, 1, this->displays, + if (!lgDisplaysPack(info, count, LG_DISPLAY_ALIGN_PIXELS(bpp), + this->displays, &atlasWidth, &atlasHeight, &desktopWidth, &desktopHeight)) { DEBUG_WARN("Failed to pack %u outputs, capturing a single display", count);