1 #ifndef ANDROID_DVR_BUFFER_HUB_DEFS_H_
2 #define ANDROID_DVR_BUFFER_HUB_DEFS_H_
3 
4 #include <dvr/dvr_api.h>
5 #include <hardware/gralloc.h>
6 #include <pdx/channel_handle.h>
7 #include <pdx/file_handle.h>
8 #include <pdx/rpc/remote_method.h>
9 #include <pdx/rpc/serializable.h>
10 #include <private/dvr/native_handle_wrapper.h>
11 #include <ui/BufferHubDefs.h>
12 
13 namespace android {
14 namespace dvr {
15 
16 namespace BufferHubDefs {
17 
18 static constexpr uint32_t kMetadataFormat = HAL_PIXEL_FORMAT_BLOB;
19 static constexpr uint32_t kMetadataUsage =
20     GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN;
21 
22 // See more details in libs/ui/include/ui/BufferHubDefs.h
23 static constexpr int kMaxNumberOfClients =
24     android::BufferHubDefs::kMaxNumberOfClients;
25 static constexpr uint32_t kLowbitsMask = android::BufferHubDefs::kLowbitsMask;
26 static constexpr uint32_t kHighBitsMask = android::BufferHubDefs::kHighBitsMask;
27 static constexpr uint32_t kFirstClientBitMask =
28     android::BufferHubDefs::kFirstClientBitMask;
29 
isAnyClientGained(uint32_t state)30 static inline bool isAnyClientGained(uint32_t state) {
31   return android::BufferHubDefs::isAnyClientGained(state);
32 }
33 
isClientGained(uint32_t state,uint32_t client_bit_mask)34 static inline bool isClientGained(uint32_t state, uint32_t client_bit_mask) {
35   return android::BufferHubDefs::isClientGained(state, client_bit_mask);
36 }
37 
isAnyClientPosted(uint32_t state)38 static inline bool isAnyClientPosted(uint32_t state) {
39   return android::BufferHubDefs::isAnyClientPosted(state);
40 }
41 
isClientPosted(uint32_t state,uint32_t client_bit_mask)42 static inline bool isClientPosted(uint32_t state, uint32_t client_bit_mask) {
43   return android::BufferHubDefs::isClientPosted(state, client_bit_mask);
44 }
45 
isAnyClientAcquired(uint32_t state)46 static inline bool isAnyClientAcquired(uint32_t state) {
47   return android::BufferHubDefs::isAnyClientAcquired(state);
48 }
49 
isClientAcquired(uint32_t state,uint32_t client_bit_mask)50 static inline bool isClientAcquired(uint32_t state, uint32_t client_bit_mask) {
51   return android::BufferHubDefs::isClientAcquired(state, client_bit_mask);
52 }
53 
isClientReleased(uint32_t state,uint32_t client_bit_mask)54 static inline bool isClientReleased(uint32_t state, uint32_t client_bit_mask) {
55   return android::BufferHubDefs::isClientReleased(state, client_bit_mask);
56 }
57 
58 // Returns the next available buffer client's client_state_masks.
59 // @params union_bits. Union of all existing clients' client_state_masks.
findNextAvailableClientStateMask(uint32_t union_bits)60 static inline uint32_t findNextAvailableClientStateMask(uint32_t union_bits) {
61   return android::BufferHubDefs::findNextAvailableClientStateMask(union_bits);
62 }
63 
64 using MetadataHeader = android::BufferHubDefs::MetadataHeader;
65 static constexpr size_t kMetadataHeaderSize =
66     android::BufferHubDefs::kMetadataHeaderSize;
67 
68 }  // namespace BufferHubDefs
69 
70 template <typename FileHandleType>
71 class BufferTraits {
72  public:
73   BufferTraits() = default;
BufferTraits(const native_handle_t * buffer_handle,const FileHandleType & metadata_handle,int id,uint32_t client_state_mask,uint64_t metadata_size,uint32_t width,uint32_t height,uint32_t layer_count,uint32_t format,uint64_t usage,uint32_t stride,const FileHandleType & acquire_fence_fd,const FileHandleType & release_fence_fd)74   BufferTraits(const native_handle_t* buffer_handle,
75                const FileHandleType& metadata_handle, int id,
76                uint32_t client_state_mask, uint64_t metadata_size,
77                uint32_t width, uint32_t height, uint32_t layer_count,
78                uint32_t format, uint64_t usage, uint32_t stride,
79                const FileHandleType& acquire_fence_fd,
80                const FileHandleType& release_fence_fd)
81       : id_(id),
82         client_state_mask_(client_state_mask),
83         metadata_size_(metadata_size),
84         width_(width),
85         height_(height),
86         layer_count_(layer_count),
87         format_(format),
88         usage_(usage),
89         stride_(stride),
90         buffer_handle_(buffer_handle),
91         metadata_handle_(metadata_handle.Borrow()),
92         acquire_fence_fd_(acquire_fence_fd.Borrow()),
93         release_fence_fd_(release_fence_fd.Borrow()) {}
94 
95   BufferTraits(BufferTraits&& other) = default;
96   BufferTraits& operator=(BufferTraits&& other) = default;
97 
98   // ID of the buffer client. All BufferHubBuffer clients derived from the same
99   // buffer in bufferhubd share the same buffer id.
id()100   int id() const { return id_; }
101 
102   // State mask of the buffer client. Each BufferHubBuffer client backed by the
103   // same buffer channel has uniqued state bit among its siblings. For a
104   // producer buffer the bit must be kFirstClientBitMask; for a consumer the bit
105   // must be one of the kConsumerStateMask.
client_state_mask()106   uint32_t client_state_mask() const { return client_state_mask_; }
metadata_size()107   uint64_t metadata_size() const { return metadata_size_; }
108 
width()109   uint32_t width() { return width_; }
height()110   uint32_t height() { return height_; }
layer_count()111   uint32_t layer_count() { return layer_count_; }
format()112   uint32_t format() { return format_; }
usage()113   uint64_t usage() { return usage_; }
stride()114   uint32_t stride() { return stride_; }
115 
buffer_handle()116   const NativeHandleWrapper<FileHandleType>& buffer_handle() const {
117     return buffer_handle_;
118   }
119 
take_buffer_handle()120   NativeHandleWrapper<FileHandleType> take_buffer_handle() {
121     return std::move(buffer_handle_);
122   }
take_metadata_handle()123   FileHandleType take_metadata_handle() { return std::move(metadata_handle_); }
take_acquire_fence()124   FileHandleType take_acquire_fence() { return std::move(acquire_fence_fd_); }
take_release_fence()125   FileHandleType take_release_fence() { return std::move(release_fence_fd_); }
126 
127  private:
128   // BufferHub specific traits.
129   int id_ = -1;
130   uint32_t client_state_mask_;
131   uint64_t metadata_size_;
132 
133   // Traits for a GraphicBuffer.
134   uint32_t width_;
135   uint32_t height_;
136   uint32_t layer_count_;
137   uint32_t format_;
138   uint64_t usage_;
139   uint32_t stride_;
140 
141   // Native handle for the graphic buffer.
142   NativeHandleWrapper<FileHandleType> buffer_handle_;
143 
144   // File handle of an ashmem that holds buffer metadata.
145   FileHandleType metadata_handle_;
146 
147   // Pamameters for shared fences.
148   FileHandleType acquire_fence_fd_;
149   FileHandleType release_fence_fd_;
150 
151   PDX_SERIALIZABLE_MEMBERS(BufferTraits<FileHandleType>, id_,
152                            client_state_mask_, metadata_size_, stride_, width_,
153                            height_, layer_count_, format_, usage_,
154                            buffer_handle_, metadata_handle_, acquire_fence_fd_,
155                            release_fence_fd_);
156 
157   BufferTraits(const BufferTraits&) = delete;
158   void operator=(const BufferTraits&) = delete;
159 };
160 
161 }  // namespace dvr
162 }  // namespace android
163 
164 #endif  // ANDROID_DVR_BUFFER_HUB_DEFS_H_
165