headless_render.sh 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #!/usr/bin/env bash
  2. # Renders a wallpaper headlessly and takes a screenshot, for environments with no way to see the
  3. # real screen (sandboxes, CI, this project's own dev container). When a GPU render node
  4. # (/dev/dri/renderD*) is accessible it renders there through the engine's headless EGL driver
  5. # (XDG_SESSION_TYPE=headless, no X server, a 4K scene takes ~1s instead of ~20s). Otherwise, or
  6. # with HEADLESS_RENDER_SOFTWARE=1, it falls back to Xvfb + Mesa llvmpipe software rendering.
  7. # LWE_HEADLESS_DEVICE=/dev/dri/renderDN picks the GPU when there are several.
  8. #
  9. # Usage:
  10. # tools/headless_render.sh <path-to-linux-wallpaperengine-binary> <output.png> [engine args...]
  11. #
  12. # Example:
  13. # tools/headless_render.sh build/output/linux-wallpaperengine /tmp/out.png \
  14. # --assets-dir /path/to/wallpaper_engine/assets --window 0x0x1920x1080 --fps 25 \
  15. # /path/to/workshop/item/dir
  16. #
  17. # Requirements: a GPU render node, or Xvfb and a software GL renderer (mesa llvmpipe is normally
  18. # already present alongside libgl1-mesa-dri); gcc (to build the D-Bus shim once, cached after that).
  19. #
  20. # Known limitations:
  21. # - No real D-Bus session bus is assumed - dbus_noop_shim.so (built automatically) patches the
  22. # one call path that isn't already null-safe against a missing bus.
  23. # - The engine is stopped as soon as the screenshot file appears (HEADLESS_RENDER_KEEP_RUNNING=1 disables
  24. # that and runs until HEADLESS_RENDER_TIMEOUT, default 60s).
  25. # - HEADLESS_RENDER_SIZE=WxH changes the window size (default 1920x1080).
  26. # - LWE_HEADLESS_CURSOR=x,y puts the GPU path's pointer at that fraction of the output (default 0.5,0.5).
  27. # - HEADLESS_RENDER_WEB=1 makes web wallpapers work in a container: a private Xvfb (HEADLESS_RENDER_WEB_DISPLAY,
  28. # default :96), no Chromium sandbox (LWE_CEF_NO_SANDBOX=1) and libcef.so preloaded into the host
  29. # (LWE_WEB_HOST_PRELOAD, else Chromium's close() wrapper crashes). WebGL runs on the GPU via headless ozone + ANGLE
  30. # EGL (LWE_CEF_SWITCHES overrides). Use HEADLESS_RENDER_DELAY=600 or more.
  31. # - --screenshot-delay is capped at 5000 frames by the engine (ApplicationContext.cpp).
  32. # - CImage.cpp's puppet/effect diagnostics are one-shot logs that fire on the first draw call,
  33. # not a chosen frame.
  34. set -euo pipefail
  35. if [ "$#" -lt 2 ]; then
  36. echo "Usage: $0 <binary> <output.png> [engine args...]" >&2
  37. exit 1
  38. fi
  39. BINARY="$1"; shift
  40. # the sandbox shares the desktop's user, a crashing test render would land in its crash handler (DrKonqi)
  41. ulimit -c 0
  42. OUTPUT="$1"; shift
  43. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  44. BINARY_DIR="$(cd "$(dirname "$BINARY")" && pwd)"
  45. SHIM_SRC="$SCRIPT_DIR/dbus_noop_shim.c"
  46. SHIM_SO="/tmp/dbus_noop_shim.so"
  47. if [ ! -f "$SHIM_SO" ] || [ "$SHIM_SRC" -nt "$SHIM_SO" ]; then
  48. gcc -shared -fPIC -o "$SHIM_SO" "$SHIM_SRC" -ldl
  49. fi
  50. USE_GPU=
  51. if [ -z "${HEADLESS_RENDER_SOFTWARE:-}" ]; then
  52. for node in /dev/dri/renderD*; do
  53. if [ -r "$node" ] && [ -w "$node" ]; then
  54. USE_GPU=1
  55. break
  56. fi
  57. done
  58. fi
  59. XVFB_DISPLAY="${HEADLESS_RENDER_DISPLAY:-:99}"
  60. XVFB_SOCKET="/tmp/.X11-unix/X${XVFB_DISPLAY#:}"
  61. # a private runtime dir and no desktop variables: libwayland falls back to $XDG_RUNTIME_DIR/wayland-0 when
  62. # WAYLAND_DISPLAY is unset, so a build without the headless driver would otherwise open a window on the real
  63. # desktop session (and reach its D-Bus / audio server)
  64. RUNTIME_DIR="$(mktemp -d /tmp/lwe-headless-run.XXXXXX)"
  65. chmod 700 "$RUNTIME_DIR"
  66. DBUS_PID=
  67. WEB_XVFB_PID=
  68. XVFB_PID=
  69. trap 'kill $DBUS_PID $WEB_XVFB_PID $XVFB_PID 2>/dev/null || true; rm -rf "$RUNTIME_DIR"' EXIT
  70. # the engine needs a session bus (MPRIS media source), it gets a private one
  71. BUS_ADDRESS=unix:path=/nonexistent-bus
  72. if command -v dbus-daemon >/dev/null; then
  73. DBUS_PID=$(dbus-daemon --session --fork --print-pid --address="unix:path=$RUNTIME_DIR/bus")
  74. BUS_ADDRESS="unix:path=$RUNTIME_DIR/bus"
  75. fi
  76. # HEADLESS_RENDER_PULSE_SERVER: private audio server to play into (tools/wecompare/wine/audio_sink.sh), never the desktop's
  77. PULSE=unix:/nonexistent-pulse
  78. if [ -n "${HEADLESS_RENDER_PULSE_SERVER:-}" ]; then
  79. # a local socket that is not the desktop's server, also through symlinks or bind mounts
  80. PULSE_SOCKET="${HEADLESS_RENDER_PULSE_SERVER#unix:}"
  81. if [[ "$PULSE_SOCKET" != /* || "$PULSE_SOCKET" == *[[:space:]]* ]]; then
  82. echo "HEADLESS_RENDER_PULSE_SERVER must be a unix socket path, got $HEADLESS_RENDER_PULSE_SERVER" >&2
  83. exit 1
  84. fi
  85. DESKTOP_PULSE=("${XDG_RUNTIME_DIR:-/run/user/$(id -u)}/pulse/native" "/run/user/$(id -u)/pulse/native")
  86. if [[ "${PULSE_SERVER:-}" == unix:/* ]]; then DESKTOP_PULSE+=("${PULSE_SERVER#unix:}"); fi
  87. if [[ "${PULSE_SERVER:-}" == /* ]]; then DESKTOP_PULSE+=("$PULSE_SERVER"); fi
  88. if [[ "$(realpath -m "$PULSE_SOCKET")" == /run/user/* ]]; then
  89. echo "HEADLESS_RENDER_PULSE_SERVER must not be the desktop's audio server" >&2
  90. exit 1
  91. fi
  92. if [ -S "$PULSE_SOCKET" ]; then
  93. for candidate in "${DESKTOP_PULSE[@]}"; do
  94. if [ -S "$candidate" ] && [ "$(stat -Lc %d:%i "$candidate")" = "$(stat -Lc %d:%i "$PULSE_SOCKET")" ]; then
  95. echo "HEADLESS_RENDER_PULSE_SERVER must not be the desktop's audio server" >&2
  96. exit 1
  97. fi
  98. done
  99. fi
  100. PULSE=unix:$PULSE_SOCKET
  101. fi
  102. ISOLATE=(-u WAYLAND_DISPLAY -u WAYLAND_SOCKET -u KDE_FULL_SESSION -u KDE_SESSION_VERSION
  103. -u DESKTOP_SESSION -u XDG_CURRENT_DESKTOP -u XDG_SESSION_DESKTOP XDG_RUNTIME_DIR="$RUNTIME_DIR"
  104. DBUS_SESSION_BUS_ADDRESS="$BUS_ADDRESS"
  105. PULSE_SERVER="$PULSE" PIPEWIRE_REMOTE=/nonexistent-pipewire)
  106. if [ -n "$USE_GPU" ]; then
  107. SESSION=(env -u DISPLAY "${ISOLATE[@]}" XDG_SESSION_TYPE=headless)
  108. else
  109. SESSION=(env "${ISOLATE[@]}" DISPLAY="$XVFB_DISPLAY" XDG_SESSION_TYPE=x11)
  110. fi
  111. if [ -n "${HEADLESS_RENDER_WEB:-}" ]; then
  112. WEB_DISPLAY="${HEADLESS_RENDER_WEB_DISPLAY:-:96}"
  113. if ! xdpyinfo -display "$WEB_DISPLAY" >/dev/null 2>&1; then
  114. Xvfb "$WEB_DISPLAY" -screen 0 1920x1080x24 -nolisten tcp >/dev/null 2>&1 &
  115. WEB_XVFB_PID=$!
  116. for _ in $(seq 1 20); do
  117. xdpyinfo -display "$WEB_DISPLAY" >/dev/null 2>&1 && break
  118. sleep 0.2
  119. done
  120. fi
  121. # headless ozone + ANGLE EGL for GPU WebGL, shared memory in /tmp: Docker's 64MB /dev/shm loses the context on big
  122. # uploads (3496522892)
  123. SESSION+=(DISPLAY="$WEB_DISPLAY" LWE_CEF_NO_SANDBOX=1
  124. LWE_WEB_HOST_PRELOAD="${LWE_WEB_HOST_PRELOAD:-$(dirname "$(readlink -f "$BINARY")")/libcef.so}"
  125. LWE_CEF_SWITCHES="${LWE_CEF_SWITCHES:---ozone-platform=headless --use-gl=angle --use-angle=gl-egl --ignore-gpu-blocklist --disable-dev-shm-usage}")
  126. fi
  127. if [ -z "$USE_GPU" ] && [ ! -S "$XVFB_SOCKET" ]; then
  128. Xvfb "$XVFB_DISPLAY" -screen 0 1920x1080x24 &
  129. XVFB_PID=$!
  130. # give it a moment to bind before launching anything against it
  131. for _ in $(seq 1 20); do
  132. [ -S "$XVFB_SOCKET" ] && break
  133. sleep 0.2
  134. done
  135. fi
  136. rm -f "$OUTPUT"
  137. "${SESSION[@]}" \
  138. LD_LIBRARY_PATH="$BINARY_DIR${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}" \
  139. LD_PRELOAD="$SHIM_SO" \
  140. timeout "${HEADLESS_RENDER_TIMEOUT:-60}" "$BINARY" \
  141. --window "0x0x${HEADLESS_RENDER_SIZE:-1920x1080}" \
  142. --screenshot "$OUTPUT" \
  143. --screenshot-delay "${HEADLESS_RENDER_DELAY:-3}" \
  144. "$@" &
  145. ENGINE_PID=$!
  146. # the engine keeps rendering after the screenshot is taken, so without this the run always lasts
  147. # the full timeout - stop it as soon as the file is written (set HEADLESS_RENDER_KEEP_RUNNING=1 to
  148. # let it run until the timeout instead)
  149. if [ -z "${HEADLESS_RENDER_KEEP_RUNNING:-}" ]; then
  150. while kill -0 "$ENGINE_PID" 2>/dev/null; do
  151. if [ -s "$OUTPUT" ]; then
  152. # wait for the file to stop growing so a half-written PNG isn't cut off
  153. prev=-1
  154. cur=$(stat -c %s "$OUTPUT")
  155. while [ "$cur" != "$prev" ]; do
  156. sleep 0.3
  157. prev=$cur
  158. cur=$(stat -c %s "$OUTPUT")
  159. done
  160. kill "$ENGINE_PID" 2>/dev/null || true
  161. break
  162. fi
  163. sleep 0.2
  164. done
  165. fi
  166. wait "$ENGINE_PID" || true
  167. if [ -f "$OUTPUT" ]; then
  168. echo "Wrote $OUTPUT"
  169. else
  170. echo "No screenshot was produced - check the engine's stderr output above" >&2
  171. exit 1
  172. fi