1 #ifndef ANDROID_DVR_DETACHED_BUFFER_H_ 2 #define ANDROID_DVR_DETACHED_BUFFER_H_ 3 4 #include <private/dvr/buffer_hub_client.h> 5 6 namespace android { 7 namespace dvr { 8 9 class DetachedBuffer { 10 public: 11 // Allocates a standalone DetachedBuffer not associated with any producer 12 // consumer set. Create(uint32_t width,uint32_t height,uint32_t layer_count,uint32_t format,uint64_t usage,size_t user_metadata_size)13 static std::unique_ptr<DetachedBuffer> Create(uint32_t width, uint32_t height, 14 uint32_t layer_count, 15 uint32_t format, uint64_t usage, 16 size_t user_metadata_size) { 17 return std::unique_ptr<DetachedBuffer>(new DetachedBuffer( 18 width, height, layer_count, format, usage, user_metadata_size)); 19 } 20 21 // Imports the given channel handle to a DetachedBuffer, taking ownership. Import(pdx::LocalChannelHandle channel_handle)22 static std::unique_ptr<DetachedBuffer> Import( 23 pdx::LocalChannelHandle channel_handle) { 24 return std::unique_ptr<DetachedBuffer>( 25 new DetachedBuffer(std::move(channel_handle))); 26 } 27 28 DetachedBuffer(const DetachedBuffer&) = delete; 29 void operator=(const DetachedBuffer&) = delete; 30 buffer()31 const sp<GraphicBuffer>& buffer() const { return buffer_.buffer(); } 32 id()33 int id() const { return id_; } 34 35 // Returns true if the buffer holds an open PDX channels towards bufferhubd. IsConnected()36 bool IsConnected() const { return client_.IsValid(); } 37 38 // Returns true if the buffer holds an valid gralloc buffer handle that's 39 // availble for the client to read from and/or write into. IsValid()40 bool IsValid() const { return buffer_.IsValid(); } 41 42 // Returns the event mask for all the events that are pending on this buffer 43 // (see sys/poll.h for all possible bits). GetEventMask(int events)44 pdx::Status<int> GetEventMask(int events) { 45 if (auto* channel = client_.GetChannel()) { 46 return channel->GetEventMask(events); 47 } else { 48 return pdx::ErrorStatus(EINVAL); 49 } 50 } 51 52 // Polls the fd for |timeout_ms| milliseconds (-1 for infinity). 53 int Poll(int timeout_ms); 54 55 // Promotes a DetachedBuffer to become a ProducerBuffer. Once promoted the 56 // DetachedBuffer channel will be closed automatically on successful IPC 57 // return. Further IPCs towards this channel will return error. 58 pdx::Status<pdx::LocalChannelHandle> Promote(); 59 60 // Takes the underlying graphic buffer out of this DetachedBuffer. This call 61 // immediately invalidates this DetachedBuffer object and transfers the 62 // underlying pdx::LocalChannelHandle into the GraphicBuffer. 63 sp<GraphicBuffer> TakeGraphicBuffer(); 64 65 private: 66 DetachedBuffer(uint32_t width, uint32_t height, uint32_t layer_count, 67 uint32_t format, uint64_t usage, size_t user_metadata_size); 68 69 DetachedBuffer(pdx::LocalChannelHandle channel_handle); 70 71 int ImportGraphicBuffer(); 72 73 // Global id for the buffer that is consistent across processes. 74 int id_; 75 IonBuffer buffer_; 76 BufferHubClient client_; 77 }; 78 79 } // namespace dvr 80 } // namespace android 81 82 #endif // ANDROID_DVR_DETACHED_BUFFER_H_ 83