Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 1 | #ifndef ANDROID_DVR_BUFFER_HUB_CLIENT_H_ |
| 2 | #define ANDROID_DVR_BUFFER_HUB_CLIENT_H_ |
| 3 | |
| 4 | #include <hardware/gralloc.h> |
| 5 | #include <pdx/channel_handle.h> |
| 6 | #include <pdx/client.h> |
| 7 | #include <pdx/file_handle.h> |
| 8 | #include <pdx/status.h> |
| 9 | |
| 10 | #include <vector> |
| 11 | |
| 12 | #include <private/dvr/ion_buffer.h> |
| 13 | |
Corey Tabaka | 52ea25c | 2017-09-13 18:02:48 -0700 | [diff] [blame] | 14 | #include "bufferhub_rpc.h" |
| 15 | |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 16 | namespace android { |
| 17 | namespace dvr { |
| 18 | |
| 19 | class BufferHubBuffer : public pdx::Client { |
| 20 | public: |
| 21 | using LocalHandle = pdx::LocalHandle; |
| 22 | using LocalChannelHandle = pdx::LocalChannelHandle; |
| 23 | template <typename T> |
| 24 | using Status = pdx::Status<T>; |
| 25 | |
| 26 | // Create a new consumer channel that is attached to the producer. Returns |
| 27 | // a file descriptor for the new channel or a negative error code. |
| 28 | Status<LocalChannelHandle> CreateConsumer(); |
| 29 | |
| 30 | // Polls the fd for |timeout_ms| milliseconds (-1 for infinity). |
| 31 | int Poll(int timeout_ms); |
| 32 | |
| 33 | // Locks the area specified by (x, y, width, height) for a specific usage. If |
| 34 | // the usage is software then |addr| will be updated to point to the address |
| 35 | // of the buffer in virtual memory. The caller should only access/modify the |
| 36 | // pixels in the specified area. anything else is undefined behavior. |
Hendrik Wagenaar | 4d3590f | 2017-05-06 22:36:04 -0700 | [diff] [blame] | 37 | int Lock(int usage, int x, int y, int width, int height, void** addr); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 38 | |
| 39 | // Must be called after Lock() when the caller has finished changing the |
| 40 | // buffer. |
Hendrik Wagenaar | 4d3590f | 2017-05-06 22:36:04 -0700 | [diff] [blame] | 41 | int Unlock(); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 42 | |
| 43 | // Gets a blob buffer that was created with BufferProducer::CreateBlob. |
| 44 | // Locking and Unlocking is handled internally. There's no need to Unlock |
| 45 | // after calling this method. |
| 46 | int GetBlobReadWritePointer(size_t size, void** addr); |
| 47 | |
| 48 | // Gets a blob buffer that was created with BufferProducer::CreateBlob. |
| 49 | // Locking and Unlocking is handled internally. There's no need to Unlock |
| 50 | // after calling this method. |
| 51 | int GetBlobReadOnlyPointer(size_t size, void** addr); |
| 52 | |
| 53 | // Returns a dup'd file descriptor for accessing the blob shared memory. The |
| 54 | // caller takes ownership of the file descriptor and must close it or pass on |
| 55 | // ownership. Some GPU API extensions can take file descriptors to bind shared |
| 56 | // memory gralloc buffers to GPU buffer objects. |
| 57 | LocalHandle GetBlobFd() const { |
| 58 | // Current GPU vendor puts the buffer allocation in one FD. If we change GPU |
| 59 | // vendors and this is the wrong fd, late-latching and EDS will very clearly |
| 60 | // stop working and we will need to correct this. The alternative is to use |
| 61 | // a GL context in the pose service to allocate this buffer or to use the |
| 62 | // ION API directly instead of gralloc. |
| 63 | return LocalHandle(dup(native_handle()->data[0])); |
| 64 | } |
| 65 | |
Hendrik Wagenaar | 10e68eb | 2017-03-15 13:29:02 -0700 | [diff] [blame] | 66 | // Get up to |max_fds_count| file descriptors for accessing the blob shared |
| 67 | // memory. |fds_count| will contain the actual number of file descriptors. |
| 68 | void GetBlobFds(int* fds, size_t* fds_count, size_t max_fds_count) const; |
| 69 | |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 70 | using Client::event_fd; |
Corey Tabaka | 3079cb7 | 2017-01-19 15:07:26 -0800 | [diff] [blame] | 71 | |
| 72 | Status<int> GetEventMask(int events) { |
| 73 | if (auto* client_channel = GetChannel()) { |
| 74 | return client_channel->GetEventMask(events); |
| 75 | } else { |
| 76 | return pdx::ErrorStatus(EINVAL); |
| 77 | } |
| 78 | } |
| 79 | |
Corey Tabaka | 52ea25c | 2017-09-13 18:02:48 -0700 | [diff] [blame] | 80 | std::vector<pdx::ClientChannel::EventSource> GetEventSources() const { |
| 81 | if (auto* client_channel = GetChannel()) { |
| 82 | return client_channel->GetEventSources(); |
| 83 | } else { |
| 84 | return {}; |
| 85 | } |
| 86 | } |
| 87 | |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 88 | native_handle_t* native_handle() const { |
Hendrik Wagenaar | 4d3590f | 2017-05-06 22:36:04 -0700 | [diff] [blame] | 89 | return const_cast<native_handle_t*>(buffer_.handle()); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Hendrik Wagenaar | 4d3590f | 2017-05-06 22:36:04 -0700 | [diff] [blame] | 92 | IonBuffer* buffer() { return &buffer_; } |
| 93 | const IonBuffer* buffer() const { return &buffer_; } |
Corey Tabaka | 2655e1c | 2017-04-04 11:07:05 -0700 | [diff] [blame] | 94 | |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 95 | int id() const { return id_; } |
| 96 | |
Jiwen 'Steve' Cai | 9e7f303 | 2017-10-20 18:39:47 -0700 | [diff] [blame] | 97 | // Returns the buffer buffer state. |
| 98 | uint64_t buffer_state() { return buffer_state_->load(); }; |
| 99 | |
Corey Tabaka | 52ea25c | 2017-09-13 18:02:48 -0700 | [diff] [blame] | 100 | // A state mask which is unique to a buffer hub client among all its siblings |
| 101 | // sharing the same concrete graphic buffer. |
| 102 | uint64_t buffer_state_bit() const { return buffer_state_bit_; } |
| 103 | |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 104 | // The following methods return settings of the first buffer. Currently, |
| 105 | // it is only possible to create multi-buffer BufferHubBuffers with the same |
| 106 | // settings. |
Hendrik Wagenaar | 4d3590f | 2017-05-06 22:36:04 -0700 | [diff] [blame] | 107 | uint32_t width() const { return buffer_.width(); } |
| 108 | uint32_t height() const { return buffer_.height(); } |
| 109 | uint32_t stride() const { return buffer_.stride(); } |
| 110 | uint32_t format() const { return buffer_.format(); } |
| 111 | uint32_t usage() const { return buffer_.usage(); } |
| 112 | uint32_t layer_count() const { return buffer_.layer_count(); } |
Jiwen 'Steve' Cai | 044963e | 2017-05-01 22:43:21 -0700 | [diff] [blame] | 113 | |
Corey Tabaka | 52ea25c | 2017-09-13 18:02:48 -0700 | [diff] [blame] | 114 | uint64_t GetQueueIndex() const { return metadata_header_->queue_index; } |
| 115 | void SetQueueIndex(uint64_t index) { metadata_header_->queue_index = index; } |
| 116 | |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 117 | protected: |
| 118 | explicit BufferHubBuffer(LocalChannelHandle channel); |
| 119 | explicit BufferHubBuffer(const std::string& endpoint_path); |
| 120 | virtual ~BufferHubBuffer(); |
| 121 | |
| 122 | // Initialization helper. |
| 123 | int ImportBuffer(); |
| 124 | |
Corey Tabaka | 52ea25c | 2017-09-13 18:02:48 -0700 | [diff] [blame] | 125 | // Check invalid metadata operation. Returns 0 if requested metadata is valid. |
| 126 | int CheckMetadata(size_t user_metadata_size) const; |
| 127 | |
| 128 | // Send out the new fence by updating the shared fence (shared_release_fence |
| 129 | // for producer and shared_acquire_fence for consumer). Note that during this |
| 130 | // should only be used in LocalPost() or LocalRelease, and the shared fence |
| 131 | // shouldn't be poll'ed by the other end. |
| 132 | int UpdateSharedFence(const LocalHandle& new_fence, |
| 133 | const LocalHandle& shared_fence); |
| 134 | |
| 135 | // IonBuffer that is shared between bufferhubd, producer, and consumers. |
| 136 | size_t metadata_buf_size_{0}; |
| 137 | size_t user_metadata_size_{0}; |
| 138 | BufferHubDefs::MetadataHeader* metadata_header_{nullptr}; |
| 139 | void* user_metadata_ptr_{nullptr}; |
| 140 | std::atomic<uint64_t>* buffer_state_{nullptr}; |
| 141 | std::atomic<uint64_t>* fence_state_{nullptr}; |
| 142 | |
| 143 | LocalHandle shared_acquire_fence_; |
| 144 | LocalHandle shared_release_fence_; |
| 145 | |
| 146 | // A local fence fd that holds the ownership of the fence fd on Post (for |
| 147 | // producer) and Release (for consumer). |
| 148 | LocalHandle pending_fence_fd_; |
| 149 | |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 150 | private: |
| 151 | BufferHubBuffer(const BufferHubBuffer&) = delete; |
| 152 | void operator=(const BufferHubBuffer&) = delete; |
| 153 | |
| 154 | // Global id for the buffer that is consistent across processes. It is meant |
| 155 | // for logging and debugging purposes only and should not be used for lookup |
| 156 | // or any other functional purpose as a security precaution. |
| 157 | int id_; |
Corey Tabaka | 52ea25c | 2017-09-13 18:02:48 -0700 | [diff] [blame] | 158 | uint64_t buffer_state_bit_{0ULL}; |
Hendrik Wagenaar | 4d3590f | 2017-05-06 22:36:04 -0700 | [diff] [blame] | 159 | IonBuffer buffer_; |
Corey Tabaka | 52ea25c | 2017-09-13 18:02:48 -0700 | [diff] [blame] | 160 | IonBuffer metadata_buffer_; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | // This represents a writable buffer. Calling Post notifies all clients and |
| 164 | // makes the buffer read-only. Call Gain to acquire write access. A buffer |
| 165 | // may have many consumers. |
| 166 | // |
| 167 | // The user of BufferProducer is responsible with making sure that the Post() is |
| 168 | // done with the correct metadata type and size. The user is also responsible |
| 169 | // for making sure that remote ends (BufferConsumers) are also using the correct |
| 170 | // metadata when acquiring the buffer. The API guarantees that a Post() with a |
| 171 | // metadata of wrong size will fail. However, it currently does not do any |
| 172 | // type checking. |
| 173 | // The API also assumes that metadata is a serializable type (plain old data). |
| 174 | class BufferProducer : public pdx::ClientBase<BufferProducer, BufferHubBuffer> { |
| 175 | public: |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 176 | // Imports a bufferhub producer channel, assuming ownership of its handle. |
| 177 | static std::unique_ptr<BufferProducer> Import(LocalChannelHandle channel); |
| 178 | static std::unique_ptr<BufferProducer> Import( |
| 179 | Status<LocalChannelHandle> status); |
| 180 | |
Corey Tabaka | 52ea25c | 2017-09-13 18:02:48 -0700 | [diff] [blame] | 181 | // Asynchronously posts a buffer. The fence and metadata are passed to |
| 182 | // consumer via shared fd and shared memory. |
| 183 | int PostAsync(const DvrNativeBufferMetadata* meta, |
| 184 | const LocalHandle& ready_fence); |
| 185 | |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 186 | // Post this buffer, passing |ready_fence| to the consumers. The bytes in |
| 187 | // |meta| are passed unaltered to the consumers. The producer must not modify |
| 188 | // the buffer until it is re-gained. |
| 189 | // This returns zero or a negative unix error code. |
| 190 | int Post(const LocalHandle& ready_fence, const void* meta, |
Corey Tabaka | 52ea25c | 2017-09-13 18:02:48 -0700 | [diff] [blame] | 191 | size_t user_metadata_size); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 192 | |
| 193 | template <typename Meta, |
| 194 | typename = typename std::enable_if<std::is_void<Meta>::value>::type> |
| 195 | int Post(const LocalHandle& ready_fence) { |
| 196 | return Post(ready_fence, nullptr, 0); |
| 197 | } |
Corey Tabaka | 2655e1c | 2017-04-04 11:07:05 -0700 | [diff] [blame] | 198 | template <typename Meta, typename = typename std::enable_if< |
| 199 | !std::is_void<Meta>::value>::type> |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 200 | int Post(const LocalHandle& ready_fence, const Meta& meta) { |
| 201 | return Post(ready_fence, &meta, sizeof(meta)); |
| 202 | } |
| 203 | |
| 204 | // Attempt to re-gain the buffer for writing. If |release_fence| is valid, it |
| 205 | // must be waited on before using the buffer. If it is not valid then the |
| 206 | // buffer is free for immediate use. This call will only succeed if the buffer |
| 207 | // is in the released state. |
| 208 | // This returns zero or a negative unix error code. |
| 209 | int Gain(LocalHandle* release_fence); |
Corey Tabaka | 52ea25c | 2017-09-13 18:02:48 -0700 | [diff] [blame] | 210 | int GainAsync(); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 211 | |
| 212 | // Asynchronously marks a released buffer as gained. This method is similar to |
| 213 | // the synchronous version above, except that it does not wait for BufferHub |
Corey Tabaka | 52ea25c | 2017-09-13 18:02:48 -0700 | [diff] [blame] | 214 | // to acknowledge success or failure. Because of the asynchronous nature of |
| 215 | // the underlying message, no error is returned if this method is called when |
| 216 | // the buffer is in an incorrect state. Returns zero if sending the message |
| 217 | // succeeded, or a negative errno code if local error check fails. |
| 218 | int GainAsync(DvrNativeBufferMetadata* out_meta, LocalHandle* out_fence); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 219 | |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 220 | private: |
| 221 | friend BASE; |
| 222 | |
| 223 | // Constructors are automatically exposed through BufferProducer::Create(...) |
| 224 | // static template methods inherited from ClientBase, which take the same |
| 225 | // arguments as the constructors. |
| 226 | |
| 227 | // Constructs a buffer with the given geometry and parameters. |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 228 | BufferProducer(uint32_t width, uint32_t height, uint32_t format, |
Jiwen 'Steve' Cai | f010817 | 2018-03-09 13:32:39 -0800 | [diff] [blame^] | 229 | uint64_t usage, size_t metadata_size = 0); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 230 | |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 231 | // Constructs a blob (flat) buffer with the given usage flags. |
Jiwen 'Steve' Cai | f010817 | 2018-03-09 13:32:39 -0800 | [diff] [blame^] | 232 | BufferProducer(uint64_t usage, size_t size); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 233 | |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 234 | // Imports the given file handle to a producer channel, taking ownership. |
| 235 | explicit BufferProducer(LocalChannelHandle channel); |
Corey Tabaka | 52ea25c | 2017-09-13 18:02:48 -0700 | [diff] [blame] | 236 | |
| 237 | // Local state transition helpers. |
| 238 | int LocalGain(DvrNativeBufferMetadata* out_meta, LocalHandle* out_fence); |
| 239 | int LocalPost(const DvrNativeBufferMetadata* meta, |
| 240 | const LocalHandle& ready_fence); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 241 | }; |
| 242 | |
| 243 | // This is a connection to a producer buffer, which can be located in another |
| 244 | // application. When that buffer is Post()ed, this fd will be signaled and |
| 245 | // Acquire allows read access. The user is responsible for making sure that |
| 246 | // Acquire is called with the correct metadata structure. The only guarantee the |
| 247 | // API currently provides is that an Acquire() with metadata of the wrong size |
| 248 | // will fail. |
| 249 | class BufferConsumer : public pdx::ClientBase<BufferConsumer, BufferHubBuffer> { |
| 250 | public: |
| 251 | // This call assumes ownership of |fd|. |
| 252 | static std::unique_ptr<BufferConsumer> Import(LocalChannelHandle channel); |
| 253 | static std::unique_ptr<BufferConsumer> Import( |
| 254 | Status<LocalChannelHandle> status); |
| 255 | |
| 256 | // Attempt to retrieve a post event from buffer hub. If successful, |
| 257 | // |ready_fence| will be set to a fence to wait on until the buffer is ready. |
| 258 | // This call will only succeed after the fd is signalled. This call may be |
| 259 | // performed as an alternative to the Acquire() with metadata. In such cases |
| 260 | // the metadata is not read. |
| 261 | // |
| 262 | // This returns zero or negative unix error code. |
| 263 | int Acquire(LocalHandle* ready_fence); |
| 264 | |
| 265 | // Attempt to retrieve a post event from buffer hub. If successful, |
| 266 | // |ready_fence| is set to a fence signaling that the contents of the buffer |
| 267 | // are available. This call will only succeed if the buffer is in the posted |
| 268 | // state. |
| 269 | // Returns zero on success, or a negative errno code otherwise. |
Corey Tabaka | 52ea25c | 2017-09-13 18:02:48 -0700 | [diff] [blame] | 270 | int Acquire(LocalHandle* ready_fence, void* meta, size_t user_metadata_size); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 271 | |
| 272 | // Attempt to retrieve a post event from buffer hub. If successful, |
| 273 | // |ready_fence| is set to a fence to wait on until the buffer is ready. This |
| 274 | // call will only succeed after the fd is signaled. This returns zero or a |
| 275 | // negative unix error code. |
| 276 | template <typename Meta> |
| 277 | int Acquire(LocalHandle* ready_fence, Meta* meta) { |
| 278 | return Acquire(ready_fence, meta, sizeof(*meta)); |
| 279 | } |
| 280 | |
Corey Tabaka | 52ea25c | 2017-09-13 18:02:48 -0700 | [diff] [blame] | 281 | // Asynchronously acquires a bufer. |
| 282 | int AcquireAsync(DvrNativeBufferMetadata* out_meta, LocalHandle* out_fence); |
| 283 | |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 284 | // This should be called after a successful Acquire call. If the fence is |
| 285 | // valid the fence determines the buffer usage, otherwise the buffer is |
| 286 | // released immediately. |
| 287 | // This returns zero or a negative unix error code. |
| 288 | int Release(const LocalHandle& release_fence); |
Corey Tabaka | 52ea25c | 2017-09-13 18:02:48 -0700 | [diff] [blame] | 289 | int ReleaseAsync(); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 290 | |
| 291 | // Asynchronously releases a buffer. Similar to the synchronous version above, |
Corey Tabaka | 52ea25c | 2017-09-13 18:02:48 -0700 | [diff] [blame] | 292 | // except that it does not wait for BufferHub to reply with success or error. |
| 293 | // The fence and metadata are passed to consumer via shared fd and shared |
| 294 | // memory. |
| 295 | int ReleaseAsync(const DvrNativeBufferMetadata* meta, |
| 296 | const LocalHandle& release_fence); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 297 | |
| 298 | // May be called after or instead of Acquire to indicate that the consumer |
| 299 | // does not need to access the buffer this cycle. This returns zero or a |
| 300 | // negative unix error code. |
| 301 | int Discard(); |
| 302 | |
| 303 | // When set, this consumer is no longer notified when this buffer is |
| 304 | // available. The system behaves as if Discard() is immediately called |
| 305 | // whenever the buffer is posted. If ignore is set to true while a buffer is |
| 306 | // pending, it will act as if Discard() was also called. |
| 307 | // This returns zero or a negative unix error code. |
| 308 | int SetIgnore(bool ignore); |
| 309 | |
| 310 | private: |
| 311 | friend BASE; |
| 312 | |
| 313 | explicit BufferConsumer(LocalChannelHandle channel); |
Corey Tabaka | 52ea25c | 2017-09-13 18:02:48 -0700 | [diff] [blame] | 314 | |
| 315 | // Local state transition helpers. |
| 316 | int LocalAcquire(DvrNativeBufferMetadata* out_meta, LocalHandle* out_fence); |
| 317 | int LocalRelease(const DvrNativeBufferMetadata* meta, |
| 318 | const LocalHandle& release_fence); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 319 | }; |
| 320 | |
| 321 | } // namespace dvr |
| 322 | } // namespace android |
| 323 | |
| 324 | #endif // ANDROID_DVR_BUFFER_HUB_CLIENT_H_ |