From 1ab3e5a123109704789c2768e11a4f9af515821d Mon Sep 17 00:00:00 2001 From: Berke Güzel Date: Wed, 14 Oct 2026 13:13:52 +0300 Subject: Redefine public and rendering APIs with explicit contracts --- tests/wayland/test_reconnect.c | 57 ++++++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 22 deletions(-) (limited to 'tests/wayland') diff --git a/tests/wayland/test_reconnect.c b/tests/wayland/test_reconnect.c index 0c42043..737afb2 100644 --- a/tests/wayland/test_reconnect.c +++ b/tests/wayland/test_reconnect.c @@ -4,6 +4,7 @@ #include #include #include +#include #if TEST_VULKAN #include #endif @@ -11,7 +12,7 @@ #include #include -#define CHECK(cond, what) do { if (!(cond)) { const char *why; yasl_get_error(&why); \ +#define CHECK(cond, what) do { if (!(cond)) { const char *why; why = strerror(errno); \ fprintf(stderr, "reconnect: %s failed at line %d (%s)\n", what, __LINE__, why); exit(1); } } while (0) typedef void (*clear_color_fn)(float, float, float, float); @@ -34,13 +35,13 @@ static yasl_event expect(enum yasl_event_type type, yasl_surface *surface, int t static void draw_gl(float red) { - CHECK(yasl_gl_make_current(gl), "binding GL"); + CHECK((yasl_gl_make_current(gl) == 0), "binding GL"); clear_color_fn clear_color = (clear_color_fn)yasl_gl_get_proc_address("glClearColor"); clear_fn clear = (clear_fn)yasl_gl_get_proc_address("glClear"); CHECK(clear_color && clear, "loading GL procedures"); clear_color(red, 0.2f, 0.3f, 1); clear(0x00004000u); - CHECK(yasl_gl_swap_buffers(gl), "swapping GL buffers"); + CHECK((yasl_gl_present(gl) == 0), "swapping GL buffers"); } #if TEST_VULKAN @@ -57,60 +58,72 @@ static VkSurfaceKHR vulkan_surface(void) int main(void) { - CHECK(yasl_set_backend(YASL_BACKEND_WAYLAND), "selecting Wayland"); - cpu = yasl_surface_create("reconnect CPU", 160, 120, YASL_SURFACE_CPU); + CHECK((yasl_init(&(struct yasl_options){.backend=YASL_BACKEND_WAYLAND}) == 0), "selecting Wayland"); + cpu = yasl_surface_create(&(struct yasl_surface_options){.title="reconnect CPU", .width=160, .height=120, .type=YASL_SURFACE_CPU, .flags=0}); CHECK(cpu, "creating CPU window"); - gl = yasl_gl_surface_create("reconnect GL", 160, 120, 0, NULL, NULL); + gl = yasl_gl_surface_create(&(struct yasl_surface_options){.title="reconnect GL", .width=160, .height=120, .type=YASL_SURFACE_NATIVE, .flags=0}, NULL); CHECK(gl, "creating GL window"); expect(YASL_EVENT_REFRESH, cpu, 5000); int w, h; - uint32_t *pixels = yasl_cpu_pixels(cpu, &w, &h); + struct yasl_cpu_buffer buffer; + CHECK(yasl_cpu_acquire(cpu, &buffer) == 0, "acquire CPU buffer"); + w=buffer.width; h=buffer.height; + uint32_t *pixels = buffer.pixels; CHECK(pixels, "getting pixels"); for (size_t i = 0; i < (size_t)w * h; i++) pixels[i] = 0xff336699u; - CHECK(yasl_cpu_present(cpu), "presenting"); + CHECK((yasl_cpu_present(cpu, &buffer) == 0), "presenting"); draw_gl(0.8f); #if TEST_VULKAN - CHECK(yasl_vulkan_init(vkGetInstanceProcAddr), "loading Vulkan"); - vk = yasl_surface_create("reconnect Vulkan", 160, 120, 0); + CHECK((yasl_vulkan_init(vkGetInstanceProcAddr) == 0), "loading Vulkan"); + vk = yasl_surface_create(&(struct yasl_surface_options){.title="reconnect Vulkan", .width=160, .height=120, .type=YASL_SURFACE_NATIVE, .flags=0}); CHECK(vk, "creating Vulkan window"); - uint32_t count = 0; - const char *const *extensions = yasl_vulkan_instance_extensions(vk, &count); + struct yasl_vulkan_requirements requirements; + CHECK(yasl_vulkan_get_requirements(vk, &requirements) == 0, "Vulkan requirements"); VkInstanceCreateInfo info = {.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, - .enabledExtensionCount = count, .ppEnabledExtensionNames = extensions}; + .flags = requirements.flags, .enabledExtensionCount = requirements.extension_count, + .ppEnabledExtensionNames = requirements.extensions}; CHECK(vkCreateInstance(&info, NULL, &instance) == VK_SUCCESS, "creating Vulkan instance"); - VkSurfaceKHR surface = vulkan_surface(); + CHECK(vulkan_surface() != VK_NULL_HANDLE, "Vulkan handle"); #endif puts("ready"); fflush(stdout); expect(YASL_EVENT_DISPLAY_LOST, NULL, 10000); + struct yasl_wayland_handles native; + CHECK(yasl_wayland_get_native(cpu, &native) == -ENOTCONN && !native.display && !native.surface, + "rejecting native handles while offline"); + CHECK(yasl_cpu_acquire(cpu, &buffer) == -ENOTCONN, "rejecting CPU acquisition while offline"); + CHECK(yasl_gl_make_current(gl) == -ENOTCONN, "rejecting GL binding while offline"); #if TEST_VULKAN - vkDestroySurfaceKHR(instance, surface, NULL); + CHECK(yasl_vulkan_destroy_surface(vk) == 0, "destroying Vulkan surface"); #endif - CHECK(!yasl_surface_create("offline", 64, 64, YASL_SURFACE_CPU), "refusing windows while offline"); - CHECK(yasl_get_error(NULL) == YASL_ERROR_DISCONNECTED, "reporting the disconnect"); + CHECK(!yasl_surface_create(&(struct yasl_surface_options){.title="offline", .width=64, .height=64, .type=YASL_SURFACE_CPU, .flags=0}), "refusing windows while offline"); + CHECK(errno == ENOTCONN, "reporting the disconnect"); puts("lost"); fflush(stdout); expect(YASL_EVENT_DISPLAY_RESTORED, NULL, 15000); + CHECK(yasl_wayland_get_native(cpu, &native) == 0 && native.display && native.surface, + "querying restored native handles"); expect(YASL_EVENT_REFRESH, cpu, 5000); int rw, rh; - pixels = yasl_cpu_pixels(cpu, &rw, &rh); + CHECK(yasl_cpu_acquire(cpu, &buffer) == 0, "reacquire CPU buffer"); + rw=buffer.width; rh=buffer.height; pixels=buffer.pixels; CHECK(pixels && rw == w && rh == h, "keeping the window size"); CHECK(pixels[0] == 0xff336699u && pixels[(size_t)w * h - 1] == 0xff336699u, "keeping the pixels"); - CHECK(yasl_cpu_present(cpu), "presenting after reconnect"); + CHECK((yasl_cpu_present(cpu, &buffer) == 0), "presenting after reconnect"); draw_gl(0.1f); #if TEST_VULKAN - surface = vulkan_surface(); - vkDestroySurfaceKHR(instance, surface, NULL); + CHECK(vulkan_surface() != VK_NULL_HANDLE, "restored Vulkan handle"); + CHECK(yasl_vulkan_destroy_surface(vk) == 0, "destroying Vulkan surface"); vkDestroyInstance(instance, NULL); #endif /* A second window proves the new connection accepts creation. */ - CHECK(yasl_surface_create("after reconnect", 64, 64, YASL_SURFACE_CPU), "creating a window after reconnect"); + CHECK(yasl_surface_create(&(struct yasl_surface_options){.title="after reconnect", .width=64, .height=64, .type=YASL_SURFACE_CPU, .flags=0}), "creating a window after reconnect"); yasl_gl_make_current(NULL); yasl_shutdown(); puts("restored"); -- cgit v1.2.3