summaryrefslogtreecommitdiff
path: root/libs/vr/libdvr/include/dvr/dvr_buffer_queue.h
blob: 8b9e0482d9d7c4d3d578f2a1d47fa96e533cae2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#ifndef ANDROID_DVR_BUFFER_QUEUE_H_
#define ANDROID_DVR_BUFFER_QUEUE_H_

#include <sys/cdefs.h>

#include <dvr/dvr_buffer.h>

__BEGIN_DECLS

typedef struct ANativeWindow ANativeWindow;

typedef struct DvrWriteBufferQueue DvrWriteBufferQueue;
typedef struct DvrReadBufferQueue DvrReadBufferQueue;

// Creates a write buffer queue to be used locally.
//
// Note that this API is mostly for testing purpose. For now there is no
// mechanism to send a DvrWriteBufferQueue cross process. Use
// dvrSurfaceCreateWriteBufferQueue if cross-process buffer transport is
// intended.
//
// @param width The width of the buffers that this queue will produce.
// @param height The height of buffers that this queue will produce.
// @param format The format of the buffers that this queue will produce. This
//     must be one of the AHARDWAREBUFFER_FORMAT_XXX enums.
// @param layer_count The number of layers of the buffers that this queue will
//     produce.
// @param usage The usage of the buffers that this queue will produce. This
//     must a combination of the AHARDWAREBUFFER_USAGE_XXX flags.
// @param capacity The number of buffer that this queue will allocate. Note that
//     all buffers will be allocated on create. Currently, the number of buffers
//     is the queue cannot be changed after creation though DVR API. However,
//     ANativeWindow can choose to reallocate, attach, or detach buffers from
//     a DvrWriteBufferQueue through Android platform logic.
// @param metadata_size The size of metadata in bytes.
// @param out_write_queue The pointer of a DvrWriteBufferQueue will be filled
//      here if the method call succeeds. The metadata size must match
//      the metadata size in dvrWriteBufferPost/dvrReadBufferAcquire.
// @return Zero on success, or negative error code.
int dvrWriteBufferQueueCreate(uint32_t width, uint32_t height, uint32_t format,
                              uint32_t layer_count, uint64_t usage,
                              size_t capacity, size_t metadata_size,
                              DvrWriteBufferQueue** out_write_queue);

// Destroy a write buffer queue.
//
// @param write_queue The DvrWriteBufferQueue of interest.
void dvrWriteBufferQueueDestroy(DvrWriteBufferQueue* write_queue);

// Get the total number of buffers in a write buffer queue.
//
// @param write_queue The DvrWriteBufferQueue of interest.
// @return The capacity on success; or negative error code.
ssize_t dvrWriteBufferQueueGetCapacity(DvrWriteBufferQueue* write_queue);

// Get the system unique queue id of a write buffer queue.
//
// @param write_queue The DvrWriteBufferQueue of interest.
// @return Queue id on success; or negative error code.
int dvrWriteBufferQueueGetId(DvrWriteBufferQueue* write_queue);

// Gets an ANativeWindow backed by the DvrWriteBufferQueue
//
// Can be casted to a Java Surface using ANativeWindow_toSurface NDK API. Note
// that the native window is lazily created at the first time |GetNativeWindow|
// is called, and the created ANativeWindow will be cached so that multiple
// calls to this method will return the same object. Also note that this method
// does not acquire an additional reference to the ANativeWindow returned, don't
// call ANativeWindow_release on it.
//
// @param write_queue The DvrWriteBufferQueue of interest.
// @param out_window The pointer of an ANativeWindow will be filled here if
//     the method call succeeds.
// @return Zero on success; or -EINVAL if this DvrWriteBufferQueue does not
//     support being used as an android Surface.
int dvrWriteBufferQueueGetANativeWindow(DvrWriteBufferQueue* write_queue,
                                        ANativeWindow** out_window);

// @deprecated Please use dvrWriteBufferQueueGetANativeWindow instead.
int dvrWriteBufferQueueGetExternalSurface(DvrWriteBufferQueue* write_queue,
                                          ANativeWindow** out_window);

// Create a read buffer queue from an existing write buffer queue.
//
// @param write_queue The DvrWriteBufferQueue of interest.
// @param out_read_queue The pointer of a DvrReadBufferQueue will be filled here
//     if the method call succeeds.
// @return Zero on success, or negative error code.
int dvrWriteBufferQueueCreateReadQueue(DvrWriteBufferQueue* write_queue,
                                       DvrReadBufferQueue** out_read_queue);

// Dequeue a buffer to write into.
//
// @param write_queue The DvrWriteBufferQueue of interest.
// @param timeout Specifies the number of milliseconds that the method will
//     block. Specifying a timeout of -1 causes it to block indefinitely,
//     while specifying a timeout equal to zero cause it to return immediately,
//     even if no buffers are available.
// @param out_buffer A targeting DvrWriteBuffer object to hold the output of the
//     dequeue operation. Must be created by |dvrWriteBufferCreateEmpty|.
// @param out_fence_fd A sync fence fd defined in NDK's sync.h API, which
//     signals the release of underlying buffer. The producer should wait until
//     this fence clears before writing data into it.
// @return Zero on success, or negative error code.
int dvrWriteBufferQueueDequeue(DvrWriteBufferQueue* write_queue, int timeout,
                               DvrWriteBuffer* out_buffer, int* out_fence_fd);

