Remove OpenGLBinding's dependency on glad

This move all the OpenGL-specific code for swapchain handling in a new
NativeSwapChainImpl in the OpenGL backend so no code outside of
dawn_native needs OpenGL.

BUG=dawn:165

Change-Id: I3c0c1055e3215a59fdc8e9550baf30762a7014b5
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/8161
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
Reviewed-by: Austin Eng <enga@chromium.org>
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
diff --git a/BUILD.gn b/BUILD.gn
index df93f90..fa5d6a4 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -292,6 +292,8 @@
       "src/dawn_native/opengl/DeviceGL.cpp",
       "src/dawn_native/opengl/DeviceGL.h",
       "src/dawn_native/opengl/Forward.h",
+      "src/dawn_native/opengl/NativeSwapChainImplGL.cpp",
+      "src/dawn_native/opengl/NativeSwapChainImplGL.h",
       "src/dawn_native/opengl/OpenGLFunctions.cpp",
       "src/dawn_native/opengl/OpenGLFunctions.h",
       "src/dawn_native/opengl/PersistentPipelineStateGL.cpp",
@@ -533,7 +535,6 @@
 
   if (dawn_enable_opengl) {
     sources += [ "src/utils/OpenGLBinding.cpp" ]
-    deps += [ "third_party:glad" ]
   }
 
   if (dawn_enable_vulkan) {
diff --git a/src/dawn_native/opengl/NativeSwapChainImplGL.cpp b/src/dawn_native/opengl/NativeSwapChainImplGL.cpp
new file mode 100644
index 0000000..5bd9113
--- /dev/null
+++ b/src/dawn_native/opengl/NativeSwapChainImplGL.cpp
@@ -0,0 +1,87 @@
+// Copyright 2019 The Dawn Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "dawn_native/opengl/NativeSwapChainImplGL.h"
+
+#include "dawn_native/opengl/DeviceGL.h"
+
+namespace dawn_native { namespace opengl {
+
+    NativeSwapChainImpl::NativeSwapChainImpl(Device* device,
+                                             PresentCallback present,
+                                             void* presentUserdata)
+        : mPresentCallback(present), mPresentUserdata(presentUserdata), mDevice(device) {
+    }
+
+    NativeSwapChainImpl::~NativeSwapChainImpl() {
+        const OpenGLFunctions& gl = mDevice->gl;
+        gl.DeleteTextures(1, &mBackTexture);
+        gl.DeleteFramebuffers(1, &mBackFBO);
+    }
+
+    void NativeSwapChainImpl::Init(DawnWSIContextGL* /*context*/) {
+        const OpenGLFunctions& gl = mDevice->gl;
+        gl.GenTextures(1, &mBackTexture);
+        gl.BindTexture(GL_TEXTURE_2D, mBackTexture);
+        gl.TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
+
+        gl.GenFramebuffers(1, &mBackFBO);
+        gl.BindFramebuffer(GL_READ_FRAMEBUFFER, mBackFBO);
+        gl.FramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
+                                mBackTexture, 0);
+    }
+
+    DawnSwapChainError NativeSwapChainImpl::Configure(DawnTextureFormat format,
+                                                      DawnTextureUsageBit usage,
+                                                      uint32_t width,
+                                                      uint32_t height) {
+        if (format != DAWN_TEXTURE_FORMAT_R8_G8_B8_A8_UNORM) {
+            return "unsupported format";
+        }
+        ASSERT(width > 0);
+        ASSERT(height > 0);
+        mWidth = width;
+        mHeight = height;
+
+        const OpenGLFunctions& gl = mDevice->gl;
+        gl.BindTexture(GL_TEXTURE_2D, mBackTexture);
+        // Reallocate the texture
+        gl.TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE,
+                      nullptr);
+
+        return DAWN_SWAP_CHAIN_NO_ERROR;
+    }
+
+    DawnSwapChainError NativeSwapChainImpl::GetNextTexture(DawnSwapChainNextTexture* nextTexture) {
+        nextTexture->texture.u32 = mBackTexture;
+        return DAWN_SWAP_CHAIN_NO_ERROR;
+    }
+
+    DawnSwapChainError NativeSwapChainImpl::Present() {
+        const OpenGLFunctions& gl = mDevice->gl;
+        gl.BindFramebuffer(GL_READ_FRAMEBUFFER, mBackFBO);
+        gl.BindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
+        gl.BlitFramebuffer(0, 0, mWidth, mHeight, 0, mHeight, mWidth, 0, GL_COLOR_BUFFER_BIT,
+                           GL_NEAREST);
+
+        mPresentCallback(mPresentUserdata);
+
+        return DAWN_SWAP_CHAIN_NO_ERROR;
+    }
+
+    dawn::TextureFormat NativeSwapChainImpl::GetPreferredFormat() const {
+        return dawn::TextureFormat::R8G8B8A8Unorm;
+    }
+
+}}  // namespace dawn_native::opengl
diff --git a/src/dawn_native/opengl/NativeSwapChainImplGL.h b/src/dawn_native/opengl/NativeSwapChainImplGL.h
new file mode 100644
index 0000000..5427ac1
--- /dev/null
+++ b/src/dawn_native/opengl/NativeSwapChainImplGL.h
@@ -0,0 +1,58 @@
+// Copyright 2017 The Dawn Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef DAWNNATIVE_OPENGL_NATIVESWAPCHAINIMPLGL_H_
+#define DAWNNATIVE_OPENGL_NATIVESWAPCHAINIMPLGL_H_
+
+#include "dawn_native/OpenGLBackend.h"
+
+#include "dawn_native/dawn_platform.h"
+#include "glad/glad.h"
+
+namespace dawn_native { namespace opengl {
+
+    class Device;
+
+    class NativeSwapChainImpl {
+      public:
+        using WSIContext = DawnWSIContextGL;
+
+        NativeSwapChainImpl(Device* device, PresentCallback present, void* presentUserdata);
+        ~NativeSwapChainImpl();
+
+        void Init(DawnWSIContextGL* context);
+        DawnSwapChainError Configure(DawnTextureFormat format,
+                                     DawnTextureUsageBit,
+                                     uint32_t width,
+                                     uint32_t height);
+        DawnSwapChainError GetNextTexture(DawnSwapChainNextTexture* nextTexture);
+        DawnSwapChainError Present();
+
+        dawn::TextureFormat GetPreferredFormat() const;
+
+      private:
+        PresentCallback mPresentCallback;
+        void* mPresentUserdata;
+
+        uint32_t mWidth = 0;
+        uint32_t mHeight = 0;
+        GLuint mBackFBO = 0;
+        GLuint mBackTexture = 0;
+
+        Device* mDevice = nullptr;
+    };
+
+}}  // namespace dawn_native::opengl
+
+#endif  // DAWNNATIVE_OPENGL_NATIVESWAPCHAINIMPLGL_H_
diff --git a/src/dawn_native/opengl/OpenGLBackend.cpp b/src/dawn_native/opengl/OpenGLBackend.cpp
index f915189..22dc697 100644
--- a/src/dawn_native/opengl/OpenGLBackend.cpp
+++ b/src/dawn_native/opengl/OpenGLBackend.cpp
@@ -17,7 +17,9 @@
 
 #include "dawn_native/OpenGLBackend.h"
 
