Add multi-monitor support via packed atlas frames
build / client (Debug, map[cc:clang cxx:clang++], libdecor) (push) Failing after 27s
build / client (Debug, map[cc:gcc cxx:g++], libdecor) (push) Failing after 28s
build / client (Debug, map[cc:clang cxx:clang++], xdg-shell) (push) Failing after 28s
build / client (Debug, map[cc:gcc cxx:g++], xdg-shell) (push) Failing after 21s
build / client (Release, map[cc:clang cxx:clang++], libdecor) (push) Failing after 20s
build / client (Release, map[cc:clang cxx:clang++], xdg-shell) (push) Failing after 21s
build / module (push) Failing after 16s
build / client (Release, map[cc:gcc cxx:g++], xdg-shell) (push) Failing after 19s
build / client (Release, map[cc:gcc cxx:g++], libdecor) (push) Failing after 20s
build / host-linux (push) Failing after 19s
build / host-windows-cross (push) Failing after 17s
build / obs (clang) (push) Failing after 18s
build / docs (push) Failing after 7s
build / obs (gcc) (push) Failing after 20s
build / host-windows-native (push) Canceled after 0s
build / idd (push) Canceled after 0s

Capture every guest display into a single frame and compose it back into
the guest's desktop on the client, so one Looking Glass session can carry
a multi-monitor guest instead of one instance per display.

KVMFR gains a per-frame display layout (version 24). A frame with
displayCount == 0 is a conventional single-display frame and behaves
exactly as before, so existing capture backends are unaffected.

- common: shared display layout and a shelf packer that minimises atlas
  area, plus atlas/desktop coordinate mapping helpers and unit tests
- host: capture interface and host app carry the layout; a layout change
  invalidates the format so clients re-read it
- host/XCB: enumerate RandR monitors and repack the root window into the
  atlas directly in shared memory
- client/EGL: compose the atlas into the presented view as its own pass,
  leaving post processing, HDR, cursor and damage handling unchanged

Also fixes the XCB backend writing a single row per frame instead of the
whole image.
This commit is contained in:
donny committed 2026-07-28 07:06:32 +00:00
1 parent 4ef6672e64
commit dfe8edfbc2
18 files changed
+1363 -5

No files matched your search

