Remove GLFW dependency for non-GL tests

As part of enabling WebGPU on Chrome OS, we would like to run the Dawn
unit and e2e tests as part of the Chrome OS test suite. This CL removes
the GLFW dependency because Chrome OS does not support GLFW.

The GLFWwindow is only used to create swap chains for the various
backends, but these swap chains are not actually used in the tests
(the e2e tests render to textures instead). The swap chains are only
referenced as part of an unused debugging function:
SwapBuffersForCapture which we can safely remove as per my discussions
with kainino@ and enga@.

We still need GLFW for OpenGL, so we conditionally include it on
platforms that enable the OpenGL backend (which Chrome OS is not).

Note: enga@ suggested to create a VulkanWindowlessBinding that has an
empty GetSwapChainImplementation, but after exploring the option, it
seems like a bit too many ifdefs. In the end, I think it's cleaner to
just remove the *Binding classes entirely.

BUG=chromium:993457
TEST=tests compile and pass for all values of dawn_enable_opengl

Change-Id: I067b12a23f2c236f5506252cd7727b847e79a667
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/10080
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
Commit-Queue: Brian Ho <hob@chromium.org>
diff --git a/BUILD.gn b/BUILD.gn
index 98a3b33..da545e3 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -549,8 +549,6 @@
   configs += [ "${dawn_root}/src/common:dawn_internal" ]
 
   sources = [
-    "src/utils/BackendBinding.cpp",
-    "src/utils/BackendBinding.h",
     "src/utils/ComboRenderPipelineDescriptor.cpp",
     "src/utils/ComboRenderPipelineDescriptor.h",
     "src/utils/DawnHelpers.cpp",
@@ -570,6 +568,24 @@
     ":libdawn_wire",
     "${dawn_root}/src/common",
     "${dawn_shaderc_dir}:libshaderc",
+  ]
+}
+
+static_library("dawn_bindings") {
+  configs += [ "${dawn_root}/src/common:dawn_internal" ]
+
+  sources = [
+    "src/utils/BackendBinding.cpp",
+    "src/utils/BackendBinding.h",
+  ]
+
+  public_deps = [
+    "${dawn_root}/src/dawn:dawn_headers",
+  ]
+
+  deps = [
+    ":libdawn_native",
+    "${dawn_root}/src/common",
     "third_party:glfw",
   ]
   libs = []
@@ -715,7 +731,6 @@
     ":libdawn_wire",
     "${dawn_root}/src/common",
     "${dawn_root}/src/dawn:libdawn",
-    "third_party:glfw",
     "third_party:gmock_and_gtest",
   ]
 
@@ -767,6 +782,10 @@
 
     libs += [ "IOSurface.framework" ]
   }
+
+  if (dawn_enable_opengl) {
+    deps += [ "third_party:glfw" ]
+  }
 }
 
 source_set("dawn_white_box_tests_sources") {
@@ -780,7 +799,6 @@
     ":libdawn_wire",
     "${dawn_root}/src/common",
     "${dawn_root}/src/dawn:libdawn",
-    "third_party:glfw",
     "third_party:gmock_and_gtest",
   ]
 
@@ -796,6 +814,10 @@
     }
   }
 
+  if (dawn_enable_opengl) {
+    deps += [ "third_party:glfw" ]
+  }
+
   libs = []
 }
 
@@ -810,7 +832,6 @@
     ":libdawn_wire",
     "${dawn_root}/src/common",
     "${dawn_root}/src/dawn:libdawn",
-    "third_party:glfw",
     "third_party:gmock_and_gtest",
   ]
 
@@ -828,6 +849,10 @@
   } else {
     sources += [ "src/tests/End2EndTestsMain.cpp" ]
   }
+
+  if (dawn_enable_opengl) {
+    deps += [ "third_party:glfw" ]
+  }
 }
 
 # Temporary groups to make a 5-way patch to fix crbug.com/913171
