From 8c050f43b6137619421045fee7dfc6b49f14f6ac Mon Sep 17 00:00:00 2001 From: Ahmad Sayed Alsayed Ebrahim Date: Sat, 10 Oct 2026 16:47:03 +0300 Subject: [PATCH] feat(vkbridge): run the bridge server inside the app's process Android picks the GPU driver (Samsung's updatable one included) while the app starts, only for the app's process. The server as a separate executable got the plain /vendor driver: on the Tab S10+ it saw no VK_EXT_robustness2 or mutable_descriptor_type, which the capability viewer (an app) listed. Winlator's Vortek runs its server in-process for the same reason. - libvkbridge_jni.so: the same server built as a JNI library; nativeStart runs the self-test once, serves on a thread for the app's life, and points the log at each new session's folder (with the self-test summary). - VkBridgeNative / VkBridgeComponent: in-process first, the executable as the fallback; the in-app server's socket is kept between sessions. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01S1U9H1evGNjHRQ3wRUdv5V --- app/src/main/cpp/vkbridge/server_instance.c | 11 ++ app/src/main/cpp/vkbridge/server_main.c | 125 +++++++++++++++--- app/src/main/cpp/vkbridge/vkb_server.h | 2 + .../launcher/gpu/VkBridgeComponent.kt | 11 +- .../droiddeck/launcher/gpu/VkBridgeNative.kt | 46 +++++++ tools/vkbridge/build-server-android.sh | 11 +- 6 files changed, 182 insertions(+), 24 deletions(-) create mode 100644 app/src/main/java/com/droiddeck/launcher/gpu/VkBridgeNative.kt diff --git a/app/src/main/cpp/vkbridge/server_instance.c b/app/src/main/cpp/vkbridge/server_instance.c index bead3238b..4528e46f4 100644 --- a/app/src/main/cpp/vkbridge/server_instance.c +++ b/app/src/main/cpp/vkbridge/server_instance.c @@ -30,6 +30,17 @@ const vkb_pd_knowledge *vkb_pd_lookup(const vkb_dispatch *dt, VkPhysicalDevice p return NULL; } +/* What the self-test found, for a later session's log (the in-app server tests once). */ +void vkb_log_summary(void) +{ + for (int i = 0; i < vkb_npds; i++) { + const vkb_pd_knowledge *k = &vkb_pds[i]; + if (k->valid) + VKB_INFO("%s: memory sharing = %s, missing 0x%x, emulating 0x%x (from the first session's self-test)", k->name, + vkb_mem_strategy_name(k->info.strategy), k->info.missing, k->info.emu); + } +} + /* ------------------------------------------------------------------ startup self-test */ static void log_device(const vkb_dispatch *dt, VkPhysicalDevice pd, const VkPhysicalDeviceProperties *p) diff --git a/app/src/main/cpp/vkbridge/server_main.c b/app/src/main/cpp/vkbridge/server_main.c index dc948cf88..8fe6d1fbe 100644 --- a/app/src/main/cpp/vkbridge/server_main.c +++ b/app/src/main/cpp/vkbridge/server_main.c @@ -72,7 +72,7 @@ void vkb_log(int level, const char *fmt, ...) extern __thread const char *vkb_srv_current_cmd; /* A crash is almost always the driver's: say in which call, then die as we would have. */ -static void on_crash(int sig) +__attribute__((unused)) static void on_crash(int sig) { char buf[256]; const char *cmd = vkb_srv_current_cmd ? vkb_srv_current_cmd : "(no Vulkan call)"; @@ -146,6 +146,107 @@ static int listen_on(const char *path) return fd; } +/* Opens (or switches to) the log file; NULL keeps logging to stderr/logcat only. */ +static void open_log(const char *path) +{ + FILE *f = path ? fopen(path, "ae") : NULL; + if (path && !f) fprintf(stderr, "vkbridge-server: cannot open log %s: %s\n", path, strerror(errno)); + pthread_mutex_lock(&log_lock); + FILE *old = log_file; + log_file = f; + pthread_mutex_unlock(&log_lock); + if (old) fclose(old); +} + +/* Loads the Vulkan loader and runs the self-test; 0 when there is nothing to serve. */ +static int server_init(const char *vk_lib, const char *cache_dir) +{ + VKB_INFO("starting (pid %d, protocol %016llx)", getpid(), (unsigned long long)VKB_PROTOCOL_HASH); + void *lib = load_vulkan(vk_lib); + if (!lib) { + VKB_ERR("no Vulkan loader; nothing to serve"); + return 0; + } + vkb_gipa = (PFN_vkGetInstanceProcAddr)dlsym(lib, "vkGetInstanceProcAddr"); + if (!vkb_gipa) { + VKB_ERR("the Vulkan loader exports no vkGetInstanceProcAddr"); + return 0; + } + vkb_dispatch_load_instance(&vkb_global_dt, vkb_gipa, VK_NULL_HANDLE); + vkb_pcache_set_dir(cache_dir); + vkb_selftest_all(); + return 1; +} + +#ifdef VKB_JNI +/* + * In the app's own process (the default on the device): Android chooses the GPU driver, Samsung's + * updatable one included, while the app starts up, and only for the app's process - a separate + * executable gets the plain /vendor driver, which on the Tab S10+ lacked VK_EXT_robustness2 and + * more that the app itself sees. Winlator's Vortek runs its server in-process for the same reason. + * Started once and kept for the app's life; each session only points the log at its own folder. + */ +#include + +static pthread_mutex_t jni_lock = PTHREAD_MUTEX_INITIALIZER; +static char jni_socket[256]; +static int jni_state; /* 0 not started, 1 serving, -1 failed */ + +static void *serve_thread(void *arg) +{ + vkb_srv_serve((int)(intptr_t)arg); + VKB_ERR("in-app server stopped serving"); + return NULL; +} + +static char *jstr(JNIEnv *env, jstring s) +{ + if (!s) return NULL; + const char *c = (*env)->GetStringUTFChars(env, s, NULL); + char *r = c ? strdup(c) : NULL; + if (c) (*env)->ReleaseStringUTFChars(env, s, c); + return r; +} + +JNIEXPORT jint JNICALL Java_com_droiddeck_launcher_gpu_VkBridgeNative_nativeStart(JNIEnv *env, jclass cls, jstring jsocket, + jstring jlog, jstring jcache) +{ + (void)cls; + char *sock = jstr(env, jsocket), *logp = jstr(env, jlog), *cache = jstr(env, jcache); + pthread_mutex_lock(&jni_lock); + open_log(logp); + if (getenv("VKBRIDGE_DEBUG")) { + vkb_verbose = 1; + log_level = VKB_LOG_DEBUG; + } + int ok = 0; + if (jni_state == 1 && sock && !strcmp(sock, jni_socket) && access(sock, F_OK) == 0) { + VKB_INFO("in-app server already serving at %s (pid %d); this session's log continues here", sock, getpid()); + vkb_log_summary(); + ok = 1; + } else if (jni_state == 1) { + VKB_ERR("in-app server serves %s, asked for %s (or its socket is gone)", jni_socket, sock ? sock : "(none)"); + } else if (jni_state == 0 && sock) { + jni_state = -1; + if (server_init(NULL, cache)) { + int lfd = listen_on(sock); + pthread_t t; + if (lfd >= 0 && pthread_create(&t, NULL, serve_thread, (void *)(intptr_t)lfd) == 0) { + pthread_detach(t); + snprintf(jni_socket, sizeof(jni_socket), "%s", sock); + jni_state = 1; + ok = 1; + VKB_INFO("listening on %s (in the app's process)", sock); + } + } + } + pthread_mutex_unlock(&jni_lock); + free(sock); + free(logp); + free(cache); + return ok; +} +#else static void usage(void) { fprintf(stderr, "usage: vkbridge-server --socket PATH [--log FILE] [--vulkan LIB] [--cache-dir DIR] [--selftest] [--verbose]\n"); @@ -199,10 +300,7 @@ int main(int argc, char **argv) } if (getenv("VKBRIDGE_DEBUG")) vkb_verbose = 1; if (vkb_verbose) log_level = VKB_LOG_DEBUG; - if (log_path) { - log_file = fopen(log_path, "ae"); - if (!log_file) fprintf(stderr, "vkbridge-server: cannot open log %s: %s\n", log_path, strerror(errno)); - } + open_log(log_path); signal(SIGPIPE, SIG_IGN); signal(SIGSEGV, on_crash); signal(SIGBUS, on_crash); @@ -215,21 +313,7 @@ int main(int argc, char **argv) * was killed 6 ms into the first device run). The parent process is watched instead. */ start_parent_watch(); - VKB_INFO("starting (pid %d, protocol %016llx)", getpid(), (unsigned long long)VKB_PROTOCOL_HASH); - void *lib = load_vulkan(vk_lib); - if (!lib) { - VKB_ERR("no Vulkan loader; nothing to serve"); - return 1; - } - vkb_gipa = (PFN_vkGetInstanceProcAddr)dlsym(lib, "vkGetInstanceProcAddr"); - if (!vkb_gipa) { - VKB_ERR("the Vulkan loader exports no vkGetInstanceProcAddr"); - return 1; - } - vkb_dispatch_load_instance(&vkb_global_dt, vkb_gipa, VK_NULL_HANDLE); - - vkb_pcache_set_dir(cache_dir); - vkb_selftest_all(); + if (!server_init(vk_lib, cache_dir)) return 1; if (selftest_only) return vkb_npds > 0 ? 0 : 1; if (!sock_path) { @@ -242,3 +326,4 @@ int main(int argc, char **argv) vkb_srv_serve(lfd); return 0; } +#endif diff --git a/app/src/main/cpp/vkbridge/vkb_server.h b/app/src/main/cpp/vkbridge/vkb_server.h index 33c1d2573..af6eccded 100644 --- a/app/src/main/cpp/vkbridge/vkb_server.h +++ b/app/src/main/cpp/vkbridge/vkb_server.h @@ -94,6 +94,8 @@ extern PFN_vkGetInstanceProcAddr vkb_gipa; extern vkb_dispatch vkb_global_dt; extern int vkb_verbose; +void vkb_log_summary(void); + /* server_dsmap.c */ void vkb_dsmap_device_init(vkb_srv_table *t); void vkb_dsmap_device_destroy(vkb_srv_table *t); diff --git a/app/src/main/java/com/droiddeck/launcher/gpu/VkBridgeComponent.kt b/app/src/main/java/com/droiddeck/launcher/gpu/VkBridgeComponent.kt index f13671f6c..b24822dad 100644 --- a/app/src/main/java/com/droiddeck/launcher/gpu/VkBridgeComponent.kt +++ b/app/src/main/java/com/droiddeck/launcher/gpu/VkBridgeComponent.kt @@ -32,10 +32,14 @@ class VkBridgeComponent(private val socket: File, private val logFile: File) : S return } socket.parentFile?.mkdirs() - socket.delete() - exited = false // Pipeline cache kept across sessions (Mali recompiles everything in a new process otherwise). val cacheDir = File(app().cacheDir, "vkbridge-pipelines").apply { mkdirs() } + if (VkBridgeNative.start(socket, logFile, cacheDir)) { + Log.i(TAG, "server ready in the app's process at $socket") + return + } + socket.delete() + exited = false val cmd = listOf(binary.absolutePath, "--socket", socket.absolutePath, "--log", logFile.absolutePath, "--cache-dir", cacheDir.absolutePath) .joinToString(" ") { it.replace("\\", "\\\\").replace(" ", "\\ ") } @@ -54,11 +58,12 @@ class VkBridgeComponent(private val socket: File, private val logFile: File) : S } override fun stop() { + // The in-app server outlives sessions: its socket stays. if (pid > 0) { Process.killProcess(pid) pid = -1 } - socket.delete() + if (!VkBridgeNative.serving) socket.delete() } override fun suspendPid(): Int = pid diff --git a/app/src/main/java/com/droiddeck/launcher/gpu/VkBridgeNative.kt b/app/src/main/java/com/droiddeck/launcher/gpu/VkBridgeNative.kt new file mode 100644 index 000000000..37d47420b --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/gpu/VkBridgeNative.kt @@ -0,0 +1,46 @@ +package com.droiddeck.launcher.gpu + +import android.util.Log +import java.io.File + +/** + * The bridge server inside the app's own process (libvkbridge_jni.so, server_main.c). + * + * Android chooses the GPU driver - Samsung's updatable one included - while the app starts up, and + * only for the app's process: the server run as a separate executable got the plain /vendor driver, + * which on the Tab S10+ lacked VK_EXT_robustness2 and more that the app itself sees. Started by the + * first bridge session and kept for the app's life; later sessions only point its log at their + * own folder. VkBridgeComponent falls back to the executable when this cannot start. + */ +object VkBridgeNative { + private const val TAG = "VkBridge" + + private val loaded: Boolean by lazy { + try { + System.loadLibrary("vkbridge_jni") + true + } catch (e: Throwable) { + Log.w(TAG, "in-app server unavailable: $e") + false + } + } + + /** Whether the in-app server is serving (its socket must then be left alone). */ + @Volatile var serving = false + private set + + /** Starts the server, or points its log at [log] when it already runs; false when it cannot serve. */ + fun start(socket: File, log: File, cacheDir: File): Boolean { + if (!loaded) return false + val ok = try { + nativeStart(socket.absolutePath, log.absolutePath, cacheDir.absolutePath) == 1 + } catch (e: Throwable) { + Log.w(TAG, "in-app server failed to start: $e") + false + } + if (ok) serving = true + return ok + } + + @JvmStatic private external fun nativeStart(socket: String, log: String, cacheDir: String): Int +} diff --git a/tools/vkbridge/build-server-android.sh b/tools/vkbridge/build-server-android.sh index 2bf1b8f4f..0c931dbb5 100755 --- a/tools/vkbridge/build-server-android.sh +++ b/tools/vkbridge/build-server-android.sh @@ -18,4 +18,13 @@ mkdir -p "$out" "$srv"/*.c "$here/common/vkb_wire.c" "$here/common/gen/vkb_gen_structs.c" "$here/common/gen/vkb_gen_server.c" \ -ldl -llog -lm "$(dirname "$cc")/llvm-strip" --strip-unneeded "$out/libvkbridge_server.so" -echo "built $out/libvkbridge_server.so" +# The same server as a JNI library the app loads into its own process (the default): only there +# does Android hand it the driver the app gets (VkBridgeNative, server_main.c). +"$cc" -O2 -g0 -fPIC -shared -DVKB_JNI -Wall -Wno-unused-parameter -Wno-missing-field-initializers \ + -I"$here/third_party/Vulkan-Headers-1.4.341/include" -I"$here/common" -I"$here/common/gen" -I"$srv" \ + -Wl,-z,max-page-size=16384 -Wl,--build-id=none -Wl,--no-undefined -Wl,-soname,libvkbridge_jni.so \ + -o "$out/libvkbridge_jni.so" \ + "$srv"/*.c "$here/common/vkb_wire.c" "$here/common/gen/vkb_gen_structs.c" "$here/common/gen/vkb_gen_server.c" \ + -ldl -llog -lm +"$(dirname "$cc")/llvm-strip" --strip-unneeded "$out/libvkbridge_jni.so" +echo "built $out/libvkbridge_server.so and libvkbridge_jni.so"