LordOS — a free, fast and secure GNU/Linux distribution based on Debian, with its own desktop environment: display server and Wayland compositor in C, a React UI engine on QuickJS, and private native apps.
C cdebiandesktop-environmentebpffirewalllandlocklinuxlinux-distributionopen-sourceprivacyquickjsraspberry-pi-5reactsecuritywaylandwayland-compositor
CPU path: heavy frames paint on a pool of up to 8 threads (par.c): the backdrop blur in phases, row-local primitives as the same call per band of rows, the remote desktop's tile comparison and slot copies; same pixels as one thread. On only while painting takes over 70 % of a frame at the target rate: a 1080p session 45 → 55–58 fps on VM 116
Federico Pereiradbc2d93eefeb
diff --git a/docs/GPU.md b/docs/GPU.mdindex 59dcc95..3b463d0 100644--- a/docs/GPU.md+++ b/docs/GPU.md@@ -88,6 +88,23 @@ line from a real VMware guest.moving the mouse composites nothing. If the driver has no cursor plane, it's drawn as a quad(`LORD_SW_CURSOR=1` forces it).+## The CPU path on several threads (0.8.5)++Without a GPU (a virtual machine like VM 116, a board without a working driver) the CPU path paints+every frame. Its heavy pixel loops run on a small pool of threads (`server/src/par.c`, up to 8): the+backdrop blur in phases (downsample, the box passes by rows and by columns, the upsample), and the+row-local primitives (fills, strokes, gradients, blits, shadow masks) as the same call on each+thread with the clip cut to a band of rows; the remote desktop's tile comparison and the copies into+the encoder's slots too. The node tree, caches and everything else stay on the main thread, and the+pixels are the same as on one thread (`blurbench`/`rrbench` compare them byte for byte).++Waking the pool costs CPU, so it is used only while frames are heavy: it turns on when painting takes+more than 70 % of a frame at the target rate (the monitor's refresh, or the fastest remote viewer's+screen without a monitor) and off after 60 light frames in a row. On VM 116 (8 cores, Xeon 1.8 GHz), a+1920×1080 session with a 60 fps video under blurred windows: 45 frames a second on one thread, 55–58+with the pool. `canvas.c` builds at -O3 (its loops vectorize); the blur itself went from 28 ms to+2.7 ms over the day's work.+## When the GPU context is re-createdThe EGL/GBM context does not live as long as the session. It is torn down and made again when thediff --git a/server/Makefile b/server/Makefileindex b8c4383..8863a51 100644--- a/server/Makefile+++ b/server/Makefile@@ -55,7 +55,7 @@ MEDIA_SRC := src/media/media.c src/media/hwdec.c src/media/mview.c src/media/capSEC_SRC := src/sec/json.c src/sec/sign.c src/sec/pkgstate.c src/sec/perms.c src/sec/cgroup.c src/sec/sandbox.c src/sec/brokersrv.c src/sec/brokercam.c src/sec/brokerkr.c src/sec/brokermail.c src/sec/restrict.c src/sec/appid.c src/sec/pwguard.c src/sec/codesig.c# App mode: lord-server --app <id> runs an .lpk app as an isolated Wayland client (src/app)APP_SRC := src/app/appclient.c src/app/appspawn.c src/ui/js_broker.c src/ui/js_brokercam.c src/ui/js_brokermail.c src/app/apphost_bc.c-SRC := src/main.c src/kms.c src/gpu/gpu.c src/input.c src/canvas.c src/admin.c src/install/install.c src/apps.c src/lpk.c src/fsops.c src/remote/mirror.c src/remote/remote_admin.c src/remote/ipnet.c src/remote/rtcwire.c src/remote/ssh_admin.c src/users/users_admin.c src/users/homedirs.c src/keyboard.c src/keylag.c src/ime.c src/wl/wl.c src/wl/x11wire.c src/wl/xwm.c src/wl/xwayland.c \+SRC := src/main.c src/kms.c src/gpu/gpu.c src/input.c src/canvas.c src/par.c src/admin.c src/install/install.c src/apps.c src/lpk.c src/fsops.c src/remote/mirror.c src/remote/remote_admin.c src/remote/ipnet.c src/remote/rtcwire.c src/remote/ssh_admin.c src/users/users_admin.c src/users/homedirs.c src/keyboard.c src/keylag.c src/ime.c src/wl/wl.c src/wl/x11wire.c src/wl/xwm.c src/wl/xwayland.c \src/ui/node.c src/ui/style.c src/ui/layout.c src/ui/transform.c src/ui/text.c src/ui/image.c \src/ui/paint.c src/ui/js.c src/ui/js_sys.c src/ui/js_term.c src/ui/js_wasm.c src/wasm/wasm.c src/wasm/wasm_run.c src/ui/js_sound.c src/ui/js_net.c src/sound/snd.c src/sound/pulse.c src/ui/otf.c src/ui/fonts.c \src/ui/unicode.c src/ui/bidi.c src/ui/shape.c src/ui/shape_indic.c src/ui/shape_syl.c src/ui/shape_khmer.c src/ui/shape_myanmar.c src/ui/shape_use.c src/ui/thbrk.c src/ui/textlayout.c \diff --git a/server/src/canvas.c b/server/src/canvas.cindex cc7e675..514cb6b 100644--- a/server/src/canvas.c+++ b/server/src/canvas.c@@ -196,7 +196,49 @@ void canvas_clear(Canvas *c, uint32_t rgb) {}}+/* ---- parallel rows (lord-server sets canvas_parallel; tests and helpers stay serial) ----+ * The row-local primitives (every pixel depends only on its own row: fills, strokes, gradients,+ * blits, shadow masks) run as the same call on each thread with the clip cut to a band of rows,+ * so the pixels are the same as on one thread. */+static CanvasPar par_fn;+static _Thread_local int in_band;+void canvas_parallel(CanvasPar fn) { par_fn = fn; }+int canvas_parallel_on(void) { return par_fn != NULL; }++typedef struct { const Canvas *c; void (*draw)(Canvas *, const void *); const void *a; } Band;+static void band_run(void *ctx, int j0, int j1) {+ const Band *b = ctx;+ Canvas c = *b->c;+ if (j0 > c.clip.y0) c.clip.y0 = j0;+ if (j1 < c.clip.y1) c.clip.y1 = j1;+ in_band = 1;+ if (c.clip.y0 < c.clip.y1) b->draw(&c, b->a);+ in_band = 0;+}+/* Worth splitting: a pool, not already in a band, and enough pixels inside the clip. */+static int banded(const Canvas *c, float x, float y, float w, float h) {+ if (!par_fn || in_band) return 0;+ int x0, y0, x1, y1;+ if (!bounds(c, x, y, w, h, &x0, &y0, &x1, &y1)) return 0;+ /* waking the pool costs tens of µs: only big jobs (about 512×512 and up) are worth it */+ return y1 - y0 >= 64 && (long)(x1 - x0) * (y1 - y0) >= 262144;+}+static void in_bands(const Canvas *c, float y, float h, void (*draw)(Canvas *, const void *), const void *a) {+ Band b = { c, draw, a };+ int y0 = clampi((int)floorf(y), c->clip.y0, c->clip.y1), y1 = clampi((int)ceilf(y + h), c->clip.y0, c->clip.y1);+ par_fn(y0, y1, band_run, &b);+}++typedef struct { float x, y, w, h, r, bw, angle, opacity; uint32_t argb, to; } ShapeArgs;+static void rrect_band(Canvas *c, const void *p) { const ShapeArgs *a = p; canvas_rrect(c, a->x, a->y, a->w, a->h, a->r, a->argb); }+static void stroke_band(Canvas *c, const void *p) { const ShapeArgs *a = p; canvas_rrect_stroke(c, a->x, a->y, a->w, a->h, a->r, a->bw, a->argb); }+static void gradient_band(Canvas *c, const void *p) {+ const ShapeArgs *a = p;+ canvas_rrect_gradient(c, a->x, a->y, a->w, a->h, a->r, a->argb, a->to, a->angle, a->opacity);+}+void canvas_rrect(Canvas *c, float x, float y, float w, float h, float r, uint32_t argb) {+ if (banded(c, x, y, w, h)) { ShapeArgs a = { x, y, w, h, r, 0, 0, 0, argb, 0 }; in_bands(c, y, h, rrect_band, &a); return; }uint32_t a = argb >> 24;int x0, y0, x1, y1;if (!a || w <= 0 || h <= 0 || !bounds(c, x, y, w, h, &x0, &y0, &x1, &y1)) return;@@ -251,6 +293,11 @@ static inline uint32_t lerp_color(uint32_t a, uint32_t b, float t) {void canvas_rrect_gradient(Canvas *c, float x, float y, float w, float h, float r,uint32_t from, uint32_t to, float angle_deg, float opacity) {+ if (banded(c, x, y, w, h)) {+ ShapeArgs a = { x, y, w, h, r, 0, angle_deg, opacity, from, to };+ in_bands(c, y, h, gradient_band, &a);+ return;+ }int x0, y0, x1, y1;if (w <= 0 || h <= 0 || !bounds(c, x, y, w, h, &x0, &y0, &x1, &y1)) return;r = fix_radius(r, w, h);@@ -275,6 +322,7 @@ void canvas_rrect_gradient(Canvas *c, float x, float y, float w, float h, float}void canvas_rrect_stroke(Canvas *c, float x, float y, float w, float h, float r, float bw, uint32_t argb) {+ if (banded(c, x, y, w, h)) { ShapeArgs a = { x, y, w, h, r, bw, 0, 0, argb, 0 }; in_bands(c, y, h, stroke_band, &a); return; }uint32_t a = argb >> 24;int x0, y0, x1, y1;if (!a || bw <= 0 || w <= 0 || h <= 0 || !bounds(c, x, y, w, h, &x0, &y0, &x1, &y1)) return;@@ -378,9 +426,72 @@ void canvas_shadow(Canvas *c, float x, float y, float w, float h, float r,/* Separable box blur, 3 passes ≈ Gaussian, edges repeated. Both directions slide a window sum* (the vertical one row by row, for the cache) and divide through a table: dt[sum] = sum / win. */-static void box_rows(uint32_t *buf, int w, int h, int rad, uint32_t *tmp, const uint8_t *dt) {- for (int j = 0; j < h; j++) {- uint32_t *row = buf + (size_t)j * w;+/* The backdrop blur's state, shared by its phases; each phase runs over rows (or columns) that are its+ * own, on the pool when there is enough to do, with its own scratch buffers. */+typedef struct {+ Canvas *c;+ int x0, y0, x1, y1, bx0, by0, bx1, by1, f, sw, sh, rad, cw, in0, in1;+ float x, y, w, h, r, cx, cy, hw, hh, inv;+ uint32_t *buf, *src;+ const uint8_t *dt; /* dt[sum] = sum / win */+ int *sum; /* per column of the vertical pass */+ int *cx0, *cx1, *cfx; /* per output column: the two small columns and the fraction */+} Blur;++static void blur_phase(Blur *b, int n, long work, void (*fn)(void *, int, int)) {+ if (par_fn && !in_band && n >= 8 && work >= 131072) par_fn(0, n, fn, b);+ else fn(b, 0, n);+}++/* the f×f average, row by row (each source pixel read once). Two channels share a 32-bit sum+ * (red|blue, alpha|green: at most 16 × 255 each, f <= 4); f = 1 is a copy. */+static void blur_down(void *ctx, int j0, int j1) {+ Blur *b = ctx;+ const Canvas *c = b->c;+ int f = b->f, sw = b->sw, bx0 = b->bx0, bx1 = b->bx1, by0 = b->by0, by1 = b->by1;+ if (f == 1) {+ for (int j = j0; j < j1; j++) memcpy(b->buf + (size_t)j * sw, c->px + (size_t)(by0 + j) * c->stride + bx0, (size_t)sw * 4);+ return;+ }+ uint32_t *acc = malloc((size_t)sw * 2 * 4);+ if (!acc) return;+ int span = bx1 - bx0, full = span / f;+ for (int j = j0; j < j1; j++) {+ memset(acc, 0, (size_t)sw * 8);+ int rows = by1 - (by0 + j * f) < f ? by1 - (by0 + j * f) : f;+ for (int v = 0; v < rows; v++) {+ const uint32_t *row = c->px + (size_t)(by0 + j * f + v) * c->stride + bx0;+ for (int i = 0; i < full; i++) {+ uint32_t rb = 0, ag = 0;+ const uint32_t *q = row + (size_t)i * f;+ for (int u = 0; u < f; u++) { rb += q[u] & 0x00ff00ffu; ag += (q[u] >> 8) & 0x00ff00ffu; }+ acc[i * 2] += rb;+ acc[i * 2 + 1] += ag;+ }+ for (int px = full * f; px < span; px++) {+ acc[full * 2] += row[px] & 0x00ff00ffu;+ acc[full * 2 + 1] += (row[px] >> 8) & 0x00ff00ffu;+ }+ }+ for (int i = 0; i < sw; i++) {+ int cols = bx1 - (bx0 + i * f) < f ? bx1 - (bx0 + i * f) : f;+ uint32_t n = (uint32_t)(cols * rows), rb = acc[i * 2], ag = acc[i * 2 + 1];+ b->buf[(size_t)j * sw + i] = n ? ((ag >> 16) / n) << 24 | ((rb >> 16) / n) << 16 | ((ag & 0xffff) / n) << 8 | ((rb & 0xffff) / n) : 0;+ }+ }+ free(acc);+}++/* Separable box blur, 3 passes ≈ Gaussian, edges repeated. Both directions slide a window sum (the+ * vertical one row by row over a range of columns, for the cache) and divide through dt. */+static void blur_rows(void *ctx, int j0, int j1) {+ Blur *b = ctx;+ int w = b->sw, rad = b->rad;+ const uint8_t *dt = b->dt;+ uint32_t *tmp = malloc((size_t)w * 4);+ if (!tmp) return;+ for (int j = j0; j < j1; j++) {+ uint32_t *row = b->buf + (size_t)j * w;memcpy(tmp, row, (size_t)w * 4);int sa = 0, sr = 0, sg = 0, sb = 0;for (int k = -rad; k <= rad; k++) {@@ -397,123 +508,59 @@ static void box_rows(uint32_t *buf, int w, int h, int rad, uint32_t *tmp, constsb += (int)(add & 255) - (int)(sub & 255);}}+ free(tmp);+}++static void blur_copy(void *ctx, int j0, int j1) {+ Blur *b = ctx;+ memcpy(b->src + (size_t)j0 * b->sw, b->buf + (size_t)j0 * b->sw, (size_t)(j1 - j0) * b->sw * 4);}-static void box_cols(uint32_t *buf, int w, int h, int rad, uint32_t *src, int *sum, const uint8_t *dt) {- memcpy(src, buf, (size_t)w * h * 4);- memset(sum, 0, (size_t)w * 4 * sizeof(int));+static void blur_cols(void *ctx, int i0, int i1) {+ Blur *b = ctx;+ int w = b->sw, h = b->sh, rad = b->rad;+ const uint8_t *dt = b->dt;+ const uint32_t *src = b->src;+ int *sum = b->sum;+ memset(sum + (size_t)i0 * 4, 0, (size_t)(i1 - i0) * 4 * sizeof(int));for (int k = -rad; k <= rad; k++) {const uint32_t *r = src + (size_t)clampi(k, 0, h - 1) * w;- for (int i = 0; i < w; i++) {+ for (int i = i0; i < i1; i++) {uint32_t p = r[i];int *s = sum + i * 4;s[0] += p >> 24; s[1] += (p >> 16) & 255; s[2] += (p >> 8) & 255; s[3] += p & 255;}}for (int j = 0; j < h; j++) {- uint32_t *out = buf + (size_t)j * w;+ uint32_t *out = b->buf + (size_t)j * w;int aj = j + rad + 1, sj = j - rad;const uint32_t *add = src + (size_t)(aj < h ? aj : h - 1) * w, *sub = src + (size_t)(sj > 0 ? sj : 0) * w;- for (int i = 0; i < w; i++) {+ for (int i = i0; i < i1; i++) {int *s = sum + i * 4;out[i] = (uint32_t)dt[s[0]] << 24 | (uint32_t)dt[s[1]] << 16 | (uint32_t)dt[s[2]] << 8 | dt[s[3]];- uint32_t a = add[i], b = sub[i];- s[0] += (int)(a >> 24) - (int)(b >> 24);- s[1] += (int)((a >> 16) & 255) - (int)((b >> 16) & 255);- s[2] += (int)((a >> 8) & 255) - (int)((b >> 8) & 255);- s[3] += (int)(a & 255) - (int)(b & 255);+ uint32_t a = add[i], d = sub[i];+ s[0] += (int)(a >> 24) - (int)(d >> 24);+ s[1] += (int)((a >> 16) & 255) - (int)((d >> 16) & 255);+ s[2] += (int)((a >> 8) & 255) - (int)((d >> 8) & 255);+ s[3] += (int)(a & 255) - (int)(d & 255);}}}-void canvas_backdrop_blur(Canvas *c, float x, float y, float w, float h, float r, int radius) {- int x0, y0, x1, y1;- if (radius < 1 || w <= 0 || h <= 0 || !bounds(c, x, y, w, h, &x0, &y0, &x1, &y1)) return;- /* Margin so the edge takes color from outside, as in CSS. */- int m = radius * 2;- int bx0 = clampi(x0 - m, 0, c->w), by0 = clampi(y0 - m, 0, c->h);- int bx1 = clampi(x1 + m, 0, c->w), by1 = clampi(y1 + m, 0, c->h);- /* Blurred at reduced resolution: same visual result, much cheaper. */- int f = radius >= 12 ? 4 : radius >= 6 ? 2 : 1;- int sw = (bx1 - bx0 + f - 1) / f, sh = (by1 - by0 + f - 1) / f;- uint32_t *buf = malloc((size_t)sw * sh * 4);- uint32_t *tmp = malloc((size_t)sw * 4);- if (!buf || !tmp) { free(buf); free(tmp); return; }- /* the f×f average, row by row (each source pixel read once). Two channels share a 32-bit sum- * (red|blue, alpha|green: at most 16 × 255 each, f <= 4); f = 1 is a copy. */- if (f == 1) {- for (int j = 0; j < sh; j++) memcpy(buf + (size_t)j * sw, c->px + (size_t)(by0 + j) * c->stride + bx0, (size_t)sw * 4);- } else {- uint32_t *acc = malloc((size_t)sw * 2 * 4);- if (!acc) { free(buf); free(tmp); return; }- int span = bx1 - bx0, full = span / f;- for (int j = 0; j < sh; j++) {- memset(acc, 0, (size_t)sw * 8);- int rows = by1 - (by0 + j * f) < f ? by1 - (by0 + j * f) : f;- for (int v = 0; v < rows; v++) {- const uint32_t *row = c->px + (size_t)(by0 + j * f + v) * c->stride + bx0;- for (int i = 0; i < full; i++) {- uint32_t rb = 0, ag = 0;- const uint32_t *q = row + (size_t)i * f;- for (int u = 0; u < f; u++) { rb += q[u] & 0x00ff00ffu; ag += (q[u] >> 8) & 0x00ff00ffu; }- acc[i * 2] += rb;- acc[i * 2 + 1] += ag;- }- for (int px = full * f; px < span; px++) {- acc[full * 2] += row[px] & 0x00ff00ffu;- acc[full * 2 + 1] += (row[px] >> 8) & 0x00ff00ffu;- }- }- for (int i = 0; i < sw; i++) {- int cols = bx1 - (bx0 + i * f) < f ? bx1 - (bx0 + i * f) : f;- uint32_t n = (uint32_t)(cols * rows), rb = acc[i * 2], ag = acc[i * 2 + 1];- buf[(size_t)j * sw + i] = n ? ((ag >> 16) / n) << 24 | ((rb >> 16) / n) << 16 | ((ag & 0xffff) / n) << 8 | ((rb & 0xffff) / n) : 0;- }- }- free(acc);- }- int rad = radius / (2 * f) > 0 ? radius / (2 * f) : 1;- int win = 2 * rad + 1;- uint8_t *dt = malloc((size_t)255 * win + 1);- uint32_t *src = malloc((size_t)sw * sh * 4);- int *sum = malloc((size_t)sw * 4 * sizeof(int));- if (!dt || !src || !sum) { free(dt); free(src); free(sum); free(buf); free(tmp); return; }- for (int k = 0; k <= 255 * win; k++) dt[k] = (uint8_t)(k / win);- for (int pass = 0; pass < 3; pass++) {- box_rows(buf, sw, sh, rad, tmp, dt);- box_cols(buf, sw, sh, rad, src, sum, dt);- }- free(dt);- free(src);- free(sum);- r = fix_radius(r, w, h);- float cx = x + w * 0.5f, cy = y + h * 0.5f, hw = w * 0.5f, hh = h * 0.5f;- float inv = 1.0f / f;- /* Back to full size: bilinear, in fixed point (8 bits of fraction), with each column's position worked- * out once and each small row interpolated across once (it serves f output rows), not per pixel. */- int cw = x1 - x0;- int *cx0 = malloc((size_t)cw * 3 * sizeof(int)), *hr = malloc((size_t)cw * 8 * sizeof(int));- if (!cx0 || !hr) { free(cx0); free(hr); free(buf); free(tmp); return; }- int *cx1 = cx0 + cw, *cfx = cx1 + cw, *hrow[2] = { hr, hr + (size_t)cw * 4 }, hidx[2] = { -1, -1 };+/* Back to full size, over output rows y0 + [j0, j1): bilinear, in fixed point (8 bits of fraction),+ * with each column's position worked out once and each small row interpolated across once (it serves+ * f output rows), not per pixel. */+static void blur_up(void *ctx, int j0, int j1) {+ Blur *b = ctx;+ Canvas *c = b->c;+ int cw = b->cw, sw = b->sw, sh = b->sh, x0 = b->x0, x1 = b->x1, by0 = b->by0, in0 = b->in0, in1 = b->in1;+ float x = b->x, y = b->y, w = b->w, h = b->h, r = b->r, inv = b->inv;+ int *hr = malloc((size_t)cw * 8 * sizeof(int));uint32_t *vrow = malloc((size_t)cw * 4);- if (!vrow) { free(cx0); free(hr); free(buf); free(tmp); return; }+ if (!hr || !vrow) { free(hr); free(vrow); return; }+ int *hrow[2] = { hr, hr + (size_t)cw * 4 }, hidx[2] = { -1, -1 };uint8_t *vrow8 = (uint8_t *)vrow;- /* the columns whose coverage is whole on rows away from the corners: the same float tests as- * the per-pixel path (i < x + 1, i + 1 > x + w - 1) */- int in0 = x0, in1 = x1;- while (in0 < x1 && in0 < x + 1) in0++;- while (in1 > in0 && in1 - 1 + 1 > x + w - 1) in1--;- if (in0 >= in1) in0 = in1 = x1;- for (int i = x0; i < x1; i++) {- float u = (i - bx0 + 0.5f) * inv - 0.5f;- if (u < 0) u = 0;- if (u > sw - 1) u = (float)(sw - 1);- int a = (int)u;- cx0[i - x0] = a;- cx1[i - x0] = a + 1 < sw ? a + 1 : a;- cfx[i - x0] = (int)((u - a) * 256);- }- for (int j = y0; j < y1; j++) {+ for (int j = b->y0 + j0; j < b->y0 + j1; j++) {uint32_t *row = c->px + (size_t)j * c->stride;float v = (j - by0 + 0.5f) * inv - 0.5f;if (v < 0) v = 0;@@ -526,13 +573,13 @@ void canvas_backdrop_blur(Canvas *c, float x, float y, float w, float h, float r/* reuse the slot that holds neither the other row of this pair nor the top one */int keep = want ? (top == hrow[0] ? 0 : 1) : (hidx[0] == ry1 ? 0 : hidx[1] == ry1 ? 1 : -1);k = keep == 0 ? 1 : 0;- const uint32_t *br = buf + (size_t)ry * sw;- int *h = hrow[k];+ const uint32_t *br = b->buf + (size_t)ry * sw;+ int *hh2 = hrow[k];for (int i = 0; i < cw; i++) {- uint32_t p0 = br[cx0[i]], p1 = br[cx1[i]];+ uint32_t p0 = br[b->cx0[i]], p1 = br[b->cx1[i]];for (int s = 0; s < 4; s++) {- int a = (p0 >> (s * 8)) & 255, b = (p1 >> (s * 8)) & 255;- h[i * 4 + s] = a * 256 + (b - a) * cfx[i];+ int a = (p0 >> (s * 8)) & 255, d = (p1 >> (s * 8)) & 255;+ hh2[i * 4 + s] = a * 256 + (d - a) * b->cfx[i];}}hidx[k] = ry;@@ -558,7 +605,7 @@ void canvas_backdrop_blur(Canvas *c, float x, float y, float w, float h, float r}uint32_t a = 255;if (corner_row || i < x + 1 || i + 1 > x + w - 1) {- float cov = cov_of(rr_sdf(i + 0.5f, j + 0.5f, cx, cy, hw, hh, r));+ float cov = cov_of(rr_sdf(i + 0.5f, j + 0.5f, b->cx, b->cy, b->hw, b->hh, r));if (cov <= 0) continue;a = (uint32_t)(cov * 255 + 0.5f);}@@ -574,11 +621,66 @@ void canvas_backdrop_blur(Canvas *c, float x, float y, float w, float h, float rrow[i] = over_alpha(pa, d >> 24) << 24 | (prb + drb) | (pg + dg);}}- free(cx0);free(hr);free(vrow);- free(buf);- free(tmp);+}++void canvas_backdrop_blur(Canvas *c, float x, float y, float w, float h, float r, int radius) {+ Blur b = { .c = c, .x = x, .y = y, .w = w, .h = h };+ if (radius < 1 || w <= 0 || h <= 0 || !bounds(c, x, y, w, h, &b.x0, &b.y0, &b.x1, &b.y1)) return;+ /* Margin so the edge takes color from outside, as in CSS. */+ int m = radius * 2;+ b.bx0 = clampi(b.x0 - m, 0, c->w); b.by0 = clampi(b.y0 - m, 0, c->h);+ b.bx1 = clampi(b.x1 + m, 0, c->w); b.by1 = clampi(b.y1 + m, 0, c->h);+ /* Blurred at reduced resolution: same visual result, much cheaper. */+ b.f = radius >= 12 ? 4 : radius >= 6 ? 2 : 1;+ b.sw = (b.bx1 - b.bx0 + b.f - 1) / b.f;+ b.sh = (b.by1 - b.by0 + b.f - 1) / b.f;+ b.rad = radius / (2 * b.f) > 0 ? radius / (2 * b.f) : 1;+ int win = 2 * b.rad + 1;+ b.cw = b.x1 - b.x0;+ b.buf = malloc((size_t)b.sw * b.sh * 4);+ b.src = malloc((size_t)b.sw * b.sh * 4);+ uint8_t *dt = malloc((size_t)255 * win + 1);+ b.sum = malloc((size_t)b.sw * 4 * sizeof(int));+ b.cx0 = malloc((size_t)b.cw * 3 * sizeof(int));+ if (!b.buf || !b.src || !dt || !b.sum || !b.cx0) goto out;+ for (int k = 0; k <= 255 * win; k++) dt[k] = (uint8_t)(k / win);+ b.dt = dt;+ long small = (long)b.sw * b.sh;+ blur_phase(&b, b.sh, (long)(b.bx1 - b.bx0) * (b.by1 - b.by0), blur_down);+ for (int pass = 0; pass < 3; pass++) {+ blur_phase(&b, b.sh, small, blur_rows);+ blur_phase(&b, b.sh, small, blur_copy);+ blur_phase(&b, b.sw, small, blur_cols);+ }+ b.r = fix_radius(r, w, h);+ b.cx = x + w * 0.5f; b.cy = y + h * 0.5f; b.hw = w * 0.5f; b.hh = h * 0.5f;+ b.inv = 1.0f / b.f;+ b.cx1 = b.cx0 + b.cw;+ b.cfx = b.cx1 + b.cw;+ for (int i = b.x0; i < b.x1; i++) {+ float u = (i - b.bx0 + 0.5f) * b.inv - 0.5f;+ if (u < 0) u = 0;+ if (u > b.sw - 1) u = (float)(b.sw - 1);+ int a = (int)u;+ b.cx0[i - b.x0] = a;+ b.cx1[i - b.x0] = a + 1 < b.sw ? a + 1 : a;+ b.cfx[i - b.x0] = (int)((u - a) * 256);+ }+ /* the columns whose coverage is whole on rows away from the corners: the same float tests as+ * the per-pixel path (i < x + 1, i + 1 > x + w - 1) */+ b.in0 = b.x0; b.in1 = b.x1;+ while (b.in0 < b.x1 && b.in0 < x + 1) b.in0++;+ while (b.in1 > b.in0 && b.in1 - 1 + 1 > x + w - 1) b.in1--;+ if (b.in0 >= b.in1) b.in0 = b.in1 = b.x1;+ blur_phase(&b, b.y1 - b.y0, (long)b.cw * (b.y1 - b.y0), blur_up);+out:+ free(b.buf);+ free(b.src);+ free(dt);+ free(b.sum);+ free(b.cx0);}void canvas_corners_save(Canvas *c, Corners *k, float x, float y, float w, float h, float r) {@@ -620,7 +722,20 @@ void canvas_corners_restore(Canvas *c, Corners *k, float x, float y, float w, flk->buf = NULL;}+typedef struct { const void *src; int sw, sh, x, y, stride, opaque, sw_px, sh_px; float r, opacity, sx, sy, fw, fh, dx, dy, dw, dh; uint32_t argb; } BlitArgs;+static void mask_band(Canvas *c, const void *p) { const BlitArgs *b = p; canvas_alpha_mask(c, b->x, b->y, b->src, b->sw, b->sh, b->stride, b->argb); }+static void blit_band(Canvas *c, const void *p) { const BlitArgs *b = p; canvas_blit(c, b->src, b->sw, b->sh, b->x, b->y, b->r, b->opacity, b->opaque); }+static void scaled_band(Canvas *c, const void *p) {+ const BlitArgs *b = p;+ canvas_blit_scaled(c, b->src, b->stride, b->sw_px, b->sh_px, b->sx, b->sy, b->fw, b->fh, b->dx, b->dy, b->dw, b->dh, b->opacity, b->opaque);+}+void canvas_alpha_mask(Canvas *c, int x, int y, const uint8_t *a, int w, int h, int stride, uint32_t argb) {+ if (banded(c, (float)x, (float)y, (float)w, (float)h)) {+ BlitArgs b = { .src = a, .x = x, .y = y, .sw = w, .sh = h, .stride = stride, .argb = argb };+ in_bands(c, (float)y, (float)h, mask_band, &b);+ return;+ }uint32_t ca = argb >> 24;int x0 = clampi(x, c->clip.x0, c->clip.x1), x1 = clampi(x + w, c->clip.x0, c->clip.x1);int y0 = clampi(y, c->clip.y0, c->clip.y1), y1 = clampi(y + h, c->clip.y0, c->clip.y1);@@ -632,6 +747,11 @@ void canvas_alpha_mask(Canvas *c, int x, int y, const uint8_t *a, int w, int h,}void canvas_blit(Canvas *c, const uint32_t *src, int sw, int sh, int x, int y, float r, float opacity, int opaque) {+ if (banded(c, (float)x, (float)y, (float)sw, (float)sh)) {+ BlitArgs b = { .src = src, .sw = sw, .sh = sh, .x = x, .y = y, .r = r, .opacity = opacity, .opaque = opaque };+ in_bands(c, (float)y, (float)sh, blit_band, &b);+ return;+ }int x0 = clampi(x, c->clip.x0, c->clip.x1), x1 = clampi(x + sw, c->clip.x0, c->clip.x1);int y0 = clampi(y, c->clip.y0, c->clip.y1), y1 = clampi(y + sh, c->clip.y0, c->clip.y1);if (x0 >= x1 || y0 >= y1) return;@@ -686,6 +806,12 @@ void canvas_bitmap(Canvas *c, int x, int y, const char *const *rows, uint32_t fgvoid canvas_blit_scaled(Canvas *c, const uint32_t *src, int stride, int sw_px, int sh_px, float sx, float sy, float sw, float sh,float dx, float dy, float dw, float dh, float opacity, int opaque) {if (dw <= 0 || dh <= 0 || sw <= 0 || sh <= 0 || sw_px <= 0 || sh_px <= 0) return;+ if (banded(c, dx, dy, dw, dh)) {+ BlitArgs b = { .src = src, .stride = stride, .sw_px = sw_px, .sh_px = sh_px, .sx = sx, .sy = sy, .fw = sw, .fh = sh,+ .dx = dx, .dy = dy, .dw = dw, .dh = dh, .opacity = opacity, .opaque = opaque };+ in_bands(c, dy, dh, scaled_band, &b);+ return;+ }if (sx == 0 && sy == 0 && sw == sw_px && sh == sh_px && stride == sw_px && dw == sw && dh == sh && dx == floorf(dx) && dy == floorf(dy)) {canvas_blit(c, src, sw_px, sh_px, (int)dx, (int)dy, 0, opacity, opaque);return;diff --git a/server/src/canvas.h b/server/src/canvas.hindex 1f6c173..8bd2bc1 100644--- a/server/src/canvas.h+++ b/server/src/canvas.h@@ -14,6 +14,11 @@ typedef struct {enum { FIT_FILL, FIT_COVER, FIT_CONTAIN };+/* Optional: run row-local pixel loops on a pool (lord-server passes par_rows, see par.h); same pixels. */+typedef void (*CanvasPar)(int j0, int j1, void (*fn)(void *ctx, int a, int b), void *ctx);+void canvas_parallel(CanvasPar fn);+int canvas_parallel_on(void); /* the pool is in use now (heavy frames) */+/* Colors as 0xAARRGGBB; AA = opacity. Float coordinates with antialiasing. */void canvas_init(Canvas *c, uint32_t *px, int w, int h, int stride);Rect canvas_push_clip(Canvas *c, float x, float y, float w, float h); /* returns the previous clip */diff --git a/server/src/main.c b/server/src/main.cindex d0c9e77..7118276 100644--- a/server/src/main.c+++ b/server/src/main.c@@ -24,6 +24,7 @@#include "keyboard.h"#include "ui/js_sys.h"#include "canvas.h"+#include "par.h"#include "input.h"#include "kms.h"#include "drmcard.h"@@ -1434,6 +1435,20 @@ static void render_frame(Server *s) {} else if (c->on) { out_free(c); c->on = 0; c->pend = (Box){ 0, 0, 0, 0 }; }}s->frames++;+ /* The pool only while frames are heavy: it costs CPU to wake (a light frame painted on one thread+ * spends less), so it turns on when painting takes more than 70 % of a frame at the target rate (the+ * monitor's refresh, or the fastest remote viewer's without a monitor) and off after 60 light frames+ * in a row (with the pool a heavy frame paints fast, so not at once: it would flap).+ * A 1080p video under blurred windows: 45 → 57 frames a second on VM 116. */+ static int par_on, light;+ Out *pm = main_out(s);+ int hz = s->headless ? (mirror_fps() > 60 ? mirror_fps() : 60) : pm && pm->kms.mode.vrefresh ? (int)pm->kms.mode.vrefresh : 60;+ double frame_ms = 1000.0 / hz;+ if (!par_on && ui.paint_ms > frame_ms * 0.7 && par_threads() > 1) { par_on = 1; light = 0; canvas_parallel(par_rows); }+ else if (par_on) {+ light = ui.paint_ms < frame_ms * 0.25 ? light + 1 : 0;+ if (light >= 60) { par_on = 0; canvas_parallel(NULL); }+ }wl_frame_done((unsigned)now_ms());/* No flip event will come (a modeset instead of a page flip: the first frame, or a driver* without page flip): the frame is on screen already, so its presentation feedback goes now.@@ -1702,6 +1717,10 @@ int main(int argc, char **argv) {static const MirrorInput mirror_in = { remote_pointer, remote_button, remote_wheel, remote_key, remote_clip, remote_clients,remote_refresh, remote_resize, remote_greeter, display_attach, display_detach,remote_scroll, remote_pinch, remote_input_add, remote_notice };+ /* the CPU path's pixel loops on a pool (par.h): blur, blits, gradients, the remote desktop's+ * comparison and copies; same pixels as one thread. Its threads sleep between jobs, and jobs only+ * run inside a frame on this thread, so the processes this server forks never inherit a busy one. */+ par_init(0);if (ok && mirror_start(s.W, s.H, &mirror_in)) fprintf(stderr, "lord-server: no remote desktop (socket)\n");char current[sizeof ui.theme_request];if (ok) ok = start_theme(theme, s.greeter, current, sizeof current) == 0;diff --git a/server/src/media/mstream.c b/server/src/media/mstream.cindex 4286e38..d2a7918 100644--- a/server/src/media/mstream.c+++ b/server/src/media/mstream.c@@ -8,6 +8,8 @@* If the encoder is behind (no free slot) the frame waits: the newest one goes when a slot frees up,* so a slow machine sends fewer frames, never old ones. Sound: the session's default output,* recorded by lord-server itself (maudio.c, PipeWire) into a ring the encoder reads. */+#include "../par.h"+#include "../canvas.h"#include "mstream.h"#include "media_int.h"@@ -196,6 +198,13 @@ void mstream_keyframe(int codec) {E[codec].pending = 1;}+/* the copy into an input slot, by rows over the pool (par.h) */+typedef struct { uint32_t *dst; int w, x0, x1; } SlotCopy;+static void slot_rows(void *ctx, int y0, int y1) {+ SlotCopy *k = ctx;+ for (int y = y0; y < y1; y++) memcpy(k->dst + (size_t)y * k->w + k->x0, fpx + (size_t)y * fstride + k->x0, (size_t)(k->x1 - k->x0) * 4);+}+static void feed(Enc *e, int codec) {MediaSrc *s = src_of(e);if (!s || s->sock < 0) {@@ -217,7 +226,11 @@ static void feed(Enc *e, int codec) {int *d = slot < 4 ? e->dirty[slot] : NULL, full[4] = { 0, 0, e->w, e->h };if (!d) d = full;int x0 = d[0] < 0 ? 0 : d[0], y0 = d[1] < 0 ? 0 : d[1], x1 = d[2] > e->w ? e->w : d[2], y1 = d[3] > e->h ? e->h : d[3];- for (int y = y0; y < y1 && x1 > x0; y++) memcpy(dst + (size_t)y * e->w + x0, fpx + (size_t)y * fstride + x0, (size_t)(x1 - x0) * 4);+ if (x1 > x0 && y1 > y0) {+ SlotCopy k = { dst, e->w, x0, x1 };+ if ((long)(x1 - x0) * (y1 - y0) >= 262144 && canvas_parallel_on()) par_rows(y0, y1, slot_rows, &k);+ else slot_rows(&k, y0, y1);+ }if (slot < 4) d[0] = d[1] = d[2] = d[3] = 0;if (e->kbps != e->sent_kbps) { mp_sendv(s->sock, MM_RATE, 0, e->kbps, 0, 0, 0); e->sent_kbps = e->kbps; }if (e->want_key) { mp_sendv(s->sock, MM_KEYFRAME, 0, 0, 0, 0, 0); e->want_key = 0; }diff --git a/server/src/par.c b/server/src/par.cnew file mode 100644index 0000000..2f4b74a--- /dev/null+++ b/server/src/par.c@@ -0,0 +1,96 @@+/* par.c — the pixel loops' thread pool (see par.h). Workers sleep on a condition variable between+ * jobs; a job is cut into a few pieces per thread, taken with an atomic counter (the caller takes+ * pieces too), so a slow piece doesn't hold the others. Only one job at a time, from the main+ * thread; a call made inside a piece, or from another thread while a job runs, runs serially. */+#include "par.h"++#include <pthread.h>+#include <sched.h>+#include <stdatomic.h>+#include <unistd.h>++#define PAR_MAX 8++static int nthreads = 1; /* the caller included */+static pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER;+static pthread_cond_t go = PTHREAD_COND_INITIALIZER, done = PTHREAD_COND_INITIALIZER;+static unsigned long gen; /* a new job (under mu) */+static ParFn job_fn;+static void *job_ctx;+static int job_j0, job_n, job_pieces;+static atomic_int next_piece, left;+static atomic_flag busy = ATOMIC_FLAG_INIT;+static _Thread_local int inside;++static void run_pieces(void) {+ inside = 1;+ for (;;) {+ int p = atomic_fetch_add(&next_piece, 1);+ if (p >= job_pieces) break;+ int a = job_j0 + (int)((long)job_n * p / job_pieces), b = job_j0 + (int)((long)job_n * (p + 1) / job_pieces);+ if (b > a) job_fn(job_ctx, a, b);+ if (atomic_fetch_sub(&left, 1) == 1) {+ pthread_mutex_lock(&mu);+ pthread_cond_signal(&done);+ pthread_mutex_unlock(&mu);+ }+ }+ inside = 0;+}++static void *worker(void *arg) {+ (void)arg;+ unsigned long seen = 0;+ for (;;) {+ pthread_mutex_lock(&mu);+ while (gen == seen) pthread_cond_wait(&go, &mu);+ seen = gen;+ pthread_mutex_unlock(&mu);+ run_pieces();+ }+ return NULL;+}++void par_init(int threads) {+ if (nthreads > 1) return;+ if (threads <= 0) {+ cpu_set_t set;+ threads = !sched_getaffinity(0, sizeof set, &set) ? CPU_COUNT(&set) : (int)sysconf(_SC_NPROCESSORS_ONLN);+ }+ if (threads > PAR_MAX) threads = PAR_MAX;+ for (int i = 1; i < threads; i++) {+ pthread_t t;+ pthread_attr_t at;+ pthread_attr_init(&at);+ pthread_attr_setstacksize(&at, 256 * 1024);+ if (pthread_create(&t, &at, worker, NULL)) { pthread_attr_destroy(&at); break; }+ pthread_attr_destroy(&at);+ pthread_detach(t);+ nthreads++;+ }+}++int par_threads(void) { return nthreads; }++void par_rows(int j0, int j1, ParFn fn, void *ctx) {+ if (j1 <= j0) return;+ if (nthreads <= 1 || inside || j1 - j0 < 2 || atomic_flag_test_and_set(&busy)) { fn(ctx, j0, j1); return; }+ int n = j1 - j0, pieces = nthreads * 3;+ if (pieces > n) pieces = n;+ pthread_mutex_lock(&mu);+ job_fn = fn;+ job_ctx = ctx;+ job_j0 = j0;+ job_n = n;+ job_pieces = pieces;+ atomic_store(&next_piece, 0);+ atomic_store(&left, pieces);+ gen++;+ pthread_cond_broadcast(&go);+ pthread_mutex_unlock(&mu);+ run_pieces();+ pthread_mutex_lock(&mu);+ while (atomic_load(&left) > 0) pthread_cond_wait(&done, &mu);+ pthread_mutex_unlock(&mu);+ atomic_flag_clear(&busy);+}diff --git a/server/src/par.h b/server/src/par.hnew file mode 100644index 0000000..947871e--- /dev/null+++ b/server/src/par.h@@ -0,0 +1,17 @@+/* par.h — a small pool of threads for the CPU path's pixel loops (rows split between them).+ *+ * par_rows(j0, j1, fn, ctx) calls fn(ctx, a, b) over disjoint pieces of [j0, j1) on the pool and the+ * calling thread, and returns when every piece is done. fn must only write its own rows (or columns)+ * and touch no shared state: it is used for pure pixel work (blur, blits, gradients, the remote+ * desktop's comparison and copies), never for the node tree, caches or anything with side effects.+ * Without par_init (or with one CPU) it runs fn(ctx, j0, j1) on the caller. */+#ifndef LORD_PAR_H+#define LORD_PAR_H++typedef void (*ParFn)(void *ctx, int a, int b);++void par_init(int threads); /* 0: the CPUs this process may use, at most 8 */+int par_threads(void); /* threads that take pieces, the caller included (1: serial) */+void par_rows(int j0, int j1, ParFn fn, void *ctx);++#endifdiff --git a/server/src/remote/mirror.c b/server/src/remote/mirror.cindex a3a4979..33e3388 100644--- a/server/src/remote/mirror.c+++ b/server/src/remote/mirror.c@@ -18,6 +18,8 @@#include "proto.h"#include "../vault/vclient.h"#include "../media/mstream.h"+#include "../par.h"+#include "../canvas.h"#include <errno.h>#include <fcntl.h>@@ -573,6 +575,29 @@ void mirror_dispatch(const struct pollfd *p, int n) {#define FULL_CHECK_MS 500 /* with damage hints, every tile is still compared this often */+/* One frame's comparison, split by rows of tiles over the pool (par.h): every tile has its own bytes in+ * fb, chg and each client's dirty map, so the rows don't share anything but "something changed". */+typedef struct { const uint32_t *px; int stride, tx0, tx1; int any; } Cmp;+static void cmp_rows(void *ctx, int ty0, int ty1) {+ Cmp *k = ctx;+ int any = 0;+ for (int ty = ty0; ty < ty1; ty++)+ for (int tx = k->tx0; tx < k->tx1; tx++) {+ int x = tx * TILE, y = ty * TILE;+ int tw = x + TILE > W ? W - x : TILE, th = y + TILE > H ? H - y : TILE;+ int changed = 0;+ for (int j = 0; j < th && !changed; j++)+ changed = memcmp(fb + (size_t)(y + j) * W + x, k->px + (size_t)(y + j) * k->stride + x, (size_t)tw * 4) != 0;+ if (!changed) continue;+ for (int j = 0; j < th; j++)+ memcpy(fb + (size_t)(y + j) * W + x, k->px + (size_t)(y + j) * k->stride + x, (size_t)tw * 4);+ for (int i = 0; i < MAX_CLIENTS; i++) if (cl[i].fd >= 0 && cl[i].dirty) cl[i].dirty[ty * TW + tx] = 1;+ if (chg) chg[ty * TW + tx] = 1;+ any = 1;+ }+ if (any) __atomic_store_n(&k->any, 1, __ATOMIC_RELAXED);+}+void mirror_frame(const uint32_t *px, int stride, int w, int h, const int *dmg) {static double full_at;if (lfd < 0 || !mirror_watchers()) { free(fb); fb = NULL; return; }@@ -606,20 +631,10 @@ void mirror_frame(const uint32_t *px, int stride, int w, int h, const int *dmg)if (tx1 > TW) tx1 = TW;if (ty1 > TH) ty1 = TH;} else full_at = t;- for (int ty = ty0; ty < ty1; ty++)- for (int tx = tx0; tx < tx1; tx++) {- int x = tx * TILE, y = ty * TILE;- int tw = x + TILE > W ? W - x : TILE, th = y + TILE > H ? H - y : TILE;- int changed = 0;- for (int j = 0; j < th && !changed; j++)- changed = memcmp(fb + (size_t)(y + j) * W + x, px + (size_t)(y + j) * stride + x, (size_t)tw * 4) != 0;- if (!changed) continue;- for (int j = 0; j < th; j++)- memcpy(fb + (size_t)(y + j) * W + x, px + (size_t)(y + j) * stride + x, (size_t)tw * 4);- for (int i = 0; i < MAX_CLIENTS; i++) if (cl[i].fd >= 0 && cl[i].dirty) cl[i].dirty[ty * TW + tx] = 1;- if (chg) chg[ty * TW + tx] = 1;- any = 1;- }+ Cmp k = { px, stride, tx0, tx1, 0 };+ if ((long)(ty1 - ty0) * (tx1 - tx0) >= 64 && canvas_parallel_on()) par_rows(ty0, ty1, cmp_rows, &k);+ else cmp_rows(&k, ty0, ty1);+ any = k.any;}if (any) {last_change = now_ms();