Enable testing on GLES backend.

Add support for the OpenGL ES backend to DawnTest.
Enable its use in BasicTests.

BUG=dawn:580

Change-Id: I920f3a8107928d3ecd5b1506fde5242bec4eef31
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/33861
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Austin Eng <enga@chromium.org>
Commit-Queue: Stephen White <senorblanco@chromium.org>
diff --git a/src/tests/DawnTest.cpp b/src/tests/DawnTest.cpp
index ded6059..5375ce1 100644
--- a/src/tests/DawnTest.cpp
+++ b/src/tests/DawnTest.cpp
@@ -54,6 +54,8 @@
                 return "Null";
             case wgpu::BackendType::OpenGL:
                 return "OpenGL";
+            case wgpu::BackendType::OpenGLES:
+                return "OpenGLES";
             case wgpu::BackendType::Vulkan:
                 return "Vulkan";
             default:
@@ -125,6 +127,12 @@
                              forceDisabledWorkarounds);
 }
 
+BackendTestConfig OpenGLESBackend(std::initializer_list<const char*> forceEnabledWorkarounds,
+                                  std::initializer_list<const char*> forceDisabledWorkarounds) {
+    return BackendTestConfig(wgpu::BackendType::OpenGLES, forceEnabledWorkarounds,
+                             forceDisabledWorkarounds);
+}
+
 BackendTestConfig VulkanBackend(std::initializer_list<const char*> forceEnabledWorkarounds,
                                 std::initializer_list<const char*> forceDisabledWorkarounds) {
     return BackendTestConfig(wgpu::BackendType::Vulkan, forceEnabledWorkarounds,
@@ -322,8 +330,7 @@
     }
 }
 
-std::unique_ptr<dawn_native::Instance> DawnTestEnvironment::CreateInstanceAndDiscoverAdapters()
-    const {
+std::unique_ptr<dawn_native::Instance> DawnTestEnvironment::CreateInstanceAndDiscoverAdapters() {
     auto instance = std::make_unique<dawn_native::Instance>();
     instance->EnableBackendValidation(mEnableBackendValidation);
     instance->EnableGPUBasedBackendValidation(mEnableBackendValidation);
@@ -341,18 +348,38 @@
     glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
 
-    std::string windowName = "Dawn OpenGL test window";
-    GLFWwindow* window = glfwCreateWindow(400, 400, windowName.c_str(), nullptr, nullptr);
+    mOpenGLWindow = glfwCreateWindow(400, 400, "Dawn OpenGL test window", nullptr, nullptr);
 
-    glfwMakeContextCurrent(window);
+    glfwMakeContextCurrent(mOpenGLWindow);
     dawn_native::opengl::AdapterDiscoveryOptions adapterOptions;
     adapterOptions.getProc = reinterpret_cast<void* (*)(const char*)>(glfwGetProcAddress);
     instance->DiscoverAdapters(&adapterOptions);
+
+    glfwDefaultWindowHints();
+    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
+    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
+    glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
+    glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API);
+
+    mOpenGLESWindow = glfwCreateWindow(400, 400, "Dawn OpenGLES test window", nullptr, nullptr);
+
+    glfwMakeContextCurrent(mOpenGLESWindow);
+    dawn_native::opengl::AdapterDiscoveryOptionsES adapterOptionsES;
+    adapterOptionsES.getProc = adapterOptions.getProc;
+    instance->DiscoverAdapters(&adapterOptionsES);
 #endif  // DAWN_ENABLE_BACKEND_OPENGL
 
     return instance;
 }
 
+GLFWwindow* DawnTestEnvironment::GetOpenGLWindow() const {
+    return mOpenGLWindow;
+}
+
+GLFWwindow* DawnTestEnvironment::GetOpenGLESWindow() const {
+    return mOpenGLESWindow;
+}
+
 void DawnTestEnvironment::SelectPreferredAdapterProperties(const dawn_native::Instance* instance) {
     // Get the first available preferred device type.
     dawn_native::DeviceType preferredDeviceType = static_cast<dawn_native::DeviceType>(-1);
@@ -592,6 +619,10 @@
     return mParam.adapterProperties.backendType == wgpu::BackendType::OpenGL;
 }
 
