Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ android/cross/aarch64-linux-android.txt

# Work dir of android/mesa-glamor/build-mesa-glamor.sh (tarball, source, stage).
/build-android-mesa-glamor/
# Work dir of android/mesa-gl/build-mesa-gl.sh (same shape, the guest GL stack).
/build-android-mesa-gl/
156 changes: 156 additions & 0 deletions android/mesa-gl/build-mesa-gl.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
#!/usr/bin/env bash
# NDK cross-build of the GL library a *guest* loads: Mesa zink with GLX + DRI3,
# i.e. the kopper path, as one monolithic libGL.so.1.2.0.
#
# Why this exists: the container image ships Mesa 24.3's classic libGL (glx/xlib
# frontend on winsys/sw/xlib, presenting with XShmPutImage). A GL guest then
# renders through zink on a headless Vulkan instance and copies the result into
# the X server, so its frames never create a Vulkan surface -- the ICD shim sees
# no swapchain, applies no acquire cap, generates nothing and never gets a release
# note. Kopper (zink's own WSI) presents through GLX/DRI3 instead: the client
# creates a VkSurfaceKHR from its X window, which the shim already serves
# (VK_KHR_xcb_surface, gamenative-drm#35), so GL rides the same ring and the same
# gate as a Vulkan guest.
#
# The source, cross file and patch set are the ones android/mesa-glamor already
# uses (pinned upstream tarball, fetch-mesa.sh), so both Mesas in the payload are
# one version and one toolchain.
#
# Installs into a stage prefix:
# lib/libGL.so.1.2.0, libGL.so.1, libGL.so the GL/GLX client, glapi built in
# lib/libEGL.so.1, lib/libgbm.so.1, libgbm.so the server's glamor EGL + GBM
# lib/dri/zink_dri.so the swrast-free DRI driver
# lib/gbm/dri_gbm.so libgbm's dri backend
# share/glvnd/... absent: glvnd is disabled on purpose
#
# Env overrides are the same as build-mesa-glamor.sh: NDK, TERMUXFS,
# GN_MESA_GL_ROOT (work dir), GN_MESA_GL_STAGE (install prefix).

set -euo pipefail

HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
GS_DIR="$(cd "$HERE/../.." && pwd)"
GLAMOR="$GS_DIR/android/mesa-glamor"
NDK="${NDK:-$HOME/Android/Sdk/ndk/27.3.13750724}"
TERMUXFS="${TERMUXFS:-$HOME/termuxfs/aarch64/data/data/com.termux/files/usr}"
ROOT="${GN_MESA_GL_ROOT:-$GS_DIR/build-android-mesa-gl}"
STAGE="${GN_MESA_GL_STAGE:-$ROOT/stage}"
BUILD_DIR="$ROOT/build"

case "$(uname -s)" in
Darwin) NDK_HOST=darwin-x86_64 ;;
*) NDK_HOST=linux-x86_64 ;;
esac
NDK_BIN="$NDK/toolchains/llvm/prebuilt/$NDK_HOST/bin"
[ -x "$NDK_BIN/aarch64-linux-android28-clang" ] || { echo "NDK not found at $NDK" >&2; exit 1; }
[ -d "$TERMUXFS/include/libdrm" ] || { echo "no libdrm headers under $TERMUXFS" >&2; exit 1; }
[ -f "$TERMUXFS/include/xcb/xcb.h" ] || { echo "no libxcb headers under $TERMUXFS" >&2; exit 1; }
[ -f "$TERMUXFS/include/GL/glx.h" ] || {
echo "no GL/glx.h under $TERMUXFS/include — GLX needs the libGL headers" >&2; exit 1; }
BISON="$(command -v bison)" || { echo "bison not in PATH" >&2; exit 1; }
FLEX="$(command -v flex)" || { echo "flex not in PATH" >&2; exit 1; }
CCACHE="$(command -v ccache || true)"

# Same build-tool venv rule as the glamor build: Mesa imports mako/PyYAML and
# needs meson >= 1.4, and a bare `meson` from PATH runs on an interpreter that
# usually has neither.
VENV="$ROOT/venv"
if [ ! -x "$VENV/bin/meson" ]; then
PYTHON=/usr/bin/python3
[ -x "$PYTHON" ] || PYTHON=python3
rm -rf "$VENV"
"$PYTHON" -m venv "$VENV" || { echo "cannot create the build-tool venv" >&2; exit 1; }
"$VENV/bin/pip" install --quiet 'meson>=1.4,<2' mako pyyaml packaging
fi
PATH="$VENV/bin:$PATH"

