Hash :
b512e42d
Author :
Date :
2024-08-20T15:12:19
Reland: Metal: Fix availability for kIOMainPortDefault Remove ad hoc deprecation disables for kIOMasterPortDefault. macCatalyst kIOMainPortDefault available 15.0, less than minimum 15.6. Remove the ifdefs. Reland: Guard macOS specific unittests with compile guards. The patch changes the tested function to be macOS only. Bug: angleproject:360147118 Change-Id: Icbcc811a066995e742825a364fc5f0b5a27a59bc Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5816832 Reviewed-by: Geoff Lang <geofflang@chromium.org> Commit-Queue: Kimmo Kinnunen <kkinnunen@apple.com> Commit-Queue: Geoff Lang <geofflang@chromium.org> Auto-Submit: Kimmo Kinnunen <kkinnunen@apple.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
//
// Copyright 2022 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
#include "common/apple_platform_utils.h"
#include "common/debug.h"
#include <Metal/Metal.h>
namespace angle
{
bool IsMetalRendererAvailable()
{
#if defined(ANGLE_PLATFORM_MACOS) || defined(ANGLE_PLATFORM_MACCATALYST)
static bool queriedMachineModel = false;
static bool machineModelSufficient = true;
if (!queriedMachineModel)
{
queriedMachineModel = true;
std::string fullMachineModel;
if (GetMacosMachineModel(&fullMachineModel))
{
using MachineModelVersion = std::pair<int32_t, int32_t>;
std::string name;
MachineModelVersion version;
ParseMacMachineModel(fullMachineModel, &name, &version.first, &version.second);
std::optional<MachineModelVersion> minVersion;
if (name == "MacBookAir")
{
minVersion = {8, 1}; // MacBook Air (Retina, 13-inch, 2018)
}
else if (name == "MacBookPro")
{
minVersion = {13, 1}; // MacBook Pro (13-inch, 2016)
}
else if (name == "MacBook")
{
minVersion = {9, 1}; // MacBook (Retina, 12-inch, Early 2016)
}
else if (name == "Macmini")
{
minVersion = {8, 1}; // Mac mini (2018)
}
else if (name == "iMac")
{
minVersion = {17, 1}; // iMac (Retina 5K, 27-inch, Late 2015)
}
else if (name == "iMacPro")
{
minVersion = {1, 1}; // iMac Pro
}
if (minVersion.has_value() && version < minVersion.value())
{
WARN() << "Disabling Metal because machine model \"" << fullMachineModel
<< "\" is below the minium supported version.";
machineModelSufficient = false;
}
}
}
if (!machineModelSufficient)
{
ASSERT(queriedMachineModel);
return false;
}
#endif
static bool gpuFamilySufficient = []() -> bool {
ANGLE_APPLE_OBJC_SCOPE
{
auto device = [MTLCreateSystemDefaultDevice() ANGLE_APPLE_AUTORELEASE];
if (!device)
{
return false;
}
#if TARGET_OS_MACCATALYST && __IPHONE_OS_VERSION_MIN_REQUIRED < 160000
// Devices in family 1, such as MacBookPro11,4, cannot use ANGLE's Metal backend.
return [device supportsFamily:MTLGPUFamilyMacCatalyst2];
#elif TARGET_OS_MACCATALYST || TARGET_OS_OSX
// Devices in family 1, such as MacBookPro11,4, cannot use ANGLE's Metal backend.
return [device supportsFamily:MTLGPUFamilyMac2];
#else
// Devices starting with A9 onwards are supported. Simulator is supported as per
// definition that running simulator on Mac Family 1 devices is not supported.
return true;
#endif
}
}();
return gpuFamilySufficient;
}
#if defined(ANGLE_PLATFORM_MACOS) || defined(ANGLE_PLATFORM_MACCATALYST)
bool GetMacosMachineModel(std::string *outMachineModel)
{
# if TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED < 120000
const mach_port_t mainPort = kIOMasterPortDefault;
# else
const mach_port_t mainPort = kIOMainPortDefault;
# endif
io_service_t platformExpert =
IOServiceGetMatchingService(mainPort, IOServiceMatching("IOPlatformExpertDevice"));
if (platformExpert == IO_OBJECT_NULL)
{
return false;
}
CFDataRef modelData = static_cast<CFDataRef>(
IORegistryEntryCreateCFProperty(platformExpert, CFSTR("model"), kCFAllocatorDefault, 0));
if (modelData == nullptr)
{
IOObjectRelease(platformExpert);
return false;
}
*outMachineModel = reinterpret_cast<const char *>(CFDataGetBytePtr(modelData));
IOObjectRelease(platformExpert);
CFRelease(modelData);
return true;
}
bool ParseMacMachineModel(const std::string &identifier,
std::string *type,
int32_t *major,
int32_t *minor)
{
size_t numberLoc = identifier.find_first_of("0123456789");
if (numberLoc == std::string::npos)
{
return false;
}
size_t commaLoc = identifier.find(',', numberLoc);
if (commaLoc == std::string::npos || commaLoc >= identifier.size())
{
return false;
}
const char *numberPtr = &identifier[numberLoc];
const char *commaPtr = &identifier[commaLoc + 1];
char *endPtr = nullptr;
int32_t majorTmp = static_cast<int32_t>(std::strtol(numberPtr, &endPtr, 10));
if (endPtr == numberPtr)
{
return false;
}
int32_t minorTmp = static_cast<int32_t>(std::strtol(commaPtr, &endPtr, 10));
if (endPtr == commaPtr)
{
return false;
}
*major = majorTmp;
*minor = minorTmp;
*type = identifier.substr(0, numberLoc);
return true;
}
#endif
} // namespace angle