From 3ee7c4afbb6792bcae06119af2410a6694708b0c Mon Sep 17 00:00:00 2001 From: wallentx Date: Sun, 11 Oct 2026 05:25:57 -0500 Subject: [PATCH] Discover and propagate the actual PowerVR render node --- .../launcher/gpu/SystemVulkanDriver.java | 58 +++++++++++++++-- .../launcher/runtime/LinuxRuntime.java | 12 ++-- .../launcher/gpu/SystemVulkanSelectionTest.kt | 62 +++++++++++++++++++ tools/linuxfs/preload/drm.c | 4 +- tools/tests/drm_handles_test.c | 23 ++++++- 5 files changed, 148 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/droiddeck/launcher/gpu/SystemVulkanDriver.java b/app/src/main/java/com/droiddeck/launcher/gpu/SystemVulkanDriver.java index 61f8cad5d..fa243c18f 100644 --- a/app/src/main/java/com/droiddeck/launcher/gpu/SystemVulkanDriver.java +++ b/app/src/main/java/com/droiddeck/launcher/gpu/SystemVulkanDriver.java @@ -14,7 +14,9 @@ import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.util.List; +import java.util.TreeSet; import java.util.UUID; +import java.util.function.Predicate; import org.json.JSONObject; /** Isolated glibc-to-Android Vulkan bridge. It does not replace the rootfs's Mesa libraries. */ @@ -40,15 +42,60 @@ public final class SystemVulkanDriver { public static boolean isAvailable() { return isPowerVr() && renderNodeAvailable(); } public static boolean renderNodeAvailable() { - String node = "/dev/dri/renderD128"; + return renderNodePath() != null; + } + + /** Select the actual PowerVR node, including devices where another GPU owns renderD128. */ + public static String renderNodePath() { + String[] nodes = new File("/dev/dri").list(); + if (nodes == null) nodes = new File("/sys/class/drm").list(); + return findRenderNode(nodes, SystemVulkanDriver::renderNodeAvailable); + } + + static String findRenderNode(String[] nodes, Predicate available) { + TreeSet candidates = new TreeSet<>(); + // Android can deny directory listing while allowing direct access. Probe the kernel's + // reserved render range too; directory entries also cover dynamically allocated minors. + for (int minor = 128; minor < 192; minor++) candidates.add("renderD" + minor); + if (nodes != null) for (String node : nodes) + if (node.matches("renderD[0-9]+")) candidates.add(node); + for (String node : candidates) { + String path = "/dev/dri/" + node; + if (available.test(path)) return path; + } + return null; + } + + private static boolean renderNodeAvailable(String node) { try { - return renderNodeAvailable(OsConstants.S_ISCHR(Os.stat(node).st_mode), - Os.access(node, OsConstants.R_OK), Os.access(node, OsConstants.W_OK)); + if (!renderNodeAvailable(OsConstants.S_ISCHR(Os.stat(node).st_mode), + Os.access(node, OsConstants.R_OK), Os.access(node, OsConstants.W_OK))) return false; + return powerVrDevice(new File("/sys/class/drm/" + new File(node).getName() + "/device")); } catch (ErrnoException error) { return false; } } + static boolean powerVrDriver(String driver) { + return "pvrsrvkm".equals(driver) || "pvr".equals(driver) || "powervr".equals(driver); + } + + static boolean powerVrDevice(File device) { + try { + return powerVrDriver(Files.readSymbolicLink(new File(device, "driver").toPath()) + .getFileName().toString()); + } catch (IOException error) { + // Some Android policies expose uevent but deny reading the driver symlink. + try (BufferedReader input = Files.newBufferedReader( + new File(device, "uevent").toPath(), StandardCharsets.US_ASCII)) { + String line; + while ((line = input.readLine()) != null) + if (line.startsWith("DRIVER=")) return powerVrDriver(line.substring(7)); + } catch (IOException ignored) { } + return false; + } + } + static boolean renderNodeAvailable(boolean characterDevice, boolean readable, boolean writable) { return characterDevice && readable && writable; } @@ -65,7 +112,7 @@ public final class SystemVulkanDriver { public static synchronized File prepare(Context context) throws IOException { if (!isPowerVr()) throw new IOException("System PowerVR Vulkan driver is unavailable"); if (!renderNodeAvailable()) throw new IOException( - "System PowerVR requires a readable and writable character device at /dev/dri/renderD128"); + "System PowerVR requires an identifiable, readable and writable DRM render node"); File target = runtimeDirectory(context); File base = target.getParentFile(); if (complete(target)) return target; @@ -128,6 +175,8 @@ public final class SystemVulkanDriver { File directory; try { directory = prepare(context); } catch (IOException error) { throw new IllegalStateException("System Vulkan runtime is unavailable", error); } + String node = renderNodePath(); + if (node == null) throw new IllegalStateException("System PowerVR render node is unavailable"); environment.add("HYBRIS_ANDROID_SDK_VERSION=" + Build.VERSION.SDK_INT); environment.add("HYBRIS_VULKAN_HAL=" + HAL); environment.add("HYBRIS_LINKER_DIR=" + new File(directory, "lib/libhybris/linker").getPath()); @@ -139,6 +188,7 @@ public final class SystemVulkanDriver { environment.add("BL_GAMESCOPE_BACKEND=sdl"); // The Android pvr kernel accepts Vulkan DMA-BUF imports but rejects PRIME GEM handles. environment.add("BL_POWERVR_DRM_HANDLES=1"); + environment.add("BL_POWERVR_RENDER_NODE=" + node); // Inner Gamescope/Xwayland lacks the bridge's Android-buffer WSI protocols. environment.add("BL_STEAM_GL_PRESENT=1"); // Only actual Proton game launches use the bundled DXVK and X11 WSI layer. diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntime.java b/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntime.java index 1cc22a13d..64c90f240 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntime.java +++ b/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntime.java @@ -307,19 +307,21 @@ public final class LinuxRuntime { */ private static void bindPowerVrGpuNode(Context context, List cmd) { if (new File("/dev/dri").list() != null) return; - String node = "/dev/dri/renderD128"; - File dri = new File(context.getCacheDir(), "powervr-drm/dri"); + String node = SystemVulkanDriver.renderNodePath(); + if (node == null) return; + String name = new File(node).getName(); + // Keep only this node listable even if a previous boot assigned a different minor. + File dri = new File(context.getCacheDir(), "powervr-drm/" + name + "/dri"); try { - if (!SystemVulkanDriver.renderNodeAvailable()) return; if (!dri.isDirectory() && !dri.mkdirs()) return; - new File(dri, "renderD128").createNewFile(); + new File(dri, name).createNewFile(); } catch (IOException e) { android.util.Log.w("LinuxRuntime", "Cannot expose the PowerVR render node", e); return; } bind(cmd, dri.getPath() + ":/dev/dri"); bind(cmd, node + ":" + node); - android.util.Log.i("LinuxRuntime", "PowerVR: exposing real renderD128 through a listable directory"); + android.util.Log.i("LinuxRuntime", "PowerVR: exposing real " + name + " through a listable directory"); } /** diff --git a/app/src/test/java/com/droiddeck/launcher/gpu/SystemVulkanSelectionTest.kt b/app/src/test/java/com/droiddeck/launcher/gpu/SystemVulkanSelectionTest.kt index e13d193d8..f1891d9df 100644 --- a/app/src/test/java/com/droiddeck/launcher/gpu/SystemVulkanSelectionTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/gpu/SystemVulkanSelectionTest.kt @@ -1,5 +1,7 @@ package com.droiddeck.launcher.gpu +import java.io.File +import java.nio.file.Files import org.junit.Assert.assertEquals import org.junit.Assert.assertNull import org.junit.Assert.assertFalse @@ -26,6 +28,66 @@ class SystemVulkanSelectionTest { assertFalse("a missing node is unavailable", SystemVulkanDriver.renderNodeAvailable(false, false, false)) } + @Test fun renderNodeDriverMustIdentifyPowerVrExactly() { + for (name in listOf("pvrsrvkm", "pvr", "powervr")) + assertTrue(name, SystemVulkanDriver.powerVrDriver(name)) + for (name in listOf(null, "", "msm", "panfrost", "not-pvr")) + assertFalse(name, SystemVulkanDriver.powerVrDriver(name)) + } + + @Test fun renderNodeIdentityUsesTheDriverLinkOrUevent() { + val device = Files.createTempDirectory("powervr-device").toFile() + try { + assertFalse("missing identity is unavailable", SystemVulkanDriver.powerVrDevice(device)) + val uevent = File(device, "uevent") + uevent.writeText("DRIVER=pvrsrvkm\nOF_NAME=gpu0\n") + assertTrue(SystemVulkanDriver.powerVrDevice(device)) + uevent.writeText("DRIVER=msm\nOF_COMPATIBLE_0=not-pvr\n") + assertFalse(SystemVulkanDriver.powerVrDevice(device)) + val driver = File(device, "driver").toPath() + Files.createSymbolicLink(driver, File("../../drivers/pvrsrvkm").toPath()) + assertTrue(SystemVulkanDriver.powerVrDevice(device)) + Files.delete(driver) + Files.createSymbolicLink(driver, File("../../drivers/msm").toPath()) + uevent.writeText("DRIVER=pvrsrvkm\n") + assertFalse("an unrelated driver link must not fall back", SystemVulkanDriver.powerVrDevice(device)) + } finally { + device.deleteRecursively() + } + } + + @Test fun renderNodeDiscoverySkipsOtherDriversAndDeniedNodes() { + val drivers = mapOf( + "/dev/dri/renderD128" to "msm", + "/dev/dri/renderD129" to "pvr", + "/dev/dri/renderD130" to "pvrsrvkm", + ) + assertEquals("/dev/dri/renderD129", SystemVulkanDriver.findRenderNode(null) { path -> + SystemVulkanDriver.powerVrDriver(drivers[path]) + }) + assertEquals("/dev/dri/renderD130", SystemVulkanDriver.findRenderNode(null) { path -> + SystemVulkanDriver.powerVrDriver(drivers[path]) && + SystemVulkanDriver.renderNodeAvailable(true, path != "/dev/dri/renderD129", true) + }) + } + + @Test fun renderNodesAreDiscoveredWithoutDirectoryListing() { + for (minor in listOf(128, 129, 191)) { + val expected = "/dev/dri/renderD$minor" + assertEquals(expected, SystemVulkanDriver.findRenderNode(null) { it == expected }) + } + assertNull(SystemVulkanDriver.findRenderNode(null) { false }) + } + + @Test fun listedRenderNodesCoverExtendedMinorNumbers() { + assertEquals("/dev/dri/renderD256", SystemVulkanDriver.findRenderNode( + arrayOf("card0", "renderD256", "renderD128/device", "../renderD129"), + ) { path -> + assertTrue(path, path.matches(Regex("/dev/dri/renderD[0-9]+"))) + path == "/dev/dri/renderD256" + }) + } + @Test fun defaultPowerVrResolvesToTheSystemIcd() { assertEquals("system/icd.json", LinuxVulkanDriver.resolveIcdPath("", true, { "system/icd.json" }, { error("default must not resolve an import") })) diff --git a/tools/linuxfs/preload/drm.c b/tools/linuxfs/preload/drm.c index 63d860770..379788372 100644 --- a/tools/linuxfs/preload/drm.c +++ b/tools/linuxfs/preload/drm.c @@ -73,8 +73,10 @@ struct bl_drm_version { static int is_powervr(int fd) { const char *enabled = getenv("BL_POWERVR_DRM_HANDLES"); if (!enabled || strcmp(enabled, "1")) return 0; + const char *path = getenv("BL_POWERVR_RENDER_NODE"); + if (!path || !*path) return 0; struct stat node, candidate; - if (stat("/dev/dri/renderD128", &node) || fstat(fd, &candidate) || + if (stat(path, &node) || fstat(fd, &candidate) || !S_ISCHR(candidate.st_mode) || !S_ISCHR(node.st_mode) || candidate.st_rdev != node.st_rdev) return 0; char name[16] = {0}; diff --git a/tools/tests/drm_handles_test.c b/tools/tests/drm_handles_test.c index 5890e4e65..2e5ce8c8f 100644 --- a/tools/tests/drm_handles_test.c +++ b/tools/tests/drm_handles_test.c @@ -25,6 +25,7 @@ static int identity_denied, kgsl_present; static int kernel_error = EINVAL; static int native_imports, native_exports, native_closes; static const char *driver_name = "pvr"; +static const char *pvr_node = "/dev/dri/renderD129"; static int fixture_stat(const char *path, struct stat *st); static long fixture_syscall(long number, ...); static int fixture_fcntl(int fd, int op, int arg); @@ -61,7 +62,9 @@ static int fixture_stat(const char *path, struct stat *st) { errno = ENOENT; return -1; } - if (!strcmp(path, "/dev/dri/renderD128")) return stat("/dev/null", st); + if (!strcmp(path, pvr_node)) return stat("/dev/null", st); + if (!strcmp(path, "/dev/dri/renderD128")) return stat("/dev/zero", st); + if (!strncmp(path, "/dev/dri/", 9)) { errno = ENOENT; return -1; } return stat(path, st); } @@ -145,10 +148,22 @@ int main(void) { int drm = test_open("/dev/null"), dma = test_open("/dev/zero"); uint32_t handle = 0; unsetenv("BL_POWERVR_DRM_HANDLES"); + setenv("BL_POWERVR_RENDER_NODE", pvr_node, 1); assert(drmPrimeFDToHandle(drm, dma, &handle) != 0); setenv("BL_POWERVR_DRM_HANDLES", "yes", 1); assert(drmPrimeFDToHandle(drm, dma, &handle) != 0); setenv("BL_POWERVR_DRM_HANDLES", "1", 1); + unsetenv("BL_POWERVR_RENDER_NODE"); + assert(drmPrimeFDToHandle(drm, dma, &handle) != 0); /* no selected node */ + setenv("BL_POWERVR_RENDER_NODE", "", 1); + assert(drmPrimeFDToHandle(drm, dma, &handle) != 0); + setenv("BL_POWERVR_RENDER_NODE", "/dev/dri/renderD128", 1); + assert(drmPrimeFDToHandle(drm, dma, &handle) != 0); /* another GPU owns 128 */ + setenv("BL_POWERVR_RENDER_NODE", "/dev/dri/renderD130", 1); + assert(drmPrimeFDToHandle(drm, dma, &handle) != 0); /* selected node is absent */ + setenv("BL_POWERVR_RENDER_NODE", __FILE__, 1); + assert(drmPrimeFDToHandle(drm, dma, &handle) != 0); /* regular files are not DRM nodes */ + setenv("BL_POWERVR_RENDER_NODE", pvr_node, 1); driver_name = "other"; assert(drmPrimeFDToHandle(drm, dma, &handle) != 0); driver_name = "pvr"; @@ -238,6 +253,12 @@ int main(void) { fixture_close(out); assert(drmCloseBufferHandle(drm, handle) == 0); + /* Preserve the original device layout when PowerVR owns renderD128. */ + pvr_node = "/dev/dri/renderD128"; + setenv("BL_POWERVR_RENDER_NODE", pvr_node, 1); + assert(drmPrimeFDToHandle(drm, dma, &handle) == 0 && (handle & PVR_HANDLE_BIT)); + assert(drmCloseBufferHandle(drm, handle) == 0); + /* Retain the old KGSL path with the PowerVR opt-in unset. */ unsetenv("BL_POWERVR_DRM_HANDLES"); kgsl_present = 1;