Make the default framebuffer owned by Surface

Reland with a fix for SurfaceTest in angle_unittests and fixes for
signed-unsigned warnings

In CGL there is no notion of default Framebuffer and MakeCurrent only
makes a context current but not a drawable. Instead, everything is done
via render to texture. For that reason, different surfaces will have
different FBOs as default framebuffers, which causes that change.

BUG=angleproject:891

Change-Id: Ie9a72de01a58e583a1bfa3f4a055debb860b912f
Reviewed-on: https://chromium-review.googlesource.com/293713
Reviewed-by: Geoff Lang <geofflang@chromium.org>
Tested-by: Corentin Wallez <cwallez@chromium.org>
diff --git a/src/libANGLE/Context.cpp b/src/libANGLE/Context.cpp
index 0752970..3325a5c 100644
--- a/src/libANGLE/Context.cpp
+++ b/src/libANGLE/Context.cpp
@@ -94,15 +94,10 @@
 
     mState.initializeZeroTextures(mZeroTextures);
 
-    // Allocate default FBO
-    mFramebufferMap[0] = new Framebuffer(mCaps, mRenderer, 0);
-
     bindVertexArray(0);
     bindArrayBuffer(0);
     bindElementArrayBuffer(0);
 
-    bindReadFramebuffer(0);
-    bindDrawFramebuffer(0);
     bindRenderbuffer(0);
 
     bindGenericUniformBuffer(0);
@@ -136,10 +131,13 @@
 {
     mState.reset();
 
-    while (!mFramebufferMap.empty())
+    for (auto framebuffer : mFramebufferMap)
     {
-        // Delete the framebuffer in reverse order to destroy the framebuffer zero last.
-        deleteFramebuffer(mFramebufferMap.rbegin()->first);
+        // Default framebuffer are owned by their respective Surface
+        if (framebuffer.second->id() != 0)
+        {
+            SafeDelete(framebuffer.second);
+        }
     }
 
     while (!mFenceNVMap.empty())
@@ -204,60 +202,43 @@
     {
         releaseSurface();
     }
-
-    ASSERT(mCurrentSurface == nullptr);
-    mCurrentSurface = surface;
     surface->setIsCurrent(true);
+    mCurrentSurface = surface;
 
-    // Update default framebuffer
-    Framebuffer *defaultFBO = mFramebufferMap[0];
-
-    GLenum drawBufferState = GL_BACK;
-    defaultFBO->setDrawBuffers(1, &drawBufferState);
-    defaultFBO->setReadBuffer(GL_BACK);
-
-    const FramebufferAttachment *backAttachment = defaultFBO->getAttachment(GL_BACK);
-
-    if (backAttachment && backAttachment->getSurface() == surface)
+    // Update default framebuffer, the binding of the previous default
+    // framebuffer (or lack of) will have a nullptr.
     {
-        // FBO already initialized to the surface.
-        return;
-    }
-
-    const egl::Config *config = surface->getConfig();
-
-    defaultFBO->setAttachment(GL_FRAMEBUFFER_DEFAULT, GL_BACK, ImageIndex::MakeInvalid(), surface);
-
-    if (config->depthSize > 0)
-    {
-        defaultFBO->setAttachment(GL_FRAMEBUFFER_DEFAULT, GL_DEPTH, ImageIndex::MakeInvalid(), surface);
-    }
-    else
-    {
-        defaultFBO->resetAttachment(GL_DEPTH);
-    }
-
-    if (config->stencilSize > 0)
-    {
-        defaultFBO->setAttachment(GL_FRAMEBUFFER_DEFAULT, GL_STENCIL, ImageIndex::MakeInvalid(), surface);
-    }
-    else
-    {
-        defaultFBO->resetAttachment(GL_STENCIL);
+        Framebuffer *newDefault = surface->getDefaultFramebuffer();
+        if (mState.getReadFramebuffer() == nullptr)
+        {
+            mState.setReadFramebufferBinding(newDefault);
+        }
+        if (mState.getDrawFramebuffer() == nullptr)
+        {
+            mState.setDrawFramebufferBinding(newDefault);
+        }
+        mFramebufferMap[0] = newDefault;
     }
 }
 
 void Context::releaseSurface()
 {
-    Framebuffer *defaultFBO = mFramebufferMap[0];
-    if (defaultFBO)
+    ASSERT(mCurrentSurface != nullptr);
+
+    // Remove the default framebuffer
     {
-        defaultFBO->resetAttachment(GL_BACK);
-        defaultFBO->resetAttachment(GL_DEPTH);
-        defaultFBO->resetAttachment(GL_STENCIL);
+        Framebuffer *currentDefault = mCurrentSurface->getDefaultFramebuffer();
+        if (mState.getReadFramebuffer() == currentDefault)
+        {
+            mState.setReadFramebufferBinding(nullptr);
+        }
+        if (mState.getDrawFramebuffer() == currentDefault)
+        {
+            mState.setDrawFramebufferBinding(nullptr);
+        }
+        mFramebufferMap.erase(0);
     }
 
-    ASSERT(mCurrentSurface != nullptr);
     mCurrentSurface->setIsCurrent(false);
     mCurrentSurface = nullptr;
 }
@@ -1402,10 +1383,19 @@
 
 EGLenum Context::getRenderBuffer() const
 {
-    ASSERT(mFramebufferMap.count(0) > 0);
-    const Framebuffer *framebuffer = mFramebufferMap.find(0)->second;
-    const FramebufferAttachment *backAttachment = framebuffer->getAttachment(GL_BACK);
-    return backAttachment ? backAttachment->getSurface()->getRenderBuffer() : EGL_NONE;
+    auto framebufferIt = mFramebufferMap.find(0);
+    if (framebufferIt != mFramebufferMap.end())
+    {
+        const Framebuffer *framebuffer              = framebufferIt->second;
+        const FramebufferAttachment *backAttachment = framebuffer->getAttachment(GL_BACK);
+
+        ASSERT(backAttachment != nullptr);
+        return backAttachment->getSurface()->getRenderBuffer();
+    }
+    else
+    {
+        return EGL_NONE;
+    }
 }
 
 const Caps &Context::getCaps() const
diff --git a/src/libANGLE/Framebuffer.cpp b/src/libANGLE/Framebuffer.cpp
index 0880984..afc1630 100644
--- a/src/libANGLE/Framebuffer.cpp
+++ b/src/libANGLE/Framebuffer.cpp
@@ -20,6 +20,7 @@
 #include "libANGLE/renderer/FramebufferImpl.h"
 #include "libANGLE/renderer/ImplFactory.h"
 #include "libANGLE/renderer/RenderbufferImpl.h"
+#include "libANGLE/renderer/SurfaceImpl.h"
 
 namespace gl
 {
@@ -37,6 +38,14 @@
 }
 }
 
