Remove root layer when clearing mLayersPendingRemoval

The previous code removed the root layer from layersSortedByZ when the
Layer handle was destroyed. However this causes issues if the Layer is
created and then dropped quickly on the client side. What happened was
the following:

1. Client creates a Layer
2. SF creates the Layer, but doesn't add it to the hierarchy yet
3. Client drops the Layer reference
4. LayerCleaner dtor is called which calls SF#onHandleDestroyed. This
   removed the Layer from layersSortedByZ and added it to mLayersPendingRemoval
5. SF main thread runs and adds the Layer to the hierarchy. So for the
   case of root, it adds it to layersSortedByZ
6. mLayersPendingRemoval is traversed and cleared, but since layersSortedByZ still
   has a reference to the Layer, it's never destroyed.

With this change, the Layer will be created, but then removed from layersSortedByZ
when mLayersPendingRemoval is traversed. That way the Layer reference
can be dropped.

Test: SurfaceFlingerStress#create_and_destroy
Fixes: 229754763
Change-Id: I0537781d6e13fbdeb2502ed6a7e65dbcfd7b28ce
1 file changed