Skip to content

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.

Step 10: composite the dragged window on the GPU (G0), off unless its self-test passes; C3 fix (a transformed node whose own style changes re-rasterizes); GL_EXT_unpack_subimage fallback (whole rows); review fixes: rounded clipping boxes painted after the window refuse G0, pixel position from a highp varying, a placement pass at output size in the self-test, a failed G0 upload skips present, premul_bad on the CPU path, self-test cached per renderer and run at gpu_init

Federico Pereira51a6f78a7833
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4d373d9..25a096f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -364,6 +364,119 @@ of the matching [GitHub release](https://github.com/lordbasex/LordOS/releases),
- Not done: skipping the copy under the window's opaque middle (it would have to know, before the layer is
painted this frame, that the layer stays opaque there and that no ancestor's rounded corner reads it). The time
saved has not been measured yet.
+ - **The dragged window composited on the GPU, with the CPU's pixels ("G0", step 10, approved by the maintainer on
+ 2026-10-28; `ui/paint.c`, `gpu/gpu.c`, `main.c`; `docs/GPU.md`, "The dragged window on the GPU").** On the GPU
+ path a frame that uses the copy of what is below the window paints only the strips of its damaged box around
+ the window's rectangle W; the GPU writes W itself, from two textures of its own (the layer and the copy of what
+ is below), with one shader that does `blit_row`'s integer arithmetic (k = 256, `over_alpha`) in 24-bit floats,
+ where every value is an integer below 2^24 and every scaling a power of two, so the pixels are the CPU's bit for
+ bit. The textures get only what changed since the GPU last had them (the layer's part, the copy's partial
+ rebuilds), so the per-frame upload of the old and new window box (2.2–2.6 MB on the Pi) becomes the strips and
+ the layer's part.
+ - **When:** only after `below_ready` decided to use the copy (its decisions are unchanged), on the display that
+ owns the layer, one damaged box, no rotation and no fullscreen app; the layer valid and painted as it is (the
+ same opacity, no full render pending, no app window in it, no pixel with a colour above its alpha:
+ `layer.premul_bad`, checked where `layer_holes` already walks it); W is the blit cut by the ancestors' clips
+ and the box; no ancestor paints over W afterwards (`below_ancestors_ok`); no app window of layer 0 is drawn
+ into W (`wl_draws_meet`); the paint order around the window is known and no node painted after it that paints
+ anything meets the box (`damage_walk` notes them, grown by a backdrop blur's reach and a transform's edge).
+ Anything that changes while the strips are painted (the layer rendered again elsewhere, an app window) paints
+ W on the CPU in the same frame. Otherwise the frame is painted as before.
+ - **The GPU side** is off unless it proves itself: a `highp` fragment shader with at least 24-bit floats
+ (`glGetShaderPrecisionFormat`), then a self-test in `gpu_init` that draws every valid premultiplied (alpha,
+ colour ≤ alpha) over every destination value, and every source alpha over every destination alpha, into a
+ framebuffer (3,145,728 pixels in three 1024 × 1024 tiles), then a placement pass (below), and compares each
+ pixel with `canvas_blit` on the CPU (NEON on the Pi). One differing pixel turns G0 off. `LORD_G0=0` turns it
+ off. `LORD_GPU_VERIFY=N` paints W on the CPU too every N-th G0 frame
+ and compares it with the framebuffer read back (`gpu-verify: frame N W=wxh differ=K worst=D`).
+ - **`main.c`** keeps, per display, that the scene and the UI texture are stale in W (`g0on`, `g0w`): W joins the
+ next frame's damage; a frame with no UI damage (the cursor, an app's new frame) draws the window again from the
+ textures if they are still that display's, else paints W on the CPU; direct scanout, a failed frame or the GPU
+ going away paint it again. A frame whose quad cannot be drawn (a failed upload, G0 turned off) is not shown
+ at all (`gpu_frame_ex` returns -2): the display keeps the frame before and the next one paints W on the CPU. `LORD_PAINT_VERIFY` leaves W out on G0 frames (`LORD_GPU_VERIFY` checks it). The
+ `drag: …` and `drag (scripted): …` lines end with `G0: N of M frames (Q quads), strips … Mpx, uploaded below …
+ Mpx, layer … Mpx (not: … layer, … clip, … ancestor, … app, … after, … post)`.
+ - Checked on VM 116 (x86_64): `canvas-test` compares G0's shader written in C (`canvas_g0_model`) with
+ `blit_row_c` and the SIMD row over all 8,421,376 valid (alpha, colour, destination) combinations, every alpha
+ pair, and 200,000 random pixels: 0 differ (NEON not run: no ARM machine was used). `engine-test` has a third
+ path, `g0`: the GPU path with G0 and a stub GPU that keeps the UI texture, the two textures (only their dirty
+ rectangles) and `main.c`'s bookkeeping, and writes W with `canvas_g0_model`; all 2,417 frames equal
+ `drag_golden_gpu.txt`, 250 of the 1,856 frames that use the copy are composited this way (refused: 436 layer,
+ 287 app, 883 after: the big scene's window is mostly near the menu bar or the Dock). Four runs without goldens
+ are compared frame by frame with the GPU path: a 30-frame pause with no damage (54 of 61 G0), two displays side
+ by side (150 of 240), the window over an app window below (5 of 180), and the window against a rounded,
+ bordered container's corner (15 of 90; 44 refused for the ancestor); the single-display ones as the GPU shows
+ them (the app windows under the UI texture, W written over both). Taking out the after-node, app-window or
+ ancestor check fails the test; the layer's own checks (app holes, invalid pixels) are not reached by these
+ scenes. `make test`: 23 passed, 0 failed, 13 skipped; `engine-test`: 3,688 checks, 0 failures. With GPU=0 the
+ build has no warnings.
+ - On VM 116 at runtime (llvmpipe forced with `LORD_GPU=1`, an isolated session lent the card by
+ `tests/seat_cycle.c`, a test scene moved by `lord.wm.script`): the self-test of that time (tiles only, in the
+ first drag frame) passes (75–113 ms); with
+ `LORD_GPU_VERIFY=1`, 1,481 of 1,483 frames were G0 and 0 pixels differed in any; across 4 GPU context
+ re-creations during the move, 1,328 G0 frames, 0 differ; with `LORD_PAINT_VERIFY=16`, 0 pixels differ from a
+ full repaint outside W in 92 checks. Layout and paint of a drag frame: 0.29–0.35 ms with G0, 0.92 ms without
+ (llvmpipe's own composition dominates the rest, so this says nothing yet about the Pi). `tests/gpu_cycle.sh` 10
+ cycles: 38 fds and 226.6–227.6 MB throughout. A real screenshot of G0 frames was taken (seat-cycle's snapshot).
+ Not measured yet: the Pi 5 (V3D), where the self-test and `LORD_GPU_VERIFY` must pass before the gain is
+ claimed.
+ - **With the real desktop G0 never composed a frame, fixed:** the list of nodes painted after the window
+ held 128 boxes and refused every frame once it was full, and LordOS's menu bar and Dock alone fill it
+ (`drag: … G0: 0 of 250 frames … 248 after`). The list now grows as needed (up to 65,536 boxes, then the
+ frame is refused as before) and skips a box inside the one noted just before it (an icon inside its bar:
+ it meets the damaged box only if the bar does), so the menu bar and the Dock are 2 boxes. The decision is
+ the same for every frame that fit before. `engine-test`'s `pause` run now has 300 more nodes painted after
+ the window, each in its own box: with the old 128-box list it fails (`G0 composed 0 of 61`), with this one
+ 54 of 61 frames, the same as the GPU path frame by frame.
+ - Checked again on VM 116 with the default theme (an isolated 1280 × 800 session lent the card by
+ `seat-cycle`, llvmpipe, driven over the remote desktop socket; real screenshots with QMP `screendump`):
+ the self-test of that time (tiles only) passes (3,145,728 pixels, 127–146 ms). Calculator dragged sideways in the band the menu bar
+ and the Dock leave free: 152 of 153 frames G0, and along a figure of eight that reaches them, 7 of 153;
+ `LORD_GPU_VERIFY=1` compared 160 G0 frames with the CPU (W up to 462 × 592), 0 pixels differ in any.
+ LordBench's Desktop impact gets 1 G0 frame of 2,024 at 1280 × 800: its window, with its shadow, always
+ reaches the Dock's or the menu bar's blurred backdrop (1,377 refused for that) and its turning square
+ makes the layer be painted whole (646); result with G0 on: 72.3 fps alone (p50 12.0 ms, p99 16.7 ms),
+ 63.8 fps under load (p50 14.4, p99 27.8 ms, 10.8 % late); `LORD_G0=0`: 73.9 fps, 12.0 / 16.7 ms alone,
+ 62.0 fps, 14.8 / 26.3 ms, 15.6 % late under load (the same within llvmpipe's noise). `LORD_GPU=0` (CPU
+ composition): the same drags paint as before (no G0 tried) and LordBench gives 75.0 fps alone and under load
+ (p50 5.1 / 5.7 ms). After the runs the VM's normal session (`lordos.service`) was started again.
+ - **Fixed after an adversarial review of step 10** (on VM 116; the runtime runs above came before these fixes
+ and were not repeated, the VM's own session was left alone):
+ - *Wrong pixels under a rounded clipping box painted after the window.* A view with `overflow: hidden` and
+ rounded corners that paints nothing itself still saves and restores its corners around its children, which
+ darkens what is below them by about 1 level; `node_paints` did not count it, so G0 left it out inside W. It
+ now counts (`ui/paint.c`). New `engine-test` run `corner` (such a 200 × 150 box, radius 60, over the
+ window's path), compared frame by frame with the GPU path: without the fix 59 of its 91 frames differ; with
+ it 0 differ (its box is in every frame's damage: of 90 frames that use the copy, 89 are refused as `after`,
+ 1 for the layer).
+ - *The pixel position no longer comes from `gl_FragCoord`*, which GLSL ES 1.00 makes `mediump`: with 16-bit
+ floats it cannot tell x or y from the next pixel past 1024. G0 has its own vertex shader that passes a
+ `highp` varying in pixels (`v_px`, the quad's corners on pixel edges) and the shader uses `floor(v_px)`. The
+ self-test gained a placement pass: a framebuffer as large as the largest output and at least 4096 × 2048
+ (else the largest the GPU allows, and the log says so), a copy of what is below that size and a layer at an
+ odd offset (37, 21) reaching its far edges, every pixel a different random value, compared with the CPU. A
+ layer or output past what it covered turns G0 off (`gpu_g0_ready(out)` refuses such an output up front).
+ Checked headless on VM 116 (a test driver that includes `gpu.c`, a surfaceless GLES2 context on llvmpipe):
+ the full self-test passes (11,373,321 pixels at 1920 × 1080, 17,678,025 for a 5120 × 2880 output); with the
+ same shader rounding `v_px` as 16-bit floats would, the tiles pass and the placement pass fails from x = 1024
+ on (6,207,545 pixels differ), so the old test alone would have passed it.
+ - *A failed G0 upload no longer shows a frame with a stale W:* when the quad cannot be drawn `gpu_frame_ex`
+ returns -2 before presenting, and `main.c` joins W to the damage and paints it on the CPU in the next frame
+ without trying G0 (`g0cpu`), keeping the GPU on. Headless, with a layer larger than the self-test covered:
+ -2, nothing presented, G0 off; again with G0 off: -2. A real failed upload was not provoked, and `main.c`'s
+ side has only been built, not run.
+ - *`layer.premul_bad` on a layer rendered on the CPU path* (no hole map) was never computed: it is now set,
+ so G0 never trusts such a layer after the GPU comes back.
+ - `LORD_GPU_VERIFY`'s CPU reference walk paints on a scratch canvas of the scene's size (an ancestor's rounded
+ corner above or left of W read before its old W-sized buffer).
+ - The self-test runs in `gpu_init`, not in the first drag frame, and its outcome is kept per GL renderer and
+ version across contexts (a GPU restart does not run it again): 311–406 ms on llvmpipe the first time
+ (it was 75–146 ms without the placement pass), about 1 ms after. Stale GL errors are drained before it,
+ dithering is off for the quad, the error drain before an upload is bounded, and G0's program and textures
+ are freed when it does not turn on.
+ - `make -j8` (GPU=1) and lord-server with GPU=0: no warnings; `make test`: 23 passed, 0 failed, 13 skipped;
+ `engine-test`: 3,690 checks, 0 failures, `drag_golden_{cpu,gpu}.txt` unchanged (250 of 1,856 frames that
+ use the copy still G0); `canvas-test`: 8,621,376 cases, 0 differ.
- **`js: unsupported style '<key>'` is logged once per key** (`style_warn` in `ui/js.c`), not on every
`setStyle` call: a style an animation sends every frame wrote a line to the session log every frame (as
`transform` did before LordBench's square and the fans moved to `rotate`). The first 64
@@ -486,6 +599,27 @@ of the matching [GitHub release](https://github.com/lordbasex/LordOS/releases),
to 11 rectangles and this one gives one is not investigated.
LordBench's desktop mode in the headless harness (three runs each): 58.9-60.0 fps, build p50 1.25-1.26 ms and p99
5.9-7.6 ms before; 60.0 fps, p50 1.24-1.27 ms and p99 5.95-6.32 ms after.
+- **A rotated or scaled node now repaints when its own style changes** (approved by the maintainer on 2026-10-28).
+ A node with its own `rotate`/`scaleX`/`scaleY` is painted into a cached raster (`tfl[]` in `server/src/ui/paint.c`).
+ `damage_walk` cleared the node's `paint_dirty` before its children, and `tf_check`, which runs after them, tested
+ that flag, so a change to the node's own background, border, radius or gradient (`lord.setStyle` or a React
+ render) kept the raster and the old look stayed on screen until something else re-rasterized it. `damage_walk`
+ now keeps the node's own flag and passes it to `tf_check`. A change of the transform alone still reuses the raster
+ (the compared style leaves the transform out). New `engine-test` check `test_transform_self_restyle` (a 40 × 40 box
+ rotated 15°, its background changed from red to blue, the centre pixel must be blue): it fails without the fix
+ (`ff0000`) and passes with it. The drag goldens (`server/tests/drag_golden_{cpu,gpu}.txt`) are unchanged: 2417
+ frames per path, the same per-frame hashes, 0 pixels differ in the 591 frames switched to the copy of what is
+ below; `make test` on VM 116: 23 passed, 0 failed, 13 skipped; `engine-test`: 3662 checks, 0 failures.
+- **GPU composition without `GL_EXT_unpack_subimage`** (approved by the maintainer on 2026-10-28;
+ `server/src/gpu/gpu.c`). The UI's damage was always uploaded with `GL_UNPACK_ROW_LENGTH_EXT`; a GLES2 driver
+ without that extension ignores it (`GL_INVALID_ENUM`) and reads each damaged rectangle narrower than the screen
+ with the wrong row length, so the uploaded pixels came out sheared. The log line also said "the whole canvas is
+ uploaded", which was not true. Now the extension is read from the GL extension list; without it the damage is
+ uploaded as whole rows in one call when the scene's rows are contiguous (they are: the scene is created with
+ `stride == width`), or one row at a time otherwise, and the log says so. With the extension (every Mesa driver,
+ so every machine LordOS has been tested on) nothing changes. Built with `GPU=1` and `GPU=0` on VM 116 with no
+ warnings; the fallback itself has not run on a driver that lacks the extension.
+
- **LordBench's turning square and the Cooling page's fans now turn** (approved by the maintainer on 2026-10-28).
`apps/sysinfo/scene.jsx` and `sdk/cooling.jsx` called `lord.setStyle(n, 'transform', 'rotate(…deg)')`, and
`transform` is not a key the engine takes (it only logged `js: unsupported style 'transform'`), so the desktop
diff --git a/docs/GPU.md b/docs/GPU.md
index 69d5211..b1cd7ac 100644
--- a/docs/GPU.md
+++ b/docs/GPU.md
@@ -68,7 +68,10 @@ milliseconds no longer turns 60 frames a second into 57.
## How it composites
- **The UI** (React → CPU canvas) is a fullscreen texture. Per frame only what changed (the
- damage) is uploaded, with `GL_EXT_unpack_subimage`.
+ damage) is uploaded, with `GL_EXT_unpack_subimage` (`GL_UNPACK_ROW_LENGTH_EXT` = the scene's
+ stride). Without that extension (Mesa lists it for every GLES2 driver; it could be missing on a
+ vendor's own GLES stack) the damage goes up as whole rows, in one call while the scene's rows are
+ contiguous, or one row at a time otherwise; the log says `damage is uploaded as whole rows`.
- **App windows** don't go through the canvas. Wherever a window goes, the UI leaves a
**transparent hole** (alpha 0) and the surface is recorded. When presenting:
1. the window textures (layer 0);
@@ -102,6 +105,127 @@ milliseconds no longer turns 60 frames a second into 57.
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 dragged window on the GPU ("G0", 0.8.6, in development)
+
+While a window is dragged its pixels are a cached layer blitted over a copy of what is below it
+(`docs/WINDOWS.md`, "The moving window is a layer"). On the GPU path, a frame that uses that copy no
+longer paints and uploads the window's rectangle: the GPU writes it, with exactly the pixels the CPU would
+have written. Approved by the maintainer on 2026-10-28 (step 10 of the drag-frame work).
+
+**What the frame does** (`paint_root_g0` in `ui/paint.c`, `gpu_frame_ex` in `gpu/gpu.c`):
+
+1. `below_ready` decides, as before, whether the frame uses the copy. G0 only acts on those frames.
+2. W is where `layer_paint` would blit the layer, cut by the window's ancestors' clips and by the
+ frame's damaged box D. The CPU paints only D minus W (up to four strips), each a walk of the tree
+ clipped to the strip (the copy, then from the window on); the scene keeps its stale W.
+3. The GPU gets the strips (the UI texture), the layer and the copy of what is below as two textures of
+ their own, each uploaded only where it changed since the GPU last had it (the layer's repainted part,
+ the copy's partial rebuilds; all of it after a full render, a new size, another display or a new
+ context), and draws one quad over W, after the UI texture and before the menus and the cursor
+ (layer 1), with blending off.
+4. The shader (`fs_g0`) does `blit_row`'s arithmetic at k = 256 (`canvas.c`): where the layer's alpha
+ is 0 the copy's pixel; else `s + floor(d·(255−a)/256)` per colour and `a + floor(((d_a·(255−a) +
+ 128)·257) / 65536)` for alpha, with integers held in `highp` floats. Every intermediate value is an
+ integer below 2^24 (the largest is 16,744,321) and every scaling is by 2^-8 or 2^-16, so a 24-bit
+ float holds it exactly; texels are read back to integers with `floor(c·255 + 0.5)` (exact even
+ through 16-bit floats) and the output c/255 is converted to c by the framebuffer as the UI
+ texture's own pixels are. Over a framebuffer that is black under W (no app window is drawn there),
+ writing the result is what blending the UI texture gave. The pixel it works on comes from `v_px`, a
+ `highp` varying in pixels that G0's own vertex shader (`vs_g0`) passes from the quad's corners, not
+ from `gl_FragCoord`: GLSL ES 1.00 makes that `mediump`, and with 16-bit floats it cannot tell x or y
+ from the next pixel past 1024. Dithering is off for the quad.
+
+**When it is not used** (the frame is painted as before; the reasons are counted in the `drag` lines):
+the layer is not valid or would be painted whole this frame, has another opacity, has an app window in it,
+or has a pixel with a colour above its alpha (`layer.premul_bad`: there `blit_row`'s C and NEON versions
+differ from each other); W is empty; an ancestor of the window paints over W after it (a border, the
+restore of rounded corners); an app window of layer 0 is drawn into W (`wl_draws_meet`); the paint order
+around the window is unknown, or a node painted after the window that paints anything meets D (a view
+with `overflow: hidden` and rounded corners counts even when it has no colour: the save and restore of its
+corners around its children change what is below them; with a backdrop blur's reach: it would read the stale W, and a blur or a turned image paints differently when
+D is cut into strips); or something changed while the strips were painted, and then W is painted by the
+CPU in the same frame (post). Also not with several damaged rectangles, a rotated display, a fullscreen
+app, or on another display than the one that owns the layer.
+
+**The stale rectangle** (`main.c`, `g0on`/`g0w` per display): the next frame that paints the display
+joins W to its damage; a frame with no UI damage (the cursor, an app's new frame) draws the window again
+from the textures while they are still that display's, else paints W on the CPU; direct scanout, a
+failed frame and the GPU going away (`cpu_takeover`) paint it again; the frame after the drag ends
+(`paint_layer_end` damages the window's box) paints it on the CPU. When the quad cannot be drawn (an
+upload failed, or the layer or output is larger than the self-test covered: G0 turns off) `gpu_frame_ex`
+returns -2 before presenting: no frame is shown with a stale W, the display keeps the one before, and
+`main.c` paints W on the CPU in the next frame without trying G0 (`g0cpu`). `LORD_PAINT_VERIFY` leaves W
+out of its count on G0 frames.
+
+**Off unless it proves itself:** `gpu_init` sets G0 up. It needs a fragment shader with at least 24-bit
+`highp` floats (`glGetShaderPrecisionFormat`) and then a self-test, drawn into a framebuffer, read back
+and compared with `canvas_blit` on the CPU (the NEON row on the Pi):
+- arithmetic: every valid premultiplied (alpha, colour ≤ alpha) over every destination value (three of
+ them a pixel, the destination alpha varying), every source alpha over every destination alpha, and
+ random valid pixels, 3,145,728 pixels in three 1024 × 1024 tiles;
+- placement: a framebuffer as large as the largest output and at least 4096 × 2048 (else the largest
+ the GPU allows, and the log says `placement in WxH, the largest framebuffer the GPU allows`), a copy
+ of what is below of that size and a layer at an odd offset (37, 21) that reaches its far edges, every
+ pixel a different random value, so a texel read from the wrong place shows. An output larger than
+ that is refused (`gpu_g0_ready(out)`), and a layer larger than it turns G0 off.
+
+One differing pixel turns it off. The outcome is kept per GL renderer and version across contexts, so a
+GPU restart doesn't run it again. The log says `gpu: G0 on: … self-test: N pixels, every valid
+premultiplied combination and a WxH placement pass, the same as the CPU's, T ms` or `gpu: G0 off: …`
+with the reason.
+
+| Switch | Effect |
+|---|---|
+| `LORD_G0=0` | G0 off: the dragged window is painted by the CPU as before |
+| `LORD_GPU_VERIFY=N` | every N-th G0 frame W is also painted on the CPU (a walk clipped to W on a scratch canvas) and compared with the framebuffer read back: `gpu-verify: frame N W=wxh differ=K worst=D` |
+
+The `drag: …` and `drag (scripted): …` lines end with `G0: N of M frames (Q quads), strips … Mpx,
+uploaded below … Mpx, layer … Mpx (not: … layer, … clip, … ancestor, … app, … after, … post)`: frames
+composited, of those that used the copy; quads drawn; what the CPU painted around W; what went up to the
+two textures; and why the others were not.
+
+**Checked** (VM 116, x86_64):
+- `canvas-test`: the shader written in C with the same float operations (`canvas_g0_model`) against
+ `blit_row_c` and the SIMD row: all 8,421,376 valid (alpha, colour, destination) combinations, every
+ alpha pair, and 200,000 random pixels, 0 differ. The NEON row was not run (no ARM machine was used).
+- `engine-test`'s `g0` path: `test_drag_exact` with G0 and a stub GPU (the UI texture, the two textures
+ with only their dirty rectangles, `main.c`'s bookkeeping, W written by `canvas_g0_model`) gives every
+ one of the 2,417 frames of `drag_golden_gpu.txt`; 250 of 1,856 frames that use the copy are G0 frames
+ there. Five more runs are compared frame by frame with the GPU path: a pause with no damage, two
+ displays side by side, the window over an app window below, the window against a rounded,
+ bordered container's corner, and (`corner`) a rounded `overflow: hidden` box with no colour painted
+ after the window. Taking out the after-node, app-window or ancestor check fails it; without counting
+ the rounded clipping box as painting, 59 of `corner`'s 91 frames differ.
+- The self-test headless (a test driver that includes `gpu.c`, a surfaceless GLES2 context on llvmpipe,
+ VM 116): it passes, 11,373,321 pixels with a 4096 × 2048 placement pass for a 1920 × 1080 output in
+ 311–406 ms, 17,678,025 pixels (5120 × 2880) for a 5K output in 520 ms, about 1 ms when kept from an
+ earlier context. With the shader changed to round `v_px` as 16-bit floats would, the tiles pass and the
+ placement pass fails from x = 1024 on (6,207,545 pixels differ): the tiles alone would not catch it.
+ With a layer larger than the placement covered, `gpu_frame_ex` returns -2, presents nothing and turns
+ G0 off. `main.c`'s side of -2 has been built, not run.
+- At runtime with llvmpipe (`LORD_GPU=1`; an isolated session lent the card by `tests/seat_cycle.c`, a
+ scene moved by `lord.wm.script`; before the fixes above, with the self-test of that time: tiles only,
+ run in the first drag frame): the self-test passes in 75–113 ms; with `LORD_GPU_VERIFY=1`, 1,481
+ G0 frames of 1,483 and 0 pixels differ; across 4 context re-creations during the move, 1,328 G0 frames,
+ 0 differ; `tests/gpu_cycle.sh` (10 cycles) keeps 38 fds and the same memory. Layout and paint of a
+ drag frame: 0.29–0.35 ms with G0, 0.92 ms without (llvmpipe's composition is most of the frame, so
+ this is no measure of the gain on a real GPU).
+- With the default theme (the same kind of session, Calculator dragged over the remote desktop socket,
+ real screenshots with QMP `screendump`): 152 of 153 frames G0 while the window stays in the band the
+ menu bar and the Dock leave free, 7 of 153 along a figure of eight that reaches them; `LORD_GPU_VERIFY=1`
+ over 160 G0 frames (W up to 462 × 592): 0 pixels differ. The nodes painted after the window are kept
+ as a list that grows as needed (up to 65,536 boxes; a box inside the one noted before it is skipped, so
+ the menu bar and the Dock are 2 boxes): a fixed list of 128 was full with LordOS's own shell and G0
+ refused every frame. LordBench's Desktop impact at 1280 × 800 gets 1 G0 frame of 2,024: its window,
+ with its shadow, always reaches the Dock's or the menu bar's blurred backdrop, and its turning square
+ makes the layer be painted whole. Its numbers with G0 on and with `LORD_G0=0` are the same within
+ llvmpipe's noise.
+
+**Not checked yet:** the Raspberry Pi 5 (V3D), the machine this is for: its self-test, `LORD_GPU_VERIFY=1`
+over full LordBench desktop runs alone and under load, a perf run, a real screenshot and one run with
+`LORD_SW_CURSOR=1`. Until then no gain is claimed. Near the menu bar or the Dock the frame is still
+painted by the CPU.
+
## 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
diff --git a/docs/WINDOWS.md b/docs/WINDOWS.md
index e5ef540..dc3a54c 100644
--- a/docs/WINDOWS.md
+++ b/docs/WINDOWS.md
@@ -137,6 +137,15 @@ wm.beginResize(id, edges) // edges as in xdg_toplevel.resize: 1 top, 2 botto
0 and `layer_paint` doesn't walk the map. A part that brings the first hole into such a map zeroes
the rest of it first, so what is read is always what the full walk would have written. The
sentinel fill writes one row and copies it.
+ - **the GPU composites the window itself** (G0, 0.8.6, in development; `docs/GPU.md`, "The dragged
+ window on the GPU"): on a frame that uses the copy of what is below, the CPU paints only the strips
+ of the damaged box around the window's rectangle, and the GPU writes the rectangle from the layer
+ and the copy (two textures uploaded only where they changed) with `blit_row`'s own arithmetic, so
+ the pixels are the same. Not when an ancestor paints over the window, an app window is under it,
+ a node painted after it (the menu bar, the Dock) meets the box, or the layer has an app window or
+ would be painted whole; then the frame is painted as before. `LORD_G0=0` turns it off,
+ `LORD_GPU_VERIFY=N` compares it with the CPU every N-th frame. `engine-test`'s `g0` path checks it
+ against the GPU path's golden hashes.
- **There's no layer** if there is a backdrop blur inside (`backdropFilter`), or a video on the GPU
path.
- **A `canvas_blit` detail:** opaque pixels are copied directly (same result as blending, without
@@ -312,7 +321,10 @@ was checked against this path:
memory as before (a node 240 bytes larger made the benchmark's drag about 40 % slower).
- **Transforms:** a scaled or rotated subtree is painted into its own raster and composited; inside
a dragged window it goes into the drag layer like anything else. A transformed window is not put in
- the drag layer.
+ the drag layer. The raster is kept while only the transform changes, and painted again when a node
+ inside it, or the transformed node's own style (background, border, radius, gradient), changes:
+ `damage_walk` keeps the node's own `paint_dirty` for `tf_check`, which runs after the children
+ (`engine-test`'s `test_transform_self_restyle`).
`build/engine-test bench` (`server/tests/engine_test.c`, in the `debian:trixie` build container, i9-10900,
medians of 3 interleaved runs; "before" is the same benchmark built against the tree of checkpoint
diff --git a/server/src/canvas.c b/server/src/canvas.c
index 1063be8..e6effb0 100644
--- a/server/src/canvas.c
+++ b/server/src/canvas.c
@@ -115,6 +115,24 @@ static void mask_row_neon(uint32_t *row, const uint8_t *m, int n, uint32_t argb,
# define CANVAS_SIMD "C"
#endif
+/* G0's shader (gpu/gpu.c, fs_g0) in C, with the same float operations in the same order: a texel read
+ * back as c/255 and rounded, then the layer over the copy of what is below with integers held in
+ * floats. tests/canvas_test.c checks it against blit_row_c (and blit_row_neon) over every valid
+ * premultiplied combination; engine-test composes with it where the GPU would. */
+static float g0_fetch(uint32_t v) { return floorf((float)v / 255.0f * 255.0f + 0.5f); }
+uint32_t canvas_g0_model(uint32_t s, uint32_t d) {
+ float sc[4], dc[4], o[4];
+ for (int i = 0; i < 4; i++) { sc[i] = g0_fetch(s >> (8 * i) & 255); dc[i] = g0_fetch(d >> (8 * i) & 255); o[i] = dc[i]; }
+ if (sc[3] > 0.0f) { /* [3]: alpha, [0..2]: blue, green, red */
+ float ia = 255.0f - sc[3];
+ for (int i = 0; i < 3; i++) o[i] = sc[i] + floorf(dc[i] * ia * 0.00390625f);
+ o[3] = sc[3] + floorf((dc[3] * ia + 128.0f) * 257.0f * 0.0000152587890625f);
+ }
+ uint32_t r = 0;
+ for (int i = 0; i < 4; i++) r |= (uint32_t)floorf(o[i] / 255.0f * 255.0f + 0.5f) << (8 * i);
+ return r;
+}
+
#ifdef LORD_CANVAS_TEST
const char *canvas_test_simd(void) { return CANVAS_SIMD; }
void canvas_test_blit_row(int simd, uint32_t *row, const uint32_t *s, int n, uint32_t k) {
diff --git a/server/src/canvas.h b/server/src/canvas.h
index bf01e75..469755a 100644
--- a/server/src/canvas.h
+++ b/server/src/canvas.h
@@ -62,6 +62,7 @@ int canvas_affine_cut(void); /* 1 if one was cut since canvas_affine_
void canvas_alpha_mask(Canvas *c, int x, int y, const uint8_t *a, int w, int h, int stride, uint32_t argb);
/* Premultiplied ARGB image, with rounded corners and opacity. */
void canvas_blit(Canvas *c, const uint32_t *src, int sw, int sh, int x, int y, float r, float opacity, int opaque);
+uint32_t canvas_g0_model(uint32_t src, uint32_t dst); /* G0's shader in C: src (valid premultiplied) over dst, as blit_row at k = 256 */
/* Character bitmap: 'X' = fg, 'o' = bg, rest transparent (cursor). */
/* Scaled, bilinear: source rect (sx, sy, sw, sh) of an image sw_px × sh_px (stride in pixels) into the
* destination rect (dx, dy, dw, dh). Wayland buffers at another scale and wp_viewporter (CPU path). */
diff --git a/server/src/gpu/gpu.c b/server/src/gpu/gpu.c
index c2c2103..6ec12c0 100644
--- a/server/src/gpu/gpu.c
+++ b/server/src/gpu/gpu.c
@@ -38,6 +38,14 @@ void gpu_set_hooks(void (*lost)(void), void (*changed)(void)) { (void)lost; (voi
int gpu_tex_read(const GpuTex *t, uint32_t *px, int w, int h) { (void)t; (void)px; (void)w; (void)h; return -1; }
int gpu_add_output(Kms *k) { (void)k; return -1; }
int gpu_frame(int out, const Canvas *s, Rect d, void (*f)(int)) { (void)out; (void)s; (void)d; (void)f; return -1; }
+int gpu_g0_ready(int out) { (void)out; return 0; }
+int gpu_frame_ex(int out, const Canvas *s, const Rect *d, int n, const GpuG0 *g, int *drawn, void (*f)(int)) {
+ (void)out; (void)s; (void)d; (void)n; (void)g; (void)f;
+ if (drawn) *drawn = 0;
+ return -1;
+}
+int gpu_g0_holds(int out) { (void)out; return 0; }
+void gpu_g0_stats(long *b, long *l, long *q) { *b = *l = *q = 0; }
void gpu_draw(GpuTex *t, float dx, float dy, float dw, float dh, float sx, float sy, float sw, float sh, Rect c, float o) {
(void)t; (void)dx; (void)dy; (void)dw; (void)dh; (void)sx; (void)sy; (void)sw; (void)sh; (void)c; (void)o;
}
@@ -55,6 +63,7 @@ int gpu_read_done(void) { return 0; }
#include <stdarg.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
+#include <time.h>
#include <unistd.h>
#include <gbm.h>
#include <EGL/egl.h>
@@ -126,6 +135,7 @@ static struct {
GLuint prog[2], vbo; /* 0 = sampler2D, 1 = samplerExternalOES */
GLint u_tex[2], u_op[2], u_opaque[2], u_swap[2];
int bgra, have_external;
+ int unpack_sub; /* GL_EXT_unpack_subimage: GL_UNPACK_ROW_LENGTH_EXT exists */
char renderer[160];
PFNEGLCREATEIMAGEKHRPROC create_image;
PFNEGLDESTROYIMAGEKHRPROC destroy_image;
@@ -135,6 +145,12 @@ static struct {
long frames, scanouts;
char render_node[64]; /* where Mesa renders (on the Pi 5: v3d, not vc4) */
unsigned gen; /* this context's generation (0 = none) */
+ /* G0, the dragged window composited here (gpu_g0_ready): 0 off, 1 on (g0_setup, at gpu_init) */
+ int g0, g0out; /* g0out: the output whose upload the textures hold (-1: none) */
+ GLuint g0prog, g0tex[2]; /* 0: the copy of what is below, 1: the layer */
+ int g0tw[2], g0th[2]; /* their sizes (0: nothing uploaded) */
+ int g0lw, g0lh; /* the largest output the self-test's placement covered */
+ GLint gu_below, gu_layer, gu_bsize, gu_lrect, gu_swap;
} G;
/* These outlive the context (G is wiped by gpu_shutdown). */
@@ -142,6 +158,7 @@ static unsigned last_gen;
static const char *last_why = ""; /* gpu_info: the last gpu_init's outcome */
static unsigned long total_scanouts;
static char last_renderer[160]; /* the software renderer that was turned down */
+static long g0_bpx, g0_lpx, g0_quads; /* G0's uploads and quads since gpu_g0_stats (across contexts) */
static void (*hook_lost)(void), (*hook_changed)(void);
void gpu_set_hooks(void (*lost)(void), void (*changed)(void)) { hook_lost = lost; hook_changed = changed; }
@@ -194,6 +211,8 @@ static void query_render_node(void) {
/* ---- shaders ---- */
+static int g0_setup(void);
+
static const char *vs_src =
"attribute vec2 a_pos; attribute vec2 a_uv; varying vec2 v_uv;\n"
"void main() { v_uv = a_uv; gl_Position = vec4(a_pos, 0.0, 1.0); }\n";
@@ -209,6 +228,38 @@ static const char *fs_ext =
"void main() { vec4 c = texture2D(u_tex, v_uv);\n"
" if (u_opaque > 0.5) c.a = 1.0; gl_FragColor = c * u_op; }\n";
+/* G0: the layer over the copy of what is below, with blit_row's integer arithmetic (canvas.c, k = 256)
+ * in 24-bit floats: every value is an integer below 2^24 and scaling by 2^-8 or 2^-16 is exact, so
+ * floor gives the CPU's result bit for bit (docs/GPU.md, "The dragged window on the GPU"). Texels come
+ * back as c/255 even through 16-bit floats close enough that floor(c·255 + 0.5) is c again; the
+ * output c/255 is converted back to c by the framebuffer as the UI texture's is. The pixel comes from
+ * v_px, a highp varying in pixels (top-down) that vs_g0 passes from the quad's corners, not from
+ * gl_FragCoord, which GLSL ES 1.00 makes mediump: with 16-bit floats it can't tell x or y from x + 1 past
+ * 1024. The self-test's placement part checks it. */
+#define G0_TILE 1024 /* the self-test's arithmetic tiles (G0_TILE²) */
+#define G0_LX 37 /* and where its placement part puts the layer (odd) */
+#define G0_LY 21
+static const char *vs_g0 =
+ "attribute vec2 a_pos; attribute vec2 a_px; varying highp vec2 v_px;\n"
+ "void main() { v_px = a_px; gl_Position = vec4(a_pos, 0.0, 1.0); }\n";
+static const char *fs_g0 =
+ "#ifndef GL_FRAGMENT_PRECISION_HIGH\n#error no highp float\n#endif\n"
+ "precision highp float;\n"
+ "uniform highp sampler2D u_below, u_layer;\n"
+ "varying highp vec2 v_px;\n"
+ "uniform vec2 u_bsize; uniform vec4 u_lrect; uniform float u_swap;\n"
+ "vec4 fetch(highp sampler2D t, vec2 q, vec2 size) {\n"
+ " vec4 c = texture2D(t, (q + 0.5) / size); if (u_swap > 0.5) c = c.bgra; return floor(c * 255.0 + 0.5); }\n"
+ "void main() {\n"
+ " vec2 p = floor(v_px);\n"
+ " vec4 d = fetch(u_below, p, u_bsize), s = fetch(u_layer, p - u_lrect.xy, u_lrect.zw), o = d;\n"
+ " if (s.a > 0.0) {\n"
+ " float ia = 255.0 - s.a;\n"
+ " o.rgb = s.rgb + floor(d.rgb * ia * 0.00390625);\n"
+ " o.a = s.a + floor((d.a * ia + 128.0) * 257.0 * 0.0000152587890625);\n"
+ " }\n"
+ " gl_FragColor = o / 255.0; }\n";
+
static GLuint compile(GLenum type, const char *src) {
GLuint s = glCreateShader(type);
glShaderSource(s, 1, &src, NULL);
@@ -440,7 +491,8 @@ int gpu_init(Kms *k) {
}
G.bgra = has_ext(gl_exts, "GL_EXT_texture_format_BGRA8888");
G.have_external = has_ext(gl_exts, "GL_OES_EGL_image_external");
- if (!has_ext(gl_exts, "GL_EXT_unpack_subimage")) dbg("no GL_EXT_unpack_subimage: the whole canvas is uploaded");
+ G.unpack_sub = has_ext(gl_exts, "GL_EXT_unpack_subimage");
+ if (!G.unpack_sub) dbg("no GL_EXT_unpack_subimage: damage is uploaded as whole rows");
if (make_prog(0, fs_2d) || (G.have_external && make_prog(1, fs_ext))) { fprintf(stderr, "gpu: shaders failed, CPU composition\n"); last_why = "shaders"; goto fail; }
if (has_ext(exts, "EGL_EXT_image_dma_buf_import")) {
G.create_image = (PFNEGLCREATEIMAGEKHRPROC)eglGetProcAddress("eglCreateImageKHR");
@@ -457,6 +509,7 @@ int gpu_init(Kms *k) {
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
G.on = 1;
+ G.g0out = -1;
last_why = "on";
if (G.debug) {
GpuFormat f[64];
@@ -466,6 +519,8 @@ int gpu_init(Kms *k) {
fprintf(stderr, "gpu: GPU composition (%s, EGL %d.%d, dmabuf %s, render %s, display %s)\n", G.renderer, maj, min,
G.create_image ? (G.query_modifiers ? "with modifiers" : "yes") : "no",
G.render_node[0] ? G.render_node : "the card's", k->driver[0] ? k->driver : drv);
+ /* G0's self-test runs here, not in the first drag frame (once per renderer: g0_memo) */
+ G.g0 = g0_setup();
if (hook_changed) hook_changed(); /* windows re-import their buffers in this context */
return 0;
fail:
@@ -744,26 +799,424 @@ static void read_back(int W, int H) {
rb.done = 1;
}
-int gpu_frame(int out, const Canvas *scene, Rect dmg, void (*draw_layer)(int layer)) {
+/* Uploads r of px (sw wide, stride px a row) into the bound texture (texw wide). Without
+ * GL_EXT_unpack_subimage GLES2 reads rows of the upload's own width: whole rows then, in one call when
+ * they are contiguous in px, else one row at a time. Returns the pixels sent. */
+static long upload_rect(const uint32_t *px, int stride, int sw, int texw, Rect r) {
+ if (r.x1 <= r.x0 || r.y1 <= r.y0) return 0;
+ GLenum fmt = G.bgra ? GL_BGRA_EXT : GL_RGBA;
+ if (G.unpack_sub) {
+ glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, stride);
+ glTexSubImage2D(GL_TEXTURE_2D, 0, r.x0, r.y0, r.x1 - r.x0, r.y1 - r.y0, fmt, GL_UNSIGNED_BYTE,
+ px + (size_t)r.y0 * stride + r.x0);
+ glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, 0);
+ return (long)(r.x1 - r.x0) * (r.y1 - r.y0);
+ }
+ int w = sw < texw ? sw : texw;
+ if (stride == w) {
+ glTexSubImage2D(GL_TEXTURE_2D, 0, 0, r.y0, w, r.y1 - r.y0, fmt, GL_UNSIGNED_BYTE, px + (size_t)r.y0 * stride);
+ return (long)w * (r.y1 - r.y0);
+ }
+ for (int y = r.y0; y < r.y1; y++)
+ glTexSubImage2D(GL_TEXTURE_2D, 0, r.x0, y, r.x1 - r.x0, 1, fmt, GL_UNSIGNED_BYTE, px + (size_t)y * stride + r.x0);
+ return (long)(r.x1 - r.x0) * (r.y1 - r.y0);
+}
+
+/* ---- G0: the dragged window on the GPU ---- */
+
+static GLuint g0_texture(void) {
+ GLuint t = 0;
+ glGenTextures(1, &t);
+ glBindTexture(GL_TEXTURE_2D, t);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+ return t;
+}
+
+/* Texture i gets px (w × h, stride w): all of it if its size changed or full, else the n rectangles. */
+static long g0_upload(int i, const uint32_t *px, int w, int h, const Rect *r, int n, int full) {
+ glBindTexture(GL_TEXTURE_2D, G.g0tex[i]);
+ if (full || G.g0tw[i] != w || G.g0th[i] != h) {
+ GLenum fmt = G.bgra ? GL_BGRA_EXT : GL_RGBA;
+ if (G.g0tw[i] != w || G.g0th[i] != h) glTexImage2D(GL_TEXTURE_2D, 0, fmt, w, h, 0, fmt, GL_UNSIGNED_BYTE, px);
+ else glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, fmt, GL_UNSIGNED_BYTE, px);
+ G.g0tw[i] = w;
+ G.g0th[i] = h;
+ return (long)w * h;
+ }
+ long sent = 0;
+ for (int k = 0; k < n; k++) {
+ Rect q = r[k];
+ if (q.x0 < 0) q.x0 = 0;
+ if (q.y0 < 0) q.y0 = 0;
+ if (q.x1 > w) q.x1 = w;
+ if (q.y1 > h) q.y1 = h;
+ sent += upload_rect(px, w, w, w, q);
+ }
+ return sent;
+}
+
+/* The G0 quad: the shader inside w (a framebuffer W × H px), blending and dithering off (it writes the
+ * result). The quad covers the framebuffer and carries each corner's pixel position (top-down) for v_px. */
+static void g0_draw(int W, int H, Rect w, float lx, float ly, float lw, float lh, float bw, float bh) {
+ glUseProgram(G.g0prog);
+ glActiveTexture(GL_TEXTURE1);
+ glBindTexture(GL_TEXTURE_2D, G.g0tex[1]);
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, G.g0tex[0]);
+ glUniform1i(G.gu_below, 0);
+ glUniform1i(G.gu_layer, 1);
+ glUniform2f(G.gu_bsize, bw, bh);
+ glUniform4f(G.gu_lrect, lx, ly, lw, lh);
+ glUniform1f(G.gu_swap, G.bgra ? 0.0f : 1.0f);
+ glDisable(GL_BLEND);
+ glDisable(GL_DITHER);
+ glEnable(GL_SCISSOR_TEST);
+ glScissor(w.x0, H - w.y1, w.x1 - w.x0, w.y1 - w.y0);
+ const GLfloat fw = (GLfloat)W, fh = (GLfloat)H;
+ const GLfloat v[] = { -1, -1, 0, fh, 1, -1, fw, fh, -1, 1, 0, 0, 1, 1, fw, 0 };
+ glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 16, v);
+ glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 16, v + 2);
+ glEnableVertexAttribArray(0);
+ glEnableVertexAttribArray(1);
+ glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
+ glDisable(GL_SCISSOR_TEST);
+ glEnable(GL_DITHER);
+ glEnable(GL_BLEND);
+}
+/* The self-test's cases, in order: every valid premultiplied (alpha a, channel c ≤ a) with every
+ * destination channel d (three cases a pixel, one per colour channel, the destination alpha varying),
+ * then every (source alpha, destination alpha), then random valid pixels to fill the last tile. */
+typedef struct { int a, j, grid; uint32_t p, rng; } G0Gen;
+static int g0_gen_done(const G0Gen *g) { return g->a > 255 && g->grid >= 65536; }
+static void g0_gen(G0Gen *g, uint32_t *s, uint32_t *d) {
+ g->p++;
+ g->rng ^= g->rng << 13; g->rng ^= g->rng >> 17; g->rng ^= g->rng << 5;
+ if (g->a <= 255) {
+ uint32_t a = (uint32_t)g->a, n = (a + 1) * 256, sc = 0, dc = 0;
+ for (int k = 0; k < 3; k++) {
+ uint32_t j = (uint32_t)g->j + (uint32_t)k < n ? (uint32_t)g->j + (uint32_t)k : n - 1;
+ sc = sc << 8 | j >> 8;
+ dc = dc << 8 | (j & 255);
+ }
+ *s = a << 24 | sc;
+ *d = ((g->p * 151u + 7u) & 255u) << 24 | dc;
+ g->j += 3;
+ if ((uint32_t)g->j >= n) { g->a++; g->j = 0; }
+ return;
+ }
+ if (g->grid < 65536) {
+ uint32_t sa = (uint32_t)g->grid & 255, da = (uint32_t)g->grid >> 8, c = sa ? g->rng % (sa + 1) : 0;
+ *s = sa << 24 | c << 16 | (sa - c) << 8 | c / 2;
+ *d = da << 24 | (g->rng >> 8 & 0xffffff);
+ g->grid++;
+ return;
+ }
+ uint32_t a = g->rng >> 24, r = g->rng & 0xffffff;
+ *s = a << 24 | (a ? (r % (a + 1)) << 16 | ((r >> 8) % (a + 1)) << 8 | (r >> 16) % (a + 1) : 0);
+ *d = g->rng * 2654435761u;
+}
+
+/* One self-test pass in the bound fw × fh framebuffer: the layer lay (lw × lh) at (lx, ly)
+ * over bel (fw × fh), drawn where the layer is and compared with canvas_blit on the CPU into ref; the rest
+ * of the framebuffer must stay as cleared (0). rd (fw × fh × 4 bytes) may be bel's memory: it is read
+ * back after the upload. -1 on a GL error, else the pixels that differ (the first few logged). */
+static long g0_pass(int fw, int fh, const uint32_t *bel, const uint32_t *lay, int lx, int ly, int lw,
+ int lh, uint32_t *ref, uint8_t *rd, const char *what) {
+ memcpy(ref, bel, (size_t)fw * fh * 4);
+ Canvas c;
+ canvas_init(&c, ref, fw, fh, fw);
+ canvas_blit(&c, lay, lw, lh, lx, ly, 0, 1, 0);
+ Rect w = { lx > 0 ? lx : 0, ly > 0 ? ly : 0, lx + lw < fw ? lx + lw : fw, ly + lh < fh ? ly + lh : fh };
+ for (int y = 0; y < fh; y++)
+ for (int x = 0; x < fw; x++)
+ if (x < w.x0 || x >= w.x1 || y < w.y0 || y >= w.y1) ref[(size_t)y * fw + x] = 0;
+ g0_upload(0, bel, fw, fh, NULL, 0, 1);
+ g0_upload(1, lay, lw, lh, NULL, 0, 1);
+ glViewport(0, 0, fw, fh);
+ glClearColor(0, 0, 0, 0);
+ glClear(GL_COLOR_BUFFER_BIT);
+ g0_draw(fw, fh, w, (float)lx, (float)ly, (float)lw, (float)lh, (float)fw, (float)fh);
+ glPixelStorei(GL_PACK_ALIGNMENT, 4);
+ glReadPixels(0, 0, fw, fh, GL_RGBA, GL_UNSIGNED_BYTE, rd);
+ if (glGetError() != GL_NO_ERROR) return -1;
+ long differ = 0;
+ for (int y = 0; y < fh; y++) { /* GL's rows are bottom to top */
+ const uint8_t *b = rd + (size_t)(fh - 1 - y) * fw * 4;
+ const uint32_t *e = ref + (size_t)y * fw;
+ for (int x = 0; x < fw; x++) {
+ uint32_t v = (uint32_t)b[4 * x + 3] << 24 | (uint32_t)b[4 * x] << 16 | (uint32_t)b[4 * x + 1] << 8 | b[4 * x + 2];
+ if (v != e[x] && differ++ < 4)
+ fprintf(stderr, "gpu: G0 self-test (%s): pixel (%d, %d): CPU %08x, GPU %08x\n", what, x, y, e[x], v);
+ }
+ }
+ return differ;
+}
+
+/* The self-test, two parts. Arithmetic: every case of g0_gen in 1024 × 1024 tiles (the layer over the
+ * copy, pixel for pixel). Placement: an fw × fh framebuffer (the output's size, at least 4096 × 2048 when
+ * the GPU allows it) and a copy of that size, with the layer at an odd offset reaching its far edges,
+ * every pixel a different random value: a texel read from the wrong place (gl_FragCoord or a varying
+ * with too few bits, the far rows and columns) shows. 0 if every pixel is the CPU's. */
+static int g0_selftest(int fw, int fh, long *cases, long *differ) {
+ enum { T = G0_TILE, LX = G0_LX, LY = G0_LY };
+ *cases = *differ = 0;
+ size_t n = (size_t)fw * fh > (size_t)T * T ? (size_t)fw * fh : (size_t)T * T;
+ uint32_t *s = malloc(n * 4), *d = malloc(n * 4), *ref = malloc(n * 4);
+ GLuint ft = 0, fbo = 0;
+ for (int i = 0; i < 16 && glGetError() != GL_NO_ERROR; i++) {} /* only the test's own errors count */
+ int ok = s && d && ref;
+ if (ok) {
+ glGenTextures(1, &ft);
+ glBindTexture(GL_TEXTURE_2D, ft);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, T, T, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
+ glGenFramebuffers(1, &fbo);
+ glBindFramebuffer(GL_FRAMEBUFFER, fbo);
+ glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, ft, 0);
+ ok = glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE;
+ }
+ G0Gen g = { 0, 0, 0, 0, 2463534242u };
+ while (ok && !g0_gen_done(&g)) {
+ for (size_t i = 0; i < (size_t)T * T; i++) g0_gen(&g, &s[i], &d[i]);
+ long k = g0_pass(T, T, d, s, 0, 0, T, T, ref, (uint8_t *)d, "arithmetic");
+ if (k < 0) { ok = 0; break; }
+ *differ += k;
+ *cases += (long)T * T;
+ }
+ if (ok) { /* placement */
+ glBindTexture(GL_TEXTURE_2D, ft);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, fw, fh, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
+ ok = glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE;
+ }
+ if (ok) {
+ uint32_t r = 88172645u;
+ for (size_t i = 0; i < (size_t)fw * fh; i++) {
+ r ^= r << 13; r ^= r >> 17; r ^= r << 5;
+ d[i] = r * 2654435761u;
+ uint32_t a = r >> 24;
+ s[i] = a << 24 | (a ? (r % (a + 1)) << 16 | ((r >> 8) % (a + 1)) << 8 | (r >> 16) % (a + 1) : 0);
+ }
+ long k = g0_pass(fw, fh, d, s, LX, LY, fw - LX, fh - LY, ref, (uint8_t *)d, "placement");
+ if (k < 0) ok = 0;
+ else {
+ *differ += k;
+ *cases += (long)(fw - LX) * (fh - LY);
+ }
+ }
+ glBindFramebuffer(GL_FRAMEBUFFER, 0);
+ if (fbo) glDeleteFramebuffers(1, &fbo);
+ if (ft) glDeleteTextures(1, &ft);
+ G.g0tw[0] = G.g0tw[1] = G.g0th[0] = G.g0th[1] = 0; /* hold nothing of an output */
+ glDeleteTextures(2, G.g0tex);
+ G.g0tex[0] = g0_texture();
+ G.g0tex[1] = g0_texture();
+ free(s); free(d); free(ref);
+ return ok && !*differ ? 0 : -1;
+}
+
+/* The self-test's outcome per GL renderer and version, kept across contexts (gpu_shutdown wipes G): a
+ * GPU restart, or a later context on the same driver, does not run it again. */
+static struct { char key[320]; int pass, w, h; } g0_memo;
+
+static void g0_teardown(void) {
+ if (G.g0prog) glDeleteProgram(G.g0prog);
+ glDeleteTextures(2, G.g0tex);
+ G.g0prog = G.g0tex[0] = G.g0tex[1] = 0;
+}
+
+/* G0's program and textures in this context, and the self-test (or its earlier outcome). 1 = on. */
+static int g0_setup(void) {
+ const char *e = getenv("LORD_G0");
+ if (e && !strcmp(e, "0")) { fprintf(stderr, "gpu: G0 off (LORD_G0=0): the dragged window is composed by the CPU\n"); return 0; }
+ GOut *o = G.cur && G.cur->used ? G.cur : &G.out[0];
+ if (!o->used || !eglMakeCurrent(G.dpy, o->surf, o->surf, G.ctx)) return 0;
+ GLint range[2] = { 0, 0 }, prec = 0;
+ glGetShaderPrecisionFormat(GL_FRAGMENT_SHADER, GL_HIGH_FLOAT, range, &prec);
+ if (prec < 23 || range[1] < 24) {
+ fprintf(stderr, "gpu: G0 off: the fragment shader's floats have %d bits (24 needed)\n", (int)prec + 1);
+ return 0;
+ }
+ GLuint v = compile(GL_VERTEX_SHADER, vs_g0), f = compile(GL_FRAGMENT_SHADER, fs_g0);
+ GLint linked = 0;
+ if (v && f) {
+ G.g0prog = glCreateProgram();
+ glAttachShader(G.g0prog, v);
+ glAttachShader(G.g0prog, f);
+ glBindAttribLocation(G.g0prog, 0, "a_pos");
+ glBindAttribLocation(G.g0prog, 1, "a_px");
+ glLinkProgram(G.g0prog);
+ glGetProgramiv(G.g0prog, GL_LINK_STATUS, &linked);
+ }
+ if (v) glDeleteShader(v);
+ if (f) glDeleteShader(f);
+ if (!linked) { g0_teardown(); fprintf(stderr, "gpu: G0 off: its shader did not build\n"); return 0; }
+ G.gu_below = glGetUniformLocation(G.g0prog, "u_below");
+ G.gu_layer = glGetUniformLocation(G.g0prog, "u_layer");
+ G.gu_bsize = glGetUniformLocation(G.g0prog, "u_bsize");
+ G.gu_lrect = glGetUniformLocation(G.g0prog, "u_lrect");
+ G.gu_swap = glGetUniformLocation(G.g0prog, "u_swap");
+ G.g0tex[0] = g0_texture();
+ G.g0tex[1] = g0_texture();
+ G.g0out = -1;
+ /* the placement part's size: the largest output now, at least 4096 × 2048, within the GPU's limits */
+ GLint maxtex = 0, vp[2] = { 0, 0 };
+ glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxtex);
+ glGetIntegerv(GL_MAX_VIEWPORT_DIMS, vp);
+ int fw = 4096, fh = 2048;
+ for (int i = 0; i < OUT_MAX; i++)
+ if (G.out[i].used) {
+ if (G.out[i].W > fw) fw = G.out[i].W;
+ if (G.out[i].H > fh) fh = G.out[i].H;
+ }
+ int lw = maxtex < vp[0] ? maxtex : vp[0], lh = maxtex < vp[1] ? maxtex : vp[1];
+ if (fw > lw) fw = lw;
+ if (fh > lh) fh = lh;
+ const char *ver = (const char *)glGetString(GL_VERSION);
+ char key[sizeof g0_memo.key];
+ snprintf(key, sizeof key, "%s|%s", G.renderer, ver ? ver : "?");
+ if (!strcmp(key, g0_memo.key) && (!g0_memo.pass || (g0_memo.w >= fw && g0_memo.h >= fh))) {
+ if (!g0_memo.pass) {
+ g0_teardown();
+ fprintf(stderr, "gpu: G0 off: its self-test failed earlier with this renderer\n");
+ return 0;
+ }
+ G.g0lw = g0_memo.w;
+ G.g0lh = g0_memo.h;
+ dbg("G0 on: its self-test passed earlier with this renderer (%dx%d)", G.g0lw, G.g0lh);
+ return 1;
+ }
+ if (fw < 4096 || fh < 2048)
+ fprintf(stderr, "gpu: G0 self-test: placement in %dx%d, the largest framebuffer the GPU allows\n", fw, fh);
+ long cases = 0, differ = 0;
+ struct timespec t0, t1;
+ clock_gettime(CLOCK_MONOTONIC, &t0);
+ int bad = fw < G0_TILE || fh < G0_TILE ? -1 : g0_selftest(fw, fh, &cases, &differ);
+ clock_gettime(CLOCK_MONOTONIC, &t1);
+ double ms = (t1.tv_sec - t0.tv_sec) * 1e3 + (t1.tv_nsec - t0.tv_nsec) / 1e6;
+ if (bad && !differ) { /* no pixel differs: it did not run to the end */
+ g0_teardown();
+ fprintf(stderr, "gpu: G0 off: its self-test could not run (texture size, memory or framebuffer)\n");
+ return 0;
+ }
+ snprintf(g0_memo.key, sizeof g0_memo.key, "%s", key);
+ g0_memo.pass = !bad;
+ g0_memo.w = fw;
+ g0_memo.h = fh;
+ if (bad) {
+ g0_teardown();
+ fprintf(stderr, "gpu: G0 off: the self-test failed (%ld of %ld pixels differ from the CPU's, %.0f ms)\n", differ, cases, ms);
+ return 0;
+ }
+ G.g0lw = fw;
+ G.g0lh = fh;
+ fprintf(stderr, "gpu: G0 on: the dragged window is composed on the GPU (self-test: %ld pixels, every valid "
+ "premultiplied combination and a %dx%d placement pass, the same as the CPU's, %.0f ms)\n", cases, fw, fh, ms);
+ return 1;
+}
+
+int gpu_g0_ready(int out) {
+ return G.on && G.g0 == 1 && out >= 0 && out < OUT_MAX && G.out[out].used && G.out[out].W <= G.g0lw &&
+ G.out[out].H <= G.g0lh;
+}
+
+int gpu_g0_holds(int out) { return G.on && G.g0 == 1 && G.g0out == out && out >= 0; }
+
+void gpu_g0_stats(long *below_px, long *layer_px, long *quads) {
+ *below_px = g0_bpx;
+ *layer_px = g0_lpx;
+ *quads = g0_quads;
+ g0_bpx = g0_lpx = g0_quads = 0;
+}
+
+/* LORD_GPU_VERIFY: what the quad wrote in w, read back, against the CPU's pixels (RGB: the
+ * framebuffer has no alpha; a frame painted by the CPU shows "premultiplied over black", its RGB). */
+static void g0_verify(GOut *o, const GpuG0 *g) {
+ int ww = g->w.x1 - g->w.x0, wh = g->w.y1 - g->w.y0;
+ uint8_t *rd = malloc((size_t)ww * wh * 4);
+ if (!rd) return;
+ glPixelStorei(GL_PACK_ALIGNMENT, 4);
+ glReadPixels(g->w.x0, o->H - g->w.y1, ww, wh, GL_RGBA, GL_UNSIGNED_BYTE, rd);
+ long differ = 0;
+ int worst = 0;
+ if (glGetError() == GL_NO_ERROR)
+ for (int y = 0; y < wh; y++) {
+ const uint8_t *b = rd + (size_t)(wh - 1 - y) * ww * 4;
+ const uint32_t *e = g->ref + (size_t)y * ww;
+ for (int x = 0; x < ww; x++) {
+ int dr = abs((int)b[4 * x] - (int)(e[x] >> 16 & 255)), dg = abs((int)b[4 * x + 1] - (int)(e[x] >> 8 & 255));
+ int db = abs((int)b[4 * x + 2] - (int)(e[x] & 255)), dm = dr > dg ? dr : dg;
+ if (db > dm) dm = db;
+ if (dm) differ++;
+ if (dm > worst) worst = dm;
+ }
+ }
+ else differ = -1;
+ fprintf(stderr, "gpu-verify: frame %ld W=%dx%d differ=%ld worst=%d\n", g->frame, ww, wh, differ, worst);
+ free(rd);
+}
+
+/* G0 off for this context, for a reason that would come back every frame */
+static int g0_off(const char *why) {
+ G.g0out = -1;
+ G.g0 = 0;
+ fprintf(stderr, "gpu: G0 off: %s\n", why);
+ return 0;
+}
+
+/* The G0 part of a frame: the textures brought up to date, then the quad. 0 if it was not drawn. */
+static int g0_frame(GOut *o, int out, const GpuG0 *g) {
+ if (G.g0 != 1) return 0;
+ int sane = g->w.x0 >= 0 && g->w.y0 >= 0 && g->w.x1 <= o->W && g->w.y1 <= o->H && g->w.x1 > g->w.x0 && g->w.y1 > g->w.y0;
+ /* coordinates past what the self-test's placement covered are not trusted */
+ int tested = o->W <= G.g0lw && o->H <= G.g0lh && g->lw <= G.g0lw - G0_LX && g->lh <= G.g0lh - G0_LY &&
+ g->bw <= G.g0lw && g->bh <= G.g0lh;
+ int other = G.g0out != out; /* another output's (or nothing yet): all of both */
+ if (g->lpx && g->bpx) {
+ if (!sane) return g0_off("a window rectangle outside the output");
+ if (!tested) return g0_off("a layer or output larger than its self-test covered");
+ for (int i = 0; i < 16 && glGetError() != GL_NO_ERROR; i++) {}
+ g0_bpx += g0_upload(0, g->bpx, g->bw, g->bh, g->bdirty, g->nb, g->bfull || other);
+ g0_lpx += g0_upload(1, g->lpx, g->lw, g->lh, g->ldirty, g->nl, g->lfull || other);
+ if (glGetError() != GL_NO_ERROR) return g0_off("an upload failed");
+ G.g0out = out;
+ } else if (!sane || !tested || other || G.g0tw[1] != g->lw || G.g0th[1] != g->lh || G.g0tw[0] != g->bw ||
+ G.g0th[0] != g->bh)
+ return 0;
+ g0_draw(o->W, o->H, g->w, (float)g->lx, (float)g->ly, (float)g->lw, (float)g->lh, (float)g->bw, (float)g->bh);
+ g0_quads++;
+ if (g->ref) g0_verify(o, g);
+ return 1;
+}
+
+int gpu_frame_ex(int out, const Canvas *scene, const Rect *dmg, int n, const GpuG0 *g0, int *g0_drawn,
+ void (*draw_layer)(int layer)) {
+ if (g0_drawn) *g0_drawn = 0;
if (!G.on || out < 0 || out >= OUT_MAX || !G.out[out].used) return -1;
GOut *o = &G.out[out];
if (!eglMakeCurrent(G.dpy, o->surf, o->surf, G.ctx)) { fprintf(stderr, "gpu: eglMakeCurrent failed\n"); return -1; }
G.cur = o;
glViewport(0, 0, o->W, o->H);
/* what changed in the UI */
- if (dmg.x1 > dmg.x0 && dmg.y1 > dmg.y0) {
- glBindTexture(GL_TEXTURE_2D, o->canvas.id);
- GLenum fmt = G.bgra ? GL_BGRA_EXT : GL_RGBA;
- glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, scene->stride);
- glTexSubImage2D(GL_TEXTURE_2D, 0, dmg.x0, dmg.y0, dmg.x1 - dmg.x0, dmg.y1 - dmg.y0, fmt, GL_UNSIGNED_BYTE,
- scene->px + (size_t)dmg.y0 * scene->stride + dmg.x0);
- glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, 0);
- }
+ glBindTexture(GL_TEXTURE_2D, o->canvas.id);
+ for (int i = 0; i < n; i++) upload_rect(scene->px, scene->stride, scene->w, o->W, dmg[i]);
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
Rect all = { 0, 0, o->W, o->H };
if (draw_layer) draw_layer(0);
gpu_draw(&o->canvas, 0, 0, (float)o->W, (float)o->H, 0, 0, (float)o->W, (float)o->H, all, 1);
+ /* the dragged window over the UI's stale rectangle: nothing of layer 0 is under it (paint.c checks).
+ * Not drawn, that rectangle is stale in the UI texture: nothing is shown (-2), the caller paints it */
+ if (g0) {
+ if (!g0_frame(o, out, g0)) {
+ rb.px = NULL;
+ return -2;
+ }
+ if (g0_drawn) *g0_drawn = 1;
+ }
if (draw_layer) draw_layer(1);
if (rb.px) {
if (rb.w == o->W && rb.h == o->H) read_back(o->W, o->H);
@@ -777,6 +1230,10 @@ int gpu_frame(int out, const Canvas *scene, Rect dmg, void (*draw_layer)(int lay
G.frames++;
return present(o, (Shown){ .kind = 1, .bo = bo, .fb = fb });
}
+
+int gpu_frame(int out, const Canvas *scene, Rect dmg, void (*draw_layer)(int layer)) {
+ return gpu_frame_ex(out, scene, &dmg, 1, NULL, NULL, draw_layer);
+}
/* ---- direct scanout ---- */
int gpu_can_scanout(const GpuDmabuf *d, int w, int h) {
diff --git a/server/src/gpu/gpu.h b/server/src/gpu/gpu.h
index 063c206..d7f886a 100644
--- a/server/src/gpu/gpu.h
+++ b/server/src/gpu/gpu.h
@@ -66,6 +66,41 @@ int gpu_tex_read(const GpuTex *t, uint32_t *px, int w, int h);
* draw_layer(0) = app windows (below the UI), draw_layer(1) = app menus and
* fullscreen (on top). */
int gpu_frame(int out, const Canvas *scene, Rect damage, void (*draw_layer)(int layer));
+
+/* G0: the dragged window composited on the GPU with the CPU's own arithmetic (docs/GPU.md, "The
+ * dragged window on the GPU"). paint_root_g0 (ui/paint.c) leaves the window's rectangle w out of the
+ * scene and fills this; one shader then writes, inside w, the layer (lpx) over the copy of what is
+ * below (bpx) exactly as canvas_blit's blit_row does. The two are textures of their own, uploaded
+ * only where they changed (ldirty, bdirty; *full: all of it). */
+#define G0_DIRTY 8
+typedef struct GpuG0 {
+ Rect w; /* where the shader writes (the output's physical px, top-down) */
+ const uint32_t *lpx; /* the layer: lw × lh, its pixel (0, 0) at (lx, ly) */
+ int lw, lh, lx, ly;
+ const uint32_t *bpx; /* the copy of what is below: bw × bh, the scene's size */
+ int bw, bh;
+ Rect ldirty[G0_DIRTY], bdirty[G0_DIRTY]; /* in the layer's and the copy's pixels */
+ int nl, nb, lfull, bfull;
+ Rect strip[4]; /* what the CPU painted around w this frame (uploaded to the UI texture) */
+ int nstrip;
+ const uint32_t *ref; /* LORD_GPU_VERIFY: the CPU's pixels for w (w's width a row), or NULL */
+ long frame; /* its number, for the log */
+} GpuG0;
+/* 1 if G0 may be used on that output: the shader has 24-bit floats, LORD_G0 is not 0, the self-test
+ * (every valid premultiplied combination, and a placement pass at least as large as the output, drawn
+ * into a framebuffer and read back) gave the CPU's pixels. gpu_init runs that test, once per GL renderer
+ * and version. */
+int gpu_g0_ready(int out);
+/* gpu_frame with several UI rectangles to upload and, with g0, the window composited over w (g0
+ * without dirty rectangles redraws the last one: a frame with no UI damage while the scene's w is
+ * stale). Returns -1 like gpu_frame; *g0_drawn says whether the G0 quad was drawn. -2: g0 was given but
+ * its quad could not be drawn (G0 turned off, or its textures are not that output's): nothing was
+ * shown, and the caller paints w on the CPU in its next frame. */
+int gpu_frame_ex(int out, const Canvas *scene, const Rect *dmg, int n, const GpuG0 *g0, int *g0_drawn,
+ void (*draw_layer)(int layer));
+int gpu_g0_holds(int out); /* the G0 textures hold that output's last upload, in this context */
+/* uploaded pixels of the copy of what is below and of the layer, and G0 quads drawn, since the last call */
+void gpu_g0_stats(long *below_px, long *layer_px, long *quads);
/* Another display (docs/DISPLAYS.md): its own GBM/EGL surface on the same context; gpu_init made
* output 0. Returns the output index for gpu_frame, or -1 (then that display goes by the CPU). */
int gpu_add_output(Kms *k);
diff --git a/server/src/main.c b/server/src/main.c
index 0557c96..2452e21 100644
--- a/server/src/main.c
+++ b/server/src/main.c
@@ -108,6 +108,20 @@ static void below_stats_text(char *buf, size_t len) {
b.refused[BR_BLUR], b.refused[BR_TRANSFORM], b.refused[BR_UNREACHED], b.refused[BR_COST]);
}
+/* G0, the dragged window composited on the GPU (docs/GPU.md): frames, the strips the CPU painted
+ * around it, what was uploaded of the copy of what is below and of the layer, and why other frames that
+ * used the copy were painted by the CPU. Appended to the "drag: …" lines. */
+static void g0_stats_text(char *buf, size_t len) {
+ G0Stats g;
+ long bpx, lpx, quads;
+ paint_g0_stats(&g);
+ gpu_g0_stats(&bpx, &lpx, &quads);
+ snprintf(buf, len, "G0: %ld of %ld frames (%ld quads), strips %.2f Mpx, uploaded below %.2f Mpx, layer %.2f Mpx "
+ "(not: %ld layer, %ld clip, %ld ancestor, %ld app, %ld after, %ld post)",
+ g.frames, g.tried, quads, g.strip_px / 1e6, bpx / 1e6, lpx / 1e6, g.refused[G0R_LAYER], g.refused[G0R_CLIP],
+ g.refused[G0R_ANCESTOR], g.refused[G0R_APP], g.refused[G0R_AFTER], g.refused[G0R_POST]);
+}
+
/* One display (docs/DISPLAYS.md): its KMS head, where it sits in the global logical space, and the
* frame the UI paints for it at its own resolution. out[0]'s head owns the card's fd; the main display
* (menu bar, Dock) is s->main_out. Without a monitor (headless) out[0] is a frame in memory. */
@@ -134,6 +148,13 @@ typedef struct {
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 */
+ /* G0 (paint_root_g0, docs/GPU.md): this frame left the dragged window's rectangle to the GPU
+ * (g0taken, g0); the last frame shown has it there, so the scene and the UI texture are stale in g0w
+ * (g0on) until a frame paints it on the CPU; g0cpu: the GPU could not draw it, the next frame paints
+ * it on the CPU without trying G0 */
+ int g0taken, g0on, g0cpu;
+ Rect g0w;
+ GpuG0 g0;
} Out;
typedef struct {
@@ -182,6 +203,15 @@ static Box box_join(Box a, Box b) {
}
static int box_empty(Box b) { return b.x1 <= b.x0 || b.y1 <= b.y0; }
+/* The scene's G0 rectangle (still the GPU's) is painted again on the CPU in the next frame of the display */
+static void g0_stale(Out *o) {
+ float x0, y0, x1, y1;
+ out_logical_box(&o->v, o->g0w, &x0, &y0, &x1, &y1);
+ if (box_empty(o->pend)) o->pl.n = 0;
+ o->pend = box_join(o->pend, (Box){ x0, y0, x1, y1 });
+ o->g0on = 0;
+}
+
/* ---- the displays ---- */
static int out_count(const Server *s) {
@@ -395,14 +425,15 @@ static void dispatch_pointer(Server *s) {
double span = dstat.last - dstat.t0;
long lr = 0, lb = 0, lp = paint_layer_parts();
paint_layer_stats(&lr, &lb);
- char bs[192];
+ char bs[192], gs[256];
below_stats_text(bs, sizeof bs);
+ g0_stats_text(gs, sizeof gs);
fprintf(stderr, "drag: %ld frames in %.0f ms = %.1f FPS (%ld moves); per frame: work %.2f ms "
"(max %.2f; JS %.2f, layout %.2f, paint %.2f, present %.2f), max gap %.1f ms; "
- "layer: %ld painted (%ld of them a part), %ld copied (%s); %s\n",
+ "layer: %ld painted (%ld of them a part), %ld copied (%s); %s; %s\n",
dstat.frames, span, 1000.0 * dstat.frames / span, dstat.moves, dstat.work / dstat.frames, dstat.work_max,
dstat.js / dstat.frames, dstat.layout / dstat.frames, (dstat.paint - dstat.layout) / dstat.frames,
- dstat.present / dstat.frames, dstat.gap_max, lr, lp, lb, gpu_active() ? "GPU" : "CPU", bs);
+ dstat.present / dstat.frames, dstat.gap_max, lr, lp, lb, gpu_active() ? "GPU" : "CPU", bs, gs);
}
dstat = (DragStats){ 0 };
}
@@ -583,6 +614,8 @@ static void cpu_takeover(Server *s) {
o->gpu = -1;
o->full_copy = 2;
o->wl_new = 0;
+ if (o->g0on) o->full = 1; /* the scene's G0 rectangle is stale */
+ o->g0on = o->g0taken = 0;
}
s->hw_cursor = 0;
}
@@ -1031,8 +1064,10 @@ static Box paint_ui(Server *s) {
* (paint_root_full: no drag layer, no copy of what is below it, no skipped node) and the pixels that
* differ from the display's frame are counted on stderr. Off by default; it only reads the frame. The
* terminals and Linux apps note where they were painted, so with N = 1 a terminal's change during a
- * drag always repaints the whole layer: use N > 1 to check the layer's partial repaints. */
-static void paint_verify(Out *o, int index) {
+ * drag always repaints the whole layer: use N > 1 to check the layer's partial repaints. On a frame
+ * whose dragged window the GPU composites (G0, skip) the scene is stale there and skip is left out:
+ * LORD_GPU_VERIFY checks those pixels against the CPU's. */
+static void paint_verify(Out *o, int index, Rect skip) {
static int every = -1;
static long frames[OUT_MAX];
static uint32_t *px;
@@ -1063,7 +1098,7 @@ static void paint_verify(Out *o, int index) {
for (int y = 0; y < o->scene.h; y++) {
const uint32_t *a = o->scene.px + (size_t)y * o->scene.stride, *b = px + (size_t)y * o->scene.w;
for (int x = 0; x < o->scene.w; x++) {
- if (a[x] == b[x]) continue;
+ if (a[x] == b[x] || (x >= skip.x0 && x < skip.x1 && y >= skip.y0 && y < skip.y1)) continue;
diff++;
for (int sh = 0; sh < 32; sh += 8) {
int d = abs((int)((a[x] >> sh) & 255) - (int)((b[x] >> sh) & 255));
@@ -1071,8 +1106,9 @@ static void paint_verify(Out *o, int index) {
}
}
}
- fprintf(stderr, "paint-verify: display %d frame %ld: %ld pixels differ from a full repaint (worst %d)%s\n",
- index, frames[index], diff, worst, paint_layer_node() ? ", drag layer" : "");
+ fprintf(stderr, "paint-verify: display %d frame %ld: %ld pixels differ from a full repaint (worst %d)%s%s\n",
+ index, frames[index], diff, worst, paint_layer_node() ? ", drag layer" : "",
+ skip.x1 > skip.x0 ? ", the GPU's window rectangle left out" : "");
}
/* Paints a display's pending damage into its frame at its scale. Returns the physical rect painted. */
@@ -1084,7 +1120,14 @@ static Rect paint_out(Server *s, Out *o, int index, int feed) {
int whole = o->full;
o->full = 0;
o->pend = (Box){ 0, 0, 0, 0 };
+ o->g0taken = 0;
if (box_empty(b) || !o->scene.px) return r;
+ /* G0: the rectangle the GPU composed last frame is stale in the scene: painted again with this damage */
+ if (o->g0on && !whole) {
+ float gx0, gy0, gx1, gy1;
+ out_logical_box(&o->v, o->g0w, &gx0, &gy0, &gx1, &gy1);
+ b = box_join(b, (Box){ gx0, gy0, gx1, gy1 });
+ }
/* 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 };
@@ -1102,6 +1145,10 @@ static Rect paint_out(Server *s, Out *o, int index, int feed) {
}
multi = !whole && !gpu_active() && l.n > 1;
int nparts = multi ? l.n : 1;
+ /* G0 (paint_root_g0): one box on a GPU display while a window is dragged */
+ int g0 = !whole && nparts == 1 && index < OUT_MAX && o->gpu >= 0 && gpu_active() && !(o->d.rot & 3) &&
+ !wl_fullscreen_active() && !o->g0cpu && paint_layer_node() && gpu_g0_ready(o->gpu);
+ o->g0cpu = 0;
Box parts[DAMAGE_MAX];
if (multi) memcpy(parts, l.r, sizeof(Box) * (size_t)nparts);
else parts[0] = b;
@@ -1117,9 +1164,18 @@ static Rect paint_out(Server *s, Out *o, int index, int feed) {
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);
- paint_root_clear(&o->scene, ui.root, 0x000000);
+ if (g0) o->g0taken = paint_root_g0(&o->scene, ui.root, &o->g0);
+ else paint_root_clear(&o->scene, ui.root, 0x000000);
if (feed) media_scene_painted(&o->scene); /* recording/remote desktop with protected windows */
r = rect_join(r, pr);
+ if (o->g0taken) { /* only the strips around the window were painted */
+ for (int i = 0; i < o->g0.nstrip; i++) {
+ Rect q = o->g0.strip[i];
+ o->painted[o->npainted++] = (Box){ (float)q.x0, (float)q.y0, (float)q.x1, (float)q.y1 };
+ px += (double)(q.x1 - q.x0) * (q.y1 - q.y0);
+ }
+ continue;
+ }
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);
}
@@ -1134,7 +1190,7 @@ static Rect paint_out(Server *s, Out *o, int index, int feed) {
if (s->debug || paint_profile)
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");
- paint_verify(o, index);
+ paint_verify(o, index, o->g0taken ? o->g0.w : (Rect){ 0, 0, 0, 0 });
return r;
}
@@ -1460,6 +1516,9 @@ static void render_frame(Server *s) {
Out *o = &s->out[i];
/* a fullscreen app's new frame (render runs when something wants a frame) goes to every display */
if (!o->on || o->kms.flip_pending || !(out_needs(s, o) || wl_fullscreen_active())) continue;
+ /* G0 with no UI damage: the GPU draws the window again from its textures, if they are still
+ * this display's; otherwise its rectangle is painted by the CPU now */
+ if (o->g0on && box_empty(o->pend) && !o->full && !gpu_g0_holds(o->gpu)) g0_stale(o);
Rect r = paint_out(s, o, i, o == feed);
if (gpu_active() && o->gpu >= 0) { /* (the loop above skips what has nothing new) */
update_cursor(s);
@@ -1478,8 +1537,43 @@ static void render_frame(Server *s) {
}
/* compatible fullscreen (one display): straight to the card; otherwise composed (always
* composed while the remote desktop reads it back) */
- int failed = !(!readback && out_count(s) == 1 && wl_try_scanout(s->hw_cursor || s->cursor_mode == 2)) && gpu_frame(o->gpu, &o->scene, r, gpu_layer);
- if (readback && !failed && gpu_read_done()) {
+ /* G0: the strips around the window, and the window from its textures (gpu_frame_ex); a frame
+ * with nothing painted while the last one had it draws it again (its rectangle is stale) */
+ Rect up[4] = { r };
+ int nup = 1, g0drawn = 0;
+ GpuG0 again;
+ const GpuG0 *g0 = NULL;
+ if (o->g0taken) {
+ g0 = &o->g0;
+ nup = o->g0.nstrip;
+ memcpy(up, o->g0.strip, sizeof up);
+ } else if (o->g0on && r.x1 <= r.x0) {
+ again = o->g0;
+ again.lpx = again.bpx = NULL;
+ again.nl = again.nb = again.lfull = again.bfull = 0;
+ again.ref = NULL;
+ g0 = &again;
+ }
+ int scanned = !readback && out_count(s) == 1 && wl_try_scanout(s->hw_cursor || s->cursor_mode == 2);
+ int res = scanned ? 0 : gpu_frame_ex(o->gpu, &o->scene, up, nup, g0, &g0drawn, gpu_layer);
+ int failed = res == -1, unshown = res == -2;
+ if (o->g0taken && g0drawn) paint_g0_done(); /* the textures have what paint.c had */
+ if (unshown) {
+ /* the quad could not be drawn: nothing was shown (the display keeps the frame before,
+ * which was right); W joins the damage and the next frame paints it on the CPU */
+ o->g0w = g0->w;
+ g0_stale(o);
+ o->g0cpu = 1;
+ } else if (g0 && !g0drawn && !failed) {
+ /* shown without the quad (direct scanout, or G0 turned off): painted next frame */
+ o->g0w = g0->w;
+ g0_stale(o);
+ } else {
+ o->g0on = g0drawn;
+ if (g0drawn) o->g0w = g0->w;
+ }
+ o->g0taken = 0;
+ if (readback && !res && gpu_read_done()) {
static int said;
if (!said++) fprintf(stderr, "remote: composing on the GPU; frames read back for the remote desktop\n");
Cur k;
@@ -1492,8 +1586,10 @@ static void render_frame(Server *s) {
}
view_reset();
gpu_out = NULL;
- o->cdirty = 0;
- o->wl_new = 0;
+ if (!unshown) { /* else they are presented with the next frame */
+ o->cdirty = 0;
+ o->wl_new = 0;
+ }
if (failed) {
fprintf(stderr, "gpu: a frame failed; going on by CPU\n");
gpu_shutdown();
@@ -1872,17 +1968,22 @@ int main(int argc, char **argv) {
if (scripted && !grab_scripted()) {
long lr = 0, lb = 0, lp = paint_layer_parts();
paint_layer_stats(&lr, &lb);
- char bs[192];
+ char bs[192], gs[256];
below_stats_text(bs, sizeof bs);
- fprintf(stderr, "drag (scripted): layer: %ld painted (%ld of them a part), %ld copied (%s); %s\n",
- lr, lp, lb, gpu_active() ? "GPU" : "CPU", bs);
+ g0_stats_text(gs, sizeof gs);
+ fprintf(stderr, "drag (scripted): layer: %ld painted (%ld of them a part), %ld copied (%s); %s; %s\n",
+ lr, lp, lb, gpu_active() ? "GPU" : "CPU", bs, gs);
}
if (!scripted && grab_scripted()) { /* its start: what came before is not counted */
long lr, lb;
BelowStats b;
+ G0Stats g;
paint_layer_stats(&lr, &lb);
paint_layer_parts();
paint_below_stats(&b);
+ paint_g0_stats(&g);
+ long q[3];
+ gpu_g0_stats(&q[0], &q[1], &q[2]);
}
scripted = grab_scripted();
/* Timers del tema (React agenda su trabajo con setTimeout/setImmediate). */
diff --git a/server/src/ui/paint.c b/server/src/ui/paint.c
index 503a5a9..bf5f974 100644
--- a/server/src/ui/paint.c
+++ b/server/src/ui/paint.c
@@ -516,8 +516,29 @@ static struct {
* shift-exact across 0: roundf, (int) truncation, canvas_corners_save) */
int ext_part; /* outside damage went into layer.part through layer.at since then */
int odd_text; /* text in it took a path that may not move exactly (text_drew_inexact) */
+ int premul_bad; /* GPU path: a pixel of it has a colour above its alpha (layer_holes; G0 needs none) */
} layer;
+/* ---- G0: the dragged window composited on the GPU (paint_root_g0, docs/GPU.md) ----
+ * What of the layer and of the copy of what is below changed since the GPU last got them: the
+ * rectangles (in their own pixels) or all of it. They add up over frames that don't use G0 and are
+ * consumed only when one does (paint_g0_done). */
+typedef struct { Rect r[G0_DIRTY]; int n, full; } G0Dirty;
+static G0Dirty g0_ld, g0_bd;
+static G0Stats g0stat;
+static void g0_dirty(G0Dirty *d, Rect r) {
+ if (d->full || r.x1 <= r.x0 || r.y1 <= r.y0) return;
+ if (d->n < G0_DIRTY) { d->r[d->n++] = r; return; }
+ Rect *l = &d->r[G0_DIRTY - 1]; /* no room: the last one grows */
+ *l = (Rect){ l->x0 < r.x0 ? l->x0 : r.x0, l->y0 < r.y0 ? l->y0 : r.y0, l->x1 > r.x1 ? l->x1 : r.x1, l->y1 > r.y1 ? l->y1 : r.y1 };
+}
+static void g0_dirty_all(void) { g0_ld = (G0Dirty){ .full = 1 }; g0_bd = (G0Dirty){ .full = 1 }; }
+void paint_g0_done(void) { g0_ld = (G0Dirty){ 0 }; g0_bd = (G0Dirty){ 0 }; }
+void paint_g0_stats(G0Stats *out) {
+ *out = g0stat;
+ memset(&g0stat, 0, sizeof g0stat);
+}
+
void paint_layer_begin(int node, int resize) {
layer.node = node;
layer.want = 1;
@@ -525,6 +546,7 @@ void paint_layer_begin(int node, int resize) {
layer.valid = 0;
layer.lg_valid = 0;
layer.bdmg_full = 1;
+ g0_dirty_all();
}
void paint_layer_end(void) {
@@ -535,6 +557,7 @@ void paint_layer_end(void) {
free(layer.below);
free(layer.hole);
memset(&layer, 0, sizeof layer);
+ g0_dirty_all();
}
void paint_layer_external_damage(void) { layer.valid = 0; }
@@ -620,14 +643,25 @@ static int in_surface(int x, int y) {
/* layer.has_holes == 0 stands for a map of zeros, which is what the loop below writes for a window
* with no app surfaces: then it neither writes the map nor does layer_paint walk it. The bytes it
* left are made zeros only when a part brings a hole into a map that had none. */
+/* A valid premultiplied pixel has no colour above its alpha; with one that does, blit_row's sum
+ * carries into the next channel (C) or wraps (NEON), and G0 would not match: noted, G0 refuses. */
+static inline int premul_bad(uint32_t p) {
+ uint32_t a = p >> 24;
+ return (p >> 16 & 255) > a || (p >> 8 & 255) > a || (p & 255) > a;
+}
static void layer_holes(int x0, int y0, int x1, int y1) {
- if (!gpu_active() || !layer.hole) return;
- int any = nlsurf || lsurf_all;
+ /* no map (the CPU path): the pixels are not checked either, so G0 must not trust them */
+ if (!gpu_active() || !layer.hole) { layer.premul_bad = 1; return; }
+ int any = nlsurf || lsurf_all, bad = 0;
if (!any && !layer.has_holes) {
for (int y = y0; y < y1; y++) {
uint32_t *row = layer.px + (size_t)y * layer.w;
- for (int x = x0; x < x1; x++) if (row[x] == SENTINEL) row[x] = 0;
+ for (int x = x0; x < x1; x++) {
+ if (row[x] == SENTINEL) row[x] = 0;
+ bad |= premul_bad(row[x]);
+ }
}
+ layer.premul_bad |= bad;
return;
}
int whole = x0 == 0 && y0 == 0 && x1 == layer.w && y1 == layer.h;
@@ -640,8 +674,10 @@ static void layer_holes(int x0, int y0, int x1, int y1) {
m[x] = row[x] == 0 && any && in_surface(x, y);
set |= m[x];
if (row[x] == SENTINEL) row[x] = 0;
+ bad |= premul_bad(row[x]);
}
}
+ layer.premul_bad |= bad;
if (whole) layer.has_holes = set;
else layer.has_holes |= set;
}
@@ -754,7 +790,9 @@ static int layer_render(Node *n, float opacity) {
skip_until = skip;
nlsurf = lsurf_all = 0;
layer_surfaces(n, x0, y0);
+ layer.premul_bad = 0;
layer_holes(0, 0, w, h);
+ g0_ld = (G0Dirty){ .full = 1 };
layer.valid = 1;
layer.painted = 1;
layer_renders++;
@@ -791,6 +829,7 @@ static int layer_render_part(Node *n, float opacity) {
nlsurf = lsurf_all = 0;
layer_surfaces(n, x0, y0);
layer_holes(px0, py0, px1, py1);
+ g0_dirty(&g0_ld, (Rect){ px0, py0, px1, py1 });
layer.painted = 1;
float k = paint_view.k, lx = (x0 - paint_view.dx) / k + paint_view.x, ly = (y0 - paint_view.dy) / k + paint_view.y;
layer.at = (Box){ lx, ly, lx + layer.w / k, ly + layer.h / k };
@@ -921,6 +960,7 @@ static int below_alloc(const Canvas *c) {
layer.bw = layer.below ? c->w : 0;
layer.bh = layer.below ? c->h : 0;
layer.bdmg_full = 1;
+ g0_bd = (G0Dirty){ .full = 1 };
return layer.below != NULL;
}
@@ -1011,7 +1051,11 @@ static void below_build(const Canvas *c, Node *root, const Rect *r, int n) {
layer.bsig = layer.osig;
layer.bview = paint_view;
bstat.full++;
- } else bstat.part++;
+ g0_bd = (G0Dirty){ .full = 1 };
+ } else {
+ bstat.part++;
+ for (int i = 0; i < n; i++) g0_dirty(&g0_bd, r[i]);
+ }
paint_cull = cull;
layer.bdmg.n = 0;
layer.bdmg_full = 0;
@@ -1203,6 +1247,43 @@ static int below_order(Node *root) {
return ord_n ? ORD_PATH : ORD_BEFORE;
}
+/* G0: the nodes painted after the window that paint something (their box on screen, grown by what a
+ * backdrop blur reads around it or a turned image's bilinear edge, logical px). A frame G0 composes
+ * must have none of them in its damaged box (paint_root_g0). */
+#define G0_AFTER_MAX 65536 /* the menu bar and the Dock alone are a few hundred */
+static Box *g0_after;
+static int ng0_after, g0_after_cap, g0_after_over;
+static int node_paints(const Node *n) {
+ const Style *s = &n->s;
+ if (n->type != NODE_VIEW) return 1; /* text, image, terminal, app window, video */
+ if ((s->bg >> 24) || s->has_gradient || (s->shadow_color >> 24) || s->backdrop_blur >= 1 || n->own_tf) return 1;
+ for (int e = EDGE_L; e <= EDGE_B; e++) if (node_border(n, e) > 0) return 1;
+ /* rounded and clipping: its corners are saved and restored around its children (paint_node), which
+ * changes what is below them even when it paints nothing itself */
+ if (s->overflow_hidden && (node_edges_plain(n) ? s->radius : max4(s->ext->r.rad)) > 0) return 1;
+ return 0;
+}
+static void g0_note_after(const Node *n, Box cur) {
+ if (cur.x1 <= cur.x0 || cur.y1 <= cur.y0 || !node_paints(n)) return;
+ if (g0_after_over) return;
+ float m = 0;
+ if (n->s.backdrop_blur >= 1) m = ceilf(n->s.backdrop_blur * 2) + 1;
+ if (n->has_tf && m < 4) m = 4;
+ Box b = { cur.x0 - m, cur.y0 - m, cur.x1 + m, cur.y1 + m };
+ if (ng0_after) { /* inside the box noted last (a child in its bar): meets D only if that one does */
+ const Box *p = &g0_after[ng0_after - 1];
+ if (b.x0 >= p->x0 && b.y0 >= p->y0 && b.x1 <= p->x1 && b.y1 <= p->y1) return;
+ }
+ if (ng0_after == g0_after_cap) {
+ int cap = g0_after_cap ? g0_after_cap * 2 : 256;
+ Box *nb = cap <= G0_AFTER_MAX ? realloc(g0_after, (size_t)cap * sizeof *nb) : NULL;
+ if (!nb) { g0_after_over = 1; return; }
+ g0_after = nb;
+ g0_after_cap = cap;
+ }
+ g0_after[ng0_after++] = b;
+}
+
/* ord: ORD_PATH for the window's ancestors, else whether the node is painted before or after it */
static void damage_walk(Node *n, int parent_dirty, int visible, int in_layer, Box clip, int ord) {
if (n->type == NODE_RAW) {
@@ -1233,6 +1314,7 @@ static void damage_walk(Node *n, int parent_dirty, int visible, int in_layer, Bo
layer.gdy = cur.y0 - n->prev_box.y0;
}
if (in_layer && vis) layer_note_exact(n, dirty);
+ if (ord == ORD_AFTER && vis) g0_note_after(n, cur);
if (dirty || !box_eq(cur, n->prev_box)) {
damage_add(n->prev_box);
damage_add(cur);
@@ -1508,6 +1590,7 @@ int paint_damage(Node *root) {
below_damage(ext);
}
int ord = below_order(root);
+ ng0_after = g0_after_over = 0;
if (root) damage_walk(root, 0, 1, 0, (Box){ -1e9f, -1e9f, 1e9f, 1e9f }, ord);
ndrects = 0;
if (multi_rects(root)) return 1;
@@ -1591,10 +1674,164 @@ static void below_verify(const Canvas *c, Node *root, uint32_t *pre) {
free(pre);
}
+/* ---- G0: the dragged window composited on the GPU (docs/GPU.md, "The dragged window on the GPU") ----
+ * On a frame that uses the copy of what is below (below_ready), the CPU's pixels inside the window's
+ * rectangle W are the layer blitted over that copy (canvas_blit, k = 256: blit_row) and nothing else,
+ * when: the layer is painted as it is (valid, the same opacity, no full render pending, no app window
+ * or invalid premultiplied pixel in it), no ancestor paints over W afterwards (border, rounded
+ * corners), no app window is drawn under W (the GPU draws those below the UI) and no node painted
+ * after the window meets the frame's box at all (a blurred one would read the stale W; one turned or
+ * blurred paints differently when the box is cut into strips). Then only the strips around W are
+ * painted, each a walk clipped to it (every primitive left gives the same pixel whatever the clip),
+ * and the GPU writes W from two textures with the same arithmetic (gpu.c, checked against blit_row
+ * once by a self-test). Anything that changes while painting the strips (the layer painted again
+ * elsewhere, an app window) sends W back to the CPU in the same frame. */
+static Rect rect_and(Rect a, Rect b) {
+ Rect r = { a.x0 > b.x0 ? a.x0 : b.x0, a.y0 > b.y0 ? a.y0 : b.y0, a.x1 < b.x1 ? a.x1 : b.x1, a.y1 < b.y1 ? a.y1 : b.y1 };
+ if (r.x1 < r.x0) r.x1 = r.x0;
+ if (r.y1 < r.y0) r.y1 = r.y0;
+ return r;
+}
+static int rect_eq(Rect a, Rect b) { return a.x0 == b.x0 && a.y0 == b.y0 && a.x1 == b.x1 && a.y1 == b.y1; }
+static int g0_bx, g0_by; /* where the layer's pixel (0, 0) lands (layer_paint's bx, by) */
+
+/* W for this frame (-1), or why there is none (G0R_*): where layer_paint's blit lands, cut by the
+ * ancestors' clips and by c->clip, the frame's box (inside the canvas). */
+static int g0_gate(const Canvas *c, Node *root, Rect *wout) {
+ Node *w = node_get(layer.node);
+ if (!w || !layer.want || !layer.valid || layer.resize || layer.rendering || paint_capture_mode ||
+ paint_view.index != layer.view || w->has_tf || w->type == NODE_RAW || w->s.display_none)
+ return G0R_LAYER;
+ if (layer.has_holes || nlsurf || lsurf_all || layer.premul_bad || !layer.px || !layer.below) return G0R_LAYER;
+ const Node *anc[SKIP_PATH_MAX];
+ int na = 0;
+ for (const Node *a = w->parent; a; a = a->parent) {
+ if (na == SKIP_PATH_MAX) return G0R_ANCESTOR;
+ if (a->type == NODE_RAW || a->s.display_none) return G0R_LAYER;
+ anc[na++] = a;
+ }
+ if (!na || anc[na - 1] != root) return G0R_LAYER;
+ const float k = paint_view.k;
+ int bx = (int)lroundf(VX(w->ax) + layer.pox), by = (int)lroundf(VY(w->ay) + layer.poy);
+ Rect r = { bx, by, bx + layer.w, by + layer.h };
+ /* the opacity layer_paint gets (paint_node's product, root first) and the ancestors' clips */
+ float op = 1;
+ for (int i = na - 1; i >= 0; i--) {
+ const Node *a = anc[i];
+ op = op * a->s.opacity;
+ if (op < 0.004f) return G0R_LAYER;
+ if (a->s.overflow_hidden) {
+ float x = VX(a->ax), y = VY(a->ay), aw = a->w * k, ah = a->h * k;
+ r = rect_and(r, (Rect){ (int)floorf(x), (int)floorf(y), (int)ceilf(x + aw), (int)ceilf(y + ah) });
+ }
+ }
+ if (op != layer.op) return G0R_LAYER;
+ Box pb;
+ if (layer.tree_part && !layer_part_exact(w, &pb)) return G0R_LAYER; /* it would be painted whole */
+ r = rect_and(r, c->clip); /* the blit is clipped to the frame's box too */
+ if (!rect_area(r)) return G0R_CLIP;
+ if (!below_ancestors_ok(w, r)) return G0R_ANCESTOR;
+ if (wl_draws_meet(r)) return G0R_APP;
+ if (!ord_n || g0_after_over) return G0R_AFTER;
+ for (int i = 0; i < ng0_after; i++) {
+ Box b = g0_after[i];
+ Rect q = { (int)floorf(VX(b.x0)) - 1, (int)floorf(VY(b.y0)) - 1, (int)ceilf(VX(b.x1)) + 1, (int)ceilf(VY(b.y1)) + 1 };
+ if (rect_meets(q, c->clip)) return G0R_AFTER;
+ }
+ g0_bx = bx;
+ g0_by = by;
+ *wout = r;
+ return -1;
+}
+
+/* One walk of a frame that uses the copy, clipped to c->clip: the copy, then from the window on */
+static void g0_walk(Canvas *c, Node *root) {
+ wl_paint_begin();
+ wl_paint_kept();
+ below_copy(c);
+ skip_until = layer.node;
+ skip_reached = 0;
+ skip_stamp();
+ paint_node(c, root, 1);
+}
+
+/* LORD_GPU_VERIFY=N: every N-th G0 frame, W painted by the CPU (a walk clipped to W on a scratch
+ * canvas the scene's size: the save of an ancestor's rounded corners reads outside the clip), leaving
+ * the frame's list of app windows and the counters as they were. W's rows are returned packed (W's
+ * width a row). */
+static const uint32_t *g0_reference(const Canvas *c, Node *root, Rect w) {
+ static uint32_t *buf;
+ static size_t cap;
+ size_t n = (size_t)c->w * c->h;
+ if (n > cap) {
+ free(buf);
+ buf = calloc(n, 4);
+ cap = buf ? n : 0;
+ if (!buf) return NULL;
+ }
+ Canvas rc;
+ canvas_init(&rc, buf, c->w, c->h, c->w);
+ canvas_set_clip(&rc, w);
+ long lb = layer_blits, lr = layer_renders, tr = tf_renders, tc = tf_composites;
+ wl_paint_save();
+ g0_walk(&rc, root);
+ wl_paint_restore();
+ layer_blits = lb; layer_renders = lr; tf_renders = tr; tf_composites = tc;
+ int ww = w.x1 - w.x0;
+ for (int y = w.y0; y < w.y1; y++) /* forwards: a row never lands past where it is read */
+ memmove(buf + (size_t)(y - w.y0) * ww, buf + (size_t)y * c->w + w.x0, (size_t)ww * 4);
+ return buf;
+}
+
+/* The strips around w, then the post-check. 1: g is filled and w is left to the GPU; 0: w was painted
+ * too (the frame is the same as without G0). */
+static int g0_paint(Canvas *c, Node *root, Rect w, GpuG0 *g) {
+ static int every = -1;
+ static long frames;
+ if (every < 0) {
+ const char *e = getenv("LORD_GPU_VERIFY");
+ every = e ? atoi(e) : 0;
+ if (every < 0) every = 0;
+ }
+ Rect d = c->clip, st[4];
+ int ns = 0;
+ if (w.y0 > d.y0) st[ns++] = (Rect){ d.x0, d.y0, d.x1, w.y0 };
+ if (w.y1 < d.y1) st[ns++] = (Rect){ d.x0, w.y1, d.x1, d.y1 };
+ if (w.x0 > d.x0) st[ns++] = (Rect){ d.x0, w.y0, w.x0, w.y1 };
+ if (w.x1 < d.x1) st[ns++] = (Rect){ w.x1, w.y0, d.x1, w.y1 };
+ long lb = layer_blits;
+ for (int i = 0; i < (ns ? ns : 1); i++) {
+ /* no strip: one walk with nothing to paint (the layer's part is rendered, app windows noted) */
+ canvas_set_clip(c, ns ? st[i] : (Rect){ w.x0, w.y0, w.x0, w.y0 });
+ g0_walk(c, root);
+ }
+ layer_blits = lb + 1;
+ canvas_set_clip(c, d);
+ Rect w2;
+ if (g0_gate(c, root, &w2) >= 0 || !rect_eq(w, w2)) {
+ canvas_set_clip(c, w);
+ g0_walk(c, root);
+ canvas_set_clip(c, d);
+ return 0;
+ }
+ *g = (GpuG0){ .w = w, .lpx = layer.px, .lw = layer.w, .lh = layer.h, .lx = g0_bx, .ly = g0_by,
+ .bpx = layer.below, .bw = layer.bw, .bh = layer.bh, .nl = g0_ld.n, .nb = g0_bd.n,
+ .lfull = g0_ld.full, .bfull = g0_bd.full, .nstrip = ns };
+ memcpy(g->ldirty, g0_ld.r, sizeof g0_ld.r);
+ memcpy(g->bdirty, g0_bd.r, sizeof g0_bd.r);
+ memcpy(g->strip, st, sizeof st);
+ g->frame = ++frames;
+ g->ref = every && frames % every == 0 ? g0_reference(c, root, w) : NULL;
+ g0stat.frames++;
+ for (int i = 0; i < ns; i++) g0stat.strip_px += rect_area(st[i]);
+ return 1;
+}
+
/* clear: fill the clip with rgb first, unless the copy of what is below the dragged window is
- * about to write every pixel of that same clip (below_copy), which makes the fill wasted work */
-static void paint_root_body(Canvas *c, Node *root, int clear, uint32_t rgb) {
- if (!root) { if (clear) canvas_clear(c, rgb); return; }
+ * about to write every pixel of that same clip (below_copy), which makes the fill wasted work.
+ * g (G0, paint_root_g0): the window's rectangle may be left to the GPU; 1 if it was. */
+static int paint_root_body(Canvas *c, Node *root, int clear, uint32_t rgb, GpuG0 *g) {
+ if (!root) { if (clear) canvas_clear(c, rgb); return 0; }
wl_paint_begin();
/* solo cambió un cuadro de video que tapa esa región: se pinta desde ese nodo (no en el camino
* GPU, que arma acá la lista de ventanas de las apps) */
@@ -1607,15 +1844,30 @@ static void paint_root_body(Canvas *c, Node *root, int clear, uint32_t rgb) {
/* once per call: it counts layer.since */
int below = !skip_until && below_ready(c, root, clear && rgb == 0x000000);
uint32_t *pre = below && layer.switched && paint_below_verify ? below_snapshot(c) : NULL;
- if (below) {
- below_copy(c);
- skip_until = layer.node;
- } else if (clear) {
- canvas_clear(c, rgb);
+ int done = 0, taken = 0;
+ if (below && g && gpu_active()) { /* G0: only after below_ready, so its decisions are the same */
+ Rect w;
+ int why = g0_gate(c, root, &w);
+ g0stat.tried++;
+ if (why < 0) {
+ done = 1;
+ taken = g0_paint(c, root, w, g);
+ if (!taken) why = G0R_POST;
+ }
+ if (!taken) g0stat.refused[why]++;
}
- skip_stamp();
- paint_node(c, root, 1);
- if (pre) below_verify(c, root, pre);
+ if (!done) {
+ if (below) {
+ below_copy(c);
+ skip_until = layer.node;
+ } else if (clear) {
+ canvas_clear(c, rgb);
+ }
+ skip_stamp();
+ paint_node(c, root, 1);
+ }
+ if (pre && taken) free(pre); /* W is the GPU's: the old way is compared there by LORD_GPU_VERIFY */
+ else if (pre) below_verify(c, root, pre);
skip_until = 0;
paint_cull = 0;
tf_gc();
@@ -1625,15 +1877,21 @@ static void paint_root_body(Canvas *c, Node *root, int clear, uint32_t rgb) {
for (int i = 0; i < 6; i++) { fprintf(stderr, " %s %.2f", prof_name[i], prof[i]); prof[i] = 0; }
fprintf(stderr, " ms\n");
}
+ return taken;
}
void paint_root(Canvas *c, Node *root) {
- paint_root_body(c, root, 0, 0);
+ paint_root_body(c, root, 0, 0, NULL);
}
/* canvas_clear(c, rgb) followed by paint_root(c, root), with the same pixels */
void paint_root_clear(Canvas *c, Node *root, uint32_t rgb) {
- paint_root_body(c, root, 1, rgb);
+ paint_root_body(c, root, 1, rgb, NULL);
+}
+
+/* paint_root_clear(c, root, 0) with the dragged window's rectangle left to the GPU when exact (G0, above) */
+int paint_root_g0(Canvas *c, Node *root, GpuG0 *g) {
+ return paint_root_body(c, root, 1, 0x000000, g);
}
/* The whole tree painted again with nothing kept from earlier frames: no drag layer, no copy of what
diff --git a/server/src/ui/ui.h b/server/src/ui/ui.h
index 91582b8..fd706c8 100644
--- a/server/src/ui/ui.h
+++ b/server/src/ui/ui.h
@@ -310,6 +310,23 @@ extern long paint_below_bad, paint_below_checked;
enum { BR_OK, BR_ANCESTOR, BR_BLUR, BR_TRANSFORM, BR_UNREACHED, BR_COST, BR_N };
typedef struct { long full, part, switched, refused[BR_N]; } BelowStats;
void paint_below_stats(BelowStats *s); /* builds (whole, part) and switched frames since the last call */
+/* G0 (docs/GPU.md, "The dragged window on the GPU"): paint_root_clear's frame, except that where
+ * the dragged window lands (g->w) nothing is painted: the GPU composites the layer over the copy of what
+ * is below there, with the CPU's arithmetic. Only on frames that use that copy and only when nothing
+ * else would paint there (see paint.c); 1 if it was taken (the scene's g->w is then stale and only
+ * g->strip were painted), 0 if the whole clip was painted as paint_root_clear(c, root, 0) does.
+ * paint_g0_done: the GPU got g's textures (the dirty rectangles start again). LORD_GPU_VERIFY=N: every
+ * N-th taken frame also paints g->w on the CPU into g->ref, for the GPU to compare. */
+struct GpuG0;
+int paint_root_g0(Canvas *c, Node *root, struct GpuG0 *g);
+void paint_g0_done(void);
+/* why a frame that used the copy was not composited on the GPU: the layer (invalid, re-rendered, app
+ * windows or invalid pixels in it, other opacity, a transform), its rectangle not inside the damage,
+ * an ancestor painting over it, an app window under it, a node painted after the window in the
+ * damage, or something changed while painting (post) */
+enum { G0R_LAYER, G0R_CLIP, G0R_ANCESTOR, G0R_APP, G0R_AFTER, G0R_POST, G0R_N };
+typedef struct { long frames, strip_px, tried, refused[G0R_N]; } G0Stats;
+void paint_g0_stats(G0Stats *s); /* since the last call */
Node *hit_test(Node *root, float x, float y);
/* Capture mode (media/capture.c): protected nodes are painted black and app windows
* are copied even when compositing on the GPU. paint_capture paints the whole tree this way. */
diff --git a/server/src/wl/wl.c b/server/src/wl/wl.c
index d5a03fa..4b2ff76 100644
--- a/server/src/wl/wl.c
+++ b/server/src/wl/wl.c
@@ -2144,6 +2144,21 @@ void wl_paint_restore(void) {
memcpy(ndraws, verify_n, sizeof ndraws);
}
+/* G0 (ui/paint.c): whether an app window noted so far for layer 0 is drawn into r (physical px of the
+ * display being painted): its box as draw_surface gives it, one pixel more on each side for the
+ * filtering, cut by its clip. A window that is gone is not drawn. */
+int wl_draws_meet(Rect r) {
+ for (int i = 0; i < ndraws[0]; i++) {
+ const Draw *d = &draws[0][i];
+ const Surf *s = find(d->sid);
+ if (!s) continue;
+ Rect b = rect_and(phys_rect(d->clip), (Rect){ (int)floorf(PVX(d->x)) - 1, (int)floorf(PVY(d->y)) - 1,
+ (int)ceilf(PVX(d->x + s->lw)) + 1, (int)ceilf(PVY(d->y + s->lh)) + 1 });
+ if (b.x0 < r.x1 && r.x0 < b.x1 && b.y0 < r.y1 && r.y0 < b.y1) return 1;
+ }
+ return 0;
+}
+
void wl_paint(Canvas *c, int id, int node, float x, float y, float w, float h, float op) {
Surf *s = find(id);
if (!s || s->role != R_TOPLEVEL) return;
diff --git a/server/src/wl/wl.h b/server/src/wl/wl.h
index 1f4890e..55986de 100644
--- a/server/src/wl/wl.h
+++ b/server/src/wl/wl.h
@@ -61,6 +61,7 @@ void wl_paint_kept(void); /* and notes them again in another fram
void wl_paint_save(void); /* keeps the frame's list of windows (LORD_PAINT_VERIFY) */
void wl_paint_restore(void); /* and puts it back */
void wl_paint_holes(int on); /* 0: wl_paint only notes the window, without opening the hole */
+int wl_draws_meet(Rect r); /* GPU path: an app window noted below the UI (layer 0) meets r (physical px of the view being painted) */
void wl_paint(Canvas *c, int id, int node, float x, float y, float w, float h, float op);
void wl_paint_popups(Canvas *c);
diff --git a/server/tests/canvas_test.c b/server/tests/canvas_test.c
index eb34ff6..a0f9817 100644
--- a/server/tests/canvas_test.c
+++ b/server/tests/canvas_test.c
@@ -1,6 +1,9 @@
/* canvas_test.c — the SIMD rows of canvas.c (NEON on ARM64, NEON=1) must give the same result,
* bit for bit, as the portable C ones. On x86-64 (no SIMD of its own) it compares C against C and also
* checks the exact division by 255 that the NEON version uses. See docs/RASPBERRY.md.
+ * G0's shader in C (canvas_g0_model, docs/GPU.md "The dragged window on the GPU") must give blit_row's
+ * pixel at k = 256 for every valid premultiplied source channel (c ≤ alpha) over every destination
+ * channel, and every source alpha over every destination alpha: checked exhaustively against both.
*
* make -C server NEON=1 build/canvas-test && server/build/canvas-test (on the Raspberry Pi)
*/
@@ -13,6 +16,7 @@
const char *canvas_test_simd(void);
void canvas_test_blit_row(int simd, uint32_t *row, const uint32_t *s, int n, uint32_t k);
void canvas_test_mask_row(int simd, uint32_t *row, const uint8_t *m, int n, uint32_t argb);
+uint32_t canvas_g0_model(uint32_t src, uint32_t dst);
static uint32_t rng = 12345;
static uint32_t rnd(void) { rng ^= rng << 13; rng ^= rng >> 17; rng ^= rng << 5; return rng; }
@@ -66,6 +70,41 @@ int main(void) {
}
}
+ /* G0's model against blit_row_c and the SIMD row, exhaustively: for every alpha a and colour c ≤ a,
+ * a row of 256 destinations with every channel value d (all three channels, so each sum is checked
+ * on each), the destination alpha (7d + a) & 255 (every value once per a) */
+ long g0cases = 0, g0bad = 0;
+ for (uint32_t a = 0; a < 256; a++)
+ for (uint32_t c = 0; c <= a; c++) {
+ static uint32_t gs[256], gd[256], gc[256], gn[256];
+ for (uint32_t i = 0; i < 256; i++) {
+ gs[i] = a << 24 | c << 16 | c << 8 | c;
+ gd[i] = ((7 * i + a) & 255) << 24 | i << 16 | (255 - i) << 8 | i;
+ }
+ memcpy(gc, gd, sizeof gd);
+ memcpy(gn, gd, sizeof gd);
+ canvas_test_blit_row(0, gc, gs, 256, 256);
+ canvas_test_blit_row(1, gn, gs, 256, 256);
+ for (int i = 0; i < 256; i++) {
+ uint32_t m = canvas_g0_model(gs[i], gd[i]);
+ g0cases++;
+ if (m != gc[i] || m != gn[i]) {
+ if (g0bad++ < 10) printf("G0 model: src=%08x dst=%08x C=%08x SIMD=%08x model=%08x\n", gs[i], gd[i], gc[i], gn[i], m);
+ }
+ }
+ }
+ /* and random valid sources with unrelated channels */
+ for (int it = 0; it < 200000; it++) {
+ uint32_t sp = premul(), dp = rnd(), cr = dp, nr = dp;
+ canvas_test_blit_row(0, &cr, &sp, 1, 256);
+ canvas_test_blit_row(1, &nr, &sp, 1, 256);
+ uint32_t m = canvas_g0_model(sp, dp);
+ g0cases++;
+ if ((m != cr || m != nr) && g0bad++ < 10) printf("G0 model: src=%08x dst=%08x C=%08x SIMD=%08x model=%08x\n", sp, dp, cr, nr, m);
+ }
+ printf("canvas-test: G0 model: %ld cases, %ld differ\n", g0cases, g0bad);
+ fails += (int)g0bad;
+
/* time for one 1920×1080 screen with each (not a test: it measures) */
static uint32_t big_s[1920], big_d[1920];
static uint8_t big_m[1920];
diff --git a/server/tests/engine_test.c b/server/tests/engine_test.c
index 6ea9ae8..313a480 100644
--- a/server/tests/engine_test.c
+++ b/server/tests/engine_test.c
@@ -15,6 +15,7 @@
#include <time.h>
#include "bench/lbinput.h"
+#include "gpu/gpu.h"
#include "outputs.h"
#include "ui/fonts.h"
#include "ui/ui.h"
@@ -60,6 +61,17 @@ void wl_paint_begin(void) { nwdraw = 0; }
void wl_paint_save(void) { nwsaved = nwdraw; memcpy(wsaved, wdraw, sizeof *wdraw * (size_t)nwdraw); }
void wl_paint_restore(void) { nwdraw = nwsaved; memcpy(wdraw, wsaved, sizeof *wdraw * (size_t)nwsaved); }
void wl_paint_popups(Canvas *c) { (void)c; }
+/* G0 (paint_root_g0): an app window in the list is drawn below the UI where its box is, as wl.c's */
+int wl_draws_meet(Rect r) {
+ const float k = paint_view.k;
+ for (int i = 0; i < nwdraw; i++) {
+ const WDraw *d = &wdraw[i];
+ Rect b = { (int)floorf((d->x - paint_view.x) * k + paint_view.dx) - 1, (int)floorf((d->y - paint_view.y) * k + paint_view.dy) - 1,
+ (int)ceilf((d->x + d->w - paint_view.x) * k + paint_view.dx) + 1, (int)ceilf((d->y + d->h - paint_view.y) * k + paint_view.dy) + 1 };
+ if (b.x0 < r.x1 && r.x0 < b.x1 && b.y0 < r.y1 && r.y0 < b.y1) return 1;
+ }
+ return 0;
+}
void media_paint(Canvas *c, int id, int node, float x, float y, float w, float h, float r, float op) { (void)c; (void)id; (void)node; (void)x; (void)y; (void)w; (void)h; (void)r; (void)op; }
int media_cover_node(float x0, float y0, float x1, float y1) { (void)x0; (void)y0; (void)x1; (void)y1; return 0; }
int gpu_active(void) { return test_gpu; }
@@ -1379,6 +1391,27 @@ static void test_transform_paint_hit(void) {
CHECK(pix(35, 35) == 0xff0000, "the scaled box with a blurred child: %06x", pix(35, 35));
}
+/* A node with its own transform whose own style changes (here its background) is rasterized again:
+ * damage_walk keeps the node's paint_dirty for tf_check, which runs after the children (before,
+ * it had been cleared and the cached raster kept the old colour). */
+static void test_transform_self_restyle(void) {
+ ui_reset();
+ Node *root = view(NULL);
+ ui.root = root;
+ sn(root, "backgroundColor", 0xffffffff);
+ Node *a = view(root); /* 40×40 red, rotated 15° */
+ ss(a, "position", "absolute");
+ sn(a, "left", 30); sn(a, "top", 30); sn(a, "width", 40); sn(a, "height", 40);
+ sn(a, "backgroundColor", 0xffff0000);
+ sn(a, "rotate", 15);
+ render(root, 100, 100);
+ CHECK(pix(50, 50) == 0xff0000, "rotated box: %06x", pix(50, 50));
+ sn(a, "backgroundColor", 0xff0000ff);
+ node_invalidate(a);
+ render(root, 100, 100);
+ CHECK(pix(50, 50) == 0x0000ff, "rotated box kept its old background: %06x", pix(50, 50));
+}
+
/* The overlay scroll bar as ScrollView (sdk/components.jsx, sdk/scrollbar.js) builds it: an
* overflow-hidden view, the content translated inside it with its normal end padding, and over it a
* 14 px hit strip at the end edge holding the 6 px thumb 2 px from the edge. The bar is painted
@@ -1474,7 +1507,10 @@ static void test_overlay_scrollbar(void) {
* most, all in those corner squares (n = ceil(r) + 1) or in the Dock's and the menu bar's backdrop
* blur within its reach of one, none further from a full repaint. "gpu" is the same run with
* gpu_active() on: paint.c's GPU path (the layer's sentinel, the app holes, no skipped nodes) on the
- * same CPU canvas the GPU uploads. Shorter runs cover what makes the layer paint again: a text
+ * same CPU canvas the GPU uploads. "g0" is the GPU path with G0 (paint_root_g0, docs/GPU.md): a stub GPU
+ * keeps the UI texture (what was uploaded of the canvas) and the layer's and the copy's textures (only
+ * their dirty rectangles), and writes the window's rectangle with canvas_g0_model (gpu.c's shader in C);
+ * what it shows must hash as drag_golden_gpu.txt. Shorter runs cover what makes the layer paint again: a text
* that changes a child's size, a child moving inside the window, a terminal whose cursor blinks
* (damage from outside the tree), a square turning across the window's edge, the window crossing
* x = 0 and y = 0 while the square turns, a child removed after frames that only copy the layer and
@@ -1511,19 +1547,29 @@ enum { DW = 1024, DH = 640, DRAG_FRAMES = 480, CASE_FRAMES = 60, FULL_EVERY = 16
* "anc" the bar and the panel, with both windows in a rounded container that clips them and has a
* border (an ancestor painted around the dragged window); "tfgrow" the still square, and a dot on it
* that changes color every 20 frames (a part of the copy that cuts the square's image). */
+/* The runs after DC_N have no golden hashes: G0 (paint_root_g0) is compared with the GPU path itself
+ * in them (test_g0_extra): "pause" stops the drag for 30 frames with nothing changing (frames with no UI
+ * damage, which only draw the window again on the GPU), "two" is the drag on two displays side by side
+ * (the window crosses from one to the other: the layer, the copy and G0 follow the one with most of it),
+ * "app" drags over the window below with its app window, "ancedge" slowly away from the rounded,
+ * bordered container's top-left corner, "corner" over a rounded overflow:hidden box painted after the
+ * window that paints nothing itself (its corners' save and restore still change the pixels below). Their frames are compared as the GPU shows them: the app
+ * windows below the UI texture, the UI blended over them, G0's rectangle written over both. */
enum { DC_DRAG, DC_TEXT, DC_CHILD, DC_TERM, DC_EDGE, DC_NEG, DC_REMOVE, DC_K2,
- DC_BELOW, DC_AFTER, DC_BELOWTF, DC_BLUR, DC_ZRAISE, DC_MIX, DC_K2MIX, DC_ANC, DC_TFGROW, DC_N };
-static const char *const dc_name[DC_N] = { "drag", "text", "child", "term", "edge", "neg", "remove", "k2",
- "below", "after", "belowtf", "blur", "zraise", "mix", "k2mix", "anc", "tfgrow" };
-static const int dc_frames[DC_N] = { DRAG_FRAMES, CASE_FRAMES, CASE_FRAMES, CASE_FRAMES, CASE_FRAMES,
- CASE_FRAMES, CASE_FRAMES, CASE_FRAMES,
- 180, 120, 180, 180, 120, 240, 120, 180, 180 };
+ DC_BELOW, DC_AFTER, DC_BELOWTF, DC_BLUR, DC_ZRAISE, DC_MIX, DC_K2MIX, DC_ANC, DC_TFGROW, DC_N,
+ DC_PAUSE = DC_N, DC_TWO, DC_APP, DC_ANCEDGE, DC_CORNER, DC_ALL };
+static const char *const dc_name[DC_ALL] = { "drag", "text", "child", "term", "edge", "neg", "remove", "k2",
+ "below", "after", "belowtf", "blur", "zraise", "mix", "k2mix", "anc", "tfgrow",
+ "pause", "two", "app", "ancedge", "corner" };
+static const int dc_frames[DC_ALL] = { DRAG_FRAMES, CASE_FRAMES, CASE_FRAMES, CASE_FRAMES, CASE_FRAMES,
+ CASE_FRAMES, CASE_FRAMES, CASE_FRAMES,
+ 180, 120, 180, 180, 120, 240, 120, 180, 180, 90, 240, 180, 90, 90 };
enum { DF_BAR = 1, DF_MENU = 2, DF_SQUARE = 4, DF_PANEL = 8, DF_RAISE = 16, DF_ANC = 32, DF_SMALL = 64, DF_TERM = 128, DF_DOT = 256 };
-static const int dc_feat[DC_N] = { 0, 0, 0, 0, 0, 0, 0, 0,
+static const int dc_feat[DC_ALL] = { 0, 0, 0, 0, 0, 0, 0, 0,
DF_BAR | DF_TERM, DF_MENU, DF_SQUARE | DF_SMALL, DF_PANEL | DF_SMALL, DF_BAR | DF_RAISE,
DF_BAR | DF_MENU | DF_SQUARE | DF_PANEL | DF_SMALL,
DF_BAR | DF_MENU | DF_SQUARE | DF_PANEL | DF_SMALL, DF_BAR | DF_ANC | DF_PANEL | DF_SMALL,
- DF_DOT | DF_SMALL };
+ DF_DOT | DF_SMALL, DF_TERM | DF_SMALL, DF_SMALL, DF_BAR | DF_SMALL, DF_ANC | DF_SMALL, DF_SMALL };
static int dc_k2(int dc) { return dc == DC_K2 || dc == DC_K2MIX; }
static int dc_hashes(void) { /* each run: its frames and the one after the drag */
int n = 0;
@@ -1531,7 +1577,21 @@ static int dc_hashes(void) { /* each run: its frames and
return n;
}
-typedef struct { OutView v; Canvas c, ref; uint32_t *px, *rpx; } DOut;
+/* G0 (test_g0): the frame as the GPU shows it. tex is the GPU's UI texture (what was uploaded of the
+ * scene c), fb the composed frame: tex with the window's rectangle written by canvas_g0_model (gpu.c's
+ * shader in C) from two textures that get only paint.c's dirty rectangles, as gpu.c's. g0on, g0w:
+ * main.c's bookkeeping of the scene's stale rectangle. */
+typedef struct {
+ OutView v;
+ Canvas c, ref, fbc;
+ uint32_t *px, *rpx, *tex, *fb, *cmp;
+ int g0on;
+ Rect g0w;
+ GpuG0 g;
+} DOut;
+static int test_g0;
+static struct { uint32_t *b, *l; int bw, bh, lw, lh, owner; } g0tex; /* the stub GPU's G0 textures */
+static long g0_quads;
typedef struct { Node *root, *win, *spin, *edge, *child, *term, *caption, *below, *btitle, *fill, *mtext, *square, *ptext, *dot; } DScene;
static uint64_t frame_hash(const Canvas *c) {
@@ -1555,28 +1615,106 @@ static void js_set(Node *n, const char *k, double v) {
node_restyled(n, &before, before.ext ? &xb : NULL);
}
-/* main.c's frame on one display: paint_ui (layout, damage, the layer's display), then paint_out (the
- * damaged box in physical px, cleared and painted with paint_root_clear) */
-static void drag_paint(Node *root, DOut *o) {
- if (!ui.dirty) return;
- ui.dirty = 0;
- layout_root(root, DW, DH);
- Box d = { 0, 0, 0, 0 };
- if (paint_damage(root)) d = ui.damage;
- ui.damage = (Box){ 0, 0, 0, 0 };
- if (paint_layer_node()) paint_layer_view(0);
+static void copy_rect(uint32_t *dst, int dstride, const uint32_t *src, int sstride, Rect r) {
+ for (int y = r.y0; y < r.y1; y++)
+ memcpy(dst + (size_t)y * dstride + r.x0, src + (size_t)y * sstride + r.x0, (size_t)(r.x1 - r.x0) * 4);
+}
+
+/* The stub GPU's texture t (*tw × *th): all of px if its size changed, it was another display's or
+ * full, else the dirty rectangles (gpu.c's g0_upload). */
+static void g0_tex_up(uint32_t **t, int *tw, int *th, const uint32_t *px, int w, int h, const Rect *r, int n, int full) {
+ if (*tw != w || *th != h) {
+ free(*t);
+ *t = malloc((size_t)w * h * 4);
+ *tw = w;
+ *th = h;
+ full = 1;
+ }
+ if (!*t) return;
+ if (full) { memcpy(*t, px, (size_t)w * h * 4); return; }
+ for (int i = 0; i < n; i++) {
+ Rect q = { r[i].x0 < 0 ? 0 : r[i].x0, r[i].y0 < 0 ? 0 : r[i].y0, r[i].x1 > w ? w : r[i].x1, r[i].y1 > h ? h : r[i].y1 };
+ if (q.x1 > q.x0 && q.y1 > q.y0) copy_rect(*t, w, px, w, q);
+ }
+}
+
+/* main.c's presentation on the GPU (render_frame, gpu_frame_ex): the painted rectangles (pr, or the
+ * strips) go to the UI texture; with G0 the two textures get their dirty rectangles and the window is
+ * written over the texture's stale rectangle in the composed frame. */
+static void g0_present(DOut *o, int index, Rect pr, int taken) {
+ int W = o->c.w, H = o->c.h;
+ if (!taken) copy_rect(o->tex, W, o->c.px, o->c.stride, pr);
+ else {
+ const GpuG0 *g = &o->g;
+ for (int i = 0; i < g->nstrip; i++) copy_rect(o->tex, W, o->c.px, o->c.stride, g->strip[i]);
+ int other = g0tex.owner != index;
+ g0_tex_up(&g0tex.b, &g0tex.bw, &g0tex.bh, g->bpx, g->bw, g->bh, g->bdirty, g->nb, g->bfull || other);
+ g0_tex_up(&g0tex.l, &g0tex.lw, &g0tex.lh, g->lpx, g->lw, g->lh, g->ldirty, g->nl, g->lfull || other);
+ g0tex.owner = index;
+ paint_g0_done();
+ g0_quads++;
+ }
+ memcpy(o->fb, o->tex, (size_t)W * H * 4);
+ if (taken) {
+ const GpuG0 *g = &o->g;
+ for (int y = g->w.y0; y < g->w.y1; y++)
+ for (int x = g->w.x0; x < g->w.x1; x++)
+ o->fb[(size_t)y * W + x] = canvas_g0_model(g0tex.l[(size_t)(y - g->ly) * g0tex.lw + (x - g->lx)],
+ g0tex.b[(size_t)y * g0tex.bw + x]);
+ o->g0w = g->w;
+ }
+ o->g0on = taken;
+}
+
+/* One display's paint_out (the damaged box in physical px, cleared and painted with paint_root_clear;
+ * with G0, main.c's paint_out and render_frame: the stale rectangle joins the damage, a display with no
+ * damage keeps its frame, unless the G0 textures are another display's) */
+static void drag_paint_out(Node *root, DOut *o, int index, Box d) {
+ int empty = d.x1 <= d.x0 || d.y1 <= d.y0;
+ if (test_g0 && o->g0on) {
+ float gx0, gy0, gx1, gy1;
+ out_logical_box(&o->v, o->g0w, &gx0, &gy0, &gx1, &gy1);
+ if (!empty) d = (Box){ fminf(d.x0, gx0), fminf(d.y0, gy0), fmaxf(d.x1, gx1), fmaxf(d.y1, gy1) };
+ else if (g0tex.owner != index) { d = (Box){ gx0, gy0, gx1, gy1 }; o->g0on = 0; } /* g0_stale */
+ /* else: drawn again from the textures, the frame stays as it is */
+ }
if (d.x1 <= d.x0 || d.y1 <= d.y0) return;
Rect pr = out_phys_rect(&o->v, d.x0, d.y0, d.x1, d.y1);
if (pr.x1 <= pr.x0 || pr.y1 <= pr.y0) return;
float lx0, ly0, lx1, ly1;
out_logical_box(&o->v, pr, &lx0, &ly0, &lx1, &ly1);
- paint_view = (PaintView){ (float)o->v.x, (float)o->v.y, (float)o->v.k, (float)o->v.dx, (float)o->v.dy, 0 };
+ paint_view = (PaintView){ (float)o->v.x, (float)o->v.y, (float)o->v.k, (float)o->v.dx, (float)o->v.dy, index };
ui.damage = (Box){ lx0, ly0, lx1, ly1 };
canvas_set_clip(&o->c, pr);
- paint_root_clear(&o->c, root, 0x000000);
+ int taken = 0;
+ if (test_g0) taken = paint_root_g0(&o->c, root, &o->g);
+ else paint_root_clear(&o->c, root, 0x000000);
canvas_set_clip(&o->c, (Rect){ 0, 0, o->c.w, o->c.h });
ui.damage = (Box){ 0, 0, 0, 0 };
paint_view = (PaintView){ 0, 0, 1, 0, 0, 0 };
+ if (test_g0) g0_present(o, index, pr, taken);
+}
+
+/* main.c's frame: paint_ui (layout, damage, the layer's display: the one with most of the window), then
+ * paint_out on each display */
+static void drag_paint_n(Node *root, DOut *const *os, int n) {
+ if (!ui.dirty) return;
+ ui.dirty = 0;
+ layout_root(root, DW, DH);
+ Box d = { 0, 0, 0, 0 };
+ if (paint_damage(root)) d = ui.damage;
+ ui.damage = (Box){ 0, 0, 0, 0 };
+ if (paint_layer_node()) {
+ int b = 0;
+ Node *w = node_get(paint_layer_node());
+ if (n > 1 && w) {
+ ORect r[4];
+ for (int i = 0; i < n && i < 4; i++) r[i] = (ORect){ os[i]->v.x, os[i]->v.y, os[i]->v.w, os[i]->v.h };
+ b = out_best(r, n < 4 ? n : 4, w->prev_box.x0, w->prev_box.y0, w->prev_box.x1, w->prev_box.y1);
+ }
+ paint_layer_view(b >= 0 ? b : 0);
+ }
+ for (int i = 0; i < n; i++) drag_paint_out(root, os[i], i, d);
}
/* Pixels of the frame that differ from a full repaint of the tree (paint_root_full); *worst: the
@@ -1589,11 +1727,12 @@ static long drag_full_diff(Node *root, DOut *o, int *worst) {
paint_view = (PaintView){ 0, 0, 1, 0, 0, 0 };
term_at = keep;
long n = 0;
+ const uint32_t *px = test_g0 ? o->fb : o->px; /* G0: the frame as shown */
for (size_t i = 0; i < (size_t)o->c.w * o->c.h; i++) {
- if (o->px[i] == o->rpx[i]) continue;
+ if (px[i] == o->rpx[i]) continue;
n++;
for (int sh = 0; sh < 32; sh += 8) {
- int d = abs((int)((o->px[i] >> sh) & 255) - (int)((o->rpx[i] >> sh) & 255));
+ int d = abs((int)((px[i] >> sh) & 255) - (int)((o->rpx[i] >> sh) & 255));
if (d > *worst) *worst = d;
}
}
@@ -1641,7 +1780,7 @@ static DScene drag_scene(int dc, float x, float y) {
if (feat & DF_ANC) {
wins = box_in(s.root, 0, 0, DW, DH, 0);
ss(wins, "overflow", "hidden");
- sn(wins, "borderRadius", 24);
+ sn(wins, "borderRadius", dc == DC_ANCEDGE ? 96 : 24); /* ancedge: its corner reaches the window */
sn(wins, "borderWidth", 3);
sn(wins, "borderColor", 0x80ffffff);
}
@@ -1763,12 +1902,22 @@ static DScene drag_scene(int dc, float x, float y) {
s.edge = box_in(s.win, 732, 120, 40, 40, 0xffbf5af2);
sn(s.edge, "borderRadius", 6);
}
- /* the Dock and the menu bar: shell layers above the windows, with backdrop blur */
+ if (dc == DC_CORNER) { /* after the window, no colour: only its corners */
+ Node *clip = box_in(s.root, x + 120, y + 80, 200, 150, 0);
+ ss(clip, "overflow", "hidden");
+ sn(clip, "borderRadius", 60);
+ }
+ /* the Dock and the menu bar: shell layers above the windows, with backdrop blur (not in ancedge:
+ * nothing painted after the window near the corner it hugs) */
+ if (dc == DC_ANCEDGE) return s;
Node *dockl = layer_node(s.root, 10, DW, DH);
Node *dock = box_in(dockl, (DW - 600) / 2, DH - 72, 600, 64, 0x80303034);
sn(dock, "borderRadius", 16);
sn(dock, "backdropBlur", 20);
for (int i = 0; i < 6; i++) dview(dock, 48, 48, accents[i], 12);
+ /* pause: 300 more nodes painted after the window, each in its own box below the Dock (LordOS's real
+ * menu bar and Dock are a few hundred; G0's list of them once held 128 and refused every frame) */
+ if (dc == DC_PAUSE) for (int i = 0; i < 300; i++) box_in(dockl, 12 + 3 * i, DH - 6, 2, 2, accents[i % 6]);
ss(dock, "flexDirection", "row"); ss(dock, "alignItems", "center"); ss(dock, "justifyContent", "center");
sn(dock, "gap", 12);
Node *barl = layer_node(s.root, 20, DW, DH);
@@ -1782,31 +1931,81 @@ static DScene drag_scene(int dc, float x, float y) {
return s;
}
-/* One run: case dc's scene, the drag (n frames), then the frame after it ends, on o (a display at
- * 100 %, or at 200 % for k2 and k2mix). hash gets n + 1 values; the largest count of pixels that differed
- * from a full repaint goes to *full, the largest difference of a channel to *worst. */
-static void drag_run(int dc, DOut *o, uint64_t *hash, long *full, int *worst) {
+/* What the GPU shows (test_compose): the app windows of the list (a solid colour each) below the UI
+ * texture, the texture blended over them (ONE, ONE_MINUS_SRC_ALPHA, rounded), and with G0 the window's
+ * rectangle written over both (blending off: its colour, opaque). */
+static int test_compose;
+static uint64_t composed_hash(DOut *o) {
+ int W = o->c.w, H = o->c.h;
+ const uint32_t *t = test_g0 ? o->tex : o->c.px; /* without G0 every painted pixel was uploaded */
+ uint32_t *f = o->cmp;
+ memset(f, 0, (size_t)W * H * 4);
+ const float k = (float)o->v.k;
+ for (int i = 0; i < nwdraw; i++) {
+ const WDraw *d = &wdraw[i];
+ int x0 = (int)floorf((d->x - o->v.x) * k), y0 = (int)floorf((d->y - o->v.y) * k);
+ int x1 = (int)ceilf((d->x + d->w - o->v.x) * k), y1 = (int)ceilf((d->y + d->h - o->v.y) * k);
+ for (int y = y0 < 0 ? 0 : y0; y < y1 && y < H; y++)
+ for (int x = x0 < 0 ? 0 : x0; x < x1 && x < W; x++) f[(size_t)y * W + x] = 0xff000000u | (0x406080u + 0x111111u * (uint32_t)d->id);
+ }
+ for (size_t i = 0; i < (size_t)W * H; i++) {
+ uint32_t s = t[i], d = f[i], ia = 255 - (s >> 24), o2 = 0xff000000u;
+ for (int sh = 0; sh < 24; sh += 8) {
+ uint32_t v = (s >> sh & 255) + ((d >> sh & 255) * ia + 127) / 255;
+ o2 |= (v > 255 ? 255 : v) << sh;
+ }
+ f[i] = o2;
+ }
+ if (test_g0 && o->g0on)
+ for (int y = o->g0w.y0; y < o->g0w.y1; y++)
+ for (int x = o->g0w.x0; x < o->g0w.x1; x++) f[(size_t)y * W + x] = 0xff000000u | (o->fb[(size_t)y * W + x] & 0xffffff);
+ Canvas c;
+ canvas_init(&c, f, W, H, W);
+ return frame_hash(&c);
+}
+
+/* The hash of what the displays show (with G0, the composed frames) */
+static uint64_t outs_hash(DOut *const *os, int nos) {
+ uint64_t h = 0;
+ for (int i = 0; i < nos; i++)
+ h = h * 1099511628211ull ^ (test_compose ? composed_hash(os[i]) : frame_hash(test_g0 ? &os[i]->fbc : &os[i]->c));
+ return h;
+}
+
+/* One run: case dc's scene, the drag (n frames), then the frame after it ends, on the displays os (one
+ * at 100 %, at 200 % for k2 and k2mix, two side by side for "two"). hash gets n + 1 values; the largest
+ * count of pixels that differed from a full repaint (the first display) goes to *full, the largest
+ * difference of a channel to *worst. */
+static void drag_run_n(int dc, DOut *const *os, int nos, uint64_t *hash, long *full, int *worst) {
const int n = dc_frames[dc];
+ DOut *o = os[0];
float x0 = (DW - 760) / 2, y0 = 28 + (DH - 28 - 480) / 2; /* scenePath: centered in the work area */
const float rx = (DW - 760) / 2 - 20, ry = (DH - 28 - 480) - 40;
if (dc == DC_NEG) { x0 = -200; y0 = -120; } /* partly off the display's top-left corner */
if (dc_feat[dc] & DF_SMALL) y0 += 54;
+ if (dc == DC_ANCEDGE) x0 = y0 = 26; /* its shadow over the container's rounded corner */
DScene s = drag_scene(dc, x0, y0);
term_cursor = 1;
term_at = (Box){ 0, 0, 0, 0 };
- canvas_set_clip(&o->c, (Rect){ 0, 0, o->c.w, o->c.h });
- canvas_clear(&o->c, 0x000000);
+ for (int i = 0; i < nos; i++) {
+ canvas_set_clip(&os[i]->c, (Rect){ 0, 0, os[i]->c.w, os[i]->c.h });
+ canvas_clear(&os[i]->c, 0x000000);
+ os[i]->g0on = 0;
+ }
+ g0tex.owner = -1;
damage_add((Box){ 0, 0, DW, DH });
ui.dirty = 1;
- drag_paint(s.root, o);
+ drag_paint_n(s.root, os, nos);
paint_layer_begin(s.win->id, 0); /* grab.c's setup */
float gx = x0, gy = y0, rot = 0;
*full = 0;
*worst = 0;
for (int f = 0; f < n; f++) {
float dx, dy;
- lbi_path(rx, ry, 6000, f * (1000.0 / 60), &dx, &dy);
+ int paused = dc == DC_PAUSE && f >= 30 && f < 60; /* nothing changes: no UI damage */
+ lbi_path(rx, ry, 6000, (paused ? 30 : f) * (1000.0 / 60), &dx, &dy);
if (dc == DC_NEG) { dx = 6 * f; dy = 4 * f; } /* its left edge crosses x = 0, its top y = 0 */
+ if (dc == DC_ANCEDGE) { dx = 0.5f * f; dy = 0.25f * f; }
float x = x0 + roundf(dx), y = y0 + roundf(dy); /* grab_motion */
if (x != gx || y != gy) {
gx = x; gy = y;
@@ -1815,8 +2014,8 @@ static void drag_run(int dc, DOut *o, uint64_t *hash, long *full, int *worst) {
node_moved(s.win);
ui.dirty = 1;
}
- rot = fmodf(rot + 6, 360); /* scene.jsx's square, every 16 ms */
- if (dc == DC_DRAG || dc == DC_NEG || dc == DC_K2 || dc >= DC_BELOW) js_set(s.spin, "rotate", rot); /* the others: their own */
+ if (!paused) rot = fmodf(rot + 6, 360); /* scene.jsx's square, every 16 ms */
+ if ((dc == DC_DRAG || dc == DC_NEG || dc == DC_K2 || dc >= DC_BELOW) && !paused) js_set(s.spin, "rotate", rot); /* the others: their own */
int feat = dc_feat[dc];
if ((feat & DF_BAR) && f % 3 == 1) js_set(s.fill, "width", 40 + (f * 37) % 260);
if ((feat & DF_BAR) && f % 30 == 15) node_set_text(s.btitle->first, (f / 30) & 1 ? "Files" : "Files, 3 items selected");
@@ -1826,7 +2025,7 @@ static void drag_run(int dc, DOut *o, uint64_t *hash, long *full, int *worst) {
if ((feat & DF_RAISE) && f == 40) js_set(s.below, "zIndex", 5);
if ((feat & DF_RAISE) && f == 80) js_set(s.below, "zIndex", 0);
if ((feat & DF_DOT) && f % 20 == 10) js_set(s.dot, "backgroundColor", (f / 20) & 1 ? 0xffff453a : 0xff64d2ff);
- if ((feat & DF_TERM) && f % 10 == 5 && term_at.x1 > term_at.x0) {
+ if ((feat & DF_TERM) && f % 10 == 5 && term_at.x1 > term_at.x0 && !paused) {
term_cursor = !term_cursor; /* the terminal below: outside damage */
damage_add((Box){ floorf(term_at.x0), floorf(term_at.y0 + 8 - 2), ceilf(term_at.x1), ceilf(term_at.y0 + 24 + 2) });
ui.dirty = 1;
@@ -1842,17 +2041,20 @@ static void drag_run(int dc, DOut *o, uint64_t *hash, long *full, int *worst) {
damage_add((Box){ floorf(term_at.x0), floorf(term_at.y0 + 8 - 2), ceilf(term_at.x1), ceilf(term_at.y0 + 24 + 2) });
ui.dirty = 1;
}
- drag_paint(s.root, o);
- hash[f] = frame_hash(&o->c);
- if (f % FULL_EVERY == FULL_EVERY - 1) { long d = drag_full_diff(s.root, o, worst); if (d > *full) *full = d; }
+ drag_paint_n(s.root, os, nos);
+ hash[f] = outs_hash(os, nos);
+ if (nos == 1 && f % FULL_EVERY == FULL_EVERY - 1) { long d = drag_full_diff(s.root, o, worst); if (d > *full) *full = d; }
}
paint_layer_end(); /* grab_finish */
ui.dirty = 1;
- drag_paint(s.root, o);
- hash[n] = frame_hash(&o->c);
- long d = drag_full_diff(s.root, o, worst);
- if (d > *full) *full = d;
+ drag_paint_n(s.root, os, nos);
+ hash[n] = outs_hash(os, nos);
+ if (nos == 1) {
+ long d = drag_full_diff(s.root, o, worst);
+ if (d > *full) *full = d;
+ }
}
+static void drag_run(int dc, DOut *o, uint64_t *hash, long *full, int *worst) { drag_run_n(dc, &o, 1, hash, full, worst); }
/* The golden file: lines "<architecture> <run> <frame> <hash>"; # starts a comment. */
static char *read_file(const char *path) {
@@ -1909,29 +2111,94 @@ static int gold_write(const char *path, const char *variant, const uint64_t *has
return 0;
}
+/* A display of pw × ph physical px for the drag runs (0: out of memory) */
+static int dout_init(DOut *o, OutView v) {
+ memset(o, 0, sizeof *o);
+ o->v = v;
+ size_t n = (size_t)v.pw * v.ph;
+ o->px = calloc(n, 4);
+ o->rpx = calloc(n, 4);
+ o->tex = calloc(n, 4);
+ o->fb = calloc(n, 4);
+ o->cmp = calloc(n, 4);
+ if (!o->px || !o->rpx || !o->tex || !o->fb || !o->cmp) return 0;
+ canvas_init(&o->c, o->px, v.pw, v.ph, v.pw);
+ canvas_init(&o->ref, o->rpx, v.pw, v.ph, v.pw);
+ canvas_init(&o->fbc, o->fb, v.pw, v.ph, v.pw);
+ return 1;
+}
+static void dout_free(DOut *o) { free(o->px); free(o->rpx); free(o->tex); free(o->fb); free(o->cmp); }
+
+/* G0's frames counted since the last call, printed for a run (some_g0: at least one is expected) */
+static void g0_report(const char *what, int some_g0) {
+ G0Stats g;
+ paint_g0_stats(&g);
+ printf("drag-exact %s: G0 composed %ld of %ld frames that used the copy (%ld presented), strips %ld px "
+ "(not: %ld layer, %ld clip, %ld ancestor, %ld app, %ld after, %ld post)\n", what, g.frames, g.tried, g0_quads,
+ g.strip_px, g.refused[G0R_LAYER], g.refused[G0R_CLIP], g.refused[G0R_ANCESTOR], g.refused[G0R_APP],
+ g.refused[G0R_AFTER], g.refused[G0R_POST]);
+ CHECK((g.frames > 0 || !some_g0) && g.frames == g0_quads, "drag-exact %s: G0 frames %ld, presented %ld", what, g.frames,
+ g0_quads);
+ g0_quads = 0;
+}
+
+/* The runs without golden hashes (pause, two displays): G0 must show what the GPU path shows, frame by
+ * frame (that path is the one the golden hashes check in the other runs). */
+static void test_g0_extra(void) {
+ DOut a, b, c;
+ int ok = dout_init(&a, out_view(0, 0, DW, DH, 100)) && dout_init(&b, out_view(0, 0, DW / 2, DH, 100)) &&
+ dout_init(&c, out_view(DW / 2, 0, DW / 2, DH, 100));
+ uint64_t *h0 = malloc(sizeof *h0 * 1024), *h1 = malloc(sizeof *h1 * 1024);
+ if (!ok || !h0 || !h1) { CHECK(0, "g0-extra: out of memory"); goto done; }
+ for (int dc = DC_N; dc < DC_ALL; dc++) {
+ DOut *two[2] = { &b, &c }, *one[1] = { &a };
+ DOut *const *os = dc == DC_TWO ? two : one;
+ int nos = dc == DC_TWO ? 2 : 1;
+ long full;
+ int worst;
+ test_gpu = 1;
+ test_g0 = 0;
+ test_compose = nos == 1;
+ drag_run_n(dc, os, nos, h0, &full, &worst);
+ test_g0 = 1;
+ G0Stats st;
+ paint_g0_stats(&st);
+ g0_quads = 0;
+ drag_run_n(dc, os, nos, h1, &full, &worst);
+ test_g0 = test_compose = 0;
+ int bad = 0, first = -1;
+ for (int f = 0; f <= dc_frames[dc]; f++) if (h0[f] != h1[f]) { if (first < 0) first = f; bad++; }
+ char what[32];
+ snprintf(what, sizeof what, "g0 %s", dc_name[dc]);
+ /* corner: the box is in every frame's damage, so G0 refuses them all (without that refusal the
+ * frames differ from the GPU path) */
+ g0_report(what, dc != DC_CORNER);
+ CHECK(!bad, "drag-exact g0, run %s: %d of %d frames differ from the GPU path (the first: frame %d)", dc_name[dc], bad,
+ dc_frames[dc] + 1, first);
+ }
+done:
+ test_gpu = 0;
+ dout_free(&a); dout_free(&b); dout_free(&c);
+ free(h0); free(h1);
+}
+
static void test_drag_exact(int write) {
- static const char *const variant[2] = { "cpu", "gpu" };
- DOut o = { 0 }, o2 = { 0 }; /* the display at 100 %, and at 200 % (DC_K2) */
- o.v = out_view(0, 0, DW, DH, 100);
- o2.v = out_view(0, 0, 2 * DW, 2 * DH, 200);
- o.px = calloc((size_t)DW * DH, 4);
- o.rpx = calloc((size_t)DW * DH, 4);
- o2.px = calloc((size_t)4 * DW * DH, 4);
- o2.rpx = calloc((size_t)4 * DW * DH, 4);
+ static const char *const variant[3] = { "cpu", "gpu", "g0" };
+ DOut o, o2; /* the display at 100 %, and at 200 % (DC_K2) */
+ int ok = dout_init(&o, out_view(0, 0, DW, DH, 100));
+ ok = dout_init(&o2, out_view(0, 0, 2 * DW, 2 * DH, 200)) && ok;
uint64_t *hash = malloc(sizeof *hash * (size_t)dc_hashes());
- if (!o.px || !o.rpx || !o2.px || !o2.rpx || !hash) {
+ if (!ok || !hash) {
CHECK(0, "drag-exact: out of memory");
- free(o.px); free(o.rpx); free(o2.px); free(o2.rpx); free(hash);
+ dout_free(&o); dout_free(&o2); free(hash);
return;
}
- canvas_init(&o.c, o.px, DW, DH, DW);
- canvas_init(&o.ref, o.rpx, DW, DH, DW);
- canvas_init(&o2.c, o2.px, 2 * DW, 2 * DH, 2 * DW);
- canvas_init(&o2.ref, o2.rpx, 2 * DW, 2 * DH, 2 * DW);
- for (int g = 0; g < 2; g++) {
- test_gpu = g;
+ /* g0: the GPU path with G0 (the window composited by canvas_g0_model), against the GPU's hashes */
+ for (int g = 0; g < (write ? 2 : 3); g++) {
+ test_gpu = g > 0;
+ test_g0 = g == 2;
char path[64];
- snprintf(path, sizeof path, "tests/drag_golden_%s.txt", variant[g]);
+ snprintf(path, sizeof path, "tests/drag_golden_%s.txt", variant[g == 2 ? 1 : g]);
long fullw[DC_N];
int worst[DC_N];
long parts = 0, renders = 0, blits = 0;
@@ -1941,7 +2208,10 @@ static void test_drag_exact(int write) {
BelowStats bs;
paint_below_stats(&bs);
paint_below_bad = paint_below_checked = 0;
- paint_below_verify = 1;
+ paint_below_verify = !test_g0; /* G0's frames leave the window to the GPU: the hashes check them */
+ G0Stats g0s;
+ paint_g0_stats(&g0s);
+ g0_quads = 0;
if (getenv("LORD_BELOW_SWITCH")) paint_below_switch = atoi(getenv("LORD_BELOW_SWITCH"));
for (int dc = 0; dc < DC_N; dc++) {
drag_run(dc, dc_k2(dc) ? &o2 : &o, hash + k, &fullw[dc], &worst[dc]);
@@ -1957,8 +2227,10 @@ static void test_drag_exact(int write) {
"%ld blur, %ld transform, %ld unreached, %ld cost), %ld pixels differ in %ld of them painted the old way\n",
variant[g], bs.full, bs.part, bs.switched, bs.refused[BR_ANCESTOR], bs.refused[BR_BLUR], bs.refused[BR_TRANSFORM],
bs.refused[BR_UNREACHED], bs.refused[BR_COST], paint_below_bad, paint_below_checked);
- CHECK(!paint_below_bad && paint_below_checked == bs.switched, "drag-exact %s: frames switched to the copy of what is below "
- "differ from painting everything: %ld pixels in %ld frames", variant[g], paint_below_bad, paint_below_checked);
+ if (!test_g0)
+ CHECK(!paint_below_bad && paint_below_checked == bs.switched, "drag-exact %s: frames switched to the copy of what is below "
+ "differ from painting everything: %ld pixels in %ld frames", variant[g], paint_below_bad, paint_below_checked);
+ else g0_report("g0", 1);
printf("drag-exact %s: most pixels differing from a full repaint (largest difference):", variant[g]);
for (int dc = 0; dc < DC_N; dc++) printf(" %s %ld (%d)", dc_name[dc], fullw[dc], worst[dc]);
printf("\n");
@@ -1985,12 +2257,11 @@ static void test_drag_exact(int write) {
}
free(file);
}
- test_gpu = 0;
- free(o.px);
- free(o.rpx);
- free(o2.px);
- free(o2.rpx);
+ test_gpu = test_g0 = 0;
+ dout_free(&o);
+ dout_free(&o2);
free(hash);
+ if (!write) test_g0_extra();
}
/* ---- A setStyle that only turns or scales a node (node_restyled's transform-only path) ----
@@ -2320,6 +2591,7 @@ int main(int argc, char **argv) {
test_logical();
test_transform_math();
test_transform_paint_hit();
+ test_transform_self_restyle();
test_layers();
test_backdrop_band();
test_multi_damage();