DO NOT MERGE Extend mPreviousPresentFences for high refresh rate

To provide hardware enough time to display a frame under high refresh
rate with long sf-duration, we extend mPreviousPresentFences by storing
extra slots. We check proper present fence at different moment
according to vsync period and sf-duration.

Bug: 241193992
Change-Id: I618dd7abdb1f9ca2cb92623cc4d423389d62e402
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index 6a17cd8..e9fbf6e 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -1955,8 +1955,16 @@
     const auto now = systemTime();
     const auto vsyncPeriod = mScheduler->getDisplayStatInfo(now).vsyncPeriod;
     const bool expectedPresentTimeIsTheNextVsync = mExpectedPresentTime - now <= vsyncPeriod;
-    return expectedPresentTimeIsTheNextVsync ? mPreviousPresentFences[0]
-                                             : mPreviousPresentFences[1];
+
+    size_t shift = 0;
+    if (!expectedPresentTimeIsTheNextVsync) {
+        shift = static_cast<size_t>((mExpectedPresentTime - now) / vsyncPeriod);
+        if (shift >= mPreviousPresentFences.size()) {
+            shift = mPreviousPresentFences.size() - 1;
+        }
+    }
+    ATRACE_FORMAT("previousFrameFence shift=%zu", shift);
+    return mPreviousPresentFences[shift];
 }
 
 bool SurfaceFlinger::previousFramePending(int graceTimeMs) {
@@ -2425,7 +2433,10 @@
         glCompositionDoneFenceTime = FenceTime::NO_FENCE;
     }
 
-    mPreviousPresentFences[1] = mPreviousPresentFences[0];
+    for (size_t i = mPreviousPresentFences.size()-1; i >= 1; i--) {
+        mPreviousPresentFences[i] = mPreviousPresentFences[i-1];
+    }
+
     mPreviousPresentFences[0].fence =
             display ? getHwComposer().getPresentFence(display->getPhysicalId()) : Fence::NO_FENCE;
     mPreviousPresentFences[0].fenceTime =