From 8ba23b42d742545b709064d6e2523cdb86de68f5 Mon Sep 17 00:00:00 2001 From: MrPurple666 Date: Tue, 23 Dec 2025 14:30:19 -0300 Subject: [PATCH] initial adrenotools env loader --- include/adrenotools/driver.h | 12 ++++++++++++ src/driver.cpp | 29 ++++++++++++++++++++++++++++- src/hook/hook_impl.cpp | 7 +++++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/include/adrenotools/driver.h b/include/adrenotools/driver.h index f81e8fe..bde7a25 100644 --- a/include/adrenotools/driver.h +++ b/include/adrenotools/driver.h @@ -66,6 +66,18 @@ bool adrenotools_validate_gpu_mapping(void *handle); */ void adrenotools_set_turbo(bool turbo); +/** + * @brief Sets a Freedreno/Turnip environment variable for driver configuration + * @param varName The environment variable name (e.g., "TU_DEBUG", "FD_RD_DUMP", "FD_MESA_DEBUG") + * @param value The value to set + * @note IMPORTANT: Must be called BEFORE adrenotools_open_libvulkan() to take effect + * @note Valid options documented at https://docs.mesa3d.org/drivers/freedreno.html + * @note Example TU_DEBUG values: "sysmem", "gmem", "nobin", "forcebin", "noubwc", "nolrz" + * @note Example FD_RD_DUMP values: "enable", "combine", "full", "trigger" + * @return true on success, false if varName or value is NULL + */ +bool adrenotools_set_freedreno_env(const char *varName, const char *value); + #ifdef __cplusplus } #endif diff --git a/src/driver.cpp b/src/driver.cpp index 4ddee4a..17f0a52 100644 --- a/src/driver.cpp +++ b/src/driver.cpp @@ -15,11 +15,19 @@ #include "hook/hook_impl_params.h" #include #include +#include + +#define TAG "adrenotools" +#define LOGI(fmt, ...) __android_log_print(ANDROID_LOG_INFO, TAG, fmt, ##__VA_ARGS__) +#define LOGW(fmt, ...) __android_log_print(ANDROID_LOG_WARN, TAG, fmt, ##__VA_ARGS__) +#define LOGE(fmt, ...) __android_log_print(ANDROID_LOG_ERROR, TAG, fmt, ##__VA_ARGS__) void *adrenotools_open_libvulkan(int dlopenFlags, int featureFlags, const char *tmpLibDir, const char *hookLibDir, const char *customDriverDir, const char *customDriverName, const char *fileRedirectDir, void **userMappingHandle) { // Bail out if linkernsbypass failed to load, this probably means we're on api < 28 - if (!linkernsbypass_load_status()) + if (!linkernsbypass_load_status()) { + LOGE("adrenotools_open_libvulkan: linkernsbypass_load_status() failed"); return nullptr; + } // Always use memfd on Q+ since it's guaranteed to work if (android_get_device_api_level() >= 29) @@ -208,3 +216,22 @@ void adrenotools_set_turbo(bool turbo) { ioctl(kgslFd, IOCTL_KGSL_SETPROPERTY, &prop); close (kgslFd); } + +bool adrenotools_set_freedreno_env(const char *varName, const char *value) { + if (!varName || !value || std::strlen(varName) == 0) + return false; + + int result = setenv(varName, value, 1); + if (result != 0) { + LOGE("adrenotools_set_freedreno_env: Failed to set '%s' (errno: %d)", varName, errno); + return false; + } + + const char *verifyValue = std::getenv(varName); + if (verifyValue && std::strcmp(verifyValue, value) == 0) { + return true; + } else { + LOGE("adrenotools_set_freedreno_env: Verification failed for '%s'", varName); + return false; + } +} diff --git a/src/hook/hook_impl.cpp b/src/hook/hook_impl.cpp index 50e79a9..1297911 100644 --- a/src/hook/hook_impl.cpp +++ b/src/hook/hook_impl.cpp @@ -14,6 +14,8 @@ #define TAG "hook_impl" #define LOGI(fmt, ...) __android_log_print(ANDROID_LOG_INFO, TAG, fmt, ##__VA_ARGS__) +#define LOGW(fmt, ...) __android_log_print(ANDROID_LOG_WARN, TAG, fmt, ##__VA_ARGS__) +#define LOGE(fmt, ...) __android_log_print(ANDROID_LOG_ERROR, TAG, fmt, ##__VA_ARGS__) const HookImplParams *hook_params; //!< Bunch of info needed to load/patch the driver int (*gsl_memory_alloc_pure_sym)(uint32_t, uint32_t, void *); @@ -27,6 +29,11 @@ using gsl_memory_free_pure_t = decltype(gsl_memory_free_pure_sym); __attribute__((visibility("default"))) void init_hook_param(const void *param) { hook_params = reinterpret_cast(param); + + if (!hook_params) { + LOGE("init_hook_param: Received NULL hook_params!"); + return; + } } __attribute__((visibility("default"))) void init_gsl(void *alloc, void *alloc64, void *free) {