+#include "common/SwapChainUtils.h"
 #include "dawn_native/opengl/DeviceGL.h"
+#include "dawn_native/opengl/NativeSwapChainImplGL.h"
 
 namespace dawn_native { namespace opengl {
 
@@ -25,4 +27,23 @@
         : AdapterDiscoveryOptionsBase(BackendType::OpenGL) {
     }
 
+    DawnSwapChainImplementation CreateNativeSwapChainImpl(DawnDevice device,
+                                                          PresentCallback present,
+                                                          void* presentUserdata) {
+        Device* backendDevice = reinterpret_cast<Device*>(device);
+
+        DawnSwapChainImplementation impl;
+        impl = CreateSwapChainImplementation(
+            new NativeSwapChainImpl(backendDevice, present, presentUserdata));
+        impl.textureUsage = DAWN_TEXTURE_USAGE_BIT_PRESENT;
+
+        return impl;
+    }
+
+    DawnTextureFormat GetNativeSwapChainPreferredFormat(
+        const DawnSwapChainImplementation* swapChain) {
+        NativeSwapChainImpl* impl = reinterpret_cast<NativeSwapChainImpl*>(swapChain->userData);
+        return static_cast<DawnTextureFormat>(impl->GetPreferredFormat());
+    }
+
 }}  // namespace dawn_native::opengl
diff --git a/src/include/dawn_native/OpenGLBackend.h b/src/include/dawn_native/OpenGLBackend.h
index 10b0000..0ebbbc7 100644
--- a/src/include/dawn_native/OpenGLBackend.h
+++ b/src/include/dawn_native/OpenGLBackend.h
@@ -15,6 +15,7 @@
 #ifndef DAWNNATIVE_OPENGLBACKEND_H_
 #define DAWNNATIVE_OPENGLBACKEND_H_
 
