Nacre/docs/engine/webkit-spike.md
astar76 63b39d7b7e webkit-spike: hidden run result, D-035..D-040, plans 08 WebKit engine S044-S055
GTK 4 host + transparent egui GLArea over WebKitGTK proven hidden (Xvfb + nested
niri): pick fall-through, real input and IME, egui menus over video, pacing equal
with and without the overlay. Spike fixes: IM client widget, wl-input Y flip,
SPIKE_EGUI=0 layout, URL trim.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 02:01:11 -06:00

101 lines
8.2 KiB
Markdown

# WebKit embedding spike (D-033)
## Result (2026-11-13): design (b) works
Nacre's chrome as a transparent egui `Gtk.GLArea` over WebKitGTK 6.0 views in a GTK 4 window holds up: egui draws
over a playing video, GTK's own pick sends pointer input to egui only where egui has something, everything else
reaches WebKit natively (click, wheel, right click, typing, IME), and egui costs almost nothing per frame. D-033
stands. The GPU path was not exercised (see Unproven).
### How it was run (D-034: hidden)
`kwin_wayland` is not installed (no sway/weston/cage either), so the hidden session is **Xvfb + a nested niri**:
niri's winit backend runs as an X11 client of Xvfb and gives a private Wayland socket with the same protocols the
user's niri has (wlr virtual pointer, input-method-v2, virtual keyboard, screencopy for `grim`). Scripts:
`spikes/webkit-egui/run/` (`nested.sh start|stop`, `spike.sh`, `seq*.sh`; outputs in `$SPIKE_RUN`, default
`/tmp/webkit-spike`). Quirks, all handled there:
- Xvfb has no DRI3, so the nested niri renders with llvmpipe and cannot import GPU dmabufs (it died on the first
WebKit frame). Clients run with software GL too: `LIBGL_ALWAYS_SOFTWARE=1 WEBKIT_DMABUF_RENDERER_FORCE_SHM=1
GSK_RENDERER=gl`. The nested output (`winit`, 1280x800 @ 60 Hz) caps painting at ~30 fps with llvmpipe.
- The nested winit output mirrors absolute virtual-pointer Y: `WL_INPUT_FLIP_Y=1` in `wl-input`.
- The Wayland socket path must be short (`SUN_LEN`): runtime dir `/run/user/<uid>/nsp`.
- `NIRI_SOCKET` from the user's session must be overridden, or `niri msg` talks to the real compositor.
- Private bus via `dbus-run-session`; `GTK_A11Y=none NO_AT_BRIDGE=1 GDK_DEBUG=no-portals` keep a11y/portal noise out.
### Checks (YouTube watch `aqz-KE-bpKQ`, Wikipedia, DuckDuckGo, example.com; real compositor input)
| Check | Result |
|---|---|
| egui flyout over a playing video, transparent elsewhere | yes (`docs/engine/spike/flyout.jpg`) |
| `pick` (GTK's hit test): toolbar, open flyout → `NacreEguiArea`; page, and page under a closed flyout → `WebKitWebView` | yes, all 8 points |
| real click on page | starts YouTube playback; clicks on recommendations navigate |
| real click on an egui flyout item over the page | "Zoom in" applied (zoom 1.1), page got nothing |
| wheel over page | `scrollY` 0 → 410 after 5 notches |
| right click on YouTube's video | YouTube's own player menu at the pointer (`ctxmenu.jpg`), as in Chromium |
| right click on a link elsewhere | WebKit `context-menu` signal (hit test gives the link) → egui menu at the pointer (`ctx_link.jpg`); its "Open link in new tab" opens tab 2 |
| typing (virtual keyboard) into the egui URL field + Enter | navigates |
| typing into a page textarea (DuckDuckGo) | `abc ` arrives |
| IME preedit + commit into a page textarea | native WebKit: `abc 日本語` (preedit visible in the value during composition) |
| IME into the egui URL field | first failed (no text-input-v3 enabled); fixed by `IMMulticontext::set_client_widget` (+ cursor location from egui's `platform_output.ime`); then preedit and commit reach egui (`ime_preedit_url.png`; CJK shows as boxes only because the spike's egui has no CJK font, Nacre's font setup has) |
| `window.open` | opens as a new tab through `create` with a related view; clicking tab 0 switches back |
| video | readyState 4; 628 frames, 0 dropped in 21 s of playback (YouTube served 30 fps) |
### Pacing and memory (one YouTube tab playing, 15 s window, software GL)
| | egui overlay on | `SPIKE_EGUI=0` |
|---|---|---|
| window frames / fps | 450 / 29.6 | 444 / 29.4 |
| frame interval p50 / p95 / max | 33 / 50 / 50 ms | 33 / 50 / 50 ms |
| egui renders in 15 s, render ms p50 / p95 | 65, 0.46 / 0.60 ms | — |
| video frames / dropped | 628 / 0 | 628 / 0 |
| tree PSS median / max (`sample.py`, ~21 processes incl. bwrap sandboxes) | 1310 / 1437 MB | 1287 / 1517 MB |
| tree CPU median (% of one core) | 204 % | 188 % |
egui only repaints on its own events (no repaint per video frame), so the overlay costs no extra frames, ~20 MB of PSS
and some llvmpipe compositing of the full-window GLArea texture (+16 % of a core in software; should be far less on
the GPU). Pacing is identical with and without the overlay.
### Fixed in the spike during the run
`wl-input` Y flip switch; IM client widget + cursor location; URL field input trimmed; with `SPIKE_EGUI=0` the
WebView stack is the overlay's child (a `Gtk.Fixed` child kept its 0x0 natural size).
### Unproven (carry into the 08 plans)
- **GPU path**: dmabuf frames from WebKit composited under the GLArea, 60 fps video with the overlay, VRAM. Needs a
run on the real GPU: visible on DP-1 when the user is awake (D-034), or `SMOKE_VISIBLE=1` in the ported harness.
- Web Inspector window, fullscreen, file chooser / dialogs, several windows, HiDPI scale > 1, drag and drop,
Ctrl+L/T capture keys while the page has focus (code present, not exercised).
- egui needs the GLArea to repaint on its own animations (`repaint_delay` timer, present) — the S039 motion work
relies on that; check animation smoothness once Nacre's chrome runs on it.
## Background
**Setup (session 1)**
- Read: CLAUDE.md, HANDOFF "Orchestrator resume (2026-11-13)", D-032, `engine-comparison.md`, `docs/engine/tune/`
(check jsons + samples), `tools/engine-test/webkit_shell.py`. Not yet read: `docs/engine/servo-removal.md` (baa3b7f;
4 open decisions: ad-block interception, cookie encryption, profile dir rename, Media Session/MPRIS).
- Tuning data, all-tabs-idle row (PSS / tree VRAM): base 2989 / 1562 MB; no-page-cache 2414 / 1019; mem500 2377 / 1139;
cache-viewer 2816 / 873; hw-never 2298 / 58 (JS checks fail: "Unsupported result type"); related 2933 / 1380.
Base WebKitGTK plays the YouTube watch page (t=14.8 s, readyState 4) in the tune runs; the blank tabs of the first
comparison run were a shell problem.
- System: `webkitgtk-6.0` 2.54.2, `gtk4` 4.24.1, `libadwaita` 1.10 installed. `wpewebkit` 2.54.2 in extra, **not
installed** (provides libWPEWebKit-2.0, no libwpe/wpebackend-fdo dependency = new WPE Platform API; needs the user's
`sudo pacman -S wpewebkit`, not asked yet). niri exposes `zwlr_virtual_pointer_manager_v1`,
`zwp_input_method_manager_v2`, `zwp_text_input_manager_v3`, `zwp_virtual_keyboard_manager_v1`; `wtype`, `grim` present.
- Crates (pinned in the spike): `gtk4 =0.11.5` (feature v4_20), `webkit6 =0.6.1` (feature v2_52; latest, no v2_54
feature), `egui`/`egui_glow =0.37.0`, `glow =0.17.0`, `wayland-client =0.31.15`, `wayland-protocols-wlr`/`-misc
=0.3.12`. No usable Rust crate for WPE Platform 2.x (would need gir/bindgen).
- Spike code `spikes/webkit-egui/` (own Cargo workspace and target/):
- `src/egui_area.rs`: `Gtk.GLArea` subclass painting egui with egui_glow (GL via eglGetProcAddress), transparent
clear; `contains()` takes the pointer only outside egui's page rect or over an egui Area, so GTK's pick falls
through to the WebView for page input; GTK motion/click/scroll/key controllers + `IMMulticontext` → egui events.
- `src/main.rs`: Gtk.Overlay { Gtk.Fixed { Gtk.Stack of WebViews moved/sized to egui's page rect }, EguiArea };
egui toolbar (tabs, back/forward/reload, URL field, ⋮ flyout over the page), egui context menu from WebKit's
`context-menu` signal, popups via `create` (related view → new tab), Web Inspector, zoom, capture-phase Ctrl+L/T.
D-032 knobs on by default (page cache off, 500 MB memory limit, DOCUMENT_VIEWER); env knobs in the file header.
Driver: `SPIKE_CMD=<file>` command file (open/go/tab/menu/js/shot/stats/pick/state/focus/sleep/mark/quit);
`stats` = GTK frame-clock pacing + egui render ms.
- `src/bin/wl-input.rs`: real compositor input for tests: `wl-input DP-1 move|click|scroll …`, `ime TEXT [secs]`
(test input method: preedit, then commit).
**Design leaning**: (b) GTK 4 host. WebKitGTK gives input, IME, scroll, dialogs, printing, Web Inspector natively, has
maintained Rust bindings, and egui keeps drawing all chrome through the overlay; winit is replaced by GTK events in
app.rs (egui-winit → own GTK→egui input glue as in egui_area.rs). WPE would need hand-written bindings, our own input
translation and frame import, and has no Inspector UI. Unproven until the spike runs: GLArea transparency over the
WebView, pick fall-through, frame pacing with video under the overlay.