+Framebuffer::Data::Data()
+    : mColorAttachments(1),
+      mDrawBufferStates(1, GL_NONE),
+      mReadBufferState(GL_COLOR_ATTACHMENT0_EXT)
+{
+    mDrawBufferStates[0] = GL_COLOR_ATTACHMENT0_EXT;
+}
+
 Framebuffer::Data::Data(const Caps &caps)
     : mColorAttachments(caps.maxColorAttachments),
       mDrawBufferStates(caps.maxDrawBuffers, GL_NONE),
@@ -83,7 +92,7 @@
     return nullptr;
 }
 
-const FramebufferAttachment *Framebuffer::Data::getColorAttachment(unsigned int colorAttachment) const
+const FramebufferAttachment *Framebuffer::Data::getColorAttachment(size_t colorAttachment) const
 {
     ASSERT(colorAttachment < mColorAttachments.size());
     return mColorAttachments[colorAttachment].isAttached() ?
@@ -116,18 +125,15 @@
 }
 
 Framebuffer::Framebuffer(const Caps &caps, rx::ImplFactory *factory, GLuint id)
-    : mData(caps),
-      mImpl(nullptr),
-      mId(id)
+    : mData(caps), mImpl(factory->createFramebuffer(mData)), mId(id)
 {
-    if (mId == 0)
-    {
-        mImpl = factory->createDefaultFramebuffer(mData);
-    }
-    else
-    {
-        mImpl = factory->createFramebuffer(mData);
-    }
+    ASSERT(mId != 0);
+    ASSERT(mImpl != nullptr);
+}
+
+Framebuffer::Framebuffer(rx::SurfaceImpl *surface)
+    : mData(), mImpl(surface->createDefaultFramebuffer(mData)), mId(0)
+{
     ASSERT(mImpl != nullptr);
 }
 
@@ -157,7 +163,7 @@
     DetachMatchingAttachment(&mData.mStencilAttachment, resourceType, resourceId);
 }
 
-const FramebufferAttachment *Framebuffer::getColorbuffer(unsigned int colorAttachment) const
+const FramebufferAttachment *Framebuffer::getColorbuffer(size_t colorAttachment) const
 {
     return mData.getColorAttachment(colorAttachment);
 }
@@ -257,7 +263,7 @@
     mImpl->setReadBuffer(buffer);
 }
 
-bool Framebuffer::isEnabledColorAttachment(unsigned int colorAttachment) const
+bool Framebuffer::isEnabledColorAttachment(size_t colorAttachment) const
 {
     ASSERT(colorAttachment < mData.mColorAttachments.size());
     return (mData.mColorAttachments[colorAttachment].isAttached() &&
@@ -277,6 +283,11 @@
     return false;
 }
 
+size_t Framebuffer::getNumColorBuffers() const
+{
+    return mData.mColorAttachments.size();
+}
+
 bool Framebuffer::hasStencil() const
 {
     return (mData.mStencilAttachment.isAttached() && mData.mStencilAttachment.getStencilSize() > 0);
diff --git a/src/libANGLE/Framebuffer.h b/src/libANGLE/Framebuffer.h
index 3b132a3..a22f317 100644
--- a/src/libANGLE/Framebuffer.h
+++ b/src/libANGLE/Framebuffer.h
@@ -23,6 +23,7 @@
 class ImplFactory;
 class FramebufferImpl;
 class RenderbufferImpl;
+class SurfaceImpl;
 }
 
 namespace egl
@@ -50,13 +51,14 @@
     class Data final : angle::NonCopyable
     {
       public:
+        explicit Data();
         explicit Data(const Caps &caps);
         ~Data();
 
         const FramebufferAttachment *getReadAttachment() const;
         const FramebufferAttachment *getFirstColorAttachment() const;
         const FramebufferAttachment *getDepthOrStencilAttachment() const;
-        const FramebufferAttachment *getColorAttachment(unsigned int colorAttachment) const;
+        const FramebufferAttachment *getColorAttachment(size_t colorAttachment) const;
         const FramebufferAttachment *getDepthAttachment() const;
         const FramebufferAttachment *getStencilAttachment() const;
         const FramebufferAttachment *getDepthStencilAttachment() const;
@@ -76,6 +78,7 @@
     };
 
     Framebuffer(const Caps &caps, rx::ImplFactory *factory, GLuint id);
+    Framebuffer(rx::SurfaceImpl *surface);
     virtual ~Framebuffer();
 
     const rx::FramebufferImpl *getImplementation() const { return mImpl; }
@@ -92,7 +95,7 @@
     void detachTexture(GLuint texture);
     void detachRenderbuffer(GLuint renderbuffer);
 
-    const FramebufferAttachment *getColorbuffer(unsigned int colorAttachment) const;
+    const FramebufferAttachment *getColorbuffer(size_t colorAttachment) const;
     const FramebufferAttachment *getDepthbuffer() const;
     const FramebufferAttachment *getStencilbuffer() const;
     const FramebufferAttachment *getDepthStencilBuffer() const;
@@ -109,8 +112,9 @@
     GLenum getReadBufferState() const;
     void setReadBuffer(GLenum buffer);
 
-    bool isEnabledColorAttachment(unsigned int colorAttachment) const;
+    bool isEnabledColorAttachment(size_t colorAttachment) const;
     bool hasEnabledColorAttachment() const;
+    size_t getNumColorBuffers() const;
     bool hasStencil() const;
     int getSamples(const gl::Data &data) const;
     bool usingExtendedDrawBuffers() const;
diff --git a/src/libANGLE/Surface.cpp b/src/libANGLE/Surface.cpp
index d85d4fc..673425e 100644
--- a/src/libANGLE/Surface.cpp
+++ b/src/libANGLE/Surface.cpp
@@ -11,6 +11,7 @@
 #include "libANGLE/Surface.h"
 
 #include "libANGLE/Config.h"
+#include "libANGLE/Framebuffer.h"
 #include "libANGLE/Texture.h"
 
 #include <EGL/eglext.h>
@@ -26,6 +27,7 @@
                  const AttributeMap &attributes)
     : FramebufferAttachmentObject(),
       mImplementation(impl),
+      mDefaultFramebuffer(nullptr),
       mCurrentCount(0),
       mDestroyed(false),
       mType(surfaceType),
@@ -55,6 +57,9 @@
         mTextureFormat = attributes.get(EGL_TEXTURE_FORMAT, EGL_NO_TEXTURE);
         mTextureTarget = attributes.get(EGL_TEXTURE_TARGET, EGL_NO_TEXTURE);
     }
+
+    mDefaultFramebuffer = createDefaultFramebuffer();
+    ASSERT(mDefaultFramebuffer != nullptr);
 }
 
 Surface::~Surface()
