Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
gui: on Linux without a GPU WebKitGTK draws without accelerated compositing (#2827)
yetone2d12ce98be66
diff --git a/internal/gui/webkit_linux.go b/internal/gui/webkit_linux.goindex bbc357c70..3fd73c5d6 100644--- a/internal/gui/webkit_linux.go+++ b/internal/gui/webkit_linux.go@@ -1,6 +1,9 @@package gui-import "os"+import (+ "os"+ "path/filepath"+)// webkitDefaults sets how WebKitGTK draws the pages before the app makes// its first webview. Under Wayland its accelerated compositing flickers the@@ -9,16 +12,48 @@ import "os"// it off the pages draw steadily and, as they said, smoother. A user who// set either variable keeps what they set; WEBKIT_DISABLE_COMPOSITING_MODE=0// turns compositing back on.+//+// Under X11 with no GPU this process can draw with — an xrdp, VNC or Xvfb+// session, or a user outside the render group — the compositing falls back+// to Mesa's llvmpipe, which rasterises and blends every layer on the CPU+// with a thread per core: the Routing page took 459%–830% CPU there, and+// 23% with compositing off, looking the same (#2827, zhang-4411). So with+// no render node this process can open, compositing is off too.func webkitDefaults() {- webkitDefaultsFor(os.Getenv, os.Setenv)+ webkitDefaultsFor(os.Getenv, os.Setenv, gpuReachable)}-func webkitDefaultsFor(getenv func(string) string, setenv func(string, string) error) {- if getenv("XDG_SESSION_TYPE") != "wayland" && getenv("WAYLAND_DISPLAY") == "" {+func webkitDefaultsFor(getenv func(string) string, setenv func(string, string) error, gpu func() bool) {+ if getenv("WEBKIT_DISABLE_COMPOSITING_MODE") != "" || getenv("WEBKIT_DISABLE_DMABUF_RENDERER") != "" {return}- if getenv("WEBKIT_DISABLE_COMPOSITING_MODE") != "" || getenv("WEBKIT_DISABLE_DMABUF_RENDERER") != "" {+ wayland := getenv("XDG_SESSION_TYPE") == "wayland" || getenv("WAYLAND_DISPLAY") != ""+ if !wayland && gpu() {return}setenv("WEBKIT_DISABLE_COMPOSITING_MODE", "1")}++// gpuReachable reports whether this process can open a DRM render node,+// which is what EGL needs to draw on a GPU: none there, or none this user+// may open (libEGL's "failed to open /dev/dri/renderD128: Permission+// denied"), and WebKit's GL is software.+func gpuReachable() bool {+ nodes, _ := filepath.Glob("/dev/dri/renderD*")+ return renderNodeOpens(nodes, func(p string) error {+ f, err := os.OpenFile(p, os.O_RDWR, 0)+ if err == nil {+ f.Close()+ }+ return err+ })+}++func renderNodeOpens(nodes []string, open func(string) error) bool {+ for _, n := range nodes {+ if open(n) == nil {+ return true+ }+ }+ return false+}diff --git a/internal/gui/webkit_linux_test.go b/internal/gui/webkit_linux_test.goindex 0d3e3c638..5dff30519 100644--- a/internal/gui/webkit_linux_test.go+++ b/internal/gui/webkit_linux_test.go@@ -1,28 +1,63 @@package gui-import "testing"+import (+ "errors"+ "testing"+)// Under Wayland the pages are drawn without accelerated compositing, which-// flickered (myxxts); X11 is left alone, and so is a user's own setting.+// flickered (myxxts); under X11 too when no GPU can be reached, where+// compositing fell back to llvmpipe and took 459%–830% CPU (#2827). X11+// with a GPU is left alone, and so is a user's own setting.func TestWebkitDefaults(t *testing.T) {for _, c := range []struct {name stringenv map[string]string+ gpu boolwant string}{- {"wayland", map[string]string{"XDG_SESSION_TYPE": "wayland"}, "1"},- {"wayland display only", map[string]string{"WAYLAND_DISPLAY": "wayland-0"}, "1"},- {"x11", map[string]string{"XDG_SESSION_TYPE": "x11"}, ""},- {"user turned it on again", map[string]string{"XDG_SESSION_TYPE": "wayland", "WEBKIT_DISABLE_COMPOSITING_MODE": "0"}, "0"},- {"user chose dmabuf off", map[string]string{"XDG_SESSION_TYPE": "wayland", "WEBKIT_DISABLE_DMABUF_RENDERER": "1"}, ""},+ {"wayland", map[string]string{"XDG_SESSION_TYPE": "wayland"}, true, "1"},+ {"wayland display only", map[string]string{"WAYLAND_DISPLAY": "wayland-0"}, true, "1"},+ {"x11", map[string]string{"XDG_SESSION_TYPE": "x11"}, true, ""},+ {"x11 without a gpu (xrdp)", map[string]string{"XDG_SESSION_TYPE": "x11", "DISPLAY": ":10"}, false, "1"},+ {"user turned it on again", map[string]string{"XDG_SESSION_TYPE": "wayland", "WEBKIT_DISABLE_COMPOSITING_MODE": "0"}, true, "0"},+ {"user turned it on again without a gpu", map[string]string{"XDG_SESSION_TYPE": "x11", "WEBKIT_DISABLE_COMPOSITING_MODE": "0"}, false, "0"},+ {"user chose dmabuf off", map[string]string{"XDG_SESSION_TYPE": "wayland", "WEBKIT_DISABLE_DMABUF_RENDERER": "1"}, true, ""},+ {"user chose dmabuf off without a gpu", map[string]string{"XDG_SESSION_TYPE": "x11", "WEBKIT_DISABLE_DMABUF_RENDERER": "1"}, false, ""},} {env := map[string]string{}for k, v := range c.env {env[k] = v}- webkitDefaultsFor(func(k string) string { return env[k] }, func(k, v string) error { env[k] = v; return nil })+ webkitDefaultsFor(func(k string) string { return env[k] }, func(k, v string) error { env[k] = v; return nil }, func() bool { return c.gpu })if got := env["WEBKIT_DISABLE_COMPOSITING_MODE"]; got != c.want {t.Errorf("%s: WEBKIT_DISABLE_COMPOSITING_MODE=%q, want %q", c.name, got, c.want)}}}++// A GPU is reachable when one render node opens: none there, or each one+// refused (the xrdp user outside the render group), is software drawing.+func TestRenderNodeOpens(t *testing.T) {+ denied := errors.New("permission denied")+ for _, c := range []struct {+ name string+ nodes []string+ ok map[string]bool+ want bool+ }{+ {"no render node", nil, nil, false},+ {"every node refused", []string{"/dev/dri/renderD128", "/dev/dri/renderD129"}, nil, false},+ {"the second node opens", []string{"/dev/dri/renderD128", "/dev/dri/renderD129"}, map[string]bool{"/dev/dri/renderD129": true}, true},+ } {+ got := renderNodeOpens(c.nodes, func(p string) error {+ if c.ok[p] {+ return nil+ }+ return denied+ })+ if got != c.want {+ t.Errorf("%s: renderNodeOpens = %v, want %v", c.name, got, c.want)+ }+ }+}