[go: nahoru, domu]

gpu: pulls webgpu blocking checking into standalone target/function

A utility process will use webgpu, and we want to use this code to
determine if we should use the gpu. In order to minimize
dependencies I pulled the check into a separate target.

Bug: b:301662165
Change-Id: I67e70981c08fef7fe246b324b7db7a93fa8ef53a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5034154
Reviewed-by: Brandon Jones <bajones@chromium.org>
Commit-Queue: Scott Violet <sky@chromium.org>
Reviewed-by: Austin Eng <enga@chromium.org>
Reviewed-by: Kenneth Russell <kbr@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1225271}
diff --git a/gpu/BUILD.gn b/gpu/BUILD.gn
index 8876b0c..2b2e27a 100644
--- a/gpu/BUILD.gn
+++ b/gpu/BUILD.gn
@@ -602,7 +602,6 @@
     "config/gpu_test_config_unittest.cc",
     "config/gpu_test_expectations_parser_unittest.cc",
     "config/gpu_util_unittest.cc",
-    "config/webgpu_blocklist_unittest.cc",
     "ipc/client/command_buffer_proxy_impl_unittest.cc",
     "ipc/client/image_decode_accelerator_proxy_unittest.cc",
     "ipc/common/gpu_memory_buffer_impl_shared_memory_unittest.cc",
@@ -703,7 +702,10 @@
   }
 
   if (use_dawn) {
-    sources += [ "command_buffer/service/dawn_caching_interface_unittest.cc" ]
+    sources += [
+      "command_buffer/service/dawn_caching_interface_unittest.cc",
+      "config/webgpu_blocklist_unittest.cc",
+    ]
   }
 
   configs += [ "//build/config:precompiled_headers" ]
@@ -767,6 +769,7 @@
 
   if (use_dawn) {
     deps += [
+      "//gpu/config:webgpu_blocklist_impl",
       "//net:test_support",
       "//third_party/dawn/src/dawn:cpp",
       "//third_party/dawn/src/dawn:proc",
diff --git a/gpu/config/BUILD.gn b/gpu/config/BUILD.gn
index f4aafdfc..4ff377c 100644
--- a/gpu/config/BUILD.gn
+++ b/gpu/config/BUILD.gn
@@ -113,6 +113,17 @@
   }
 }
 
+source_set("webgpu_blocklist_impl") {
+  sources = [
+    "webgpu_blocklist_impl.cc",
+    "webgpu_blocklist_impl.h",
+  ]
+
+  deps = [ "//base" ]
+
+  public_deps = [ "//third_party/dawn/include/dawn:headers" ]
+}
+
 source_set("config_sources") {
   # External code should depend on this via //gpu/config above rather than
   # depending on this directly or the component build will break.
@@ -207,6 +218,10 @@
     ]
   }
 
+  if (use_dawn) {
+    deps += [ ":webgpu_blocklist_impl" ]
+  }
+
   # GpuPreferences is using its own mojo bindings which creates a
   # cycle between this target and gpu_preferences_interface.
   allow_circular_includes_from =
diff --git a/gpu/config/webgpu_blocklist.cc b/gpu/config/webgpu_blocklist.cc
index fcd9fc288..2199be1 100644
--- a/gpu/config/webgpu_blocklist.cc
+++ b/gpu/config/webgpu_blocklist.cc
@@ -5,27 +5,19 @@
 #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 "build/build_config.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(IS_ANDROID)
-#include "base/android/build_info.h"
-#endif
-
 #if BUILDFLAG(USE_DAWN)
