mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-23 06:46:02 +00:00
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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01S1U9H1evGNjHRQ3wRUdv5V
This commit is contained in:
1 parent
af5098e699
commit
8c050f43b6
6 files changed
+182
-24
No files matched your search
@@ -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)
|
||||
|
||||
@@ -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 <jni.h>
|
||||
|
||||
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
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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"
|
||||
Reference in new issue
Block a user