nvidia-vaapi-driver icon indicating copy to clipboard operation
nvidia-vaapi-driver copied to clipboard

Allow running in Flatpak Firefox

Open Gert-dev opened this issue 4 years ago • 16 comments

Hello

I did some cursory tests when running Firefox in a Flatpak, but it doesn't appear to work here. VA-API appears to return -1 when trying to select the nvidia driver when the first video is started.

I'm not too familiar with how Flatpak works, but IIRC it has its own VA-API packages, so it is possible that they cannot access a built version of this driver plug-in and a Flatpak extension might need to be built (but this might be complete nonsense, as I said I don't know too much about Flatpak).

I verified it did work with a native, non-flatpak-ed, Firefox.

In any case, great work on this project!

Gert-dev avatar Jan 11 '22 18:01 Gert-dev

Sorry, I didn't ever get around to commenting on this. It's definitely something I'll look into, but it's not a priority for me.

elFarto avatar Jan 19 '22 12:01 elFarto

On a related note, there is now a feature request on the FreeDesktop SDK issue tracker asking for this to be packaged, which would/could make it available in Flatpaks using this runtime.

Gert-dev avatar Jan 28 '22 20:01 Gert-dev

The VA-API NVDEC (used by Firefox, FFmpeg, Mpv) and VDPAU (used by Chrome) backends are both needed. That issue request will break flatpaks running Chrome and derivatives. The reason is that the two backends have conflicting file names: nvidia_drv_video.so.

Edit: The FreeDesktop SDK does not provide the VDPAU backend VA-API driver. Meaning, no file conflict issues. Thank you, for submitting the request.

marioroy avatar Aug 30 '22 06:08 marioroy

I am using a little script to set env variable overrides for the firefox flatpak, and copy the driver to where the flatpak can read it. After running the script you just run the firefox flatpak as you would normally. Copying the driver is kind of hacky -- the script must be run whenever you update nvidia-vaapi-driver on the host -- but thought it might be useful to share here as a stopgap.

#!/bin/sh

DRI_PATH=${HOME}/.var/app/org.mozilla.firefox/dri

echo "Clearing firefox flatpak overrides"
flatpak override --user --reset org.mozilla.firefox

echo "Adding firefox flatpak overrides"
flatpak override --user --env=LIBVA_DRIVER_NAME=nvidia \
                        --env=LIBVA_DRIVERS_PATH=${DRI_PATH} \
                        --env=LIBVA_MESSAGING_LEVEL=1 \
                        --env=MOZ_DISABLE_RDD_SANDBOX=1 \
                        --env=NVD_BACKEND=direct \
                        org.mozilla.firefox

# uncomment the following if you use keepass running on the host
# flatpak override --user --filesystem=xdg-run/app/org.keepassxc.KeePassXC org.mozilla.firefox

echo "Copying nvidia vaapi driver into ${DRI_PATH}"
mkdir -p ${DRI_PATH}
cp /usr/lib/dri/nvidia_drv_video.so ${DRI_PATH}/nvidia_drv_video.so

cat <<"EOF"

    Now open about:config and change `gfx.webrender.all` and `media.ffmpeg.vaapi.enabled` to true.

EOF

You probably also need to install the ffmpeg-full platform extension for the version of the runtime firefox is using. You can check with flatpak info --show-runtime org.mozilla.firefox. The current stable version is using the 22.08 runtime so you can install the extension with flatpak install --user org.freedesktop.Platform.ffmpeg-full//22.08. I also have flatpak nvidia drivers installed (org.freedesktop.Platform.GL.nvidia-525-60-11 as of writing) which may also be needed.

Edit: Flatpak nvidia drivers are definitely needed and they need to match the host driver version. For 530.30.02 host drivers you would need the org.freedesktop.Platform.GL.nvidia-530-30-02 flatpak drivers installed for example.

JCallicoat avatar Jan 03 '23 04:01 JCallicoat

I am using a little script to set env variable overrides for the firefox flatpak, and copy the driver to where the flatpak can read it. After running the script you just run the firefox flatpak as you would normally. Copying the driver is kind of hacky -- the script must be run whenever you update nvidia-vaapi-driver on the host -- but thought it might be useful to share here as a stopgap.

#!/bin/sh

DRI_PATH=${HOME}/.var/app/org.mozilla.firefox/dri

echo "Clearing firefox flatpak overrides"
flatpak override --user --reset org.mozilla.firefox

echo "Adding firefox flatpak overrides"
flatpak override --user --env=LIBVA_DRIVER_NAME=nvidia \
                        --env=LIBVA_DRIVERS_PATH=${DRI_PATH} \
                        --env=LIBVA_MESSAGING_LEVEL=1 \
                        --env=MOZ_DISABLE_RDD_SANDBOX=1 \
                        --env=NVD_BACKEND=direct \
                        org.mozilla.firefox

