Files
fastfetch/src/detection/gpu/gpu_apple.c
T

101 lines
3.8 KiB
C
Raw Normal View History

2022-09-05 17:42:42 +02:00
#include "gpu.h"
2022-09-05 13:41:05 -07:00
#include "common/library.h"
2022-09-09 05:40:27 -07:00
#include "detection/cpu/cpu.h"
2022-09-25 15:23:00 +08:00
#include "detection/temps/temps_apple.h"
#include "util/apple/cf_helpers.h"
2022-09-05 13:41:05 -07:00
#include <IOKit/graphics/IOGraphicsLib.h>
2022-09-05 17:42:42 +02:00
2022-09-25 15:23:00 +08:00
static double detectGpuTemp(const FFstrbuf* gpuName)
{
2023-08-07 10:15:08 +08:00
double result = 0;
const char* error;
2022-09-25 15:23:00 +08:00
if(ffStrbufStartsWithS(gpuName, "Apple M1"))
2023-08-07 10:15:08 +08:00
error = ffDetectCoreTemps(FF_TEMP_GPU_M1X, &result);
2022-09-25 15:23:00 +08:00
else if(ffStrbufStartsWithS(gpuName, "Apple M2"))
2023-08-07 10:15:08 +08:00
error = ffDetectCoreTemps(FF_TEMP_GPU_M2X, &result);
else if(ffStrbufStartsWithS(gpuName, "Intel"))
2023-08-07 10:15:08 +08:00
error = ffDetectCoreTemps(FF_TEMP_GPU_INTEL, &result);
2022-09-25 15:23:00 +08:00
else if(ffStrbufStartsWithS(gpuName, "Radeon") || ffStrbufStartsWithS(gpuName, "AMD"))
2023-08-07 10:15:08 +08:00
error = ffDetectCoreTemps(FF_TEMP_GPU_AMD, &result);
2022-09-25 15:23:00 +08:00
else
2023-08-07 10:15:08 +08:00
error = ffDetectCoreTemps(FF_TEMP_GPU_UNKNOWN, &result);
2022-09-25 15:23:00 +08:00
2023-08-07 10:15:08 +08:00
if(error)
2022-09-25 15:23:00 +08:00
return FF_GPU_TEMP_UNSET;
return result;
}
const char* ffDetectGPUImpl(const FFGPUOptions* options, FFlist* gpus)
2022-09-05 17:42:42 +02:00
{
2022-09-05 13:41:05 -07:00
io_iterator_t iterator;
2023-10-07 15:51:05 +08:00
if(IOServiceGetMatchingServices(MACH_PORT_NULL, IOServiceMatching(kIOAcceleratorClassName), &iterator) != kIOReturnSuccess)
2022-09-16 18:11:39 +08:00
return "IOServiceGetMatchingServices() failed";
2022-09-05 13:41:05 -07:00
io_registry_entry_t registryEntry;
2022-09-14 14:55:28 +02:00
while((registryEntry = IOIteratorNext(iterator)) != 0)
2022-09-05 13:41:05 -07:00
{
2022-09-06 02:28:32 -07:00
CFMutableDictionaryRef properties;
2022-09-14 14:55:28 +02:00
if(IORegistryEntryCreateCFProperties(registryEntry, &properties, kCFAllocatorDefault, kNilOptions) != kIOReturnSuccess)
2022-09-06 02:28:32 -07:00
{
2022-09-14 14:55:28 +02:00
IOObjectRelease(registryEntry);
2022-09-06 02:28:32 -07:00
continue;
}
2022-09-05 13:41:05 -07:00
FFGPUResult* gpu = ffListAdd(gpus);
2023-02-11 12:34:57 +08:00
gpu->dedicated.total = gpu->dedicated.used = gpu->shared.total = gpu->shared.used = FF_GPU_VMEM_SIZE_UNSET;
2023-01-12 11:41:07 +01:00
gpu->type = FF_GPU_TYPE_UNKNOWN;
ffStrbufInit(&gpu->driver); // Ok for both Apple and Intel
2022-09-17 02:17:27 +08:00
ffCfDictGetString(properties, CFSTR("CFBundleIdentifier"), &gpu->driver);
2022-09-16 18:11:39 +08:00
int vram; // Supported on Intel
if(!ffCfDictGetInt(properties, CFSTR("VRAM,totalMB"), &vram))
2023-06-25 23:40:20 +08:00
gpu->dedicated.total = (uint64_t) vram * 1024 * 1024;
if(ffCfDictGetInt(properties, CFSTR("gpu-core-count"), &gpu->coreCount)) // For Apple
gpu->coreCount = FF_GPU_CORE_COUNT_UNSET;
2022-09-17 00:35:07 +08:00
ffStrbufInit(&gpu->name);
//IOAccelerator returns model / vendor-id properties for Apple Silicon, but not for Intel Iris GPUs.
2022-09-17 00:35:07 +08:00
//Still needs testing for AMD's
if(ffCfDictGetString(properties, CFSTR("model"), &gpu->name))
2022-09-17 02:59:07 +08:00
{
CFRelease(properties);
io_registry_entry_t parentEntry;
IORegistryEntryGetParentEntry(registryEntry, kIOServicePlane, &parentEntry);
if(IORegistryEntryCreateCFProperties(parentEntry, &properties, kCFAllocatorDefault, kNilOptions) != kIOReturnSuccess)
{
IOObjectRelease(parentEntry);
IOObjectRelease(registryEntry);
continue;
}
ffCfDictGetString(properties, CFSTR("model"), &gpu->name);
}
2022-09-17 00:35:07 +08:00
ffStrbufInit(&gpu->vendor);
int vendorId;
if(!ffCfDictGetInt(properties, CFSTR("vendor-id"), &vendorId))
{
const char* vendorStr = ffGetGPUVendorString((unsigned) vendorId);
ffStrbufAppendS(&gpu->vendor, vendorStr);
if (vendorStr == FF_GPU_VENDOR_NAME_APPLE || vendorStr == FF_GPU_VENDOR_NAME_INTEL)
gpu->type = FF_GPU_TYPE_INTEGRATED;
else if (vendorStr == FF_GPU_VENDOR_NAME_NVIDIA || vendorStr == FF_GPU_VENDOR_NAME_AMD)
gpu->type = FF_GPU_TYPE_DISCRETE;
}
2022-09-17 00:35:07 +08:00
2023-08-07 10:15:08 +08:00
gpu->temperature = options->temp ? detectGpuTemp(&gpu->name) : FF_GPU_TEMP_UNSET;
2022-09-06 02:28:32 -07:00
2022-09-14 14:55:28 +02:00
CFRelease(properties);
IOObjectRelease(registryEntry);
2022-09-05 13:41:05 -07:00
}
2022-09-14 14:55:28 +02:00
IOObjectRelease(iterator);
2022-09-16 18:11:39 +08:00
return NULL;
2022-09-05 17:42:42 +02:00
}