From ebf810ad4b2964a3546acb33b4aaee71ed0b0350 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 28 Aug 2025 14:24:40 +0800 Subject: [PATCH] GPU (Android): adds GPU temp detection support --- CMakeLists.txt | 2 +- src/detection/gpu/gpu.c | 7 +++++ src/detection/gpu/gpu_android.c | 53 +++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 src/detection/gpu/gpu_android.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 97340f30b..aacf30aed 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -599,7 +599,7 @@ elseif(ANDROID) src/detection/diskio/diskio_linux.c src/detection/displayserver/displayserver_android.c src/detection/font/font_nosupport.c - src/detection/gpu/gpu_nosupport.c + src/detection/gpu/gpu_android.c src/detection/host/host_android.c src/detection/icons/icons_nosupport.c src/detection/initsystem/initsystem_linux.c diff --git a/src/detection/gpu/gpu.c b/src/detection/gpu/gpu.c index 5bd667ff2..c66d780e6 100644 --- a/src/detection/gpu/gpu.c +++ b/src/detection/gpu/gpu.c @@ -116,6 +116,13 @@ const char* ffDetectGPU(const FFGPUOptions* options, FFlist* result) { ffListDestroy(result); ffListInitMove(result, &vulkan->gpus); + + #ifdef __ANDROID__ + double ffGPUDetectTempFromTZ(void); + if (options->temp && result->length == 1) + FF_LIST_GET(FFGPUResult, *result, 0)->temperature = ffGPUDetectTempFromTZ(); + #endif + return NULL; } } diff --git a/src/detection/gpu/gpu_android.c b/src/detection/gpu/gpu_android.c new file mode 100644 index 000000000..dc3c44953 --- /dev/null +++ b/src/detection/gpu/gpu_android.c @@ -0,0 +1,53 @@ +#include "gpu.h" +#include "common/io/io.h" +#include "util/stringUtils.h" + +#include + +static double parseTZDir(int dfd, FFstrbuf* buffer) +{ + if(!ffReadFileBufferRelative(dfd, "temp", buffer)) + return FF_GPU_TEMP_UNSET; + + double value = ffStrbufToDouble(buffer, FF_GPU_TEMP_UNSET);// millidegree Celsius + if(value == FF_GPU_TEMP_UNSET) + return FF_GPU_TEMP_UNSET; + + if (!ffReadFileBufferRelative(dfd, "type", buffer) || ffStrbufStartsWithS(buffer, "gpu")) + return FF_GPU_TEMP_UNSET; + + return value / 1000.; +} + +double ffGPUDetectTempFromTZ(void) +{ + FF_AUTO_CLOSE_DIR DIR* dirp = opendir("/sys/class/thermal/"); + if(dirp) + { + FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate(); + int dfd = dirfd(dirp); + struct dirent* entry; + while((entry = readdir(dirp)) != NULL) + { + if(entry->d_name[0] == '.') + continue; + if(!ffStrStartsWith(entry->d_name, "thermal_zone")) + continue; + + FF_AUTO_CLOSE_FD int subfd = openat(dfd, entry->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC); + if(subfd < 0) + continue; + + double result = parseTZDir(subfd, &buffer); + if (result != FF_GPU_TEMP_UNSET) + return result; + } + } + return FF_GPU_TEMP_UNSET; +} + +const char* ffDetectGPUImpl(const FFGPUOptions* options, FFlist* gpus) +{ + FF_UNUSED(options, gpus); + return "No permission. Fallbacks to Vulkan, OpenCL or OpenGL instead"; +}