# uncomment the following if you use keepass running on the host
# flatpak override --user --filesystem=xdg-run/app/org.keepassxc.KeePassXC org.mozilla.firefox

echo "Copying nvidia vaapi driver into ${DRI_PATH}"
mkdir -p ${DRI_PATH}
cp /usr/lib/dri/nvidia_drv_video.so ${DRI_PATH}/nvidia_drv_video.so

cat <<"EOF"

    Now open about:config and change `gfx.webrender.all` and `media.ffmpeg.vaapi.enabled` to true.

EOF

You probably also need to install the ffmpeg-full platform extension for the version of the runtime firefox is using. You can check with flatpak info --show-runtime org.mozilla.firefox. The current stable version is using the 22.08 runtime so you can install the extension with flatpak install --user org.freedesktop.Platform.ffmpeg-full//22.08. I also have flatpak nvidia drivers installed (org.freedesktop.Platform.GL.nvidia-525-60-11 as of writing) which may also be needed.

Thanks, worked for me, just had to change the path from the driver from cp /usr/lib/dri/nvidia_drv_video.so ${DRI_PATH}/nvidia_drv_video.so to cp /usr/lib64/dri/nvidia_drv_video.so ${DRI_PATH}/nvidia_drv_video.so on line 21 ~~shouldn't a symlink work too and prevent the need to re-run the script each time the api is updated?~~ Forgot that the whole idea of Flatpak is for the apps to be isolated so it clearly won't have access to the folder. I don't know if giving access to that specific folder through flatseal and then creating a symlink would be any different

frandmb avatar Jan 23 '23 14:01 frandmb

I don't know if giving access to that specific folder through flatseal and then creating a symlink would be any different.

Yeah linking or just allowing filesystem access is not possible because /usr is blacklisted, because the flatpak basically just bind-mounts an ostree repo into it's own mount namespace and has it's own system dirs like /usr that are provided by the runtimes. Which also means a symlink would just point to the non-existent file in the runtime /usr and a hardlink wouldn't be allowed to access the inode on underlying the host /usr.

I think the best option for flatpak end-users would be to have it included in the runtime per the issue @Gert-dev linked. For folks who want to run nvidia-vaapi-driver git builds after that, we will still need to just copy the driver and force the vaapi driver search path it seems.

JCallicoat avatar Feb 08 '23 16:02 JCallicoat

Thank you, @JCallicoat. Passing with NVIDIA driver 525.89.02. But, not 530.30.02 resulting in 999 initialization error. So, I reverted back to 525.89.02. I also copied the vainfo binary to the DRI_PATH.

cp /usr/bin/vainfo ${DRI_PATH}/vainfo

For testing, one may specify --command=bash to get a shell and run vainfo.

$ flatpak run --user --command=bash org.mozilla.firefox

$ printenv | grep LIBVA
LIBVA_DRIVER_NAME=nvidia
LIBVA_MESSAGING_LEVEL=1
LIBVA_DRIVERS_PATH=/home/mario/.var/app/org.mozilla.firefox/dri

$ cd ${LIBVA_DRIVERS_PATH}
$ NVD_LOG=1 ./vainfo
Trying display: wayland
Trying display: x11
        96.385762585 [14-14] ../src/vabackend.c:2165       __vaDriverInit_1_0 Initialising NVIDIA VA-API Driver: 10
        96.385772136 [14-14] ../src/vabackend.c:2174       __vaDriverInit_1_0 Now have 0 (0 max) instances
        96.385776347 [14-14] ../src/vabackend.c:2200       __vaDriverInit_1_0 Selecting Direct backend
        96.432484332 [14-14] ../src/direct/direct-export-buf.c:  85      direct_initExporter Found NVIDIA GPU 0 at /dev/dri/renderD128
        96.432491283 [14-14] ../src/direct/nv-driver.c: 217            init_nvdriver Initing nvdriver...
        96.432494164 [14-14] ../src/direct/nv-driver.c: 222            init_nvdriver Got dev info: 100 1 2 6
        96.432590447 [14-14] ../src/direct/nv-driver.c: 283            init_nvdriver NVIDIA kernel driver version: 525.89.02
