AIDL BufferPool implementation (HIDL -> AIDL)

Bug: 254050250
Test: m
Change-Id: I0d7bae2c01bd480d1e99f4b39c4a9013a2828897
diff --git a/media/bufferpool/aidl/default/BufferStatus.cpp b/media/bufferpool/aidl/default/BufferStatus.cpp
index 6937260..19caa1e 100644
--- a/media/bufferpool/aidl/default/BufferStatus.cpp
+++ b/media/bufferpool/aidl/default/BufferStatus.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2018 The Android Open Source Project
+ * Copyright (C) 2022 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -14,29 +14,17 @@
  * limitations under the License.
  */
 
-#define LOG_TAG "BufferPoolStatus"
+#define LOG_TAG "AidlBufferPoolStatus"
 //#define LOG_NDEBUG 0
 
 #include <thread>
 #include <time.h>
+#include <aidl/android/hardware/media/bufferpool2/BufferStatus.h>
 #include "BufferStatus.h"
 
-namespace android {
-namespace hardware {
-namespace media {
-namespace bufferpool {
-namespace V2_0 {
-namespace implementation {
+namespace aidl::android::hardware::media::bufferpool2::implementation {
 
-int64_t getTimestampNow() {
-    int64_t stamp;
-    struct timespec ts;
-    // TODO: CLOCK_MONOTONIC_COARSE?
-    clock_gettime(CLOCK_MONOTONIC, &ts);
-    stamp = ts.tv_nsec / 1000;
-    stamp += (ts.tv_sec * 1000000LL);
-    return stamp;
-}
+using aidl::android::hardware::media::bufferpool2::BufferStatus;
 
 bool isMessageLater(uint32_t curMsgId, uint32_t prevMsgId) {
     return curMsgId != prevMsgId && curMsgId - prevMsgId < prevMsgId - curMsgId;
@@ -53,30 +41,26 @@
 static constexpr int kNumElementsInQueue = 1024*16;
 static constexpr int kMinElementsToSyncInQueue = 128;
 
-ResultStatus BufferStatusObserver::open(
-        ConnectionId id, const StatusDescriptor** fmqDescPtr) {
+BufferPoolStatus BufferStatusObserver::open(
+        ConnectionId id, StatusDescriptor* fmqDescPtr) {
     if (mBufferStatusQueues.find(id) != mBufferStatusQueues.end()) {
-        // TODO: id collision log?
+        ALOGE("connection id collision %lld", (unsigned long long)id);
         return ResultStatus::CRITICAL_ERROR;
     }
-    std::unique_ptr<BufferStatusQueue> queue =
-            std::make_unique<BufferStatusQueue>(kNumElementsInQueue);
+    auto queue = std::make_unique<BufferStatusQueue>(kNumElementsInQueue);
     if (!queue || queue->isValid() == false) {
-        *fmqDescPtr = nullptr;
         return ResultStatus::NO_MEMORY;
-    } else {
-        *fmqDescPtr = queue->getDesc();
     }
+    *fmqDescPtr = queue->dupeDesc();
     auto result = mBufferStatusQueues.insert(
             std::make_pair(id, std::move(queue)));
     if (!result.second) {
-        *fmqDescPtr = nullptr;
         return ResultStatus::NO_MEMORY;
     }
     return ResultStatus::OK;
 }
 
-ResultStatus BufferStatusObserver::close(ConnectionId id) {
+BufferPoolStatus BufferStatusObserver::close(ConnectionId id) {
     if (mBufferStatusQueues.find(id) == mBufferStatusQueues.end()) {
         return ResultStatus::CRITICAL_ERROR;
     }
@@ -90,7 +74,7 @@
         size_t avail = it->second->availableToRead();
         while (avail > 0) {
             if (!it->second->read(&message, 1)) {
-                // Since avaliable # of reads are already confirmed,
+                // Since available # of reads are already confirmed,
                 // this should not happen.
                 // TODO: error handling (spurious client?)
                 ALOGW("FMQ message cannot be read from %lld", (long long)it->first);
@@ -105,8 +89,7 @@
 
 BufferStatusChannel::BufferStatusChannel(
         const StatusDescriptor &fmqDesc) {
-    std::unique_ptr<BufferStatusQueue> queue =
-            std::make_unique<BufferStatusQueue>(fmqDesc);
+    auto queue = std::make_unique<BufferStatusQueue>(fmqDesc);
     if (!queue || queue->isValid() == false) {
         mValid = false;
         return;
@@ -136,11 +119,11 @@
         BufferStatusMessage message;
         for (size_t i = 0 ; i < avail; ++i) {
             BufferId id = pending.front();
-            message.newStatus = BufferStatus::NOT_USED;
+            message.status = BufferStatus::NOT_USED;
             message.bufferId = id;
             message.connectionId = connectionId;
             if (!mBufferStatusQueue->write(&message, 1)) {
-                // Since avaliable # of writes are already confirmed,
+                // Since available # of writes are already confirmed,
                 // this should not happen.
                 // TODO: error handing?
                 ALOGW("FMQ message cannot be sent from %lld", (long long)connectionId);
@@ -160,11 +143,11 @@
         size_t avail = mBufferStatusQueue->availableToWrite();
         if (avail > 0) {
             BufferStatusMessage message;
-            message.newStatus = BufferStatus::INVALIDATION_ACK;
+            message.status = BufferStatus::INVALIDATION_ACK;
             message.bufferId = invalidateId;
             message.connectionId = connectionId;
             if (!mBufferStatusQueue->write(&message, 1)) {
-                // Since avaliable # of writes are already confirmed,
+                // Since available # of writes are already confirmed,
                 // this should not happen.
                 // TODO: error handing?
                 ALOGW("FMQ message cannot be sent from %lld", (long long)connectionId);
@@ -186,11 +169,11 @@
             BufferStatusMessage release, message;
             for (size_t i = 0; i < numPending; ++i) {
                 BufferId id = pending.front();
-                release.newStatus = BufferStatus::NOT_USED;
+                release.status = BufferStatus::NOT_USED;
                 release.bufferId = id;
                 release.connectionId = connectionId;
                 if (!mBufferStatusQueue->write(&release, 1)) {
-                    // Since avaliable # of writes are already confirmed,
+                    // Since available # of writes are already confirmed,
                     // this should not happen.
                     // TODO: error handling?
                     ALOGW("FMQ message cannot be sent from %lld", (long long)connectionId);
@@ -201,13 +184,13 @@
             }
             message.transactionId = transactionId;
             message.bufferId = bufferId;
-            message.newStatus = status;
+            message.status = status;
             message.connectionId = connectionId;
             message.targetConnectionId = targetId;
             // TODO : timesatamp
             message.timestampUs = 0;
             if (!mBufferStatusQueue->write(&message, 1)) {
-                // Since avaliable # of writes are already confirmed,
+                // Since available # of writes are already confirmed,
                 // this should not happen.
                 ALOGW("FMQ message cannot be sent from %lld", (long long)connectionId);
                 return false;
@@ -276,12 +259,11 @@
     return mValid;
 }
 
-void BufferInvalidationChannel::getDesc(const InvalidationDescriptor **fmqDescPtr) {
+void BufferInvalidationChannel::getDesc(InvalidationDescriptor *fmqDescPtr) {
     if (mValid) {
-        *fmqDescPtr = mBufferInvalidationQueue->getDesc();
-    } else {
-        *fmqDescPtr = nullptr;
+        *fmqDescPtr = mBufferInvalidationQueue->dupeDesc();
     }
+    // TODO: writing invalid descriptor?
 }
 
 void BufferInvalidationChannel::postInvalidation(
@@ -295,10 +277,5 @@
     mBufferInvalidationQueue->write(&message);
 }
 
-}  // namespace implementation
-}  // namespace V2_0
-}  // namespace bufferpool
-}  // namespace media
-}  // namespace hardware
-}  // namespace android
+}  // namespace ::aidl::android::hardware::media::bufferpool2::implementation