Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit efce32ef authored by Fan Xu's avatar Fan Xu Committed by Tianyu Jiang
Browse files

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
parent 427fb957
Loading
Loading
Loading
Loading
+23 −0
Original line number Diff line number Diff line
@@ -158,6 +158,29 @@ struct __attribute__((aligned(8))) MetadataHeader {
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
+29 −4
Original line number Diff line number Diff line
@@ -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 @@ Return<void> BufferHubService::allocateBuffer(const HardwareBufferDescription& d
    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 @@ Return<void> BufferHubService::importBuffer(const hidl_handle& tokenHandle,
    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 @@ void BufferHubService::onClientClosed(const BufferClient* client) {
    }
}

// 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();
+4 −0
Original line number Diff line number Diff line
@@ -57,6 +57,10 @@ public:
    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);