Fix concurrent writes to the hardware composer

Stale hardware composer layers were causing concurrent writes to the
Composer object from the surface flinger and vr flinger threads, a big
no-no. The concurrent writes would sometimes stomp on each other,
causing the hardware composer service to fail to read the command buffer
containing surface flinger's composer commands, leading to all sorts of
issues.

Bug: 62925812

Test: Locally added logs to surface flinger to catch the concurrent
writes, and confirmed the logs are no longer present with this patch
applied. Went through a bunch of sleep/wake cycles and confirmed the
device continues to function normally.

Change-Id: I70929c4a3c71142f5e9083cac294c122d127aa27
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index c10493b..8a9b2ed 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -1253,17 +1253,14 @@
             enabled ? HWC2::Vsync::Enable : HWC2::Vsync::Disable);
 }
 
-void SurfaceFlinger::clearHwcLayers(const LayerVector& layers) {
-    for (size_t i = 0; i < layers.size(); ++i) {
-        layers[i]->clearHwcLayers();
-    }
-}
-
 // Note: it is assumed the caller holds |mStateLock| when this is called
 void SurfaceFlinger::resetHwcLocked() {
     disableHardwareVsync(true);
     clearHwcLayers(mDrawingState.layersSortedByZ);
     clearHwcLayers(mCurrentState.layersSortedByZ);
+    for (size_t disp = 0; disp < mDisplays.size(); ++disp) {
+        clearHwcLayers(mDisplays[disp]->getVisibleLayersSortedByZ());
+    }
     // Clear the drawing state so that the logic inside of
     // handleTransactionLocked will fire. It will determine the delta between
     // mCurrentState and mDrawingState and re-apply all changes when we make the
diff --git a/services/surfaceflinger/SurfaceFlinger.h b/services/surfaceflinger/SurfaceFlinger.h
index 2360a61..dc0fb58 100644
--- a/services/surfaceflinger/SurfaceFlinger.h
+++ b/services/surfaceflinger/SurfaceFlinger.h
@@ -577,7 +577,12 @@
     /* ------------------------------------------------------------------------
      * VrFlinger
      */
-    void clearHwcLayers(const LayerVector& layers);
+    template<typename T>
+    void clearHwcLayers(const T& layers) {
+        for (size_t i = 0; i < layers.size(); ++i) {
+            layers[i]->clearHwcLayers();
+        }
+    }
     void resetHwcLocked();
 
     // Check to see if we should handoff to vr flinger.