Revert "Make the default framebuffer owned by Surface"

This reverts commit 87e63a9982803b5e4c12afa9a40ff0e2b04a7369.

Speculative revert to fix the webgl cts on Windows D3D9 and the
unittests on Linux.

BUG=

Change-Id: I488f4e0b2dc67270eed45f1c10bfba1d13c98739
Reviewed-on: https://chromium-review.googlesource.com/293350
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Tested-by: Corentin Wallez <cwallez@chromium.org>
diff --git a/src/libANGLE/Context.cpp b/src/libANGLE/Context.cpp
index 18b5ab0..05b9d55 100644
--- a/src/libANGLE/Context.cpp
+++ b/src/libANGLE/Context.cpp
@@ -94,10 +94,15 @@
 
     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);
@@ -131,13 +136,10 @@
 {
     mState.reset();
 
-    for (auto framebuffer : mFramebufferMap)
+    while (!mFramebufferMap.empty())
     {
-        // Default framebuffer are owned by their respective Surface
-        if (framebuffer.second->id() != 0)
-        {
-            SafeDelete(framebuffer.second);
-        }
+        // Delete the framebuffer in reverse order to destroy the framebuffer zero last.
+        deleteFramebuffer(mFramebufferMap.rbegin()->first);
     }
 
     while (!mFenceNVMap.empty())
@@ -202,43 +204,60 @@
     {
         releaseSurface();
     }
-    surface->setIsCurrent(true);
-    mCurrentSurface = surface;
 
-    // Update default framebuffer, the binding of the previous default
-    // framebuffer (or lack of) will have a nullptr.
+    ASSERT(mCurrentSurface == nullptr);
+    mCurrentSurface = surface;
+    surface->setIsCurrent(true);
+
+    // 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)
     {
-        Framebuffer *newDefault = surface->getDefaultFramebuffer();
-        if (mState.getReadFramebuffer() == nullptr)
-        {
-            mState.setReadFramebufferBinding(newDefault);
-        }
-        if (mState.getDrawFramebuffer() == nullptr)
-        {
-            mState.setDrawFramebufferBinding(newDefault);
-        }
-        mFramebufferMap[0] = newDefault;
+        // 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);
     }
 }
 
 void Context::releaseSurface()
 {
-    ASSERT(mCurrentSurface != nullptr);
-
-    // Remove the default framebuffer
+    Framebuffer *defaultFBO = mFramebufferMap[0];
+    if (defaultFBO)
     {
-        Framebuffer *currentDefault = mCurrentSurface->getDefaultFramebuffer();
-        if (mState.getReadFramebuffer() == currentDefault)
-        {
-            mState.setReadFramebufferBinding(nullptr);
-        }
-        if (mState.getDrawFramebuffer() == currentDefault)
-        {
-            mState.setDrawFramebufferBinding(nullptr);
-        }
-        mFramebufferMap.erase(0);
+        defaultFBO->resetAttachment(GL_BACK);
+        defaultFBO->resetAttachment(GL_DEPTH);
+        defaultFBO->resetAttachment(GL_STENCIL);
     }
 
+    ASSERT(mCurrentSurface != nullptr);
     mCurrentSurface->setIsCurrent(false);
     mCurrentSurface = nullptr;
 }
@@ -1381,19 +1400,10 @@
 
 EGLenum Context::getRenderBuffer() const
 {
-    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;
-    }
+    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;
 }
 
 const Caps &Context::getCaps() const