// Overrides buffer dimension with new width and height.
//
// After the call successfully returns, each |dvrWriteBufferQueueDequeue| call
// will return buffer with newly assigned |width| and |height|. When necessary,
// old buffer will be removed from the buffer queue and replaced with new buffer
// matching the new buffer size.
//
// @param write_queue The DvrWriteBufferQueue of interest.
// @param width Desired width, cannot be Zero.
// @param height Desired height, cannot be Zero.
// @return Zero on success, or negative error code.
int dvrWriteBufferQueueResizeBuffer(DvrWriteBufferQueue* write_queue,
                                    uint32_t width, uint32_t height);

// Destroy a read buffer queue.
//
// @param read_queue The DvrReadBufferQueue of interest.
void dvrReadBufferQueueDestroy(DvrReadBufferQueue* read_queue);

// Get the total number of buffers in a read buffer queue.
//
// @param read_queue The DvrReadBufferQueue of interest.
// @return The capacity on success; or negative error code.
ssize_t dvrReadBufferQueueGetCapacity(DvrReadBufferQueue* read_queue);

// Get the system unique queue id of a read buffer queue.
//
// @param read_queue The DvrReadBufferQueue of interest.
// @return Queue id on success; or negative error code.
int dvrReadBufferQueueGetId(DvrReadBufferQueue* read_queue);

// Get the event fd that signals when queue updates occur.
//
// Use ReadBufferQueueHandleEvents to trigger registered event callbacks.
//
// @param read_queue The DvrReadBufferQueue of interest.
// @return Fd on success; or negative error code.
int dvrReadBufferQueueGetEventFd(DvrReadBufferQueue* read_queue);

// Create a read buffer queue from an existing read buffer queue.
//
// @param read_queue The DvrReadBufferQueue of interest.
// @param out_read_queue The pointer of a DvrReadBufferQueue will be filled here
//     if the method call succeeds.
// @return Zero on success, or negative error code.
int dvrReadBufferQueueCreateReadQueue(DvrReadBufferQueue* read_queue,
                                      DvrReadBufferQueue** out_read_queue);

// Dequeue a buffer to read from.
//
// @param read_queue The DvrReadBufferQueue of interest.
// @param timeout Specifies the number of milliseconds that the method will
//     block. Specifying a timeout of -1 causes it to block indefinitely,
//     while specifying a timeout equal to zero cause it to return immediately,
//     even if no buffers are available.
// @param out_buffer A targeting DvrReadBuffer object to hold the output of the
//     dequeue operation. Must be created by |dvrReadBufferCreateEmpty|.
// @param out_fence_fd A sync fence fd defined in NDK's sync.h API, which
//     signals the release of underlying buffer. The consumer should wait until
//     this fence clears before reading data from it.
// @param out_meta The memory area where a metadata object will be filled.
//     Can be nullptr iff |meta_size_bytes| is zero (i.e., there is no
//     metadata).
// @param meta_size_bytes Size of the metadata object caller expects. If it
//     doesn't match the size of actually metadata transported by the buffer
//     queue, the method returns -EINVAL.
// @return Zero on success, or negative error code.
int dvrReadBufferQueueDequeue(DvrReadBufferQueue* read_queue, int timeout,
                              DvrReadBuffer* out_buffer, int* out_fence_fd,
                              void* out_meta, size_t meta_size_bytes);

// Callback function which will be called when a buffer is avaiable.
//
// Note that there is no guarantee of thread safety and on which thread the
// callback will be fired.
//
// @param context User provided opaque pointer.
typedef void (*DvrReadBufferQueueBufferAvailableCallback)(void* context);

// Set buffer avaiable callback.
//
// @param read_queue The DvrReadBufferQueue of interest.
// @param callback The callback function. Set this to NULL if caller no longer
//     needs to listen to new buffer available events.
// @param context User provided opaque pointer, will be passed back during
//     callback. The caller is responsible for ensuring the validity of the
//     context through the life cycle of the DvrReadBufferQueue.
// @return Zero on success, or negative error code.
int dvrReadBufferQueueSetBufferAvailableCallback(
    DvrReadBufferQueue* read_queue,
    DvrReadBufferQueueBufferAvailableCallback callback, void* context);

// Callback function which will be called when a buffer is about to be removed.
//
// Note that there is no guarantee of thread safety and on which thread the
// callback will be fired.
//
// @param buffer The buffer being removed. Once the callbacks returns, this
//     buffer will be dereferenced from the buffer queue. If user has ever
//     cached other DvrReadBuffer/AHardwareBuffer/EglImageKHR objects derived
//     from this buffer, it's the user's responsibility to clean them up.
//     Note that the ownership of the read buffer is not passed to this
//     callback, so it should call dvrReadBufferDestroy on the buffer.
// @param context User provided opaque pointer.
typedef void (*DvrReadBufferQueueBufferRemovedCallback)(DvrReadBuffer* buffer,
                                                        void* context);

// Set buffer removed callback.
//
// @param read_queue The DvrReadBufferQueue of interest.
// @param callback The callback function. Set this to NULL if caller no longer
//     needs to listen to buffer removed events.
// @param context User provided opaque pointer, will be passed back during
//     callback. The caller is responsible for ensuring the validity of the
//     context through the life cycle of the DvrReadBufferQueue.
// @return Zero on success, or negative error code.
int dvrReadBufferQueueSetBufferRemovedCallback(
    DvrReadBufferQueue* read_queue,
    DvrReadBufferQueueBufferRemovedCallback callback, void* context);

// Handle all pending events on the read queue.
//
// @param read_queue The DvrReadBufferQueue of interest.
// @return Zero on success, or negative error code.
int dvrReadBufferQueueHandleEvents(DvrReadBufferQueue* read_queue);

__END_DECLS

#endif  // ANDROID_DVR_BUFFER_QUEUE_H_