vainfo: VA-API version: 1.15 (libva 2.17.0)
vainfo: Driver version: VA-API NVDEC driver [direct backend]
vainfo: Supported profile and entrypoints
      VAProfileMPEG2Simple            :	VAEntrypointVLD
      VAProfileMPEG2Main              :	VAEntrypointVLD
      VAProfileVC1Simple              :	VAEntrypointVLD
      VAProfileVC1Main                :	VAEntrypointVLD
      VAProfileVC1Advanced            :	VAEntrypointVLD
      VAProfileH264Main               :	VAEntrypointVLD
      VAProfileH264High               :	VAEntrypointVLD
      VAProfileH264ConstrainedBaseline:	VAEntrypointVLD
      VAProfileHEVCMain               :	VAEntrypointVLD
      VAProfileVP8Version0_3          :	VAEntrypointVLD
      VAProfileVP9Profile0            :	VAEntrypointVLD
      VAProfileAV1Profile0            :	VAEntrypointVLD
      VAProfileHEVCMain10             :	VAEntrypointVLD
      VAProfileHEVCMain12             :	VAEntrypointVLD
      VAProfileVP9Profile2            :	VAEntrypointVLD
      VAProfileHEVCMain444            :	VAEntrypointVLD
        96.516364821 [14-14] ../src/vabackend.c:2075              nvTerminate Terminating 0x55e62db28400
        96.516425630 [14-14] ../src/vabackend.c:2089              nvTerminate Now have 0 (0 max) instances

$ exit

Prior to running vainfo, I ran ldd to ensure no dependencies are missing.

$ flatpak run --user --command=bash org.mozilla.firefox

$ cd ${HOME}/.var/app/org.mozilla.firefox/dri
$ ldd nvidia_drv_video.so 
	linux-vdso.so.1 (0x00007ffc4a7ba000)
	libEGL.so.1 => /usr/lib/x86_64-linux-gnu/libEGL.so.1 (0x00007fb9c219d000)
	libgstcodecparsers-1.0.so.0 => /usr/lib/x86_64-linux-gnu/libgstcodecparsers-1.0.so.0 (0x00007fb9c212b000)
	libc.so.6 => /usr/lib/x86_64-linux-gnu/libc.so.6 (0x00007fb9c1e00000)
	libm.so.6 => /usr/lib/x86_64-linux-gnu/libm.so.6 (0x00007fb9c2048000)
	libGLdispatch.so.0 => /usr/lib/x86_64-linux-gnu/libGLdispatch.so.0 (0x00007fb9c1d47000)
	libgstbase-1.0.so.0 => /usr/lib/x86_64-linux-gnu/libgstbase-1.0.so.0 (0x00007fb9c1cc5000)
	libgstreamer-1.0.so.0 => /usr/lib/x86_64-linux-gnu/libgstreamer-1.0.so.0 (0x00007fb9c1b77000)
	libglib-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007fb9c1a3e000)
	/lib64/ld-linux-x86-64.so.2 (0x00007fb9c21e4000)
	libgobject-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0 (0x00007fb9c19dd000)
	libgmodule-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgmodule-2.0.so.0 (0x00007fb9c203f000)
	libunwind.so.8 => /usr/lib/x86_64-linux-gnu/libunwind.so.8 (0x00007fb9c2024000)
	libdw.so.1 => /usr/lib/x86_64-linux-gnu/libdw.so.1 (0x00007fb9c1940000)
	libpcre.so.1 => /usr/lib/x86_64-linux-gnu/libpcre.so.1 (0x00007fb9c18f8000)
	libffi.so.8 => /usr/lib/x86_64-linux-gnu/libffi.so.8 (0x00007fb9c2012000)
	libgcc_s.so.1 => /usr/lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fb9c18d7000)
	liblzma.so.5 => /usr/lib/x86_64-linux-gnu/liblzma.so.5 (0x00007fb9c18ad000)
	libz.so.1 => /usr/lib/x86_64-linux-gnu/libz.so.1 (0x00007fb9c1892000)
	libelf.so.1 => /usr/lib/x86_64-linux-gnu/libelf.so.1 (0x00007fb9c1876000)
	libbz2.so.1 => /usr/lib/x86_64-linux-gnu/libbz2.so.1 (0x00007fb9c1862000)

$ exit

The following are my overrides. Webrender is enabled via the MOZ_WEBRENDER env; about:config settings media.ffmpeg.vaapi.enabled=true and widget.dmabuf.force-enabled=true.

$ cat ~/.local/share/flatpak/overrides/org.mozilla.firefox 
[Environment]
NVD_BACKEND=direct
LIBVA_DRIVER_NAME=nvidia
LIBVA_DRIVERS_PATH=/home/mario/.var/app/org.mozilla.firefox/dri
LIBVA_MESSAGING_LEVEL=1
MOZ_DISABLE_RDD_SANDBOX=1
MOZ_DRM_DEVICE=/dev/dri/renderD128
MOZ_X11_EGL=1
MOZ_WEBRENDER=1

The MOZ_ENABLE_WAYLAND=1 env override is needed if running on Wayland.

