diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b4669f7172..c1b215973b 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -150,7 +150,7 @@ jobs: uses: actions/cache@v4 with: path: app/src/main/assets/linuxfs - key: preloads-${{ env.PRELOAD_CACHE_VERSION }}-${{ hashFiles('app/src/main/cpp/fakeinput_steam.cpp', 'tools/linuxfs/preload/*.c', 'tools/linuxfs/overlay/usr/local/bin/*', 'tools/linuxfs/overlay/usr/bin/**', 'tools/linuxfs/desktop/*', 'tools/gamescope/release.env', 'tools/mangoapp/*') }} + key: preloads-${{ env.PRELOAD_CACHE_VERSION }}-${{ hashFiles('app/src/main/cpp/fakeinput_steam.cpp', 'tools/linuxfs/preload/*.c', 'tools/linuxfs/overlay/usr/local/bin/*', 'tools/linuxfs/overlay/usr/bin/**', 'tools/linuxfs/desktop/*', 'tools/gamescope/release.env', 'tools/wlroots/release.env', 'tools/mangoapp/*') }} - name: Build libfakeinput.so and libblsession.so (glibc aarch64) if: steps.preloads.outputs.cache-hit != 'true' @@ -191,6 +191,15 @@ jobs: test -f "$D/usr/local/bin/gamescope" rm gamescope.tzst fi + # The patched wlroots for the desktop's GPU renderers (tools/wlroots), the same way. + if [ -f tools/wlroots/release.env ]; then + . tools/wlroots/release.env + gh release download "${WLROOTS_TAG}" -R "${GITHUB_REPOSITORY}" -p wlroots.tzst -O wlroots.tzst + echo "${WLROOTS_SHA256} wlroots.tzst" | sha256sum -c - + tar --use-compress-program=unzstd -xf wlroots.tzst -C "$D" + test -f "$D/usr/local/lib/droiddeck-wlroots/libwlroots-0.20.so" + rm wlroots.tzst + fi # Valve's aarch64 mangoapp for Deck mode's performance overlay, plus the five libraries the # runtime lacks, pinned by sha256 in tools/mangoapp/packages.txt. Stored under the SONAME # each is loaded by, as real files: assets cannot carry symlinks. @@ -212,6 +221,8 @@ jobs: ls -l "$M" # And the desktop's launcher and labwc defaults, staged over the desktop package's copies. install -Dm644 tools/linuxfs/desktop/droiddeck-desktop "$D/usr/local/bin/droiddeck-desktop" + install -Dm644 tools/linuxfs/desktop/droiddeck-gpu "$D/usr/local/bin/droiddeck-gpu" + install -Dm644 tools/linuxfs/desktop/droiddeck-desktop-gpu "$D/usr/local/bin/droiddeck-desktop-gpu" install -Dm644 tools/linuxfs/desktop/autostart "$D/etc/xdg/labwc/autostart" install -Dm644 tools/linuxfs/desktop/rc.xml "$D/etc/xdg/labwc/rc.xml" install -Dm644 tools/linuxfs/desktop/panel.conf "$D/etc/xdg/lxqt/panel.conf" diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt index 78b09fc5ec..a517693cc5 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt @@ -56,6 +56,9 @@ object SessionFiles { // staging them into a runtime without it would make the desktop look present when it is not. val desktop = arrayOf( "usr/local/bin/droiddeck-desktop" to "usr/local/bin/droiddeck-desktop", + // Games and emulators from the menu, full screen in a gamescope of their own. + "usr/local/bin/droiddeck-gpu" to "usr/local/bin/droiddeck-gpu", + "usr/local/bin/droiddeck-desktop-gpu" to "usr/local/bin/droiddeck-desktop-gpu", "etc/xdg/labwc/autostart" to "etc/xdg/labwc/autostart", "etc/xdg/labwc/rc.xml" to "etc/xdg/labwc/rc.xml", "etc/xdg/lxqt/panel.conf" to "etc/xdg/lxqt/panel.conf", @@ -75,9 +78,13 @@ object SessionFiles { "usr/local/lib/mangoapp/libtraceevent.so.1", "usr/local/lib/mangoapp/libtracefs.so.1", ).map { it to it } + // The patched wlroots (tools/wlroots) the desktop loads for its vulkan / gles2 renderers. + val wlroots = if (File(root, "usr/bin/labwc").isFile) { + arrayOf("usr/local/lib/droiddeck-wlroots/libwlroots-0.20.so" to "usr/local/lib/droiddeck-wlroots/libwlroots-0.20.so") + } else emptyArray() val optional = (arrayOf( "usr/local/bin/gamescope" to "usr/local/bin/gamescope", - ) + mangoapp).filter { (asset, _) -> + ) + wlroots + mangoapp).filter { (asset, _) -> val dir = asset.substringBeforeLast('/') runCatching { context.assets.list("linuxfs/$dir")?.contains(asset.substringAfterLast('/')) == true }.getOrDefault(false) } diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt index 3555e4e7c8..8efbe7156a 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt @@ -330,9 +330,10 @@ object SessionPrefs { } /** - * What the desktop shell composites with: pixman (software, the default - the Adreno stand-in - * is not a DRM render node, so labwc's gbm allocator cannot use it), or gles2 / vulkan for a - * device that has a real node. `Download/droiddeck-wlr-renderer` still overrides it. + * What the desktop shell composites with: pixman (software, the default), or vulkan / gles2 on + * the GPU - those need the app's patched wlroots (tools/wlroots), since the Adreno stand-in is + * not a DRM device and stock wlroots cannot allocate on it. `Download/droiddeck-wlr-renderer` + * still overrides it. */ fun desktopRenderer(context: Context): String = prefs(context).getString("desktopRenderer", "pixman") ?: "pixman" diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt index 07241da903..d74caa21f8 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt @@ -407,12 +407,11 @@ class SessionService : Service() { } SessionState.fakeInputDir = fakeInputDir } - // The desktop is wlroots (labwc), and wlroots allocates its buffers through gbm on a real - // DRM render node. Ours is a KGSL stand-in that gbm cannot use - labwc dies at "unable to - // create allocator" - so the desktop shell is composited by pixman (software, a shm - // allocator, no DRM). Accelerated clients on it pay a CPU copy; a 2D emulator does not - // notice, a demanding one does. droiddeck-wlr-renderer in Downloads (pixman/vulkan/gles2) - // overrides it, for trying acceleration on a device that has a real node. + // The desktop is wlroots (labwc). Stock wlroots allocates through gbm on a real DRM render + // node, and ours is a KGSL stand-in - labwc died at "unable to create allocator" - so the + // default is pixman (software, shm), where a Vulkan program cannot draw at all; those run + // through droiddeck-gpu instead. vulkan / gles2 use the app's patched wlroots and fall back + // to pixman by themselves. droiddeck-wlr-renderer in Downloads overrides the choice. if (SessionState.mode == MODE_DESKTOP) { val override = File(Environment.getExternalStorageDirectory(), "Download/droiddeck-wlr-renderer") .takeIf { it.isFile }?.let { FileUtils.readString(it)?.trim() } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/ModeSettingsDialog.kt b/app/src/main/java/com/droiddeck/launcher/ui/ModeSettingsDialog.kt index 8c3d99abb1..9183c0781e 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/ModeSettingsDialog.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/ModeSettingsDialog.kt @@ -322,8 +322,11 @@ fun ModeSettingsPage(s: ModeSettings, a: ModeSettingsActions) { if (!steam && s.renderer != null) SettingsGroup("Renderer") { ChoiceRow( host, "renderer", "Desktop renderer", "Composites the desktop.", - listOf("pixman" to "pixman - software", "gles2" to "gles2", "vulkan" to "vulkan"), s.renderer, - note = "GLES2 and Vulkan require a DRM render node, unavailable on most devices.", onPick = a.onRenderer, + listOf("pixman" to "pixman - software", "vulkan" to "vulkan - GPU (experimental)", "gles2" to "gles2 - GPU (experimental)"), s.renderer, + note = "On Vulkan, programs on the desktop draw with the GPU in their own windows; if it cannot start, " + + "the desktop comes up on pixman. On pixman, games and emulators from the menu open full screen " + + "on the GPU instead (right-click one for a desktop window).", + onPick = a.onRenderer, ) } } diff --git a/tools/linuxfs/desktop/droiddeck-desktop b/tools/linuxfs/desktop/droiddeck-desktop index 8a90ef8d19..66136d05fb 100644 --- a/tools/linuxfs/desktop/droiddeck-desktop +++ b/tools/linuxfs/desktop/droiddeck-desktop @@ -14,9 +14,25 @@ export QT_QPA_PLATFORMTHEME=lxqt # The prefs for this are only advisory and a profile can override them; the environment is not. export MOZ_DISABLE_CONTENT_SANDBOX=1 MOZ_DISABLE_GMP_SANDBOX=1 MOZ_DISABLE_RDD_SANDBOX=1 export MOZ_DISABLE_SOCKET_PROCESS_SANDBOX=1 MOZ_DISABLE_UTILITY_SANDBOX=1 -# wlroots allocates through gbm on a real DRM node, which the KGSL stand-in is not: pixman -# (software, shm) is what boots here. BL_WLR_RENDERER=vulkan/gles2 tries acceleration. +# pixman composites in software and gives programs shm buffers only, so a Vulkan program on it +# fails at its swapchain. vulkan (or gles2) composite on the GPU and give programs DMA-BUFs, but +# stock wlroots allocates through gbm on a real DRM node and the KGSL stand-in is not one ("unable +# to create allocator"): they need the app's patched wlroots (tools/wlroots), whose Vulkan +# allocator exports the buffers instead. BL_WLR_RENDERER picks; see the fallback at the end. export WLR_RENDERER=${BL_WLR_RENDERER:-pixman} +WLROOTS_DIR=/usr/local/lib/droiddeck-wlroots +BASE_LD_LIBRARY_PATH=${LD_LIBRARY_PATH:-} +use_renderer() { + export WLR_RENDERER=$1 + if [ "$1" != pixman ] && [ -f "$WLROOTS_DIR/libwlroots-0.20.so" ]; then + export LD_LIBRARY_PATH=$WLROOTS_DIR${BASE_LD_LIBRARY_PATH:+:$BASE_LD_LIBRARY_PATH} + elif [ -n "$BASE_LD_LIBRARY_PATH" ]; then + export LD_LIBRARY_PATH=$BASE_LD_LIBRARY_PATH + else + unset LD_LIBRARY_PATH + fi +} +use_renderer "$WLR_RENDERER" # The desktop's own log, beside the session's, so labwc and the programs on it can be read apart # from the session that started them. The session log keeps the milestones either way. if [ -n "${BL_DEBUG_DIR:-}" ]; then @@ -85,12 +101,38 @@ export BL_DESKTOP=1 ln -sf /usr/local/bin/bannerlator-steam-launch /usr/local/bin/steamdeck-steam 2>/dev/null ln -sf /usr/local/bin/bannerlator-steam-shim /usr/local/bin/steam 2>/dev/null python3 /usr/local/bin/bannerlator-desktop-games 2>&1 | sed 's/^/[games] /' +# Games and emulators from the menu run through droiddeck-gpu (their own gamescope on the app's +# compositor) while the desktop is on pixman, where they could not draw with the GPU. +# BL_DESKTOP_GPU=on/off overrides; each entry keeps "Run in a desktop window" either way. +gpu_entries() { python3 /usr/local/bin/droiddeck-desktop-gpu 2>&1 | sed 's/^/[gpu] /'; } +gpu_entries -echo "== droiddeck-desktop: labwc ($WLR_RENDERER) on $WAYLAND_DISPLAY" # ~/.droiddeck-desktop-debug: labwc's own debug log (views and layer surfaces mapping) too. DEBUG=; [ -f "$HOME/.droiddeck-desktop-debug" ] && DEBUG=-d -# A session bus: the panel's menus, the file manager and the portal all expect one. -if command -v dbus-run-session >/dev/null 2>&1; then - exec dbus-run-session -- labwc $DEBUG -C "$HOME/.config/labwc" +run_labwc() { + echo "== droiddeck-desktop: labwc ($WLR_RENDERER${LD_LIBRARY_PATH:+, wlroots from $WLROOTS_DIR}) on $WAYLAND_DISPLAY" + # A session bus: the panel's menus, the file manager and the portal all expect one. + if command -v dbus-run-session >/dev/null 2>&1; then + dbus-run-session -- labwc $DEBUG -C "$HOME/.config/labwc" + else + labwc $DEBUG -C "$HOME/.config/labwc" + fi +} +if [ "$WLR_RENDERER" = pixman ]; then + run_labwc + exit $? fi -exec labwc $DEBUG -C "$HOME/.config/labwc" +# A GPU renderer this device cannot start ends the desktop within a second or two; rather than a +# session that closes at once, the desktop comes up on pixman and the log says why. +started=$(date +%s) +run_labwc +rc=$? +if [ "$rc" -ne 0 ] && [ $(( $(date +%s) - started )) -lt 15 ]; then + echo "== droiddeck-desktop: labwc on $WLR_RENDERER stopped at start (exit $rc); starting on pixman instead" + use_renderer pixman + find "$XDG_RUNTIME_DIR" -mindepth 1 -maxdepth 1 ! -name 'wayland-0*' ! -name 'keymap.xkb' -exec rm -rf {} + 2>/dev/null + gpu_entries + run_labwc + rc=$? +fi +exit $rc diff --git a/tools/linuxfs/desktop/droiddeck-desktop-gpu b/tools/linuxfs/desktop/droiddeck-desktop-gpu new file mode 100644 index 0000000000..08d280aecf --- /dev/null +++ b/tools/linuxfs/desktop/droiddeck-desktop-gpu @@ -0,0 +1,123 @@ +#!/usr/bin/python3 +# The desktop's games and emulators, started on the GPU. Every menu entry whose categories say +# Game or Emulator gets a copy of its own in ~/.local/share/applications (which wins over +# /usr/share/applications) whose Exec runs it through droiddeck-gpu: full screen on the app's +# compositor, where Vulkan and OpenGL work. The entry keeps the old way as a second action, "Run in +# a desktop window". Run at every desktop start; copies whose original is gone, or all of them when +# the switch is off, are removed. +# +# BL_DESKTOP_GPU=auto (default: on while the desktop is composited by pixman) | on | off +import os +import re + +HOME = os.environ.get("HOME", "/root") +SYSTEM_DIR = "/usr/share/applications" +USER_DIR = os.path.join(HOME, ".local/share/applications") +MARK = "X-DroidDeck-GPU=1" +# The client and its games already hand themselves to the app (bannerlator-steam-launch). +SKIP = re.compile(r"^(steam|droiddeck-game-|droiddeck-steam|steamdeck-)", re.I) + + +def enabled(): + mode = os.environ.get("BL_DESKTOP_GPU", "auto").strip().lower() + if mode in ("on", "1", "yes", "true"): + return True + if mode in ("off", "0", "no", "false"): + return False + return os.environ.get("WLR_RENDERER", "pixman") == "pixman" + + +def main_group(text): + """The [Desktop Entry] group's lines, and everything after it.""" + lines = text.splitlines() + start = next((i for i, l in enumerate(lines) if l.strip() == "[Desktop Entry]"), None) + if start is None: + return None, None, None + end = next((i for i in range(start + 1, len(lines)) if lines[i].startswith("[")), len(lines)) + return lines[:start + 1], lines[start + 1:end], lines[end:] + + +def wrap(text): + head, group, rest = main_group(text) + if group is None: + return None + keys = {} + for l in group: + if "=" in l and not l.lstrip().startswith("#"): + k, v = l.split("=", 1) + keys.setdefault(k.strip(), v) + cats = set(filter(None, keys.get("Categories", "").split(";"))) + if not cats & {"Game", "Emulator"}: + return None + if keys.get("Type", "Application") != "Application" or keys.get("NoDisplay", "").lower() == "true": + return None + exec_line = keys.get("Exec", "").strip() + if not exec_line or exec_line.startswith("droiddeck-gpu "): + return None + out_group = [] + actions = keys.get("Actions", "") + for l in group: + k = l.split("=", 1)[0].strip() if "=" in l else None + if k == "Exec": + out_group.append("Exec=droiddeck-gpu " + exec_line) + elif k == "Actions": + continue + else: + out_group.append(l) + while out_group and not out_group[-1].strip(): + out_group.pop() + out_group.append("Actions=" + ("droiddeck-window;" + actions if actions else "droiddeck-window;")) + out_group.append(MARK) + window = ["", "[Desktop Action droiddeck-window]", "Name=Run in a desktop window", "Exec=" + exec_line] + # The entry's own actions (new window, a different mode...) run on the GPU as well. + wrapped_rest = [] + for l in rest: + if l.startswith("Exec=") and not l[5:].startswith("droiddeck-gpu "): + l = "Exec=droiddeck-gpu " + l[5:] + wrapped_rest.append(l) + if wrapped_rest and wrapped_rest[0].strip(): + wrapped_rest.insert(0, "") + return "\n".join(head + out_group + window + wrapped_rest) + "\n" + + +def ours(path): + try: + with open(path, encoding="utf-8", errors="replace") as f: + return MARK in f.read() + except OSError: + return False + + +def main(): + on = enabled() + os.makedirs(USER_DIR, exist_ok=True) + written = set() + if on and os.path.isdir(SYSTEM_DIR): + for name in sorted(os.listdir(SYSTEM_DIR)): + if not name.endswith(".desktop") or SKIP.match(name): + continue + target = os.path.join(USER_DIR, name) + # A copy the user made or edited themselves is theirs. + if os.path.exists(target) and not ours(target): + continue + try: + with open(os.path.join(SYSTEM_DIR, name), encoding="utf-8", errors="replace") as f: + text = wrap(f.read()) + except OSError: + continue + if text is None: + continue + with open(target + ".tmp", "w", encoding="utf-8") as f: + f.write(text) + os.replace(target + ".tmp", target) + written.add(name) + removed = 0 + for name in os.listdir(USER_DIR): + path = os.path.join(USER_DIR, name) + if name.endswith(".desktop") and name not in written and ours(path): + os.remove(path) + removed += 1 + print(f"droiddeck-desktop-gpu: {'on' if on else 'off'}, {len(written)} entries on the GPU, {removed} removed") + + +main() diff --git a/tools/linuxfs/desktop/droiddeck-gpu b/tools/linuxfs/desktop/droiddeck-gpu new file mode 100644 index 0000000000..ff6fab7f0f --- /dev/null +++ b/tools/linuxfs/desktop/droiddeck-gpu @@ -0,0 +1,32 @@ +#!/bin/bash +# Runs a program from the desktop on the GPU: in a gamescope of its own that draws straight into +# the app's compositor (wayland-0), not inside labwc. The desktop is composited by pixman, and +# pixman gives its windows no DMA-BUFs - a Vulkan program there fails at its swapchain +# (VK_ERROR_SURFACE_LOST, RPCS3's error). Here it gets the same path as a program started from the +# app's rail: full screen over the desktop until it exits, then the desktop is back. +# droiddeck-gpu [args...] +set -u +[ $# -gt 0 ] || { echo "usage: droiddeck-gpu [args...]" >&2; exit 64; } +if [ -n "${BL_DEBUG_DIR:-}" ]; then + exec >>"$BL_DEBUG_DIR/desktop.log" 2>&1 +fi +width=${BL_WIDTH:-1280} +height=${BL_HEIGHT:-720} +args=(--backend wayland --expose-wayland -f -W "$width" -H "$height" -w "$width" -h "$height") +if [ "${BL_FPS:-0}" -gt 0 ]; then + args+=(-r "$BL_FPS") +elif [ "${BL_REFRESH:-0}" -gt 0 ]; then + args+=(-r "$BL_REFRESH") +fi +[ "${BL_HDR:-0}" = 1 ] && args+=(--hdr-enabled) +echo "== droiddeck-gpu $(date -u +%H:%M:%S): $* (gamescope ${width}x${height} on wayland-0)" +# gamescope is a client of the app's compositor, beside labwc; the program inside it reaches it +# through gamescope's own Xwayland, as in a Steam session. +export WAYLAND_DISPLAY=wayland-0 +export GDK_BACKEND=x11 QT_QPA_PLATFORM=xcb SDL_VIDEODRIVER=x11 +# AppImages cannot mount under proot; the menu entries already say so, a hand-typed one may not. +export APPIMAGE_EXTRACT_AND_RUN=1 +gamescope "${args[@]}" -- "$@" +rc=$? +echo "== droiddeck-gpu $(date -u +%H:%M:%S): exited $rc" +exit $rc