Add makeCurrent and unMakeCurrent to SurfaceImpl.

SurfaceGL had these methods already so they are just moving up the inheritance
hierarchy.  This ends up simplifying some state tracking we had in our surface
implementations.

BUG=angleproject:2464

Change-Id: I480588ca8470d9ef507f95e0c0297fe126b3abfb
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/1595434
Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org>
Reviewed-by: Jamie Madill <jmadill@chromium.org>
Commit-Queue: Geoff Lang <geofflang@chromium.org>
diff --git a/src/libANGLE/Context.cpp b/src/libANGLE/Context.cpp
index 857f73d..6036492 100644
--- a/src/libANGLE/Context.cpp
+++ b/src/libANGLE/Context.cpp
@@ -8198,7 +8198,7 @@
     Framebuffer *newDefault = nullptr;
     if (surface != nullptr)
     {
-        ANGLE_TRY(surface->setIsCurrent(this, true));
+        ANGLE_TRY(surface->makeCurrent(this));
         mCurrentSurface = surface;
         newDefault      = surface->createDefaultFramebuffer(this);
     }
@@ -8254,7 +8254,7 @@
     mCurrentSurface       = nullptr;
     if (surface)
     {
-        ANGLE_TRY(surface->setIsCurrent(this, false));
+        ANGLE_TRY(surface->unMakeCurrent(this));
     }
 
     return egl::NoError();
diff --git a/src/libANGLE/Surface.cpp b/src/libANGLE/Surface.cpp
index 6daec4f..5fff977 100644
--- a/src/libANGLE/Surface.cpp
+++ b/src/libANGLE/Surface.cpp
@@ -177,14 +177,17 @@
     return NoError();
 }
 
-Error Surface::setIsCurrent(const gl::Context *context, bool isCurrent)
+Error Surface::makeCurrent(const gl::Context *context)
 {
-    if (isCurrent)
-    {
-        mRefCount++;
-        return NoError();
-    }
+    ANGLE_TRY(mImplementation->makeCurrent(context));
 
+    mRefCount++;
+    return NoError();
+}
+
+Error Surface::unMakeCurrent(const gl::Context *context)
+{
+    ANGLE_TRY(mImplementation->unMakeCurrent(context));
     return releaseRef(context->getDisplay());
 }
 
diff --git a/src/libANGLE/Surface.h b/src/libANGLE/Surface.h
index 9721938..a11cbee 100644
--- a/src/libANGLE/Surface.h
+++ b/src/libANGLE/Surface.h
@@ -68,6 +68,8 @@
     EGLint getType() const;
 
     Error initialize(const Display *display);
