GPU acceleration for Firefox

About

Hardware-accelerated video playback is typically available with modern GPUs. Unforunately, Linux distributions typically don't enable out-of-the-box support for it in Firefox. This document covers what's needed to enable such support. Several NVidia-specific details are mentioned (namely use of Nvidia NVDEC) but is GPU-agnostic overall. To see which video formats are supported by your hardware run vainfo

Note: Support for GPU video acceleration is only available for Firefox (Chrome does not support it).

Driver

Install the Video Acceleration API (VAAPI) implementation that uses Nvidia NVDEC as a backend.

There is one implementation available: nvidia-vaapi-driver. This only supports decoding, not encoding, but suffices for Firefox acceleration. This is available via most distributions’ package managers but for those that don’t offer it (e.g. older Ubuntu versions) it can be easily installed manually:

apt install build-essential git gstreamer1.0-plugins-bad libgstreamer-plugins-bad1.0-dev libffmpeg-nvenc-dev libva-dev libegl-dev meson
git clone https://github.com/elFarto/nvidia-vaapi-driver
cd nvidia-vaapi-driver/
meson setup build
sudo meson install -C build

This driver also supports recent versions (≥0.3.5) of the mpv video player using mpv --hwdec=vaapi. It is not needed though since using mpv --hwdec=auto should already work fine for accelerated playback (one can also put hwdec=auto into ~/.config/mpv/mpv.conf).

Kernel

Make sure that the nvidia_drm module is loaded with modeset support. This is done by setting the nvidia-drm.modeset=1 kernel parameter. It should be sufficient to include the following lines in /etc/modprobe.d/nvidia-graphics-drivers-kms.conf

# This file was generated by nvidia-driver-535
# Set value to 0 to disable modesetting
options nvidia-drm modeset=1

If correctly-configured then cat /sys/module/nvidia_drm/parameters/modeset should output Y and lsmod|grep nvidia_drm should include something like nvidia_modeset 1314816 18 nvidia_drm

Missing file

The /etc/glvnd/egl_vendor.d/10_nvidia.json file advertises the EGL library. This file is missing on some systems. To create it run the following as root:

cat > /etc/glvnd/egl_vendor.d/10_nvidia.json << END
{
    "file_format_version" : "1.0.0",
    "ICD" : {
        "library_path" : "libEGL_nvidia.so.0"
    }
}
END

Firefox environment variables

Environment variables can be placed in /etc/environment or in a wrapper script. The following variables should be set:

MOZ_DISABLE_RDD_SANDBOX=1
LIBVA_DRIVER_NAME=nvidia
MOZ_X11_EGL=1

Details:

Firefox settings

These are set via about:config (manually enter it since for security reasons the page can't be linked to from here).

All options under DOM and Media GPU Decode must be set for GPU-accelerated video playback.

GPU-accelerated DOM rendering

GPU-accelerated media decoding

Optional settings

Verification

Test basic support by running eglinfo -B | grep -C2 'EGL vendor string:'

If things are working correctly then running correctly this will output:

X11 platform:
EGL API version: 1.5
EGL vendor string: NVIDIA
EGL version string: 1.5
EGL client APIs: OpenGL_ES OpenGL
--
Device platform:
EGL API version: 1.5
EGL vendor string: NVIDIA
EGL version string: 1.5
EGL client APIs: OpenGL_ES OpenGL

If things are not correct then the output will be:

libEGL warning: DRI3: Screen seems not DRI3 capable
libEGL warning: DRI2: failed to authenticate
X11 platform:
EGL API version: 1.5
EGL vendor string: Mesa Project
EGL version string: 1.5
EGL client APIs: OpenGL OpenGL_ES

Next open Firefox and go to about:support (manually enter it since for security reasons the page can't be linked to from here).

The following should be visible:

Checking that video decoding is being done on the GPU:

One can see which processes are using the GPU using nvidia-smi When Firefox is running it should appear multiple times under Processes with the types listed as G. When playing video via Firefox one of the lines should have its type listed as C+G.