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/VertexArray.cpp b/src/libANGLE/VertexArray.cpp
index 2ee0581..04b5aa3 100644
--- a/src/libANGLE/VertexArray.cpp
+++ b/src/libANGLE/VertexArray.cpp
@@ -28,11 +28,6 @@
 
 VertexArrayState::~VertexArrayState()
 {
-    for (auto &binding : mVertexBindings)
-    {
-        binding.setBuffer(nullptr);
-    }
-    mElementArrayBuffer.set(nullptr);
 }
 
 VertexArray::VertexArray(rx::GLImplFactory *factory,
@@ -45,9 +40,21 @@
 {
 }
 
+void VertexArray::onDestroy(const Context *context)
+{
+    for (auto &binding : mState.mVertexBindings)
+    {
+        binding.setBuffer(context, nullptr);
+    }
+    mState.mElementArrayBuffer.set(context, nullptr);
+    mVertexArray->destroy(context);
+    SafeDelete(mVertexArray);
+    delete this;
+}
+
 VertexArray::~VertexArray()
 {
-    SafeDelete(mVertexArray);
+    ASSERT(!mVertexArray);
 }
 
 GLuint VertexArray::id() const
@@ -65,19 +72,19 @@
     return mState.mLabel;
 }
 
-void VertexArray::detachBuffer(GLuint bufferName)
+void VertexArray::detachBuffer(const Context *context, GLuint bufferName)
 {
     for (auto &binding : mState.mVertexBindings)
     {
         if (binding.getBuffer().id() == bufferName)
         {
-            binding.setBuffer(nullptr);
+            binding.setBuffer(context, nullptr);
         }
     }
 
     if (mState.mElementArrayBuffer.id() == bufferName)
     {
-        mState.mElementArrayBuffer.set(nullptr);
+        mState.mElementArrayBuffer.set(context, nullptr);
     }
 }
 
@@ -101,7 +108,8 @@
     return (dirtyBit - DIRTY_BIT_ATTRIB_0_ENABLED) % gl::MAX_VERTEX_ATTRIBS;
 }
 
-void VertexArray::bindVertexBuffer(size_t bindingIndex,
+void VertexArray::bindVertexBuffer(const Context *context,
+                                   size_t bindingIndex,
                                    Buffer *boundBuffer,
                                    GLintptr offset,
                                    GLsizei stride)
@@ -110,7 +118,7 @@
 
     VertexBinding *binding = &mState.mVertexBindings[bindingIndex];
 
-    binding->setBuffer(boundBuffer);
+    binding->setBuffer(context, boundBuffer);
     binding->setOffset(offset);
     binding->setStride(stride);
     mDirtyBits.set(DIRTY_BIT_BINDING_0_BUFFER + bindingIndex);
@@ -181,7 +189,8 @@
     }
 }
 
-void VertexArray::setAttributeState(size_t attribIndex,
+void VertexArray::setAttributeState(const Context *context,
+                                    size_t attribIndex,
                                     gl::Buffer *boundBuffer,
                                     GLint size,
                                     GLenum type,
@@ -203,14 +212,14 @@
     attrib.pointer                 = pointer;
     attrib.vertexAttribArrayStride = stride;
 
-    bindVertexBuffer(attribIndex, boundBuffer, offset, effectiveStride);
+    bindVertexBuffer(context, attribIndex, boundBuffer, offset, effectiveStride);
 
     mDirtyBits.set(DIRTY_BIT_ATTRIB_0_POINTER + attribIndex);
 }
 
-void VertexArray::setElementArrayBuffer(Buffer *buffer)
+void VertexArray::setElementArrayBuffer(const Context *context, Buffer *buffer)
 {
-    mState.mElementArrayBuffer.set(buffer);
+    mState.mElementArrayBuffer.set(context, buffer);
     mDirtyBits.set(DIRTY_BIT_ELEMENT_ARRAY_BUFFER);
 }