Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 63 additions & 6 deletions electron/gpuSwitches.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { describe, expect, it } from "vitest";

import { getGpuSwitches, shouldForceLinuxEgl } from "./gpuSwitches";
import {
getGpuSwitches,
getLinuxOzonePlatformOverride,
isHyprlandSession,
shouldForceLinuxEgl,
} from "./gpuSwitches";

describe("shouldForceLinuxEgl", () => {
it("does not force EGL in a Wayland session", () => {
Expand All @@ -21,24 +26,23 @@ describe("shouldForceLinuxEgl", () => {
).toBe(false);
});

it("falls back to Electron's ozone hint when OZONE_PLATFORM is invalid", () => {
it("ignores invalid OZONE_PLATFORM values", () => {
expect(
shouldForceLinuxEgl({
OZONE_PLATFORM: "auto",
ELECTRON_OZONE_PLATFORM_HINT: "wayland",
XDG_SESSION_TYPE: "x11",
}),
).toBe(false);
).toBe(true);
});

it("forces EGL in an X11 session", () => {
expect(shouldForceLinuxEgl({ XDG_SESSION_TYPE: "x11" })).toBe(true);
});

it("forces EGL when x11 is explicitly requested via Electron's ozone hint", () => {
it("forces EGL when X11 is explicitly requested via OZONE_PLATFORM", () => {
expect(
shouldForceLinuxEgl({
ELECTRON_OZONE_PLATFORM_HINT: "x11",
OZONE_PLATFORM: "x11",
WAYLAND_DISPLAY: "wayland-0",
}),
).toBe(true);
Expand All @@ -65,3 +69,56 @@ describe("getGpuSwitches", () => {
});
});
});

describe("isHyprlandSession", () => {
it("detects Hyprland from the compositor instance signature", () => {
expect(isHyprlandSession({ HYPRLAND_INSTANCE_SIGNATURE: "abc" })).toBe(true);
});

it("detects Hyprland from XDG_CURRENT_DESKTOP", () => {
expect(isHyprlandSession({ XDG_CURRENT_DESKTOP: "Hyprland" })).toBe(true);
expect(isHyprlandSession({ XDG_CURRENT_DESKTOP: "sway:Hyprland" })).toBe(true);
});

it("ignores non-Hyprland sessions", () => {
expect(isHyprlandSession({ XDG_CURRENT_DESKTOP: "GNOME" })).toBe(false);
});
});

describe("getLinuxOzonePlatformOverride", () => {
it("defaults Hyprland to X11/Ozone", () => {
expect(
getLinuxOzonePlatformOverride({
XDG_SESSION_TYPE: "wayland",
XDG_CURRENT_DESKTOP: "Hyprland",
}),
).toBe("x11");
});

it("does not override an explicit Wayland request via OZONE_PLATFORM", () => {
expect(
getLinuxOzonePlatformOverride({
XDG_CURRENT_DESKTOP: "Hyprland",
OZONE_PLATFORM: "wayland",
}),
).toBeNull();
});

it("does not override an explicit X11 request", () => {
expect(
getLinuxOzonePlatformOverride({
XDG_CURRENT_DESKTOP: "Hyprland",
OZONE_PLATFORM: "x11",
}),
).toBeNull();
});

it("does not affect other Linux desktops", () => {
expect(
getLinuxOzonePlatformOverride({
XDG_SESSION_TYPE: "wayland",
XDG_CURRENT_DESKTOP: "GNOME",
}),
).toBeNull();
});
});
28 changes: 23 additions & 5 deletions electron/gpuSwitches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ export interface GpuSwitches {
disableFeatures?: string[];
}

function normalizeLinuxWindowSystem(value: string | undefined): "wayland" | "x11" | null {
export type LinuxOzonePlatform = "wayland" | "x11";

function normalizeLinuxWindowSystem(value: string | undefined): LinuxOzonePlatform | null {
const normalized = value?.trim().toLowerCase();
if (normalized === "wayland" || normalized === "x11") {
return normalized;
Expand All @@ -13,13 +15,29 @@ function normalizeLinuxWindowSystem(value: string | undefined): "wayland" | "x11
return null;
}

function getForcedLinuxWindowSystem(env: NodeJS.ProcessEnv): "wayland" | "x11" | null {
return (
normalizeLinuxWindowSystem(env.OZONE_PLATFORM) ??
normalizeLinuxWindowSystem(env.ELECTRON_OZONE_PLATFORM_HINT)
function getForcedLinuxWindowSystem(env: NodeJS.ProcessEnv): LinuxOzonePlatform | null {
return normalizeLinuxWindowSystem(env.OZONE_PLATFORM);
}

export function isHyprlandSession(env: NodeJS.ProcessEnv): boolean {
const currentDesktop = env.XDG_CURRENT_DESKTOP?.toLowerCase() ?? "";
const desktopSession = env.DESKTOP_SESSION?.toLowerCase() ?? "";

return Boolean(
env.HYPRLAND_INSTANCE_SIGNATURE ||
currentDesktop.split(":").includes("hyprland") ||
desktopSession.includes("hyprland"),
);
}

export function getLinuxOzonePlatformOverride(env: NodeJS.ProcessEnv): LinuxOzonePlatform | null {
if (getForcedLinuxWindowSystem(env)) {
return null;
}

return isHyprlandSession(env) ? "x11" : null;
}

export function shouldForceLinuxEgl(env: NodeJS.ProcessEnv): boolean {
const forcedWindowSystem = getForcedLinuxWindowSystem(env);
if (forcedWindowSystem === "wayland") {
Expand Down
14 changes: 9 additions & 5 deletions electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
import { RECORDINGS_DIR } from "./appPaths";
import { showCursor } from "./cursorHider";
import { registerExtensionIpcHandlers } from "./extensions/extensionIpc";
import { getGpuSwitches } from "./gpuSwitches";
import { getGpuSwitches, getLinuxOzonePlatformOverride } from "./gpuSwitches";
import {
cleanupAllExportStreams,
cleanupNativeVideoExportSessions,
Expand All @@ -30,10 +30,7 @@ import {
import { ensureMediaServer } from "./mediaServer";
import { shouldGrantDisplayCapture, shouldGrantMediaPermission } from "./permissionPolicy";
import { ensurePackagedRendererServer, getPackagedRendererBaseUrl } from "./rendererServer";
import {
hardenWebContentsNavigation,
shouldHardenWebContentsType,
} from "./navigationPolicy";
import { hardenWebContentsNavigation, shouldHardenWebContentsType } from "./navigationPolicy";
import type { UpdateToastPayload } from "./updater";
import {
checkForAppUpdates,
Expand Down Expand Up @@ -89,6 +86,13 @@ app.on("web-contents-created", (_event, contents) => {
});

function configureGpuAccelerationSwitches() {
if (process.platform === "linux" && !app.commandLine.hasSwitch("ozone-platform")) {
const ozonePlatform = getLinuxOzonePlatformOverride(process.env);
if (ozonePlatform) {
app.commandLine.appendSwitch("ozone-platform", ozonePlatform);
}
}

const { useAngle, useGl, disableFeatures } = getGpuSwitches(process.platform, process.env);
if (useAngle) {
app.commandLine.appendSwitch("use-angle", useAngle);
Expand Down