+    Error makeCurrent(const gl::Context *context);
+    Error unMakeCurrent(const gl::Context *context);
     Error swap(const gl::Context *context);
     Error swapWithDamage(const gl::Context *context, EGLint *rects, EGLint n_rects);
     Error postSubBuffer(const gl::Context *context,
@@ -85,7 +87,6 @@
     EGLint isPostSubBufferSupported() const;
 
     void setSwapInterval(EGLint interval);
-    Error setIsCurrent(const gl::Context *context, bool isCurrent);
     Error onDestroy(const Display *display);
 
     void setMipmapLevel(EGLint level);
diff --git a/src/libANGLE/renderer/SurfaceImpl.cpp b/src/libANGLE/renderer/SurfaceImpl.cpp
index 071accd..3abc355 100644
--- a/src/libANGLE/renderer/SurfaceImpl.cpp
+++ b/src/libANGLE/renderer/SurfaceImpl.cpp
@@ -15,6 +15,16 @@
 
 SurfaceImpl::~SurfaceImpl() {}
 
+egl::Error SurfaceImpl::makeCurrent(const gl::Context *context)
+{
+    return egl::NoError();
+}
+
+egl::Error SurfaceImpl::unMakeCurrent(const gl::Context *context)
+{
+    return egl::NoError();
+}
+
 egl::Error SurfaceImpl::swapWithDamage(const gl::Context *context, EGLint *rects, EGLint n_rects)
 {
     UNREACHABLE();
diff --git a/src/libANGLE/renderer/SurfaceImpl.h b/src/libANGLE/renderer/SurfaceImpl.h
index ea1a051..e223c31 100644
--- a/src/libANGLE/renderer/SurfaceImpl.h
+++ b/src/libANGLE/renderer/SurfaceImpl.h
@@ -53,6 +53,8 @@
     virtual egl::Error initialize(const egl::Display *display)                           = 0;
     virtual FramebufferImpl *createDefaultFramebuffer(const gl::Context *context,
                                                       const gl::FramebufferState &state) = 0;
+    virtual egl::Error makeCurrent(const gl::Context *context);
+    virtual egl::Error unMakeCurrent(const gl::Context *context);
     virtual egl::Error swap(const gl::Context *context)                                  = 0;
     virtual egl::Error swapWithDamage(const gl::Context *context, EGLint *rects, EGLint n_rects);
     virtual egl::Error postSubBuffer(const gl::Context *context,
diff --git a/src/libANGLE/renderer/gl/DisplayGL.cpp b/src/libANGLE/renderer/gl/DisplayGL.cpp
index a89e136..8ea2876 100644
--- a/src/libANGLE/renderer/gl/DisplayGL.cpp
+++ b/src/libANGLE/renderer/gl/DisplayGL.cpp
@@ -22,9 +22,7 @@
 namespace rx
 {
 
-DisplayGL::DisplayGL(const egl::DisplayState &state)
-    : DisplayImpl(state), mCurrentDrawSurface(nullptr)
-{}
+DisplayGL::DisplayGL(const egl::DisplayState &state) : DisplayImpl(state) {}
 
 DisplayGL::~DisplayGL() {}
 
@@ -56,14 +54,6 @@
                                   egl::Surface *readSurface,
                                   gl::Context *context)
 {
-    // Notify the previous surface (if it still exists) that it is no longer current
-    if (mCurrentDrawSurface &&
-        mState.surfaceSet.find(mCurrentDrawSurface) != mState.surfaceSet.end())
-    {
-        ANGLE_TRY(GetImplAs<SurfaceGL>(mCurrentDrawSurface)->unMakeCurrent());
-    }
-    mCurrentDrawSurface = nullptr;
-
     if (!context)
     {
         return egl::NoError();
@@ -73,17 +63,12 @@
     ContextGL *glContext = GetImplAs<ContextGL>(context);
     glContext->getStateManager()->pauseTransformFeedback();
 
-    if (drawSurface != nullptr)
+    if (drawSurface == nullptr)
     {
-        SurfaceGL *glDrawSurface = GetImplAs<SurfaceGL>(drawSurface);
-        ANGLE_TRY(glDrawSurface->makeCurrent(context));
-        mCurrentDrawSurface = drawSurface;
-        return egl::NoError();
+        ANGLE_TRY(makeCurrentSurfaceless(context));
     }
-    else
-    {
-        return makeCurrentSurfaceless(context);
-    }
+
+    return egl::NoError();
 }
 
 void DisplayGL::generateExtensions(egl::DisplayExtensions *outExtensions) const
diff --git a/src/libANGLE/renderer/gl/DisplayGL.h b/src/libANGLE/renderer/gl/DisplayGL.h
index 45676d2..28347dc 100644
--- a/src/libANGLE/renderer/gl/DisplayGL.h
+++ b/src/libANGLE/renderer/gl/DisplayGL.h
@@ -48,8 +48,6 @@
 
   private:
     virtual egl::Error makeCurrentSurfaceless(gl::Context *context);
-
-    egl::Surface *mCurrentDrawSurface;
 };
 
 }  // namespace rx
diff --git a/src/libANGLE/renderer/gl/SurfaceGL.cpp b/src/libANGLE/renderer/gl/SurfaceGL.cpp
index 6959bb6..7e24ba8 100644
--- a/src/libANGLE/renderer/gl/SurfaceGL.cpp
+++ b/src/libANGLE/renderer/gl/SurfaceGL.cpp
@@ -34,11 +34,6 @@
     return egl::EglBadSurface();
 }
 
-egl::Error SurfaceGL::unMakeCurrent()
-{
-    return egl::NoError();
-}
-
 angle::Result SurfaceGL::initializeContents(const gl::Context *context,
                                             const gl::ImageIndex &imageIndex)
 {
diff --git a/src/libANGLE/renderer/gl/SurfaceGL.h b/src/libANGLE/renderer/gl/SurfaceGL.h
index d0e05ff..c08d336 100644
--- a/src/libANGLE/renderer/gl/SurfaceGL.h
+++ b/src/libANGLE/renderer/gl/SurfaceGL.h
@@ -26,9 +26,6 @@
 
     angle::Result initializeContents(const gl::Context *context,
                                      const gl::ImageIndex &imageIndex) override;
-
-    virtual egl::Error makeCurrent(const gl::Context *context) = 0;
-    virtual egl::Error unMakeCurrent();
 };
 
 }  // namespace rx
