diff options
author | 2019-01-29 10:54:24 -0800 | |
---|---|---|
committer | 2019-01-31 02:42:29 +0000 | |
commit | 8adb319152c639d382625789c7be558be7f7c9b5 (patch) | |
tree | 1c892915882887a133fe27f95da310e774114f9b | |
parent | 4f2e7e6778aecf5c93655bbc4a02ef4ade6c096a (diff) |
Remove unnecessary copies of hidl_handle and its contents
Fix: 123582513
Test: BufferHub_test BufferHubServer_test VtsHalBufferHubV1_0TargetTest
Change-Id: I4d186d5ce34bc3b9714d94bfbcd6b0ce34294a6d
-rw-r--r-- | services/bufferhub/BufferHubService.cpp | 48 | ||||
-rw-r--r-- | services/bufferhub/include/bufferhub/BufferHubService.h | 4 |
2 files changed, 31 insertions, 21 deletions
diff --git a/services/bufferhub/BufferHubService.cpp b/services/bufferhub/BufferHubService.cpp index ae357cda8a..6b802fbf83 100644 --- a/services/bufferhub/BufferHubService.cpp +++ b/services/bufferhub/BufferHubService.cpp @@ -65,15 +65,20 @@ 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->eventFd().get(), - node->metadata().ashmem_fd()); + // Allocate memory for bufferInfo of type hidl_handle on the stack. See + // http://aosp/286282 for the usage of NATIVE_HANDLE_DECLARE_STORAGE. + 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, /*bufferHandle=*/hidl_handle(node->buffer_handle()), - /*bufferInfo=*/bufferInfo}; + /*bufferInfo=*/std::move(bufferInfo)}; _hidl_cb(/*status=*/BufferHubStatus::NO_ERROR, /*bufferClient=*/client, - /*bufferTraits=*/bufferTraits); + /*bufferTraits=*/std::move(bufferTraits)); return Void(); } @@ -154,15 +159,19 @@ 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->eventFd().get(), node->metadata().ashmem_fd()); + // Allocate memory for bufferInfo of type hidl_handle on the stack. See + // http://aosp/286282 for the usage of NATIVE_HANDLE_DECLARE_STORAGE. + 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, /*bufferHandle=*/hidl_handle(node->buffer_handle()), - /*bufferInfo=*/bufferInfo}; + /*bufferInfo=*/std::move(bufferInfo)}; _hidl_cb(/*status=*/BufferHubStatus::NO_ERROR, /*bufferClient=*/client, - /*bufferTraits=*/bufferTraits); + /*bufferTraits=*/std::move(bufferTraits)); 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 // ui/BufferHubDefs.h. -hidl_handle BufferHubService::buildBufferInfo(int bufferId, uint32_t clientBitMask, - uint32_t userMetadataSize, const int eventFd, - const int metadataFd) { - native_handle_t* infoHandle = native_handle_create(BufferHubDefs::kBufferInfoNumFds, - BufferHubDefs::kBufferInfoNumInts); - - infoHandle->data[0] = dup(metadataFd); - infoHandle->data[1] = dup(eventFd); +hidl_handle BufferHubService::buildBufferInfo(char* bufferInfoStorage, int bufferId, + uint32_t clientBitMask, uint32_t userMetadataSize, + int metadataFd, int eventFd) { + native_handle_t* infoHandle = + native_handle_init(bufferInfoStorage, BufferHubDefs::kBufferInfoNumFds, + BufferHubDefs::kBufferInfoNumInts); + + infoHandle->data[0] = metadataFd; + infoHandle->data[1] = eventFd; infoHandle->data[2] = bufferId; // Use memcpy to convert to int without missing digit. // 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)); hidl_handle bufferInfo; - bufferInfo.setTo(infoHandle, /*shouldOwn=*/true); + bufferInfo.setTo(infoHandle, /*shouldOwn=*/false); return bufferInfo; } diff --git a/services/bufferhub/include/bufferhub/BufferHubService.h b/services/bufferhub/include/bufferhub/BufferHubService.h index 9015e05e46..edad20b194 100644 --- a/services/bufferhub/include/bufferhub/BufferHubService.h +++ b/services/bufferhub/include/bufferhub/BufferHubService.h @@ -58,8 +58,8 @@ public: private: // Helper function to build BufferTraits.bufferInfo handle - hidl_handle buildBufferInfo(int bufferId, uint32_t clientBitMask, uint32_t userMetadataSize, - const int eventFd, const int metadataFd); + hidl_handle buildBufferInfo(char* bufferInfoStorage, int bufferId, uint32_t clientBitMask, + uint32_t userMetadataSize, int metadataFd, int eventFd); // Helper function to remove all the token belongs to a specific client. void removeTokenByClient(const BufferClient* client); |