Use new HAL API to clear buffer slots

Bug: 262041682
Test: VtsHalGraphicsComposer3_TargetTest
Change-Id: I08aa7e0f3831c37c600d9aa9dffb33acb8661347
diff --git a/services/surfaceflinger/DisplayHardware/AidlComposerHal.cpp b/services/surfaceflinger/DisplayHardware/AidlComposerHal.cpp
index d061a99..e372b72 100644
--- a/services/surfaceflinger/DisplayHardware/AidlComposerHal.cpp
+++ b/services/surfaceflinger/DisplayHardware/AidlComposerHal.cpp
@@ -233,15 +233,23 @@
 
     addReader(translate<Display>(kSingleReaderKey));
 
-    // TODO(b/262041682): When using new API to clear buffer slots, don't allocate this buffer.
-    mClearSlotBuffer = sp<GraphicBuffer>::make(1, 1, PIXEL_FORMAT_RGBX_8888,
-                                               GraphicBuffer::USAGE_HW_COMPOSER |
-                                                       GraphicBuffer::USAGE_SW_READ_OFTEN |
-                                                       GraphicBuffer::USAGE_SW_WRITE_OFTEN,
-                                               "AidlComposer");
-    if (!mClearSlotBuffer || mClearSlotBuffer->initCheck() != ::android::OK) {
-        LOG_ALWAYS_FATAL("Failed to allocate a buffer for clearing layer buffer slots");
-        return;
+    // If unable to read interface version, then become backwards compatible.
+    int32_t version = 1;
+    const auto status = mAidlComposerClient->getInterfaceVersion(&version);
+    if (!status.isOk()) {
+        ALOGE("getInterfaceVersion for AidlComposer constructor failed %s",
+              status.getDescription().c_str());
+    }
+    if (version == 1) {
+        mClearSlotBuffer = sp<GraphicBuffer>::make(1, 1, PIXEL_FORMAT_RGBX_8888,
+                                                   GraphicBuffer::USAGE_HW_COMPOSER |
+                                                           GraphicBuffer::USAGE_SW_READ_OFTEN |
+                                                           GraphicBuffer::USAGE_SW_WRITE_OFTEN,
+                                                   "AidlComposer");
+        if (!mClearSlotBuffer || mClearSlotBuffer->initCheck() != ::android::OK) {
+            LOG_ALWAYS_FATAL("Failed to allocate a buffer for clearing layer buffer slots");
+            return;
+        }
     }
 
     ALOGI("Loaded AIDL composer3 HAL service");
@@ -835,25 +843,32 @@
     Error error = Error::NONE;
     mMutex.lock_shared();
     if (auto writer = getWriter(display)) {
-        // Backwards compatible way of clearing buffer slots is tricky...
-        for (uint32_t slot : slotsToClear) {
-            // Don't clear the active buffer slot because we need to restore the active buffer
-            // after clearing the requested buffer slots with a placeholder buffer.
-            if (slot != activeBufferSlot) {
-                writer->get().setLayerBufferWithNewCommand(translate<int64_t>(display),
-                                                           translate<int64_t>(layer), slot,
-                                                           mClearSlotBuffer->handle, /*fence*/ -1);
+        // Backwards compatible way of clearing buffer is to set the layer buffer with a placeholder
+        // buffer, using the slot that needs to cleared... tricky.
+        if (mClearSlotBuffer == nullptr) {
+            writer->get().setLayerBufferSlotsToClear(translate<int64_t>(display),
+                                                     translate<int64_t>(layer), slotsToClear);
+        } else {
+            for (uint32_t slot : slotsToClear) {
+                // Don't clear the active buffer slot because we need to restore the active buffer
+                // after clearing the requested buffer slots with a placeholder buffer.
+                if (slot != activeBufferSlot) {
+                    writer->get().setLayerBufferWithNewCommand(translate<int64_t>(display),
+                                                               translate<int64_t>(layer), slot,
+                                                               mClearSlotBuffer->handle,
+                                                               /*fence*/ -1);
+                }
             }
+            // Since we clear buffers by setting them to a placeholder buffer, we want to make
+            // sure that the last setLayerBuffer command is sent with the currently active
+            // buffer, not the placeholder buffer, so that there is no perceptual change when
+            // buffers are discarded.
+            writer->get().setLayerBufferWithNewCommand(translate<int64_t>(display),
+                                                       translate<int64_t>(layer), activeBufferSlot,
+                                                       // The active buffer is still cached in
+                                                       // its slot and doesn't need a fence.
+                                                       /*buffer*/ nullptr, /*fence*/ -1);
         }
-        // Since we clear buffers by setting them to a placeholder buffer, we want to make
-        // sure that the last setLayerBuffer command is sent with the currently active
-        // buffer, not the placeholder buffer, so that there is no perceptual change when
-        // buffers are discarded.
-        writer->get().setLayerBufferWithNewCommand(translate<int64_t>(display),
-                                                   translate<int64_t>(layer), activeBufferSlot,
-                                                   // The active buffer is still cached in
-                                                   // its slot and doesn't need a fence.
-                                                   /*buffer*/ nullptr, /*fence*/ -1);
     } else {
         error = Error::BAD_DISPLAY;
     }