blob: c7912500630e649e8fb497cc336b822ac4f518b1 [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 Tabaka52ea25c2017-09-13 18:02:48 -0700114 uint64_t GetQueueIndex() const { return metadata_header_->queue_index; }
115 void SetQueueIndex(uint64_t index) { metadata_header_->queue_index = index; }
116
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800117 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 Tabaka52ea25c2017-09-13 18:02:48 -0700125 // 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 Vakulenkoe4eec202017-01-27 14:41:04 -0800150 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 Tabaka52ea25c2017-09-13 18:02:48 -0700158 uint64_t buffer_state_bit_{0ULL};
Hendrik Wagenaar4d3590f2017-05-06 22:36:04 -0700159 IonBuffer buffer_;
Corey Tabaka52ea25c2017-09-13 18:02:48 -0700160 IonBuffer metadata_buffer_;
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800161};
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).
174class BufferProducer : public pdx::ClientBase<BufferProducer, BufferHubBuffer> {
175 public:
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800176 // 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 Tabaka52ea25c2017-09-13 18:02:48 -0700181 // 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 Vakulenkoe4eec202017-01-27 14:41:04 -0800186 // 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 Tabaka52ea25c2017-09-13 18:02:48 -0700191 size_t user_metadata_size);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800192
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 Tabaka2655e1c2017-04-04 11:07:05 -0700198 template <typename Meta, typename = typename std::enable_if<
199 !std::is_void<Meta>::value>::type>
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800200 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 Tabaka52ea25c2017-09-13 18:02:48 -0700210 int GainAsync();
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800211
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 Tabaka52ea25c2017-09-13 18:02:48 -0700214 // 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 Vakulenkoe4eec202017-01-27 14:41:04 -0800219
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800220 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 Tabakacd52dd92017-04-07 18:03:57 -0700228 BufferProducer(uint32_t width, uint32_t height, uint32_t format,
Jiwen 'Steve' Caif0108172018-03-09 13:32:39 -0800229 uint64_t usage, size_t metadata_size = 0);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800230
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800231 // Constructs a blob (flat) buffer with the given usage flags.
Jiwen 'Steve' Caif0108172018-03-09 13:32:39 -0800232 BufferProducer(uint64_t usage, size_t size);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800233
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800234 // Imports the given file handle to a producer channel, taking ownership.
235 explicit BufferProducer(LocalChannelHandle channel);
Corey Tabaka52ea25c2017-09-13 18:02:48 -0700236
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 Vakulenkoe4eec202017-01-27 14:41:04 -0800241};
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.
249class 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 Tabaka52ea25c2017-09-13 18:02:48 -0700270 int Acquire(LocalHandle* ready_fence, void* meta, size_t user_metadata_size);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800271
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 Tabaka52ea25c2017-09-13 18:02:48 -0700281 // Asynchronously acquires a bufer.
282 int AcquireAsync(DvrNativeBufferMetadata* out_meta, LocalHandle* out_fence);
283
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800284 // 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 Tabaka52ea25c2017-09-13 18:02:48 -0700289 int ReleaseAsync();
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800290
291 // Asynchronously releases a buffer. Similar to the synchronous version above,
Corey Tabaka52ea25c2017-09-13 18:02:48 -0700292 // 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 Vakulenkoe4eec202017-01-27 14:41:04 -0800297
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 Tabaka52ea25c2017-09-13 18:02:48 -0700314
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 Vakulenkoe4eec202017-01-27 14:41:04 -0800319};
320
321} // namespace dvr
322} // namespace android
323
324#endif // ANDROID_DVR_BUFFER_HUB_CLIENT_H_