强制无头铬/铬使用实际 gpu 而不是 Google SwiftShader

Posted

技术标签:

【中文标题】强制无头铬/铬使用实际 gpu 而不是 Google SwiftShader【英文标题】:Force headless chromium/chrome to use actual gpu instead of Google SwiftShader 【发布时间】:2020-10-15 21:31:45 【问题描述】:

我正在尝试使用 headless chromium(使用 puppeteer)将 html 打印为 pdf 并且一切正常,除非 html 包含大的 png 图像(超过 10.000x10.000px)渲染页面的整个过程需要很长时间(最多半小时,但如果使用非无头模式只需要大约 10 秒)。经过几天的调查和调整,我得出结论,这一定是页面合成过程的问题。

以下是来自chrome://gpu 页面在无头和非无头模式下的转储。 我注意到的唯一显着区别是,当无头运行 chrome 时,puppeteer 会自行附加 --disable-gpu-compositing--allow-pre-commit-input,我认为这是导致性能大幅下降的原因。

此外,在非无头模式下,chrome 会看到 2 个 gpu 单元:

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

在无头模式下只有一个:

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

这是 Vulkan 和 OpenGL ES 图形 API 的基于 CPU 的实现。

所以基本上我的问题是:

有没有办法使用实际的 gpu(特别是 gpu 合成)通过 puppeteer 运行 headless chrome/chromium,或者有没有办法在非无头模式下将页面打印到 pdf?

这是我的非无头 chrome gpu 配置(页面渲染速度很快):

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 

这里是无头 chrome gpu 配置(速度极慢)

 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 

即使我添加了--disable-webgl--disable-webgl2 仍然 VENDOR= 0xffff [Google Inc.], DEVICE=0xffff [Google SwiftShader] *ACTIVE* 正在使用中。

另外:

已通过黑名单、about:flags 或命令行禁用 Gpu 合成。浏览器会 回退到软件合成,硬件加速将不可用。 禁用功能:gpu_compositing WebGL 已通过黑名单或命令行禁用。 禁用功能:webgl WebGL2 已通过黑名单或命令行禁用。 禁用功能:webgl2

通知出现在chrome://gpu 页面上

图形功能状态:

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

【问题讨论】:

您找到解决此问题的合适方法了吗? 您找到最适合您的配置了吗? 【参考方案1】:

你真的需要无头模式的 WebGL 吗?

如果您删除此参数...

--enable-webgl

...它应该停止加载 SwiftShader 并坚持使用 GPU。

【讨论】:

以上是关于强制无头铬/铬使用实际 gpu 而不是 Google SwiftShader的主要内容,如果未能解决你的问题,请参考以下文章

ruby capybara.rb(无头铬)

我为 VM 收集铬,TypeError:JSON 对象必须是 str,而不是“字节”

铬嵌入式框架

在无头模式下运行 Chrome

Debian apt-get删除firefox-esr强制安装铬?

uwp 中的无铬窗口