# Short-circuit so the pinned tarball and its patches are shared, not refetched.
SRC="$(bash "$GLAMOR/fetch-mesa.sh" "$ROOT" | tail -1)"

CROSS="$ROOT/cross-aarch64-linux-android.txt"
sed -e "s|@CCACHE@|${CCACHE:+'$CCACHE', }|g" \
-e "s|@NDK_BIN@|$NDK_BIN|g" \
-e "s|@BISON@|$BISON|g" \
-e "s|@FLEX@|$FLEX|g" \
-e "s|@DEPS@|$TERMUXFS|g" \
-e "s|@OVERLAY@|$GLAMOR/overlay|g" \
"$GLAMOR/cross.txt.in" > "$CROSS"
if grep -q '@[A-Z_]*@' "$CROSS"; then
echo "cross file still has unsubstituted placeholders: $CROSS" >&2
exit 1
fi

# The glamor build looks only at the overlay's .pc files, because the prefix's
# add -I${includedir} and break C++. This build needs the prefix's real ones --
# x11, xcb, xshmfence and the DRI protocol packages have no overlay copy -- and it
# compiles C only, so that hazard does not apply. Overlay stays first so libdrm
# still comes from it.
python3 - "$CROSS" "$TERMUXFS" <<'EOF'
import sys, re
path, deps = sys.argv[1], sys.argv[2]
text = open(path).read()
want = "pkg_config_libdir = ['%s/overlay/pkgconfig', '%s/lib/pkgconfig']" % (
re.search(r"-I([^']*)/overlay/include", text).group(1), deps)
text = re.sub(r"pkg_config_libdir = \[[^\]]*\]", want, text)
open(path, "w").write(text)
print("pkg_config_libdir:", want)
EOF

# platforms=x11 the GLX client talks X directly; this is what the glamor build
# turns off because glamor is EGL+GBM only. The GBM EGL platform
# is not a choice in `platforms` -- it comes with -Dgbm=enabled,
# which is why this stage builds both sides of the socket: the
# guest's GLX client and the server's EGL-on-GBM.
# glx=dri GLX with a DRI backend -- the frontend that carries kopper.
# 'disabled' means no GLX at all, 'xlib' would mean an indirect
# server-side path that presents by copy
# opengl=true build libGL; without it there is no client library
# glvnd=disabled one monolithic libGL.so.1.2.0, the shape the container image
# already has, so nothing has to learn about libGLX_mesa
# vulkan-drivers= zink dlopens the system ICD at runtime
# egl=gbm=enabled with gles2 is what Xwayland's glamor actually runs on, and the
# reason is not a build preference. Mesa refuses desktop GL on
# Android outright -- src/egl/main/eglcurrent.h gates EGL_OPENGL_API
# on `#if HAVE_OPENGL && !DETECT_OS_ANDROID` -- so
# eglBindAPI(EGL_OPENGL_API) returns EGL_BAD_PARAMETER, the display
# advertises EGL_CLIENT_APIS="OpenGL_ES" only, and every config's
# RENDERABLE_TYPE carries no EGL_OPENGL_BIT. Measured on device:
# GL 3.1 core -> EGL_BAD_ATTRIBUTE (profile mask rejected because the
# bound API was still ES), 2.1 -> EGL_BAD_MATCH, 1.0 -> EGL_BAD_ALLOC,
# GLES2/GLES3 -> SUCCESS. glamor's try_big_gl_api can therefore
# never succeed here; try_gles_api is the only path, which is also
# how Android Xwayland is normally configured.
# glvnd=disabled additionally removes a vendor lookup that cannot
# work on device: libglvnd's stub has compiled-in vendor dirs under
# /data/data/com.termux/files/usr/{etc,share}/glvnd/egl_vendor.d,
# which do not exist there, so with that stub no vendor loads at all
rm -rf "$BUILD_DIR"
meson setup "$BUILD_DIR" "$SRC" \
--cross-file "$CROSS" \
-Dprefix="$STAGE" \
-Dbuildtype=release \
-Dplatforms=x11 \
-Dglx=dri \
-Dopengl=true \
-Dgles1=disabled -Dgles2=enabled \
-Degl=enabled -Dgbm=enabled \
-Dgallium-drivers=zink \
-Dvulkan-drivers= \
-Dshared-glapi=disabled \
-Dglvnd=disabled \
-Dllvm=disabled \
-Dvalgrind=disabled \
-Dzstd=disabled \
-Dxmlconfig=disabled -Dexpat=disabled \
-Dgallium-va=disabled \
-Dgallium-rusticl=false \
-Dgallium-d3d10umd=false \
-Dtools= \
-Dbuild-tests=false -Dlmsensors=disabled -Dperfetto=false \
-Dandroid-libbacktrace=disabled

