(Reland) Disable GL/EGL ops in snapshot when no GL emulation

Reland note: no changes from previous aosp/2385358.

Bug: b/233939967
Test: android build
Test: cmake build
Test: cvd start --gpu_mode=gfxstream
Test: gfxstream unit tests
Change-Id: I28014992e6bb90d3df21cd56d9c97bb12c133ba3
diff --git a/stream-servers/FrameBuffer.cpp b/stream-servers/FrameBuffer.cpp
index 2126e62..97175c2 100644
--- a/stream-servers/FrameBuffer.cpp
+++ b/stream-servers/FrameBuffer.cpp
@@ -2873,19 +2873,28 @@
     //     m_prevReadSurf
     //     m_prevDrawSurf
     AutoLock mutex(m_lock);
-    // set up a context because some snapshot commands try using GL
-    RecursiveScopedContextBind scopedBind(getPbufferSurfaceContextHelper());
-    // eglPreSaveContext labels all guest context textures to be saved
-    // (textures created by the host are not saved!)
-    // eglSaveAllImages labels all EGLImages (both host and guest) to be saved
-    // and save all labeled textures and EGLImages.
-    if (s_egl.eglPreSaveContext && s_egl.eglSaveAllImages) {
-        for (const auto& ctx : m_contexts) {
-            s_egl.eglPreSaveContext(getDisplay(), ctx.second->getEGLContext(),
-                    stream);
+
+    std::unique_ptr<RecursiveScopedContextBind> bind;
+    if (m_emulationGl) {
+        // Some snapshot commands try using GL.
+        bind = std::make_unique<RecursiveScopedContextBind>(getPbufferSurfaceContextHelper());
+        if (!bind->isOk()) {
+            ERR("Failed to make context current for saving snapshot.");
         }
-        s_egl.eglSaveAllImages(getDisplay(), stream, &textureSaver);
+
+        // eglPreSaveContext labels all guest context textures to be saved
+        // (textures created by the host are not saved!)
+        // eglSaveAllImages labels all EGLImages (both host and guest) to be saved
+        // and save all labeled textures and EGLImages.
+        if (s_egl.eglPreSaveContext && s_egl.eglSaveAllImages) {
+            for (const auto& ctx : m_contexts) {
+                s_egl.eglPreSaveContext(getDisplay(), ctx.second->getEGLContext(),
+                        stream);
+            }
+            s_egl.eglSaveAllImages(getDisplay(), stream, &textureSaver);
+        }
     }
+
     // Don't save subWindow's x/y/w/h here - those are related to the current
     // emulator UI state, not guest state that we're saving.
     stream->putBe32(m_framebufferWidth);
@@ -2952,19 +2961,21 @@
         goldfish_vk::VkDecoderGlobalState::get()->save(stream);
     }
 
-    if (s_egl.eglPostSaveContext) {
-        for (const auto& ctx : m_contexts) {
-            s_egl.eglPostSaveContext(getDisplay(), ctx.second->getEGLContext(),
-                    stream);
+    if (m_emulationGl) {
+        if (s_egl.eglPostSaveContext) {
+            for (const auto& ctx : m_contexts) {
+                s_egl.eglPostSaveContext(getDisplay(), ctx.second->getEGLContext(),
+                        stream);
+            }
+            // We need to run the post save step for m_eglContext
+            // to mark their texture handles dirty
+            if (getContext() != EGL_NO_CONTEXT) {
+                s_egl.eglPostSaveContext(getDisplay(), getContext(), stream);
+            }
         }
-        // We need to run the post save step for m_eglContext
-        // to mark their texture handles dirty
-        if (getContext() != EGL_NO_CONTEXT) {
-            s_egl.eglPostSaveContext(getDisplay(), getContext(), stream);
-        }
-    }
 
-    EmulatedEglFenceSync::onSave(stream);
+        EmulatedEglFenceSync::onSave(stream);
+    }
 }
 
 bool FrameBuffer::onLoad(Stream* stream,
@@ -2974,7 +2985,15 @@
     {
         sweepColorBuffersLocked();
 
-        RecursiveScopedContextBind scopedBind(getPbufferSurfaceContextHelper());
+        std::unique_ptr<RecursiveScopedContextBind> bind;
+        if (m_emulationGl) {
+            // Some snapshot commands try using GL.
+            bind = std::make_unique<RecursiveScopedContextBind>(getPbufferSurfaceContextHelper());
+            if (!bind->isOk()) {
+                ERR("Failed to make context current for loading snapshot.");
+            }
+        }
+
         bool cleanupComplete = false;
         {
             AutoLock colorBufferMapLock(m_colorBufferMapLock);
@@ -3064,8 +3083,10 @@
 #ifdef SNAPSHOT_PROFILE
         uint64_t texTime = android::base::getUnixTimeUs();
 #endif
-        if (s_egl.eglLoadAllImages) {
-            s_egl.eglLoadAllImages(getDisplay(), stream, &textureLoader);
+        if (m_emulationGl) {
+            if (s_egl.eglLoadAllImages) {
+                s_egl.eglLoadAllImages(getDisplay(), stream, &textureLoader);
+            }
         }
 #ifdef SNAPSHOT_PROFILE
         printf("Texture load time: %lld ms\n",
@@ -3154,14 +3175,24 @@
     loadProcOwnedCollection(stream, &m_procOwnedEmulatedEglImages);
     loadProcOwnedCollection(stream, &m_procOwnedEmulatedEglContexts);
 
-    if (s_egl.eglPostLoadAllImages) {
-        s_egl.eglPostLoadAllImages(getDisplay(), stream);
+    if (m_emulationGl) {
+        if (s_egl.eglPostLoadAllImages) {
+            s_egl.eglPostLoadAllImages(getDisplay(), stream);
+        }
     }
 
     registerTriggerWait();
 
     {
-        RecursiveScopedContextBind scopedBind(getPbufferSurfaceContextHelper());
+        std::unique_ptr<RecursiveScopedContextBind> bind;
+        if (m_emulationGl) {
+            // Some snapshot commands try using GL.
+            bind = std::make_unique<RecursiveScopedContextBind>(getPbufferSurfaceContextHelper());
+            if (!bind->isOk()) {
+                ERR("Failed to make context current for loading snapshot.");
+            }
+        }
+
         AutoLock colorBufferMapLock(m_colorBufferMapLock);
         for (auto& it : m_colorbuffers) {
             if (it.second.cb) {
@@ -3183,7 +3214,9 @@
 
     repost(false);
 
-    EmulatedEglFenceSync::onLoad(stream);
+    if (m_emulationGl) {
+        EmulatedEglFenceSync::onLoad(stream);
+    }
 
     return true;
     // TODO: restore memory management