diff --git a/src/libANGLE/renderer/gl/cgl/IOSurfaceSurfaceCGL.h b/src/libANGLE/renderer/gl/cgl/IOSurfaceSurfaceCGL.h
index ed26a59..049fd15 100644
--- a/src/libANGLE/renderer/gl/cgl/IOSurfaceSurfaceCGL.h
+++ b/src/libANGLE/renderer/gl/cgl/IOSurfaceSurfaceCGL.h
@@ -39,7 +39,7 @@
 
     egl::Error initialize(const egl::Display *display) override;
     egl::Error makeCurrent(const gl::Context *context) override;
-    egl::Error unMakeCurrent() override;
+    egl::Error unMakeCurrent(const gl::Context *context) override;
 
     egl::Error swap(const gl::Context *context) override;
     egl::Error postSubBuffer(const gl::Context *context,
@@ -67,7 +67,6 @@
   private:
     CGLContextObj mCGLContext;
     IOSurfaceRef mIOSurface;
-    const gl::Context *mCurrentContext;
     int mWidth;
     int mHeight;
     int mPlane;
diff --git a/src/libANGLE/renderer/gl/cgl/IOSurfaceSurfaceCGL.mm b/src/libANGLE/renderer/gl/cgl/IOSurfaceSurfaceCGL.mm
index 261c1db..eb697d6 100644
--- a/src/libANGLE/renderer/gl/cgl/IOSurfaceSurfaceCGL.mm
+++ b/src/libANGLE/renderer/gl/cgl/IOSurfaceSurfaceCGL.mm
@@ -71,7 +71,6 @@
     : SurfaceGL(state),
       mCGLContext(cglContext),
       mIOSurface(nullptr),
-      mCurrentContext(nullptr),
       mWidth(0),
       mHeight(0),
       mPlane(0),
@@ -108,16 +107,12 @@
 
 egl::Error IOSurfaceSurfaceCGL::makeCurrent(const gl::Context *context)
 {
-    ASSERT(!mCurrentContext);
-    mCurrentContext = context;
     return egl::NoError();
 }
 
-egl::Error IOSurfaceSurfaceCGL::unMakeCurrent()
+egl::Error IOSurfaceSurfaceCGL::unMakeCurrent(const gl::Context *context)
 {
-    ASSERT(mCurrentContext);
-    GetFunctionsGL(mCurrentContext)->flush();
-    mCurrentContext = nullptr;
+    GetFunctionsGL(context)->flush();
     return egl::NoError();
 }
 
diff --git a/src/libANGLE/renderer/gl/wgl/D3DTextureSurfaceWGL.cpp b/src/libANGLE/renderer/gl/wgl/D3DTextureSurfaceWGL.cpp
index 627bd8b..bf5eb5f 100644
--- a/src/libANGLE/renderer/gl/wgl/D3DTextureSurfaceWGL.cpp
+++ b/src/libANGLE/renderer/gl/wgl/D3DTextureSurfaceWGL.cpp
@@ -359,7 +359,7 @@
     return egl::NoError();
 }
 
-egl::Error D3DTextureSurfaceWGL::unMakeCurrent()
+egl::Error D3DTextureSurfaceWGL::unMakeCurrent(const gl::Context *context)
 {
     if (!mFunctionsWGL->dxUnlockObjectsNV(mDeviceHandle, 1, &mBoundObjectRenderbufferHandle))
     {
diff --git a/src/libANGLE/renderer/gl/wgl/D3DTextureSurfaceWGL.h b/src/libANGLE/renderer/gl/wgl/D3DTextureSurfaceWGL.h
index 88fe356..6c38817 100644
--- a/src/libANGLE/renderer/gl/wgl/D3DTextureSurfaceWGL.h
+++ b/src/libANGLE/renderer/gl/wgl/D3DTextureSurfaceWGL.h
@@ -42,7 +42,7 @@
 
     egl::Error initialize(const egl::Display *display) override;
     egl::Error makeCurrent(const gl::Context *context) override;
-    egl::Error unMakeCurrent() override;
+    egl::Error unMakeCurrent(const gl::Context *context) override;
 
     egl::Error swap(const gl::Context *context) override;
     egl::Error postSubBuffer(const gl::Context *context,