blob: 8a4440fc83cdee74c381ec76bf41b8eecdf62a0d [file] [log] [blame]
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08001#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 Tabaka52ea25c2017-09-13 18:02:48 -070014#include "bufferhub_rpc.h"
15
Alex Vakulenkoe4eec202017-01-27 14:41:04 -080016namespace android {
17namespace dvr {
18
19class 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 Wagenaar4d3590f2017-05-06 22:36:04 -070037 int Lock(int usage, int x, int y, int width, int height, void** addr);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -080038
39 // Must be called after Lock() when the caller has finished changing the
40 // buffer.
Hendrik Wagenaar4d3590f2017-05-06 22:36:04 -070041 int Unlock();
Alex Vakulenkoe4eec202017-01-27 14:41:04 -080042
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 Wagenaar10e68eb2017-03-15 13:29:02 -070066 // 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 Vakulenkoe4eec202017-01-27 14:41:04 -080070 using Client::event_fd;
Corey Tabaka3079cb72017-01-19 15:07:26 -080071
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 Tabaka52ea25c2017-09-13 18:02:48 -070080 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 Vakulenkoe4eec202017-01-27 14:41:04 -080088 native_handle_t* native_handle() const {
Hendrik Wagenaar4d3590f2017-05-06 22:36:04 -070089 return const_cast<native_handle_t*>(buffer_.handle());
Alex Vakulenkoe4eec202017-01-27 14:41:04 -080090 }
91
Hendrik Wagenaar4d3590f2017-05-06 22:36:04 -070092 IonBuffer* buffer() { return &buffer_; }
93 const IonBuffer* buffer() const { return &buffer_; }
Corey Tabaka2655e1c2017-04-04 11:07:05 -070094
Alex Vakulenkoe4eec202017-01-27 14:41:04 -080095 int id() const { return id_; }
96
Jiwen 'Steve' Cai9e7f3032017-10-20 18:39:47 -070097 // Returns the buffer buffer state.
98 uint64_t buffer_state() { return buffer_state_->load(); };
99
Corey Tabaka52ea25c2017-09-13 18:02:48 -0700100 // 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 Vakulenkoe4eec202017-01-27 14:41:04 -0800104 // 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 Wagenaar4d3590f2017-05-06 22:36:04 -0700107 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' Cai044963e2017-05-01 22:43:21 -0700113
Corey Tabakaec971362017-05-05 18:14:54 +0000114 // TODO(b/37881101) Clean up producer/consumer usage.
Hendrik Wagenaar4d3590f2017-05-06 22:36:04 -0700115 uint64_t producer_usage() const { return buffer_.usage(); }
116 uint64_t consumer_usage() const { return buffer_.usage(); }
Corey Tabakaec971362017-05-05 18:14:54 +0000117
Corey Tabaka52ea25c2017-09-13 18:02:48 -0700118 uint64_t GetQueueIndex() const { return metadata_header_->queue_index; }
119 void SetQueueIndex(uint64_t index) { metadata_header_->queue_index = index; }
120
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800121 protected:
122 explicit BufferHubBuffer(LocalChannelHandle channel);
123 explicit BufferHubBuffer(const std::string& endpoint_path);
124 virtual ~BufferHubBuffer();
125
126 // Initialization helper.
127 int ImportBuffer();
128
Corey Tabaka52ea25c2017-09-13 18:02:48 -0700129 // Check invalid metadata operation. Returns 0 if requested metadata is valid.
130 int CheckMetadata(size_t user_metadata_size) const;
131
132 // Send out the new fence by updating the shared fence (shared_release_fence
133 // for producer and shared_acquire_fence for consumer). Note that during this
134 // should only be used in LocalPost() or LocalRelease, and the shared fence
135 // shouldn't be poll'ed by the other end.
136 int UpdateSharedFence(const LocalHandle& new_fence,
137 const LocalHandle& shared_fence);
138
139 // IonBuffer that is shared between bufferhubd, producer, and consumers.
140 size_t metadata_buf_size_{0};
141 size_t user_metadata_size_{0};
142 BufferHubDefs::MetadataHeader* metadata_header_{nullptr};
143 void* user_metadata_ptr_{nullptr};
144 std::atomic<uint64_t>* buffer_state_{nullptr};
145 std::atomic<uint64_t>* fence_state_{nullptr};
146
147 LocalHandle shared_acquire_fence_;
148 LocalHandle shared_release_fence_;
149
150 // A local fence fd that holds the ownership of the fence fd on Post (for
151 // producer) and Release (for consumer).
152 LocalHandle pending_fence_fd_;
153
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800154 private:
155 BufferHubBuffer(const BufferHubBuffer&) = delete;
156 void operator=(const BufferHubBuffer&) = delete;
157
158 // Global id for the buffer that is consistent across processes. It is meant
159 // for logging and debugging purposes only and should not be used for lookup
160 // or any other functional purpose as a security precaution.
161 int id_;
Corey Tabaka52ea25c2017-09-13 18:02:48 -0700162 uint64_t buffer_state_bit_{0ULL};
Hendrik Wagenaar4d3590f2017-05-06 22:36:04 -0700163 IonBuffer buffer_;
Corey Tabaka52ea25c2017-09-13 18:02:48 -0700164 IonBuffer metadata_buffer_;
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800165};
166
167// This represents a writable buffer. Calling Post notifies all clients and
168// makes the buffer read-only. Call Gain to acquire write access. A buffer
169// may have many consumers.
170//
171// The user of BufferProducer is responsible with making sure that the Post() is
172// done with the correct metadata type and size. The user is also responsible
173// for making sure that remote ends (BufferConsumers) are also using the correct
174// metadata when acquiring the buffer. The API guarantees that a Post() with a
175// metadata of wrong size will fail. However, it currently does not do any
176// type checking.
177// The API also assumes that metadata is a serializable type (plain old data).
178class BufferProducer : public pdx::ClientBase<BufferProducer, BufferHubBuffer> {
179 public:
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800180 // Imports a bufferhub producer channel, assuming ownership of its handle.
181 static std::unique_ptr<BufferProducer> Import(LocalChannelHandle channel);
182 static std::unique_ptr<BufferProducer> Import(
183 Status<LocalChannelHandle> status);
184
Corey Tabaka52ea25c2017-09-13 18:02:48 -0700185 // Asynchronously posts a buffer. The fence and metadata are passed to
186 // consumer via shared fd and shared memory.
187 int PostAsync(const DvrNativeBufferMetadata* meta,
188 const LocalHandle& ready_fence);
189
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800190 // Post this buffer, passing |ready_fence| to the consumers. The bytes in
191 // |meta| are passed unaltered to the consumers. The producer must not modify
192 // the buffer until it is re-gained.
193 // This returns zero or a negative unix error code.
194 int Post(const LocalHandle& ready_fence, const void* meta,
Corey Tabaka52ea25c2017-09-13 18:02:48 -0700195 size_t user_metadata_size);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800196
197 template <typename Meta,
198 typename = typename std::enable_if<std::is_void<Meta>::value>::type>
199 int Post(const LocalHandle& ready_fence) {
200 return Post(ready_fence, nullptr, 0);
201 }
Corey Tabaka2655e1c2017-04-04 11:07:05 -0700202 template <typename Meta, typename = typename std::enable_if<
203 !std::is_void<Meta>::value>::type>
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800204 int Post(const LocalHandle& ready_fence, const Meta& meta) {
205 return Post(ready_fence, &meta, sizeof(meta));
206 }
207
208 // Attempt to re-gain the buffer for writing. If |release_fence| is valid, it
209 // must be waited on before using the buffer. If it is not valid then the
210 // buffer is free for immediate use. This call will only succeed if the buffer
211 // is in the released state.
212 // This returns zero or a negative unix error code.
213 int Gain(LocalHandle* release_fence);
Corey Tabaka52ea25c2017-09-13 18:02:48 -0700214 int GainAsync();
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800215
216 // Asynchronously marks a released buffer as gained. This method is similar to
217 // the synchronous version above, except that it does not wait for BufferHub
Corey Tabaka52ea25c2017-09-13 18:02:48 -0700218 // to acknowledge success or failure. Because of the asynchronous nature of
219 // the underlying message, no error is returned if this method is called when
220 // the buffer is in an incorrect state. Returns zero if sending the message
221 // succeeded, or a negative errno code if local error check fails.
222 int GainAsync(DvrNativeBufferMetadata* out_meta, LocalHandle* out_fence);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800223
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800224 private:
225 friend BASE;
226
227 // Constructors are automatically exposed through BufferProducer::Create(...)
228 // static template methods inherited from ClientBase, which take the same
229 // arguments as the constructors.
230
231 // Constructs a buffer with the given geometry and parameters.
Corey Tabakacd52dd92017-04-07 18:03:57 -0700232 BufferProducer(uint32_t width, uint32_t height, uint32_t format,
Hendrik Wagenaar4d3590f2017-05-06 22:36:04 -0700233 uint32_t usage, size_t metadata_size = 0);
Corey Tabakaec971362017-05-05 18:14:54 +0000234 BufferProducer(uint32_t width, uint32_t height, uint32_t format,
235 uint64_t producer_usage, uint64_t consumer_usage,
Hendrik Wagenaar4d3590f2017-05-06 22:36:04 -0700236 size_t metadata_size);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800237
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800238 // Constructs a blob (flat) buffer with the given usage flags.
Corey Tabakaec971362017-05-05 18:14:54 +0000239 BufferProducer(uint32_t usage, size_t size);
240 BufferProducer(uint64_t producer_usage, uint64_t consumer_usage, size_t size);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800241
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800242 // Imports the given file handle to a producer channel, taking ownership.
243 explicit BufferProducer(LocalChannelHandle channel);
Corey Tabaka52ea25c2017-09-13 18:02:48 -0700244
245 // Local state transition helpers.
246 int LocalGain(DvrNativeBufferMetadata* out_meta, LocalHandle* out_fence);
247 int LocalPost(const DvrNativeBufferMetadata* meta,
248 const LocalHandle& ready_fence);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800249};
250
251// This is a connection to a producer buffer, which can be located in another
252// application. When that buffer is Post()ed, this fd will be signaled and
253// Acquire allows read access. The user is responsible for making sure that
254// Acquire is called with the correct metadata structure. The only guarantee the
255// API currently provides is that an Acquire() with metadata of the wrong size
256// will fail.
257class BufferConsumer : public pdx::ClientBase<BufferConsumer, BufferHubBuffer> {
258 public:
259 // This call assumes ownership of |fd|.
260 static std::unique_ptr<BufferConsumer> Import(LocalChannelHandle channel);
261 static std::unique_ptr<BufferConsumer> Import(
262 Status<LocalChannelHandle> status);
263
264 // Attempt to retrieve a post event from buffer hub. If successful,
265 // |ready_fence| will be set to a fence to wait on until the buffer is ready.
266 // This call will only succeed after the fd is signalled. This call may be
267 // performed as an alternative to the Acquire() with metadata. In such cases
268 // the metadata is not read.
269 //
270 // This returns zero or negative unix error code.
271 int Acquire(LocalHandle* ready_fence);
272
273 // Attempt to retrieve a post event from buffer hub. If successful,
274 // |ready_fence| is set to a fence signaling that the contents of the buffer
275 // are available. This call will only succeed if the buffer is in the posted
276 // state.
277 // Returns zero on success, or a negative errno code otherwise.
Corey Tabaka52ea25c2017-09-13 18:02:48 -0700278 int Acquire(LocalHandle* ready_fence, void* meta, size_t user_metadata_size);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800279
280 // Attempt to retrieve a post event from buffer hub. If successful,
281 // |ready_fence| is set to a fence to wait on until the buffer is ready. This
282 // call will only succeed after the fd is signaled. This returns zero or a
283 // negative unix error code.
284 template <typename Meta>
285 int Acquire(LocalHandle* ready_fence, Meta* meta) {
286 return Acquire(ready_fence, meta, sizeof(*meta));
287 }
288
Corey Tabaka52ea25c2017-09-13 18:02:48 -0700289 // Asynchronously acquires a bufer.
290 int AcquireAsync(DvrNativeBufferMetadata* out_meta, LocalHandle* out_fence);
291
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800292 // This should be called after a successful Acquire call. If the fence is
293 // valid the fence determines the buffer usage, otherwise the buffer is
294 // released immediately.
295 // This returns zero or a negative unix error code.
296 int Release(const LocalHandle& release_fence);
Corey Tabaka52ea25c2017-09-13 18:02:48 -0700297 int ReleaseAsync();
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800298
299 // Asynchronously releases a buffer. Similar to the synchronous version above,
Corey Tabaka52ea25c2017-09-13 18:02:48 -0700300 // except that it does not wait for BufferHub to reply with success or error.
301 // The fence and metadata are passed to consumer via shared fd and shared
302 // memory.
303 int ReleaseAsync(const DvrNativeBufferMetadata* meta,
304 const LocalHandle& release_fence);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800305
306 // May be called after or instead of Acquire to indicate that the consumer
307 // does not need to access the buffer this cycle. This returns zero or a
308 // negative unix error code.
309 int Discard();
310
311 // When set, this consumer is no longer notified when this buffer is
312 // available. The system behaves as if Discard() is immediately called
313 // whenever the buffer is posted. If ignore is set to true while a buffer is
314 // pending, it will act as if Discard() was also called.
315 // This returns zero or a negative unix error code.
316 int SetIgnore(bool ignore);
317
318 private:
319 friend BASE;
320
321 explicit BufferConsumer(LocalChannelHandle channel);
Corey Tabaka52ea25c2017-09-13 18:02:48 -0700322
323 // Local state transition helpers.
324 int LocalAcquire(DvrNativeBufferMetadata* out_meta, LocalHandle* out_fence);
325 int LocalRelease(const DvrNativeBufferMetadata* meta,
326 const LocalHandle& release_fence);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800327};
328
329} // namespace dvr
330} // namespace android
331
332#endif // ANDROID_DVR_BUFFER_HUB_CLIENT_H_