headless_render.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. # - --screenshot-delay is capped at 5000 frames by the engine (ApplicationContext.cpp).
  27. # - CImage.cpp's puppet/effect diagnostics are one-shot logs that fire on the first draw call,
  28. # not a chosen frame.
  29. set -euo pipefail
  30. if [ "$#" -lt 2 ]; then
  31. echo "Usage: $0 <binary> <output.png> [engine args...]" >&2
  32. exit 1
  33. fi
  34. BINARY="$1"; shift
  35. OUTPUT="$1"; shift
  36. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  37. BINARY_DIR="$(cd "$(dirname "$BINARY")" && pwd)"
  38. SHIM_SRC="$SCRIPT_DIR/dbus_noop_shim.c"
  39. SHIM_SO="/tmp/dbus_noop_shim.so"
  40. if [ ! -f "$SHIM_SO" ] || [ "$SHIM_SRC" -nt "$SHIM_SO" ]; then
  41. gcc -shared -fPIC -o "$SHIM_SO" "$SHIM_SRC" -ldl
  42. fi
  43. USE_GPU=
  44. if [ -z "${HEADLESS_RENDER_SOFTWARE:-}" ]; then
  45. for node in /dev/dri/renderD*; do
  46. if [ -r "$node" ] && [ -w "$node" ]; then
  47. USE_GPU=1
  48. break
  49. fi
  50. done
  51. fi
  52. XVFB_DISPLAY="${HEADLESS_RENDER_DISPLAY:-:99}"
  53. XVFB_SOCKET="/tmp/.X11-unix/X${XVFB_DISPLAY#:}"
  54. if [ -n "$USE_GPU" ]; then
  55. SESSION=(env -u DISPLAY -u WAYLAND_DISPLAY XDG_SESSION_TYPE=headless)
  56. else
  57. SESSION=(env -u WAYLAND_DISPLAY DISPLAY="$XVFB_DISPLAY" XDG_SESSION_TYPE=x11)
  58. fi
  59. if [ -z "$USE_GPU" ] && [ ! -S "$XVFB_SOCKET" ]; then
  60. Xvfb "$XVFB_DISPLAY" -screen 0 1920x1080x24 &
  61. XVFB_PID=$!
  62. trap 'kill "$XVFB_PID" 2>/dev/null || true' EXIT
  63. # give it a moment to bind before launching anything against it
  64. for _ in $(seq 1 20); do
  65. [ -S "$XVFB_SOCKET" ] && break
  66. sleep 0.2
  67. done
  68. fi
  69. rm -f "$OUTPUT"
  70. "${SESSION[@]}" \
  71. LD_LIBRARY_PATH="$BINARY_DIR${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}" \
  72. LD_PRELOAD="$SHIM_SO" \
  73. timeout "${HEADLESS_RENDER_TIMEOUT:-60}" "$BINARY" \
  74. --window "0x0x${HEADLESS_RENDER_SIZE:-1920x1080}" \
  75. --screenshot "$OUTPUT" \
  76. --screenshot-delay "${HEADLESS_RENDER_DELAY:-3}" \
  77. "$@" &
  78. ENGINE_PID=$!
  79. # the engine keeps rendering after the screenshot is taken, so without this the run always lasts
  80. # the full timeout - stop it as soon as the file is written (set HEADLESS_RENDER_KEEP_RUNNING=1 to
  81. # let it run until the timeout instead)
  82. if [ -z "${HEADLESS_RENDER_KEEP_RUNNING:-}" ]; then
  83. while kill -0 "$ENGINE_PID" 2>/dev/null; do
  84. if [ -s "$OUTPUT" ]; then
  85. # wait for the file to stop growing so a half-written PNG isn't cut off
  86. prev=-1
  87. cur=$(stat -c %s "$OUTPUT")
  88. while [ "$cur" != "$prev" ]; do
  89. sleep 0.3
  90. prev=$cur
  91. cur=$(stat -c %s "$OUTPUT")
  92. done
  93. kill "$ENGINE_PID" 2>/dev/null || true
  94. break
  95. fi
  96. sleep 0.2
  97. done
  98. fi
  99. wait "$ENGINE_PID" || true
  100. if [ -f "$OUTPUT" ]; then
  101. echo "Wrote $OUTPUT"
  102. else
  103. echo "No screenshot was produced - check the engine's stderr output above" >&2
  104. exit 1
  105. fi