[RenderEngine] add an ImageManager thread

Prior to this change, EGLImage management would be performed by both
binder threads and the main rendering thread in a synchronized manner.
But because of BufferQueue implementation details, it's possible for an
app's Surface to be disconnected (destroying all backing EGLImages per
the disconnect api spec), while still latching and presenting the most
recently queued image (which requires recreating the EGLImage on the
main thread), which can cause jank in some scenarios such as app-launch.

To mitigate this, defer EGLImage creation and destruction to a separate
thread behind RenderEngine so that creating the EGLImage should never be
done on the main thread. So scenarios such as app-launch avoid jank by
performing the EGLImage creation asynchronously with the main thread, so
that the creation is done by the time RenderEngine needs to start
rendering.

Bug: 136806342
Bug: 137191934
Test: Launching photos app shows no bindExternalImage* calls
during the rendering path
Test: librenderengine_test
Change-Id: Ib809e36267b5604bbbedd4089d15666b22cabc95
diff --git a/services/surfaceflinger/BufferLayerConsumer.cpp b/services/surfaceflinger/BufferLayerConsumer.cpp
index bd9bd81..414814a 100644
--- a/services/surfaceflinger/BufferLayerConsumer.cpp
+++ b/services/surfaceflinger/BufferLayerConsumer.cpp
@@ -494,7 +494,6 @@
         if (oldImage == nullptr || oldImage->graphicBuffer() == nullptr ||
             oldImage->graphicBuffer()->getId() != item.mGraphicBuffer->getId()) {
             mImages[item.mSlot] = std::make_shared<Image>(item.mGraphicBuffer, mRE);
-            mRE.cacheExternalTextureBuffer(item.mGraphicBuffer);
         }
     }
 }
@@ -531,6 +530,12 @@
     ConsumerBase::dumpLocked(result, prefix);
 }
 
+BufferLayerConsumer::Image::Image(const sp<GraphicBuffer>& graphicBuffer,
+                                  renderengine::RenderEngine& engine)
+      : mGraphicBuffer(graphicBuffer), mRE(engine) {
+    mRE.cacheExternalTextureBuffer(mGraphicBuffer);
+}
+
 BufferLayerConsumer::Image::~Image() {
     if (mGraphicBuffer != nullptr) {
         ALOGV("Destroying buffer: %" PRId64, mGraphicBuffer->getId());