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

115 lines
4.0 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>
2023-01-13 10:43:33 +08:00
#include <wchar.h>
2022-09-05 17:42:42 +02:00
2022-09-25 15:23:00 +08:00
static double detectGpuTemp(const FFstrbuf* gpuName)
{
2022-11-13 00:04:34 +08:00
FF_LIST_AUTO_DESTROY temps;
2022-09-25 15:23:00 +08:00
ffListInit(&temps, sizeof(FFTempValue));
if(ffStrbufStartsWithS(gpuName, "Apple M1"))
ffDetectCoreTemps(FF_TEMP_GPU_M1X, &temps);
else if(ffStrbufStartsWithS(gpuName, "Apple M2"))
ffDetectCoreTemps(FF_TEMP_GPU_M2X, &temps);
else if(ffStrbufStartsWithS(gpuName, "Intel"))
ffDetectCoreTemps(FF_TEMP_GPU_INTEL, &temps);
2022-09-25 15:23:00 +08:00
else if(ffStrbufStartsWithS(gpuName, "Radeon") || ffStrbufStartsWithS(gpuName, "AMD"))
ffDetectCoreTemps(FF_TEMP_GPU_AMD, &temps);
else
ffDetectCoreTemps(FF_TEMP_GPU_UNKNOWN, &temps);
if(temps.length == 0)
return FF_GPU_TEMP_UNSET;
double result = 0;
for(uint32_t i = 0; i < temps.length; ++i)
{
FFTempValue* tempValue = (FFTempValue*)ffListGet(&temps, i);
result += tempValue->value;
//TODO: do we really need this?
ffStrbufDestroy(&tempValue->name);
ffStrbufDestroy(&tempValue->deviceClass);
}
result /= temps.length;
return result;
}
2022-09-16 18:11:39 +08:00
const char* ffDetectGPUImpl(FFlist* gpus, const FFinstance* instance)
2022-09-05 17:42:42 +02:00
{
2022-09-05 13:41:05 -07:00
FF_UNUSED(instance);
2022-09-16 18:11:39 +08:00
CFMutableDictionaryRef matchDict = IOServiceMatching(kIOAcceleratorClassName);
2022-09-05 13:41:05 -07:00
io_iterator_t iterator;
if(IOServiceGetMatchingServices(MACH_PORT_NULL, matchDict, &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);
gpu->id = 0;
gpu->dedicatedTotal = gpu->dedicatedUsed = gpu->sharedTotal = gpu->sharedUsed = FF_GPU_VMEM_SIZE_UNSET;
2023-01-12 11:41:07 +01:00
gpu->type = FF_GPU_TYPE_UNKNOWN;
2023-01-13 10:43:33 +08:00
ffStrbufInit(&gpu->vendor);
int vendorId;
if(!ffCfDictGetInt(properties, CFSTR("vendor-id"), &vendorId))
{
2023-02-06 11:07:49 +08:00
const char* vendorStr = ffGetGPUVendorString((unsigned) vendorId);
ffStrbufAppendS(&gpu->vendor, vendorStr);
if (vendorStr == FF_GPU_VENDOR_NAME_APPLE || vendorStr == FF_GPU_VENDOR_NAME_INTEL)
2023-01-13 15:44:52 +08:00
gpu->type = FF_GPU_TYPE_INTEGRATED;
2023-02-06 11:07:49 +08:00
else if (vendorStr == FF_GPU_VENDOR_NAME_NVIDIA || vendorStr == FF_GPU_VENDOR_NAME_AMD)
2023-01-13 15:44:52 +08:00
gpu->type = FF_GPU_TYPE_DISCRETE;
2023-01-13 10:43:33 +08:00
}
2022-09-16 18:11:39 +08:00
ffStrbufInit(&gpu->driver);
2022-09-17 02:17:27 +08:00
ffCfDictGetString(properties, CFSTR("CFBundleIdentifier"), &gpu->driver);
2022-09-16 18:11:39 +08:00
2022-09-17 00:35:07 +08:00
ffStrbufInit(&gpu->name);
//IOAccelerator returns model property for Apple Silicon, but not for Intel Iris GPUs.
//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
if(ffCfDictGetInt(properties, CFSTR("gpu-core-count"), &gpu->coreCount))
2022-09-17 00:35:07 +08:00
gpu->coreCount = FF_GPU_CORE_COUNT_UNSET;
if(instance->config.gpuTemp)
gpu->temperature = detectGpuTemp(&gpu->name);
else
gpu->temperature = 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
}