Add captureLayersSync function

This captureLayers function in SurfaceFlinger will wait on the
requested binder thread for the screenshot composition to complete
before returning the buffer to the client. This is different than the
existing captureLayers method since the other request is oneway and
invokes the screen capture callback on a different binder thread.

This is needed because there are places in system server that request
screenshots while holding a lock and then wait synchronously on the
results. While waiting on the buffer and holding the lock, additional
two way binder calls can be made into system server that are waiting to
acquire the same lock. If there are enough requests, we may run out of
binder threads and then the screen capture result can't be posted back
to system server because it needs a free binder thread. This will result
in a deadlock because the lock that the screenshot request is holding
can never be unlocked without a free binder thread. Binder threads will
never be freed up because they are waiting to acquire the lock.

The async screencapture code is still useful for cases where there's no
global lock being held while waiting on results or the results is posted
to another thread.

Test: Screenshots
Bug: 321263247
Change-Id: I259173a59f488995e13af8f7dd2ca98c3bbf8639
diff --git a/libs/gui/SurfaceComposerClient.cpp b/libs/gui/SurfaceComposerClient.cpp
index 8d18551..83c2b7f 100644
--- a/libs/gui/SurfaceComposerClient.cpp
+++ b/libs/gui/SurfaceComposerClient.cpp
@@ -56,6 +56,7 @@
 
 #include <android-base/thread_annotations.h>
 #include <gui/LayerStatePermissions.h>
+#include <gui/ScreenCaptureResults.h>
 #include <private/gui/ComposerService.h>
 #include <private/gui/ComposerServiceAIDL.h>
 
@@ -3138,11 +3139,19 @@
 }
 
 status_t ScreenshotClient::captureLayers(const LayerCaptureArgs& captureArgs,
-                                         const sp<IScreenCaptureListener>& captureListener) {
+                                         const sp<IScreenCaptureListener>& captureListener,
+                                         bool sync) {
     sp<gui::ISurfaceComposer> s(ComposerServiceAIDL::getComposerService());
     if (s == nullptr) return NO_INIT;
 
-    binder::Status status = s->captureLayers(captureArgs, captureListener);
+    binder::Status status;
+    if (sync) {
+        gui::ScreenCaptureResults captureResults;
+        status = s->captureLayersSync(captureArgs, &captureResults);
+        captureListener->onScreenCaptureCompleted(captureResults);
+    } else {
+        status = s->captureLayers(captureArgs, captureListener);
+    }
     return statusTFromBinderStatus(status);
 }