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
57 changes: 9 additions & 48 deletions electron/gpuSwitches.test.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,27 @@
import { describe, expect, it } from "vitest";

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

describe("shouldForceLinuxEgl", () => {
it("does not force EGL in a Wayland session", () => {
expect(
shouldForceLinuxEgl({
XDG_SESSION_TYPE: "wayland",
WAYLAND_DISPLAY: "wayland-0",
}),
).toBe(false);
});

it("does not force EGL when Wayland is explicitly requested via Ozone", () => {
expect(
shouldForceLinuxEgl({
OZONE_PLATFORM: "wayland",
XDG_SESSION_TYPE: "x11",
}),
).toBe(false);
});

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

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", () => {
expect(
shouldForceLinuxEgl({
ELECTRON_OZONE_PLATFORM_HINT: "x11",
WAYLAND_DISPLAY: "wayland-0",
}),
).toBe(true);
});
});
import { getGpuSwitches } from "./gpuSwitches";

describe("getGpuSwitches", () => {
it("returns the Linux VAAPI workaround without forcing EGL on Wayland", () => {
it("returns the Linux VAAPI workaround without forcing a GL implementation on Wayland", () => {
expect(
getGpuSwitches("linux", {
XDG_SESSION_TYPE: "wayland",
WAYLAND_DISPLAY: "wayland-0",
}),
).toEqual({
useGl: undefined,
disableFeatures: ["VaapiVideoDecoder", "VaapiVideoEncoder"],
});
});

it("returns the X11 EGL workaround on Linux X11", () => {
it("does not force a GL implementation on Linux X11", () => {
expect(getGpuSwitches("linux", { XDG_SESSION_TYPE: "x11" })).toEqual({
useGl: "egl",
disableFeatures: ["VaapiVideoDecoder", "VaapiVideoEncoder"],
});
});

it("does not force a GL implementation when the session type is unknown", () => {
expect(getGpuSwitches("linux", {})).toEqual({
disableFeatures: ["VaapiVideoDecoder", "VaapiVideoEncoder"],
});
});
Expand Down
42 changes: 3 additions & 39 deletions electron/gpuSwitches.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,11 @@
export interface GpuSwitches {
useAngle?: string;
useGl?: string;
disableFeatures?: string[];
}

function normalizeLinuxWindowSystem(value: string | undefined): "wayland" | "x11" | null {
const normalized = value?.trim().toLowerCase();
if (normalized === "wayland" || normalized === "x11") {
return normalized;
}

return null;
}

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

export function shouldForceLinuxEgl(env: NodeJS.ProcessEnv): boolean {
const forcedWindowSystem = getForcedLinuxWindowSystem(env);
if (forcedWindowSystem === "wayland") {
return false;
}
if (forcedWindowSystem === "x11") {
return true;
}

const sessionType = env.XDG_SESSION_TYPE?.toLowerCase();
if (sessionType === "wayland") {
return false;
}
if (sessionType === "x11") {
return true;
}

return !env.WAYLAND_DISPLAY;
}

export function getGpuSwitches(
platform: NodeJS.Platform,
env: NodeJS.ProcessEnv = process.env,
_env: NodeJS.ProcessEnv = process.env,
): GpuSwitches {
if (platform === "darwin") {
return {
Expand All @@ -56,8 +19,9 @@ export function getGpuSwitches(
}

if (platform === "linux") {
// No use-gl override: Chromium only allows (gl=egl-angle, angle=default) on Linux now.
// Passing use-gl=egl requests (gl=egl-gles2, angle=none) and kills the GPU process.
return {
useGl: shouldForceLinuxEgl(env) ? "egl" : undefined,
disableFeatures: ["VaapiVideoDecoder", "VaapiVideoEncoder"],
};
}
Expand Down
6 changes: 2 additions & 4 deletions electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,11 @@ app.on("web-contents-created", (_event, contents) => {
});

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

if (disableFeatures && disableFeatures.length > 0) {
app.commandLine.appendSwitch("disable-features", disableFeatures.join(","));
}
Expand Down