Track the last release fence per layer stack

Merging fences prior to calling CompositionEngine#present results in
occassional missed frames in Surfaceflinger. Merging fences here was
originally needed to relieve memory pressure. Instead, we can track
each layer stack's future release fence with a map between a layer
stack and its last fence, as it can be assumed that multiple fences
for the same layer stack can be be dropped.

Fixes: b/330841053
Test: SurfaceFlinger_test
Test: presubmit
Change-Id: I7ca3a226ff77bc31d93fdb4708c3e9089f423803
diff --git a/services/surfaceflinger/Layer.h b/services/surfaceflinger/Layer.h
index d283d6f..fc39660 100644
--- a/services/surfaceflinger/Layer.h
+++ b/services/surfaceflinger/Layer.h
@@ -18,6 +18,7 @@
 
 #include <android/gui/DropInputMode.h>
 #include <android/gui/ISurfaceComposerClient.h>
+#include <ftl/small_map.h>
 #include <gui/BufferQueue.h>
 #include <gui/LayerState.h>
 #include <gui/WindowInfo.h>
@@ -25,9 +26,11 @@
 #include <math/vec4.h>
 #include <sys/types.h>
 #include <ui/BlurRegion.h>
+#include <ui/DisplayMap.h>
 #include <ui/FloatRect.h>
 #include <ui/FrameStats.h>
 #include <ui/GraphicBuffer.h>
+#include <ui/LayerStack.h>
 #include <ui/PixelFormat.h>
 #include <ui/Region.h>
 #include <ui/StretchEffect.h>
@@ -958,8 +961,11 @@
     // screenshots asynchronously. There may be no buffer update for the
     // layer, but the layer will still be composited on the screen in every
     // frame. Kepping track of these fences ensures that they are not dropped
-    // and can be dispatched to the client at a later time.
-    std::vector<ftl::Future<FenceResult>> mAdditionalPreviousReleaseFences;
+    // and can be dispatched to the client at a later time. Older fences are
+    // dropped when a layer stack receives a new fence.
+    // TODO(b/300533018): Track fence per multi-instance RenderEngine
+    ftl::SmallMap<ui::LayerStack, ftl::Future<FenceResult>, ui::kDisplayCapacity>
+            mAdditionalPreviousReleaseFences;
 
     // Exposed so SurfaceFlinger can assert that it's held
     const sp<SurfaceFlinger> mFlinger;