blob: da19a6fb1d30f5d527fb61ba14b8c912247dd5a3 [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>
Tianyu Jiang2ceb3202018-12-17 12:58:34 -08005#include <log/log.h>
Fan Xu574a6852018-11-02 13:22:42 -07006#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.
Fan Xud34a80a2018-12-04 11:32:39 -080017 BufferHubDefs::MetadataHeader* metadata_header = metadata_.metadata_header();
Tianyu Jianga99f9112018-12-13 18:23:07 -080018 buffer_state_ = new (&metadata_header->buffer_state) std::atomic<uint32_t>(0);
19 fence_state_ = new (&metadata_header->fence_state) std::atomic<uint32_t>(0);
Fan Xu574a6852018-11-02 13:22:42 -070020 active_clients_bit_mask_ =
Tianyu Jianga99f9112018-12-13 18:23:07 -080021 new (&metadata_header->active_clients_bit_mask) std::atomic<uint32_t>(0);
Tianyu Jiang2ceb3202018-12-17 12:58:34 -080022 // The C++ standard recommends (but does not require) that lock-free atomic operations are
23 // also address-free, that is, suitable for communication between processes using shared
24 // memory.
25 LOG_ALWAYS_FATAL_IF(!std::atomic_is_lock_free(buffer_state_) ||
26 !std::atomic_is_lock_free(fence_state_) ||
27 !std::atomic_is_lock_free(active_clients_bit_mask_),
28 "Atomic variables in ashmen are not lock free.");
Fan Xu574a6852018-11-02 13:22:42 -070029}
30
31// Allocates a new BufferNode.
32BufferNode::BufferNode(uint32_t width, uint32_t height, uint32_t layer_count, uint32_t format,
Fan Xuffde7862018-11-08 16:29:13 -080033 uint64_t usage, size_t user_metadata_size, uint32_t id)
34 : mId(id) {
Fan Xu574a6852018-11-02 13:22:42 -070035 uint32_t out_stride = 0;
36 // graphicBufferId is not used in GraphicBufferAllocator::allocate
37 // TODO(b/112338294) After move to the service folder, stop using the
38 // hardcoded service name "bufferhub".
39 int ret = GraphicBufferAllocator::get().allocate(width, height, format, layer_count, usage,
40 const_cast<const native_handle_t**>(
41 &buffer_handle_),
42 &out_stride,
43 /*graphicBufferId=*/0,
44 /*requestor=*/"bufferhub");
45
46 if (ret != OK || buffer_handle_ == nullptr) {
Fan Xu19a3e2b2018-11-12 13:45:33 -080047 ALOGE("%s: Failed to allocate buffer: %s", __FUNCTION__, strerror(-ret));
Fan Xu574a6852018-11-02 13:22:42 -070048 return;
49 }
50
51 buffer_desc_.width = width;
52 buffer_desc_.height = height;
53 buffer_desc_.layers = layer_count;
54 buffer_desc_.format = format;
55 buffer_desc_.usage = usage;
56 buffer_desc_.stride = out_stride;
57
58 metadata_ = BufferHubMetadata::Create(user_metadata_size);
59 if (!metadata_.IsValid()) {
Fan Xu19a3e2b2018-11-12 13:45:33 -080060 ALOGE("%s: Failed to allocate metadata.", __FUNCTION__);
Fan Xu574a6852018-11-02 13:22:42 -070061 return;
62 }
63 InitializeMetadata();
64}
65
Fan Xu574a6852018-11-02 13:22:42 -070066BufferNode::~BufferNode() {
Fan Xuffde7862018-11-08 16:29:13 -080067 // Free the handle
Fan Xu574a6852018-11-02 13:22:42 -070068 if (buffer_handle_ != nullptr) {
69 status_t ret = GraphicBufferAllocator::get().free(buffer_handle_);
70 if (ret != OK) {
Fan Xu19a3e2b2018-11-12 13:45:33 -080071 ALOGE("%s: Failed to free handle; Got error: %d", __FUNCTION__, ret);
Fan Xu574a6852018-11-02 13:22:42 -070072 }
73 }
Fan Xuffde7862018-11-08 16:29:13 -080074
75 // Free the id, if valid
Fan Xu1c16df52018-11-19 16:27:27 -080076 if (id() != BufferHubIdGenerator::kInvalidId) {
77 if (BufferHubIdGenerator::getInstance().freeId(id())) {
Fan Xuffde7862018-11-08 16:29:13 -080078 ALOGI("%s: id #%u is freed.", __FUNCTION__, id());
79 } else {
80 ALOGE("%s: Cannot free nonexistent id #%u", __FUNCTION__, id());
81 }
82 }
Fan Xu574a6852018-11-02 13:22:42 -070083}
84
Tianyu Jianga99f9112018-12-13 18:23:07 -080085uint32_t BufferNode::GetActiveClientsBitMask() const {
Fan Xu574a6852018-11-02 13:22:42 -070086 return active_clients_bit_mask_->load(std::memory_order_acquire);
87}
88
Tianyu Jianga99f9112018-12-13 18:23:07 -080089uint32_t BufferNode::AddNewActiveClientsBitToMask() {
90 uint32_t current_active_clients_bit_mask = GetActiveClientsBitMask();
91 uint32_t client_state_mask = 0U;
92 uint32_t updated_active_clients_bit_mask = 0U;
Fan Xu574a6852018-11-02 13:22:42 -070093 do {
Fan Xud34a80a2018-12-04 11:32:39 -080094 client_state_mask =
95 BufferHubDefs::FindNextAvailableClientStateMask(current_active_clients_bit_mask);
Tianyu Jianga99f9112018-12-13 18:23:07 -080096 if (client_state_mask == 0U) {
Fan Xud34a80a2018-12-04 11:32:39 -080097 ALOGE("%s: reached the maximum number of channels per buffer node: %d.", __FUNCTION__,
98 BufferHubDefs::kMaxNumberOfClients);
Fan Xu574a6852018-11-02 13:22:42 -070099 errno = E2BIG;
Tianyu Jianga99f9112018-12-13 18:23:07 -0800100 return 0U;
Fan Xu574a6852018-11-02 13:22:42 -0700101 }
102 updated_active_clients_bit_mask = current_active_clients_bit_mask | client_state_mask;
103 } while (!(active_clients_bit_mask_->compare_exchange_weak(current_active_clients_bit_mask,
104 updated_active_clients_bit_mask,
105 std::memory_order_acq_rel,
106 std::memory_order_acquire)));
107 return client_state_mask;
108}
109
Tianyu Jianga99f9112018-12-13 18:23:07 -0800110void BufferNode::RemoveClientsBitFromMask(const uint32_t& value) {
Fan Xu574a6852018-11-02 13:22:42 -0700111 active_clients_bit_mask_->fetch_and(~value);
112}
113
114} // namespace implementation
115} // namespace V1_0
116} // namespace bufferhub
117} // namespace frameworks
118} // namespace android