+bool DawnTestBase::IsOpenGLES() const {
+    return mParam.adapterProperties.backendType == wgpu::BackendType::OpenGLES;
+}
+
 bool DawnTestBase::IsVulkan() const {
     return mParam.adapterProperties.backendType == wgpu::BackendType::Vulkan;
 }
@@ -830,6 +861,13 @@
 
     device.SetUncapturedErrorCallback(OnDeviceError, this);
     device.SetDeviceLostCallback(OnDeviceLost, this);
+#if defined(DAWN_ENABLE_BACKEND_OPENGL)
+    if (IsOpenGL()) {
+        glfwMakeContextCurrent(gTestEnv->GetOpenGLWindow());
+    } else if (IsOpenGLES()) {
+        glfwMakeContextCurrent(gTestEnv->GetOpenGLESWindow());
+    }
+#endif
 }
 
 void DawnTestBase::TearDown() {
diff --git a/src/tests/DawnTest.h b/src/tests/DawnTest.h
index 264f6f4..e175132 100644
--- a/src/tests/DawnTest.h
+++ b/src/tests/DawnTest.h
@@ -152,9 +152,14 @@
 BackendTestConfig OpenGLBackend(std::initializer_list<const char*> forceEnabledWorkarounds = {},
                                 std::initializer_list<const char*> forceDisabledWorkarounds = {});
 
+BackendTestConfig OpenGLESBackend(std::initializer_list<const char*> forceEnabledWorkarounds = {},
+                                  std::initializer_list<const char*> forceDisabledWorkarounds = {});
+
 BackendTestConfig VulkanBackend(std::initializer_list<const char*> forceEnabledWorkarounds = {},
                                 std::initializer_list<const char*> forceDisabledWorkarounds = {});
 
+struct GLFWwindow;
+
 namespace utils {
     class PlatformDebugLogger;
     class TerribleCommandBuffer;
@@ -195,6 +200,8 @@
     bool HasVendorIdFilter() const;
     uint32_t GetVendorIdFilter() const;
     const char* GetWireTraceDir() const;
+    GLFWwindow* GetOpenGLWindow() const;
+    GLFWwindow* GetOpenGLESWindow() const;
 
     const std::vector<std::string>& GetEnabledToggles() const;
     const std::vector<std::string>& GetDisabledToggles() const;
@@ -204,7 +211,7 @@
 
   private:
     void ParseArgs(int argc, char** argv);
-    std::unique_ptr<dawn_native::Instance> CreateInstanceAndDiscoverAdapters() const;
+    std::unique_ptr<dawn_native::Instance> CreateInstanceAndDiscoverAdapters();
     void SelectPreferredAdapterProperties(const dawn_native::Instance* instance);
     void PrintTestConfigurationAndAdapterInfo(dawn_native::Instance* instance) const;
 
@@ -221,6 +228,8 @@
     std::vector<TestAdapterProperties> mAdapterProperties;
 
     std::unique_ptr<utils::PlatformDebugLogger> mPlatformDebugLogger;
+    GLFWwindow* mOpenGLWindow;
+    GLFWwindow* mOpenGLESWindow;
 };
 
 class DawnTestBase {
@@ -237,6 +246,7 @@
     bool IsMetal() const;
     bool IsNull() const;
     bool IsOpenGL() const;
+    bool IsOpenGLES() const;
     bool IsVulkan() const;
 
     bool IsAMD() const;
diff --git a/src/tests/end2end/BasicTests.cpp b/src/tests/end2end/BasicTests.cpp
index 4a0409f..0f39dec 100644
--- a/src/tests/end2end/BasicTests.cpp
+++ b/src/tests/end2end/BasicTests.cpp
@@ -51,4 +51,9 @@
     ASSERT_DEVICE_ERROR(queue.WriteBuffer(buffer, 1000, &value, sizeof(value)));
 }
 
-DAWN_INSTANTIATE_TEST(BasicTests, D3D12Backend(), MetalBackend(), OpenGLBackend(), VulkanBackend());
+DAWN_INSTANTIATE_TEST(BasicTests,
+                      D3D12Backend(),
+                      MetalBackend(),
+                      OpenGLBackend(),
+                      OpenGLESBackend(),
+                      VulkanBackend());