• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <errno.h>
2 
3 #include <bufferhub/BufferHubService.h>
4 #include <bufferhub/BufferNode.h>
5 #include <log/log.h>
6 #include <ui/GraphicBufferAllocator.h>
7 
8 namespace android {
9 namespace frameworks {
10 namespace bufferhub {
11 namespace V1_0 {
12 namespace implementation {
13 
initializeMetadata()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     BufferHubDefs::MetadataHeader* metadataHeader = mMetadata.metadataHeader();
18     mBufferState = new (&metadataHeader->bufferState) std::atomic<uint32_t>(0);
19     mFenceState = new (&metadataHeader->fenceState) std::atomic<uint32_t>(0);
20     mActiveClientsBitMask = new (&metadataHeader->activeClientsBitMask) std::atomic<uint32_t>(0);
21     // The C++ standard recommends (but does not require) that lock-free atomic operations are
22     // also address-free, that is, suitable for communication between processes using shared
23     // memory.
24     LOG_ALWAYS_FATAL_IF(!std::atomic_is_lock_free(mBufferState) ||
25                                 !std::atomic_is_lock_free(mFenceState) ||
26                                 !std::atomic_is_lock_free(mActiveClientsBitMask),
27                         "Atomic variables in ashmen are not lock free.");
28 }
29 
30 // Allocates a new BufferNode.
BufferNode(uint32_t width,uint32_t height,uint32_t layerCount,uint32_t format,uint64_t usage,size_t userMetadataSize,int id)31 BufferNode::BufferNode(uint32_t width, uint32_t height, uint32_t layerCount, uint32_t format,
32                        uint64_t usage, size_t userMetadataSize, int id)
33       : mId(id) {
34     uint32_t outStride = 0;
35     // graphicBufferId is not used in GraphicBufferAllocator::allocate
36     // TODO(b/112338294) After move to the service folder, stop using the
37     // hardcoded service name "bufferhub".
38     int ret = GraphicBufferAllocator::get().allocate(width, height, format, layerCount, usage,
39                                                      const_cast<const native_handle_t**>(
40                                                              &mBufferHandle),
41                                                      &outStride,
42                                                      /*graphicBufferId=*/0,
43                                                      /*requestor=*/"bufferhub");
44 
45     if (ret != OK || mBufferHandle == nullptr) {
46         ALOGE("%s: Failed to allocate buffer: %s", __FUNCTION__, strerror(-ret));
47         return;
48     }
49 
50     mBufferDesc.width = width;
51     mBufferDesc.height = height;
52     mBufferDesc.layers = layerCount;
53     mBufferDesc.format = format;
54     mBufferDesc.usage = usage;
55     mBufferDesc.stride = outStride;
56 
57     mMetadata = BufferHubMetadata::create(userMetadataSize);
58     if (!mMetadata.isValid()) {
59         ALOGE("%s: Failed to allocate metadata.", __FUNCTION__);
60         return;
61     }
62     initializeMetadata();
63 }
64 
~BufferNode()65 BufferNode::~BufferNode() {
66     // Free the handle
67     if (mBufferHandle != nullptr) {
68         status_t ret = GraphicBufferAllocator::get().free(mBufferHandle);
69         if (ret != OK) {
70             ALOGE("%s: Failed to free handle; Got error: %d", __FUNCTION__, ret);
71         }
72     }
73 
74     // Free the id, if valid
75     if (mId >= 0) {
76         BufferHubIdGenerator::getInstance().freeId(mId);
77     }
78 }
79 
getActiveClientsBitMask() const80 uint32_t BufferNode::getActiveClientsBitMask() const {
81     return mActiveClientsBitMask->load(std::memory_order_acquire);
82 }
83 
addNewActiveClientsBitToMask()84 uint32_t BufferNode::addNewActiveClientsBitToMask() {
85     uint32_t currentActiveClientsBitMask = getActiveClientsBitMask();
86     uint32_t clientStateMask = 0U;
87     uint32_t updatedActiveClientsBitMask = 0U;
88     do {
89         clientStateMask =
90                 BufferHubDefs::findNextAvailableClientStateMask(currentActiveClientsBitMask);
91         if (clientStateMask == 0U) {
92             ALOGE("%s: reached the maximum number of channels per buffer node: %d.", __FUNCTION__,
93                   BufferHubDefs::kMaxNumberOfClients);
94             errno = E2BIG;
95             return 0U;
96         }
97         updatedActiveClientsBitMask = currentActiveClientsBitMask | clientStateMask;
98     } while (!(mActiveClientsBitMask->compare_exchange_weak(currentActiveClientsBitMask,
99                                                             updatedActiveClientsBitMask,
100                                                             std::memory_order_acq_rel,
101                                                             std::memory_order_acquire)));
102     return clientStateMask;
103 }
104 
removeClientsBitFromMask(const uint32_t & value)105 void BufferNode::removeClientsBitFromMask(const uint32_t& value) {
106     mActiveClientsBitMask->fetch_and(~value);
107 }
108 
109 } // namespace implementation
110 } // namespace V1_0
111 } // namespace bufferhub
112 } // namespace frameworks
113 } // namespace android
114