| Craig Donner | c7e8dae | 2017-01-03 10:24:58 -0800 | [diff] [blame] | 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 | /** | 
|  | 18 | * @file hardware_buffer.h | 
|  | 19 | */ | 
|  | 20 |  | 
|  | 21 | #ifndef ANDROID_HARDWARE_BUFFER_H | 
|  | 22 | #define ANDROID_HARDWARE_BUFFER_H | 
|  | 23 |  | 
|  | 24 | #include <inttypes.h> | 
|  | 25 |  | 
|  | 26 | #include <sys/cdefs.h> | 
|  | 27 |  | 
|  | 28 | #include <android/rect.h> | 
|  | 29 |  | 
|  | 30 | __BEGIN_DECLS | 
|  | 31 |  | 
|  | 32 | /** | 
|  | 33 | * Buffer pixel formats. | 
|  | 34 | */ | 
|  | 35 | enum { | 
|  | 36 | /** | 
|  | 37 | * Corresponding formats: | 
|  | 38 | *   Vulkan: VK_FORMAT_R8G8B8A8_UNORM | 
|  | 39 | *   OpenGL ES: GL_RGBA8 | 
|  | 40 | */ | 
|  | 41 | AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM      = 1, | 
|  | 42 |  | 
|  | 43 | /** | 
|  | 44 | * Corresponding formats: | 
|  | 45 | *   Vulkan: VK_FORMAT_R8G8B8A8_UNORM | 
|  | 46 | *   OpenGL ES: GL_RGBA8 | 
|  | 47 | */ | 
|  | 48 | AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM      = 2, | 
|  | 49 |  | 
|  | 50 | /** | 
|  | 51 | * Corresponding formats: | 
|  | 52 | *   Vulkan: VK_FORMAT_R8G8B8_UNORM | 
|  | 53 | *   OpenGL ES: GL_RGB8 | 
|  | 54 | */ | 
|  | 55 | AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM        = 3, | 
|  | 56 |  | 
|  | 57 | /** | 
|  | 58 | * Corresponding formats: | 
|  | 59 | *   Vulkan: VK_FORMAT_R5G6B5_UNORM_PACK16 | 
|  | 60 | *   OpenGL ES: GL_RGB565 | 
|  | 61 | */ | 
|  | 62 | AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM        = 4, | 
|  | 63 |  | 
|  | 64 | /** | 
|  | 65 | * Corresponding formats: | 
|  | 66 | *   Vulkan: VK_FORMAT_R16G16B16A16_SFLOAT | 
|  | 67 | *   OpenGL ES: GL_RGBA16F | 
|  | 68 | */ | 
|  | 69 | AHARDWAREBUFFER_FORMAT_R16G16B16A16_SFLOAT = 5, | 
|  | 70 | }; | 
|  | 71 |  | 
|  | 72 | enum { | 
|  | 73 | /* The buffer will sometimes be read by the CPU */ | 
|  | 74 | AHARDWAREBUFFER_USAGE0_CPU_READ               = 1ULL << 1, | 
|  | 75 | /* The buffer will often be read by the CPU*/ | 
|  | 76 | AHARDWAREBUFFER_USAGE0_CPU_READ_OFTEN         = 1ULL << 2 | | 
|  | 77 | AHARDWAREBUFFER_USAGE0_CPU_READ, | 
|  | 78 | /* The buffer will sometimes be written to by the CPU */ | 
|  | 79 | AHARDWAREBUFFER_USAGE0_CPU_WRITE              = 1ULL << 5, | 
|  | 80 | /* The buffer will often be written to by the CPU */ | 
|  | 81 | AHARDWAREBUFFER_USAGE0_CPU_WRITE_OFTEN        = 1ULL << 6 | | 
|  | 82 | AHARDWAREBUFFER_USAGE0_CPU_WRITE, | 
|  | 83 | /* The buffer will be read from by the GPU */ | 
|  | 84 | AHARDWAREBUFFER_USAGE0_GPU_SAMPLED_IMAGE      = 1ULL << 10, | 
|  | 85 | /* The buffer will be written to by the GPU */ | 
|  | 86 | AHARDWAREBUFFER_USAGE0_GPU_COLOR_OUTPUT       = 1ULL << 11, | 
|  | 87 | /* The buffer will be read from and written to by the GPU */ | 
|  | 88 | AHARDWAREBUFFER_USAGE0_GPU_STORAGE_IMAGE      = | 
|  | 89 | AHARDWAREBUFFER_USAGE0_GPU_SAMPLED_IMAGE | | 
|  | 90 | AHARDWAREBUFFER_USAGE0_GPU_COLOR_OUTPUT, | 
|  | 91 | /* The buffer will be used as a cubemap texture */ | 
|  | 92 | AHARDWAREBUFFER_USAGE0_GPU_CUBEMAP            = 1ULL << 13, | 
|  | 93 | /* The buffer will be used as a shader storage or uniform buffer object*/ | 
|  | 94 | AHARDWAREBUFFER_USAGE0_GPU_DATA_BUFFER        = 1ULL << 14, | 
|  | 95 | /* The buffer must not be used outside of a protected hardware path */ | 
|  | 96 | AHARDWAREBUFFER_USAGE0_PROTECTED_CONTENT      = 1ULL << 18, | 
|  | 97 | /** The buffer will be used for sensor direct data */ | 
|  | 98 | AHARDWAREBUFFER_USAGE0_SENSOR_DIRECT_DATA     = 1ULL << 29, | 
|  | 99 | /* The buffer will be read by a hardware video encoder */ | 
|  | 100 | AHARDWAREBUFFER_USAGE0_VIDEO_ENCODE           = 1ULL << 21, | 
|  | 101 | }; | 
|  | 102 |  | 
|  | 103 | typedef struct AHardwareBuffer_Desc { | 
|  | 104 | uint32_t    width; | 
|  | 105 | uint32_t    height; | 
|  | 106 | uint32_t    layers; | 
|  | 107 | uint64_t    usage0;     // Combination of AHARDWAREBUFFER_USAGE0_* | 
|  | 108 | uint64_t    usage1;     // Initialize to zero, reserved for future use | 
|  | 109 | uint32_t    format;     // One of AHARDWAREBUFFER_FORMAT_* | 
|  | 110 | } AHardwareBuffer_Desc; | 
|  | 111 |  | 
|  | 112 | typedef struct AHardwareBuffer AHardwareBuffer; | 
|  | 113 |  | 
|  | 114 | /** | 
|  | 115 | * Allocates a buffer that backs an AHardwareBuffer using the passed | 
|  | 116 | * AHardwareBuffer_Desc. | 
|  | 117 | * | 
|  | 118 | * Returns NO_ERROR on success, or an error number of the allocation fails for | 
|  | 119 | * any reason. | 
|  | 120 | */ | 
|  | 121 | int AHardwareBuffer_allocate(const AHardwareBuffer_Desc* desc, | 
|  | 122 | AHardwareBuffer** outBuffer); | 
|  | 123 | /** | 
|  | 124 | * Acquire a reference on the given AHardwareBuffer object.  This prevents the | 
|  | 125 | * object from being deleted until the last reference is removed. | 
|  | 126 | */ | 
|  | 127 | void AHardwareBuffer_acquire(AHardwareBuffer* buffer); | 
|  | 128 |  | 
|  | 129 | /** | 
|  | 130 | * Remove a reference that was previously acquired with | 
|  | 131 | * AHardwareBuffer_acquire(). | 
|  | 132 | */ | 
|  | 133 | void AHardwareBuffer_release(AHardwareBuffer* buffer); | 
|  | 134 |  | 
|  | 135 | /** | 
|  | 136 | * Return a description of the AHardwareBuffer in the passed | 
|  | 137 | * AHardwareBuffer_Desc struct. | 
|  | 138 | */ | 
|  | 139 | void AHardwareBuffer_describe(const AHardwareBuffer* buffer, | 
|  | 140 | AHardwareBuffer_Desc* outDesc); | 
|  | 141 |  | 
|  | 142 | /* | 
|  | 143 | * Lock the AHardwareBuffer for reading or writing, depending on the usage flags | 
|  | 144 | * passed.  This call may block if the hardware needs to finish rendering or if | 
|  | 145 | * CPU caches need to be synchronized, or possibly for other implementation- | 
|  | 146 | * specific reasons.  If fence is not negative, then it specifies a fence file | 
|  | 147 | * descriptor that will be signaled when the buffer is locked, otherwise the | 
|  | 148 | * caller will block until the buffer is available. | 
|  | 149 | * | 
|  | 150 | * If rect is not NULL, the caller promises to modify only data in the area | 
|  | 151 | * specified by rect. If rect is NULL, the caller may modify the contents of the | 
|  | 152 | * entire buffer. | 
|  | 153 | * | 
|  | 154 | * The content of the buffer outside of the specified rect is NOT modified | 
|  | 155 | * by this call. | 
|  | 156 | * | 
|  | 157 | * The buffer usage may only specify AHARDWAREBUFFER_USAGE0_CPU_*. If set, then | 
|  | 158 | * outVirtualAddress is filled with the address of the buffer in virtual memory, | 
|  | 159 | * otherwise this function will fail. | 
|  | 160 | * | 
|  | 161 | * THREADING CONSIDERATIONS: | 
|  | 162 | * | 
|  | 163 | * It is legal for several different threads to lock a buffer for read access; | 
|  | 164 | * none of the threads are blocked. | 
|  | 165 | * | 
|  | 166 | * Locking a buffer simultaneously for write or read/write is undefined, but | 
|  | 167 | * will neither terminate the process nor block the caller; AHardwareBuffer_lock | 
|  | 168 | * may return an error or leave the buffer's content into an indeterminate | 
|  | 169 | * state. | 
|  | 170 | * | 
|  | 171 | * Returns NO_ERROR on success, BAD_VALUE if the buffer is NULL or if the usage0 | 
|  | 172 | * flags are not a combination of AHARDWAREBUFFER_USAGE0_CPU_*, or an error | 
|  | 173 | * number of the lock fails for any reason. | 
|  | 174 | */ | 
|  | 175 | int AHardwareBuffer_lock(AHardwareBuffer* buffer, uint64_t usage0, | 
|  | 176 | int32_t fence, const ARect* rect, void** outVirtualAddress); | 
|  | 177 |  | 
|  | 178 | /* | 
|  | 179 | * Unlock the AHardwareBuffer; must be called after all changes to the buffer | 
|  | 180 | * are completed by the caller. If fence is not NULL then it will be set to a | 
|  | 181 | * file descriptor that is signaled when all pending work on the buffer is | 
|  | 182 | * completed. The caller is responsible for closing the fence when it is no | 
|  | 183 | * longer needed. | 
|  | 184 | * | 
|  | 185 | * Returns NO_ERROR on success, BAD_VALUE if the buffer is NULL, or an error | 
|  | 186 | * number of the lock fails for any reason. | 
|  | 187 | */ | 
|  | 188 | int AHardwareBuffer_unlock(AHardwareBuffer* buffer, int32_t* fence); | 
|  | 189 |  | 
|  | 190 | /* | 
|  | 191 | * Send the AHardwareBuffer to an AF_UNIX socket. | 
|  | 192 | * | 
|  | 193 | * Returns NO_ERROR on success, BAD_VALUE if the buffer is NULL, or an error | 
|  | 194 | * number of the lock fails for any reason. | 
|  | 195 | */ | 
|  | 196 | int AHardwareBuffer_sendHandleToUnixSocket(const AHardwareBuffer* buffer, | 
|  | 197 | int socketFd); | 
|  | 198 |  | 
|  | 199 | /* | 
|  | 200 | * Receive the AHardwareBuffer from an AF_UNIX socket. | 
|  | 201 | * | 
|  | 202 | * Returns NO_ERROR on success, BAD_VALUE if the buffer is NULL, or an error | 
|  | 203 | * number of the lock fails for any reason. | 
|  | 204 | */ | 
|  | 205 | int AHardwareBuffer_recvHandleFromUnixSocket(int socketFd, | 
|  | 206 | AHardwareBuffer** outBuffer); | 
|  | 207 |  | 
|  | 208 | // ---------------------------------------------------------------------------- | 
|  | 209 | // Everything below here is part of the public NDK API, but is intended only | 
|  | 210 | // for use by device-specific graphics drivers. | 
|  | 211 | struct native_handle; | 
|  | 212 | const struct native_handle* AHardwareBuffer_getNativeHandle( | 
|  | 213 | const AHardwareBuffer* buffer); | 
|  | 214 |  | 
|  | 215 | __END_DECLS | 
|  | 216 |  | 
|  | 217 | #endif // ANDROID_HARDWARE_BUFFER_H |