1 /* 2 * Copyright 2017 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 #ifndef ANDROID_VNDK_NATIVEWINDOW_AHARDWAREBUFFER_H 18 #define ANDROID_VNDK_NATIVEWINDOW_AHARDWAREBUFFER_H 19 20 // vndk is a superset of the NDK 21 #include <android/hardware_buffer.h> 22 23 #include <cutils/native_handle.h> 24 #include <errno.h> 25 26 __BEGIN_DECLS 27 28 /** 29 * Get the native handle from an AHardwareBuffer. 30 * 31 * \return a non-NULL native handle on success, NULL if \a buffer is nullptr or the operation fails 32 * for any reason. 33 */ 34 const native_handle_t* _Nullable AHardwareBuffer_getNativeHandle( 35 const AHardwareBuffer* _Nonnull buffer); 36 37 enum CreateFromHandleMethod { 38 // enum values chosen to match internal GraphicBuffer::HandleWrapMethod 39 AHARDWAREBUFFER_CREATE_FROM_HANDLE_METHOD_REGISTER = 2, 40 AHARDWAREBUFFER_CREATE_FROM_HANDLE_METHOD_CLONE = 3, 41 }; 42 43 /** 44 * Create an AHardwareBuffer from a native handle. 45 * 46 * This function wraps a native handle in an AHardwareBuffer suitable for use by applications or 47 * other parts of the system. The contents of desc will be returned by AHardwareBuffer_describe(). 48 * 49 * If method is AHARDWAREBUFFER_CREATE_FROM_HANDLE_METHOD_REGISTER, the handle is assumed to be 50 * unregistered, and it will be registered/imported before being wrapped in the AHardwareBuffer. 51 * If successful, the AHardwareBuffer will own the handle. 52 * 53 * If method is AHARDWAREBUFFER_CREATE_FROM_HANDLE_METHOD_CLONE, the handle will be cloned and the 54 * clone registered. The AHardwareBuffer will own the cloned handle but not the original. 55 * 56 * \return 0 on success, -EINVAL if \a desc or \a handle or outBuffer is NULL, or an error number if 57 * the operation fails for any reason. 58 */ 59 int AHardwareBuffer_createFromHandle(const AHardwareBuffer_Desc* _Nonnull desc, 60 const native_handle_t* _Nonnull handle, int32_t method, 61 AHardwareBuffer* _Nullable* _Nonnull outBuffer); 62 63 /** 64 * Buffer pixel formats. 65 */ 66 enum { 67 /* for future proofing, keep these in sync with system/graphics-base.h */ 68 69 /* same as HAL_PIXEL_FORMAT_BGRA_8888 */ 70 AHARDWAREBUFFER_FORMAT_B8G8R8A8_UNORM = 5, 71 /* same as HAL_PIXEL_FORMAT_YV12 */ 72 AHARDWAREBUFFER_FORMAT_YV12 = 0x32315659, 73 /* same as HAL_PIXEL_FORMAT_Y8 */ 74 AHARDWAREBUFFER_FORMAT_Y8 = 0x20203859, 75 /* same as HAL_PIXEL_FORMAT_Y16 */ 76 AHARDWAREBUFFER_FORMAT_Y16 = 0x20363159, 77 /* same as HAL_PIXEL_FORMAT_RAW16 */ 78 AHARDWAREBUFFER_FORMAT_RAW16 = 0x20, 79 /* same as HAL_PIXEL_FORMAT_RAW10 */ 80 AHARDWAREBUFFER_FORMAT_RAW10 = 0x25, 81 /* same as HAL_PIXEL_FORMAT_RAW12 */ 82 AHARDWAREBUFFER_FORMAT_RAW12 = 0x26, 83 /* same as HAL_PIXEL_FORMAT_RAW_OPAQUE */ 84 AHARDWAREBUFFER_FORMAT_RAW_OPAQUE = 0x24, 85 /* same as HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED */ 86 AHARDWAREBUFFER_FORMAT_IMPLEMENTATION_DEFINED = 0x22, 87 /* same as HAL_PIXEL_FORMAT_YCBCR_422_SP */ 88 AHARDWAREBUFFER_FORMAT_YCbCr_422_SP = 0x10, 89 /* same as HAL_PIXEL_FORMAT_YCRCB_420_SP */ 90 AHARDWAREBUFFER_FORMAT_YCrCb_420_SP = 0x11, 91 /* same as HAL_PIXEL_FORMAT_YCBCR_422_I */ 92 AHARDWAREBUFFER_FORMAT_YCbCr_422_I = 0x14, 93 }; 94 95 /** 96 * Buffer usage flags. 97 */ 98 enum { 99 /* for future proofing, keep these in sync with hardware/gralloc.h */ 100 101 /* The buffer will be written by the HW camera pipeline. */ 102 AHARDWAREBUFFER_USAGE_CAMERA_WRITE = 2UL << 16, 103 /* The buffer will be read by the HW camera pipeline. */ 104 AHARDWAREBUFFER_USAGE_CAMERA_READ = 4UL << 16, 105 /* Mask for the camera access values. */ 106 AHARDWAREBUFFER_USAGE_CAMERA_MASK = 6UL << 16, 107 }; 108 109 /** 110 * Additional options for AHardwareBuffer_allocateWithOptions. These correspond to 111 * android.hardware.graphics.common.ExtendableType 112 */ 113 typedef struct { 114 const char* _Nonnull name; 115 int64_t value; 116 } AHardwareBufferLongOptions; 117 118 enum AHardwareBufferStatus : int32_t { 119 /* Success, no error */ 120 AHARDWAREBUFFER_STATUS_OK = 0, 121 /* There's insufficient memory to satisfy the request */ 122 AHARDWAREBUFFER_STATUS_NO_MEMORY = -ENOMEM, 123 /* The given argument is invalid */ 124 AHARDWAREBUFFER_STATUS_BAD_VALUE = -EINVAL, 125 /* The requested operation is not supported by the device */ 126 AHARDWAREBUFFER_STATUS_UNSUPPORTED = -ENOSYS, 127 /* An unknown error occurred */ 128 AHARDWAREBUFFER_STATUS_UNKNOWN_ERROR = (-2147483647 - 1), 129 }; 130 131 /** 132 * Allocates a buffer that matches the passed AHardwareBuffer_Desc with additional options 133 * 134 * If allocation succeeds, the buffer can be used according to the 135 * usage flags specified in its description. If a buffer is used in ways 136 * not compatible with its usage flags, the results are undefined and 137 * may include program termination. 138 * 139 * @param desc The AHardwareBuffer_Desc that describes the allocation to request. Note that `stride` 140 * is ignored. 141 * @param additionalOptions A pointer to an array of AHardwareBufferLongOptions with additional 142 * string key + long value options that may be specified. May be null if 143 * `additionalOptionsSize` is 0 144 * @param additionalOptionsSize The number of additional options to pass 145 * @param outBuffer The resulting buffer allocation 146 * @return AHARDWAREBUFFER_STATUS_OK on success 147 * AHARDWAREBUFFER_STATUS_NO_MEMORY if there's insufficient resources for the allocation 148 * AHARDWAREBUFFER_STATUS_BAD_VALUE if the provided description & options are not supported 149 * by the device 150 * AHARDWAREBUFFER_STATUS_UNKNOWN_ERROR for any other error 151 * any reason. The returned buffer has a reference count of 1. 152 */ 153 enum AHardwareBufferStatus AHardwareBuffer_allocateWithOptions( 154 const AHardwareBuffer_Desc* _Nonnull desc, 155 const AHardwareBufferLongOptions* _Nullable additionalOptions, size_t additionalOptionsSize, 156 AHardwareBuffer* _Nullable* _Nonnull outBuffer) __INTRODUCED_IN(__ANDROID_API_V__); 157 158 /** 159 * Queries the dataspace of the given AHardwareBuffer. 160 * 161 * @param buffer The non-null buffer for which to query the Dataspace 162 * @return The dataspace of the buffer, or ADATASPACE_UNKNOWN if one hasn't been set 163 */ 164 enum ADataSpace AHardwareBuffer_getDataSpace(const AHardwareBuffer* _Nonnull buffer) 165 __INTRODUCED_IN(__ANDROID_API_V__); 166 167 /** 168 * Sets the dataspace of the given AHardwareBuffer 169 * @param buffer The non-null buffer for which to set the dataspace 170 * @param dataSpace The dataspace to set 171 * @return AHARDWAREBUFFER_STATUS_OK on success, 172 * AHARDWAREBUFFER_STATUS_UNSUPPORTED if the device doesn't support setting the dataspace, 173 * AHARDWAREBUFFER_STATUS_UNKNOWN_ERROR for any other failure. 174 */ 175 enum AHardwareBufferStatus AHardwareBuffer_setDataSpace(AHardwareBuffer* _Nonnull buffer, 176 enum ADataSpace dataSpace) 177 __INTRODUCED_IN(__ANDROID_API_V__); 178 179 __END_DECLS 180 181 #endif /* ANDROID_VNDK_NATIVEWINDOW_AHARDWAREBUFFER_H */ 182