Refactor Framebuffer management into a FramebufferManager class.

BUG=angleproject:1639

Change-Id: I03a0950f26557983c8dc816a27cb4038c60e5755
Reviewed-on: https://chromium-review.googlesource.com/415611
Reviewed-by: Jamie Madill <jmadill@chromium.org>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Geoff Lang <geofflang@chromium.org>
Commit-Queue: Geoff Lang <geofflang@chromium.org>
diff --git a/src/libANGLE/ResourceManager.cpp b/src/libANGLE/ResourceManager.cpp
index 2e7f2a8..b3b598e 100644
--- a/src/libANGLE/ResourceManager.cpp
+++ b/src/libANGLE/ResourceManager.cpp
@@ -485,4 +485,70 @@
     }
 }
 
+FramebufferManager::~FramebufferManager()
+{
+    for (auto framebuffer : mFramebuffers)
+    {
+        // Default framebuffer are owned by their respective Surface
+        if (framebuffer.second != nullptr && framebuffer.second->id() != 0)
+        {
+            SafeDelete(framebuffer.second);
+        }
+    }
+}
+
+GLuint FramebufferManager::createFramebuffer()
+{
+    return AllocateEmptyObject(&mHandleAllocator, &mFramebuffers);
+}
+
+void FramebufferManager::deleteFramebuffer(GLuint framebuffer)
+{
+    DeleteObject(&mHandleAllocator, &mFramebuffers, framebuffer, [](Framebuffer *framebuffer) {
+        ASSERT(framebuffer->id() != 0);
+        delete framebuffer;
+    });
+}
+
+Framebuffer *FramebufferManager::checkFramebufferAllocation(rx::GLImplFactory *factory,
+                                                            const Caps &caps,
+                                                            GLuint handle)
+{
+    // Can be called from Bind without a prior call to Gen.
+    auto framebufferIt = mFramebuffers.find(handle);
+    bool neverCreated  = framebufferIt == mFramebuffers.end();
+    if (neverCreated || framebufferIt->second == nullptr)
+    {
+        ASSERT(handle != 0);
+        Framebuffer *newFBO = new Framebuffer(caps, factory, handle);
+        if (neverCreated)
+        {
+            mHandleAllocator.reserve(handle);
+            mFramebuffers[handle] = newFBO;
+            return newFBO;
+        }
+
+        framebufferIt->second = newFBO;
+    }
+
+    return framebufferIt->second;
+}
+
+Framebuffer *FramebufferManager::getFramebuffer(GLuint handle) const
+{
+    return GetObject(mFramebuffers, handle);
+}
+
+void FramebufferManager::setDefaultFramebuffer(Framebuffer *framebuffer)
+{
+    ASSERT(framebuffer == nullptr || framebuffer->id() == 0);
+    mFramebuffers[0] = framebuffer;
+}
+
+bool FramebufferManager::isFramebufferGenerated(GLuint framebuffer)
+{
+    ASSERT(mFramebuffers.find(0) != mFramebuffers.end());
+    return mFramebuffers.find(framebuffer) != mFramebuffers.end();
+}
+
 }  // namespace gl