-#include "third_party/dawn/include/dawn/webgpu.h"  // nogncheck
+#include "gpu/config/webgpu_blocklist_impl.h"  // nogncheck
 #endif
 
 namespace gpu {
 
 namespace {
 
+#if BUILDFLAG(USE_DAWN)
 // List of patterns, delimited by |. Each pattern is of the form:
 // `vendorId(:deviceIdOrArchitecture(:driverDescription)?)?`
 // Vendor and device ids should be in hexadecimal representation, without a
@@ -36,93 +28,13 @@
 // where `params` is URL-encoded.
 const base::FeatureParam<std::string> kAdapterBlockList{
     &features::kWebGPUService, "AdapterBlockList", ""};
+#endif
 
 }  // namespace
 
 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;
-  // Blocklisted due to https://crbug.com/tint/1094
-  if (base::mac::MacOSMajorVersion() < 13 &&
-      properties.vendorID == kAMDVendorID &&
-      properties.backendType == WGPUBackendType_Metal) {
-    return true;
-  }
-#endif
-
-#if BUILDFLAG(IS_ANDROID)
-  constexpr uint32_t kARMVendorID = 0x13B5;
-  constexpr uint32_t kQualcommVendorID = 0x5143;
-
-  const auto* build_info = base::android::BuildInfo::GetInstance();
-  // Only Android 12 with an ARM or Qualcomm GPU is enabled for initially.
-  // Other OS versions and GPU vendors may be fine, but have not had sufficient
-  // testing yet.
-  if (build_info->sdk_int() < base::android::SDK_VERSION_S ||
-      (properties.vendorID != kARMVendorID &&
-       properties.vendorID != kQualcommVendorID)) {
-    return true;
-  }
-#endif  // BUILDFLAG(IS_ANDROID)
-
-  // TODO(crbug.com/1266550): SwiftShader and CPU adapters are blocked until
-  // fully tested.
-  if (properties.adapterType == WGPUAdapterType_CPU) {
-    return true;
-  }
-
-  // TODO(dawn:1705): d3d11 is not full implemented yet.
-  if (properties.backendType == WGPUBackendType_D3D11) {
-    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);
-
-    // Check if the vendorID matches the first segment.
-    if (segments.size() >= 1) {
-      if (!base::MatchPattern(U32ToHexString(properties.vendorID),
-                              segments[0])) {
-        continue;
-      }
-    }
-
-    // Check if the deviceID or architecture matches the second segment.
-    if (segments.size() >= 2) {
-      if (!base::MatchPattern(U32ToHexString(properties.deviceID),
-                              segments[1]) &&
-          !base::MatchPattern(properties.architecture, segments[1])) {
-        continue;
-      }
-    }
-
-    // Check if the driver description matches the third segment.
-    if (segments.size() >= 3) {
-      if (!base::MatchPattern(properties.driverDescription, segments[2])) {
-        continue;
-      }
-    }
-
-    // Adapter is blocked.
-    return true;
-  }
-  return false;
+  return IsWebGPUAdapterBlocklisted(properties, kAdapterBlockList.Get());
 #else
   return true;
 #endif
diff --git a/gpu/config/webgpu_blocklist.h b/gpu/config/webgpu_blocklist.h
index b7aca55..496f204 100644
--- a/gpu/config/webgpu_blocklist.h
+++ b/gpu/config/webgpu_blocklist.h
@@ -5,8 +5,6 @@
 #ifndef GPU_CONFIG_WEBGPU_BLOCKLIST_H_
 #define GPU_CONFIG_WEBGPU_BLOCKLIST_H_
 
-#include <string>
-
 #include "gpu/gpu_export.h"
 
 struct WGPUAdapterProperties;
@@ -14,8 +12,6 @@
 namespace gpu {
 
 GPU_EXPORT bool IsWebGPUAdapterBlocklisted(const WGPUAdapterProperties&);
-GPU_EXPORT bool IsWebGPUAdapterBlocklisted(const WGPUAdapterProperties&,
-                                           const std::string& blocklist);
 
 }  // namespace gpu
 
