[SurfaceFlinger] add minimum frame count for early gl offsets.

This is to avoid edge case behavior by rapidly switching in and out
of gl composition, causing poor vsync timelines.

Bug: 132997413
Test: systrace
Change-Id: I4665f34f7e027a7883cfb5e47e14f41d845d1298
diff --git a/services/surfaceflinger/Scheduler/VSyncModulator.h b/services/surfaceflinger/Scheduler/VSyncModulator.h
index 81a7864..72f9050 100644
--- a/services/surfaceflinger/Scheduler/VSyncModulator.h
+++ b/services/surfaceflinger/Scheduler/VSyncModulator.h
@@ -35,6 +35,11 @@
     // sending new transactions.
     const int MIN_EARLY_FRAME_COUNT_TRANSACTION = 2;
 
+    // Number of frames we'll keep the early gl phase offsets once they are activated.
+    // This acts as a low-pass filter to avoid scenarios where we rapidly
+    // switch in and out of gl composition.
+    const int MIN_EARLY_GL_FRAME_COUNT_TRANSACTION = 2;
+
 public:
     struct Offsets {
         nsecs_t sf;
@@ -130,10 +135,14 @@
             mRemainingEarlyFrameCount--;
             updateOffsetsNeeded = true;
         }
-        if (usedRenderEngine != mLastFrameUsedRenderEngine) {
-            mLastFrameUsedRenderEngine = usedRenderEngine;
+        if (usedRenderEngine) {
+            mRemainingRenderEngineUsageCount = MIN_EARLY_GL_FRAME_COUNT_TRANSACTION;
+            updateOffsetsNeeded = true;
+        } else if (mRemainingRenderEngineUsageCount > 0) {
+            mRemainingRenderEngineUsageCount--;
             updateOffsetsNeeded = true;
         }
+
         if (updateOffsetsNeeded) {
             updateOffsets();
         }
@@ -145,7 +154,7 @@
         if (mTransactionStart == Scheduler::TransactionStart::EARLY ||
             mRemainingEarlyFrameCount > 0 || mRefreshRateChangePending) {
             return mEarlyOffsets;
-        } else if (mLastFrameUsedRenderEngine) {
+        } else if (mRemainingRenderEngineUsageCount > 0) {
             return mEarlyGlOffsets;
         } else {
             return mLateOffsets;
@@ -195,9 +204,9 @@
 
     std::atomic<Scheduler::TransactionStart> mTransactionStart =
             Scheduler::TransactionStart::NORMAL;
-    std::atomic<bool> mLastFrameUsedRenderEngine = false;
     std::atomic<bool> mRefreshRateChangePending = false;
     std::atomic<int> mRemainingEarlyFrameCount = 0;
+    std::atomic<int> mRemainingRenderEngineUsageCount = 0;
 };
 
 } // namespace android