Skip to content

Commit a614acf

Browse files
committed
feat: 尝试加载 Turnip
1 parent e737be8 commit a614acf

8 files changed

Lines changed: 76 additions & 24 deletions

File tree

ZalithLauncher/src/main/java/com/movtery/zalithlauncher/game/plugin/driver/Driver.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class Driver(
2626
appVersion: String,
2727
val name: String,
2828
val summary: String? = null,
29-
val path: String
29+
val path: String,
30+
val isLauncher: Boolean,
3031
): ApkPlugin(
3132
packageName = id,
3233
appName = appName,

ZalithLauncher/src/main/java/com/movtery/zalithlauncher/game/plugin/driver/DriverPluginManager.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ object DriverPluginManager: ApkPluginManager() {
5858
appName = "",
5959
appVersion = "",
6060
name = "Turnip",
61-
path = applicationInfo.nativeLibraryDir
61+
path = applicationInfo.nativeLibraryDir,
62+
isLauncher = true
6263
)
6364
)
6465
setDriverById(AllSettings.vulkanDriver.getValue())
@@ -89,7 +90,8 @@ object DriverPluginManager: ApkPluginManager() {
8990
appVersion = appVersion,
9091
name = driver,
9192
summary = context.getString(R.string.settings_renderer_from_plugins, appName),
92-
path = nativeLibraryDir
93+
path = nativeLibraryDir,
94+
isLauncher = false
9395
)
9496

9597
driverList.add(plugin)

ZalithLauncher/src/main/java/com/movtery/zalithlauncher/ui/activities/MainActivity.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ import com.movtery.zalithlauncher.context.COPY_LABEL_LINK
4444
import com.movtery.zalithlauncher.coroutine.Task
4545
import com.movtery.zalithlauncher.coroutine.TaskSystem
4646
import com.movtery.zalithlauncher.game.control.ControlManager
47+
import com.movtery.zalithlauncher.game.plugin.driver.DriverPluginManager
4748
import com.movtery.zalithlauncher.game.version.installed.VersionsManager
4849
import com.movtery.zalithlauncher.notification.NotificationManager
50+
import com.movtery.zalithlauncher.path.PathManager
4951
import com.movtery.zalithlauncher.path.URL_SUPPORT
5052
import com.movtery.zalithlauncher.setting.AllSettings
5153
import com.movtery.zalithlauncher.ui.base.BaseAppCompatActivity
@@ -64,6 +66,7 @@ import com.movtery.zalithlauncher.ui.theme.feativals.FestivalEffects
6466
import com.movtery.zalithlauncher.upgrade.TooFrequentOperationException
6567
import com.movtery.zalithlauncher.utils.compareLangTag
6668
import com.movtery.zalithlauncher.utils.copyText
69+
import com.movtery.zalithlauncher.utils.device.VulkanChecker
6770
import com.movtery.zalithlauncher.utils.festival.getTodayFestivals
6871
import com.movtery.zalithlauncher.utils.file.shareFile
6972
import com.movtery.zalithlauncher.utils.isChinese
@@ -92,6 +95,7 @@ import dagger.hilt.android.AndroidEntryPoint
9295
import kotlinx.coroutines.Dispatchers
9396
import kotlinx.coroutines.launch
9497
import kotlinx.coroutines.withContext
98+
import java.io.File
9599
import java.util.Locale
96100

97101
@AndroidEntryPoint
@@ -256,6 +260,18 @@ class MainActivity : BaseAppCompatActivity() {
256260
containsChinese = isChinese(this@MainActivity)
257261
)
258262

