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

Commit 8adb3191 authored by Tianyu Jiang's avatar Tianyu Jiang
Browse files

Remove unnecessary copies of hidl_handle and its contents

Fix: 123582513
Test: BufferHub_test BufferHubServer_test VtsHalBufferHubV1_0TargetTest

Change-Id: I4d186d5ce34bc3b9714d94bfbcd6b0ce34294a6d
parent 4f2e7e67
Loading
Loading
Loading
Loading
+29 −19
Original line number Original line Diff line number Diff line
@@ -65,15 +65,20 @@ Return<void> BufferHubService::allocateBuffer(const HardwareBufferDescription& d
    std::lock_guard<std::mutex> lock(mClientSetMutex);
    std::lock_guard<std::mutex> lock(mClientSetMutex);
    mClientSet.emplace(client);
    mClientSet.emplace(client);


    hidl_handle bufferInfo = buildBufferInfo(node->id(), node->AddNewActiveClientsBitToMask(),
    // Allocate memory for bufferInfo of type hidl_handle on the stack. See
                                             node->user_metadata_size(), node->eventFd().get(),
    // http://aosp/286282 for the usage of NATIVE_HANDLE_DECLARE_STORAGE.
                                             node->metadata().ashmem_fd());
    NATIVE_HANDLE_DECLARE_STORAGE(bufferInfoStorage, BufferHubDefs::kBufferInfoNumFds,
                                  BufferHubDefs::kBufferInfoNumInts);
    hidl_handle bufferInfo =
            buildBufferInfo(bufferInfoStorage, node->id(), node->AddNewActiveClientsBitToMask(),
                            node->user_metadata_size(), node->metadata().ashmem_fd(),
                            node->eventFd().get());
    BufferTraits bufferTraits = {/*bufferDesc=*/description,
    BufferTraits bufferTraits = {/*bufferDesc=*/description,
                                 /*bufferHandle=*/hidl_handle(node->buffer_handle()),
                                 /*bufferHandle=*/hidl_handle(node->buffer_handle()),
                                 /*bufferInfo=*/bufferInfo};
                                 /*bufferInfo=*/std::move(bufferInfo)};


    _hidl_cb(/*status=*/BufferHubStatus::NO_ERROR, /*bufferClient=*/client,
    _hidl_cb(/*status=*/BufferHubStatus::NO_ERROR, /*bufferClient=*/client,
             /*bufferTraits=*/bufferTraits);
             /*bufferTraits=*/std::move(bufferTraits));
    return Void();
    return Void();
}
}


@@ -154,15 +159,19 @@ Return<void> BufferHubService::importBuffer(const hidl_handle& tokenHandle,
    HardwareBufferDescription bufferDesc;
    HardwareBufferDescription bufferDesc;
    memcpy(&bufferDesc, &node->buffer_desc(), sizeof(HardwareBufferDescription));
    memcpy(&bufferDesc, &node->buffer_desc(), sizeof(HardwareBufferDescription));


    hidl_handle bufferInfo =
    // Allocate memory for bufferInfo of type hidl_handle on the stack. See
            buildBufferInfo(node->id(), clientStateMask, node->user_metadata_size(),
    // http://aosp/286282 for the usage of NATIVE_HANDLE_DECLARE_STORAGE.
                            node->eventFd().get(), node->metadata().ashmem_fd());
    NATIVE_HANDLE_DECLARE_STORAGE(bufferInfoStorage, BufferHubDefs::kBufferInfoNumFds,
                                  BufferHubDefs::kBufferInfoNumInts);
    hidl_handle bufferInfo = buildBufferInfo(bufferInfoStorage, node->id(), clientStateMask,
                                             node->user_metadata_size(),
                                             node->metadata().ashmem_fd(), node->eventFd().get());
    BufferTraits bufferTraits = {/*bufferDesc=*/bufferDesc,
    BufferTraits bufferTraits = {/*bufferDesc=*/bufferDesc,
                                 /*bufferHandle=*/hidl_handle(node->buffer_handle()),
                                 /*bufferHandle=*/hidl_handle(node->buffer_handle()),
                                 /*bufferInfo=*/bufferInfo};
                                 /*bufferInfo=*/std::move(bufferInfo)};


    _hidl_cb(/*status=*/BufferHubStatus::NO_ERROR, /*bufferClient=*/client,
    _hidl_cb(/*status=*/BufferHubStatus::NO_ERROR, /*bufferClient=*/client,
             /*bufferTraits=*/bufferTraits);
             /*bufferTraits=*/std::move(bufferTraits));
    return Void();
    return Void();
}
}


