#include "gpu/config/webgpu_blocklist.h"
#include "base/metrics/field_trial_params.h"
#include "base/strings/pattern.h"
#include "base/strings/string_split.h"
#include "gpu/config/gpu_finch_features.h"
#include "ui/gl/buildflags.h"
#if BUILDFLAG(IS_MAC)
#include "base/mac/mac_util.h"
#endif
#if BUILDFLAG(USE_DAWN)
#include "third_party/dawn/include/dawn/webgpu.h"
#endif
namespace gpu {
namespace {
const base::FeatureParam<std::string> kAdapterBlockList{
&features::kWebGPUService, "AdapterBlockList", ""};
}
bool IsWebGPUAdapterBlocklisted(const WGPUAdapterProperties& properties) {
return IsWebGPUAdapterBlocklisted(properties, kAdapterBlockList.Get());
}
bool IsWebGPUAdapterBlocklisted(const WGPUAdapterProperties& properties,
const std::string& blocklist) {
#if BUILDFLAG(USE_DAWN)
#if BUILDFLAG(IS_MAC)
constexpr uint32_t kAMDVendorID = 0x1002;
if (!base::mac::IsAtLeastOS13() && properties.vendorID == kAMDVendorID &&
properties.backendType == WGPUBackendType_Metal) {
return true;
}
#endif
if (properties.adapterType == WGPUAdapterType_CPU) {
return true;
}
auto U32ToHexString = [](uint32_t value) {
std::ostringstream o;
o << std::hex << value;
return o.str();
};
auto blocked_patterns = base::SplitString(
blocklist, "|", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
for (const auto& blocked_pattern : blocked_patterns) {
std::vector<std::string> segments = base::SplitString(
blocked_pattern, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
if (segments.size() >= 1) {
if (!base::MatchPattern(U32ToHexString(properties.vendorID),
segments[0])) {
continue;
}
}
if (segments.size() >= 2) {
if (!base::MatchPattern(U32ToHexString(properties.deviceID),
segments[1]) &&
!base::MatchPattern(properties.architecture, segments[1])) {
continue;
}
}
if (segments.size() >= 3) {
if (!base::MatchPattern(properties.driverDescription, segments[2])) {
continue;
}
}
return true;
}
return false;
#else
return true;
#endif
}
}