GL: Assume we only use EGL for now.

Classes that are not specific to EGL already started having EGL-isms
like the display or EGL function. There is a "Context" abstraction of
EGL vs. GLX but only one implementation with ContextEGL. For future
swapchain work we make the Device explicitly have a ContextEGL that can
be queried (so that the swapchain can interact more precisely with it).

In the future we could re-introduce the egl vs. glx context, potentially
by adding two subdirectories egl/ and glx/ and refactoring code in
opengl/ to not rely on it at all.

Bug: 344814083
Bug: 42241500
Change-Id: I69215aaeb95f52016421337a3a2d6a6c4014707f
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/192140
Reviewed-by: Stephen White <senorblanco@chromium.org>
Reviewed-by: Austin Eng <enga@chromium.org>
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
diff --git a/src/dawn/native/opengl/ContextEGL.h b/src/dawn/native/opengl/ContextEGL.h
index 8252fa1..dc55aad 100644
--- a/src/dawn/native/opengl/ContextEGL.h
+++ b/src/dawn/native/opengl/ContextEGL.h
@@ -30,23 +30,25 @@
 
 #include <memory>
 
+#include "dawn/common/NonMovable.h"
 #include "dawn/common/egl_platform.h"
 #include "dawn/native/opengl/DeviceGL.h"
 #include "dawn/native/opengl/EGLFunctions.h"
 
 namespace dawn::native::opengl {
 
-class ContextEGL : public Device::Context {
+class ContextEGL : NonMovable {
   public:
     static ResultOrError<std::unique_ptr<ContextEGL>> Create(const EGLFunctions& functions,
                                                              EGLenum api,
                                                              EGLDisplay display,
                                                              bool useANGLETextureSharing);
-    void MakeCurrent() override;
-    EGLDisplay GetEGLDisplay() const override;
-    const EGLFunctions& GetEGL() const override;
-    const EGLExtensionSet& GetExtensions() const override;
-    ~ContextEGL() override;
+    ~ContextEGL();
+
+    void MakeCurrent();
+    EGLDisplay GetEGLDisplay() const;
+    const EGLFunctions& GetEGL() const;
+    const EGLExtensionSet& GetExtensions() const;
 
   private:
     ContextEGL(const EGLFunctions& functions,
diff --git a/src/dawn/native/opengl/DeviceGL.cpp b/src/dawn/native/opengl/DeviceGL.cpp
index 6195811..2846706 100644
--- a/src/dawn/native/opengl/DeviceGL.cpp
+++ b/src/dawn/native/opengl/DeviceGL.cpp
@@ -39,6 +39,7 @@
 #include "dawn/native/opengl/BufferGL.h"
 #include "dawn/native/opengl/CommandBufferGL.h"
 #include "dawn/native/opengl/ComputePipelineGL.h"
+#include "dawn/native/opengl/ContextEGL.h"
 #include "dawn/native/opengl/PipelineLayoutGL.h"
 #include "dawn/native/opengl/QuerySetGL.h"
 #include "dawn/native/opengl/QueueGL.h"
@@ -121,7 +122,7 @@
 ResultOrError<Ref<Device>> Device::Create(AdapterBase* adapter,
                                           const UnpackedPtr<DeviceDescriptor>& descriptor,
                                           const OpenGLFunctions& functions,
-                                          std::unique_ptr<Context> context,
+                                          std::unique_ptr<ContextEGL> context,
                                           const TogglesState& deviceToggles,
                                           Ref<DeviceBase::DeviceLostEvent>&& lostEvent) {
     Ref<Device> device = AcquireRef(new Device(adapter, descriptor, functions, std::move(context),
@@ -133,7 +134,7 @@
 Device::Device(AdapterBase* adapter,
                const UnpackedPtr<DeviceDescriptor>& descriptor,
                const OpenGLFunctions& functions,
-               std::unique_ptr<Context> context,
+               std::unique_ptr<ContextEGL> context,
                const TogglesState& deviceToggles,
                Ref<DeviceBase::DeviceLostEvent>&& lostEvent)
     : DeviceBase(adapter, descriptor, deviceToggles, std::move(lostEvent)),
@@ -451,4 +452,8 @@
     return mContext->GetEGLDisplay();
 }
 
+ContextEGL* Device::GetContext() const {
+    return mContext.get();
+}
+
 }  // namespace dawn::native::opengl
diff --git a/src/dawn/native/opengl/DeviceGL.h b/src/dawn/native/opengl/DeviceGL.h
index e252bda..e809b71 100644
--- a/src/dawn/native/opengl/DeviceGL.h
+++ b/src/dawn/native/opengl/DeviceGL.h
@@ -46,6 +46,7 @@
 
 namespace dawn::native::opengl {
 
+class ContextEGL;
 struct EGLFunctions;
 
 enum class EGLExtension {
@@ -64,19 +65,21 @@
     static ResultOrError<Ref<Device>> Create(AdapterBase* adapter,
                                              const UnpackedPtr<DeviceDescriptor>& descriptor,
                                              const OpenGLFunctions& functions,
-                                             std::unique_ptr<Context> context,
+                                             std::unique_ptr<ContextEGL> context,
                                              const TogglesState& deviceToggles,
                                              Ref<DeviceBase::DeviceLostEvent>&& lostEvent);
     ~Device() override;
 
     MaybeError Initialize(const UnpackedPtr<DeviceDescriptor>& descriptor);
 
-    // Returns all the OpenGL entry points and ensures that the associated
-    // Context is current.
+    // Returns all the OpenGL entry points and ensures that the associated GL context is current.
     const OpenGLFunctions& GetGL() const;
+
+    // Helper functions to get access to relevant EGL objects.
     const EGLFunctions& GetEGL(bool makeCurrent) const;
     const EGLExtensionSet& GetEGLExtensions() const;
     EGLDisplay GetEGLDisplay() const;
+    ContextEGL* GetContext() const;
 
     const GLFormat& GetGLFormat(const Format& format);
 
@@ -113,21 +116,11 @@
     bool MayRequireDuplicationOfIndirectParameters() const override;
     bool ShouldApplyIndexBufferOffsetToFirstIndex() const override;
 
-    class Context {
-      public:
-        virtual ~Context() {}
-        virtual void MakeCurrent() = 0;
-        // TODO(dawn:2544) Abstract EGL-isms for use with desktop GL.
-        virtual EGLDisplay GetEGLDisplay() const = 0;
-        virtual const EGLFunctions& GetEGL() const = 0;
-        virtual const EGLExtensionSet& GetExtensions() const = 0;
-    };
-
   private:
     Device(AdapterBase* adapter,
            const UnpackedPtr<DeviceDescriptor>& descriptor,
            const OpenGLFunctions& functions,
-           std::unique_ptr<Context> context,
+           std::unique_ptr<ContextEGL> context,
            const TogglesState& deviceToggles,
            Ref<DeviceBase::DeviceLostEvent>&& lostEvent);
 
@@ -166,7 +159,7 @@
     const OpenGLFunctions mGL;
 
     GLFormatTable mFormatTable;
-    std::unique_ptr<Context> mContext = nullptr;
+    std::unique_ptr<ContextEGL> mContext = nullptr;
     int mMaxTextureMaxAnisotropy = 0;
 };
 
diff --git a/src/dawn/native/opengl/PhysicalDeviceGL.cpp b/src/dawn/native/opengl/PhysicalDeviceGL.cpp
index 29af3d7..f67b564 100644
--- a/src/dawn/native/opengl/PhysicalDeviceGL.cpp
+++ b/src/dawn/native/opengl/PhysicalDeviceGL.cpp
@@ -407,7 +407,6 @@
     Ref<DeviceBase::DeviceLostEvent>&& lostEvent) {
     EGLenum api =
         GetBackendType() == wgpu::BackendType::OpenGL ? EGL_OPENGL_API : EGL_OPENGL_ES_API;
-    std::unique_ptr<Device::Context> context;
     bool useANGLETextureSharing = false;
     for (size_t i = 0; i < descriptor->requiredFeatureCount; ++i) {
         if (descriptor->requiredFeatures[i] == wgpu::FeatureName::ANGLETextureSharing) {
@@ -415,8 +414,10 @@
         }
     }
 
+    std::unique_ptr<ContextEGL> context;
     DAWN_TRY_ASSIGN(context,
                     ContextEGL::Create(mEGLFunctions, api, mDisplay, useANGLETextureSharing));
+
     return Device::Create(adapter, descriptor, mFunctions, std::move(context), deviceToggles,
                           std::move(lostEvent));
 }