buffer_hub_client.h revision 3079cb7c32421099a73e1a774b228635eeb3f189
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 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 73 using Client::event_fd; 74 75 Status<int> GetEventMask(int events) { 76 if (auto* client_channel = GetChannel()) { 77 return client_channel->GetEventMask(events); 78 } else { 79 return pdx::ErrorStatus(EINVAL); 80 } 81 } 82 83 native_handle_t* native_handle() const { 84 return const_cast<native_handle_t*>(slices_[0].handle()); 85 } 86 // If index is greater than or equal to slice_count(), the result is 87 // undefined. 88 native_handle_t* native_handle(size_t index) const { 89 return const_cast<native_handle_t*>(slices_[index].handle()); 90 } 91 92 IonBuffer* buffer() { return &slices_[0]; } 93 // If index is greater than or equal to slice_count(), the result is 94 // undefined. 95 IonBuffer* slice(size_t index) { return &slices_[index]; } 96 97 int slice_count() const { return static_cast<int>(slices_.size()); } 98 int id() const { return id_; } 99 100 // The following methods return settings of the first buffer. Currently, 101 // it is only possible to create multi-buffer BufferHubBuffers with the same 102 // settings. 103 int width() const { return slices_[0].width(); } 104 int height() const { return slices_[0].height(); } 105 int stride() const { return slices_[0].stride(); } 106 int format() const { return slices_[0].format(); } 107 int usage() const { return slices_[0].usage(); } 108 109 protected: 110 explicit BufferHubBuffer(LocalChannelHandle channel); 111 explicit BufferHubBuffer(const std::string& endpoint_path); 112 virtual ~BufferHubBuffer(); 113 114 // Initialization helper. 115 int ImportBuffer(); 116 117 private: 118 BufferHubBuffer(const BufferHubBuffer&) = delete; 119 void operator=(const BufferHubBuffer&) = delete; 120 121 // Global id for the buffer that is consistent across processes. It is meant 122 // for logging and debugging purposes only and should not be used for lookup 123 // or any other functional purpose as a security precaution. 124 int id_; 125 126 // A BufferHubBuffer may contain multiple slices of IonBuffers with same 127 // configurations. 128 std::vector<IonBuffer> slices_; 129}; 130 131// This represents a writable buffer. Calling Post notifies all clients and 132// makes the buffer read-only. Call Gain to acquire write access. A buffer 133// may have many consumers. 134// 135// The user of BufferProducer is responsible with making sure that the Post() is 136// done with the correct metadata type and size. The user is also responsible 137// for making sure that remote ends (BufferConsumers) are also using the correct 138// metadata when acquiring the buffer. The API guarantees that a Post() with a 139// metadata of wrong size will fail. However, it currently does not do any 140// type checking. 141// The API also assumes that metadata is a serializable type (plain old data). 142class BufferProducer : public pdx::ClientBase<BufferProducer, BufferHubBuffer> { 143 public: 144 // Create a buffer designed to hold arbitrary bytes that can be read and 145 // written from CPU, GPU and DSP. The buffer is mapped uncached so that CPU 146 // reads and writes are predictable. 147 static std::unique_ptr<BufferProducer> CreateUncachedBlob(size_t size); 148 149 // Creates a persistent uncached buffer with the given name and access. 150 static std::unique_ptr<BufferProducer> CreatePersistentUncachedBlob( 151 const std::string& name, int user_id, int group_id, size_t size); 152 153 // Imports a bufferhub producer channel, assuming ownership of its handle. 154 static std::unique_ptr<BufferProducer> Import(LocalChannelHandle channel); 155 static std::unique_ptr<BufferProducer> Import( 156 Status<LocalChannelHandle> status); 157 158 // Post this buffer, passing |ready_fence| to the consumers. The bytes in 159 // |meta| are passed unaltered to the consumers. The producer must not modify 160 // the buffer until it is re-gained. 161 // This returns zero or a negative unix error code. 162 int Post(const LocalHandle& ready_fence, const void* meta, 163 size_t meta_size_bytes); 164 165 template <typename Meta, 166 typename = typename std::enable_if<std::is_void<Meta>::value>::type> 167 int Post(const LocalHandle& ready_fence) { 168 return Post(ready_fence, nullptr, 0); 169 } 170 template < 171 typename Meta, 172 typename = typename std::enable_if<!std::is_void<Meta>::value>::type> 173 int Post(const LocalHandle& ready_fence, const Meta& meta) { 174 return Post(ready_fence, &meta, sizeof(meta)); 175 } 176 177 // Attempt to re-gain the buffer for writing. If |release_fence| is valid, it 178 // must be waited on before using the buffer. If it is not valid then the 179 // buffer is free for immediate use. This call will only succeed if the buffer 180 // is in the released state. 181 // This returns zero or a negative unix error code. 182 int Gain(LocalHandle* release_fence); 183 184 // Asynchronously marks a released buffer as gained. This method is similar to 185 // the synchronous version above, except that it does not wait for BufferHub 186 // to acknowledge success or failure, nor does it transfer a release fence to 187 // the client. This version may be used in situations where a release fence is 188 // not needed. Because of the asynchronous nature of the underlying message, 189 // no error is returned if this method is called when the buffer is in an 190 // incorrect state. Returns zero if sending the message succeeded, or a 191 // negative errno code otherwise. 192 int GainAsync(); 193 194 // Attaches the producer to |name| so that it becomes a persistent buffer that 195 // may be retrieved by name at a later time. This may be used in cases where a 196 // shared memory buffer should persist across the life of the producer process 197 // (i.e. the buffer may be held by clients across a service restart). The 198 // buffer may be associated with a user and/or group id to restrict access to 199 // the buffer. If user_id or group_id is -1 then checks for the respective id 200 // are disabled. If user_id or group_id is 0 then the respective id of the 201 // calling process is used instead. 202 int MakePersistent(const std::string& name, int user_id, int group_id); 203 204 // Removes the persistence of the producer. 205 int RemovePersistence(); 206 207 private: 208 friend BASE; 209 210 // Constructors are automatically exposed through BufferProducer::Create(...) 211 // static template methods inherited from ClientBase, which take the same 212 // arguments as the constructors. 213 214 // Constructs a buffer with the given geometry and parameters. 215 BufferProducer(int width, int height, int format, int usage, 216 size_t metadata_size = 0, size_t slice_count = 1); 217 218 // Constructs a persistent buffer with the given geometry and parameters and 219 // binds it to |name| in one shot. If a persistent buffer with the same name 220 // and settings already exists and matches the given geometry and parameters, 221 // that buffer is connected to this client instead of creating a new buffer. 222 // If the name matches but the geometry or settings do not match then 223 // construction fails and BufferProducer::Create() returns nullptr. 224 // 225 // Access to the persistent buffer may be restricted by |user_id| and/or 226 // |group_id|; these settings are established only when the buffer is first 227 // created and cannot be changed. A user or group id of -1 disables checks for 228 // that respective id. A user or group id of 0 is substituted with the 229 // effective user or group id of the calling process. 230 BufferProducer(const std::string& name, int user_id, int group_id, int width, 231 int height, int format, int usage, size_t metadata_size = 0, 232 size_t slice_count = 1); 233 234 // Constructs a blob (flat) buffer with the given usage flags. 235 BufferProducer(int usage, size_t size); 236 237 // Constructs a persistent blob (flat) buffer and binds it to |name|. 238 BufferProducer(const std::string& name, int user_id, int group_id, int usage, 239 size_t size); 240 241 // Constructs a channel to persistent buffer by name only. The buffer must 242 // have been previously created or made persistent. 243 explicit BufferProducer(const std::string& name); 244 245 // Imports the given file handle to a producer channel, taking ownership. 246 explicit BufferProducer(LocalChannelHandle channel); 247}; 248 249// This is a connection to a producer buffer, which can be located in another 250// application. When that buffer is Post()ed, this fd will be signaled and 251// Acquire allows read access. The user is responsible for making sure that 252// Acquire is called with the correct metadata structure. The only guarantee the 253// API currently provides is that an Acquire() with metadata of the wrong size 254// will fail. 255class BufferConsumer : public pdx::ClientBase<BufferConsumer, BufferHubBuffer> { 256 public: 257 // This call assumes ownership of |fd|. 258 static std::unique_ptr<BufferConsumer> Import(LocalChannelHandle channel); 259 static std::unique_ptr<BufferConsumer> Import( 260 Status<LocalChannelHandle> status); 261 262 // Attempt to retrieve a post event from buffer hub. If successful, 263 // |ready_fence| will be set to a fence to wait on until the buffer is ready. 264 // This call will only succeed after the fd is signalled. This call may be 265 // performed as an alternative to the Acquire() with metadata. In such cases 266 // the metadata is not read. 267 // 268 // This returns zero or negative unix error code. 269 int Acquire(LocalHandle* ready_fence); 270 271 // Attempt to retrieve a post event from buffer hub. If successful, 272 // |ready_fence| is set to a fence signaling that the contents of the buffer 273 // are available. This call will only succeed if the buffer is in the posted 274 // state. 275 // Returns zero on success, or a negative errno code otherwise. 276 int Acquire(LocalHandle* ready_fence, void* meta, size_t meta_size_bytes); 277 278 // Attempt to retrieve a post event from buffer hub. If successful, 279 // |ready_fence| is set to a fence to wait on until the buffer is ready. This 280 // call will only succeed after the fd is signaled. This returns zero or a 281 // negative unix error code. 282 template <typename Meta> 283 int Acquire(LocalHandle* ready_fence, Meta* meta) { 284 return Acquire(ready_fence, meta, sizeof(*meta)); 285 } 286 287 // This should be called after a successful Acquire call. If the fence is 288 // valid the fence determines the buffer usage, otherwise the buffer is 289 // released immediately. 290 // This returns zero or a negative unix error code. 291 int Release(const LocalHandle& release_fence); 292 293 // Asynchronously releases a buffer. Similar to the synchronous version above, 294 // except that it does not wait for BufferHub to reply with success or error, 295 // nor does it transfer a release fence. This version may be used in 296 // situations where a release fence is not needed. Because of the asynchronous 297 // nature of the underlying message, no error is returned if this method is 298 // called when the buffer is in an incorrect state. Returns zero if sending 299 // the message succeeded, or a negative errno code otherwise. 300 int ReleaseAsync(); 301 302 // May be called after or instead of Acquire to indicate that the consumer 303 // does not need to access the buffer this cycle. This returns zero or a 304 // negative unix error code. 305 int Discard(); 306 307 // When set, this consumer is no longer notified when this buffer is 308 // available. The system behaves as if Discard() is immediately called 309 // whenever the buffer is posted. If ignore is set to true while a buffer is 310 // pending, it will act as if Discard() was also called. 311 // This returns zero or a negative unix error code. 312 int SetIgnore(bool ignore); 313 314 private: 315 friend BASE; 316 317 explicit BufferConsumer(LocalChannelHandle channel); 318}; 319 320} // namespace dvr 321} // namespace android 322 323#endif // ANDROID_DVR_BUFFER_HUB_CLIENT_H_ 324