virglrenderer: Termux GPU hardware acceleration tutorial

🇹🇼 中文版

This article is about how to enable GPU hardware acceleration in Termux proot-distro by installing and running virgl server in Termux.

By enabling virlgl in proot, we will be able to play 4K60p videos and run 3D Linux games or Windows games (via Box86 + Wine) in proot.

SuperTuxKart, Glxgears

No root permission is required.

Testing device: Sony Xperia 5 II. Android 12. Snapdragon 865 (Adreno 650). RAM 8GB.

If you are confused , watch a video demonstration first (with subtitles)

1. Install Termux and Termux X11

Install Termux from F-droid

And setup Termux X11

2. Install proot-distro

Install a proot-distro with desktop environment.

I use Debian with XFCE4.

3. Install virglrenderer

Choose one method to install virglrenderer.

Method 1: Install Virglrenderer Android
  1. Install virglrenderer-android
1
pkg install virglrenderer-android
  1. You can start virgl server by executing this command
1
virgl_test_server_android &

Method 2: Compile Virglrenderer with Zink

P.S. The following steps are done in Termux, not inside proot-distro.

  1. Install dependencies
1
2
3
4
5
6
7
8
9
pkg install -y x11-repo

pkg install -y clang lld binutils cmake autoconf automake libtool ndk-sysroot ndk-multilib make python git libandroid-shmem-static ninja llvm bison flex libx11 xorgproto libdrm libpixman libxfixes libjpeg-turbo xtrans libxxf86vm xorg-xrandr xorg-font-util xorg-util-macros libxfont2 libxkbfile libpciaccess xcb-util-renderutil xcb-util-image xcb-util-keysyms xcb-util-wm xorg-xkbcomp xkeyboard-config libxdamage libxinerama

pkg install clvk glfw mesa-vulkan-icd-swrast swiftshader vulkan-extension-layer vulkan-extension-layer-static vulkan-headers vulkan-loader vulkan-loader-generic vulkan-tools

pkg install vulkan-loader-android

pip install meson mako
  1. Clone repositories of libxshmfence, mesa, libepoxy, virglrenderer
1
2
3
4
5
6
mkdir ~/tmp
cd ~/tmp
LD_PRELOAD='' git clone --depth 1 -b libxshmfence-1.3 https://gitlab.freedesktop.org/xorg/lib/libxshmfence.git
LD_PRELOAD='' git clone --depth 1 -b mesa-22.0.5 https://gitlab.freedesktop.org/mesa/mesa.git
LD_PRELOAD='' git clone --depth 1 -b 1.5.10 https://github.com/anholt/libepoxy.git
LD_PRELOAD='' git clone --shallow-since 2022-06-27 https://gitlab.freedesktop.org/virgl/virglrenderer.git
  1. Compile libxshmfence
1
2
3
4
cd ~/tmp/libxshmfence
./autogen.sh --prefix=$PREFIX --with-shared-memory-dir=$TMPDIR
sed -i s/values.h/limits.h/ ./src/xshmfence_futex.h
make -j8 install CPPFLAGS=-DMAXINT=INT_MAX
  1. Compile mesa
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
cd ~/tmp/mesa
sed -i '40s+^$+#include "X11/Xlib.h"+' src/egl/main/egldisplay.h
sed -i 's/^import os$/import os, shutil\ndef link(src, dest):\n shutil.copyfile(src, dest)\ndef unlink(src):\n os.remove(src)\nos.link = link\nos.unlink = unlink/' bin/install_megadrivers.py
mkdir b && cd b
LDFLAGS='-l:libandroid-shmem.a -llog' meson .. -Dprefix=$PREFIX -Dplatforms=x11 -Dgbm=enabled -Ddri-drivers='' -Dgallium-drivers=zink,swrast -Dllvm=disabled -Dvulkan-drivers='' -Dcpp_rtti=false -Dc_args=-Wno-error=incompatible-function-pointer-types -Dbuildtype=release
rm $PREFIX/lib/libglapi.so*
rm $PREFIX/lib/libGL.so*
rm $PREFIX/lib/libGLES*
rm $PREFIX/lib/libEGL*
rm $PREFIX/lib/libgbm*
ninja install
  1. Compile libepoxy
