Release virtual display hwcId during vr mode transition.

When we recreate the display state during VR mode transition, we were not
previously releasing hwcId with vr hardware composer. That resulted in VR
using the only VirtualDisplay provided by vr_hwc during the first run.
Every subsequent run failed to allocate a new hwcId id.

This manifested itself when DON (device on) flow quickly kicked the device
into and out of VR mode. Normally the binding is cleaned up when java
frameworks destroy the VD upon exiting VR mode, but it does so with a
short delay to account for rapid in/out transition, during which the
hwcId was getting left behind.

Bug: 34281731
Test: Run VR app, launch 2D app in VirtualDisplay, see it on the screen.
Close VR app, exit to 2D mode, repeat and observe 2D app in VR again.

Change-Id: I8faabdcd3516190ad88d1361c468f7f472ead588
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index 76a5d06..4aaff6f 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -1236,6 +1236,13 @@
     // mCurrentState and mDrawingState and re-apply all changes when we make the
     // transition.
     mDrawingState.displays.clear();
+    // Release virtual display hwcId during vr mode transition.
+    for (size_t displayId = 0; displayId < mDisplays.size(); ++displayId) {
+        const sp<DisplayDevice>& displayDevice = mDisplays[displayId];
+        if (displayDevice->getDisplayType() == DisplayDevice::DISPLAY_VIRTUAL) {
+            displayDevice->disconnect(getHwComposer());
+        }
+    }
     mDisplays.clear();
     initializeDisplays();
 }