mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-05 12:35:42 +00:00
Use the verified Gamescope component and preserve the safe copy fallback. Direct game frames remains experimental and off by default.
171 lines
6.7 KiB
Python
171 lines
6.7 KiB
Python
import os
|
|
from pathlib import Path
|
|
import re
|
|
import shutil
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
|
|
from test_vk_pending_completion import function
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
SRC = ROOT / 'app/src/main/cpp/waylandcomp/src'
|
|
|
|
HARNESS = r'''
|
|
#define _GNU_SOURCE
|
|
#include <assert.h>
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <poll.h>
|
|
#include <stdint.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/ioctl.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
#include <wayland-util.h>
|
|
struct wl_listener { struct wl_list link; void (*notify)(struct wl_listener *, void *); };
|
|
struct wl_event_source;
|
|
struct wl_client;
|
|
struct wl_resource;
|
|
struct surface { int unused; };
|
|
struct vkp_draw { int unused; };
|
|
struct dmabuf_buffer { int fd; void *slot; };
|
|
typedef struct AHardwareBuffer { int unused; } AHardwareBuffer;
|
|
DEFINES
|
|
STRUCT
|
|
static unsigned g_stat_zero_copy;
|
|
static int g_export_failed_logged;
|
|
static struct surface *g_announced;
|
|
static volatile int64_t g_last_zero_copy_ns;
|
|
static int explicit_proof;
|
|
static int export_ok, exports, presents, presented_acquire, logs, polls;
|
|
static int fake_ioctl(int fd, unsigned long req, void *arg) {
|
|
struct droiddeck_dma_buf_sync_file *f = arg;
|
|
(void)fd;
|
|
assert(req == DROIDDECK_DMA_BUF_IOCTL_EXPORT_SYNC_FILE && f->flags == DROIDDECK_DMA_BUF_SYNC_READ);
|
|
++exports;
|
|
if (!export_ok) { errno = ENOTTY; return -1; }
|
|
f->fd = dup(0);
|
|
return 0;
|
|
}
|
|
#define ioctl fake_ioctl
|
|
static int fake_poll(struct pollfd *p, nfds_t n, int timeout) {
|
|
assert(n == 1 && p->events == POLLIN && timeout == 100);
|
|
++polls; p->revents = POLLIN; return 1;
|
|
}
|
|
#define poll fake_poll
|
|
static int64_t now_ns(void) { return 1; }
|
|
static void **droiddeck_dmabuf_ahb_slot(struct dmabuf_buffer *b) { return &b->slot; }
|
|
static int droiddeck_dmabuf_fd(struct dmabuf_buffer *b) { return b->fd; }
|
|
static int droiddeck_surface_dup_acquire(const struct surface *s, const struct dmabuf_buffer *b) {
|
|
assert(s && b);
|
|
if (explicit_proof != 1) return explicit_proof ? -2 : -1;
|
|
int fd = dup(0);
|
|
assert(fd >= 0);
|
|
return fd;
|
|
}
|
|
static const void *droiddeck_surface_color(struct surface *s) { (void)s; return NULL; }
|
|
static void droiddeck_surface_describe(struct surface *s, char *out, size_t n) { (void)s; snprintf(out, n, "game"); }
|
|
static void droiddeck_log(const char *tag, const char *fmt, ...) { (void)tag; (void)fmt; ++logs; }
|
|
static int sc_layer_present_ahb(AHardwareBuffer *a, int w, int h, int acquire, void *token, int sw, int sh,
|
|
const struct vkp_draw *d, const void *color, uint32_t format) {
|
|
(void)a; (void)w; (void)h; (void)token; (void)sw; (void)sh; (void)d; (void)color; (void)format;
|
|
++presents; presented_acquire = acquire;
|
|
if (acquire >= 0) close(acquire);
|
|
return 0;
|
|
}
|
|
FUNCTIONS
|
|
static void run(int fd, int can_export, int copyable) {
|
|
struct dmabuf_buffer b = {.fd = fd};
|
|
struct vkp_draw d;
|
|
const struct vkp_draw *draw = copyable ? &d : NULL;
|
|
struct ahb_buf ab;
|
|
struct surface s;
|
|
memset(&ab, 0, sizeof(ab));
|
|
ab.id = 7; ab.w = 64; ab.h = 64; ab.fence_fd = -1;
|
|
b.slot = &ab;
|
|
export_ok = can_export;
|
|
int r = ahb_swapchain_present(&b, &s, 64, 64, draw);
|
|
if (explicit_proof) {
|
|
assert(exports == 0 && polls == 0);
|
|
if (explicit_proof == 1) {
|
|
assert(r == 0 && presents == 1 && presented_acquire >= 0 && ab.on_layer);
|
|
assert(fcntl(presented_acquire, F_GETFD) == -1 && errno == EBADF);
|
|
} else {
|
|
assert(r == -1 && presents == 0 && !ab.on_layer);
|
|
}
|
|
} else if (fd >= 0 && can_export) {
|
|
assert(r == 0 && presents == 1 && presented_acquire >= 0 && ab.on_layer && g_stat_zero_copy == 1 && polls == 0);
|
|
} else if (copyable) {
|
|
assert(r == -1 && presents == 0 && !ab.on_layer && g_stat_zero_copy == 0 && polls == (fd >= 0));
|
|
r = ahb_swapchain_present(&b, &s, 64, 64, draw);
|
|
assert(r == -1 && presents == 0 && logs == 1);
|
|
} else {
|
|
assert(r == 0 && presents == 1 && presented_acquire == -1 && ab.on_layer && polls == (fd >= 0));
|
|
}
|
|
}
|
|
int main(int argc, char **argv) {
|
|
assert(argc == 5);
|
|
explicit_proof = atoi(argv[4]);
|
|
run(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]));
|
|
return 0;
|
|
}
|
|
'''
|
|
|
|
|
|
class ZeroCopyAcquireFenceTest(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
cc = shutil.which(os.environ.get('CC', 'cc'))
|
|
if not cc:
|
|
raise AssertionError('C compiler required')
|
|
ahb = (SRC / 'ahb_swapchain.c').read_text()
|
|
defines = '\n'.join(re.findall(r'^(?:struct droiddeck_dma_buf_sync_file \{.*?\};|#define DROIDDECK_DMA_BUF_\w+ .*)$',
|
|
ahb, re.M))
|
|
struct = re.search(r'struct ahb_buf \{.*?\n\};', ahb, re.S).group()
|
|
source = HARNESS.replace('DEFINES', defines).replace('STRUCT', struct)
|
|
source = source.replace('FUNCTIONS', function(ahb, 'ahb_swapchain_present'))
|
|
cls.tmp = tempfile.TemporaryDirectory(prefix='droiddeck-acquire-fence-')
|
|
cls.addClassCleanup(cls.tmp.cleanup)
|
|
probe = Path(cls.tmp.name) / 'probe.c'
|
|
probe.write_text(source)
|
|
cls.binary = probe.with_suffix('')
|
|
built = subprocess.run([cc, '-std=c11', '-Wall', '-Wextra', '-Werror', '-Wno-unused-function', '-UNDEBUG',
|
|
'-include', 'stdio.h', '-I', str(SRC.parent / 'prebuilt/include'), str(probe),
|
|
'-o', str(cls.binary)],
|
|
capture_output=True, text=True)
|
|
if built.returncode:
|
|
raise AssertionError(built.stdout + built.stderr)
|
|
|
|
def scenario(self, fd, can_export, copyable=1, explicit_proof=0):
|
|
result = subprocess.run([str(self.binary), str(fd), str(can_export), str(copyable), str(explicit_proof)], capture_output=True, text=True,
|
|
timeout=10)
|
|
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
|
|
|
def test_explicit_fence_goes_to_display_without_dmabuf_ioctl(self):
|
|
self.scenario(5, 0, explicit_proof=1)
|
|
|
|
def test_failed_explicit_dup_never_falls_through_to_legacy(self):
|
|
self.scenario(5, 1, explicit_proof=2)
|
|
self.scenario(5, 0, copyable=0, explicit_proof=2)
|
|
|
|
def test_exported_fence_goes_to_the_display(self):
|
|
self.scenario(5, 1)
|
|
|
|
def test_kernel_without_fence_export_falls_back_to_copy(self):
|
|
self.scenario(5, 0)
|
|
|
|
def test_buffer_without_dmabuf_fd_falls_back_to_copy(self):
|
|
self.scenario(-1, 1)
|
|
|
|
def test_uncopyable_buffer_waits_on_the_cpu_and_stays_on_the_layer(self):
|
|
self.scenario(5, 0, 0)
|
|
|
|
def test_uncopyable_buffer_without_dmabuf_fd_stays_on_the_layer(self):
|
|
self.scenario(-1, 1, 0)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|