Fix frozen screen after exiting vr

When a hotplug connected event occurs, don't recreate any
already-existing entries in mBuiltinDisplays. This fixes a problem where
the screen would freeze after exiting vr.

In the future, we may want to use hotplug connected events to notify
surface flinger to reinitialize display state. This CL includes a change
to the code in HWC2.cpp to always reinitialize the display state when we
get a hotplug connected event, even if the display is already connected.

Bug: 74985350

Test: - Confirmed exiting vr no longer freezes the screen.

- Added code to simulate a hotplug connected event while the device is
  running, to test the changes in HWC2.cpp. Confirmed the device
  continued to function normally.

Change-Id: I6afda67cae84842b2568c773e6b5aa4f38df6a96
Merged-In: I6afda67cae84842b2568c773e6b5aa4f38df6a96
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index 11658e8..7e02922 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -2120,16 +2120,16 @@
         getBE().mHwc->onHotplug(event.display, displayType, event.connection);
 
         if (event.connection == HWC2::Connection::Connected) {
-            ALOGV("Creating built in display %d", displayType);
-            ALOGW_IF(mBuiltinDisplays[displayType],
-                    "Overwriting display token for display type %d", displayType);
-            mBuiltinDisplays[displayType] = new BBinder();
-            // All non-virtual displays are currently considered secure.
-            DisplayDeviceState info(displayType, true);
-            info.displayName = displayType == DisplayDevice::DISPLAY_PRIMARY ?
-                    "Built-in Screen" : "External Screen";
-            mCurrentState.displays.add(mBuiltinDisplays[displayType], info);
-            mInterceptor.saveDisplayCreation(info);
+            if (!mBuiltinDisplays[displayType].get()) {
+                ALOGV("Creating built in display %d", displayType);
+                mBuiltinDisplays[displayType] = new BBinder();
+                // All non-virtual displays are currently considered secure.
+                DisplayDeviceState info(displayType, true);
+                info.displayName = displayType == DisplayDevice::DISPLAY_PRIMARY ?
+                        "Built-in Screen" : "External Screen";
+                mCurrentState.displays.add(mBuiltinDisplays[displayType], info);
+                mInterceptor.saveDisplayCreation(info);
+            }
         } else {
             ALOGV("Removing built in display %d", displayType);