/** * 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 /* 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 alignment, 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; /* 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 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; for(unsigned i = 0; i < count; ++i) if (in[i].width > maxWidth) maxWidth = in[i].width; /* Candidate shelf widths: the total width of every subset of displays. * * Any shelf is some set of displays sitting side by side, so its width is a * subset sum; enumerating them covers every arrangement worth trying. Only * considering running totals in packing order misses layouts where the * displays that share a shelf are not the first few — a 4K above two 1440p * side by side, for instance, since the 4K sorts first but belongs alone. * * With at most 8 displays this is 255 subsets, which costs nothing. Every * candidate is raised to maxWidth so no display is ever too wide to place. */ unsigned candidates[1u << KVMFR_MAX_DISPLAYS]; unsigned candidateCount = 0; for(uint32_t mask = 1; mask < (1u << count); ++mask) { unsigned width = 0; for(unsigned i = 0; i < count; ++i) if (mask & (1u << i)) { // 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; // duplicates are common when displays share a width, so drop them bool seen = false; for(unsigned c = 0; c < candidateCount; ++c) if (candidates[c] == width) { seen = true; break; } if (!seen) candidates[candidateCount++] = width; } 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, alignment, 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; } 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); } } bool lgDisplaysRectToAtlas(const KVMFRDisplay * d, int32_t x, int32_t y, int32_t width, int32_t height, FrameDamageRect * out) { if (width <= 0 || height <= 0) return false; int64_t x0 = x, y0 = y; int64_t x1 = (int64_t)x + width; int64_t y1 = (int64_t)y + height; // clip to the display it came from if (x0 < 0) x0 = 0; if (y0 < 0) y0 = 0; if (x1 > (int64_t)d->atlasWidth ) x1 = d->atlasWidth; if (y1 > (int64_t)d->atlasHeight) y1 = d->atlasHeight; if (x0 >= x1 || y0 >= y1) return false; out->x = (uint32_t)(d->atlasX + x0); out->y = (uint32_t)(d->atlasY + y0); out->width = (uint32_t)(x1 - x0); out->height = (uint32_t)(y1 - y0); 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; }