From c18aa23ac23227aa00202a538921e11820d48929 Mon Sep 17 00:00:00 2001 From: The412Banner Date: Fri, 4 Dec 2026 07:54:00 -0500 Subject: [PATCH] feat(upscale): run the NPU on the phone's own QNN runtime Phones whose vendor offers the QNN HTP libraries to apps (public.libraries.txt) now run the NPU engines on them, any HTP generation, with nothing downloaded: the app finds the generation from the vendor's libQnnHtpVStub.so and passes DROIDDECK_UPSCALE_HTP_ARCH; the layer takes the bundled QNN files only for an arch whose skel came with them, else loads the vendor libraries by name. Red Magic 12 Pro (SM8975, HTP V85, no root), layer QNN code in a probe: FSR 4.1.2 Lite INT8 graph compiled on the phone (2.0 s, cached 0.3 s), 1.2 ms per run at perf, 0 failures; arch found without HTP_ARCH. --- .../launcher/session/UpscaleComponent.kt | 43 ++++++++++++++--- .../session/UpscaleComponentNpuTest.kt | 42 +++++++++++++++++ tools/upscale/layer/fsr4_qnn.c | 47 +++++++++++++++++-- 3 files changed, 122 insertions(+), 10 deletions(-) create mode 100644 app/src/test/java/com/droiddeck/launcher/session/UpscaleComponentNpuTest.kt diff --git a/app/src/main/java/com/droiddeck/launcher/session/UpscaleComponent.kt b/app/src/main/java/com/droiddeck/launcher/session/UpscaleComponent.kt index 95619111d1..b4bf6dad1f 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/UpscaleComponent.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/UpscaleComponent.kt @@ -150,8 +150,33 @@ object UpscaleComponent { fun bridgesDir(context: Context) = File(root(context), "bridges") fun runDir(context: Context) = File(LinuxRuntime.rootDir(context), GUEST_RUN.removePrefix("/")) - /** The Hexagon NPU generation the bundled QNN files serve, or null: v79 on the Snapdragon 8 Elite, v81 on the 8 Elite Gen 5. */ - fun npuArch(model: String = SocNames.model()): String? { + /** The NPU this phone has: its Hexagon HTP generation and whether its own QNN libraries run it. */ + data class Npu(val arch: String, val systemLibs: Boolean) + + private val SYSTEM_QNN = listOf("libQnnHtp.so", "libQnnHtpPrepare.so", "libQnnSystem.so") + private val SYSTEM_STUB = Regex("libQnnHtpV(\\d{2,3})Stub\\.so") + + /** + * The HTP generation of the phone's own QNN runtime, when the vendor offers it to apps (listed in + * public.libraries.txt): named by its one libQnnHtpVStub.so. Null when absent or ambiguous. + */ + internal fun systemNpuArch(libDir: File = File("/vendor/lib64"), publicList: File = File("/vendor/etc/public.libraries.txt")): String? { + val listed = runCatching { + publicList.readLines().map { it.trim() }.filter { it.isNotEmpty() && !it.startsWith("#") }.map { it.substringBefore(' ') }.toSet() + }.getOrNull() ?: return null + if (!SYSTEM_QNN.all { it in listed && File(libDir, it).isFile }) return null + val archs = libDir.list().orEmpty().filter { it in listed }.mapNotNull { SYSTEM_STUB.matchEntire(it)?.groupValues?.get(1) }.distinct() + return archs.singleOrNull()?.let { "v$it" } + } + + private val systemNpu: String? by lazy { runCatching { systemNpuArch() }.getOrNull() } + + /** + * This phone's NPU, or null. The phone's own QNN runtime first (any HTP generation, nothing to download); + * else the bundled QNN files: v79 on the Snapdragon 8 Elite, v81 on the 8 Elite Gen 5. + */ + fun npu(model: String = SocNames.model()): Npu? { + systemNpu?.let { return Npu(it, systemLibs = true) } val soc = SocNames.normalizedModel(model) val arch = when { soc.startsWith("SM8750") -> "v79" @@ -159,13 +184,15 @@ object UpscaleComponent { else -> null } ?: return null val fastrpc = listOf("/dev/fastrpc-cdsp", "/dev/fastrpc-cdsp-secure", "/dev/adsprpc-smd").any { File(it).exists() } - return arch.takeIf { fastrpc } + return Npu(arch, systemLibs = false).takeIf { fastrpc } } + fun npuArch(model: String = SocNames.model()): String? = npu(model)?.arch + fun groups(engine: GameUpscaler.Engine, npu: Boolean = npuArch() != null): Set = buildSet { add(Group.CORE) if (engine == GameUpscaler.Engine.XESS) add(Group.XESS) - if (engine.npu && npu) add(Group.NPU) + if (engine.npu && npu && this.npu()?.systemLibs != true) add(Group.NPU) } /** The bridge DLLs (tools/upscale/bridges/build.sh), each in both Wine PE folders. */ @@ -356,11 +383,13 @@ object UpscaleComponent { return dir } - /** Session-wide guest variables: whether NPU engines can run here, and the bionic layer's manifest folder. */ + /** Session-wide guest variables: whether NPU engines can run here (and its HTP generation), and the bionic layer's manifest folder. */ fun guestEnv(context: Context): List { runCatching { runDir(context).mkdirs() } - return listOf( - "DROIDDECK_UPSCALE_NPU=" + (if (npuArch() != null) "1" else "0"), + val here = npu() + return listOfNotNull( + "DROIDDECK_UPSCALE_NPU=" + (if (here != null) "1" else "0"), + here?.let { "DROIDDECK_UPSCALE_HTP_ARCH=${it.arch}" }, "DROIDDECK_UPSCALE_LAYER=" + bionicLayerDir(context).absolutePath, ) } diff --git a/app/src/test/java/com/droiddeck/launcher/session/UpscaleComponentNpuTest.kt b/app/src/test/java/com/droiddeck/launcher/session/UpscaleComponentNpuTest.kt new file mode 100644 index 0000000000..4375edad95 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/session/UpscaleComponentNpuTest.kt @@ -0,0 +1,42 @@ +package com.droiddeck.launcher.session + +import java.io.File +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder + +class UpscaleComponentNpuTest { + @get:Rule val tmp = TemporaryFolder() + + private val runtime = listOf("libQnnHtp.so", "libQnnHtpPrepare.so", "libQnnSystem.so") + + private fun vendor(libs: List, listed: List = libs): Pair { + val dir = tmp.newFolder("lib64") + libs.forEach { File(dir, it).writeText("") } + val list = tmp.newFile("public.libraries.txt") + list.writeText("# vendor libraries apps may load\n" + listed.joinToString("\n") { "$it 64" } + "\n") + return dir to list + } + + @Test fun thePhonesOwnRuntimeNamesItsHtpGeneration() { + val (dir, list) = vendor(runtime + listOf("libQnnHtpV85Stub.so", "libcdsprpc.so")) + assertEquals("v85", UpscaleComponent.systemNpuArch(dir, list)) + } + + @Test fun librariesTheVendorDoesNotOfferToAppsDoNotCount() { + val libs = runtime + "libQnnHtpV85Stub.so" + val (dir, list) = vendor(libs, listed = libs - "libQnnHtpPrepare.so") + assertNull(UpscaleComponent.systemNpuArch(dir, list)) + } + + @Test fun twoStubGenerationsAreAmbiguous() { + val (dir, list) = vendor(runtime + listOf("libQnnHtpV79Stub.so", "libQnnHtpV81Stub.so")) + assertNull(UpscaleComponent.systemNpuArch(dir, list)) + } + + @Test fun noPublicLibraryListMeansNoSystemRuntime() { + assertNull(UpscaleComponent.systemNpuArch(tmp.newFolder("empty"), File(tmp.root, "missing.txt"))) + } +} diff --git a/tools/upscale/layer/fsr4_qnn.c b/tools/upscale/layer/fsr4_qnn.c index a035793401..97787931ca 100644 --- a/tools/upscale/layer/fsr4_qnn.c +++ b/tools/upscale/layer/fsr4_qnn.c @@ -1,4 +1,4 @@ -// Clean-room Qualcomm QNN HTP (v79/v81) runtime management and zero-copy AHardwareBuffer/dmabuf +// Clean-room Qualcomm QNN HTP runtime management (bundled v79/v81 files, or any arch on the device's own QNN runtime) and zero-copy AHardwareBuffer/dmabuf // shared tensor binding for the DroidDeck upscaler layer (ported from Winlator-NeuralScale). // Adapted from BananaNative's verified SM8850 (Adreno 840 / Hexagon HTP v81) runtime. // DD_UPSCALE_NO_ANDROID builds (glibc) compile only the stubs at the end: every NPU init fails @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -195,6 +196,31 @@ static const char *htp_arch_from_soc(void) { return NULL; } +// The phone's own QNN runtime names its arch by its stub (/vendor/lib64/libQnnHtpVStub.so), for SoCs the +// table above does not list. Stubs of two arches: no answer here. +static int htp_arch_from_vendor(char *out, size_t out_sz) { + DIR *d = opendir("/vendor/lib64"); + if (!d) return 0; + char found[8] = ""; + int ambiguous = 0; + struct dirent *e; + while (!ambiguous && (e = readdir(d))) { + if (strncmp(e->d_name, "libQnnHtpV", 10) != 0) continue; + const char *dg = e->d_name + 10; + size_t n = 0; + while (n < 3 && isdigit((unsigned char)dg[n])) n++; + if (n < 2 || strcmp(dg + n, "Stub.so") != 0) continue; + char a[8]; + snprintf(a, sizeof a, "v%.*s", (int)n, dg); + if (!found[0]) snprintf(found, sizeof found, "%s", a); + else if (strcmp(found, a) != 0) ambiguous = 1; + } + closedir(d); + if (!found[0] || ambiguous) return 0; + snprintf(out, out_sz, "%s", found); + return 1; +} + // libQnnHtp.so maps the arch stub (libQnnHtpVNNStub.so) once a device exists. static int htp_arch_from_maps(char *out, size_t out_sz) { FILE *f = fopen("/proc/self/maps", "r"); @@ -228,7 +254,7 @@ static const char *htp_arch_from_skels(const char *fsr4_dir) { return NULL; } -// Order: HTP_ARCH env, SoC properties, mapped stub (once a QNN device exists), the only skel in +// Order: HTP_ARCH env, SoC properties, the vendor's own stub, mapped stub (once a QNN device exists), the only skel in // /adsp, else "htp". A hit from the first three is kept for the process. static const char *htp_arch_for_dir(const char *fsr4_dir) { pthread_mutex_lock(&g_htp_arch_mu); @@ -237,6 +263,7 @@ static const char *htp_arch_for_dir(const char *fsr4_dir) { const char *env = dd_upscale_env("HTP_ARCH"); const char *a = htp_arch_valid(env) ? env : htp_arch_from_soc(); if (a) snprintf(g_htp_arch, sizeof g_htp_arch, "%s", a); + else htp_arch_from_vendor(g_htp_arch, sizeof g_htp_arch); } if (!g_htp_arch[0] && g_htp_rt_up && !g_htp_stubs_preloaded_all) { char mapped[8]; @@ -331,9 +358,21 @@ static int resolve_qnn_lib_path(const char *fsr4_dir, const char *soname, char * return 0; } +// The bundled QNN libraries (the pinned download in ) serve only the arches whose skel came with them +// (/adsp/libQnnHtpVSkel.so). Any other arch runs on the phone's own QNN runtime, loaded by name +// (vendor libraries listed in public.libraries.txt). An arch not known yet keeps the bundled ones. +static int use_bundled_qnn(const char *fsr4_dir) { + char path[640]; + if (!resolve_qnn_lib_path(fsr4_dir, "libQnnHtp.so", path, sizeof path)) return 0; + const char *arch = htp_arch_for_dir(fsr4_dir); + if (!htp_arch_valid(arch)) return 1; + snprintf(path, sizeof path, "%s/adsp/libQnnHtpV%sSkel.so", fsr4_dir, arch + 1); + return access(path, R_OK) == 0; +} + static void *dlopen_qnn_lib_flags(const char *fsr4_dir, const char *soname, int flags) { char full[640]; - if (resolve_qnn_lib_path(fsr4_dir, soname, full, sizeof full)) { + if (use_bundled_qnn(fsr4_dir) && resolve_qnn_lib_path(fsr4_dir, soname, full, sizeof full)) { // Prefer loading QNN libs in 'sphal' so libQnnHtpV81Stub.so resolves vendor libcdsprpc.so void *h = dlopen_in_sphal(full, flags); if (h) return h; @@ -362,6 +401,7 @@ static void *dlopen_cdsprpc(void) { // search list (DIR/jni/arm64-v8a, DIR, NATIVELIB_DIR, LD_LIBRARY_PATH) lets that lookup find it // without LD_LIBRARY_PATH. Unknown arch: every stub that is present gets loaded. static void preload_htp_stubs(qnn_rt *rt, const char *fsr4_dir) { + if (!use_bundled_qnn(fsr4_dir)) return; // the phone's libQnnHtp.so finds its own stub static const char *const k_arch[2] = {"v79", "v81"}; static const char *const k_stubs[2] = {"libQnnHtpV79Stub.so", "libQnnHtpV81Stub.so"}; const char *arch = htp_arch_for_dir(fsr4_dir); @@ -650,6 +690,7 @@ static int rt_acquire_locked(const char *fsr4_dir, const char *power_mode) { snprintf(g_rt.power_mode, sizeof g_rt.power_mode, "%s", (power_mode && *power_mode) ? power_mode : "turbo"); ensure_adsp_path(fsr4_dir); + F4_LOGI("HTP %s: %s QNN libraries", htp_arch_for_dir(fsr4_dir), use_bundled_qnn(fsr4_dir) ? "bundled" : "this device's own"); request_unsigned_pd(&g_rt); preload_htp_stubs(&g_rt, fsr4_dir);