@@ -344,14 +353,15 @@ void BufferHubService::onClientClosed(const BufferClient* client) {


// Implementation of this function should be consistent with the definition of bufferInfo handle in
// Implementation of this function should be consistent with the definition of bufferInfo handle in
// ui/BufferHubDefs.h.
// ui/BufferHubDefs.h.
hidl_handle BufferHubService::buildBufferInfo(int bufferId, uint32_t clientBitMask,
hidl_handle BufferHubService::buildBufferInfo(char* bufferInfoStorage, int bufferId,
                                              uint32_t userMetadataSize, const int eventFd,
                                              uint32_t clientBitMask, uint32_t userMetadataSize,
                                              const int metadataFd) {
                                              int metadataFd, int eventFd) {
    native_handle_t* infoHandle = native_handle_create(BufferHubDefs::kBufferInfoNumFds,
    native_handle_t* infoHandle =
            native_handle_init(bufferInfoStorage, BufferHubDefs::kBufferInfoNumFds,
                               BufferHubDefs::kBufferInfoNumInts);
                               BufferHubDefs::kBufferInfoNumInts);


    infoHandle->data[0] = dup(metadataFd);
    infoHandle->data[0] = metadataFd;
    infoHandle->data[1] = dup(eventFd);
    infoHandle->data[1] = eventFd;
    infoHandle->data[2] = bufferId;
    infoHandle->data[2] = bufferId;
    // Use memcpy to convert to int without missing digit.
    // Use memcpy to convert to int without missing digit.
    // TOOD(b/121345852): use bit_cast to unpack bufferInfo when C++20 becomes available.
    // TOOD(b/121345852): use bit_cast to unpack bufferInfo when C++20 becomes available.
@@ -359,7 +369,7 @@ hidl_handle BufferHubService::buildBufferInfo(int bufferId, uint32_t clientBitMa
    memcpy(&infoHandle->data[4], &userMetadataSize, sizeof(userMetadataSize));
    memcpy(&infoHandle->data[4], &userMetadataSize, sizeof(userMetadataSize));


    hidl_handle bufferInfo;
    hidl_handle bufferInfo;
    bufferInfo.setTo(infoHandle, /*shouldOwn=*/true);
    bufferInfo.setTo(infoHandle, /*shouldOwn=*/false);


    return bufferInfo;
    return bufferInfo;
}
}
+2 −2
Original line number Original line Diff line number Diff line
@@ -58,8 +58,8 @@ public:


private:
private:
    // Helper function to build BufferTraits.bufferInfo handle
    // Helper function to build BufferTraits.bufferInfo handle
    hidl_handle buildBufferInfo(int bufferId, uint32_t clientBitMask, uint32_t userMetadataSize,
    hidl_handle buildBufferInfo(char* bufferInfoStorage, int bufferId, uint32_t clientBitMask,
                                const int eventFd, const int metadataFd);
                                uint32_t userMetadataSize, int metadataFd, int eventFd);


    // Helper function to remove all the token belongs to a specific client.
    // Helper function to remove all the token belongs to a specific client.
    void removeTokenByClient(const BufferClient* client);
    void removeTokenByClient(const BufferClient* client);