From 8f51ec64984c1f03cdb6edef95b1e4098939da45 Mon Sep 17 00:00:00 2001 From: Jiwen 'Steve' Cai Date: Tue, 7 Aug 2018 21:50:51 -0700 Subject: Move detached buffer to libui Move it into libui early so that new modifications towards it can be coded in the libui style This CL only moves the file and updates Android.bp with proper dependencies. Will re-format the coding style in a follow up CL. The reason behind this is to keep this CL small enough so that the "git mv" operation will be considered as an renaming rather than a complete rewrite. Note that DetachedBuffer is not exposed to VNDK, so that we won't need to worry about ABI compatibility. Also, it temporarily introduces some clang warning exceptions, we should be able to remove them very soon once pdx to binder refactor is done for detached buffer. Bug: 112010261 Test: atest BufferHubMetadata_test Change-Id: I63659b9a9b7cb56f30fc2ae8cc5b87977d79b59c --- libs/ui/BufferHubBuffer.cpp | 215 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 215 insertions(+) create mode 100644 libs/ui/BufferHubBuffer.cpp (limited to 'libs/ui/BufferHubBuffer.cpp') diff --git a/libs/ui/BufferHubBuffer.cpp b/libs/ui/BufferHubBuffer.cpp new file mode 100644 index 0000000000..9293711d0e --- /dev/null +++ b/libs/ui/BufferHubBuffer.cpp @@ -0,0 +1,215 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// We would eliminate the clang warnings introduced by libdpx. +// TODO(b/112338294): Remove those once BufferHub moved to use Binder +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wconversion" +#pragma clang diagnostic ignored "-Wdouble-promotion" +#pragma clang diagnostic ignored "-Wgnu-case-range" +#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments" +#pragma clang diagnostic ignored "-Winconsistent-missing-destructor-override" +#pragma clang diagnostic ignored "-Wnested-anon-types" +#pragma clang diagnostic ignored "-Wpacked" +#pragma clang diagnostic ignored "-Wshadow" +#pragma clang diagnostic ignored "-Wsign-conversion" +#pragma clang diagnostic ignored "-Wswitch-enum" +#pragma clang diagnostic ignored "-Wundefined-func-template" +#pragma clang diagnostic ignored "-Wunused-template" +#pragma clang diagnostic ignored "-Wweak-vtables" +#include +#include +#include +#include +#pragma clang diagnostic pop + +#include +#include + +#include + +using android::dvr::BufferHubMetadata; +using android::dvr::BufferTraits; +using android::dvr::DetachedBufferRPC; +using android::dvr::NativeHandleWrapper; +using android::pdx::LocalChannelHandle; +using android::pdx::LocalHandle; +using android::pdx::Status; +using android::pdx::default_transport::ClientChannel; +using android::pdx::default_transport::ClientChannelFactory; + +namespace android { + +namespace { + +// TODO(b/112338294): Remove this string literal after refactoring BufferHub +// to use Binder. +static constexpr char kBufferHubClientPath[] = "system/buffer_hub/client"; + +} // namespace + +BufferHubClient::BufferHubClient() + : Client(ClientChannelFactory::Create(kBufferHubClientPath)) {} + +BufferHubClient::BufferHubClient(LocalChannelHandle channel_handle) + : Client(ClientChannel::Create(std::move(channel_handle))) {} + +BufferHubClient::~BufferHubClient() {} + +bool BufferHubClient::IsValid() const { + return IsConnected() && GetChannelHandle().valid(); +} + +LocalChannelHandle BufferHubClient::TakeChannelHandle() { + if (IsConnected()) { + return std::move(GetChannelHandle()); + } else { + return {}; + } +} + +DetachedBuffer::DetachedBuffer(uint32_t width, uint32_t height, + uint32_t layer_count, uint32_t format, + uint64_t usage, size_t user_metadata_size) { + ATRACE_NAME("DetachedBuffer::DetachedBuffer"); + ALOGD("DetachedBuffer::DetachedBuffer: width=%u height=%u layer_count=%u, format=%u " + "usage=%" PRIx64 " user_metadata_size=%zu", + width, height, layer_count, format, usage, user_metadata_size); + + auto status = client_.InvokeRemoteMethod( + width, height, layer_count, format, usage, user_metadata_size); + if (!status) { + ALOGE( + "DetachedBuffer::DetachedBuffer: Failed to create detached buffer: %s", + status.GetErrorMessage().c_str()); + client_.Close(-status.error()); + } + + const int ret = ImportGraphicBuffer(); + if (ret < 0) { + ALOGE("DetachedBuffer::DetachedBuffer: Failed to import buffer: %s", + strerror(-ret)); + client_.Close(ret); + } +} + +DetachedBuffer::DetachedBuffer(LocalChannelHandle channel_handle) + : client_(std::move(channel_handle)) { + const int ret = ImportGraphicBuffer(); + if (ret < 0) { + ALOGE("DetachedBuffer::DetachedBuffer: Failed to import buffer: %s", + strerror(-ret)); + client_.Close(ret); + } +} + +int DetachedBuffer::ImportGraphicBuffer() { + ATRACE_NAME("DetachedBuffer::ImportGraphicBuffer"); + + auto status = client_.InvokeRemoteMethod(); + if (!status) { + ALOGE("DetachedBuffer::DetachedBuffer: Failed to import GraphicBuffer: %s", + status.GetErrorMessage().c_str()); + return -status.error(); + } + + BufferTraits buffer_traits = status.take(); + if (buffer_traits.id() < 0) { + ALOGE("DetachedBuffer::DetachedBuffer: Received an invalid id!"); + return -EIO; + } + + // Stash the buffer id to replace the value in id_. + const int buffer_id = buffer_traits.id(); + + // Import the metadata. + metadata_ = BufferHubMetadata::Import(buffer_traits.take_metadata_handle()); + + if (!metadata_.IsValid()) { + ALOGE("DetachedBuffer::ImportGraphicBuffer: invalid metadata."); + return -ENOMEM; + } + + if (metadata_.metadata_size() != buffer_traits.metadata_size()) { + ALOGE( + "DetachedBuffer::ImportGraphicBuffer: metadata buffer too small: " + "%zu, expected: %" PRIu64 ".", + metadata_.metadata_size(), buffer_traits.metadata_size()); + return -ENOMEM; + } + + size_t metadata_buf_size = static_cast(buffer_traits.metadata_size()); + if (metadata_buf_size < dvr::BufferHubDefs::kMetadataHeaderSize) { + ALOGE("DetachedBuffer::ImportGraphicBuffer: metadata too small: %zu", + metadata_buf_size); + return -EINVAL; + } + + // Import the buffer: We only need to hold on the native_handle_t here so that + // GraphicBuffer instance can be created in future. + buffer_handle_ = buffer_traits.take_buffer_handle(); + + // If all imports succeed, replace the previous buffer and id. + id_ = buffer_id; + buffer_state_bit_ = buffer_traits.buffer_state_bit(); + + // TODO(b/112012161) Set up shared fences. + ALOGD("DetachedBuffer::ImportGraphicBuffer: id=%d, buffer_state=%" PRIx64 ".", id(), + metadata_.metadata_header()->buffer_state.load(std::memory_order_acquire)); + return 0; +} + +int DetachedBuffer::Poll(int timeout_ms) { + ATRACE_NAME("DetachedBuffer::Poll"); + pollfd p = {client_.event_fd(), POLLIN, 0}; + return poll(&p, 1, timeout_ms); +} + +Status DetachedBuffer::Promote() { + // TODO(b/112338294) remove after migrate producer buffer to binder + ALOGW("DetachedBuffer::Promote: not supported operation during migration"); + return {}; + + ATRACE_NAME("DetachedBuffer::Promote"); + ALOGD("DetachedBuffer::Promote: id=%d.", id_); + + auto status_or_handle = + client_.InvokeRemoteMethod(); + if (status_or_handle.ok()) { + // Invalidate the buffer. + buffer_handle_ = {}; + } else { + ALOGE("DetachedBuffer::Promote: Failed to promote buffer (id=%d): %s.", id_, + status_or_handle.GetErrorMessage().c_str()); + } + return status_or_handle; +} + +Status DetachedBuffer::Duplicate() { + ATRACE_NAME("DetachedBuffer::Duplicate"); + ALOGD("DetachedBuffer::Duplicate: id=%d.", id_); + + auto status_or_handle = + client_.InvokeRemoteMethod(); + + if (!status_or_handle.ok()) { + ALOGE("DetachedBuffer::Duplicate: Failed to duplicate buffer (id=%d): %s.", + id_, status_or_handle.GetErrorMessage().c_str()); + } + return status_or_handle; +} + +} // namespace android -- cgit v1.2.3-59-g8ed1b From ff675b716822808a93e1f445754cddc4458f1249 Mon Sep 17 00:00:00 2001 From: Jiwen 'Steve' Cai Date: Tue, 9 Oct 2018 18:08:29 -0700 Subject: Format BufferHub{Buffer,Metadata}.{h,cpp} to match libui style 1/ Run clang-format 2/ Rename variables to use camel case 3/ 100 column per line 4/ Rename DetachedBuffer to BufferHubBuffer Bug: 68273829 Bug: 112010261 Bug: 112940221 Test: BufferHubMetadata_test, buffer_hub-test Change-Id: Iaf0dbbe797daf8b61864dc2e3ea1693703ef9d7f --- libs/gui/BufferHubProducer.cpp | 6 +- libs/ui/BufferHubBuffer.cpp | 252 +++++++++++++++---------------- libs/ui/BufferHubMetadata.cpp | 128 ++++++++-------- libs/ui/include/ui/BufferHubBuffer.h | 204 +++++++++++++------------ libs/ui/include/ui/BufferHubMetadata.h | 114 +++++++------- libs/vr/libbufferhub/buffer_hub-test.cpp | 44 +++--- 6 files changed, 372 insertions(+), 376 deletions(-) (limited to 'libs/ui/BufferHubBuffer.cpp') diff --git a/libs/gui/BufferHubProducer.cpp b/libs/gui/BufferHubProducer.cpp index 0623a8ee11..3dac037880 100644 --- a/libs/gui/BufferHubProducer.cpp +++ b/libs/gui/BufferHubProducer.cpp @@ -400,14 +400,14 @@ status_t BufferHubProducer::attachBuffer(int* out_slot, const sp& ALOGE("attachBuffer: DetachedBufferHandle cannot be NULL."); return BAD_VALUE; } - auto detached_buffer = DetachedBuffer::Import(std::move(detached_handle->handle())); + auto detached_buffer = BufferHubBuffer::Import(std::move(detached_handle->handle())); if (detached_buffer == nullptr) { - ALOGE("attachBuffer: DetachedBuffer cannot be NULL."); + ALOGE("attachBuffer: BufferHubBuffer cannot be NULL."); return BAD_VALUE; } auto status_or_handle = detached_buffer->Promote(); if (!status_or_handle.ok()) { - ALOGE("attachBuffer: Failed to promote a DetachedBuffer into a BufferProducer, error=%d.", + ALOGE("attachBuffer: Failed to promote a BufferHubBuffer into a BufferProducer, error=%d.", status_or_handle.error()); return BAD_VALUE; } diff --git a/libs/ui/BufferHubBuffer.cpp b/libs/ui/BufferHubBuffer.cpp index 9293711d0e..606386c740 100644 --- a/libs/ui/BufferHubBuffer.cpp +++ b/libs/ui/BufferHubBuffer.cpp @@ -45,6 +45,8 @@ using android::dvr::BufferHubMetadata; using android::dvr::BufferTraits; using android::dvr::DetachedBufferRPC; using android::dvr::NativeHandleWrapper; + +// TODO(b/112338294): Remove PDX dependencies from libui. using android::pdx::LocalChannelHandle; using android::pdx::LocalHandle; using android::pdx::Status; @@ -59,13 +61,12 @@ namespace { // to use Binder. static constexpr char kBufferHubClientPath[] = "system/buffer_hub/client"; -} // namespace +} // namespace -BufferHubClient::BufferHubClient() - : Client(ClientChannelFactory::Create(kBufferHubClientPath)) {} +BufferHubClient::BufferHubClient() : Client(ClientChannelFactory::Create(kBufferHubClientPath)) {} -BufferHubClient::BufferHubClient(LocalChannelHandle channel_handle) - : Client(ClientChannel::Create(std::move(channel_handle))) {} +BufferHubClient::BufferHubClient(LocalChannelHandle mChannelHandle) + : Client(ClientChannel::Create(std::move(mChannelHandle))) {} BufferHubClient::~BufferHubClient() {} @@ -74,142 +75,137 @@ bool BufferHubClient::IsValid() const { } LocalChannelHandle BufferHubClient::TakeChannelHandle() { - if (IsConnected()) { - return std::move(GetChannelHandle()); - } else { - return {}; - } + if (IsConnected()) { + return std::move(GetChannelHandle()); + } else { + return {}; + } } -DetachedBuffer::DetachedBuffer(uint32_t width, uint32_t height, - uint32_t layer_count, uint32_t format, - uint64_t usage, size_t user_metadata_size) { - ATRACE_NAME("DetachedBuffer::DetachedBuffer"); - ALOGD("DetachedBuffer::DetachedBuffer: width=%u height=%u layer_count=%u, format=%u " - "usage=%" PRIx64 " user_metadata_size=%zu", - width, height, layer_count, format, usage, user_metadata_size); - - auto status = client_.InvokeRemoteMethod( - width, height, layer_count, format, usage, user_metadata_size); - if (!status) { - ALOGE( - "DetachedBuffer::DetachedBuffer: Failed to create detached buffer: %s", - status.GetErrorMessage().c_str()); - client_.Close(-status.error()); - } - - const int ret = ImportGraphicBuffer(); - if (ret < 0) { - ALOGE("DetachedBuffer::DetachedBuffer: Failed to import buffer: %s", - strerror(-ret)); - client_.Close(ret); - } +BufferHubBuffer::BufferHubBuffer(uint32_t width, uint32_t height, uint32_t layerCount, + uint32_t format, uint64_t usage, size_t mUserMetadataSize) { + ATRACE_CALL(); + ALOGD("BufferHubBuffer::BufferHubBuffer: width=%u height=%u layerCount=%u, format=%u " + "usage=%" PRIx64 " mUserMetadataSize=%zu", + width, height, layerCount, format, usage, mUserMetadataSize); + + auto status = + mClient.InvokeRemoteMethod(width, height, layerCount, format, + usage, mUserMetadataSize); + if (!status) { + ALOGE("BufferHubBuffer::BufferHubBuffer: Failed to create detached buffer: %s", + status.GetErrorMessage().c_str()); + mClient.Close(-status.error()); + } + + const int ret = ImportGraphicBuffer(); + if (ret < 0) { + ALOGE("BufferHubBuffer::BufferHubBuffer: Failed to import buffer: %s", strerror(-ret)); + mClient.Close(ret); + } } -DetachedBuffer::DetachedBuffer(LocalChannelHandle channel_handle) - : client_(std::move(channel_handle)) { - const int ret = ImportGraphicBuffer(); - if (ret < 0) { - ALOGE("DetachedBuffer::DetachedBuffer: Failed to import buffer: %s", - strerror(-ret)); - client_.Close(ret); - } +BufferHubBuffer::BufferHubBuffer(LocalChannelHandle mChannelHandle) + : mClient(std::move(mChannelHandle)) { + const int ret = ImportGraphicBuffer(); + if (ret < 0) { + ALOGE("BufferHubBuffer::BufferHubBuffer: Failed to import buffer: %s", strerror(-ret)); + mClient.Close(ret); + } } -int DetachedBuffer::ImportGraphicBuffer() { - ATRACE_NAME("DetachedBuffer::ImportGraphicBuffer"); - - auto status = client_.InvokeRemoteMethod(); - if (!status) { - ALOGE("DetachedBuffer::DetachedBuffer: Failed to import GraphicBuffer: %s", - status.GetErrorMessage().c_str()); - return -status.error(); - } - - BufferTraits buffer_traits = status.take(); - if (buffer_traits.id() < 0) { - ALOGE("DetachedBuffer::DetachedBuffer: Received an invalid id!"); - return -EIO; - } - - // Stash the buffer id to replace the value in id_. - const int buffer_id = buffer_traits.id(); - - // Import the metadata. - metadata_ = BufferHubMetadata::Import(buffer_traits.take_metadata_handle()); - - if (!metadata_.IsValid()) { - ALOGE("DetachedBuffer::ImportGraphicBuffer: invalid metadata."); - return -ENOMEM; - } - - if (metadata_.metadata_size() != buffer_traits.metadata_size()) { - ALOGE( - "DetachedBuffer::ImportGraphicBuffer: metadata buffer too small: " - "%zu, expected: %" PRIu64 ".", - metadata_.metadata_size(), buffer_traits.metadata_size()); - return -ENOMEM; - } - - size_t metadata_buf_size = static_cast(buffer_traits.metadata_size()); - if (metadata_buf_size < dvr::BufferHubDefs::kMetadataHeaderSize) { - ALOGE("DetachedBuffer::ImportGraphicBuffer: metadata too small: %zu", - metadata_buf_size); - return -EINVAL; - } - - // Import the buffer: We only need to hold on the native_handle_t here so that - // GraphicBuffer instance can be created in future. - buffer_handle_ = buffer_traits.take_buffer_handle(); - - // If all imports succeed, replace the previous buffer and id. - id_ = buffer_id; - buffer_state_bit_ = buffer_traits.buffer_state_bit(); - - // TODO(b/112012161) Set up shared fences. - ALOGD("DetachedBuffer::ImportGraphicBuffer: id=%d, buffer_state=%" PRIx64 ".", id(), - metadata_.metadata_header()->buffer_state.load(std::memory_order_acquire)); - return 0; +int BufferHubBuffer::ImportGraphicBuffer() { + ATRACE_CALL(); + + auto status = mClient.InvokeRemoteMethod(); + if (!status) { + ALOGE("BufferHubBuffer::BufferHubBuffer: Failed to import GraphicBuffer: %s", + status.GetErrorMessage().c_str()); + return -status.error(); + } + + BufferTraits bufferTraits = status.take(); + if (bufferTraits.id() < 0) { + ALOGE("BufferHubBuffer::BufferHubBuffer: Received an invalid id!"); + return -EIO; + } + + // Stash the buffer id to replace the value in mId. + const int bufferId = bufferTraits.id(); + + // Import the metadata. + mMetadata = BufferHubMetadata::Import(bufferTraits.take_metadata_handle()); + + if (!mMetadata.IsValid()) { + ALOGE("BufferHubBuffer::ImportGraphicBuffer: invalid metadata."); + return -ENOMEM; + } + + if (mMetadata.metadata_size() != bufferTraits.metadata_size()) { + ALOGE("BufferHubBuffer::ImportGraphicBuffer: metadata buffer too small: " + "%zu, expected: %" PRIu64 ".", + mMetadata.metadata_size(), bufferTraits.metadata_size()); + return -ENOMEM; + } + + size_t metadataSize = static_cast(bufferTraits.metadata_size()); + if (metadataSize < dvr::BufferHubDefs::kMetadataHeaderSize) { + ALOGE("BufferHubBuffer::ImportGraphicBuffer: metadata too small: %zu", metadataSize); + return -EINVAL; + } + + // Import the buffer: We only need to hold on the native_handle_t here so that + // GraphicBuffer instance can be created in future. + mBufferHandle = bufferTraits.take_buffer_handle(); + + // If all imports succeed, replace the previous buffer and id. + mId = bufferId; + mBfferStateBit = bufferTraits.buffer_state_bit(); + + // TODO(b/112012161) Set up shared fences. + ALOGD("BufferHubBuffer::ImportGraphicBuffer: id=%d, buffer_state=%" PRIx64 ".", id(), + mMetadata.metadata_header()->buffer_state.load(std::memory_order_acquire)); + return 0; } -int DetachedBuffer::Poll(int timeout_ms) { - ATRACE_NAME("DetachedBuffer::Poll"); - pollfd p = {client_.event_fd(), POLLIN, 0}; - return poll(&p, 1, timeout_ms); +int BufferHubBuffer::Poll(int timeoutMs) { + ATRACE_CALL(); + + pollfd p = {mClient.event_fd(), POLLIN, 0}; + return poll(&p, 1, timeoutMs); } -Status DetachedBuffer::Promote() { - // TODO(b/112338294) remove after migrate producer buffer to binder - ALOGW("DetachedBuffer::Promote: not supported operation during migration"); - return {}; - - ATRACE_NAME("DetachedBuffer::Promote"); - ALOGD("DetachedBuffer::Promote: id=%d.", id_); - - auto status_or_handle = - client_.InvokeRemoteMethod(); - if (status_or_handle.ok()) { - // Invalidate the buffer. - buffer_handle_ = {}; - } else { - ALOGE("DetachedBuffer::Promote: Failed to promote buffer (id=%d): %s.", id_, - status_or_handle.GetErrorMessage().c_str()); - } - return status_or_handle; +Status BufferHubBuffer::Promote() { + ATRACE_CALL(); + + // TODO(b/112338294) remove after migrate producer buffer to binder + ALOGW("BufferHubBuffer::Promote: not supported operation during migration"); + return {}; + + ALOGD("BufferHubBuffer::Promote: id=%d.", mId); + + auto statusOrHandle = mClient.InvokeRemoteMethod(); + if (statusOrHandle.ok()) { + // Invalidate the buffer. + mBufferHandle = {}; + } else { + ALOGE("BufferHubBuffer::Promote: Failed to promote buffer (id=%d): %s.", mId, + statusOrHandle.GetErrorMessage().c_str()); + } + return statusOrHandle; } -Status DetachedBuffer::Duplicate() { - ATRACE_NAME("DetachedBuffer::Duplicate"); - ALOGD("DetachedBuffer::Duplicate: id=%d.", id_); +Status BufferHubBuffer::Duplicate() { + ATRACE_CALL(); + ALOGD("BufferHubBuffer::Duplicate: id=%d.", mId); - auto status_or_handle = - client_.InvokeRemoteMethod(); + auto statusOrHandle = mClient.InvokeRemoteMethod(); - if (!status_or_handle.ok()) { - ALOGE("DetachedBuffer::Duplicate: Failed to duplicate buffer (id=%d): %s.", - id_, status_or_handle.GetErrorMessage().c_str()); - } - return status_or_handle; + if (!statusOrHandle.ok()) { + ALOGE("BufferHubBuffer::Duplicate: Failed to duplicate buffer (id=%d): %s.", mId, + statusOrHandle.GetErrorMessage().c_str()); + } + return statusOrHandle; } -} // namespace android +} // namespace android diff --git a/libs/ui/BufferHubMetadata.cpp b/libs/ui/BufferHubMetadata.cpp index 0e5fce0c46..b0c451061d 100644 --- a/libs/ui/BufferHubMetadata.cpp +++ b/libs/ui/BufferHubMetadata.cpp @@ -28,84 +28,78 @@ namespace { static const int kAshmemProt = PROT_READ | PROT_WRITE; -} // namespace +} // namespace using BufferHubDefs::kMetadataHeaderSize; using BufferHubDefs::MetadataHeader; /* static */ -BufferHubMetadata BufferHubMetadata::Create(size_t user_metadata_size) { - // The size the of metadata buffer is used as the "width" parameter during - // allocation. Thus it cannot overflow uint32_t. - if (user_metadata_size >= - (std::numeric_limits::max() - kMetadataHeaderSize)) { - ALOGE("BufferHubMetadata::Create: metadata size too big: %zu.", - user_metadata_size); - return {}; - } - - const size_t metadata_size = user_metadata_size + kMetadataHeaderSize; - int fd = ashmem_create_region(/*name=*/"BufferHubMetadata", metadata_size); - if (fd < 0) { - ALOGE("BufferHubMetadata::Create: failed to create ashmem region."); - return {}; - } - - // Hand over the ownership of the fd to a pdx::LocalHandle immediately after - // the successful return of ashmem_create_region. The ashmem_handle is going - // to own the fd and to prevent fd leaks during error handling. - pdx::LocalHandle ashmem_handle{fd}; - - if (ashmem_set_prot_region(ashmem_handle.Get(), kAshmemProt) != 0) { - ALOGE("BufferHubMetadata::Create: failed to set protect region."); - return {}; - } - - return BufferHubMetadata::Import(std::move(ashmem_handle)); +BufferHubMetadata BufferHubMetadata::Create(size_t userMetadataSize) { + // The size the of metadata buffer is used as the "width" parameter during allocation. Thus it + // cannot overflow uint32_t. + if (userMetadataSize >= (std::numeric_limits::max() - kMetadataHeaderSize)) { + ALOGE("BufferHubMetadata::Create: metadata size too big: %zu.", userMetadataSize); + return {}; + } + + const size_t metadataSize = userMetadataSize + kMetadataHeaderSize; + int fd = ashmem_create_region(/*name=*/"BufferHubMetadata", metadataSize); + if (fd < 0) { + ALOGE("BufferHubMetadata::Create: failed to create ashmem region."); + return {}; + } + + // Hand over the ownership of the fd to a pdx::LocalHandle immediately after the successful + // return of ashmem_create_region. The ashmemHandle is going to own the fd and to prevent fd + // leaks during error handling. + pdx::LocalHandle ashmemHandle{fd}; + + if (ashmem_set_prot_region(ashmemHandle.Get(), kAshmemProt) != 0) { + ALOGE("BufferHubMetadata::Create: failed to set protect region."); + return {}; + } + + return BufferHubMetadata::Import(std::move(ashmemHandle)); } /* static */ -BufferHubMetadata BufferHubMetadata::Import(pdx::LocalHandle ashmem_handle) { - if (!ashmem_valid(ashmem_handle.Get())) { - ALOGE("BufferHubMetadata::Import: invalid ashmem fd."); - return {}; - } - - size_t metadata_size = static_cast(ashmem_get_size_region(ashmem_handle.Get())); - size_t user_metadata_size = metadata_size - kMetadataHeaderSize; - - // Note that here the buffer state is mapped from shared memory as an atomic - // object. The std::atomic's constructor will not be called so that the - // original value stored in the memory region can be preserved. - auto metadata_header = static_cast( - mmap(nullptr, metadata_size, kAshmemProt, MAP_SHARED, ashmem_handle.Get(), - /*offset=*/0)); - if (metadata_header == nullptr) { - ALOGE("BufferHubMetadata::Import: failed to map region."); - return {}; - } - - return BufferHubMetadata(user_metadata_size, std::move(ashmem_handle), - metadata_header); +BufferHubMetadata BufferHubMetadata::Import(pdx::LocalHandle ashmemHandle) { + if (!ashmem_valid(ashmemHandle.Get())) { + ALOGE("BufferHubMetadata::Import: invalid ashmem fd."); + return {}; + } + + size_t metadataSize = static_cast(ashmem_get_size_region(ashmemHandle.Get())); + size_t userMetadataSize = metadataSize - kMetadataHeaderSize; + + // Note that here the buffer state is mapped from shared memory as an atomic object. The + // std::atomic's constructor will not be called so that the original value stored in the memory + // region can be preserved. + auto metadataHeader = static_cast(mmap(nullptr, metadataSize, kAshmemProt, + MAP_SHARED, ashmemHandle.Get(), + /*offset=*/0)); + if (metadataHeader == nullptr) { + ALOGE("BufferHubMetadata::Import: failed to map region."); + return {}; + } + + return BufferHubMetadata(userMetadataSize, std::move(ashmemHandle), metadataHeader); } -BufferHubMetadata::BufferHubMetadata(size_t user_metadata_size, - pdx::LocalHandle ashmem_handle, - MetadataHeader* metadata_header) - : user_metadata_size_(user_metadata_size), - ashmem_handle_(std::move(ashmem_handle)), - metadata_header_(metadata_header) {} +BufferHubMetadata::BufferHubMetadata(size_t userMetadataSize, pdx::LocalHandle ashmemHandle, + MetadataHeader* metadataHeader) + : mUserMetadataSize(userMetadataSize), + mAshmemHandle(std::move(ashmemHandle)), + mMetadataHeader(metadataHeader) {} BufferHubMetadata::~BufferHubMetadata() { - if (metadata_header_ != nullptr) { - int ret = munmap(metadata_header_, metadata_size()); - ALOGE_IF(ret != 0, - "BufferHubMetadata::~BufferHubMetadata: failed to unmap ashmem, " - "error=%d.", - errno); - metadata_header_ = nullptr; - } + if (mMetadataHeader != nullptr) { + int ret = munmap(mMetadataHeader, metadata_size()); + ALOGE_IF(ret != 0, + "BufferHubMetadata::~BufferHubMetadata: failed to unmap ashmem, error=%d.", errno); + mMetadataHeader = nullptr; + } } -} // namespace dvr -} // namespace android +} // namespace dvr +} // namespace android diff --git a/libs/ui/include/ui/BufferHubBuffer.h b/libs/ui/include/ui/BufferHubBuffer.h index 8cf1a6d805..e8ae244549 100644 --- a/libs/ui/include/ui/BufferHubBuffer.h +++ b/libs/ui/include/ui/BufferHubBuffer.h @@ -1,5 +1,21 @@ -#ifndef ANDROID_DVR_DETACHED_BUFFER_H_ -#define ANDROID_DVR_DETACHED_BUFFER_H_ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ANDROID_BUFFER_HUB_BUFFER_H_ +#define ANDROID_BUFFER_HUB_BUFFER_H_ // We would eliminate the clang warnings introduced by libdpx. // TODO(b/112338294): Remove those once BufferHub moved to use Binder @@ -20,7 +36,6 @@ #include #include #include -#include #pragma clang diagnostic pop #include @@ -28,114 +43,107 @@ namespace android { class BufferHubClient : public pdx::Client { - public: - BufferHubClient(); - virtual ~BufferHubClient(); - explicit BufferHubClient(pdx::LocalChannelHandle channel_handle); - - bool IsValid() const; - pdx::LocalChannelHandle TakeChannelHandle(); - - using pdx::Client::Close; - using pdx::Client::event_fd; - using pdx::Client::GetChannel; - using pdx::Client::InvokeRemoteMethod; +public: + BufferHubClient(); + virtual ~BufferHubClient(); + explicit BufferHubClient(pdx::LocalChannelHandle mChannelHandle); + + bool IsValid() const; + pdx::LocalChannelHandle TakeChannelHandle(); + + using pdx::Client::Close; + using pdx::Client::event_fd; + using pdx::Client::GetChannel; + using pdx::Client::InvokeRemoteMethod; }; -class DetachedBuffer { - public: - // Allocates a standalone DetachedBuffer not associated with any producer - // consumer set. - static std::unique_ptr Create(uint32_t width, uint32_t height, - uint32_t layer_count, - uint32_t format, uint64_t usage, - size_t user_metadata_size) { - return std::unique_ptr(new DetachedBuffer( - width, height, layer_count, format, usage, user_metadata_size)); - } - - // Imports the given channel handle to a DetachedBuffer, taking ownership. - static std::unique_ptr Import( - pdx::LocalChannelHandle channel_handle) { - return std::unique_ptr( - new DetachedBuffer(std::move(channel_handle))); - } - - DetachedBuffer(const DetachedBuffer&) = delete; - void operator=(const DetachedBuffer&) = delete; - - // Gets ID of the buffer client. All DetachedBuffer clients derived from the - // same buffer in bufferhubd share the same buffer id. - int id() const { return id_; } - - const native_handle_t* DuplicateHandle() { - return buffer_handle_.DuplicateHandle(); - } - - // Returns the current value of MetadataHeader::buffer_state. - uint64_t buffer_state() { - return metadata_.metadata_header()->buffer_state.load( - std::memory_order_acquire); - } - - // A state mask which is unique to a buffer hub client among all its siblings - // sharing the same concrete graphic buffer. - uint64_t buffer_state_bit() const { return buffer_state_bit_; } - - size_t user_metadata_size() const { return metadata_.user_metadata_size(); } - - // Returns true if the buffer holds an open PDX channels towards bufferhubd. - bool IsConnected() const { return client_.IsValid(); } - - // Returns true if the buffer holds an valid native buffer handle that's - // availble for the client to read from and/or write into. - bool IsValid() const { return buffer_handle_.IsValid(); } - - // Returns the event mask for all the events that are pending on this buffer - // (see sys/poll.h for all possible bits). - pdx::Status GetEventMask(int events) { - if (auto* channel = client_.GetChannel()) { - return channel->GetEventMask(events); - } else { - return pdx::ErrorStatus(EINVAL); +class BufferHubBuffer { +public: + // Allocates a standalone BufferHubBuffer not associated with any producer consumer set. + static std::unique_ptr Create(uint32_t width, uint32_t height, + uint32_t layerCount, uint32_t format, + uint64_t usage, size_t mUserMetadataSize) { + return std::unique_ptr( + new BufferHubBuffer(width, height, layerCount, format, usage, mUserMetadataSize)); + } + + // Imports the given channel handle to a BufferHubBuffer, taking ownership. + static std::unique_ptr Import(pdx::LocalChannelHandle mChannelHandle) { + return std::unique_ptr(new BufferHubBuffer(std::move(mChannelHandle))); + } + + BufferHubBuffer(const BufferHubBuffer&) = delete; + void operator=(const BufferHubBuffer&) = delete; + + // Gets ID of the buffer client. All BufferHubBuffer clients derived from the same buffer in + // bufferhubd share the same buffer id. + int id() const { return mId; } + + const native_handle_t* DuplicateHandle() { return mBufferHandle.DuplicateHandle(); } + + // Returns the current value of MetadataHeader::buffer_state. + uint64_t buffer_state() { + return mMetadata.metadata_header()->buffer_state.load(std::memory_order_acquire); + } + + // A state mask which is unique to a buffer hub client among all its siblings sharing the same + // concrete graphic buffer. + uint64_t buffer_state_bit() const { return mBfferStateBit; } + + size_t user_metadata_size() const { return mMetadata.user_metadata_size(); } + + // Returns true if the buffer holds an open PDX channels towards bufferhubd. + bool IsConnected() const { return mClient.IsValid(); } + + // Returns true if the buffer holds an valid native buffer handle that's availble for the client + // to read from and/or write into. + bool IsValid() const { return mBufferHandle.IsValid(); } + + // Returns the event mask for all the events that are pending on this buffer (see sys/poll.h for + // all possible bits). + pdx::Status GetEventMask(int events) { + if (auto* channel = mClient.GetChannel()) { + return channel->GetEventMask(events); + } else { + return pdx::ErrorStatus(EINVAL); + } } - } - // Polls the fd for |timeout_ms| milliseconds (-1 for infinity). - int Poll(int timeout_ms); + // Polls the fd for |timeoutMs| milliseconds (-1 for infinity). + int Poll(int timeoutMs); - // Promotes a DetachedBuffer to become a ProducerBuffer. Once promoted the - // DetachedBuffer channel will be closed automatically on successful IPC - // return. Further IPCs towards this channel will return error. - pdx::Status Promote(); + // Promotes a BufferHubBuffer to become a ProducerBuffer. Once promoted the BufferHubBuffer + // channel will be closed automatically on successful IPC return. Further IPCs towards this + // channel will return error. + pdx::Status Promote(); - // Creates a DetachedBuffer client from an existing one. The new client will - // share the same underlying gralloc buffer and ashmem region for metadata. - pdx::Status Duplicate(); + // Creates a BufferHubBuffer client from an existing one. The new client will + // share the same underlying gralloc buffer and ashmem region for metadata. + pdx::Status Duplicate(); - private: - DetachedBuffer(uint32_t width, uint32_t height, uint32_t layer_count, - uint32_t format, uint64_t usage, size_t user_metadata_size); +private: + BufferHubBuffer(uint32_t width, uint32_t height, uint32_t layerCount, uint32_t format, + uint64_t usage, size_t mUserMetadataSize); - DetachedBuffer(pdx::LocalChannelHandle channel_handle); + BufferHubBuffer(pdx::LocalChannelHandle mChannelHandle); - int ImportGraphicBuffer(); + int ImportGraphicBuffer(); - // Global id for the buffer that is consistent across processes. - int id_; - uint64_t buffer_state_bit_; + // Global id for the buffer that is consistent across processes. + int mId; + uint64_t mBfferStateBit; - // Wrapps the gralloc buffer handle of this buffer. - dvr::NativeHandleWrapper buffer_handle_; + // Wrapps the gralloc buffer handle of this buffer. + dvr::NativeHandleWrapper mBufferHandle; - // An ashmem-based metadata object. The same shared memory are mapped to the - // bufferhubd daemon and all buffer clients. - dvr::BufferHubMetadata metadata_; + // An ashmem-based metadata object. The same shared memory are mapped to the + // bufferhubd daemon and all buffer clients. + dvr::BufferHubMetadata mMetadata; - // PDX backend. - BufferHubClient client_; + // PDX backend. + BufferHubClient mClient; }; -} // namespace android +} // namespace android -#endif // ANDROID_DVR_DETACHED_BUFFER_H_ +#endif // ANDROID_BUFFER_HUB_BUFFER_H_ diff --git a/libs/ui/include/ui/BufferHubMetadata.h b/libs/ui/include/ui/BufferHubMetadata.h index 52d156dbd5..46adc6a02e 100644 --- a/libs/ui/include/ui/BufferHubMetadata.h +++ b/libs/ui/include/ui/BufferHubMetadata.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef ANDROID_DVR_BUFFER_HUB_METADATA_H_ -#define ANDROID_DVR_BUFFER_HUB_METADATA_H_ +#ifndef ANDROID_BUFFER_HUB_METADATA_H_ +#define ANDROID_BUFFER_HUB_METADATA_H_ // We would eliminate the clang warnings introduced by libdpx. // TODO(b/112338294): Remove those once BufferHub moved to use Binder @@ -41,70 +41,66 @@ namespace android { namespace dvr { class BufferHubMetadata { - public: - // Creates a new BufferHubMetadata backed by an ashmem region. - // - // @param user_metadata_size Size in bytes of the user defined metadata. The - // entire metadata shared memory region to be allocated is the size of - // canonical BufferHubDefs::MetadataHeader plus user_metadata_size. - static BufferHubMetadata Create(size_t user_metadata_size); - - // Imports an existing BufferHubMetadata from an ashmem FD. - // - // TODO(b/112338294): Refactor BufferHub to use Binder as its internal IPC - // backend instead of UDS. - // - // @param ashmem_handle Ashmem file handle representing an ashmem region. - static BufferHubMetadata Import(pdx::LocalHandle ashmem_handle); - - BufferHubMetadata() = default; - - BufferHubMetadata(BufferHubMetadata&& other) { *this = std::move(other); } - - ~BufferHubMetadata(); - - BufferHubMetadata& operator=(BufferHubMetadata&& other) { - if (this != &other) { - user_metadata_size_ = other.user_metadata_size_; - other.user_metadata_size_ = 0; - - ashmem_handle_ = std::move(other.ashmem_handle_); - - // The old raw metadata_header_ pointer must be cleared, otherwise the - // destructor will automatically mummap() the shared memory. - metadata_header_ = other.metadata_header_; - other.metadata_header_ = nullptr; +public: + // Creates a new BufferHubMetadata backed by an ashmem region. + // + // @param userMetadataSize Size in bytes of the user defined metadata. The entire metadata + // shared memory region to be allocated is the size of canonical + // BufferHubDefs::MetadataHeader plus userMetadataSize. + static BufferHubMetadata Create(size_t userMetadataSize); + + // Imports an existing BufferHubMetadata from an ashmem FD. + // + // TODO(b/112338294): Refactor BufferHub to use Binder as its internal IPC backend instead of + // UDS. + // + // @param ashmemHandle Ashmem file handle representing an ashmem region. + static BufferHubMetadata Import(pdx::LocalHandle ashmemHandle); + + BufferHubMetadata() = default; + + BufferHubMetadata(BufferHubMetadata&& other) { *this = std::move(other); } + + ~BufferHubMetadata(); + + BufferHubMetadata& operator=(BufferHubMetadata&& other) { + if (this != &other) { + mUserMetadataSize = other.mUserMetadataSize; + other.mUserMetadataSize = 0; + + mAshmemHandle = std::move(other.mAshmemHandle); + + // The old raw mMetadataHeader pointer must be cleared, otherwise the destructor will + // automatically mummap() the shared memory. + mMetadataHeader = other.mMetadataHeader; + other.mMetadataHeader = nullptr; + } + return *this; } - return *this; - } - // Returns true if the metadata is valid, i.e. the metadata has a valid ashmem - // fd and the ashmem has been mapped into virtual address space. - bool IsValid() const { - return ashmem_handle_.IsValid() && metadata_header_ != nullptr; - } + // Returns true if the metadata is valid, i.e. the metadata has a valid ashmem fd and the ashmem + // has been mapped into virtual address space. + bool IsValid() const { return mAshmemHandle.IsValid() && mMetadataHeader != nullptr; } - size_t user_metadata_size() const { return user_metadata_size_; } - size_t metadata_size() const { - return user_metadata_size_ + BufferHubDefs::kMetadataHeaderSize; - } + size_t user_metadata_size() const { return mUserMetadataSize; } + size_t metadata_size() const { return mUserMetadataSize + BufferHubDefs::kMetadataHeaderSize; } - const pdx::LocalHandle& ashmem_handle() const { return ashmem_handle_; } - BufferHubDefs::MetadataHeader* metadata_header() { return metadata_header_; } + const pdx::LocalHandle& ashmem_handle() const { return mAshmemHandle; } + BufferHubDefs::MetadataHeader* metadata_header() { return mMetadataHeader; } - private: - BufferHubMetadata(size_t user_metadata_size, pdx::LocalHandle ashmem_handle, - BufferHubDefs::MetadataHeader* metadata_header); +private: + BufferHubMetadata(size_t userMetadataSize, pdx::LocalHandle ashmemHandle, + BufferHubDefs::MetadataHeader* metadataHeader); - BufferHubMetadata(const BufferHubMetadata&) = delete; - void operator=(const BufferHubMetadata&) = delete; + BufferHubMetadata(const BufferHubMetadata&) = delete; + void operator=(const BufferHubMetadata&) = delete; - size_t user_metadata_size_ = 0; - pdx::LocalHandle ashmem_handle_; - BufferHubDefs::MetadataHeader* metadata_header_ = nullptr; + size_t mUserMetadataSize = 0; + pdx::LocalHandle mAshmemHandle; + BufferHubDefs::MetadataHeader* mMetadataHeader = nullptr; }; -} // namespace dvr -} // namespace android +} // namespace dvr +} // namespace android -#endif // ANDROID_DVR_BUFFER_HUB_METADATA_H_ +#endif // ANDROID_BUFFER_HUB_METADATA_H_ diff --git a/libs/vr/libbufferhub/buffer_hub-test.cpp b/libs/vr/libbufferhub/buffer_hub-test.cpp index 08c6bededc..b477f1a571 100644 --- a/libs/vr/libbufferhub/buffer_hub-test.cpp +++ b/libs/vr/libbufferhub/buffer_hub-test.cpp @@ -19,7 +19,7 @@ return result; \ })() -using android::DetachedBuffer; +using android::BufferHubBuffer; using android::GraphicBuffer; using android::sp; using android::dvr::ConsumerBuffer; @@ -784,8 +784,9 @@ TEST_F(LibBufferHubTest, TestDetachBufferFromProducer) { // is gone. EXPECT_EQ(s3.error(), EPIPE); - // Detached buffer handle can be use to construct a new DetachedBuffer object. - auto d = DetachedBuffer::Import(std::move(handle)); + // Detached buffer handle can be use to construct a new BufferHubBuffer + // object. + auto d = BufferHubBuffer::Import(std::move(handle)); EXPECT_FALSE(handle.valid()); EXPECT_TRUE(d->IsConnected()); EXPECT_TRUE(d->IsValid()); @@ -793,17 +794,17 @@ TEST_F(LibBufferHubTest, TestDetachBufferFromProducer) { EXPECT_EQ(d->id(), p_id); } -TEST_F(LibBufferHubTest, TestCreateDetachedBufferFails) { +TEST_F(LibBufferHubTest, TestCreateBufferHubBufferFails) { // Buffer Creation will fail: BLOB format requires height to be 1. - auto b1 = DetachedBuffer::Create(kWidth, /*height=2*/ 2, kLayerCount, - /*format=*/HAL_PIXEL_FORMAT_BLOB, kUsage, - kUserMetadataSize); + auto b1 = BufferHubBuffer::Create(kWidth, /*height=2*/ 2, kLayerCount, + /*format=*/HAL_PIXEL_FORMAT_BLOB, kUsage, + kUserMetadataSize); EXPECT_FALSE(b1->IsConnected()); EXPECT_FALSE(b1->IsValid()); // Buffer Creation will fail: user metadata size too large. - auto b2 = DetachedBuffer::Create( + auto b2 = BufferHubBuffer::Create( kWidth, kHeight, kLayerCount, kFormat, kUsage, /*user_metadata_size=*/std::numeric_limits::max()); @@ -811,7 +812,7 @@ TEST_F(LibBufferHubTest, TestCreateDetachedBufferFails) { EXPECT_FALSE(b2->IsValid()); // Buffer Creation will fail: user metadata size too large. - auto b3 = DetachedBuffer::Create( + auto b3 = BufferHubBuffer::Create( kWidth, kHeight, kLayerCount, kFormat, kUsage, /*user_metadata_size=*/std::numeric_limits::max() - kMetadataHeaderSize); @@ -820,20 +821,20 @@ TEST_F(LibBufferHubTest, TestCreateDetachedBufferFails) { EXPECT_FALSE(b3->IsValid()); } -TEST_F(LibBufferHubTest, TestCreateDetachedBuffer) { - auto b1 = DetachedBuffer::Create(kWidth, kHeight, kLayerCount, kFormat, - kUsage, kUserMetadataSize); +TEST_F(LibBufferHubTest, TestCreateBufferHubBuffer) { + auto b1 = BufferHubBuffer::Create(kWidth, kHeight, kLayerCount, kFormat, + kUsage, kUserMetadataSize); EXPECT_TRUE(b1->IsConnected()); EXPECT_TRUE(b1->IsValid()); EXPECT_NE(b1->id(), 0); } -TEST_F(LibBufferHubTest, TestPromoteDetachedBuffer) { +TEST_F(LibBufferHubTest, TestPromoteBufferHubBuffer) { // TODO(b/112338294) rewrite test after migration return; - auto b1 = DetachedBuffer::Create(kWidth, kHeight, kLayerCount, kFormat, - kUsage, kUserMetadataSize); + auto b1 = BufferHubBuffer::Create(kWidth, kHeight, kLayerCount, kFormat, + kUsage, kUserMetadataSize); int b1_id = b1->id(); EXPECT_TRUE(b1->IsValid()); @@ -879,8 +880,9 @@ TEST_F(LibBufferHubTest, TestDetachThenPromote) { LocalChannelHandle h1 = status_or_handle.take(); EXPECT_TRUE(h1.valid()); - // Detached buffer handle can be use to construct a new DetachedBuffer object. - auto b1 = DetachedBuffer::Import(std::move(h1)); + // Detached buffer handle can be use to construct a new BufferHubBuffer + // object. + auto b1 = BufferHubBuffer::Import(std::move(h1)); EXPECT_FALSE(h1.valid()); EXPECT_TRUE(b1->IsValid()); int b1_id = b1->id(); @@ -907,9 +909,9 @@ TEST_F(LibBufferHubTest, TestDetachThenPromote) { EXPECT_TRUE(IsBufferGained(p2->buffer_state())); } -TEST_F(LibBufferHubTest, TestDuplicateDetachedBuffer) { - auto b1 = DetachedBuffer::Create(kWidth, kHeight, kLayerCount, kFormat, - kUsage, kUserMetadataSize); +TEST_F(LibBufferHubTest, TestDuplicateBufferHubBuffer) { + auto b1 = BufferHubBuffer::Create(kWidth, kHeight, kLayerCount, kFormat, + kUsage, kUserMetadataSize); int b1_id = b1->id(); EXPECT_TRUE(b1->IsValid()); EXPECT_EQ(b1->user_metadata_size(), kUserMetadataSize); @@ -925,7 +927,7 @@ TEST_F(LibBufferHubTest, TestDuplicateDetachedBuffer) { LocalChannelHandle h2 = status_or_handle.take(); EXPECT_TRUE(h2.valid()); - std::unique_ptr b2 = DetachedBuffer::Import(std::move(h2)); + std::unique_ptr b2 = BufferHubBuffer::Import(std::move(h2)); EXPECT_FALSE(h2.valid()); ASSERT_TRUE(b2 != nullptr); EXPECT_TRUE(b2->IsValid()); -- cgit v1.2.3-59-g8ed1b From 1850da231eafb185add6db88251343a6e6e333db Mon Sep 17 00:00:00 2001 From: Jiwen 'Steve' Cai Date: Mon, 15 Oct 2018 21:29:14 -0700 Subject: BufferHub{Buffer,Metadata}: Some other minor changes 1/ Move BufferHubMetadata out of android::dvr namespace 2/ Fix some misnamed variables Bug: 111976433 Test: BufferHubBuffer_test Change-Id: I06ec1c3cc4831c865b672680b49adb006d6e1835 --- libs/ui/BufferHubBuffer.cpp | 3 +-- libs/ui/BufferHubMetadata.cpp | 6 ++---- libs/ui/include/ui/BufferHubBuffer.h | 8 ++++---- libs/ui/include/ui/BufferHubMetadata.h | 12 ++++++------ 4 files changed, 13 insertions(+), 16 deletions(-) (limited to 'libs/ui/BufferHubBuffer.cpp') diff --git a/libs/ui/BufferHubBuffer.cpp b/libs/ui/BufferHubBuffer.cpp index 606386c740..2696c6c213 100644 --- a/libs/ui/BufferHubBuffer.cpp +++ b/libs/ui/BufferHubBuffer.cpp @@ -41,7 +41,6 @@ #include -using android::dvr::BufferHubMetadata; using android::dvr::BufferTraits; using android::dvr::DetachedBufferRPC; using android::dvr::NativeHandleWrapper; @@ -160,7 +159,7 @@ int BufferHubBuffer::ImportGraphicBuffer() { // If all imports succeed, replace the previous buffer and id. mId = bufferId; - mBfferStateBit = bufferTraits.buffer_state_bit(); + mBufferStateBit = bufferTraits.buffer_state_bit(); // TODO(b/112012161) Set up shared fences. ALOGD("BufferHubBuffer::ImportGraphicBuffer: id=%d, buffer_state=%" PRIx64 ".", id(), diff --git a/libs/ui/BufferHubMetadata.cpp b/libs/ui/BufferHubMetadata.cpp index b0c451061d..1e08ed1388 100644 --- a/libs/ui/BufferHubMetadata.cpp +++ b/libs/ui/BufferHubMetadata.cpp @@ -22,7 +22,6 @@ #include namespace android { -namespace dvr { namespace { @@ -30,8 +29,8 @@ static const int kAshmemProt = PROT_READ | PROT_WRITE; } // namespace -using BufferHubDefs::kMetadataHeaderSize; -using BufferHubDefs::MetadataHeader; +using dvr::BufferHubDefs::kMetadataHeaderSize; +using dvr::BufferHubDefs::MetadataHeader; /* static */ BufferHubMetadata BufferHubMetadata::Create(size_t userMetadataSize) { @@ -101,5 +100,4 @@ BufferHubMetadata::~BufferHubMetadata() { } } -} // namespace dvr } // namespace android diff --git a/libs/ui/include/ui/BufferHubBuffer.h b/libs/ui/include/ui/BufferHubBuffer.h index e8ae244549..d33be8cf87 100644 --- a/libs/ui/include/ui/BufferHubBuffer.h +++ b/libs/ui/include/ui/BufferHubBuffer.h @@ -88,7 +88,7 @@ public: // A state mask which is unique to a buffer hub client among all its siblings sharing the same // concrete graphic buffer. - uint64_t buffer_state_bit() const { return mBfferStateBit; } + uint64_t buffer_state_bit() const { return mBufferStateBit; } size_t user_metadata_size() const { return mMetadata.user_metadata_size(); } @@ -130,15 +130,15 @@ private: int ImportGraphicBuffer(); // Global id for the buffer that is consistent across processes. - int mId; - uint64_t mBfferStateBit; + int mId = -1; + uint64_t mBufferStateBit = 0; // Wrapps the gralloc buffer handle of this buffer. dvr::NativeHandleWrapper mBufferHandle; // An ashmem-based metadata object. The same shared memory are mapped to the // bufferhubd daemon and all buffer clients. - dvr::BufferHubMetadata mMetadata; + BufferHubMetadata mMetadata; // PDX backend. BufferHubClient mClient; diff --git a/libs/ui/include/ui/BufferHubMetadata.h b/libs/ui/include/ui/BufferHubMetadata.h index 46adc6a02e..94a9000a32 100644 --- a/libs/ui/include/ui/BufferHubMetadata.h +++ b/libs/ui/include/ui/BufferHubMetadata.h @@ -38,7 +38,6 @@ #pragma clang diagnostic pop namespace android { -namespace dvr { class BufferHubMetadata { public: @@ -83,24 +82,25 @@ public: bool IsValid() const { return mAshmemHandle.IsValid() && mMetadataHeader != nullptr; } size_t user_metadata_size() const { return mUserMetadataSize; } - size_t metadata_size() const { return mUserMetadataSize + BufferHubDefs::kMetadataHeaderSize; } + size_t metadata_size() const { + return mUserMetadataSize + dvr::BufferHubDefs::kMetadataHeaderSize; + } const pdx::LocalHandle& ashmem_handle() const { return mAshmemHandle; } - BufferHubDefs::MetadataHeader* metadata_header() { return mMetadataHeader; } + dvr::BufferHubDefs::MetadataHeader* metadata_header() { return mMetadataHeader; } private: BufferHubMetadata(size_t userMetadataSize, pdx::LocalHandle ashmemHandle, - BufferHubDefs::MetadataHeader* metadataHeader); + dvr::BufferHubDefs::MetadataHeader* metadataHeader); BufferHubMetadata(const BufferHubMetadata&) = delete; void operator=(const BufferHubMetadata&) = delete; size_t mUserMetadataSize = 0; pdx::LocalHandle mAshmemHandle; - BufferHubDefs::MetadataHeader* mMetadataHeader = nullptr; + dvr::BufferHubDefs::MetadataHeader* mMetadataHeader = nullptr; }; -} // namespace dvr } // namespace android #endif // ANDROID_BUFFER_HUB_METADATA_H_ -- cgit v1.2.3-59-g8ed1b From b61df91163d6e8a8639dfe81201d72d96b16acd4 Mon Sep 17 00:00:00 2001 From: Tianyu Date: Tue, 16 Oct 2018 14:55:39 -0700 Subject: Remove the functionality of promoting a BufferHubBuffer to ProducerBuffer. Test: buffer_hub-test buffer_hub_queue-test dvr_api-test dvr_buffer_queue-test on marlin-eng Bug: 77153033 Change-Id: I155fa5c5740243d84207f37e6a2fe37d6331628f --- libs/gui/BufferHubProducer.cpp | 13 +-- libs/ui/BufferHubBuffer.cpp | 20 ---- libs/ui/include/ui/BufferHubBuffer.h | 5 - libs/ui/tests/BufferHubBuffer_test.cpp | 6 -- libs/vr/libbufferhub/buffer_hub-test.cpp | 105 +++++++++++++-------- .../include/private/dvr/buffer_hub_defs.h | 8 +- services/vr/bufferhubd/buffer_channel.cpp | 69 -------------- services/vr/bufferhubd/buffer_hub.cpp | 8 -- .../include/private/dvr/buffer_channel.h | 1 - 9 files changed, 66 insertions(+), 169 deletions(-) (limited to 'libs/ui/BufferHubBuffer.cpp') diff --git a/libs/gui/BufferHubProducer.cpp b/libs/gui/BufferHubProducer.cpp index 3dac037880..1a7c2d3826 100644 --- a/libs/gui/BufferHubProducer.cpp +++ b/libs/gui/BufferHubProducer.cpp @@ -400,19 +400,8 @@ status_t BufferHubProducer::attachBuffer(int* out_slot, const sp& ALOGE("attachBuffer: DetachedBufferHandle cannot be NULL."); return BAD_VALUE; } - auto detached_buffer = BufferHubBuffer::Import(std::move(detached_handle->handle())); - if (detached_buffer == nullptr) { - ALOGE("attachBuffer: BufferHubBuffer cannot be NULL."); - return BAD_VALUE; - } - auto status_or_handle = detached_buffer->Promote(); - if (!status_or_handle.ok()) { - ALOGE("attachBuffer: Failed to promote a BufferHubBuffer into a BufferProducer, error=%d.", - status_or_handle.error()); - return BAD_VALUE; - } std::shared_ptr buffer_producer = - BufferProducer::Import(status_or_handle.take()); + BufferProducer::Import(std::move(detached_handle->handle())); if (buffer_producer == nullptr) { ALOGE("attachBuffer: Failed to import BufferProducer."); return BAD_VALUE; diff --git a/libs/ui/BufferHubBuffer.cpp b/libs/ui/BufferHubBuffer.cpp index 2696c6c213..44a947e5e3 100644 --- a/libs/ui/BufferHubBuffer.cpp +++ b/libs/ui/BufferHubBuffer.cpp @@ -174,26 +174,6 @@ int BufferHubBuffer::Poll(int timeoutMs) { return poll(&p, 1, timeoutMs); } -Status BufferHubBuffer::Promote() { - ATRACE_CALL(); - - // TODO(b/112338294) remove after migrate producer buffer to binder - ALOGW("BufferHubBuffer::Promote: not supported operation during migration"); - return {}; - - ALOGD("BufferHubBuffer::Promote: id=%d.", mId); - - auto statusOrHandle = mClient.InvokeRemoteMethod(); - if (statusOrHandle.ok()) { - // Invalidate the buffer. - mBufferHandle = {}; - } else { - ALOGE("BufferHubBuffer::Promote: Failed to promote buffer (id=%d): %s.", mId, - statusOrHandle.GetErrorMessage().c_str()); - } - return statusOrHandle; -} - Status BufferHubBuffer::Duplicate() { ATRACE_CALL(); ALOGD("BufferHubBuffer::Duplicate: id=%d.", mId); diff --git a/libs/ui/include/ui/BufferHubBuffer.h b/libs/ui/include/ui/BufferHubBuffer.h index d33be8cf87..cefe5b3f3f 100644 --- a/libs/ui/include/ui/BufferHubBuffer.h +++ b/libs/ui/include/ui/BufferHubBuffer.h @@ -112,11 +112,6 @@ public: // Polls the fd for |timeoutMs| milliseconds (-1 for infinity). int Poll(int timeoutMs); - // Promotes a BufferHubBuffer to become a ProducerBuffer. Once promoted the BufferHubBuffer - // channel will be closed automatically on successful IPC return. Further IPCs towards this - // channel will return error. - pdx::Status Promote(); - // Creates a BufferHubBuffer client from an existing one. The new client will // share the same underlying gralloc buffer and ashmem region for metadata. pdx::Status Duplicate(); diff --git a/libs/ui/tests/BufferHubBuffer_test.cpp b/libs/ui/tests/BufferHubBuffer_test.cpp index e71c99cc21..be06ad281a 100644 --- a/libs/ui/tests/BufferHubBuffer_test.cpp +++ b/libs/ui/tests/BufferHubBuffer_test.cpp @@ -113,12 +113,6 @@ TEST_F(BufferHubBufferTest, DuplicateBufferHubBuffer) { // TODO(b/112338294): rewrite test after migration return; - - // Promote the detached buffer should fail as b1 is no longer the exclusive - // owner of the buffer.. - statusOrHandle = b1->Promote(); - EXPECT_FALSE(statusOrHandle.ok()); - EXPECT_EQ(statusOrHandle.error(), EINVAL); } } // namespace android diff --git a/libs/vr/libbufferhub/buffer_hub-test.cpp b/libs/vr/libbufferhub/buffer_hub-test.cpp index 571d3823cd..8c6e7e2495 100644 --- a/libs/vr/libbufferhub/buffer_hub-test.cpp +++ b/libs/vr/libbufferhub/buffer_hub-test.cpp @@ -794,43 +794,42 @@ TEST_F(LibBufferHubTest, TestDetachBufferFromProducer) { EXPECT_EQ(d->id(), p_id); } -TEST_F(LibBufferHubTest, TestPromoteBufferHubBuffer) { - // TODO(b/112338294) rewrite test after migration - return; +TEST_F(LibBufferHubTest, TestCreateBufferHubBufferFails) { + // Buffer Creation will fail: BLOB format requires height to be 1. + auto b1 = BufferHubBuffer::Create(kWidth, /*height=2*/ 2, kLayerCount, + /*format=*/HAL_PIXEL_FORMAT_BLOB, kUsage, + kUserMetadataSize); - auto b1 = BufferHubBuffer::Create(kWidth, kHeight, kLayerCount, kFormat, - kUsage, kUserMetadataSize); - int b1_id = b1->id(); - EXPECT_TRUE(b1->IsValid()); + EXPECT_FALSE(b1->IsConnected()); + EXPECT_FALSE(b1->IsValid()); - auto status_or_handle = b1->Promote(); - EXPECT_TRUE(status_or_handle); + // Buffer Creation will fail: user metadata size too large. + auto b2 = BufferHubBuffer::Create( + kWidth, kHeight, kLayerCount, kFormat, kUsage, + /*user_metadata_size=*/std::numeric_limits::max()); - // The detached buffer should have hangup. - EXPECT_GT(RETRY_EINTR(b1->Poll(kPollTimeoutMs)), 0); - auto status_or_int = b1->GetEventMask(POLLHUP); - EXPECT_TRUE(status_or_int.ok()); - EXPECT_EQ(status_or_int.get(), POLLHUP); + EXPECT_FALSE(b2->IsConnected()); + EXPECT_FALSE(b2->IsValid()); - // The buffer client is still considered as connected but invalid. - EXPECT_TRUE(b1->IsConnected()); - EXPECT_FALSE(b1->IsValid()); - - // Gets the channel handle for the producer. - LocalChannelHandle h1 = status_or_handle.take(); - EXPECT_TRUE(h1.valid()); + // Buffer Creation will fail: user metadata size too large. + auto b3 = BufferHubBuffer::Create( + kWidth, kHeight, kLayerCount, kFormat, kUsage, + /*user_metadata_size=*/std::numeric_limits::max() - + kMetadataHeaderSize); - std::unique_ptr p1 = ProducerBuffer::Import(std::move(h1)); - EXPECT_FALSE(h1.valid()); - ASSERT_TRUE(p1 != nullptr); - int p1_id = p1->id(); + EXPECT_FALSE(b3->IsConnected()); + EXPECT_FALSE(b3->IsValid()); +} - // A newly promoted ProducerBuffer should inherit the same buffer id. - EXPECT_EQ(b1_id, p1_id); - EXPECT_TRUE(IsBufferGained(p1->buffer_state())); +TEST_F(LibBufferHubTest, TestCreateBufferHubBuffer) { + auto b1 = BufferHubBuffer::Create(kWidth, kHeight, kLayerCount, kFormat, + kUsage, kUserMetadataSize); + EXPECT_TRUE(b1->IsConnected()); + EXPECT_TRUE(b1->IsValid()); + EXPECT_NE(b1->id(), 0); } -TEST_F(LibBufferHubTest, TestDetachThenPromote) { +TEST_F(LibBufferHubTest, TestDetach) { // TODO(b/112338294) rewrite test after migration return; @@ -852,24 +851,48 @@ TEST_F(LibBufferHubTest, TestDetachThenPromote) { EXPECT_TRUE(b1->IsValid()); int b1_id = b1->id(); EXPECT_EQ(b1_id, p1_id); +} - // Promote the detached buffer. - status_or_handle = b1->Promote(); - // The buffer client is still considered as connected but invalid. +TEST_F(LibBufferHubTest, TestDuplicateBufferHubBuffer) { + auto b1 = BufferHubBuffer::Create(kWidth, kHeight, kLayerCount, kFormat, + kUsage, kUserMetadataSize); + int b1_id = b1->id(); + EXPECT_TRUE(b1->IsValid()); + EXPECT_EQ(b1->user_metadata_size(), kUserMetadataSize); + + auto status_or_handle = b1->Duplicate(); + EXPECT_TRUE(status_or_handle); + + // The detached buffer should still be valid. EXPECT_TRUE(b1->IsConnected()); - EXPECT_FALSE(b1->IsValid()); - EXPECT_TRUE(status_or_handle.ok()); + EXPECT_TRUE(b1->IsValid()); - // Gets the channel handle for the producer. + // Gets the channel handle for the duplicated buffer. LocalChannelHandle h2 = status_or_handle.take(); EXPECT_TRUE(h2.valid()); - std::unique_ptr p2 = ProducerBuffer::Import(std::move(h2)); + std::unique_ptr b2 = BufferHubBuffer::Import(std::move(h2)); EXPECT_FALSE(h2.valid()); - ASSERT_TRUE(p2 != nullptr); - int p2_id = p2->id(); + ASSERT_TRUE(b2 != nullptr); + EXPECT_TRUE(b2->IsValid()); + EXPECT_EQ(b2->user_metadata_size(), kUserMetadataSize); + + int b2_id = b2->id(); + + // These two buffer instances are based on the same physical buffer under the + // hood, so they should share the same id. + EXPECT_EQ(b1_id, b2_id); + // We use buffer_state_bit() to tell those two instances apart. + EXPECT_NE(b1->buffer_state_bit(), b2->buffer_state_bit()); + EXPECT_NE(b1->buffer_state_bit(), 0ULL); + EXPECT_NE(b2->buffer_state_bit(), 0ULL); + EXPECT_NE(b1->buffer_state_bit(), kProducerStateBit); + EXPECT_NE(b2->buffer_state_bit(), kProducerStateBit); + + // Both buffer instances should be in gained state. + EXPECT_TRUE(IsBufferGained(b1->buffer_state())); + EXPECT_TRUE(IsBufferGained(b2->buffer_state())); - // A newly promoted ProducerBuffer should inherit the same buffer id. - EXPECT_EQ(b1_id, p2_id); - EXPECT_TRUE(IsBufferGained(p2->buffer_state())); + // TODO(b/112338294) rewrite test after migration + return; } diff --git a/libs/vr/libbufferhub/include/private/dvr/buffer_hub_defs.h b/libs/vr/libbufferhub/include/private/dvr/buffer_hub_defs.h index 6d28d41b11..49f9c3ebd6 100644 --- a/libs/vr/libbufferhub/include/private/dvr/buffer_hub_defs.h +++ b/libs/vr/libbufferhub/include/private/dvr/buffer_hub_defs.h @@ -191,11 +191,6 @@ struct DetachedBufferRPC { // Imports the given channel handle to a DetachedBuffer, taking ownership. kOpImport, - // Promotes a DetachedBuffer to become a ProducerBuffer. Once promoted the - // DetachedBuffer channel will be closed automatically on successful IPC - // return. Further IPCs towards this channel will return error. - kOpPromote, - // Creates a DetachedBuffer client from an existing one. The new client will // share the same underlying gralloc buffer and ashmem region for metadata. kOpDuplicate, @@ -212,10 +207,9 @@ struct DetachedBufferRPC { uint32_t format, uint64_t usage, size_t user_metadata_size)); PDX_REMOTE_METHOD(Import, kOpImport, BufferTraits(Void)); - PDX_REMOTE_METHOD(Promote, kOpPromote, LocalChannelHandle(Void)); PDX_REMOTE_METHOD(Duplicate, kOpDuplicate, LocalChannelHandle(Void)); - PDX_REMOTE_API(API, Create, Import, Promote, Duplicate); + PDX_REMOTE_API(API, Create, Import, Duplicate); }; } // namespace dvr diff --git a/services/vr/bufferhubd/buffer_channel.cpp b/services/vr/bufferhubd/buffer_channel.cpp index dcc6ea4fe2..cc9cf20ff9 100644 --- a/services/vr/bufferhubd/buffer_channel.cpp +++ b/services/vr/bufferhubd/buffer_channel.cpp @@ -74,11 +74,6 @@ bool BufferChannel::HandleMessage(Message& message) { *this, &BufferChannel::OnDuplicate, message); return true; - case DetachedBufferRPC::Promote::Opcode: - DispatchRemoteMethod( - *this, &BufferChannel::OnPromote, message); - return true; - default: return false; } @@ -154,69 +149,5 @@ Status BufferChannel::OnDuplicate( return status; } -Status BufferChannel::OnPromote( - Message& message) { - ATRACE_NAME("BufferChannel::OnPromote"); - ALOGD_IF(TRACE, "BufferChannel::OnPromote: buffer_id=%d", buffer_id()); - - // Check whether this is the channel exclusive owner of the buffer_node_. - if (buffer_state_bit_ != buffer_node_->active_buffer_bit_mask()) { - ALOGE( - "BufferChannel::OnPromote: Cannot promote this BufferChannel as its " - "BufferNode is shared between multiple channels. This channel's state " - "bit=0x%" PRIx64 ", acitve_buffer_bit_mask=0x%" PRIx64 ".", - buffer_state_bit_, buffer_node_->active_buffer_bit_mask()); - return ErrorStatus(EINVAL); - } - - // Note that the new ProducerChannel will have different channel_id, but - // inherits the buffer_id from the DetachedBuffer. - int channel_id; - auto status = message.PushChannel(0, nullptr, &channel_id); - if (!status) { - ALOGE( - "BufferChannel::OnPromote: Failed to push ProducerChannel: %s.", - status.GetErrorMessage().c_str()); - return ErrorStatus(ENOMEM); - } - - IonBuffer buffer = std::move(buffer_node_->buffer()); - IonBuffer metadata_buffer; - if (int ret = metadata_buffer.Alloc(buffer_node_->metadata().metadata_size(), - /*height=*/1, - /*layer_count=*/1, - BufferHubDefs::kMetadataFormat, - BufferHubDefs::kMetadataUsage)) { - ALOGE("BufferChannel::OnPromote: Failed to allocate metadata: %s", - strerror(-ret)); - return ErrorStatus(EINVAL); - } - - size_t user_metadata_size = buffer_node_->user_metadata_size(); - - std::unique_ptr channel = ProducerChannel::Create( - service(), buffer_id(), channel_id, std::move(buffer), - std::move(metadata_buffer), user_metadata_size); - if (!channel) { - ALOGE( - "BufferChannel::OnPromote: Failed to create ProducerChannel from a " - "BufferChannel, buffer_id=%d.", - buffer_id()); - } - - const auto channel_status = - service()->SetChannel(channel_id, std::move(channel)); - if (!channel_status) { - // Technically, this should never fail, as we just pushed the channel. Note - // that LOG_FATAL will be stripped out in non-debug build. - LOG_FATAL( - "BufferChannel::OnPromote: Failed to set new producer buffer channel: " - "%s.", - channel_status.GetErrorMessage().c_str()); - } - - return status; -} - } // namespace dvr } // namespace android diff --git a/services/vr/bufferhubd/buffer_hub.cpp b/services/vr/bufferhubd/buffer_hub.cpp index 15391da953..6421a0b3d9 100644 --- a/services/vr/bufferhubd/buffer_hub.cpp +++ b/services/vr/bufferhubd/buffer_hub.cpp @@ -265,14 +265,6 @@ pdx::Status BufferHubService::HandleMessage(Message& message) { SetChannel(channel->channel_id(), nullptr); return {}; - case DetachedBufferRPC::Promote::Opcode: - // In addition to the message handler in the BufferChannel's - // HandleMessage method, we also need to invalid the channel. Note that - // this has to be done after HandleMessage returns to make sure the IPC - // request has went back to the client first. - SetChannel(channel->channel_id(), nullptr); - return {}; - default: return DefaultHandleMessage(message); } diff --git a/services/vr/bufferhubd/include/private/dvr/buffer_channel.h b/services/vr/bufferhubd/include/private/dvr/buffer_channel.h index 1697251620..0ce991a5d2 100644 --- a/services/vr/bufferhubd/include/private/dvr/buffer_channel.h +++ b/services/vr/bufferhubd/include/private/dvr/buffer_channel.h @@ -50,7 +50,6 @@ class BufferChannel : public BufferHubChannel { pdx::Status> OnImport( pdx::Message& message); pdx::Status OnDuplicate(pdx::Message& message); - pdx::Status OnPromote(pdx::Message& message); // The concrete implementation of the Buffer object. std::shared_ptr buffer_node_; -- cgit v1.2.3-59-g8ed1b From 7e204b7213c8f73e1fa4e7cdf21638358240cfb1 Mon Sep 17 00:00:00 2001 From: Tianyu Jiang Date: Fri, 26 Oct 2018 15:39:18 -0700 Subject: Change the variable name "buffer_state_bit" into "client_state_mask". Reasons: 1. This variable is not refering to a property of the buffer. It refers to a client of the buffer. 2. The original "buffer_state_bit" of a producer/consumer is actually the client state bit mask for both buffer_state and fence_state in shared memory. Thus, "buffer_state_bit" does not make sense for the fence state. 3. In the future, Every clients takes up two bits in the buffer_state. For simpler bit manipulation, there will be a future change making the client_state_bits two bits as well. Please refer to ag/5236978 for an early look at the future bit manipulation. Thus, this change replaces "bit" with "mask". Test: build Bug: 112007999 Change-Id: I72f59ab9491bd2f135da068f578195fbf5e6c2b6 --- libs/ui/BufferHubBuffer.cpp | 2 +- libs/ui/include/ui/BufferHubBuffer.h | 4 +- libs/ui/tests/BufferHubBuffer_test.cpp | 12 ++--- libs/vr/libbufferhub/buffer_hub-test.cpp | 52 +++++++++++----------- libs/vr/libbufferhub/buffer_hub_base.cpp | 8 ++-- libs/vr/libbufferhub/consumer_buffer.cpp | 8 ++-- .../include/private/dvr/buffer_hub_base.h | 4 +- .../include/private/dvr/buffer_hub_defs.h | 16 +++---- .../include/private/dvr/bufferhub_rpc.h | 10 ++--- services/vr/bufferhubd/buffer_channel.cpp | 14 +++--- services/vr/bufferhubd/buffer_node.cpp | 10 ++--- .../include/private/dvr/buffer_channel.h | 2 +- .../bufferhubd/include/private/dvr/buffer_node.h | 6 +-- .../include/private/dvr/producer_channel.h | 2 +- services/vr/bufferhubd/producer_channel.cpp | 4 +- services/vr/bufferhubd/tests/buffer_node-test.cpp | 34 +++++++------- 16 files changed, 95 insertions(+), 93 deletions(-) (limited to 'libs/ui/BufferHubBuffer.cpp') diff --git a/libs/ui/BufferHubBuffer.cpp b/libs/ui/BufferHubBuffer.cpp index 44a947e5e3..a6e6d73819 100644 --- a/libs/ui/BufferHubBuffer.cpp +++ b/libs/ui/BufferHubBuffer.cpp @@ -159,7 +159,7 @@ int BufferHubBuffer::ImportGraphicBuffer() { // If all imports succeed, replace the previous buffer and id. mId = bufferId; - mBufferStateBit = bufferTraits.buffer_state_bit(); + mClientStateMask = bufferTraits.client_state_mask(); // TODO(b/112012161) Set up shared fences. ALOGD("BufferHubBuffer::ImportGraphicBuffer: id=%d, buffer_state=%" PRIx64 ".", id(), diff --git a/libs/ui/include/ui/BufferHubBuffer.h b/libs/ui/include/ui/BufferHubBuffer.h index cefe5b3f3f..daf61928a4 100644 --- a/libs/ui/include/ui/BufferHubBuffer.h +++ b/libs/ui/include/ui/BufferHubBuffer.h @@ -88,7 +88,7 @@ public: // A state mask which is unique to a buffer hub client among all its siblings sharing the same // concrete graphic buffer. - uint64_t buffer_state_bit() const { return mBufferStateBit; } + uint64_t client_state_mask() const { return mClientStateMask; } size_t user_metadata_size() const { return mMetadata.user_metadata_size(); } @@ -126,7 +126,7 @@ private: // Global id for the buffer that is consistent across processes. int mId = -1; - uint64_t mBufferStateBit = 0; + uint64_t mClientStateMask = 0; // Wrapps the gralloc buffer handle of this buffer. dvr::NativeHandleWrapper mBufferHandle; diff --git a/libs/ui/tests/BufferHubBuffer_test.cpp b/libs/ui/tests/BufferHubBuffer_test.cpp index be06ad281a..f0253b21f7 100644 --- a/libs/ui/tests/BufferHubBuffer_test.cpp +++ b/libs/ui/tests/BufferHubBuffer_test.cpp @@ -100,12 +100,12 @@ TEST_F(BufferHubBufferTest, DuplicateBufferHubBuffer) { // These two buffer instances are based on the same physical buffer under the // hood, so they should share the same id. EXPECT_EQ(id1, id2); - // We use buffer_state_bit() to tell those two instances apart. - EXPECT_NE(b1->buffer_state_bit(), b2->buffer_state_bit()); - EXPECT_NE(b1->buffer_state_bit(), 0ULL); - EXPECT_NE(b2->buffer_state_bit(), 0ULL); - EXPECT_NE(b1->buffer_state_bit(), kProducerStateBit); - EXPECT_NE(b2->buffer_state_bit(), kProducerStateBit); + // We use client_state_mask() to tell those two instances apart. + EXPECT_NE(b1->client_state_mask(), b2->client_state_mask()); + EXPECT_NE(b1->client_state_mask(), 0ULL); + EXPECT_NE(b2->client_state_mask(), 0ULL); + EXPECT_NE(b1->client_state_mask(), kProducerStateBit); + EXPECT_NE(b2->client_state_mask(), kProducerStateBit); // Both buffer instances should be in gained state. EXPECT_TRUE(IsBufferGained(b1->buffer_state())); diff --git a/libs/vr/libbufferhub/buffer_hub-test.cpp b/libs/vr/libbufferhub/buffer_hub-test.cpp index 8351efce40..3e26dae386 100644 --- a/libs/vr/libbufferhub/buffer_hub-test.cpp +++ b/libs/vr/libbufferhub/buffer_hub-test.cpp @@ -59,19 +59,19 @@ TEST_F(LibBufferHubTest, TestBasicUsage) { ASSERT_TRUE(c2.get() != nullptr); // Producer state mask is unique, i.e. 1. - EXPECT_EQ(p->buffer_state_bit(), kProducerStateBit); + EXPECT_EQ(p->client_state_mask(), kProducerStateBit); // Consumer state mask cannot have producer bit on. - EXPECT_EQ(c->buffer_state_bit() & kProducerStateBit, 0U); + EXPECT_EQ(c->client_state_mask() & kProducerStateBit, 0U); // Consumer state mask must be a single, i.e. power of 2. - EXPECT_NE(c->buffer_state_bit(), 0U); - EXPECT_EQ(c->buffer_state_bit() & (c->buffer_state_bit() - 1), 0U); + EXPECT_NE(c->client_state_mask(), 0U); + EXPECT_EQ(c->client_state_mask() & (c->client_state_mask() - 1), 0U); // Consumer state mask cannot have producer bit on. - EXPECT_EQ(c2->buffer_state_bit() & kProducerStateBit, 0U); + EXPECT_EQ(c2->client_state_mask() & kProducerStateBit, 0U); // Consumer state mask must be a single, i.e. power of 2. - EXPECT_NE(c2->buffer_state_bit(), 0U); - EXPECT_EQ(c2->buffer_state_bit() & (c2->buffer_state_bit() - 1), 0U); + EXPECT_NE(c2->client_state_mask(), 0U); + EXPECT_EQ(c2->client_state_mask() & (c2->client_state_mask() - 1), 0U); // Each consumer should have unique bit. - EXPECT_EQ(c->buffer_state_bit() & c2->buffer_state_bit(), 0U); + EXPECT_EQ(c->client_state_mask() & c2->client_state_mask(), 0U); // Initial state: producer not available, consumers not available. EXPECT_EQ(0, RETRY_EINTR(p->Poll(kPollTimeoutMs))); @@ -178,16 +178,16 @@ TEST_F(LibBufferHubTest, TestStateMask) { ASSERT_TRUE(p.get() != nullptr); // It's ok to create up to kMaxConsumerCount consumer buffers. - uint64_t buffer_state_bits = p->buffer_state_bit(); + uint64_t client_state_masks = p->client_state_mask(); std::array, kMaxConsumerCount> cs; for (size_t i = 0; i < kMaxConsumerCount; i++) { cs[i] = ConsumerBuffer::Import(p->CreateConsumer()); ASSERT_TRUE(cs[i].get() != nullptr); // Expect all buffers have unique state mask. - EXPECT_EQ(buffer_state_bits & cs[i]->buffer_state_bit(), 0U); - buffer_state_bits |= cs[i]->buffer_state_bit(); + EXPECT_EQ(client_state_masks & cs[i]->client_state_mask(), 0U); + client_state_masks |= cs[i]->client_state_mask(); } - EXPECT_EQ(buffer_state_bits, kProducerStateBit | kConsumerStateMask); + EXPECT_EQ(client_state_masks, kProducerStateBit | kConsumerStateMask); // The 64th creation will fail with out-of-memory error. auto state = p->CreateConsumer(); @@ -195,14 +195,14 @@ TEST_F(LibBufferHubTest, TestStateMask) { // Release any consumer should allow us to re-create. for (size_t i = 0; i < kMaxConsumerCount; i++) { - buffer_state_bits &= ~cs[i]->buffer_state_bit(); + client_state_masks &= ~cs[i]->client_state_mask(); cs[i] = nullptr; cs[i] = ConsumerBuffer::Import(p->CreateConsumer()); ASSERT_TRUE(cs[i].get() != nullptr); // The released state mask will be reused. - EXPECT_EQ(buffer_state_bits & cs[i]->buffer_state_bit(), 0U); - buffer_state_bits |= cs[i]->buffer_state_bit(); - EXPECT_EQ(buffer_state_bits, kProducerStateBit | kConsumerStateMask); + EXPECT_EQ(client_state_masks & cs[i]->client_state_mask(), 0U); + client_state_masks |= cs[i]->client_state_mask(); + EXPECT_EQ(client_state_masks, kProducerStateBit | kConsumerStateMask); } } @@ -402,7 +402,7 @@ TEST_F(LibBufferHubTest, TestMaxConsumers) { EXPECT_TRUE(IsBufferPosted(p->buffer_state())); for (size_t i = 0; i < kMaxConsumerCount; i++) { EXPECT_TRUE( - IsBufferPosted(cs[i]->buffer_state(), cs[i]->buffer_state_bit())); + IsBufferPosted(cs[i]->buffer_state(), cs[i]->client_state_mask())); EXPECT_LT(0, RETRY_EINTR(cs[i]->Poll(kPollTimeoutMs))); EXPECT_EQ(0, cs[i]->AcquireAsync(&metadata, &invalid_fence)); EXPECT_TRUE(IsBufferAcquired(p->buffer_state())); @@ -707,7 +707,7 @@ TEST_F(LibBufferHubTest, TestOrphanedAcquire) { std::unique_ptr c1 = ConsumerBuffer::Import(p->CreateConsumer()); ASSERT_TRUE(c1.get() != nullptr); - const uint64_t consumer_state_bit1 = c1->buffer_state_bit(); + const uint64_t consumer_state_bit1 = c1->client_state_mask(); DvrNativeBufferMetadata meta; EXPECT_EQ(0, p->PostAsync(&meta, LocalHandle())); @@ -723,7 +723,7 @@ TEST_F(LibBufferHubTest, TestOrphanedAcquire) { std::unique_ptr c2 = ConsumerBuffer::Import(p->CreateConsumer()); ASSERT_TRUE(c2.get() != nullptr); - const uint64_t consumer_state_bit2 = c2->buffer_state_bit(); + const uint64_t consumer_state_bit2 = c2->client_state_mask(); EXPECT_NE(consumer_state_bit1, consumer_state_bit2); // The new consumer is available for acquire. @@ -739,7 +739,7 @@ TEST_F(LibBufferHubTest, TestOrphanedAcquire) { std::unique_ptr c3 = ConsumerBuffer::Import(p->CreateConsumer()); ASSERT_TRUE(c3.get() != nullptr); - const uint64_t consumer_state_bit3 = c3->buffer_state_bit(); + const uint64_t consumer_state_bit3 = c3->client_state_mask(); EXPECT_NE(consumer_state_bit2, consumer_state_bit3); // The consumer buffer is not acquirable. EXPECT_GE(0, RETRY_EINTR(c3->Poll(kPollTimeoutMs))); @@ -911,12 +911,12 @@ TEST_F(LibBufferHubTest, TestDuplicateBufferHubBuffer) { // These two buffer instances are based on the same physical buffer under the // hood, so they should share the same id. EXPECT_EQ(b1_id, b2_id); - // We use buffer_state_bit() to tell those two instances apart. - EXPECT_NE(b1->buffer_state_bit(), b2->buffer_state_bit()); - EXPECT_NE(b1->buffer_state_bit(), 0ULL); - EXPECT_NE(b2->buffer_state_bit(), 0ULL); - EXPECT_NE(b1->buffer_state_bit(), kProducerStateBit); - EXPECT_NE(b2->buffer_state_bit(), kProducerStateBit); + // We use client_state_mask() to tell those two instances apart. + EXPECT_NE(b1->client_state_mask(), b2->client_state_mask()); + EXPECT_NE(b1->client_state_mask(), 0ULL); + EXPECT_NE(b2->client_state_mask(), 0ULL); + EXPECT_NE(b1->client_state_mask(), kProducerStateBit); + EXPECT_NE(b2->client_state_mask(), kProducerStateBit); // Both buffer instances should be in gained state. EXPECT_TRUE(IsBufferGained(b1->buffer_state())); diff --git a/libs/vr/libbufferhub/buffer_hub_base.cpp b/libs/vr/libbufferhub/buffer_hub_base.cpp index bc6aece893..b4c5d428cc 100644 --- a/libs/vr/libbufferhub/buffer_hub_base.cpp +++ b/libs/vr/libbufferhub/buffer_hub_base.cpp @@ -114,7 +114,7 @@ int BufferHubBase::ImportBuffer() { id_ = new_id; cid_ = buffer_desc.buffer_cid(); - buffer_state_bit_ = buffer_desc.buffer_state_bit(); + client_state_mask_ = buffer_desc.client_state_mask(); // Note that here the buffer_state, fence_state and active_clients_bit_mask // are mapped from shared memory as an atomic object. The std::atomic's @@ -169,7 +169,7 @@ int BufferHubBase::UpdateSharedFence(const LocalHandle& new_fence, // If ready fence is valid, we put that into the epoll set. epoll_event event; event.events = EPOLLIN; - event.data.u64 = buffer_state_bit(); + event.data.u64 = client_state_mask(); pending_fence_fd_ = new_fence.Duplicate(); if (epoll_ctl(shared_fence.Get(), EPOLL_CTL_ADD, pending_fence_fd_.Get(), &event) < 0) { @@ -182,12 +182,12 @@ int BufferHubBase::UpdateSharedFence(const LocalHandle& new_fence, } // Set bit in fence state to indicate that there is a fence from this // producer or consumer. - fence_state_->fetch_or(buffer_state_bit()); + fence_state_->fetch_or(client_state_mask()); } else { // Unset bit in fence state to indicate that there is no fence, so that // when consumer to acquire or producer to acquire, it knows no need to // check fence for this buffer. - fence_state_->fetch_and(~buffer_state_bit()); + fence_state_->fetch_and(~client_state_mask()); } } diff --git a/libs/vr/libbufferhub/consumer_buffer.cpp b/libs/vr/libbufferhub/consumer_buffer.cpp index c9d8554e0e..a91e842a35 100644 --- a/libs/vr/libbufferhub/consumer_buffer.cpp +++ b/libs/vr/libbufferhub/consumer_buffer.cpp @@ -39,10 +39,10 @@ int ConsumerBuffer::LocalAcquire(DvrNativeBufferMetadata* out_meta, // The buffer is can be acquired iff: 1) producer bit is set; 2) consumer bit // is not set. uint64_t buffer_state = buffer_state_->load(); - if (!BufferHubDefs::IsBufferPosted(buffer_state, buffer_state_bit())) { + if (!BufferHubDefs::IsBufferPosted(buffer_state, client_state_mask())) { ALOGE("ConsumerBuffer::LocalAcquire: not posted, id=%d state=%" PRIx64 - " buffer_state_bit=%" PRIx64 ".", - id(), buffer_state, buffer_state_bit()); + " client_state_mask=%" PRIx64 ".", + id(), buffer_state, client_state_mask()); return -EBUSY; } @@ -64,7 +64,7 @@ int ConsumerBuffer::LocalAcquire(DvrNativeBufferMetadata* out_meta, } // Set the consumer bit unique to this consumer. - BufferHubDefs::ModifyBufferState(buffer_state_, 0ULL, buffer_state_bit()); + BufferHubDefs::ModifyBufferState(buffer_state_, 0ULL, client_state_mask()); return 0; } diff --git a/libs/vr/libbufferhub/include/private/dvr/buffer_hub_base.h b/libs/vr/libbufferhub/include/private/dvr/buffer_hub_base.h index 282be46f4d..1ea8302d02 100644 --- a/libs/vr/libbufferhub/include/private/dvr/buffer_hub_base.h +++ b/libs/vr/libbufferhub/include/private/dvr/buffer_hub_base.h @@ -94,7 +94,7 @@ class BufferHubBase : public pdx::Client { // A state mask which is unique to a buffer hub client among all its siblings // sharing the same concrete graphic buffer. - uint64_t buffer_state_bit() const { return buffer_state_bit_; } + uint64_t client_state_mask() const { return client_state_mask_; } // The following methods return settings of the first buffer. Currently, // it is only possible to create multi-buffer BufferHubBases with the same @@ -157,7 +157,7 @@ class BufferHubBase : public pdx::Client { // Client bit mask which indicates the locations of this client object in the // buffer_state_. - uint64_t buffer_state_bit_{0ULL}; + uint64_t client_state_mask_{0ULL}; IonBuffer buffer_; IonBuffer metadata_buffer_; }; diff --git a/libs/vr/libbufferhub/include/private/dvr/buffer_hub_defs.h b/libs/vr/libbufferhub/include/private/dvr/buffer_hub_defs.h index 49f9c3ebd6..4953f9bd00 100644 --- a/libs/vr/libbufferhub/include/private/dvr/buffer_hub_defs.h +++ b/libs/vr/libbufferhub/include/private/dvr/buffer_hub_defs.h @@ -94,13 +94,13 @@ class BufferTraits { BufferTraits() = default; BufferTraits(const native_handle_t* buffer_handle, const FileHandleType& metadata_handle, int id, - uint64_t buffer_state_bit, uint64_t metadata_size, + uint64_t client_state_mask, uint64_t metadata_size, uint32_t width, uint32_t height, uint32_t layer_count, uint32_t format, uint64_t usage, uint32_t stride, const FileHandleType& acquire_fence_fd, const FileHandleType& release_fence_fd) : id_(id), - buffer_state_bit_(buffer_state_bit), + client_state_mask_(client_state_mask), metadata_size_(metadata_size), width_(width), height_(height), @@ -124,7 +124,7 @@ class BufferTraits { // same buffer channel has uniqued state bit among its siblings. For a // producer buffer the bit must be kProducerStateBit; for a consumer the bit // must be one of the kConsumerStateMask. - uint64_t buffer_state_bit() const { return buffer_state_bit_; } + uint64_t client_state_mask() const { return client_state_mask_; } uint64_t metadata_size() const { return metadata_size_; } uint32_t width() { return width_; } @@ -148,7 +148,7 @@ class BufferTraits { private: // BufferHub specific traits. int id_ = -1; - uint64_t buffer_state_bit_; + uint64_t client_state_mask_; uint64_t metadata_size_; // Traits for a GraphicBuffer. @@ -169,10 +169,10 @@ class BufferTraits { FileHandleType acquire_fence_fd_; FileHandleType release_fence_fd_; - PDX_SERIALIZABLE_MEMBERS(BufferTraits, id_, buffer_state_bit_, - metadata_size_, stride_, width_, height_, - layer_count_, format_, usage_, buffer_handle_, - metadata_handle_, acquire_fence_fd_, + PDX_SERIALIZABLE_MEMBERS(BufferTraits, id_, + client_state_mask_, metadata_size_, stride_, width_, + height_, layer_count_, format_, usage_, + buffer_handle_, metadata_handle_, acquire_fence_fd_, release_fence_fd_); BufferTraits(const BufferTraits&) = delete; diff --git a/libs/vr/libbufferhub/include/private/dvr/bufferhub_rpc.h b/libs/vr/libbufferhub/include/private/dvr/bufferhub_rpc.h index 7330aa181d..de51f25ebd 100644 --- a/libs/vr/libbufferhub/include/private/dvr/bufferhub_rpc.h +++ b/libs/vr/libbufferhub/include/private/dvr/bufferhub_rpc.h @@ -99,12 +99,12 @@ class BufferDescription { public: BufferDescription() = default; BufferDescription(const IonBuffer& buffer, const IonBuffer& metadata, int id, - int buffer_cid, uint64_t buffer_state_bit, + int buffer_cid, uint64_t client_state_mask, const FileHandleType& acquire_fence_fd, const FileHandleType& release_fence_fd) : id_(id), buffer_cid_(buffer_cid), - buffer_state_bit_(buffer_state_bit), + client_state_mask_(client_state_mask), buffer_(buffer, id), metadata_(metadata, id), acquire_fence_fd_(acquire_fence_fd.Borrow()), @@ -125,7 +125,7 @@ class BufferDescription { // same buffer channel has uniqued state bit among its siblings. For a // producer buffer the bit must be kProducerStateBit; for a consumer the bit // must be one of the kConsumerStateMask. - uint64_t buffer_state_bit() const { return buffer_state_bit_; } + uint64_t client_state_mask() const { return client_state_mask_; } FileHandleType take_acquire_fence() { return std::move(acquire_fence_fd_); } FileHandleType take_release_fence() { return std::move(release_fence_fd_); } @@ -135,7 +135,7 @@ class BufferDescription { private: int id_{-1}; int buffer_cid_{-1}; - uint64_t buffer_state_bit_{0}; + uint64_t client_state_mask_{0}; // Two IonBuffers: one for the graphic buffer and one for metadata. NativeBufferHandle buffer_; NativeBufferHandle metadata_; @@ -145,7 +145,7 @@ class BufferDescription { FileHandleType release_fence_fd_; PDX_SERIALIZABLE_MEMBERS(BufferDescription, id_, buffer_cid_, - buffer_state_bit_, buffer_, metadata_, + client_state_mask_, buffer_, metadata_, acquire_fence_fd_, release_fence_fd_); BufferDescription(const BufferDescription&) = delete; diff --git a/services/vr/bufferhubd/buffer_channel.cpp b/services/vr/bufferhubd/buffer_channel.cpp index ee857462f8..d5e6dfbbef 100644 --- a/services/vr/bufferhubd/buffer_channel.cpp +++ b/services/vr/bufferhubd/buffer_channel.cpp @@ -18,7 +18,7 @@ BufferChannel::BufferChannel(BufferHubService* service, int buffer_id, : BufferHubChannel(service, buffer_id, channel_id, kDetachedBufferType), buffer_node_( std::make_shared(std::move(buffer), user_metadata_size)) { - buffer_state_bit_ = buffer_node_->AddNewActiveClientsBitToMask(); + client_state_mask_ = buffer_node_->AddNewActiveClientsBitToMask(); } BufferChannel::BufferChannel(BufferHubService* service, int buffer_id, @@ -28,7 +28,7 @@ BufferChannel::BufferChannel(BufferHubService* service, int buffer_id, : BufferHubChannel(service, buffer_id, buffer_id, kDetachedBufferType), buffer_node_(std::make_shared( width, height, layer_count, format, usage, user_metadata_size)) { - buffer_state_bit_ = buffer_node_->AddNewActiveClientsBitToMask(); + client_state_mask_ = buffer_node_->AddNewActiveClientsBitToMask(); } BufferChannel::BufferChannel(BufferHubService* service, int buffer_id, @@ -36,8 +36,8 @@ BufferChannel::BufferChannel(BufferHubService* service, int buffer_id, std::shared_ptr buffer_node) : BufferHubChannel(service, buffer_id, channel_id, kDetachedBufferType), buffer_node_(buffer_node) { - buffer_state_bit_ = buffer_node_->AddNewActiveClientsBitToMask(); - if (buffer_state_bit_ == 0ULL) { + client_state_mask_ = buffer_node_->AddNewActiveClientsBitToMask(); + if (client_state_mask_ == 0ULL) { ALOGE("BufferChannel::BufferChannel: %s", strerror(errno)); buffer_node_ = nullptr; } @@ -46,8 +46,8 @@ BufferChannel::BufferChannel(BufferHubService* service, int buffer_id, BufferChannel::~BufferChannel() { ALOGD_IF(TRACE, "BufferChannel::~BufferChannel: channel_id=%d buffer_id=%d.", channel_id(), buffer_id()); - if (buffer_state_bit_ != 0ULL) { - buffer_node_->RemoveClientsBitFromMask(buffer_state_bit_); + if (client_state_mask_ != 0ULL) { + buffer_node_->RemoveClientsBitFromMask(client_state_mask_); } Hangup(); } @@ -93,7 +93,7 @@ Status> BufferChannel::OnImport( /*buffer_handle=*/buffer_node_->buffer().handle(), /*metadata_handle=*/buffer_node_->metadata().ashmem_handle().Borrow(), /*id=*/buffer_id(), - /*buffer_state_bit=*/buffer_state_bit_, + /*client_state_mask=*/client_state_mask_, /*metadata_size=*/buffer_node_->metadata().metadata_size(), /*width=*/buffer_node_->buffer().width(), /*height=*/buffer_node_->buffer().height(), diff --git a/services/vr/bufferhubd/buffer_node.cpp b/services/vr/bufferhubd/buffer_node.cpp index bedec6feb9..f8ec59da09 100644 --- a/services/vr/bufferhubd/buffer_node.cpp +++ b/services/vr/bufferhubd/buffer_node.cpp @@ -43,12 +43,12 @@ uint64_t BufferNode::GetActiveClientsBitMask() const { uint64_t BufferNode::AddNewActiveClientsBitToMask() { uint64_t current_active_clients_bit_mask = GetActiveClientsBitMask(); - uint64_t buffer_state_bit = 0ULL; + uint64_t client_state_mask = 0ULL; uint64_t updated_active_clients_bit_mask = 0ULL; do { - buffer_state_bit = + client_state_mask = BufferHubDefs::FindNextClearedBit(current_active_clients_bit_mask); - if (buffer_state_bit == 0ULL) { + if (client_state_mask == 0ULL) { ALOGE( "BufferNode::AddNewActiveClientsBitToMask: reached the maximum " "mumber of channels per buffer node: 32."); @@ -56,11 +56,11 @@ uint64_t BufferNode::AddNewActiveClientsBitToMask() { return 0ULL; } updated_active_clients_bit_mask = - current_active_clients_bit_mask | buffer_state_bit; + current_active_clients_bit_mask | client_state_mask; } while (!(active_clients_bit_mask_->compare_exchange_weak( current_active_clients_bit_mask, updated_active_clients_bit_mask, std::memory_order_acq_rel, std::memory_order_acquire))); - return buffer_state_bit; + return client_state_mask; } void BufferNode::RemoveClientsBitFromMask(const uint64_t& value) { diff --git a/services/vr/bufferhubd/include/private/dvr/buffer_channel.h b/services/vr/bufferhubd/include/private/dvr/buffer_channel.h index e9bdb37351..6d1e9e14fd 100644 --- a/services/vr/bufferhubd/include/private/dvr/buffer_channel.h +++ b/services/vr/bufferhubd/include/private/dvr/buffer_channel.h @@ -55,7 +55,7 @@ class BufferChannel : public BufferHubChannel { std::shared_ptr buffer_node_ = nullptr; // The state bit of this buffer. Must be one the lower 63 bits. - uint64_t buffer_state_bit_ = 0ULL; + uint64_t client_state_mask_ = 0ULL; }; } // namespace dvr diff --git a/services/vr/bufferhubd/include/private/dvr/buffer_node.h b/services/vr/bufferhubd/include/private/dvr/buffer_node.h index e1e8057104..067584e222 100644 --- a/services/vr/bufferhubd/include/private/dvr/buffer_node.h +++ b/services/vr/bufferhubd/include/private/dvr/buffer_node.h @@ -34,9 +34,9 @@ class BufferNode { // active_clients_bit_mask from all threads will be returned here. uint64_t GetActiveClientsBitMask() const; - // Find and add a new buffer_state_bit to active_clients_bit_mask in + // Find and add a new client_state_mask to active_clients_bit_mask in // metadata_. - // Return the new buffer_state_bit that is added to active_clients_bit_mask. + // Return the new client_state_mask that is added to active_clients_bit_mask. // Return 0ULL if there are already 32 bp clients of the buffer. uint64_t AddNewActiveClientsBitToMask(); @@ -69,7 +69,7 @@ class BufferNode { std::atomic* fence_state_ = nullptr; // active_clients_bit_mask_ tracks all the bp clients of the buffer. It is the - // union of all buffer_state_bit of all bp clients. + // union of all client_state_mask of all bp clients. std::atomic* active_clients_bit_mask_ = nullptr; }; diff --git a/services/vr/bufferhubd/include/private/dvr/producer_channel.h b/services/vr/bufferhubd/include/private/dvr/producer_channel.h index 18bb7bf368..b5c92be515 100644 --- a/services/vr/bufferhubd/include/private/dvr/producer_channel.h +++ b/services/vr/bufferhubd/include/private/dvr/producer_channel.h @@ -49,7 +49,7 @@ class ProducerChannel : public BufferHubChannel { BufferInfo GetBufferInfo() const override; - BufferDescription GetBuffer(uint64_t buffer_state_bit); + BufferDescription GetBuffer(uint64_t client_state_mask); pdx::Status CreateConsumer(Message& message); pdx::Status OnNewConsumer(Message& message); diff --git a/services/vr/bufferhubd/producer_channel.cpp b/services/vr/bufferhubd/producer_channel.cpp index 581390f374..beedc4c38d 100644 --- a/services/vr/bufferhubd/producer_channel.cpp +++ b/services/vr/bufferhubd/producer_channel.cpp @@ -227,12 +227,12 @@ bool ProducerChannel::HandleMessage(Message& message) { } BufferDescription ProducerChannel::GetBuffer( - uint64_t buffer_state_bit) { + uint64_t client_state_mask) { return {buffer_, metadata_buffer_, buffer_id(), channel_id(), - buffer_state_bit, + client_state_mask, acquire_fence_fd_.Borrow(), release_fence_fd_.Borrow()}; } diff --git a/services/vr/bufferhubd/tests/buffer_node-test.cpp b/services/vr/bufferhubd/tests/buffer_node-test.cpp index c2526fe021..9a927f1801 100644 --- a/services/vr/bufferhubd/tests/buffer_node-test.cpp +++ b/services/vr/bufferhubd/tests/buffer_node-test.cpp @@ -37,49 +37,51 @@ TEST_F(BufferNodeTest, TestCreateBufferNode) { } TEST_F(BufferNodeTest, TestAddNewActiveClientsBitToMask_twoNewClients) { - uint64_t new_buffer_state_bit_1 = buffer_node->AddNewActiveClientsBitToMask(); - EXPECT_EQ(buffer_node->GetActiveClientsBitMask(), new_buffer_state_bit_1); + uint64_t new_client_state_mask_1 = + buffer_node->AddNewActiveClientsBitToMask(); + EXPECT_EQ(buffer_node->GetActiveClientsBitMask(), new_client_state_mask_1); - // Request and add a new buffer_state_bit again. + // Request and add a new client_state_mask again. // Active clients bit mask should be the union of the two new - // buffer_state_bits. - uint64_t new_buffer_state_bit_2 = buffer_node->AddNewActiveClientsBitToMask(); + // client_state_masks. + uint64_t new_client_state_mask_2 = + buffer_node->AddNewActiveClientsBitToMask(); EXPECT_EQ(buffer_node->GetActiveClientsBitMask(), - new_buffer_state_bit_1 | new_buffer_state_bit_2); + new_client_state_mask_1 | new_client_state_mask_2); } TEST_F(BufferNodeTest, TestAddNewActiveClientsBitToMask_32NewClients) { - uint64_t new_buffer_state_bit = 0ULL; + uint64_t new_client_state_mask = 0ULL; uint64_t current_mask = 0ULL; uint64_t expected_mask = 0ULL; for (int i = 0; i < 64; ++i) { - new_buffer_state_bit = buffer_node->AddNewActiveClientsBitToMask(); - EXPECT_NE(new_buffer_state_bit, 0); - EXPECT_FALSE(new_buffer_state_bit & current_mask); - expected_mask = current_mask | new_buffer_state_bit; + new_client_state_mask = buffer_node->AddNewActiveClientsBitToMask(); + EXPECT_NE(new_client_state_mask, 0); + EXPECT_FALSE(new_client_state_mask & current_mask); + expected_mask = current_mask | new_client_state_mask; current_mask = buffer_node->GetActiveClientsBitMask(); EXPECT_EQ(current_mask, expected_mask); } // Method should fail upon requesting for more than maximum allowable clients. - new_buffer_state_bit = buffer_node->AddNewActiveClientsBitToMask(); - EXPECT_EQ(new_buffer_state_bit, 0ULL); + new_client_state_mask = buffer_node->AddNewActiveClientsBitToMask(); + EXPECT_EQ(new_client_state_mask, 0ULL); EXPECT_EQ(errno, E2BIG); } TEST_F(BufferNodeTest, TestRemoveActiveClientsBitFromMask) { buffer_node->AddNewActiveClientsBitToMask(); uint64_t current_mask = buffer_node->GetActiveClientsBitMask(); - uint64_t new_buffer_state_bit = buffer_node->AddNewActiveClientsBitToMask(); + uint64_t new_client_state_mask = buffer_node->AddNewActiveClientsBitToMask(); EXPECT_NE(buffer_node->GetActiveClientsBitMask(), current_mask); - buffer_node->RemoveClientsBitFromMask(new_buffer_state_bit); + buffer_node->RemoveClientsBitFromMask(new_client_state_mask); EXPECT_EQ(buffer_node->GetActiveClientsBitMask(), current_mask); // Remove the test_mask again to the active client bit mask should not modify // the value of active clients bit mask. - buffer_node->RemoveClientsBitFromMask(new_buffer_state_bit); + buffer_node->RemoveClientsBitFromMask(new_client_state_mask); EXPECT_EQ(buffer_node->GetActiveClientsBitMask(), current_mask); } -- cgit v1.2.3-59-g8ed1b From 77565571fc884e74cdf7a7b9ed0cbf37a70b6747 Mon Sep 17 00:00:00 2001 From: Jiwen 'Steve' Cai Date: Mon, 15 Oct 2018 11:31:29 -0700 Subject: Remove DetachedBufferHandle from libui DetachedBufferHandle was a temporary measure introduced in Android P to bridge BufferHub and GraphicBuffer. In Android Q however, GraphicBuffer will be directly backed by BufferHub, thus DetachedBufferHandle becomes redundant. Also note that removing DetachedBufferHandle from libui should bare no impact on vendors for two reasons: 1. BufferHub in P is only enabled for Daydream ready devices (i.e. Pixel lines and VR AIO devices). No other vendors should have BufferHub enabled. 2. DetachedBufferHandle.h was hidden from vndk and thus vendors shouldn't get access to it anyway. Bug: 117522732 Test: Build system Change-Id: I3828eaa9499051e5ad5e4e270b5c26bae5f2c707 --- libs/gui/BufferHubProducer.cpp | 55 +++------------------- libs/gui/tests/IGraphicBufferProducer_test.cpp | 17 ------- libs/ui/BufferHubBuffer.cpp | 1 - libs/ui/GraphicBuffer.cpp | 19 -------- libs/ui/include/ui/DetachedBufferHandle.h | 52 --------------------- libs/ui/include/ui/GraphicBuffer.h | 17 ------- libs/ui/tests/Android.bp | 7 --- libs/ui/tests/GraphicBuffer_test.cpp | 64 -------------------------- libs/vr/libbufferhub/buffer_hub-test.cpp | 1 - 9 files changed, 7 insertions(+), 226 deletions(-) delete mode 100644 libs/ui/include/ui/DetachedBufferHandle.h delete mode 100644 libs/ui/tests/GraphicBuffer_test.cpp (limited to 'libs/ui/BufferHubBuffer.cpp') diff --git a/libs/gui/BufferHubProducer.cpp b/libs/gui/BufferHubProducer.cpp index 1a7c2d3826..ed773e003f 100644 --- a/libs/gui/BufferHubProducer.cpp +++ b/libs/gui/BufferHubProducer.cpp @@ -20,7 +20,6 @@ #include #include #include -#include namespace android { @@ -276,14 +275,10 @@ status_t BufferHubProducer::DetachBufferLocked(size_t slot) { status_or_handle.error()); return BAD_VALUE; } - std::unique_ptr handle = - DetachedBufferHandle::Create(status_or_handle.take()); - if (!handle->isValid()) { - ALOGE("detachBuffer: Failed to create a DetachedBufferHandle at slot %zu.", slot); - return BAD_VALUE; - } - return graphic_buffer->setDetachedBufferHandle(std::move(handle)); + // TODO(b/70912269): Reimplement BufferHubProducer::DetachBufferLocked() once GraphicBuffer can + // be directly backed by BufferHub. + return INVALID_OPERATION; } status_t BufferHubProducer::detachNextBuffer(sp* out_buffer, sp* out_fence) { @@ -373,7 +368,7 @@ status_t BufferHubProducer::attachBuffer(int* out_slot, const sp& ALOGE("attachBuffer: out_slot cannot be NULL."); return BAD_VALUE; } - if (buffer == nullptr || !buffer->isDetachedBuffer()) { + if (buffer == nullptr) { ALOGE("attachBuffer: invalid GraphicBuffer."); return BAD_VALUE; } @@ -394,45 +389,9 @@ status_t BufferHubProducer::attachBuffer(int* out_slot, const sp& return BAD_VALUE; } - // Creates a BufferProducer from the GraphicBuffer. - std::unique_ptr detached_handle = buffer->takeDetachedBufferHandle(); - if (detached_handle == nullptr) { - ALOGE("attachBuffer: DetachedBufferHandle cannot be NULL."); - return BAD_VALUE; - } - std::shared_ptr buffer_producer = - BufferProducer::Import(std::move(detached_handle->handle())); - if (buffer_producer == nullptr) { - ALOGE("attachBuffer: Failed to import BufferProducer."); - return BAD_VALUE; - } - - // Adds the BufferProducer into the Queue. - auto status_or_slot = queue_->InsertBuffer(buffer_producer); - if (!status_or_slot.ok()) { - ALOGE("attachBuffer: Failed to insert buffer, error=%d.", status_or_slot.error()); - return BAD_VALUE; - } - - size_t slot = status_or_slot.get(); - ALOGV("attachBuffer: returning slot %zu.", slot); - if (slot >= static_cast(max_buffer_count_)) { - ALOGE("attachBuffer: Invalid slot: %zu.", slot); - return BAD_VALUE; - } - - // The just attached buffer should be in dequeued state according to IGraphicBufferProducer - // interface. In BufferHub's language the buffer should be in Gained state. - buffers_[slot].mGraphicBuffer = buffer; - buffers_[slot].mBufferState.attachProducer(); - buffers_[slot].mEglFence = EGL_NO_SYNC_KHR; - buffers_[slot].mFence = Fence::NO_FENCE; - buffers_[slot].mRequestBufferCalled = true; - buffers_[slot].mAcquireCalled = false; - buffers_[slot].mNeedsReallocation = false; - - *out_slot = static_cast(slot); - return NO_ERROR; + // TODO(b/70912269): Reimplement BufferHubProducer::DetachBufferLocked() once GraphicBuffer can + // be directly backed by BufferHub. + return INVALID_OPERATION; } status_t BufferHubProducer::queueBuffer(int slot, const QueueBufferInput& input, diff --git a/libs/gui/tests/IGraphicBufferProducer_test.cpp b/libs/gui/tests/IGraphicBufferProducer_test.cpp index 6d03374810..aef7aed52c 100644 --- a/libs/gui/tests/IGraphicBufferProducer_test.cpp +++ b/libs/gui/tests/IGraphicBufferProducer_test.cpp @@ -777,14 +777,6 @@ TEST_P(IGraphicBufferProducerTest, ASSERT_OK(mProducer->detachBuffer(slot)); EXPECT_OK(buffer->initCheck()); - if (GetParam() == USE_BUFFER_HUB_PRODUCER) { - // For a GraphicBuffer backed by BufferHub, once detached from an IGBP, it should have - // isDetachedBuffer() set. Note that this only applies to BufferHub. - EXPECT_TRUE(buffer->isDetachedBuffer()); - } else { - EXPECT_FALSE(buffer->isDetachedBuffer()); - } - ASSERT_OK(mProducer->disconnect(TEST_API)); ASSERT_EQ(NO_INIT, mProducer->attachBuffer(&slot, buffer)); @@ -801,16 +793,7 @@ TEST_P(IGraphicBufferProducerTest, DetachThenAttach_Succeeds) { ASSERT_OK(mProducer->detachBuffer(slot)); EXPECT_OK(buffer->initCheck()); - if (GetParam() == USE_BUFFER_HUB_PRODUCER) { - // For a GraphicBuffer backed by BufferHub, once detached from an IGBP, it should have - // isDetachedBuffer() set. Note that this only applies to BufferHub. - EXPECT_TRUE(buffer->isDetachedBuffer()); - } else { - EXPECT_FALSE(buffer->isDetachedBuffer()); - } - EXPECT_OK(mProducer->attachBuffer(&slot, buffer)); - EXPECT_FALSE(buffer->isDetachedBuffer()); EXPECT_OK(buffer->initCheck()); } diff --git a/libs/ui/BufferHubBuffer.cpp b/libs/ui/BufferHubBuffer.cpp index a6e6d73819..8cc1a4e3b4 100644 --- a/libs/ui/BufferHubBuffer.cpp +++ b/libs/ui/BufferHubBuffer.cpp @@ -37,7 +37,6 @@ #pragma clang diagnostic pop #include -#include #include diff --git a/libs/ui/GraphicBuffer.cpp b/libs/ui/GraphicBuffer.cpp index c50d1d038e..5a1ddee002 100644 --- a/libs/ui/GraphicBuffer.cpp +++ b/libs/ui/GraphicBuffer.cpp @@ -22,7 +22,6 @@ #include -#include #include #include #include @@ -490,24 +489,6 @@ status_t GraphicBuffer::unflatten( return NO_ERROR; } -bool GraphicBuffer::isDetachedBuffer() const { - return mDetachedBufferHandle && mDetachedBufferHandle->isValid(); -} - -status_t GraphicBuffer::setDetachedBufferHandle(std::unique_ptr channel) { - if (isDetachedBuffer()) { - ALOGW("setDetachedBuffer: there is already a BufferHub channel associated with this " - "GraphicBuffer. Replacing the old one."); - } - - mDetachedBufferHandle = std::move(channel); - return NO_ERROR; -} - -std::unique_ptr GraphicBuffer::takeDetachedBufferHandle() { - return std::move(mDetachedBufferHandle); -} - // --------------------------------------------------------------------------- }; // namespace android diff --git a/libs/ui/include/ui/DetachedBufferHandle.h b/libs/ui/include/ui/DetachedBufferHandle.h deleted file mode 100644 index f3c328d52b..0000000000 --- a/libs/ui/include/ui/DetachedBufferHandle.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (C) 2018 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef ANDROID_DETACHED_BUFFER_HUB_HANDLE_H -#define ANDROID_DETACHED_BUFFER_HUB_HANDLE_H - -#include - -#include - -namespace android { - -// A wrapper that holds a pdx::LocalChannelHandle object. From the handle, a BufferHub buffer can be -// created. Current implementation assumes that the underlying transport is using libpdx (thus -// holding a pdx::LocalChannelHandle object), but future implementation can change it to a Binder -// backend if ever needed. -class DetachedBufferHandle { -public: - static std::unique_ptr Create(pdx::LocalChannelHandle handle) { - return std::unique_ptr(new DetachedBufferHandle(std::move(handle))); - } - - // Accessors to get or take the internal pdx::LocalChannelHandle. - pdx::LocalChannelHandle& handle() { return mHandle; } - const pdx::LocalChannelHandle& handle() const { return mHandle; } - - // Returns whether the DetachedBufferHandle holds a BufferHub channel. - bool isValid() const { return mHandle.valid(); } - -private: - // Constructs a DetachedBufferHandle from a pdx::LocalChannelHandle. - explicit DetachedBufferHandle(pdx::LocalChannelHandle handle) : mHandle(std::move(handle)) {} - - pdx::LocalChannelHandle mHandle; -}; - -} // namespace android - -#endif // ANDROID_DETACHED_BUFFER_HUB_HANDLE_H diff --git a/libs/ui/include/ui/GraphicBuffer.h b/libs/ui/include/ui/GraphicBuffer.h index cc38982e64..e794462f60 100644 --- a/libs/ui/include/ui/GraphicBuffer.h +++ b/libs/ui/include/ui/GraphicBuffer.h @@ -34,7 +34,6 @@ namespace android { -class DetachedBufferHandle; class GraphicBufferMapper; // =========================================================================== @@ -191,11 +190,6 @@ public: status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const; status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count); - // Sets and takes DetachedBuffer. Should only be called from BufferHub. - bool isDetachedBuffer() const; - status_t setDetachedBufferHandle(std::unique_ptr detachedBuffer); - std::unique_ptr takeDetachedBufferHandle(); - private: ~GraphicBuffer(); @@ -246,17 +240,6 @@ private: // match the BufferQueue's internal generation number (set through // IGBP::setGenerationNumber), attempts to attach the buffer will fail. uint32_t mGenerationNumber; - - // Stores a BufferHub handle that can be used to re-attach this GraphicBuffer back into a - // BufferHub producer/consumer set. In terms of GraphicBuffer's relationship with BufferHub, - // there are three different modes: - // 1. Legacy mode: GraphicBuffer is not backed by BufferHub and mDetachedBufferHandle must be - // invalid. - // 2. Detached mode: GraphicBuffer is backed by BufferHub, but not part of a producer/consumer - // set. In this mode, mDetachedBufferHandle must be valid. - // 3. Attached mode: GraphicBuffer is backed by BufferHub and it's part of a producer/consumer - // set. In this mode, mDetachedBufferHandle must be invalid. - std::unique_ptr mDetachedBufferHandle; }; }; // namespace android diff --git a/libs/ui/tests/Android.bp b/libs/ui/tests/Android.bp index e8bda677fe..1521e1d758 100644 --- a/libs/ui/tests/Android.bp +++ b/libs/ui/tests/Android.bp @@ -28,13 +28,6 @@ cc_test { cflags: ["-Wall", "-Werror"], } -cc_test { - name: "GraphicBuffer_test", - shared_libs: ["libpdx_default_transport", "libui", "libutils"], - srcs: ["GraphicBuffer_test.cpp"], - cflags: ["-Wall", "-Werror"], -} - cc_test { name: "BufferHubBuffer_test", header_libs: [ diff --git a/libs/ui/tests/GraphicBuffer_test.cpp b/libs/ui/tests/GraphicBuffer_test.cpp deleted file mode 100644 index eb679ac236..0000000000 --- a/libs/ui/tests/GraphicBuffer_test.cpp +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright 2018 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#define LOG_TAG "GraphicBufferTest" - -#include -#include - -#include - -namespace android { - -namespace { - -constexpr uint32_t kTestWidth = 1024; -constexpr uint32_t kTestHeight = 1; -constexpr uint32_t kTestFormat = HAL_PIXEL_FORMAT_BLOB; -constexpr uint32_t kTestLayerCount = 1; -constexpr uint64_t kTestUsage = GraphicBuffer::USAGE_SW_WRITE_OFTEN; - -} // namespace - -class GraphicBufferTest : public testing::Test {}; - -TEST_F(GraphicBufferTest, DetachedBuffer) { - sp buffer( - new GraphicBuffer(kTestWidth, kTestHeight, kTestFormat, kTestLayerCount, kTestUsage)); - - // Currently a newly allocated GraphicBuffer is in legacy mode, i.e. not associated with - // BufferHub. But this may change in the future. - EXPECT_FALSE(buffer->isDetachedBuffer()); - - pdx::LocalChannelHandle channel{nullptr, 1234}; - EXPECT_TRUE(channel.valid()); - - std::unique_ptr handle = DetachedBufferHandle::Create(std::move(channel)); - EXPECT_FALSE(channel.valid()); - EXPECT_TRUE(handle->isValid()); - EXPECT_TRUE(handle->handle().valid()); - - buffer->setDetachedBufferHandle(std::move(handle)); - EXPECT_TRUE(handle == nullptr); - EXPECT_TRUE(buffer->isDetachedBuffer()); - - handle = buffer->takeDetachedBufferHandle(); - EXPECT_TRUE(handle != nullptr); - EXPECT_TRUE(handle->isValid()); - EXPECT_FALSE(buffer->isDetachedBuffer()); -} - -} // namespace android diff --git a/libs/vr/libbufferhub/buffer_hub-test.cpp b/libs/vr/libbufferhub/buffer_hub-test.cpp index db624de260..b248ecefca 100644 --- a/libs/vr/libbufferhub/buffer_hub-test.cpp +++ b/libs/vr/libbufferhub/buffer_hub-test.cpp @@ -5,7 +5,6 @@ #include #include #include -#include #include #include -- cgit v1.2.3-59-g8ed1b From 069e8389df7aee7e8812a036bcda43c3fe24cabc Mon Sep 17 00:00:00 2001 From: Fan Xu Date: Fri, 16 Nov 2018 16:28:08 -0800 Subject: Move BufferHubMetadata off pdx::fileHandle Use android::base::unique_fd to replace LocalHandle. For BorrowedHandle, a const reference of the unique_fd is returned. Updated the test cases to fit in current behavior. Test: BufferHubMetadata_test, BufferHubBuffer_test, BufferNode_test, buffer_hub-test (passed) Fix: 118888072 Change-Id: I34de335ed9a10864ac226cd4d7d261ba0078045d --- libs/ui/BufferHubBuffer.cpp | 10 +++++++--- libs/ui/BufferHubMetadata.cpp | 24 ++++++++++++------------ libs/ui/include/ui/BufferHubMetadata.h | 24 +++++++++++++----------- libs/ui/tests/BufferHubMetadata_test.cpp | 22 ++++++++++------------ services/vr/bufferhubd/buffer_channel.cpp | 5 ++++- 5 files changed, 46 insertions(+), 39 deletions(-) (limited to 'libs/ui/BufferHubBuffer.cpp') diff --git a/libs/ui/BufferHubBuffer.cpp b/libs/ui/BufferHubBuffer.cpp index 8cc1a4e3b4..dd79775d01 100644 --- a/libs/ui/BufferHubBuffer.cpp +++ b/libs/ui/BufferHubBuffer.cpp @@ -36,10 +36,12 @@ #include #pragma clang diagnostic pop -#include - #include +#include +#include + +using android::base::unique_fd; using android::dvr::BufferTraits; using android::dvr::DetachedBufferRPC; using android::dvr::NativeHandleWrapper; @@ -132,7 +134,9 @@ int BufferHubBuffer::ImportGraphicBuffer() { const int bufferId = bufferTraits.id(); // Import the metadata. - mMetadata = BufferHubMetadata::Import(bufferTraits.take_metadata_handle()); + LocalHandle metadataHandle = bufferTraits.take_metadata_handle(); + unique_fd metadataFd(metadataHandle.Release()); + mMetadata = BufferHubMetadata::Import(std::move(metadataFd)); if (!mMetadata.IsValid()) { ALOGE("BufferHubBuffer::ImportGraphicBuffer: invalid metadata."); diff --git a/libs/ui/BufferHubMetadata.cpp b/libs/ui/BufferHubMetadata.cpp index 1e08ed1388..18d9a2c963 100644 --- a/libs/ui/BufferHubMetadata.cpp +++ b/libs/ui/BufferHubMetadata.cpp @@ -48,47 +48,47 @@ BufferHubMetadata BufferHubMetadata::Create(size_t userMetadataSize) { return {}; } - // Hand over the ownership of the fd to a pdx::LocalHandle immediately after the successful - // return of ashmem_create_region. The ashmemHandle is going to own the fd and to prevent fd + // Hand over the ownership of the fd to a unique_fd immediately after the successful + // return of ashmem_create_region. The ashmemFd is going to own the fd and to prevent fd // leaks during error handling. - pdx::LocalHandle ashmemHandle{fd}; + unique_fd ashmemFd{fd}; - if (ashmem_set_prot_region(ashmemHandle.Get(), kAshmemProt) != 0) { + if (ashmem_set_prot_region(ashmemFd.get(), kAshmemProt) != 0) { ALOGE("BufferHubMetadata::Create: failed to set protect region."); return {}; } - return BufferHubMetadata::Import(std::move(ashmemHandle)); + return BufferHubMetadata::Import(std::move(ashmemFd)); } /* static */ -BufferHubMetadata BufferHubMetadata::Import(pdx::LocalHandle ashmemHandle) { - if (!ashmem_valid(ashmemHandle.Get())) { +BufferHubMetadata BufferHubMetadata::Import(unique_fd ashmemFd) { + if (!ashmem_valid(ashmemFd.get())) { ALOGE("BufferHubMetadata::Import: invalid ashmem fd."); return {}; } - size_t metadataSize = static_cast(ashmem_get_size_region(ashmemHandle.Get())); + size_t metadataSize = static_cast(ashmem_get_size_region(ashmemFd.get())); size_t userMetadataSize = metadataSize - kMetadataHeaderSize; // Note that here the buffer state is mapped from shared memory as an atomic object. The // std::atomic's constructor will not be called so that the original value stored in the memory // region can be preserved. auto metadataHeader = static_cast(mmap(nullptr, metadataSize, kAshmemProt, - MAP_SHARED, ashmemHandle.Get(), + MAP_SHARED, ashmemFd.get(), /*offset=*/0)); if (metadataHeader == nullptr) { ALOGE("BufferHubMetadata::Import: failed to map region."); return {}; } - return BufferHubMetadata(userMetadataSize, std::move(ashmemHandle), metadataHeader); + return BufferHubMetadata(userMetadataSize, std::move(ashmemFd), metadataHeader); } -BufferHubMetadata::BufferHubMetadata(size_t userMetadataSize, pdx::LocalHandle ashmemHandle, +BufferHubMetadata::BufferHubMetadata(size_t userMetadataSize, unique_fd ashmemFd, MetadataHeader* metadataHeader) : mUserMetadataSize(userMetadataSize), - mAshmemHandle(std::move(ashmemHandle)), + mAshmemFd(std::move(ashmemFd)), mMetadataHeader(metadataHeader) {} BufferHubMetadata::~BufferHubMetadata() { diff --git a/libs/ui/include/ui/BufferHubMetadata.h b/libs/ui/include/ui/BufferHubMetadata.h index 94a9000a32..4261971f55 100644 --- a/libs/ui/include/ui/BufferHubMetadata.h +++ b/libs/ui/include/ui/BufferHubMetadata.h @@ -33,12 +33,17 @@ #pragma clang diagnostic ignored "-Wundefined-func-template" #pragma clang diagnostic ignored "-Wunused-template" #pragma clang diagnostic ignored "-Wweak-vtables" -#include #include #pragma clang diagnostic pop +#include + namespace android { +namespace { +using base::unique_fd; +} // namespace + class BufferHubMetadata { public: // Creates a new BufferHubMetadata backed by an ashmem region. @@ -50,11 +55,8 @@ public: // Imports an existing BufferHubMetadata from an ashmem FD. // - // TODO(b/112338294): Refactor BufferHub to use Binder as its internal IPC backend instead of - // UDS. - // - // @param ashmemHandle Ashmem file handle representing an ashmem region. - static BufferHubMetadata Import(pdx::LocalHandle ashmemHandle); + // @param ashmemFd Ashmem file descriptor representing an ashmem region. + static BufferHubMetadata Import(unique_fd ashmemFd); BufferHubMetadata() = default; @@ -67,7 +69,7 @@ public: mUserMetadataSize = other.mUserMetadataSize; other.mUserMetadataSize = 0; - mAshmemHandle = std::move(other.mAshmemHandle); + mAshmemFd = std::move(other.mAshmemFd); // The old raw mMetadataHeader pointer must be cleared, otherwise the destructor will // automatically mummap() the shared memory. @@ -79,25 +81,25 @@ public: // Returns true if the metadata is valid, i.e. the metadata has a valid ashmem fd and the ashmem // has been mapped into virtual address space. - bool IsValid() const { return mAshmemHandle.IsValid() && mMetadataHeader != nullptr; } + bool IsValid() const { return mAshmemFd.get() != -1 && mMetadataHeader != nullptr; } size_t user_metadata_size() const { return mUserMetadataSize; } size_t metadata_size() const { return mUserMetadataSize + dvr::BufferHubDefs::kMetadataHeaderSize; } - const pdx::LocalHandle& ashmem_handle() const { return mAshmemHandle; } + const unique_fd& ashmem_fd() const { return mAshmemFd; } dvr::BufferHubDefs::MetadataHeader* metadata_header() { return mMetadataHeader; } private: - BufferHubMetadata(size_t userMetadataSize, pdx::LocalHandle ashmemHandle, + BufferHubMetadata(size_t userMetadataSize, unique_fd ashmemFd, dvr::BufferHubDefs::MetadataHeader* metadataHeader); BufferHubMetadata(const BufferHubMetadata&) = delete; void operator=(const BufferHubMetadata&) = delete; size_t mUserMetadataSize = 0; - pdx::LocalHandle mAshmemHandle; + unique_fd mAshmemFd; dvr::BufferHubDefs::MetadataHeader* mMetadataHeader = nullptr; }; diff --git a/libs/ui/tests/BufferHubMetadata_test.cpp b/libs/ui/tests/BufferHubMetadata_test.cpp index 4209392afa..70f86b3ef2 100644 --- a/libs/ui/tests/BufferHubMetadata_test.cpp +++ b/libs/ui/tests/BufferHubMetadata_test.cpp @@ -43,11 +43,11 @@ TEST_F(BufferHubMetadataTest, Import_Success) { EXPECT_TRUE(m1.IsValid()); EXPECT_NE(m1.metadata_header(), nullptr); - pdx::LocalHandle h2 = m1.ashmem_handle().Duplicate(); - EXPECT_TRUE(h2.IsValid()); + unique_fd h2 = unique_fd(dup(m1.ashmem_fd().get())); + EXPECT_NE(h2.get(), -1); BufferHubMetadata m2 = BufferHubMetadata::Import(std::move(h2)); - EXPECT_FALSE(h2.IsValid()); + EXPECT_EQ(h2.get(), -1); EXPECT_TRUE(m1.IsValid()); BufferHubDefs::MetadataHeader* mh1 = m1.metadata_header(); EXPECT_NE(mh1, nullptr); @@ -71,31 +71,29 @@ TEST_F(BufferHubMetadataTest, MoveMetadataInvalidatesOldOne) { BufferHubMetadata m1 = BufferHubMetadata::Create(sizeof(int)); EXPECT_TRUE(m1.IsValid()); EXPECT_NE(m1.metadata_header(), nullptr); - EXPECT_TRUE(m1.ashmem_handle().IsValid()); + EXPECT_NE(m1.ashmem_fd().get(), -1); EXPECT_EQ(m1.user_metadata_size(), sizeof(int)); BufferHubMetadata m2 = std::move(m1); - // After the move, the metadata header (a raw pointer) should be reset in the - // older buffer. + // After the move, the metadata header (a raw pointer) should be reset in the older buffer. EXPECT_EQ(m1.metadata_header(), nullptr); EXPECT_NE(m2.metadata_header(), nullptr); - EXPECT_FALSE(m1.ashmem_handle().IsValid()); - EXPECT_TRUE(m2.ashmem_handle().IsValid()); + EXPECT_EQ(m1.ashmem_fd().get(), -1); + EXPECT_NE(m2.ashmem_fd().get(), -1); EXPECT_EQ(m1.user_metadata_size(), 0U); EXPECT_EQ(m2.user_metadata_size(), sizeof(int)); BufferHubMetadata m3{std::move(m2)}; - // After the move, the metadata header (a raw pointer) should be reset in the - // older buffer. + // After the move, the metadata header (a raw pointer) should be reset in the older buffer. EXPECT_EQ(m2.metadata_header(), nullptr); EXPECT_NE(m3.metadata_header(), nullptr); - EXPECT_FALSE(m2.ashmem_handle().IsValid()); - EXPECT_TRUE(m3.ashmem_handle().IsValid()); + EXPECT_EQ(m2.ashmem_fd().get(), -1); + EXPECT_NE(m3.ashmem_fd().get(), -1); EXPECT_EQ(m2.user_metadata_size(), 0U); EXPECT_EQ(m3.user_metadata_size(), sizeof(int)); diff --git a/services/vr/bufferhubd/buffer_channel.cpp b/services/vr/bufferhubd/buffer_channel.cpp index 589b31a285..6c64bcd279 100644 --- a/services/vr/bufferhubd/buffer_channel.cpp +++ b/services/vr/bufferhubd/buffer_channel.cpp @@ -83,10 +83,13 @@ Status> BufferChannel::OnImport( ATRACE_NAME("BufferChannel::OnImport"); ALOGD_IF(TRACE, "BufferChannel::OnImport: buffer=%d.", buffer_id()); + BorrowedHandle ashmem_handle = + BorrowedHandle(buffer_node_->metadata().ashmem_fd().get()); + // TODO(b/112057680) Move away from the GraphicBuffer-based IonBuffer. return BufferTraits{ /*buffer_handle=*/buffer_node_->buffer_handle(), - /*metadata_handle=*/buffer_node_->metadata().ashmem_handle().Borrow(), + /*metadata_handle=*/ashmem_handle, /*id=*/buffer_id(), /*client_state_mask=*/client_state_mask_, /*metadata_size=*/buffer_node_->metadata().metadata_size(), -- cgit v1.2.3-59-g8ed1b From 2daf5189a8f9c28a804a550f3734c5617bac17aa Mon Sep 17 00:00:00 2001 From: Jiwen 'Steve' Cai Date: Tue, 16 Oct 2018 00:14:03 -0700 Subject: Create GraphicBuffer backed by BufferHubBuffer The newly created GraphicBuffer shall own the BufferHubBuffer object through out its life cycle. Bug: 70912269 Bug: 111976433 Test: GraphicBuffer_test Change-Id: I698573e26f85dd40d30c267aeea545e65a7e2a8b --- libs/ui/Android.bp | 4 +++ libs/ui/BufferHubBuffer.cpp | 10 +++++++ libs/ui/GraphicBuffer.cpp | 25 +++++++++++++++++ libs/ui/include/ui/BufferHubBuffer.h | 13 +++++++-- libs/ui/include/ui/GraphicBuffer.h | 19 +++++++++++++ libs/ui/tests/Android.bp | 16 +++++++++++ libs/ui/tests/GraphicBuffer_test.cpp | 54 ++++++++++++++++++++++++++++++++++++ 7 files changed, 138 insertions(+), 3 deletions(-) create mode 100644 libs/ui/tests/GraphicBuffer_test.cpp (limited to 'libs/ui/BufferHubBuffer.cpp') diff --git a/libs/ui/Android.bp b/libs/ui/Android.bp index 0a0c8ca014..f7709754c8 100644 --- a/libs/ui/Android.bp +++ b/libs/ui/Android.bp @@ -109,6 +109,7 @@ cc_library_shared { // bufferhub is not used when building libgui for vendors target: { vendor: { + cflags: ["-DLIBUI_IN_VNDK"], exclude_srcs: [ "BufferHubBuffer.cpp", "BufferHubMetadata.cpp", @@ -116,6 +117,7 @@ cc_library_shared { exclude_header_libs: [ "libbufferhub_headers", "libdvr_headers", + "libnativewindow_headers", ], exclude_shared_libs: [ "libpdx_default_transport", @@ -128,6 +130,7 @@ cc_library_shared { "libbufferhub_headers", "libdvr_headers", "libnativebase_headers", + "libnativewindow_headers", "libhardware_headers", "libui_headers", "libpdx_headers", @@ -155,6 +158,7 @@ cc_library_headers { vendor_available: true, target: { vendor: { + cflags: ["-DLIBUI_IN_VNDK"], override_export_include_dirs: ["include_vndk"], }, }, diff --git a/libs/ui/BufferHubBuffer.cpp b/libs/ui/BufferHubBuffer.cpp index dd79775d01..e747ee11be 100644 --- a/libs/ui/BufferHubBuffer.cpp +++ b/libs/ui/BufferHubBuffer.cpp @@ -160,6 +160,16 @@ int BufferHubBuffer::ImportGraphicBuffer() { // GraphicBuffer instance can be created in future. mBufferHandle = bufferTraits.take_buffer_handle(); + // Populate buffer desc based on buffer traits. + mBufferDesc.width = bufferTraits.width(); + mBufferDesc.height = bufferTraits.height(); + mBufferDesc.layers = bufferTraits.layer_count(); + mBufferDesc.format = bufferTraits.format(); + mBufferDesc.usage = bufferTraits.usage(); + mBufferDesc.stride = bufferTraits.stride(); + mBufferDesc.rfu0 = 0U; + mBufferDesc.rfu1 = 0U; + // If all imports succeed, replace the previous buffer and id. mId = bufferId; mClientStateMask = bufferTraits.client_state_mask(); diff --git a/libs/ui/GraphicBuffer.cpp b/libs/ui/GraphicBuffer.cpp index 29deb010e8..e606e26f8b 100644 --- a/libs/ui/GraphicBuffer.cpp +++ b/libs/ui/GraphicBuffer.cpp @@ -22,6 +22,10 @@ #include +#ifndef LIBUI_IN_VNDK +#include +#endif // LIBUI_IN_VNDK + #include #include #include @@ -89,6 +93,21 @@ GraphicBuffer::GraphicBuffer(const native_handle_t* inHandle, HandleWrapMethod m inUsage, inStride); } +#ifndef LIBUI_IN_VNDK +GraphicBuffer::GraphicBuffer(std::unique_ptr buffer) : GraphicBuffer() { + if (buffer == nullptr) { + mInitCheck = BAD_VALUE; + return; + } + + mInitCheck = initWithHandle(buffer->DuplicateHandle(), /*method=*/TAKE_UNREGISTERED_HANDLE, + buffer->desc().width, buffer->desc().height, + static_cast(buffer->desc().format), + buffer->desc().layers, buffer->desc().usage, buffer->desc().stride); + mBufferHubBuffer = std::move(buffer); +} +#endif // LIBUI_IN_VNDK + GraphicBuffer::~GraphicBuffer() { if (handle) { @@ -483,6 +502,12 @@ status_t GraphicBuffer::unflatten( return NO_ERROR; } +#ifndef LIBUI_IN_VNDK +bool GraphicBuffer::isBufferHubBuffer() const { + return mBufferHubBuffer != nullptr; +} +#endif // LIBUI_IN_VNDK + // --------------------------------------------------------------------------- }; // namespace android diff --git a/libs/ui/include/ui/BufferHubBuffer.h b/libs/ui/include/ui/BufferHubBuffer.h index daf61928a4..6850b43f8a 100644 --- a/libs/ui/include/ui/BufferHubBuffer.h +++ b/libs/ui/include/ui/BufferHubBuffer.h @@ -38,6 +38,7 @@ #include #pragma clang diagnostic pop +#include #include namespace android { @@ -62,9 +63,9 @@ public: // Allocates a standalone BufferHubBuffer not associated with any producer consumer set. static std::unique_ptr Create(uint32_t width, uint32_t height, uint32_t layerCount, uint32_t format, - uint64_t usage, size_t mUserMetadataSize) { + uint64_t usage, size_t userMetadataSize) { return std::unique_ptr( - new BufferHubBuffer(width, height, layerCount, format, usage, mUserMetadataSize)); + new BufferHubBuffer(width, height, layerCount, format, usage, userMetadataSize)); } // Imports the given channel handle to a BufferHubBuffer, taking ownership. @@ -79,6 +80,9 @@ public: // bufferhubd share the same buffer id. int id() const { return mId; } + // Returns the buffer description, which is guaranteed to be faithful values from bufferhubd. + const AHardwareBuffer_Desc& desc() const { return mBufferDesc; } + const native_handle_t* DuplicateHandle() { return mBufferHandle.DuplicateHandle(); } // Returns the current value of MetadataHeader::buffer_state. @@ -118,7 +122,7 @@ public: private: BufferHubBuffer(uint32_t width, uint32_t height, uint32_t layerCount, uint32_t format, - uint64_t usage, size_t mUserMetadataSize); + uint64_t usage, size_t userMetadataSize); BufferHubBuffer(pdx::LocalChannelHandle mChannelHandle); @@ -128,6 +132,9 @@ private: int mId = -1; uint64_t mClientStateMask = 0; + // Stores ground truth of the buffer. + AHardwareBuffer_Desc mBufferDesc; + // Wrapps the gralloc buffer handle of this buffer. dvr::NativeHandleWrapper mBufferHandle; diff --git a/libs/ui/include/ui/GraphicBuffer.h b/libs/ui/include/ui/GraphicBuffer.h index fe6229ab48..81f6cd9061 100644 --- a/libs/ui/include/ui/GraphicBuffer.h +++ b/libs/ui/include/ui/GraphicBuffer.h @@ -34,6 +34,10 @@ namespace android { +#ifndef LIBUI_IN_VNDK +class BufferHubBuffer; +#endif // LIBUI_IN_VNDK + class GraphicBufferMapper; // =========================================================================== @@ -133,6 +137,11 @@ public: GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat, uint32_t inUsage, std::string requestorName = ""); +#ifndef LIBUI_IN_VNDK + // Create a GraphicBuffer from an existing BufferHubBuffer. + GraphicBuffer(std::unique_ptr buffer); +#endif // LIBUI_IN_VNDK + // return status status_t initCheck() const; @@ -188,6 +197,11 @@ public: status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const; status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count); +#ifndef LIBUI_IN_VNDK + // Returns whether this GraphicBuffer is backed by BufferHubBuffer. + bool isBufferHubBuffer() const; +#endif // LIBUI_IN_VNDK + private: ~GraphicBuffer(); @@ -237,6 +251,11 @@ private: // match the BufferQueue's internal generation number (set through // IGBP::setGenerationNumber), attempts to attach the buffer will fail. uint32_t mGenerationNumber; + +#ifndef LIBUI_IN_VNDK + // Stores a BufferHubBuffer that handles buffer signaling, identification. + std::unique_ptr mBufferHubBuffer; +#endif // LIBUI_IN_VNDK }; }; // namespace android diff --git a/libs/ui/tests/Android.bp b/libs/ui/tests/Android.bp index 4c9c1768e0..b7ad4e5a80 100644 --- a/libs/ui/tests/Android.bp +++ b/libs/ui/tests/Android.bp @@ -28,6 +28,22 @@ cc_test { cflags: ["-Wall", "-Werror"], } +cc_test { + name: "GraphicBuffer_test", + header_libs: [ + "libbufferhub_headers", + "libdvr_headers", + "libnativewindow_headers", + ], + shared_libs: [ + "libpdx_default_transport", + "libui", + "libutils", + ], + srcs: ["GraphicBuffer_test.cpp"], + cflags: ["-Wall", "-Werror"], +} + cc_test { name: "BufferHubBuffer_test", header_libs: [ diff --git a/libs/ui/tests/GraphicBuffer_test.cpp b/libs/ui/tests/GraphicBuffer_test.cpp new file mode 100644 index 0000000000..95ca2c16b3 --- /dev/null +++ b/libs/ui/tests/GraphicBuffer_test.cpp @@ -0,0 +1,54 @@ +/* + * Copyright 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define LOG_TAG "GraphicBufferTest" + +#include +#include + +#include + +namespace android { + +namespace { + +constexpr uint32_t kTestWidth = 1024; +constexpr uint32_t kTestHeight = 1; +constexpr uint32_t kTestFormat = HAL_PIXEL_FORMAT_BLOB; +constexpr uint32_t kTestLayerCount = 1; +constexpr uint64_t kTestUsage = GraphicBuffer::USAGE_SW_WRITE_OFTEN; + +} // namespace + +class GraphicBufferTest : public testing::Test {}; + +TEST_F(GraphicBufferTest, CreateFromBufferHubBuffer) { + std::unique_ptr b1 = + BufferHubBuffer::Create(kTestWidth, kTestHeight, kTestLayerCount, kTestFormat, + kTestUsage, /*userMetadataSize=*/0); + EXPECT_TRUE(b1->IsValid()); + + sp gb(new GraphicBuffer(std::move(b1))); + EXPECT_TRUE(gb->isBufferHubBuffer()); + + EXPECT_EQ(gb->getWidth(), kTestWidth); + EXPECT_EQ(gb->getHeight(), kTestHeight); + EXPECT_EQ(static_cast(gb->getPixelFormat()), kTestFormat); + EXPECT_EQ(gb->getUsage(), kTestUsage); + EXPECT_EQ(gb->getLayerCount(), kTestLayerCount); +} + +} // namespace android -- cgit v1.2.3-59-g8ed1b From b08b72261ae14b76fc8bffe6ddd4cb344efdd34d Mon Sep 17 00:00:00 2001 From: Tianyu Jiang Date: Fri, 16 Nov 2018 17:55:26 -0800 Subject: Implement changes of buffer state in Gain/Post/Acquire/Release methods in BufferHubBuffer. Create shortcuts to atomics in BufferHubBuffer; Implement changes of buffer state in the four usages of the buffer; Create 16 unit tests for the 16 buffer state transitions. Test: BufferHubBuffer_test Bug: 118718711 Change-Id: I2067cd141611e66732e28344f26d73f261072b8b --- libs/ui/BufferHubBuffer.cpp | 108 ++++++++++++++++- libs/ui/include/ui/BufferHubBuffer.h | 28 +++++ libs/ui/tests/BufferHubBuffer_test.cpp | 213 +++++++++++++++++++++++++++++++++ 3 files changed, 346 insertions(+), 3 deletions(-) (limited to 'libs/ui/BufferHubBuffer.cpp') diff --git a/libs/ui/BufferHubBuffer.cpp b/libs/ui/BufferHubBuffer.cpp index e747ee11be..3816c1bc4f 100644 --- a/libs/ui/BufferHubBuffer.cpp +++ b/libs/ui/BufferHubBuffer.cpp @@ -61,6 +61,16 @@ namespace { // to use Binder. static constexpr char kBufferHubClientPath[] = "system/buffer_hub/client"; +using dvr::BufferHubDefs::AnyClientAcquired; +using dvr::BufferHubDefs::AnyClientGained; +using dvr::BufferHubDefs::AnyClientPosted; +using dvr::BufferHubDefs::IsClientAcquired; +using dvr::BufferHubDefs::IsClientGained; +using dvr::BufferHubDefs::IsClientPosted; +using dvr::BufferHubDefs::IsClientReleased; +using dvr::BufferHubDefs::kHighBitsMask; +using dvr::BufferHubDefs::kMetadataHeaderSize; + } // namespace BufferHubClient::BufferHubClient() : Client(ClientChannelFactory::Create(kBufferHubClientPath)) {} @@ -71,7 +81,7 @@ BufferHubClient::BufferHubClient(LocalChannelHandle mChannelHandle) BufferHubClient::~BufferHubClient() {} bool BufferHubClient::IsValid() const { - return IsConnected() && GetChannelHandle().valid(); + return IsConnected() && GetChannelHandle().valid(); } LocalChannelHandle BufferHubClient::TakeChannelHandle() { @@ -151,11 +161,17 @@ int BufferHubBuffer::ImportGraphicBuffer() { } size_t metadataSize = static_cast(bufferTraits.metadata_size()); - if (metadataSize < dvr::BufferHubDefs::kMetadataHeaderSize) { + if (metadataSize < kMetadataHeaderSize) { ALOGE("BufferHubBuffer::ImportGraphicBuffer: metadata too small: %zu", metadataSize); return -EINVAL; } + // Populate shortcuts to the atomics in metadata. + auto metadata_header = mMetadata.metadata_header(); + buffer_state_ = &metadata_header->buffer_state; + fence_state_ = &metadata_header->fence_state; + active_clients_bit_mask_ = &metadata_header->active_clients_bit_mask; + // Import the buffer: We only need to hold on the native_handle_t here so that // GraphicBuffer instance can be created in future. mBufferHandle = bufferTraits.take_buffer_handle(); @@ -176,7 +192,93 @@ int BufferHubBuffer::ImportGraphicBuffer() { // TODO(b/112012161) Set up shared fences. ALOGD("BufferHubBuffer::ImportGraphicBuffer: id=%d, buffer_state=%" PRIx64 ".", id(), - mMetadata.metadata_header()->buffer_state.load(std::memory_order_acquire)); + buffer_state_->load(std::memory_order_acquire)); + return 0; +} + +int BufferHubBuffer::Gain() { + uint64_t current_buffer_state = buffer_state_->load(std::memory_order_acquire); + if (IsClientGained(current_buffer_state, mClientStateMask)) { + ALOGV("%s: Buffer is already gained by this client %" PRIx64 ".", __FUNCTION__, + mClientStateMask); + return 0; + } + do { + if (AnyClientGained(current_buffer_state & (~mClientStateMask)) || + AnyClientAcquired(current_buffer_state)) { + ALOGE("%s: Buffer is in use, id=%d mClientStateMask=%" PRIx64 " state=%" PRIx64 ".", + __FUNCTION__, mId, mClientStateMask, current_buffer_state); + return -EBUSY; + } + // Change the buffer state to gained state, whose value happens to be the same as + // mClientStateMask. + } while (!buffer_state_->compare_exchange_weak(current_buffer_state, mClientStateMask, + std::memory_order_acq_rel, + std::memory_order_acquire)); + // TODO(b/119837586): Update fence state and return GPU fence. + return 0; +} + +int BufferHubBuffer::Post() { + uint64_t current_buffer_state = buffer_state_->load(std::memory_order_acquire); + uint64_t current_active_clients_bit_mask = 0ULL; + uint64_t updated_buffer_state = 0ULL; + do { + if (!IsClientGained(current_buffer_state, mClientStateMask)) { + ALOGE("%s: Cannot post a buffer that is not gained by this client. buffer_id=%d " + "mClientStateMask=%" PRIx64 " state=%" PRIx64 ".", + __FUNCTION__, mId, mClientStateMask, current_buffer_state); + return -EBUSY; + } + // Set the producer client buffer state to released, other clients' buffer state to posted. + current_active_clients_bit_mask = active_clients_bit_mask_->load(std::memory_order_acquire); + updated_buffer_state = + current_active_clients_bit_mask & (~mClientStateMask) & kHighBitsMask; + } while (!buffer_state_->compare_exchange_weak(current_buffer_state, updated_buffer_state, + std::memory_order_acq_rel, + std::memory_order_acquire)); + // TODO(b/119837586): Update fence state and return GPU fence if needed. + return 0; +} + +int BufferHubBuffer::Acquire() { + uint64_t current_buffer_state = buffer_state_->load(std::memory_order_acquire); + if (IsClientAcquired(current_buffer_state, mClientStateMask)) { + ALOGV("%s: Buffer is already acquired by this client %" PRIx64 ".", __FUNCTION__, + mClientStateMask); + return 0; + } + uint64_t updated_buffer_state = 0ULL; + do { + if (!IsClientPosted(current_buffer_state, mClientStateMask)) { + ALOGE("%s: Cannot acquire a buffer that is not in posted state. buffer_id=%d " + "mClientStateMask=%" PRIx64 " state=%" PRIx64 ".", + __FUNCTION__, mId, mClientStateMask, current_buffer_state); + return -EBUSY; + } + // Change the buffer state for this consumer from posted to acquired. + updated_buffer_state = current_buffer_state ^ mClientStateMask; + } while (!buffer_state_->compare_exchange_weak(current_buffer_state, updated_buffer_state, + std::memory_order_acq_rel, + std::memory_order_acquire)); + // TODO(b/119837586): Update fence state and return GPU fence. + return 0; +} + +int BufferHubBuffer::Release() { + uint64_t current_buffer_state = buffer_state_->load(std::memory_order_acquire); + if (IsClientReleased(current_buffer_state, mClientStateMask)) { + ALOGV("%s: Buffer is already released by this client %" PRIx64 ".", __FUNCTION__, + mClientStateMask); + return 0; + } + uint64_t updated_buffer_state = 0ULL; + do { + updated_buffer_state = current_buffer_state & (~mClientStateMask); + } while (!buffer_state_->compare_exchange_weak(current_buffer_state, updated_buffer_state, + std::memory_order_acq_rel, + std::memory_order_acquire)); + // TODO(b/119837586): Update fence state and return GPU fence if needed. return 0; } diff --git a/libs/ui/include/ui/BufferHubBuffer.h b/libs/ui/include/ui/BufferHubBuffer.h index 6850b43f8a..03d10e7a9a 100644 --- a/libs/ui/include/ui/BufferHubBuffer.h +++ b/libs/ui/include/ui/BufferHubBuffer.h @@ -103,6 +103,27 @@ public: // to read from and/or write into. bool IsValid() const { return mBufferHandle.IsValid(); } + // Gains the buffer for exclusive write permission. Read permission is implied once a buffer is + // gained. + // The buffer can be gained as long as there is no other client in acquired or gained state. + int Gain(); + + // Posts the gained buffer for other buffer clients to use the buffer. + // The buffer can be posted iff the buffer state for this client is gained. + // After posting the buffer, this client is put to released state and does not have access to + // the buffer for this cycle of the usage of the buffer. + int Post(); + + // Acquires the buffer for shared read permission. + // The buffer can be acquired iff the buffer state for this client is posted. + int Acquire(); + + // Releases the buffer. + // The buffer can be released from any buffer state. + // After releasing the buffer, this client no longer have any permissions to the buffer for the + // current cycle of the usage of the buffer. + int Release(); + // Returns the event mask for all the events that are pending on this buffer (see sys/poll.h for // all possible bits). pdx::Status GetEventMask(int events) { @@ -130,6 +151,9 @@ private: // Global id for the buffer that is consistent across processes. int mId = -1; + + // Client state mask of this BufferHubBuffer object. It is unique amoung all + // clients/users of the buffer. uint64_t mClientStateMask = 0; // Stores ground truth of the buffer. @@ -141,6 +165,10 @@ private: // An ashmem-based metadata object. The same shared memory are mapped to the // bufferhubd daemon and all buffer clients. BufferHubMetadata mMetadata; + // Shortcuts to the atomics inside the header of mMetadata. + std::atomic* buffer_state_{nullptr}; + std::atomic* fence_state_{nullptr}; + std::atomic* active_clients_bit_mask_{nullptr}; // PDX backend. BufferHubClient mClient; diff --git a/libs/ui/tests/BufferHubBuffer_test.cpp b/libs/ui/tests/BufferHubBuffer_test.cpp index 6c7d06bd0d..e33acf6d11 100644 --- a/libs/ui/tests/BufferHubBuffer_test.cpp +++ b/libs/ui/tests/BufferHubBuffer_test.cpp @@ -36,7 +36,14 @@ const int kFormat = HAL_PIXEL_FORMAT_RGBA_8888; const int kUsage = 0; const size_t kUserMetadataSize = 0; +using dvr::BufferHubDefs::AnyClientAcquired; +using dvr::BufferHubDefs::AnyClientGained; +using dvr::BufferHubDefs::AnyClientPosted; using dvr::BufferHubDefs::IsBufferReleased; +using dvr::BufferHubDefs::IsClientAcquired; +using dvr::BufferHubDefs::IsClientGained; +using dvr::BufferHubDefs::IsClientPosted; +using dvr::BufferHubDefs::IsClientReleased; using dvr::BufferHubDefs::kFirstClientBitMask; using dvr::BufferHubDefs::kMetadataHeaderSize; using frameworks::bufferhub::V1_0::BufferHubStatus; @@ -48,9 +55,40 @@ using hidl::base::V1_0::IBase; using pdx::LocalChannelHandle; class BufferHubBufferTest : public ::testing::Test { +protected: void SetUp() override { android::hardware::ProcessState::self()->startThreadPool(); } }; +class BufferHubBufferStateTransitionTest : public BufferHubBufferTest { +protected: + void SetUp() override { + BufferHubBufferTest::SetUp(); + CreateTwoClientsOfABuffer(); + } + + std::unique_ptr b1; + uint64_t b1ClientMask = 0ULL; + std::unique_ptr b2; + uint64_t b2ClientMask = 0ULL; + +private: + // Creates b1 and b2 as the clients of the same buffer for testing. + void CreateTwoClientsOfABuffer(); +}; + +void BufferHubBufferStateTransitionTest::CreateTwoClientsOfABuffer() { + b1 = BufferHubBuffer::Create(kWidth, kHeight, kLayerCount, kFormat, kUsage, kUserMetadataSize); + b1ClientMask = b1->client_state_mask(); + ASSERT_NE(b1ClientMask, 0ULL); + auto statusOrHandle = b1->Duplicate(); + ASSERT_TRUE(statusOrHandle); + LocalChannelHandle h2 = statusOrHandle.take(); + b2 = BufferHubBuffer::Import(std::move(h2)); + b2ClientMask = b2->client_state_mask(); + ASSERT_NE(b2ClientMask, 0ULL); + ASSERT_NE(b2ClientMask, b1ClientMask); +} + TEST_F(BufferHubBufferTest, CreateBufferHubBufferFails) { // Buffer Creation will fail: BLOB format requires height to be 1. auto b1 = BufferHubBuffer::Create(kWidth, /*height=*/2, kLayerCount, @@ -315,5 +353,180 @@ TEST_F(BufferHubBufferTest, ImportFreedBuffer) { EXPECT_EQ(nullptr, client2.get()); } +TEST_F(BufferHubBufferStateTransitionTest, GainBuffer_fromReleasedState) { + ASSERT_TRUE(IsBufferReleased(b1->buffer_state())); + + // Successful gaining the buffer should change the buffer state bit of b1 to + // gained state, other client state bits to released state. + EXPECT_EQ(b1->Gain(), 0); + EXPECT_TRUE(IsClientGained(b1->buffer_state(), b1ClientMask)); +} + +TEST_F(BufferHubBufferStateTransitionTest, GainBuffer_fromGainedState) { + ASSERT_EQ(b1->Gain(), 0); + auto current_buffer_state = b1->buffer_state(); + ASSERT_TRUE(IsClientGained(current_buffer_state, b1ClientMask)); + + // Gaining from gained state by the same client should not return error. + EXPECT_EQ(b1->Gain(), 0); + + // Gaining from gained state by another client should return error. + EXPECT_EQ(b2->Gain(), -EBUSY); +} + +TEST_F(BufferHubBufferStateTransitionTest, GainBuffer_fromAcquiredState) { + ASSERT_EQ(b1->Gain(), 0); + ASSERT_EQ(b1->Post(), 0); + ASSERT_EQ(b2->Acquire(), 0); + ASSERT_TRUE(AnyClientAcquired(b1->buffer_state())); + + // Gaining from acquired state should fail. + EXPECT_EQ(b1->Gain(), -EBUSY); + EXPECT_EQ(b2->Gain(), -EBUSY); +} + +TEST_F(BufferHubBufferStateTransitionTest, GainBuffer_fromOtherClientInPostedState) { + ASSERT_EQ(b1->Gain(), 0); + ASSERT_EQ(b1->Post(), 0); + ASSERT_TRUE(AnyClientPosted(b1->buffer_state())); + + // Gaining a buffer who has other posted client should succeed. + EXPECT_EQ(b1->Gain(), 0); +} + +TEST_F(BufferHubBufferStateTransitionTest, GainBuffer_fromSelfInPostedState) { + ASSERT_EQ(b1->Gain(), 0); + ASSERT_EQ(b1->Post(), 0); + ASSERT_TRUE(AnyClientPosted(b1->buffer_state())); + + // A posted client should be able to gain the buffer when there is no other clients in + // acquired state. + EXPECT_EQ(b2->Gain(), 0); +} + +TEST_F(BufferHubBufferStateTransitionTest, PostBuffer_fromOtherInGainedState) { + ASSERT_EQ(b1->Gain(), 0); + ASSERT_TRUE(IsClientGained(b1->buffer_state(), b1ClientMask)); + + EXPECT_EQ(b2->Post(), -EBUSY); +} + +TEST_F(BufferHubBufferStateTransitionTest, PostBuffer_fromSelfInGainedState) { + ASSERT_EQ(b1->Gain(), 0); + ASSERT_TRUE(IsClientGained(b1->buffer_state(), b1ClientMask)); + + EXPECT_EQ(b1->Post(), 0); + auto current_buffer_state = b1->buffer_state(); + EXPECT_TRUE(IsClientReleased(current_buffer_state, b1ClientMask)); + EXPECT_TRUE(IsClientPosted(current_buffer_state, b2ClientMask)); +} + +TEST_F(BufferHubBufferStateTransitionTest, PostBuffer_fromPostedState) { + ASSERT_EQ(b1->Gain(), 0); + ASSERT_EQ(b1->Post(), 0); + ASSERT_TRUE(AnyClientPosted(b1->buffer_state())); + + // Post from posted state should fail. + EXPECT_EQ(b1->Post(), -EBUSY); + EXPECT_EQ(b2->Post(), -EBUSY); +} + +TEST_F(BufferHubBufferStateTransitionTest, PostBuffer_fromAcquiredState) { + ASSERT_EQ(b1->Gain(), 0); + ASSERT_EQ(b1->Post(), 0); + ASSERT_EQ(b2->Acquire(), 0); + ASSERT_TRUE(AnyClientAcquired(b1->buffer_state())); + + // Posting from acquired state should fail. + EXPECT_EQ(b1->Post(), -EBUSY); + EXPECT_EQ(b2->Post(), -EBUSY); +} + +TEST_F(BufferHubBufferStateTransitionTest, PostBuffer_fromReleasedState) { + ASSERT_TRUE(IsBufferReleased(b1->buffer_state())); + + // Posting from released state should fail. + EXPECT_EQ(b1->Post(), -EBUSY); + EXPECT_EQ(b2->Post(), -EBUSY); +} + +TEST_F(BufferHubBufferStateTransitionTest, AcquireBuffer_fromSelfInPostedState) { + ASSERT_EQ(b1->Gain(), 0); + ASSERT_EQ(b1->Post(), 0); + ASSERT_TRUE(IsClientPosted(b1->buffer_state(), b2ClientMask)); + + // Acquire from posted state should pass. + EXPECT_EQ(b2->Acquire(), 0); +} + +TEST_F(BufferHubBufferStateTransitionTest, AcquireBuffer_fromOtherInPostedState) { + ASSERT_EQ(b1->Gain(), 0); + ASSERT_EQ(b1->Post(), 0); + ASSERT_TRUE(IsClientPosted(b1->buffer_state(), b2ClientMask)); + + // Acquire from released state should fail, although there are other clients + // in posted state. + EXPECT_EQ(b1->Acquire(), -EBUSY); +} + +TEST_F(BufferHubBufferStateTransitionTest, AcquireBuffer_fromSelfInAcquiredState) { + ASSERT_EQ(b1->Gain(), 0); + ASSERT_EQ(b1->Post(), 0); + ASSERT_EQ(b2->Acquire(), 0); + auto current_buffer_state = b1->buffer_state(); + ASSERT_TRUE(IsClientAcquired(current_buffer_state, b2ClientMask)); + + // Acquiring from acquired state by the same client should not error out. + EXPECT_EQ(b2->Acquire(), 0); +} + +TEST_F(BufferHubBufferStateTransitionTest, AcquireBuffer_fromReleasedState) { + ASSERT_TRUE(IsBufferReleased(b1->buffer_state())); + + // Acquiring form released state should fail. + EXPECT_EQ(b1->Acquire(), -EBUSY); + EXPECT_EQ(b2->Acquire(), -EBUSY); +} + +TEST_F(BufferHubBufferStateTransitionTest, AcquireBuffer_fromGainedState) { + ASSERT_EQ(b1->Gain(), 0); + ASSERT_TRUE(AnyClientGained(b1->buffer_state())); + + // Acquiring from gained state should fail. + EXPECT_EQ(b1->Acquire(), -EBUSY); + EXPECT_EQ(b2->Acquire(), -EBUSY); +} + +TEST_F(BufferHubBufferStateTransitionTest, ReleaseBuffer_fromSelfInReleasedState) { + ASSERT_TRUE(IsBufferReleased(b1->buffer_state())); + + EXPECT_EQ(b1->Release(), 0); +} + +TEST_F(BufferHubBufferStateTransitionTest, ReleaseBuffer_fromSelfInGainedState) { + ASSERT_TRUE(IsBufferReleased(b1->buffer_state())); + ASSERT_EQ(b1->Gain(), 0); + ASSERT_TRUE(AnyClientGained(b1->buffer_state())); + + EXPECT_EQ(b1->Release(), 0); +} + +TEST_F(BufferHubBufferStateTransitionTest, ReleaseBuffer_fromSelfInPostedState) { + ASSERT_EQ(b1->Gain(), 0); + ASSERT_EQ(b1->Post(), 0); + ASSERT_TRUE(AnyClientPosted(b1->buffer_state())); + + EXPECT_EQ(b2->Release(), 0); +} + +TEST_F(BufferHubBufferStateTransitionTest, ReleaseBuffer_fromSelfInAcquiredState) { + ASSERT_EQ(b1->Gain(), 0); + ASSERT_EQ(b1->Post(), 0); + ASSERT_EQ(b2->Acquire(), 0); + ASSERT_TRUE(AnyClientAcquired(b1->buffer_state())); + + EXPECT_EQ(b2->Release(), 0); +} + } // namespace } // namespace android -- cgit v1.2.3-59-g8ed1b From cfbe07453ebd7cde74a7ed75568ee2c81d726049 Mon Sep 17 00:00:00 2001 From: Fan Xu Date: Wed, 21 Nov 2018 15:03:32 -0800 Subject: Move MetadataHeader to libui To remove BufferHubMetadata off pdx dependency. Note that the __attribute__(packed) is removed as part of this CL, as it's not really needed and is triggering clang warnings. Test: build passed. Not test needed as no behavior changes. Bug: 118893702 Change-Id: Ifae94a143a2bedef68a653c57f089b95d166e6d7 --- libs/ui/BufferHubBuffer.cpp | 4 +- libs/ui/BufferHubMetadata.cpp | 5 +- libs/ui/include/ui/BufferHubDefs.h | 64 ++++++++++++++++++++++ libs/ui/include/ui/BufferHubMetadata.h | 30 ++-------- libs/ui/tests/Android.bp | 7 ++- libs/ui/tests/BufferHubMetadata_test.cpp | 2 + .../include/private/dvr/buffer_hub_defs.h | 36 ++---------- libs/vr/libdvr/include/dvr/dvr_api.h | 1 + services/bufferhub/BufferClient.cpp | 1 + services/bufferhub/include/bufferhub/BufferNode.h | 1 + services/bufferhub/tests/BufferNode_test.cpp | 4 +- 11 files changed, 91 insertions(+), 64 deletions(-) create mode 100644 libs/ui/include/ui/BufferHubDefs.h (limited to 'libs/ui/BufferHubBuffer.cpp') diff --git a/libs/ui/BufferHubBuffer.cpp b/libs/ui/BufferHubBuffer.cpp index 3816c1bc4f..36eaed93ad 100644 --- a/libs/ui/BufferHubBuffer.cpp +++ b/libs/ui/BufferHubBuffer.cpp @@ -40,6 +40,7 @@ #include #include +#include using android::base::unique_fd; using android::dvr::BufferTraits; @@ -69,7 +70,6 @@ using dvr::BufferHubDefs::IsClientGained; using dvr::BufferHubDefs::IsClientPosted; using dvr::BufferHubDefs::IsClientReleased; using dvr::BufferHubDefs::kHighBitsMask; -using dvr::BufferHubDefs::kMetadataHeaderSize; } // namespace @@ -161,7 +161,7 @@ int BufferHubBuffer::ImportGraphicBuffer() { } size_t metadataSize = static_cast(bufferTraits.metadata_size()); - if (metadataSize < kMetadataHeaderSize) { + if (metadataSize < BufferHubDefs::kMetadataHeaderSize) { ALOGE("BufferHubBuffer::ImportGraphicBuffer: metadata too small: %zu", metadataSize); return -EINVAL; } diff --git a/libs/ui/BufferHubMetadata.cpp b/libs/ui/BufferHubMetadata.cpp index 18d9a2c963..816707db9d 100644 --- a/libs/ui/BufferHubMetadata.cpp +++ b/libs/ui/BufferHubMetadata.cpp @@ -16,6 +16,7 @@ #include #include +#include #include #include @@ -29,8 +30,8 @@ static const int kAshmemProt = PROT_READ | PROT_WRITE; } // namespace -using dvr::BufferHubDefs::kMetadataHeaderSize; -using dvr::BufferHubDefs::MetadataHeader; +using BufferHubDefs::kMetadataHeaderSize; +using BufferHubDefs::MetadataHeader; /* static */ BufferHubMetadata BufferHubMetadata::Create(size_t userMetadataSize) { diff --git a/libs/ui/include/ui/BufferHubDefs.h b/libs/ui/include/ui/BufferHubDefs.h new file mode 100644 index 0000000000..a1948256f5 --- /dev/null +++ b/libs/ui/include/ui/BufferHubDefs.h @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ANDROID_BUFFER_HUB_DEFS_H_ +#define ANDROID_BUFFER_HUB_DEFS_H_ + +#include + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpacked" +// TODO(b/118893702): remove dependency once DvrNativeBufferMetadata moved out of libdvr +#include +#pragma clang diagnostic pop + +namespace android { + +namespace BufferHubDefs { + +struct __attribute__((aligned(8))) MetadataHeader { + // Internal data format, which can be updated as long as the size, padding and field alignment + // of the struct is consistent within the same ABI. As this part is subject for future updates, + // it's not stable cross Android version, so don't have it visible from outside of the Android + // platform (include Apps and vendor HAL). + + // Every client takes up one bit from the higher 32 bits and one bit from the lower 32 bits in + // buffer_state. + std::atomic buffer_state; + + // Every client takes up one bit in fence_state. Only the lower 32 bits are valid. The upper 32 + // bits are there for easier manipulation, but the value should be ignored. + std::atomic fence_state; + + // Every client takes up one bit from the higher 32 bits and one bit from the lower 32 bits in + // active_clients_bit_mask. + std::atomic active_clients_bit_mask; + + // The index of the buffer queue where the buffer belongs to. + uint64_t queue_index; + + // Public data format, which should be updated with caution. See more details in dvr_api.h + DvrNativeBufferMetadata metadata; +}; + +static_assert(sizeof(MetadataHeader) == 136, "Unexpected MetadataHeader size"); +static constexpr size_t kMetadataHeaderSize = sizeof(MetadataHeader); + +} // namespace BufferHubDefs + +} // namespace android + +#endif // ANDROID_BUFFER_HUB_DEFS_H_ diff --git a/libs/ui/include/ui/BufferHubMetadata.h b/libs/ui/include/ui/BufferHubMetadata.h index 4261971f55..212189497a 100644 --- a/libs/ui/include/ui/BufferHubMetadata.h +++ b/libs/ui/include/ui/BufferHubMetadata.h @@ -17,26 +17,8 @@ #ifndef ANDROID_BUFFER_HUB_METADATA_H_ #define ANDROID_BUFFER_HUB_METADATA_H_ -// We would eliminate the clang warnings introduced by libdpx. -// TODO(b/112338294): Remove those once BufferHub moved to use Binder -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wconversion" -#pragma clang diagnostic ignored "-Wdouble-promotion" -#pragma clang diagnostic ignored "-Wgnu-case-range" -#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments" -#pragma clang diagnostic ignored "-Winconsistent-missing-destructor-override" -#pragma clang diagnostic ignored "-Wnested-anon-types" -#pragma clang diagnostic ignored "-Wpacked" -#pragma clang diagnostic ignored "-Wshadow" -#pragma clang diagnostic ignored "-Wsign-conversion" -#pragma clang diagnostic ignored "-Wswitch-enum" -#pragma clang diagnostic ignored "-Wundefined-func-template" -#pragma clang diagnostic ignored "-Wunused-template" -#pragma clang diagnostic ignored "-Wweak-vtables" -#include -#pragma clang diagnostic pop - #include +#include namespace android { @@ -84,23 +66,21 @@ public: bool IsValid() const { return mAshmemFd.get() != -1 && mMetadataHeader != nullptr; } size_t user_metadata_size() const { return mUserMetadataSize; } - size_t metadata_size() const { - return mUserMetadataSize + dvr::BufferHubDefs::kMetadataHeaderSize; - } + size_t metadata_size() const { return mUserMetadataSize + BufferHubDefs::kMetadataHeaderSize; } const unique_fd& ashmem_fd() const { return mAshmemFd; } - dvr::BufferHubDefs::MetadataHeader* metadata_header() { return mMetadataHeader; } + BufferHubDefs::MetadataHeader* metadata_header() { return mMetadataHeader; } private: BufferHubMetadata(size_t userMetadataSize, unique_fd ashmemFd, - dvr::BufferHubDefs::MetadataHeader* metadataHeader); + BufferHubDefs::MetadataHeader* metadataHeader); BufferHubMetadata(const BufferHubMetadata&) = delete; void operator=(const BufferHubMetadata&) = delete; size_t mUserMetadataSize = 0; unique_fd mAshmemFd; - dvr::BufferHubDefs::MetadataHeader* mMetadataHeader = nullptr; + BufferHubDefs::MetadataHeader* mMetadataHeader = nullptr; }; } // namespace android diff --git a/libs/ui/tests/Android.bp b/libs/ui/tests/Android.bp index 18bbb3e876..ca73be79d1 100644 --- a/libs/ui/tests/Android.bp +++ b/libs/ui/tests/Android.bp @@ -73,10 +73,13 @@ cc_test { cc_test { name: "BufferHubMetadata_test", - header_libs: ["libbufferhub_headers", "libdvr_headers"], + header_libs: [ + "libbufferhub_headers", + "libdvr_headers", + "libpdx_headers", + ], shared_libs: [ "libbase", - "libpdx_default_transport", "libui", "libutils", ], diff --git a/libs/ui/tests/BufferHubMetadata_test.cpp b/libs/ui/tests/BufferHubMetadata_test.cpp index 14422bf987..2265336c30 100644 --- a/libs/ui/tests/BufferHubMetadata_test.cpp +++ b/libs/ui/tests/BufferHubMetadata_test.cpp @@ -15,8 +15,10 @@ */ #include +#include #include +// TODO(b/118893702): move this function to ui/BufferHubDefs.h after ag/5483995 is landed using android::dvr::BufferHubDefs::IsBufferReleased; namespace android { diff --git a/libs/vr/libbufferhub/include/private/dvr/buffer_hub_defs.h b/libs/vr/libbufferhub/include/private/dvr/buffer_hub_defs.h index 400def7341..62ef475f16 100644 --- a/libs/vr/libbufferhub/include/private/dvr/buffer_hub_defs.h +++ b/libs/vr/libbufferhub/include/private/dvr/buffer_hub_defs.h @@ -8,8 +8,7 @@ #include #include #include - -#include +#include namespace android { namespace dvr { @@ -120,36 +119,9 @@ static inline uint64_t FindNextAvailableClientStateMask(uint64_t union_bits) { return new_low_bit + (new_low_bit << kMaxNumberOfClients); } -struct __attribute__((packed, aligned(8))) MetadataHeader { - // Internal data format, which can be updated as long as the size, padding and - // field alignment of the struct is consistent within the same ABI. As this - // part is subject for future updates, it's not stable cross Android version, - // so don't have it visible from outside of the Android platform (include Apps - // and vendor HAL). - - // Every client takes up one bit from the higher 32 bits and one bit from the - // lower 32 bits in buffer_state. - std::atomic buffer_state; - - // Every client takes up one bit in fence_state. Only the lower 32 bits are - // valid. The upper 32 bits are there for easier manipulation, but the value - // should be ignored. - std::atomic fence_state; - - // Every client takes up one bit from the higher 32 bits and one bit from the - // lower 32 bits in active_clients_bit_mask. - std::atomic active_clients_bit_mask; - - // The index of the buffer queue where the buffer belongs to. - uint64_t queue_index; - - // Public data format, which should be updated with caution. See more details - // in dvr_api.h - DvrNativeBufferMetadata metadata; -}; - -static_assert(sizeof(MetadataHeader) == 136, "Unexpected MetadataHeader size"); -static constexpr size_t kMetadataHeaderSize = sizeof(MetadataHeader); +using MetadataHeader = android::BufferHubDefs::MetadataHeader; +static constexpr size_t kMetadataHeaderSize = + android::BufferHubDefs::kMetadataHeaderSize; } // namespace BufferHubDefs diff --git a/libs/vr/libdvr/include/dvr/dvr_api.h b/libs/vr/libdvr/include/dvr/dvr_api.h index fef8512266..a204f62fff 100644 --- a/libs/vr/libdvr/include/dvr/dvr_api.h +++ b/libs/vr/libdvr/include/dvr/dvr_api.h @@ -412,6 +412,7 @@ typedef int (*DvrTrackingSensorsStopPtr)(DvrTrackingSensors* sensors); // existing data members. If new fields need to be added, please take extra care // to make sure that new data field is padded properly the size of the struct // stays same. +// TODO(b/118893702): move the definition to libnativewindow or libui struct ALIGNED_DVR_STRUCT(8) DvrNativeBufferMetadata { #ifdef __cplusplus DvrNativeBufferMetadata() diff --git a/services/bufferhub/BufferClient.cpp b/services/bufferhub/BufferClient.cpp index 745951745a..e312011696 100644 --- a/services/bufferhub/BufferClient.cpp +++ b/services/bufferhub/BufferClient.cpp @@ -17,6 +17,7 @@ #include #include #include +#include namespace android { namespace frameworks { diff --git a/services/bufferhub/include/bufferhub/BufferNode.h b/services/bufferhub/include/bufferhub/BufferNode.h index 94ef505d41..02bb5af0e0 100644 --- a/services/bufferhub/include/bufferhub/BufferNode.h +++ b/services/bufferhub/include/bufferhub/BufferNode.h @@ -3,6 +3,7 @@ #include #include +#include #include namespace android { diff --git a/services/bufferhub/tests/BufferNode_test.cpp b/services/bufferhub/tests/BufferNode_test.cpp index df31d78b89..3358c87eb8 100644 --- a/services/bufferhub/tests/BufferNode_test.cpp +++ b/services/bufferhub/tests/BufferNode_test.cpp @@ -1,7 +1,9 @@ -#include #include + +#include #include #include +#include #include namespace android { -- cgit v1.2.3-59-g8ed1b From 7359dc9c9a66d04db438683ffa46ce8697dd2b57 Mon Sep 17 00:00:00 2001 From: Tianyu Jiang Date: Thu, 13 Dec 2018 11:22:28 -0800 Subject: Replace android::dvr::BufferHubDefs with android::BufferHubDefs New libraries in libui and bufferhub shall be using android::BufferHubDefs instead of android::dvr::BufferHubDefs. Bug: None Test: atest AHardwareBufferTest BufferHubBuffer_test BufferHubMetadata_test buffer_hub_queue_producer-test libgui_test libsensor_test vrflinger_test buffer_hub-test buffer_hub_queue-test dvr_buffer_queue-test dvr_api-test dvr_display-test Test: in libui_test InputSurfacesTest are segfault on top of master already. tested on Blueline and Vega Change-Id: Ia755022c96147969a401ed32544082a8c76936a0 --- libs/ui/BufferHubBuffer.cpp | 16 ++++++++-------- libs/ui/include/ui/BufferHubBuffer.h | 2 +- libs/ui/tests/BufferHubBuffer_test.cpp | 20 ++++++++++---------- libs/vr/libbufferhub/buffer_hub-test.cpp | 21 +++++++++++---------- .../include/private/dvr/native_handle_wrapper.h | 2 ++ 5 files changed, 32 insertions(+), 29 deletions(-) (limited to 'libs/ui/BufferHubBuffer.cpp') diff --git a/libs/ui/BufferHubBuffer.cpp b/libs/ui/BufferHubBuffer.cpp index 36eaed93ad..1464e48367 100644 --- a/libs/ui/BufferHubBuffer.cpp +++ b/libs/ui/BufferHubBuffer.cpp @@ -62,14 +62,14 @@ namespace { // to use Binder. static constexpr char kBufferHubClientPath[] = "system/buffer_hub/client"; -using dvr::BufferHubDefs::AnyClientAcquired; -using dvr::BufferHubDefs::AnyClientGained; -using dvr::BufferHubDefs::AnyClientPosted; -using dvr::BufferHubDefs::IsClientAcquired; -using dvr::BufferHubDefs::IsClientGained; -using dvr::BufferHubDefs::IsClientPosted; -using dvr::BufferHubDefs::IsClientReleased; -using dvr::BufferHubDefs::kHighBitsMask; +using BufferHubDefs::AnyClientAcquired; +using BufferHubDefs::AnyClientGained; +using BufferHubDefs::AnyClientPosted; +using BufferHubDefs::IsClientAcquired; +using BufferHubDefs::IsClientGained; +using BufferHubDefs::IsClientPosted; +using BufferHubDefs::IsClientReleased; +using BufferHubDefs::kHighBitsMask; } // namespace diff --git a/libs/ui/include/ui/BufferHubBuffer.h b/libs/ui/include/ui/BufferHubBuffer.h index 03d10e7a9a..11e9b5c88d 100644 --- a/libs/ui/include/ui/BufferHubBuffer.h +++ b/libs/ui/include/ui/BufferHubBuffer.h @@ -34,11 +34,11 @@ #pragma clang diagnostic ignored "-Wunused-template" #pragma clang diagnostic ignored "-Wweak-vtables" #include -#include #include #pragma clang diagnostic pop #include +#include #include namespace android { diff --git a/libs/ui/tests/BufferHubBuffer_test.cpp b/libs/ui/tests/BufferHubBuffer_test.cpp index e33acf6d11..f616785387 100644 --- a/libs/ui/tests/BufferHubBuffer_test.cpp +++ b/libs/ui/tests/BufferHubBuffer_test.cpp @@ -36,16 +36,16 @@ const int kFormat = HAL_PIXEL_FORMAT_RGBA_8888; const int kUsage = 0; const size_t kUserMetadataSize = 0; -using dvr::BufferHubDefs::AnyClientAcquired; -using dvr::BufferHubDefs::AnyClientGained; -using dvr::BufferHubDefs::AnyClientPosted; -using dvr::BufferHubDefs::IsBufferReleased; -using dvr::BufferHubDefs::IsClientAcquired; -using dvr::BufferHubDefs::IsClientGained; -using dvr::BufferHubDefs::IsClientPosted; -using dvr::BufferHubDefs::IsClientReleased; -using dvr::BufferHubDefs::kFirstClientBitMask; -using dvr::BufferHubDefs::kMetadataHeaderSize; +using BufferHubDefs::AnyClientAcquired; +using BufferHubDefs::AnyClientGained; +using BufferHubDefs::AnyClientPosted; +using BufferHubDefs::IsBufferReleased; +using BufferHubDefs::IsClientAcquired; +using BufferHubDefs::IsClientGained; +using BufferHubDefs::IsClientPosted; +using BufferHubDefs::IsClientReleased; +using BufferHubDefs::kFirstClientBitMask; +using BufferHubDefs::kMetadataHeaderSize; using frameworks::bufferhub::V1_0::BufferHubStatus; using frameworks::bufferhub::V1_0::IBufferClient; using frameworks::bufferhub::V1_0::IBufferHub; diff --git a/libs/vr/libbufferhub/buffer_hub-test.cpp b/libs/vr/libbufferhub/buffer_hub-test.cpp index 9bcfaa1cde..c3fa77b958 100644 --- a/libs/vr/libbufferhub/buffer_hub-test.cpp +++ b/libs/vr/libbufferhub/buffer_hub-test.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -21,17 +22,17 @@ using android::BufferHubBuffer; using android::GraphicBuffer; using android::sp; +using android::BufferHubDefs::AnyClientAcquired; +using android::BufferHubDefs::AnyClientGained; +using android::BufferHubDefs::AnyClientPosted; +using android::BufferHubDefs::IsBufferReleased; +using android::BufferHubDefs::IsClientAcquired; +using android::BufferHubDefs::IsClientPosted; +using android::BufferHubDefs::IsClientReleased; +using android::BufferHubDefs::kFirstClientBitMask; +using android::BufferHubDefs::kMetadataHeaderSize; using android::dvr::ConsumerBuffer; using android::dvr::ProducerBuffer; -using android::dvr::BufferHubDefs::AnyClientAcquired; -using android::dvr::BufferHubDefs::AnyClientGained; -using android::dvr::BufferHubDefs::AnyClientPosted; -using android::dvr::BufferHubDefs::IsBufferReleased; -using android::dvr::BufferHubDefs::IsClientAcquired; -using android::dvr::BufferHubDefs::IsClientPosted; -using android::dvr::BufferHubDefs::IsClientReleased; -using android::dvr::BufferHubDefs::kFirstClientBitMask; -using android::dvr::BufferHubDefs::kMetadataHeaderSize; using android::pdx::LocalChannelHandle; using android::pdx::LocalHandle; using android::pdx::Status; @@ -45,7 +46,7 @@ const size_t kUserMetadataSize = 0; // Maximum number of consumers for the buffer that only has one producer in the // test. const size_t kMaxConsumerCount = - android::dvr::BufferHubDefs::kMaxNumberOfClients - 1; + android::BufferHubDefs::kMaxNumberOfClients - 1; const int kPollTimeoutMs = 100; using LibBufferHubTest = ::testing::Test; diff --git a/libs/vr/libbufferhub/include/private/dvr/native_handle_wrapper.h b/libs/vr/libbufferhub/include/private/dvr/native_handle_wrapper.h index ae70b77110..a5c6ca238a 100644 --- a/libs/vr/libbufferhub/include/private/dvr/native_handle_wrapper.h +++ b/libs/vr/libbufferhub/include/private/dvr/native_handle_wrapper.h @@ -2,6 +2,8 @@ #define ANDROID_DVR_NATIVE_HANDLE_WRAPPER_H_ #include +#include +#include #include -- cgit v1.2.3-59-g8ed1b From a99f9114d15add6bf1d67264e2a60a73e13009fe Mon Sep 17 00:00:00 2001 From: Tianyu Jiang Date: Thu, 13 Dec 2018 18:23:07 -0800 Subject: Change atomics in ashmem from uint64_t to uint32_t Fix: 117849512 Test: Blueline: atest AHardwareBufferTest BufferHub_test buffer_hub_queue_producer-test libgui_test libsensor_test vrflinger_test buffer_hub-test buffer_hub_queue-test dvr_buffer_queue-test dvr_api-test dvr_display-test Test: in libui_test InputSurfacesTest are segfault on top of master already. Test: Vega: AHardwareBufferTest BufferHubBuffer_test BufferHubMetadata_test buffer_hub_queue_producer-test buffer_hub-test dvr_buffer_queue-test buffer_hub_queue-test dvr_api-test libdvrtracking-test Change-Id: I55f91c21f7ac07615b5451b5413521d7938cf591 --- libs/gui/BufferHubProducer.cpp | 2 +- libs/ui/BufferHubBuffer.cpp | 30 +++--- libs/ui/include/ui/BufferHubBuffer.h | 12 +-- libs/ui/include/ui/BufferHubDefs.h | 101 +++++++++++---------- libs/ui/tests/BufferHubBuffer_test.cpp | 12 +-- libs/vr/libbufferhub/buffer_hub-test.cpp | 22 ++--- libs/vr/libbufferhub/buffer_hub_base.cpp | 8 +- libs/vr/libbufferhub/consumer_buffer.cpp | 22 ++--- .../include/private/dvr/buffer_hub_base.h | 16 ++-- .../include/private/dvr/buffer_hub_defs.h | 30 +++--- .../include/private/dvr/bufferhub_rpc.h | 6 +- libs/vr/libbufferhub/producer_buffer.cpp | 36 ++++---- libs/vr/libdvr/include/dvr/dvr_api.h | 4 +- libs/vr/libdvr/tests/dvr_buffer_queue-test.cpp | 2 +- services/bufferhub/BufferNode.cpp | 22 ++--- .../include/bufferhub/BufferHubIdGenerator.h | 2 +- services/bufferhub/include/bufferhub/BufferNode.h | 14 +-- services/bufferhub/tests/BufferNode_test.cpp | 16 ++-- services/vr/bufferhubd/buffer_channel.cpp | 4 +- services/vr/bufferhubd/consumer_channel.cpp | 2 +- .../include/private/dvr/buffer_channel.h | 4 +- .../include/private/dvr/consumer_channel.h | 6 +- .../include/private/dvr/producer_channel.h | 20 ++-- services/vr/bufferhubd/producer_channel.cpp | 82 ++++++++--------- 24 files changed, 239 insertions(+), 236 deletions(-) (limited to 'libs/ui/BufferHubBuffer.cpp') diff --git a/libs/gui/BufferHubProducer.cpp b/libs/gui/BufferHubProducer.cpp index ed773e003f..16952a6625 100644 --- a/libs/gui/BufferHubProducer.cpp +++ b/libs/gui/BufferHubProducer.cpp @@ -520,7 +520,7 @@ status_t BufferHubProducer::cancelBuffer(int slot, const sp& fence) { } auto buffer_producer = buffers_[slot].mBufferProducer; - queue_->Enqueue(buffer_producer, size_t(slot), 0ULL); + queue_->Enqueue(buffer_producer, size_t(slot), 0U); buffers_[slot].mBufferState.cancel(); buffers_[slot].mFence = fence; ALOGV("cancelBuffer: slot %d", slot); diff --git a/libs/ui/BufferHubBuffer.cpp b/libs/ui/BufferHubBuffer.cpp index 1464e48367..5bc113f4b2 100644 --- a/libs/ui/BufferHubBuffer.cpp +++ b/libs/ui/BufferHubBuffer.cpp @@ -191,22 +191,22 @@ int BufferHubBuffer::ImportGraphicBuffer() { mClientStateMask = bufferTraits.client_state_mask(); // TODO(b/112012161) Set up shared fences. - ALOGD("BufferHubBuffer::ImportGraphicBuffer: id=%d, buffer_state=%" PRIx64 ".", id(), + ALOGD("BufferHubBuffer::ImportGraphicBuffer: id=%d, buffer_state=%" PRIx32 ".", id(), buffer_state_->load(std::memory_order_acquire)); return 0; } int BufferHubBuffer::Gain() { - uint64_t current_buffer_state = buffer_state_->load(std::memory_order_acquire); + uint32_t current_buffer_state = buffer_state_->load(std::memory_order_acquire); if (IsClientGained(current_buffer_state, mClientStateMask)) { - ALOGV("%s: Buffer is already gained by this client %" PRIx64 ".", __FUNCTION__, + ALOGV("%s: Buffer is already gained by this client %" PRIx32 ".", __FUNCTION__, mClientStateMask); return 0; } do { if (AnyClientGained(current_buffer_state & (~mClientStateMask)) || AnyClientAcquired(current_buffer_state)) { - ALOGE("%s: Buffer is in use, id=%d mClientStateMask=%" PRIx64 " state=%" PRIx64 ".", + ALOGE("%s: Buffer is in use, id=%d mClientStateMask=%" PRIx32 " state=%" PRIx32 ".", __FUNCTION__, mId, mClientStateMask, current_buffer_state); return -EBUSY; } @@ -220,13 +220,13 @@ int BufferHubBuffer::Gain() { } int BufferHubBuffer::Post() { - uint64_t current_buffer_state = buffer_state_->load(std::memory_order_acquire); - uint64_t current_active_clients_bit_mask = 0ULL; - uint64_t updated_buffer_state = 0ULL; + uint32_t current_buffer_state = buffer_state_->load(std::memory_order_acquire); + uint32_t current_active_clients_bit_mask = 0U; + uint32_t updated_buffer_state = 0U; do { if (!IsClientGained(current_buffer_state, mClientStateMask)) { ALOGE("%s: Cannot post a buffer that is not gained by this client. buffer_id=%d " - "mClientStateMask=%" PRIx64 " state=%" PRIx64 ".", + "mClientStateMask=%" PRIx32 " state=%" PRIx32 ".", __FUNCTION__, mId, mClientStateMask, current_buffer_state); return -EBUSY; } @@ -242,17 +242,17 @@ int BufferHubBuffer::Post() { } int BufferHubBuffer::Acquire() { - uint64_t current_buffer_state = buffer_state_->load(std::memory_order_acquire); + uint32_t current_buffer_state = buffer_state_->load(std::memory_order_acquire); if (IsClientAcquired(current_buffer_state, mClientStateMask)) { - ALOGV("%s: Buffer is already acquired by this client %" PRIx64 ".", __FUNCTION__, + ALOGV("%s: Buffer is already acquired by this client %" PRIx32 ".", __FUNCTION__, mClientStateMask); return 0; } - uint64_t updated_buffer_state = 0ULL; + uint32_t updated_buffer_state = 0U; do { if (!IsClientPosted(current_buffer_state, mClientStateMask)) { ALOGE("%s: Cannot acquire a buffer that is not in posted state. buffer_id=%d " - "mClientStateMask=%" PRIx64 " state=%" PRIx64 ".", + "mClientStateMask=%" PRIx32 " state=%" PRIx32 ".", __FUNCTION__, mId, mClientStateMask, current_buffer_state); return -EBUSY; } @@ -266,13 +266,13 @@ int BufferHubBuffer::Acquire() { } int BufferHubBuffer::Release() { - uint64_t current_buffer_state = buffer_state_->load(std::memory_order_acquire); + uint32_t current_buffer_state = buffer_state_->load(std::memory_order_acquire); if (IsClientReleased(current_buffer_state, mClientStateMask)) { - ALOGV("%s: Buffer is already released by this client %" PRIx64 ".", __FUNCTION__, + ALOGV("%s: Buffer is already released by this client %" PRIx32 ".", __FUNCTION__, mClientStateMask); return 0; } - uint64_t updated_buffer_state = 0ULL; + uint32_t updated_buffer_state = 0U; do { updated_buffer_state = current_buffer_state & (~mClientStateMask); } while (!buffer_state_->compare_exchange_weak(current_buffer_state, updated_buffer_state, diff --git a/libs/ui/include/ui/BufferHubBuffer.h b/libs/ui/include/ui/BufferHubBuffer.h index 11e9b5c88d..90dd391d8d 100644 --- a/libs/ui/include/ui/BufferHubBuffer.h +++ b/libs/ui/include/ui/BufferHubBuffer.h @@ -86,13 +86,13 @@ public: const native_handle_t* DuplicateHandle() { return mBufferHandle.DuplicateHandle(); } // Returns the current value of MetadataHeader::buffer_state. - uint64_t buffer_state() { + uint32_t buffer_state() { return mMetadata.metadata_header()->buffer_state.load(std::memory_order_acquire); } // A state mask which is unique to a buffer hub client among all its siblings sharing the same // concrete graphic buffer. - uint64_t client_state_mask() const { return mClientStateMask; } + uint32_t client_state_mask() const { return mClientStateMask; } size_t user_metadata_size() const { return mMetadata.user_metadata_size(); } @@ -154,7 +154,7 @@ private: // Client state mask of this BufferHubBuffer object. It is unique amoung all // clients/users of the buffer. - uint64_t mClientStateMask = 0; + uint32_t mClientStateMask = 0U; // Stores ground truth of the buffer. AHardwareBuffer_Desc mBufferDesc; @@ -166,9 +166,9 @@ private: // bufferhubd daemon and all buffer clients. BufferHubMetadata mMetadata; // Shortcuts to the atomics inside the header of mMetadata. - std::atomic* buffer_state_{nullptr}; - std::atomic* fence_state_{nullptr}; - std::atomic* active_clients_bit_mask_{nullptr}; + std::atomic* buffer_state_ = nullptr; + std::atomic* fence_state_ = nullptr; + std::atomic* active_clients_bit_mask_ = nullptr; // PDX backend. BufferHubClient mClient; diff --git a/libs/ui/include/ui/BufferHubDefs.h b/libs/ui/include/ui/BufferHubDefs.h index ef6668bd76..d259fefb8f 100644 --- a/libs/ui/include/ui/BufferHubDefs.h +++ b/libs/ui/include/ui/BufferHubDefs.h @@ -29,10 +29,10 @@ namespace android { namespace BufferHubDefs { -// Single buffer clients (up to 32) ownership signal. -// 64-bit atomic unsigned int. -// Each client takes 2 bits. The first bit locates in the first 32 bits of -// buffer_state; the second bit locates in the last 32 bits of buffer_state. +// Single buffer clients (up to 16) ownership signal. +// 32-bit atomic unsigned int. +// Each client takes 2 bits. The first bit locates in the first 16 bits of +// buffer_state; the second bit locates in the last 16 bits of buffer_state. // Client states: // Gained state 11. Exclusive write state. // Posted state 10. @@ -42,88 +42,88 @@ namespace BufferHubDefs { // MSB LSB // | | // v v -// [C31|...|C1|C0|C31| ... |C1|C0] +// [C15|...|C1|C0|C15| ... |C1|C0] // Maximum number of clients a buffer can have. -static constexpr int kMaxNumberOfClients = 32; +static constexpr int kMaxNumberOfClients = 16; // Definition of bit masks. // MSB LSB // | kHighBitsMask | kLowbitsMask | // v v v -// [b63| ... |b32|b31| ... |b0] +// [b31| ... |b16|b15| ... |b0] -// The location of lower 32 bits in the 64-bit buffer state. -static constexpr uint64_t kLowbitsMask = (1ULL << kMaxNumberOfClients) - 1ULL; +// The location of lower 16 bits in the 32-bit buffer state. +static constexpr uint32_t kLowbitsMask = (1U << kMaxNumberOfClients) - 1U; -// The location of higher 32 bits in the 64-bit buffer state. -static constexpr uint64_t kHighBitsMask = ~kLowbitsMask; +// The location of higher 16 bits in the 32-bit buffer state. +static constexpr uint32_t kHighBitsMask = ~kLowbitsMask; // The client bit mask of the first client. -static constexpr uint64_t kFirstClientBitMask = (1ULL << kMaxNumberOfClients) + 1ULL; +static constexpr uint32_t kFirstClientBitMask = (1U << kMaxNumberOfClients) + 1U; // Returns true if any of the client is in gained state. -static inline bool AnyClientGained(uint64_t state) { - uint64_t high_bits = state >> kMaxNumberOfClients; - uint64_t low_bits = state & kLowbitsMask; - return high_bits == low_bits && low_bits != 0ULL; +static inline bool AnyClientGained(uint32_t state) { + uint32_t high_bits = state >> kMaxNumberOfClients; + uint32_t low_bits = state & kLowbitsMask; + return high_bits == low_bits && low_bits != 0U; } // Returns true if the input client is in gained state. -static inline bool IsClientGained(uint64_t state, uint64_t client_bit_mask) { +static inline bool IsClientGained(uint32_t state, uint32_t client_bit_mask) { return state == client_bit_mask; } // Returns true if any of the client is in posted state. -static inline bool AnyClientPosted(uint64_t state) { - uint64_t high_bits = state >> kMaxNumberOfClients; - uint64_t low_bits = state & kLowbitsMask; - uint64_t posted_or_acquired = high_bits ^ low_bits; +static inline bool AnyClientPosted(uint32_t state) { + uint32_t high_bits = state >> kMaxNumberOfClients; + uint32_t low_bits = state & kLowbitsMask; + uint32_t posted_or_acquired = high_bits ^ low_bits; return posted_or_acquired & high_bits; } // Returns true if the input client is in posted state. -static inline bool IsClientPosted(uint64_t state, uint64_t client_bit_mask) { - uint64_t client_bits = state & client_bit_mask; - if (client_bits == 0ULL) return false; - uint64_t low_bits = client_bits & kLowbitsMask; - return low_bits == 0ULL; +static inline bool IsClientPosted(uint32_t state, uint32_t client_bit_mask) { + uint32_t client_bits = state & client_bit_mask; + if (client_bits == 0U) return false; + uint32_t low_bits = client_bits & kLowbitsMask; + return low_bits == 0U; } // Return true if any of the client is in acquired state. -static inline bool AnyClientAcquired(uint64_t state) { - uint64_t high_bits = state >> kMaxNumberOfClients; - uint64_t low_bits = state & kLowbitsMask; - uint64_t posted_or_acquired = high_bits ^ low_bits; +static inline bool AnyClientAcquired(uint32_t state) { + uint32_t high_bits = state >> kMaxNumberOfClients; + uint32_t low_bits = state & kLowbitsMask; + uint32_t posted_or_acquired = high_bits ^ low_bits; return posted_or_acquired & low_bits; } // Return true if the input client is in acquired state. -static inline bool IsClientAcquired(uint64_t state, uint64_t client_bit_mask) { - uint64_t client_bits = state & client_bit_mask; - if (client_bits == 0ULL) return false; - uint64_t high_bits = client_bits & kHighBitsMask; - return high_bits == 0ULL; +static inline bool IsClientAcquired(uint32_t state, uint32_t client_bit_mask) { + uint32_t client_bits = state & client_bit_mask; + if (client_bits == 0U) return false; + uint32_t high_bits = client_bits & kHighBitsMask; + return high_bits == 0U; } // Returns true if all clients are in released state. -static inline bool IsBufferReleased(uint64_t state) { - return state == 0ULL; +static inline bool IsBufferReleased(uint32_t state) { + return state == 0U; } // Returns true if the input client is in released state. -static inline bool IsClientReleased(uint64_t state, uint64_t client_bit_mask) { - return (state & client_bit_mask) == 0ULL; +static inline bool IsClientReleased(uint32_t state, uint32_t client_bit_mask) { + return (state & client_bit_mask) == 0U; } // Returns the next available buffer client's client_state_masks. // @params union_bits. Union of all existing clients' client_state_masks. -static inline uint64_t FindNextAvailableClientStateMask(uint64_t union_bits) { - uint64_t low_union = union_bits & kLowbitsMask; - if (low_union == kLowbitsMask) return 0ULL; - uint64_t incremented = low_union + 1ULL; - uint64_t difference = incremented ^ low_union; - uint64_t new_low_bit = (difference + 1ULL) >> 1; +static inline uint32_t FindNextAvailableClientStateMask(uint32_t union_bits) { + uint32_t low_union = union_bits & kLowbitsMask; + if (low_union == kLowbitsMask) return 0U; + uint32_t incremented = low_union + 1U; + uint32_t difference = incremented ^ low_union; + uint32_t new_low_bit = (difference + 1U) >> 1; return new_low_bit + (new_low_bit << kMaxNumberOfClients); } @@ -135,15 +135,18 @@ struct __attribute__((aligned(8))) MetadataHeader { // Every client takes up one bit from the higher 32 bits and one bit from the lower 32 bits in // buffer_state. - std::atomic buffer_state; + std::atomic buffer_state; // Every client takes up one bit in fence_state. Only the lower 32 bits are valid. The upper 32 // bits are there for easier manipulation, but the value should be ignored. - std::atomic fence_state; + std::atomic fence_state; // Every client takes up one bit from the higher 32 bits and one bit from the lower 32 bits in // active_clients_bit_mask. - std::atomic active_clients_bit_mask; + std::atomic active_clients_bit_mask; + + // Explicit padding 4 bytes. + uint32_t padding; // The index of the buffer queue where the buffer belongs to. uint64_t queue_index; @@ -152,7 +155,7 @@ struct __attribute__((aligned(8))) MetadataHeader { DvrNativeBufferMetadata metadata; }; -static_assert(sizeof(MetadataHeader) == 136, "Unexpected MetadataHeader size"); +static_assert(sizeof(MetadataHeader) == 128, "Unexpected MetadataHeader size"); static constexpr size_t kMetadataHeaderSize = sizeof(MetadataHeader); } // namespace BufferHubDefs diff --git a/libs/ui/tests/BufferHubBuffer_test.cpp b/libs/ui/tests/BufferHubBuffer_test.cpp index f616785387..a894f201c5 100644 --- a/libs/ui/tests/BufferHubBuffer_test.cpp +++ b/libs/ui/tests/BufferHubBuffer_test.cpp @@ -67,9 +67,9 @@ protected: } std::unique_ptr b1; - uint64_t b1ClientMask = 0ULL; + uint64_t b1ClientMask = 0U; std::unique_ptr b2; - uint64_t b2ClientMask = 0ULL; + uint64_t b2ClientMask = 0U; private: // Creates b1 and b2 as the clients of the same buffer for testing. @@ -79,13 +79,13 @@ private: void BufferHubBufferStateTransitionTest::CreateTwoClientsOfABuffer() { b1 = BufferHubBuffer::Create(kWidth, kHeight, kLayerCount, kFormat, kUsage, kUserMetadataSize); b1ClientMask = b1->client_state_mask(); - ASSERT_NE(b1ClientMask, 0ULL); + ASSERT_NE(b1ClientMask, 0U); auto statusOrHandle = b1->Duplicate(); ASSERT_TRUE(statusOrHandle); LocalChannelHandle h2 = statusOrHandle.take(); b2 = BufferHubBuffer::Import(std::move(h2)); b2ClientMask = b2->client_state_mask(); - ASSERT_NE(b2ClientMask, 0ULL); + ASSERT_NE(b2ClientMask, 0U); ASSERT_NE(b2ClientMask, b1ClientMask); } @@ -126,7 +126,7 @@ TEST_F(BufferHubBufferTest, DuplicateBufferHubBuffer) { kUserMetadataSize); int id1 = b1->id(); uint64_t bufferStateMask1 = b1->client_state_mask(); - EXPECT_NE(bufferStateMask1, 0ULL); + EXPECT_NE(bufferStateMask1, 0U); EXPECT_TRUE(b1->IsValid()); EXPECT_EQ(b1->user_metadata_size(), kUserMetadataSize); @@ -149,7 +149,7 @@ TEST_F(BufferHubBufferTest, DuplicateBufferHubBuffer) { int id2 = b2->id(); uint64_t bufferStateMask2 = b2->client_state_mask(); - EXPECT_NE(bufferStateMask2, 0ULL); + EXPECT_NE(bufferStateMask2, 0U); // These two buffer instances are based on the same physical buffer under the // hood, so they should share the same id. diff --git a/libs/vr/libbufferhub/buffer_hub-test.cpp b/libs/vr/libbufferhub/buffer_hub-test.cpp index c3fa77b958..6cb6541c52 100644 --- a/libs/vr/libbufferhub/buffer_hub-test.cpp +++ b/libs/vr/libbufferhub/buffer_hub-test.cpp @@ -175,7 +175,7 @@ TEST_F(LibBufferHubTest, TestStateMask) { ASSERT_TRUE(p.get() != nullptr); // It's ok to create up to kMaxConsumerCount consumer buffers. - uint64_t client_state_masks = p->client_state_mask(); + uint32_t client_state_masks = p->client_state_mask(); std::array, kMaxConsumerCount> cs; for (size_t i = 0; i < kMaxConsumerCount; i++) { cs[i] = ConsumerBuffer::Import(p->CreateConsumer()); @@ -184,7 +184,7 @@ TEST_F(LibBufferHubTest, TestStateMask) { EXPECT_EQ(client_state_masks & cs[i]->client_state_mask(), 0U); client_state_masks |= cs[i]->client_state_mask(); } - EXPECT_EQ(client_state_masks, ~0ULL); + EXPECT_EQ(client_state_masks, ~0U); // The 64th creation will fail with out-of-memory error. auto state = p->CreateConsumer(); @@ -373,7 +373,7 @@ TEST_F(LibBufferHubTest, TestMaxConsumers) { std::unique_ptr p = ProducerBuffer::Create( kWidth, kHeight, kFormat, kUsage, sizeof(uint64_t)); ASSERT_TRUE(p.get() != nullptr); - uint64_t producer_state_mask = p->client_state_mask(); + uint32_t producer_state_mask = p->client_state_mask(); std::array, kMaxConsumerCount> cs; for (size_t i = 0; i < kMaxConsumerCount; ++i) { @@ -719,7 +719,7 @@ TEST_F(LibBufferHubTest, TestOrphanedAcquire) { std::unique_ptr c1 = ConsumerBuffer::Import(p->CreateConsumer()); ASSERT_TRUE(c1.get() != nullptr); - const uint64_t client_state_mask1 = c1->client_state_mask(); + const uint32_t client_state_mask1 = c1->client_state_mask(); EXPECT_EQ(0, p->GainAsync()); DvrNativeBufferMetadata meta; @@ -739,7 +739,7 @@ TEST_F(LibBufferHubTest, TestOrphanedAcquire) { std::unique_ptr c2 = ConsumerBuffer::Import(p->CreateConsumer()); ASSERT_TRUE(c2.get() != nullptr); - const uint64_t client_state_mask2 = c2->client_state_mask(); + const uint32_t client_state_mask2 = c2->client_state_mask(); EXPECT_NE(client_state_mask1, client_state_mask2); EXPECT_EQ(0, RETRY_EINTR(c2->Poll(kPollTimeoutMs))); EXPECT_EQ(-EBUSY, c2->AcquireAsync(&meta, &fence)); @@ -755,7 +755,7 @@ TEST_F(LibBufferHubTest, TestAcquireLastPosted) { std::unique_ptr c1 = ConsumerBuffer::Import(p->CreateConsumer()); ASSERT_TRUE(c1.get() != nullptr); - const uint64_t client_state_mask1 = c1->client_state_mask(); + const uint32_t client_state_mask1 = c1->client_state_mask(); EXPECT_EQ(0, p->GainAsync()); DvrNativeBufferMetadata meta; @@ -767,7 +767,7 @@ TEST_F(LibBufferHubTest, TestAcquireLastPosted) { std::unique_ptr c2 = ConsumerBuffer::Import(p->CreateConsumer()); ASSERT_TRUE(c2.get() != nullptr); - const uint64_t client_state_mask2 = c2->client_state_mask(); + const uint32_t client_state_mask2 = c2->client_state_mask(); EXPECT_NE(client_state_mask1, client_state_mask2); EXPECT_LT(0, RETRY_EINTR(c2->Poll(kPollTimeoutMs))); LocalHandle invalid_fence; @@ -781,7 +781,7 @@ TEST_F(LibBufferHubTest, TestAcquireLastPosted) { std::unique_ptr c3 = ConsumerBuffer::Import(p->CreateConsumer()); ASSERT_TRUE(c3.get() != nullptr); - const uint64_t client_state_mask3 = c3->client_state_mask(); + const uint32_t client_state_mask3 = c3->client_state_mask(); EXPECT_NE(client_state_mask1, client_state_mask3); EXPECT_NE(client_state_mask2, client_state_mask3); EXPECT_LT(0, RETRY_EINTR(c3->Poll(kPollTimeoutMs))); @@ -802,7 +802,7 @@ TEST_F(LibBufferHubTest, TestAcquireLastPosted) { std::unique_ptr c4 = ConsumerBuffer::Import(p->CreateConsumer()); ASSERT_TRUE(c4.get() != nullptr); - const uint64_t client_state_mask4 = c4->client_state_mask(); + const uint32_t client_state_mask4 = c4->client_state_mask(); EXPECT_NE(client_state_mask3, client_state_mask4); EXPECT_GE(0, RETRY_EINTR(c3->Poll(kPollTimeoutMs))); EXPECT_EQ(-EBUSY, c3->AcquireAsync(&meta, &invalid_fence)); @@ -952,7 +952,7 @@ TEST_F(LibBufferHubTest, TestDuplicateBufferHubBuffer) { int b1_id = b1->id(); EXPECT_TRUE(b1->IsValid()); EXPECT_EQ(b1->user_metadata_size(), kUserMetadataSize); - EXPECT_NE(b1->client_state_mask(), 0ULL); + EXPECT_NE(b1->client_state_mask(), 0U); auto status_or_handle = b1->Duplicate(); EXPECT_TRUE(status_or_handle); @@ -970,7 +970,7 @@ TEST_F(LibBufferHubTest, TestDuplicateBufferHubBuffer) { ASSERT_TRUE(b2 != nullptr); EXPECT_TRUE(b2->IsValid()); EXPECT_EQ(b2->user_metadata_size(), kUserMetadataSize); - EXPECT_NE(b2->client_state_mask(), 0ULL); + EXPECT_NE(b2->client_state_mask(), 0U); int b2_id = b2->id(); diff --git a/libs/vr/libbufferhub/buffer_hub_base.cpp b/libs/vr/libbufferhub/buffer_hub_base.cpp index 2dc427aec1..8497f3edc1 100644 --- a/libs/vr/libbufferhub/buffer_hub_base.cpp +++ b/libs/vr/libbufferhub/buffer_hub_base.cpp @@ -124,16 +124,16 @@ int BufferHubBase::ImportBuffer() { // memory region will be preserved. buffer_state_ = &metadata_header_->buffer_state; ALOGD_IF(TRACE, - "BufferHubBase::ImportBuffer: id=%d, buffer_state=%" PRIx64 ".", + "BufferHubBase::ImportBuffer: id=%d, buffer_state=%" PRIx32 ".", id(), buffer_state_->load(std::memory_order_acquire)); fence_state_ = &metadata_header_->fence_state; ALOGD_IF(TRACE, - "BufferHubBase::ImportBuffer: id=%d, fence_state=%" PRIx64 ".", id(), + "BufferHubBase::ImportBuffer: id=%d, fence_state=%" PRIx32 ".", id(), fence_state_->load(std::memory_order_acquire)); active_clients_bit_mask_ = &metadata_header_->active_clients_bit_mask; ALOGD_IF( TRACE, - "BufferHubBase::ImportBuffer: id=%d, active_clients_bit_mask=%" PRIx64 + "BufferHubBase::ImportBuffer: id=%d, active_clients_bit_mask=%" PRIx32 ".", id(), active_clients_bit_mask_->load(std::memory_order_acquire)); @@ -171,7 +171,7 @@ int BufferHubBase::UpdateSharedFence(const LocalHandle& new_fence, // If ready fence is valid, we put that into the epoll set. epoll_event event; event.events = EPOLLIN; - event.data.u64 = client_state_mask(); + event.data.u32 = client_state_mask(); pending_fence_fd_ = new_fence.Duplicate(); if (epoll_ctl(shared_fence.Get(), EPOLL_CTL_ADD, pending_fence_fd_.Get(), &event) < 0) { diff --git a/libs/vr/libbufferhub/consumer_buffer.cpp b/libs/vr/libbufferhub/consumer_buffer.cpp index 62fb5fd769..b6ca64eef2 100644 --- a/libs/vr/libbufferhub/consumer_buffer.cpp +++ b/libs/vr/libbufferhub/consumer_buffer.cpp @@ -36,33 +36,33 @@ int ConsumerBuffer::LocalAcquire(DvrNativeBufferMetadata* out_meta, return -EINVAL; // The buffer can be acquired iff the buffer state for this client is posted. - uint64_t current_buffer_state = + uint32_t current_buffer_state = buffer_state_->load(std::memory_order_acquire); if (!BufferHubDefs::IsClientPosted(current_buffer_state, client_state_mask())) { ALOGE( "%s: Failed to acquire the buffer. The buffer is not posted, id=%d " - "state=%" PRIx64 " client_state_mask=%" PRIx64 ".", + "state=%" PRIx32 " client_state_mask=%" PRIx32 ".", __FUNCTION__, id(), current_buffer_state, client_state_mask()); return -EBUSY; } // Change the buffer state for this consumer from posted to acquired. - uint64_t updated_buffer_state = current_buffer_state ^ client_state_mask(); + uint32_t updated_buffer_state = current_buffer_state ^ client_state_mask(); while (!buffer_state_->compare_exchange_weak( current_buffer_state, updated_buffer_state, std::memory_order_acq_rel, std::memory_order_acquire)) { ALOGD( "%s Failed to acquire the buffer. Current buffer state was changed to " - "%" PRIx64 + "%" PRIx32 " when trying to acquire the buffer and modify the buffer state to " - "%" PRIx64 ". About to try again if the buffer is still posted.", + "%" PRIx32 ". About to try again if the buffer is still posted.", __FUNCTION__, current_buffer_state, updated_buffer_state); if (!BufferHubDefs::IsClientPosted(current_buffer_state, client_state_mask())) { ALOGE( "%s: Failed to acquire the buffer. The buffer is no longer posted, " - "id=%d state=%" PRIx64 " client_state_mask=%" PRIx64 ".", + "id=%d state=%" PRIx32 " client_state_mask=%" PRIx32 ".", __FUNCTION__, id(), current_buffer_state, client_state_mask()); return -EBUSY; } @@ -81,7 +81,7 @@ int ConsumerBuffer::LocalAcquire(DvrNativeBufferMetadata* out_meta, out_meta->user_metadata_ptr = 0; } - uint64_t fence_state = fence_state_->load(std::memory_order_acquire); + uint32_t fence_state = fence_state_->load(std::memory_order_acquire); // If there is an acquire fence from producer, we need to return it. // The producer state bit mask is kFirstClientBitMask for now. if (fence_state & BufferHubDefs::kFirstClientBitMask) { @@ -142,21 +142,21 @@ int ConsumerBuffer::LocalRelease(const DvrNativeBufferMetadata* meta, // Set the buffer state of this client to released if it is not already in // released state. - uint64_t current_buffer_state = + uint32_t current_buffer_state = buffer_state_->load(std::memory_order_acquire); if (BufferHubDefs::IsClientReleased(current_buffer_state, client_state_mask())) { return 0; } - uint64_t updated_buffer_state = current_buffer_state & (~client_state_mask()); + uint32_t updated_buffer_state = current_buffer_state & (~client_state_mask()); while (!buffer_state_->compare_exchange_weak( current_buffer_state, updated_buffer_state, std::memory_order_acq_rel, std::memory_order_acquire)) { ALOGD( "%s: Failed to release the buffer. Current buffer state was changed to " - "%" PRIx64 + "%" PRIx32 " when trying to release the buffer and modify the buffer state to " - "%" PRIx64 ". About to try again.", + "%" PRIx32 ". About to try again.", __FUNCTION__, current_buffer_state, updated_buffer_state); // The failure of compare_exchange_weak updates current_buffer_state. updated_buffer_state = current_buffer_state & (~client_state_mask()); diff --git a/libs/vr/libbufferhub/include/private/dvr/buffer_hub_base.h b/libs/vr/libbufferhub/include/private/dvr/buffer_hub_base.h index 09feb73f81..440a59dc71 100644 --- a/libs/vr/libbufferhub/include/private/dvr/buffer_hub_base.h +++ b/libs/vr/libbufferhub/include/private/dvr/buffer_hub_base.h @@ -90,13 +90,13 @@ class BufferHubBase : public pdx::Client { int cid() const { return cid_; } // Returns the buffer buffer state. - uint64_t buffer_state() { + uint32_t buffer_state() { return buffer_state_->load(std::memory_order_acquire); }; // A state mask which is unique to a buffer hub client among all its siblings // sharing the same concrete graphic buffer. - uint64_t client_state_mask() const { return client_state_mask_; } + uint32_t client_state_mask() const { return client_state_mask_; } // The following methods return settings of the first buffer. Currently, // it is only possible to create multi-buffer BufferHubBases with the same @@ -132,11 +132,11 @@ class BufferHubBase : public pdx::Client { // IonBuffer that is shared between bufferhubd, producer, and consumers. size_t metadata_buf_size_{0}; size_t user_metadata_size_{0}; - BufferHubDefs::MetadataHeader* metadata_header_{nullptr}; - void* user_metadata_ptr_{nullptr}; - std::atomic* buffer_state_{nullptr}; - std::atomic* fence_state_{nullptr}; - std::atomic* active_clients_bit_mask_{nullptr}; + BufferHubDefs::MetadataHeader* metadata_header_ = nullptr; + void* user_metadata_ptr_ = nullptr; + std::atomic* buffer_state_ = nullptr; + std::atomic* fence_state_ = nullptr; + std::atomic* active_clients_bit_mask_ = nullptr; LocalHandle shared_acquire_fence_; LocalHandle shared_release_fence_; @@ -159,7 +159,7 @@ class BufferHubBase : public pdx::Client { // Client bit mask which indicates the locations of this client object in the // buffer_state_. - uint64_t client_state_mask_{0ULL}; + uint32_t client_state_mask_{0U}; IonBuffer buffer_; IonBuffer metadata_buffer_; }; diff --git a/libs/vr/libbufferhub/include/private/dvr/buffer_hub_defs.h b/libs/vr/libbufferhub/include/private/dvr/buffer_hub_defs.h index 2de36f26a6..f2c40fe3d3 100644 --- a/libs/vr/libbufferhub/include/private/dvr/buffer_hub_defs.h +++ b/libs/vr/libbufferhub/include/private/dvr/buffer_hub_defs.h @@ -22,46 +22,46 @@ static constexpr uint32_t kMetadataUsage = // See more details in libs/ui/include/ui/BufferHubDefs.h static constexpr int kMaxNumberOfClients = android::BufferHubDefs::kMaxNumberOfClients; -static constexpr uint64_t kLowbitsMask = android::BufferHubDefs::kLowbitsMask; -static constexpr uint64_t kHighBitsMask = android::BufferHubDefs::kHighBitsMask; -static constexpr uint64_t kFirstClientBitMask = +static constexpr uint32_t kLowbitsMask = android::BufferHubDefs::kLowbitsMask; +static constexpr uint32_t kHighBitsMask = android::BufferHubDefs::kHighBitsMask; +static constexpr uint32_t kFirstClientBitMask = android::BufferHubDefs::kFirstClientBitMask; -static inline bool AnyClientGained(uint64_t state) { +static inline bool AnyClientGained(uint32_t state) { return android::BufferHubDefs::AnyClientGained(state); } -static inline bool IsClientGained(uint64_t state, uint64_t client_bit_mask) { +static inline bool IsClientGained(uint32_t state, uint32_t client_bit_mask) { return android::BufferHubDefs::IsClientGained(state, client_bit_mask); } -static inline bool AnyClientPosted(uint64_t state) { +static inline bool AnyClientPosted(uint32_t state) { return android::BufferHubDefs::AnyClientPosted(state); } -static inline bool IsClientPosted(uint64_t state, uint64_t client_bit_mask) { +static inline bool IsClientPosted(uint32_t state, uint32_t client_bit_mask) { return android::BufferHubDefs::IsClientPosted(state, client_bit_mask); } -static inline bool AnyClientAcquired(uint64_t state) { +static inline bool AnyClientAcquired(uint32_t state) { return android::BufferHubDefs::AnyClientAcquired(state); } -static inline bool IsClientAcquired(uint64_t state, uint64_t client_bit_mask) { +static inline bool IsClientAcquired(uint32_t state, uint32_t client_bit_mask) { return android::BufferHubDefs::IsClientAcquired(state, client_bit_mask); } -static inline bool IsBufferReleased(uint64_t state) { +static inline bool IsBufferReleased(uint32_t state) { return android::BufferHubDefs::IsBufferReleased(state); } -static inline bool IsClientReleased(uint64_t state, uint64_t client_bit_mask) { +static inline bool IsClientReleased(uint32_t state, uint32_t client_bit_mask) { return android::BufferHubDefs::IsClientReleased(state, client_bit_mask); } // Returns the next available buffer client's client_state_masks. // @params union_bits. Union of all existing clients' client_state_masks. -static inline uint64_t FindNextAvailableClientStateMask(uint64_t union_bits) { +static inline uint32_t FindNextAvailableClientStateMask(uint32_t union_bits) { return android::BufferHubDefs::FindNextAvailableClientStateMask(union_bits); } @@ -77,7 +77,7 @@ class BufferTraits { BufferTraits() = default; BufferTraits(const native_handle_t* buffer_handle, const FileHandleType& metadata_handle, int id, - uint64_t client_state_mask, uint64_t metadata_size, + uint32_t client_state_mask, uint64_t metadata_size, uint32_t width, uint32_t height, uint32_t layer_count, uint32_t format, uint64_t usage, uint32_t stride, const FileHandleType& acquire_fence_fd, @@ -107,7 +107,7 @@ class BufferTraits { // same buffer channel has uniqued state bit among its siblings. For a // producer buffer the bit must be kFirstClientBitMask; for a consumer the bit // must be one of the kConsumerStateMask. - uint64_t client_state_mask() const { return client_state_mask_; } + uint32_t client_state_mask() const { return client_state_mask_; } uint64_t metadata_size() const { return metadata_size_; } uint32_t width() { return width_; } @@ -131,7 +131,7 @@ class BufferTraits { private: // BufferHub specific traits. int id_ = -1; - uint64_t client_state_mask_; + uint32_t client_state_mask_; uint64_t metadata_size_; // Traits for a GraphicBuffer. diff --git a/libs/vr/libbufferhub/include/private/dvr/bufferhub_rpc.h b/libs/vr/libbufferhub/include/private/dvr/bufferhub_rpc.h index 5ff4e00a01..f1cd0b4adc 100644 --- a/libs/vr/libbufferhub/include/private/dvr/bufferhub_rpc.h +++ b/libs/vr/libbufferhub/include/private/dvr/bufferhub_rpc.h @@ -99,7 +99,7 @@ class BufferDescription { public: BufferDescription() = default; BufferDescription(const IonBuffer& buffer, const IonBuffer& metadata, int id, - int buffer_cid, uint64_t client_state_mask, + int buffer_cid, uint32_t client_state_mask, const FileHandleType& acquire_fence_fd, const FileHandleType& release_fence_fd) : id_(id), @@ -123,7 +123,7 @@ class BufferDescription { // State mask of the buffer client. Each BufferHub client backed by the // same buffer channel has uniqued state bit among its siblings. - uint64_t client_state_mask() const { return client_state_mask_; } + uint32_t client_state_mask() const { return client_state_mask_; } FileHandleType take_acquire_fence() { return std::move(acquire_fence_fd_); } FileHandleType take_release_fence() { return std::move(release_fence_fd_); } @@ -133,7 +133,7 @@ class BufferDescription { private: int id_{-1}; int buffer_cid_{-1}; - uint64_t client_state_mask_{0}; + uint32_t client_state_mask_{0U}; // Two IonBuffers: one for the graphic buffer and one for metadata. NativeBufferHandle buffer_; NativeBufferHandle metadata_; diff --git a/libs/vr/libbufferhub/producer_buffer.cpp b/libs/vr/libbufferhub/producer_buffer.cpp index cd92b625d5..5274bf25d5 100644 --- a/libs/vr/libbufferhub/producer_buffer.cpp +++ b/libs/vr/libbufferhub/producer_buffer.cpp @@ -80,20 +80,20 @@ int ProducerBuffer::LocalPost(const DvrNativeBufferMetadata* meta, return error; // The buffer can be posted iff the buffer state for this client is gained. - uint64_t current_buffer_state = + uint32_t current_buffer_state = buffer_state_->load(std::memory_order_acquire); if (!BufferHubDefs::IsClientGained(current_buffer_state, client_state_mask())) { - ALOGE("%s: not gained, id=%d state=%" PRIx64 ".", __FUNCTION__, id(), + ALOGE("%s: not gained, id=%d state=%" PRIx32 ".", __FUNCTION__, id(), current_buffer_state); return -EBUSY; } // Set the producer client buffer state to released, other clients' buffer // state to posted. - uint64_t current_active_clients_bit_mask = + uint32_t current_active_clients_bit_mask = active_clients_bit_mask_->load(std::memory_order_acquire); - uint64_t updated_buffer_state = current_active_clients_bit_mask & + uint32_t updated_buffer_state = current_active_clients_bit_mask & (~client_state_mask()) & BufferHubDefs::kHighBitsMask; while (!buffer_state_->compare_exchange_weak( @@ -101,16 +101,16 @@ int ProducerBuffer::LocalPost(const DvrNativeBufferMetadata* meta, std::memory_order_acquire)) { ALOGD( "%s: Failed to post the buffer. Current buffer state was changed to " - "%" PRIx64 + "%" PRIx32 " when trying to post the buffer and modify the buffer state to " - "%" PRIx64 + "%" PRIx32 ". About to try again if the buffer is still gained by this client.", __FUNCTION__, current_buffer_state, updated_buffer_state); if (!BufferHubDefs::IsClientGained(current_buffer_state, client_state_mask())) { ALOGE( "%s: Failed to post the buffer. The buffer is no longer gained, " - "id=%d state=%" PRIx64 ".", + "id=%d state=%" PRIx32 ".", __FUNCTION__, id(), current_buffer_state); return -EBUSY; } @@ -164,9 +164,9 @@ int ProducerBuffer::LocalGain(DvrNativeBufferMetadata* out_meta, if (!out_meta) return -EINVAL; - uint64_t current_buffer_state = + uint32_t current_buffer_state = buffer_state_->load(std::memory_order_acquire); - ALOGD_IF(TRACE, "%s: buffer=%d, state=%" PRIx64 ".", __FUNCTION__, id(), + ALOGD_IF(TRACE, "%s: buffer=%d, state=%" PRIx32 ".", __FUNCTION__, id(), current_buffer_state); if (BufferHubDefs::IsClientGained(current_buffer_state, @@ -178,20 +178,20 @@ int ProducerBuffer::LocalGain(DvrNativeBufferMetadata* out_meta, BufferHubDefs::AnyClientGained(current_buffer_state) || (BufferHubDefs::AnyClientPosted(current_buffer_state) && !gain_posted_buffer)) { - ALOGE("%s: not released id=%d state=%" PRIx64 ".", __FUNCTION__, id(), + ALOGE("%s: not released id=%d state=%" PRIx32 ".", __FUNCTION__, id(), current_buffer_state); return -EBUSY; } // Change the buffer state to gained state. - uint64_t updated_buffer_state = client_state_mask(); + uint32_t updated_buffer_state = client_state_mask(); while (!buffer_state_->compare_exchange_weak( current_buffer_state, updated_buffer_state, std::memory_order_acq_rel, std::memory_order_acquire)) { ALOGD( "%s: Failed to gain the buffer. Current buffer state was changed to " - "%" PRIx64 + "%" PRIx32 " when trying to gain the buffer and modify the buffer state to " - "%" PRIx64 + "%" PRIx32 ". About to try again if the buffer is still not read by other " "clients.", __FUNCTION__, current_buffer_state, updated_buffer_state); @@ -202,7 +202,7 @@ int ProducerBuffer::LocalGain(DvrNativeBufferMetadata* out_meta, !gain_posted_buffer)) { ALOGE( "%s: Failed to gain the buffer. The buffer is no longer released. " - "id=%d state=%" PRIx64 ".", + "id=%d state=%" PRIx32 ".", __FUNCTION__, id(), current_buffer_state); return -EBUSY; } @@ -221,8 +221,8 @@ int ProducerBuffer::LocalGain(DvrNativeBufferMetadata* out_meta, out_meta->user_metadata_ptr = 0; } - uint64_t current_fence_state = fence_state_->load(std::memory_order_acquire); - uint64_t current_active_clients_bit_mask = + uint32_t current_fence_state = fence_state_->load(std::memory_order_acquire); + uint32_t current_active_clients_bit_mask = active_clients_bit_mask_->load(std::memory_order_acquire); // If there are release fence(s) from consumer(s), we need to return it to the // consumer(s). @@ -289,11 +289,11 @@ Status ProducerBuffer::Detach() { // TODO(b/112338294) Keep here for reference. Remove it after new logic is // written. - /* uint64_t buffer_state = buffer_state_->load(std::memory_order_acquire); + /* uint32_t buffer_state = buffer_state_->load(std::memory_order_acquire); if (!BufferHubDefs::IsClientGained( buffer_state, BufferHubDefs::kFirstClientStateMask)) { // Can only detach a ProducerBuffer when it's in gained state. - ALOGW("ProducerBuffer::Detach: The buffer (id=%d, state=0x%" PRIx64 + ALOGW("ProducerBuffer::Detach: The buffer (id=%d, state=0x%" PRIx32 ") is not in gained state.", id(), buffer_state); return {}; diff --git a/libs/vr/libdvr/include/dvr/dvr_api.h b/libs/vr/libdvr/include/dvr/dvr_api.h index a204f62fff..e383bb2cb3 100644 --- a/libs/vr/libdvr/include/dvr/dvr_api.h +++ b/libs/vr/libdvr/include/dvr/dvr_api.h @@ -466,11 +466,11 @@ struct ALIGNED_DVR_STRUCT(8) DvrNativeBufferMetadata { // Only applicable for metadata retrieved from GainAsync. This indicates which // consumer has pending fence that producer should epoll on. - uint64_t release_fence_mask; + uint32_t release_fence_mask; // Reserved bytes for so that the struct is forward compatible and padding to // 104 bytes so the size is a multiple of 8. - int32_t reserved[8]; + int32_t reserved[9]; }; #ifdef __cplusplus diff --git a/libs/vr/libdvr/tests/dvr_buffer_queue-test.cpp b/libs/vr/libdvr/tests/dvr_buffer_queue-test.cpp index 2d5f0043e3..df060973ec 100644 --- a/libs/vr/libdvr/tests/dvr_buffer_queue-test.cpp +++ b/libs/vr/libdvr/tests/dvr_buffer_queue-test.cpp @@ -62,7 +62,7 @@ class DvrBufferQueueTest : public DvrApiTest { buffer_removed_count_); } - DvrWriteBufferQueue* write_queue_{nullptr}; + DvrWriteBufferQueue* write_queue_ = nullptr; int buffer_available_count_{0}; int buffer_removed_count_{0}; }; diff --git a/services/bufferhub/BufferNode.cpp b/services/bufferhub/BufferNode.cpp index 4bad829210..cc87e15917 100644 --- a/services/bufferhub/BufferNode.cpp +++ b/services/bufferhub/BufferNode.cpp @@ -14,10 +14,10 @@ void BufferNode::InitializeMetadata() { // Using placement new here to reuse shared memory instead of new allocation // Initialize the atomic variables to zero. BufferHubDefs::MetadataHeader* metadata_header = metadata_.metadata_header(); - buffer_state_ = new (&metadata_header->buffer_state) std::atomic(0); - fence_state_ = new (&metadata_header->fence_state) std::atomic(0); + buffer_state_ = new (&metadata_header->buffer_state) std::atomic(0); + fence_state_ = new (&metadata_header->fence_state) std::atomic(0); active_clients_bit_mask_ = - new (&metadata_header->active_clients_bit_mask) std::atomic(0); + new (&metadata_header->active_clients_bit_mask) std::atomic(0); } // Allocates a new BufferNode. @@ -74,22 +74,22 @@ BufferNode::~BufferNode() { } } -uint64_t BufferNode::GetActiveClientsBitMask() const { +uint32_t BufferNode::GetActiveClientsBitMask() const { return active_clients_bit_mask_->load(std::memory_order_acquire); } -uint64_t BufferNode::AddNewActiveClientsBitToMask() { - uint64_t current_active_clients_bit_mask = GetActiveClientsBitMask(); - uint64_t client_state_mask = 0ULL; - uint64_t updated_active_clients_bit_mask = 0ULL; +uint32_t BufferNode::AddNewActiveClientsBitToMask() { + uint32_t current_active_clients_bit_mask = GetActiveClientsBitMask(); + uint32_t client_state_mask = 0U; + uint32_t updated_active_clients_bit_mask = 0U; do { client_state_mask = BufferHubDefs::FindNextAvailableClientStateMask(current_active_clients_bit_mask); - if (client_state_mask == 0ULL) { + if (client_state_mask == 0U) { ALOGE("%s: reached the maximum number of channels per buffer node: %d.", __FUNCTION__, BufferHubDefs::kMaxNumberOfClients); errno = E2BIG; - return 0ULL; + return 0U; } updated_active_clients_bit_mask = current_active_clients_bit_mask | client_state_mask; } while (!(active_clients_bit_mask_->compare_exchange_weak(current_active_clients_bit_mask, @@ -99,7 +99,7 @@ uint64_t BufferNode::AddNewActiveClientsBitToMask() { return client_state_mask; } -void BufferNode::RemoveClientsBitFromMask(const uint64_t& value) { +void BufferNode::RemoveClientsBitFromMask(const uint32_t& value) { active_clients_bit_mask_->fetch_and(~value); } diff --git a/services/bufferhub/include/bufferhub/BufferHubIdGenerator.h b/services/bufferhub/include/bufferhub/BufferHubIdGenerator.h index c5b2cdef33..b51fcda3cd 100644 --- a/services/bufferhub/include/bufferhub/BufferHubIdGenerator.h +++ b/services/bufferhub/include/bufferhub/BufferHubIdGenerator.h @@ -32,7 +32,7 @@ namespace implementation { class BufferHubIdGenerator { public: // 0 is considered invalid - static constexpr uint32_t kInvalidId = 0UL; + static constexpr uint32_t kInvalidId = 0U; // Get the singleton instance of this class static BufferHubIdGenerator& getInstance(); diff --git a/services/bufferhub/include/bufferhub/BufferNode.h b/services/bufferhub/include/bufferhub/BufferNode.h index 02bb5af0e0..cf56c33ec0 100644 --- a/services/bufferhub/include/bufferhub/BufferNode.h +++ b/services/bufferhub/include/bufferhub/BufferNode.h @@ -38,19 +38,19 @@ public: // Gets the current value of active_clients_bit_mask in metadata_ with // std::memory_order_acquire, so that all previous releases of // active_clients_bit_mask from all threads will be returned here. - uint64_t GetActiveClientsBitMask() const; + uint32_t GetActiveClientsBitMask() const; // Find and add a new client_state_mask to active_clients_bit_mask in // metadata_. // Return the new client_state_mask that is added to active_clients_bit_mask. - // Return 0ULL if there are already 32 bp clients of the buffer. - uint64_t AddNewActiveClientsBitToMask(); + // Return 0U if there are already 16 clients of the buffer. + uint32_t AddNewActiveClientsBitToMask(); // Removes the value from active_clients_bit_mask in metadata_ with // std::memory_order_release, so that the change will be visible to any // acquire of active_clients_bit_mask_ in any threads after the succeed of // this operation. - void RemoveClientsBitFromMask(const uint64_t& value); + void RemoveClientsBitFromMask(const uint32_t& value); private: // Helper method for constructors to initialize atomic metadata header @@ -75,14 +75,14 @@ private: // buffer_state_ tracks the state of the buffer. Buffer can be in one of these // four states: gained, posted, acquired, released. - std::atomic* buffer_state_ = nullptr; + std::atomic* buffer_state_ = nullptr; // TODO(b/112012161): add comments to fence_state_. - std::atomic* fence_state_ = nullptr; + std::atomic* fence_state_ = nullptr; // active_clients_bit_mask_ tracks all the bp clients of the buffer. It is the // union of all client_state_mask of all bp clients. - std::atomic* active_clients_bit_mask_ = nullptr; + std::atomic* active_clients_bit_mask_ = nullptr; }; } // namespace implementation diff --git a/services/bufferhub/tests/BufferNode_test.cpp b/services/bufferhub/tests/BufferNode_test.cpp index 8555eb7800..dbf10e8cc5 100644 --- a/services/bufferhub/tests/BufferNode_test.cpp +++ b/services/bufferhub/tests/BufferNode_test.cpp @@ -56,21 +56,21 @@ TEST_F(BufferNodeTest, TestCreateBufferNode) { } TEST_F(BufferNodeTest, TestAddNewActiveClientsBitToMask_twoNewClients) { - uint64_t new_client_state_mask_1 = buffer_node->AddNewActiveClientsBitToMask(); + uint32_t new_client_state_mask_1 = buffer_node->AddNewActiveClientsBitToMask(); EXPECT_EQ(buffer_node->GetActiveClientsBitMask(), new_client_state_mask_1); // Request and add a new client_state_mask again. // Active clients bit mask should be the union of the two new // client_state_masks. - uint64_t new_client_state_mask_2 = buffer_node->AddNewActiveClientsBitToMask(); + uint32_t new_client_state_mask_2 = buffer_node->AddNewActiveClientsBitToMask(); EXPECT_EQ(buffer_node->GetActiveClientsBitMask(), new_client_state_mask_1 | new_client_state_mask_2); } TEST_F(BufferNodeTest, TestAddNewActiveClientsBitToMask_32NewClients) { - uint64_t new_client_state_mask = 0ULL; - uint64_t current_mask = 0ULL; - uint64_t expected_mask = 0ULL; + uint32_t new_client_state_mask = 0U; + uint32_t current_mask = 0U; + uint32_t expected_mask = 0U; for (int i = 0; i < BufferHubDefs::kMaxNumberOfClients; ++i) { new_client_state_mask = buffer_node->AddNewActiveClientsBitToMask(); @@ -83,14 +83,14 @@ TEST_F(BufferNodeTest, TestAddNewActiveClientsBitToMask_32NewClients) { // Method should fail upon requesting for more than maximum allowable clients. new_client_state_mask = buffer_node->AddNewActiveClientsBitToMask(); - EXPECT_EQ(new_client_state_mask, 0ULL); + EXPECT_EQ(new_client_state_mask, 0U); EXPECT_EQ(errno, E2BIG); } TEST_F(BufferNodeTest, TestRemoveActiveClientsBitFromMask) { buffer_node->AddNewActiveClientsBitToMask(); - uint64_t current_mask = buffer_node->GetActiveClientsBitMask(); - uint64_t new_client_state_mask = buffer_node->AddNewActiveClientsBitToMask(); + uint32_t current_mask = buffer_node->GetActiveClientsBitMask(); + uint32_t new_client_state_mask = buffer_node->AddNewActiveClientsBitToMask(); EXPECT_NE(buffer_node->GetActiveClientsBitMask(), current_mask); buffer_node->RemoveClientsBitFromMask(new_client_state_mask); diff --git a/services/vr/bufferhubd/buffer_channel.cpp b/services/vr/bufferhubd/buffer_channel.cpp index cf072b667d..695396caf2 100644 --- a/services/vr/bufferhubd/buffer_channel.cpp +++ b/services/vr/bufferhubd/buffer_channel.cpp @@ -32,7 +32,7 @@ BufferChannel::BufferChannel(BufferHubService* service, int buffer_id, : BufferHubChannel(service, buffer_id, channel_id, kDetachedBufferType), buffer_node_(buffer_node) { client_state_mask_ = buffer_node_->AddNewActiveClientsBitToMask(); - if (client_state_mask_ == 0ULL) { + if (client_state_mask_ == 0U) { ALOGE("BufferChannel::BufferChannel: %s", strerror(errno)); buffer_node_ = nullptr; } @@ -41,7 +41,7 @@ BufferChannel::BufferChannel(BufferHubService* service, int buffer_id, BufferChannel::~BufferChannel() { ALOGD_IF(TRACE, "BufferChannel::~BufferChannel: channel_id=%d buffer_id=%d.", channel_id(), buffer_id()); - if (client_state_mask_ != 0ULL) { + if (client_state_mask_ != 0U) { buffer_node_->RemoveClientsBitFromMask(client_state_mask_); } Hangup(); diff --git a/services/vr/bufferhubd/consumer_channel.cpp b/services/vr/bufferhubd/consumer_channel.cpp index 158ef9ceaa..c7695bc51f 100644 --- a/services/vr/bufferhubd/consumer_channel.cpp +++ b/services/vr/bufferhubd/consumer_channel.cpp @@ -17,7 +17,7 @@ namespace android { namespace dvr { ConsumerChannel::ConsumerChannel(BufferHubService* service, int buffer_id, - int channel_id, uint64_t client_state_mask, + int channel_id, uint32_t client_state_mask, const std::shared_ptr producer) : BufferHubChannel(service, buffer_id, channel_id, kConsumerType), client_state_mask_(client_state_mask), diff --git a/services/vr/bufferhubd/include/private/dvr/buffer_channel.h b/services/vr/bufferhubd/include/private/dvr/buffer_channel.h index 744c095338..9888db6642 100644 --- a/services/vr/bufferhubd/include/private/dvr/buffer_channel.h +++ b/services/vr/bufferhubd/include/private/dvr/buffer_channel.h @@ -51,8 +51,8 @@ class BufferChannel : public BufferHubChannel { // The concrete implementation of the Buffer object. std::shared_ptr buffer_node_ = nullptr; - // The state bit of this buffer. Must be one the lower 63 bits. - uint64_t client_state_mask_ = 0ULL; + // The state bit of this buffer. + uint32_t client_state_mask_ = 0U; }; } // namespace dvr diff --git a/services/vr/bufferhubd/include/private/dvr/consumer_channel.h b/services/vr/bufferhubd/include/private/dvr/consumer_channel.h index 5fb4ec1725..5ee551f115 100644 --- a/services/vr/bufferhubd/include/private/dvr/consumer_channel.h +++ b/services/vr/bufferhubd/include/private/dvr/consumer_channel.h @@ -16,14 +16,14 @@ class ConsumerChannel : public BufferHubChannel { using Message = pdx::Message; ConsumerChannel(BufferHubService* service, int buffer_id, int channel_id, - uint64_t client_state_mask, + uint32_t client_state_mask, const std::shared_ptr producer); ~ConsumerChannel() override; bool HandleMessage(Message& message) override; void HandleImpulse(Message& message) override; - uint64_t client_state_mask() const { return client_state_mask_; } + uint32_t client_state_mask() const { return client_state_mask_; } BufferInfo GetBufferInfo() const override; void OnProducerGained(); @@ -39,7 +39,7 @@ class ConsumerChannel : public BufferHubChannel { pdx::Status OnConsumerRelease(Message& message, LocalFence release_fence); - uint64_t client_state_mask_{0}; + uint32_t client_state_mask_{0U}; bool acquired_{false}; bool released_{true}; std::weak_ptr producer_; diff --git a/services/vr/bufferhubd/include/private/dvr/producer_channel.h b/services/vr/bufferhubd/include/private/dvr/producer_channel.h index 4734439196..96ef1a20a0 100644 --- a/services/vr/bufferhubd/include/private/dvr/producer_channel.h +++ b/services/vr/bufferhubd/include/private/dvr/producer_channel.h @@ -42,7 +42,7 @@ class ProducerChannel : public BufferHubChannel { ~ProducerChannel() override; - uint64_t buffer_state() const { + uint32_t buffer_state() const { return buffer_state_->load(std::memory_order_acquire); } @@ -51,18 +51,18 @@ class ProducerChannel : public BufferHubChannel { BufferInfo GetBufferInfo() const override; - BufferDescription GetBuffer(uint64_t client_state_mask); + BufferDescription GetBuffer(uint32_t client_state_mask); pdx::Status CreateConsumer(Message& message, - uint64_t consumer_state_mask); - pdx::Status CreateConsumerStateMask(); + uint32_t consumer_state_mask); + pdx::Status CreateConsumerStateMask(); pdx::Status OnNewConsumer(Message& message); pdx::Status OnConsumerAcquire(Message& message); pdx::Status OnConsumerRelease(Message& message, LocalFence release_fence); - void OnConsumerOrphaned(const uint64_t& consumer_state_mask); + void OnConsumerOrphaned(const uint32_t& consumer_state_mask); void AddConsumer(ConsumerChannel* channel); void RemoveConsumer(ConsumerChannel* channel); @@ -79,13 +79,13 @@ class ProducerChannel : public BufferHubChannel { // IonBuffer that is shared between bufferhubd, producer, and consumers. IonBuffer metadata_buffer_; BufferHubDefs::MetadataHeader* metadata_header_ = nullptr; - std::atomic* buffer_state_ = nullptr; - std::atomic* fence_state_ = nullptr; - std::atomic* active_clients_bit_mask_ = nullptr; + std::atomic* buffer_state_ = nullptr; + std::atomic* fence_state_ = nullptr; + std::atomic* active_clients_bit_mask_ = nullptr; // All orphaned consumer bits. Valid bits are the lower 63 bits, while the // highest bit is reserved for the producer and should not be set. - uint64_t orphaned_consumer_bit_mask_{0ULL}; + uint32_t orphaned_consumer_bit_mask_{0U}; LocalFence post_fence_; LocalFence returned_fence_; @@ -110,7 +110,7 @@ class ProducerChannel : public BufferHubChannel { // Remove consumer from atomics in shared memory based on consumer_state_mask. // This function is used for clean up for failures in CreateConsumer method. - void RemoveConsumerClientMask(uint64_t consumer_state_mask); + void RemoveConsumerClientMask(uint32_t consumer_state_mask); ProducerChannel(const ProducerChannel&) = delete; void operator=(const ProducerChannel&) = delete; diff --git a/services/vr/bufferhubd/producer_channel.cpp b/services/vr/bufferhubd/producer_channel.cpp index b541fb3963..1682bfeb33 100644 --- a/services/vr/bufferhubd/producer_channel.cpp +++ b/services/vr/bufferhubd/producer_channel.cpp @@ -93,10 +93,10 @@ int ProducerChannel::InitializeBuffer() { // Using placement new here to reuse shared memory instead of new allocation // and also initialize the value to zero. buffer_state_ = - new (&metadata_header_->buffer_state) std::atomic(0); - fence_state_ = new (&metadata_header_->fence_state) std::atomic(0); + new (&metadata_header_->buffer_state) std::atomic(0); + fence_state_ = new (&metadata_header_->fence_state) std::atomic(0); active_clients_bit_mask_ = - new (&metadata_header_->active_clients_bit_mask) std::atomic(0); + new (&metadata_header_->active_clients_bit_mask) std::atomic(0); // Producer channel is never created after consumer channel, and one buffer // only have one fixed producer for now. Thus, it is correct to assume @@ -119,7 +119,7 @@ int ProducerChannel::InitializeBuffer() { epoll_event event; event.events = 0; - event.data.u64 = 0ULL; + event.data.u32 = 0U; if (epoll_ctl(release_fence_fd_.Get(), EPOLL_CTL_ADD, dummy_fence_fd_.Get(), &event) < 0) { ALOGE( @@ -164,7 +164,7 @@ Status> ProducerChannel::Create( ProducerChannel::~ProducerChannel() { ALOGD_IF(TRACE, "ProducerChannel::~ProducerChannel: channel_id=%d buffer_id=%d " - "state=%" PRIx64 ".", + "state=%" PRIx32 ".", channel_id(), buffer_id(), buffer_state_->load(std::memory_order_acquire)); for (auto consumer : consumer_channels_) { @@ -175,7 +175,7 @@ ProducerChannel::~ProducerChannel() { BufferHubChannel::BufferInfo ProducerChannel::GetBufferInfo() const { // Derive the mask of signaled buffers in this producer / consumer set. - uint64_t signaled_mask = signaled() ? BufferHubDefs::kFirstClientBitMask : 0; + uint32_t signaled_mask = signaled() ? BufferHubDefs::kFirstClientBitMask : 0; for (const ConsumerChannel* consumer : consumer_channels_) { signaled_mask |= consumer->signaled() ? consumer->client_state_mask() : 0; } @@ -228,7 +228,7 @@ bool ProducerChannel::HandleMessage(Message& message) { } BufferDescription ProducerChannel::GetBuffer( - uint64_t client_state_mask) { + uint32_t client_state_mask) { return {buffer_, metadata_buffer_, buffer_id(), @@ -241,27 +241,27 @@ BufferDescription ProducerChannel::GetBuffer( Status> ProducerChannel::OnGetBuffer( Message& /*message*/) { ATRACE_NAME("ProducerChannel::OnGetBuffer"); - ALOGD_IF(TRACE, "ProducerChannel::OnGetBuffer: buffer=%d, state=%" PRIx64 ".", + ALOGD_IF(TRACE, "ProducerChannel::OnGetBuffer: buffer=%d, state=%" PRIx32 ".", buffer_id(), buffer_state_->load(std::memory_order_acquire)); return {GetBuffer(BufferHubDefs::kFirstClientBitMask)}; } -Status ProducerChannel::CreateConsumerStateMask() { +Status ProducerChannel::CreateConsumerStateMask() { // Try find the next consumer state bit which has not been claimed by any // consumer yet. // memory_order_acquire is chosen here because all writes in other threads // that release active_clients_bit_mask_ need to be visible here. - uint64_t current_active_clients_bit_mask = + uint32_t current_active_clients_bit_mask = active_clients_bit_mask_->load(std::memory_order_acquire); - uint64_t consumer_state_mask = + uint32_t consumer_state_mask = BufferHubDefs::FindNextAvailableClientStateMask( current_active_clients_bit_mask | orphaned_consumer_bit_mask_); - if (consumer_state_mask == 0ULL) { + if (consumer_state_mask == 0U) { ALOGE("%s: reached the maximum mumber of consumers per producer: 63.", __FUNCTION__); return ErrorStatus(E2BIG); } - uint64_t updated_active_clients_bit_mask = + uint32_t updated_active_clients_bit_mask = current_active_clients_bit_mask | consumer_state_mask; // Set the updated value only if the current value stays the same as what was // read before. If the comparison succeeds, update the value without @@ -274,15 +274,15 @@ Status ProducerChannel::CreateConsumerStateMask() { while (!active_clients_bit_mask_->compare_exchange_weak( current_active_clients_bit_mask, updated_active_clients_bit_mask, std::memory_order_acq_rel, std::memory_order_acquire)) { - ALOGE("%s: Current active clients bit mask is changed to %" PRIx64 - ", which was expected to be %" PRIx64 + ALOGE("%s: Current active clients bit mask is changed to %" PRIx32 + ", which was expected to be %" PRIx32 ". Trying to generate a new client state mask to resolve race " "condition.", __FUNCTION__, updated_active_clients_bit_mask, current_active_clients_bit_mask); consumer_state_mask = BufferHubDefs::FindNextAvailableClientStateMask( current_active_clients_bit_mask | orphaned_consumer_bit_mask_); - if (consumer_state_mask == 0ULL) { + if (consumer_state_mask == 0U) { ALOGE("%s: reached the maximum mumber of consumers per producer: %d.", __FUNCTION__, (BufferHubDefs::kMaxNumberOfClients - 1)); return ErrorStatus(E2BIG); @@ -294,7 +294,7 @@ Status ProducerChannel::CreateConsumerStateMask() { return {consumer_state_mask}; } -void ProducerChannel::RemoveConsumerClientMask(uint64_t consumer_state_mask) { +void ProducerChannel::RemoveConsumerClientMask(uint32_t consumer_state_mask) { // Clear up the buffer state and fence state in case there is already // something there due to possible race condition between producer post and // consumer failed to create channel. @@ -308,7 +308,7 @@ void ProducerChannel::RemoveConsumerClientMask(uint64_t consumer_state_mask) { } Status ProducerChannel::CreateConsumer( - Message& message, uint64_t consumer_state_mask) { + Message& message, uint32_t consumer_state_mask) { ATRACE_NAME(__FUNCTION__); ALOGD_IF(TRACE, "%s: buffer_id=%d", __FUNCTION__, buffer_id()); @@ -332,7 +332,7 @@ Status ProducerChannel::CreateConsumer( return ErrorStatus(ENOMEM); } - uint64_t current_buffer_state = + uint32_t current_buffer_state = buffer_state_->load(std::memory_order_acquire); if (BufferHubDefs::IsBufferReleased(current_buffer_state) || BufferHubDefs::AnyClientGained(current_buffer_state)) { @@ -343,7 +343,7 @@ Status ProducerChannel::CreateConsumer( bool update_buffer_state = true; if (!BufferHubDefs::IsClientPosted(current_buffer_state, consumer_state_mask)) { - uint64_t updated_buffer_state = + uint32_t updated_buffer_state = current_buffer_state ^ (consumer_state_mask & BufferHubDefs::kHighBitsMask); while (!buffer_state_->compare_exchange_weak( @@ -351,15 +351,15 @@ Status ProducerChannel::CreateConsumer( std::memory_order_acquire)) { ALOGI( "%s: Failed to post to the new consumer. " - "Current buffer state was changed to %" PRIx64 + "Current buffer state was changed to %" PRIx32 " when trying to acquire the buffer and modify the buffer state to " - "%" PRIx64 + "%" PRIx32 ". About to try again if the buffer is still not gained nor fully " "released.", __FUNCTION__, current_buffer_state, updated_buffer_state); if (BufferHubDefs::IsBufferReleased(current_buffer_state) || BufferHubDefs::AnyClientGained(current_buffer_state)) { - ALOGI("%s: buffer is gained or fully released, state=%" PRIx64 ".", + ALOGI("%s: buffer is gained or fully released, state=%" PRIx32 ".", __FUNCTION__, current_buffer_state); update_buffer_state = false; break; @@ -393,7 +393,7 @@ Status ProducerChannel::OnProducerPost(Message&, epoll_event event; event.events = 0; - event.data.u64 = 0ULL; + event.data.u32 = 0U; int ret = epoll_ctl(release_fence_fd_.Get(), EPOLL_CTL_MOD, dummy_fence_fd_.Get(), &event); ALOGE_IF(ret < 0, @@ -401,7 +401,7 @@ Status ProducerChannel::OnProducerPost(Message&, "release fence to include the dummy fence: %s", strerror(errno)); - eventfd_t dummy_fence_count = 0ULL; + eventfd_t dummy_fence_count = 0U; if (eventfd_read(dummy_fence_fd_.Get(), &dummy_fence_count) < 0) { const int error = errno; if (error != EAGAIN) { @@ -451,13 +451,13 @@ Status ProducerChannel::OnProducerGain(Message& /*message*/) { ALOGD_IF(TRACE, "ProducerChannel::OnProducerDetach: buffer_id=%d", buffer_id()); - uint64_t buffer_state = buffer_state_->load(std::memory_order_acquire); + uint32_t buffer_state = buffer_state_->load(std::memory_order_acquire); if (!BufferHubDefs::IsClientGained( buffer_state, BufferHubDefs::kFirstClientStateMask)) { // Can only detach a BufferProducer when it's in gained state. ALOGW( "ProducerChannel::OnProducerDetach: The buffer (id=%d, state=%" - PRIx64 + PRIx32 ") is not in gained state.", buffer_id(), buffer_state); return {}; @@ -534,7 +534,7 @@ Status ProducerChannel::OnConsumerRelease(Message&, } } - uint64_t current_buffer_state = + uint32_t current_buffer_state = buffer_state_->load(std::memory_order_acquire); if (BufferHubDefs::IsBufferReleased(current_buffer_state & ~orphaned_consumer_bit_mask_)) { @@ -542,7 +542,7 @@ Status ProducerChannel::OnConsumerRelease(Message&, if (orphaned_consumer_bit_mask_) { ALOGW( "%s: orphaned buffer detected during the this acquire/release cycle: " - "id=%d orphaned=0x%" PRIx64 " queue_index=%" PRIx64 ".", + "id=%d orphaned=0x%" PRIx32 " queue_index=%" PRIx64 ".", __FUNCTION__, buffer_id(), orphaned_consumer_bit_mask_, metadata_header_->queue_index); orphaned_consumer_bit_mask_ = 0; @@ -552,16 +552,16 @@ Status ProducerChannel::OnConsumerRelease(Message&, return {}; } -void ProducerChannel::OnConsumerOrphaned(const uint64_t& consumer_state_mask) { +void ProducerChannel::OnConsumerOrphaned(const uint32_t& consumer_state_mask) { // Remember the ignored consumer so that newly added consumer won't be // taking the same state mask as this orphaned consumer. ALOGE_IF(orphaned_consumer_bit_mask_ & consumer_state_mask, - "%s: Consumer (consumer_state_mask=%" PRIx64 + "%s: Consumer (consumer_state_mask=%" PRIx32 ") is already orphaned.", __FUNCTION__, consumer_state_mask); orphaned_consumer_bit_mask_ |= consumer_state_mask; - uint64_t current_buffer_state = + uint32_t current_buffer_state = buffer_state_->load(std::memory_order_acquire); if (BufferHubDefs::IsBufferReleased(current_buffer_state & ~orphaned_consumer_bit_mask_)) { @@ -577,8 +577,8 @@ void ProducerChannel::OnConsumerOrphaned(const uint64_t& consumer_state_mask) { ALOGW( "%s: detected new orphaned consumer buffer_id=%d " - "consumer_state_mask=%" PRIx64 " queue_index=%" PRIx64 - " buffer_state=%" PRIx64 " fence_state=%" PRIx64 ".", + "consumer_state_mask=%" PRIx32 " queue_index=%" PRIx64 + " buffer_state=%" PRIx32 " fence_state=%" PRIx32 ".", __FUNCTION__, buffer_id(), consumer_state_mask, metadata_header_->queue_index, buffer_state_->load(std::memory_order_acquire), @@ -594,18 +594,18 @@ void ProducerChannel::RemoveConsumer(ConsumerChannel* channel) { std::find(consumer_channels_.begin(), consumer_channels_.end(), channel)); // Restore the consumer state bit and make it visible in other threads that // acquire the active_clients_bit_mask_. - uint64_t consumer_state_mask = channel->client_state_mask(); - uint64_t current_active_clients_bit_mask = + uint32_t consumer_state_mask = channel->client_state_mask(); + uint32_t current_active_clients_bit_mask = active_clients_bit_mask_->load(std::memory_order_acquire); - uint64_t updated_active_clients_bit_mask = + uint32_t updated_active_clients_bit_mask = current_active_clients_bit_mask & (~consumer_state_mask); while (!active_clients_bit_mask_->compare_exchange_weak( current_active_clients_bit_mask, updated_active_clients_bit_mask, std::memory_order_acq_rel, std::memory_order_acquire)) { ALOGI( "%s: Failed to remove consumer state mask. Current active clients bit " - "mask is changed to %" PRIu64 - " when trying to acquire and modify it to %" PRIu64 + "mask is changed to %" PRIx32 + " when trying to acquire and modify it to %" PRIx32 ". About to try again.", __FUNCTION__, current_active_clients_bit_mask, updated_active_clients_bit_mask); @@ -613,7 +613,7 @@ void ProducerChannel::RemoveConsumer(ConsumerChannel* channel) { current_active_clients_bit_mask & (~consumer_state_mask); } - const uint64_t current_buffer_state = + const uint32_t current_buffer_state = buffer_state_->load(std::memory_order_acquire); if (BufferHubDefs::IsClientPosted(current_buffer_state, consumer_state_mask) || @@ -634,7 +634,7 @@ void ProducerChannel::RemoveConsumer(ConsumerChannel* channel) { if (fence_state_->load(std::memory_order_acquire) & consumer_state_mask) { epoll_event event; event.events = EPOLLIN; - event.data.u64 = consumer_state_mask; + event.data.u32 = consumer_state_mask; if (epoll_ctl(release_fence_fd_.Get(), EPOLL_CTL_MOD, dummy_fence_fd_.Get(), &event) < 0) { ALOGE( -- cgit v1.2.3-59-g8ed1b From df9d91d0f822342d634d4d0b618a35fca100f1f4 Mon Sep 17 00:00:00 2001 From: Tianyu Jiang Date: Mon, 17 Dec 2018 10:48:54 -0800 Subject: Fix wrong log messages and format the code Bug: None Test: build Change-Id: I86d91e44fd96f067151eae700b702ae6e0beaeb7 --- libs/ui/BufferHubBuffer.cpp | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) (limited to 'libs/ui/BufferHubBuffer.cpp') diff --git a/libs/ui/BufferHubBuffer.cpp b/libs/ui/BufferHubBuffer.cpp index 5bc113f4b2..226b6ee9ad 100644 --- a/libs/ui/BufferHubBuffer.cpp +++ b/libs/ui/BufferHubBuffer.cpp @@ -95,22 +95,21 @@ LocalChannelHandle BufferHubClient::TakeChannelHandle() { BufferHubBuffer::BufferHubBuffer(uint32_t width, uint32_t height, uint32_t layerCount, uint32_t format, uint64_t usage, size_t mUserMetadataSize) { ATRACE_CALL(); - ALOGD("BufferHubBuffer::BufferHubBuffer: width=%u height=%u layerCount=%u, format=%u " - "usage=%" PRIx64 " mUserMetadataSize=%zu", - width, height, layerCount, format, usage, mUserMetadataSize); + ALOGD("%s: width=%u height=%u layerCount=%u, format=%u usage=%" PRIx64 " mUserMetadataSize=%zu", + __FUNCTION__, width, height, layerCount, format, usage, mUserMetadataSize); auto status = mClient.InvokeRemoteMethod(width, height, layerCount, format, usage, mUserMetadataSize); if (!status) { - ALOGE("BufferHubBuffer::BufferHubBuffer: Failed to create detached buffer: %s", + ALOGE("%s: Failed to create detached buffer: %s", __FUNCTION__, status.GetErrorMessage().c_str()); mClient.Close(-status.error()); } const int ret = ImportGraphicBuffer(); if (ret < 0) { - ALOGE("BufferHubBuffer::BufferHubBuffer: Failed to import buffer: %s", strerror(-ret)); + ALOGE("%s: Failed to import buffer: %s", __FUNCTION__, strerror(-ret)); mClient.Close(ret); } } @@ -119,7 +118,7 @@ BufferHubBuffer::BufferHubBuffer(LocalChannelHandle mChannelHandle) : mClient(std::move(mChannelHandle)) { const int ret = ImportGraphicBuffer(); if (ret < 0) { - ALOGE("BufferHubBuffer::BufferHubBuffer: Failed to import buffer: %s", strerror(-ret)); + ALOGE("%s: Failed to import buffer: %s", __FUNCTION__, strerror(-ret)); mClient.Close(ret); } } @@ -129,14 +128,14 @@ int BufferHubBuffer::ImportGraphicBuffer() { auto status = mClient.InvokeRemoteMethod(); if (!status) { - ALOGE("BufferHubBuffer::BufferHubBuffer: Failed to import GraphicBuffer: %s", + ALOGE("%s: Failed to import GraphicBuffer: %s", __FUNCTION__, status.GetErrorMessage().c_str()); return -status.error(); } BufferTraits bufferTraits = status.take(); if (bufferTraits.id() < 0) { - ALOGE("BufferHubBuffer::BufferHubBuffer: Received an invalid id!"); + ALOGE("%s: Received an invalid id!", __FUNCTION__); return -EIO; } @@ -149,20 +148,19 @@ int BufferHubBuffer::ImportGraphicBuffer() { mMetadata = BufferHubMetadata::Import(std::move(metadataFd)); if (!mMetadata.IsValid()) { - ALOGE("BufferHubBuffer::ImportGraphicBuffer: invalid metadata."); + ALOGE("%s: invalid metadata.", __FUNCTION__); return -ENOMEM; } if (mMetadata.metadata_size() != bufferTraits.metadata_size()) { - ALOGE("BufferHubBuffer::ImportGraphicBuffer: metadata buffer too small: " - "%zu, expected: %" PRIu64 ".", + ALOGE("%s: metadata buffer too small: %zu, expected: %" PRIu64 ".", __FUNCTION__, mMetadata.metadata_size(), bufferTraits.metadata_size()); return -ENOMEM; } size_t metadataSize = static_cast(bufferTraits.metadata_size()); if (metadataSize < BufferHubDefs::kMetadataHeaderSize) { - ALOGE("BufferHubBuffer::ImportGraphicBuffer: metadata too small: %zu", metadataSize); + ALOGE("%s: metadata too small: %zu", __FUNCTION__, metadataSize); return -EINVAL; } @@ -191,7 +189,7 @@ int BufferHubBuffer::ImportGraphicBuffer() { mClientStateMask = bufferTraits.client_state_mask(); // TODO(b/112012161) Set up shared fences. - ALOGD("BufferHubBuffer::ImportGraphicBuffer: id=%d, buffer_state=%" PRIx32 ".", id(), + ALOGD("%s: id=%d, buffer_state=%" PRIx32 ".", __FUNCTION__, id(), buffer_state_->load(std::memory_order_acquire)); return 0; } @@ -291,12 +289,12 @@ int BufferHubBuffer::Poll(int timeoutMs) { Status BufferHubBuffer::Duplicate() { ATRACE_CALL(); - ALOGD("BufferHubBuffer::Duplicate: id=%d.", mId); + ALOGD("%s: id=%d.", __FUNCTION__, mId); auto statusOrHandle = mClient.InvokeRemoteMethod(); if (!statusOrHandle.ok()) { - ALOGE("BufferHubBuffer::Duplicate: Failed to duplicate buffer (id=%d): %s.", mId, + ALOGE("%s: Failed to duplicate buffer (id=%d): %s.", __FUNCTION__, mId, statusOrHandle.GetErrorMessage().c_str()); } return statusOrHandle; -- cgit v1.2.3-59-g8ed1b From 2ceb320ea35a14ef8f6df460314f85a40a7581de Mon Sep 17 00:00:00 2001 From: Tianyu Jiang Date: Mon, 17 Dec 2018 12:58:34 -0800 Subject: Check atomics in shared memory are lock free when they are created in bufferhub server side in BufferNode, and client side in BufferHubBuffer. Fix: 117849512 Test: BufferHub_test BufferHubServer_test Change-Id: Ifc5b681a6a86fa02cb598b33bf68dfefc07a76f9 --- libs/ui/BufferHubBuffer.cpp | 7 +++++++ services/bufferhub/BufferNode.cpp | 8 ++++++++ 2 files changed, 15 insertions(+) (limited to 'libs/ui/BufferHubBuffer.cpp') diff --git a/libs/ui/BufferHubBuffer.cpp b/libs/ui/BufferHubBuffer.cpp index 226b6ee9ad..0582e1afe7 100644 --- a/libs/ui/BufferHubBuffer.cpp +++ b/libs/ui/BufferHubBuffer.cpp @@ -169,6 +169,13 @@ int BufferHubBuffer::ImportGraphicBuffer() { buffer_state_ = &metadata_header->buffer_state; fence_state_ = &metadata_header->fence_state; active_clients_bit_mask_ = &metadata_header->active_clients_bit_mask; + // The C++ standard recommends (but does not require) that lock-free atomic operations are + // also address-free, that is, suitable for communication between processes using shared + // memory. + LOG_ALWAYS_FATAL_IF(!std::atomic_is_lock_free(buffer_state_) || + !std::atomic_is_lock_free(fence_state_) || + !std::atomic_is_lock_free(active_clients_bit_mask_), + "Atomic variables in ashmen are not lock free."); // Import the buffer: We only need to hold on the native_handle_t here so that // GraphicBuffer instance can be created in future. diff --git a/services/bufferhub/BufferNode.cpp b/services/bufferhub/BufferNode.cpp index cc87e15917..da19a6fb1d 100644 --- a/services/bufferhub/BufferNode.cpp +++ b/services/bufferhub/BufferNode.cpp @@ -2,6 +2,7 @@ #include #include +#include #include namespace android { @@ -18,6 +19,13 @@ void BufferNode::InitializeMetadata() { fence_state_ = new (&metadata_header->fence_state) std::atomic(0); active_clients_bit_mask_ = new (&metadata_header->active_clients_bit_mask) std::atomic(0); + // The C++ standard recommends (but does not require) that lock-free atomic operations are + // also address-free, that is, suitable for communication between processes using shared + // memory. + LOG_ALWAYS_FATAL_IF(!std::atomic_is_lock_free(buffer_state_) || + !std::atomic_is_lock_free(fence_state_) || + !std::atomic_is_lock_free(active_clients_bit_mask_), + "Atomic variables in ashmen are not lock free."); } // Allocates a new BufferNode. -- cgit v1.2.3-59-g8ed1b From ec97b76db1eda00fae7041ddb9f46f01b830071b Mon Sep 17 00:00:00 2001 From: Tianyu Jiang Date: Mon, 7 Jan 2019 17:14:02 -0800 Subject: Post to all existing and non-existing clients of the buffer in BufferHubBuffer Fix: 122483970 Test: AHardwareBufferTest BufferHub_test BufferHubServer_test VtsHalBufferHubV1_0TargetTest Change-Id: Ia0d3119c34fb68573747948f38ecc3477c65fcd5 --- libs/ui/BufferHubBuffer.cpp | 8 ++--- libs/ui/tests/BufferHubBuffer_test.cpp | 56 +++++++++++++++++++++++++++++++--- 2 files changed, 54 insertions(+), 10 deletions(-) (limited to 'libs/ui/BufferHubBuffer.cpp') diff --git a/libs/ui/BufferHubBuffer.cpp b/libs/ui/BufferHubBuffer.cpp index 0582e1afe7..11849608ba 100644 --- a/libs/ui/BufferHubBuffer.cpp +++ b/libs/ui/BufferHubBuffer.cpp @@ -64,7 +64,6 @@ static constexpr char kBufferHubClientPath[] = "system/buffer_hub/client"; using BufferHubDefs::AnyClientAcquired; using BufferHubDefs::AnyClientGained; -using BufferHubDefs::AnyClientPosted; using BufferHubDefs::IsClientAcquired; using BufferHubDefs::IsClientGained; using BufferHubDefs::IsClientPosted; @@ -226,8 +225,7 @@ int BufferHubBuffer::Gain() { int BufferHubBuffer::Post() { uint32_t current_buffer_state = buffer_state_->load(std::memory_order_acquire); - uint32_t current_active_clients_bit_mask = 0U; - uint32_t updated_buffer_state = 0U; + uint32_t updated_buffer_state = (~mClientStateMask) & kHighBitsMask; do { if (!IsClientGained(current_buffer_state, mClientStateMask)) { ALOGE("%s: Cannot post a buffer that is not gained by this client. buffer_id=%d " @@ -236,9 +234,7 @@ int BufferHubBuffer::Post() { return -EBUSY; } // Set the producer client buffer state to released, other clients' buffer state to posted. - current_active_clients_bit_mask = active_clients_bit_mask_->load(std::memory_order_acquire); - updated_buffer_state = - current_active_clients_bit_mask & (~mClientStateMask) & kHighBitsMask; + // Post to all existing and non-existing clients. } while (!buffer_state_->compare_exchange_weak(current_buffer_state, updated_buffer_state, std::memory_order_acq_rel, std::memory_order_acquire)); diff --git a/libs/ui/tests/BufferHubBuffer_test.cpp b/libs/ui/tests/BufferHubBuffer_test.cpp index 1b339a06d9..69b9590d40 100644 --- a/libs/ui/tests/BufferHubBuffer_test.cpp +++ b/libs/ui/tests/BufferHubBuffer_test.cpp @@ -67,9 +67,9 @@ protected: } std::unique_ptr b1; - uint64_t b1ClientMask = 0U; + uint32_t b1ClientMask = 0U; std::unique_ptr b2; - uint64_t b2ClientMask = 0U; + uint32_t b2ClientMask = 0U; private: // Creates b1 and b2 as the clients of the same buffer for testing. @@ -125,7 +125,7 @@ TEST_F(BufferHubBufferTest, DuplicateBufferHubBuffer) { auto b1 = BufferHubBuffer::Create(kWidth, kHeight, kLayerCount, kFormat, kUsage, kUserMetadataSize); int id1 = b1->id(); - uint64_t bufferStateMask1 = b1->client_state_mask(); + uint32_t bufferStateMask1 = b1->client_state_mask(); EXPECT_NE(bufferStateMask1, 0U); EXPECT_TRUE(b1->IsValid()); EXPECT_EQ(b1->user_metadata_size(), kUserMetadataSize); @@ -148,7 +148,7 @@ TEST_F(BufferHubBufferTest, DuplicateBufferHubBuffer) { EXPECT_EQ(b2->user_metadata_size(), kUserMetadataSize); int id2 = b2->id(); - uint64_t bufferStateMask2 = b2->client_state_mask(); + uint32_t bufferStateMask2 = b2->client_state_mask(); EXPECT_NE(bufferStateMask2, 0U); // These two buffer instances are based on the same physical buffer under the @@ -340,5 +340,53 @@ TEST_F(BufferHubBufferStateTransitionTest, ReleaseBuffer_fromSelfInAcquiredState EXPECT_EQ(b2->Release(), 0); } +TEST_F(BufferHubBufferStateTransitionTest, BasicUsage) { + // 1 producer buffer and 1 consumer buffer initialised in testcase setup. + // Test if this set of basic operation succeed: + // Producer post three times to the consumer, and released by consumer. + for (int i = 0; i < 3; ++i) { + ASSERT_EQ(b1->Gain(), 0); + ASSERT_EQ(b1->Post(), 0); + ASSERT_EQ(b2->Acquire(), 0); + ASSERT_EQ(b2->Release(), 0); + } +} + +TEST_F(BufferHubBufferTest, createNewConsumerAfterGain) { + // Create a poducer buffer and gain. + std::unique_ptr b1 = + BufferHubBuffer::Create(kWidth, kHeight, kLayerCount, kFormat, kUsage, + kUserMetadataSize); + ASSERT_EQ(b1->Gain(), 0); + + // Create a consumer of the buffer and test if the consumer can acquire the + // buffer if producer posts. + auto statusOrHandle = b1->Duplicate(); + ASSERT_TRUE(statusOrHandle); + std::unique_ptr b2 = BufferHubBuffer::Import(std::move(statusOrHandle.take())); + ASSERT_NE(b1->client_state_mask(), b2->client_state_mask()); + + ASSERT_EQ(b1->Post(), 0); + EXPECT_EQ(b2->Acquire(), 0); +} + +TEST_F(BufferHubBufferTest, createNewConsumerAfterPost) { + // Create a poducer buffer and post. + std::unique_ptr b1 = + BufferHubBuffer::Create(kWidth, kHeight, kLayerCount, kFormat, kUsage, + kUserMetadataSize); + ASSERT_EQ(b1->Gain(), 0); + ASSERT_EQ(b1->Post(), 0); + + // Create a consumer of the buffer and test if the consumer can acquire the + // buffer if producer posts. + auto statusOrHandle = b1->Duplicate(); + ASSERT_TRUE(statusOrHandle); + std::unique_ptr b2 = BufferHubBuffer::Import(std::move(statusOrHandle.take())); + ASSERT_NE(b1->client_state_mask(), b2->client_state_mask()); + + EXPECT_EQ(b2->Acquire(), 0); +} + } // namespace } // namespace android -- cgit v1.2.3-59-g8ed1b From 021776e289849daf3b3c327d5b0a57ee55873225 Mon Sep 17 00:00:00 2001 From: Fan Xu Date: Wed, 5 Dec 2018 13:34:48 -0800 Subject: Refactor BufferHubBuffer to use hwbinder Entirely move BufferHubBuffer off pdx dependency and using hwbinder backend and rewrite test cases to fit in current behavior. Remove duplicated test cases in buffer_hub-test. Commented out BufferHubBuffer related parts and remove related include and using statement. Add hidl interface dependency to libs/gui build file to avoid compile errors. Test: BufferHub_test, GraphicBuffer_test, buffer_hub-test Bug: 116681016 Change-Id: I9d8f734f681e04d3d50b7a02c27b17df8c66cbad --- libs/gui/Android.bp | 2 + libs/ui/Android.bp | 6 +- libs/ui/BufferHubBuffer.cpp | 316 ++++++++++++++++++------------- libs/ui/include/ui/BufferHubBuffer.h | 108 ++++------- libs/ui/tests/Android.bp | 8 +- libs/ui/tests/BufferHubBuffer_test.cpp | 172 +++++++++++------ libs/ui/tests/GraphicBuffer_test.cpp | 2 +- libs/vr/libbufferhub/buffer_hub-test.cpp | 93 +-------- 8 files changed, 342 insertions(+), 365 deletions(-) (limited to 'libs/ui/BufferHubBuffer.cpp') diff --git a/libs/gui/Android.bp b/libs/gui/Android.bp index d1c732bc1f..3521e89ae6 100644 --- a/libs/gui/Android.bp +++ b/libs/gui/Android.bp @@ -121,6 +121,7 @@ cc_library_shared { ], shared_libs: [ + "android.frameworks.bufferhub@1.0", "android.hardware.graphics.common@1.1", "libbase", "libsync", @@ -153,6 +154,7 @@ cc_library_shared { "BufferHubProducer.cpp", ], exclude_shared_libs: [ + "android.frameworks.bufferhub@1.0", "libbufferhub", "libbufferhubqueue", "libpdx_default_transport", diff --git a/libs/ui/Android.bp b/libs/ui/Android.bp index 956465c52a..d089bf6bc3 100644 --- a/libs/ui/Android.bp +++ b/libs/ui/Android.bp @@ -77,6 +77,7 @@ cc_library_shared { ], shared_libs: [ + "android.frameworks.bufferhub@1.0", "android.hardware.graphics.allocator@2.0", "android.hardware.graphics.common@1.2", "android.hardware.graphics.mapper@2.0", @@ -93,7 +94,6 @@ cc_library_shared { "libutils", "libutilscallstack", "liblog", - "libpdx_default_transport", // TODO(b/112338294): Remove this once BufferHub moved to use Binder. ], export_shared_lib_headers: [ @@ -121,6 +121,7 @@ cc_library_shared { "libnativewindow_headers", ], exclude_shared_libs: [ + "android.frameworks.bufferhub@1.0", "libpdx_default_transport", ], }, @@ -148,9 +149,6 @@ cc_library_shared { "libhardware_headers", "libui_headers", ], - - // TODO(b/117568153): Temporarily opt out using libcrt. - no_libcrt: true, } cc_library_headers { diff --git a/libs/ui/BufferHubBuffer.cpp b/libs/ui/BufferHubBuffer.cpp index 11849608ba..c70f18823c 100644 --- a/libs/ui/BufferHubBuffer.cpp +++ b/libs/ui/BufferHubBuffer.cpp @@ -14,150 +14,190 @@ * limitations under the License. */ -// We would eliminate the clang warnings introduced by libdpx. -// TODO(b/112338294): Remove those once BufferHub moved to use Binder -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wconversion" -#pragma clang diagnostic ignored "-Wdouble-promotion" -#pragma clang diagnostic ignored "-Wgnu-case-range" -#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments" -#pragma clang diagnostic ignored "-Winconsistent-missing-destructor-override" -#pragma clang diagnostic ignored "-Wnested-anon-types" -#pragma clang diagnostic ignored "-Wpacked" -#pragma clang diagnostic ignored "-Wshadow" -#pragma clang diagnostic ignored "-Wsign-conversion" -#pragma clang diagnostic ignored "-Wswitch-enum" -#pragma clang diagnostic ignored "-Wundefined-func-template" -#pragma clang diagnostic ignored "-Wunused-template" -#pragma clang diagnostic ignored "-Wweak-vtables" -#include -#include -#include -#include -#pragma clang diagnostic pop - #include #include +#include +#include #include #include - -using android::base::unique_fd; -using android::dvr::BufferTraits; -using android::dvr::DetachedBufferRPC; -using android::dvr::NativeHandleWrapper; - -// TODO(b/112338294): Remove PDX dependencies from libui. -using android::pdx::LocalChannelHandle; -using android::pdx::LocalHandle; -using android::pdx::Status; -using android::pdx::default_transport::ClientChannel; -using android::pdx::default_transport::ClientChannelFactory; +#include + +using ::android::base::unique_fd; +using ::android::BufferHubDefs::AnyClientAcquired; +using ::android::BufferHubDefs::AnyClientGained; +using ::android::BufferHubDefs::IsClientAcquired; +using ::android::BufferHubDefs::IsClientGained; +using ::android::BufferHubDefs::IsClientPosted; +using ::android::BufferHubDefs::IsClientReleased; +using ::android::frameworks::bufferhub::V1_0::BufferHubStatus; +using ::android::frameworks::bufferhub::V1_0::BufferTraits; +using ::android::frameworks::bufferhub::V1_0::IBufferClient; +using ::android::frameworks::bufferhub::V1_0::IBufferHub; +using ::android::hardware::hidl_handle; +using ::android::hardware::graphics::common::V1_2::HardwareBufferDescription; namespace android { -namespace { - -// TODO(b/112338294): Remove this string literal after refactoring BufferHub -// to use Binder. -static constexpr char kBufferHubClientPath[] = "system/buffer_hub/client"; - -using BufferHubDefs::AnyClientAcquired; -using BufferHubDefs::AnyClientGained; -using BufferHubDefs::IsClientAcquired; -using BufferHubDefs::IsClientGained; -using BufferHubDefs::IsClientPosted; -using BufferHubDefs::IsClientReleased; -using BufferHubDefs::kHighBitsMask; +std::unique_ptr BufferHubBuffer::Create(uint32_t width, uint32_t height, + uint32_t layerCount, uint32_t format, + uint64_t usage, size_t userMetadataSize) { + auto buffer = std::unique_ptr( + new BufferHubBuffer(width, height, layerCount, format, usage, userMetadataSize)); + return buffer->IsValid() ? std::move(buffer) : nullptr; +} -} // namespace +std::unique_ptr BufferHubBuffer::Import(const native_handle_t* token) { + if (token == nullptr) { + ALOGE("%s: token cannot be nullptr!", __FUNCTION__); + return nullptr; + } -BufferHubClient::BufferHubClient() : Client(ClientChannelFactory::Create(kBufferHubClientPath)) {} + auto buffer = std::unique_ptr(new BufferHubBuffer(token)); + return buffer->IsValid() ? std::move(buffer) : nullptr; +} -BufferHubClient::BufferHubClient(LocalChannelHandle mChannelHandle) - : Client(ClientChannel::Create(std::move(mChannelHandle))) {} +BufferHubBuffer::BufferHubBuffer(uint32_t width, uint32_t height, uint32_t layerCount, + uint32_t format, uint64_t usage, size_t userMetadataSize) { + ATRACE_CALL(); + ALOGD("%s: width=%u height=%u layerCount=%u, format=%u " + "usage=%" PRIx64 " mUserMetadataSize=%zu", + __FUNCTION__, width, height, layerCount, format, usage, userMetadataSize); + + sp bufferhub = IBufferHub::getService(); + if (bufferhub.get() == nullptr) { + ALOGE("%s: BufferHub service not found!", __FUNCTION__); + return; + } -BufferHubClient::~BufferHubClient() {} + AHardwareBuffer_Desc aDesc = {width, height, layerCount, format, + usage, /*stride=*/0UL, /*rfu0=*/0UL, /*rfu1=*/0ULL}; + HardwareBufferDescription desc; + memcpy(&desc, &aDesc, sizeof(HardwareBufferDescription)); + + BufferHubStatus ret; + sp client; + BufferTraits bufferTraits; + IBufferHub::allocateBuffer_cb alloc_cb = [&](const auto& status, const auto& outClient, + const auto& traits) { + ret = status; + client = std::move(outClient); + bufferTraits = std::move(traits); + }; + + if (!bufferhub->allocateBuffer(desc, static_cast(userMetadataSize), alloc_cb) + .isOk()) { + ALOGE("%s: allocateBuffer transaction failed!", __FUNCTION__); + return; + } else if (ret != BufferHubStatus::NO_ERROR) { + ALOGE("%s: allocateBuffer failed with error %u.", __FUNCTION__, ret); + return; + } else if (client == nullptr) { + ALOGE("%s: allocateBuffer got null BufferClient.", __FUNCTION__); + return; + } -bool BufferHubClient::IsValid() const { - return IsConnected() && GetChannelHandle().valid(); + const int importRet = initWithBufferTraits(bufferTraits); + if (importRet < 0) { + ALOGE("%s: Failed to import buffer: %s", __FUNCTION__, strerror(-importRet)); + client->close(); + } + mBufferClient = std::move(client); } -LocalChannelHandle BufferHubClient::TakeChannelHandle() { - if (IsConnected()) { - return std::move(GetChannelHandle()); - } else { - return {}; +BufferHubBuffer::BufferHubBuffer(const native_handle_t* token) { + sp bufferhub = IBufferHub::getService(); + if (bufferhub.get() == nullptr) { + ALOGE("%s: BufferHub service not found!", __FUNCTION__); + return; } -} -BufferHubBuffer::BufferHubBuffer(uint32_t width, uint32_t height, uint32_t layerCount, - uint32_t format, uint64_t usage, size_t mUserMetadataSize) { - ATRACE_CALL(); - ALOGD("%s: width=%u height=%u layerCount=%u, format=%u usage=%" PRIx64 " mUserMetadataSize=%zu", - __FUNCTION__, width, height, layerCount, format, usage, mUserMetadataSize); - - auto status = - mClient.InvokeRemoteMethod(width, height, layerCount, format, - usage, mUserMetadataSize); - if (!status) { - ALOGE("%s: Failed to create detached buffer: %s", __FUNCTION__, - status.GetErrorMessage().c_str()); - mClient.Close(-status.error()); + BufferHubStatus ret; + sp client; + BufferTraits bufferTraits; + IBufferHub::importBuffer_cb import_cb = [&](const auto& status, const auto& outClient, + const auto& traits) { + ret = status; + client = std::move(outClient); + bufferTraits = std::move(traits); + }; + + // hidl_handle(native_handle_t*) simply creates a raw pointer reference withouth ownership + // transfer. + if (!bufferhub->importBuffer(hidl_handle(token), import_cb).isOk()) { + ALOGE("%s: importBuffer transaction failed!", __FUNCTION__); + return; + } else if (ret != BufferHubStatus::NO_ERROR) { + ALOGE("%s: importBuffer failed with error %u.", __FUNCTION__, ret); + return; + } else if (client == nullptr) { + ALOGE("%s: importBuffer got null BufferClient.", __FUNCTION__); + return; } - const int ret = ImportGraphicBuffer(); - if (ret < 0) { - ALOGE("%s: Failed to import buffer: %s", __FUNCTION__, strerror(-ret)); - mClient.Close(ret); + const int importRet = initWithBufferTraits(bufferTraits); + if (importRet < 0) { + ALOGE("%s: Failed to import buffer: %s", __FUNCTION__, strerror(-importRet)); + client->close(); } + mBufferClient = std::move(client); } -BufferHubBuffer::BufferHubBuffer(LocalChannelHandle mChannelHandle) - : mClient(std::move(mChannelHandle)) { - const int ret = ImportGraphicBuffer(); - if (ret < 0) { - ALOGE("%s: Failed to import buffer: %s", __FUNCTION__, strerror(-ret)); - mClient.Close(ret); +BufferHubBuffer::~BufferHubBuffer() { + // Close buffer client to avoid possible race condition: user could first duplicate and hold + // token with the original buffer gone, and then try to import the token. The close function + // will explicitly invalidate the token to avoid this. + if (mBufferClient != nullptr) { + if (!mBufferClient->close().isOk()) { + ALOGE("%s: close BufferClient transaction failed!", __FUNCTION__); + } } } -int BufferHubBuffer::ImportGraphicBuffer() { +int BufferHubBuffer::initWithBufferTraits(const BufferTraits& bufferTraits) { ATRACE_CALL(); - auto status = mClient.InvokeRemoteMethod(); - if (!status) { - ALOGE("%s: Failed to import GraphicBuffer: %s", __FUNCTION__, - status.GetErrorMessage().c_str()); - return -status.error(); + if (bufferTraits.bufferInfo.getNativeHandle() == nullptr) { + ALOGE("%s: missing buffer info handle.", __FUNCTION__); + return -EINVAL; + } + + if (bufferTraits.bufferHandle.getNativeHandle() == nullptr) { + ALOGE("%s: missing gralloc handle.", __FUNCTION__); + return -EINVAL; } - BufferTraits bufferTraits = status.take(); - if (bufferTraits.id() < 0) { - ALOGE("%s: Received an invalid id!", __FUNCTION__); - return -EIO; + int bufferId = bufferTraits.bufferInfo->data[1]; + if (bufferId < 0) { + ALOGE("%s: Received an invalid (negative) id!", __FUNCTION__); + return -EINVAL; } - // Stash the buffer id to replace the value in mId. - const int bufferId = bufferTraits.id(); + uint32_t clientBitMask; + memcpy(&clientBitMask, &bufferTraits.bufferInfo->data[2], sizeof(clientBitMask)); + if (clientBitMask == 0U) { + ALOGE("%s: Received a invalid client state mask!", __FUNCTION__); + return -EINVAL; + } - // Import the metadata. - LocalHandle metadataHandle = bufferTraits.take_metadata_handle(); - unique_fd metadataFd(metadataHandle.Release()); - mMetadata = BufferHubMetadata::Import(std::move(metadataFd)); + // Import the metadata. Dup since hidl_handle owns the fd + unique_fd ashmemFd(dup(bufferTraits.bufferInfo->data[0])); + mMetadata = BufferHubMetadata::Import(std::move(ashmemFd)); if (!mMetadata.IsValid()) { ALOGE("%s: invalid metadata.", __FUNCTION__); - return -ENOMEM; + return -EINVAL; } - if (mMetadata.metadata_size() != bufferTraits.metadata_size()) { - ALOGE("%s: metadata buffer too small: %zu, expected: %" PRIu64 ".", __FUNCTION__, - mMetadata.metadata_size(), bufferTraits.metadata_size()); - return -ENOMEM; + uint32_t userMetadataSize; + memcpy(&userMetadataSize, &bufferTraits.bufferInfo->data[3], 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()); + return -EINVAL; } - size_t metadataSize = static_cast(bufferTraits.metadata_size()); + size_t metadataSize = static_cast(mMetadata.metadata_size()); if (metadataSize < BufferHubDefs::kMetadataHeaderSize) { ALOGE("%s: metadata too small: %zu", __FUNCTION__, metadataSize); return -EINVAL; @@ -178,24 +218,14 @@ int BufferHubBuffer::ImportGraphicBuffer() { // Import the buffer: We only need to hold on the native_handle_t here so that // GraphicBuffer instance can be created in future. - mBufferHandle = bufferTraits.take_buffer_handle(); - - // Populate buffer desc based on buffer traits. - mBufferDesc.width = bufferTraits.width(); - mBufferDesc.height = bufferTraits.height(); - mBufferDesc.layers = bufferTraits.layer_count(); - mBufferDesc.format = bufferTraits.format(); - mBufferDesc.usage = bufferTraits.usage(); - mBufferDesc.stride = bufferTraits.stride(); - mBufferDesc.rfu0 = 0U; - mBufferDesc.rfu1 = 0U; - - // If all imports succeed, replace the previous buffer and id. + mBufferHandle = std::move(bufferTraits.bufferHandle); + memcpy(&mBufferDesc, &bufferTraits.bufferDesc, sizeof(AHardwareBuffer_Desc)); + mId = bufferId; - mClientStateMask = bufferTraits.client_state_mask(); + mClientStateMask = clientBitMask; // TODO(b/112012161) Set up shared fences. - ALOGD("%s: id=%d, buffer_state=%" PRIx32 ".", __FUNCTION__, id(), + ALOGD("%s: id=%d, buffer_state=%" PRIx32 ".", __FUNCTION__, mId, buffer_state_->load(std::memory_order_acquire)); return 0; } @@ -225,7 +255,7 @@ int BufferHubBuffer::Gain() { int BufferHubBuffer::Post() { uint32_t current_buffer_state = buffer_state_->load(std::memory_order_acquire); - uint32_t updated_buffer_state = (~mClientStateMask) & kHighBitsMask; + uint32_t updated_buffer_state = (~mClientStateMask) & BufferHubDefs::kHighBitsMask; do { if (!IsClientGained(current_buffer_state, mClientStateMask)) { ALOGE("%s: Cannot post a buffer that is not gained by this client. buffer_id=%d " @@ -283,24 +313,42 @@ int BufferHubBuffer::Release() { return 0; } -int BufferHubBuffer::Poll(int timeoutMs) { - ATRACE_CALL(); - - pollfd p = {mClient.event_fd(), POLLIN, 0}; - return poll(&p, 1, timeoutMs); +bool BufferHubBuffer::IsValid() const { + // TODO(b/68770788): check eventFd once implemented + return mBufferHandle.getNativeHandle() != nullptr && mId >= 0 && mClientStateMask != 0U && + mMetadata.IsValid() && mBufferClient != nullptr; } -Status BufferHubBuffer::Duplicate() { - ATRACE_CALL(); - ALOGD("%s: id=%d.", __FUNCTION__, mId); +// TODO(b/68770788): implement Poll() once we have event fd +int BufferHubBuffer::Poll(int /* timeoutMs */) { + return -ENOSYS; +} - auto statusOrHandle = mClient.InvokeRemoteMethod(); +native_handle_t* BufferHubBuffer::Duplicate() { + if (mBufferClient == nullptr) { + ALOGE("%s: missing BufferClient!", __FUNCTION__); + return nullptr; + } - if (!statusOrHandle.ok()) { - ALOGE("%s: Failed to duplicate buffer (id=%d): %s.", __FUNCTION__, mId, - statusOrHandle.GetErrorMessage().c_str()); + hidl_handle token; + BufferHubStatus ret; + IBufferClient::duplicate_cb dup_cb = [&](const auto& outToken, const auto& status) { + token = std::move(outToken); + ret = status; + }; + + if (!mBufferClient->duplicate(dup_cb).isOk()) { + ALOGE("%s: duplicate transaction failed!", __FUNCTION__); + return nullptr; + } else if (ret != BufferHubStatus::NO_ERROR) { + ALOGE("%s: duplicate failed with error %u.", __FUNCTION__, ret); + return nullptr; + } else if (token.getNativeHandle() == nullptr) { + ALOGE("%s: duplicate got null token.", __FUNCTION__); + return nullptr; } - return statusOrHandle; + + return native_handle_clone(token.getNativeHandle()); } } // namespace android diff --git a/libs/ui/include/ui/BufferHubBuffer.h b/libs/ui/include/ui/BufferHubBuffer.h index 90dd391d8d..44dfa95e67 100644 --- a/libs/ui/include/ui/BufferHubBuffer.h +++ b/libs/ui/include/ui/BufferHubBuffer.h @@ -17,73 +17,43 @@ #ifndef ANDROID_BUFFER_HUB_BUFFER_H_ #define ANDROID_BUFFER_HUB_BUFFER_H_ -// We would eliminate the clang warnings introduced by libdpx. -// TODO(b/112338294): Remove those once BufferHub moved to use Binder -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wconversion" -#pragma clang diagnostic ignored "-Wdouble-promotion" -#pragma clang diagnostic ignored "-Wgnu-case-range" -#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments" -#pragma clang diagnostic ignored "-Winconsistent-missing-destructor-override" -#pragma clang diagnostic ignored "-Wnested-anon-types" -#pragma clang diagnostic ignored "-Wpacked" -#pragma clang diagnostic ignored "-Wshadow" -#pragma clang diagnostic ignored "-Wsign-conversion" -#pragma clang diagnostic ignored "-Wswitch-enum" -#pragma clang diagnostic ignored "-Wundefined-func-template" -#pragma clang diagnostic ignored "-Wunused-template" -#pragma clang diagnostic ignored "-Wweak-vtables" -#include -#include -#pragma clang diagnostic pop - +#include #include +#include #include #include namespace android { -class BufferHubClient : public pdx::Client { -public: - BufferHubClient(); - virtual ~BufferHubClient(); - explicit BufferHubClient(pdx::LocalChannelHandle mChannelHandle); - - bool IsValid() const; - pdx::LocalChannelHandle TakeChannelHandle(); - - using pdx::Client::Close; - using pdx::Client::event_fd; - using pdx::Client::GetChannel; - using pdx::Client::InvokeRemoteMethod; -}; - class BufferHubBuffer { public: - // Allocates a standalone BufferHubBuffer not associated with any producer consumer set. + // Allocates a standalone BufferHubBuffer. static std::unique_ptr Create(uint32_t width, uint32_t height, uint32_t layerCount, uint32_t format, - uint64_t usage, size_t userMetadataSize) { - return std::unique_ptr( - new BufferHubBuffer(width, height, layerCount, format, usage, userMetadataSize)); - } + uint64_t usage, size_t userMetadataSize); - // Imports the given channel handle to a BufferHubBuffer, taking ownership. - static std::unique_ptr Import(pdx::LocalChannelHandle mChannelHandle) { - return std::unique_ptr(new BufferHubBuffer(std::move(mChannelHandle))); - } + // Imports the given token to a BufferHubBuffer. Not taking ownership of the token. Caller + // should close and destroy the token after calling this function regardless of output. + // TODO(b/122543147): use a movable wrapper for token + static std::unique_ptr Import(const native_handle_t* token); BufferHubBuffer(const BufferHubBuffer&) = delete; void operator=(const BufferHubBuffer&) = delete; + virtual ~BufferHubBuffer(); + // Gets ID of the buffer client. All BufferHubBuffer clients derived from the same buffer in - // bufferhubd share the same buffer id. + // BufferHub share the same buffer id. int id() const { return mId; } - // Returns the buffer description, which is guaranteed to be faithful values from bufferhubd. + // Returns the buffer description, which is guaranteed to be faithful values from BufferHub. const AHardwareBuffer_Desc& desc() const { return mBufferDesc; } - const native_handle_t* DuplicateHandle() { return mBufferHandle.DuplicateHandle(); } + // Duplicate the underlying Gralloc buffer handle. Caller is responsible to free the handle + // after use. + native_handle_t* DuplicateHandle() { + return native_handle_clone(mBufferHandle.getNativeHandle()); + } // Returns the current value of MetadataHeader::buffer_state. uint32_t buffer_state() { @@ -96,12 +66,12 @@ public: size_t user_metadata_size() const { return mMetadata.user_metadata_size(); } - // Returns true if the buffer holds an open PDX channels towards bufferhubd. - bool IsConnected() const { return mClient.IsValid(); } + // Returns true if the BufferClient is still alive. + bool IsConnected() const { return mBufferClient->ping().isOk(); } - // Returns true if the buffer holds an valid native buffer handle that's availble for the client - // to read from and/or write into. - bool IsValid() const { return mBufferHandle.IsValid(); } + // Returns true if the buffer is valid: non-null buffer handle, valid id, valid client bit mask, + // valid metadata and valid buffer client + bool IsValid() const; // Gains the buffer for exclusive write permission. Read permission is implied once a buffer is // gained. @@ -124,33 +94,27 @@ public: // current cycle of the usage of the buffer. int Release(); - // Returns the event mask for all the events that are pending on this buffer (see sys/poll.h for - // all possible bits). - pdx::Status GetEventMask(int events) { - if (auto* channel = mClient.GetChannel()) { - return channel->GetEventMask(events); - } else { - return pdx::ErrorStatus(EINVAL); - } - } - // Polls the fd for |timeoutMs| milliseconds (-1 for infinity). int Poll(int timeoutMs); - // Creates a BufferHubBuffer client from an existing one. The new client will - // share the same underlying gralloc buffer and ashmem region for metadata. - pdx::Status Duplicate(); + // Creates a token that stands for this BufferHubBuffer client and could be used for Import to + // create another BufferHubBuffer. The new BufferHubBuffer will share the same underlying + // gralloc buffer and ashmem region for metadata. Note that the caller owns the token and + // should free it after use. + // Returns a valid token on success, nullptr on failure. + // TODO(b/122543147): use a movable wrapper for token + native_handle_t* Duplicate(); private: BufferHubBuffer(uint32_t width, uint32_t height, uint32_t layerCount, uint32_t format, uint64_t usage, size_t userMetadataSize); - BufferHubBuffer(pdx::LocalChannelHandle mChannelHandle); + BufferHubBuffer(const native_handle_t* token); - int ImportGraphicBuffer(); + int initWithBufferTraits(const frameworks::bufferhub::V1_0::BufferTraits& bufferTraits); // Global id for the buffer that is consistent across processes. - int mId = -1; + int mId = 0; // Client state mask of this BufferHubBuffer object. It is unique amoung all // clients/users of the buffer. @@ -159,8 +123,8 @@ private: // Stores ground truth of the buffer. AHardwareBuffer_Desc mBufferDesc; - // Wrapps the gralloc buffer handle of this buffer. - dvr::NativeHandleWrapper mBufferHandle; + // Wraps the gralloc buffer handle of this buffer. + hardware::hidl_handle mBufferHandle; // An ashmem-based metadata object. The same shared memory are mapped to the // bufferhubd daemon and all buffer clients. @@ -170,8 +134,8 @@ private: std::atomic* fence_state_ = nullptr; std::atomic* active_clients_bit_mask_ = nullptr; - // PDX backend. - BufferHubClient mClient; + // HwBinder backend + sp mBufferClient; }; } // namespace android diff --git a/libs/ui/tests/Android.bp b/libs/ui/tests/Android.bp index a670b3ce63..665469e015 100644 --- a/libs/ui/tests/Android.bp +++ b/libs/ui/tests/Android.bp @@ -31,12 +31,14 @@ cc_test { cc_test { name: "GraphicBuffer_test", header_libs: [ - "libbufferhub_headers", "libdvr_headers", "libnativewindow_headers", ], shared_libs: [ - "libpdx_default_transport", + "android.frameworks.bufferhub@1.0", + "libcutils", + "libhidlbase", + "libhwbinder", "libui", "libutils", ], @@ -47,7 +49,6 @@ cc_test { cc_test { name: "BufferHub_test", header_libs: [ - "libbufferhub_headers", "libdvr_headers", "libnativewindow_headers", ], @@ -60,7 +61,6 @@ cc_test { "libhidlbase", "libhwbinder", "liblog", - "libpdx_default_transport", "libui", "libutils" ], diff --git a/libs/ui/tests/BufferHubBuffer_test.cpp b/libs/ui/tests/BufferHubBuffer_test.cpp index 69b9590d40..cd744cd106 100644 --- a/libs/ui/tests/BufferHubBuffer_test.cpp +++ b/libs/ui/tests/BufferHubBuffer_test.cpp @@ -16,10 +16,9 @@ #define LOG_TAG "BufferHubBufferTest" -#include -#include #include #include +#include #include #include #include @@ -29,36 +28,38 @@ namespace android { namespace { +using ::android::BufferHubDefs::AnyClientAcquired; +using ::android::BufferHubDefs::AnyClientGained; +using ::android::BufferHubDefs::AnyClientPosted; +using ::android::BufferHubDefs::IsBufferReleased; +using ::android::BufferHubDefs::IsClientAcquired; +using ::android::BufferHubDefs::IsClientGained; +using ::android::BufferHubDefs::IsClientPosted; +using ::android::BufferHubDefs::IsClientReleased; +using ::android::BufferHubDefs::kMetadataHeaderSize; +using ::testing::IsNull; +using ::testing::NotNull; + const int kWidth = 640; const int kHeight = 480; const int kLayerCount = 1; const int kFormat = HAL_PIXEL_FORMAT_RGBA_8888; const int kUsage = 0; -const size_t kUserMetadataSize = 0; - -using BufferHubDefs::AnyClientAcquired; -using BufferHubDefs::AnyClientGained; -using BufferHubDefs::AnyClientPosted; -using BufferHubDefs::IsBufferReleased; -using BufferHubDefs::IsClientAcquired; -using BufferHubDefs::IsClientGained; -using BufferHubDefs::IsClientPosted; -using BufferHubDefs::IsClientReleased; -using BufferHubDefs::kFirstClientBitMask; -using BufferHubDefs::kMetadataHeaderSize; -using frameworks::bufferhub::V1_0::BufferHubStatus; -using frameworks::bufferhub::V1_0::IBufferClient; -using frameworks::bufferhub::V1_0::IBufferHub; -using hardware::hidl_handle; -using hardware::graphics::common::V1_2::HardwareBufferDescription; -using hidl::base::V1_0::IBase; -using pdx::LocalChannelHandle; +const AHardwareBuffer_Desc kDesc = {kWidth, kHeight, kLayerCount, kFormat, + kUsage, /*stride=*/0UL, /*rfu0=*/0UL, /*rfu1=*/0ULL}; +const size_t kUserMetadataSize = 1; class BufferHubBufferTest : public ::testing::Test { protected: void SetUp() override { android::hardware::ProcessState::self()->startThreadPool(); } }; +bool cmpAHardwareBufferDesc(const AHardwareBuffer_Desc& desc, const AHardwareBuffer_Desc& other) { + // Not comparing stride because it's unknown before allocation + return desc.format == other.format && desc.height == other.height && + desc.layers == other.layers && desc.usage == other.usage && desc.width == other.width; +} + class BufferHubBufferStateTransitionTest : public BufferHubBufferTest { protected: void SetUp() override { @@ -78,91 +79,126 @@ private: void BufferHubBufferStateTransitionTest::CreateTwoClientsOfABuffer() { b1 = BufferHubBuffer::Create(kWidth, kHeight, kLayerCount, kFormat, kUsage, kUserMetadataSize); + ASSERT_THAT(b1, NotNull()); b1ClientMask = b1->client_state_mask(); ASSERT_NE(b1ClientMask, 0U); - auto statusOrHandle = b1->Duplicate(); - ASSERT_TRUE(statusOrHandle); - LocalChannelHandle h2 = statusOrHandle.take(); - b2 = BufferHubBuffer::Import(std::move(h2)); + + native_handle_t* token = b1->Duplicate(); + ASSERT_THAT(token, NotNull()); + + // TODO(b/122543147): use a movalbe wrapper for token + b2 = BufferHubBuffer::Import(token); + native_handle_close(token); + native_handle_delete(token); + ASSERT_THAT(b2, NotNull()); + b2ClientMask = b2->client_state_mask(); ASSERT_NE(b2ClientMask, 0U); ASSERT_NE(b2ClientMask, b1ClientMask); } -TEST_F(BufferHubBufferTest, CreateBufferHubBufferFails) { +TEST_F(BufferHubBufferTest, CreateBufferFails) { // Buffer Creation will fail: BLOB format requires height to be 1. auto b1 = BufferHubBuffer::Create(kWidth, /*height=*/2, kLayerCount, /*format=*/HAL_PIXEL_FORMAT_BLOB, kUsage, kUserMetadataSize); - EXPECT_FALSE(b1->IsConnected()); - EXPECT_FALSE(b1->IsValid()); + EXPECT_THAT(b1, IsNull()); // Buffer Creation will fail: user metadata size too large. auto b2 = BufferHubBuffer::Create(kWidth, kHeight, kLayerCount, kFormat, kUsage, /*userMetadataSize=*/std::numeric_limits::max()); - EXPECT_FALSE(b2->IsConnected()); - EXPECT_FALSE(b2->IsValid()); + EXPECT_THAT(b2, IsNull()); // Buffer Creation will fail: user metadata size too large. const size_t userMetadataSize = std::numeric_limits::max() - kMetadataHeaderSize; auto b3 = BufferHubBuffer::Create(kWidth, kHeight, kLayerCount, kFormat, kUsage, userMetadataSize); - EXPECT_FALSE(b3->IsConnected()); - EXPECT_FALSE(b3->IsValid()); + EXPECT_THAT(b3, IsNull()); } -TEST_F(BufferHubBufferTest, CreateBufferHubBuffer) { +TEST_F(BufferHubBufferTest, CreateBuffer) { auto b1 = BufferHubBuffer::Create(kWidth, kHeight, kLayerCount, kFormat, kUsage, kUserMetadataSize); + ASSERT_THAT(b1, NotNull()); EXPECT_TRUE(b1->IsConnected()); EXPECT_TRUE(b1->IsValid()); - EXPECT_NE(b1->id(), 0); + EXPECT_TRUE(cmpAHardwareBufferDesc(b1->desc(), kDesc)); + EXPECT_EQ(b1->user_metadata_size(), kUserMetadataSize); } -TEST_F(BufferHubBufferTest, DuplicateBufferHubBuffer) { +TEST_F(BufferHubBufferTest, DuplicateAndImportBuffer) { auto b1 = BufferHubBuffer::Create(kWidth, kHeight, kLayerCount, kFormat, kUsage, kUserMetadataSize); - int id1 = b1->id(); - uint32_t bufferStateMask1 = b1->client_state_mask(); - EXPECT_NE(bufferStateMask1, 0U); + ASSERT_THAT(b1, NotNull()); EXPECT_TRUE(b1->IsValid()); - EXPECT_EQ(b1->user_metadata_size(), kUserMetadataSize); - auto statusOrHandle = b1->Duplicate(); - EXPECT_TRUE(statusOrHandle); + native_handle_t* token = b1->Duplicate(); + EXPECT_TRUE(token); // The detached buffer should still be valid. EXPECT_TRUE(b1->IsConnected()); EXPECT_TRUE(b1->IsValid()); - // Gets the channel handle for the duplicated buffer. - LocalChannelHandle h2 = statusOrHandle.take(); - EXPECT_TRUE(h2.valid()); + std::unique_ptr b2 = BufferHubBuffer::Import(token); + native_handle_close(token); + native_handle_delete(token); - std::unique_ptr b2 = BufferHubBuffer::Import(std::move(h2)); - EXPECT_FALSE(h2.valid()); - ASSERT_TRUE(b2 != nullptr); + ASSERT_THAT(b2, NotNull()); EXPECT_TRUE(b2->IsValid()); - EXPECT_EQ(b2->user_metadata_size(), kUserMetadataSize); - int id2 = b2->id(); - uint32_t bufferStateMask2 = b2->client_state_mask(); - EXPECT_NE(bufferStateMask2, 0U); + EXPECT_TRUE(cmpAHardwareBufferDesc(b1->desc(), b2->desc())); + EXPECT_EQ(b1->user_metadata_size(), b2->user_metadata_size()); // These two buffer instances are based on the same physical buffer under the // hood, so they should share the same id. - EXPECT_EQ(id1, id2); + EXPECT_EQ(b1->id(), b2->id()); // We use client_state_mask() to tell those two instances apart. - EXPECT_NE(bufferStateMask1, bufferStateMask2); + EXPECT_NE(b1->client_state_mask(), b2->client_state_mask()); // Both buffer instances should be in released state currently. EXPECT_TRUE(IsBufferReleased(b1->buffer_state())); EXPECT_TRUE(IsBufferReleased(b2->buffer_state())); +} + +TEST_F(BufferHubBufferTest, ImportFreedBuffer) { + auto b1 = BufferHubBuffer::Create(kWidth, kHeight, kLayerCount, kFormat, kUsage, + kUserMetadataSize); + ASSERT_THAT(b1, NotNull()); + EXPECT_TRUE(b1->IsValid()); + + native_handle_t* token = b1->Duplicate(); + EXPECT_TRUE(token); - // TODO(b/112338294): rewrite test after migration - return; + // Explicitly destroy b1. Backend buffer should be freed and token becomes invalid + b1.reset(); + + // TODO(b/122543147): use a movalbe wrapper for token + std::unique_ptr b2 = BufferHubBuffer::Import(token); + native_handle_close(token); + native_handle_delete(token); + + // Import should fail with INVALID_TOKEN + EXPECT_THAT(b2, IsNull()); +} + +// nullptr must not crash the service +TEST_F(BufferHubBufferTest, ImportNullToken) { + auto b1 = BufferHubBuffer::Import(nullptr); + EXPECT_THAT(b1, IsNull()); +} + +// TODO(b/118180214): remove the comment after ag/5856474 landed +// This test has a very little chance to fail (number of existing tokens / 2 ^ 32) +TEST_F(BufferHubBufferTest, ImportInvalidToken) { + native_handle_t* token = native_handle_create(/*numFds=*/0, /*numInts=*/1); + token->data[0] = 0; + + auto b1 = BufferHubBuffer::Import(token); + native_handle_delete(token); + + EXPECT_THAT(b1, IsNull()); } TEST_F(BufferHubBufferStateTransitionTest, GainBuffer_fromReleasedState) { @@ -357,13 +393,20 @@ TEST_F(BufferHubBufferTest, createNewConsumerAfterGain) { std::unique_ptr b1 = BufferHubBuffer::Create(kWidth, kHeight, kLayerCount, kFormat, kUsage, kUserMetadataSize); + ASSERT_THAT(b1, NotNull()); ASSERT_EQ(b1->Gain(), 0); // Create a consumer of the buffer and test if the consumer can acquire the // buffer if producer posts. - auto statusOrHandle = b1->Duplicate(); - ASSERT_TRUE(statusOrHandle); - std::unique_ptr b2 = BufferHubBuffer::Import(std::move(statusOrHandle.take())); + // TODO(b/122543147): use a movalbe wrapper for token + native_handle_t* token = b1->Duplicate(); + ASSERT_TRUE(token); + + std::unique_ptr b2 = BufferHubBuffer::Import(token); + native_handle_close(token); + native_handle_delete(token); + + ASSERT_THAT(b2, NotNull()); ASSERT_NE(b1->client_state_mask(), b2->client_state_mask()); ASSERT_EQ(b1->Post(), 0); @@ -380,13 +423,20 @@ TEST_F(BufferHubBufferTest, createNewConsumerAfterPost) { // Create a consumer of the buffer and test if the consumer can acquire the // buffer if producer posts. - auto statusOrHandle = b1->Duplicate(); - ASSERT_TRUE(statusOrHandle); - std::unique_ptr b2 = BufferHubBuffer::Import(std::move(statusOrHandle.take())); + // TODO(b/122543147): use a movalbe wrapper for token + native_handle_t* token = b1->Duplicate(); + ASSERT_TRUE(token); + + std::unique_ptr b2 = BufferHubBuffer::Import(token); + native_handle_close(token); + native_handle_delete(token); + + ASSERT_THAT(b2, NotNull()); ASSERT_NE(b1->client_state_mask(), b2->client_state_mask()); EXPECT_EQ(b2->Acquire(), 0); } } // namespace + } // namespace android diff --git a/libs/ui/tests/GraphicBuffer_test.cpp b/libs/ui/tests/GraphicBuffer_test.cpp index 81ab3acfe4..5b46454c60 100644 --- a/libs/ui/tests/GraphicBuffer_test.cpp +++ b/libs/ui/tests/GraphicBuffer_test.cpp @@ -39,7 +39,7 @@ TEST_F(GraphicBufferTest, CreateFromBufferHubBuffer) { std::unique_ptr b1 = BufferHubBuffer::Create(kTestWidth, kTestHeight, kTestLayerCount, kTestFormat, kTestUsage, /*userMetadataSize=*/0); - EXPECT_NE(b1, nullptr); + ASSERT_NE(b1, nullptr); EXPECT_TRUE(b1->IsValid()); sp gb(new GraphicBuffer(std::move(b1))); diff --git a/libs/vr/libbufferhub/buffer_hub-test.cpp b/libs/vr/libbufferhub/buffer_hub-test.cpp index 487a604890..ed5a992f3c 100644 --- a/libs/vr/libbufferhub/buffer_hub-test.cpp +++ b/libs/vr/libbufferhub/buffer_hub-test.cpp @@ -5,7 +5,6 @@ #include #include #include -#include #include #include @@ -20,9 +19,6 @@ return result; \ })() -using android::BufferHubBuffer; -using android::GraphicBuffer; -using android::sp; using android::BufferHubDefs::AnyClientAcquired; using android::BufferHubDefs::AnyClientGained; using android::BufferHubDefs::AnyClientPosted; @@ -31,19 +27,15 @@ using android::BufferHubDefs::IsClientAcquired; using android::BufferHubDefs::IsClientPosted; using android::BufferHubDefs::IsClientReleased; using android::BufferHubDefs::kFirstClientBitMask; -using android::BufferHubDefs::kMetadataHeaderSize; using android::dvr::ConsumerBuffer; using android::dvr::ProducerBuffer; -using android::pdx::LocalChannelHandle; using android::pdx::LocalHandle; using android::pdx::Status; const int kWidth = 640; const int kHeight = 480; -const int kLayerCount = 1; const int kFormat = HAL_PIXEL_FORMAT_RGBA_8888; const int kUsage = 0; -const size_t kUserMetadataSize = 0; // Maximum number of consumers for the buffer that only has one producer in the // test. const size_t kMaxConsumerCount = @@ -808,7 +800,7 @@ TEST_F(LibBufferHubTest, TestDetachBufferFromProducer) { // TODO(b/112338294) rewrite test after migration return; - std::unique_ptr p = ProducerBuffer::Create( + /* std::unique_ptr p = ProducerBuffer::Create( kWidth, kHeight, kFormat, kUsage, sizeof(uint64_t)); std::unique_ptr c = ConsumerBuffer::Import(p->CreateConsumer()); @@ -876,49 +868,14 @@ TEST_F(LibBufferHubTest, TestDetachBufferFromProducer) { EXPECT_TRUE(d->IsConnected()); EXPECT_TRUE(d->IsValid()); - EXPECT_EQ(d->id(), p_id); -} - -TEST_F(LibBufferHubTest, TestCreateBufferHubBufferFails) { - // Buffer Creation will fail: BLOB format requires height to be 1. - auto b1 = BufferHubBuffer::Create(kWidth, /*height=2*/ 2, kLayerCount, - /*format=*/HAL_PIXEL_FORMAT_BLOB, kUsage, - kUserMetadataSize); - - EXPECT_FALSE(b1->IsConnected()); - EXPECT_FALSE(b1->IsValid()); - - // Buffer Creation will fail: user metadata size too large. - auto b2 = BufferHubBuffer::Create( - kWidth, kHeight, kLayerCount, kFormat, kUsage, - /*user_metadata_size=*/std::numeric_limits::max()); - - EXPECT_FALSE(b2->IsConnected()); - EXPECT_FALSE(b2->IsValid()); - - // Buffer Creation will fail: user metadata size too large. - auto b3 = BufferHubBuffer::Create( - kWidth, kHeight, kLayerCount, kFormat, kUsage, - /*user_metadata_size=*/std::numeric_limits::max() - - kMetadataHeaderSize); - - EXPECT_FALSE(b3->IsConnected()); - EXPECT_FALSE(b3->IsValid()); -} - -TEST_F(LibBufferHubTest, TestCreateBufferHubBuffer) { - auto b1 = BufferHubBuffer::Create(kWidth, kHeight, kLayerCount, kFormat, - kUsage, kUserMetadataSize); - EXPECT_TRUE(b1->IsConnected()); - EXPECT_TRUE(b1->IsValid()); - EXPECT_NE(b1->id(), 0); + EXPECT_EQ(d->id(), p_id); */ } TEST_F(LibBufferHubTest, TestDetach) { // TODO(b/112338294) rewrite test after migration return; - std::unique_ptr p1 = ProducerBuffer::Create( + /* std::unique_ptr p1 = ProducerBuffer::Create( kWidth, kHeight, kFormat, kUsage, sizeof(uint64_t)); ASSERT_TRUE(p1.get() != nullptr); int p1_id = p1->id(); @@ -936,47 +893,5 @@ TEST_F(LibBufferHubTest, TestDetach) { EXPECT_FALSE(h1.valid()); EXPECT_TRUE(b1->IsValid()); int b1_id = b1->id(); - EXPECT_EQ(b1_id, p1_id); -} - -TEST_F(LibBufferHubTest, TestDuplicateBufferHubBuffer) { - auto b1 = BufferHubBuffer::Create(kWidth, kHeight, kLayerCount, kFormat, - kUsage, kUserMetadataSize); - int b1_id = b1->id(); - EXPECT_TRUE(b1->IsValid()); - EXPECT_EQ(b1->user_metadata_size(), kUserMetadataSize); - EXPECT_NE(b1->client_state_mask(), 0U); - - auto status_or_handle = b1->Duplicate(); - EXPECT_TRUE(status_or_handle); - - // The detached buffer should still be valid. - EXPECT_TRUE(b1->IsConnected()); - EXPECT_TRUE(b1->IsValid()); - - // Gets the channel handle for the duplicated buffer. - LocalChannelHandle h2 = status_or_handle.take(); - EXPECT_TRUE(h2.valid()); - - std::unique_ptr b2 = BufferHubBuffer::Import(std::move(h2)); - EXPECT_FALSE(h2.valid()); - ASSERT_TRUE(b2 != nullptr); - EXPECT_TRUE(b2->IsValid()); - EXPECT_EQ(b2->user_metadata_size(), kUserMetadataSize); - EXPECT_NE(b2->client_state_mask(), 0U); - - int b2_id = b2->id(); - - // These two buffer instances are based on the same physical buffer under the - // hood, so they should share the same id. - EXPECT_EQ(b1_id, b2_id); - // We use client_state_mask() to tell those two instances apart. - EXPECT_NE(b1->client_state_mask(), b2->client_state_mask()); - - // Both buffer instances should be in gained state. - EXPECT_TRUE(IsBufferReleased(b1->buffer_state())); - EXPECT_TRUE(IsBufferReleased(b2->buffer_state())); - - // TODO(b/112338294) rewrite test after migration - return; + EXPECT_EQ(b1_id, p1_id); */ } -- cgit v1.2.3-59-g8ed1b From 876a049fd49f26dbb1607a285ed740ab8ffc8767 Mon Sep 17 00:00:00 2001 From: Nick Kralevich Date: Mon, 14 Jan 2019 11:51:50 -0800 Subject: BufferHubBuffer.cpp: replace dup() with fcntl(F_DUPFD_CLOEXEC) Replace calls to dup() with fcntl(F_DUPFD_CLOEXEC). The only difference between the two is that O_CLOEXEC is set on the newly duped file descriptor. This helps address file descriptor leaks crossing an exec() boundary. Test: compiles and boots Bug: 120983106 Change-Id: I2f705e0666c5276ad5239da283d5ee91561e8579 --- libs/ui/BufferHubBuffer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'libs/ui/BufferHubBuffer.cpp') diff --git a/libs/ui/BufferHubBuffer.cpp b/libs/ui/BufferHubBuffer.cpp index c70f18823c..052e650bec 100644 --- a/libs/ui/BufferHubBuffer.cpp +++ b/libs/ui/BufferHubBuffer.cpp @@ -181,7 +181,7 @@ int BufferHubBuffer::initWithBufferTraits(const BufferTraits& bufferTraits) { } // Import the metadata. Dup since hidl_handle owns the fd - unique_fd ashmemFd(dup(bufferTraits.bufferInfo->data[0])); + unique_fd ashmemFd(fcntl(bufferTraits.bufferInfo->data[0], F_DUPFD_CLOEXEC, 0)); mMetadata = BufferHubMetadata::Import(std::move(ashmemFd)); if (!mMetadata.IsValid()) { -- cgit v1.2.3-59-g8ed1b From f6b3dc2a356c3147a8b6525716439c2c773b13a0 Mon Sep 17 00:00:00 2001 From: Fan Xu Date: Mon, 14 Jan 2019 15:27:40 -0800 Subject: Remove BufferHubBuffer:Poll function In the new eventFd design, BufferHubBuffer will only provide with the eventfd itself, and user can decide what to do with it. Therefore, the Poll function is not needed any more. Test: BufferHub_test, GraphicBuffer_test Bug: 118891412 Change-Id: I5f5ff71bcedc20de81f246764e878d81b0768eb1 --- libs/ui/BufferHubBuffer.cpp | 5 ----- libs/ui/include/ui/BufferHubBuffer.h | 3 --- 2 files changed, 8 deletions(-) (limited to 'libs/ui/BufferHubBuffer.cpp') diff --git a/libs/ui/BufferHubBuffer.cpp b/libs/ui/BufferHubBuffer.cpp index c70f18823c..87314ecf76 100644 --- a/libs/ui/BufferHubBuffer.cpp +++ b/libs/ui/BufferHubBuffer.cpp @@ -319,11 +319,6 @@ bool BufferHubBuffer::IsValid() const { mMetadata.IsValid() && mBufferClient != nullptr; } -// TODO(b/68770788): implement Poll() once we have event fd -int BufferHubBuffer::Poll(int /* timeoutMs */) { - return -ENOSYS; -} - native_handle_t* BufferHubBuffer::Duplicate() { if (mBufferClient == nullptr) { ALOGE("%s: missing BufferClient!", __FUNCTION__); diff --git a/libs/ui/include/ui/BufferHubBuffer.h b/libs/ui/include/ui/BufferHubBuffer.h index 44dfa95e67..c6a4a232d1 100644 --- a/libs/ui/include/ui/BufferHubBuffer.h +++ b/libs/ui/include/ui/BufferHubBuffer.h @@ -94,9 +94,6 @@ public: // current cycle of the usage of the buffer. int Release(); - // Polls the fd for |timeoutMs| milliseconds (-1 for infinity). - int Poll(int timeoutMs); - // Creates a token that stands for this BufferHubBuffer client and could be used for Import to // create another BufferHubBuffer. The new BufferHubBuffer will share the same underlying // gralloc buffer and ashmem region for metadata. Note that the caller owns the token and -- cgit v1.2.3-59-g8ed1b From 5cf47bcf8dd51d3cc5a3733516acffa5e69d7fb1 Mon Sep 17 00:00:00 2001 From: Fan Xu Date: Tue, 15 Jan 2019 15:02:15 -0800 Subject: Add BufferHubEventFd to bufferhub system Now BufferNode will create a new BufferHubEventFd instance during allocation, BufferHubService will pass that fd to client side, and BufferHubBuffer will now extract and import it. User can now access the eventFd by a public function of BufferHubBuffer. Update BufferHubBuffer_test to check if the two event fds are actually linked to each other. Test: VtsHalBufferHubV1_0TargetTest, BufferHubServer_test, BufferHub_test Fix: 68770788 Change-Id: I7f6e07d17615d2b45c0e7e086c481292c5798e97 --- libs/ui/BufferHubBuffer.cpp | 16 +++++++++---- libs/ui/include/ui/BufferHubBuffer.h | 6 +++++ libs/ui/include/ui/BufferHubDefs.h | 11 +++++---- libs/ui/tests/BufferHubBuffer_test.cpp | 27 ++++++++++++++++++++++ services/bufferhub/BufferHubService.cpp | 18 ++++++++------- .../bufferhub/include/bufferhub/BufferHubService.h | 2 +- services/bufferhub/include/bufferhub/BufferNode.h | 7 ++++++ 7 files changed, 68 insertions(+), 19 deletions(-) (limited to 'libs/ui/BufferHubBuffer.cpp') diff --git a/libs/ui/BufferHubBuffer.cpp b/libs/ui/BufferHubBuffer.cpp index 6310f29152..9669135ad6 100644 --- a/libs/ui/BufferHubBuffer.cpp +++ b/libs/ui/BufferHubBuffer.cpp @@ -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)); @@ -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()); @@ -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() { diff --git a/libs/ui/include/ui/BufferHubBuffer.h b/libs/ui/include/ui/BufferHubBuffer.h index c6a4a232d1..42d9320545 100644 --- a/libs/ui/include/ui/BufferHubBuffer.h +++ b/libs/ui/include/ui/BufferHubBuffer.h @@ -21,6 +21,7 @@ #include #include #include +#include #include namespace android { @@ -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); @@ -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; diff --git a/libs/ui/include/ui/BufferHubDefs.h b/libs/ui/include/ui/BufferHubDefs.h index 069f0dc25e..43d900c034 100644 --- a/libs/ui/include/ui/BufferHubDefs.h +++ b/libs/ui/include/ui/BufferHubDefs.h @@ -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 diff --git a/libs/ui/tests/BufferHubBuffer_test.cpp b/libs/ui/tests/BufferHubBuffer_test.cpp index cd744cd106..77ea19cdfb 100644 --- a/libs/ui/tests/BufferHubBuffer_test.cpp +++ b/libs/ui/tests/BufferHubBuffer_test.cpp @@ -16,6 +16,8 @@ #define LOG_TAG "BufferHubBufferTest" +#include + #include #include #include @@ -23,6 +25,7 @@ #include #include #include +#include namespace android { @@ -160,6 +163,30 @@ TEST_F(BufferHubBufferTest, DuplicateAndImportBuffer) { // Both buffer instances should be in released state currently. EXPECT_TRUE(IsBufferReleased(b1->buffer_state())); EXPECT_TRUE(IsBufferReleased(b2->buffer_state())); + + // The event fd should behave like duped event fds. + const BufferHubEventFd& eventFd1 = b1->eventFd(); + const BufferHubEventFd& eventFd2 = b2->eventFd(); + + base::unique_fd epollFd(epoll_create(64)); + ASSERT_GE(epollFd.get(), 0); + + // Add eventFd1 to epoll set, and signal eventFd2. + epoll_event e = {.events = EPOLLIN | EPOLLET, .data = {.u32 = 0}}; + ASSERT_EQ(epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, eventFd1.get(), &e), 0); + + std::array events; + EXPECT_EQ(epoll_wait(epollFd.get(), events.data(), events.size(), 0), 0); + + eventFd2.signal(); + EXPECT_EQ(epoll_wait(epollFd.get(), events.data(), events.size(), 0), 1); + + // The epoll fd is edge triggered, so it only responds to the eventFd once. + EXPECT_EQ(epoll_wait(epollFd.get(), events.data(), events.size(), 0), 0); + + eventFd2.signal(); + eventFd2.clear(); + EXPECT_EQ(epoll_wait(epollFd.get(), events.data(), events.size(), 0), 0); } TEST_F(BufferHubBufferTest, ImportFreedBuffer) { diff --git a/services/bufferhub/BufferHubService.cpp b/services/bufferhub/BufferHubService.cpp index a40443d8d9..0b29a80e00 100644 --- a/services/bufferhub/BufferHubService.cpp +++ b/services/bufferhub/BufferHubService.cpp @@ -65,9 +65,9 @@ Return BufferHubService::allocateBuffer(const HardwareBufferDescription& d std::lock_guard lock(mClientSetMutex); mClientSet.emplace(client); - hidl_handle bufferInfo = - buildBufferInfo(node->id(), node->AddNewActiveClientsBitToMask(), - node->user_metadata_size(), node->metadata().ashmem_fd()); + hidl_handle bufferInfo = buildBufferInfo(node->id(), node->AddNewActiveClientsBitToMask(), + node->user_metadata_size(), node->eventFd().get(), + node->metadata().ashmem_fd()); BufferTraits bufferTraits = {/*bufferDesc=*/description, /*bufferHandle=*/hidl_handle(node->buffer_handle()), /*bufferInfo=*/bufferInfo}; @@ -156,7 +156,7 @@ Return BufferHubService::importBuffer(const hidl_handle& tokenHandle, hidl_handle bufferInfo = buildBufferInfo(node->id(), clientStateMask, node->user_metadata_size(), - node->metadata().ashmem_fd()); + node->eventFd().get(), node->metadata().ashmem_fd()); BufferTraits bufferTraits = {/*bufferDesc=*/bufferDesc, /*bufferHandle=*/hidl_handle(node->buffer_handle()), /*bufferInfo=*/bufferInfo}; @@ -345,16 +345,18 @@ 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 metadataFd) { + 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] = bufferId; + infoHandle->data[1] = dup(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. - memcpy(&infoHandle->data[2], &clientBitMask, sizeof(clientBitMask)); - memcpy(&infoHandle->data[3], &userMetadataSize, sizeof(userMetadataSize)); + memcpy(&infoHandle->data[3], &clientBitMask, sizeof(clientBitMask)); + memcpy(&infoHandle->data[4], &userMetadataSize, sizeof(userMetadataSize)); hidl_handle bufferInfo; bufferInfo.setTo(infoHandle, /*shouldOwn=*/true); diff --git a/services/bufferhub/include/bufferhub/BufferHubService.h b/services/bufferhub/include/bufferhub/BufferHubService.h index 23e664e036..9015e05e46 100644 --- a/services/bufferhub/include/bufferhub/BufferHubService.h +++ b/services/bufferhub/include/bufferhub/BufferHubService.h @@ -59,7 +59,7 @@ public: private: // Helper function to build BufferTraits.bufferInfo handle hidl_handle buildBufferInfo(int bufferId, uint32_t clientBitMask, uint32_t userMetadataSize, - const int metadataFd); + const int eventFd, const int metadataFd); // Helper function to remove all the token belongs to a specific client. void removeTokenByClient(const BufferClient* client); diff --git a/services/bufferhub/include/bufferhub/BufferNode.h b/services/bufferhub/include/bufferhub/BufferNode.h index b7a195b0a1..4729e1cc73 100644 --- a/services/bufferhub/include/bufferhub/BufferNode.h +++ b/services/bufferhub/include/bufferhub/BufferNode.h @@ -4,6 +4,7 @@ #include #include #include +#include #include namespace android { @@ -31,6 +32,9 @@ public: const native_handle_t* buffer_handle() const { return buffer_handle_; } const AHardwareBuffer_Desc& buffer_desc() const { return buffer_desc_; } + // Accessor of event fd. + const BufferHubEventFd& eventFd() const { return mEventFd; } + // Accessors of metadata. const BufferHubMetadata& metadata() const { return metadata_; } @@ -60,6 +64,9 @@ private: native_handle_t* buffer_handle_; AHardwareBuffer_Desc buffer_desc_; + // Eventfd used for signalling buffer events among the clients of the buffer. + BufferHubEventFd mEventFd; + // Metadata in shared memory. BufferHubMetadata metadata_; -- cgit v1.2.3-59-g8ed1b From ce232740fdef692a99283914cb96693e23c7bb56 Mon Sep 17 00:00:00 2001 From: Tianyu Jiang Date: Tue, 29 Jan 2019 10:17:31 -0800 Subject: Fix BufferHubBuffer::Create crash Change-Id: I4c832c86cc4be9a5622bbd533a71d72c5d669ab1 Fix: 123535377 Test: BufferHub_test BufferHubServer_test VtsHalBufferHubV1_0TargetTest --- libs/ui/BufferHubBuffer.cpp | 4 ++-- libs/ui/tests/BufferHubBuffer_test.cpp | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) (limited to 'libs/ui/BufferHubBuffer.cpp') diff --git a/libs/ui/BufferHubBuffer.cpp b/libs/ui/BufferHubBuffer.cpp index 9669135ad6..f3856d0cc5 100644 --- a/libs/ui/BufferHubBuffer.cpp +++ b/libs/ui/BufferHubBuffer.cpp @@ -180,14 +180,14 @@ int BufferHubBuffer::initWithBufferTraits(const BufferTraits& bufferTraits) { return -EINVAL; } - const int eventFd = bufferTraits.bufferInfo->data[1]; + // Import fds. Dup fds because hidl_handle owns the fds. + const int eventFd = fcntl(bufferTraits.bufferInfo->data[1], F_DUPFD_CLOEXEC, 0); 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)); diff --git a/libs/ui/tests/BufferHubBuffer_test.cpp b/libs/ui/tests/BufferHubBuffer_test.cpp index 77ea19cdfb..cdd5a04120 100644 --- a/libs/ui/tests/BufferHubBuffer_test.cpp +++ b/libs/ui/tests/BufferHubBuffer_test.cpp @@ -16,6 +16,7 @@ #define LOG_TAG "BufferHubBufferTest" +#include #include #include @@ -166,14 +167,16 @@ TEST_F(BufferHubBufferTest, DuplicateAndImportBuffer) { // The event fd should behave like duped event fds. const BufferHubEventFd& eventFd1 = b1->eventFd(); + ASSERT_GE(eventFd1.get(), 0); const BufferHubEventFd& eventFd2 = b2->eventFd(); + ASSERT_GE(eventFd2.get(), 0); base::unique_fd epollFd(epoll_create(64)); ASSERT_GE(epollFd.get(), 0); // Add eventFd1 to epoll set, and signal eventFd2. epoll_event e = {.events = EPOLLIN | EPOLLET, .data = {.u32 = 0}}; - ASSERT_EQ(epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, eventFd1.get(), &e), 0); + ASSERT_EQ(epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, eventFd1.get(), &e), 0) << strerror(errno); std::array events; EXPECT_EQ(epoll_wait(epollFd.get(), events.data(), events.size(), 0), 0); -- cgit v1.2.3-59-g8ed1b From 6f54dca05107d4da9bb66c467967d950e4f1e14b Mon Sep 17 00:00:00 2001 From: Tianyu Jiang Date: Tue, 29 Jan 2019 12:46:35 -0800 Subject: Clean up initWithBufferTraits Bug: None Test: BufferHub_test BufferHubServer_test VtsHalBufferHubV1_0TargetTest Change-Id: I3b386c3710ca1268587e3a3b03632e02cd353b6e --- libs/ui/BufferHubBuffer.cpp | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) (limited to 'libs/ui/BufferHubBuffer.cpp') diff --git a/libs/ui/BufferHubBuffer.cpp b/libs/ui/BufferHubBuffer.cpp index f3856d0cc5..4b3d3ba06e 100644 --- a/libs/ui/BufferHubBuffer.cpp +++ b/libs/ui/BufferHubBuffer.cpp @@ -167,32 +167,30 @@ int BufferHubBuffer::initWithBufferTraits(const BufferTraits& bufferTraits) { return -EINVAL; } - int bufferId = bufferTraits.bufferInfo->data[2]; - if (bufferId < 0) { - ALOGE("%s: Received an invalid (negative) id!", __FUNCTION__); + // Import fds. Dup fds because hidl_handle owns the fds. + unique_fd ashmemFd(fcntl(bufferTraits.bufferInfo->data[0], F_DUPFD_CLOEXEC, 0)); + mMetadata = BufferHubMetadata::Import(std::move(ashmemFd)); + if (!mMetadata.IsValid()) { + ALOGE("%s: Received an invalid metadata.", __FUNCTION__); return -EINVAL; } - uint32_t clientBitMask; - memcpy(&clientBitMask, &bufferTraits.bufferInfo->data[3], sizeof(clientBitMask)); - if (clientBitMask == 0U) { - ALOGE("%s: Received a invalid client state mask!", __FUNCTION__); + mEventFd = BufferHubEventFd(fcntl(bufferTraits.bufferInfo->data[1], F_DUPFD_CLOEXEC, 0)); + if (!mEventFd.isValid()) { + ALOGE("%s: Received ad invalid event fd.", __FUNCTION__); return -EINVAL; } - // Import fds. Dup fds because hidl_handle owns the fds. - const int eventFd = fcntl(bufferTraits.bufferInfo->data[1], F_DUPFD_CLOEXEC, 0); - if (eventFd < 0) { - ALOGE("%s: Received a invalid event fd!", __FUNCTION__); + int bufferId = bufferTraits.bufferInfo->data[2]; + if (bufferId < 0) { + ALOGE("%s: Received an invalid (negative) id.", __FUNCTION__); return -EINVAL; } - mEventFd = BufferHubEventFd(eventFd); - - unique_fd ashmemFd(fcntl(bufferTraits.bufferInfo->data[0], F_DUPFD_CLOEXEC, 0)); - mMetadata = BufferHubMetadata::Import(std::move(ashmemFd)); - if (!mMetadata.IsValid()) { - ALOGE("%s: invalid metadata.", __FUNCTION__); + uint32_t clientBitMask; + memcpy(&clientBitMask, &bufferTraits.bufferInfo->data[3], sizeof(clientBitMask)); + if (clientBitMask == 0U) { + ALOGE("%s: Received an invalid client state mask.", __FUNCTION__); return -EINVAL; } -- cgit v1.2.3-59-g8ed1b From a8df5f30d3d664eea7b4fb9b8d65ef7c467655a0 Mon Sep 17 00:00:00 2001 From: Tianyu Jiang Date: Mon, 14 Jan 2019 18:42:12 -0800 Subject: Replace the use of helper function IsBufferReleased to member function IsReleased() or is_released(). Fix: 122854791 Test: BufferHub_test BufferHubServer_test VtsHalBufferHubV1_0TargetTest buffer_hub-test buffer_hub_queue-test dvr_buffer_queue-test on walleye_xr Change-Id: I2431a4ddd78cb2eef9bdeafc0d9048571f7a0c61 --- libs/ui/BufferHubBuffer.cpp | 5 ++++ libs/ui/include/ui/BufferHubBuffer.h | 7 +++-- libs/ui/include/ui/BufferHubDefs.h | 5 ---- libs/ui/tests/BufferHubBuffer_test.cpp | 15 +++++----- libs/ui/tests/BufferHubMetadata_test.cpp | 10 ++++--- libs/vr/libbufferhub/buffer_hub-test.cpp | 17 +++++------ .../include/private/dvr/buffer_hub_defs.h | 4 --- .../include/private/dvr/producer_channel.h | 6 +++- services/vr/bufferhubd/producer_channel.cpp | 34 ++++++++++------------ 9 files changed, 50 insertions(+), 53 deletions(-) (limited to 'libs/ui/BufferHubBuffer.cpp') diff --git a/libs/ui/BufferHubBuffer.cpp b/libs/ui/BufferHubBuffer.cpp index 4b3d3ba06e..4b20772b75 100644 --- a/libs/ui/BufferHubBuffer.cpp +++ b/libs/ui/BufferHubBuffer.cpp @@ -318,6 +318,11 @@ int BufferHubBuffer::Release() { return 0; } +bool BufferHubBuffer::IsReleased() const { + return (buffer_state_->load(std::memory_order_acquire) & + active_clients_bit_mask_->load(std::memory_order_acquire)) == 0; +} + bool BufferHubBuffer::IsValid() const { return mBufferHandle.getNativeHandle() != nullptr && mId >= 0 && mClientStateMask != 0U && mEventFd.get() >= 0 && mMetadata.IsValid() && mBufferClient != nullptr; diff --git a/libs/ui/include/ui/BufferHubBuffer.h b/libs/ui/include/ui/BufferHubBuffer.h index 42d9320545..0b6d75a9cb 100644 --- a/libs/ui/include/ui/BufferHubBuffer.h +++ b/libs/ui/include/ui/BufferHubBuffer.h @@ -59,9 +59,7 @@ public: 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); - } + uint32_t buffer_state() const { return buffer_state_->load(std::memory_order_acquire); } // A state mask which is unique to a buffer hub client among all its siblings sharing the same // concrete graphic buffer. @@ -97,6 +95,9 @@ public: // current cycle of the usage of the buffer. int Release(); + // Returns whether the buffer is released by all active clients or not. + bool IsReleased() const; + // Creates a token that stands for this BufferHubBuffer client and could be used for Import to // create another BufferHubBuffer. The new BufferHubBuffer will share the same underlying // gralloc buffer and ashmem region for metadata. Note that the caller owns the token and diff --git a/libs/ui/include/ui/BufferHubDefs.h b/libs/ui/include/ui/BufferHubDefs.h index 43d900c034..ff970cbd64 100644 --- a/libs/ui/include/ui/BufferHubDefs.h +++ b/libs/ui/include/ui/BufferHubDefs.h @@ -106,11 +106,6 @@ static inline bool IsClientAcquired(uint32_t state, uint32_t client_bit_mask) { return high_bits == 0U; } -// Returns true if all clients are in released state. -static inline bool IsBufferReleased(uint32_t state) { - return state == 0U; -} - // Returns true if the input client is in released state. static inline bool IsClientReleased(uint32_t state, uint32_t client_bit_mask) { return (state & client_bit_mask) == 0U; diff --git a/libs/ui/tests/BufferHubBuffer_test.cpp b/libs/ui/tests/BufferHubBuffer_test.cpp index 58965c1859..3bcd9353fc 100644 --- a/libs/ui/tests/BufferHubBuffer_test.cpp +++ b/libs/ui/tests/BufferHubBuffer_test.cpp @@ -35,7 +35,6 @@ namespace { using ::android::BufferHubDefs::AnyClientAcquired; using ::android::BufferHubDefs::AnyClientGained; using ::android::BufferHubDefs::AnyClientPosted; -using ::android::BufferHubDefs::IsBufferReleased; using ::android::BufferHubDefs::IsClientAcquired; using ::android::BufferHubDefs::IsClientGained; using ::android::BufferHubDefs::IsClientPosted; @@ -162,8 +161,8 @@ TEST_F(BufferHubBufferTest, DuplicateAndImportBuffer) { EXPECT_NE(b1->client_state_mask(), b2->client_state_mask()); // Both buffer instances should be in released state currently. - EXPECT_TRUE(IsBufferReleased(b1->buffer_state())); - EXPECT_TRUE(IsBufferReleased(b2->buffer_state())); + EXPECT_TRUE(b1->IsReleased()); + EXPECT_TRUE(b2->IsReleased()); // The event fd should behave like duped event fds. const BufferHubEventFd& eventFd1 = b1->eventFd(); @@ -230,7 +229,7 @@ TEST_F(BufferHubBufferTest, ImportInvalidToken) { } TEST_F(BufferHubBufferStateTransitionTest, GainBuffer_fromReleasedState) { - ASSERT_TRUE(IsBufferReleased(b1->buffer_state())); + ASSERT_TRUE(b1->IsReleased()); // Successful gaining the buffer should change the buffer state bit of b1 to // gained state, other client state bits to released state. @@ -319,7 +318,7 @@ TEST_F(BufferHubBufferStateTransitionTest, PostBuffer_fromAcquiredState) { } TEST_F(BufferHubBufferStateTransitionTest, PostBuffer_fromReleasedState) { - ASSERT_TRUE(IsBufferReleased(b1->buffer_state())); + ASSERT_TRUE(b1->IsReleased()); // Posting from released state should fail. EXPECT_EQ(b1->Post(), -EBUSY); @@ -357,7 +356,7 @@ TEST_F(BufferHubBufferStateTransitionTest, AcquireBuffer_fromSelfInAcquiredState } TEST_F(BufferHubBufferStateTransitionTest, AcquireBuffer_fromReleasedState) { - ASSERT_TRUE(IsBufferReleased(b1->buffer_state())); + ASSERT_TRUE(b1->IsReleased()); // Acquiring form released state should fail. EXPECT_EQ(b1->Acquire(), -EBUSY); @@ -374,13 +373,13 @@ TEST_F(BufferHubBufferStateTransitionTest, AcquireBuffer_fromGainedState) { } TEST_F(BufferHubBufferStateTransitionTest, ReleaseBuffer_fromSelfInReleasedState) { - ASSERT_TRUE(IsBufferReleased(b1->buffer_state())); + ASSERT_TRUE(b1->IsReleased()); EXPECT_EQ(b1->Release(), 0); } TEST_F(BufferHubBufferStateTransitionTest, ReleaseBuffer_fromSelfInGainedState) { - ASSERT_TRUE(IsBufferReleased(b1->buffer_state())); + ASSERT_TRUE(b1->IsReleased()); ASSERT_EQ(b1->Gain(), 0); ASSERT_TRUE(AnyClientGained(b1->buffer_state())); diff --git a/libs/ui/tests/BufferHubMetadata_test.cpp b/libs/ui/tests/BufferHubMetadata_test.cpp index 11f8e57adc..b7f0b4b140 100644 --- a/libs/ui/tests/BufferHubMetadata_test.cpp +++ b/libs/ui/tests/BufferHubMetadata_test.cpp @@ -17,8 +17,6 @@ #include #include -using android::BufferHubDefs::IsBufferReleased; - namespace android { namespace dvr { @@ -52,13 +50,17 @@ TEST_F(BufferHubMetadataTest, Import_Success) { BufferHubDefs::MetadataHeader* mh1 = m1.metadata_header(); EXPECT_NE(mh1, nullptr); - EXPECT_TRUE(IsBufferReleased(mh1->buffer_state.load())); + // Check if the newly allocated buffer is initialized in released state (i.e. + // state equals to 0U). + EXPECT_TRUE(mh1->buffer_state.load() == 0U); EXPECT_TRUE(m2.IsValid()); BufferHubDefs::MetadataHeader* mh2 = m2.metadata_header(); EXPECT_NE(mh2, nullptr); - EXPECT_TRUE(IsBufferReleased(mh2->buffer_state.load())); + // Check if the newly allocated buffer is initialized in released state (i.e. + // state equals to 0U). + EXPECT_TRUE(mh2->buffer_state.load() == 0U); } TEST_F(BufferHubMetadataTest, MoveMetadataInvalidatesOldOne) { diff --git a/libs/vr/libbufferhub/buffer_hub-test.cpp b/libs/vr/libbufferhub/buffer_hub-test.cpp index ed5a992f3c..a47a98f3b6 100644 --- a/libs/vr/libbufferhub/buffer_hub-test.cpp +++ b/libs/vr/libbufferhub/buffer_hub-test.cpp @@ -22,7 +22,6 @@ using android::BufferHubDefs::AnyClientAcquired; using android::BufferHubDefs::AnyClientGained; using android::BufferHubDefs::AnyClientPosted; -using android::BufferHubDefs::IsBufferReleased; using android::BufferHubDefs::IsClientAcquired; using android::BufferHubDefs::IsClientPosted; using android::BufferHubDefs::IsClientReleased; @@ -284,7 +283,7 @@ TEST_F(LibBufferHubTest, TestAsyncStateTransitions) { EXPECT_EQ(0, c->ReleaseAsync(&metadata, invalid_fence)); EXPECT_LT(0, RETRY_EINTR(p->Poll(kPollTimeoutMs))); EXPECT_EQ(p->buffer_state(), c->buffer_state()); - EXPECT_TRUE(IsBufferReleased(p->buffer_state())); + EXPECT_TRUE(p->is_released()); // Acquire and post in released state should fail. EXPECT_EQ(-EBUSY, c->AcquireAsync(&metadata, &invalid_fence)); @@ -356,7 +355,7 @@ TEST_F(LibBufferHubTest, TestGainPostedBuffer_noConsumer) { // Producer state bit is in released state after post, other clients shall be // in posted state although there is no consumer of this buffer yet. ASSERT_TRUE(IsClientReleased(p->buffer_state(), p->client_state_mask())); - ASSERT_FALSE(IsBufferReleased(p->buffer_state())); + ASSERT_TRUE(p->is_released()); ASSERT_TRUE(AnyClientPosted(p->buffer_state())); // Gain in released state should succeed. @@ -374,7 +373,7 @@ TEST_F(LibBufferHubTest, TestMaxConsumers) { for (size_t i = 0; i < kMaxConsumerCount; ++i) { cs[i] = ConsumerBuffer::Import(p->CreateConsumer()); ASSERT_TRUE(cs[i].get() != nullptr); - EXPECT_TRUE(IsBufferReleased(cs[i]->buffer_state())); + EXPECT_TRUE(cs[i]->is_released()); EXPECT_NE(producer_state_mask, cs[i]->client_state_mask()); } @@ -397,12 +396,12 @@ TEST_F(LibBufferHubTest, TestMaxConsumers) { // All consumers have to release before the buffer is considered to be // released. for (size_t i = 0; i < kMaxConsumerCount; i++) { - EXPECT_FALSE(IsBufferReleased(p->buffer_state())); + EXPECT_FALSE(p->is_released()); EXPECT_EQ(0, cs[i]->ReleaseAsync(&metadata, invalid_fence)); } EXPECT_LT(0, RETRY_EINTR(p->Poll(kPollTimeoutMs))); - EXPECT_TRUE(IsBufferReleased(p->buffer_state())); + EXPECT_TRUE(p->is_released()); // Buffer state cross all clients must be consistent. for (size_t i = 0; i < kMaxConsumerCount; i++) { @@ -445,7 +444,7 @@ TEST_F(LibBufferHubTest, TestCreateTheFirstConsumerAfterPostingBuffer) { // Post the gained buffer before any consumer gets created. EXPECT_EQ(0, p->PostAsync(&metadata, invalid_fence)); - EXPECT_FALSE(IsBufferReleased(p->buffer_state())); + EXPECT_TRUE(p->is_released()); EXPECT_EQ(0, RETRY_EINTR(p->Poll(kPollTimeoutMs))); // Newly created consumer will be signalled for the posted buffer although it @@ -481,7 +480,7 @@ TEST_F(LibBufferHubTest, TestCreateConsumerWhenBufferReleased) { // need to wait until the releasd is confirmed before creating another // consumer. EXPECT_LT(0, RETRY_EINTR(p->Poll(kPollTimeoutMs))); - EXPECT_TRUE(IsBufferReleased(p->buffer_state())); + EXPECT_TRUE(p->is_released()); // Create another consumer immediately after the release, should not make the // buffer un-released. @@ -489,7 +488,7 @@ TEST_F(LibBufferHubTest, TestCreateConsumerWhenBufferReleased) { ConsumerBuffer::Import(p->CreateConsumer()); ASSERT_TRUE(c2.get() != nullptr); - EXPECT_TRUE(IsBufferReleased(p->buffer_state())); + EXPECT_TRUE(p->is_released()); EXPECT_EQ(0, p->GainAsync(&metadata, &invalid_fence)); EXPECT_TRUE(AnyClientGained(p->buffer_state())); } diff --git a/libs/vr/libbufferhub/include/private/dvr/buffer_hub_defs.h b/libs/vr/libbufferhub/include/private/dvr/buffer_hub_defs.h index f5761d5a04..bab7367caa 100644 --- a/libs/vr/libbufferhub/include/private/dvr/buffer_hub_defs.h +++ b/libs/vr/libbufferhub/include/private/dvr/buffer_hub_defs.h @@ -51,10 +51,6 @@ static inline bool IsClientAcquired(uint32_t state, uint32_t client_bit_mask) { return android::BufferHubDefs::IsClientAcquired(state, client_bit_mask); } -static inline bool IsBufferReleased(uint32_t state) { - return android::BufferHubDefs::IsBufferReleased(state); -} - static inline bool IsClientReleased(uint32_t state, uint32_t client_bit_mask) { return android::BufferHubDefs::IsClientReleased(state, client_bit_mask); } diff --git a/services/vr/bufferhubd/include/private/dvr/producer_channel.h b/services/vr/bufferhubd/include/private/dvr/producer_channel.h index 96ef1a20a0..45593ad955 100644 --- a/services/vr/bufferhubd/include/private/dvr/producer_channel.h +++ b/services/vr/bufferhubd/include/private/dvr/producer_channel.h @@ -69,7 +69,7 @@ class ProducerChannel : public BufferHubChannel { bool CheckParameters(uint32_t width, uint32_t height, uint32_t layer_count, uint32_t format, uint64_t usage, - size_t user_metadata_size); + size_t user_metadata_size) const; private: std::vector consumer_channels_; @@ -112,6 +112,10 @@ class ProducerChannel : public BufferHubChannel { // This function is used for clean up for failures in CreateConsumer method. void RemoveConsumerClientMask(uint32_t consumer_state_mask); + // Checks whether the buffer is released by all active clients, excluding + // orphaned consumers. + bool IsBufferReleasedByAllActiveClientsExceptForOrphans() const; + ProducerChannel(const ProducerChannel&) = delete; void operator=(const ProducerChannel&) = delete; }; diff --git a/services/vr/bufferhubd/producer_channel.cpp b/services/vr/bufferhubd/producer_channel.cpp index 895dee0701..164d9e6b19 100644 --- a/services/vr/bufferhubd/producer_channel.cpp +++ b/services/vr/bufferhubd/producer_channel.cpp @@ -333,7 +333,10 @@ Status ProducerChannel::CreateConsumer( uint32_t current_buffer_state = buffer_state_->load(std::memory_order_acquire); - if (BufferHubDefs::IsBufferReleased(current_buffer_state) || + // Return the consumer channel handle without signal when adding the new + // consumer to a buffer that is available to producer (a.k.a a fully-released + // buffer) or a gained buffer. + if (current_buffer_state == 0U || BufferHubDefs::AnyClientGained(current_buffer_state)) { return {status.take()}; } @@ -356,7 +359,7 @@ Status ProducerChannel::CreateConsumer( ". About to try again if the buffer is still not gained nor fully " "released.", __FUNCTION__, current_buffer_state, updated_buffer_state); - if (BufferHubDefs::IsBufferReleased(current_buffer_state) || + if (current_buffer_state == 0U || BufferHubDefs::AnyClientGained(current_buffer_state)) { ALOGI("%s: buffer is gained or fully released, state=%" PRIx32 ".", __FUNCTION__, current_buffer_state); @@ -536,14 +539,7 @@ Status ProducerChannel::OnConsumerRelease(Message&, } } - uint32_t current_buffer_state = - buffer_state_->load(std::memory_order_acquire); - uint32_t current_active_clients_bit_mask = - active_clients_bit_mask_->load(std::memory_order_acquire); - // Signal producer if all current active consumers have released the buffer. - if (BufferHubDefs::IsBufferReleased(current_buffer_state & - ~orphaned_consumer_bit_mask_ & - current_active_clients_bit_mask)) { + if (IsBufferReleasedByAllActiveClientsExceptForOrphans()) { buffer_state_->store(0U); SignalAvailable(); if (orphaned_consumer_bit_mask_) { @@ -568,14 +564,7 @@ void ProducerChannel::OnConsumerOrphaned(const uint32_t& consumer_state_mask) { __FUNCTION__, consumer_state_mask); orphaned_consumer_bit_mask_ |= consumer_state_mask; - uint32_t current_buffer_state = - buffer_state_->load(std::memory_order_acquire); - uint32_t current_active_clients_bit_mask = - active_clients_bit_mask_->load(std::memory_order_acquire); - // Signal producer if all current active consumers have released the buffer. - if (BufferHubDefs::IsBufferReleased(current_buffer_state & - ~orphaned_consumer_bit_mask_ & - current_active_clients_bit_mask)) { + if (IsBufferReleasedByAllActiveClientsExceptForOrphans()) { buffer_state_->store(0U); SignalAvailable(); } @@ -667,12 +656,19 @@ void ProducerChannel::RemoveConsumer(ConsumerChannel* channel) { bool ProducerChannel::CheckParameters(uint32_t width, uint32_t height, uint32_t layer_count, uint32_t format, uint64_t usage, - size_t user_metadata_size) { + size_t user_metadata_size) const { return user_metadata_size == user_metadata_size_ && buffer_.width() == width && buffer_.height() == height && buffer_.layer_count() == layer_count && buffer_.format() == format && buffer_.usage() == usage; } +bool ProducerChannel::IsBufferReleasedByAllActiveClientsExceptForOrphans() + const { + return (buffer_state_->load(std::memory_order_acquire) & + ~orphaned_consumer_bit_mask_ & + active_clients_bit_mask_->load(std::memory_order_acquire)) == 0U; +} + } // namespace dvr } // namespace android -- cgit v1.2.3-59-g8ed1b From 1543d888f5e8ccc01930369a26c371649bc18068 Mon Sep 17 00:00:00 2001 From: Tianyu Jiang Date: Mon, 4 Feb 2019 10:53:53 -0800 Subject: Fix non camelCase member variables Android Framework C++ Code Style Guidelines says that member variables should be camelCase: http://go/droidcppstyle. Test: m, mma in frameworks/native Bug: 68273829 Change-Id: I859b916244f1e2ca2ba35c0a6e816b6250cd71ce --- libs/ui/BufferHubBuffer.cpp | 80 +++++++++++------------ libs/ui/include/ui/BufferHubBuffer.h | 8 +-- libs/ui/tests/BufferHubBuffer_test.cpp | 14 ++-- services/bufferhub/BufferNode.cpp | 59 ++++++++--------- services/bufferhub/include/bufferhub/BufferNode.h | 46 ++++++------- 5 files changed, 103 insertions(+), 104 deletions(-) (limited to 'libs/ui/BufferHubBuffer.cpp') diff --git a/libs/ui/BufferHubBuffer.cpp b/libs/ui/BufferHubBuffer.cpp index 4b20772b75..c3144b924d 100644 --- a/libs/ui/BufferHubBuffer.cpp +++ b/libs/ui/BufferHubBuffer.cpp @@ -210,15 +210,15 @@ int BufferHubBuffer::initWithBufferTraits(const BufferTraits& bufferTraits) { // Populate shortcuts to the atomics in metadata. auto metadata_header = mMetadata.metadata_header(); - buffer_state_ = &metadata_header->buffer_state; - fence_state_ = &metadata_header->fence_state; - active_clients_bit_mask_ = &metadata_header->active_clients_bit_mask; + mBufferState = &metadata_header->buffer_state; + mFenceState = &metadata_header->fence_state; + mActiveClientsBitMask = &metadata_header->active_clients_bit_mask; // The C++ standard recommends (but does not require) that lock-free atomic operations are // also address-free, that is, suitable for communication between processes using shared // memory. - LOG_ALWAYS_FATAL_IF(!std::atomic_is_lock_free(buffer_state_) || - !std::atomic_is_lock_free(fence_state_) || - !std::atomic_is_lock_free(active_clients_bit_mask_), + LOG_ALWAYS_FATAL_IF(!std::atomic_is_lock_free(mBufferState) || + !std::atomic_is_lock_free(mFenceState) || + !std::atomic_is_lock_free(mActiveClientsBitMask), "Atomic variables in ashmen are not lock free."); // Import the buffer: We only need to hold on the native_handle_t here so that @@ -231,96 +231,96 @@ int BufferHubBuffer::initWithBufferTraits(const BufferTraits& bufferTraits) { // TODO(b/112012161) Set up shared fences. ALOGD("%s: id=%d, buffer_state=%" PRIx32 ".", __FUNCTION__, mId, - buffer_state_->load(std::memory_order_acquire)); + mBufferState->load(std::memory_order_acquire)); return 0; } int BufferHubBuffer::Gain() { - uint32_t current_buffer_state = buffer_state_->load(std::memory_order_acquire); - if (IsClientGained(current_buffer_state, mClientStateMask)) { + uint32_t currentBufferState = mBufferState->load(std::memory_order_acquire); + if (IsClientGained(currentBufferState, mClientStateMask)) { ALOGV("%s: Buffer is already gained by this client %" PRIx32 ".", __FUNCTION__, mClientStateMask); return 0; } do { - if (AnyClientGained(current_buffer_state & (~mClientStateMask)) || - AnyClientAcquired(current_buffer_state)) { + if (AnyClientGained(currentBufferState & (~mClientStateMask)) || + AnyClientAcquired(currentBufferState)) { ALOGE("%s: Buffer is in use, id=%d mClientStateMask=%" PRIx32 " state=%" PRIx32 ".", - __FUNCTION__, mId, mClientStateMask, current_buffer_state); + __FUNCTION__, mId, mClientStateMask, currentBufferState); return -EBUSY; } // Change the buffer state to gained state, whose value happens to be the same as // mClientStateMask. - } while (!buffer_state_->compare_exchange_weak(current_buffer_state, mClientStateMask, - std::memory_order_acq_rel, - std::memory_order_acquire)); + } while (!mBufferState->compare_exchange_weak(currentBufferState, mClientStateMask, + std::memory_order_acq_rel, + std::memory_order_acquire)); // TODO(b/119837586): Update fence state and return GPU fence. return 0; } int BufferHubBuffer::Post() { - uint32_t current_buffer_state = buffer_state_->load(std::memory_order_acquire); - uint32_t updated_buffer_state = (~mClientStateMask) & BufferHubDefs::kHighBitsMask; + uint32_t currentBufferState = mBufferState->load(std::memory_order_acquire); + uint32_t updatedBufferState = (~mClientStateMask) & BufferHubDefs::kHighBitsMask; do { - if (!IsClientGained(current_buffer_state, mClientStateMask)) { + if (!IsClientGained(currentBufferState, mClientStateMask)) { ALOGE("%s: Cannot post a buffer that is not gained by this client. buffer_id=%d " "mClientStateMask=%" PRIx32 " state=%" PRIx32 ".", - __FUNCTION__, mId, mClientStateMask, current_buffer_state); + __FUNCTION__, mId, mClientStateMask, currentBufferState); return -EBUSY; } // Set the producer client buffer state to released, other clients' buffer state to posted. // Post to all existing and non-existing clients. - } while (!buffer_state_->compare_exchange_weak(current_buffer_state, updated_buffer_state, - std::memory_order_acq_rel, - std::memory_order_acquire)); + } while (!mBufferState->compare_exchange_weak(currentBufferState, updatedBufferState, + std::memory_order_acq_rel, + std::memory_order_acquire)); // TODO(b/119837586): Update fence state and return GPU fence if needed. return 0; } int BufferHubBuffer::Acquire() { - uint32_t current_buffer_state = buffer_state_->load(std::memory_order_acquire); - if (IsClientAcquired(current_buffer_state, mClientStateMask)) { + uint32_t currentBufferState = mBufferState->load(std::memory_order_acquire); + if (IsClientAcquired(currentBufferState, mClientStateMask)) { ALOGV("%s: Buffer is already acquired by this client %" PRIx32 ".", __FUNCTION__, mClientStateMask); return 0; } - uint32_t updated_buffer_state = 0U; + uint32_t updatedBufferState = 0U; do { - if (!IsClientPosted(current_buffer_state, mClientStateMask)) { + if (!IsClientPosted(currentBufferState, mClientStateMask)) { ALOGE("%s: Cannot acquire a buffer that is not in posted state. buffer_id=%d " "mClientStateMask=%" PRIx32 " state=%" PRIx32 ".", - __FUNCTION__, mId, mClientStateMask, current_buffer_state); + __FUNCTION__, mId, mClientStateMask, currentBufferState); return -EBUSY; } // Change the buffer state for this consumer from posted to acquired. - updated_buffer_state = current_buffer_state ^ mClientStateMask; - } while (!buffer_state_->compare_exchange_weak(current_buffer_state, updated_buffer_state, - std::memory_order_acq_rel, - std::memory_order_acquire)); + updatedBufferState = currentBufferState ^ mClientStateMask; + } while (!mBufferState->compare_exchange_weak(currentBufferState, updatedBufferState, + std::memory_order_acq_rel, + std::memory_order_acquire)); // TODO(b/119837586): Update fence state and return GPU fence. return 0; } int BufferHubBuffer::Release() { - uint32_t current_buffer_state = buffer_state_->load(std::memory_order_acquire); - if (IsClientReleased(current_buffer_state, mClientStateMask)) { + uint32_t currentBufferState = mBufferState->load(std::memory_order_acquire); + if (IsClientReleased(currentBufferState, mClientStateMask)) { ALOGV("%s: Buffer is already released by this client %" PRIx32 ".", __FUNCTION__, mClientStateMask); return 0; } - uint32_t updated_buffer_state = 0U; + uint32_t updatedBufferState = 0U; do { - updated_buffer_state = current_buffer_state & (~mClientStateMask); - } while (!buffer_state_->compare_exchange_weak(current_buffer_state, updated_buffer_state, - std::memory_order_acq_rel, - std::memory_order_acquire)); + updatedBufferState = currentBufferState & (~mClientStateMask); + } while (!mBufferState->compare_exchange_weak(currentBufferState, updatedBufferState, + std::memory_order_acq_rel, + std::memory_order_acquire)); // TODO(b/119837586): Update fence state and return GPU fence if needed. return 0; } bool BufferHubBuffer::IsReleased() const { - return (buffer_state_->load(std::memory_order_acquire) & - active_clients_bit_mask_->load(std::memory_order_acquire)) == 0; + return (mBufferState->load(std::memory_order_acquire) & + mActiveClientsBitMask->load(std::memory_order_acquire)) == 0; } bool BufferHubBuffer::IsValid() const { diff --git a/libs/ui/include/ui/BufferHubBuffer.h b/libs/ui/include/ui/BufferHubBuffer.h index 0b6d75a9cb..06955e4161 100644 --- a/libs/ui/include/ui/BufferHubBuffer.h +++ b/libs/ui/include/ui/BufferHubBuffer.h @@ -59,7 +59,7 @@ public: const BufferHubEventFd& eventFd() const { return mEventFd; } // Returns the current value of MetadataHeader::buffer_state. - uint32_t buffer_state() const { return buffer_state_->load(std::memory_order_acquire); } + uint32_t buffer_state() const { return mBufferState->load(std::memory_order_acquire); } // A state mask which is unique to a buffer hub client among all its siblings sharing the same // concrete graphic buffer. @@ -134,9 +134,9 @@ private: // bufferhubd daemon and all buffer clients. BufferHubMetadata mMetadata; // Shortcuts to the atomics inside the header of mMetadata. - std::atomic* buffer_state_ = nullptr; - std::atomic* fence_state_ = nullptr; - std::atomic* active_clients_bit_mask_ = nullptr; + std::atomic* mBufferState = nullptr; + std::atomic* mFenceState = nullptr; + std::atomic* mActiveClientsBitMask = nullptr; // HwBinder backend sp mBufferClient; diff --git a/libs/ui/tests/BufferHubBuffer_test.cpp b/libs/ui/tests/BufferHubBuffer_test.cpp index 3bcd9353fc..4ad2c4c2ce 100644 --- a/libs/ui/tests/BufferHubBuffer_test.cpp +++ b/libs/ui/tests/BufferHubBuffer_test.cpp @@ -239,8 +239,8 @@ TEST_F(BufferHubBufferStateTransitionTest, GainBuffer_fromReleasedState) { TEST_F(BufferHubBufferStateTransitionTest, GainBuffer_fromGainedState) { ASSERT_EQ(b1->Gain(), 0); - auto current_buffer_state = b1->buffer_state(); - ASSERT_TRUE(IsClientGained(current_buffer_state, b1ClientMask)); + auto currentBufferState = b1->buffer_state(); + ASSERT_TRUE(IsClientGained(currentBufferState, b1ClientMask)); // Gaining from gained state by the same client should not return error. EXPECT_EQ(b1->Gain(), 0); @@ -291,9 +291,9 @@ TEST_F(BufferHubBufferStateTransitionTest, PostBuffer_fromSelfInGainedState) { ASSERT_TRUE(IsClientGained(b1->buffer_state(), b1ClientMask)); EXPECT_EQ(b1->Post(), 0); - auto current_buffer_state = b1->buffer_state(); - EXPECT_TRUE(IsClientReleased(current_buffer_state, b1ClientMask)); - EXPECT_TRUE(IsClientPosted(current_buffer_state, b2ClientMask)); + auto currentBufferState = b1->buffer_state(); + EXPECT_TRUE(IsClientReleased(currentBufferState, b1ClientMask)); + EXPECT_TRUE(IsClientPosted(currentBufferState, b2ClientMask)); } TEST_F(BufferHubBufferStateTransitionTest, PostBuffer_fromPostedState) { @@ -348,8 +348,8 @@ TEST_F(BufferHubBufferStateTransitionTest, AcquireBuffer_fromSelfInAcquiredState ASSERT_EQ(b1->Gain(), 0); ASSERT_EQ(b1->Post(), 0); ASSERT_EQ(b2->Acquire(), 0); - auto current_buffer_state = b1->buffer_state(); - ASSERT_TRUE(IsClientAcquired(current_buffer_state, b2ClientMask)); + auto currentBufferState = b1->buffer_state(); + ASSERT_TRUE(IsClientAcquired(currentBufferState, b2ClientMask)); // Acquiring from acquired state by the same client should not error out. EXPECT_EQ(b2->Acquire(), 0); diff --git a/services/bufferhub/BufferNode.cpp b/services/bufferhub/BufferNode.cpp index 51063903b5..dfa2de0173 100644 --- a/services/bufferhub/BufferNode.cpp +++ b/services/bufferhub/BufferNode.cpp @@ -14,17 +14,16 @@ namespace implementation { void BufferNode::InitializeMetadata() { // Using placement new here to reuse shared memory instead of new allocation // Initialize the atomic variables to zero. - BufferHubDefs::MetadataHeader* metadata_header = metadata_.metadata_header(); - buffer_state_ = new (&metadata_header->buffer_state) std::atomic(0); - fence_state_ = new (&metadata_header->fence_state) std::atomic(0); - active_clients_bit_mask_ = - new (&metadata_header->active_clients_bit_mask) std::atomic(0); + BufferHubDefs::MetadataHeader* metadataHeader = mMetadata.metadata_header(); + mBufferState = new (&metadataHeader->buffer_state) std::atomic(0); + mFenceState = new (&metadataHeader->fence_state) std::atomic(0); + mActiveClientsBitMask = new (&metadataHeader->active_clients_bit_mask) std::atomic(0); // The C++ standard recommends (but does not require) that lock-free atomic operations are // also address-free, that is, suitable for communication between processes using shared // memory. - LOG_ALWAYS_FATAL_IF(!std::atomic_is_lock_free(buffer_state_) || - !std::atomic_is_lock_free(fence_state_) || - !std::atomic_is_lock_free(active_clients_bit_mask_), + LOG_ALWAYS_FATAL_IF(!std::atomic_is_lock_free(mBufferState) || + !std::atomic_is_lock_free(mFenceState) || + !std::atomic_is_lock_free(mActiveClientsBitMask), "Atomic variables in ashmen are not lock free."); } @@ -38,25 +37,25 @@ BufferNode::BufferNode(uint32_t width, uint32_t height, uint32_t layer_count, ui // hardcoded service name "bufferhub". int ret = GraphicBufferAllocator::get().allocate(width, height, format, layer_count, usage, const_cast( - &buffer_handle_), + &mBufferHandle), &out_stride, /*graphicBufferId=*/0, /*requestor=*/"bufferhub"); - if (ret != OK || buffer_handle_ == nullptr) { + if (ret != OK || mBufferHandle == nullptr) { ALOGE("%s: Failed to allocate buffer: %s", __FUNCTION__, strerror(-ret)); return; } - buffer_desc_.width = width; - buffer_desc_.height = height; - buffer_desc_.layers = layer_count; - buffer_desc_.format = format; - buffer_desc_.usage = usage; - buffer_desc_.stride = out_stride; + mBufferDesc.width = width; + mBufferDesc.height = height; + mBufferDesc.layers = layer_count; + mBufferDesc.format = format; + mBufferDesc.usage = usage; + mBufferDesc.stride = out_stride; - metadata_ = BufferHubMetadata::Create(user_metadata_size); - if (!metadata_.IsValid()) { + mMetadata = BufferHubMetadata::Create(user_metadata_size); + if (!mMetadata.IsValid()) { ALOGE("%s: Failed to allocate metadata.", __FUNCTION__); return; } @@ -65,8 +64,8 @@ BufferNode::BufferNode(uint32_t width, uint32_t height, uint32_t layer_count, ui BufferNode::~BufferNode() { // Free the handle - if (buffer_handle_ != nullptr) { - status_t ret = GraphicBufferAllocator::get().free(buffer_handle_); + if (mBufferHandle != nullptr) { + status_t ret = GraphicBufferAllocator::get().free(mBufferHandle); if (ret != OK) { ALOGE("%s: Failed to free handle; Got error: %d", __FUNCTION__, ret); } @@ -79,32 +78,32 @@ BufferNode::~BufferNode() { } uint32_t BufferNode::GetActiveClientsBitMask() const { - return active_clients_bit_mask_->load(std::memory_order_acquire); + return mActiveClientsBitMask->load(std::memory_order_acquire); } uint32_t BufferNode::AddNewActiveClientsBitToMask() { - uint32_t current_active_clients_bit_mask = GetActiveClientsBitMask(); + uint32_t currentActiveClientsBitMask = GetActiveClientsBitMask(); uint32_t client_state_mask = 0U; - uint32_t updated_active_clients_bit_mask = 0U; + uint32_t updatedActiveClientsBitMask = 0U; do { client_state_mask = - BufferHubDefs::FindNextAvailableClientStateMask(current_active_clients_bit_mask); + BufferHubDefs::FindNextAvailableClientStateMask(currentActiveClientsBitMask); if (client_state_mask == 0U) { ALOGE("%s: reached the maximum number of channels per buffer node: %d.", __FUNCTION__, BufferHubDefs::kMaxNumberOfClients); errno = E2BIG; return 0U; } - updated_active_clients_bit_mask = current_active_clients_bit_mask | client_state_mask; - } while (!(active_clients_bit_mask_->compare_exchange_weak(current_active_clients_bit_mask, - updated_active_clients_bit_mask, - std::memory_order_acq_rel, - std::memory_order_acquire))); + updatedActiveClientsBitMask = currentActiveClientsBitMask | client_state_mask; + } while (!(mActiveClientsBitMask->compare_exchange_weak(currentActiveClientsBitMask, + updatedActiveClientsBitMask, + std::memory_order_acq_rel, + std::memory_order_acquire))); return client_state_mask; } void BufferNode::RemoveClientsBitFromMask(const uint32_t& value) { - active_clients_bit_mask_->fetch_and(~value); + mActiveClientsBitMask->fetch_and(~value); } } // namespace implementation diff --git a/services/bufferhub/include/bufferhub/BufferNode.h b/services/bufferhub/include/bufferhub/BufferNode.h index 4729e1cc73..112a21c9af 100644 --- a/services/bufferhub/include/bufferhub/BufferNode.h +++ b/services/bufferhub/include/bufferhub/BufferNode.h @@ -22,36 +22,36 @@ public: ~BufferNode(); // Returns whether the object holds a valid metadata. - bool IsValid() const { return metadata_.IsValid(); } + bool IsValid() const { return mMetadata.IsValid(); } int id() const { return mId; } - size_t user_metadata_size() const { return metadata_.user_metadata_size(); } + size_t user_metadata_size() const { return mMetadata.user_metadata_size(); } // Accessors of the buffer description and handle - const native_handle_t* buffer_handle() const { return buffer_handle_; } - const AHardwareBuffer_Desc& buffer_desc() const { return buffer_desc_; } + const native_handle_t* buffer_handle() const { return mBufferHandle; } + const AHardwareBuffer_Desc& buffer_desc() const { return mBufferDesc; } // Accessor of event fd. const BufferHubEventFd& eventFd() const { return mEventFd; } - // Accessors of metadata. - const BufferHubMetadata& metadata() const { return metadata_; } + // Accessors of mMetadata. + const BufferHubMetadata& metadata() const { return mMetadata; } - // Gets the current value of active_clients_bit_mask in metadata_ with + // Gets the current value of mActiveClientsBitMask in mMetadata with // std::memory_order_acquire, so that all previous releases of - // active_clients_bit_mask from all threads will be returned here. + // mActiveClientsBitMask from all threads will be returned here. uint32_t GetActiveClientsBitMask() const; - // Find and add a new client_state_mask to active_clients_bit_mask in - // metadata_. - // Return the new client_state_mask that is added to active_clients_bit_mask. + // Find and add a new client state mask to mActiveClientsBitMask in + // mMetadata. + // Return the new client state mask that is added to mActiveClientsBitMask. // Return 0U if there are already 16 clients of the buffer. uint32_t AddNewActiveClientsBitToMask(); - // Removes the value from active_clients_bit_mask in metadata_ with + // Removes the value from active_clients_bit_mask in mMetadata with // std::memory_order_release, so that the change will be visible to any - // acquire of active_clients_bit_mask_ in any threads after the succeed of + // acquire of mActiveClientsBitMask in any threads after the succeed of // this operation. void RemoveClientsBitFromMask(const uint32_t& value); @@ -61,34 +61,34 @@ private: void InitializeMetadata(); // Gralloc buffer handles. - native_handle_t* buffer_handle_; - AHardwareBuffer_Desc buffer_desc_; + native_handle_t* mBufferHandle; + AHardwareBuffer_Desc mBufferDesc; // Eventfd used for signalling buffer events among the clients of the buffer. BufferHubEventFd mEventFd; // Metadata in shared memory. - BufferHubMetadata metadata_; + BufferHubMetadata mMetadata; // A system-unique id generated by bufferhub from 0 to std::numeric_limits::max(). // BufferNodes not created by bufferhub will have id < 0, meaning "not specified". // TODO(b/118891412): remove default id = -1 and update comments after pdx is no longer in use const int mId = -1; - // The following variables are atomic variables in metadata_ that are visible + // The following variables are atomic variables in mMetadata that are visible // to Bn object and Bp objects. Please find more info in // BufferHubDefs::MetadataHeader. - // buffer_state_ tracks the state of the buffer. Buffer can be in one of these + // mBufferState tracks the state of the buffer. Buffer can be in one of these // four states: gained, posted, acquired, released. - std::atomic* buffer_state_ = nullptr; + std::atomic* mBufferState = nullptr; - // TODO(b/112012161): add comments to fence_state_. - std::atomic* fence_state_ = nullptr; + // TODO(b/112012161): add comments to mFenceState. + std::atomic* mFenceState = nullptr; - // active_clients_bit_mask_ tracks all the bp clients of the buffer. It is the + // mActiveClientsBitMask tracks all the bp clients of the buffer. It is the // union of all client_state_mask of all bp clients. - std::atomic* active_clients_bit_mask_ = nullptr; + std::atomic* mActiveClientsBitMask = nullptr; }; } // namespace implementation -- cgit v1.2.3-59-g8ed1b From 727ede4a0fd4fbc6621833b21579da255eb1fb68 Mon Sep 17 00:00:00 2001 From: Tianyu Jiang Date: Fri, 1 Feb 2019 11:44:51 -0800 Subject: Fix non camelCase function names Android Framework C++ Code Style Guidelines says that function names should be camelCase: http://go/droidcppstyle Test: m, mma in frameworks/native Bug: 68273829 Change-Id: I2f661c06b31b2e72cd0eee3d91b95531b60ec939 --- libs/ui/BufferHubBuffer.cpp | 62 ++--- libs/ui/BufferHubMetadata.cpp | 8 +- libs/ui/GraphicBuffer.cpp | 2 +- libs/ui/include/ui/BufferHubBuffer.h | 28 +-- libs/ui/include/ui/BufferHubDefs.h | 16 +- libs/ui/include/ui/BufferHubMetadata.h | 14 +- libs/ui/tests/BufferHubBuffer_test.cpp | 272 ++++++++++----------- libs/ui/tests/BufferHubMetadata_test.cpp | 101 ++++---- libs/ui/tests/GraphicBuffer_test.cpp | 8 +- libs/vr/libbufferhub/buffer_hub-test.cpp | 46 ++-- libs/vr/libbufferhub/consumer_buffer.cpp | 6 +- .../include/private/dvr/buffer_hub_defs.h | 32 +-- libs/vr/libbufferhub/producer_buffer.cpp | 20 +- .../libbufferhubqueue/buffer_hub_queue_client.cpp | 4 +- services/bufferhub/BufferHubService.cpp | 22 +- services/bufferhub/BufferNode.cpp | 20 +- services/bufferhub/include/bufferhub/BufferNode.h | 16 +- services/bufferhub/tests/BufferNode_test.cpp | 47 ++-- services/vr/bufferhubd/producer_channel.cpp | 22 +- services/vr/bufferhubd/producer_queue_channel.cpp | 2 +- 20 files changed, 373 insertions(+), 375 deletions(-) (limited to 'libs/ui/BufferHubBuffer.cpp') diff --git a/libs/ui/BufferHubBuffer.cpp b/libs/ui/BufferHubBuffer.cpp index c3144b924d..7693fcbfe0 100644 --- a/libs/ui/BufferHubBuffer.cpp +++ b/libs/ui/BufferHubBuffer.cpp @@ -24,12 +24,12 @@ #include using ::android::base::unique_fd; -using ::android::BufferHubDefs::AnyClientAcquired; -using ::android::BufferHubDefs::AnyClientGained; -using ::android::BufferHubDefs::IsClientAcquired; -using ::android::BufferHubDefs::IsClientGained; -using ::android::BufferHubDefs::IsClientPosted; -using ::android::BufferHubDefs::IsClientReleased; +using ::android::BufferHubDefs::isAnyClientAcquired; +using ::android::BufferHubDefs::isAnyClientGained; +using ::android::BufferHubDefs::isClientAcquired; +using ::android::BufferHubDefs::isClientGained; +using ::android::BufferHubDefs::isClientPosted; +using ::android::BufferHubDefs::isClientReleased; using ::android::frameworks::bufferhub::V1_0::BufferHubStatus; using ::android::frameworks::bufferhub::V1_0::BufferTraits; using ::android::frameworks::bufferhub::V1_0::IBufferClient; @@ -39,22 +39,22 @@ using ::android::hardware::graphics::common::V1_2::HardwareBufferDescription; namespace android { -std::unique_ptr BufferHubBuffer::Create(uint32_t width, uint32_t height, +std::unique_ptr BufferHubBuffer::create(uint32_t width, uint32_t height, uint32_t layerCount, uint32_t format, uint64_t usage, size_t userMetadataSize) { auto buffer = std::unique_ptr( new BufferHubBuffer(width, height, layerCount, format, usage, userMetadataSize)); - return buffer->IsValid() ? std::move(buffer) : nullptr; + return buffer->isValid() ? std::move(buffer) : nullptr; } -std::unique_ptr BufferHubBuffer::Import(const native_handle_t* token) { +std::unique_ptr BufferHubBuffer::import(const native_handle_t* token) { if (token == nullptr) { ALOGE("%s: token cannot be nullptr!", __FUNCTION__); return nullptr; } auto buffer = std::unique_ptr(new BufferHubBuffer(token)); - return buffer->IsValid() ? std::move(buffer) : nullptr; + return buffer->isValid() ? std::move(buffer) : nullptr; } BufferHubBuffer::BufferHubBuffer(uint32_t width, uint32_t height, uint32_t layerCount, @@ -169,8 +169,8 @@ int BufferHubBuffer::initWithBufferTraits(const BufferTraits& bufferTraits) { // Import fds. Dup fds because hidl_handle owns the fds. unique_fd ashmemFd(fcntl(bufferTraits.bufferInfo->data[0], F_DUPFD_CLOEXEC, 0)); - mMetadata = BufferHubMetadata::Import(std::move(ashmemFd)); - if (!mMetadata.IsValid()) { + mMetadata = BufferHubMetadata::import(std::move(ashmemFd)); + if (!mMetadata.isValid()) { ALOGE("%s: Received an invalid metadata.", __FUNCTION__); return -EINVAL; } @@ -196,20 +196,20 @@ int BufferHubBuffer::initWithBufferTraits(const BufferTraits& bufferTraits) { uint32_t userMetadataSize; memcpy(&userMetadataSize, &bufferTraits.bufferInfo->data[4], sizeof(userMetadataSize)); - if (mMetadata.user_metadata_size() != userMetadataSize) { + if (mMetadata.userMetadataSize() != userMetadataSize) { ALOGE("%s: user metadata size not match: expected %u, actual %zu.", __FUNCTION__, - userMetadataSize, mMetadata.user_metadata_size()); + userMetadataSize, mMetadata.userMetadataSize()); return -EINVAL; } - size_t metadataSize = static_cast(mMetadata.metadata_size()); + size_t metadataSize = static_cast(mMetadata.metadataSize()); if (metadataSize < BufferHubDefs::kMetadataHeaderSize) { ALOGE("%s: metadata too small: %zu", __FUNCTION__, metadataSize); return -EINVAL; } // Populate shortcuts to the atomics in metadata. - auto metadata_header = mMetadata.metadata_header(); + auto metadata_header = mMetadata.metadataHeader(); mBufferState = &metadata_header->buffer_state; mFenceState = &metadata_header->fence_state; mActiveClientsBitMask = &metadata_header->active_clients_bit_mask; @@ -235,16 +235,16 @@ int BufferHubBuffer::initWithBufferTraits(const BufferTraits& bufferTraits) { return 0; } -int BufferHubBuffer::Gain() { +int BufferHubBuffer::gain() { uint32_t currentBufferState = mBufferState->load(std::memory_order_acquire); - if (IsClientGained(currentBufferState, mClientStateMask)) { + if (isClientGained(currentBufferState, mClientStateMask)) { ALOGV("%s: Buffer is already gained by this client %" PRIx32 ".", __FUNCTION__, mClientStateMask); return 0; } do { - if (AnyClientGained(currentBufferState & (~mClientStateMask)) || - AnyClientAcquired(currentBufferState)) { + if (isAnyClientGained(currentBufferState & (~mClientStateMask)) || + isAnyClientAcquired(currentBufferState)) { ALOGE("%s: Buffer is in use, id=%d mClientStateMask=%" PRIx32 " state=%" PRIx32 ".", __FUNCTION__, mId, mClientStateMask, currentBufferState); return -EBUSY; @@ -258,11 +258,11 @@ int BufferHubBuffer::Gain() { return 0; } -int BufferHubBuffer::Post() { +int BufferHubBuffer::post() { uint32_t currentBufferState = mBufferState->load(std::memory_order_acquire); uint32_t updatedBufferState = (~mClientStateMask) & BufferHubDefs::kHighBitsMask; do { - if (!IsClientGained(currentBufferState, mClientStateMask)) { + if (!isClientGained(currentBufferState, mClientStateMask)) { ALOGE("%s: Cannot post a buffer that is not gained by this client. buffer_id=%d " "mClientStateMask=%" PRIx32 " state=%" PRIx32 ".", __FUNCTION__, mId, mClientStateMask, currentBufferState); @@ -277,16 +277,16 @@ int BufferHubBuffer::Post() { return 0; } -int BufferHubBuffer::Acquire() { +int BufferHubBuffer::acquire() { uint32_t currentBufferState = mBufferState->load(std::memory_order_acquire); - if (IsClientAcquired(currentBufferState, mClientStateMask)) { + if (isClientAcquired(currentBufferState, mClientStateMask)) { ALOGV("%s: Buffer is already acquired by this client %" PRIx32 ".", __FUNCTION__, mClientStateMask); return 0; } uint32_t updatedBufferState = 0U; do { - if (!IsClientPosted(currentBufferState, mClientStateMask)) { + if (!isClientPosted(currentBufferState, mClientStateMask)) { ALOGE("%s: Cannot acquire a buffer that is not in posted state. buffer_id=%d " "mClientStateMask=%" PRIx32 " state=%" PRIx32 ".", __FUNCTION__, mId, mClientStateMask, currentBufferState); @@ -301,9 +301,9 @@ int BufferHubBuffer::Acquire() { return 0; } -int BufferHubBuffer::Release() { +int BufferHubBuffer::release() { uint32_t currentBufferState = mBufferState->load(std::memory_order_acquire); - if (IsClientReleased(currentBufferState, mClientStateMask)) { + if (isClientReleased(currentBufferState, mClientStateMask)) { ALOGV("%s: Buffer is already released by this client %" PRIx32 ".", __FUNCTION__, mClientStateMask); return 0; @@ -318,17 +318,17 @@ int BufferHubBuffer::Release() { return 0; } -bool BufferHubBuffer::IsReleased() const { +bool BufferHubBuffer::isReleased() const { return (mBufferState->load(std::memory_order_acquire) & mActiveClientsBitMask->load(std::memory_order_acquire)) == 0; } -bool BufferHubBuffer::IsValid() const { +bool BufferHubBuffer::isValid() const { return mBufferHandle.getNativeHandle() != nullptr && mId >= 0 && mClientStateMask != 0U && - mEventFd.get() >= 0 && mMetadata.IsValid() && mBufferClient != nullptr; + mEventFd.get() >= 0 && mMetadata.isValid() && mBufferClient != nullptr; } -native_handle_t* BufferHubBuffer::Duplicate() { +native_handle_t* BufferHubBuffer::duplicate() { if (mBufferClient == nullptr) { ALOGE("%s: missing BufferClient!", __FUNCTION__); return nullptr; diff --git a/libs/ui/BufferHubMetadata.cpp b/libs/ui/BufferHubMetadata.cpp index 816707db9d..05bc7ddfbe 100644 --- a/libs/ui/BufferHubMetadata.cpp +++ b/libs/ui/BufferHubMetadata.cpp @@ -34,7 +34,7 @@ using BufferHubDefs::kMetadataHeaderSize; using BufferHubDefs::MetadataHeader; /* static */ -BufferHubMetadata BufferHubMetadata::Create(size_t userMetadataSize) { +BufferHubMetadata BufferHubMetadata::create(size_t userMetadataSize) { // The size the of metadata buffer is used as the "width" parameter during allocation. Thus it // cannot overflow uint32_t. if (userMetadataSize >= (std::numeric_limits::max() - kMetadataHeaderSize)) { @@ -59,11 +59,11 @@ BufferHubMetadata BufferHubMetadata::Create(size_t userMetadataSize) { return {}; } - return BufferHubMetadata::Import(std::move(ashmemFd)); + return BufferHubMetadata::import(std::move(ashmemFd)); } /* static */ -BufferHubMetadata BufferHubMetadata::Import(unique_fd ashmemFd) { +BufferHubMetadata BufferHubMetadata::import(unique_fd ashmemFd) { if (!ashmem_valid(ashmemFd.get())) { ALOGE("BufferHubMetadata::Import: invalid ashmem fd."); return {}; @@ -94,7 +94,7 @@ BufferHubMetadata::BufferHubMetadata(size_t userMetadataSize, unique_fd ashmemFd BufferHubMetadata::~BufferHubMetadata() { if (mMetadataHeader != nullptr) { - int ret = munmap(mMetadataHeader, metadata_size()); + int ret = munmap(mMetadataHeader, metadataSize()); ALOGE_IF(ret != 0, "BufferHubMetadata::~BufferHubMetadata: failed to unmap ashmem, error=%d.", errno); mMetadataHeader = nullptr; diff --git a/libs/ui/GraphicBuffer.cpp b/libs/ui/GraphicBuffer.cpp index 41ae2531cd..f487dfa81b 100644 --- a/libs/ui/GraphicBuffer.cpp +++ b/libs/ui/GraphicBuffer.cpp @@ -100,7 +100,7 @@ GraphicBuffer::GraphicBuffer(std::unique_ptr buffer) : GraphicB return; } - mInitCheck = initWithHandle(buffer->DuplicateHandle(), /*method=*/TAKE_UNREGISTERED_HANDLE, + mInitCheck = initWithHandle(buffer->duplicateHandle(), /*method=*/TAKE_UNREGISTERED_HANDLE, buffer->desc().width, buffer->desc().height, static_cast(buffer->desc().format), buffer->desc().layers, buffer->desc().usage, buffer->desc().stride); diff --git a/libs/ui/include/ui/BufferHubBuffer.h b/libs/ui/include/ui/BufferHubBuffer.h index 06955e4161..eac8c8437b 100644 --- a/libs/ui/include/ui/BufferHubBuffer.h +++ b/libs/ui/include/ui/BufferHubBuffer.h @@ -29,14 +29,14 @@ namespace android { class BufferHubBuffer { public: // Allocates a standalone BufferHubBuffer. - static std::unique_ptr Create(uint32_t width, uint32_t height, + static std::unique_ptr create(uint32_t width, uint32_t height, uint32_t layerCount, uint32_t format, uint64_t usage, size_t userMetadataSize); // Imports the given token to a BufferHubBuffer. Not taking ownership of the token. Caller // should close and destroy the token after calling this function regardless of output. // TODO(b/122543147): use a movable wrapper for token - static std::unique_ptr Import(const native_handle_t* token); + static std::unique_ptr import(const native_handle_t* token); BufferHubBuffer(const BufferHubBuffer&) = delete; void operator=(const BufferHubBuffer&) = delete; @@ -52,51 +52,51 @@ public: // Duplicate the underlying Gralloc buffer handle. Caller is responsible to free the handle // after use. - native_handle_t* DuplicateHandle() { + native_handle_t* duplicateHandle() { return native_handle_clone(mBufferHandle.getNativeHandle()); } const BufferHubEventFd& eventFd() const { return mEventFd; } // Returns the current value of MetadataHeader::buffer_state. - uint32_t buffer_state() const { return mBufferState->load(std::memory_order_acquire); } + uint32_t bufferState() const { return mBufferState->load(std::memory_order_acquire); } // A state mask which is unique to a buffer hub client among all its siblings sharing the same // concrete graphic buffer. - uint32_t client_state_mask() const { return mClientStateMask; } + uint32_t clientStateMask() const { return mClientStateMask; } - size_t user_metadata_size() const { return mMetadata.user_metadata_size(); } + size_t userMetadataSize() const { return mMetadata.userMetadataSize(); } // Returns true if the BufferClient is still alive. - bool IsConnected() const { return mBufferClient->ping().isOk(); } + bool isConnected() const { return mBufferClient->ping().isOk(); } // Returns true if the buffer is valid: non-null buffer handle, valid id, valid client bit mask, // valid metadata and valid buffer client - bool IsValid() const; + bool isValid() const; // Gains the buffer for exclusive write permission. Read permission is implied once a buffer is // gained. // The buffer can be gained as long as there is no other client in acquired or gained state. - int Gain(); + int gain(); // Posts the gained buffer for other buffer clients to use the buffer. // The buffer can be posted iff the buffer state for this client is gained. // After posting the buffer, this client is put to released state and does not have access to // the buffer for this cycle of the usage of the buffer. - int Post(); + int post(); // Acquires the buffer for shared read permission. // The buffer can be acquired iff the buffer state for this client is posted. - int Acquire(); + int acquire(); // Releases the buffer. // The buffer can be released from any buffer state. // After releasing the buffer, this client no longer have any permissions to the buffer for the // current cycle of the usage of the buffer. - int Release(); + int release(); // Returns whether the buffer is released by all active clients or not. - bool IsReleased() const; + bool isReleased() const; // Creates a token that stands for this BufferHubBuffer client and could be used for Import to // create another BufferHubBuffer. The new BufferHubBuffer will share the same underlying @@ -104,7 +104,7 @@ public: // should free it after use. // Returns a valid token on success, nullptr on failure. // TODO(b/122543147): use a movable wrapper for token - native_handle_t* Duplicate(); + native_handle_t* duplicate(); private: BufferHubBuffer(uint32_t width, uint32_t height, uint32_t layerCount, uint32_t format, diff --git a/libs/ui/include/ui/BufferHubDefs.h b/libs/ui/include/ui/BufferHubDefs.h index ff970cbd64..722a060597 100644 --- a/libs/ui/include/ui/BufferHubDefs.h +++ b/libs/ui/include/ui/BufferHubDefs.h @@ -63,19 +63,19 @@ static constexpr uint32_t kHighBitsMask = ~kLowbitsMask; static constexpr uint32_t kFirstClientBitMask = (1U << kMaxNumberOfClients) + 1U; // Returns true if any of the client is in gained state. -static inline bool AnyClientGained(uint32_t state) { +static inline bool isAnyClientGained(uint32_t state) { uint32_t high_bits = state >> kMaxNumberOfClients; uint32_t low_bits = state & kLowbitsMask; return high_bits == low_bits && low_bits != 0U; } // Returns true if the input client is in gained state. -static inline bool IsClientGained(uint32_t state, uint32_t client_bit_mask) { +static inline bool isClientGained(uint32_t state, uint32_t client_bit_mask) { return state == client_bit_mask; } // Returns true if any of the client is in posted state. -static inline bool AnyClientPosted(uint32_t state) { +static inline bool isAnyClientPosted(uint32_t state) { uint32_t high_bits = state >> kMaxNumberOfClients; uint32_t low_bits = state & kLowbitsMask; uint32_t posted_or_acquired = high_bits ^ low_bits; @@ -83,7 +83,7 @@ static inline bool AnyClientPosted(uint32_t state) { } // Returns true if the input client is in posted state. -static inline bool IsClientPosted(uint32_t state, uint32_t client_bit_mask) { +static inline bool isClientPosted(uint32_t state, uint32_t client_bit_mask) { uint32_t client_bits = state & client_bit_mask; if (client_bits == 0U) return false; uint32_t low_bits = client_bits & kLowbitsMask; @@ -91,7 +91,7 @@ static inline bool IsClientPosted(uint32_t state, uint32_t client_bit_mask) { } // Return true if any of the client is in acquired state. -static inline bool AnyClientAcquired(uint32_t state) { +static inline bool isAnyClientAcquired(uint32_t state) { uint32_t high_bits = state >> kMaxNumberOfClients; uint32_t low_bits = state & kLowbitsMask; uint32_t posted_or_acquired = high_bits ^ low_bits; @@ -99,7 +99,7 @@ static inline bool AnyClientAcquired(uint32_t state) { } // Return true if the input client is in acquired state. -static inline bool IsClientAcquired(uint32_t state, uint32_t client_bit_mask) { +static inline bool isClientAcquired(uint32_t state, uint32_t client_bit_mask) { uint32_t client_bits = state & client_bit_mask; if (client_bits == 0U) return false; uint32_t high_bits = client_bits & kHighBitsMask; @@ -107,13 +107,13 @@ static inline bool IsClientAcquired(uint32_t state, uint32_t client_bit_mask) { } // Returns true if the input client is in released state. -static inline bool IsClientReleased(uint32_t state, uint32_t client_bit_mask) { +static inline bool isClientReleased(uint32_t state, uint32_t client_bit_mask) { return (state & client_bit_mask) == 0U; } // Returns the next available buffer client's client_state_masks. // @params union_bits. Union of all existing clients' client_state_masks. -static inline uint32_t FindNextAvailableClientStateMask(uint32_t union_bits) { +static inline uint32_t findNextAvailableClientStateMask(uint32_t union_bits) { uint32_t low_union = union_bits & kLowbitsMask; if (low_union == kLowbitsMask) return 0U; uint32_t incremented = low_union + 1U; diff --git a/libs/ui/include/ui/BufferHubMetadata.h b/libs/ui/include/ui/BufferHubMetadata.h index 212189497a..3482507399 100644 --- a/libs/ui/include/ui/BufferHubMetadata.h +++ b/libs/ui/include/ui/BufferHubMetadata.h @@ -33,12 +33,12 @@ public: // @param userMetadataSize Size in bytes of the user defined metadata. The entire metadata // shared memory region to be allocated is the size of canonical // BufferHubDefs::MetadataHeader plus userMetadataSize. - static BufferHubMetadata Create(size_t userMetadataSize); + static BufferHubMetadata create(size_t userMetadataSize); // Imports an existing BufferHubMetadata from an ashmem FD. // // @param ashmemFd Ashmem file descriptor representing an ashmem region. - static BufferHubMetadata Import(unique_fd ashmemFd); + static BufferHubMetadata import(unique_fd ashmemFd); BufferHubMetadata() = default; @@ -63,13 +63,13 @@ public: // Returns true if the metadata is valid, i.e. the metadata has a valid ashmem fd and the ashmem // has been mapped into virtual address space. - bool IsValid() const { return mAshmemFd.get() != -1 && mMetadataHeader != nullptr; } + bool isValid() const { return mAshmemFd.get() != -1 && mMetadataHeader != nullptr; } - size_t user_metadata_size() const { return mUserMetadataSize; } - size_t metadata_size() const { return mUserMetadataSize + BufferHubDefs::kMetadataHeaderSize; } + size_t userMetadataSize() const { return mUserMetadataSize; } + size_t metadataSize() const { return mUserMetadataSize + BufferHubDefs::kMetadataHeaderSize; } - const unique_fd& ashmem_fd() const { return mAshmemFd; } - BufferHubDefs::MetadataHeader* metadata_header() { return mMetadataHeader; } + const unique_fd& ashmemFd() const { return mAshmemFd; } + BufferHubDefs::MetadataHeader* metadataHeader() { return mMetadataHeader; } private: BufferHubMetadata(size_t userMetadataSize, unique_fd ashmemFd, diff --git a/libs/ui/tests/BufferHubBuffer_test.cpp b/libs/ui/tests/BufferHubBuffer_test.cpp index 4ad2c4c2ce..3087a90787 100644 --- a/libs/ui/tests/BufferHubBuffer_test.cpp +++ b/libs/ui/tests/BufferHubBuffer_test.cpp @@ -32,13 +32,13 @@ namespace android { namespace { -using ::android::BufferHubDefs::AnyClientAcquired; -using ::android::BufferHubDefs::AnyClientGained; -using ::android::BufferHubDefs::AnyClientPosted; -using ::android::BufferHubDefs::IsClientAcquired; -using ::android::BufferHubDefs::IsClientGained; -using ::android::BufferHubDefs::IsClientPosted; -using ::android::BufferHubDefs::IsClientReleased; +using ::android::BufferHubDefs::isAnyClientAcquired; +using ::android::BufferHubDefs::isAnyClientGained; +using ::android::BufferHubDefs::isAnyClientPosted; +using ::android::BufferHubDefs::isClientAcquired; +using ::android::BufferHubDefs::isClientGained; +using ::android::BufferHubDefs::isClientPosted; +using ::android::BufferHubDefs::isClientReleased; using ::android::BufferHubDefs::kMetadataHeaderSize; using ::testing::IsNull; using ::testing::NotNull; @@ -81,88 +81,88 @@ private: }; void BufferHubBufferStateTransitionTest::CreateTwoClientsOfABuffer() { - b1 = BufferHubBuffer::Create(kWidth, kHeight, kLayerCount, kFormat, kUsage, kUserMetadataSize); + b1 = BufferHubBuffer::create(kWidth, kHeight, kLayerCount, kFormat, kUsage, kUserMetadataSize); ASSERT_THAT(b1, NotNull()); - b1ClientMask = b1->client_state_mask(); + b1ClientMask = b1->clientStateMask(); ASSERT_NE(b1ClientMask, 0U); - native_handle_t* token = b1->Duplicate(); + native_handle_t* token = b1->duplicate(); ASSERT_THAT(token, NotNull()); // TODO(b/122543147): use a movalbe wrapper for token - b2 = BufferHubBuffer::Import(token); + b2 = BufferHubBuffer::import(token); native_handle_close(token); native_handle_delete(token); ASSERT_THAT(b2, NotNull()); - b2ClientMask = b2->client_state_mask(); + b2ClientMask = b2->clientStateMask(); ASSERT_NE(b2ClientMask, 0U); ASSERT_NE(b2ClientMask, b1ClientMask); } TEST_F(BufferHubBufferTest, CreateBufferFails) { // Buffer Creation will fail: BLOB format requires height to be 1. - auto b1 = BufferHubBuffer::Create(kWidth, /*height=*/2, kLayerCount, + auto b1 = BufferHubBuffer::create(kWidth, /*height=*/2, kLayerCount, /*format=*/HAL_PIXEL_FORMAT_BLOB, kUsage, kUserMetadataSize); EXPECT_THAT(b1, IsNull()); // Buffer Creation will fail: user metadata size too large. - auto b2 = BufferHubBuffer::Create(kWidth, kHeight, kLayerCount, kFormat, kUsage, + auto b2 = BufferHubBuffer::create(kWidth, kHeight, kLayerCount, kFormat, kUsage, /*userMetadataSize=*/std::numeric_limits::max()); EXPECT_THAT(b2, IsNull()); // Buffer Creation will fail: user metadata size too large. const size_t userMetadataSize = std::numeric_limits::max() - kMetadataHeaderSize; - auto b3 = BufferHubBuffer::Create(kWidth, kHeight, kLayerCount, kFormat, kUsage, + auto b3 = BufferHubBuffer::create(kWidth, kHeight, kLayerCount, kFormat, kUsage, userMetadataSize); EXPECT_THAT(b3, IsNull()); } TEST_F(BufferHubBufferTest, CreateBuffer) { - auto b1 = BufferHubBuffer::Create(kWidth, kHeight, kLayerCount, kFormat, kUsage, + auto b1 = BufferHubBuffer::create(kWidth, kHeight, kLayerCount, kFormat, kUsage, kUserMetadataSize); ASSERT_THAT(b1, NotNull()); - EXPECT_TRUE(b1->IsConnected()); - EXPECT_TRUE(b1->IsValid()); + EXPECT_TRUE(b1->isConnected()); + EXPECT_TRUE(b1->isValid()); EXPECT_TRUE(cmpAHardwareBufferDesc(b1->desc(), kDesc)); - EXPECT_EQ(b1->user_metadata_size(), kUserMetadataSize); + EXPECT_EQ(b1->userMetadataSize(), kUserMetadataSize); } TEST_F(BufferHubBufferTest, DuplicateAndImportBuffer) { - auto b1 = BufferHubBuffer::Create(kWidth, kHeight, kLayerCount, kFormat, kUsage, + auto b1 = BufferHubBuffer::create(kWidth, kHeight, kLayerCount, kFormat, kUsage, kUserMetadataSize); ASSERT_THAT(b1, NotNull()); - EXPECT_TRUE(b1->IsValid()); + EXPECT_TRUE(b1->isValid()); - native_handle_t* token = b1->Duplicate(); + native_handle_t* token = b1->duplicate(); EXPECT_TRUE(token); // The detached buffer should still be valid. - EXPECT_TRUE(b1->IsConnected()); - EXPECT_TRUE(b1->IsValid()); + EXPECT_TRUE(b1->isConnected()); + EXPECT_TRUE(b1->isValid()); - std::unique_ptr b2 = BufferHubBuffer::Import(token); + std::unique_ptr b2 = BufferHubBuffer::import(token); native_handle_close(token); native_handle_delete(token); ASSERT_THAT(b2, NotNull()); - EXPECT_TRUE(b2->IsValid()); + EXPECT_TRUE(b2->isValid()); EXPECT_TRUE(cmpAHardwareBufferDesc(b1->desc(), b2->desc())); - EXPECT_EQ(b1->user_metadata_size(), b2->user_metadata_size()); + EXPECT_EQ(b1->userMetadataSize(), b2->userMetadataSize()); // These two buffer instances are based on the same physical buffer under the // hood, so they should share the same id. EXPECT_EQ(b1->id(), b2->id()); - // We use client_state_mask() to tell those two instances apart. - EXPECT_NE(b1->client_state_mask(), b2->client_state_mask()); + // We use clientStateMask() to tell those two instances apart. + EXPECT_NE(b1->clientStateMask(), b2->clientStateMask()); // Both buffer instances should be in released state currently. - EXPECT_TRUE(b1->IsReleased()); - EXPECT_TRUE(b2->IsReleased()); + EXPECT_TRUE(b1->isReleased()); + EXPECT_TRUE(b2->isReleased()); // The event fd should behave like duped event fds. const BufferHubEventFd& eventFd1 = b1->eventFd(); @@ -192,19 +192,19 @@ TEST_F(BufferHubBufferTest, DuplicateAndImportBuffer) { } TEST_F(BufferHubBufferTest, ImportFreedBuffer) { - auto b1 = BufferHubBuffer::Create(kWidth, kHeight, kLayerCount, kFormat, kUsage, + auto b1 = BufferHubBuffer::create(kWidth, kHeight, kLayerCount, kFormat, kUsage, kUserMetadataSize); ASSERT_THAT(b1, NotNull()); - EXPECT_TRUE(b1->IsValid()); + EXPECT_TRUE(b1->isValid()); - native_handle_t* token = b1->Duplicate(); + native_handle_t* token = b1->duplicate(); EXPECT_TRUE(token); // Explicitly destroy b1. Backend buffer should be freed and token becomes invalid b1.reset(); // TODO(b/122543147): use a movalbe wrapper for token - std::unique_ptr b2 = BufferHubBuffer::Import(token); + std::unique_ptr b2 = BufferHubBuffer::import(token); native_handle_close(token); native_handle_delete(token); @@ -214,7 +214,7 @@ TEST_F(BufferHubBufferTest, ImportFreedBuffer) { // nullptr must not crash the service TEST_F(BufferHubBufferTest, ImportNullToken) { - auto b1 = BufferHubBuffer::Import(nullptr); + auto b1 = BufferHubBuffer::import(nullptr); EXPECT_THAT(b1, IsNull()); } @@ -222,185 +222,185 @@ TEST_F(BufferHubBufferTest, ImportInvalidToken) { native_handle_t* token = native_handle_create(/*numFds=*/0, /*numInts=*/1); token->data[0] = 0; - auto b1 = BufferHubBuffer::Import(token); + auto b1 = BufferHubBuffer::import(token); native_handle_delete(token); EXPECT_THAT(b1, IsNull()); } TEST_F(BufferHubBufferStateTransitionTest, GainBuffer_fromReleasedState) { - ASSERT_TRUE(b1->IsReleased()); + ASSERT_TRUE(b1->isReleased()); // Successful gaining the buffer should change the buffer state bit of b1 to // gained state, other client state bits to released state. - EXPECT_EQ(b1->Gain(), 0); - EXPECT_TRUE(IsClientGained(b1->buffer_state(), b1ClientMask)); + EXPECT_EQ(b1->gain(), 0); + EXPECT_TRUE(isClientGained(b1->bufferState(), b1ClientMask)); } TEST_F(BufferHubBufferStateTransitionTest, GainBuffer_fromGainedState) { - ASSERT_EQ(b1->Gain(), 0); - auto currentBufferState = b1->buffer_state(); - ASSERT_TRUE(IsClientGained(currentBufferState, b1ClientMask)); + ASSERT_EQ(b1->gain(), 0); + auto currentBufferState = b1->bufferState(); + ASSERT_TRUE(isClientGained(currentBufferState, b1ClientMask)); // Gaining from gained state by the same client should not return error. - EXPECT_EQ(b1->Gain(), 0); + EXPECT_EQ(b1->gain(), 0); // Gaining from gained state by another client should return error. - EXPECT_EQ(b2->Gain(), -EBUSY); + EXPECT_EQ(b2->gain(), -EBUSY); } TEST_F(BufferHubBufferStateTransitionTest, GainBuffer_fromAcquiredState) { - ASSERT_EQ(b1->Gain(), 0); - ASSERT_EQ(b1->Post(), 0); - ASSERT_EQ(b2->Acquire(), 0); - ASSERT_TRUE(AnyClientAcquired(b1->buffer_state())); + ASSERT_EQ(b1->gain(), 0); + ASSERT_EQ(b1->post(), 0); + ASSERT_EQ(b2->acquire(), 0); + ASSERT_TRUE(isAnyClientAcquired(b1->bufferState())); // Gaining from acquired state should fail. - EXPECT_EQ(b1->Gain(), -EBUSY); - EXPECT_EQ(b2->Gain(), -EBUSY); + EXPECT_EQ(b1->gain(), -EBUSY); + EXPECT_EQ(b2->gain(), -EBUSY); } TEST_F(BufferHubBufferStateTransitionTest, GainBuffer_fromOtherClientInPostedState) { - ASSERT_EQ(b1->Gain(), 0); - ASSERT_EQ(b1->Post(), 0); - ASSERT_TRUE(AnyClientPosted(b1->buffer_state())); + ASSERT_EQ(b1->gain(), 0); + ASSERT_EQ(b1->post(), 0); + ASSERT_TRUE(isAnyClientPosted(b1->bufferState())); // Gaining a buffer who has other posted client should succeed. - EXPECT_EQ(b1->Gain(), 0); + EXPECT_EQ(b1->gain(), 0); } TEST_F(BufferHubBufferStateTransitionTest, GainBuffer_fromSelfInPostedState) { - ASSERT_EQ(b1->Gain(), 0); - ASSERT_EQ(b1->Post(), 0); - ASSERT_TRUE(AnyClientPosted(b1->buffer_state())); + ASSERT_EQ(b1->gain(), 0); + ASSERT_EQ(b1->post(), 0); + ASSERT_TRUE(isAnyClientPosted(b1->bufferState())); // A posted client should be able to gain the buffer when there is no other clients in // acquired state. - EXPECT_EQ(b2->Gain(), 0); + EXPECT_EQ(b2->gain(), 0); } TEST_F(BufferHubBufferStateTransitionTest, PostBuffer_fromOtherInGainedState) { - ASSERT_EQ(b1->Gain(), 0); - ASSERT_TRUE(IsClientGained(b1->buffer_state(), b1ClientMask)); + ASSERT_EQ(b1->gain(), 0); + ASSERT_TRUE(isClientGained(b1->bufferState(), b1ClientMask)); - EXPECT_EQ(b2->Post(), -EBUSY); + EXPECT_EQ(b2->post(), -EBUSY); } TEST_F(BufferHubBufferStateTransitionTest, PostBuffer_fromSelfInGainedState) { - ASSERT_EQ(b1->Gain(), 0); - ASSERT_TRUE(IsClientGained(b1->buffer_state(), b1ClientMask)); + ASSERT_EQ(b1->gain(), 0); + ASSERT_TRUE(isClientGained(b1->bufferState(), b1ClientMask)); - EXPECT_EQ(b1->Post(), 0); - auto currentBufferState = b1->buffer_state(); - EXPECT_TRUE(IsClientReleased(currentBufferState, b1ClientMask)); - EXPECT_TRUE(IsClientPosted(currentBufferState, b2ClientMask)); + EXPECT_EQ(b1->post(), 0); + auto currentBufferState = b1->bufferState(); + EXPECT_TRUE(isClientReleased(currentBufferState, b1ClientMask)); + EXPECT_TRUE(isClientPosted(currentBufferState, b2ClientMask)); } TEST_F(BufferHubBufferStateTransitionTest, PostBuffer_fromPostedState) { - ASSERT_EQ(b1->Gain(), 0); - ASSERT_EQ(b1->Post(), 0); - ASSERT_TRUE(AnyClientPosted(b1->buffer_state())); + ASSERT_EQ(b1->gain(), 0); + ASSERT_EQ(b1->post(), 0); + ASSERT_TRUE(isAnyClientPosted(b1->bufferState())); // Post from posted state should fail. - EXPECT_EQ(b1->Post(), -EBUSY); - EXPECT_EQ(b2->Post(), -EBUSY); + EXPECT_EQ(b1->post(), -EBUSY); + EXPECT_EQ(b2->post(), -EBUSY); } TEST_F(BufferHubBufferStateTransitionTest, PostBuffer_fromAcquiredState) { - ASSERT_EQ(b1->Gain(), 0); - ASSERT_EQ(b1->Post(), 0); - ASSERT_EQ(b2->Acquire(), 0); - ASSERT_TRUE(AnyClientAcquired(b1->buffer_state())); + ASSERT_EQ(b1->gain(), 0); + ASSERT_EQ(b1->post(), 0); + ASSERT_EQ(b2->acquire(), 0); + ASSERT_TRUE(isAnyClientAcquired(b1->bufferState())); // Posting from acquired state should fail. - EXPECT_EQ(b1->Post(), -EBUSY); - EXPECT_EQ(b2->Post(), -EBUSY); + EXPECT_EQ(b1->post(), -EBUSY); + EXPECT_EQ(b2->post(), -EBUSY); } TEST_F(BufferHubBufferStateTransitionTest, PostBuffer_fromReleasedState) { - ASSERT_TRUE(b1->IsReleased()); + ASSERT_TRUE(b1->isReleased()); // Posting from released state should fail. - EXPECT_EQ(b1->Post(), -EBUSY); - EXPECT_EQ(b2->Post(), -EBUSY); + EXPECT_EQ(b1->post(), -EBUSY); + EXPECT_EQ(b2->post(), -EBUSY); } TEST_F(BufferHubBufferStateTransitionTest, AcquireBuffer_fromSelfInPostedState) { - ASSERT_EQ(b1->Gain(), 0); - ASSERT_EQ(b1->Post(), 0); - ASSERT_TRUE(IsClientPosted(b1->buffer_state(), b2ClientMask)); + ASSERT_EQ(b1->gain(), 0); + ASSERT_EQ(b1->post(), 0); + ASSERT_TRUE(isClientPosted(b1->bufferState(), b2ClientMask)); // Acquire from posted state should pass. - EXPECT_EQ(b2->Acquire(), 0); + EXPECT_EQ(b2->acquire(), 0); } TEST_F(BufferHubBufferStateTransitionTest, AcquireBuffer_fromOtherInPostedState) { - ASSERT_EQ(b1->Gain(), 0); - ASSERT_EQ(b1->Post(), 0); - ASSERT_TRUE(IsClientPosted(b1->buffer_state(), b2ClientMask)); + ASSERT_EQ(b1->gain(), 0); + ASSERT_EQ(b1->post(), 0); + ASSERT_TRUE(isClientPosted(b1->bufferState(), b2ClientMask)); // Acquire from released state should fail, although there are other clients // in posted state. - EXPECT_EQ(b1->Acquire(), -EBUSY); + EXPECT_EQ(b1->acquire(), -EBUSY); } TEST_F(BufferHubBufferStateTransitionTest, AcquireBuffer_fromSelfInAcquiredState) { - ASSERT_EQ(b1->Gain(), 0); - ASSERT_EQ(b1->Post(), 0); - ASSERT_EQ(b2->Acquire(), 0); - auto currentBufferState = b1->buffer_state(); - ASSERT_TRUE(IsClientAcquired(currentBufferState, b2ClientMask)); + ASSERT_EQ(b1->gain(), 0); + ASSERT_EQ(b1->post(), 0); + ASSERT_EQ(b2->acquire(), 0); + auto currentBufferState = b1->bufferState(); + ASSERT_TRUE(isClientAcquired(currentBufferState, b2ClientMask)); // Acquiring from acquired state by the same client should not error out. - EXPECT_EQ(b2->Acquire(), 0); + EXPECT_EQ(b2->acquire(), 0); } TEST_F(BufferHubBufferStateTransitionTest, AcquireBuffer_fromReleasedState) { - ASSERT_TRUE(b1->IsReleased()); + ASSERT_TRUE(b1->isReleased()); // Acquiring form released state should fail. - EXPECT_EQ(b1->Acquire(), -EBUSY); - EXPECT_EQ(b2->Acquire(), -EBUSY); + EXPECT_EQ(b1->acquire(), -EBUSY); + EXPECT_EQ(b2->acquire(), -EBUSY); } TEST_F(BufferHubBufferStateTransitionTest, AcquireBuffer_fromGainedState) { - ASSERT_EQ(b1->Gain(), 0); - ASSERT_TRUE(AnyClientGained(b1->buffer_state())); + ASSERT_EQ(b1->gain(), 0); + ASSERT_TRUE(isAnyClientGained(b1->bufferState())); // Acquiring from gained state should fail. - EXPECT_EQ(b1->Acquire(), -EBUSY); - EXPECT_EQ(b2->Acquire(), -EBUSY); + EXPECT_EQ(b1->acquire(), -EBUSY); + EXPECT_EQ(b2->acquire(), -EBUSY); } TEST_F(BufferHubBufferStateTransitionTest, ReleaseBuffer_fromSelfInReleasedState) { - ASSERT_TRUE(b1->IsReleased()); + ASSERT_TRUE(b1->isReleased()); - EXPECT_EQ(b1->Release(), 0); + EXPECT_EQ(b1->release(), 0); } TEST_F(BufferHubBufferStateTransitionTest, ReleaseBuffer_fromSelfInGainedState) { - ASSERT_TRUE(b1->IsReleased()); - ASSERT_EQ(b1->Gain(), 0); - ASSERT_TRUE(AnyClientGained(b1->buffer_state())); + ASSERT_TRUE(b1->isReleased()); + ASSERT_EQ(b1->gain(), 0); + ASSERT_TRUE(isAnyClientGained(b1->bufferState())); - EXPECT_EQ(b1->Release(), 0); + EXPECT_EQ(b1->release(), 0); } TEST_F(BufferHubBufferStateTransitionTest, ReleaseBuffer_fromSelfInPostedState) { - ASSERT_EQ(b1->Gain(), 0); - ASSERT_EQ(b1->Post(), 0); - ASSERT_TRUE(AnyClientPosted(b1->buffer_state())); + ASSERT_EQ(b1->gain(), 0); + ASSERT_EQ(b1->post(), 0); + ASSERT_TRUE(isAnyClientPosted(b1->bufferState())); - EXPECT_EQ(b2->Release(), 0); + EXPECT_EQ(b2->release(), 0); } TEST_F(BufferHubBufferStateTransitionTest, ReleaseBuffer_fromSelfInAcquiredState) { - ASSERT_EQ(b1->Gain(), 0); - ASSERT_EQ(b1->Post(), 0); - ASSERT_EQ(b2->Acquire(), 0); - ASSERT_TRUE(AnyClientAcquired(b1->buffer_state())); + ASSERT_EQ(b1->gain(), 0); + ASSERT_EQ(b1->post(), 0); + ASSERT_EQ(b2->acquire(), 0); + ASSERT_TRUE(isAnyClientAcquired(b1->bufferState())); - EXPECT_EQ(b2->Release(), 0); + EXPECT_EQ(b2->release(), 0); } TEST_F(BufferHubBufferStateTransitionTest, BasicUsage) { @@ -408,60 +408,60 @@ TEST_F(BufferHubBufferStateTransitionTest, BasicUsage) { // Test if this set of basic operation succeed: // Producer post three times to the consumer, and released by consumer. for (int i = 0; i < 3; ++i) { - ASSERT_EQ(b1->Gain(), 0); - ASSERT_EQ(b1->Post(), 0); - ASSERT_EQ(b2->Acquire(), 0); - ASSERT_EQ(b2->Release(), 0); + ASSERT_EQ(b1->gain(), 0); + ASSERT_EQ(b1->post(), 0); + ASSERT_EQ(b2->acquire(), 0); + ASSERT_EQ(b2->release(), 0); } } TEST_F(BufferHubBufferTest, createNewConsumerAfterGain) { // Create a poducer buffer and gain. std::unique_ptr b1 = - BufferHubBuffer::Create(kWidth, kHeight, kLayerCount, kFormat, kUsage, + BufferHubBuffer::create(kWidth, kHeight, kLayerCount, kFormat, kUsage, kUserMetadataSize); ASSERT_THAT(b1, NotNull()); - ASSERT_EQ(b1->Gain(), 0); + ASSERT_EQ(b1->gain(), 0); // Create a consumer of the buffer and test if the consumer can acquire the // buffer if producer posts. // TODO(b/122543147): use a movalbe wrapper for token - native_handle_t* token = b1->Duplicate(); + native_handle_t* token = b1->duplicate(); ASSERT_TRUE(token); - std::unique_ptr b2 = BufferHubBuffer::Import(token); + std::unique_ptr b2 = BufferHubBuffer::import(token); native_handle_close(token); native_handle_delete(token); ASSERT_THAT(b2, NotNull()); - ASSERT_NE(b1->client_state_mask(), b2->client_state_mask()); + ASSERT_NE(b1->clientStateMask(), b2->clientStateMask()); - ASSERT_EQ(b1->Post(), 0); - EXPECT_EQ(b2->Acquire(), 0); + ASSERT_EQ(b1->post(), 0); + EXPECT_EQ(b2->acquire(), 0); } TEST_F(BufferHubBufferTest, createNewConsumerAfterPost) { // Create a poducer buffer and post. std::unique_ptr b1 = - BufferHubBuffer::Create(kWidth, kHeight, kLayerCount, kFormat, kUsage, + BufferHubBuffer::create(kWidth, kHeight, kLayerCount, kFormat, kUsage, kUserMetadataSize); - ASSERT_EQ(b1->Gain(), 0); - ASSERT_EQ(b1->Post(), 0); + ASSERT_EQ(b1->gain(), 0); + ASSERT_EQ(b1->post(), 0); // Create a consumer of the buffer and test if the consumer can acquire the // buffer if producer posts. // TODO(b/122543147): use a movalbe wrapper for token - native_handle_t* token = b1->Duplicate(); + native_handle_t* token = b1->duplicate(); ASSERT_TRUE(token); - std::unique_ptr b2 = BufferHubBuffer::Import(token); + std::unique_ptr b2 = BufferHubBuffer::import(token); native_handle_close(token); native_handle_delete(token); ASSERT_THAT(b2, NotNull()); - ASSERT_NE(b1->client_state_mask(), b2->client_state_mask()); + ASSERT_NE(b1->clientStateMask(), b2->clientStateMask()); - EXPECT_EQ(b2->Acquire(), 0); + EXPECT_EQ(b2->acquire(), 0); } } // namespace diff --git a/libs/ui/tests/BufferHubMetadata_test.cpp b/libs/ui/tests/BufferHubMetadata_test.cpp index b7f0b4b140..f02c4fc178 100644 --- a/libs/ui/tests/BufferHubMetadata_test.cpp +++ b/libs/ui/tests/BufferHubMetadata_test.cpp @@ -25,74 +25,73 @@ constexpr size_t kEmptyUserMetadataSize = 0; class BufferHubMetadataTest : public ::testing::Test {}; TEST_F(BufferHubMetadataTest, Create_UserMetdataSizeTooBig) { - BufferHubMetadata m1 = - BufferHubMetadata::Create(std::numeric_limits::max()); - EXPECT_FALSE(m1.IsValid()); + BufferHubMetadata m1 = BufferHubMetadata::create(std::numeric_limits::max()); + EXPECT_FALSE(m1.isValid()); } TEST_F(BufferHubMetadataTest, Create_Success) { - BufferHubMetadata m1 = BufferHubMetadata::Create(kEmptyUserMetadataSize); - EXPECT_TRUE(m1.IsValid()); - EXPECT_NE(m1.metadata_header(), nullptr); + BufferHubMetadata m1 = BufferHubMetadata::create(kEmptyUserMetadataSize); + EXPECT_TRUE(m1.isValid()); + EXPECT_NE(m1.metadataHeader(), nullptr); } TEST_F(BufferHubMetadataTest, Import_Success) { - BufferHubMetadata m1 = BufferHubMetadata::Create(kEmptyUserMetadataSize); - EXPECT_TRUE(m1.IsValid()); - EXPECT_NE(m1.metadata_header(), nullptr); - - unique_fd h2 = unique_fd(dup(m1.ashmem_fd().get())); - EXPECT_NE(h2.get(), -1); - - BufferHubMetadata m2 = BufferHubMetadata::Import(std::move(h2)); - EXPECT_EQ(h2.get(), -1); - EXPECT_TRUE(m1.IsValid()); - BufferHubDefs::MetadataHeader* mh1 = m1.metadata_header(); - EXPECT_NE(mh1, nullptr); - - // Check if the newly allocated buffer is initialized in released state (i.e. - // state equals to 0U). - EXPECT_TRUE(mh1->buffer_state.load() == 0U); - - EXPECT_TRUE(m2.IsValid()); - BufferHubDefs::MetadataHeader* mh2 = m2.metadata_header(); - EXPECT_NE(mh2, nullptr); - - // Check if the newly allocated buffer is initialized in released state (i.e. - // state equals to 0U). - EXPECT_TRUE(mh2->buffer_state.load() == 0U); + BufferHubMetadata m1 = BufferHubMetadata::create(kEmptyUserMetadataSize); + EXPECT_TRUE(m1.isValid()); + EXPECT_NE(m1.metadataHeader(), nullptr); + + unique_fd h2 = unique_fd(dup(m1.ashmemFd().get())); + EXPECT_NE(h2.get(), -1); + + BufferHubMetadata m2 = BufferHubMetadata::import(std::move(h2)); + EXPECT_EQ(h2.get(), -1); + EXPECT_TRUE(m1.isValid()); + BufferHubDefs::MetadataHeader* mh1 = m1.metadataHeader(); + EXPECT_NE(mh1, nullptr); + + // Check if the newly allocated buffer is initialized in released state (i.e. + // state equals to 0U). + EXPECT_TRUE(mh1->buffer_state.load() == 0U); + + EXPECT_TRUE(m2.isValid()); + BufferHubDefs::MetadataHeader* mh2 = m2.metadataHeader(); + EXPECT_NE(mh2, nullptr); + + // Check if the newly allocated buffer is initialized in released state (i.e. + // state equals to 0U). + EXPECT_TRUE(mh2->buffer_state.load() == 0U); } TEST_F(BufferHubMetadataTest, MoveMetadataInvalidatesOldOne) { - BufferHubMetadata m1 = BufferHubMetadata::Create(sizeof(int)); - EXPECT_TRUE(m1.IsValid()); - EXPECT_NE(m1.metadata_header(), nullptr); - EXPECT_NE(m1.ashmem_fd().get(), -1); - EXPECT_EQ(m1.user_metadata_size(), sizeof(int)); + BufferHubMetadata m1 = BufferHubMetadata::create(sizeof(int)); + EXPECT_TRUE(m1.isValid()); + EXPECT_NE(m1.metadataHeader(), nullptr); + EXPECT_NE(m1.ashmemFd().get(), -1); + EXPECT_EQ(m1.userMetadataSize(), sizeof(int)); - BufferHubMetadata m2 = std::move(m1); + BufferHubMetadata m2 = std::move(m1); - // After the move, the metadata header (a raw pointer) should be reset in the older buffer. - EXPECT_EQ(m1.metadata_header(), nullptr); - EXPECT_NE(m2.metadata_header(), nullptr); + // After the move, the metadata header (a raw pointer) should be reset in the older buffer. + EXPECT_EQ(m1.metadataHeader(), nullptr); + EXPECT_NE(m2.metadataHeader(), nullptr); - EXPECT_EQ(m1.ashmem_fd().get(), -1); - EXPECT_NE(m2.ashmem_fd().get(), -1); + EXPECT_EQ(m1.ashmemFd().get(), -1); + EXPECT_NE(m2.ashmemFd().get(), -1); - EXPECT_EQ(m1.user_metadata_size(), 0U); - EXPECT_EQ(m2.user_metadata_size(), sizeof(int)); + EXPECT_EQ(m1.userMetadataSize(), 0U); + EXPECT_EQ(m2.userMetadataSize(), sizeof(int)); - BufferHubMetadata m3{std::move(m2)}; + BufferHubMetadata m3{std::move(m2)}; - // After the move, the metadata header (a raw pointer) should be reset in the older buffer. - EXPECT_EQ(m2.metadata_header(), nullptr); - EXPECT_NE(m3.metadata_header(), nullptr); + // After the move, the metadata header (a raw pointer) should be reset in the older buffer. + EXPECT_EQ(m2.metadataHeader(), nullptr); + EXPECT_NE(m3.metadataHeader(), nullptr); - EXPECT_EQ(m2.ashmem_fd().get(), -1); - EXPECT_NE(m3.ashmem_fd().get(), -1); + EXPECT_EQ(m2.ashmemFd().get(), -1); + EXPECT_NE(m3.ashmemFd().get(), -1); - EXPECT_EQ(m2.user_metadata_size(), 0U); - EXPECT_EQ(m3.user_metadata_size(), sizeof(int)); + EXPECT_EQ(m2.userMetadataSize(), 0U); + EXPECT_EQ(m3.userMetadataSize(), sizeof(int)); } } // namespace dvr diff --git a/libs/ui/tests/GraphicBuffer_test.cpp b/libs/ui/tests/GraphicBuffer_test.cpp index 5b46454c60..c767ce02fc 100644 --- a/libs/ui/tests/GraphicBuffer_test.cpp +++ b/libs/ui/tests/GraphicBuffer_test.cpp @@ -37,10 +37,10 @@ class GraphicBufferTest : public testing::Test {}; TEST_F(GraphicBufferTest, CreateFromBufferHubBuffer) { std::unique_ptr b1 = - BufferHubBuffer::Create(kTestWidth, kTestHeight, kTestLayerCount, kTestFormat, + BufferHubBuffer::create(kTestWidth, kTestHeight, kTestLayerCount, kTestFormat, kTestUsage, /*userMetadataSize=*/0); ASSERT_NE(b1, nullptr); - EXPECT_TRUE(b1->IsValid()); + EXPECT_TRUE(b1->isValid()); sp gb(new GraphicBuffer(std::move(b1))); EXPECT_TRUE(gb->isBufferHubBuffer()); @@ -61,10 +61,10 @@ TEST_F(GraphicBufferTest, InvalidBufferIdForNoneBufferHubBuffer) { TEST_F(GraphicBufferTest, BufferIdMatchesBufferHubBufferId) { std::unique_ptr b1 = - BufferHubBuffer::Create(kTestWidth, kTestHeight, kTestLayerCount, kTestFormat, + BufferHubBuffer::create(kTestWidth, kTestHeight, kTestLayerCount, kTestFormat, kTestUsage, /*userMetadataSize=*/0); EXPECT_NE(b1, nullptr); - EXPECT_TRUE(b1->IsValid()); + EXPECT_TRUE(b1->isValid()); int b1_id = b1->id(); EXPECT_GE(b1_id, 0); diff --git a/libs/vr/libbufferhub/buffer_hub-test.cpp b/libs/vr/libbufferhub/buffer_hub-test.cpp index 527a27d5dd..27ab0242ad 100644 --- a/libs/vr/libbufferhub/buffer_hub-test.cpp +++ b/libs/vr/libbufferhub/buffer_hub-test.cpp @@ -20,12 +20,12 @@ namespace { return result; \ })() -using android::BufferHubDefs::AnyClientAcquired; -using android::BufferHubDefs::AnyClientGained; -using android::BufferHubDefs::AnyClientPosted; -using android::BufferHubDefs::IsClientAcquired; -using android::BufferHubDefs::IsClientPosted; -using android::BufferHubDefs::IsClientReleased; +using android::BufferHubDefs::isAnyClientAcquired; +using android::BufferHubDefs::isAnyClientGained; +using android::BufferHubDefs::isAnyClientPosted; +using android::BufferHubDefs::isClientAcquired; +using android::BufferHubDefs::isClientPosted; +using android::BufferHubDefs::isClientReleased; using android::BufferHubDefs::kFirstClientBitMask; using android::dvr::ConsumerBuffer; using android::dvr::ProducerBuffer; @@ -268,7 +268,7 @@ TEST_F(LibBufferHubTest, TestAsyncStateTransitions) { // Post in gained state should succeed. EXPECT_EQ(0, p->PostAsync(&metadata, invalid_fence)); EXPECT_EQ(p->buffer_state(), c->buffer_state()); - EXPECT_TRUE(AnyClientPosted(p->buffer_state())); + EXPECT_TRUE(isAnyClientPosted(p->buffer_state())); // Post and gain in posted state should fail. EXPECT_EQ(-EBUSY, p->PostAsync(&metadata, invalid_fence)); @@ -280,7 +280,7 @@ TEST_F(LibBufferHubTest, TestAsyncStateTransitions) { EXPECT_EQ(0, c->AcquireAsync(&metadata, &invalid_fence)); EXPECT_FALSE(invalid_fence.IsValid()); EXPECT_EQ(p->buffer_state(), c->buffer_state()); - EXPECT_TRUE(AnyClientAcquired(p->buffer_state())); + EXPECT_TRUE(isAnyClientAcquired(p->buffer_state())); // Acquire, post, and gain in acquired state should fail. EXPECT_EQ(-EBUSY, c->AcquireAsync(&metadata, &invalid_fence)); @@ -304,7 +304,7 @@ TEST_F(LibBufferHubTest, TestAsyncStateTransitions) { EXPECT_EQ(0, p->GainAsync(&metadata, &invalid_fence)); EXPECT_FALSE(invalid_fence.IsValid()); EXPECT_EQ(p->buffer_state(), c->buffer_state()); - EXPECT_TRUE(AnyClientGained(p->buffer_state())); + EXPECT_TRUE(isAnyClientGained(p->buffer_state())); // Acquire and gain in gained state should fail. EXPECT_EQ(-EBUSY, c->AcquireAsync(&metadata, &invalid_fence)); @@ -329,7 +329,7 @@ TEST_F(LibBufferHubTest, TestGainPostedBuffer) { ASSERT_TRUE(c.get() != nullptr); ASSERT_EQ(0, p->GainAsync()); ASSERT_EQ(0, p->Post(LocalHandle())); - ASSERT_TRUE(AnyClientPosted(p->buffer_state())); + ASSERT_TRUE(isAnyClientPosted(p->buffer_state())); // Gain in posted state should only succeed with gain_posted_buffer = true. LocalHandle invalid_fence; @@ -346,7 +346,7 @@ TEST_F(LibBufferHubTest, TestGainPostedBufferAsync) { ASSERT_TRUE(c.get() != nullptr); ASSERT_EQ(0, p->GainAsync()); ASSERT_EQ(0, p->Post(LocalHandle())); - ASSERT_TRUE(AnyClientPosted(p->buffer_state())); + ASSERT_TRUE(isAnyClientPosted(p->buffer_state())); // GainAsync in posted state should only succeed with gain_posted_buffer // equals true. @@ -364,9 +364,9 @@ TEST_F(LibBufferHubTest, TestGainPostedBuffer_noConsumer) { ASSERT_EQ(0, p->Post(LocalHandle())); // Producer state bit is in released state after post, other clients shall be // in posted state although there is no consumer of this buffer yet. - ASSERT_TRUE(IsClientReleased(p->buffer_state(), p->client_state_mask())); + ASSERT_TRUE(isClientReleased(p->buffer_state(), p->client_state_mask())); ASSERT_TRUE(p->is_released()); - ASSERT_TRUE(AnyClientPosted(p->buffer_state())); + ASSERT_TRUE(isAnyClientPosted(p->buffer_state())); // Gain in released state should succeed. LocalHandle invalid_fence; @@ -393,14 +393,14 @@ TEST_F(LibBufferHubTest, TestMaxConsumers) { // Post the producer should trigger all consumers to be available. EXPECT_EQ(0, p->PostAsync(&metadata, invalid_fence)); - EXPECT_TRUE(IsClientReleased(p->buffer_state(), p->client_state_mask())); + EXPECT_TRUE(isClientReleased(p->buffer_state(), p->client_state_mask())); for (size_t i = 0; i < kMaxConsumerCount; ++i) { EXPECT_TRUE( - IsClientPosted(cs[i]->buffer_state(), cs[i]->client_state_mask())); + isClientPosted(cs[i]->buffer_state(), cs[i]->client_state_mask())); EXPECT_LT(0, RETRY_EINTR(PollBufferEvent(cs[i]))); EXPECT_EQ(0, cs[i]->AcquireAsync(&metadata, &invalid_fence)); EXPECT_TRUE( - IsClientAcquired(p->buffer_state(), cs[i]->client_state_mask())); + isClientAcquired(p->buffer_state(), cs[i]->client_state_mask())); } // All consumers have to release before the buffer is considered to be @@ -424,22 +424,22 @@ TEST_F(LibBufferHubTest, TestCreateConsumerWhenBufferGained) { kWidth, kHeight, kFormat, kUsage, sizeof(uint64_t)); ASSERT_TRUE(p.get() != nullptr); EXPECT_EQ(0, p->GainAsync()); - EXPECT_TRUE(AnyClientGained(p->buffer_state())); + EXPECT_TRUE(isAnyClientGained(p->buffer_state())); std::unique_ptr c = ConsumerBuffer::Import(p->CreateConsumer()); ASSERT_TRUE(c.get() != nullptr); - EXPECT_TRUE(AnyClientGained(c->buffer_state())); + EXPECT_TRUE(isAnyClientGained(c->buffer_state())); DvrNativeBufferMetadata metadata; LocalHandle invalid_fence; // Post the gained buffer should signal already created consumer. EXPECT_EQ(0, p->PostAsync(&metadata, invalid_fence)); - EXPECT_TRUE(AnyClientPosted(p->buffer_state())); + EXPECT_TRUE(isAnyClientPosted(p->buffer_state())); EXPECT_LT(0, RETRY_EINTR(PollBufferEvent(c))); EXPECT_EQ(0, c->AcquireAsync(&metadata, &invalid_fence)); - EXPECT_TRUE(AnyClientAcquired(c->buffer_state())); + EXPECT_TRUE(isAnyClientAcquired(c->buffer_state())); } TEST_F(LibBufferHubTest, TestCreateTheFirstConsumerAfterPostingBuffer) { @@ -447,7 +447,7 @@ TEST_F(LibBufferHubTest, TestCreateTheFirstConsumerAfterPostingBuffer) { kWidth, kHeight, kFormat, kUsage, sizeof(uint64_t)); ASSERT_TRUE(p.get() != nullptr); EXPECT_EQ(0, p->GainAsync()); - EXPECT_TRUE(AnyClientGained(p->buffer_state())); + EXPECT_TRUE(isAnyClientGained(p->buffer_state())); DvrNativeBufferMetadata metadata; LocalHandle invalid_fence; @@ -462,7 +462,7 @@ TEST_F(LibBufferHubTest, TestCreateTheFirstConsumerAfterPostingBuffer) { std::unique_ptr c = ConsumerBuffer::Import(p->CreateConsumer()); ASSERT_TRUE(c.get() != nullptr); - EXPECT_TRUE(IsClientPosted(c->buffer_state(), c->client_state_mask())); + EXPECT_TRUE(isClientPosted(c->buffer_state(), c->client_state_mask())); EXPECT_EQ(0, c->AcquireAsync(&metadata, &invalid_fence)); } @@ -500,7 +500,7 @@ TEST_F(LibBufferHubTest, TestCreateConsumerWhenBufferReleased) { EXPECT_TRUE(p->is_released()); EXPECT_EQ(0, p->GainAsync(&metadata, &invalid_fence)); - EXPECT_TRUE(AnyClientGained(p->buffer_state())); + EXPECT_TRUE(isAnyClientGained(p->buffer_state())); } TEST_F(LibBufferHubTest, TestWithCustomMetadata) { diff --git a/libs/vr/libbufferhub/consumer_buffer.cpp b/libs/vr/libbufferhub/consumer_buffer.cpp index b6ca64eef2..115e8666e5 100644 --- a/libs/vr/libbufferhub/consumer_buffer.cpp +++ b/libs/vr/libbufferhub/consumer_buffer.cpp @@ -38,7 +38,7 @@ int ConsumerBuffer::LocalAcquire(DvrNativeBufferMetadata* out_meta, // The buffer can be acquired iff the buffer state for this client is posted. uint32_t current_buffer_state = buffer_state_->load(std::memory_order_acquire); - if (!BufferHubDefs::IsClientPosted(current_buffer_state, + if (!BufferHubDefs::isClientPosted(current_buffer_state, client_state_mask())) { ALOGE( "%s: Failed to acquire the buffer. The buffer is not posted, id=%d " @@ -58,7 +58,7 @@ int ConsumerBuffer::LocalAcquire(DvrNativeBufferMetadata* out_meta, " when trying to acquire the buffer and modify the buffer state to " "%" PRIx32 ". About to try again if the buffer is still posted.", __FUNCTION__, current_buffer_state, updated_buffer_state); - if (!BufferHubDefs::IsClientPosted(current_buffer_state, + if (!BufferHubDefs::isClientPosted(current_buffer_state, client_state_mask())) { ALOGE( "%s: Failed to acquire the buffer. The buffer is no longer posted, " @@ -144,7 +144,7 @@ int ConsumerBuffer::LocalRelease(const DvrNativeBufferMetadata* meta, // released state. uint32_t current_buffer_state = buffer_state_->load(std::memory_order_acquire); - if (BufferHubDefs::IsClientReleased(current_buffer_state, + if (BufferHubDefs::isClientReleased(current_buffer_state, client_state_mask())) { return 0; } diff --git a/libs/vr/libbufferhub/include/private/dvr/buffer_hub_defs.h b/libs/vr/libbufferhub/include/private/dvr/buffer_hub_defs.h index bab7367caa..e610e18849 100644 --- a/libs/vr/libbufferhub/include/private/dvr/buffer_hub_defs.h +++ b/libs/vr/libbufferhub/include/private/dvr/buffer_hub_defs.h @@ -27,38 +27,38 @@ static constexpr uint32_t kHighBitsMask = android::BufferHubDefs::kHighBitsMask; static constexpr uint32_t kFirstClientBitMask = android::BufferHubDefs::kFirstClientBitMask; -static inline bool AnyClientGained(uint32_t state) { - return android::BufferHubDefs::AnyClientGained(state); +static inline bool isAnyClientGained(uint32_t state) { + return android::BufferHubDefs::isAnyClientGained(state); } -static inline bool IsClientGained(uint32_t state, uint32_t client_bit_mask) { - return android::BufferHubDefs::IsClientGained(state, client_bit_mask); +static inline bool isClientGained(uint32_t state, uint32_t client_bit_mask) { + return android::BufferHubDefs::isClientGained(state, client_bit_mask); } -static inline bool AnyClientPosted(uint32_t state) { - return android::BufferHubDefs::AnyClientPosted(state); +static inline bool isAnyClientPosted(uint32_t state) { + return android::BufferHubDefs::isAnyClientPosted(state); } -static inline bool IsClientPosted(uint32_t state, uint32_t client_bit_mask) { - return android::BufferHubDefs::IsClientPosted(state, client_bit_mask); +static inline bool isClientPosted(uint32_t state, uint32_t client_bit_mask) { + return android::BufferHubDefs::isClientPosted(state, client_bit_mask); } -static inline bool AnyClientAcquired(uint32_t state) { - return android::BufferHubDefs::AnyClientAcquired(state); +static inline bool isAnyClientAcquired(uint32_t state) { + return android::BufferHubDefs::isAnyClientAcquired(state); } -static inline bool IsClientAcquired(uint32_t state, uint32_t client_bit_mask) { - return android::BufferHubDefs::IsClientAcquired(state, client_bit_mask); +static inline bool isClientAcquired(uint32_t state, uint32_t client_bit_mask) { + return android::BufferHubDefs::isClientAcquired(state, client_bit_mask); } -static inline bool IsClientReleased(uint32_t state, uint32_t client_bit_mask) { - return android::BufferHubDefs::IsClientReleased(state, client_bit_mask); +static inline bool isClientReleased(uint32_t state, uint32_t client_bit_mask) { + return android::BufferHubDefs::isClientReleased(state, client_bit_mask); } // Returns the next available buffer client's client_state_masks. // @params union_bits. Union of all existing clients' client_state_masks. -static inline uint32_t FindNextAvailableClientStateMask(uint32_t union_bits) { - return android::BufferHubDefs::FindNextAvailableClientStateMask(union_bits); +static inline uint32_t findNextAvailableClientStateMask(uint32_t union_bits) { + return android::BufferHubDefs::findNextAvailableClientStateMask(union_bits); } using MetadataHeader = android::BufferHubDefs::MetadataHeader; diff --git a/libs/vr/libbufferhub/producer_buffer.cpp b/libs/vr/libbufferhub/producer_buffer.cpp index edfdddf363..3d88ba5dbe 100644 --- a/libs/vr/libbufferhub/producer_buffer.cpp +++ b/libs/vr/libbufferhub/producer_buffer.cpp @@ -82,7 +82,7 @@ int ProducerBuffer::LocalPost(const DvrNativeBufferMetadata* meta, // The buffer can be posted iff the buffer state for this client is gained. uint32_t current_buffer_state = buffer_state_->load(std::memory_order_acquire); - if (!BufferHubDefs::IsClientGained(current_buffer_state, + if (!BufferHubDefs::isClientGained(current_buffer_state, client_state_mask())) { ALOGE("%s: not gained, id=%d state=%" PRIx32 ".", __FUNCTION__, id(), current_buffer_state); @@ -103,7 +103,7 @@ int ProducerBuffer::LocalPost(const DvrNativeBufferMetadata* meta, "%" PRIx32 ". About to try again if the buffer is still gained by this client.", __FUNCTION__, current_buffer_state, updated_buffer_state); - if (!BufferHubDefs::IsClientGained(current_buffer_state, + if (!BufferHubDefs::isClientGained(current_buffer_state, client_state_mask())) { ALOGE( "%s: Failed to post the buffer. The buffer is no longer gained, " @@ -166,14 +166,14 @@ int ProducerBuffer::LocalGain(DvrNativeBufferMetadata* out_meta, ALOGD_IF(TRACE, "%s: buffer=%d, state=%" PRIx32 ".", __FUNCTION__, id(), current_buffer_state); - if (BufferHubDefs::IsClientGained(current_buffer_state, + if (BufferHubDefs::isClientGained(current_buffer_state, client_state_mask())) { ALOGV("%s: already gained id=%d.", __FUNCTION__, id()); return 0; } - if (BufferHubDefs::AnyClientAcquired(current_buffer_state) || - BufferHubDefs::AnyClientGained(current_buffer_state) || - (BufferHubDefs::AnyClientPosted( + if (BufferHubDefs::isAnyClientAcquired(current_buffer_state) || + BufferHubDefs::isAnyClientGained(current_buffer_state) || + (BufferHubDefs::isAnyClientPosted( current_buffer_state & active_clients_bit_mask_->load(std::memory_order_acquire)) && !gain_posted_buffer)) { @@ -195,9 +195,9 @@ int ProducerBuffer::LocalGain(DvrNativeBufferMetadata* out_meta, "clients.", __FUNCTION__, current_buffer_state, updated_buffer_state); - if (BufferHubDefs::AnyClientAcquired(current_buffer_state) || - BufferHubDefs::AnyClientGained(current_buffer_state) || - (BufferHubDefs::AnyClientPosted( + if (BufferHubDefs::isAnyClientAcquired(current_buffer_state) || + BufferHubDefs::isAnyClientGained(current_buffer_state) || + (BufferHubDefs::isAnyClientPosted( current_buffer_state & active_clients_bit_mask_->load(std::memory_order_acquire)) && !gain_posted_buffer)) { @@ -291,7 +291,7 @@ Status ProducerBuffer::Detach() { // TODO(b/112338294) Keep here for reference. Remove it after new logic is // written. /* uint32_t buffer_state = buffer_state_->load(std::memory_order_acquire); - if (!BufferHubDefs::IsClientGained( + if (!BufferHubDefs::isClientGained( buffer_state, BufferHubDefs::kFirstClientStateMask)) { // Can only detach a ProducerBuffer when it's in gained state. ALOGW("ProducerBuffer::Detach: The buffer (id=%d, state=0x%" PRIx32 diff --git a/libs/vr/libbufferhubqueue/buffer_hub_queue_client.cpp b/libs/vr/libbufferhubqueue/buffer_hub_queue_client.cpp index d7833f382b..2d3fa4aec0 100644 --- a/libs/vr/libbufferhubqueue/buffer_hub_queue_client.cpp +++ b/libs/vr/libbufferhubqueue/buffer_hub_queue_client.cpp @@ -532,7 +532,7 @@ Status ProducerQueue::AddBuffer( Status ProducerQueue::InsertBuffer( const std::shared_ptr& buffer) { if (buffer == nullptr || - !BufferHubDefs::IsClientGained(buffer->buffer_state(), + !BufferHubDefs::isClientGained(buffer->buffer_state(), buffer->client_state_mask())) { ALOGE( "ProducerQueue::InsertBuffer: Can only insert a buffer when it's in " @@ -638,7 +638,7 @@ Status> ProducerQueue::DequeueUnacquiredBuffer( static_cast(*slot)); return ErrorStatus(EIO); } - if (!BufferHubDefs::AnyClientAcquired(buffer->buffer_state())) { + if (!BufferHubDefs::isAnyClientAcquired(buffer->buffer_state())) { *slot = *iter; unavailable_buffers_slot_.erase(iter); unavailable_buffers_slot_.push_back(*slot); diff --git a/services/bufferhub/BufferHubService.cpp b/services/bufferhub/BufferHubService.cpp index 6b802fbf83..90ac1c2535 100644 --- a/services/bufferhub/BufferHubService.cpp +++ b/services/bufferhub/BufferHubService.cpp @@ -53,7 +53,7 @@ Return BufferHubService::allocateBuffer(const HardwareBufferDescription& d std::make_shared(desc.width, desc.height, desc.layers, desc.format, desc.usage, userMetadataSize, BufferHubIdGenerator::getInstance().getId()); - if (node == nullptr || !node->IsValid()) { + if (node == nullptr || !node->isValid()) { ALOGE("%s: creating BufferNode failed.", __FUNCTION__); _hidl_cb(/*status=*/BufferHubStatus::ALLOCATION_FAILED, /*bufferClient=*/nullptr, /*bufferTraits=*/{}); @@ -70,11 +70,11 @@ Return BufferHubService::allocateBuffer(const HardwareBufferDescription& d 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(), + buildBufferInfo(bufferInfoStorage, node->id(), node->addNewActiveClientsBitToMask(), + node->userMetadataSize(), node->metadata().ashmemFd(), node->eventFd().get()); BufferTraits bufferTraits = {/*bufferDesc=*/description, - /*bufferHandle=*/hidl_handle(node->buffer_handle()), + /*bufferHandle=*/hidl_handle(node->bufferHandle()), /*bufferInfo=*/std::move(bufferInfo)}; _hidl_cb(/*status=*/BufferHubStatus::NO_ERROR, /*bufferClient=*/client, @@ -141,7 +141,7 @@ Return BufferHubService::importBuffer(const hidl_handle& tokenHandle, } sp client = new BufferClient(*originClient); - uint32_t clientStateMask = client->getBufferNode()->AddNewActiveClientsBitToMask(); + uint32_t clientStateMask = client->getBufferNode()->addNewActiveClientsBitToMask(); if (clientStateMask == 0U) { // Reach max client count ALOGE("%s: import failed, BufferNode#%u reached maximum clients.", __FUNCTION__, @@ -157,17 +157,17 @@ Return BufferHubService::importBuffer(const hidl_handle& tokenHandle, std::shared_ptr node = client->getBufferNode(); HardwareBufferDescription bufferDesc; - memcpy(&bufferDesc, &node->buffer_desc(), sizeof(HardwareBufferDescription)); + memcpy(&bufferDesc, &node->bufferDesc(), sizeof(HardwareBufferDescription)); // 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()); + node->userMetadataSize(), node->metadata().ashmemFd(), + node->eventFd().get()); BufferTraits bufferTraits = {/*bufferDesc=*/bufferDesc, - /*bufferHandle=*/hidl_handle(node->buffer_handle()), + /*bufferHandle=*/hidl_handle(node->bufferHandle()), /*bufferInfo=*/std::move(bufferInfo)}; _hidl_cb(/*status=*/BufferHubStatus::NO_ERROR, /*bufferClient=*/client, @@ -231,10 +231,10 @@ Return BufferHubService::debug(const hidl_handle& fd, const hidl_vec node = std::move(iter->second.first); const uint32_t clientCount = iter->second.second; - AHardwareBuffer_Desc desc = node->buffer_desc(); + AHardwareBuffer_Desc desc = node->bufferDesc(); MetadataHeader* metadataHeader = - const_cast(&node->metadata())->metadata_header(); + const_cast(&node->metadata())->metadataHeader(); const uint32_t state = metadataHeader->buffer_state.load(std::memory_order_acquire); const uint64_t index = metadataHeader->queue_index; diff --git a/services/bufferhub/BufferNode.cpp b/services/bufferhub/BufferNode.cpp index dfa2de0173..4f877b264a 100644 --- a/services/bufferhub/BufferNode.cpp +++ b/services/bufferhub/BufferNode.cpp @@ -11,10 +11,10 @@ namespace bufferhub { namespace V1_0 { namespace implementation { -void BufferNode::InitializeMetadata() { +void BufferNode::initializeMetadata() { // Using placement new here to reuse shared memory instead of new allocation // Initialize the atomic variables to zero. - BufferHubDefs::MetadataHeader* metadataHeader = mMetadata.metadata_header(); + BufferHubDefs::MetadataHeader* metadataHeader = mMetadata.metadataHeader(); mBufferState = new (&metadataHeader->buffer_state) std::atomic(0); mFenceState = new (&metadataHeader->fence_state) std::atomic(0); mActiveClientsBitMask = new (&metadataHeader->active_clients_bit_mask) std::atomic(0); @@ -54,12 +54,12 @@ BufferNode::BufferNode(uint32_t width, uint32_t height, uint32_t layer_count, ui mBufferDesc.usage = usage; mBufferDesc.stride = out_stride; - mMetadata = BufferHubMetadata::Create(user_metadata_size); - if (!mMetadata.IsValid()) { + mMetadata = BufferHubMetadata::create(user_metadata_size); + if (!mMetadata.isValid()) { ALOGE("%s: Failed to allocate metadata.", __FUNCTION__); return; } - InitializeMetadata(); + initializeMetadata(); } BufferNode::~BufferNode() { @@ -77,17 +77,17 @@ BufferNode::~BufferNode() { } } -uint32_t BufferNode::GetActiveClientsBitMask() const { +uint32_t BufferNode::getActiveClientsBitMask() const { return mActiveClientsBitMask->load(std::memory_order_acquire); } -uint32_t BufferNode::AddNewActiveClientsBitToMask() { - uint32_t currentActiveClientsBitMask = GetActiveClientsBitMask(); +uint32_t BufferNode::addNewActiveClientsBitToMask() { + uint32_t currentActiveClientsBitMask = getActiveClientsBitMask(); uint32_t client_state_mask = 0U; uint32_t updatedActiveClientsBitMask = 0U; do { client_state_mask = - BufferHubDefs::FindNextAvailableClientStateMask(currentActiveClientsBitMask); + BufferHubDefs::findNextAvailableClientStateMask(currentActiveClientsBitMask); if (client_state_mask == 0U) { ALOGE("%s: reached the maximum number of channels per buffer node: %d.", __FUNCTION__, BufferHubDefs::kMaxNumberOfClients); @@ -102,7 +102,7 @@ uint32_t BufferNode::AddNewActiveClientsBitToMask() { return client_state_mask; } -void BufferNode::RemoveClientsBitFromMask(const uint32_t& value) { +void BufferNode::removeClientsBitFromMask(const uint32_t& value) { mActiveClientsBitMask->fetch_and(~value); } diff --git a/services/bufferhub/include/bufferhub/BufferNode.h b/services/bufferhub/include/bufferhub/BufferNode.h index 112a21c9af..04970fd14d 100644 --- a/services/bufferhub/include/bufferhub/BufferNode.h +++ b/services/bufferhub/include/bufferhub/BufferNode.h @@ -22,15 +22,15 @@ public: ~BufferNode(); // Returns whether the object holds a valid metadata. - bool IsValid() const { return mMetadata.IsValid(); } + bool isValid() const { return mMetadata.isValid(); } int id() const { return mId; } - size_t user_metadata_size() const { return mMetadata.user_metadata_size(); } + size_t userMetadataSize() const { return mMetadata.userMetadataSize(); } // Accessors of the buffer description and handle - const native_handle_t* buffer_handle() const { return mBufferHandle; } - const AHardwareBuffer_Desc& buffer_desc() const { return mBufferDesc; } + const native_handle_t* bufferHandle() const { return mBufferHandle; } + const AHardwareBuffer_Desc& bufferDesc() const { return mBufferDesc; } // Accessor of event fd. const BufferHubEventFd& eventFd() const { return mEventFd; } @@ -41,24 +41,24 @@ public: // Gets the current value of mActiveClientsBitMask in mMetadata with // std::memory_order_acquire, so that all previous releases of // mActiveClientsBitMask from all threads will be returned here. - uint32_t GetActiveClientsBitMask() const; + uint32_t getActiveClientsBitMask() const; // Find and add a new client state mask to mActiveClientsBitMask in // mMetadata. // Return the new client state mask that is added to mActiveClientsBitMask. // Return 0U if there are already 16 clients of the buffer. - uint32_t AddNewActiveClientsBitToMask(); + uint32_t addNewActiveClientsBitToMask(); // Removes the value from active_clients_bit_mask in mMetadata with // std::memory_order_release, so that the change will be visible to any // acquire of mActiveClientsBitMask in any threads after the succeed of // this operation. - void RemoveClientsBitFromMask(const uint32_t& value); + void removeClientsBitFromMask(const uint32_t& value); private: // Helper method for constructors to initialize atomic metadata header // variables in shared memory. - void InitializeMetadata(); + void initializeMetadata(); // Gralloc buffer handles. native_handle_t* mBufferHandle; diff --git a/services/bufferhub/tests/BufferNode_test.cpp b/services/bufferhub/tests/BufferNode_test.cpp index ccb1197498..b9f1c81c63 100644 --- a/services/bufferhub/tests/BufferNode_test.cpp +++ b/services/bufferhub/tests/BufferNode_test.cpp @@ -28,7 +28,7 @@ protected: void SetUp() override { buffer_node = new BufferNode(kWidth, kHeight, kLayerCount, kFormat, kUsage, kUserMetadataSize); - ASSERT_TRUE(buffer_node->IsValid()); + ASSERT_TRUE(buffer_node->isValid()); } void TearDown() override { @@ -41,65 +41,64 @@ protected: }; TEST_F(BufferNodeTest, TestCreateBufferNode) { - EXPECT_EQ(buffer_node->user_metadata_size(), kUserMetadataSize); + EXPECT_EQ(buffer_node->userMetadataSize(), kUserMetadataSize); // Test the handle just allocated is good (i.e. able to be imported) GraphicBufferMapper& mapper = GraphicBufferMapper::get(); const native_handle_t* outHandle; status_t ret = - mapper.importBuffer(buffer_node->buffer_handle(), buffer_node->buffer_desc().width, - buffer_node->buffer_desc().height, - buffer_node->buffer_desc().layers, - buffer_node->buffer_desc().format, buffer_node->buffer_desc().usage, - buffer_node->buffer_desc().stride, &outHandle); + mapper.importBuffer(buffer_node->bufferHandle(), buffer_node->bufferDesc().width, + buffer_node->bufferDesc().height, buffer_node->bufferDesc().layers, + buffer_node->bufferDesc().format, buffer_node->bufferDesc().usage, + buffer_node->bufferDesc().stride, &outHandle); EXPECT_EQ(ret, OK); EXPECT_THAT(outHandle, NotNull()); } -TEST_F(BufferNodeTest, TestAddNewActiveClientsBitToMask_twoNewClients) { - uint32_t new_client_state_mask_1 = buffer_node->AddNewActiveClientsBitToMask(); - EXPECT_EQ(buffer_node->GetActiveClientsBitMask(), new_client_state_mask_1); +TEST_F(BufferNodeTest, TestaddNewActiveClientsBitToMask_twoNewClients) { + uint32_t new_client_state_mask_1 = buffer_node->addNewActiveClientsBitToMask(); + EXPECT_EQ(buffer_node->getActiveClientsBitMask(), new_client_state_mask_1); // Request and add a new client_state_mask again. // Active clients bit mask should be the union of the two new // client_state_masks. - uint32_t new_client_state_mask_2 = buffer_node->AddNewActiveClientsBitToMask(); - EXPECT_EQ(buffer_node->GetActiveClientsBitMask(), + uint32_t new_client_state_mask_2 = buffer_node->addNewActiveClientsBitToMask(); + EXPECT_EQ(buffer_node->getActiveClientsBitMask(), new_client_state_mask_1 | new_client_state_mask_2); } -TEST_F(BufferNodeTest, TestAddNewActiveClientsBitToMask_32NewClients) { +TEST_F(BufferNodeTest, TestaddNewActiveClientsBitToMask_32NewClients) { uint32_t new_client_state_mask = 0U; uint32_t current_mask = 0U; uint32_t expected_mask = 0U; for (int i = 0; i < BufferHubDefs::kMaxNumberOfClients; ++i) { - new_client_state_mask = buffer_node->AddNewActiveClientsBitToMask(); + new_client_state_mask = buffer_node->addNewActiveClientsBitToMask(); EXPECT_NE(new_client_state_mask, 0U); EXPECT_FALSE(new_client_state_mask & current_mask); expected_mask = current_mask | new_client_state_mask; - current_mask = buffer_node->GetActiveClientsBitMask(); + current_mask = buffer_node->getActiveClientsBitMask(); EXPECT_EQ(current_mask, expected_mask); } // Method should fail upon requesting for more than maximum allowable clients. - new_client_state_mask = buffer_node->AddNewActiveClientsBitToMask(); + new_client_state_mask = buffer_node->addNewActiveClientsBitToMask(); EXPECT_EQ(new_client_state_mask, 0U); EXPECT_EQ(errno, E2BIG); } TEST_F(BufferNodeTest, TestRemoveActiveClientsBitFromMask) { - buffer_node->AddNewActiveClientsBitToMask(); - uint32_t current_mask = buffer_node->GetActiveClientsBitMask(); - uint32_t new_client_state_mask = buffer_node->AddNewActiveClientsBitToMask(); - EXPECT_NE(buffer_node->GetActiveClientsBitMask(), current_mask); + buffer_node->addNewActiveClientsBitToMask(); + uint32_t current_mask = buffer_node->getActiveClientsBitMask(); + uint32_t new_client_state_mask = buffer_node->addNewActiveClientsBitToMask(); + EXPECT_NE(buffer_node->getActiveClientsBitMask(), current_mask); - buffer_node->RemoveClientsBitFromMask(new_client_state_mask); - EXPECT_EQ(buffer_node->GetActiveClientsBitMask(), current_mask); + buffer_node->removeClientsBitFromMask(new_client_state_mask); + EXPECT_EQ(buffer_node->getActiveClientsBitMask(), current_mask); // Remove the test_mask again to the active client bit mask should not modify // the value of active clients bit mask. - buffer_node->RemoveClientsBitFromMask(new_client_state_mask); - EXPECT_EQ(buffer_node->GetActiveClientsBitMask(), current_mask); + buffer_node->removeClientsBitFromMask(new_client_state_mask); + EXPECT_EQ(buffer_node->getActiveClientsBitMask(), current_mask); } } // namespace diff --git a/services/vr/bufferhubd/producer_channel.cpp b/services/vr/bufferhubd/producer_channel.cpp index 164d9e6b19..f3e54a0ed2 100644 --- a/services/vr/bufferhubd/producer_channel.cpp +++ b/services/vr/bufferhubd/producer_channel.cpp @@ -253,7 +253,7 @@ Status ProducerChannel::CreateConsumerStateMask() { uint32_t current_active_clients_bit_mask = active_clients_bit_mask_->load(std::memory_order_acquire); uint32_t consumer_state_mask = - BufferHubDefs::FindNextAvailableClientStateMask( + BufferHubDefs::findNextAvailableClientStateMask( current_active_clients_bit_mask | orphaned_consumer_bit_mask_); if (consumer_state_mask == 0U) { ALOGE("%s: reached the maximum mumber of consumers per producer: 63.", @@ -279,7 +279,7 @@ Status ProducerChannel::CreateConsumerStateMask() { "condition.", __FUNCTION__, updated_active_clients_bit_mask, current_active_clients_bit_mask); - consumer_state_mask = BufferHubDefs::FindNextAvailableClientStateMask( + consumer_state_mask = BufferHubDefs::findNextAvailableClientStateMask( current_active_clients_bit_mask | orphaned_consumer_bit_mask_); if (consumer_state_mask == 0U) { ALOGE("%s: reached the maximum mumber of consumers per producer: %d.", @@ -337,13 +337,13 @@ Status ProducerChannel::CreateConsumer( // consumer to a buffer that is available to producer (a.k.a a fully-released // buffer) or a gained buffer. if (current_buffer_state == 0U || - BufferHubDefs::AnyClientGained(current_buffer_state)) { + BufferHubDefs::isAnyClientGained(current_buffer_state)) { return {status.take()}; } // Signal the new consumer when adding it to a posted producer. bool update_buffer_state = true; - if (!BufferHubDefs::IsClientPosted(current_buffer_state, + if (!BufferHubDefs::isClientPosted(current_buffer_state, consumer_state_mask)) { uint32_t updated_buffer_state = current_buffer_state ^ @@ -360,7 +360,7 @@ Status ProducerChannel::CreateConsumer( "released.", __FUNCTION__, current_buffer_state, updated_buffer_state); if (current_buffer_state == 0U || - BufferHubDefs::AnyClientGained(current_buffer_state)) { + BufferHubDefs::isAnyClientGained(current_buffer_state)) { ALOGI("%s: buffer is gained or fully released, state=%" PRIx32 ".", __FUNCTION__, current_buffer_state); update_buffer_state = false; @@ -371,7 +371,7 @@ Status ProducerChannel::CreateConsumer( (consumer_state_mask & BufferHubDefs::kHighBitsMask); } } - if (update_buffer_state || BufferHubDefs::IsClientPosted( + if (update_buffer_state || BufferHubDefs::isClientPosted( buffer_state_->load(std::memory_order_acquire), consumer_state_mask)) { consumer->OnProducerPosted(); @@ -457,7 +457,7 @@ Status ProducerChannel::OnProducerGain(Message& /*message*/) { buffer_id()); uint32_t buffer_state = buffer_state_->load(std::memory_order_acquire); - if (!BufferHubDefs::IsClientGained( + if (!BufferHubDefs::isClientGained( buffer_state, BufferHubDefs::kFirstClientStateMask)) { // Can only detach a ProducerBuffer when it's in gained state. ALOGW( @@ -616,9 +616,9 @@ void ProducerChannel::RemoveConsumer(ConsumerChannel* channel) { const uint32_t current_buffer_state = buffer_state_->load(std::memory_order_acquire); - if (BufferHubDefs::IsClientPosted(current_buffer_state, + if (BufferHubDefs::isClientPosted(current_buffer_state, consumer_state_mask) || - BufferHubDefs::IsClientAcquired(current_buffer_state, + BufferHubDefs::isClientAcquired(current_buffer_state, consumer_state_mask)) { // The consumer client is being destoryed without releasing. This could // happen in corner cases when the consumer crashes. Here we mark it @@ -627,9 +627,9 @@ void ProducerChannel::RemoveConsumer(ConsumerChannel* channel) { return; } - if (BufferHubDefs::IsClientReleased(current_buffer_state, + if (BufferHubDefs::isClientReleased(current_buffer_state, consumer_state_mask) || - BufferHubDefs::AnyClientGained(current_buffer_state)) { + BufferHubDefs::isAnyClientGained(current_buffer_state)) { // The consumer is being close while it is suppose to signal a release // fence. Signal the dummy fence here. if (fence_state_->load(std::memory_order_acquire) & consumer_state_mask) { diff --git a/services/vr/bufferhubd/producer_queue_channel.cpp b/services/vr/bufferhubd/producer_queue_channel.cpp index 6b33f5094c..004dc7cb4f 100644 --- a/services/vr/bufferhubd/producer_queue_channel.cpp +++ b/services/vr/bufferhubd/producer_queue_channel.cpp @@ -323,7 +323,7 @@ Status ProducerQueueChannel::OnProducerQueueInsertBuffer( // memory to indicate which client is the last producer of the buffer. // Currently, the first client is the only producer to the buffer. // Thus, it checks whether the first client gains the buffer below. - if (!BufferHubDefs::IsClientGained(buffer_state, + if (!BufferHubDefs::isClientGained(buffer_state, BufferHubDefs::kFirstClientBitMask)) { // Rejects the request if the requested buffer is not in Gained state. ALOGE( -- cgit v1.2.3-59-g8ed1b From c9d8d589b838234f372e1c83578f0a6247f5cced Mon Sep 17 00:00:00 2001 From: Tianyu Jiang Date: Sun, 10 Feb 2019 22:05:34 -0800 Subject: Fix style violation according to go/droidcppstyle Local variable should be lowerCamelCase. This change leave the buffer_state fence_state, active_clients_bit_mask and queue_index in MetadataHeader unchanged because there are too many instances of them that need to be changed in both new bufferhub system and the old pdx-backed bufferhubd system. I will do a separate change for that. Test: m, mma Bug: 68273829 Change-Id: Ia91cf7debe01a402f61171f069988e0ac13cec6a --- libs/ui/BufferHubBuffer.cpp | 8 +-- services/bufferhub/BufferNode.cpp | 26 ++++----- .../bufferhub/include/bufferhub/BufferClient.h | 2 +- services/bufferhub/include/bufferhub/BufferNode.h | 4 +- services/bufferhub/tests/BufferNode_test.cpp | 67 +++++++++++----------- 5 files changed, 53 insertions(+), 54 deletions(-) (limited to 'libs/ui/BufferHubBuffer.cpp') diff --git a/libs/ui/BufferHubBuffer.cpp b/libs/ui/BufferHubBuffer.cpp index 7693fcbfe0..cce35fdf29 100644 --- a/libs/ui/BufferHubBuffer.cpp +++ b/libs/ui/BufferHubBuffer.cpp @@ -209,10 +209,10 @@ int BufferHubBuffer::initWithBufferTraits(const BufferTraits& bufferTraits) { } // Populate shortcuts to the atomics in metadata. - auto metadata_header = mMetadata.metadataHeader(); - mBufferState = &metadata_header->buffer_state; - mFenceState = &metadata_header->fence_state; - mActiveClientsBitMask = &metadata_header->active_clients_bit_mask; + auto metadataHeader = mMetadata.metadataHeader(); + mBufferState = &metadataHeader->buffer_state; + mFenceState = &metadataHeader->fence_state; + mActiveClientsBitMask = &metadataHeader->active_clients_bit_mask; // The C++ standard recommends (but does not require) that lock-free atomic operations are // also address-free, that is, suitable for communication between processes using shared // memory. diff --git a/services/bufferhub/BufferNode.cpp b/services/bufferhub/BufferNode.cpp index 4f877b264a..1efb27ee0a 100644 --- a/services/bufferhub/BufferNode.cpp +++ b/services/bufferhub/BufferNode.cpp @@ -28,17 +28,17 @@ void BufferNode::initializeMetadata() { } // Allocates a new BufferNode. -BufferNode::BufferNode(uint32_t width, uint32_t height, uint32_t layer_count, uint32_t format, - uint64_t usage, size_t user_metadata_size, int id) +BufferNode::BufferNode(uint32_t width, uint32_t height, uint32_t layerCount, uint32_t format, + uint64_t usage, size_t userMetadataSize, int id) : mId(id) { - uint32_t out_stride = 0; + uint32_t outStride = 0; // graphicBufferId is not used in GraphicBufferAllocator::allocate // TODO(b/112338294) After move to the service folder, stop using the // hardcoded service name "bufferhub". - int ret = GraphicBufferAllocator::get().allocate(width, height, format, layer_count, usage, + int ret = GraphicBufferAllocator::get().allocate(width, height, format, layerCount, usage, const_cast( &mBufferHandle), - &out_stride, + &outStride, /*graphicBufferId=*/0, /*requestor=*/"bufferhub"); @@ -49,12 +49,12 @@ BufferNode::BufferNode(uint32_t width, uint32_t height, uint32_t layer_count, ui mBufferDesc.width = width; mBufferDesc.height = height; - mBufferDesc.layers = layer_count; + mBufferDesc.layers = layerCount; mBufferDesc.format = format; mBufferDesc.usage = usage; - mBufferDesc.stride = out_stride; + mBufferDesc.stride = outStride; - mMetadata = BufferHubMetadata::create(user_metadata_size); + mMetadata = BufferHubMetadata::create(userMetadataSize); if (!mMetadata.isValid()) { ALOGE("%s: Failed to allocate metadata.", __FUNCTION__); return; @@ -83,23 +83,23 @@ uint32_t BufferNode::getActiveClientsBitMask() const { uint32_t BufferNode::addNewActiveClientsBitToMask() { uint32_t currentActiveClientsBitMask = getActiveClientsBitMask(); - uint32_t client_state_mask = 0U; + uint32_t clientStateMask = 0U; uint32_t updatedActiveClientsBitMask = 0U; do { - client_state_mask = + clientStateMask = BufferHubDefs::findNextAvailableClientStateMask(currentActiveClientsBitMask); - if (client_state_mask == 0U) { + if (clientStateMask == 0U) { ALOGE("%s: reached the maximum number of channels per buffer node: %d.", __FUNCTION__, BufferHubDefs::kMaxNumberOfClients); errno = E2BIG; return 0U; } - updatedActiveClientsBitMask = currentActiveClientsBitMask | client_state_mask; + updatedActiveClientsBitMask = currentActiveClientsBitMask | clientStateMask; } while (!(mActiveClientsBitMask->compare_exchange_weak(currentActiveClientsBitMask, updatedActiveClientsBitMask, std::memory_order_acq_rel, std::memory_order_acquire))); - return client_state_mask; + return clientStateMask; } void BufferNode::removeClientsBitFromMask(const uint32_t& value) { diff --git a/services/bufferhub/include/bufferhub/BufferClient.h b/services/bufferhub/include/bufferhub/BufferClient.h index 66ed4bd13f..644b40377d 100644 --- a/services/bufferhub/include/bufferhub/BufferClient.h +++ b/services/bufferhub/include/bufferhub/BufferClient.h @@ -72,4 +72,4 @@ private: } // namespace frameworks } // namespace android -#endif \ No newline at end of file +#endif diff --git a/services/bufferhub/include/bufferhub/BufferNode.h b/services/bufferhub/include/bufferhub/BufferNode.h index 04970fd14d..62a8d63b38 100644 --- a/services/bufferhub/include/bufferhub/BufferNode.h +++ b/services/bufferhub/include/bufferhub/BufferNode.h @@ -16,8 +16,8 @@ namespace implementation { class BufferNode { public: // Allocates a new BufferNode. - BufferNode(uint32_t width, uint32_t height, uint32_t layer_count, uint32_t format, - uint64_t usage, size_t user_metadata_size, int id = -1); + BufferNode(uint32_t width, uint32_t height, uint32_t layerCount, uint32_t format, + uint64_t usage, size_t userMetadataSize, int id = -1); ~BufferNode(); diff --git a/services/bufferhub/tests/BufferNode_test.cpp b/services/bufferhub/tests/BufferNode_test.cpp index b9f1c81c63..2dfd4fc52e 100644 --- a/services/bufferhub/tests/BufferNode_test.cpp +++ b/services/bufferhub/tests/BufferNode_test.cpp @@ -26,79 +26,78 @@ const size_t kUserMetadataSize = 0; class BufferNodeTest : public ::testing::Test { protected: void SetUp() override { - buffer_node = + mBufferNode = new BufferNode(kWidth, kHeight, kLayerCount, kFormat, kUsage, kUserMetadataSize); - ASSERT_TRUE(buffer_node->isValid()); + ASSERT_TRUE(mBufferNode->isValid()); } void TearDown() override { - if (buffer_node != nullptr) { - delete buffer_node; + if (mBufferNode != nullptr) { + delete mBufferNode; } } - BufferNode* buffer_node = nullptr; + BufferNode* mBufferNode = nullptr; }; TEST_F(BufferNodeTest, TestCreateBufferNode) { - EXPECT_EQ(buffer_node->userMetadataSize(), kUserMetadataSize); + EXPECT_EQ(mBufferNode->userMetadataSize(), kUserMetadataSize); // Test the handle just allocated is good (i.e. able to be imported) GraphicBufferMapper& mapper = GraphicBufferMapper::get(); const native_handle_t* outHandle; status_t ret = - mapper.importBuffer(buffer_node->bufferHandle(), buffer_node->bufferDesc().width, - buffer_node->bufferDesc().height, buffer_node->bufferDesc().layers, - buffer_node->bufferDesc().format, buffer_node->bufferDesc().usage, - buffer_node->bufferDesc().stride, &outHandle); + mapper.importBuffer(mBufferNode->bufferHandle(), mBufferNode->bufferDesc().width, + mBufferNode->bufferDesc().height, mBufferNode->bufferDesc().layers, + mBufferNode->bufferDesc().format, mBufferNode->bufferDesc().usage, + mBufferNode->bufferDesc().stride, &outHandle); EXPECT_EQ(ret, OK); EXPECT_THAT(outHandle, NotNull()); } TEST_F(BufferNodeTest, TestaddNewActiveClientsBitToMask_twoNewClients) { - uint32_t new_client_state_mask_1 = buffer_node->addNewActiveClientsBitToMask(); - EXPECT_EQ(buffer_node->getActiveClientsBitMask(), new_client_state_mask_1); + uint32_t newClientStateMask1 = mBufferNode->addNewActiveClientsBitToMask(); + EXPECT_EQ(mBufferNode->getActiveClientsBitMask(), newClientStateMask1); // Request and add a new client_state_mask again. // Active clients bit mask should be the union of the two new // client_state_masks. - uint32_t new_client_state_mask_2 = buffer_node->addNewActiveClientsBitToMask(); - EXPECT_EQ(buffer_node->getActiveClientsBitMask(), - new_client_state_mask_1 | new_client_state_mask_2); + uint32_t newClientStateMask2 = mBufferNode->addNewActiveClientsBitToMask(); + EXPECT_EQ(mBufferNode->getActiveClientsBitMask(), newClientStateMask1 | newClientStateMask2); } TEST_F(BufferNodeTest, TestaddNewActiveClientsBitToMask_32NewClients) { - uint32_t new_client_state_mask = 0U; - uint32_t current_mask = 0U; - uint32_t expected_mask = 0U; + uint32_t newClientStateMask = 0U; + uint32_t currentMask = 0U; + uint32_t expectedMask = 0U; for (int i = 0; i < BufferHubDefs::kMaxNumberOfClients; ++i) { - new_client_state_mask = buffer_node->addNewActiveClientsBitToMask(); - EXPECT_NE(new_client_state_mask, 0U); - EXPECT_FALSE(new_client_state_mask & current_mask); - expected_mask = current_mask | new_client_state_mask; - current_mask = buffer_node->getActiveClientsBitMask(); - EXPECT_EQ(current_mask, expected_mask); + newClientStateMask = mBufferNode->addNewActiveClientsBitToMask(); + EXPECT_NE(newClientStateMask, 0U); + EXPECT_FALSE(newClientStateMask & currentMask); + expectedMask = currentMask | newClientStateMask; + currentMask = mBufferNode->getActiveClientsBitMask(); + EXPECT_EQ(currentMask, expectedMask); } // Method should fail upon requesting for more than maximum allowable clients. - new_client_state_mask = buffer_node->addNewActiveClientsBitToMask(); - EXPECT_EQ(new_client_state_mask, 0U); + newClientStateMask = mBufferNode->addNewActiveClientsBitToMask(); + EXPECT_EQ(newClientStateMask, 0U); EXPECT_EQ(errno, E2BIG); } TEST_F(BufferNodeTest, TestRemoveActiveClientsBitFromMask) { - buffer_node->addNewActiveClientsBitToMask(); - uint32_t current_mask = buffer_node->getActiveClientsBitMask(); - uint32_t new_client_state_mask = buffer_node->addNewActiveClientsBitToMask(); - EXPECT_NE(buffer_node->getActiveClientsBitMask(), current_mask); + mBufferNode->addNewActiveClientsBitToMask(); + uint32_t currentMask = mBufferNode->getActiveClientsBitMask(); + uint32_t newClientStateMask = mBufferNode->addNewActiveClientsBitToMask(); + EXPECT_NE(mBufferNode->getActiveClientsBitMask(), currentMask); - buffer_node->removeClientsBitFromMask(new_client_state_mask); - EXPECT_EQ(buffer_node->getActiveClientsBitMask(), current_mask); + mBufferNode->removeClientsBitFromMask(newClientStateMask); + EXPECT_EQ(mBufferNode->getActiveClientsBitMask(), currentMask); // Remove the test_mask again to the active client bit mask should not modify // the value of active clients bit mask. - buffer_node->removeClientsBitFromMask(new_client_state_mask); - EXPECT_EQ(buffer_node->getActiveClientsBitMask(), current_mask); + mBufferNode->removeClientsBitFromMask(newClientStateMask); + EXPECT_EQ(mBufferNode->getActiveClientsBitMask(), currentMask); } } // namespace -- cgit v1.2.3-59-g8ed1b From 8a7e765b0a1c2b20db1e9408570071019f3c0e4e Mon Sep 17 00:00:00 2001 From: Tianyu Jiang Date: Mon, 11 Feb 2019 13:41:20 -0800 Subject: Replace the use of native_handle_t by NativeHandle so that user do not need to explicitly destroy and close the native handle after importing a BufferHubBuffer using the token obtained from duplicate method. Change-Id: Id4f878e8881db7495444b1a43a33b70eabfcb7d7 Fix: 122543147 Test: BufferHub_test --- libs/ui/BufferHubBuffer.cpp | 12 ++++++------ libs/ui/include/ui/BufferHubBuffer.h | 15 ++++++--------- libs/ui/tests/BufferHubBuffer_test.cpp | 34 ++++++++++------------------------ 3 files changed, 22 insertions(+), 39 deletions(-) (limited to 'libs/ui/BufferHubBuffer.cpp') diff --git a/libs/ui/BufferHubBuffer.cpp b/libs/ui/BufferHubBuffer.cpp index cce35fdf29..405bcb62e1 100644 --- a/libs/ui/BufferHubBuffer.cpp +++ b/libs/ui/BufferHubBuffer.cpp @@ -47,8 +47,8 @@ std::unique_ptr BufferHubBuffer::create(uint32_t width, uint32_ return buffer->isValid() ? std::move(buffer) : nullptr; } -std::unique_ptr BufferHubBuffer::import(const native_handle_t* token) { - if (token == nullptr) { +std::unique_ptr BufferHubBuffer::import(const sp& token) { + if (token == nullptr || token.get() == nullptr) { ALOGE("%s: token cannot be nullptr!", __FUNCTION__); return nullptr; } @@ -105,7 +105,7 @@ BufferHubBuffer::BufferHubBuffer(uint32_t width, uint32_t height, uint32_t layer mBufferClient = std::move(client); } -BufferHubBuffer::BufferHubBuffer(const native_handle_t* token) { +BufferHubBuffer::BufferHubBuffer(const sp& token) { sp bufferhub = IBufferHub::getService(); if (bufferhub.get() == nullptr) { ALOGE("%s: BufferHub service not found!", __FUNCTION__); @@ -124,7 +124,7 @@ BufferHubBuffer::BufferHubBuffer(const native_handle_t* token) { // hidl_handle(native_handle_t*) simply creates a raw pointer reference withouth ownership // transfer. - if (!bufferhub->importBuffer(hidl_handle(token), import_cb).isOk()) { + if (!bufferhub->importBuffer(hidl_handle(token.get()->handle()), import_cb).isOk()) { ALOGE("%s: importBuffer transaction failed!", __FUNCTION__); return; } else if (ret != BufferHubStatus::NO_ERROR) { @@ -328,7 +328,7 @@ bool BufferHubBuffer::isValid() const { mEventFd.get() >= 0 && mMetadata.isValid() && mBufferClient != nullptr; } -native_handle_t* BufferHubBuffer::duplicate() { +sp BufferHubBuffer::duplicate() { if (mBufferClient == nullptr) { ALOGE("%s: missing BufferClient!", __FUNCTION__); return nullptr; @@ -352,7 +352,7 @@ native_handle_t* BufferHubBuffer::duplicate() { return nullptr; } - return native_handle_clone(token.getNativeHandle()); + return NativeHandle::create(native_handle_clone(token.getNativeHandle()), /*ownsHandle=*/true); } } // namespace android diff --git a/libs/ui/include/ui/BufferHubBuffer.h b/libs/ui/include/ui/BufferHubBuffer.h index eac8c8437b..5c0903251a 100644 --- a/libs/ui/include/ui/BufferHubBuffer.h +++ b/libs/ui/include/ui/BufferHubBuffer.h @@ -23,6 +23,7 @@ #include #include #include +#include namespace android { @@ -33,10 +34,8 @@ public: uint32_t layerCount, uint32_t format, uint64_t usage, size_t userMetadataSize); - // Imports the given token to a BufferHubBuffer. Not taking ownership of the token. Caller - // should close and destroy the token after calling this function regardless of output. - // TODO(b/122543147): use a movable wrapper for token - static std::unique_ptr import(const native_handle_t* token); + // Imports the given token to a BufferHubBuffer. Not taking ownership of the token. + static std::unique_ptr import(const sp& token); BufferHubBuffer(const BufferHubBuffer&) = delete; void operator=(const BufferHubBuffer&) = delete; @@ -100,17 +99,15 @@ public: // Creates a token that stands for this BufferHubBuffer client and could be used for Import to // create another BufferHubBuffer. The new BufferHubBuffer will share the same underlying - // gralloc buffer and ashmem region for metadata. Note that the caller owns the token and - // should free it after use. + // gralloc buffer and ashmem region for metadata. Not taking ownership of the token. // Returns a valid token on success, nullptr on failure. - // TODO(b/122543147): use a movable wrapper for token - native_handle_t* duplicate(); + sp duplicate(); private: BufferHubBuffer(uint32_t width, uint32_t height, uint32_t layerCount, uint32_t format, uint64_t usage, size_t userMetadataSize); - BufferHubBuffer(const native_handle_t* token); + BufferHubBuffer(const sp& token); int initWithBufferTraits(const frameworks::bufferhub::V1_0::BufferTraits& bufferTraits); diff --git a/libs/ui/tests/BufferHubBuffer_test.cpp b/libs/ui/tests/BufferHubBuffer_test.cpp index 3087a90787..634bce17fe 100644 --- a/libs/ui/tests/BufferHubBuffer_test.cpp +++ b/libs/ui/tests/BufferHubBuffer_test.cpp @@ -86,13 +86,10 @@ void BufferHubBufferStateTransitionTest::CreateTwoClientsOfABuffer() { b1ClientMask = b1->clientStateMask(); ASSERT_NE(b1ClientMask, 0U); - native_handle_t* token = b1->duplicate(); + sp token = b1->duplicate(); ASSERT_THAT(token, NotNull()); - // TODO(b/122543147): use a movalbe wrapper for token b2 = BufferHubBuffer::import(token); - native_handle_close(token); - native_handle_delete(token); ASSERT_THAT(b2, NotNull()); b2ClientMask = b2->clientStateMask(); @@ -137,16 +134,14 @@ TEST_F(BufferHubBufferTest, DuplicateAndImportBuffer) { ASSERT_THAT(b1, NotNull()); EXPECT_TRUE(b1->isValid()); - native_handle_t* token = b1->duplicate(); - EXPECT_TRUE(token); + sp token = b1->duplicate(); + ASSERT_THAT(token, NotNull()); // The detached buffer should still be valid. EXPECT_TRUE(b1->isConnected()); EXPECT_TRUE(b1->isValid()); std::unique_ptr b2 = BufferHubBuffer::import(token); - native_handle_close(token); - native_handle_delete(token); ASSERT_THAT(b2, NotNull()); EXPECT_TRUE(b2->isValid()); @@ -197,16 +192,13 @@ TEST_F(BufferHubBufferTest, ImportFreedBuffer) { ASSERT_THAT(b1, NotNull()); EXPECT_TRUE(b1->isValid()); - native_handle_t* token = b1->duplicate(); - EXPECT_TRUE(token); + sp token = b1->duplicate(); + ASSERT_THAT(token, NotNull()); // Explicitly destroy b1. Backend buffer should be freed and token becomes invalid b1.reset(); - // TODO(b/122543147): use a movalbe wrapper for token std::unique_ptr b2 = BufferHubBuffer::import(token); - native_handle_close(token); - native_handle_delete(token); // Import should fail with INVALID_TOKEN EXPECT_THAT(b2, IsNull()); @@ -222,7 +214,7 @@ TEST_F(BufferHubBufferTest, ImportInvalidToken) { native_handle_t* token = native_handle_create(/*numFds=*/0, /*numInts=*/1); token->data[0] = 0; - auto b1 = BufferHubBuffer::import(token); + auto b1 = BufferHubBuffer::import(NativeHandle::create(token, /*ownHandle=*/true)); native_handle_delete(token); EXPECT_THAT(b1, IsNull()); @@ -425,13 +417,10 @@ TEST_F(BufferHubBufferTest, createNewConsumerAfterGain) { // Create a consumer of the buffer and test if the consumer can acquire the // buffer if producer posts. - // TODO(b/122543147): use a movalbe wrapper for token - native_handle_t* token = b1->duplicate(); - ASSERT_TRUE(token); + sp token = b1->duplicate(); + ASSERT_THAT(token, NotNull()); std::unique_ptr b2 = BufferHubBuffer::import(token); - native_handle_close(token); - native_handle_delete(token); ASSERT_THAT(b2, NotNull()); ASSERT_NE(b1->clientStateMask(), b2->clientStateMask()); @@ -450,13 +439,10 @@ TEST_F(BufferHubBufferTest, createNewConsumerAfterPost) { // Create a consumer of the buffer and test if the consumer can acquire the // buffer if producer posts. - // TODO(b/122543147): use a movalbe wrapper for token - native_handle_t* token = b1->duplicate(); - ASSERT_TRUE(token); + sp token = b1->duplicate(); + ASSERT_THAT(token, NotNull()); std::unique_ptr b2 = BufferHubBuffer::import(token); - native_handle_close(token); - native_handle_delete(token); ASSERT_THAT(b2, NotNull()); ASSERT_NE(b1->clientStateMask(), b2->clientStateMask()); -- cgit v1.2.3-59-g8ed1b From dfe0f4c69ad4f547db7109e7b4a1db42448d1233 Mon Sep 17 00:00:00 2001 From: Jiwen 'Steve' Cai Date: Tue, 15 Jan 2019 21:49:07 -0800 Subject: Fix BufferHubBuffer black screen 1/ BufferHubService should read back fields of HardwareBufferDescription of allocated BufferNode and send them back to the client. 2/ Update some minor naming for better readability. Bug: 122959735 Test: cts-tradefed run commandAndExit cts -m CtsNativeHardwareTestCases Change-Id: I10a176810d78c754fe0b69d4128078fc17d50035 --- libs/ui/BufferHubBuffer.cpp | 8 ++++---- services/bufferhub/BufferHubService.cpp | 8 +++++++- 2 files changed, 11 insertions(+), 5 deletions(-) (limited to 'libs/ui/BufferHubBuffer.cpp') diff --git a/libs/ui/BufferHubBuffer.cpp b/libs/ui/BufferHubBuffer.cpp index 405bcb62e1..604665bc91 100644 --- a/libs/ui/BufferHubBuffer.cpp +++ b/libs/ui/BufferHubBuffer.cpp @@ -79,10 +79,10 @@ BufferHubBuffer::BufferHubBuffer(uint32_t width, uint32_t height, uint32_t layer sp client; BufferTraits bufferTraits; IBufferHub::allocateBuffer_cb alloc_cb = [&](const auto& status, const auto& outClient, - const auto& traits) { + const auto& outTraits) { ret = status; client = std::move(outClient); - bufferTraits = std::move(traits); + bufferTraits = std::move(outTraits); }; if (!bufferhub->allocateBuffer(desc, static_cast(userMetadataSize), alloc_cb) @@ -116,10 +116,10 @@ BufferHubBuffer::BufferHubBuffer(const sp& token) { sp client; BufferTraits bufferTraits; IBufferHub::importBuffer_cb import_cb = [&](const auto& status, const auto& outClient, - const auto& traits) { + const auto& outTraits) { ret = status; client = std::move(outClient); - bufferTraits = std::move(traits); + bufferTraits = std::move(outTraits); }; // hidl_handle(native_handle_t*) simply creates a raw pointer reference withouth ownership diff --git a/services/bufferhub/BufferHubService.cpp b/services/bufferhub/BufferHubService.cpp index 90ac1c2535..ade08e7d17 100644 --- a/services/bufferhub/BufferHubService.cpp +++ b/services/bufferhub/BufferHubService.cpp @@ -73,7 +73,13 @@ Return BufferHubService::allocateBuffer(const HardwareBufferDescription& d buildBufferInfo(bufferInfoStorage, node->id(), node->addNewActiveClientsBitToMask(), node->userMetadataSize(), node->metadata().ashmemFd(), node->eventFd().get()); - BufferTraits bufferTraits = {/*bufferDesc=*/description, + // During the gralloc allocation carried out by BufferNode, gralloc allocator will populate the + // fields of its HardwareBufferDescription (i.e. strides) according to the actual + // gralloc implementation. We need to read those fields back and send them to the client via + // BufferTraits. + HardwareBufferDescription allocatedBufferDesc; + memcpy(&allocatedBufferDesc, &node->bufferDesc(), sizeof(AHardwareBuffer_Desc)); + BufferTraits bufferTraits = {/*bufferDesc=*/allocatedBufferDesc, /*bufferHandle=*/hidl_handle(node->bufferHandle()), /*bufferInfo=*/std::move(bufferInfo)}; -- cgit v1.2.3-59-g8ed1b From f377a76a47cdcfd15437c94d547a30a9da82fb7c Mon Sep 17 00:00:00 2001 From: Tianyu Jiang Date: Wed, 13 Feb 2019 13:46:42 -0800 Subject: Fix code style violation in the variables in BufferHubDefs::MetadataHeader Violation: variables should be lowerCaseCamel in the following directories: frameworks/native/libs/ui and frameworks/native/services/bufferhub Test: m, mma Bug: 68273829 Change-Id: I7dc56ec17089456f98b018032f04f779b12632b2 --- libs/ui/BufferHubBuffer.cpp | 25 ++++---- libs/ui/include/ui/BufferHubBuffer.h | 2 +- libs/ui/include/ui/BufferHubDefs.h | 66 +++++++++++----------- libs/ui/tests/BufferHubMetadata_test.cpp | 4 +- libs/vr/libbufferhub/buffer_hub_base.cpp | 6 +- .../include/private/dvr/buffer_hub_base.h | 4 +- services/bufferhub/BufferHubService.cpp | 4 +- services/bufferhub/BufferNode.cpp | 6 +- services/vr/bufferhubd/producer_channel.cpp | 13 ++--- 9 files changed, 64 insertions(+), 66 deletions(-) (limited to 'libs/ui/BufferHubBuffer.cpp') diff --git a/libs/ui/BufferHubBuffer.cpp b/libs/ui/BufferHubBuffer.cpp index 604665bc91..da91a979fe 100644 --- a/libs/ui/BufferHubBuffer.cpp +++ b/libs/ui/BufferHubBuffer.cpp @@ -78,15 +78,14 @@ BufferHubBuffer::BufferHubBuffer(uint32_t width, uint32_t height, uint32_t layer BufferHubStatus ret; sp client; BufferTraits bufferTraits; - IBufferHub::allocateBuffer_cb alloc_cb = [&](const auto& status, const auto& outClient, - const auto& outTraits) { + IBufferHub::allocateBuffer_cb allocCb = [&](const auto& status, const auto& outClient, + const auto& outTraits) { ret = status; client = std::move(outClient); bufferTraits = std::move(outTraits); }; - if (!bufferhub->allocateBuffer(desc, static_cast(userMetadataSize), alloc_cb) - .isOk()) { + if (!bufferhub->allocateBuffer(desc, static_cast(userMetadataSize), allocCb).isOk()) { ALOGE("%s: allocateBuffer transaction failed!", __FUNCTION__); return; } else if (ret != BufferHubStatus::NO_ERROR) { @@ -115,8 +114,8 @@ BufferHubBuffer::BufferHubBuffer(const sp& token) { BufferHubStatus ret; sp client; BufferTraits bufferTraits; - IBufferHub::importBuffer_cb import_cb = [&](const auto& status, const auto& outClient, - const auto& outTraits) { + IBufferHub::importBuffer_cb importCb = [&](const auto& status, const auto& outClient, + const auto& outTraits) { ret = status; client = std::move(outClient); bufferTraits = std::move(outTraits); @@ -124,7 +123,7 @@ BufferHubBuffer::BufferHubBuffer(const sp& token) { // hidl_handle(native_handle_t*) simply creates a raw pointer reference withouth ownership // transfer. - if (!bufferhub->importBuffer(hidl_handle(token.get()->handle()), import_cb).isOk()) { + if (!bufferhub->importBuffer(hidl_handle(token.get()->handle()), importCb).isOk()) { ALOGE("%s: importBuffer transaction failed!", __FUNCTION__); return; } else if (ret != BufferHubStatus::NO_ERROR) { @@ -210,9 +209,9 @@ int BufferHubBuffer::initWithBufferTraits(const BufferTraits& bufferTraits) { // Populate shortcuts to the atomics in metadata. auto metadataHeader = mMetadata.metadataHeader(); - mBufferState = &metadataHeader->buffer_state; - mFenceState = &metadataHeader->fence_state; - mActiveClientsBitMask = &metadataHeader->active_clients_bit_mask; + mBufferState = &metadataHeader->bufferState; + mFenceState = &metadataHeader->fenceState; + mActiveClientsBitMask = &metadataHeader->activeClientsBitMask; // The C++ standard recommends (but does not require) that lock-free atomic operations are // also address-free, that is, suitable for communication between processes using shared // memory. @@ -230,7 +229,7 @@ int BufferHubBuffer::initWithBufferTraits(const BufferTraits& bufferTraits) { mClientStateMask = clientBitMask; // TODO(b/112012161) Set up shared fences. - ALOGD("%s: id=%d, buffer_state=%" PRIx32 ".", __FUNCTION__, mId, + ALOGD("%s: id=%d, mBufferState=%" PRIx32 ".", __FUNCTION__, mId, mBufferState->load(std::memory_order_acquire)); return 0; } @@ -336,12 +335,12 @@ sp BufferHubBuffer::duplicate() { hidl_handle token; BufferHubStatus ret; - IBufferClient::duplicate_cb dup_cb = [&](const auto& outToken, const auto& status) { + IBufferClient::duplicate_cb dupCb = [&](const auto& outToken, const auto& status) { token = std::move(outToken); ret = status; }; - if (!mBufferClient->duplicate(dup_cb).isOk()) { + if (!mBufferClient->duplicate(dupCb).isOk()) { ALOGE("%s: duplicate transaction failed!", __FUNCTION__); return nullptr; } else if (ret != BufferHubStatus::NO_ERROR) { diff --git a/libs/ui/include/ui/BufferHubBuffer.h b/libs/ui/include/ui/BufferHubBuffer.h index 5c0903251a..5ba189c2c3 100644 --- a/libs/ui/include/ui/BufferHubBuffer.h +++ b/libs/ui/include/ui/BufferHubBuffer.h @@ -57,7 +57,7 @@ public: const BufferHubEventFd& eventFd() const { return mEventFd; } - // Returns the current value of MetadataHeader::buffer_state. + // Returns the current value of MetadataHeader::bufferState. uint32_t bufferState() const { return mBufferState->load(std::memory_order_acquire); } // A state mask which is unique to a buffer hub client among all its siblings sharing the same diff --git a/libs/ui/include/ui/BufferHubDefs.h b/libs/ui/include/ui/BufferHubDefs.h index 722a060597..10f274f569 100644 --- a/libs/ui/include/ui/BufferHubDefs.h +++ b/libs/ui/include/ui/BufferHubDefs.h @@ -32,7 +32,7 @@ namespace BufferHubDefs { // Single buffer clients (up to 16) ownership signal. // 32-bit atomic unsigned int. // Each client takes 2 bits. The first bit locates in the first 16 bits of -// buffer_state; the second bit locates in the last 16 bits of buffer_state. +// bufferState; the second bit locates in the last 16 bits of bufferState. // Client states: // Gained state 11. Exclusive write state. // Posted state 10. @@ -64,9 +64,9 @@ static constexpr uint32_t kFirstClientBitMask = (1U << kMaxNumberOfClients) + 1U // Returns true if any of the client is in gained state. static inline bool isAnyClientGained(uint32_t state) { - uint32_t high_bits = state >> kMaxNumberOfClients; - uint32_t low_bits = state & kLowbitsMask; - return high_bits == low_bits && low_bits != 0U; + uint32_t highBits = state >> kMaxNumberOfClients; + uint32_t lowBits = state & kLowbitsMask; + return highBits == lowBits && lowBits != 0U; } // Returns true if the input client is in gained state. @@ -76,34 +76,34 @@ static inline bool isClientGained(uint32_t state, uint32_t client_bit_mask) { // Returns true if any of the client is in posted state. static inline bool isAnyClientPosted(uint32_t state) { - uint32_t high_bits = state >> kMaxNumberOfClients; - uint32_t low_bits = state & kLowbitsMask; - uint32_t posted_or_acquired = high_bits ^ low_bits; - return posted_or_acquired & high_bits; + uint32_t highBits = state >> kMaxNumberOfClients; + uint32_t lowBits = state & kLowbitsMask; + uint32_t postedOrAcquired = highBits ^ lowBits; + return postedOrAcquired & highBits; } // Returns true if the input client is in posted state. static inline bool isClientPosted(uint32_t state, uint32_t client_bit_mask) { - uint32_t client_bits = state & client_bit_mask; - if (client_bits == 0U) return false; - uint32_t low_bits = client_bits & kLowbitsMask; - return low_bits == 0U; + uint32_t clientBits = state & client_bit_mask; + if (clientBits == 0U) return false; + uint32_t lowBits = clientBits & kLowbitsMask; + return lowBits == 0U; } // Return true if any of the client is in acquired state. static inline bool isAnyClientAcquired(uint32_t state) { - uint32_t high_bits = state >> kMaxNumberOfClients; - uint32_t low_bits = state & kLowbitsMask; - uint32_t posted_or_acquired = high_bits ^ low_bits; - return posted_or_acquired & low_bits; + uint32_t highBits = state >> kMaxNumberOfClients; + uint32_t lowBits = state & kLowbitsMask; + uint32_t postedOrAcquired = highBits ^ lowBits; + return postedOrAcquired & lowBits; } // Return true if the input client is in acquired state. static inline bool isClientAcquired(uint32_t state, uint32_t client_bit_mask) { - uint32_t client_bits = state & client_bit_mask; - if (client_bits == 0U) return false; - uint32_t high_bits = client_bits & kHighBitsMask; - return high_bits == 0U; + uint32_t clientBits = state & client_bit_mask; + if (clientBits == 0U) return false; + uint32_t highBits = clientBits & kHighBitsMask; + return highBits == 0U; } // Returns true if the input client is in released state. @@ -114,12 +114,12 @@ static inline bool isClientReleased(uint32_t state, uint32_t client_bit_mask) { // Returns the next available buffer client's client_state_masks. // @params union_bits. Union of all existing clients' client_state_masks. static inline uint32_t findNextAvailableClientStateMask(uint32_t union_bits) { - uint32_t low_union = union_bits & kLowbitsMask; - if (low_union == kLowbitsMask) return 0U; - uint32_t incremented = low_union + 1U; - uint32_t difference = incremented ^ low_union; - uint32_t new_low_bit = (difference + 1U) >> 1; - return new_low_bit + (new_low_bit << kMaxNumberOfClients); + uint32_t lowUnion = union_bits & kLowbitsMask; + if (lowUnion == kLowbitsMask) return 0U; + uint32_t incremented = lowUnion + 1U; + uint32_t difference = incremented ^ lowUnion; + uint32_t newLowBit = (difference + 1U) >> 1; + return newLowBit + (newLowBit << kMaxNumberOfClients); } struct __attribute__((aligned(8))) MetadataHeader { @@ -129,22 +129,22 @@ struct __attribute__((aligned(8))) MetadataHeader { // platform (include Apps and vendor HAL). // Every client takes up one bit from the higher 32 bits and one bit from the lower 32 bits in - // buffer_state. - std::atomic buffer_state; + // bufferState. + std::atomic bufferState; - // Every client takes up one bit in fence_state. Only the lower 32 bits are valid. The upper 32 + // Every client takes up one bit in fenceState. Only the lower 32 bits are valid. The upper 32 // bits are there for easier manipulation, but the value should be ignored. - std::atomic fence_state; + std::atomic fenceState; // Every client takes up one bit from the higher 32 bits and one bit from the lower 32 bits in - // active_clients_bit_mask. - std::atomic active_clients_bit_mask; + // activeClientsBitMask. + std::atomic activeClientsBitMask; // Explicit padding 4 bytes. uint32_t padding; // The index of the buffer queue where the buffer belongs to. - uint64_t queue_index; + uint64_t queueIndex; // Public data format, which should be updated with caution. See more details in dvr_api.h DvrNativeBufferMetadata metadata; diff --git a/libs/ui/tests/BufferHubMetadata_test.cpp b/libs/ui/tests/BufferHubMetadata_test.cpp index f02c4fc178..eb978cabc6 100644 --- a/libs/ui/tests/BufferHubMetadata_test.cpp +++ b/libs/ui/tests/BufferHubMetadata_test.cpp @@ -51,7 +51,7 @@ TEST_F(BufferHubMetadataTest, Import_Success) { // Check if the newly allocated buffer is initialized in released state (i.e. // state equals to 0U). - EXPECT_TRUE(mh1->buffer_state.load() == 0U); + EXPECT_TRUE(mh1->bufferState.load() == 0U); EXPECT_TRUE(m2.isValid()); BufferHubDefs::MetadataHeader* mh2 = m2.metadataHeader(); @@ -59,7 +59,7 @@ TEST_F(BufferHubMetadataTest, Import_Success) { // Check if the newly allocated buffer is initialized in released state (i.e. // state equals to 0U). - EXPECT_TRUE(mh2->buffer_state.load() == 0U); + EXPECT_TRUE(mh2->bufferState.load() == 0U); } TEST_F(BufferHubMetadataTest, MoveMetadataInvalidatesOldOne) { diff --git a/libs/vr/libbufferhub/buffer_hub_base.cpp b/libs/vr/libbufferhub/buffer_hub_base.cpp index b28d10147b..17930b4405 100644 --- a/libs/vr/libbufferhub/buffer_hub_base.cpp +++ b/libs/vr/libbufferhub/buffer_hub_base.cpp @@ -122,15 +122,15 @@ int BufferHubBase::ImportBuffer() { // are mapped from shared memory as an atomic object. The std::atomic's // constructor will not be called so that the original value stored in the // memory region will be preserved. - buffer_state_ = &metadata_header_->buffer_state; + buffer_state_ = &metadata_header_->bufferState; ALOGD_IF(TRACE, "BufferHubBase::ImportBuffer: id=%d, buffer_state=%" PRIx32 ".", id(), buffer_state_->load(std::memory_order_acquire)); - fence_state_ = &metadata_header_->fence_state; + fence_state_ = &metadata_header_->fenceState; ALOGD_IF(TRACE, "BufferHubBase::ImportBuffer: id=%d, fence_state=%" PRIx32 ".", id(), fence_state_->load(std::memory_order_acquire)); - active_clients_bit_mask_ = &metadata_header_->active_clients_bit_mask; + active_clients_bit_mask_ = &metadata_header_->activeClientsBitMask; ALOGD_IF( TRACE, "BufferHubBase::ImportBuffer: id=%d, active_clients_bit_mask=%" PRIx32 diff --git a/libs/vr/libbufferhub/include/private/dvr/buffer_hub_base.h b/libs/vr/libbufferhub/include/private/dvr/buffer_hub_base.h index fa39d081e1..8a490d9983 100644 --- a/libs/vr/libbufferhub/include/private/dvr/buffer_hub_base.h +++ b/libs/vr/libbufferhub/include/private/dvr/buffer_hub_base.h @@ -97,8 +97,8 @@ class BufferHubBase : public pdx::Client { uint32_t usage() const { return buffer_.usage(); } uint32_t layer_count() const { return buffer_.layer_count(); } - uint64_t GetQueueIndex() const { return metadata_header_->queue_index; } - void SetQueueIndex(uint64_t index) { metadata_header_->queue_index = index; } + uint64_t GetQueueIndex() const { return metadata_header_->queueIndex; } + void SetQueueIndex(uint64_t index) { metadata_header_->queueIndex = index; } protected: explicit BufferHubBase(LocalChannelHandle channel); diff --git a/services/bufferhub/BufferHubService.cpp b/services/bufferhub/BufferHubService.cpp index ade08e7d17..7a3472fa7e 100644 --- a/services/bufferhub/BufferHubService.cpp +++ b/services/bufferhub/BufferHubService.cpp @@ -241,8 +241,8 @@ Return BufferHubService::debug(const hidl_handle& fd, const hidl_vec(&node->metadata())->metadataHeader(); - const uint32_t state = metadataHeader->buffer_state.load(std::memory_order_acquire); - const uint64_t index = metadataHeader->queue_index; + const uint32_t state = metadataHeader->bufferState.load(std::memory_order_acquire); + const uint64_t index = metadataHeader->queueIndex; stream << std::right; stream << std::setw(6) << /*Id=*/node->id(); diff --git a/services/bufferhub/BufferNode.cpp b/services/bufferhub/BufferNode.cpp index 1efb27ee0a..04ca6493f7 100644 --- a/services/bufferhub/BufferNode.cpp +++ b/services/bufferhub/BufferNode.cpp @@ -15,9 +15,9 @@ void BufferNode::initializeMetadata() { // Using placement new here to reuse shared memory instead of new allocation // Initialize the atomic variables to zero. BufferHubDefs::MetadataHeader* metadataHeader = mMetadata.metadataHeader(); - mBufferState = new (&metadataHeader->buffer_state) std::atomic(0); - mFenceState = new (&metadataHeader->fence_state) std::atomic(0); - mActiveClientsBitMask = new (&metadataHeader->active_clients_bit_mask) std::atomic(0); + mBufferState = new (&metadataHeader->bufferState) std::atomic(0); + mFenceState = new (&metadataHeader->fenceState) std::atomic(0); + mActiveClientsBitMask = new (&metadataHeader->activeClientsBitMask) std::atomic(0); // The C++ standard recommends (but does not require) that lock-free atomic operations are // also address-free, that is, suitable for communication between processes using shared // memory. diff --git a/services/vr/bufferhubd/producer_channel.cpp b/services/vr/bufferhubd/producer_channel.cpp index f3e54a0ed2..b49d89470f 100644 --- a/services/vr/bufferhubd/producer_channel.cpp +++ b/services/vr/bufferhubd/producer_channel.cpp @@ -91,11 +91,10 @@ int ProducerChannel::InitializeBuffer() { // Using placement new here to reuse shared memory instead of new allocation // and also initialize the value to zero. - buffer_state_ = - new (&metadata_header_->buffer_state) std::atomic(0); - fence_state_ = new (&metadata_header_->fence_state) std::atomic(0); + buffer_state_ = new (&metadata_header_->bufferState) std::atomic(0); + fence_state_ = new (&metadata_header_->fenceState) std::atomic(0); active_clients_bit_mask_ = - new (&metadata_header_->active_clients_bit_mask) std::atomic(0); + new (&metadata_header_->activeClientsBitMask) std::atomic(0); // Producer channel is never created after consumer channel, and one buffer // only have one fixed producer for now. Thus, it is correct to assume @@ -183,7 +182,7 @@ BufferHubChannel::BufferInfo ProducerChannel::GetBufferInfo() const { buffer_.height(), buffer_.layer_count(), buffer_.format(), buffer_.usage(), buffer_state_->load(std::memory_order_acquire), - signaled_mask, metadata_header_->queue_index); + signaled_mask, metadata_header_->queueIndex); } void ProducerChannel::HandleImpulse(Message& message) { @@ -547,7 +546,7 @@ Status ProducerChannel::OnConsumerRelease(Message&, "%s: orphaned buffer detected during the this acquire/release cycle: " "id=%d orphaned=0x%" PRIx32 " queue_index=%" PRId64 ".", __FUNCTION__, buffer_id(), orphaned_consumer_bit_mask_, - metadata_header_->queue_index); + metadata_header_->queueIndex); orphaned_consumer_bit_mask_ = 0; } } @@ -581,7 +580,7 @@ void ProducerChannel::OnConsumerOrphaned(const uint32_t& consumer_state_mask) { "consumer_state_mask=%" PRIx32 " queue_index=%" PRId64 " buffer_state=%" PRIx32 " fence_state=%" PRIx32 ".", __FUNCTION__, buffer_id(), consumer_state_mask, - metadata_header_->queue_index, + metadata_header_->queueIndex, buffer_state_->load(std::memory_order_acquire), fence_state_->load(std::memory_order_acquire)); } -- cgit v1.2.3-59-g8ed1b