perf: compress Quest XR captures over adb

This commit is contained in:
XxJSONDeruloxX committed 2026-10-15 16:27:39 -04:00
1 parent 15ef4b064b
commit 2f5537acef
3 files changed
+25 -10

No files matched your search

+10 -5
View File
@@ -5,6 +5,7 @@ from __future__ import annotations
import argparse
import base64
import gzip
import json
import os
import re
@@ -386,7 +387,7 @@ def input_request(args: argparse.Namespace) -> dict:
raise ControlError(f"Unknown input type {kind}", "INVALID_INPUT")
def xr_ppm_to_png(data: bytes) -> bytes:
def xr_ppm_to_png(data: bytes, linear: bool = False) -> bytes:
header = re.match(rb"P6\n([0-9]+) ([0-9]+)\n255\n", data)
if not header:
raise ControlError("Invalid XR image header", "XR_CAPTURE_FAILED")
@@ -398,7 +399,8 @@ def xr_ppm_to_png(data: bytes) -> bytes:
return struct.pack('>I', len(value)) + kind + value + struct.pack('>I', zlib.crc32(kind + value))
stride = width * 3
rows = b''.join(b'\0' + pixels[start:start + stride] for start in range(0, len(pixels), stride))
return (b'\x89PNG\r\n\x1a\n' + chunk(b'IHDR', struct.pack('>IIBBBBB', width, height, 8, 2, 0, 0, 0)) +
transfer = chunk(b'gAMA', struct.pack('>I', 100000)) if linear else chunk(b'sRGB', b'\0')
return (b'\x89PNG\r\n\x1a\n' + chunk(b'IHDR', struct.pack('>IIBBBBB', width, height, 8, 2, 0, 0, 0)) + transfer +
chunk(b'IDAT', zlib.compress(rows)) + chunk(b'IEND', b''))
@@ -436,11 +438,14 @@ def xr_capture(adb: str, serial: str, destination: Path, timeout: float) -> dict
raise ControlError(detail, 'XR_CAPTURE_FAILED')
images = {}
for eye in (('left', 'right', 'source') if metadata.get('sourceImage') else ('left', 'right')):
local = Path(temporary) / (eye + '.ppm')
pulled = run_adb(adb, serial, 'pull', remote + '-' + eye + '.ppm', str(local), timeout=120)
pulled = run_adb(adb, serial, 'exec-out', 'gzip', '-c', remote + '-' + eye + '.ppm', timeout=120)
if pulled.returncode:
raise ControlError(pulled.stderr.decode(errors='replace'), 'XR_CAPTURE_FAILED')
images[eye] = xr_ppm_to_png(local.read_bytes())
try:
pixels = gzip.decompress(pulled.stdout)
except (OSError, EOFError, zlib.error) as error:
raise ControlError('Incomplete compressed XR image', 'XR_CAPTURE_FAILED') from error
images[eye] = xr_ppm_to_png(pixels, linear=eye == 'source' and metadata.get('sourceColorSpace') == 'linear')
# Detect overlapping captures before publishing images with another request's metadata.
check = run_adb(adb, serial, 'shell', 'cat', remote + '.json')
try:
+13 -5
View File
@@ -1,6 +1,7 @@
import importlib.machinery
import importlib.util
import json
import gzip
from pathlib import Path
import struct
import subprocess
@@ -34,7 +35,12 @@ class XrCaptureTest(unittest.TestCase):
with self.subTest(data=data), self.assertRaises(ctl.ControlError):
ctl.xr_ppm_to_png(data)
def capture(self, root, failed=False, replaced=False, no_response=False, source=False):
def test_linear_source_and_display_eye_have_distinct_png_transfer_tags(self):
image = b'P6\n1 1\n255\n\x37\x37\x37'
self.assertIn(b'gAMA' + struct.pack('>I', 100000), ctl.xr_ppm_to_png(image, linear=True))
self.assertIn(b'sRGB\0', ctl.xr_ppm_to_png(image))
def capture(self, root, failed=False, replaced=False, no_response=False, source=False, truncated=False):
calls = []
token = None
reads = 0
@@ -48,8 +54,10 @@ class XrCaptureTest(unittest.TestCase):
reads += 1
stdout = json.dumps({'id': ('old' if reads == 1 or no_response or (replaced and reads > 2) else token),
'ok': not failed, 'validLayer': not failed, 'sourceImage': source}).encode()
if args[0] == 'pull':
Path(args[2]).write_bytes(b'P6\n1 1\n255\n\x12\x34\x56')
if args[0] == 'exec-out':
stdout = gzip.compress(b'P6\n1 1\n255\n\x12\x34\x56')
if truncated:
stdout = stdout[:-5]
return subprocess.CompletedProcess(args, 0, stdout, b'')
with patch.object(ctl, 'run_adb', side_effect=adb), patch.object(ctl.time, 'sleep'):
result = ctl.xr_capture('adb', 'quest', root, 0.01 if no_response else 2)
@@ -65,7 +73,7 @@ class XrCaptureTest(unittest.TestCase):
self.assertTrue((root / 'capture.json').is_file())
def test_invalid_layer_replaced_capture_and_timeout_publish_no_images(self):
for flags in ({'failed': True}, {'replaced': True}, {'no_response': True}):
for flags in ({'failed': True}, {'replaced': True}, {'no_response': True}, {'truncated': True}):
with self.subTest(flags=flags), tempfile.TemporaryDirectory() as directory:
root = Path(directory) / 'capture'
with self.assertRaises(ctl.ControlError):
@@ -77,7 +85,7 @@ class XrCaptureTest(unittest.TestCase):
root = Path(directory) / 'capture'
result, calls = self.capture(root, source=True)
self.assertEqual(result['source'], str((root / 'source.png').resolve()))
self.assertEqual(len([c for c in calls if c[0] == 'pull']), 3)
self.assertEqual(len([c for c in calls if c[0] == 'exec-out']), 3)
if __name__ == '__main__':