diff --git a/apps/desktop/src/app/DesktopEarlyElectronStartup.test.ts b/apps/desktop/src/app/DesktopEarlyElectronStartup.test.ts index 6d40bd128b77f..93695bbfd1586 100644 --- a/apps/desktop/src/app/DesktopEarlyElectronStartup.test.ts +++ b/apps/desktop/src/app/DesktopEarlyElectronStartup.test.ts @@ -6,24 +6,28 @@ import { restoreEarlyLinuxDeviceScaleFactor, resolveEarlyLinuxPasswordStorePreference, resolveLinuxDisableFeaturesSwitch, + shouldDisableGpuOnNvidiaWayland, } from "./DesktopEarlyElectronStartup.ts"; describe("DesktopEarlyElectronStartup", () => { const joinPath = NodePath.posix.join; - it("reads the persisted linux password-store preference before Electron is ready", () => { - const preference = resolveEarlyLinuxPasswordStorePreference({ - env: { T3CODE_HOME: "/home/user/.t3-test" }, - homeDirectory: "/home/user", - joinPath, - readFileString: (path) => { - assert.equal(path, "/home/user/.t3-test/userdata/desktop-settings.json"); - return JSON.stringify({ linuxPasswordStore: "kwallet6" }); - }, - }); + it.each([undefined, false, true, null, "false"])( + "reads the persisted linux password-store preference with GPU setting %j", + (disableGpuAcceleration) => { + const preference = resolveEarlyLinuxPasswordStorePreference({ + env: { T3CODE_HOME: "/home/user/.t3-test" }, + homeDirectory: "/home/user", + joinPath, + readFileString: (path) => { + assert.equal(path, "/home/user/.t3-test/userdata/desktop-settings.json"); + return JSON.stringify({ linuxPasswordStore: "kwallet6", disableGpuAcceleration }); + }, + }); - assert.equal(preference, "kwallet6"); - }); + assert.equal(preference, "kwallet6"); + }, + ); it("accepts JSONC in the early desktop settings file", () => { const preference = resolveEarlyLinuxPasswordStorePreference({ @@ -196,4 +200,115 @@ describe("Linux device scale persistence failures", () => { assert.deepEqual(reads, []); assert.deepEqual(applied, []); }); + + describe("shouldDisableGpuOnNvidiaWayland", () => { + const base = { + ozonePlatform: "wayland", + homeDirectory: "/home/user", + joinPath: NodePath.posix.join, + existsSync: (path: string) => path === "/dev/nvidiactl", + readFileString: () => "{}", + hasSwitch: () => false, + }; + + it("is true on NVIDIA Wayland with no explicit GPU switch", () => { + assert.isTrue( + shouldDisableGpuOnNvidiaWayland({ ...base, env: { XDG_SESSION_TYPE: "wayland" } }), + ); + }); + + it("is false on X11", () => { + assert.isFalse( + shouldDisableGpuOnNvidiaWayland({ + ...base, + env: { XDG_SESSION_TYPE: "x11" }, + ozonePlatform: "x11", + }), + ); + }); + + it("respects an explicit --ozone-platform=x11 override in a Wayland session", () => { + assert.isFalse( + shouldDisableGpuOnNvidiaWayland({ + ...base, + env: { XDG_SESSION_TYPE: "wayland" }, + ozonePlatform: "x11", + }), + ); + }); + + it("respects an explicit --ozone-platform=wayland when the session variable is absent", () => { + assert.isTrue( + shouldDisableGpuOnNvidiaWayland({ ...base, env: {}, ozonePlatform: "wayland" }), + ); + }); + + it.each([null, "auto", "headless"])( + "does not infer Wayland from the environment for unresolved platform %s", + (ozonePlatform) => { + assert.isFalse( + shouldDisableGpuOnNvidiaWayland({ + ...base, + env: { XDG_SESSION_TYPE: "wayland", WAYLAND_DISPLAY: "wayland-0" }, + ozonePlatform, + }), + ); + }, + ); + + it("uses Electron's resolved Wayland backend without session or display variables", () => { + assert.isTrue(shouldDisableGpuOnNvidiaWayland({ ...base, env: {} })); + }); + + it("is false without NVIDIA devices", () => { + assert.isFalse( + shouldDisableGpuOnNvidiaWayland({ + ...base, + env: { XDG_SESSION_TYPE: "wayland" }, + existsSync: () => false, + }), + ); + }); + + it("is false when --disable-gpu or --enable-gpu was passed explicitly", () => { + for (const name of ["disable-gpu", "enable-gpu"]) { + assert.isFalse( + shouldDisableGpuOnNvidiaWayland({ + ...base, + env: { XDG_SESSION_TYPE: "wayland" }, + hasSwitch: (switchName) => switchName === name, + }), + ); + } + }); + + it("is false when the user opts out via disableGpuAcceleration=false", () => { + assert.isFalse( + shouldDisableGpuOnNvidiaWayland({ + ...base, + env: { XDG_SESSION_TYPE: "wayland" }, + readFileString: () => JSON.stringify({ disableGpuAcceleration: false }), + }), + ); + }); + + it("is true when the setting is explicit true or missing file", () => { + assert.isTrue( + shouldDisableGpuOnNvidiaWayland({ + ...base, + env: { XDG_SESSION_TYPE: "wayland" }, + readFileString: () => JSON.stringify({ disableGpuAcceleration: true }), + }), + ); + assert.isTrue( + shouldDisableGpuOnNvidiaWayland({ + ...base, + env: { XDG_SESSION_TYPE: "wayland" }, + readFileString: () => { + throw new Error("missing"); + }, + }), + ); + }); + }); }); diff --git a/apps/desktop/src/app/DesktopEarlyElectronStartup.ts b/apps/desktop/src/app/DesktopEarlyElectronStartup.ts index 3c571bcf02b1f..86ba3a006ff8f 100644 --- a/apps/desktop/src/app/DesktopEarlyElectronStartup.ts +++ b/apps/desktop/src/app/DesktopEarlyElectronStartup.ts @@ -40,6 +40,7 @@ const trimNonEmpty = (value: string | undefined): string | null => { const EarlyDesktopSettingsJson = fromLenientJson( Schema.Struct({ + disableGpuAcceleration: Schema.optionalKey(Schema.Unknown), linuxPasswordStore: Schema.optionalKey(Schema.Unknown), }), ); @@ -80,6 +81,33 @@ export function resolveEarlyLinuxPasswordStorePreference( } } +const isNvidiaPresent = (existsSync: (path: string) => boolean): boolean => + existsSync("/dev/nvidiactl") || existsSync("/dev/nvidia0"); + +export function shouldDisableGpuOnNvidiaWayland(input: { + readonly env: NodeJS.ProcessEnv; + readonly ozonePlatform: string | null; + readonly homeDirectory: string; + readonly joinPath: JoinPath; + readonly existsSync: (path: string) => boolean; + readonly readFileString: (path: string) => string; + readonly hasSwitch: (name: string) => boolean; +}): boolean { + // Electron resolves --ozone-platform before loading the main module, including auto selection. + if (input.ozonePlatform !== "wayland") return false; + if (input.hasSwitch("disable-gpu") || input.hasSwitch("enable-gpu")) return false; + const settingsPath = resolveEarlyDesktopSettingsPath({ + env: input.env, + homeDirectory: input.homeDirectory, + joinPath: input.joinPath, + }); + try { + const parsed = decodeEarlyDesktopSettingsJson(input.readFileString(settingsPath)); + if (parsed.disableGpuAcceleration === false) return false; + } catch {} + return isNvidiaPresent(input.existsSync); +} + const WAYLAND_COLOR_MANAGER_FEATURE = "WaylandWpColorManagerV1"; const featureListNames = (value: string | null): ReadonlyArray => diff --git a/apps/desktop/src/app/DesktopPreReadyPlatform.test.ts b/apps/desktop/src/app/DesktopPreReadyPlatform.test.ts index 45887a928abb7..1737669f3f25d 100644 --- a/apps/desktop/src/app/DesktopPreReadyPlatform.test.ts +++ b/apps/desktop/src/app/DesktopPreReadyPlatform.test.ts @@ -16,6 +16,7 @@ const { copyFileSyncMock, execFileSyncMock, readFileSyncMock, + existsSyncMock, symlinkSyncMock, } = vi.hoisted(() => ({ appendSwitchMock: vi.fn(), @@ -28,6 +29,7 @@ const { copyFileSyncMock: vi.fn(), execFileSyncMock: vi.fn(), readFileSyncMock: vi.fn(), + existsSyncMock: vi.fn(), symlinkSyncMock: vi.fn(), })); @@ -53,6 +55,7 @@ vi.mock("node:fs", () => ({ mkdirSync: mkdirSyncMock, writeFileSync: writeFileSyncMock, copyFileSync: copyFileSyncMock, + existsSync: existsSyncMock, symlinkSync: symlinkSyncMock, })); @@ -75,9 +78,81 @@ describe("DesktopPreReadyPlatform", () => { copyFileSyncMock.mockReset(); execFileSyncMock.mockReset(); readFileSyncMock.mockReset().mockReturnValue("{}"); + existsSyncMock.mockReset().mockReturnValue(false); symlinkSyncMock.mockReset(); }); + it.effect.each([ + { settings: "{}", label: "disables GPU on NVIDIA Wayland", expected: true }, + { + settings: JSON.stringify({ disableGpuAcceleration: false }), + label: "honors disableGpuAcceleration:false opt-out", + expected: false, + }, + ])("pre-ready GPU switch: $label", ({ settings, expected }) => { + vi.stubEnv("T3CODE_HOME", "/isolated"); + vi.stubEnv("VITE_DEV_SERVER_URL", ""); + vi.stubEnv("XDG_SESSION_TYPE", "wayland"); + vi.stubEnv("XDG_DATA_HOME", "/xdg"); + vi.stubEnv("APPIMAGE", "/Applications/current.AppImage"); + existsSyncMock.mockReturnValue(true); + readFileSyncMock.mockReturnValue(settings); + getSwitchValueMock.mockImplementation((name: string) => + name === "ozone-platform" ? "wayland" : "", + ); + hasSwitchMock.mockImplementation((name: string) => name === "ozone-platform"); + + return Effect.scoped( + Effect.gen(function* () { + yield* Layer.build( + DesktopPreReadyPlatform.layer.pipe( + Layer.provide(Layer.succeed(HostProcess.Platform, "linux")), + ), + ); + const appended = appendSwitchMock.mock.calls.some(([name]) => name === "disable-gpu"); + assert.equal(appended, expected); + }), + ).pipe(Effect.ensuring(Effect.sync(() => vi.unstubAllEnvs()))); + }); + + it.effect.each([ + { session: "wayland", platform: "wayland", hint: "x11", expected: true }, + { session: "x11", platform: "x11", hint: "wayland", expected: false }, + ])( + "pre-ready GPU switch: ignores stale hint=$hint on $session", + ({ session, platform, hint, expected }) => { + vi.stubEnv("T3CODE_HOME", "/isolated"); + vi.stubEnv("VITE_DEV_SERVER_URL", ""); + vi.stubEnv("XDG_SESSION_TYPE", session); + vi.stubEnv("XDG_DATA_HOME", "/xdg"); + vi.stubEnv("APPIMAGE", "/Applications/current.AppImage"); + existsSyncMock.mockReturnValue(true); + readFileSyncMock.mockReturnValue("{}"); + getSwitchValueMock.mockImplementation((name: string) => + name === "ozone-platform" ? platform : name === "ozone-platform-hint" ? hint : "", + ); + hasSwitchMock.mockImplementation( + (name: string) => name === "ozone-platform" || name === "ozone-platform-hint", + ); + + return Effect.scoped( + Effect.gen(function* () { + yield* Layer.build( + DesktopPreReadyPlatform.layer.pipe( + Layer.provide(Layer.succeed(HostProcess.Platform, "linux")), + ), + ); + const appended = appendSwitchMock.mock.calls.some(([name]) => name === "disable-gpu"); + assert.equal(appended, expected); + assert.isFalse( + getSwitchValueMock.mock.calls.some(([name]) => name === "ozone-platform-hint"), + ); + assert.isFalse(hasSwitchMock.mock.calls.some(([name]) => name === "ozone-platform-hint")); + }), + ).pipe(Effect.ensuring(Effect.sync(() => vi.unstubAllEnvs()))); + }, + ); + it.effect("persists Linux scaling synchronously across relaunches and settings rewrites", () => { vi.stubEnv("T3CODE_HOME", "/isolated"); vi.stubEnv("VITE_DEV_SERVER_URL", ""); diff --git a/apps/desktop/src/app/DesktopPreReadyPlatform.ts b/apps/desktop/src/app/DesktopPreReadyPlatform.ts index 8ac4654636ab6..c2d69def65295 100644 --- a/apps/desktop/src/app/DesktopPreReadyPlatform.ts +++ b/apps/desktop/src/app/DesktopPreReadyPlatform.ts @@ -161,6 +161,19 @@ export const make = Effect.gen(function* () { } catch {} // Chromium caches portal identity; set it before any asynchronous initialization. Electron.app.setDesktopName(linux.linuxDesktopEntryName); + if ( + DesktopEarlyElectronStartup.shouldDisableGpuOnNvidiaWayland({ + env: process.env, + ozonePlatform: readCommandLineSwitchValue(Electron.app.commandLine, "ozone-platform"), + homeDirectory, + joinPath: NodePath.posix.join, + existsSync: (path) => NodeFS.existsSync(path), + readFileString: (path) => NodeFS.readFileSync(path, "utf8"), + hasSwitch: (name) => Electron.app.commandLine.hasSwitch(name), + }) + ) { + Electron.app.commandLine.appendSwitch("disable-gpu"); + } if (linux.passwordStore !== null && linuxPasswordStoreCommandLine === null) { Electron.app.commandLine.appendSwitch("password-store", linux.passwordStore); } diff --git a/apps/desktop/src/settings/DesktopAppSettings.test.ts b/apps/desktop/src/settings/DesktopAppSettings.test.ts index 04406f63c6b3d..c3876a9c53ae8 100644 --- a/apps/desktop/src/settings/DesktopAppSettings.test.ts +++ b/apps/desktop/src/settings/DesktopAppSettings.test.ts @@ -147,6 +147,31 @@ describe("DesktopSettings", () => { ), ); + it.effect("preserves disableGpuAcceleration:false across unrelated settings writes", () => + withSettings( + Effect.gen(function* () { + const environment = yield* DesktopEnvironment.DesktopEnvironment; + const fileSystem = yield* FileSystem.FileSystem; + const settings = yield* DesktopAppSettings.DesktopAppSettings; + yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true }); + yield* fileSystem.writeFileString( + environment.desktopSettingsPath, + `${JSON.stringify({ disableGpuAcceleration: false })}\n`, + ); + assert.equal((yield* settings.load).disableGpuAcceleration, false); + yield* settings.setMainWindowBounds({ x: 10, y: 20, width: 900, height: 600 }, true); + yield* settings.setServerExposureMode("network-accessible"); + const reloaded = yield* settings.load; + assert.equal(reloaded.disableGpuAcceleration, false); + assert.equal(reloaded.serverExposureMode, "network-accessible"); + const raw = yield* fileSystem.readFileString(environment.desktopSettingsPath); + assert.include(raw, `"disableGpuAcceleration":false`); + yield* settings.setWslBackendEnabled(true); + assert.equal((yield* settings.load).disableGpuAcceleration, false); + }), + ), + ); + it.effect("falls back to defaults when the settings file is malformed", () => withSettings( Effect.gen(function* () { @@ -223,6 +248,7 @@ describe("DesktopSettings", () => { ); assert.deepEqual(yield* settings.load, { + disableGpuAcceleration: undefined, linuxPasswordStore: "auto", localEnvironmentEnabled: true, mainWindowBounds: { x: 120, y: 80, width: 1280, height: 900 }, @@ -376,6 +402,7 @@ describe("DesktopSettings", () => { }); assert.deepEqual(yield* settings.load, { + disableGpuAcceleration: undefined, linuxPasswordStore: "auto", localEnvironmentEnabled: true, mainWindowBounds: null, @@ -405,6 +432,7 @@ describe("DesktopSettings", () => { }); assert.deepEqual(yield* settings.load, { + disableGpuAcceleration: undefined, linuxPasswordStore: "auto", localEnvironmentEnabled: true, mainWindowBounds: null, diff --git a/apps/desktop/src/settings/DesktopAppSettings.ts b/apps/desktop/src/settings/DesktopAppSettings.ts index e81da70660648..612c163c876c5 100644 --- a/apps/desktop/src/settings/DesktopAppSettings.ts +++ b/apps/desktop/src/settings/DesktopAppSettings.ts @@ -28,6 +28,7 @@ import { desktopUpdateChannelOf } from "@t3tools/shared/cliRelease"; import { isValidDistroName } from "../wsl/wslPathParsing.ts"; export interface DesktopSettings { + readonly disableGpuAcceleration: boolean | undefined; readonly localEnvironmentEnabled: boolean; readonly linuxPasswordStore: LinuxPasswordStorePreference; readonly mainWindowBounds: DesktopWindowBounds | null; @@ -69,6 +70,7 @@ export const DEFAULT_MAIN_WINDOW_SIZE = { } as const; export const DEFAULT_DESKTOP_SETTINGS: DesktopSettings = { + disableGpuAcceleration: undefined, localEnvironmentEnabled: true, linuxPasswordStore: DEFAULT_LINUX_PASSWORD_STORE, mainWindowBounds: null, @@ -96,6 +98,7 @@ const optionalReadableKey = (schema: S) => Schema.optionalKey(schema.pipe(Schema.catchDecoding(() => Effect.succeedNone))); const DesktopSettingsDocument = Schema.Struct({ + disableGpuAcceleration: optionalReadableKey(Schema.Boolean), localEnvironmentEnabled: optionalReadableKey(Schema.Boolean), linuxPasswordStore: optionalReadableKey(Schema.Unknown), mainWindowBounds: optionalReadableKey(Schema.NullOr(DesktopWindowBoundsDocument)), @@ -225,6 +228,7 @@ function normalizeDesktopSettingsDocument( (parsed.wslBackendEnabled === undefined && parsed.wslMode === "wsl"); return { + disableGpuAcceleration: parsed.disableGpuAcceleration, localEnvironmentEnabled: parsed.localEnvironmentEnabled !== false, linuxPasswordStore: normalizeLinuxPasswordStorePreference(parsed.linuxPasswordStore), mainWindowBounds, @@ -249,6 +253,9 @@ function toDesktopSettingsDocument( ): DesktopSettingsDocument { const document: Mutable = {}; + if (settings.disableGpuAcceleration !== defaults.disableGpuAcceleration) { + document.disableGpuAcceleration = settings.disableGpuAcceleration as boolean; + } if (settings.localEnvironmentEnabled !== defaults.localEnvironmentEnabled) { document.localEnvironmentEnabled = settings.localEnvironmentEnabled; }