1 #ifndef ANDROID_DVR_CONSUMER_BUFFER_H_
2 #define ANDROID_DVR_CONSUMER_BUFFER_H_
3 
4 #include <private/dvr/buffer_hub_base.h>
5 
6 namespace android {
7 namespace dvr {
8 
9 // This is a connection to a producer buffer, which can be located in another
10 // application. When that buffer is Post()ed, this fd will be signaled and
11 // Acquire allows read access. The user is responsible for making sure that
12 // Acquire is called with the correct metadata structure. The only guarantee the
13 // API currently provides is that an Acquire() with metadata of the wrong size
14 // will fail.
15 class ConsumerBuffer : public pdx::ClientBase<ConsumerBuffer, BufferHubBase> {
16  public:
17   // This call assumes ownership of |fd|.
18   static std::unique_ptr<ConsumerBuffer> Import(LocalChannelHandle channel);
19   static std::unique_ptr<ConsumerBuffer> Import(
20       Status<LocalChannelHandle> status);
21 
22   // Attempt to retrieve a post event from buffer hub. If successful,
23   // |ready_fence| will be set to a fence to wait on until the buffer is ready.
24   // This call will only succeed after the fd is signalled. This call may be
25   // performed as an alternative to the Acquire() with metadata. In such cases
26   // the metadata is not read.
27   //
28   // This returns zero or negative unix error code.
29   int Acquire(LocalHandle* ready_fence);
30 
31   // Attempt to retrieve a post event from buffer hub. If successful,
32   // |ready_fence| is set to a fence signaling that the contents of the buffer
33   // are available. This call will only succeed if the buffer is in the posted
34   // state.
35   // Returns zero on success, or a negative errno code otherwise.
36   int Acquire(LocalHandle* ready_fence, void* meta, size_t user_metadata_size);
37 
38   // Asynchronously acquires a bufer.
39   int AcquireAsync(DvrNativeBufferMetadata* out_meta, LocalHandle* out_fence);
40 
41   // Releases the buffer from any buffer state. If the fence is valid the fence
42   // determines the buffer usage, otherwise the buffer is released immediately.
43   // This returns zero or a negative unix error code.
44   int Release(const LocalHandle& release_fence);
45   int ReleaseAsync();
46 
47   // Asynchronously releases a buffer. Similar to the synchronous version above,
48   // except that it does not wait for BufferHub to reply with success or error.
49   // The fence and metadata are passed to consumer via shared fd and shared
50   // memory.
51   int ReleaseAsync(const DvrNativeBufferMetadata* meta,
52                    const LocalHandle& release_fence);
53 
54   // May be called after or instead of Acquire to indicate that the consumer
55   // does not need to access the buffer this cycle. This returns zero or a
56   // negative unix error code.
57   int Discard();
58 
59  private:
60   friend BASE;
61 
62   explicit ConsumerBuffer(LocalChannelHandle channel);
63 
64   // Local state transition helpers.
65   int LocalAcquire(DvrNativeBufferMetadata* out_meta, LocalHandle* out_fence);
66   int LocalRelease(const DvrNativeBufferMetadata* meta,
67                    const LocalHandle& release_fence);
68 };
69 
70 }  // namespace dvr
71 }  // namespace android
72 
73 #endif  // ANDROID_DVR_CONSUMER_BUFFER_H_
74