RustForge

rust-kernel

Rustbelt: a hobby x86_64 kernel written in Rust

rust-kernel / scripts / gpu-check.sh
2845 bytes ยท raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env bash
# virtio-gpu read-back test: boot the interactive kernel (screenshot.sh),
# type `tile`, dump the virtio-gpu scanout as a PPM image and check pixels
# at known places against the colours user/tile.rs draws. This tests the
# whole path: user program -> /dev/fb0 ioctls -> framebuffer memory ->
# TRANSFER_TO_HOST_2D + RESOURCE_FLUSH -> QEMU's display.
#
# Usage: ./scripts/gpu-check.sh
set -euo pipefail
cd "$(dirname "$0")/.."

PPM=target/gpu-check.ppm
SESSION=$(mktemp /tmp/gpu-session.XXXXXX)
echo tile > "$SESSION"
rm -f "$PPM"
./scripts/screenshot.sh "$PPM" "$SESSION" "tile: 4 windows on 1024x768, console 61x46" > target/gpu-check.log 2>&1 \
    || { tail -20 target/gpu-check.log; echo "[gpu-check] screenshot session failed"; exit 1; }
rm -f "$SESSION"

python3 - "$PPM" <<'PY'
import sys
data = open(sys.argv[1], 'rb').read()
# PPM "P6": a text header (magic, width, height, max value), then RGB bytes.
magic, w, h, maxval, pixels = data.split(maxsplit=4)
w, h = int(w), int(h)
assert magic == b'P6' and maxval == b'255', 'not a 24-bit PPM'
def px(x, y):
    i = (y * w + x) * 3
    return tuple(pixels[i:i + 3])
def rgb(c):
    return (c >> 16, (c >> 8) & 0xff, c & 0xff)
# The layout of `tile` on 1024x768 (gaps 10, borders 3, dwindle split):
# window 0 = x 10..507 (console), window 1 = x 517..1014, y 10..379,
# window 2 = x 517..760, y 389..758, window 3 = x 770..1014, y 389..758.
def wallpaper(y):                     # tile.rs: gradient()
    mix = lambda a, b: a + (b - a) * y // 768
    return (mix(0x1e, 0x50), mix(0x1e, 0x2a), mix(0x2e, 0x78))
checks = [
    ((w, h), (1024, 768), 'screen size'),
    (px(2, 2), wallpaper(2), 'wallpaper, top left corner'),
    (px(5, 700), wallpaper(700), 'wallpaper, left edge'),
    (px(512, 300), wallpaper(300), 'gap between windows'),
    (px(11, 300), rgb(0x33ccff), 'window 0 border (focused, cyan)'),
    (px(518, 100), rgb(0x595959), 'window 1 border (gray)'),
    (px(525, 18), rgb(0x89b4fa), 'window 1 fill (blue)'),
    (px(540, 31), rgb(0x445a7d), 'window 1 "text" bar (darker blue)'),
    (px(525, 397), rgb(0xa6e3a1), 'window 2 fill (green)'),
    (px(778, 397), rgb(0xf9e2af), 'window 3 fill (yellow)'),
    (px(1020, 760), wallpaper(760), 'wallpaper, bottom right'),
]
bad = 0
for got, want, what in checks:
    ok = got == want
    bad += not ok
    print(f"[gpu-check] {'ok  ' if ok else 'FAIL'} {what}: {got}" + ('' if ok else f", expected {want}"))
# The console window: black background with text in some other colour.
console = {px(x, y) for x in range(13, 505, 3) for y in range(13, 756, 3)}
ok = (0, 0, 0) in console and len(console) > 1
bad += not ok
print(f"[gpu-check] {'ok  ' if ok else 'FAIL'} console window has black background and text ({len(console)} colours)")
sys.exit(1 if bad else 0)
PY
echo "[gpu-check] virtio-gpu output matches"