Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force headless chromium/chrome to use actual gpu instead of Google SwiftShader

I'm trying to print html to pdf using headless chromium (using puppeteer) and everything works fine except if html contains large png images (over 10.000x10.000px) the whole process of rendering page takes extremely long (up to half an hour, but if using non-headless mode it takes only about 10 seconds). After days of investigating and tweaking I came to conclusion that this must be issue with page compositing process.

Below are dumps from chrome://gpu page in headless and non-headless modes. Only significant difference i've noticed is that, when runing chrome headlessly, puppeteer appends by itself --disable-gpu-compositing and --allow-pre-commit-input which i believe are responsible for dramatic performace dropdown.

Also, in non-headless mode chrome sees 2 gpu units:

GPU0 VENDOR= 0x10de, DEVICE=0x1d01 *ACTIVE*
GPU1 VENDOR= 0x8086, DEVICE=0x1912

and in headless mode only one:

GPU0 VENDOR= 0xffff [Google Inc.], DEVICE=0xffff [Google SwiftShader] *ACTIVE*

which is CPU-based implementation of the Vulkan and OpenGL ES graphics APIs.

So basicaly my question is:

Is there any way to run headless chrome/chromium with puppeteer using actual gpu (especialy for gpu-compositing), or is there any way to print page to pdf in non-headless mode?

Here is my non-headless chrome gpu config (where page rendering is fast):

Canvas: Hardware accelerated
Flash: Hardware accelerated
Flash Stage3D: Hardware accelerated
Flash Stage3D Baseline profile: Hardware accelerated
Compositing: Hardware accelerated
Multiple Raster Threads: Force enabled
Out-of-process Rasterization: Hardware accelerated
OpenGL: Enabled
Hardware Protected Video Decode: Unavailable
Rasterization: Hardware accelerated on all pages
Skia Renderer: Enabled
Video Decode: Unavailable
Vulkan: Disabled
WebGL: Hardware accelerated
WebGL2: Hardware accelerated

Chrome version: Chrome/83.0.4103.0
Operating system: Linux 4.13.0-46-generic
2D graphics backend: Skia/83 8ce842d38d0b32149e874d6855c91e8c68ba65a7

Command line:
/home/wojtas/projects/project-generator/node_modules/puppeteer/.local-
chromium/linux-756035/chrome-linux/chrome 
--disable-background-networking 
--enable-features=NetworkService,NetworkServiceInProcess 
--disable-background-timer-throttling 
--disable-backgrounding-occluded-windows 
--disable-breakpad 
--disable-client-side-phishing-detection 
--disable-component-extensions-with-background-pages 
--disable-default-apps 
--disable-dev-shm-usage 
--disable-extensions 
--disable-features=TranslateUI 
--disable-hang-monitor 
--disable-ipc-flooding-protection 
--disable-popup-blocking 
--disable-prompt-on-repost 
--disable-renderer-backgrounding 
--disable-sync 
--force-color-profile=srgb 
--metrics-recording-only 
--no-first-run
--enable-automation 
--password-store=basic 
--use-mock-keychain 
--disable-web-security 
--user-data-dir=/var/www/project-generator/var/chrome-user-data 
--allow-file-access-from-files 
--no-sandbox
--no-sandbox-and-elevated 
--no-zygote 
--enable-webgl 
--use-gl=desktop 
--use-skia-renderer 
--enable-gpu-rasterization 
--enable-zero-copy 
--disable-gpu-sandbox 
--enable-native-gpu-memory-buffers 
--disable-background-timer-throttling 
--disable-backgrounding-occluded-windows 
--disable-renderer-backgrounding 
--ignore-certificate-errors 
--enable-hardware-overlays 
--num-raster-threads=4 
--default-tile-width=512 
--default-tile-height=512 
--enable-oop-rasterization 
--remote-debugging-port=0 
--flag-switches-begin 
--flag-switches-end 
--enable-audio-service-sandbox 

And here is headless chrome gpu config (which is extemely slow)

 Graphics Feature Status
Canvas: Hardware accelerated
Flash: Hardware accelerated
Flash Stage3D: Hardware accelerated
Flash Stage3D Baseline profile: Hardware accelerated
Compositing: Software only. Hardware acceleration disabled
Multiple Raster Threads: Force enabled
Out-of-process Rasterization: Hardware accelerated
OpenGL: Enabled
Hardware Protected Video Decode: Unavailable
Rasterization: Hardware accelerated on all pages
Skia Renderer: Enabled
Video Decode: Unavailable
Vulkan: Disabled
WebGL: Hardware accelerated but at reduced performance
WebGL2: Hardware accelerated but at reduced performance

