Fulfill hanging promises upon LayerFE destruction

Attempted screenshots can have broken promises when there is an
invalid render area and no memory for the capture result. This
case would return without a capture result, but should also
clean up any promises associated with the LayerFEs in that
screenshot.

Fixes: b/330811287
Test: presubmit
Change-Id: I777aaff0096347332eb6ba731a8a74b4e1860a57
diff --git a/services/surfaceflinger/LayerFE.cpp b/services/surfaceflinger/LayerFE.cpp
index 620edca..c2251a8 100644
--- a/services/surfaceflinger/LayerFE.cpp
+++ b/services/surfaceflinger/LayerFE.cpp
@@ -27,6 +27,7 @@
 
 #include "LayerFE.h"
 #include "SurfaceFlinger.h"
+#include "common/FlagManager.h"
 #include "ui/FenceResult.h"
 #include "ui/LayerStack.h"
 
@@ -80,6 +81,16 @@
 
 LayerFE::LayerFE(const std::string& name) : mName(name) {}
 
+LayerFE::~LayerFE() {
+    // Ensures that no promise is left unfulfilled before the LayerFE is destroyed.
+    // An unfulfilled promise could occur when a screenshot is attempted, but the
+    // render area is invalid and there is no memory for the capture result.
+    if (FlagManager::getInstance().ce_fence_promise() &&
+        mReleaseFencePromiseStatus == ReleaseFencePromiseStatus::INITIALIZED) {
+        setReleaseFence(Fence::NO_FENCE);
+    }
+}
+
 const compositionengine::LayerFECompositionState* LayerFE::getCompositionState() const {
     return mSnapshot.get();
 }