Fan Xu | 574a685 | 2018-11-02 13:22:42 -0700 | [diff] [blame] | 1 | #include <errno.h> |
| 2 | |
| 3 | #include <bufferhub/BufferNode.h> |
| 4 | #include <private/dvr/buffer_hub_defs.h> |
| 5 | #include <ui/GraphicBufferAllocator.h> |
| 6 | |
| 7 | namespace android { |
| 8 | namespace frameworks { |
| 9 | namespace bufferhub { |
| 10 | namespace V1_0 { |
| 11 | namespace implementation { |
| 12 | |
| 13 | void BufferNode::InitializeMetadata() { |
| 14 | // Using placement new here to reuse shared memory instead of new allocation |
| 15 | // Initialize the atomic variables to zero. |
| 16 | dvr::BufferHubDefs::MetadataHeader* metadata_header = metadata_.metadata_header(); |
| 17 | buffer_state_ = new (&metadata_header->buffer_state) std::atomic<uint64_t>(0); |
| 18 | fence_state_ = new (&metadata_header->fence_state) std::atomic<uint64_t>(0); |
| 19 | active_clients_bit_mask_ = |
| 20 | new (&metadata_header->active_clients_bit_mask) std::atomic<uint64_t>(0); |
| 21 | } |
| 22 | |
| 23 | // Allocates a new BufferNode. |
| 24 | BufferNode::BufferNode(uint32_t width, uint32_t height, uint32_t layer_count, uint32_t format, |
| 25 | uint64_t usage, size_t user_metadata_size) { |
| 26 | uint32_t out_stride = 0; |
| 27 | // graphicBufferId is not used in GraphicBufferAllocator::allocate |
| 28 | // TODO(b/112338294) After move to the service folder, stop using the |
| 29 | // hardcoded service name "bufferhub". |
| 30 | int ret = GraphicBufferAllocator::get().allocate(width, height, format, layer_count, usage, |
| 31 | const_cast<const native_handle_t**>( |
| 32 | &buffer_handle_), |
| 33 | &out_stride, |
| 34 | /*graphicBufferId=*/0, |
| 35 | /*requestor=*/"bufferhub"); |
| 36 | |
| 37 | if (ret != OK || buffer_handle_ == nullptr) { |
Fan Xu | 19a3e2b | 2018-11-12 13:45:33 -0800 | [diff] [blame] | 38 | ALOGE("%s: Failed to allocate buffer: %s", __FUNCTION__, strerror(-ret)); |
Fan Xu | 574a685 | 2018-11-02 13:22:42 -0700 | [diff] [blame] | 39 | return; |
| 40 | } |
| 41 | |
| 42 | buffer_desc_.width = width; |
| 43 | buffer_desc_.height = height; |
| 44 | buffer_desc_.layers = layer_count; |
| 45 | buffer_desc_.format = format; |
| 46 | buffer_desc_.usage = usage; |
| 47 | buffer_desc_.stride = out_stride; |
| 48 | |
| 49 | metadata_ = BufferHubMetadata::Create(user_metadata_size); |
| 50 | if (!metadata_.IsValid()) { |
Fan Xu | 19a3e2b | 2018-11-12 13:45:33 -0800 | [diff] [blame] | 51 | ALOGE("%s: Failed to allocate metadata.", __FUNCTION__); |
Fan Xu | 574a685 | 2018-11-02 13:22:42 -0700 | [diff] [blame] | 52 | return; |
| 53 | } |
| 54 | InitializeMetadata(); |
| 55 | } |
| 56 | |
| 57 | // Free the handle |
| 58 | BufferNode::~BufferNode() { |
| 59 | if (buffer_handle_ != nullptr) { |
| 60 | status_t ret = GraphicBufferAllocator::get().free(buffer_handle_); |
| 61 | if (ret != OK) { |
Fan Xu | 19a3e2b | 2018-11-12 13:45:33 -0800 | [diff] [blame] | 62 | ALOGE("%s: Failed to free handle; Got error: %d", __FUNCTION__, ret); |
Fan Xu | 574a685 | 2018-11-02 13:22:42 -0700 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | uint64_t BufferNode::GetActiveClientsBitMask() const { |
| 68 | return active_clients_bit_mask_->load(std::memory_order_acquire); |
| 69 | } |
| 70 | |
| 71 | uint64_t BufferNode::AddNewActiveClientsBitToMask() { |
| 72 | uint64_t current_active_clients_bit_mask = GetActiveClientsBitMask(); |
| 73 | uint64_t client_state_mask = 0ULL; |
| 74 | uint64_t updated_active_clients_bit_mask = 0ULL; |
| 75 | do { |
| 76 | client_state_mask = dvr::BufferHubDefs::FindNextAvailableClientStateMask( |
| 77 | current_active_clients_bit_mask); |
| 78 | if (client_state_mask == 0ULL) { |
Fan Xu | 19a3e2b | 2018-11-12 13:45:33 -0800 | [diff] [blame] | 79 | ALOGE("%s: reached the maximum number of channels per buffer node: 32.", __FUNCTION__); |
Fan Xu | 574a685 | 2018-11-02 13:22:42 -0700 | [diff] [blame] | 80 | errno = E2BIG; |
| 81 | return 0ULL; |
| 82 | } |
| 83 | updated_active_clients_bit_mask = current_active_clients_bit_mask | client_state_mask; |
| 84 | } while (!(active_clients_bit_mask_->compare_exchange_weak(current_active_clients_bit_mask, |
| 85 | updated_active_clients_bit_mask, |
| 86 | std::memory_order_acq_rel, |
| 87 | std::memory_order_acquire))); |
| 88 | return client_state_mask; |
| 89 | } |
| 90 | |
| 91 | void BufferNode::RemoveClientsBitFromMask(const uint64_t& value) { |
| 92 | active_clients_bit_mask_->fetch_and(~value); |
| 93 | } |
| 94 | |
| 95 | } // namespace implementation |
| 96 | } // namespace V1_0 |
| 97 | } // namespace bufferhub |
| 98 | } // namespace frameworks |
| 99 | } // namespace android |