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