Implement bufferInfo handle packing logic
The service now will use buildBufferInfo() function to build the handle
and return real data to client.
Test: VtsHalBufferHubV1_0TargetTest
Bug: 116681016
Change-Id: I45baa91cc4f91f817c82b1c59243f48440a5558a
diff --git a/libs/ui/include/ui/BufferHubDefs.h b/libs/ui/include/ui/BufferHubDefs.h
index d259fef..069f0dc 100644
--- a/libs/ui/include/ui/BufferHubDefs.h
+++ b/libs/ui/include/ui/BufferHubDefs.h
@@ -158,6 +158,29 @@
static_assert(sizeof(MetadataHeader) == 128, "Unexpected MetadataHeader size");
static constexpr size_t kMetadataHeaderSize = sizeof(MetadataHeader);
+/**
+ * android.frameworks.bufferhub@1.0::BufferTraits.bufferInfo is an opaque handle. See
+ * https://cs.corp.google.com/android/frameworks/hardware/interfaces/bufferhub/1.0/types.hal for
+ * more details about android.frameworks.bufferhub@1.0::BufferTraits.
+ *
+ * This definition could be changed, but implementation of BufferHubService::buildBufferInfo
+ * (frameworks/native/services/bufferhub), VtsHalBufferHubV1_0TargetTest
+ * (frameworks/hardware/interfaces/bufferhub) and BufferHubBuffer::readBufferTraits (libui) will
+ * also need to be updated.
+ *
+ * It's definition should follow the following format:
+ * {
+ * NumFds = 1,
+ * NumInts = 3,
+ * data[0] = Ashmem fd for BufferHubMetadata,
+ * data[1] = buffer id,
+ * data[2] = client state bit mask,
+ * data[3] = user metadata size,
+ * }
+ */
+static constexpr int kBufferInfoNumFds = 1;
+static constexpr int kBufferInfoNumInts = 3;
+
} // namespace BufferHubDefs
} // namespace android
diff --git a/services/bufferhub/BufferHubService.cpp b/services/bufferhub/BufferHubService.cpp
index 43235f2..6389521 100644
--- a/services/bufferhub/BufferHubService.cpp
+++ b/services/bufferhub/BufferHubService.cpp
@@ -25,6 +25,7 @@
#include <log/log.h>
#include <openssl/hmac.h>
#include <system/graphics-base.h>
+#include <ui/BufferHubDefs.h>
using ::android::BufferHubDefs::MetadataHeader;
using ::android::hardware::Void;
@@ -64,10 +65,12 @@
std::lock_guard<std::mutex> lock(mClientSetMutex);
mClientSet.emplace(client);
+ hidl_handle bufferInfo =
+ buildBufferInfo(node->id(), node->AddNewActiveClientsBitToMask(),
+ node->user_metadata_size(), node->metadata().ashmem_fd());
BufferTraits bufferTraits = {/*bufferDesc=*/description,
/*bufferHandle=*/hidl_handle(node->buffer_handle()),
- // TODO(b/116681016): return real data to client
- /*bufferInfo=*/hidl_handle()};
+ /*bufferInfo=*/bufferInfo};
_hidl_cb(/*status=*/BufferHubStatus::NO_ERROR, /*bufferClient=*/client,
/*bufferTraits=*/bufferTraits);
@@ -151,10 +154,12 @@
HardwareBufferDescription bufferDesc;
memcpy(&bufferDesc, &node->buffer_desc(), sizeof(HardwareBufferDescription));
+ hidl_handle bufferInfo =
+ buildBufferInfo(node->id(), clientStateMask, node->user_metadata_size(),
+ node->metadata().ashmem_fd());
BufferTraits bufferTraits = {/*bufferDesc=*/bufferDesc,
/*bufferHandle=*/hidl_handle(node->buffer_handle()),
- // TODO(b/116681016): return real data to client
- /*bufferInfo=*/hidl_handle()};
+ /*bufferInfo=*/bufferInfo};
_hidl_cb(/*status=*/BufferHubStatus::NO_ERROR, /*bufferClient=*/client,
/*bufferTraits=*/bufferTraits);
@@ -336,6 +341,26 @@
}
}
+// Implementation of this function should be consistent with the definition of bufferInfo handle in
+// ui/BufferHubDefs.h.
+hidl_handle BufferHubService::buildBufferInfo(uint32_t bufferId, uint32_t clientBitMask,
+ uint32_t userMetadataSize, const int metadataFd) {
+ native_handle_t* infoHandle = native_handle_create(BufferHubDefs::kBufferInfoNumFds,
+ BufferHubDefs::kBufferInfoNumInts);
+
+ infoHandle->data[0] = dup(metadataFd);
+ // Use memcpy to convert to int without missing digit.
+ // TOOD(b/121345852): use bit_cast to unpack bufferInfo when C++20 becomes available.
+ memcpy(&infoHandle->data[1], &bufferId, sizeof(bufferId));
+ memcpy(&infoHandle->data[2], &clientBitMask, sizeof(clientBitMask));
+ memcpy(&infoHandle->data[3], &userMetadataSize, sizeof(userMetadataSize));
+
+ hidl_handle bufferInfo;
+ bufferInfo.setTo(infoHandle, /*shouldOwn=*/true);
+
+ return bufferInfo;
+}
+
void BufferHubService::removeTokenByClient(const BufferClient* client) {
std::lock_guard<std::mutex> lock(mTokenMutex);
auto iter = mTokenMap.begin();
diff --git a/services/bufferhub/include/bufferhub/BufferHubService.h b/services/bufferhub/include/bufferhub/BufferHubService.h
index 0195bcf..04ba5b7 100644
--- a/services/bufferhub/include/bufferhub/BufferHubService.h
+++ b/services/bufferhub/include/bufferhub/BufferHubService.h
@@ -57,6 +57,10 @@
void onClientClosed(const BufferClient* client);
private:
+ // Helper function to build BufferTraits.bufferInfo handle
+ hidl_handle buildBufferInfo(uint32_t bufferId, uint32_t clientBitMask,
+ uint32_t userMetadataSize, const int metadataFd);
+
// Helper function to remove all the token belongs to a specific client.
void removeTokenByClient(const BufferClient* client);