blob: 715d0a1f679966d56f1b42588418c391a5e6b38e [file] [log] [blame]
Fan Xu574a6852018-11-02 13:22:42 -07001#include <errno.h>
2
Fan Xuffde7862018-11-08 16:29:13 -08003#include <bufferhub/BufferHubService.h>
Fan Xu574a6852018-11-02 13:22:42 -07004#include <bufferhub/BufferNode.h>
5#include <private/dvr/buffer_hub_defs.h>
6#include <ui/GraphicBufferAllocator.h>
7
8namespace android {
9namespace frameworks {
10namespace bufferhub {
11namespace V1_0 {
12namespace implementation {
13
14void 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.
25BufferNode::BufferNode(uint32_t width, uint32_t height, uint32_t layer_count, uint32_t format,
Fan Xuffde7862018-11-08 16:29:13 -080026 uint64_t usage, size_t user_metadata_size, uint32_t id)
27 : mId(id) {
Fan Xu574a6852018-11-02 13:22:42 -070028 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 Xu19a3e2b2018-11-12 13:45:33 -080040 ALOGE("%s: Failed to allocate buffer: %s", __FUNCTION__, strerror(-ret));
Fan Xu574a6852018-11-02 13:22:42 -070041 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 Xu19a3e2b2018-11-12 13:45:33 -080053 ALOGE("%s: Failed to allocate metadata.", __FUNCTION__);
Fan Xu574a6852018-11-02 13:22:42 -070054 return;
55 }
56 InitializeMetadata();
57}
58
Fan Xu574a6852018-11-02 13:22:42 -070059BufferNode::~BufferNode() {
Fan Xuffde7862018-11-08 16:29:13 -080060 // Free the handle
Fan Xu574a6852018-11-02 13:22:42 -070061 if (buffer_handle_ != nullptr) {
62 status_t ret = GraphicBufferAllocator::get().free(buffer_handle_);
63 if (ret != OK) {
Fan Xu19a3e2b2018-11-12 13:45:33 -080064 ALOGE("%s: Failed to free handle; Got error: %d", __FUNCTION__, ret);
Fan Xu574a6852018-11-02 13:22:42 -070065 }
66 }
Fan Xuffde7862018-11-08 16:29:13 -080067
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 Xu574a6852018-11-02 13:22:42 -070076}
77
78uint64_t BufferNode::GetActiveClientsBitMask() const {
79 return active_clients_bit_mask_->load(std::memory_order_acquire);
80}
81
82uint64_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 Xu19a3e2b2018-11-12 13:45:33 -080090 ALOGE("%s: reached the maximum number of channels per buffer node: 32.", __FUNCTION__);
Fan Xu574a6852018-11-02 13:22:42 -070091 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
102void 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