SurfaceFlinger: Hold lock while constructing BufferStateLayer

This mirrors the createBufferQueue layer function. We have a crash
cluster around here and I'm not able to think of a better explanation.
Still unsure exactly what would trigger the problem though, will
monitor if this stops the crashes.

Bug: 173538294
Test: Existing tests pass
Change-Id: Id3324ebdde6f4b83274abddba59bb8c2adc68a36
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index b8b2fbc..d116f1e 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -4036,7 +4036,14 @@
                                                 sp<Layer>* outLayer) {
     LayerCreationArgs args(this, client, std::move(name), w, h, flags, std::move(metadata));
     args.textureName = getNewTexture();
-    sp<BufferStateLayer> layer = getFactory().createBufferStateLayer(args);
+    sp<BufferStateLayer> layer;
+    {
+        // TODO (b/173538294): Investigate why we need mStateLock here and above in
+        // createBufferQueue layer. Is it the renderengine::Image?
+        Mutex::Autolock lock(mStateLock);
+        layer = getFactory().createBufferStateLayer(args);
+
+    }
     *handle = layer->getHandle();
     *outLayer = layer;