Skip to content

armgit / BUI

Public Imported from GitHub
0

Blender's GPU Accelerated UI, Decoupled from it's original codebase. Supports Rust, Python, JS, React, C & C++. Twice as good as GPUI

tests: GPU start, idle GPU switch and closing a window in its frame callback

nusaturn19d95f364b04
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6703466..4bde17a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -83,6 +83,11 @@ if(BUI_BUILD_TESTS)
target_link_libraries(bui_window_smoke PRIVATE bui::kit)
add_test(NAME bui_window_smoke COMMAND bui_window_smoke)
set_tests_properties(bui_window_smoke PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 30)
+ # GPU start on a worker thread, the idle switch, and a window closed by its frame callback
+ add_executable(bui_gpu_start_test tests/gpu_start_test.c)
+ target_link_libraries(bui_gpu_start_test PRIVATE bui::kit)
+ add_test(NAME bui_gpu_start_test COMMAND bui_gpu_start_test)
+ set_tests_properties(bui_gpu_start_test PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 60)
# themes: Blender's Dark / Light presets (BK_set_theme), dark stays pixel-identical
add_executable(bui_theme_test tests/theme_test.c)
target_link_libraries(bui_theme_test PRIVATE bui::kit)
diff --git a/tests/gpu_start_test.c b/tests/gpu_start_test.c
new file mode 100644
index 0000000..faa1881
--- /dev/null
+++ b/tests/gpu_start_test.c
@@ -0,0 +1,137 @@
+/* GPU start and window frames:
+ * - offscreen: the GPU device starts on a worker thread, BUI_raster_gpu_pending
+ * reports it, and BUI_raster_gpu_poll switches the raster when it is ready;
+ * - an idle window (no input, no animation) switches to the GPU;
+ * - a frame callback that closes its window ends the loop without an X error.
+ * Each check passes, fails or is skipped (no GPU, no display). Exits 77 when
+ * all checks are skipped.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later */
+#include <stdio.h>
+#ifdef _WIN32
+# define WIN32_LEAN_AND_MEAN
+# include <windows.h>
+# define sleep_ms(ms) Sleep(ms)
+#else
+# include <unistd.h>
+# define sleep_ms(ms) usleep((ms) * 1000)
+#endif
+
+#include "BUI_kit.h"
+#include "GPU_raster.h"
+
+enum { PASS = 0, FAIL = 1, SKIP = 77 };
+
+static int offscreen_start(void)
+{
+ BuiRaster *r = BUI_raster_create(64, 64);
+ if (!BUI_raster_gpu_attach(r, NULL, 0)) {
+ BUI_raster_free(r);
+ return SKIP; /* a build without the GPU renderer */
+ }
+ const int was_pending = BUI_raster_gpu_pending(r);
+ int switched = 0;
+ for (int i = 0; i < 1000 && BUI_raster_gpu_pending(r); i++) {
+ switched |= BUI_raster_gpu_poll(r);
+ sleep_ms(5);
+ }
+ const int pending = BUI_raster_gpu_pending(r);
+ const int gpu = BUI_raster_is_gpu(r);
+ BUI_raster_free(r);
+ if (!pending && !gpu) {
+ return SKIP; /* no GPU device on this machine */
+ }
+ printf("offscreen: pending at start %d, switched %d, pending at end %d\n", was_pending, switched,
+ pending);
+ return was_pending && switched && gpu && !pending ? PASS : FAIL;
+}
+
+static void quit_on_timer(void *data, BKWindow *win, int id)
+{
+ (void)data;
+ (void)win;
+ (void)id;
+ BK_window_quit();
+}
+
+static int idle_window_switch(void)
+{
+ BKNode *root = BK_node_new("column");
+ BKContext *ctx = BK_context_new(root);
+ BKWindow *win = BK_window_new(ctx, "BUI GPU idle switch", 320, 200);
+ if (!win) {
+ BK_context_free(ctx);
+ BK_node_free(root);
+ return FAIL;
+ }
+ BKWindowCallbacks cb = {NULL, quit_on_timer, NULL, NULL};
+ BK_window_set_callbacks(win, &cb);
+ BK_window_set_gpu(win, 1);
+ BK_window_show(win);
+ BK_window_timer(win, 2000); /* no input and no animation until then */
+ BK_window_run();
+ const int gpu = BK_window_gpu(win);
+ printf("idle window: gpu %d\n", gpu);
+ BK_window_free(win);
+ BK_context_free(ctx);
+ BK_node_free(root);
+ return gpu ? PASS : FAIL;
+}
+
+static void close_on_frame(void *data, BKWindow *win)
+{
+ (void)data;
+ BK_window_close(win);
+}
+
+static int close_in_frame_callback(void)
+{
+ BKNode *root = BK_node_new("column");
+ BKContext *ctx = BK_context_new(root);
+ BKWindow *win = BK_window_new(ctx, "BUI close in frame", 320, 200);
+ if (!win) {
+ BK_context_free(ctx);
+ BK_node_free(root);
+ return FAIL;
+ }
+ BKWindowCallbacks cb = {NULL, quit_on_timer, NULL, close_on_frame};
+ BK_window_set_callbacks(win, &cb);
+ BK_window_show(win);
+ BK_window_request_frame(win, 1);
+ BK_window_timer(win, 3000); /* ends the loop if the frame never runs */
+ BK_window_run();
+ const int open = BK_window_is_open(win);
+ printf("close in frame callback: open %d\n", open);
+ BK_window_free(win);
+ /* X errors arrive later: run a second window, so a pending error ends the process here */
+ BKWindow *probe = BK_window_new(ctx, "BUI close in frame (probe)", 160, 100);
+ if (probe) {
+ BK_window_set_callbacks(probe, &cb);
+ BK_window_show(probe);
+ BK_window_timer(probe, 300);
+ BK_window_run();
+ BK_window_free(probe);
+ }
+ BK_context_free(ctx);
+ BK_node_free(root);
+ return open ? FAIL : PASS;
+}
+
+int main(void)
+{
+ const int display = BK_window_supported();
+ const int offscreen = offscreen_start();
+ const int results[] = {
+ offscreen,
+ offscreen == PASS && display ? idle_window_switch() : SKIP,
+ display ? close_in_frame_callback() : SKIP,
+ };
+ const char *names[] = {"offscreen start", "idle window switch", "close in frame callback"};
+ int failed = 0, skipped = 0;
+ for (int i = 0; i < 3; i++) {
+ printf("%-24s %s\n", names[i], results[i] == PASS ? "pass" : results[i] == SKIP ? "skip" : "FAIL");
+ failed += results[i] == FAIL;
+ skipped += results[i] == SKIP;
+ }
+ return failed ? 1 : skipped == 3 ? SKIP : PASS;
+}