diff --git a/gpu/config/webgpu_blocklist_impl.cc b/gpu/config/webgpu_blocklist_impl.cc
new file mode 100644
index 0000000..f062117
--- /dev/null
+++ b/gpu/config/webgpu_blocklist_impl.cc
@@ -0,0 +1,103 @@
+// Copyright 2023 The Chromium Authors
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "gpu/config/webgpu_blocklist_impl.h"
+
+#include <sstream>
+
+#include "base/strings/pattern.h"
+#include "base/strings/string_split.h"
+
+#if BUILDFLAG(IS_MAC)
+#include "base/mac/mac_util.h"
+#endif
+
+#if BUILDFLAG(IS_ANDROID)
+#include "base/android/build_info.h"
+#endif
+
+namespace gpu {
+
+bool IsWebGPUAdapterBlocklisted(const WGPUAdapterProperties& properties,
+                                const std::string& blocklist) {
+#if BUILDFLAG(IS_MAC)
+  constexpr uint32_t kAMDVendorID = 0x1002;
+  // Blocklisted due to https://crbug.com/tint/1094
+  if (base::mac::MacOSMajorVersion() < 13 &&
+      properties.vendorID == kAMDVendorID &&
+      properties.backendType == WGPUBackendType_Metal) {
+    return true;
+  }
+#endif
+
+#if BUILDFLAG(IS_ANDROID)
+  constexpr uint32_t kARMVendorID = 0x13B5;
+  constexpr uint32_t kQualcommVendorID = 0x5143;
+
+  const auto* build_info = base::android::BuildInfo::GetInstance();
+  // Only Android 12 with an ARM or Qualcomm GPU is enabled for initially.
+  // Other OS versions and GPU vendors may be fine, but have not had sufficient
+  // testing yet.
+  if (build_info->sdk_int() < base::android::SDK_VERSION_S ||
+      (properties.vendorID != kARMVendorID &&
+       properties.vendorID != kQualcommVendorID)) {
+    return true;
+  }
+#endif  // BUILDFLAG(IS_ANDROID)
+
+  // TODO(crbug.com/1266550): SwiftShader and CPU adapters are blocked until
+  // fully tested.
+  if (properties.adapterType == WGPUAdapterType_CPU) {
+    return true;
+  }
+
+  // TODO(dawn:1705): d3d11 is not full implemented yet.
+  if (properties.backendType == WGPUBackendType_D3D11) {
+    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);
+
+    // Check if the vendorID matches the first segment.
+    if (segments.size() >= 1) {
+      if (!base::MatchPattern(U32ToHexString(properties.vendorID),
+                              segments[0])) {
+        continue;
+      }
+    }
+
+    // Check if the deviceID or architecture matches the second segment.
+    if (segments.size() >= 2) {
+      if (!base::MatchPattern(U32ToHexString(properties.deviceID),
+                              segments[1]) &&
+          !base::MatchPattern(properties.architecture, segments[1])) {
+        continue;
+      }
+    }
+
+    // Check if the driver description matches the third segment.
+    if (segments.size() >= 3) {
+      if (!base::MatchPattern(properties.driverDescription, segments[2])) {
+        continue;
+      }
+    }
+
+    // Adapter is blocked.
+    return true;
+  }
+  return false;
+}
+
+}  // namespace gpu
diff --git a/gpu/config/webgpu_blocklist_impl.h b/gpu/config/webgpu_blocklist_impl.h
new file mode 100644
index 0000000..7c7d89c
--- /dev/null
+++ b/gpu/config/webgpu_blocklist_impl.h
@@ -0,0 +1,19 @@
+// Copyright 2023 The Chromium Authors
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef GPU_CONFIG_WEBGPU_BLOCKLIST_IMPL_H_
+#define GPU_CONFIG_WEBGPU_BLOCKLIST_IMPL_H_
+
+#include <string>
+
+#include "third_party/dawn/include/dawn/webgpu.h"
+
+namespace gpu {
+
+bool IsWebGPUAdapterBlocklisted(const WGPUAdapterProperties& properties,
+                                const std::string& blocklist_string);
+
+}  // namespace gpu
+
+#endif  // GPU_CONFIG_WEBGPU_BLOCKLIST_IMPL_H_
diff --git a/gpu/config/webgpu_blocklist_unittest.cc b/gpu/config/webgpu_blocklist_unittest.cc
index 91fb15d..a5dd27a 100644
--- a/gpu/config/webgpu_blocklist_unittest.cc
+++ b/gpu/config/webgpu_blocklist_unittest.cc
@@ -3,17 +3,17 @@
 // found in the LICENSE file.
 
 #include "gpu/config/webgpu_blocklist.h"
+
 #include "build/build_config.h"
+#include "gpu/config/webgpu_blocklist_impl.h"
 #include "testing/gtest/include/gtest/gtest.h"
+#include "third_party/dawn/include/dawn/webgpu.h"
 #include "ui/gl/buildflags.h"
 
 #if BUILDFLAG(IS_ANDROID)
 #include "base/android/build_info.h"
 #endif
 
-#if BUILDFLAG(USE_DAWN)
-#include "third_party/dawn/include/dawn/webgpu.h"
-
 namespace gpu {
 
 class WebGPUBlocklistTest : public testing::Test {};
@@ -197,5 +197,3 @@
 #endif  // BUILDFLAG(IS_ANDROID) else
 
 }  // namespace gpu
-
-#endif
\ No newline at end of file