Files
DroidDeck/tools/tests/test_zero_copy_release_at_latch.py
T

258 lines
11 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 <poll.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.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 { int unused; };
struct wl_event_loop;
enum { WL_EVENT_READABLE = 1 };
struct wl_client;
struct wl_resource { int unused; };
struct surface;
struct dmabuf_buffer { int fd; };
typedef struct AHardwareBuffer { int unused; } AHardwareBuffer;
DEFINES
STRUCT
static struct wl_list g_bufs;
static struct wl_event_loop *g_loop;
static int g_import_failed_logged;
static int import_ok, imports, imported_flags, deferred_releases, frees, sources;
static struct wl_event_source g_source;
void wl_list_init(struct wl_list *l) { l->prev = l; l->next = l; }
void wl_list_insert(struct wl_list *l, struct wl_list *e) {
e->next = l->next; e->prev = l; l->next->prev = e; l->next = e;
}
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_IMPORT_SYNC_FILE);
++imports; imported_flags = (int)f->flags;
if (import_ok) return 0;
errno = ENOTTY; return -1;
}
#define ioctl fake_ioctl
static int droiddeck_dmabuf_fd(struct dmabuf_buffer *b) { return b->fd; }
static void droiddeck_log(const char *tag, const char *fmt, ...) { (void)tag; (void)fmt; }
static struct wl_event_source *wl_event_loop_add_fd(struct wl_event_loop *l, int fd, uint32_t m,
int (*cb)(int, uint32_t, void *), void *d) {
(void)l; (void)fd; (void)m; (void)cb; (void)d; ++sources; return &g_source;
}
static int on_fence_readable(int fd, uint32_t mask, void *data) { (void)fd; (void)mask; (void)data; return 0; }
static void send_deferred_release(struct ahb_buf *ab) { ++deferred_releases; ab->release_pending = 0; }
static void maybe_free(struct ahb_buf *ab) { (void)ab; ++frees; }
FUNCTIONS
static void reset(void) { import_ok = imports = imported_flags = deferred_releases = frees = sources = 0; }
static void run(int display, int client_allocated, int can_import, int fence_signalled) {
reset();
wl_list_init(&g_bufs);
struct dmabuf_buffer b = {.fd = 7};
struct ahb_buf ab;
memset(&ab, 0, sizeof(ab));
ab.id = 42; ab.b = &b; ab.display = display; ab.client_allocated = client_allocated; ab.on_layer = 1; ab.release_pending = 1; ab.fence_fd = -1;
wl_list_insert(&g_bufs, &ab.link);
int fds[2]; assert(pipe(fds) == 0);
if (fence_signalled) assert(write(fds[1], "x", 1) == 1);
import_ok = can_import;
handle_released(42, fds[0]);
const int at_latch = !display || client_allocated;
assert(!ab.on_layer && imports == at_latch);
if (at_latch) assert(imported_flags == DROIDDECK_DMA_BUF_SYNC_READ);
if ((at_latch && can_import) || fence_signalled) {
assert(deferred_releases == 1 && frees == 1 && !ab.fence_src && ab.fence_fd == -1);
errno = 0; assert(fcntl(fds[0], F_GETFD) == -1 && errno == EBADF);
} else {
assert(deferred_releases == 0 && sources == 1 && ab.fence_src == &g_source && ab.fence_fd == fds[0]);
close(fds[0]);
}
close(fds[1]);
}
int main(int argc, char **argv) {
assert(argc == 5);
run(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]), atoi(argv[4]));
return 0;
}
'''
class ZeroCopyReleaseAtLatchTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cc = shutil.which(os.environ.get('CC', 'cc'))
if not cc:
raise AssertionError('C compiler required')
source_dir = Path(os.environ.get('DROIDDECK_DMABUF_TEST_SOURCE', SRC))
ahb = (source_dir / '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()
functions = '\n'.join(function(ahb, n) for n in ('find_buf', 'handle_released'))
source = HARNESS.replace('DEFINES', '#include <fcntl.h>\n#include <sys/ioctl.h>\n' + defines)
source = source.replace('STRUCT', struct).replace('FUNCTIONS', functions)
cls.tmp = tempfile.TemporaryDirectory(prefix='droiddeck-release-latch-')
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',
'-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, display, client_allocated, can_import, fence_signalled):
result = subprocess.run([str(self.binary), str(display), str(client_allocated), str(can_import),
str(fence_signalled)],
capture_output=True, text=True, timeout=10)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
def test_display_buffer_released_at_latch_when_fence_attaches(self):
self.scenario(1, 1, 1, 0)
def test_swapchain_buffer_released_at_latch_when_fence_attaches(self):
self.scenario(0, 0, 1, 0)
def test_display_buffer_waits_for_fence_when_kernel_cannot_attach(self):
self.scenario(1, 1, 0, 0)
def test_swapchain_buffer_waits_for_fence_when_kernel_cannot_attach(self):
self.scenario(0, 0, 0, 0)
def test_signalled_fence_releases_without_waiting(self):
self.scenario(1, 1, 0, 1)
def test_forwarded_display_buffer_waits_for_fence_even_when_kernel_can_attach(self):
self.scenario(1, 0, 1, 0)
def test_forwarded_display_buffer_with_signalled_fence_releases_without_waiting(self):
self.scenario(1, 0, 1, 1)
MATCH_HARNESS = r'''
#define _GNU_SOURCE
#include <assert.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <wayland-util.h>
#define MOD_LINEAR 0x0000000000000000ULL
#define MOD_INVALID 0x00ffffffffffffffULL
#define MOD_QCOM_COMPRESSED 0x0500000000000001ULL
struct wl_client { pid_t pid; };
struct wl_resource { int unused; };
struct wl_event_loop { int unused; };
struct dmabuf_buffer {
int fd[4];
uint32_t offset[4], stride[4];
int n_planes;
int32_t width, height;
uint32_t format;
uint64_t modifier;
};
typedef struct AHardwareBuffer { uint32_t width, height; } AHardwareBuffer;
typedef struct { uint32_t width, height; } AHardwareBuffer_Desc;
STRUCTS
static struct wl_list g_entries;
static struct wl_event_loop *g_loop;
static int adopted = -1, adoptions;
static void AHardwareBuffer_describe(const AHardwareBuffer *a, AHardwareBuffer_Desc *d) {
d->width = a->width; d->height = a->height;
}
static uint32_t ahb_format_of(uint32_t drm, uint64_t modifier) { (void)modifier; return drm; }
static void wl_client_get_credentials(struct wl_client *c, pid_t *pid, uid_t *uid, gid_t *gid) {
*pid = c->pid; *uid = 0; *gid = 0;
}
static int ahb_swapchain_adopt(struct wl_client *c, struct wl_resource *buffer, struct dmabuf_buffer *b,
AHardwareBuffer *ahb, uint64_t modifier, int client_allocated) {
(void)c; (void)buffer; (void)b; (void)ahb; (void)modifier;
++adoptions; adopted = client_allocated; return 0;
}
FUNCTIONS
int main(int argc, char **argv) {
assert(argc == 4);
int fd = open(argv[1], O_RDONLY);
assert(fd >= 0);
struct stat st;
assert(fstat(fd, &st) == 0);
static struct wl_event_loop loop;
g_loop = &loop;
wl_list_init(&g_entries);
AHardwareBuffer ahb = {1920, 1080};
struct peer allocator = {.pid = (pid_t)atoi(argv[2])};
struct entry e = {.dev = st.st_dev, .ino = st.st_ino, .ahb = &ahb, .format = 1, .stride = 7680,
.width = 1920, .height = 1080, .modifier = MOD_LINEAR, .peer = &allocator};
wl_list_insert(&g_entries, &e.link);
struct wl_client client = {.pid = (pid_t)atoi(argv[3])};
struct wl_resource resource;
struct dmabuf_buffer b = {.fd = {fd}, .stride = {7680}, .n_planes = 1, .width = 1920, .height = 1080,
.format = 1, .modifier = MOD_LINEAR};
display_buffers_match(&client, &resource, &b);
assert(adoptions == 1);
return adopted == (allocator.pid == client.pid) ? 0 : 1;
}
'''
class DisplayBufferAllocatorTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cc = shutil.which(os.environ.get('CC', 'cc'))
if not cc:
raise AssertionError('C compiler required')
text = (SRC / 'display_buffers.c').read_text()
structs = '\n'.join(re.search(r'struct %s \{.*?\n\};' % n, text, re.S).group() for n in ('peer', 'entry'))
source = MATCH_HARNESS.replace('STRUCTS', structs).replace('FUNCTIONS', function(text, 'display_buffers_match'))
source = source.replace('#include <wayland-util.h>', '#include <wayland-util.h>\n'
'void wl_list_init(struct wl_list *l) { l->prev = l; l->next = l; }\n'
'void wl_list_insert(struct wl_list *l, struct wl_list *e) {\n'
' e->next = l->next; e->prev = l; l->next->prev = e; l->next = e;\n}\n'
'int wl_list_empty(const struct wl_list *l) { return l->next == l; }')
cls.tmp = tempfile.TemporaryDirectory(prefix='droiddeck-display-match-')
cls.addClassCleanup(cls.tmp.cleanup)
probe = Path(cls.tmp.name) / 'match.c'
probe.write_text(source)
cls.binary = probe.with_suffix('')
cls.buffer = Path(cls.tmp.name) / 'buffer'
cls.buffer.write_bytes(b'\0' * 16)
built = subprocess.run([cc, '-std=c11', '-Wall', '-Wextra', '-Werror', '-Wno-unused-function', '-UNDEBUG',
'-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 adopt(self, allocator_pid, client_pid):
result = subprocess.run([str(self.binary), str(self.buffer), str(allocator_pid), str(client_pid)],
capture_output=True, text=True, timeout=10)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
def test_buffer_the_attaching_client_allocated_is_its_own(self):
self.adopt(1200, 1200)
def test_buffer_a_nested_compositor_forwards_from_another_process_is_not_its_own(self):
self.adopt(3400, 1200)
if __name__ == '__main__':
unittest.main()