@@ -859,6 +884,7 @@
 
     # Export all of these as public deps so that `gn check` allows includes
     public_deps = [
+      ":dawn_bindings",
       ":dawn_utils",
       ":libdawn_native",
       ":libdawn_wire",
diff --git a/src/tests/DawnTest.cpp b/src/tests/DawnTest.cpp
index ea2070c..938c943 100644
--- a/src/tests/DawnTest.cpp
+++ b/src/tests/DawnTest.cpp
@@ -21,7 +21,6 @@
 #include "dawn_native/DawnNative.h"
 #include "dawn_wire/WireClient.h"
 #include "dawn_wire/WireServer.h"
-#include "utils/BackendBinding.h"
 #include "utils/DawnHelpers.h"
 #include "utils/SystemUtils.h"
 #include "utils/TerribleCommandBuffer.h"
@@ -31,7 +30,11 @@
 #include <iostream>
 #include <sstream>
 #include <unordered_map>
-#include "GLFW/glfw3.h"
+
+#ifdef DAWN_ENABLE_BACKEND_OPENGL
+#    include "GLFW/glfw3.h"
+#    include "dawn_native/OpenGLBackend.h"
+#endif  // DAWN_ENABLE_BACKEND_OPENGL
 
 namespace {
 
@@ -141,27 +144,25 @@
 }
 
 void DawnTestEnvironment::SetUp() {
-    ASSERT_TRUE(glfwInit());
-
     mInstance = std::make_unique<dawn_native::Instance>();
     mInstance->EnableBackendValidation(mEnableBackendValidation);
     mInstance->EnableBeginCaptureOnStartup(mBeginCaptureOnStartup);
 
-    static constexpr dawn_native::BackendType kAllBackends[] = {
+    static constexpr dawn_native::BackendType kWindowlessBackends[] = {
         dawn_native::BackendType::D3D12,
         dawn_native::BackendType::Metal,
-        dawn_native::BackendType::OpenGL,
         dawn_native::BackendType::Vulkan,
     };
-
-    // Create a test window for each backend and discover an adapter using it.
-    for (dawn_native::BackendType backend : kAllBackends) {
+    for (dawn_native::BackendType backend : kWindowlessBackends) {
         if (detail::IsBackendAvailable(backend)) {
-            CreateBackendWindow(backend);
-            utils::DiscoverAdapter(mInstance.get(), mWindows[backend], backend);
+            mInstance.get()->DiscoverDefaultAdapters();
         }
     }
 
+    if (detail::IsBackendAvailable(dawn_native::BackendType::OpenGL)) {
+        DiscoverOpenGLAdapter();
+    }
+
     std::cout << "Testing configuration\n"
                  "---------------------\n"
                  "UseWire: "
@@ -211,10 +212,6 @@
     return mInstance.get();
 }
 
-GLFWwindow* DawnTestEnvironment::GetWindowForBackend(dawn_native::BackendType type) const {
-    return mWindows.at(type);
-}
-
 bool DawnTestEnvironment::HasVendorIdFilter() const {
     return mHasVendorIdFilter;
 }
@@ -223,14 +220,23 @@
     return mVendorIdFilter;
 }
 
-void DawnTestEnvironment::CreateBackendWindow(dawn_native::BackendType type) {
+void DawnTestEnvironment::DiscoverOpenGLAdapter() {
+#ifdef DAWN_ENABLE_BACKEND_OPENGL
+    ASSERT_TRUE(glfwInit());
     glfwDefaultWindowHints();
-    utils::SetupGLFWWindowHintsForBackend(type);
+    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
+    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
+    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
+    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
 
-    std::string windowName = "Dawn " + ParamName(type) + " test window";
+    std::string windowName = "Dawn OpenGL test window";
     GLFWwindow* window = glfwCreateWindow(400, 400, windowName.c_str(), nullptr, nullptr);
 
-    mWindows[type] = window;
+    glfwMakeContextCurrent(window);
+    dawn_native::opengl::AdapterDiscoveryOptions adapterOptions;
+    adapterOptions.getProc = reinterpret_cast<void* (*)(const char*)>(glfwGetProcAddress);
+    mInstance->DiscoverAdapters(&adapterOptions);
+#endif  // DAWN_ENABLE_BACKEND_OPENGL
 }
 
 // Implementation of DawnTest
@@ -241,7 +247,6 @@
     // We need to destroy child objects before the Device
     mReadbackSlots.clear();
     queue = dawn::Queue();
-    swapchain = dawn::SwapChain();
     device = dawn::Device();
 
     mWireClient = nullptr;
@@ -405,12 +410,6 @@
 
     backendProcs = dawn_native::GetProcs();
 
-    // Get the test window and create the device using it (esp. for OpenGL)
-    GLFWwindow* testWindow = gTestEnv->GetWindowForBackend(backendType);
-    DAWN_ASSERT(testWindow != nullptr);
-    mBinding.reset(utils::CreateBinding(backendType, testWindow, backendDevice));
-    DAWN_ASSERT(mBinding != nullptr);
-
     // Choose whether to use the backend procs and devices directly, or set up the wire.
     DawnDevice cDevice = nullptr;
     DawnProcTable procs;
@@ -448,21 +447,10 @@
     device = dawn::Device::Acquire(cDevice);
     queue = device.CreateQueue();
 
-    // The swapchain isn't used by tests but is useful when debugging with graphics debuggers that
-    // capture at frame boundaries.
-    dawn::SwapChainDescriptor swapChainDesc;
-    swapChainDesc.implementation = mBinding->GetSwapChainImplementation();
-    swapchain = device.CreateSwapChain(&swapChainDesc);
-    FlushWire();
-    swapchain.Configure(
-        static_cast<dawn::TextureFormat>(mBinding->GetPreferredSwapChainTextureFormat()),
-        dawn::TextureUsageBit::OutputAttachment, 400, 400);
-
     device.SetErrorCallback(OnDeviceError, this);
 }
 
 void DawnTest::TearDown() {
-    swapchain = dawn::SwapChain();
     FlushWire();
 
     MapSlotsSynchronously();
@@ -574,12 +562,6 @@
     utils::USleep(100);
 }
 
-void DawnTest::SwapBuffersForCapture() {
-    // Insert a frame boundary for API capture tools.
-    dawn::Texture backBuffer = swapchain.GetNextTexture();
-    swapchain.Present(backBuffer);
-}
-
 void DawnTest::FlushWire() {
     if (gTestEnv->UsesWire()) {
         bool C2SFlushed = mC2sBuf->Flush();
diff --git a/src/tests/DawnTest.h b/src/tests/DawnTest.h
index bbde83e..34a0669 100644
--- a/src/tests/DawnTest.h
+++ b/src/tests/DawnTest.h
@@ -93,10 +93,7 @@
                                std::initializer_list<const char*> forceEnabledWorkarounds,
                                std::initializer_list<const char*> forceDisabledWorkarounds = {});
 
-struct GLFWwindow;
-
 namespace utils {
-    class BackendBinding;
     class TerribleCommandBuffer;
 }  // namespace utils
 
@@ -121,12 +118,11 @@
     bool UsesWire() const;
     bool IsBackendValidationEnabled() const;
     dawn_native::Instance* GetInstance() const;
-    GLFWwindow* GetWindowForBackend(dawn_native::BackendType type) const;
     bool HasVendorIdFilter() const;
     uint32_t GetVendorIdFilter() const;
 
   private:
-    void CreateBackendWindow(dawn_native::BackendType type);
+    void DiscoverOpenGLAdapter();
 
     bool mUseWire = false;
     bool mEnableBackendValidation = false;
@@ -134,11 +130,6 @@
     bool mHasVendorIdFilter = false;
     uint32_t mVendorIdFilter = 0;
     std::unique_ptr<dawn_native::Instance> mInstance;
-
-    // Windows don't usually like to be bound to one API than the other, for example switching
-    // from Vulkan to OpenGL causes crashes on some drivers. Because of this, we lazily created
-    // a window for each backing API.
-    std::unordered_map<dawn_native::BackendType, GLFWwindow*> mWindows;
 };
 
 class DawnTest : public ::testing::TestWithParam<DawnTestParam> {
@@ -177,7 +168,6 @@
   protected:
     dawn::Device device;
     dawn::Queue queue;
-    dawn::SwapChain swapchain;
 
     DawnProcTable backendProcs = {};
     DawnDevice backendDevice = nullptr;
@@ -204,8 +194,6 @@
     void WaitABit();
     void FlushWire();
 
-    void SwapBuffersForCapture();
-
     bool SupportsExtensions(const std::vector<const char*>& extensions);
 
     // Called in SetUp() to get the extensions required to be enabled in the tests. The tests must
@@ -268,8 +256,6 @@
     // Assuming the data is mapped, checks all expectations
     void ResolveExpectations();
 
-    std::unique_ptr<utils::BackendBinding> mBinding;
-
     dawn_native::PCIInfo mPCIInfo;
 
     dawn_native::Adapter mBackendAdapter;