SF: Add GL texture pool

Adds a pool of GL texture names.

Prior to this change, Layer creation was forced to run on the SF main
thread because it would need to call into RenderEngine to generate a new
texture name.

By creating a pool of pre-generated texture names, this operation no
longer needs to run on the main thread, which unblocks the rest of the
system during operations such as fingerprint unlock.

Bug: 110477323
Test: SurfaceFlinger_test + manual: examine systrace and observe that
      layer creation no longer blocks on access to the main thread

Change-Id: I9d68874d6c6f704c8884676454e84d916cd86507
Merged-In: I9d68874d6c6f704c8884676454e84d916cd86507
diff --git a/services/surfaceflinger/BufferLayer.cpp b/services/surfaceflinger/BufferLayer.cpp
index 6feec53..4b332bb 100644
--- a/services/surfaceflinger/BufferLayer.cpp
+++ b/services/surfaceflinger/BufferLayer.cpp
@@ -63,7 +63,7 @@
         mRefreshPending(false) {
     ALOGV("Creating Layer %s", name.string());
 
-    mFlinger->getRenderEngine().genTextures(1, &mTextureName);
+    mTextureName = mFlinger->getNewTexture();
     mTexture.init(Texture::TEXTURE_EXTERNAL, mTextureName);
 
     if (flags & ISurfaceComposerClient::eNonPremultiplied) mPremultipliedAlpha = false;
@@ -706,8 +706,12 @@
     sp<IGraphicBufferConsumer> consumer;
     BufferQueue::createBufferQueue(&producer, &consumer, true);
     mProducer = new MonitoredProducer(producer, mFlinger, this);
-    mConsumer = new BufferLayerConsumer(consumer,
-            mFlinger->getRenderEngine(), mTextureName, this);
+    {
+        // Grab the SF state lock during this since it's the only safe way to access RenderEngine
+        Mutex::Autolock lock(mFlinger->mStateLock);
+        mConsumer = new BufferLayerConsumer(consumer, mFlinger->getRenderEngine(), mTextureName,
+                                            this);
+    }
     mConsumer->setConsumerUsageBits(getEffectiveUsage(0));
     mConsumer->setContentsChangedListener(this);
     mConsumer->setName(mName);