+288
View File
@@ -0,0 +1,288 @@
/**
* 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
*/
#include "common/displays.h"
#include "common/debug.h"
#include <string.h>
/* Place displays onto shelves of the given width, in the supplied order.
*
* This is first-fit decreasing-height shelf packing: with `order` sorted by
* descending height, the first display on a shelf is its tallest, so the shelf
* height is known as soon as it is opened and no display is ever placed above
* a taller neighbour. It is not optimal in general, but monitor sets are tiny
* and usually uniform, where it is exact.
*
* Returns false if any display is wider than the shelf width. */
static bool packWithWidth(
const LGDisplayInfo * in,
const unsigned * order,
unsigned count,
unsigned width,
unsigned * outX,
unsigned * outY,
unsigned * outHeight,
unsigned * outUsedWidth)
{
unsigned shelfX = 0, shelfY = 0, shelfHeight = 0, usedWidth = 0;
for(unsigned i = 0; i < count; ++i)
{
const unsigned idx = order[i];
const unsigned w = in[idx].width;
const unsigned h = in[idx].height;
if (w > width)
return false;
if (shelfX + w > width)
{
// no room left on this shelf, open a new one above it
shelfY += shelfHeight;
shelfX = 0;
shelfHeight = 0;
}
outX[idx] = shelfX;
outY[idx] = shelfY;
shelfX += w;
if (h > shelfHeight)
shelfHeight = h;
if (shelfX > usedWidth)
usedWidth = shelfX;
}
*outHeight = shelfY + shelfHeight;
*outUsedWidth = usedWidth;
return true;
}
bool lgDisplaysPack(
const LGDisplayInfo * in,
unsigned count,
unsigned alignment,
KVMFRDisplay * out,
unsigned * atlasWidth,
unsigned * atlasHeight,
unsigned * desktopWidth,
unsigned * desktopHeight)
{
if (!in || !out || count == 0 || count > KVMFR_MAX_DISPLAYS)
return false;
if (alignment == 0)
alignment = 1;
for(unsigned i = 0; i < count; ++i)
if (in[i].width == 0 || in[i].height == 0)
{
DEBUG_ERROR("Display %u (%s) has a zero dimension (%ux%u)",
i, in[i].name, in[i].width, in[i].height);
return false;
}
/* sort indices by descending height, breaking ties on descending width and
* then ascending id so the layout is stable across frames */
unsigned order[KVMFR_MAX_DISPLAYS];
for(unsigned i = 0; i < count; ++i)
order[i] = i;
for(unsigned i = 1; i < count; ++i)
{
const unsigned v = order[i];
unsigned j = i;
while(j > 0)
{
const LGDisplayInfo * a = &in[order[j - 1]];
const LGDisplayInfo * b = &in[v];
const bool swap =
(b->height > a->height) ||
(b->height == a->height && b->width > a->width) ||
(b->height == a->height && b->width == a->width && b->id < a->id);
if (!swap)
break;
order[j] = order[j - 1];
--j;
}
order[j] = v;
}
unsigned maxWidth = 0, totalWidth = 0;
for(unsigned i = 0; i < count; ++i)
{
if (in[i].width > maxWidth)
maxWidth = in[i].width;
totalWidth += in[i].width;
}
/* Candidate shelf widths: the widest single display (a pure vertical stack),
* every running total in packing order (which produces the natural "N per
* row" arrangements), and the sum of all widths (a single row). Every
* candidate is at least maxWidth so no display is ever too wide to place. */
unsigned candidates[KVMFR_MAX_DISPLAYS + 2];
unsigned candidateCount = 0;
candidates[candidateCount++] = maxWidth;
unsigned running = 0;
for(unsigned i = 0; i < count; ++i)
{
running += in[order[i]].width;
candidates[candidateCount++] = running < maxWidth ? maxWidth : running;
}
candidates[candidateCount++] = totalWidth < maxWidth ? maxWidth : totalWidth;
unsigned bestX[KVMFR_MAX_DISPLAYS], bestY[KVMFR_MAX_DISPLAYS];
unsigned bestWidth = 0, bestHeight = 0;
uint64_t bestArea = 0;
bool haveBest = false;
for(unsigned c = 0; c < candidateCount; ++c)
{
const unsigned width = candidates[c];
if (width > LG_DISPLAY_MAX_ATLAS_EDGE)
continue;
unsigned x[KVMFR_MAX_DISPLAYS], y[KVMFR_MAX_DISPLAYS];
unsigned height, usedWidth;
if (!packWithWidth(in, order, count, width, x, y, &height, &usedWidth))
continue;
// align the width only after tightening, so alignment is applied once
unsigned alignedWidth =
((usedWidth + alignment - 1) / alignment) * alignment;
if (alignedWidth > LG_DISPLAY_MAX_ATLAS_EDGE ||
height > LG_DISPLAY_MAX_ATLAS_EDGE)
continue;
const uint64_t area = (uint64_t)alignedWidth * height;
const unsigned edge = alignedWidth > height ? alignedWidth : height;
const unsigned bestEdge = bestWidth > bestHeight ? bestWidth : bestHeight;
/* prefer the smallest atlas; on a tie prefer the squarer one, which is
* kinder to texture size limits and to the GPU's cache */
if (!haveBest || area < bestArea || (area == bestArea && edge < bestEdge))
{
memcpy(bestX, x, sizeof(*x) * count);
memcpy(bestY, y, sizeof(*y) * count);
bestWidth = alignedWidth;
bestHeight = height;
bestArea = area;
haveBest = true;
}
}
if (!haveBest)
{
DEBUG_ERROR("No atlas layout for %u displays fits within %ux%u",
count, LG_DISPLAY_MAX_ATLAS_EDGE, LG_DISPLAY_MAX_ATLAS_EDGE);
return false;
}
/* normalise the virtual desktop so its bounding box starts at (0, 0) */
int32_t minX = in[0].x, minY = in[0].y;
int64_t maxX = (int64_t)in[0].x + in[0].width;
int64_t maxY = (int64_t)in[0].y + in[0].height;
for(unsigned i = 1; i < count; ++i)
{
if (in[i].x < minX)
minX = in[i].x;
if (in[i].y < minY)
minY = in[i].y;
if ((int64_t)in[i].x + in[i].width > maxX)
maxX = (int64_t)in[i].x + in[i].width;
if ((int64_t)in[i].y + in[i].height > maxY)
maxY = (int64_t)in[i].y + in[i].height;
}
for(unsigned i = 0; i < count; ++i)
{
KVMFRDisplay * d = &out[i];
memset(d, 0, sizeof(*d));
d->id = in[i].id;
d->flags = in[i].primary ? KVMFR_DISPLAY_FLAG_PRIMARY : 0;
d->atlasX = bestX[i];
d->atlasY = bestY[i];
d->atlasWidth = in[i].width;
d->atlasHeight = in[i].height;
d->desktopX = in[i].x - minX;
d->desktopY = in[i].y - minY;
d->desktopWidth = in[i].width;
d->desktopHeight = in[i].height;
d->rotation = in[i].rotation;
memcpy(d->name, in[i].name, sizeof(d->name) - 1);
d->name[sizeof(d->name) - 1] = '\0';
}
*atlasWidth = bestWidth;
*atlasHeight = bestHeight;
*desktopWidth = (unsigned)(maxX - minX);
*desktopHeight = (unsigned)(maxY - minY);
return true;
}
int lgDisplaysFindAtPoint(const KVMFRDisplay * displays, unsigned count,
int32_t x, int32_t y)
{
for(unsigned i = 0; i < count; ++i)
{
const KVMFRDisplay * d = &displays[i];
if (x >= d->desktopX && x < d->desktopX + (int32_t)d->desktopWidth &&
y >= d->desktopY && y < d->desktopY + (int32_t)d->desktopHeight)
return (int)i;
}
return -1;
}
bool lgDisplaysDesktopToAtlas(const KVMFRDisplay * displays, unsigned count,
int32_t dx, int32_t dy, int32_t * ax, int32_t * ay)
{
const int index = lgDisplaysFindAtPoint(displays, count, dx, dy);
if (index < 0)
return false;
const KVMFRDisplay * d = &displays[index];
*ax = (int32_t)d->atlasX + (dx - d->desktopX);
*ay = (int32_t)d->atlasY + (dy - d->desktopY);
return true;
}
bool lgDisplaysAtlasToDesktop(const KVMFRDisplay * displays, unsigned count,
int32_t ax, int32_t ay, int32_t * dx, int32_t * dy)
{
for(unsigned i = 0; i < count; ++i)
{
const KVMFRDisplay * d = &displays[i];
if (ax >= (int32_t)d->atlasX &&
ax < (int32_t)d->atlasX + (int32_t)d->atlasWidth &&
ay >= (int32_t)d->atlasY &&
ay < (int32_t)d->atlasY + (int32_t)d->atlasHeight)
{
*dx = d->desktopX + (ax - (int32_t)d->atlasX);
*dy = d->desktopY + (ay - (int32_t)d->atlasY);
return true;
}
}
return false;
}