1
2
3
4
5
cd ~/tmp/libepoxy
mkdir b && cd b
meson -Dprefix=$PREFIX -Dbuildtype=release -Dglx=yes -Degl=yes -Dtests=false -Dc_args=-U__ANDROID__ ..
rm $PREFIX/lib/libepoxy.so*
ninja install
  1. Compile virglrenderer
1
2
3
4
5
6
cd ~/tmp/virglrenderer
git checkout -f dd301caf7e05ec9c09634fb7872067542aad89b7~2
sed -i 's+"/tmp+"/data/data/com.termux/files/usr/tmp+' vtest/vtest_protocol.h
mkdir b && cd b
meson -Dbuildtype=release -Dprefix=$PREFIX -Dplatforms=egl ..
ninja install
  1. You can start virgl server by executing this command
1
MESA_NO_ERROR=1 MESA_GL_VERSION_OVERRIDE=4.0 GALLIUM_DRIVER=zink virgl_test_server --use-egl-surfaceless

4. Run application with virgl

Note: virgl server may be broken after upgrading Termux packages. (You will see errors like libEGL warning: DRI2: failed to create dri screen in terminal) If so, recompile everything in Part 3 and try again.

4.1. In proot

  1. Start virgl server in Termux.

  2. Launch Termux X11 app, keep it opening in the background. Go back to Termux, start Termux X11

1
2
export DISPLAY=:0
termux-x11 :0 &
  1. Swipe from left edge, clcik New Session to open a new terminal. Log into proot-distro as user and mount Termux’s /tmp to proot.
1
proot-distro login debian --user user --shared-tmp
  1. Then start XFCE session
1
2
export DISPLAY=:0
dbus-launch --exit-with-session startxfce4 &
  1. Open terminal emulator in XFCE desktop. Run applications with two parameters: GALLIUM_DRIVER=virpipe MESA_GL_VERSION_OVERRIDE=4.0 to use virgl. Note some applications may crash when MESA_GL_VERSION_OVERRIDE=4.0 is used.

  2. For instance, to run Minetest with virgl:

1
GALLIUM_DRIVER=virpipe MESA_GL_VERSION_OVERRIDE=4.0 minetest

The FPS of glxgears is higher when virgl enabled.

After enabling virgl, Minetest FPS grows from 10 FPS up to 20 FPS.

VLC with virgl is capable of playing 4K60p videos.

SueprTuxKart becomes playable after enabling vrigl.

The FPS of WebGL Aquarium doubled after enabling virgl. (You need to force enable WebGL in about:config)

4.2. In chroot

  1. Setup a chroot environment, for exmaple chroot Ubuntu with XFCE4

  2. Set SELinux to Permissive

1
sudo setenforce 0
  1. Start Termux X11 APP. Go back to Termux, execute:
1
XDG_RUNTIME_DIR=${TMPDIR} termux-x11 :0 -ac &
  1. Mount Termux’s tmp to chroot’s tmp directory
1
2
export CHROOT_DIR=/data/local/tmp/chrootubuntu
sudo busybox mount --bind $PREFIX/tmp $CHROOT_DIR/tmp
  1. Log into chroot. Start XFCE4
1
2
3
chmod -R 777 /tmp
export DISPLAY=:0 PULSE_SERVER=tcp:127.0.0.1:4713
dbus-launch --exit-with-session startxfce4 &
  1. Run apps with virgl
1
GALLIUM_DRIVER=virpipe MESA_GL_VERSION_OVERRIDE=4.0 minetest

4.3. Run Windows games

Install Box86 + Wine in proot-distro.

Start virgl server in Termux.

Run box86 with GALLIUM_DRIVER parameters:

1
GALLIUM_DRIVER=virpipe MESA_GL_VERSION_OVERRIDE=4.0 box86 wine AIR.exe

“Undertale” works well with virgl enabled.

Also visual novel “Air”

References


Thanks for your reading. You are welcome to share articles of Ivon's Blog (ivonblog.com). Please include the original URL when reprinting or citing articles, and abide by CC BY-NC-ND 4.0 license. For commercial use, please write an e-mail to me.

written by human, not by AI

If this article is helpful to you please consider supporting me.

Leave a comment

Choose the commenting system you like.

Please login to your Github account to leave a comment. You can post codes here for discussion. For images, please upload to Imgur and copy links. Your comments would be visible to everyone on Github Discussions.

This comment system is provided by Disqus, you may see some Disqus ADs on this page. You can leave a comment anonymously but you won't receive reply notifications.