263+
if (AllSettings.zinkPreferSystemDriver.getValue()) {
264+
VulkanChecker.checkCapabilities(null, null, null)
265+
} else {
266+
val driver = DriverPluginManager.getDriver()
267+
if (driver.isLauncher) {
268+
VulkanChecker.checkCapabilities(null, null, null)
269+
} else {
270+
val tempDir = File(PathManager.DIR_CACHE, "vulkan_temp")
271+
VulkanChecker.checkCapabilities(null, driver.path, tempDir.absolutePath)
272+
}
273+
}
274+
259275
setContent {
260276
ZalithLauncherTheme(
261277
backgroundViewModel = backgroundViewModel,

ZalithLauncher/src/main/java/com/movtery/zalithlauncher/utils/device/VulkanCapabilities.kt

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ package com.movtery.zalithlauncher.utils.device
2020

2121
import android.util.Log
2222
import androidx.annotation.Keep
23+
import org.apache.commons.io.FileUtils
24+
import java.io.File
2325

2426
@Keep
2527
data class VulkanCapabilities(
@@ -96,9 +98,17 @@ object VulkanChecker {
9698
* 查询系统 Vulkan 支持情况
9799
* @return 如果不支持 Vulkan 或初始化失败,返回 null
98100
*/
99-
fun checkCapabilities(): VulkanCapabilities? {
101+
fun checkCapabilities(
102+
driverPath: String?,
103+
nativeDir: String?,
104+
cacheDir: String?
105+
): VulkanCapabilities? {
100106
return try {
101-
nativeCheckVulkan()?.also { caps ->
107+
nativeCheckVulkan(
108+
driverPath = driverPath,
109+
nativeDir = nativeDir,
110+
cacheDir = cacheDir,
111+
)?.also { caps ->
102112
Log.i(TAG, "Vulkan version: ${caps.apiVersionMajor}.${caps.apiVersionMinor}.${caps.apiVersionPatch}")
103113
Log.i(TAG, "Version >= 1.2: ${caps.isVersionSupported}")
104114
if (caps.missingExtensions.isNotEmpty()) {
@@ -115,12 +125,20 @@ object VulkanChecker {
115125
} catch (e: Exception) {
116126
Log.e(TAG, "Native check failed", e)
117127
null
128+
} finally {
129+
if (nativeDir != null && cacheDir != null) {
130+
FileUtils.deleteQuietly(File(cacheDir))
131+
}
118132
}
119133
}
120134

121135
@JvmStatic
122136
private external fun nativeSetLogCallback(callback: VulkanLogCallback)
123137

124138
@JvmStatic
125-
private external fun nativeCheckVulkan(): VulkanCapabilities?
139+
private external fun nativeCheckVulkan(
140+
driverPath: String?,
141+
nativeDir: String?,
142+
cacheDir: String?
143+
): VulkanCapabilities?
126144
}

ZalithLauncher/src/main/jni/Android.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ include $(BUILD_SHARED_LIBRARY)
4646
include $(CLEAR_VARS)
4747
LOCAL_LDLIBS := -ldl -llog
4848
LOCAL_MODULE := vulkan_check
49+
LOCAL_SHARED_LIBRARIES := driver_helper
4950
LOCAL_SRC_FILES := vulkan_checker.c
5051
include $(BUILD_SHARED_LIBRARY)
5152

ZalithLauncher/src/main/jni/driver_helper/driver_helper.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,24 +58,18 @@ bool checkAdrenoGraphics() {
5858
return is_adreno;
5959
}
6060

61-
void* loadTurnipVulkan() {
61+
void* loadTurnipVulkan(const char* driver_path, const char* native_dir, const char* cache_dir) {
6262
if (!checkAdrenoGraphics())
6363
return NULL;
6464

65-
const char* native_dir = getenv("DRIVER_PATH");
66-
const char* cache_dir = getenv("TMPDIR");
67-
68-
if (!native_dir)
69-
return NULL;
70-
71-
if (!linker_ns_load(native_dir))
72-
return NULL;
65+
if (!native_dir || !linker_ns_load(native_dir)) return NULL;
7366

7467
void* linkerhook = linker_ns_dlopen("liblinkerhook.so", RTLD_LOCAL | RTLD_NOW);
75-
if (!linkerhook)
76-
return NULL;
68+
if (!linkerhook) return NULL;
69+
70+
const char* target_driver = (driver_path && strlen(driver_path) > 0) ? driver_path : "libvulkan_freedreno.so";
71+
void* turnip_driver_handle = linker_ns_dlopen(target_driver, RTLD_LOCAL | RTLD_NOW);
7772

78-
void* turnip_driver_handle = linker_ns_dlopen("libvulkan_freedreno.so", RTLD_LOCAL | RTLD_NOW);
7973
if (!turnip_driver_handle) {
8074
dlclose(linkerhook);
8175
return NULL;

ZalithLauncher/src/main/jni/egl_bridge.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
EGLConfig config;
4646
struct PotatoBridge potatoBridge;
4747

48-
void* loadTurnipVulkan();
48+
void* loadTurnipVulkan(const char* driver_path, const char* native_dir, const char* cache_dir);
4949
void calculateFPS();
5050

5151
EXTERNAL_API void pojavTerminate() {
@@ -99,7 +99,10 @@ void load_vulkan() {
9999
int deviceApiLevel = android_get_device_api_level();
100100
if (zinkPreferSystemDriver == NULL && deviceApiLevel >= 28) {
101101
#ifdef ADRENO_POSSIBLE
102-
void* result = loadTurnipVulkan();
102+
const char* native_dir = getenv("DRIVER_PATH");
103+
const char* cache_dir = getenv("TMPDIR");
104+
105+
void* result = loadTurnipVulkan(NULL, native_dir, cache_dir);
103106
if (result != NULL)
104107
{
105108
printf("AdrenoSupp: Loaded Turnip, loader address: %p\n", result);

ZalithLauncher/src/main/jni/vulkan_checker.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,31 @@ Java_com_movtery_zalithlauncher_utils_device_VulkanChecker_nativeSetLogCallback(
6060

6161
#define LOAD_VK_FUNC(name) PFN_##name p##name = (PFN_##name)dlsym(vulkan_handle, #name)
6262

63+
void* loadTurnipVulkan(const char* driver_path, const char* native_dir, const char* cache_dir);
64+
6365
JNIEXPORT jobject JNICALL
64-
Java_com_movtery_zalithlauncher_utils_device_VulkanChecker_nativeCheckVulkan(JNIEnv *env, jclass clazz)
65-
{
66+
Java_com_movtery_zalithlauncher_utils_device_VulkanChecker_nativeCheckVulkan(
67+
JNIEnv *env, jclass clazz, jstring jDriverPath, jstring jNativeDir, jstring jCacheDir
68+
) {
6669
(void)clazz;
6770

68-
void* vulkan_handle = dlopen("libvulkan.so", RTLD_NOW | RTLD_LOCAL);
71+
const char *driverPath = jDriverPath ? (*env)->GetStringUTFChars(env, jDriverPath, NULL) : NULL;
72+
const char *nativeDir = jNativeDir ? (*env)->GetStringUTFChars(env, jNativeDir, NULL) : NULL;
73+
const char *cacheDir = jCacheDir ? (*env)->GetStringUTFChars(env, jCacheDir, NULL) : NULL;
74+
75+
void* vulkan_handle = NULL;
76+
if (nativeDir && cacheDir) {
77+
vulkan_handle = loadTurnipVulkan(driverPath, nativeDir, cacheDir);
78+
} else {
79+
vulkan_handle = dlopen("libvulkan.so", RTLD_NOW | RTLD_LOCAL);
80+
}
81+
82+
if (driverPath) (*env)->ReleaseStringUTFChars(env, jDriverPath, driverPath);
83+
if (nativeDir) (*env)->ReleaseStringUTFChars(env, jNativeDir, nativeDir);
84+
if (cacheDir) (*env)->ReleaseStringUTFChars(env, jCacheDir, cacheDir);
85+
6986
if (!vulkan_handle) {
70-
LOG_E("Failed to dlopen libvulkan.so: %s", dlerror());
87+
LOG_E("Failed to load Vulkan library.");
7188
return NULL;
7289
}
7390

0 commit comments

Comments
 (0)