Proliferate gl::Context everywhere.

This gives the D3D back-end access to the GL state almost anywhere.
This uses the onDestroy hook for Textures to push errors up from
destructors, although they still don't quite make it to the Context.

There are places, such as in EGL object (Context/Surface) destruction,
where we end up calling through to GL implementation internals without
having access to a gl::Context. We handle this via a proxy Context
to a Display, basically a null context, that has access to impl-side
state like the Renderer pointer if necessary. It does not have access
to the normal GL state.

Also Pass gl::Context to RefCountObject::release(). Since we're using
destroy() methods now, we should not ever call the destructor directly.

BUG=angleproject:1156

Change-Id: Ie4c32ad6bf6caaff0289901f30b5c6bafa2ce259
Reviewed-on: https://chromium-review.googlesource.com/529707
Commit-Queue: Jamie Madill <jmadill@chromium.org>
Reviewed-by: Geoff Lang <geofflang@chromium.org>
diff --git a/src/libANGLE/renderer/gl/FramebufferGL.cpp b/src/libANGLE/renderer/gl/FramebufferGL.cpp
index e9e0e15..81211b1 100644
--- a/src/libANGLE/renderer/gl/FramebufferGL.cpp
+++ b/src/libANGLE/renderer/gl/FramebufferGL.cpp
@@ -132,13 +132,13 @@
     }
 }
 
-Error FramebufferGL::discard(size_t count, const GLenum *attachments)
+Error FramebufferGL::discard(const gl::Context *context, size_t count, const GLenum *attachments)
 {
     // glInvalidateFramebuffer accepts the same enums as glDiscardFramebufferEXT
-    return invalidate(count, attachments);
+    return invalidate(context, count, attachments);
 }
 
-Error FramebufferGL::invalidate(size_t count, const GLenum *attachments)
+Error FramebufferGL::invalidate(const gl::Context *context, size_t count, const GLenum *attachments)
 {
     const GLenum *finalAttachmentsPtr = attachments;
 
@@ -165,7 +165,8 @@
     return gl::NoError();
 }
 
-Error FramebufferGL::invalidateSub(size_t count,
+Error FramebufferGL::invalidateSub(const gl::Context *context,
+                                   size_t count,
                                    const GLenum *attachments,
                                    const gl::Rectangle &area)
 {
@@ -249,14 +250,14 @@
     return gl::NoError();
 }
 
-GLenum FramebufferGL::getImplementationColorReadFormat() const
+GLenum FramebufferGL::getImplementationColorReadFormat(const gl::Context *context) const
 {
     const auto *readAttachment = mState.getReadAttachment();
     const Format &format       = readAttachment->getFormat();
     return format.info->getReadPixelsFormat();
 }
 
-GLenum FramebufferGL::getImplementationColorReadType() const
+GLenum FramebufferGL::getImplementationColorReadType(const gl::Context *context) const
 {
     const auto *readAttachment = mState.getReadAttachment();
     const Format &format       = readAttachment->getFormat();
@@ -284,7 +285,7 @@
     if (mWorkarounds.packOverlappingRowsSeparatelyPackBuffer && packState.pixelBuffer.get() &&
         packState.rowLength != 0 && packState.rowLength < area.width)
     {
-        return readPixelsRowByRowWorkaround(area, readFormat, readType, packState, pixels);
+        return readPixelsRowByRowWorkaround(context, area, readFormat, readType, packState, pixels);
     }
 
     if (mWorkarounds.packLastRowSeparatelyForPaddingInclusion)
@@ -298,7 +299,8 @@
 
         if (apply)
         {
-            return readPixelsPaddingWorkaround(area, readFormat, readType, packState, pixels);
+            return readPixelsPaddingWorkaround(context, area, readFormat, readType, packState,
+                                               pixels);
         }
     }
 
@@ -369,7 +371,7 @@
     }
 
     // Enable FRAMEBUFFER_SRGB if needed
-    mStateManager->setFramebufferSRGBEnabledForFramebuffer(context->getContextState(), true, this);
+    mStateManager->setFramebufferSRGBEnabledForFramebuffer(context, true, this);
 
     GLenum blitMask = mask;
     if (needManualColorBlit && (mask & GL_COLOR_BUFFER_BIT) && readAttachmentSamples <= 1)
@@ -527,11 +529,11 @@
                 }
             }
 
-            mStateManager->setFramebufferSRGBEnabled(context->getContextState(), hasSRGBAttachment);
+            mStateManager->setFramebufferSRGBEnabled(context, hasSRGBAttachment);
         }
         else
         {
-            mStateManager->setFramebufferSRGBEnabled(context->getContextState(), !mIsDefault);
+            mStateManager->setFramebufferSRGBEnabled(context, !mIsDefault);
         }
     }
 }
@@ -561,13 +563,13 @@
 
             if (attachment != nullptr)
             {
-                mStateManager->setFramebufferSRGBEnabled(context->getContextState(),
+                mStateManager->setFramebufferSRGBEnabled(context,
                                                          attachment->getColorEncoding() == GL_SRGB);
             }
         }
         else
         {
-            mStateManager->setFramebufferSRGBEnabled(context->getContextState(), !mIsDefault);
+            mStateManager->setFramebufferSRGBEnabled(context, !mIsDefault);
         }
     }
 }
@@ -609,7 +611,8 @@
     return true;
 }
 
-gl::Error FramebufferGL::readPixelsRowByRowWorkaround(const gl::Rectangle &area,
+gl::Error FramebufferGL::readPixelsRowByRowWorkaround(const gl::Context *context,
+                                                      const gl::Rectangle &area,
                                                       GLenum format,
                                                       GLenum type,
                                                       const gl::PixelPackState &pack,
@@ -625,10 +628,10 @@
     ANGLE_TRY_RESULT(glFormat.computeSkipBytes(rowBytes, 0, pack, false), skipBytes);
 
     gl::PixelPackState directPack;
-    directPack.pixelBuffer = pack.pixelBuffer;
+    directPack.pixelBuffer.set(context, pack.pixelBuffer.get());
     directPack.alignment   = 1;
     mStateManager->setPixelPackState(directPack);
-    directPack.pixelBuffer.set(nullptr);
+    directPack.pixelBuffer.set(context, nullptr);
 
     offset += skipBytes;
     for (GLint row = 0; row < area.height; ++row)
@@ -641,7 +644,8 @@
     return gl::NoError();
 }
 
-gl::Error FramebufferGL::readPixelsPaddingWorkaround(const gl::Rectangle &area,
+gl::Error FramebufferGL::readPixelsPaddingWorkaround(const gl::Context *context,
+                                                     const gl::Rectangle &area,
                                                      GLenum format,
                                                      GLenum type,
                                                      const gl::PixelPackState &pack,
@@ -662,10 +666,10 @@
 
     // Get the last row manually
     gl::PixelPackState directPack;
-    directPack.pixelBuffer = pack.pixelBuffer;
+    directPack.pixelBuffer.set(context, pack.pixelBuffer.get());
     directPack.alignment   = 1;
     mStateManager->setPixelPackState(directPack);
-    directPack.pixelBuffer.set(nullptr);
+    directPack.pixelBuffer.set(context, nullptr);
 
     intptr_t lastRowOffset =
         reinterpret_cast<intptr_t>(pixels) + skipBytes + (area.height - 1) * rowBytes;