+#include <dawn/dawn_wsi.h>
 #include <dawn_native/DawnNative.h>
 
 namespace dawn_native { namespace opengl {
@@ -25,6 +26,12 @@
         void* (*getProc)(const char*);
     };
 
+    using PresentCallback = void (*)(void*);
+    DAWN_NATIVE_EXPORT DawnSwapChainImplementation
+    CreateNativeSwapChainImpl(DawnDevice device, PresentCallback present, void* presentUserdata);
+    DAWN_NATIVE_EXPORT DawnTextureFormat
+    GetNativeSwapChainPreferredFormat(const DawnSwapChainImplementation* swapChain);
+
 }}  // namespace dawn_native::opengl
 
 #endif  // DAWNNATIVE_OPENGLBACKEND_H_
diff --git a/src/utils/OpenGLBinding.cpp b/src/utils/OpenGLBinding.cpp
index d5737f6..4a20e5d 100644
--- a/src/utils/OpenGLBinding.cpp
+++ b/src/utils/OpenGLBinding.cpp
@@ -20,95 +20,28 @@
 #include "dawn/dawn_wsi.h"
 #include "dawn_native/OpenGLBackend.h"
 
-// Glad needs to be included before GLFW otherwise it complain that GL.h was already included
-#include "glad/glad.h"
-
 #include <cstdio>
 #include "GLFW/glfw3.h"
 
 namespace utils {
-    class SwapChainImplGL {
-      public:
-        using WSIContext = DawnWSIContextGL;
-
-        SwapChainImplGL(GLFWwindow* window) : mWindow(window) {
-        }
-
-        ~SwapChainImplGL() {
-            glDeleteTextures(1, &mBackTexture);
-            glDeleteFramebuffers(1, &mBackFBO);
-        }
-
-        void Init(DawnWSIContextGL*) {
-            glGenTextures(1, &mBackTexture);
-            glBindTexture(GL_TEXTURE_2D, mBackTexture);
-            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
-
-            glGenFramebuffers(1, &mBackFBO);
-            glBindFramebuffer(GL_READ_FRAMEBUFFER, mBackFBO);
-            glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
-                                   mBackTexture, 0);
-        }
-
-        DawnSwapChainError Configure(DawnTextureFormat format,
-                                     DawnTextureUsageBit,
-                                     uint32_t width,
-                                     uint32_t height) {
-            if (format != DAWN_TEXTURE_FORMAT_R8_G8_B8_A8_UNORM) {
-                return "unsupported format";
-            }
-            ASSERT(width > 0);
-            ASSERT(height > 0);
-            mWidth = width;
-            mHeight = height;
-
-            glBindTexture(GL_TEXTURE_2D, mBackTexture);
-            // Reallocate the texture
-            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE,
-                         nullptr);
-
-            return DAWN_SWAP_CHAIN_NO_ERROR;
-        }
-
-        DawnSwapChainError GetNextTexture(DawnSwapChainNextTexture* nextTexture) {
-            nextTexture->texture.u32 = mBackTexture;
-            return DAWN_SWAP_CHAIN_NO_ERROR;
-        }
-
-        DawnSwapChainError Present() {
-            glBindFramebuffer(GL_READ_FRAMEBUFFER, mBackFBO);
-            glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
-            glBlitFramebuffer(0, 0, mWidth, mHeight, 0, mHeight, mWidth, 0, GL_COLOR_BUFFER_BIT,
-                              GL_NEAREST);
-            glfwSwapBuffers(mWindow);
-
-            return DAWN_SWAP_CHAIN_NO_ERROR;
-        }
-
-      private:
-        GLFWwindow* mWindow = nullptr;
-        uint32_t mWidth = 0;
-        uint32_t mHeight = 0;
-        GLuint mBackFBO = 0;
-        GLuint mBackTexture = 0;
-    };
 
     class OpenGLBinding : public BackendBinding {
       public:
         OpenGLBinding(GLFWwindow* window, DawnDevice device) : BackendBinding(window, device) {
-            // Load the GL entry points in our copy of the glad static library
-            gladLoadGLLoader(reinterpret_cast<GLADloadproc>(glfwGetProcAddress));
         }
 
         uint64_t GetSwapChainImplementation() override {
             if (mSwapchainImpl.userData == nullptr) {
-                mSwapchainImpl = CreateSwapChainImplementation(new SwapChainImplGL(mWindow));
+                mSwapchainImpl = dawn_native::opengl::CreateNativeSwapChainImpl(
+                    mDevice,
+                    [](void* userdata) { glfwSwapBuffers(static_cast<GLFWwindow*>(userdata)); },
+                    mWindow);
             }
             return reinterpret_cast<uint64_t>(&mSwapchainImpl);
         }
 
         DawnTextureFormat GetPreferredSwapChainTextureFormat() override {
-            return DAWN_TEXTURE_FORMAT_R8_G8_B8_A8_UNORM;
+            return dawn_native::opengl::GetNativeSwapChainPreferredFormat(&mSwapchainImpl);
         }
 
       private: