SF: increase the number of buffers for SurfaceView

3 buffers are not sufficient for most apps to render at high refresh
rates. This change increases the maxAcquiredBufferCount on the buffer
queue for all surfaces, and removes the hwui-specific extra buffer
allocations. All Surfaces connected to SurfaceFlinger will have now
enough buffers to account for configured
'debug.sf.late.app.duration' value.

Test: Run backpressure based game on 60Hz and 120Hz and collect
traces
Bug: 188553729

Change-Id: Ib8fda5db3f48c3bfc6fbf07167b4313674512cee
diff --git a/libs/gui/BLASTBufferQueue.cpp b/libs/gui/BLASTBufferQueue.cpp
index b9a293f..8024482 100644
--- a/libs/gui/BLASTBufferQueue.cpp
+++ b/libs/gui/BLASTBufferQueue.cpp
@@ -29,9 +29,10 @@
 #include <gui/IProducerListener.h>
 #include <gui/Surface.h>
 #include <utils/Singleton.h>
-
 #include <utils/Trace.h>
 
+#include <private/gui/ComposerService.h>
+
 #include <chrono>
 
 using namespace std::chrono_literals;
@@ -158,6 +159,11 @@
     mBufferItemConsumer->setDefaultBufferFormat(convertBufferFormat(format));
     mBufferItemConsumer->setBlastBufferQueue(this);
 
+    int extraBufferCount = 0;
+    ComposerService::getComposerService()->getExtraBufferCount(&extraBufferCount);
+    mMaxAcquiredBuffers = 1 + extraBufferCount;
+    mBufferItemConsumer->setMaxAcquiredBufferCount(mMaxAcquiredBuffers);
+
     mTransformHint = mSurfaceControl->getTransformHint();
     mBufferItemConsumer->setTransformHint(mTransformHint);
     SurfaceComposerClient::Transaction()
@@ -567,7 +573,7 @@
 // includeExtraAcquire is true to include this buffer to the count. Since this depends on the state
 // of the buffer, the next acquire may return with NO_BUFFER_AVAILABLE.
 bool BLASTBufferQueue::maxBuffersAcquired(bool includeExtraAcquire) const {
-    int maxAcquiredBuffers = MAX_ACQUIRED_BUFFERS + (includeExtraAcquire ? 2 : 1);
+    int maxAcquiredBuffers = mMaxAcquiredBuffers + (includeExtraAcquire ? 2 : 1);
     return mNumAcquired == maxAcquiredBuffers;
 }
 
diff --git a/libs/gui/Surface.cpp b/libs/gui/Surface.cpp
index e117d11..821ec16 100644
--- a/libs/gui/Surface.cpp
+++ b/libs/gui/Surface.cpp
@@ -1501,9 +1501,6 @@
     case NATIVE_WINDOW_SET_FRAME_TIMELINE_INFO:
         res = dispatchSetFrameTimelineInfo(args);
         break;
-    case NATIVE_WINDOW_GET_EXTRA_BUFFER_COUNT:
-        res = dispatchGetExtraBufferCount(args);
-        break;
     default:
         res = NAME_NOT_FOUND;
         break;
@@ -1853,14 +1850,6 @@
     return setFrameTimelineInfo({frameTimelineVsyncId, inputEventId});
 }
 
-int Surface::dispatchGetExtraBufferCount(va_list args) {
-    ATRACE_CALL();
-    auto extraBuffers = static_cast<int*>(va_arg(args, int*));
-
-    ALOGV("Surface::dispatchGetExtraBufferCount");
-    return getExtraBufferCount(extraBuffers);
-}
-
 bool Surface::transformToDisplayInverse() const {
     return (mTransform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) ==
             NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY;
@@ -2632,8 +2621,4 @@
     return composerService()->setFrameTimelineInfo(mGraphicBufferProducer, frameTimelineInfo);
 }
 
-status_t Surface::getExtraBufferCount(int* extraBuffers) const {
-    return composerService()->getExtraBufferCount(extraBuffers);
-}
-
 }; // namespace android
diff --git a/libs/gui/SurfaceControl.cpp b/libs/gui/SurfaceControl.cpp
index d7c07b9..6529a4e 100644
--- a/libs/gui/SurfaceControl.cpp
+++ b/libs/gui/SurfaceControl.cpp
@@ -139,7 +139,7 @@
                                  ISurfaceComposerClient::eOpaque);
     mBbqChild = mClient->createSurface(String8("bbq-wrapper"), 0, 0, mFormat,
                                        flags, mHandle, {}, &ignore);
-    mBbq = new BLASTBufferQueue("bbq-adapter", mBbqChild, mWidth, mHeight, mFormat);
+    mBbq = sp<BLASTBufferQueue>::make("bbq-adapter", mBbqChild, mWidth, mHeight, mFormat);
 
     // This surface is always consumed by SurfaceFlinger, so the
     // producerControlledByApp value doesn't matter; using false.
diff --git a/libs/gui/include/gui/BLASTBufferQueue.h b/libs/gui/include/gui/BLASTBufferQueue.h
index 3ab1ee1..d63425e 100644
--- a/libs/gui/include/gui/BLASTBufferQueue.h
+++ b/libs/gui/include/gui/BLASTBufferQueue.h
@@ -132,7 +132,7 @@
 
     // BufferQueue internally allows 1 more than
     // the max to be acquired
-    static const int MAX_ACQUIRED_BUFFERS = 1;
+    int32_t mMaxAcquiredBuffers = 1;
 
     int32_t mNumFrameAvailable GUARDED_BY(mMutex);
     int32_t mNumAcquired GUARDED_BY(mMutex);
diff --git a/libs/gui/include/gui/Surface.h b/libs/gui/include/gui/Surface.h
index 48885eb..7e4143b 100644
--- a/libs/gui/include/gui/Surface.h
+++ b/libs/gui/include/gui/Surface.h
@@ -190,7 +190,6 @@
     virtual status_t setFrameRate(float frameRate, int8_t compatibility,
                                   int8_t changeFrameRateStrategy);
     virtual status_t setFrameTimelineInfo(const FrameTimelineInfo& info);
-    virtual status_t getExtraBufferCount(int* extraBuffers) const;
 
 protected:
     virtual ~Surface();
@@ -278,7 +277,6 @@
     int dispatchGetLastQueuedBuffer(va_list args);
     int dispatchGetLastQueuedBuffer2(va_list args);
     int dispatchSetFrameTimelineInfo(va_list args);
-    int dispatchGetExtraBufferCount(va_list args);
 
 protected:
     virtual int dequeueBuffer(ANativeWindowBuffer** buffer, int* fenceFd);
diff --git a/libs/gui/tests/BLASTBufferQueue_test.cpp b/libs/gui/tests/BLASTBufferQueue_test.cpp
index 5a5da97..06660b8 100644
--- a/libs/gui/tests/BLASTBufferQueue_test.cpp
+++ b/libs/gui/tests/BLASTBufferQueue_test.cpp
@@ -394,7 +394,11 @@
     setUpProducer(adapter, igbProducer);
 
     std::vector<std::pair<int, sp<Fence>>> allocated;
-    for (int i = 0; i < 3; i++) {
+    int minUndequeuedBuffers = 0;
+    ASSERT_EQ(OK, igbProducer->query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers));
+    const auto bufferCount = minUndequeuedBuffers + 2;
+
+    for (int i = 0; i < bufferCount; i++) {
         int slot;
         sp<Fence> fence;
         sp<GraphicBuffer> buf;