diff options
Diffstat (limited to 'src/android')
| -rw-r--r-- | src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/GpuDriverHelper.java | 130 | ||||
| -rw-r--r-- | src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/GpuDriverHelper.kt | 145 |
2 files changed, 145 insertions, 130 deletions
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/GpuDriverHelper.java b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/GpuDriverHelper.java deleted file mode 100644 index 822a3f1f9..000000000 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/GpuDriverHelper.java +++ /dev/null | |||
| @@ -1,130 +0,0 @@ | |||
| 1 | package org.yuzu.yuzu_emu.utils; | ||
| 2 | |||
| 3 | import android.content.Context; | ||
| 4 | import android.net.Uri; | ||
| 5 | |||
| 6 | import org.yuzu.yuzu_emu.NativeLibrary; | ||
| 7 | |||
| 8 | import java.io.File; | ||
| 9 | import java.io.FileInputStream; | ||
| 10 | import java.io.FileOutputStream; | ||
| 11 | import java.io.IOException; | ||
| 12 | import java.util.zip.ZipEntry; | ||
| 13 | import java.util.zip.ZipInputStream; | ||
| 14 | |||
| 15 | public class GpuDriverHelper { | ||
| 16 | private static final String META_JSON_FILENAME = "meta.json"; | ||
| 17 | private static final String DRIVER_INTERNAL_FILENAME = "gpu_driver.zip"; | ||
| 18 | private static String fileRedirectionPath; | ||
| 19 | private static String driverInstallationPath; | ||
| 20 | private static String hookLibPath; | ||
| 21 | |||
| 22 | private static void unzip(String zipFilePath, String destDir) throws IOException { | ||
| 23 | File dir = new File(destDir); | ||
| 24 | |||
| 25 | // Create output directory if it doesn't exist | ||
| 26 | if (!dir.exists()) dir.mkdirs(); | ||
| 27 | |||
| 28 | // Unpack the files. | ||
| 29 | ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFilePath)); | ||
| 30 | byte[] buffer = new byte[1024]; | ||
| 31 | ZipEntry ze = zis.getNextEntry(); | ||
| 32 | while (ze != null) { | ||
| 33 | String fileName = ze.getName(); | ||
| 34 | File newFile = new File(destDir + fileName); | ||
| 35 | newFile.getParentFile().mkdirs(); | ||
| 36 | FileOutputStream fos = new FileOutputStream(newFile); | ||
| 37 | int len; | ||
| 38 | while ((len = zis.read(buffer)) > 0) { | ||
| 39 | fos.write(buffer, 0, len); | ||
| 40 | } | ||
| 41 | fos.close(); | ||
| 42 | zis.closeEntry(); | ||
| 43 | ze = zis.getNextEntry(); | ||
| 44 | } | ||
| 45 | zis.closeEntry(); | ||
| 46 | } | ||
| 47 | |||
| 48 | public static void initializeDriverParameters(Context context) { | ||
| 49 | try { | ||
| 50 | // Initialize the file redirection directory. | ||
| 51 | fileRedirectionPath = context.getExternalFilesDir(null).getCanonicalPath() + "/gpu/vk_file_redirect/"; | ||
| 52 | |||
| 53 | // Initialize the driver installation directory. | ||
| 54 | driverInstallationPath = context.getFilesDir().getCanonicalPath() + "/gpu_driver/"; | ||
| 55 | } catch (IOException e) { | ||
| 56 | throw new RuntimeException(e); | ||
| 57 | } | ||
| 58 | |||
| 59 | // Initialize directories. | ||
| 60 | initializeDirectories(); | ||
| 61 | |||
| 62 | // Initialize hook libraries directory. | ||
| 63 | hookLibPath = context.getApplicationInfo().nativeLibraryDir + "/"; | ||
| 64 | |||
| 65 | // Initialize GPU driver. | ||
| 66 | NativeLibrary.InitializeGpuDriver(hookLibPath, driverInstallationPath, getCustomDriverLibraryName(), fileRedirectionPath); | ||
| 67 | } | ||
| 68 | |||
| 69 | public static void installDefaultDriver(Context context) { | ||
| 70 | // Removing the installed driver will result in the backend using the default system driver. | ||
| 71 | File driverInstallationDir = new File(driverInstallationPath); | ||
| 72 | deleteRecursive(driverInstallationDir); | ||
| 73 | initializeDriverParameters(context); | ||
| 74 | } | ||
| 75 | |||
| 76 | public static void installCustomDriver(Context context, Uri driverPathUri) { | ||
| 77 | // Revert to system default in the event the specified driver is bad. | ||
| 78 | installDefaultDriver(context); | ||
| 79 | |||
| 80 | // Ensure we have directories. | ||
| 81 | initializeDirectories(); | ||
| 82 | |||
| 83 | // Copy the zip file URI into our private storage. | ||
| 84 | FileUtil.copyUriToInternalStorage(context, driverPathUri, driverInstallationPath, DRIVER_INTERNAL_FILENAME); | ||
| 85 | |||
| 86 | // Unzip the driver. | ||
| 87 | try { | ||
| 88 | unzip(driverInstallationPath + DRIVER_INTERNAL_FILENAME, driverInstallationPath); | ||
| 89 | } catch (IOException e) { | ||
| 90 | throw new RuntimeException(e); | ||
| 91 | } | ||
| 92 | |||
| 93 | // Initialize the driver parameters. | ||
| 94 | initializeDriverParameters(context); | ||
| 95 | } | ||
| 96 | |||
| 97 | public static String getCustomDriverName() { | ||
| 98 | // Parse the custom driver metadata to retrieve the name. | ||
| 99 | GpuDriverMetadata metadata = new GpuDriverMetadata(driverInstallationPath + META_JSON_FILENAME); | ||
| 100 | return metadata.name; | ||
| 101 | } | ||
| 102 | |||
| 103 | private static String getCustomDriverLibraryName() { | ||
| 104 | // Parse the custom driver metadata to retrieve the library name. | ||
| 105 | GpuDriverMetadata metadata = new GpuDriverMetadata(driverInstallationPath + META_JSON_FILENAME); | ||
| 106 | return metadata.libraryName; | ||
| 107 | } | ||
| 108 | |||
| 109 | private static void initializeDirectories() { | ||
| 110 | // Ensure the file redirection directory exists. | ||
| 111 | File fileRedirectionDir = new File(fileRedirectionPath); | ||
| 112 | if (!fileRedirectionDir.exists()) { | ||
| 113 | fileRedirectionDir.mkdirs(); | ||
| 114 | } | ||
| 115 | // Ensure the driver installation directory exists. | ||
| 116 | File driverInstallationDir = new File(driverInstallationPath); | ||
| 117 | if (!driverInstallationDir.exists()) { | ||
| 118 | driverInstallationDir.mkdirs(); | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | private static void deleteRecursive(File fileOrDirectory) { | ||
| 123 | if (fileOrDirectory.isDirectory()) { | ||
| 124 | for (File child : fileOrDirectory.listFiles()) { | ||
| 125 | deleteRecursive(child); | ||
| 126 | } | ||
| 127 | } | ||
| 128 | fileOrDirectory.delete(); | ||
| 129 | } | ||
| 130 | } | ||
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/GpuDriverHelper.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/GpuDriverHelper.kt new file mode 100644 index 000000000..399491ffb --- /dev/null +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/GpuDriverHelper.kt | |||
| @@ -0,0 +1,145 @@ | |||
| 1 | package org.yuzu.yuzu_emu.utils | ||
| 2 | |||
| 3 | import android.content.Context | ||
| 4 | import android.net.Uri | ||
| 5 | import org.yuzu.yuzu_emu.NativeLibrary | ||
| 6 | import org.yuzu.yuzu_emu.utils.FileUtil.copyUriToInternalStorage | ||
| 7 | import java.io.File | ||
| 8 | import java.io.FileInputStream | ||
| 9 | import java.io.FileOutputStream | ||
| 10 | import java.io.IOException | ||
| 11 | import java.util.zip.ZipInputStream | ||
| 12 | |||
| 13 | object GpuDriverHelper { | ||
| 14 | private const val META_JSON_FILENAME = "meta.json" | ||
| 15 | private const val DRIVER_INTERNAL_FILENAME = "gpu_driver.zip" | ||
| 16 | private var fileRedirectionPath: String? = null | ||
| 17 | private var driverInstallationPath: String? = null | ||
| 18 | private var hookLibPath: String? = null | ||
| 19 | |||
| 20 | @Throws(IOException::class) | ||
| 21 | private fun unzip(zipFilePath: String, destDir: String) { | ||
| 22 | val dir = File(destDir) | ||
| 23 | |||
| 24 | // Create output directory if it doesn't exist | ||
| 25 | if (!dir.exists()) dir.mkdirs() | ||
| 26 | |||
| 27 | // Unpack the files. | ||
| 28 | val zis = ZipInputStream(FileInputStream(zipFilePath)) | ||
| 29 | val buffer = ByteArray(1024) | ||
| 30 | var ze = zis.nextEntry | ||
| 31 | while (ze != null) { | ||
| 32 | val fileName = ze.name | ||
| 33 | val newFile = File(destDir + fileName) | ||
| 34 | newFile.parentFile!!.mkdirs() | ||
| 35 | val fos = FileOutputStream(newFile) | ||
| 36 | var len: Int | ||
| 37 | while (zis.read(buffer).also { len = it } > 0) { | ||
| 38 | fos.write(buffer, 0, len) | ||
| 39 | } | ||
| 40 | fos.close() | ||
| 41 | zis.closeEntry() | ||
| 42 | ze = zis.nextEntry | ||
| 43 | } | ||
| 44 | zis.closeEntry() | ||
| 45 | } | ||
| 46 | |||
| 47 | @JvmStatic | ||
| 48 | fun initializeDriverParameters(context: Context) { | ||
| 49 | try { | ||
| 50 | // Initialize the file redirection directory. | ||
| 51 | fileRedirectionPath = | ||
| 52 | context.getExternalFilesDir(null)!!.canonicalPath + "/gpu/vk_file_redirect/" | ||
| 53 | |||
| 54 | // Initialize the driver installation directory. | ||
| 55 | driverInstallationPath = context.filesDir.canonicalPath + "/gpu_driver/" | ||
| 56 | } catch (e: IOException) { | ||
| 57 | throw RuntimeException(e) | ||
| 58 | } | ||
| 59 | |||
| 60 | // Initialize directories. | ||
| 61 | initializeDirectories() | ||
| 62 | |||
| 63 | // Initialize hook libraries directory. | ||
| 64 | hookLibPath = context.applicationInfo.nativeLibraryDir + "/" | ||
| 65 | |||
| 66 | // Initialize GPU driver. | ||
| 67 | NativeLibrary.InitializeGpuDriver( | ||
| 68 | hookLibPath, | ||
| 69 | driverInstallationPath, | ||
| 70 | customDriverLibraryName, | ||
| 71 | fileRedirectionPath | ||
| 72 | ) | ||
| 73 | } | ||
| 74 | |||
| 75 | fun installDefaultDriver(context: Context) { | ||
| 76 | // Removing the installed driver will result in the backend using the default system driver. | ||
| 77 | val driverInstallationDir = File(driverInstallationPath!!) | ||
| 78 | deleteRecursive(driverInstallationDir) | ||
| 79 | initializeDriverParameters(context) | ||
| 80 | } | ||
| 81 | |||
| 82 | fun installCustomDriver(context: Context, driverPathUri: Uri?) { | ||
| 83 | // Revert to system default in the event the specified driver is bad. | ||
| 84 | installDefaultDriver(context) | ||
| 85 | |||
| 86 | // Ensure we have directories. | ||
| 87 | initializeDirectories() | ||
| 88 | |||
| 89 | // Copy the zip file URI into our private storage. | ||
| 90 | copyUriToInternalStorage( | ||
| 91 | context, | ||
| 92 | driverPathUri, | ||
| 93 | driverInstallationPath!!, | ||
| 94 | DRIVER_INTERNAL_FILENAME | ||
| 95 | ) | ||
| 96 | |||
| 97 | // Unzip the driver. | ||
| 98 | try { | ||
| 99 | unzip(driverInstallationPath + DRIVER_INTERNAL_FILENAME, driverInstallationPath!!) | ||
| 100 | } catch (e: IOException) { | ||
| 101 | throw RuntimeException(e) | ||
| 102 | } | ||
| 103 | |||
| 104 | // Initialize the driver parameters. | ||
| 105 | initializeDriverParameters(context) | ||
| 106 | } | ||
| 107 | |||
| 108 | // Parse the custom driver metadata to retrieve the name. | ||
| 109 | val customDriverName: String? | ||
| 110 | get() { | ||
| 111 | // Parse the custom driver metadata to retrieve the name. | ||
| 112 | val metadata = GpuDriverMetadata(driverInstallationPath + META_JSON_FILENAME) | ||
| 113 | return metadata.name | ||
| 114 | } | ||
| 115 | |||
| 116 | // Parse the custom driver metadata to retrieve the library name. | ||
| 117 | private val customDriverLibraryName: String? | ||
| 118 | get() { | ||
| 119 | // Parse the custom driver metadata to retrieve the library name. | ||
| 120 | val metadata = GpuDriverMetadata(driverInstallationPath + META_JSON_FILENAME) | ||
| 121 | return metadata.libraryName | ||
| 122 | } | ||
| 123 | |||
| 124 | private fun initializeDirectories() { | ||
| 125 | // Ensure the file redirection directory exists. | ||
| 126 | val fileRedirectionDir = File(fileRedirectionPath!!) | ||
| 127 | if (!fileRedirectionDir.exists()) { | ||
| 128 | fileRedirectionDir.mkdirs() | ||
| 129 | } | ||
| 130 | // Ensure the driver installation directory exists. | ||
| 131 | val driverInstallationDir = File(driverInstallationPath!!) | ||
| 132 | if (!driverInstallationDir.exists()) { | ||
| 133 | driverInstallationDir.mkdirs() | ||
| 134 | } | ||
| 135 | } | ||
| 136 | |||
| 137 | private fun deleteRecursive(fileOrDirectory: File) { | ||
| 138 | if (fileOrDirectory.isDirectory) { | ||
| 139 | for (child in fileOrDirectory.listFiles()!!) { | ||
| 140 | deleteRecursive(child) | ||
| 141 | } | ||
| 142 | } | ||
| 143 | fileOrDirectory.delete() | ||
| 144 | } | ||
| 145 | } | ||