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
Paint: several damaged rectangles per frame, presented one by one
Federico Pereira26bb7d6fd615
diff --git a/docs/LAYERS.md b/docs/LAYERS.mdindex 8827e57..92a981b 100644--- a/docs/LAYERS.md+++ b/docs/LAYERS.md@@ -95,6 +95,12 @@ change touches a blurred surface or the margin its blur reads, so the blur onlyThe GPU path composites that same canvas, so it gets the same fix (tested on VM 122 on the CPUpath).+Since 0.8.2 the damage can be several rectangles painted one by one (`paint.c`, "Several damaged+rectangles"; the Monitor's moving map dots). The rule holds for each of them: a rectangle that+touches a blurred surface or its margin grows to the whole surface, and every other rectangle+touching that margin joins it, so a blur never reads a neighbour rectangle half painted+(`test_multi_damage` in `build/engine-test`, against a full repaint).+| | ||---|---||  |  |diff --git a/server/src/main.c b/server/src/main.cindex cb1502f..ff92643 100644--- a/server/src/main.c+++ b/server/src/main.c@@ -96,11 +96,17 @@ typedef struct {Canvas scene; /* upright physical frame (pw × ph), without the cursor */uint32_t *frame; /* rotated displays and the capture view: scene + fullscreen + cursor */Box pend; /* logical damage not painted on it yet (its flip was pending) */+ DamageList pl; /* the same as separate rectangles (paint_damage_rects); pend is their box */int full; /* paint all of it next time */int cdirty, cur_on; /* the cursor moved since it was presented; it was drawn on it *//* CPU path: each KMS buffer only gets what changed since it was last drawn into (what was* repainted in the last two frames and where the cursor was), not the whole frame */Rect stale[2], curs[2];+ /* the same as separate rectangles (physical): what paint_out painted this frame, and what each+ * buffer is missing; with several damaged rectangles only those are copied (0.8.2) */+ DamageList pstale[2];+ Box painted[DAMAGE_MAX];+ int npainted;int full_copy, was_fs;int gpu; /* its GPU output (gpu_frame), -1 = composed by the CPU */int wl_new; /* GPU path: a Linux app's window has new content not presented here yet */@@ -676,10 +682,13 @@ static void outputs_apply(Server *s) {o->v = p.o[j].v;if (out_frames(o, o->d.rot != 0)) { fprintf(stderr, "displays: out of memory for a %dx%d frame\n", o->v.pw, o->v.ph); ok = 0; }memset(o->stale, 0, sizeof o->stale);+ memset(o->pstale, 0, sizeof o->pstale);+ o->npainted = 0;memset(o->curs, 0, sizeof o->curs);o->full = 1;o->full_copy = 2;o->pend = (Box){ 0, 0, 0, 0 };+ o->pl.n = 0;if (j == p.main) s->main_out = head[j];fprintf(stderr, "displays: %s (%s) %dx%d at %d%%, logical %dx%d at (%d, %d)%s%s\n", o->d.connector, o->d.name, o->v.pw, o->v.ph,out_scale(o), o->v.w, o->v.h, o->v.x, o->v.y, j == p.main ? ", main" : "", p.mirror ? ", mirror" : "");@@ -958,8 +967,15 @@ static Box paint_ui(Server *s) {paint_layer_view(b >= 0 ? idx[b] : 0);}if (!box_empty(d)) {- for (int i = 0; i < OUT_MAX; i++) if (s->out[i].on) s->out[i].pend = box_join(s->out[i].pend, d);- s->cap.pend = box_join(s->cap.pend, d);+ Box rs[DAMAGE_MAX];+ int nr = paint_damage_rects(rs, DAMAGE_MAX);+ for (int i = 0; i <= OUT_MAX; i++) {+ Out *o = i < OUT_MAX ? &s->out[i] : &s->cap;+ if (i < OUT_MAX && !o->on) continue;+ if (box_empty(o->pend)) o->pl.n = 0;+ for (int k = 0; k < nr; k++) dlist_add(&o->pl, rs[k]);+ o->pend = box_join(o->pend, d);+ }}ui.layout_ms = t1 - t0;s->layout_total += ui.layout_ms;@@ -971,32 +987,50 @@ static Box paint_ui(Server *s) {static Rect paint_out(Server *s, Out *o, int index, int feed) {Rect r = { 0, 0, 0, 0 };Box b = o->pend;+ o->npainted = 0;if (o->full) b = (Box){ (float)o->v.x, (float)o->v.y, (float)(o->v.x + o->v.w), (float)(o->v.y + o->v.h) };int whole = o->full;o->full = 0;o->pend = (Box){ 0, 0, 0, 0 };if (box_empty(b) || !o->scene.px) return r;- r = whole ? (Rect){ 0, 0, o->scene.w, o->scene.h } : out_phys_rect(&o->v, b.x0, b.y0, b.x1, b.y1);- if (r.x1 <= r.x0 || r.y1 <= r.y0) return (Rect){ 0, 0, 0, 0 };- double t0 = now_ms();+ /* several damaged rectangles (paint.c, "Several damaged rectangles"): each one painted on its+ * own; r, what is presented, is the box around them (the scene keeps the pixels in between) */+ Box lb = o->pl.n ? o->pl.r[0] : (Box){ 0, 0, 0, 0 };+ for (int k = 1; k < o->pl.n; k++) lb = box_join(lb, o->pl.r[k]);+ int multi = !whole && o->pl.n > 1 && !gpu_active() && lb.x0 == b.x0 && lb.y0 == b.y0 && lb.x1 == b.x1 && lb.y1 == b.y1;+ int nparts = multi ? o->pl.n : 1;+ Box parts[DAMAGE_MAX];+ if (multi) memcpy(parts, o->pl.r, sizeof(Box) * (size_t)nparts);+ else parts[0] = b;+ o->pl.n = 0;+ double t0 = now_ms(), px = 0;view_of(o, index);- float lx0, ly0, lx1, ly1;- out_logical_box(&o->v, r, &lx0, &ly0, &lx1, &ly1);- ui.damage = (Box){ lx0, ly0, lx1, ly1 }; /* paint_root skips what this region does not need */- canvas_set_clip(&o->scene, r);- canvas_clear(&o->scene, 0x000000);- paint_root(&o->scene, ui.root);- if (feed) media_scene_painted(&o->scene); /* recording/remote desktop with protected windows */+ for (int k = 0; k < nparts; k++) {+ Box q = parts[k];+ Rect pr = whole ? (Rect){ 0, 0, o->scene.w, o->scene.h } : out_phys_rect(&o->v, q.x0, q.y0, q.x1, q.y1);+ if (pr.x1 <= pr.x0 || pr.y1 <= pr.y0) continue;+ float lx0, ly0, lx1, ly1;+ out_logical_box(&o->v, pr, &lx0, &ly0, &lx1, &ly1);+ ui.damage = (Box){ lx0, ly0, lx1, ly1 }; /* paint_root skips what this region does not need */+ canvas_set_clip(&o->scene, pr);+ canvas_clear(&o->scene, 0x000000);+ paint_root(&o->scene, ui.root);+ if (feed) media_scene_painted(&o->scene); /* recording/remote desktop with protected windows */+ r = rect_join(r, pr);+ o->painted[o->npainted++] = (Box){ (float)pr.x0, (float)pr.y0, (float)pr.x1, (float)pr.y1 };+ px += (double)(pr.x1 - pr.x0) * (pr.y1 - pr.y0);+ }canvas_set_clip(&o->scene, (Rect){ 0, 0, o->scene.w, o->scene.h });ui.damage = (Box){ 0, 0, 0, 0 };view_reset();+ if (r.x1 <= r.x0 || r.y1 <= r.y0) return (Rect){ 0, 0, 0, 0 };double ms = now_ms() - t0;ui.paint_ms += ms;s->paint_total += ms;- ui.damage_px += (double)(r.x1 - r.x0) * (r.y1 - r.y0);+ ui.damage_px += px;if (s->debug || paint_profile)- fprintf(stderr, "render %ld: display %d, layout %.2f ms, paint %.2f ms, repainted %.0f%%\n", s->renders, index, ui.layout_ms, ms,- 100.0 * (r.x1 - r.x0) * (r.y1 - r.y0) / ((double)o->scene.w * o->scene.h));+ fprintf(stderr, "render %ld: display %d, layout %.2f ms, paint %.2f ms, repainted %.1f%% in %d rect%s\n", s->renders, index, ui.layout_ms, ms,+ 100.0 * px / ((double)o->scene.w * o->scene.h), nparts, nparts == 1 ? "" : "s");return r;}@@ -1133,6 +1167,7 @@ static void present_cpu(Server *s, Out *o, int index, Rect r, int feed) {int bi = o->kms.front ^ 1, fs = wl_fullscreen_active();if (fs != o->was_fs) { o->was_fs = fs; o->full_copy = 2; }Rect need = { 0, 0, W, H }; /* what this buffer gets now (also tinted) */+ int tinted = 0; /* the copied rectangles were tinted one by one */if (o->full_copy > 0 || fs) {canvas_copy(&out, &o->scene);if (o->full_copy > 0) o->full_copy--;@@ -1142,11 +1177,37 @@ static void present_cpu(Server *s, Out *o, int index, Rect r, int feed) {if (need.y0 < 0) need.y0 = 0;if (need.x1 > out.w) need.x1 = out.w;if (need.y1 > out.h) need.y1 = out.h;- for (int y = need.y0; y < need.y1; y++)- memcpy(out.px + (size_t)y * out.stride + need.x0, o->scene.px + (size_t)y * o->scene.stride + need.x0, (size_t)(need.x1 - need.x0) * 4);+ /* several damaged rectangles: only they go into the buffer (with the moving dots spread over+ * the Monitor's map, the box around them was most of the screen) */+ DamageList cp = o->pstale[bi];+ for (int i = 0; i < o->npainted; i++) dlist_add(&cp, o->painted[i]);+ Rect cu = o->curs[bi];+ if (cu.x1 > cu.x0 && cu.y1 > cu.y0) dlist_add(&cp, (Box){ (float)cu.x0, (float)cu.y0, (float)cu.x1, (float)cu.y1 });+ Box cb = cp.n ? cp.r[0] : (Box){ 0, 0, 0, 0 };+ for (int i = 1; i < cp.n; i++) cb = box_join(cb, cp.r[i]);+ Rect cbr = { (int)cb.x0, (int)cb.y0, (int)cb.x1, (int)cb.y1 };+ Rect nj = rect_join(rect_join(o->stale[bi], r), o->curs[bi]);+ if (!xf && cp.n > 1 && cbr.x0 == nj.x0 && cbr.y0 == nj.y0 && cbr.x1 == nj.x1 && cbr.y1 == nj.y1) {+ for (int i = 0; i < cp.n; i++) {+ Rect q = { (int)cp.r[i].x0, (int)cp.r[i].y0, (int)cp.r[i].x1, (int)cp.r[i].y1 };+ if (q.x0 < 0) q.x0 = 0;+ if (q.y0 < 0) q.y0 = 0;+ if (q.x1 > out.w) q.x1 = out.w;+ if (q.y1 > out.h) q.y1 = out.h;+ if (q.x1 <= q.x0 || q.y1 <= q.y0) continue;+ for (int y = q.y0; y < q.y1; y++)+ memcpy(out.px + (size_t)y * out.stride + q.x0, o->scene.px + (size_t)y * o->scene.stride + q.x0, (size_t)(q.x1 - q.x0) * 4);+ display_tint(&out, q); /* Night Light, on what was copied */+ }+ tinted = 1;+ } else+ for (int y = need.y0; y < need.y1; y++)+ memcpy(out.px + (size_t)y * out.stride + need.x0, o->scene.px + (size_t)y * o->scene.stride + need.x0, (size_t)(need.x1 - need.x0) * 4);}o->stale[bi] = (Rect){ 0, 0, 0, 0 };o->stale[bi ^ 1] = rect_join(o->stale[bi ^ 1], r);+ o->pstale[bi].n = 0;+ for (int i = 0; i < o->npainted; i++) dlist_add(&o->pstale[bi ^ 1], o->painted[i]);o->curs[bi] = (Rect){ 0, 0, 0, 0 };view_of(o, index);wl_paint_fullscreen(&out);@@ -1168,7 +1229,7 @@ static void present_cpu(Server *s, Out *o, int index, Rect r, int feed) {Rect cr = cursor_rect(s, o, &k);o->cur_on = cr.x1 > cr.x0;if (!xf) {- display_tint(&out, need); /* Night Light without a gamma LUT (software) */+ if (!tinted) display_tint(&out, need); /* Night Light without a gamma LUT (software) */if (o->cur_on) {canvas_blit(&out, k.px, k.w, k.h, cr.x0, cr.y0, 0, 1, 0);o->curs[bi] = cr;@@ -1496,6 +1557,7 @@ int main(int argc, char **argv) {if (ok && snd_start()) fprintf(stderr, "lord-server: no PipeWire for now (sound); it retries on its own\n");if (ok && !s.greeter) portal_start(); /* dark mode, accent and buttons for GTK/GNOME/Qt (ui/portal.c) */if (ok && !s.greeter) term_restore(); /* shells still alive from the previous start */+ if (getenv("LORD_DAMAGE") && !strcmp(getenv("LORD_DAMAGE"), "1")) paint_multi_damage = 0; /* one damaged box (paint.c) */remote_server = &s;tp_route_init(&tp_host);static const MirrorInput mirror_in = { remote_pointer, remote_button, remote_wheel, remote_key, remote_clip, remote_clients,diff --git a/server/src/ui/node.c b/server/src/ui/node.cindex 8cf5ed2..8caf002 100644--- a/server/src/ui/node.c+++ b/server/src/ui/node.c@@ -1,6 +1,7 @@/* node.c — tree of native nodes that React creates and modifies. */#include "ui.h"+#include <math.h>#include <stdlib.h>#include <string.h>@@ -47,14 +48,56 @@ int node_count(void) {return n;}+float box_area(Box b) { return b.x1 > b.x0 && b.y1 > b.y0 ? (b.x1 - b.x0) * (b.y1 - b.y0) : 0; }+static int dbox_meets(Box a, Box b) { return a.x0 < b.x1 && b.x0 < a.x1 && a.y0 < b.y1 && b.y0 < a.y1; }+static Box dbox_join(Box a, Box b) { return (Box){ fminf(a.x0, b.x0), fminf(a.y0, b.y0), fmaxf(a.x1, b.x1), fmaxf(a.y1, b.y1) }; }++/* Adds b to the list: it swallows every rectangle it overlaps (and grows by them, so the list never+ * holds two that overlap); past DAMAGE_MAX the two whose union adds the least area are joined. */+void dlist_add(DamageList *l, Box b) {+ if (b.x1 <= b.x0 || b.y1 <= b.y0) return;+ for (int again = 1; again;) {+ again = 0;+ for (int i = 0; i < l->n; i++) {+ if (!dbox_meets(l->r[i], b)) continue;+ b = dbox_join(b, l->r[i]);+ l->r[i--] = l->r[--l->n];+ again = 1;+ }+ }+ if (l->n == DAMAGE_MAX) {+ int bi = 0, bj = 1;+ float best = 1e30f;+ for (int i = 0; i < l->n; i++)+ for (int j = i + 1; j < l->n; j++) {+ float w = box_area(dbox_join(l->r[i], l->r[j])) - box_area(l->r[i]) - box_area(l->r[j]);+ if (w < best) { best = w; bi = i; bj = j; }+ }+ Box u = dbox_join(l->r[bi], l->r[bj]);+ l->r[bj] = l->r[--l->n];+ l->r[bi] = l->r[--l->n];+ dlist_add(l, u); /* the union may overlap a third one */+ }+ l->r[l->n++] = b;+}+void damage_add(Box b) {if (b.x1 <= b.x0 || b.y1 <= b.y0) return;Box *d = &ui.damage;- if (d->x1 <= d->x0 || d->y1 <= d->y0) { *d = b; return; }- if (b.x0 < d->x0) d->x0 = b.x0;- if (b.y0 < d->y0) d->y0 = b.y0;- if (b.x1 > d->x1) d->x1 = b.x1;- if (b.y1 > d->y1) d->y1 = b.y1;+ /* ui.damage set or cleared directly (a full repaint, the end of a frame): the list starts from it */+ if (d->x0 != ui.dlast.x0 || d->y0 != ui.dlast.y0 || d->x1 != ui.dlast.x1 || d->y1 != ui.dlast.y1) {+ ui.dlist.n = 0;+ dlist_add(&ui.dlist, *d);+ }+ if (d->x1 <= d->x0 || d->y1 <= d->y0) *d = b;+ else {+ if (b.x0 < d->x0) d->x0 = b.x0;+ if (b.y0 < d->y0) d->y0 = b.y0;+ if (b.x1 > d->x1) d->x1 = b.x1;+ if (b.y1 > d->y1) d->y1 = b.y1;+ }+ dlist_add(&ui.dlist, b);+ ui.dlast = *d;}/* A node that leaves the tree stops being visible: its previous area must be repainted. */diff --git a/server/src/ui/paint.c b/server/src/ui/paint.cindex ddbb5a7..9de9a72 100644--- a/server/src/ui/paint.c+++ b/server/src/ui/paint.c@@ -39,6 +39,7 @@ static uint32_t with_opacity(uint32_t argb, float op) {/* Cuadro de video que tapa todo lo que cambió (media_cover_node): lo anterior a ese nodo en el* orden de pintado no se ve, así que no se pinta (solo se recorre, por los recortes). */static int skip_until, skip_reached;+static int paint_cull; /* paint_node may skip clipped subtrees outside the clip (CPU path) */static int layer_paint(Canvas *c, Node *n, float opacity);static int tf_paint(Canvas *c, Node *n, float opacity);@@ -65,6 +66,15 @@ static void paint_node(Canvas *c, Node *n, float opacity) {if (op < 0.004f) return;const float k = paint_view.k;float x = VX(n->ax), y = VY(n->ay), w = n->w * k, h = n->h * k, rad = s->radius * k;+ /* A box that clips its children and lies outside the region being painted: nothing of it or of+ * its subtree can land there (0.8.2; each of several damaged rectangles is a walk of the tree,+ * and this keeps that walk to the branches that reach it). Not through transforms, not on the+ * GPU path (its list of app windows is built while painting) and not before the node a+ * partial repaint starts from (its ancestors are inside the region by construction). */+ if (paint_cull && s->overflow_hidden && !n->has_tf) {+ float e = (s->shadow_color >> 24) ? (s->shadow_blur + fmaxf(fabsf(s->shadow_x), fabsf(s->shadow_y))) * k + 1 : 1;+ if (x - e >= c->clip.x1 || y - e >= c->clip.y1 || x + w + e <= c->clip.x0 || y + h + e <= c->clip.y0) return;+ }/* per-corner radii and per-side borders (not plain): the *4 canvas functions */int plain = node_edges_plain(n);const StyleExt *ex = s->ext;@@ -706,6 +716,121 @@ static void blur_walk(Node *n, int *grew) {for (Node *c = n->first; c; c = c->next) blur_walk(c, grew);}+/* ---- Several damaged rectangles (0.8.2) ----+ * damage_add keeps, besides the box around everything (ui.damage), the changes as a short list of+ * rectangles (ui.dlist). When the changes are small and far apart, the Monitor's moving dots on a+ * world map, each one is repainted on its own: the box around them was most of the map (0.3 % of a+ * core per frame per second, docs/MONITOR.md). Every rectangle is a complete repaint of its pixels+ * (clear, paint the tree clipped to it), so the result is the same as with the one box.+ * - Two rectangles whose union wastes less than DAMAGE_PASS_PX are joined (each one is a walk of+ * the tree), and when they cover most of their box it is one box again.+ * - Blurred surfaces (backdropFilter) read the canvas around them. The 0.8.1 rule stays: a+ * rectangle that touches a blurred node or the margin its blur samples grows to the whole node,+ * and every rectangle that touches that margin joins it, so the margin is either repainted in+ * the same pass or not repainted this frame at all (then it still holds what it showed).+ * - Only on the CPU path and without a window being dragged (its layer and the GPU's window list+ * are built for one paint_root per frame); otherwise one box, as before. */+#ifdef LORD_DAMAGE_SINGLE+int paint_multi_damage = 0; /* a build for comparing (docs/MONITOR.md, measurements) */+#else+int paint_multi_damage = 1; /* LORD_DAMAGE=1 in lord-server's environment: one box */+#endif+static Box drects[DAMAGE_MAX];+static int ndrects;++#ifndef DAMAGE_PASS_PX+#define DAMAGE_PASS_PX 1024.0f /* about what one more pass costs, in pixels (measured, docs/MONITOR.md) */+#endif+#define DAMAGE_COVER 0.7f /* rectangles covering this much of their box: one box */+#define BLUR_MAX 48++static Box dl_bbox(const DamageList *l) {+ Box b = l->n ? l->r[0] : (Box){ 0, 0, 0, 0 };+ for (int i = 1; i < l->n; i++)+ b = (Box){ fminf(b.x0, l->r[i].x0), fminf(b.y0, l->r[i].y0), fmaxf(b.x1, l->r[i].x1), fmaxf(b.y1, l->r[i].y1) };+ return b;+}++static void blur_collect(Node *n, Box *bb, float *bm, int *k) {+ if (n->type == NODE_RAW || n->s.display_none) return;+ Box b = n->prev_box;+ if (n->s.backdrop_blur >= 1 && b.x1 > b.x0 && b.y1 > b.y0) {+ if (*k < BLUR_MAX) { bb[*k] = b; bm[*k] = ceilf(n->s.backdrop_blur * 2) + 1; } /* as blur_walk */+ (*k)++;+ }+ for (Node *c = n->first; c; c = c->next) blur_collect(c, bb, bm, k);+}++/* The list for this frame, or 0 if it has to be one box. */+static int multi_rects(Node *root) {+ if (!paint_multi_damage || layer.want || gpu_active() || !root) return 0;+ Box *d = &ui.damage;+ if (d->x0 != ui.dlast.x0 || d->y0 != ui.dlast.y0 || d->x1 != ui.dlast.x1 || d->y1 != ui.dlast.y1) return 0; /* set directly */+ if (ui.dlist.n < 2) return 0;+ DamageList l = { .n = 0 };+ for (int i = 0; i < ui.dlist.n; i++) {+ Box b = ui.dlist.r[i];+ dlist_add(&l, (Box){ floorf(b.x0), floorf(b.y0), ceilf(b.x1), ceilf(b.y1) });+ }+ /* join what is cheaper to paint together */+ while (l.n > 1) {+ int bi = -1, bj = -1;+ float best = DAMAGE_PASS_PX;+ for (int i = 0; i < l.n; i++)+ for (int j = i + 1; j < l.n; j++) {+ Box u = { fminf(l.r[i].x0, l.r[j].x0), fminf(l.r[i].y0, l.r[j].y0), fmaxf(l.r[i].x1, l.r[j].x1), fmaxf(l.r[i].y1, l.r[j].y1) };+ float w = box_area(u) - box_area(l.r[i]) - box_area(l.r[j]);+ if (w < best) { best = w; bi = i; bj = j; }+ }+ if (bi < 0) break;+ Box u = { fminf(l.r[bi].x0, l.r[bj].x0), fminf(l.r[bi].y0, l.r[bj].y0), fmaxf(l.r[bi].x1, l.r[bj].x1), fmaxf(l.r[bi].y1, l.r[bj].y1) };+ l.r[bj] = l.r[--l.n];+ l.r[bi] = l.r[--l.n];+ dlist_add(&l, u);+ }+ /* blurred surfaces: see above */+ Box bb[BLUR_MAX];+ float bm[BLUR_MAX];+ int nb = 0;+ blur_collect(root, bb, bm, &nb);+ if (nb > BLUR_MAX) return 0;+ int changed = 1;+ for (int pass = 0; changed && pass < 8; pass++) {+ changed = 0;+ for (int k = 0; k < nb; k++) {+ Box b = bb[k], near = { b.x0 - bm[k], b.y0 - bm[k], b.x1 + bm[k], b.y1 + bm[k] };+ int hits = 0, inside = 0;+ Box u = b;+ for (int i = 0; i < l.n; i++) {+ if (!box_meets(l.r[i], near)) continue;+ hits++;+ if (box_inside(b, l.r[i])) inside = 1;+ u = (Box){ fminf(u.x0, l.r[i].x0), fminf(u.y0, l.r[i].y0), fmaxf(u.x1, l.r[i].x1), fmaxf(u.y1, l.r[i].y1) };+ }+ if (!hits || (hits == 1 && inside)) continue;+ for (int i = 0; i < l.n; i++) if (box_meets(l.r[i], near)) l.r[i--] = l.r[--l.n];+ dlist_add(&l, (Box){ floorf(u.x0), floorf(u.y0), ceilf(u.x1), ceilf(u.y1) });+ changed = 1;+ }+ }+ if (changed || l.n < 2) return 0;+ Box all = dl_bbox(&l);+ float sum = 0;+ for (int i = 0; i < l.n; i++) sum += box_area(l.r[i]);+ if (sum >= DAMAGE_COVER * box_area(all)) return 0;+ for (int i = 0; i < l.n; i++) drects[i] = l.r[i];+ ndrects = l.n;+ *d = all;+ ui.dlast = all;+ return 1;+}++int paint_damage_rects(Box *out, int max) {+ int n = ndrects < max ? ndrects : max;+ for (int i = 0; i < n; i++) out[i] = drects[i];+ return n;+}+int paint_damage(Node *root) {/* lo que llegó de afuera desde el cuadro anterior (terminal, apps Linux, video): si toca la* ventana, se vuelve a pintar su capa; si cae afuera, lo de abajo */@@ -724,6 +849,8 @@ int paint_damage(Node *root) {if (!box_inside(ext, layer.at) && !box_inside(ext, now)) layer.below_valid = 0;}if (root) damage_walk(root, 0, 1, 0, (Box){ -1e9f, -1e9f, 1e9f, 1e9f });+ ndrects = 0;+ if (multi_rects(root)) return 1;/* the damage is one box: growing it for a blurred node can reach another one (the bar and a menu) */for (int pass = 0, grew = 1; root && grew && pass < 4 && ui.damage.x1 > ui.damage.x0; pass++) { grew = 0; blur_walk(root, &grew); }if (layer.want && layer.valid && layer.part.x1 > layer.part.x0) { /* esa parte, donde está ahora la ventana */@@ -734,6 +861,7 @@ int paint_damage(Node *root) {}}Box *d = &ui.damage;+ if (d->x1 > d->x0 && d->y1 > d->y0) { drects[0] = *d; ndrects = 1; }return d->x1 > d->x0 && d->y1 > d->y0;}@@ -751,7 +879,13 @@ static void cover_walk(Node *n, Box d, Box clip, int *found) {if (d.x0 >= in.x0 && d.y0 >= in.y0 && d.x1 <= in.x1 && d.y1 <= in.y1) *found = n->id;}if (n->type == NODE_TEXT) return;+ /* its children are clipped to its box: if that doesn't hold the region, none of them can */+ if (s->overflow_hidden && !(d.x0 >= box.x0 && d.y0 >= box.y0 && d.x1 <= box.x1 && d.y1 <= box.y1)) return;Box inner = s->overflow_hidden ? box : clip;+ /* rounded corners clip the children too: what lies in them shows this node's background, so+ * a child only covers away from the corners (0.8.2) */+ float rr = node_edges_plain(n) ? s->radius : max4(s->ext->r.rad);+ if (s->overflow_hidden && rr > 0) inner = (Box){ inner.x0 + rr, inner.y0 + rr, inner.x1 - rr, inner.y1 - rr };Node *kids[MAX_KIDS];int k = node_children_ordered(n, kids, MAX_KIDS);for (int i = 0; i < k; i++) cover_walk(kids[i], d, inner, found);@@ -771,6 +905,7 @@ void paint_root(Canvas *c, Node *root) {skip_until = gpu_active() ? 0 : media_cover_node(ui.damage.x0, ui.damage.y0, ui.damage.x1, ui.damage.y1);if (!skip_until && !gpu_active() && !layer.want) skip_until = opaque_cover_node(root);skip_reached = 0;+ paint_cull = !gpu_active();/* una ventana que se mueve: lo de abajo se copia y se pinta desde ella (capa del arrastre) */if (!skip_until && below_ready(c, root)) {below_copy(c);@@ -778,6 +913,7 @@ void paint_root(Canvas *c, Node *root) {}paint_node(c, root, 1);skip_until = 0;+ paint_cull = 0;tf_gc();wl_paint_popups(c); /* menús de las apps Wayland, encima de todo */if (paint_profile) {diff --git a/server/src/ui/ui.h b/server/src/ui/ui.hindex 7004882..8675366 100644--- a/server/src/ui/ui.h+++ b/server/src/ui/ui.h@@ -120,6 +120,12 @@ static inline const float *node_padding(const Node *n) { return n->s.ext ? n->s.static inline float node_border(const Node *n, int e) { return n->s.ext ? n->s.ext->r.bw[e] : n->s.border_width; }static inline int node_edges_plain(const Node *n) { return !n->s.ext || n->s.ext->r.plain; }+/* Damage as a short list of rectangles (0.8.2): small changes far apart (the Monitor's moving+ * dots) are repainted one by one instead of the box around all of them. dlist_add merges a+ * rectangle into the ones it overlaps and, past DAMAGE_MAX, the pair that wastes the least area. */+#define DAMAGE_MAX 16+typedef struct { Box r[DAMAGE_MAX]; int n; } DamageList;+typedef struct {Node **nodes;int cap;@@ -128,7 +134,9 @@ typedef struct {float width, height;char theme_dir[1024];double layout_ms, paint_ms; /* last frame */- Box damage; /* region to repaint */+ Box damage; /* region to repaint (the box around dlist) */+ DamageList dlist; /* the same damage as separate rectangles (damage_add) */+ Box dlast; /* ui.damage as damage_add left it: if it differs, it was set directly */double damage_px; /* pixels repainted in the last frame */int quit_requested; /* lord.quit() */uint8_t rtl; /* interface direction (Arabic, Hebrew… language) */@@ -153,6 +161,8 @@ void node_moved(Node *n); /* only left/top changed (window drag): no rint node_gather_text(const Node *n, char *buf, int size);int node_children_ordered(Node *n, Node **out, int max);void damage_add(Box b);+void dlist_add(DamageList *l, Box b);+float box_area(Box b);void damage_subtree(Node *n);void ui_reset(void);@@ -266,6 +276,10 @@ extern PaintView paint_view;void paint_layer_view(int index); /* the display with most of the window being moved */int paint_layer_node(void); /* that window's node (0: no drag layer) */int paint_damage(Node *root); /* computes ui.damage; 0 if there is nothing to repaint */+/* after paint_damage: the rectangles to repaint (1 = just ui.damage), each inside ui.damage;+ * see "Several damaged rectangles" in paint.c */+int paint_damage_rects(Box *out, int max);+extern int paint_multi_damage; /* 0: always one rectangle (LORD_DAMAGE=1, tests) */void paint_root(Canvas *c, Node *root);Node *hit_test(Node *root, float x, float y);/* Capture mode (media/capture.c): protected nodes are painted black and app windowsdiff --git a/server/tests/engine_test.c b/server/tests/engine_test.cindex 562d426..cc08baf 100644--- a/server/tests/engine_test.c+++ b/server/tests/engine_test.c@@ -442,6 +442,151 @@ static void test_backdrop_band(void) {free(partial);}+/* Several damaged rectangles (0.8.2): dots moving far apart on a "map" are repainted one rectangle+ * at a time, and every frame must be pixel-identical to a full repaint, also with a translucent,+ * blurred bar whose margin a dot crosses (the 0.8.1 band rule, test_backdrop_band). */+static uint32_t rng_state = 12345;+static uint32_t rnd(void) { rng_state = rng_state * 1103515245u + 12345u; return rng_state >> 8; }++static int paint_partial(Node *root, int w, int h, int *nrects, float *area) {+ Box d = { 0, 0, 0, 0 };+ if (!paint_damage(root)) { *nrects = 0; *area = 0; return 0; }+ d = ui.damage;+ Box rs[DAMAGE_MAX];+ int n = paint_damage_rects(rs, DAMAGE_MAX);+ *nrects = n;+ *area = 0;+ for (int i = 0; i < n; i++) {+ Box b = rs[i];+ if (b.x0 < d.x0 || b.y0 < d.y0 || b.x1 > d.x1 || b.y1 > d.y1) return -1; /* outside the box */+ for (int j = i + 1; j < n; j++)+ if (b.x0 < rs[j].x1 && rs[j].x0 < b.x1 && b.y0 < rs[j].y1 && rs[j].y0 < b.y1) return -2; /* overlap */+ Rect r = { (int)fmaxf(0, floorf(b.x0)), (int)fmaxf(0, floorf(b.y0)), (int)fminf((float)w, ceilf(b.x1)), (int)fminf((float)h, ceilf(b.y1)) };+ if (r.x1 <= r.x0 || r.y1 <= r.y0) continue;+ *area += (float)(r.x1 - r.x0) * (r.y1 - r.y0);+ ui.damage = (Box){ (float)r.x0, (float)r.y0, (float)r.x1, (float)r.y1 };+ canvas_set_clip(&frame, r);+ canvas_clear(&frame, 0);+ paint_root(&frame, root);+ }+ canvas_set_clip(&frame, (Rect){ 0, 0, w, h });+ ui.damage = (Box){ 0, 0, 0, 0 };+ return 1;+}++static int full_differs(Node *root, int w, int h, int *where) {+ static uint32_t *ref;+ static int rw, rh;+ if (rw != w || rh != h) { free(ref); ref = calloc((size_t)w * h, 4); rw = w; rh = h; }+ if (!ref) return 999;+ Canvas rc;+ canvas_init(&rc, ref, w, h, w);+ ui.damage = (Box){ 0, 0, (float)w, (float)h };+ canvas_clear(&rc, 0);+ paint_root(&rc, root);+ ui.damage = (Box){ 0, 0, 0, 0 };+ int worst = 0;+ for (int i = 0; i < w * h; i++)+ for (int sh = 0; sh < 24; sh += 8) {+ int dv = abs((int)((ref[i] >> sh) & 255) - (int)((fb[i] >> sh) & 255));+ if (dv > worst) { worst = dv; *where = i; }+ }+ return worst;+}++static void test_multi_damage(void) {+ enum { W = 640, H = 400, DOTS = 24 };+ ui_reset();+ Node *root = view(NULL);+ ui.root = root;+ sn(root, "backgroundColor", 0xff1c1c1e);+ for (int i = 0; i < 16; i++) box_in(root, (float)(i * 40), 0, 14, H, 0xff2c4a7a); /* something to blur */+ Node *map = box_in(root, 40, 34, 560, 340, 0xff20304a);+ ss(map, "overflow", "hidden");+ sn(map, "borderRadius", 12);+ for (int i = 0; i < 6; i++) box_in(map, (float)(30 + i * 80), (float)(40 + (i % 3) * 70), 50, 30, 0xff3a5a3a); /* "land" */+ Node *dots[DOTS];+ float dx[DOTS], dy[DOTS];+ for (int i = 0; i < DOTS; i++) {+ dx[i] = (float)(rnd() % 540);+ dy[i] = (float)(rnd() % 300);+ dots[i] = box_in(map, dx[i], dy[i], 6, 6, i % 2 ? 0xd8ff9f0a : 0xd830d158); /* translucent, round */+ sn(dots[i], "borderRadius", 3);+ }+ Node *barl = layer_node(root, 20, W, H);+ Node *bar = box_in(barl, 0, 0, W, 28, 0x80ffffff);+ sn(bar, "backdropBlur", 10);+ render(root, W, H);+ int multi = 0, worst_all = 0, bad = 0;+ float saved = 0;+ for (int f = 0; f < 40; f++) {+ for (int i = 0; i < DOTS; i++) {+ if (i % 3 == f % 3) continue; /* not all of them every frame */+ dx[i] += (float)((int)(rnd() % 9) - 4) + 0.5f;+ dy[i] += (float)((int)(rnd() % 7) - 3);+ if (f % 10 == 7 && i == 0) dy[i] = -20; /* up near the bar: inside its blur margin (the map clips it) */+ if (dx[i] < -6) dx[i] = 540;+ if (dx[i] > 560) dx[i] = 0;+ if (dy[i] > 320 || dy[i] < -20) dy[i] = 100;+ sn(dots[i], "left", dx[i]);+ sn(dots[i], "top", dy[i]);+ }+ if (f % 13 == 5) sn(bar, "backgroundColor", f % 2 ? 0x80ffffff : 0x90f0f0ff); /* the bar itself */+ if (f % 17 == 9) { Node *m = box_in(barl, 200, 20, 80, 60, 0xffff3030); render(root, W, H); node_destroy(m); } /* a menu opens, then closes */+ layout_root(root, W, H);+ int n = 0;+ float area = 0;+ int ok = paint_partial(root, W, H, &n, &area);+ if (ok < 0) { bad++; continue; }+ if (n > 1) { multi++; saved += area; }+ int where = 0, worst = full_differs(root, W, H, &where);+ if (worst > worst_all) worst_all = worst;+ CHECK(worst <= 1, "frame %d (%d rects) differs from a full repaint by %d at %d,%d", f, n, worst, where % W, where / W);+ }+ CHECK(!bad, "%d frames had a rectangle outside the box or two that overlap", bad);+ CHECK(multi >= 20, "only %d of 40 frames used several rectangles", multi);+ /* the same with one rectangle forced, for the numbers */+ paint_multi_damage = 0;+ float one = 0;+ int ones = 0;+ for (int f = 0; f < 10; f++) {+ for (int i = 0; i < DOTS; i++) { dx[i] = (float)(rnd() % 540); sn(dots[i], "left", dx[i]); }+ layout_root(root, W, H);+ int n = 0;+ float area = 0;+ paint_partial(root, W, H, &n, &area);+ one += area;+ ones++;+ int where = 0;+ CHECK(n <= 1 && full_differs(root, W, H, &where) <= 1, "one rectangle (LORD_DAMAGE=1) differs from a full repaint");+ }+ paint_multi_damage = 1;+ if (multi && ones) printf("multi-damage: %d frames, %.0f px per frame in rectangles; one box: %.0f px\n", multi, saved / multi, one / ones);+ /* ui.damage set directly (a full repaint): one rectangle, the whole of it */+ damage_add((Box){ 10, 10, 20, 20 });+ ui.damage = (Box){ 0, 0, W, H };+ damage_add((Box){ 100, 100, 110, 110 });+ layout_root(root, W, H);+ paint_damage(root);+ Box rs[DAMAGE_MAX];+ int n = paint_damage_rects(rs, DAMAGE_MAX);+ CHECK(n == 1 && rs[0].x0 <= 0 && rs[0].x1 >= W && rs[0].y1 >= H, "a full repaint set directly stays one rectangle (%d)", n);+ ui.damage = (Box){ 0, 0, 0, 0 };+ /* the list itself: overlaps merge, the cap holds */+ DamageList l = { .n = 0 };+ dlist_add(&l, (Box){ 0, 0, 10, 10 });+ dlist_add(&l, (Box){ 5, 5, 15, 15 });+ dlist_add(&l, (Box){ 100, 100, 110, 110 });+ CHECK(l.n == 2 && l.r[0].x1 == 15 && l.r[1].x0 == 100, "dlist merges overlaps (%d)", l.n);+ for (int i = 0; i < 100; i++) dlist_add(&l, (Box){ (float)(i * 20), 300, (float)(i * 20 + 5), 305 });+ CHECK(l.n <= DAMAGE_MAX, "dlist stays at %d rectangles (%d)", DAMAGE_MAX, l.n);+ int overl = 0;+ for (int i = 0; i < l.n; i++) for (int j = i + 1; j < l.n; j++)+ if (l.r[i].x0 < l.r[j].x1 && l.r[j].x0 < l.r[i].x1 && l.r[i].y0 < l.r[j].y1 && l.r[j].y0 < l.r[i].y1) overl++;+ CHECK(!overl, "dlist never holds two overlapping rectangles (%d)", overl);+ (void)worst_all;+}+static void test_transform_paint_hit(void) {ui_reset();Node *root = view(NULL);@@ -715,6 +860,7 @@ int main(int argc, char **argv) {test_transform_paint_hit();test_layers();test_backdrop_band();+ test_multi_damage();printf("engine-test: %d checks, %d failures\n", checks, fails);return fails ? 1 : 0;#endif