ninja -C "$BUILD_DIR" install

echo "staged: $STAGE"
ls -l "$STAGE/lib"/libGL.so* "$STAGE/lib"/libEGL.so* "$STAGE/lib"/libgbm.so* \
"$STAGE/lib/dri" "$STAGE/lib/gbm" 2>/dev/null || true
91 changes: 90 additions & 1 deletion android/package-gamescope.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ DEPS="${GAMESCOPE_TERMUX_PREFIX:-$HOME/termuxfs/aarch64/data/data/com.termux/fil
# libxml2/libicu). libglvnd's loader/GLES libs and everything else still come
# from the prefix.
GN_MESA_GLAMOR_STAGE="${GN_MESA_GLAMOR_STAGE:-$GS_DIR/build-android-mesa-glamor/stage}"
# Second Mesa stage: the GL a *guest* loads (android/mesa-gl/build-mesa-gl.sh),
# GLX + DRI3 + kopper rather than the glamor stage's GLES/EGL.
GN_MESA_GL_STAGE="${GN_MESA_GL_STAGE:-$GS_DIR/build-android-mesa-gl/stage}"
# macOS writes an AppleDouble "._name" sibling for every file it copies or
# archives, which lands in the payload as a thousand extra members. BSD cp and
# tar honour this; on Linux it is inert.
Expand Down Expand Up @@ -279,8 +282,94 @@ mkdir -p "$STAGE/lib/gbm"
cp -L "$GN_MESA_GLAMOR_STAGE/lib/gbm/dri_gbm.so" "$STAGE/lib/gbm/dri_gbm.so"
resolve_libs "$STAGE/lib/gbm/dri_gbm.so"

