Remove from top layer list if re-parenting top layer.

If a layer is given a parent when it originally didn't have one,
remove the layer from the top layer list. This is to ensure the
layer isn't in duplicate places and that it can get properly
cleaned up.

Test: Transaction_test#ReparentFromNoParent successfully removes
layer when test completes.

Change-Id: I1f1c4ee386626c1ef2e3a2e50324ec336d234d5b
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index d81178c..f29d980 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -3224,7 +3224,11 @@
         // changed, we don't want this to cause any more work
     }
     if (what & layer_state_t::eReparent) {
+        bool hadParent = layer->hasParent();
         if (layer->reparent(s.parentHandleForChild)) {
+            if (!hadParent) {
+                mCurrentState.layersSortedByZ.remove(layer);
+            }
             flags |= eTransactionNeeded|eTraversalNeeded;
         }
     }