@@ -213,4 +218,30 @@
     UNREACHABLE();
     return 0;
 }
+
+gl::Framebuffer *Surface::createDefaultFramebuffer()
+{
+    gl::Framebuffer *framebuffer = new gl::Framebuffer(mImplementation);
+
+    GLenum drawBufferState = GL_BACK;
+    framebuffer->setDrawBuffers(1, &drawBufferState);
+    framebuffer->setReadBuffer(GL_BACK);
+
+    framebuffer->setAttachment(GL_FRAMEBUFFER_DEFAULT, GL_BACK, gl::ImageIndex::MakeInvalid(),
+                               this);
+
+    if (mConfig->depthSize > 0)
+    {
+        framebuffer->setAttachment(GL_FRAMEBUFFER_DEFAULT, GL_DEPTH, gl::ImageIndex::MakeInvalid(),
+                                   this);
+    }
+
+    if (mConfig->stencilSize > 0)
+    {
+        framebuffer->setAttachment(GL_FRAMEBUFFER_DEFAULT, GL_STENCIL,
+                                   gl::ImageIndex::MakeInvalid(), this);
+    }
+
+    return framebuffer;
+}
 }
diff --git a/src/libANGLE/Surface.h b/src/libANGLE/Surface.h
index 019777e..cc6279f 100644
--- a/src/libANGLE/Surface.h
+++ b/src/libANGLE/Surface.h
@@ -21,6 +21,7 @@
 
 namespace gl
 {
+class Framebuffer;
 class Texture;
 }
 
@@ -64,6 +65,7 @@
     EGLenum getTextureTarget() const;
 
     gl::Texture *getBoundTexture() const { return mTexture.get(); }
+    gl::Framebuffer *getDefaultFramebuffer() { return mDefaultFramebuffer; }
 
     EGLint isFixedSize() const;
 
@@ -81,11 +83,14 @@
     virtual ~Surface();
     rx::FramebufferAttachmentObjectImpl *getAttachmentImpl() const override { return mImplementation; }
 
+    gl::Framebuffer *createDefaultFramebuffer();
+
     // ANGLE-only method, used internally
     friend class gl::Texture;
     void releaseTexImageFromTexture();
 
     rx::SurfaceImpl *mImplementation;
+    gl::Framebuffer *mDefaultFramebuffer;
     int mCurrentCount;
     bool mDestroyed;
 
diff --git a/src/libANGLE/Surface_unittest.cpp b/src/libANGLE/Surface_unittest.cpp
index f732a9d..3d1cb8b 100644
--- a/src/libANGLE/Surface_unittest.cpp
+++ b/src/libANGLE/Surface_unittest.cpp
@@ -6,9 +6,13 @@
 
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
+#include "libANGLE/angletypes.h"
 #include "libANGLE/AttributeMap.h"
 #include "libANGLE/Config.h"
+#include "libANGLE/Data.h"
+#include "libANGLE/State.h"
 #include "libANGLE/Surface.h"
+#include "libANGLE/renderer/FramebufferImpl.h"
 #include "libANGLE/renderer/SurfaceImpl.h"
 
 namespace
@@ -20,6 +24,8 @@
     virtual ~MockSurfaceImpl() { destroy(); }
 
     MOCK_METHOD0(initialize, egl::Error());
+    MOCK_METHOD1(createDefaultFramebuffer,
+                 rx::FramebufferImpl *(const gl::Framebuffer::Data &data));
     MOCK_METHOD0(swap, egl::Error());
     MOCK_METHOD4(postSubBuffer, egl::Error(EGLint, EGLint, EGLint, EGLint));
     MOCK_METHOD2(querySurfacePointerANGLE, egl::Error(EGLint, void**));
@@ -35,15 +41,71 @@
     MOCK_METHOD0(destroy, void());
 };
 
+class MockFramebufferImpl : public rx::FramebufferImpl
+{
+  public:
+    MockFramebufferImpl() : rx::FramebufferImpl(gl::Framebuffer::Data()) {}
+    virtual ~MockFramebufferImpl() { destroy(); }
+
+    MOCK_METHOD1(onUpdateColorAttachment, void(size_t));
+    MOCK_METHOD0(onUpdateDepthAttachment, void());
+    MOCK_METHOD0(onUpdateStencilAttachment, void());
+    MOCK_METHOD0(onUpdateDepthStencilAttachment, void());
+
+    MOCK_METHOD2(setDrawBuffers, void(size_t, const GLenum *));
+    MOCK_METHOD1(setReadBuffer, void(GLenum));
+
+    MOCK_METHOD2(discard, gl::Error(size_t, const GLenum *));
+    MOCK_METHOD2(invalidate, gl::Error(size_t, const GLenum *));
+    MOCK_METHOD3(invalidateSub, gl::Error(size_t, const GLenum *, const gl::Rectangle &));
+
+    MOCK_METHOD2(clear, gl::Error(const gl::Data &, GLbitfield));
+    MOCK_METHOD4(clearBufferfv, gl::Error(const gl::State &, GLenum, GLint, const GLfloat *));
+    MOCK_METHOD4(clearBufferuiv, gl::Error(const gl::State &, GLenum, GLint, const GLuint *));
+    MOCK_METHOD4(clearBufferiv, gl::Error(const gl::State &, GLenum, GLint, const GLint *));
+    MOCK_METHOD5(clearBufferfi, gl::Error(const gl::State &, GLenum, GLint, GLfloat, GLint));
+
+    MOCK_CONST_METHOD0(getImplementationColorReadFormat, GLenum());
+    MOCK_CONST_METHOD0(getImplementationColorReadType, GLenum());
+    MOCK_CONST_METHOD5(
+        readPixels,
+        gl::Error(const gl::State &, const gl::Rectangle &, GLenum, GLenum, GLvoid *));
+
+    MOCK_METHOD6(blit,
+                 gl::Error(const gl::State &,
+                           const gl::Rectangle &,
+                           const gl::Rectangle &,
+                           GLbitfield,
+                           GLenum,
+                           const gl::Framebuffer *));
+
+    MOCK_CONST_METHOD0(checkStatus, GLenum());
+
+    MOCK_METHOD0(destroy, void());
+};
+
 class SurfaceTest : public testing::Test
 {
   protected:
     virtual void SetUp()
     {
-        mImpl = new MockSurfaceImpl;
-        EXPECT_CALL(*mImpl, getSwapBehavior());
-        EXPECT_CALL(*mImpl, destroy());
-        mSurface = new egl::Surface(mImpl, EGL_WINDOW_BIT, &mConfig, egl::AttributeMap());
+        MockFramebufferImpl *framebuffer = new MockFramebufferImpl;
+
+        mSurfaceImpl = new MockSurfaceImpl;
+
+        EXPECT_CALL(*mSurfaceImpl, getSwapBehavior());
+        EXPECT_CALL(*mSurfaceImpl, createDefaultFramebuffer(testing::_))
+            .WillOnce(testing::Return(framebuffer));
+        EXPECT_CALL(*mSurfaceImpl, destroy());
+
+        EXPECT_CALL(*framebuffer, setDrawBuffers(1, testing::_));
+        EXPECT_CALL(*framebuffer, setReadBuffer(GL_BACK));
+        EXPECT_CALL(*framebuffer, onUpdateColorAttachment(0));
+
+        mSurface = new egl::Surface(mSurfaceImpl, EGL_WINDOW_BIT, &mConfig, egl::AttributeMap());
+
+        EXPECT_CALL(*framebuffer, destroy());
+        SafeDelete(framebuffer);
     }
 
     virtual void TearDown()
@@ -51,20 +113,31 @@
         mSurface->onDestroy();
     }
 