# --- the guest's GL: GLX + DRI3 + kopper (android/mesa-gl) ---------------------
# A GL guest loads libGL.so.1 through wine's opengl32. The container image's own
# library is Mesa 24.3's glx/xlib build, which presents with XShmPutImage: the
# frames are copied into the X server and never create a Vulkan surface, so the
# shim sees no swapchain, applies no acquire cap, generates nothing and never gets
# a release note. This one presents through GLX/DRI3 (kopper), so the client
# creates a surface from its X window and rides the same ring and the same gate as
# a Vulkan guest.
#
# -Dglvnd=disabled means this is one monolithic library that dispatches nothing,
# so no libGLX_mesa vendor for glvnd to find is needed. glapi is built in
# (-Dshared-glapi=disabled), so the imagefs's Mesa 24.3 libglapi.so.0 is
# untouched: nothing at this path links it.
ensure_mesa_gl_stage() {
local want
want="$(ls "$GN_MESA_GL_STAGE"/lib/libGL.so.1.* 2>/dev/null | head -1)"
if [ -n "$want" ] && [ -z "$(find "$GS_DIR/android/mesa-gl" -type f -newer "$want" 2>/dev/null | head -1)" ]; then
return 0
fi
echo "mesa-gl: building the guest GL stage at $GN_MESA_GL_STAGE"
bash "$GS_DIR/android/mesa-gl/build-mesa-gl.sh"
}
ensure_mesa_gl_stage
GL_LIB="$(ls "$GN_MESA_GL_STAGE"/lib/libGL.so.1.* 2>/dev/null | head -1)"
[ -f "$GL_LIB" ] || { echo "error: no libGL.so.1.* in $GN_MESA_GL_STAGE/lib" >&2; exit 1; }
GL_GALLIUM=("$GN_MESA_GL_STAGE"/lib/libgallium-*.so)
[ "${#GL_GALLIUM[@]}" = 1 ] && [ -f "${GL_GALLIUM[0]}" ] || {
echo "error: expected one libgallium-*.so in $GN_MESA_GL_STAGE/lib" >&2; exit 1; }
# Its own directory, because both Mesa stages emit the same megadriver soname
# (libgallium-26.2.6.so) and one would overwrite the other. lib/gl comes first on
# LD_LIBRARY_PATH (launcher), so a GL guest resolves this libGL and this gallium.
# Both are the same Mesa version, so the split cannot produce two different
# gallium APIs in one process.
#
# The same directory also carries this build's EGL + GBM, and that is what the
# *server* side of the socket loads: Xwayland's glamor is EGL-on-GBM. With the
# glamor stage's EGL (libglvnd's dispatch stub, compiled for termux-prefix vendor
# dirs) Xwayland logged "Failed to make EGL context current with GL" and
# "with GLES2", then disabled glamor and fell back to software — and every GL
# client then reported "DRI3 not available" and stayed off the shim. This one is
# glvnd=disabled, so LIBEGL IS the implementation and there is no vendor lookup
# to get wrong. GBM comes with it because EGL's GBM platform is linked against
# it, and libgbm's dri backend must be the one built from the same tree.
mkdir -p "$STAGE/lib/gl/dri" "$STAGE/lib/gl/gbm"
cp -L "$GL_LIB" "$STAGE/lib/gl/libGL.so.1.2.0"
ln -sfn libGL.so.1.2.0 "$STAGE/lib/gl/libGL.so.1"
ln -sfn libGL.so.1 "$STAGE/lib/gl/libGL.so"
cp -L "${GL_GALLIUM[0]}" "$STAGE/lib/gl/${GL_GALLIUM[0]##*/}"
cp -L "$GN_MESA_GL_STAGE/lib/dri/libdril_dri.so" "$STAGE/lib/gl/dri/libdril_dri.so"
ln -sfn libdril_dri.so "$STAGE/lib/gl/dri/zink_dri.so"
GL_EGL=(libEGL.so.1 libgbm.so.1)
for n in "${GL_EGL[@]}"; do
src="$(ls "$GN_MESA_GL_STAGE/lib/$n".*.* 2>/dev/null | head -1)"
[ -f "$src" ] || { echo "error: no $n.*.* in $GN_MESA_GL_STAGE/lib" >&2; exit 1; }
cp -L "$src" "$STAGE/lib/gl/$n.0.0"
ln -sfn "$n.0.0" "$STAGE/lib/gl/$n"
ln -sfn "$n" "$STAGE/lib/gl/${n%.*}"
done
cp -L "$GN_MESA_GL_STAGE/lib/gbm/dri_gbm.so" "$STAGE/lib/gl/gbm/dri_gbm.so"
# The server's glamor runs on GLES2, not GL: Mesa's EGL refuses EGL_OPENGL_API
# under DETECT_OS_ANDROID ("OpenGL is not a valid/supported API on Android",
# src/egl/main/eglcurrent.h), so eglBindAPI(EGL_OPENGL_API) returns
# EGL_BAD_PARAMETER and the display advertises only EGL_OPENGL_ES. That leaves
# the GLES2 path as the only one Xwayland can take -- and it needs a real
# libGLESv2. The one in lib/ is libglvnd's dispatch stub from the termux prefix:
# it resolves entry points through libGLdispatch, whose vendor is registered by a
# glvnd EGL vendor library, and this stage is glvnd=disabled, so every GLES call
# came back NULL and glamor stopped at "glGetString() returned NULL, your GL is
# broken". This copy is the same Mesa, monolithic, so it needs no vendor.
# Measured after shipping it: glGetString reports "zink Vulkan 1.4(Turnip Adreno
# (TM) 740)", glamor initialises on GLES2, DRI3 appears, and a GL guest (GLX +
# kopper libGL above) reaches the shim -- the first GL title ever to create a
# swapchain through it.
GL_GLES=(libGLESv2.so.2)
for n in "${GL_GLES[@]}"; do
src="$(ls "$GN_MESA_GL_STAGE/lib/$n".*.* 2>/dev/null | head -1)"
[ -f "$src" ] || { echo "error: no $n.*.* in $GN_MESA_GL_STAGE/lib" >&2; exit 1; }
cp -L "$src" "$STAGE/lib/gl/$n.0.0"
ln -sfn "$n.0.0" "$STAGE/lib/gl/$n"
ln -sfn "$n" "$STAGE/lib/gl/${n%.*}"
done
resolve_libs "$STAGE/lib/gl/libGL.so.1.2.0" "$STAGE/lib/gl/${GL_GALLIUM[0]##*/}" \
"$STAGE/lib/gl/dri/libdril_dri.so" "$STAGE/lib/gl/libEGL.so.1.0.0" \
"$STAGE/lib/gl/libgbm.so.1.0.0" "$STAGE/lib/gl/gbm/dri_gbm.so" \
"$STAGE/lib/gl/libGLESv2.so.2.0.0"

# glvnd EGL vendor json (library_path: libEGL_mesa.so.0, resolved from
# LD_LIBRARY_PATH = filesDir/gamescope/lib on device).
# LD_LIBRARY_PATH = filesDir/gamescope/lib and .../lib/gl on device).
mkdir -p "$STAGE/share/glvnd/egl_vendor.d"
cp "$GN_MESA_GLAMOR_STAGE/share/glvnd/egl_vendor.d/50_mesa.json" \
"$STAGE/share/glvnd/egl_vendor.d/"
Expand Down
Loading