Loading libs/ui/BufferHubBuffer.cpp +11 −5 Original line number Diff line number Diff line Loading @@ -167,19 +167,26 @@ int BufferHubBuffer::initWithBufferTraits(const BufferTraits& bufferTraits) { return -EINVAL; } int bufferId = bufferTraits.bufferInfo->data[1]; int bufferId = bufferTraits.bufferInfo->data[2]; if (bufferId < 0) { ALOGE("%s: Received an invalid (negative) id!", __FUNCTION__); return -EINVAL; } uint32_t clientBitMask; memcpy(&clientBitMask, &bufferTraits.bufferInfo->data[2], sizeof(clientBitMask)); memcpy(&clientBitMask, &bufferTraits.bufferInfo->data[3], sizeof(clientBitMask)); if (clientBitMask == 0U) { ALOGE("%s: Received a invalid client state mask!", __FUNCTION__); return -EINVAL; } const int eventFd = bufferTraits.bufferInfo->data[1]; if (eventFd < 0) { ALOGE("%s: Received a invalid event fd!", __FUNCTION__); return -EINVAL; } mEventFd = BufferHubEventFd(eventFd); // Import the metadata. Dup since hidl_handle owns the fd unique_fd ashmemFd(fcntl(bufferTraits.bufferInfo->data[0], F_DUPFD_CLOEXEC, 0)); mMetadata = BufferHubMetadata::Import(std::move(ashmemFd)); Loading @@ -190,7 +197,7 @@ int BufferHubBuffer::initWithBufferTraits(const BufferTraits& bufferTraits) { } uint32_t userMetadataSize; memcpy(&userMetadataSize, &bufferTraits.bufferInfo->data[3], sizeof(userMetadataSize)); memcpy(&userMetadataSize, &bufferTraits.bufferInfo->data[4], sizeof(userMetadataSize)); if (mMetadata.user_metadata_size() != userMetadataSize) { ALOGE("%s: user metadata size not match: expected %u, actual %zu.", __FUNCTION__, userMetadataSize, mMetadata.user_metadata_size()); Loading Loading @@ -314,9 +321,8 @@ int BufferHubBuffer::Release() { } bool BufferHubBuffer::IsValid() const { // TODO(b/68770788): check eventFd once implemented return mBufferHandle.getNativeHandle() != nullptr && mId >= 0 && mClientStateMask != 0U && mMetadata.IsValid() && mBufferClient != nullptr; mEventFd.get() >= 0 && mMetadata.IsValid() && mBufferClient != nullptr; } native_handle_t* BufferHubBuffer::Duplicate() { Loading libs/ui/BufferHubEventFd.cpp +2 −0 Original line number Diff line number Diff line Loading @@ -23,6 +23,8 @@ namespace android { BufferHubEventFd::BufferHubEventFd() : mFd(eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK)) {} BufferHubEventFd::BufferHubEventFd(int fd) : mFd(fd) {} status_t BufferHubEventFd::signal() const { if (!isValid()) { ALOGE("%s: cannot signal an invalid eventfd.", __FUNCTION__); Loading libs/ui/include/ui/BufferHubBuffer.h +6 −0 Original line number Diff line number Diff line Loading @@ -21,6 +21,7 @@ #include <android/hardware_buffer.h> #include <cutils/native_handle.h> #include <ui/BufferHubDefs.h> #include <ui/BufferHubEventFd.h> #include <ui/BufferHubMetadata.h> namespace android { Loading Loading @@ -55,6 +56,8 @@ public: return native_handle_clone(mBufferHandle.getNativeHandle()); } const BufferHubEventFd& eventFd() const { return mEventFd; } // Returns the current value of MetadataHeader::buffer_state. uint32_t buffer_state() { return mMetadata.metadata_header()->buffer_state.load(std::memory_order_acquire); Loading Loading @@ -123,6 +126,9 @@ private: // Wraps the gralloc buffer handle of this buffer. hardware::hidl_handle mBufferHandle; // Event fd used for signalling buffer state changes. Shared by all clients of the same buffer. BufferHubEventFd mEventFd; // An ashmem-based metadata object. The same shared memory are mapped to the // bufferhubd daemon and all buffer clients. BufferHubMetadata mMetadata; Loading libs/ui/include/ui/BufferHubDefs.h +6 −5 Original line number Diff line number Diff line Loading @@ -170,15 +170,16 @@ static constexpr size_t kMetadataHeaderSize = sizeof(MetadataHeader); * * It's definition should follow the following format: * { * NumFds = 1, * NumFds = 2, * NumInts = 3, * data[0] = Ashmem fd for BufferHubMetadata, * data[1] = buffer id, * data[2] = client state bit mask, * data[3] = user metadata size, * data[1] = event fd, * data[2] = buffer id, * data[3] = client state bit mask, * data[4] = user metadata size, * } */ static constexpr int kBufferInfoNumFds = 1; static constexpr int kBufferInfoNumFds = 2; static constexpr int kBufferInfoNumInts = 3; } // namespace BufferHubDefs Loading libs/ui/include/ui/BufferHubEventFd.h +6 −0 Original line number Diff line number Diff line Loading @@ -29,6 +29,12 @@ public: */ BufferHubEventFd(); /** * Constructs from a valid event fd. Caller is responsible for the validity of the fd. Takes * ownership. */ BufferHubEventFd(int fd); /** * Returns whether this BufferHubEventFd holds a valid event_fd. */ Loading Loading
libs/ui/BufferHubBuffer.cpp +11 −5 Original line number Diff line number Diff line Loading @@ -167,19 +167,26 @@ int BufferHubBuffer::initWithBufferTraits(const BufferTraits& bufferTraits) { return -EINVAL; } int bufferId = bufferTraits.bufferInfo->data[1]; int bufferId = bufferTraits.bufferInfo->data[2]; if (bufferId < 0) { ALOGE("%s: Received an invalid (negative) id!", __FUNCTION__); return -EINVAL; } uint32_t clientBitMask; memcpy(&clientBitMask, &bufferTraits.bufferInfo->data[2], sizeof(clientBitMask)); memcpy(&clientBitMask, &bufferTraits.bufferInfo->data[3], sizeof(clientBitMask)); if (clientBitMask == 0U) { ALOGE("%s: Received a invalid client state mask!", __FUNCTION__); return -EINVAL; } const int eventFd = bufferTraits.bufferInfo->data[1]; if (eventFd < 0) { ALOGE("%s: Received a invalid event fd!", __FUNCTION__); return -EINVAL; } mEventFd = BufferHubEventFd(eventFd); // Import the metadata. Dup since hidl_handle owns the fd unique_fd ashmemFd(fcntl(bufferTraits.bufferInfo->data[0], F_DUPFD_CLOEXEC, 0)); mMetadata = BufferHubMetadata::Import(std::move(ashmemFd)); Loading @@ -190,7 +197,7 @@ int BufferHubBuffer::initWithBufferTraits(const BufferTraits& bufferTraits) { } uint32_t userMetadataSize; memcpy(&userMetadataSize, &bufferTraits.bufferInfo->data[3], sizeof(userMetadataSize)); memcpy(&userMetadataSize, &bufferTraits.bufferInfo->data[4], sizeof(userMetadataSize)); if (mMetadata.user_metadata_size() != userMetadataSize) { ALOGE("%s: user metadata size not match: expected %u, actual %zu.", __FUNCTION__, userMetadataSize, mMetadata.user_metadata_size()); Loading Loading @@ -314,9 +321,8 @@ int BufferHubBuffer::Release() { } bool BufferHubBuffer::IsValid() const { // TODO(b/68770788): check eventFd once implemented return mBufferHandle.getNativeHandle() != nullptr && mId >= 0 && mClientStateMask != 0U && mMetadata.IsValid() && mBufferClient != nullptr; mEventFd.get() >= 0 && mMetadata.IsValid() && mBufferClient != nullptr; } native_handle_t* BufferHubBuffer::Duplicate() { Loading
libs/ui/BufferHubEventFd.cpp +2 −0 Original line number Diff line number Diff line Loading @@ -23,6 +23,8 @@ namespace android { BufferHubEventFd::BufferHubEventFd() : mFd(eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK)) {} BufferHubEventFd::BufferHubEventFd(int fd) : mFd(fd) {} status_t BufferHubEventFd::signal() const { if (!isValid()) { ALOGE("%s: cannot signal an invalid eventfd.", __FUNCTION__); Loading
libs/ui/include/ui/BufferHubBuffer.h +6 −0 Original line number Diff line number Diff line Loading @@ -21,6 +21,7 @@ #include <android/hardware_buffer.h> #include <cutils/native_handle.h> #include <ui/BufferHubDefs.h> #include <ui/BufferHubEventFd.h> #include <ui/BufferHubMetadata.h> namespace android { Loading Loading @@ -55,6 +56,8 @@ public: return native_handle_clone(mBufferHandle.getNativeHandle()); } const BufferHubEventFd& eventFd() const { return mEventFd; } // Returns the current value of MetadataHeader::buffer_state. uint32_t buffer_state() { return mMetadata.metadata_header()->buffer_state.load(std::memory_order_acquire); Loading Loading @@ -123,6 +126,9 @@ private: // Wraps the gralloc buffer handle of this buffer. hardware::hidl_handle mBufferHandle; // Event fd used for signalling buffer state changes. Shared by all clients of the same buffer. BufferHubEventFd mEventFd; // An ashmem-based metadata object. The same shared memory are mapped to the // bufferhubd daemon and all buffer clients. BufferHubMetadata mMetadata; Loading
libs/ui/include/ui/BufferHubDefs.h +6 −5 Original line number Diff line number Diff line Loading @@ -170,15 +170,16 @@ static constexpr size_t kMetadataHeaderSize = sizeof(MetadataHeader); * * It's definition should follow the following format: * { * NumFds = 1, * NumFds = 2, * NumInts = 3, * data[0] = Ashmem fd for BufferHubMetadata, * data[1] = buffer id, * data[2] = client state bit mask, * data[3] = user metadata size, * data[1] = event fd, * data[2] = buffer id, * data[3] = client state bit mask, * data[4] = user metadata size, * } */ static constexpr int kBufferInfoNumFds = 1; static constexpr int kBufferInfoNumFds = 2; static constexpr int kBufferInfoNumInts = 3; } // namespace BufferHubDefs Loading
libs/ui/include/ui/BufferHubEventFd.h +6 −0 Original line number Diff line number Diff line Loading @@ -29,6 +29,12 @@ public: */ BufferHubEventFd(); /** * Constructs from a valid event fd. Caller is responsible for the validity of the fd. Takes * ownership. */ BufferHubEventFd(int fd); /** * Returns whether this BufferHubEventFd holds a valid event_fd. */ Loading