Files
looking-glass-2/client/renderers/EGL/compose.c
T
donny dfe8edfbc2
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
Add multi-monitor support via packed atlas frames
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.
2026-07-28 07:06:32 +00:00

286 lines
8.5 KiB
C

/**
* 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 "compose.h"
#include "framebuffer.h"
#include "shader.h"
#include "state.h"
#include "texture.h"
#include "common/debug.h"
#include <stdlib.h>
#include <string.h>
#include <GLES3/gl3.h>
#include "compose.vert.h"
#include "compose.frag.h"
/* Two triangles per display, as an indexed quad. */
#define VERTS_PER_DISPLAY 4
#define INDICES_PER_DISPLAY 6
/* each vertex is x, y, u, v */
#define FLOATS_PER_VERTEX 4
struct EGL_Compose
{
EGL_Framebuffer * framebuffer;
EGL_Shader * shader;
EGL_Uniform * uViewSize;
EGL_Uniform * uAtlasSize;
GLuint vao;
GLuint buffers[2];
bool configured;
unsigned int viewWidth, viewHeight;
unsigned int atlasWidth, atlasHeight;
int quadCount;
};
bool egl_composeInit(EGL_Compose ** compose)
{
*compose = calloc(1, sizeof(**compose));
if (!*compose)
{
DEBUG_ERROR("Failed to allocate the display composition");
return false;
}
EGL_Compose * this = *compose;
if (!egl_framebufferInit(&this->framebuffer) ||
!egl_shaderInit(&this->shader) ||
!egl_shaderCompile(this->shader,
b_shader_compose_vert, b_shader_compose_vert_size,
b_shader_compose_frag, b_shader_compose_frag_size,
false, NULL))
{
DEBUG_ERROR("Failed to initialize the display composition");
egl_composeFree(compose);
return false;
}
this->uViewSize = egl_shaderGetUniform(this->shader, "viewSize" );
this->uAtlasSize = egl_shaderGetUniform(this->shader, "atlasSize");
glGenVertexArrays(1, &this->vao);
egl_stateBindVertexArray(this->vao);
glGenBuffers(2, this->buffers);
egl_stateBindBuffer(GL_ARRAY_BUFFER, this->buffers[0]);
glBufferData(GL_ARRAY_BUFFER,
KVMFR_MAX_DISPLAYS * VERTS_PER_DISPLAY * FLOATS_PER_VERTEX *
sizeof(GLfloat),
NULL, GL_STREAM_DRAW);
// location 0 is the view position, location 1 the atlas position
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE,
FLOATS_PER_VERTEX * sizeof(GLfloat), (void *)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
FLOATS_PER_VERTEX * sizeof(GLfloat),
(void *)(2 * sizeof(GLfloat)));
GLushort indices[KVMFR_MAX_DISPLAYS * INDICES_PER_DISPLAY];
for(int i = 0; i < KVMFR_MAX_DISPLAYS; ++i)
{
indices[INDICES_PER_DISPLAY * i + 0] = VERTS_PER_DISPLAY * i + 0;
indices[INDICES_PER_DISPLAY * i + 1] = VERTS_PER_DISPLAY * i + 1;
indices[INDICES_PER_DISPLAY * i + 2] = VERTS_PER_DISPLAY * i + 2;
indices[INDICES_PER_DISPLAY * i + 3] = VERTS_PER_DISPLAY * i + 0;
indices[INDICES_PER_DISPLAY * i + 4] = VERTS_PER_DISPLAY * i + 2;
indices[INDICES_PER_DISPLAY * i + 5] = VERTS_PER_DISPLAY * i + 3;
}
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->buffers[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof indices, indices,
GL_STATIC_DRAW);
return true;
}
void egl_composeFree(EGL_Compose ** compose)
{
if (!*compose)
return;
EGL_Compose * this = *compose;
if (this->vao)
{
glDeleteVertexArrays(1, &this->vao);
glDeleteBuffers(2, this->buffers);
egl_stateInvalidateShared();
}
egl_shaderFree(&this->shader);
egl_framebufferFree(&this->framebuffer);
free(this);
*compose = NULL;
}
bool egl_composeSetup(EGL_Compose * this, const LG_RendererFormat * format)
{
this->configured = false;
this->quadCount = 0;
if (!format->displayCount)
return false;
if (format->displayCount > KVMFR_MAX_DISPLAYS)
{
DEBUG_ERROR("Frame reports %u displays, the maximum is %d",
format->displayCount, KVMFR_MAX_DISPLAYS);
return false;
}
if (!format->viewWidth || !format->viewHeight)
{
DEBUG_ERROR("Frame reports an empty view region");
return false;
}
/* The composed image is the same pixel format as the atlas, so a display is
* copied without conversion and HDR handling downstream is unaffected. */
enum EGL_PixelFormat pixFmt;
switch(format->type)
{
case FRAME_TYPE_BGRA : pixFmt = EGL_PF_BGRA ; break;
case FRAME_TYPE_RGBA : pixFmt = EGL_PF_RGBA ; break;
case FRAME_TYPE_RGBA10 : pixFmt = EGL_PF_RGBA10 ; break;
case FRAME_TYPE_RGBA16F: pixFmt = EGL_PF_RGBA16F; break;
default:
DEBUG_ERROR("Frame type %d cannot be composed", format->type);
return false;
}
if (!egl_framebufferSetup(this->framebuffer, pixFmt,
format->viewWidth, format->viewHeight))
{
DEBUG_ERROR("Failed to create the %ux%u composition target",
format->viewWidth, format->viewHeight);
return false;
}
this->viewWidth = format->viewWidth;
this->viewHeight = format->viewHeight;
this->atlasWidth = format->frameWidth;
this->atlasHeight = format->frameHeight;
/* Build one quad per display that intersects the view, mapping its atlas
* rectangle onto its place in the view. Displays fully outside the view are
* skipped, which is what makes selecting a single display cost nothing
* extra to draw. */
GLfloat verts[KVMFR_MAX_DISPLAYS * VERTS_PER_DISPLAY * FLOATS_PER_VERTEX];
int quads = 0;
for(unsigned i = 0; i < format->displayCount; ++i)
{
const KVMFRDisplay * d = &format->displays[i];
// position within the view, which may be a single display's region
const int vx = d->desktopX - format->viewX;
const int vy = d->desktopY - format->viewY;
if (vx + (int)d->desktopWidth <= 0 ||
vy + (int)d->desktopHeight <= 0 ||
vx >= (int)format->viewWidth ||
vy >= (int)format->viewHeight)
continue;
if (d->atlasX + d->atlasWidth > format->frameWidth ||
d->atlasY + d->atlasHeight > format->frameHeight)
{
DEBUG_ERROR("Display %u (%s) lies outside the %ux%u frame",
i, d->name, format->frameWidth, format->frameHeight);
return false;
}
const GLfloat x0 = vx, y0 = vy;
const GLfloat x1 = vx + (GLfloat)d->desktopWidth;
const GLfloat y1 = vy + (GLfloat)d->desktopHeight;
const GLfloat u0 = d->atlasX, v0 = d->atlasY;
const GLfloat u1 = d->atlasX + (GLfloat)d->atlasWidth;
const GLfloat v1 = d->atlasY + (GLfloat)d->atlasHeight;
GLfloat * v = verts + quads * VERTS_PER_DISPLAY * FLOATS_PER_VERTEX;
v[ 0] = x0; v[ 1] = y0; v[ 2] = u0; v[ 3] = v0;
v[ 4] = x1; v[ 5] = y0; v[ 6] = u1; v[ 7] = v0;
v[ 8] = x1; v[ 9] = y1; v[10] = u1; v[11] = v1;
v[12] = x0; v[13] = y1; v[14] = u0; v[15] = v1;
++quads;
}
if (!quads)
{
DEBUG_ERROR("No display intersects the requested view region");
return false;
}
egl_stateBindVertexArray(this->vao);
egl_stateBindBuffer(GL_ARRAY_BUFFER, this->buffers[0]);
glBufferSubData(GL_ARRAY_BUFFER, 0,
quads * VERTS_PER_DISPLAY * FLOATS_PER_VERTEX * sizeof(GLfloat), verts);
this->quadCount = quads;
this->configured = true;
return true;
}
EGL_Texture * egl_composeRun(EGL_Compose * this, EGL_Texture * atlas)
{
if (!this->configured)
return NULL;
egl_framebufferBind(this->framebuffer);
/* Displays never overlap and the view is not guaranteed to be fully
* covered, so clear first to keep any gap between monitors black rather
* than showing the previous frame there. */
egl_stateBlend(false);
egl_stateScissor(false);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
egl_textureBind(atlas);
egl_shaderUse(this->shader);
egl_uniform2f(this->uViewSize , this->viewWidth , this->viewHeight );
egl_uniform2f(this->uAtlasSize, this->atlasWidth, this->atlasHeight);
egl_stateBindVertexArray(this->vao);
glDrawElements(GL_TRIANGLES, this->quadCount * INDICES_PER_DISPLAY,
GL_UNSIGNED_SHORT, NULL);
return egl_framebufferGetTexture(this->framebuffer);
}
void egl_composeGetSize(EGL_Compose * this,
unsigned int * width, unsigned int * height)
{
*width = this->viewWidth;
*height = this->viewHeight;
}