test_web_frames.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import os
  2. import signal
  3. import time
  4. from pathlib import Path
  5. import pytest
  6. from PIL import Image
  7. from conftest import Unsupported
  8. PAGE = Path(__file__).parent / "fixtures/webframes"
  9. # the same page with red and blue swapped
  10. SWAPPED = Path(__file__).parent / "fixtures/webframes_swapped"
  11. # the engine's web host renders on the session's Wayland display and hands its GPU buffers over (--web-frames dmabuf)
  12. SESSIONS = ["sway", "labwc", "wayfire", "niri", "hyprland", "kde", "gnome"]
  13. RED, GREEN, BLUE = (255, 0, 0), (0, 255, 0), (0, 0, 255)
  14. BROWSER = "--ignore-gpu-blocklist --disable-dev-shm-usage"
  15. def _env(engine, switches=BROWSER):
  16. # a container has no setuid chrome-sandbox, and Chromium's close() wrapper needs libcef.so ahead of libc
  17. return {
  18. "LWE_CEF_NO_SANDBOX": "1",
  19. "LWE_WEB_HOST_PRELOAD": str(Path(engine["binary"]).resolve().parent / "libcef.so"),
  20. "LWE_WEB_STATS": "1",
  21. "LWE_CEF_SWITCHES": switches,
  22. }
  23. def _page_drawn(path, swapped=False):
  24. """the fixture's three coloured corners, as the page paints them on a screen of any size"""
  25. image = Image.open(path).convert("RGB")
  26. width, height = image.size
  27. found = (
  28. image.getpixel((width // 10, height // 10)),
  29. image.getpixel((width * 9 // 10, height // 10)),
  30. image.getpixel((width // 10, height * 9 // 10)),
  31. )
  32. colours = (BLUE, GREEN, RED) if swapped else (RED, GREEN, BLUE)
  33. return all(max(abs(a - b) for a, b in zip(pixel, expected)) <= 3 for pixel, expected in zip(found, colours))
  34. def _run(desktop, engine, tmp_path, frames, switches=BROWSER, until=None, timeout=60, extra=()):
  35. log = tmp_path / "engine.log"
  36. process = desktop.client(
  37. [engine["binary"], "--assets-dir", engine["assets"], "--screen-root", desktop.output_names()[0], "--silent",
  38. "--fps", "30", "--web-frames", frames, *extra, str(PAGE)],
  39. log=log, env=_env(engine, switches),
  40. )
  41. shot = tmp_path / "shot.png"
  42. deadline = time.monotonic() + timeout
  43. drawn = False
  44. while time.monotonic() < deadline:
  45. time.sleep(2)
  46. assert process.poll() is None, f"the engine exited with {process.returncode}:\n{log.read_text()[-1500:]}"
  47. try:
  48. desktop.shot(shot)
  49. except Unsupported as error:
  50. pytest.skip(str(error))
  51. drawn = _page_drawn(shot)
  52. if drawn and (until is None or until in log.read_text(errors="replace")):
  53. break
  54. return process, log, shot, drawn
  55. @pytest.mark.parametrize("desktop", SESSIONS, indirect=True)
  56. def test_dmabuf_frames_draw_the_page(desktop, engine, tmp_path):
  57. process, log, shot, drawn = _run(desktop, engine, tmp_path, "dmabuf", until="frames arrive as dmabufs")
  58. try:
  59. text = log.read_text(errors="replace")
  60. assert "frames arrive as dmabufs" in text, f"the host sent no GPU buffer on {desktop.name}:\n{text[-1500:]}"
  61. assert "restarting the web host" not in text
  62. assert drawn, "the page's colours are not on the screen"
  63. finally:
  64. process.terminate()
  65. process.wait(10)
  66. @pytest.mark.parametrize("desktop", ["sway"], indirect=True)
  67. def test_shared_memory_frames_still_draw_the_page(desktop, engine, tmp_path):
  68. process, log, shot, drawn = _run(desktop, engine, tmp_path, "shm")
  69. try:
  70. assert "frames arrive as dmabufs" not in log.read_text(errors="replace")
  71. assert drawn
  72. finally:
  73. process.terminate()
  74. process.wait(10)
  75. @pytest.mark.parametrize("desktop", ["sway"], indirect=True)
  76. def test_a_browser_without_gpu_buffers_falls_back_to_shared_memory(desktop, engine, tmp_path):
  77. """headless Chromium cannot allocate shareable buffers: the host restarts with shared memory frames"""
  78. process, log, shot, drawn = _run(
  79. desktop, engine, tmp_path, "dmabuf", switches=BROWSER + " --ozone-platform=headless --use-angle=gl-egl",
  80. until="restarting the web host", timeout=90,
  81. )
  82. try:
  83. assert "restarting the web host with shared memory frames" in log.read_text(errors="replace")
  84. assert drawn, "the page did not show after the fallback"
  85. finally:
  86. process.terminate()
  87. process.wait(10)
  88. @pytest.mark.parametrize("desktop", ["sway"], indirect=True)
  89. def test_dmabuf_frames_follow_an_output_resize(desktop, engine, tmp_path):
  90. process, log, shot, drawn = _run(desktop, engine, tmp_path, "dmabuf", until="frames arrive as dmabufs")
  91. output = desktop.output_names()[0]
  92. try:
  93. assert drawn
  94. desktop.run(["wlr-randr", "--output", output, "--custom-mode", "1280x720"])
  95. deadline = time.monotonic() + 30
  96. size = None
  97. while time.monotonic() < deadline:
  98. time.sleep(2)
  99. desktop.shot(shot)
  100. size = Image.open(shot).size
  101. if size == (1280, 720) and _page_drawn(shot):
  102. break
  103. assert size == (1280, 720), f"the output is {size}"
  104. assert _page_drawn(shot), "the page is not drawn at the new size"
  105. assert "restarting the web host" not in log.read_text(errors="replace")
  106. finally:
  107. desktop.run(["wlr-randr", "--output", output, "--custom-mode", "1920x1080"])
  108. process.terminate()
  109. process.wait(10)
  110. @pytest.mark.parametrize("desktop", ["sway"], indirect=True)
  111. def test_dmabuf_frames_survive_a_hotswap_between_web_wallpapers(desktop, engine, tmp_path):
  112. control = tmp_path / "control"
  113. process, log, shot, drawn = _run(
  114. desktop, engine, tmp_path, "dmabuf", until="frames arrive as dmabufs", extra=("--control-file", str(control))
  115. )
  116. try:
  117. assert drawn
  118. control.write_text(f"path={SWAPPED}\n")
  119. os.kill(process.pid, signal.SIGUSR1)
  120. deadline = time.monotonic() + 40
  121. swapped = False
  122. while time.monotonic() < deadline and not swapped:
  123. time.sleep(2)
  124. desktop.shot(shot)
  125. swapped = _page_drawn(shot, swapped=True)
  126. assert swapped, "the second web wallpaper never replaced the first"
  127. text = log.read_text(errors="replace")
  128. assert text.count("frames arrive as dmabufs") == 2, "the new host did not send GPU buffers"
  129. assert "restarting the web host" not in text
  130. finally:
  131. process.terminate()
  132. process.wait(10)