Chrome version: HeadlessChrome/83.0.4103.0
Operating system: Linux 4.13.0-46-generic
2D graphics backend: Skia/83 8ce842d38d0b32149e874d6855c91e8c68ba65a7

Command Line:
/home/wojtas/projects/project-generator/node_modules/puppeteer/.local-chromium/linux-756035/chrome-linux/chrome 
--disable-background-networking 
--enable-features=NetworkService,NetworkServiceInProcess 
--disable-background-timer-throttling 
--disable-backgrounding-occluded-windows 
--disable-breakpad 
--disable-client-side-phishing-detection 
--disable-component-extensions-with-background-pages 
--disable-default-apps 
--disable-dev-shm-usage 
--disable-extensions 
--disable-features=TranslateUI 
--disable-hang-monitor
--disable-ipc-flooding-protection 
--disable-popup-blocking 
--disable-prompt-on-repost 
--disable-renderer-backgrounding 
--disable-sync 
--force-color-profile=srgb
--metrics-recording-only 
--no-first-run 
--enable-automation 
--password-store=basic 
--use-mock-keychain 
--headless 
--hide-scrollbars 
--mute-audio 
--disable-web-security 
--user-data-dir=/var/www/project-generator/var/chrome-user-data 
--allow-file-access-from-files 
--no-sandbox 
--no-sandbox-and-elevated 
--no-zygote 
--enable-webgl 
--use-gl=desktop 
--use-skia-renderer 
--enable-gpu-rasterization 
--enable-zero-copy 
--disable-gpu-sandbox 
--enable-native-gpu-memory-buffers 
--disable-background-timer-throttling 
--disable-backgrounding-occluded-windows 
--disable-renderer-backgrounding 
--ignore-certificate-errors 
--enable-hardware-overlays 
--num-raster-threads=4 
--default-tile-width=512 
--default-tile-height=512
--enable-oop-rasterization 
--remote-debugging-port=0 
--disable-gpu-compositing 
--allow-pre-commit-input 

Even if i added --disable-webgl and --disable-webgl2 still VENDOR= 0xffff [Google Inc.], DEVICE=0xffff [Google SwiftShader] *ACTIVE* is being used.

Additionally:

Gpu compositing has been disabled, either via blacklist, about:flags or the command line. The browser will fall back to software compositing and hardware acceleration will be unavailable. Disabled Features: gpu_compositing WebGL has been disabled via blacklist or the command line. Disabled Features: webgl WebGL2 has been disabled via blacklist or the command line. Disabled Features: webgl2

notice appears on chrome://gpu page

Graphics Feature Status:

Canvas: Hardware accelerated
Flash: Hardware accelerated
Flash Stage3D: Hardware accelerated
Flash Stage3D Baseline profile: Hardware accelerated
Compositing: Software only. Hardware acceleration disabled
Multiple Raster Threads: Enabled
Out-of-process Rasterization: Disabled
OpenGL: Enabled
Hardware Protected Video Decode: Unavailable
Rasterization: Hardware accelerated on all pages
Skia Renderer: Enabled
Video Decode: Unavailable
Vulkan: Disabled
WebGL: Disabled
WebGL2: Disabled
like image 571
Danhoss Avatar asked Jun 25 '20 12:06

Danhoss


People also ask

Can you use WebGL without a GPU?

Without a graphics card, you cannot run WebGL on Windows.

Does Chrome use GPU rendering?

Chrome: GPU Usage Obviously, Chrome uses the GPU not only for video decoding but also for 2D rendering. Especially during video playback, but also with a regular website such as Boxtrolls the GPU is still used extensively.

How do I disable GPU compositing in Chrome?

Open the Chrome menu using the three-dot menu button given on the top right-hand side of the chrome browser. Scroll down and select the Advanced option from the list. Move to the System section and check the toggle button to enable or disable the hardware acceleration in Chrome.


Video Answer


1 Answers

Do you really need WebGL in headless mode?

If you remove this parameter...

--enable-webgl

...it should stop loading SwiftShader and stick to the GPU.

like image 122
user4723924 Avatar answered Oct 30 '22 15:10

user4723924