marioroy avatar Mar 10 '23 22:03 marioroy

Did you make sure you have the correct nvidia flatpack drivers installed to match the host drivers? For 530.30.02 host drivers you would need the org.freedesktop.Platform.GL32.nvidia-530-30-02 flatpak drivers installed.

JCallicoat avatar Mar 16 '23 03:03 JCallicoat

Similarly, I think, Snap-installed Firefox doesn't work either.

berkant avatar Mar 18 '23 02:03 berkant

@berkant snaps do something ~~crazy~~ different and mount all the packages on their own loopback devices, but in theory a similar workaround to the script I am using (posted above) would work for snaps? I.e., copy the driver somewhere the snap can read it, override env variables for the snap to use the driver. I have never really used snaps so not sure how feasible it is.

JCallicoat avatar Apr 29 '23 10:04 JCallicoat

I ran into an issue that was a pain to figure out so documenting here. Arch uses a newer glibc (2.38 as of writing) but the current freedesktop platform runtime uses 2.35 and cannot load nvidia_drv_video.so built with system gcc.

You just get the log message:

[GFX1-]: vaapitest: ERROR
[GFX1-]: vaapitest: VA-API test failed: failed to initialise VAAPI connection.

None of the logging options for NVD, LIBVA, MOZ_LOG show anything because the loader was actually failing with version `GLIBC_2.38' not found because of the newer symbols from 2.38. I managed to work around this by "cross-compiling" to an older version of glibc using zig cc to compile nvidia-vaapi-driver. I used these changes for the libva-nvidia-driver-git PKGBUILD.

 build() {
     cd ${pkgname}

+    export CC="zig cc"
+    export CFLAGS="-target x86_64-linux.6.4.8...6.4.8-gnu.2.34 -march=x86-64 -mtune=x86_64 -I/usr/include"
+
     arch-meson . build

     ninja -C build

Similar should work for other bleeding edge distros like gentoo and rawhide.

JCallicoat avatar Aug 07 '23 04:08 JCallicoat

I am using a little script to set env variable overrides for the firefox flatpak, and copy the driver to where the flatpak can read it. After running the script you just run the firefox flatpak as you would normally. Copying the driver is kind of hacky -- the script must be run whenever you update nvidia-vaapi-driver on the host -- but thought it might be useful to share here as a stopgap.

#!/bin/sh

DRI_PATH=${HOME}/.var/app/org.mozilla.firefox/dri

echo "Clearing firefox flatpak overrides"
flatpak override --user --reset org.mozilla.firefox

echo "Adding firefox flatpak overrides"
flatpak override --user --env=LIBVA_DRIVER_NAME=nvidia \
                        --env=LIBVA_DRIVERS_PATH=${DRI_PATH} \
                        --env=LIBVA_MESSAGING_LEVEL=1 \
                        --env=MOZ_DISABLE_RDD_SANDBOX=1 \
                        --env=NVD_BACKEND=direct \
                        org.mozilla.firefox

# uncomment the following if you use keepass running on the host
# flatpak override --user --filesystem=xdg-run/app/org.keepassxc.KeePassXC org.mozilla.firefox

echo "Copying nvidia vaapi driver into ${DRI_PATH}"
mkdir -p ${DRI_PATH}
cp /usr/lib/dri/nvidia_drv_video.so ${DRI_PATH}/nvidia_drv_video.so

cat <<"EOF"

    Now open about:config and change `gfx.webrender.all` and `media.ffmpeg.vaapi.enabled` to true.

EOF

You probably also need to install the ffmpeg-full platform extension for the version of the runtime firefox is using. You can check with flatpak info --show-runtime org.mozilla.firefox. The current stable version is using the 22.08 runtime so you can install the extension with flatpak install --user org.freedesktop.Platform.ffmpeg-full//22.08. I also have flatpak nvidia drivers installed (org.freedesktop.Platform.GL.nvidia-525-60-11 as of writing) which may also be needed.

Edit: Flatpak nvidia drivers are definitely needed and they need to match the host driver version. For 530.30.02 host drivers you would need the org.freedesktop.Platform.GL.nvidia-530-30-02 flatpak drivers installed for example.

Adding this for future refence. I was able to make the driver work under ubuntu 22.04 + firefox 121 flatpak + nvidia-535.146.02 flatpak + nvidia-vaapi-driver v0.0.11 compiled from source. I had to change the cp /usr/lib/dri/nvidia_drv_video.so ${DRI_PATH}/nvidia_drv_video.so to cp /usr/lib/x86_64-linux-gnu/dri/nvidia_drv_video.so ${DRI_PATH}/nvidia_drv_video.so.

Thanks a lot for the script!

Maccer1 avatar Jan 21 '24 16:01 Maccer1