1 #ifndef ANDROID_FRAMEWORKS_BUFFERHUB_V1_0_BUFFER_NODE_H_
2 #define ANDROID_FRAMEWORKS_BUFFERHUB_V1_0_BUFFER_NODE_H_
3 
4 #include <android/hardware_buffer.h>
5 #include <bufferhub/BufferHubIdGenerator.h>
6 #include <cutils/native_handle.h>
7 #include <ui/BufferHubEventFd.h>
8 #include <ui/BufferHubMetadata.h>
9 
10 namespace android {
11 namespace frameworks {
12 namespace bufferhub {
13 namespace V1_0 {
14 namespace implementation {
15 
16 class BufferNode {
17 public:
18     // Allocates a new BufferNode.
19     BufferNode(uint32_t width, uint32_t height, uint32_t layerCount, uint32_t format,
20                uint64_t usage, size_t userMetadataSize, int id = -1);
21 
22     ~BufferNode();
23 
24     // Returns whether the object holds a valid metadata.
isValid()25     bool isValid() const { return mMetadata.isValid(); }
26 
id()27     int id() const { return mId; }
28 
userMetadataSize()29     size_t userMetadataSize() const { return mMetadata.userMetadataSize(); }
30 
31     // Accessors of the buffer description and handle
bufferHandle()32     const native_handle_t* bufferHandle() const { return mBufferHandle; }
bufferDesc()33     const AHardwareBuffer_Desc& bufferDesc() const { return mBufferDesc; }
34 
35     // Accessor of event fd.
eventFd()36     const BufferHubEventFd& eventFd() const { return mEventFd; }
37 
38     // Accessors of mMetadata.
metadata()39     const BufferHubMetadata& metadata() const { return mMetadata; }
40 
41     // Gets the current value of mActiveClientsBitMask in mMetadata with
42     // std::memory_order_acquire, so that all previous releases of
43     // mActiveClientsBitMask from all threads will be returned here.
44     uint32_t getActiveClientsBitMask() const;
45 
46     // Find and add a new client state mask to mActiveClientsBitMask in
47     // mMetadata.
48     // Return the new client state mask that is added to mActiveClientsBitMask.
49     // Return 0U if there are already 16 clients of the buffer.
50     uint32_t addNewActiveClientsBitToMask();
51 
52     // Removes the value from active_clients_bit_mask in mMetadata with
53     // std::memory_order_release, so that the change will be visible to any
54     // acquire of mActiveClientsBitMask in any threads after the succeed of
55     // this operation.
56     void removeClientsBitFromMask(const uint32_t& value);
57 
58 private:
59     // Helper method for constructors to initialize atomic metadata header
60     // variables in shared memory.
61     void initializeMetadata();
62 
63     // Gralloc buffer handles.
64     native_handle_t* mBufferHandle;
65     AHardwareBuffer_Desc mBufferDesc;
66 
67     // Eventfd used for signalling buffer events among the clients of the buffer.
68     BufferHubEventFd mEventFd;
69 
70     // Metadata in shared memory.
71     BufferHubMetadata mMetadata;
72 
73     // A system-unique id generated by bufferhub from 0 to std::numeric_limits<int>::max().
74     // BufferNodes not created by bufferhub will have id < 0, meaning "not specified".
75     // TODO(b/118891412): remove default id = -1 and update comments after pdx is no longer in use
76     const int mId = -1;
77 
78     // The following variables are atomic variables in mMetadata that are visible
79     // to Bn object and Bp objects. Please find more info in
80     // BufferHubDefs::MetadataHeader.
81 
82     // mBufferState tracks the state of the buffer. Buffer can be in one of these
83     // four states: gained, posted, acquired, released.
84     std::atomic<uint32_t>* mBufferState = nullptr;
85 
86     // TODO(b/112012161): add comments to mFenceState.
87     std::atomic<uint32_t>* mFenceState = nullptr;
88 
89     // mActiveClientsBitMask tracks all the bp clients of the buffer. It is the
90     // union of all client_state_mask of all bp clients.
91     std::atomic<uint32_t>* mActiveClientsBitMask = nullptr;
92 };
93 
94 } // namespace implementation
95 } // namespace V1_0
96 } // namespace bufferhub
97 } // namespace frameworks
98 } // namespace android
99 
100 #endif // ANDROID_FRAMEWORKS_BUFFERHUB_V1_0_BUFFER_NODE_H_
101