Skip to content

Commit

Permalink
fix(puppet): enable gpu by default
Browse files Browse the repository at this point in the history
  • Loading branch information
blakebyrnes committed Mar 26, 2021
1 parent 67d3e18 commit 9a06165
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
19 changes: 18 additions & 1 deletion core/lib/GlobalPool.ts
Expand Up @@ -5,7 +5,7 @@ import { createPromise } from '@secret-agent/commons/utils';
import Log from '@secret-agent/commons/Logger';
import { MitmProxy as MitmServer } from '@secret-agent/mitm';
import ICreateSessionOptions from '@secret-agent/core-interfaces/ICreateSessionOptions';
import Puppet from '@secret-agent/puppet';
import Puppet, { ILaunchArgs } from '@secret-agent/puppet';
import * as Os from 'os';
import IBrowserEngine from '@secret-agent/core-interfaces/IBrowserEngine';
import DefaultBrowser from '@secret-agent/emulate-chrome-83';
Expand All @@ -28,6 +28,7 @@ export default class GlobalPool {
return this.activeSessionCount < GlobalPool.maxConcurrentAgentsCount;
}

private static defaultLaunchArgs: ILaunchArgs;
private static _activeSessionCount = 0;
private static puppets: Puppet[] = [];
private static mitmServer: MitmServer;
Expand Down Expand Up @@ -103,11 +104,27 @@ export default class GlobalPool {
return existing;
}

if (!this.defaultLaunchArgs) {
this.defaultLaunchArgs = {
showBrowser: Boolean(JSON.parse(process.env.SA_SHOW_BROWSER ?? 'false')),
disableDevtools: Boolean(JSON.parse(process.env.SA_DISABLE_DEVTOOLS ?? 'false')),
noChromeSandbox: Boolean(JSON.parse(process.env.SA_NO_CHROME_SANDBOX ?? 'false')),
disableGpu: Boolean(JSON.parse(process.env.SA_DISABLE_GPU ?? 'false')),
};
}

// if showing all browsers, make sure to set on the engine
if (!engine.isHeaded && this.defaultLaunchArgs.showBrowser) {
engine.isHeaded = true;
}

const puppet = new Puppet(engine);
this.puppets.push(puppet);

const browserOrError = await puppet.start({
...this.defaultLaunchArgs,
proxyPort: this.mitmServer.port,
showBrowser: engine.isHeaded,
});
if (browserOrError instanceof Error) throw browserOrError;
return puppet;
Expand Down
29 changes: 20 additions & 9 deletions puppet-chrome/index.ts
Expand Up @@ -11,7 +11,13 @@ import { Connection } from './lib/Connection';
let counter = 0;

const PuppetLauncher: IPuppetLauncher = {
getLaunchArgs(options: { proxyPort?: number; showBrowser?: boolean }) {
getLaunchArgs(options: {
proxyPort?: number;
showBrowser?: boolean;
disableGpu?: boolean;
disableDevtools?: boolean;
disableSandbox?: boolean;
}) {
const chromeArguments = [...defaultArgs];
if (!options.showBrowser) {
chromeArguments.push(
Expand All @@ -25,15 +31,20 @@ const PuppetLauncher: IPuppetLauncher = {
`--user-data-dir=${Path.join(os.tmpdir(), 'chromium-headed-data', String((counter += 1)))}`,
); // required to allow multiple browsers to be headed

const shouldDisableDevtools = Boolean(JSON.parse(process.env.SA_DISABLE_DEVTOOLS ?? 'false'));
if (!shouldDisableDevtools) chromeArguments.push('--auto-open-devtools-for-tabs');
if (!options.disableDevtools) chromeArguments.push('--auto-open-devtools-for-tabs');
}

if (options.proxyPort !== undefined) {
chromeArguments.push(`--proxy-server=localhost:${options.proxyPort}`);
}

if (Boolean(JSON.parse(process.env.SA_NO_CHROME_SANDBOX ?? 'false')) === true) {
if (options.disableGpu === true) {
chromeArguments.push('--disable-gpu', '--disable-software-rasterizer');
const idx = chromeArguments.indexOf('--use-gl=any');
if (idx >= 0) chromeArguments.splice(idx, 1);
}

if (options.disableSandbox === true) {
chromeArguments.push('--no-sandbox');
} else if (os.platform() === 'linux') {
const runningAsRoot = process.geteuid && process.geteuid() === 0;
Expand Down Expand Up @@ -100,18 +111,18 @@ const defaultArgs = [
'--disable-default-apps', // Disable installation of default apps on first run
'--disable-dev-shm-usage', // https://github.com/GoogleChrome/puppeteer/issues/1834
'--disable-extensions', // Disable all chrome extensions.
'--disable-features=TranslateUI,site-per-process,OutOfBlinkCors', // site-per-process = Disables OOPIF, OutOfBlinkCors = Disables feature in chrome80/81 for out of process cors
'--disable-features=PaintHolding,TranslateUI,site-per-process,OutOfBlinkCors', // site-per-process = Disables OOPIF, OutOfBlinkCors = Disables feature in chrome80/81 for out of process cors
'--disable-hang-monitor',
'--disable-ipc-flooding-protection', // Some javascript functions can be used to flood the browser process with IPC. By default, protection is on to limit the number of IPC sent to 10 per second per frame.
'--disable-prompt-on-repost', // Reloading a page that came from a POST normally prompts the user.
'--disable-renderer-backgrounding', // This disables non-foreground tabs from getting a lower process priority This doesn't (on its own) affect timers or painting behavior. karma-chrome-launcher#123
'--disable-sync', // Disable syncing to a Google account
'--disable-gpu',
'--enable-logging',

'--force-color-profile=srgb', // Force all monitors to be treated as though they have the specified color profile.
'--use-gl=swiftshader-webgl',
'--use-gl=swiftshader', // Select which implementation of GL the GPU process should use. Options are: desktop: whatever desktop OpenGL the user has installed (Linux and Mac default). egl: whatever EGL / GLES2 the user has installed (Windows default - actually ANGLE). swiftshader: The SwiftShader software renderer.
'--use-gl=osmesa',
'--use-gl=any', // Select which implementation of GL the GPU process should use. Options are: desktop: whatever desktop OpenGL the user has installed (Linux and Mac default). egl: whatever EGL / GLES2 the user has installed (Windows default - actually ANGLE). swiftshader: The SwiftShader software renderer.
'--disable-partial-raster', // https://crbug.com/919955
'--disable-skia-runtime-opts', // Do not use runtime-detected high-end CPU optimizations in Skia.

'--incognito',

Expand Down
12 changes: 6 additions & 6 deletions puppet/index.ts
Expand Up @@ -84,11 +84,7 @@ export default class Puppet {
}

try {
const { proxyPort } = args;
const launchArgs = launcher.getLaunchArgs({
showBrowser: !!this.engine.isHeaded,
proxyPort,
});
const launchArgs = launcher.getLaunchArgs(args);

// exists, but can't launch, try to launch
await validateHostRequirements(this.engine);
Expand Down Expand Up @@ -133,6 +129,10 @@ ${remedyMessage}`);
}
}

interface ILaunchArgs {
export interface ILaunchArgs {
proxyPort?: number;
showBrowser?: boolean;
disableDevtools?: boolean;
disableGpu?: boolean;
noChromeSandbox?: boolean;
}

0 comments on commit 9a06165

Please sign in to comment.