SF: Defer setDisplayBrightness to Binder thread

setDisplayBrightness blocks for a long time so cannot run on the main
thread. Keep display lookup on the main thread, but return a deferred
future to the Binder thread and run it there.

Bug: 154202427
Bug: 123715322
Test: systrace with backdoor to set brightness
Change-Id: I88e60c95db5fdcaa1bf5ebb9711e3bf5ab6539a1
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index 2943499..794e8a7 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -108,6 +108,7 @@
 #include "LayerVector.h"
 #include "MonitoredProducer.h"
 #include "NativeWindowSurface.h"
+#include "Promise.h"
 #include "RefreshRateOverlay.h"
 #include "RegionSamplingThread.h"
 #include "Scheduler/DispSync.h"
@@ -1491,14 +1492,15 @@
         return BAD_VALUE;
     }
 
-    return schedule([=]() -> status_t {
+    return promise::chain(schedule([=] {
                if (const auto displayId = getPhysicalDisplayIdLocked(displayToken)) {
                    return getHwComposer().setDisplayBrightness(*displayId, brightness);
                } else {
                    ALOGE("%s: Invalid display token %p", __FUNCTION__, displayToken.get());
-                   return NAME_NOT_FOUND;
+                   return promise::yield<status_t>(NAME_NOT_FOUND);
                }
-           })
+           }))
+            .then([](std::future<status_t> task) { return task; })
             .get();
 }