Prevent buggy display orientation when exiting vr

The underlying issue is that the viewport settings were being re-set to
(0,0,-1,-1) on every transition to VR. I've instead extracted the vsync
period updates, and only apply those, as that was really all we wanted
out of it.

Bug: 37093920
Test: Compile and run Vr application. The 2d scene is now no longer in
the weird landscape viewport with portrait orientation if the phone is
in landscape when exiting VR mode.

Change-Id: I2bab6e4d834e15f477d1b56991375d1a18a77b8c
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index 2a43910..7a17889 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -1251,7 +1251,6 @@
     // transition.
     mDrawingState.displays.clear();
     mDisplays.clear();
-    initializeDisplays();
 }
 
 void SurfaceFlinger::updateVrFlinger() {
@@ -1298,6 +1297,12 @@
     // parts of this class rely on the primary display always being available.
     createDefaultDisplayDevice();
 
+    // Reset the timing values to account for the period of the swapped in HWC
+    const auto& activeConfig = mHwc->getActiveConfig(HWC_DISPLAY_PRIMARY);
+    const nsecs_t period = activeConfig->getVsyncPeriod();
+    mAnimFrameTracker.setDisplayRefreshPeriod(period);
+    setCompositorTimingSnapped(0, period, 0);
+
     android_atomic_or(1, &mRepaintEverything);
     setTransactionFlags(eDisplayTransactionNeeded);
 }