• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /**
18  * @addtogroup Media
19  * @{
20  */
21 
22 /**
23  * @file NdkImageReader.h
24  */
25 
26 /*
27  * This file defines an NDK API.
28  * Do not remove methods.
29  * Do not change method signatures.
30  * Do not change the value of constants.
31  * Do not change the size of any of the classes defined in here.
32  * Do not reference types that are not part of the NDK.
33  * Do not #include files that aren't part of the NDK.
34  */
35 
36 #ifndef _NDK_IMAGE_READER_H
37 #define _NDK_IMAGE_READER_H
38 
39 #include <sys/cdefs.h>
40 #ifdef __ANDROID_VNDK__
41 #include <cutils/native_handle.h>
42 #endif
43 
44 #include <android/native_window.h>
45 #include "NdkMediaError.h"
46 #include "NdkImage.h"
47 
48 __BEGIN_DECLS
49 
50 /**
51  * AImage is an opaque type that allows direct application access to image data rendered into a
52  * {@link ANativeWindow}.
53  */
54 typedef struct AImageReader AImageReader;
55 
56 #if __ANDROID_API__ >= 24
57 
58 /**
59  * Create a new reader for images of the desired size and format.
60  *
61  * <p>
62  * The maxImages parameter determines the maximum number of {@link AImage} objects that can be
63  * acquired from the {@link AImageReader} simultaneously. Requesting more buffers will use up
64  * more memory, so it is important to use only the minimum number necessary for the use case.
65  * </p>
66  * <p>
67  * The valid sizes and formats depend on the source of the image data.
68  * </p>
69  *
70  * @param width The default width in pixels of the Images that this reader will produce.
71  * @param height The default height in pixels of the Images that this reader will produce.
72  * @param format The format of the Image that this reader will produce. This must be one of the
73  *            AIMAGE_FORMAT_* enum value defined in {@link AIMAGE_FORMATS}. Note that not all
74  *            formats are supported. One example is {@link AIMAGE_FORMAT_PRIVATE}, as it is not
75  *            intended to be read by applications directly. That format is supported by
76  *            {@link AImageReader_newWithUsage} introduced in API 26.
77  * @param maxImages The maximum number of images the user will want to access simultaneously. This
78  *            should be as small as possible to limit memory use. Once maxImages Images are obtained
79  *            by the user, one of them has to be released before a new {@link AImage} will become
80  *            available for access through {@link AImageReader_acquireLatestImage} or
81  *            {@link AImageReader_acquireNextImage}. Must be greater than 0.
82  * @param reader The created image reader will be filled here if the method call succeeeds.
83  *
84  * @return <ul>
85  *         <li>{@link AMEDIA_OK} if the method call succeeds.</li>
86  *         <li>{@link AMEDIA_ERROR_INVALID_PARAMETER} if reader is NULL, or one or more of width,
87  *                 height, format, maxImages arguments is not supported.</li>
88  *         <li>{@link AMEDIA_ERROR_UNKNOWN} if the method fails for some other reasons.</li></ul>
89  *
90  * @see AImage
91  */
92 media_status_t AImageReader_new(
93         int32_t width, int32_t height, int32_t format, int32_t maxImages,
94         /*out*/AImageReader** reader) __INTRODUCED_IN(24);
95 
96 /**
97  * Delete an {@link AImageReader} and return all images generated by this reader to system.
98  *
99  * <p>This method will return all {@link AImage} objects acquired by this reader (via
100  * {@link AImageReader_acquireNextImage} or {@link AImageReader_acquireLatestImage}) to system,
101  * making any of data pointers obtained from {@link AImage_getPlaneData} invalid. Do NOT access
102  * the reader object or any of those data pointers after this method returns.</p>
103  *
104  * @param reader The image reader to be deleted.
105  */
106 void AImageReader_delete(AImageReader* reader) __INTRODUCED_IN(24);
107 
108 /**
109  * Get a {@link ANativeWindow} that can be used to produce {@link AImage} for this image reader.
110  *
111  * @param reader The image reader of interest.
112  * @param window The output {@link ANativeWindow} will be filled here if the method call succeeds.
113  *                The {@link ANativeWindow} is managed by this image reader. Do NOT call
114  *                {@link ANativeWindow_release} on it. Instead, use {@link AImageReader_delete}.
115  *
116  * @return <ul>
117  *         <li>{@link AMEDIA_OK} if the method call succeeds.</li>
118  *         <li>{@link AMEDIA_ERROR_INVALID_PARAMETER} if reader or window is NULL.</li></ul>
119  */
120 media_status_t AImageReader_getWindow(AImageReader* reader, /*out*/ANativeWindow** window) __INTRODUCED_IN(24);
121 
122 /**
123  * Query the default width of the {@link AImage} generated by this reader, in pixels.
124  *
125  * <p>The width may be overridden by the producer sending buffers to this reader's
126  * {@link ANativeWindow}. If so, the actual width of the images can be found using
127  * {@link AImage_getWidth}.</p>
128  *
129  * @param reader The image reader of interest.
130  * @param width the default width of the reader will be filled here if the method call succeeeds.
131  *
132  * @return <ul>
133  *         <li>{@link AMEDIA_OK} if the method call succeeds.</li>
134  *         <li>{@link AMEDIA_ERROR_INVALID_PARAMETER} if reader or width is NULL.</li></ul>
135  */
136 media_status_t AImageReader_getWidth(const AImageReader* reader, /*out*/int32_t* width) __INTRODUCED_IN(24);
137 
138 /**
139  * Query the default height of the {@link AImage} generated by this reader, in pixels.
140  *
141  * <p>The height may be overridden by the producer sending buffers to this reader's
142  * {@link ANativeWindow}. If so, the actual height of the images can be found using
143  * {@link AImage_getHeight}.</p>
144  *
145  * @param reader The image reader of interest.
146  * @param height the default height of the reader will be filled here if the method call succeeeds.
147  *
148  * @return <ul>
149  *         <li>{@link AMEDIA_OK} if the method call succeeds.</li>
150  *         <li>{@link AMEDIA_ERROR_INVALID_PARAMETER} if reader or height is NULL.</li></ul>
151  */
152 media_status_t AImageReader_getHeight(const AImageReader* reader, /*out*/int32_t* height) __INTRODUCED_IN(24);
153 
154 /**
155  * Query the format of the {@link AImage} generated by this reader.
156  *
157  * @param reader The image reader of interest.
158  * @param format the fromat of the reader will be filled here if the method call succeeeds. The
159  *                value will be one of the AIMAGE_FORMAT_* enum value defiend in {@link NdkImage.h}.
160  *
161  * @return <ul>
162  *         <li>{@link AMEDIA_OK} if the method call succeeds.</li>
163  *         <li>{@link AMEDIA_ERROR_INVALID_PARAMETER} if reader or format is NULL.</li></ul>
164  */
165 media_status_t AImageReader_getFormat(const AImageReader* reader, /*out*/int32_t* format) __INTRODUCED_IN(24);
166 
167 /**
168  * Query the maximum number of concurrently acquired {@link AImage}s of this reader.
169  *
170  * @param reader The image reader of interest.
171  * @param maxImages the maximum number of concurrently acquired images of the reader will be filled
172  *                here if the method call succeeeds.
173  *
174  * @return <ul>
175  *         <li>{@link AMEDIA_OK} if the method call succeeds.</li>
176  *         <li>{@link AMEDIA_ERROR_INVALID_PARAMETER} if reader or maxImages is NULL.</li></ul>
177  */
178 media_status_t AImageReader_getMaxImages(const AImageReader* reader, /*out*/int32_t* maxImages) __INTRODUCED_IN(24);
179 
180 /**
181  * Acquire the next {@link AImage} from the image reader's queue.
182  *
183  * <p>Warning: Consider using {@link AImageReader_acquireLatestImage} instead, as it will
184  * automatically release older images, and allow slower-running processing routines to catch
185  * up to the newest frame. Usage of {@link AImageReader_acquireNextImage} is recommended for
186  * batch/background processing. Incorrectly using this method can cause images to appear
187  * with an ever-increasing delay, followed by a complete stall where no new images seem to appear.
188  * </p>
189  *
190  * <p>
191  * This method will fail if {@link AImageReader_getMaxImages maxImages} have been acquired with
192  * {@link AImageReader_acquireNextImage} or {@link AImageReader_acquireLatestImage}. In particular
193  * a sequence of {@link AImageReader_acquireNextImage} or {@link AImageReader_acquireLatestImage}
194  * calls greater than {@link AImageReader_getMaxImages maxImages} without calling
195  * {@link AImage_delete} in-between will exhaust the underlying queue. At such a time,
196  * {@link AMEDIA_IMGREADER_MAX_IMAGES_ACQUIRED} will be returned until more images are released with
197  * {@link AImage_delete}.
198  * </p>
199  *
200  * @param reader The image reader of interest.
201  * @param image the acquired {@link AImage} will be filled here if the method call succeeeds.
202  *
203  * @return <ul>
204  *         <li>{@link AMEDIA_OK} if the method call succeeds.</li>
205  *         <li>{@link AMEDIA_ERROR_INVALID_PARAMETER} if reader or image is NULL.</li>
206  *         <li>{@link AMEDIA_IMGREADER_MAX_IMAGES_ACQUIRED} if the number of concurrently acquired
207  *                 images has reached the limit.</li>
208  *         <li>{@link AMEDIA_IMGREADER_NO_BUFFER_AVAILABLE} if there is no buffers currently
209  *                 available in the reader queue.</li>
210  *         <li>{@link AMEDIA_ERROR_UNKNOWN} if the method fails for some other reasons.</li></ul>
211  *
212  * @see AImageReader_acquireLatestImage
213  */
214 media_status_t AImageReader_acquireNextImage(AImageReader* reader, /*out*/AImage** image) __INTRODUCED_IN(24);
215 
216 /**
217 
218  * Acquire the latest {@link AImage} from the image reader's queue, dropping older images.
219  *
220  * <p>
221  * This operation will acquire all the images possible from the image reader, but
222  * {@link AImage_delete} all images that aren't the latest. This function is recommended to use over
223  * {@link AImageReader_acquireNextImage} for most use-cases, as it's more suited for real-time
224  * processing.
225  * </p>
226  * <p>
227  * Note that {@link AImageReader_getMaxImages maxImages} should be at least 2 for
228  * {@link AImageReader_acquireLatestImage} to be any different than
229  * {@link AImageReader_acquireNextImage} - discarding all-but-the-newest {@link AImage} requires
230  * temporarily acquiring two {@link AImage}s at once. Or more generally, calling
231  * {@link AImageReader_acquireLatestImage} with less than two images of margin, that is
232  * (maxImages - currentAcquiredImages < 2) will not discard as expected.
233  * </p>
234  * <p>
235  * This method will fail if {@link AImageReader_getMaxImages maxImages} have been acquired with
236  * {@link AImageReader_acquireNextImage} or {@link AImageReader_acquireLatestImage}. In particular
237  * a sequence of {@link AImageReader_acquireNextImage} or {@link AImageReader_acquireLatestImage}
238  * calls greater than {@link AImageReader_getMaxImages maxImages} without calling
239  * {@link AImage_delete} in-between will exhaust the underlying queue. At such a time,
240  * {@link AMEDIA_IMGREADER_MAX_IMAGES_ACQUIRED} will be returned until more images are released with
241  * {@link AImage_delete}.
242  * </p>
243  *
244  * @param reader The image reader of interest.
245  * @param image the acquired {@link AImage} will be filled here if the method call succeeeds.
246  *
247  * @return <ul>
248  *         <li>{@link AMEDIA_OK} if the method call succeeds.</li>
249  *         <li>{@link AMEDIA_ERROR_INVALID_PARAMETER} if reader or image is NULL.</li>
250  *         <li>{@link AMEDIA_IMGREADER_MAX_IMAGES_ACQUIRED} if the number of concurrently acquired
251  *                 images has reached the limit.</li>
252  *         <li>{@link AMEDIA_IMGREADER_NO_BUFFER_AVAILABLE} if there is no buffers currently
253  *                 available in the reader queue.</li>
254  *         <li>{@link AMEDIA_ERROR_UNKNOWN} if the method fails for some other reasons.</li></ul>
255  *
256  * @see AImageReader_acquireNextImage
257  */
258 media_status_t AImageReader_acquireLatestImage(AImageReader* reader, /*out*/AImage** image) __INTRODUCED_IN(24);
259 
260 
261 /**
262  * Signature of the callback which is called when a new image is available from {@link AImageReader}.
263  *
264  * @param context The optional application context provided by user in
265  *                {@link AImageReader_setImageListener}.
266  * @param session The camera capture session whose state is changing.
267  */
268 typedef void (*AImageReader_ImageCallback)(void* context, AImageReader* reader);
269 
270 typedef struct AImageReader_ImageListener {
271     /// Optional application context passed as the first parameter of the callback.
272     void*                      context;
273 
274     /**
275      * This callback is called when there is a new image available in the image reader's queue.
276      *
277      * <p>The callback happens on one dedicated thread per {@link AImageReader} instance. It is okay
278      * to use AImageReader_* and AImage_* methods within the callback. Note that it is possible that
279      * calling {@link AImageReader_acquireNextImage} or {@link AImageReader_acquireLatestImage}
280      * returns {@link AMEDIA_IMGREADER_NO_BUFFER_AVAILABLE} within this callback. For example, when
281      * there are multiple images and callbacks queued, if application called
282      * {@link AImageReader_acquireLatestImage}, some images will be returned to system before their
283      * corresponding callback is executed.</p>
284      */
285     AImageReader_ImageCallback onImageAvailable;
286 } AImageReader_ImageListener;
287 
288 /**
289  * Set the onImageAvailable listener of this image reader.
290  *
291  * Calling this method will replace previously registered listeners.
292  *
293  * @param reader The image reader of interest.
294  * @param listener The {@link AImageReader_ImageListener} to be registered. Set this to NULL if
295  *                 the application no longer needs to listen to new images.
296  *
297  * @return <ul>
298  *         <li>{@link AMEDIA_OK} if the method call succeeds.</li>
299  *         <li>{@link AMEDIA_ERROR_INVALID_PARAMETER} if reader is NULL.</li></ul>
300  */
301 media_status_t AImageReader_setImageListener(
302         AImageReader* reader, AImageReader_ImageListener* listener) __INTRODUCED_IN(24);
303 
304 #endif /* __ANDROID_API__ >= 24 */
305 
306 #if __ANDROID_API__ >= 26
307 
308 /**
309  * AImageReader constructor similar to {@link AImageReader_new} that takes an additional parameter
310  * for the consumer usage. All other parameters and the return values are identical to those passed
311  * to {@link AImageReader_new}.
312  *
313  * <p>If the \c format is {@link AIMAGE_FORMAT_PRIVATE}, the created {@link AImageReader}
314  * will produce images whose contents are not directly accessible by the application. The application can
315  * still acquire images from this {@link AImageReader} and access {@link AHardwareBuffer} via
316  * {@link AImage_getHardwareBuffer()}. The {@link AHardwareBuffer} gained this way can then
317  * be passed back to hardware (such as GPU or hardware encoder if supported) for future processing.
318  * For example, you can obtain an {@link EGLClientBuffer} from the {@link AHardwareBuffer} by using
319  * {@link eglGetNativeClientBufferANDROID} extension and pass that {@link EGLClientBuffer} to {@link
320  * eglCreateImageKHR} to create an {@link EGLImage} resource type, which may then be bound to a
321  * texture via {@link glEGLImageTargetTexture2DOES} on supported devices. This can be useful for
322  * transporting textures that may be shared cross-process.</p>
323  * <p>In general, when software access to image data is not necessary, an {@link AImageReader}
324  * created with {@link AIMAGE_FORMAT_PRIVATE} format is more efficient, compared with {@link
325  * AImageReader}s using other format such as {@link AIMAGE_FORMAT_YUV_420_888}.</p>
326  *
327  * <p>Note that not all format and usage flag combination is supported by the {@link AImageReader},
328  * especially if \c format is {@link AIMAGE_FORMAT_PRIVATE}, \c usage must not include either
329  * {@link AHARDWAREBUFFER_USAGE_READ_RARELY} or {@link AHARDWAREBUFFER_USAGE_READ_OFTEN}</p>
330  *
331  * @param width The default width in pixels of the Images that this reader will produce.
332  * @param height The default height in pixels of the Images that this reader will produce.
333  * @param format The format of the Image that this reader will produce. This must be one of the
334  *            AIMAGE_FORMAT_* enum value defined in {@link AIMAGE_FORMATS}.
335  * @param usage specifies how the consumer will access the AImage, using combination of the
336  *            AHARDWAREBUFFER_USAGE flags described in {@link hardware_buffer.h}.
337  *            Passing {@link AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN} is equivalent to calling
338  *            {@link AImageReader_new} with the same parameters.
339  *
340  * Note that not all format and usage flag combination is supported by the {@link AImageReader}.
341  * Below are the combinations supported by the {@link AImageReader}.
342  * <table>
343  * <tr>
344  *   <th>Format</th>
345  *   <th>Compatible usage flags</th>
346  * </tr>
347  * <tr>
348  *   <td>non-{@link AIMAGE_FORMAT_PRIVATE PRIVATE} formats defined in {@link AImage.h}
349  * </td>
350  *   <td>{@link AHARDWAREBUFFER_USAGE_CPU_READ_RARELY} or
351  *   {@link AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN}</td>
352  * </tr>
353  * <tr>
354  *   <td>{@link AIMAGE_FORMAT_RGBA_8888}</td>
355  *   <td>{@link AHARDWAREBUFFER_USAGE_VIDEO_ENCODE} or
356  *   {@link AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE}, or combined</td>
357  * </tr>
358  * </table>
359  * @return <ul>
360  *         <li>{@link AMEDIA_OK} if the method call succeeds.</li>
361  *         <li>{@link AMEDIA_ERROR_INVALID_PARAMETER} if reader is NULL, or one or more of width,
362  *                 height, format, maxImages, or usage arguments is not supported.</li>
363  *         <li>{@link AMEDIA_ERROR_UNKNOWN} if the method fails for some other reasons.</li></ul>
364  *
365  * @see AImage
366  * @see AImageReader_new
367  * @see AHardwareBuffer
368  */
369 media_status_t AImageReader_newWithUsage(
370         int32_t width, int32_t height, int32_t format, uint64_t usage, int32_t maxImages,
371         /*out*/ AImageReader** reader) __INTRODUCED_IN(26);
372 
373 /**
374  * Acquire the next {@link AImage} from the image reader's queue asynchronously.
375  *
376  * <p>AImageReader acquire method similar to {@link AImageReader_acquireNextImage} that takes an
377  * additional parameter for the sync fence. All other parameters and the return values are
378  * identical to those passed to {@link AImageReader_acquireNextImage}.</p>
379  *
380  * @param acquireFenceFd A sync fence fd defined in {@link sync.h}, which is used to signal when the
381  *         buffer is ready to consume. When synchronization fence is not needed, fence will be set
382  *         to -1 and the {@link AImage} returned is ready for use immediately. Otherwise, user shall
383  *         use syscalls such as \c poll(), \c epoll(), \c select() to wait for the
384  *         fence fd to change status before attempting to access the {@link AImage} returned.
385  *
386  * @see sync.h
387  * @see sync_get_fence_info
388  */
389 media_status_t AImageReader_acquireNextImageAsync(
390         AImageReader* reader, /*out*/AImage** image, /*out*/int* acquireFenceFd) __INTRODUCED_IN(26);
391 
392 /**
393  * Acquire the latest {@link AImage} from the image reader's queue asynchronously, dropping older
394  * images.
395  *
396  * <p>AImageReader acquire method similar to {@link AImageReader_acquireLatestImage} that takes an
397  * additional parameter for the sync fence. All other parameters and the return values are
398  * identical to those passed to {@link AImageReader_acquireLatestImage}.</p>
399  *
400  * @param acquireFenceFd A sync fence fd defined in {@link sync.h}, which is used to signal when the
401  *         buffer is ready to consume. When synchronization fence is not needed, fence will be set
402  *         to -1 and the {@link AImage} returned is ready for use immediately. Otherwise, user shall
403  *         use syscalls such as \c poll(), \c epoll(), \c select() to wait for the
404  *         fence fd to change status before attempting to access the {@link AImage} returned.
405  *
406  * @see sync.h
407  * @see sync_get_fence_info
408  */
409 media_status_t AImageReader_acquireLatestImageAsync(
410         AImageReader* reader, /*out*/AImage** image, /*out*/int* acquireFenceFd) __INTRODUCED_IN(26);
411 /**
412  * Signature of the callback which is called when {@link AImageReader} is about to remove a buffer.
413  *
414  * @param context The optional application context provided by user in
415  *                {@link AImageReader_setBufferRemovedListener}.
416  * @param reader The {@link AImageReader} of interest.
417  * @param buffer The {@link AHardwareBuffer} that is being removed from this image reader.
418  */
419 typedef void (*AImageReader_BufferRemovedCallback)(void* context,
420         AImageReader* reader,
421         AHardwareBuffer* buffer);
422 
423 typedef struct AImageReader_BufferRemovedListener {
424     /// Optional application context passed as the first parameter of the callback.
425     void*                      context;
426 
427     /**
428      * This callback is called when an old {@link AHardwareBuffer} is about to be removed from the
429      * image reader.
430      *
431      * <p>Note that registering this callback is optional unless the user holds on extra reference
432      * to {@link AHardwareBuffer} returned from {@link AImage_getHardwareBuffer} by calling {@link
433      * AHardwareBuffer_acquire} or creating external graphic objects, such as EglImage, from it.</p>
434      *
435      * <p>If the callback is registered, the {@link AImageReader} will hold on the last of its
436      * references to the {@link AHardwareBuffer} until this callback returns. User can use the
437      * callback to get notified that it becomes the last owner of the buffer. It is up to the user
438      * to decide to either 1) immediately release all of its references to the buffer; or 2) keep
439      * using the buffer and release it in future. Note that, if option 2 if used, user of this API
440      * is responsible to deallocate the buffer properly by calling {@link AHardwareBuffer_release}.
441      * </p>
442      *
443      * @see AHardwareBuffer_release
444      * @see AImage_getHardwareBuffer
445      */
446     AImageReader_BufferRemovedCallback onBufferRemoved;
447 } AImageReader_BufferRemovedListener;
448 
449 /**
450  * Set the onBufferRemoved listener of this image reader.
451  *
452  * <p>Note that calling this method will replace previously registered listeners.</p>
453  *
454  * @param reader The image reader of interest.
455  * @param listener the {@link AImageReader_BufferRemovedListener} to be registered. Set this to
456  * NULL if application no longer needs to listen to buffer removed events.
457  *
458  * @return <ul>
459  *         <li>{@link AMEDIA_OK} if the method call succeeds.</li>
460  *         <li>{@link AMEDIA_ERROR_INVALID_PARAMETER} if reader is NULL.</li></ul>
461  *
462  * @see AImage_getHardwareBuffer
463  */
464 media_status_t AImageReader_setBufferRemovedListener(
465         AImageReader* reader, AImageReader_BufferRemovedListener* listener) __INTRODUCED_IN(26);
466 
467 #ifdef __ANDROID_VNDK__
468 /*
469  * Get the native_handle_t corresponding to the ANativeWindow owned by the
470  * AImageReader provided.
471  *
472  * @param reader The image reader of interest.
473  * @param handle The output native_handle_t. This native handle is owned by
474  *               this image reader.
475  *
476  * @return AMEDIA_OK if the method call succeeds.
477  *         AMEDIA_ERROR_INVALID_PARAMETER if reader or handle are NULL.
478  *         AMEDIA_ERROR_UNKNOWN if some other error is encountered.
479  */
480 media_status_t AImageReader_getWindowNativeHandle(
481     AImageReader *reader, /* out */native_handle_t **handle);
482 #endif
483 
484 #endif /* __ANDROID_API__ >= 26 */
485 
486 __END_DECLS
487 
488 #endif //_NDK_IMAGE_READER_H
489 
490 /** @} */
491