blob: c772ed301ba48548742ce079b481f5eac7fde67e [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
14namespace android {
15namespace dvr {
16
17class BufferHubBuffer : public pdx::Client {
18 public:
19 using LocalHandle = pdx::LocalHandle;
20 using LocalChannelHandle = pdx::LocalChannelHandle;
21 template <typename T>
22 using Status = pdx::Status<T>;
23
24 // Create a new consumer channel that is attached to the producer. Returns
25 // a file descriptor for the new channel or a negative error code.
26 Status<LocalChannelHandle> CreateConsumer();
27
28 // Polls the fd for |timeout_ms| milliseconds (-1 for infinity).
29 int Poll(int timeout_ms);
30
31 // Locks the area specified by (x, y, width, height) for a specific usage. If
32 // the usage is software then |addr| will be updated to point to the address
33 // of the buffer in virtual memory. The caller should only access/modify the
34 // pixels in the specified area. anything else is undefined behavior.
35 int Lock(int usage, int x, int y, int width, int height, void** addr,
36 size_t index);
37
38 // Must be called after Lock() when the caller has finished changing the
39 // buffer.
40 int Unlock(size_t index);
41
42 // Helper for when index is 0.
43 int Lock(int usage, int x, int y, int width, int height, void** addr) {
44 return Lock(usage, x, y, width, height, addr, 0);
45 }
46
47 // Helper for when index is 0.
48 int Unlock() { return Unlock(0); }
49
50 // Gets a blob buffer that was created with BufferProducer::CreateBlob.
51 // Locking and Unlocking is handled internally. There's no need to Unlock
52 // after calling this method.
53 int GetBlobReadWritePointer(size_t size, void** addr);
54
55 // Gets a blob buffer that was created with BufferProducer::CreateBlob.
56 // Locking and Unlocking is handled internally. There's no need to Unlock
57 // after calling this method.
58 int GetBlobReadOnlyPointer(size_t size, void** addr);
59
60 // Returns a dup'd file descriptor for accessing the blob shared memory. The
61 // caller takes ownership of the file descriptor and must close it or pass on
62 // ownership. Some GPU API extensions can take file descriptors to bind shared
63 // memory gralloc buffers to GPU buffer objects.
64 LocalHandle GetBlobFd() const {
65 // Current GPU vendor puts the buffer allocation in one FD. If we change GPU
66 // vendors and this is the wrong fd, late-latching and EDS will very clearly
67 // stop working and we will need to correct this. The alternative is to use
68 // a GL context in the pose service to allocate this buffer or to use the
69 // ION API directly instead of gralloc.
70 return LocalHandle(dup(native_handle()->data[0]));
71 }
72
Hendrik Wagenaar10e68eb2017-03-15 13:29:02 -070073 // Get up to |max_fds_count| file descriptors for accessing the blob shared
74 // memory. |fds_count| will contain the actual number of file descriptors.
75 void GetBlobFds(int* fds, size_t* fds_count, size_t max_fds_count) const;
76
Alex Vakulenkoe4eec202017-01-27 14:41:04 -080077 using Client::event_fd;
Corey Tabaka3079cb72017-01-19 15:07:26 -080078
79 Status<int> GetEventMask(int events) {
80 if (auto* client_channel = GetChannel()) {
81 return client_channel->GetEventMask(events);
82 } else {
83 return pdx::ErrorStatus(EINVAL);
84 }
85 }
86
Alex Vakulenkoe4eec202017-01-27 14:41:04 -080087 native_handle_t* native_handle() const {
88 return const_cast<native_handle_t*>(slices_[0].handle());
89 }
90 // If index is greater than or equal to slice_count(), the result is
91 // undefined.
92 native_handle_t* native_handle(size_t index) const {
93 return const_cast<native_handle_t*>(slices_[index].handle());
94 }
95
96 IonBuffer* buffer() { return &slices_[0]; }
Corey Tabaka2655e1c2017-04-04 11:07:05 -070097 const IonBuffer* buffer() const { return &slices_[0]; }
98
Alex Vakulenkoe4eec202017-01-27 14:41:04 -080099 // If index is greater than or equal to slice_count(), the result is
100 // undefined.
101 IonBuffer* slice(size_t index) { return &slices_[index]; }
Corey Tabaka2655e1c2017-04-04 11:07:05 -0700102 const IonBuffer* slice(size_t index) const { return &slices_[index]; }
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800103
104 int slice_count() const { return static_cast<int>(slices_.size()); }
105 int id() const { return id_; }
106
107 // The following methods return settings of the first buffer. Currently,
108 // it is only possible to create multi-buffer BufferHubBuffers with the same
109 // settings.
Corey Tabakacd52dd92017-04-07 18:03:57 -0700110 uint32_t width() const { return slices_[0].width(); }
111 uint32_t height() const { return slices_[0].height(); }
112 uint32_t stride() const { return slices_[0].stride(); }
113 uint32_t format() const { return slices_[0].format(); }
114 uint32_t usage() const { return slices_[0].usage(); }
115 uint32_t layer_count() const { return slices_[0].layer_count(); }
116 uint64_t producer_usage() const { return slices_[0].producer_usage(); }
117 uint64_t consumer_usage() const { return slices_[0].consumer_usage(); }
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800118
119 protected:
120 explicit BufferHubBuffer(LocalChannelHandle channel);
121 explicit BufferHubBuffer(const std::string& endpoint_path);
122 virtual ~BufferHubBuffer();
123
124 // Initialization helper.
125 int ImportBuffer();
126
127 private:
128 BufferHubBuffer(const BufferHubBuffer&) = delete;
129 void operator=(const BufferHubBuffer&) = delete;
130
131 // Global id for the buffer that is consistent across processes. It is meant
132 // for logging and debugging purposes only and should not be used for lookup
133 // or any other functional purpose as a security precaution.
134 int id_;
135
136 // A BufferHubBuffer may contain multiple slices of IonBuffers with same
137 // configurations.
138 std::vector<IonBuffer> slices_;
139};
140
141// This represents a writable buffer. Calling Post notifies all clients and
142// makes the buffer read-only. Call Gain to acquire write access. A buffer
143// may have many consumers.
144//
145// The user of BufferProducer is responsible with making sure that the Post() is
146// done with the correct metadata type and size. The user is also responsible
147// for making sure that remote ends (BufferConsumers) are also using the correct
148// metadata when acquiring the buffer. The API guarantees that a Post() with a
149// metadata of wrong size will fail. However, it currently does not do any
150// type checking.
151// The API also assumes that metadata is a serializable type (plain old data).
152class BufferProducer : public pdx::ClientBase<BufferProducer, BufferHubBuffer> {
153 public:
154 // Create a buffer designed to hold arbitrary bytes that can be read and
155 // written from CPU, GPU and DSP. The buffer is mapped uncached so that CPU
156 // reads and writes are predictable.
157 static std::unique_ptr<BufferProducer> CreateUncachedBlob(size_t size);
158
159 // Creates a persistent uncached buffer with the given name and access.
160 static std::unique_ptr<BufferProducer> CreatePersistentUncachedBlob(
161 const std::string& name, int user_id, int group_id, size_t size);
162
163 // Imports a bufferhub producer channel, assuming ownership of its handle.
164 static std::unique_ptr<BufferProducer> Import(LocalChannelHandle channel);
165 static std::unique_ptr<BufferProducer> Import(
166 Status<LocalChannelHandle> status);
167
168 // Post this buffer, passing |ready_fence| to the consumers. The bytes in
169 // |meta| are passed unaltered to the consumers. The producer must not modify
170 // the buffer until it is re-gained.
171 // This returns zero or a negative unix error code.
172 int Post(const LocalHandle& ready_fence, const void* meta,
173 size_t meta_size_bytes);
174
175 template <typename Meta,
176 typename = typename std::enable_if<std::is_void<Meta>::value>::type>
177 int Post(const LocalHandle& ready_fence) {
178 return Post(ready_fence, nullptr, 0);
179 }
Corey Tabaka2655e1c2017-04-04 11:07:05 -0700180 template <typename Meta, typename = typename std::enable_if<
181 !std::is_void<Meta>::value>::type>
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800182 int Post(const LocalHandle& ready_fence, const Meta& meta) {
183 return Post(ready_fence, &meta, sizeof(meta));
184 }
185
186 // Attempt to re-gain the buffer for writing. If |release_fence| is valid, it
187 // must be waited on before using the buffer. If it is not valid then the
188 // buffer is free for immediate use. This call will only succeed if the buffer
189 // is in the released state.
190 // This returns zero or a negative unix error code.
191 int Gain(LocalHandle* release_fence);
192
193 // Asynchronously marks a released buffer as gained. This method is similar to
194 // the synchronous version above, except that it does not wait for BufferHub
195 // to acknowledge success or failure, nor does it transfer a release fence to
196 // the client. This version may be used in situations where a release fence is
197 // not needed. Because of the asynchronous nature of the underlying message,
198 // no error is returned if this method is called when the buffer is in an
199 // incorrect state. Returns zero if sending the message succeeded, or a
200 // negative errno code otherwise.
201 int GainAsync();
202
203 // Attaches the producer to |name| so that it becomes a persistent buffer that
204 // may be retrieved by name at a later time. This may be used in cases where a
205 // shared memory buffer should persist across the life of the producer process
206 // (i.e. the buffer may be held by clients across a service restart). The
207 // buffer may be associated with a user and/or group id to restrict access to
208 // the buffer. If user_id or group_id is -1 then checks for the respective id
209 // are disabled. If user_id or group_id is 0 then the respective id of the
210 // calling process is used instead.
211 int MakePersistent(const std::string& name, int user_id, int group_id);
212
213 // Removes the persistence of the producer.
214 int RemovePersistence();
215
216 private:
217 friend BASE;
218
219 // Constructors are automatically exposed through BufferProducer::Create(...)
220 // static template methods inherited from ClientBase, which take the same
221 // arguments as the constructors.
222
223 // Constructs a buffer with the given geometry and parameters.
Corey Tabakacd52dd92017-04-07 18:03:57 -0700224 BufferProducer(uint32_t width, uint32_t height, uint32_t format,
225 uint32_t usage, size_t metadata_size = 0,
226 size_t slice_count = 1);
227 BufferProducer(uint32_t width, uint32_t height, uint32_t format,
228 uint64_t producer_usage, uint64_t consumer_usage,
229 size_t metadata_size, size_t slice_count);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800230
231 // Constructs a persistent buffer with the given geometry and parameters and
232 // binds it to |name| in one shot. If a persistent buffer with the same name
233 // and settings already exists and matches the given geometry and parameters,
234 // that buffer is connected to this client instead of creating a new buffer.
235 // If the name matches but the geometry or settings do not match then
236 // construction fails and BufferProducer::Create() returns nullptr.
237 //
238 // Access to the persistent buffer may be restricted by |user_id| and/or
239 // |group_id|; these settings are established only when the buffer is first
240 // created and cannot be changed. A user or group id of -1 disables checks for
241 // that respective id. A user or group id of 0 is substituted with the
242 // effective user or group id of the calling process.
Corey Tabakacd52dd92017-04-07 18:03:57 -0700243 BufferProducer(const std::string& name, int user_id, int group_id,
244 uint32_t width, uint32_t height, uint32_t format,
245 uint32_t usage, size_t metadata_size = 0,
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800246 size_t slice_count = 1);
Corey Tabakacd52dd92017-04-07 18:03:57 -0700247 BufferProducer(const std::string& name, int user_id, int group_id,
248 uint32_t width, uint32_t height, uint32_t format,
249 uint64_t producer_usage, uint64_t consumer_usage,
250 size_t metadata_size, size_t slice_count);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800251
252 // Constructs a blob (flat) buffer with the given usage flags.
Corey Tabakacd52dd92017-04-07 18:03:57 -0700253 BufferProducer(uint32_t usage, size_t size);
254 BufferProducer(uint64_t producer_usage, uint64_t consumer_usage, size_t size);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800255
256 // Constructs a persistent blob (flat) buffer and binds it to |name|.
Corey Tabakacd52dd92017-04-07 18:03:57 -0700257 BufferProducer(const std::string& name, int user_id, int group_id,
258 uint32_t usage, size_t size);
259 BufferProducer(const std::string& name, int user_id, int group_id,
260 uint64_t producer_usage, uint64_t consumer_usage, size_t size);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800261
262 // Constructs a channel to persistent buffer by name only. The buffer must
263 // have been previously created or made persistent.
264 explicit BufferProducer(const std::string& name);
265
266 // Imports the given file handle to a producer channel, taking ownership.
267 explicit BufferProducer(LocalChannelHandle channel);
268};
269
270// This is a connection to a producer buffer, which can be located in another
271// application. When that buffer is Post()ed, this fd will be signaled and
272// Acquire allows read access. The user is responsible for making sure that
273// Acquire is called with the correct metadata structure. The only guarantee the
274// API currently provides is that an Acquire() with metadata of the wrong size
275// will fail.
276class BufferConsumer : public pdx::ClientBase<BufferConsumer, BufferHubBuffer> {
277 public:
278 // This call assumes ownership of |fd|.
279 static std::unique_ptr<BufferConsumer> Import(LocalChannelHandle channel);
280 static std::unique_ptr<BufferConsumer> Import(
281 Status<LocalChannelHandle> status);
282
283 // Attempt to retrieve a post event from buffer hub. If successful,
284 // |ready_fence| will be set to a fence to wait on until the buffer is ready.
285 // This call will only succeed after the fd is signalled. This call may be
286 // performed as an alternative to the Acquire() with metadata. In such cases
287 // the metadata is not read.
288 //
289 // This returns zero or negative unix error code.
290 int Acquire(LocalHandle* ready_fence);
291
292 // Attempt to retrieve a post event from buffer hub. If successful,
293 // |ready_fence| is set to a fence signaling that the contents of the buffer
294 // are available. This call will only succeed if the buffer is in the posted
295 // state.
296 // Returns zero on success, or a negative errno code otherwise.
297 int Acquire(LocalHandle* ready_fence, void* meta, size_t meta_size_bytes);
298
299 // Attempt to retrieve a post event from buffer hub. If successful,
300 // |ready_fence| is set to a fence to wait on until the buffer is ready. This
301 // call will only succeed after the fd is signaled. This returns zero or a
302 // negative unix error code.
303 template <typename Meta>
304 int Acquire(LocalHandle* ready_fence, Meta* meta) {
305 return Acquire(ready_fence, meta, sizeof(*meta));
306 }
307
308 // This should be called after a successful Acquire call. If the fence is
309 // valid the fence determines the buffer usage, otherwise the buffer is
310 // released immediately.
311 // This returns zero or a negative unix error code.
312 int Release(const LocalHandle& release_fence);
313
314 // Asynchronously releases a buffer. Similar to the synchronous version above,
315 // except that it does not wait for BufferHub to reply with success or error,
316 // nor does it transfer a release fence. This version may be used in
317 // situations where a release fence is not needed. Because of the asynchronous
318 // nature of the underlying message, no error is returned if this method is
319 // called when the buffer is in an incorrect state. Returns zero if sending
320 // the message succeeded, or a negative errno code otherwise.
321 int ReleaseAsync();
322
323 // May be called after or instead of Acquire to indicate that the consumer
324 // does not need to access the buffer this cycle. This returns zero or a
325 // negative unix error code.
326 int Discard();
327
328 // When set, this consumer is no longer notified when this buffer is
329 // available. The system behaves as if Discard() is immediately called
330 // whenever the buffer is posted. If ignore is set to true while a buffer is
331 // pending, it will act as if Discard() was also called.
332 // This returns zero or a negative unix error code.
333 int SetIgnore(bool ignore);
334
335 private:
336 friend BASE;
337
338 explicit BufferConsumer(LocalChannelHandle channel);
339};
340
341} // namespace dvr
342} // namespace android
343
344#endif // ANDROID_DVR_BUFFER_HUB_CLIENT_H_