Reduce FBO rebinding on make current

This CL checks if it actually needs to rebind FBO and set up framebuffer
attachments when calling initDefaultFBO. glBindFramebuffer seems to
crash frequenly on some drivers. Hopefully we could reduce crash rate by
reducing the amount of bind framebuffer calls.

Bug: 281872037
Change-Id: I622b19be25955a2909c30643a8de0baa872b420c
GitOrigin-RevId: 2367fff5432290d6b610552f3ade6aa0f45b36d9
diff --git a/host/gl/glestranslator/GLcommon/GLEScontext.cpp b/host/gl/glestranslator/GLcommon/GLEScontext.cpp
index 9687ea0..ae1685f 100644
--- a/host/gl/glestranslator/GLcommon/GLEScontext.cpp
+++ b/host/gl/glestranslator/GLcommon/GLEScontext.cpp
@@ -2274,10 +2274,11 @@
         GLuint* eglSurfaceRBColorId, GLuint* eglSurfaceRBDepthId,
         GLuint readWidth, GLint readHeight, GLint readColorFormat, GLint readDepthStencilFormat, GLint readMultisamples,
         GLuint* eglReadSurfaceRBColorId, GLuint* eglReadSurfaceRBDepthId) {
-
+    bool needUpdateDefaultFbo = false;
     if (!m_defaultFBO) {
         dispatcher().glGenFramebuffers(1, &m_defaultFBO);
         m_defaultReadFBO = m_defaultFBO;
+        needUpdateDefaultFbo = true;
     }
 
     bool needReallocateRbo = false;
@@ -2325,19 +2326,33 @@
     if (needReallocateRbo) {
         initEmulatedEGLSurface(width, height, colorFormat, depthstencilFormat, multisamples,
                                 *eglSurfaceRBColorId, *eglSurfaceRBDepthId);
+        needUpdateDefaultFbo = true;
     }
 
     if (needReallocateReadRbo) {
         initEmulatedEGLSurface(readWidth, readHeight, readColorFormat, readDepthStencilFormat, readMultisamples,
                                 *eglReadSurfaceRBColorId, *eglReadSurfaceRBDepthId);
+        needUpdateDefaultFbo = true;
     }
 
+    needUpdateDefaultFbo |=
+        m_defaultFboRBColor != *eglSurfaceRBColorId || m_defaultFboRBDepth != *eglSurfaceRBDepthId;
+    needUpdateDefaultFbo |=
+        separateReadRbo && (m_defaultReadFboRBColor != *eglReadSurfaceRBColorId ||
+                            m_defaultReadFboRBDepth != *eglReadSurfaceRBDepthId);
+
+    if (!needUpdateDefaultFbo) {
+        return;
+    }
     dispatcher().glBindFramebuffer(GL_FRAMEBUFFER, m_defaultFBO);
 
     dispatcher().glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, *eglSurfaceRBColorId);
     dispatcher().glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, *eglSurfaceRBDepthId);
     dispatcher().glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, *eglSurfaceRBDepthId);
 
+    m_defaultFboRBColor = *eglSurfaceRBColorId;
+    m_defaultFboRBDepth = *eglSurfaceRBDepthId;
+
     if (m_defaultFBODrawBuffer != GL_COLOR_ATTACHMENT0) {
         dispatcher().glDrawBuffers(1, &m_defaultFBODrawBuffer);
     }
@@ -2350,6 +2365,8 @@
         dispatcher().glFramebufferRenderbuffer(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, *eglReadSurfaceRBColorId);
         dispatcher().glFramebufferRenderbuffer(GL_READ_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, *eglReadSurfaceRBDepthId);
         dispatcher().glFramebufferRenderbuffer(GL_READ_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, *eglReadSurfaceRBDepthId);
+        m_defaultReadFboRBColor = *eglReadSurfaceRBColorId;
+        m_defaultReadFboRBDepth = *eglReadSurfaceRBDepthId;
     }
 
     dispatcher().glBindRenderbuffer(GL_RENDERBUFFER, prevRbo);
diff --git a/host/gl/glestranslator/include/GLcommon/GLEScontext.h b/host/gl/glestranslator/include/GLcommon/GLEScontext.h
index 25a3a7b..7c31ba6 100644
--- a/host/gl/glestranslator/include/GLcommon/GLEScontext.h
+++ b/host/gl/glestranslator/include/GLcommon/GLEScontext.h
@@ -636,8 +636,10 @@
     // Default FBO per-context state
     GLuint m_defaultFBO = 0;
     GLuint m_defaultReadFBO = 0;
-    GLuint m_defaultRBColor = 0;
-    GLuint m_defaultRBDepth = 0;
+    GLuint m_defaultFboRBColor = 0;
+    GLuint m_defaultFboRBDepth = 0;
+    GLuint m_defaultReadFboRBColor = 0;
+    GLuint m_defaultReadFboRBDepth = 0;
     GLint m_defaultFBOWidth = 0;
     GLint m_defaultFBOHeight = 0;
     GLint m_defaultFBOColorFormat = 0;