Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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 | #define LOG_TAG "AHardwareBuffer" |
| 18 | |
Jesse Hall | 1ee859f | 2017-04-17 08:52:32 -0700 | [diff] [blame] | 19 | #include <vndk/hardware_buffer.h> |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 20 | |
| 21 | #include <errno.h> |
| 22 | #include <sys/socket.h> |
| 23 | #include <memory> |
| 24 | |
| 25 | #include <cutils/native_handle.h> |
| 26 | #include <log/log.h> |
| 27 | #include <utils/StrongPointer.h> |
| 28 | #include <ui/GraphicBuffer.h> |
| 29 | #include <system/graphics.h> |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 30 | |
| 31 | #include <private/android/AHardwareBufferHelpers.h> |
Craig Donner | e6ecb92 | 2017-12-27 14:59:29 -0800 | [diff] [blame^] | 32 | #include <android/hardware/graphics/common/1.1/types.h> |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 33 | |
| 34 | |
Craig Donner | 1b1d300 | 2017-04-12 18:38:07 -0700 | [diff] [blame] | 35 | static constexpr int kFdBufferSize = 128 * sizeof(int); // 128 ints |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 36 | |
| 37 | using namespace android; |
| 38 | |
| 39 | // ---------------------------------------------------------------------------- |
| 40 | // Public functions |
| 41 | // ---------------------------------------------------------------------------- |
| 42 | |
| 43 | int AHardwareBuffer_allocate(const AHardwareBuffer_Desc* desc, AHardwareBuffer** outBuffer) { |
| 44 | if (!outBuffer || !desc) |
| 45 | return BAD_VALUE; |
| 46 | |
Mathias Agopian | 3a11413a | 2017-06-08 19:44:17 -0700 | [diff] [blame] | 47 | if (!AHardwareBuffer_isValidPixelFormat(desc->format)) { |
| 48 | ALOGE("Invalid AHardwareBuffer pixel format %u (%#x))", desc->format, desc->format); |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 49 | return BAD_VALUE; |
| 50 | } |
| 51 | |
Mathias Agopian | 3a11413a | 2017-06-08 19:44:17 -0700 | [diff] [blame] | 52 | int format = AHardwareBuffer_convertToPixelFormat(desc->format); |
Mathias Agopian | 07b920e | 2017-04-25 17:38:08 -0700 | [diff] [blame] | 53 | if (desc->rfu0 != 0 || desc->rfu1 != 0) { |
| 54 | ALOGE("AHardwareBuffer_Desc::rfu fields must be 0"); |
Mathias Agopian | 2c38b56 | 2017-04-20 16:35:39 -0700 | [diff] [blame] | 55 | return BAD_VALUE; |
| 56 | } |
| 57 | |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 58 | if (desc->format == AHARDWAREBUFFER_FORMAT_BLOB && desc->height != 1) { |
| 59 | ALOGE("Height must be 1 when using the AHARDWAREBUFFER_FORMAT_BLOB format"); |
| 60 | return BAD_VALUE; |
| 61 | } |
| 62 | |
Mathias Agopian | 2c38b56 | 2017-04-20 16:35:39 -0700 | [diff] [blame] | 63 | uint64_t usage = AHardwareBuffer_convertToGrallocUsageBits(desc->usage); |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 64 | sp<GraphicBuffer> gbuffer(new GraphicBuffer( |
Chris Forbes | d410630 | 2017-04-20 12:43:04 -0700 | [diff] [blame] | 65 | desc->width, desc->height, format, desc->layers, usage, |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 66 | std::string("AHardwareBuffer pid [") + std::to_string(getpid()) + "]")); |
| 67 | |
| 68 | status_t err = gbuffer->initCheck(); |
| 69 | if (err != 0 || gbuffer->handle == 0) { |
| 70 | if (err == NO_MEMORY) { |
| 71 | GraphicBuffer::dumpAllocationsToSystemLog(); |
| 72 | } |
Mathias Agopian | 0556d79 | 2017-03-22 15:49:32 -0700 | [diff] [blame] | 73 | ALOGE("GraphicBuffer(w=%u, h=%u, lc=%u) failed (%s), handle=%p", |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 74 | desc->width, desc->height, desc->layers, strerror(-err), gbuffer->handle); |
| 75 | return err; |
| 76 | } |
| 77 | |
| 78 | *outBuffer = AHardwareBuffer_from_GraphicBuffer(gbuffer.get()); |
| 79 | |
| 80 | // Ensure the buffer doesn't get destroyed when the sp<> goes away. |
| 81 | AHardwareBuffer_acquire(*outBuffer); |
| 82 | return NO_ERROR; |
| 83 | } |
| 84 | |
| 85 | void AHardwareBuffer_acquire(AHardwareBuffer* buffer) { |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame] | 86 | // incStrong/decStrong token must be the same, doesn't matter what it is |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 87 | AHardwareBuffer_to_GraphicBuffer(buffer)->incStrong((void*)AHardwareBuffer_acquire); |
| 88 | } |
| 89 | |
| 90 | void AHardwareBuffer_release(AHardwareBuffer* buffer) { |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame] | 91 | // incStrong/decStrong token must be the same, doesn't matter what it is |
| 92 | AHardwareBuffer_to_GraphicBuffer(buffer)->decStrong((void*)AHardwareBuffer_acquire); |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void AHardwareBuffer_describe(const AHardwareBuffer* buffer, |
| 96 | AHardwareBuffer_Desc* outDesc) { |
| 97 | if (!buffer || !outDesc) return; |
| 98 | |
| 99 | const GraphicBuffer* gbuffer = AHardwareBuffer_to_GraphicBuffer(buffer); |
| 100 | |
| 101 | outDesc->width = gbuffer->getWidth(); |
| 102 | outDesc->height = gbuffer->getHeight(); |
| 103 | outDesc->layers = gbuffer->getLayerCount(); |
Mathias Agopian | 07b920e | 2017-04-25 17:38:08 -0700 | [diff] [blame] | 104 | outDesc->format = AHardwareBuffer_convertFromPixelFormat(uint32_t(gbuffer->getPixelFormat())); |
Mathias Agopian | 2c38b56 | 2017-04-20 16:35:39 -0700 | [diff] [blame] | 105 | outDesc->usage = AHardwareBuffer_convertFromGrallocUsageBits(gbuffer->getUsage()); |
Mathias Agopian | 07b920e | 2017-04-25 17:38:08 -0700 | [diff] [blame] | 106 | outDesc->stride = gbuffer->getStride(); |
| 107 | outDesc->rfu0 = 0; |
| 108 | outDesc->rfu1 = 0; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Mathias Agopian | 2c38b56 | 2017-04-20 16:35:39 -0700 | [diff] [blame] | 111 | int AHardwareBuffer_lock(AHardwareBuffer* buffer, uint64_t usage, |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 112 | int32_t fence, const ARect* rect, void** outVirtualAddress) { |
| 113 | if (!buffer) return BAD_VALUE; |
| 114 | |
Mathias Agopian | 2c38b56 | 2017-04-20 16:35:39 -0700 | [diff] [blame] | 115 | if (usage & ~(AHARDWAREBUFFER_USAGE_CPU_READ_MASK | |
| 116 | AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK)) { |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 117 | ALOGE("Invalid usage flags passed to AHardwareBuffer_lock; only " |
Mathias Agopian | 2c38b56 | 2017-04-20 16:35:39 -0700 | [diff] [blame] | 118 | " AHARDWAREBUFFER_USAGE_CPU_* flags are allowed"); |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 119 | return BAD_VALUE; |
| 120 | } |
| 121 | |
Mathias Agopian | 2c38b56 | 2017-04-20 16:35:39 -0700 | [diff] [blame] | 122 | usage = AHardwareBuffer_convertToGrallocUsageBits(usage); |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 123 | GraphicBuffer* gBuffer = AHardwareBuffer_to_GraphicBuffer(buffer); |
| 124 | Rect bounds; |
| 125 | if (!rect) { |
| 126 | bounds.set(Rect(gBuffer->getWidth(), gBuffer->getHeight())); |
| 127 | } else { |
| 128 | bounds.set(Rect(rect->left, rect->top, rect->right, rect->bottom)); |
| 129 | } |
Mathias Agopian | 2c38b56 | 2017-04-20 16:35:39 -0700 | [diff] [blame] | 130 | return gBuffer->lockAsync(usage, usage, bounds, outVirtualAddress, fence); |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | int AHardwareBuffer_unlock(AHardwareBuffer* buffer, int32_t* fence) { |
| 134 | if (!buffer) return BAD_VALUE; |
| 135 | |
| 136 | GraphicBuffer* gBuffer = AHardwareBuffer_to_GraphicBuffer(buffer); |
Jiwen 'Steve' Cai | ae6955f | 2017-06-02 15:23:26 -0700 | [diff] [blame] | 137 | if (fence == nullptr) |
| 138 | return gBuffer->unlock(); |
| 139 | else |
| 140 | return gBuffer->unlockAsync(fence); |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame] | 143 | int AHardwareBuffer_sendHandleToUnixSocket(const AHardwareBuffer* buffer, int socketFd) { |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 144 | if (!buffer) return BAD_VALUE; |
| 145 | const GraphicBuffer* gBuffer = AHardwareBuffer_to_GraphicBuffer(buffer); |
| 146 | |
| 147 | size_t flattenedSize = gBuffer->getFlattenedSize(); |
| 148 | size_t fdCount = gBuffer->getFdCount(); |
| 149 | |
| 150 | std::unique_ptr<uint8_t[]> data(new uint8_t[flattenedSize]); |
| 151 | std::unique_ptr<int[]> fds(new int[fdCount]); |
| 152 | |
| 153 | // Make copies of needed items since flatten modifies them, and we don't |
| 154 | // want to send anything if there's an error during flatten. |
| 155 | size_t flattenedSizeCopy = flattenedSize; |
| 156 | size_t fdCountCopy = fdCount; |
| 157 | void* dataStart = data.get(); |
| 158 | int* fdsStart = fds.get(); |
| 159 | status_t err = gBuffer->flatten(dataStart, flattenedSizeCopy, fdsStart, |
| 160 | fdCountCopy); |
| 161 | if (err != NO_ERROR) { |
| 162 | return err; |
| 163 | } |
| 164 | |
| 165 | struct iovec iov[1]; |
| 166 | iov[0].iov_base = data.get(); |
| 167 | iov[0].iov_len = flattenedSize; |
| 168 | |
Craig Donner | 1b1d300 | 2017-04-12 18:38:07 -0700 | [diff] [blame] | 169 | char buf[CMSG_SPACE(kFdBufferSize)]; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 170 | struct msghdr msg = { |
| 171 | .msg_control = buf, |
| 172 | .msg_controllen = sizeof(buf), |
| 173 | .msg_iov = &iov[0], |
| 174 | .msg_iovlen = 1, |
| 175 | }; |
| 176 | |
| 177 | struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); |
| 178 | cmsg->cmsg_level = SOL_SOCKET; |
| 179 | cmsg->cmsg_type = SCM_RIGHTS; |
| 180 | cmsg->cmsg_len = CMSG_LEN(sizeof(int) * fdCount); |
| 181 | int* fdData = reinterpret_cast<int*>(CMSG_DATA(cmsg)); |
| 182 | memcpy(fdData, fds.get(), sizeof(int) * fdCount); |
| 183 | msg.msg_controllen = cmsg->cmsg_len; |
| 184 | |
Jesse Hall | 81d3197 | 2017-05-15 14:14:01 -0700 | [diff] [blame] | 185 | int result; |
| 186 | do { |
| 187 | result = sendmsg(socketFd, &msg, 0); |
| 188 | } while (result == -1 && errno == EINTR); |
| 189 | if (result == -1) { |
| 190 | result = errno; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 191 | ALOGE("Error writing AHardwareBuffer to socket: error %#x (%s)", |
Jesse Hall | 81d3197 | 2017-05-15 14:14:01 -0700 | [diff] [blame] | 192 | result, strerror(result)); |
| 193 | return -result; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 194 | } |
Jesse Hall | 81d3197 | 2017-05-15 14:14:01 -0700 | [diff] [blame] | 195 | |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 196 | return NO_ERROR; |
| 197 | } |
| 198 | |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame] | 199 | int AHardwareBuffer_recvHandleFromUnixSocket(int socketFd, AHardwareBuffer** outBuffer) { |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 200 | if (!outBuffer) return BAD_VALUE; |
| 201 | |
Craig Donner | 1b1d300 | 2017-04-12 18:38:07 -0700 | [diff] [blame] | 202 | static constexpr int kMessageBufferSize = 4096 * sizeof(int); |
| 203 | |
| 204 | std::unique_ptr<char[]> dataBuf(new char[kMessageBufferSize]); |
| 205 | char fdBuf[CMSG_SPACE(kFdBufferSize)]; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 206 | struct iovec iov[1]; |
Craig Donner | 1b1d300 | 2017-04-12 18:38:07 -0700 | [diff] [blame] | 207 | iov[0].iov_base = dataBuf.get(); |
| 208 | iov[0].iov_len = kMessageBufferSize; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 209 | |
| 210 | struct msghdr msg = { |
| 211 | .msg_control = fdBuf, |
| 212 | .msg_controllen = sizeof(fdBuf), |
| 213 | .msg_iov = &iov[0], |
| 214 | .msg_iovlen = 1, |
| 215 | }; |
| 216 | |
Jesse Hall | 81d3197 | 2017-05-15 14:14:01 -0700 | [diff] [blame] | 217 | int result; |
| 218 | do { |
| 219 | result = recvmsg(socketFd, &msg, 0); |
| 220 | } while (result == -1 && errno == EINTR); |
| 221 | if (result == -1) { |
| 222 | result = errno; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 223 | ALOGE("Error reading AHardwareBuffer from socket: error %#x (%s)", |
Jesse Hall | 81d3197 | 2017-05-15 14:14:01 -0700 | [diff] [blame] | 224 | result, strerror(result)); |
| 225 | return -result; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | if (msg.msg_iovlen != 1) { |
| 229 | ALOGE("Error reading AHardwareBuffer from socket: bad data length"); |
| 230 | return INVALID_OPERATION; |
| 231 | } |
| 232 | |
| 233 | if (msg.msg_controllen % sizeof(int) != 0) { |
| 234 | ALOGE("Error reading AHardwareBuffer from socket: bad fd length"); |
| 235 | return INVALID_OPERATION; |
| 236 | } |
| 237 | |
| 238 | size_t dataLen = msg.msg_iov[0].iov_len; |
| 239 | const void* data = static_cast<const void*>(msg.msg_iov[0].iov_base); |
| 240 | if (!data) { |
| 241 | ALOGE("Error reading AHardwareBuffer from socket: no buffer data"); |
| 242 | return INVALID_OPERATION; |
| 243 | } |
| 244 | |
| 245 | struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); |
| 246 | if (!cmsg) { |
| 247 | ALOGE("Error reading AHardwareBuffer from socket: no fd header"); |
| 248 | return INVALID_OPERATION; |
| 249 | } |
| 250 | |
| 251 | size_t fdCount = msg.msg_controllen >> 2; |
| 252 | const int* fdData = reinterpret_cast<const int*>(CMSG_DATA(cmsg)); |
| 253 | if (!fdData) { |
| 254 | ALOGE("Error reading AHardwareBuffer from socket: no fd data"); |
| 255 | return INVALID_OPERATION; |
| 256 | } |
| 257 | |
| 258 | GraphicBuffer* gBuffer = new GraphicBuffer(); |
| 259 | status_t err = gBuffer->unflatten(data, dataLen, fdData, fdCount); |
| 260 | if (err != NO_ERROR) { |
| 261 | return err; |
| 262 | } |
| 263 | *outBuffer = AHardwareBuffer_from_GraphicBuffer(gBuffer); |
| 264 | // Ensure the buffer has a positive ref-count. |
| 265 | AHardwareBuffer_acquire(*outBuffer); |
| 266 | |
| 267 | return NO_ERROR; |
| 268 | } |
| 269 | |
Jesse Hall | 1ee859f | 2017-04-17 08:52:32 -0700 | [diff] [blame] | 270 | |
| 271 | // ---------------------------------------------------------------------------- |
| 272 | // VNDK functions |
| 273 | // ---------------------------------------------------------------------------- |
| 274 | |
| 275 | const native_handle_t* AHardwareBuffer_getNativeHandle( |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 276 | const AHardwareBuffer* buffer) { |
| 277 | if (!buffer) return nullptr; |
| 278 | const GraphicBuffer* gbuffer = AHardwareBuffer_to_GraphicBuffer(buffer); |
| 279 | return gbuffer->handle; |
| 280 | } |
| 281 | |
| 282 | |
| 283 | // ---------------------------------------------------------------------------- |
| 284 | // Helpers implementation |
| 285 | // ---------------------------------------------------------------------------- |
| 286 | |
| 287 | namespace android { |
| 288 | |
Craig Donner | a5a719e | 2017-02-24 16:02:08 -0800 | [diff] [blame] | 289 | // A 1:1 mapping of AHardwaqreBuffer bitmasks to gralloc1 bitmasks. |
| 290 | struct UsageMaskMapping { |
| 291 | uint64_t hardwareBufferMask; |
| 292 | uint64_t grallocMask; |
| 293 | }; |
| 294 | |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 295 | static inline bool containsBits(uint64_t mask, uint64_t bitsToCheck) { |
Craig Donner | a5a719e | 2017-02-24 16:02:08 -0800 | [diff] [blame] | 296 | return (mask & bitsToCheck) == bitsToCheck && bitsToCheck; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Mathias Agopian | 3a11413a | 2017-06-08 19:44:17 -0700 | [diff] [blame] | 299 | bool AHardwareBuffer_isValidPixelFormat(uint32_t format) { |
| 300 | static_assert(HAL_PIXEL_FORMAT_RGBA_8888 == AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM, |
| 301 | "HAL and AHardwareBuffer pixel format don't match"); |
| 302 | static_assert(HAL_PIXEL_FORMAT_RGBX_8888 == AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM, |
| 303 | "HAL and AHardwareBuffer pixel format don't match"); |
| 304 | static_assert(HAL_PIXEL_FORMAT_RGB_565 == AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM, |
| 305 | "HAL and AHardwareBuffer pixel format don't match"); |
| 306 | static_assert(HAL_PIXEL_FORMAT_RGB_888 == AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM, |
| 307 | "HAL and AHardwareBuffer pixel format don't match"); |
| 308 | static_assert(HAL_PIXEL_FORMAT_RGBA_FP16 == AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT, |
| 309 | "HAL and AHardwareBuffer pixel format don't match"); |
| 310 | static_assert(HAL_PIXEL_FORMAT_RGBA_1010102 == AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM, |
| 311 | "HAL and AHardwareBuffer pixel format don't match"); |
| 312 | static_assert(HAL_PIXEL_FORMAT_BLOB == AHARDWAREBUFFER_FORMAT_BLOB, |
| 313 | "HAL and AHardwareBuffer pixel format don't match"); |
Craig Donner | e6ecb92 | 2017-12-27 14:59:29 -0800 | [diff] [blame^] | 314 | static_assert(HAL_PIXEL_FORMAT_DEPTH_16 == AHARDWAREBUFFER_FORMAT_D16_UNORM, |
| 315 | "HAL and AHardwareBuffer pixel format don't match"); |
| 316 | static_assert(HAL_PIXEL_FORMAT_DEPTH_24 == AHARDWAREBUFFER_FORMAT_D24_UNORM, |
| 317 | "HAL and AHardwareBuffer pixel format don't match"); |
| 318 | static_assert(HAL_PIXEL_FORMAT_DEPTH_24_STENCIL_8 == AHARDWAREBUFFER_FORMAT_D24_UNORM_S8_UINT, |
| 319 | "HAL and AHardwareBuffer pixel format don't match"); |
| 320 | static_assert(HAL_PIXEL_FORMAT_DEPTH_32F == AHARDWAREBUFFER_FORMAT_D32_FLOAT, |
| 321 | "HAL and AHardwareBuffer pixel format don't match"); |
| 322 | static_assert(HAL_PIXEL_FORMAT_DEPTH_32F_STENCIL_8 == AHARDWAREBUFFER_FORMAT_D32_FLOAT_S8_UINT, |
| 323 | "HAL and AHardwareBuffer pixel format don't match"); |
| 324 | static_assert(HAL_PIXEL_FORMAT_STENCIL_8 == AHARDWAREBUFFER_FORMAT_S8_UINT, |
| 325 | "HAL and AHardwareBuffer pixel format don't match"); |
Mathias Agopian | 3a11413a | 2017-06-08 19:44:17 -0700 | [diff] [blame] | 326 | static_assert(HAL_PIXEL_FORMAT_BGRA_8888 == AHARDWAREBUFFER_FORMAT_B8G8R8A8_UNORM, |
| 327 | "HAL and AHardwareBuffer pixel format don't match"); |
| 328 | static_assert(HAL_PIXEL_FORMAT_YV12 == AHARDWAREBUFFER_FORMAT_YV12, |
| 329 | "HAL and AHardwareBuffer pixel format don't match"); |
| 330 | static_assert(HAL_PIXEL_FORMAT_Y8 == AHARDWAREBUFFER_FORMAT_Y8, |
| 331 | "HAL and AHardwareBuffer pixel format don't match"); |
| 332 | static_assert(HAL_PIXEL_FORMAT_Y16 == AHARDWAREBUFFER_FORMAT_Y16, |
| 333 | "HAL and AHardwareBuffer pixel format don't match"); |
| 334 | static_assert(HAL_PIXEL_FORMAT_RAW16 == AHARDWAREBUFFER_FORMAT_RAW16, |
| 335 | "HAL and AHardwareBuffer pixel format don't match"); |
| 336 | static_assert(HAL_PIXEL_FORMAT_RAW10 == AHARDWAREBUFFER_FORMAT_RAW10, |
| 337 | "HAL and AHardwareBuffer pixel format don't match"); |
| 338 | static_assert(HAL_PIXEL_FORMAT_RAW12 == AHARDWAREBUFFER_FORMAT_RAW12, |
| 339 | "HAL and AHardwareBuffer pixel format don't match"); |
| 340 | static_assert(HAL_PIXEL_FORMAT_RAW_OPAQUE == AHARDWAREBUFFER_FORMAT_RAW_OPAQUE, |
| 341 | "HAL and AHardwareBuffer pixel format don't match"); |
| 342 | static_assert(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED == AHARDWAREBUFFER_FORMAT_IMPLEMENTATION_DEFINED, |
| 343 | "HAL and AHardwareBuffer pixel format don't match"); |
| 344 | static_assert(HAL_PIXEL_FORMAT_YCBCR_420_888 == AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420, |
| 345 | "HAL and AHardwareBuffer pixel format don't match"); |
| 346 | static_assert(HAL_PIXEL_FORMAT_YCBCR_422_888 == AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_422, |
| 347 | "HAL and AHardwareBuffer pixel format don't match"); |
| 348 | static_assert(HAL_PIXEL_FORMAT_YCBCR_444_888 == AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_444, |
| 349 | "HAL and AHardwareBuffer pixel format don't match"); |
| 350 | static_assert(HAL_PIXEL_FORMAT_FLEX_RGB_888 == AHARDWAREBUFFER_FORMAT_FLEX_R8G8B8, |
| 351 | "HAL and AHardwareBuffer pixel format don't match"); |
| 352 | static_assert(HAL_PIXEL_FORMAT_FLEX_RGBA_8888 == AHARDWAREBUFFER_FORMAT_FLEX_R8G8B8A8, |
| 353 | "HAL and AHardwareBuffer pixel format don't match"); |
| 354 | static_assert(HAL_PIXEL_FORMAT_YCBCR_422_SP == AHARDWAREBUFFER_FORMAT_YCbCr_422_SP, |
| 355 | "HAL and AHardwareBuffer pixel format don't match"); |
| 356 | static_assert(HAL_PIXEL_FORMAT_YCRCB_420_SP == AHARDWAREBUFFER_FORMAT_YCrCb_420_SP, |
| 357 | "HAL and AHardwareBuffer pixel format don't match"); |
| 358 | static_assert(HAL_PIXEL_FORMAT_YCBCR_422_I == AHARDWAREBUFFER_FORMAT_YCbCr_422_I, |
| 359 | "HAL and AHardwareBuffer pixel format don't match"); |
| 360 | |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 361 | switch (format) { |
Mathias Agopian | 3a11413a | 2017-06-08 19:44:17 -0700 | [diff] [blame] | 362 | case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM: |
| 363 | case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM: |
| 364 | case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM: |
| 365 | case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM: |
| 366 | case AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT: |
| 367 | case AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM: |
| 368 | case AHARDWAREBUFFER_FORMAT_BLOB: |
Craig Donner | e6ecb92 | 2017-12-27 14:59:29 -0800 | [diff] [blame^] | 369 | case AHARDWAREBUFFER_FORMAT_D16_UNORM: |
| 370 | case AHARDWAREBUFFER_FORMAT_D24_UNORM: |
| 371 | case AHARDWAREBUFFER_FORMAT_D24_UNORM_S8_UINT: |
| 372 | case AHARDWAREBUFFER_FORMAT_D32_FLOAT: |
| 373 | case AHARDWAREBUFFER_FORMAT_D32_FLOAT_S8_UINT: |
| 374 | case AHARDWAREBUFFER_FORMAT_S8_UINT: |
Mathias Agopian | 3a11413a | 2017-06-08 19:44:17 -0700 | [diff] [blame] | 375 | // VNDK formats only -- unfortunately we can't differentiate from where we're called |
| 376 | case AHARDWAREBUFFER_FORMAT_B8G8R8A8_UNORM: |
| 377 | case AHARDWAREBUFFER_FORMAT_YV12: |
| 378 | case AHARDWAREBUFFER_FORMAT_Y8: |
| 379 | case AHARDWAREBUFFER_FORMAT_Y16: |
| 380 | case AHARDWAREBUFFER_FORMAT_RAW16: |
| 381 | case AHARDWAREBUFFER_FORMAT_RAW10: |
| 382 | case AHARDWAREBUFFER_FORMAT_RAW12: |
| 383 | case AHARDWAREBUFFER_FORMAT_RAW_OPAQUE: |
| 384 | case AHARDWAREBUFFER_FORMAT_IMPLEMENTATION_DEFINED: |
| 385 | case AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420: |
| 386 | case AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_422: |
| 387 | case AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_444: |
| 388 | case AHARDWAREBUFFER_FORMAT_FLEX_R8G8B8: |
| 389 | case AHARDWAREBUFFER_FORMAT_FLEX_R8G8B8A8: |
| 390 | case AHARDWAREBUFFER_FORMAT_YCbCr_422_SP: |
| 391 | case AHARDWAREBUFFER_FORMAT_YCrCb_420_SP: |
| 392 | case AHARDWAREBUFFER_FORMAT_YCbCr_422_I: |
| 393 | return true; |
| 394 | |
| 395 | default: |
| 396 | return false; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | |
Mathias Agopian | 3a11413a | 2017-06-08 19:44:17 -0700 | [diff] [blame] | 400 | uint32_t AHardwareBuffer_convertFromPixelFormat(uint32_t hal_format) { |
| 401 | return hal_format; |
| 402 | } |
| 403 | |
| 404 | uint32_t AHardwareBuffer_convertToPixelFormat(uint32_t ahardwarebuffer_format) { |
| 405 | return ahardwarebuffer_format; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 406 | } |
| 407 | |
Mathias Agopian | 2c38b56 | 2017-04-20 16:35:39 -0700 | [diff] [blame] | 408 | uint64_t AHardwareBuffer_convertToGrallocUsageBits(uint64_t usage) { |
Craig Donner | e6ecb92 | 2017-12-27 14:59:29 -0800 | [diff] [blame^] | 409 | using android::hardware::graphics::common::V1_1::BufferUsage; |
Mathias Agopian | 2c38b56 | 2017-04-20 16:35:39 -0700 | [diff] [blame] | 410 | static_assert(AHARDWAREBUFFER_USAGE_CPU_READ_NEVER == (uint64_t)BufferUsage::CPU_READ_NEVER, |
| 411 | "gralloc and AHardwareBuffer flags don't match"); |
| 412 | static_assert(AHARDWAREBUFFER_USAGE_CPU_READ_RARELY == (uint64_t)BufferUsage::CPU_READ_RARELY, |
| 413 | "gralloc and AHardwareBuffer flags don't match"); |
| 414 | static_assert(AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN == (uint64_t)BufferUsage::CPU_READ_OFTEN, |
| 415 | "gralloc and AHardwareBuffer flags don't match"); |
| 416 | static_assert(AHARDWAREBUFFER_USAGE_CPU_WRITE_NEVER == (uint64_t)BufferUsage::CPU_WRITE_NEVER, |
| 417 | "gralloc and AHardwareBuffer flags don't match"); |
| 418 | static_assert(AHARDWAREBUFFER_USAGE_CPU_WRITE_RARELY == (uint64_t)BufferUsage::CPU_WRITE_RARELY, |
| 419 | "gralloc and AHardwareBuffer flags don't match"); |
| 420 | static_assert(AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN == (uint64_t)BufferUsage::CPU_WRITE_OFTEN, |
| 421 | "gralloc and AHardwareBuffer flags don't match"); |
| 422 | static_assert(AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE == (uint64_t)BufferUsage::GPU_TEXTURE, |
| 423 | "gralloc and AHardwareBuffer flags don't match"); |
| 424 | static_assert(AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT == (uint64_t)BufferUsage::GPU_RENDER_TARGET, |
| 425 | "gralloc and AHardwareBuffer flags don't match"); |
| 426 | static_assert(AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT == (uint64_t)BufferUsage::PROTECTED, |
| 427 | "gralloc and AHardwareBuffer flags don't match"); |
| 428 | static_assert(AHARDWAREBUFFER_USAGE_VIDEO_ENCODE == (uint64_t)BufferUsage::VIDEO_ENCODER, |
| 429 | "gralloc and AHardwareBuffer flags don't match"); |
| 430 | static_assert(AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER == (uint64_t)BufferUsage::GPU_DATA_BUFFER, |
| 431 | "gralloc and AHardwareBuffer flags don't match"); |
| 432 | static_assert(AHARDWAREBUFFER_USAGE_SENSOR_DIRECT_DATA == (uint64_t)BufferUsage::SENSOR_DIRECT_DATA, |
| 433 | "gralloc and AHardwareBuffer flags don't match"); |
Craig Donner | e6ecb92 | 2017-12-27 14:59:29 -0800 | [diff] [blame^] | 434 | static_assert(AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP == (uint64_t)BufferUsage::GPU_CUBE_MAP, |
| 435 | "gralloc and AHardwareBuffer flags don't match"); |
| 436 | static_assert(AHARDWAREBUFFER_USAGE_GPU_MIPMAP_COMPLETE == (uint64_t)BufferUsage::GPU_MIPMAP_COMPLETE, |
| 437 | "gralloc and AHardwareBuffer flags don't match"); |
Mathias Agopian | 2c38b56 | 2017-04-20 16:35:39 -0700 | [diff] [blame] | 438 | return usage; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 439 | } |
| 440 | |
Mathias Agopian | 2c38b56 | 2017-04-20 16:35:39 -0700 | [diff] [blame] | 441 | uint64_t AHardwareBuffer_convertFromGrallocUsageBits(uint64_t usage) { |
| 442 | return usage; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | const GraphicBuffer* AHardwareBuffer_to_GraphicBuffer(const AHardwareBuffer* buffer) { |
| 446 | return reinterpret_cast<const GraphicBuffer*>(buffer); |
| 447 | } |
| 448 | |
| 449 | GraphicBuffer* AHardwareBuffer_to_GraphicBuffer(AHardwareBuffer* buffer) { |
| 450 | return reinterpret_cast<GraphicBuffer*>(buffer); |
| 451 | } |
| 452 | |
Mathias Agopian | 6196340 | 2017-02-24 16:38:15 -0800 | [diff] [blame] | 453 | const ANativeWindowBuffer* AHardwareBuffer_to_ANativeWindowBuffer(const AHardwareBuffer* buffer) { |
| 454 | return AHardwareBuffer_to_GraphicBuffer(buffer)->getNativeBuffer(); |
| 455 | } |
| 456 | |
| 457 | ANativeWindowBuffer* AHardwareBuffer_to_ANativeWindowBuffer(AHardwareBuffer* buffer) { |
| 458 | return AHardwareBuffer_to_GraphicBuffer(buffer)->getNativeBuffer(); |
| 459 | } |
| 460 | |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 461 | AHardwareBuffer* AHardwareBuffer_from_GraphicBuffer(GraphicBuffer* buffer) { |
| 462 | return reinterpret_cast<AHardwareBuffer*>(buffer); |
| 463 | } |
| 464 | |
| 465 | } // namespace android |