headless_render.sh 8.3 KB

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