Revert "SurfaceFlinger: Share ownership of layers between State and Handle."

This reverts commit f478f9f70b544d9f93680d6734a1af45bbd7f509.

Reason for revert: Causes presubmit issues.

Change-Id: Ibe872baed863a8ee9b9da5722e1671d087ec3a9f
Bug: 118257858
Bug: 62536731
Bug: 111373437
Bug: 111297488
diff --git a/services/surfaceflinger/Layer.cpp b/services/surfaceflinger/Layer.cpp
index 9f1c662..8afd3b3 100644
--- a/services/surfaceflinger/Layer.cpp
+++ b/services/surfaceflinger/Layer.cpp
@@ -118,20 +118,13 @@
         c->detachLayer(this);
     }
 
-    mFrameTracker.logAndResetStats(mName);
-
-    // The remote sync points are cleared out when we are
-    // removed from current state.
-    Mutex::Autolock lock(mLocalSyncPointMutex);
+    for (auto& point : mRemoteSyncPoints) {
+        point->setTransactionApplied();
+    }
     for (auto& point : mLocalSyncPoints) {
         point->setFrameAvailable();
     }
-
-    abandon();
-
-    destroyAllHwcLayers();
-
-    mFlinger->onLayerDestroyed();
+    mFrameTracker.logAndResetStats(mName);
 }
 
 // ---------------------------------------------------------------------------
@@ -146,9 +139,10 @@
 void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
 
 void Layer::onRemovedFromCurrentState() {
-    mRemovedFromCurrentState = true;
-
     // the layer is removed from SF mCurrentState to mLayersPendingRemoval
+
+    mPendingRemoval = true;
+
     if (mCurrentState.zOrderRelativeOf != nullptr) {
         sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
         if (strongRelative != nullptr) {
@@ -158,21 +152,22 @@
         mCurrentState.zOrderRelativeOf = nullptr;
     }
 
-    // Since we are no longer reachable from CurrentState SurfaceFlinger
-    // will no longer invoke doTransaction for us, and so we will
-    // never finish applying transactions. We signal the sync point
-    // now so that another layer will not become indefinitely
-    // blocked.
-    for (auto& point: mRemoteSyncPoints) {
-        point->setTransactionApplied();
-    }
-    mRemoteSyncPoints.clear();
-
     for (const auto& child : mCurrentChildren) {
         child->onRemovedFromCurrentState();
     }
 }
 
+void Layer::onRemoved() {
+    // the layer is removed from SF mLayersPendingRemoval
+    abandon();
+
+    destroyAllHwcLayers();
+
+    for (const auto& child : mCurrentChildren) {
+        child->onRemoved();
+    }
+}
+
 // ---------------------------------------------------------------------------
 // set-up
 // ---------------------------------------------------------------------------
@@ -232,10 +227,6 @@
     }
     LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
                         "All hardware composer layers should have been destroyed");
-
-    for (const sp<Layer>& child : mDrawingChildren) {
-        child->destroyAllHwcLayers();
-    }
 }
 
 Rect Layer::getContentCrop() const {
@@ -792,9 +783,7 @@
 
     // If this transaction is waiting on the receipt of a frame, generate a sync
     // point and send it to the remote layer.
-    // We don't allow installing sync points after we are removed from the current state
-    // as we won't be able to signal our end.
-    if (mCurrentState.barrierLayer_legacy != nullptr && !mRemovedFromCurrentState) {
+    if (mCurrentState.barrierLayer_legacy != nullptr) {
         sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
         if (barrierLayer == nullptr) {
             ALOGE("[%s] Unable to promote barrier Layer.", mName.string());