-    MockSurfaceImpl *mImpl;
+    MockSurfaceImpl *mSurfaceImpl;
     egl::Surface *mSurface;
     egl::Config mConfig;
 };
 
 TEST_F(SurfaceTest, DestructionDeletesImpl)
 {
+    MockFramebufferImpl *framebuffer = new MockFramebufferImpl;
+
     MockSurfaceImpl *impl = new MockSurfaceImpl;
     EXPECT_CALL(*impl, getSwapBehavior());
+    EXPECT_CALL(*impl, createDefaultFramebuffer(testing::_)).WillOnce(testing::Return(framebuffer));
+
+    EXPECT_CALL(*framebuffer, setDrawBuffers(1, testing::_));
+    EXPECT_CALL(*framebuffer, setReadBuffer(GL_BACK));
+    EXPECT_CALL(*framebuffer, onUpdateColorAttachment(0));
+
     EXPECT_CALL(*impl, destroy()).Times(1).RetiresOnSaturation();
 
     egl::Surface *surface = new egl::Surface(impl, EGL_WINDOW_BIT, &mConfig, egl::AttributeMap());
     surface->onDestroy();
 
+    EXPECT_CALL(*framebuffer, destroy());
+    SafeDelete(framebuffer);
+
     // Only needed because the mock is leaked if bugs are present,
     // which logs an error, but does not cause the test to fail.
     // Ordinarily mocks are verified when destroyed.
diff --git a/src/libANGLE/renderer/DisplayImpl.cpp b/src/libANGLE/renderer/DisplayImpl.cpp
index 16446f9..2eb44eb 100644
--- a/src/libANGLE/renderer/DisplayImpl.cpp
+++ b/src/libANGLE/renderer/DisplayImpl.cpp
@@ -29,8 +29,8 @@
 
 void DisplayImpl::destroySurface(egl::Surface *surface)
 {
-    surface->onDestroy();
     mSurfaceSet.erase(surface);
+    surface->onDestroy();
 }
 
 const egl::DisplayExtensions &DisplayImpl::getExtensions() const
diff --git a/src/libANGLE/renderer/ImplFactory.h b/src/libANGLE/renderer/ImplFactory.h
index 43b4efe..f28751e 100644
--- a/src/libANGLE/renderer/ImplFactory.h
+++ b/src/libANGLE/renderer/ImplFactory.h
@@ -41,7 +41,6 @@
     virtual ProgramImpl *createProgram(const gl::Program::Data &data) = 0;
 
     // Framebuffer creation
-    virtual FramebufferImpl *createDefaultFramebuffer(const gl::Framebuffer::Data &data) = 0;
     virtual FramebufferImpl *createFramebuffer(const gl::Framebuffer::Data &data) = 0;
 
     // Texture creation
diff --git a/src/libANGLE/renderer/SurfaceImpl.h b/src/libANGLE/renderer/SurfaceImpl.h
index cc23478..1d5211b 100644
--- a/src/libANGLE/renderer/SurfaceImpl.h
+++ b/src/libANGLE/renderer/SurfaceImpl.h
@@ -11,6 +11,7 @@
 
 #include "common/angleutils.h"
 #include "libANGLE/Error.h"
+#include "libANGLE/Framebuffer.h"
 #include "libANGLE/FramebufferAttachment.h"
 
 namespace egl
@@ -22,6 +23,8 @@
 namespace rx
 {
 
+class FramebufferImpl;
+
 class SurfaceImpl : public FramebufferAttachmentObjectImpl
 {
   public:
@@ -29,6 +32,7 @@
     virtual ~SurfaceImpl();
 
     virtual egl::Error initialize() = 0;
+    virtual FramebufferImpl *createDefaultFramebuffer(const gl::Framebuffer::Data &data) = 0;
     virtual egl::Error swap() = 0;
     virtual egl::Error postSubBuffer(EGLint x, EGLint y, EGLint width, EGLint height) = 0;
     virtual egl::Error querySurfacePointerANGLE(EGLint attribute, void **value) = 0;
diff --git a/src/libANGLE/renderer/d3d/RendererD3D.cpp b/src/libANGLE/renderer/d3d/RendererD3D.cpp
index c64b1f8..36b45e9 100644
--- a/src/libANGLE/renderer/d3d/RendererD3D.cpp
+++ b/src/libANGLE/renderer/d3d/RendererD3D.cpp
@@ -519,7 +519,7 @@
     size_t textureCount = 0;
 
     const gl::Framebuffer *drawFramebuffer = data.state->getDrawFramebuffer();
-    for (unsigned int i = 0; i < data.caps->maxColorAttachments; i++)
+    for (int i = 0; i < drawFramebuffer->getNumColorBuffers(); i++)
     {
         const gl::FramebufferAttachment *attachment = drawFramebuffer->getColorbuffer(i);
         if (attachment && attachment->type() == GL_TEXTURE)
diff --git a/src/libANGLE/renderer/d3d/SurfaceD3D.cpp b/src/libANGLE/renderer/d3d/SurfaceD3D.cpp
index 146f6b2..77ccd29 100644
--- a/src/libANGLE/renderer/d3d/SurfaceD3D.cpp
+++ b/src/libANGLE/renderer/d3d/SurfaceD3D.cpp
@@ -80,6 +80,11 @@
     return egl::Error(EGL_SUCCESS);
 }
 
+FramebufferImpl *SurfaceD3D::createDefaultFramebuffer(const gl::Framebuffer::Data &data)
+{
+    return mRenderer->createFramebuffer(data);
+}
+
 egl::Error SurfaceD3D::bindTexImage(EGLint)
 {
     return egl::Error(EGL_SUCCESS);
diff --git a/src/libANGLE/renderer/d3d/SurfaceD3D.h b/src/libANGLE/renderer/d3d/SurfaceD3D.h
index 3fb248a..e6f727a 100644
--- a/src/libANGLE/renderer/d3d/SurfaceD3D.h
+++ b/src/libANGLE/renderer/d3d/SurfaceD3D.h
@@ -33,6 +33,7 @@
     void releaseSwapChain();
 
     egl::Error initialize() override;
+    FramebufferImpl *createDefaultFramebuffer(const gl::Framebuffer::Data &data) override;
 
     egl::Error swap() override;
     egl::Error postSubBuffer(EGLint x, EGLint y, EGLint width, EGLint height) override;
diff --git a/src/libANGLE/renderer/d3d/d3d11/Renderer11.cpp b/src/libANGLE/renderer/d3d/d3d11/Renderer11.cpp
index 5c5a490..f163dc8 100644
--- a/src/libANGLE/renderer/d3d/d3d11/Renderer11.cpp
+++ b/src/libANGLE/renderer/d3d/d3d11/Renderer11.cpp
@@ -2962,11 +2962,6 @@
     return gl::Error(GL_NO_ERROR);
 }
 
-FramebufferImpl *Renderer11::createDefaultFramebuffer(const gl::Framebuffer::Data &data)
-{
-    return createFramebuffer(data);
-}
-
 FramebufferImpl *Renderer11::createFramebuffer(const gl::Framebuffer::Data &data)
 {
     return new Framebuffer11(data, this);
diff --git a/src/libANGLE/renderer/d3d/d3d11/Renderer11.h b/src/libANGLE/renderer/d3d/d3d11/Renderer11.h
index 591da23..abdc356 100644
--- a/src/libANGLE/renderer/d3d/d3d11/Renderer11.h
+++ b/src/libANGLE/renderer/d3d/d3d11/Renderer11.h
@@ -182,7 +182,6 @@
     virtual gl::Error createRenderTarget(int width, int height, GLenum format, GLsizei samples, RenderTargetD3D **outRT);
 
     // Framebuffer creation
-    FramebufferImpl *createDefaultFramebuffer(const gl::Framebuffer::Data &data) override;
     FramebufferImpl *createFramebuffer(const gl::Framebuffer::Data &data) override;
 
     // Shader creation
diff --git a/src/libANGLE/renderer/d3d/d3d9/Renderer9.cpp b/src/libANGLE/renderer/d3d/d3d9/Renderer9.cpp
index 2f2d959..f53a1db 100644
--- a/src/libANGLE/renderer/d3d/d3d9/Renderer9.cpp
+++ b/src/libANGLE/renderer/d3d/d3d9/Renderer9.cpp
@@ -2652,11 +2652,6 @@
     return gl::Error(GL_NO_ERROR);
 }
 
-FramebufferImpl *Renderer9::createDefaultFramebuffer(const gl::Framebuffer::Data &data)
-{
-    return createFramebuffer(data);
-}
-
 FramebufferImpl *Renderer9::createFramebuffer(const gl::Framebuffer::Data &data)
 {
     return new Framebuffer9(data, this);
diff --git a/src/libANGLE/renderer/d3d/d3d9/Renderer9.h b/src/libANGLE/renderer/d3d/d3d9/Renderer9.h
index ab828cd..0b4553e 100644
--- a/src/libANGLE/renderer/d3d/d3d9/Renderer9.h
+++ b/src/libANGLE/renderer/d3d/d3d9/Renderer9.h
@@ -167,7 +167,6 @@
     virtual gl::Error createRenderTarget(int width, int height, GLenum format, GLsizei samples, RenderTargetD3D **outRT);
 
     // Framebuffer creation
-    FramebufferImpl *createDefaultFramebuffer(const gl::Framebuffer::Data &data) override;
     FramebufferImpl *createFramebuffer(const gl::Framebuffer::Data &data) override;
 
     // Shader creation
diff --git a/src/libANGLE/renderer/gl/DisplayGL.h b/src/libANGLE/renderer/gl/DisplayGL.h
index 27f2b5d..363d31a 100644
--- a/src/libANGLE/renderer/gl/DisplayGL.h
+++ b/src/libANGLE/renderer/gl/DisplayGL.h
@@ -36,6 +36,7 @@
     egl::Error makeCurrent(egl::Surface *drawSurface, egl::Surface *readSurface, gl::Context *context) override;
 
   protected:
+    RendererGL *getRenderer() const { return mRenderer; };
     const gl::Version &getMaxSupportedESVersion() const;
 
   private:
diff --git a/src/libANGLE/renderer/gl/RendererGL.cpp b/src/libANGLE/renderer/gl/RendererGL.cpp
index 1dbcaee..ac9d471 100644
--- a/src/libANGLE/renderer/gl/RendererGL.cpp
+++ b/src/libANGLE/renderer/gl/RendererGL.cpp
@@ -181,11 +181,6 @@
     return new ProgramGL(data, mFunctions, mStateManager);
 }
 
-FramebufferImpl *RendererGL::createDefaultFramebuffer(const gl::Framebuffer::Data &data)
-{
-    return new FramebufferGL(data, mFunctions, mStateManager, true);
-}
-
 FramebufferImpl *RendererGL::createFramebuffer(const gl::Framebuffer::Data &data)
 {
     return new FramebufferGL(data, mFunctions, mStateManager, false);
diff --git a/src/libANGLE/renderer/gl/RendererGL.h b/src/libANGLE/renderer/gl/RendererGL.h
index 23c9182..67e0cf4 100644
--- a/src/libANGLE/renderer/gl/RendererGL.h
+++ b/src/libANGLE/renderer/gl/RendererGL.h
@@ -39,7 +39,6 @@
     ProgramImpl *createProgram(const gl::Program::Data &data) override;
 
     // Framebuffer creation
-    FramebufferImpl *createDefaultFramebuffer(const gl::Framebuffer::Data &data) override;
     FramebufferImpl *createFramebuffer(const gl::Framebuffer::Data &data) override;
 
     // Texture creation
@@ -80,6 +79,8 @@
     void syncState(const gl::State &state, const gl::State::DirtyBits &dirtyBits) override;
 
     const gl::Version &getMaxSupportedESVersion() const;
+    const FunctionsGL *getFunctions() const { return mFunctions; }
+    StateManagerGL *getStateManager() const { return mStateManager; }
 
   private:
     void generateCaps(gl::Caps *outCaps, gl::TextureCapsMap* outTextureCaps,
diff --git a/src/libANGLE/renderer/gl/SurfaceGL.cpp b/src/libANGLE/renderer/gl/SurfaceGL.cpp
index 592b1d2..162a62f 100644
--- a/src/libANGLE/renderer/gl/SurfaceGL.cpp
+++ b/src/libANGLE/renderer/gl/SurfaceGL.cpp
@@ -8,11 +8,13 @@
 
 #include "libANGLE/renderer/gl/SurfaceGL.h"
 
+#include "libANGLE/renderer/gl/FramebufferGL.h"
+#include "libANGLE/renderer/gl/RendererGL.h"
+
 namespace rx
 {
 
-SurfaceGL::SurfaceGL()
-    : SurfaceImpl()
+SurfaceGL::SurfaceGL(RendererGL *renderer) : SurfaceImpl(), mRenderer(renderer)
 {
 }
 
@@ -20,4 +22,8 @@
 {
 }
 
+FramebufferImpl *SurfaceGL::createDefaultFramebuffer(const gl::Framebuffer::Data &data)
+{
+    return new FramebufferGL(data, mRenderer->getFunctions(), mRenderer->getStateManager(), true);
+}
 }
diff --git a/src/libANGLE/renderer/gl/SurfaceGL.h b/src/libANGLE/renderer/gl/SurfaceGL.h
index aa8c7ee..e056dd2 100644
--- a/src/libANGLE/renderer/gl/SurfaceGL.h
+++ b/src/libANGLE/renderer/gl/SurfaceGL.h
@@ -14,10 +14,12 @@
 namespace rx
 {
 
+class RendererGL;
+
 class SurfaceGL : public SurfaceImpl
 {
   public:
-    SurfaceGL();
+    SurfaceGL(RendererGL *renderer);
     ~SurfaceGL() override;
 
     gl::Error getAttachmentRenderTarget(const gl::FramebufferAttachment::Target &target,
@@ -26,7 +28,12 @@
         return gl::Error(GL_OUT_OF_MEMORY, "Not supported on OpenGL");
     }
 
+    FramebufferImpl *createDefaultFramebuffer(const gl::Framebuffer::Data &data) override;
+
     virtual egl::Error makeCurrent() = 0;
+
+  private:
+    RendererGL *mRenderer;
 };
 
 }
diff --git a/src/libANGLE/renderer/gl/cgl/DisplayCGL.mm b/src/libANGLE/renderer/gl/cgl/DisplayCGL.mm
index d400d78..efeea31 100644
--- a/src/libANGLE/renderer/gl/cgl/DisplayCGL.mm
+++ b/src/libANGLE/renderer/gl/cgl/DisplayCGL.mm
@@ -42,7 +42,7 @@
                                              const egl::AttributeMap &attribs)
 {
     UNIMPLEMENTED();
-    return new WindowSurfaceCGL();
+    return new WindowSurfaceCGL(this->getRenderer());
 }
 
 SurfaceImpl *DisplayCGL::createPbufferSurface(const egl::Config *configuration,
diff --git a/src/libANGLE/renderer/gl/cgl/WindowSurfaceCGL.h b/src/libANGLE/renderer/gl/cgl/WindowSurfaceCGL.h
index 4982270..d1fe66d 100644
--- a/src/libANGLE/renderer/gl/cgl/WindowSurfaceCGL.h
+++ b/src/libANGLE/renderer/gl/cgl/WindowSurfaceCGL.h
@@ -17,7 +17,7 @@
 class WindowSurfaceCGL : public SurfaceGL
 {
   public:
-    WindowSurfaceCGL();
+    WindowSurfaceCGL(RendererGL *renderer);
     ~WindowSurfaceCGL() override;
 
     egl::Error initialize() override;
diff --git a/src/libANGLE/renderer/gl/cgl/WindowSurfaceCGL.mm b/src/libANGLE/renderer/gl/cgl/WindowSurfaceCGL.mm
index f6fe4f6..8c8e0a8 100644
--- a/src/libANGLE/renderer/gl/cgl/WindowSurfaceCGL.mm
+++ b/src/libANGLE/renderer/gl/cgl/WindowSurfaceCGL.mm
@@ -9,12 +9,12 @@
 #include "libANGLE/renderer/gl/cgl/WindowSurfaceCGL.h"
 
 #include "common/debug.h"
+#include "libANGLE/renderer/gl/cgl/DisplayCGL.h"
 
 namespace rx
 {
 
-WindowSurfaceCGL::WindowSurfaceCGL()
-    : SurfaceGL()
+WindowSurfaceCGL::WindowSurfaceCGL(RendererGL *renderer) : SurfaceGL(renderer)
 {
 }
 
diff --git a/src/libANGLE/renderer/gl/glx/DisplayGLX.cpp b/src/libANGLE/renderer/gl/glx/DisplayGLX.cpp
index 43662e8..1b82c2c 100644
--- a/src/libANGLE/renderer/gl/glx/DisplayGLX.cpp
+++ b/src/libANGLE/renderer/gl/glx/DisplayGLX.cpp
@@ -197,7 +197,8 @@
     ASSERT(configIdToGLXConfig.count(configuration->configID) > 0);
     glx::FBConfig fbConfig = configIdToGLXConfig[configuration->configID];
 
-    return new WindowSurfaceGLX(mGLX, *this, window, mGLX.getDisplay(), mContext, fbConfig);
+    return new WindowSurfaceGLX(mGLX, this, this->getRenderer(), window, mGLX.getDisplay(),
+                                mContext, fbConfig);
 }
 
 SurfaceImpl *DisplayGLX::createPbufferSurface(const egl::Config *configuration,
@@ -210,7 +211,8 @@
     EGLint height = attribs.get(EGL_HEIGHT, 0);
     bool largest = (attribs.get(EGL_LARGEST_PBUFFER, EGL_FALSE) == EGL_TRUE);
 
-    return new PbufferSurfaceGLX(width, height, largest, mGLX, mContext, fbConfig);
+    return new PbufferSurfaceGLX(this->getRenderer(), width, height, largest, mGLX, mContext,
+                                 fbConfig);
 }
 
 SurfaceImpl* DisplayGLX::createPbufferFromClientBuffer(const egl::Config *configuration,
diff --git a/src/libANGLE/renderer/gl/glx/PbufferSurfaceGLX.cpp b/src/libANGLE/renderer/gl/glx/PbufferSurfaceGLX.cpp
index 80e9e7c..d876c6d 100644
--- a/src/libANGLE/renderer/gl/glx/PbufferSurfaceGLX.cpp
+++ b/src/libANGLE/renderer/gl/glx/PbufferSurfaceGLX.cpp
@@ -9,14 +9,20 @@
 #include "libANGLE/renderer/gl/glx/PbufferSurfaceGLX.h"
 
 #include "common/debug.h"
+#include "libANGLE/renderer/gl/glx/DisplayGLX.h"
 #include "libANGLE/renderer/gl/glx/FunctionsGLX.h"
 
 namespace rx
 {
 
-PbufferSurfaceGLX::PbufferSurfaceGLX(EGLint width, EGLint height, bool largest, const FunctionsGLX &glx,
-                                     glx::Context context, glx::FBConfig fbConfig)
-    : SurfaceGL(),
+PbufferSurfaceGLX::PbufferSurfaceGLX(RendererGL *renderer,
+                                     EGLint width,
+                                     EGLint height,
+                                     bool largest,
+                                     const FunctionsGLX &glx,
+                                     glx::Context context,
+                                     glx::FBConfig fbConfig)
+    : SurfaceGL(renderer),
       mWidth(width),
       mHeight(height),
       mLargest(largest),
diff --git a/src/libANGLE/renderer/gl/glx/PbufferSurfaceGLX.h b/src/libANGLE/renderer/gl/glx/PbufferSurfaceGLX.h
index d1548ed..f8d1bf7 100644
--- a/src/libANGLE/renderer/gl/glx/PbufferSurfaceGLX.h
+++ b/src/libANGLE/renderer/gl/glx/PbufferSurfaceGLX.h
@@ -15,13 +15,19 @@
 namespace rx
 {
 
+class DisplayGLX;
 class FunctionsGLX;
 
 class PbufferSurfaceGLX : public SurfaceGL
 {
   public:
-    PbufferSurfaceGLX(EGLint width, EGLint height, bool largest, const FunctionsGLX &glx,
-                      glx::Context context, glx::FBConfig fbConfig);
+    PbufferSurfaceGLX(RendererGL *renderer,
+                      EGLint width,
+                      EGLint height,
+                      bool largest,
+                      const FunctionsGLX &glx,
+                      glx::Context context,
+                      glx::FBConfig fbConfig);
     ~PbufferSurfaceGLX() override;
 
     egl::Error initialize() override;
diff --git a/src/libANGLE/renderer/gl/glx/WindowSurfaceGLX.cpp b/src/libANGLE/renderer/gl/glx/WindowSurfaceGLX.cpp
index 80c9e4a..4224eeb 100644
--- a/src/libANGLE/renderer/gl/glx/WindowSurfaceGLX.cpp
+++ b/src/libANGLE/renderer/gl/glx/WindowSurfaceGLX.cpp
@@ -16,14 +16,19 @@
 namespace rx
 {
 
-WindowSurfaceGLX::WindowSurfaceGLX(const FunctionsGLX &glx, const DisplayGLX &glxDisplay, Window window, Display *display,
-                                   glx::Context context, glx::FBConfig fbConfig)
-    : SurfaceGL(),
+WindowSurfaceGLX::WindowSurfaceGLX(const FunctionsGLX &glx,
+                                   DisplayGLX *glxDisplay,
+                                   RendererGL *renderer,
+                                   Window window,
+                                   Display *display,
+                                   glx::Context context,
+                                   glx::FBConfig fbConfig)
+    : SurfaceGL(renderer),
       mParent(window),
       mWindow(0),
       mDisplay(display),
       mGLX(glx),
-      mGLXDisplay(glxDisplay),
+      mGLXDisplay(*glxDisplay),
       mContext(context),
       mFBConfig(fbConfig),
       mGLXWindow(0),
diff --git a/src/libANGLE/renderer/gl/glx/WindowSurfaceGLX.h b/src/libANGLE/renderer/gl/glx/WindowSurfaceGLX.h
index de18f2b..f8447ab 100644
--- a/src/libANGLE/renderer/gl/glx/WindowSurfaceGLX.h
+++ b/src/libANGLE/renderer/gl/glx/WindowSurfaceGLX.h
@@ -21,8 +21,13 @@
 class WindowSurfaceGLX : public SurfaceGL
 {
   public:
-    WindowSurfaceGLX(const FunctionsGLX &glx, const DisplayGLX &glxDisplay, Window window, Display *display,
-                     glx::Context context, glx::FBConfig fbConfig);
+    WindowSurfaceGLX(const FunctionsGLX &glx,
+                     DisplayGLX *glxDisplay,
+                     RendererGL *renderer,
+                     Window window,
+                     Display *display,
+                     glx::Context context,
+                     glx::FBConfig fbConfig);
     ~WindowSurfaceGLX() override;
 
     egl::Error initialize() override;
diff --git a/src/libANGLE/renderer/gl/wgl/DisplayWGL.cpp b/src/libANGLE/renderer/gl/wgl/DisplayWGL.cpp
index 71bdf28..81418bf 100644
--- a/src/libANGLE/renderer/gl/wgl/DisplayWGL.cpp
+++ b/src/libANGLE/renderer/gl/wgl/DisplayWGL.cpp
@@ -321,7 +321,8 @@
                                              EGLNativeWindowType window,
                                              const egl::AttributeMap &attribs)
 {
-    return new WindowSurfaceWGL(window, mPixelFormat, mWGLContext, mFunctionsWGL);
+    return new WindowSurfaceWGL(this->getRenderer(), window, mPixelFormat, mWGLContext,
+                                mFunctionsWGL);
 }
 
 SurfaceImpl *DisplayWGL::createPbufferSurface(const egl::Config *configuration,
@@ -333,8 +334,8 @@
     EGLenum textureFormat = attribs.get(EGL_TEXTURE_FORMAT, EGL_NO_TEXTURE);
     EGLenum textureTarget = attribs.get(EGL_TEXTURE_TARGET, EGL_NO_TEXTURE);
 
-    return new PbufferSurfaceWGL(width, height, textureFormat, textureTarget, largest,
-                                 mPixelFormat, mDeviceContext, mWGLContext, mFunctionsWGL);
+    return new PbufferSurfaceWGL(this->getRenderer(), width, height, textureFormat, textureTarget,
+                                 largest, mPixelFormat, mDeviceContext, mWGLContext, mFunctionsWGL);
 }
 
 SurfaceImpl *DisplayWGL::createPbufferFromClientBuffer(const egl::Config *configuration,
diff --git a/src/libANGLE/renderer/gl/wgl/PbufferSurfaceWGL.cpp b/src/libANGLE/renderer/gl/wgl/PbufferSurfaceWGL.cpp
index 7d0cacb..0615c87 100644
--- a/src/libANGLE/renderer/gl/wgl/PbufferSurfaceWGL.cpp
+++ b/src/libANGLE/renderer/gl/wgl/PbufferSurfaceWGL.cpp
@@ -9,16 +9,24 @@
 #include "libANGLE/renderer/gl/wgl/PbufferSurfaceWGL.h"
 
 #include "common/debug.h"
+#include "libANGLE/renderer/gl/RendererGL.h"
 #include "libANGLE/renderer/gl/wgl/FunctionsWGL.h"
 #include "libANGLE/renderer/gl/wgl/wgl_utils.h"
 
 namespace rx
 {
 
-PbufferSurfaceWGL::PbufferSurfaceWGL(EGLint width, EGLint height, EGLenum textureFormat, EGLenum textureTarget,
-                                     bool largest, int pixelFormat, HDC deviceContext, HGLRC wglContext,
+PbufferSurfaceWGL::PbufferSurfaceWGL(RendererGL *renderer,
+                                     EGLint width,
+                                     EGLint height,
+                                     EGLenum textureFormat,
+                                     EGLenum textureTarget,
+                                     bool largest,
+                                     int pixelFormat,
+                                     HDC deviceContext,
+                                     HGLRC wglContext,
                                      const FunctionsWGL *functions)
-    : SurfaceGL(),
+    : SurfaceGL(renderer),
       mWidth(width),
       mHeight(height),
       mLargest(largest),
diff --git a/src/libANGLE/renderer/gl/wgl/PbufferSurfaceWGL.h b/src/libANGLE/renderer/gl/wgl/PbufferSurfaceWGL.h
index 9608a2c..3deab4b 100644
--- a/src/libANGLE/renderer/gl/wgl/PbufferSurfaceWGL.h
+++ b/src/libANGLE/renderer/gl/wgl/PbufferSurfaceWGL.h
@@ -21,8 +21,15 @@
 class PbufferSurfaceWGL : public SurfaceGL
 {
   public:
-    PbufferSurfaceWGL(EGLint width, EGLint height, EGLenum textureFormat, EGLenum textureTarget,
-                      bool largest, int pixelFormat, HDC deviceContext, HGLRC wglContext,
+    PbufferSurfaceWGL(RendererGL *renderer,
+                      EGLint width,
+                      EGLint height,
+                      EGLenum textureFormat,
+                      EGLenum textureTarget,
+                      bool largest,
+                      int pixelFormat,
+                      HDC deviceContext,
+                      HGLRC wglContext,
                       const FunctionsWGL *functions);
     ~PbufferSurfaceWGL() override;
 
diff --git a/src/libANGLE/renderer/gl/wgl/WindowSurfaceWGL.cpp b/src/libANGLE/renderer/gl/wgl/WindowSurfaceWGL.cpp
index 4bfea67..9c898a7 100644
--- a/src/libANGLE/renderer/gl/wgl/WindowSurfaceWGL.cpp
+++ b/src/libANGLE/renderer/gl/wgl/WindowSurfaceWGL.cpp
@@ -9,17 +9,19 @@
 #include "libANGLE/renderer/gl/wgl/WindowSurfaceWGL.h"
 
 #include "common/debug.h"
+#include "libANGLE/renderer/gl/RendererGL.h"
 #include "libANGLE/renderer/gl/wgl/FunctionsWGL.h"
 #include "libANGLE/renderer/gl/wgl/wgl_utils.h"
 
 namespace rx
 {
 
-WindowSurfaceWGL::WindowSurfaceWGL(EGLNativeWindowType window,
+WindowSurfaceWGL::WindowSurfaceWGL(RendererGL *renderer,
+                                   EGLNativeWindowType window,
                                    int pixelFormat,
                                    HGLRC wglContext,
                                    const FunctionsWGL *functions)
-    : SurfaceGL(),
+    : SurfaceGL(renderer),
       mPixelFormat(pixelFormat),
       mWGLContext(wglContext),
       mWindow(window),
diff --git a/src/libANGLE/renderer/gl/wgl/WindowSurfaceWGL.h b/src/libANGLE/renderer/gl/wgl/WindowSurfaceWGL.h
index b89096e..568db3d 100644
--- a/src/libANGLE/renderer/gl/wgl/WindowSurfaceWGL.h
+++ b/src/libANGLE/renderer/gl/wgl/WindowSurfaceWGL.h
@@ -21,7 +21,11 @@
 class WindowSurfaceWGL : public SurfaceGL
 {
   public:
-    WindowSurfaceWGL(EGLNativeWindowType window, int pixelFormat, HGLRC wglContext, const FunctionsWGL *functions);
+    WindowSurfaceWGL(RendererGL *renderer,
+                     EGLNativeWindowType window,
+                     int pixelFormat,
+                     HGLRC wglContext,
+                     const FunctionsWGL *functions);
     ~WindowSurfaceWGL() override;
 
     egl::Error initialize() override;
diff --git a/src/libANGLE/validationES.cpp b/src/libANGLE/validationES.cpp
index 75abf3b..73aafab 100644
--- a/src/libANGLE/validationES.cpp
+++ b/src/libANGLE/validationES.cpp
@@ -605,7 +605,7 @@
             GLenum readInternalFormat = readColorBuffer->getInternalFormat();
             const InternalFormat &readFormatInfo = GetInternalFormatInfo(readInternalFormat);
 
-            for (GLuint i = 0; i < context->getCaps().maxColorAttachments; i++)
+            for (size_t i = 0; i < drawFramebuffer->getNumColorBuffers(); i++)
             {
                 if (drawFramebuffer->isEnabledColorAttachment(i))
                 {
@@ -661,7 +661,8 @@
                     return false;
                 }
 
-                for (GLuint colorAttachment = 0; colorAttachment < context->getCaps().maxColorAttachments; ++colorAttachment)
+                for (size_t colorAttachment = 0;
+                     colorAttachment < drawFramebuffer->getNumColorBuffers(); ++colorAttachment)
                 {
                     if (drawFramebuffer->isEnabledColorAttachment(colorAttachment))
                     {
diff --git a/src/tests/angle_unittests_utils.h b/src/tests/angle_unittests_utils.h
index 84c5d7e..cf2f321 100644
--- a/src/tests/angle_unittests_utils.h
+++ b/src/tests/angle_unittests_utils.h
@@ -26,7 +26,6 @@
     ProgramImpl *createProgram(const gl::Program::Data &data) override { return nullptr; }
 
     // Framebuffer creation
-    FramebufferImpl *createDefaultFramebuffer(const gl::Framebuffer::Data &data) override { return nullptr; }
     FramebufferImpl *createFramebuffer(const gl::Framebuffer::Data &data) override { return nullptr; }
 
     // Texture creation
diff --git a/src/tests/gl_tests/D3D11EmulatedIndexedBufferTest.cpp b/src/tests/gl_tests/D3D11EmulatedIndexedBufferTest.cpp
index 3798839..dc7f43b 100644
--- a/src/tests/gl_tests/D3D11EmulatedIndexedBufferTest.cpp
+++ b/src/tests/gl_tests/D3D11EmulatedIndexedBufferTest.cpp
@@ -55,7 +55,7 @@
     void TearDown() override
     {
         SafeDelete(mSourceBuffer);
-         ANGLETest::TearDown();
+        ANGLETest::TearDown();
     }
 
     void createMappableCompareBufferFromEmulatedBuffer(ID3D11Buffer *sourceBuffer, GLuint size, ID3D11Buffer **mappableBuffer)