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) { |
Krzysztof Kosiński | a5c54bc | 2018-11-16 18:24:33 -0800 | [diff] [blame] | 44 | if (!outBuffer || !desc) return BAD_VALUE; |
| 45 | if (!AHardwareBuffer_isValidDescription(desc, /*log=*/true)) return BAD_VALUE; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 46 | |
Mathias Agopian | 3a11413a | 2017-06-08 19:44:17 -0700 | [diff] [blame] | 47 | int format = AHardwareBuffer_convertToPixelFormat(desc->format); |
Krzysztof Kosiński | a5c54bc | 2018-11-16 18:24:33 -0800 | [diff] [blame] | 48 | uint64_t usage = AHardwareBuffer_convertToGrallocUsageBits(desc->usage); |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 49 | sp<GraphicBuffer> gbuffer(new GraphicBuffer( |
Chris Forbes | d410630 | 2017-04-20 12:43:04 -0700 | [diff] [blame] | 50 | desc->width, desc->height, format, desc->layers, usage, |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 51 | std::string("AHardwareBuffer pid [") + std::to_string(getpid()) + "]")); |
| 52 | |
| 53 | status_t err = gbuffer->initCheck(); |
| 54 | if (err != 0 || gbuffer->handle == 0) { |
| 55 | if (err == NO_MEMORY) { |
| 56 | GraphicBuffer::dumpAllocationsToSystemLog(); |
| 57 | } |
Mathias Agopian | 0556d79 | 2017-03-22 15:49:32 -0700 | [diff] [blame] | 58 | ALOGE("GraphicBuffer(w=%u, h=%u, lc=%u) failed (%s), handle=%p", |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 59 | desc->width, desc->height, desc->layers, strerror(-err), gbuffer->handle); |
| 60 | return err; |
| 61 | } |
| 62 | |
| 63 | *outBuffer = AHardwareBuffer_from_GraphicBuffer(gbuffer.get()); |
| 64 | |
| 65 | // Ensure the buffer doesn't get destroyed when the sp<> goes away. |
| 66 | AHardwareBuffer_acquire(*outBuffer); |
| 67 | return NO_ERROR; |
| 68 | } |
| 69 | |
| 70 | void AHardwareBuffer_acquire(AHardwareBuffer* buffer) { |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame] | 71 | // 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] | 72 | AHardwareBuffer_to_GraphicBuffer(buffer)->incStrong((void*)AHardwareBuffer_acquire); |
| 73 | } |
| 74 | |
| 75 | void AHardwareBuffer_release(AHardwareBuffer* buffer) { |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame] | 76 | // incStrong/decStrong token must be the same, doesn't matter what it is |
| 77 | AHardwareBuffer_to_GraphicBuffer(buffer)->decStrong((void*)AHardwareBuffer_acquire); |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void AHardwareBuffer_describe(const AHardwareBuffer* buffer, |
| 81 | AHardwareBuffer_Desc* outDesc) { |
| 82 | if (!buffer || !outDesc) return; |
| 83 | |
| 84 | const GraphicBuffer* gbuffer = AHardwareBuffer_to_GraphicBuffer(buffer); |
| 85 | |
| 86 | outDesc->width = gbuffer->getWidth(); |
| 87 | outDesc->height = gbuffer->getHeight(); |
| 88 | outDesc->layers = gbuffer->getLayerCount(); |
Mathias Agopian | 07b920e | 2017-04-25 17:38:08 -0700 | [diff] [blame] | 89 | outDesc->format = AHardwareBuffer_convertFromPixelFormat(uint32_t(gbuffer->getPixelFormat())); |
Mathias Agopian | 2c38b56 | 2017-04-20 16:35:39 -0700 | [diff] [blame] | 90 | outDesc->usage = AHardwareBuffer_convertFromGrallocUsageBits(gbuffer->getUsage()); |
Mathias Agopian | 07b920e | 2017-04-25 17:38:08 -0700 | [diff] [blame] | 91 | outDesc->stride = gbuffer->getStride(); |
| 92 | outDesc->rfu0 = 0; |
| 93 | outDesc->rfu1 = 0; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Valerie Hau | d2f4daf | 2019-02-15 13:49:00 -0800 | [diff] [blame] | 96 | int AHardwareBuffer_lockAndGetInfo(AHardwareBuffer* buffer, uint64_t usage, |
| 97 | int32_t fence, const ARect* rect, void** outVirtualAddress, |
| 98 | int32_t* outBytesPerPixel, int32_t* outBytesPerStride) { |
Valerie Hau | 12d2d64 | 2019-03-22 08:47:00 -0700 | [diff] [blame] | 99 | if (outBytesPerPixel) *outBytesPerPixel = -1; |
| 100 | if (outBytesPerStride) *outBytesPerStride = -1; |
| 101 | |
| 102 | if (!buffer) { |
| 103 | return BAD_VALUE; |
| 104 | } |
Valerie Hau | d2f4daf | 2019-02-15 13:49:00 -0800 | [diff] [blame] | 105 | |
| 106 | if (usage & ~(AHARDWAREBUFFER_USAGE_CPU_READ_MASK | |
| 107 | AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK)) { |
| 108 | ALOGE("Invalid usage flags passed to AHardwareBuffer_lock; only " |
| 109 | "AHARDWAREBUFFER_USAGE_CPU_* flags are allowed"); |
| 110 | return BAD_VALUE; |
| 111 | } |
| 112 | |
| 113 | usage = AHardwareBuffer_convertToGrallocUsageBits(usage); |
| 114 | GraphicBuffer* gbuffer = AHardwareBuffer_to_GraphicBuffer(buffer); |
| 115 | |
| 116 | //Mapper implementations before 3.0 will not return bytes per pixel or |
| 117 | //bytes per stride information. |
| 118 | if (gbuffer->getBufferMapperVersion() == GraphicBufferMapper::Version::GRALLOC_2) { |
| 119 | ALOGE("Mapper versions before 3.0 cannot retrieve bytes per pixel and bytes per stride info"); |
| 120 | return INVALID_OPERATION; |
| 121 | } |
| 122 | |
| 123 | if (gbuffer->getLayerCount() > 1) { |
| 124 | ALOGE("Buffer with multiple layers passed to AHardwareBuffer_lock; " |
| 125 | "only buffers with one layer are allowed"); |
| 126 | return INVALID_OPERATION; |
| 127 | } |
| 128 | |
| 129 | Rect bounds; |
| 130 | if (!rect) { |
| 131 | bounds.set(Rect(gbuffer->getWidth(), gbuffer->getHeight())); |
| 132 | } else { |
| 133 | bounds.set(Rect(rect->left, rect->top, rect->right, rect->bottom)); |
| 134 | } |
Valerie Hau | 12d2d64 | 2019-03-22 08:47:00 -0700 | [diff] [blame] | 135 | int32_t bytesPerPixel; |
| 136 | int32_t bytesPerStride; |
| 137 | int result = gbuffer->lockAsync(usage, usage, bounds, outVirtualAddress, fence, &bytesPerPixel, &bytesPerStride); |
Valerie Hau | d2f4daf | 2019-02-15 13:49:00 -0800 | [diff] [blame] | 138 | |
| 139 | // if hardware returns -1 for bytes per pixel or bytes per stride, we fail |
| 140 | // and unlock the buffer |
Valerie Hau | 12d2d64 | 2019-03-22 08:47:00 -0700 | [diff] [blame] | 141 | if (bytesPerPixel == -1 || bytesPerStride == -1) { |
Valerie Hau | d2f4daf | 2019-02-15 13:49:00 -0800 | [diff] [blame] | 142 | gbuffer->unlock(); |
| 143 | return INVALID_OPERATION; |
| 144 | } |
| 145 | |
Valerie Hau | 12d2d64 | 2019-03-22 08:47:00 -0700 | [diff] [blame] | 146 | if (outBytesPerPixel) *outBytesPerPixel = bytesPerPixel; |
| 147 | if (outBytesPerStride) *outBytesPerStride = bytesPerStride; |
Valerie Hau | d2f4daf | 2019-02-15 13:49:00 -0800 | [diff] [blame] | 148 | return result; |
| 149 | } |
| 150 | |
Mathias Agopian | 2c38b56 | 2017-04-20 16:35:39 -0700 | [diff] [blame] | 151 | int AHardwareBuffer_lock(AHardwareBuffer* buffer, uint64_t usage, |
Valerie Hau | d2f4daf | 2019-02-15 13:49:00 -0800 | [diff] [blame] | 152 | int32_t fence, const ARect* rect, void** outVirtualAddress) { |
| 153 | int32_t bytesPerPixel; |
| 154 | int32_t bytesPerStride; |
| 155 | |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 156 | if (!buffer) return BAD_VALUE; |
| 157 | |
Mathias Agopian | 2c38b56 | 2017-04-20 16:35:39 -0700 | [diff] [blame] | 158 | if (usage & ~(AHARDWAREBUFFER_USAGE_CPU_READ_MASK | |
| 159 | AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK)) { |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 160 | ALOGE("Invalid usage flags passed to AHardwareBuffer_lock; only " |
Krzysztof Kosiński | a5c54bc | 2018-11-16 18:24:33 -0800 | [diff] [blame] | 161 | "AHARDWAREBUFFER_USAGE_CPU_* flags are allowed"); |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 162 | return BAD_VALUE; |
| 163 | } |
| 164 | |
Mathias Agopian | 2c38b56 | 2017-04-20 16:35:39 -0700 | [diff] [blame] | 165 | usage = AHardwareBuffer_convertToGrallocUsageBits(usage); |
Krzysztof Kosiński | a5c54bc | 2018-11-16 18:24:33 -0800 | [diff] [blame] | 166 | GraphicBuffer* gbuffer = AHardwareBuffer_to_GraphicBuffer(buffer); |
| 167 | |
| 168 | if (gbuffer->getLayerCount() > 1) { |
| 169 | ALOGE("Buffer with multiple layers passed to AHardwareBuffer_lock; " |
| 170 | "only buffers with one layer are allowed"); |
| 171 | return INVALID_OPERATION; |
| 172 | } |
| 173 | |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 174 | Rect bounds; |
| 175 | if (!rect) { |
Krzysztof Kosiński | a5c54bc | 2018-11-16 18:24:33 -0800 | [diff] [blame] | 176 | bounds.set(Rect(gbuffer->getWidth(), gbuffer->getHeight())); |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 177 | } else { |
| 178 | bounds.set(Rect(rect->left, rect->top, rect->right, rect->bottom)); |
| 179 | } |
Valerie Hau | d2f4daf | 2019-02-15 13:49:00 -0800 | [diff] [blame] | 180 | return gbuffer->lockAsync(usage, usage, bounds, outVirtualAddress, fence, &bytesPerPixel, &bytesPerStride); |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 181 | } |
| 182 | |
renn | 4e9e48d | 2018-10-11 14:43:13 -0700 | [diff] [blame] | 183 | int AHardwareBuffer_lockPlanes(AHardwareBuffer* buffer, uint64_t usage, |
| 184 | int32_t fence, const ARect* rect, AHardwareBuffer_Planes* outPlanes) { |
| 185 | if (!buffer || !outPlanes) return BAD_VALUE; |
| 186 | |
| 187 | if (usage & ~(AHARDWAREBUFFER_USAGE_CPU_READ_MASK | |
| 188 | AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK)) { |
| 189 | ALOGE("Invalid usage flags passed to AHardwareBuffer_lock; only " |
| 190 | " AHARDWAREBUFFER_USAGE_CPU_* flags are allowed"); |
| 191 | return BAD_VALUE; |
| 192 | } |
| 193 | |
| 194 | usage = AHardwareBuffer_convertToGrallocUsageBits(usage); |
| 195 | GraphicBuffer* gBuffer = AHardwareBuffer_to_GraphicBuffer(buffer); |
| 196 | Rect bounds; |
| 197 | if (!rect) { |
| 198 | bounds.set(Rect(gBuffer->getWidth(), gBuffer->getHeight())); |
| 199 | } else { |
| 200 | bounds.set(Rect(rect->left, rect->top, rect->right, rect->bottom)); |
| 201 | } |
| 202 | int format = AHardwareBuffer_convertFromPixelFormat(uint32_t(gBuffer->getPixelFormat())); |
| 203 | memset(outPlanes->planes, 0, sizeof(outPlanes->planes)); |
| 204 | if (AHardwareBuffer_formatIsYuv(format)) { |
| 205 | android_ycbcr yuvData; |
| 206 | int result = gBuffer->lockAsyncYCbCr(usage, bounds, &yuvData, fence); |
| 207 | if (result == 0) { |
| 208 | outPlanes->planeCount = 3; |
| 209 | outPlanes->planes[0].data = yuvData.y; |
| 210 | outPlanes->planes[0].pixelStride = 1; |
| 211 | outPlanes->planes[0].rowStride = yuvData.ystride; |
| 212 | outPlanes->planes[1].data = yuvData.cb; |
| 213 | outPlanes->planes[1].pixelStride = yuvData.chroma_step; |
| 214 | outPlanes->planes[1].rowStride = yuvData.cstride; |
| 215 | outPlanes->planes[2].data = yuvData.cr; |
| 216 | outPlanes->planes[2].pixelStride = yuvData.chroma_step; |
| 217 | outPlanes->planes[2].rowStride = yuvData.cstride; |
| 218 | } else { |
| 219 | outPlanes->planeCount = 0; |
| 220 | } |
| 221 | return result; |
| 222 | } else { |
| 223 | const uint32_t pixelStride = AHardwareBuffer_bytesPerPixel(format); |
| 224 | outPlanes->planeCount = 1; |
| 225 | outPlanes->planes[0].pixelStride = pixelStride; |
| 226 | outPlanes->planes[0].rowStride = gBuffer->getStride() * pixelStride; |
| 227 | return gBuffer->lockAsync(usage, usage, bounds, &outPlanes->planes[0].data, fence); |
| 228 | } |
| 229 | } |
| 230 | |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 231 | int AHardwareBuffer_unlock(AHardwareBuffer* buffer, int32_t* fence) { |
| 232 | if (!buffer) return BAD_VALUE; |
| 233 | |
| 234 | GraphicBuffer* gBuffer = AHardwareBuffer_to_GraphicBuffer(buffer); |
Jiwen 'Steve' Cai | ae6955f | 2017-06-02 15:23:26 -0700 | [diff] [blame] | 235 | if (fence == nullptr) |
| 236 | return gBuffer->unlock(); |
| 237 | else |
| 238 | return gBuffer->unlockAsync(fence); |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 239 | } |
| 240 | |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame] | 241 | int AHardwareBuffer_sendHandleToUnixSocket(const AHardwareBuffer* buffer, int socketFd) { |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 242 | if (!buffer) return BAD_VALUE; |
| 243 | const GraphicBuffer* gBuffer = AHardwareBuffer_to_GraphicBuffer(buffer); |
| 244 | |
| 245 | size_t flattenedSize = gBuffer->getFlattenedSize(); |
| 246 | size_t fdCount = gBuffer->getFdCount(); |
| 247 | |
| 248 | std::unique_ptr<uint8_t[]> data(new uint8_t[flattenedSize]); |
| 249 | std::unique_ptr<int[]> fds(new int[fdCount]); |
| 250 | |
| 251 | // Make copies of needed items since flatten modifies them, and we don't |
| 252 | // want to send anything if there's an error during flatten. |
| 253 | size_t flattenedSizeCopy = flattenedSize; |
| 254 | size_t fdCountCopy = fdCount; |
| 255 | void* dataStart = data.get(); |
| 256 | int* fdsStart = fds.get(); |
| 257 | status_t err = gBuffer->flatten(dataStart, flattenedSizeCopy, fdsStart, |
| 258 | fdCountCopy); |
| 259 | if (err != NO_ERROR) { |
| 260 | return err; |
| 261 | } |
| 262 | |
| 263 | struct iovec iov[1]; |
| 264 | iov[0].iov_base = data.get(); |
| 265 | iov[0].iov_len = flattenedSize; |
| 266 | |
Craig Donner | 1b1d300 | 2017-04-12 18:38:07 -0700 | [diff] [blame] | 267 | char buf[CMSG_SPACE(kFdBufferSize)]; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 268 | struct msghdr msg = { |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 269 | .msg_iov = &iov[0], |
| 270 | .msg_iovlen = 1, |
Nick Desaulniers | 57e41f5 | 2019-10-15 14:24:44 -0700 | [diff] [blame] | 271 | .msg_control = buf, |
| 272 | .msg_controllen = sizeof(buf), |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 273 | }; |
| 274 | |
| 275 | struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); |
| 276 | cmsg->cmsg_level = SOL_SOCKET; |
| 277 | cmsg->cmsg_type = SCM_RIGHTS; |
| 278 | cmsg->cmsg_len = CMSG_LEN(sizeof(int) * fdCount); |
| 279 | int* fdData = reinterpret_cast<int*>(CMSG_DATA(cmsg)); |
| 280 | memcpy(fdData, fds.get(), sizeof(int) * fdCount); |
| 281 | msg.msg_controllen = cmsg->cmsg_len; |
| 282 | |
Jesse Hall | 81d3197 | 2017-05-15 14:14:01 -0700 | [diff] [blame] | 283 | int result; |
| 284 | do { |
| 285 | result = sendmsg(socketFd, &msg, 0); |
| 286 | } while (result == -1 && errno == EINTR); |
| 287 | if (result == -1) { |
| 288 | result = errno; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 289 | ALOGE("Error writing AHardwareBuffer to socket: error %#x (%s)", |
Jesse Hall | 81d3197 | 2017-05-15 14:14:01 -0700 | [diff] [blame] | 290 | result, strerror(result)); |
| 291 | return -result; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 292 | } |
Jesse Hall | 81d3197 | 2017-05-15 14:14:01 -0700 | [diff] [blame] | 293 | |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 294 | return NO_ERROR; |
| 295 | } |
| 296 | |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame] | 297 | int AHardwareBuffer_recvHandleFromUnixSocket(int socketFd, AHardwareBuffer** outBuffer) { |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 298 | if (!outBuffer) return BAD_VALUE; |
| 299 | |
Craig Donner | 1b1d300 | 2017-04-12 18:38:07 -0700 | [diff] [blame] | 300 | static constexpr int kMessageBufferSize = 4096 * sizeof(int); |
| 301 | |
| 302 | std::unique_ptr<char[]> dataBuf(new char[kMessageBufferSize]); |
| 303 | char fdBuf[CMSG_SPACE(kFdBufferSize)]; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 304 | struct iovec iov[1]; |
Craig Donner | 1b1d300 | 2017-04-12 18:38:07 -0700 | [diff] [blame] | 305 | iov[0].iov_base = dataBuf.get(); |
| 306 | iov[0].iov_len = kMessageBufferSize; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 307 | |
| 308 | struct msghdr msg = { |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 309 | .msg_iov = &iov[0], |
| 310 | .msg_iovlen = 1, |
Nick Desaulniers | 57e41f5 | 2019-10-15 14:24:44 -0700 | [diff] [blame] | 311 | .msg_control = fdBuf, |
| 312 | .msg_controllen = sizeof(fdBuf), |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 313 | }; |
| 314 | |
Jesse Hall | 81d3197 | 2017-05-15 14:14:01 -0700 | [diff] [blame] | 315 | int result; |
| 316 | do { |
| 317 | result = recvmsg(socketFd, &msg, 0); |
| 318 | } while (result == -1 && errno == EINTR); |
| 319 | if (result == -1) { |
| 320 | result = errno; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 321 | ALOGE("Error reading AHardwareBuffer from socket: error %#x (%s)", |
Jesse Hall | 81d3197 | 2017-05-15 14:14:01 -0700 | [diff] [blame] | 322 | result, strerror(result)); |
| 323 | return -result; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | if (msg.msg_iovlen != 1) { |
| 327 | ALOGE("Error reading AHardwareBuffer from socket: bad data length"); |
| 328 | return INVALID_OPERATION; |
| 329 | } |
| 330 | |
| 331 | if (msg.msg_controllen % sizeof(int) != 0) { |
| 332 | ALOGE("Error reading AHardwareBuffer from socket: bad fd length"); |
| 333 | return INVALID_OPERATION; |
| 334 | } |
| 335 | |
| 336 | size_t dataLen = msg.msg_iov[0].iov_len; |
| 337 | const void* data = static_cast<const void*>(msg.msg_iov[0].iov_base); |
| 338 | if (!data) { |
| 339 | ALOGE("Error reading AHardwareBuffer from socket: no buffer data"); |
| 340 | return INVALID_OPERATION; |
| 341 | } |
| 342 | |
| 343 | struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); |
| 344 | if (!cmsg) { |
| 345 | ALOGE("Error reading AHardwareBuffer from socket: no fd header"); |
| 346 | return INVALID_OPERATION; |
| 347 | } |
| 348 | |
| 349 | size_t fdCount = msg.msg_controllen >> 2; |
| 350 | const int* fdData = reinterpret_cast<const int*>(CMSG_DATA(cmsg)); |
| 351 | if (!fdData) { |
| 352 | ALOGE("Error reading AHardwareBuffer from socket: no fd data"); |
| 353 | return INVALID_OPERATION; |
| 354 | } |
| 355 | |
| 356 | GraphicBuffer* gBuffer = new GraphicBuffer(); |
| 357 | status_t err = gBuffer->unflatten(data, dataLen, fdData, fdCount); |
| 358 | if (err != NO_ERROR) { |
| 359 | return err; |
| 360 | } |
| 361 | *outBuffer = AHardwareBuffer_from_GraphicBuffer(gBuffer); |
| 362 | // Ensure the buffer has a positive ref-count. |
| 363 | AHardwareBuffer_acquire(*outBuffer); |
| 364 | |
| 365 | return NO_ERROR; |
| 366 | } |
| 367 | |
Krzysztof Kosiński | a5c54bc | 2018-11-16 18:24:33 -0800 | [diff] [blame] | 368 | int AHardwareBuffer_isSupported(const AHardwareBuffer_Desc* desc) { |
| 369 | if (!desc) return 0; |
| 370 | if (!AHardwareBuffer_isValidDescription(desc, /*log=*/false)) return 0; |
| 371 | |
Valerie Hau | a2414f0 | 2019-02-07 13:42:43 -0800 | [diff] [blame] | 372 | bool supported = false; |
| 373 | GraphicBuffer* gBuffer = new GraphicBuffer(); |
| 374 | status_t err = gBuffer->isSupported(desc->width, desc->height, desc->format, desc->layers, |
| 375 | desc->usage, &supported); |
| 376 | |
| 377 | if (err == NO_ERROR) { |
| 378 | return supported; |
| 379 | } |
| 380 | |
| 381 | // function isSupported is not implemented on device or an error occurred during HAL |
| 382 | // query. Make a trial allocation. |
Krzysztof Kosiński | a5c54bc | 2018-11-16 18:24:33 -0800 | [diff] [blame] | 383 | AHardwareBuffer_Desc trialDesc = *desc; |
| 384 | trialDesc.width = 4; |
| 385 | trialDesc.height = desc->format == AHARDWAREBUFFER_FORMAT_BLOB ? 1 : 4; |
Krzysztof Kosiński | c4b9ce0 | 2019-01-02 16:27:05 -0800 | [diff] [blame] | 386 | if (desc->usage & AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP) { |
| 387 | trialDesc.layers = desc->layers == 6 ? 6 : 12; |
| 388 | } else { |
| 389 | trialDesc.layers = desc->layers == 1 ? 1 : 2; |
| 390 | } |
Krzysztof Kosiński | a5c54bc | 2018-11-16 18:24:33 -0800 | [diff] [blame] | 391 | AHardwareBuffer* trialBuffer = nullptr; |
| 392 | int result = AHardwareBuffer_allocate(&trialDesc, &trialBuffer); |
| 393 | if (result == NO_ERROR) { |
| 394 | AHardwareBuffer_release(trialBuffer); |
| 395 | return 1; |
| 396 | } |
| 397 | return 0; |
| 398 | } |
| 399 | |
Jesse Hall | 1ee859f | 2017-04-17 08:52:32 -0700 | [diff] [blame] | 400 | |
| 401 | // ---------------------------------------------------------------------------- |
| 402 | // VNDK functions |
| 403 | // ---------------------------------------------------------------------------- |
| 404 | |
| 405 | const native_handle_t* AHardwareBuffer_getNativeHandle( |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 406 | const AHardwareBuffer* buffer) { |
| 407 | if (!buffer) return nullptr; |
| 408 | const GraphicBuffer* gbuffer = AHardwareBuffer_to_GraphicBuffer(buffer); |
| 409 | return gbuffer->handle; |
| 410 | } |
| 411 | |
Jesse Hall | fbe5835 | 2018-04-14 11:20:59 -0700 | [diff] [blame] | 412 | int AHardwareBuffer_createFromHandle(const AHardwareBuffer_Desc* desc, |
| 413 | const native_handle_t* handle, int32_t method, |
| 414 | AHardwareBuffer** outBuffer) { |
| 415 | static_assert(static_cast<int32_t>(AHARDWAREBUFFER_CREATE_FROM_HANDLE_METHOD_REGISTER) == |
| 416 | static_cast<int32_t>(GraphicBuffer::TAKE_UNREGISTERED_HANDLE)); |
| 417 | static_assert(static_cast<int32_t>(AHARDWAREBUFFER_CREATE_FROM_HANDLE_METHOD_CLONE) == |
| 418 | static_cast<int32_t>(GraphicBuffer::CLONE_HANDLE)); |
| 419 | |
| 420 | if (!desc || !handle || !outBuffer) return BAD_VALUE; |
| 421 | if (!(method == AHARDWAREBUFFER_CREATE_FROM_HANDLE_METHOD_REGISTER || |
| 422 | method == AHARDWAREBUFFER_CREATE_FROM_HANDLE_METHOD_CLONE)) |
| 423 | return BAD_VALUE; |
| 424 | if (desc->rfu0 != 0 || desc->rfu1 != 0) return BAD_VALUE; |
| 425 | if (desc->format == AHARDWAREBUFFER_FORMAT_BLOB && desc->height != 1) return BAD_VALUE; |
| 426 | |
| 427 | const int format = AHardwareBuffer_convertToPixelFormat(desc->format); |
| 428 | const uint64_t usage = AHardwareBuffer_convertToGrallocUsageBits(desc->usage); |
| 429 | const auto wrapMethod = static_cast<GraphicBuffer::HandleWrapMethod>(method); |
| 430 | sp<GraphicBuffer> gbuffer(new GraphicBuffer(handle, wrapMethod, desc->width, desc->height, |
| 431 | format, desc->layers, usage, desc->stride)); |
| 432 | status_t err = gbuffer->initCheck(); |
| 433 | if (err != 0 || gbuffer->handle == 0) return err; |
| 434 | |
| 435 | *outBuffer = AHardwareBuffer_from_GraphicBuffer(gbuffer.get()); |
| 436 | // Ensure the buffer doesn't get destroyed when the sp<> goes away. |
| 437 | AHardwareBuffer_acquire(*outBuffer); |
| 438 | |
| 439 | return NO_ERROR; |
| 440 | } |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 441 | |
| 442 | // ---------------------------------------------------------------------------- |
| 443 | // Helpers implementation |
| 444 | // ---------------------------------------------------------------------------- |
| 445 | |
| 446 | namespace android { |
| 447 | |
Krzysztof Kosiński | a5c54bc | 2018-11-16 18:24:33 -0800 | [diff] [blame] | 448 | bool AHardwareBuffer_isValidDescription(const AHardwareBuffer_Desc* desc, bool log) { |
| 449 | if (desc->width == 0 || desc->height == 0 || desc->layers == 0) { |
| 450 | ALOGE_IF(log, "Width, height and layers must all be nonzero"); |
| 451 | return false; |
| 452 | } |
Craig Donner | a5a719e | 2017-02-24 16:02:08 -0800 | [diff] [blame] | 453 | |
Krzysztof Kosiński | a5c54bc | 2018-11-16 18:24:33 -0800 | [diff] [blame] | 454 | if (!AHardwareBuffer_isValidPixelFormat(desc->format)) { |
| 455 | ALOGE_IF(log, "Invalid AHardwareBuffer pixel format %u (%#x))", |
| 456 | desc->format, desc->format); |
| 457 | return false; |
| 458 | } |
| 459 | |
| 460 | if (desc->rfu0 != 0 || desc->rfu1 != 0) { |
| 461 | ALOGE_IF(log, "AHardwareBuffer_Desc::rfu fields must be 0"); |
| 462 | return false; |
| 463 | } |
| 464 | |
| 465 | if (desc->format == AHARDWAREBUFFER_FORMAT_BLOB) { |
| 466 | if (desc->height != 1 || desc->layers != 1) { |
| 467 | ALOGE_IF(log, "Height and layers must be 1 for AHARDWAREBUFFER_FORMAT_BLOB"); |
| 468 | return false; |
| 469 | } |
| 470 | const uint64_t blobInvalidGpuMask = |
| 471 | AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE | |
| 472 | AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER | |
| 473 | AHARDWAREBUFFER_USAGE_GPU_MIPMAP_COMPLETE | |
| 474 | AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP; |
| 475 | if (desc->usage & blobInvalidGpuMask) { |
| 476 | ALOGE_IF(log, "Invalid GPU usage flag for AHARDWAREBUFFER_FORMAT_BLOB; " |
| 477 | "only AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER is allowed"); |
| 478 | return false; |
| 479 | } |
| 480 | if (desc->usage & AHARDWAREBUFFER_USAGE_VIDEO_ENCODE) { |
| 481 | ALOGE_IF(log, "AHARDWAREBUFFER_FORMAT_BLOB cannot be encoded as video"); |
| 482 | return false; |
| 483 | } |
renn | 4e9e48d | 2018-10-11 14:43:13 -0700 | [diff] [blame] | 484 | } else if (AHardwareBuffer_formatIsYuv(desc->format)) { |
| 485 | if (desc->layers != 1) { |
| 486 | ALOGE_IF(log, "Layers must be 1 for YUV formats."); |
| 487 | return false; |
| 488 | } |
| 489 | const uint64_t yuvInvalidGpuMask = |
| 490 | AHARDWAREBUFFER_USAGE_GPU_MIPMAP_COMPLETE | |
| 491 | AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP; |
| 492 | if (desc->usage & yuvInvalidGpuMask) { |
| 493 | ALOGE_IF(log, "Invalid usage flags specified for YUV format; " |
| 494 | "mip-mapping and cube-mapping are not allowed."); |
| 495 | return false; |
| 496 | } |
Krzysztof Kosiński | a5c54bc | 2018-11-16 18:24:33 -0800 | [diff] [blame] | 497 | } else { |
| 498 | if (desc->usage & AHARDWAREBUFFER_USAGE_SENSOR_DIRECT_DATA) { |
| 499 | ALOGE_IF(log, "AHARDWAREBUFFER_USAGE_SENSOR_DIRECT_DATA requires AHARDWAREBUFFER_FORMAT_BLOB"); |
| 500 | return false; |
| 501 | } |
| 502 | if (desc->usage & AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER) { |
| 503 | ALOGE_IF(log, "AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER requires AHARDWAREBUFFER_FORMAT_BLOB"); |
| 504 | return false; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | if ((desc->usage & (AHARDWAREBUFFER_USAGE_CPU_READ_MASK | AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK)) && |
| 509 | (desc->usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT)) { |
| 510 | ALOGE_IF(log, "AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT requires AHARDWAREBUFFER_USAGE_CPU_READ_NEVER " |
| 511 | "and AHARDWAREBUFFER_USAGE_CPU_WRITE_NEVER"); |
| 512 | return false; |
| 513 | } |
| 514 | |
| 515 | if (desc->usage & AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP) { |
| 516 | if (desc->width != desc->height) { |
| 517 | ALOGE_IF(log, "Cube maps must be square"); |
| 518 | return false; |
| 519 | } |
| 520 | if (desc->layers % 6 != 0) { |
| 521 | ALOGE_IF(log, "Cube map layers must be a multiple of 6"); |
| 522 | return false; |
| 523 | } |
| 524 | } |
| 525 | return true; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 526 | } |
| 527 | |
Mathias Agopian | 3a11413a | 2017-06-08 19:44:17 -0700 | [diff] [blame] | 528 | bool AHardwareBuffer_isValidPixelFormat(uint32_t format) { |
| 529 | static_assert(HAL_PIXEL_FORMAT_RGBA_8888 == AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM, |
| 530 | "HAL and AHardwareBuffer pixel format don't match"); |
| 531 | static_assert(HAL_PIXEL_FORMAT_RGBX_8888 == AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM, |
| 532 | "HAL and AHardwareBuffer pixel format don't match"); |
| 533 | static_assert(HAL_PIXEL_FORMAT_RGB_565 == AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM, |
| 534 | "HAL and AHardwareBuffer pixel format don't match"); |
| 535 | static_assert(HAL_PIXEL_FORMAT_RGB_888 == AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM, |
| 536 | "HAL and AHardwareBuffer pixel format don't match"); |
| 537 | static_assert(HAL_PIXEL_FORMAT_RGBA_FP16 == AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT, |
| 538 | "HAL and AHardwareBuffer pixel format don't match"); |
| 539 | static_assert(HAL_PIXEL_FORMAT_RGBA_1010102 == AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM, |
| 540 | "HAL and AHardwareBuffer pixel format don't match"); |
| 541 | static_assert(HAL_PIXEL_FORMAT_BLOB == AHARDWAREBUFFER_FORMAT_BLOB, |
| 542 | "HAL and AHardwareBuffer pixel format don't match"); |
Craig Donner | e6ecb92 | 2017-12-27 14:59:29 -0800 | [diff] [blame] | 543 | static_assert(HAL_PIXEL_FORMAT_DEPTH_16 == AHARDWAREBUFFER_FORMAT_D16_UNORM, |
| 544 | "HAL and AHardwareBuffer pixel format don't match"); |
| 545 | static_assert(HAL_PIXEL_FORMAT_DEPTH_24 == AHARDWAREBUFFER_FORMAT_D24_UNORM, |
| 546 | "HAL and AHardwareBuffer pixel format don't match"); |
| 547 | static_assert(HAL_PIXEL_FORMAT_DEPTH_24_STENCIL_8 == AHARDWAREBUFFER_FORMAT_D24_UNORM_S8_UINT, |
| 548 | "HAL and AHardwareBuffer pixel format don't match"); |
| 549 | static_assert(HAL_PIXEL_FORMAT_DEPTH_32F == AHARDWAREBUFFER_FORMAT_D32_FLOAT, |
| 550 | "HAL and AHardwareBuffer pixel format don't match"); |
| 551 | static_assert(HAL_PIXEL_FORMAT_DEPTH_32F_STENCIL_8 == AHARDWAREBUFFER_FORMAT_D32_FLOAT_S8_UINT, |
| 552 | "HAL and AHardwareBuffer pixel format don't match"); |
| 553 | static_assert(HAL_PIXEL_FORMAT_STENCIL_8 == AHARDWAREBUFFER_FORMAT_S8_UINT, |
| 554 | "HAL and AHardwareBuffer pixel format don't match"); |
Mathias Agopian | 3a11413a | 2017-06-08 19:44:17 -0700 | [diff] [blame] | 555 | static_assert(HAL_PIXEL_FORMAT_BGRA_8888 == AHARDWAREBUFFER_FORMAT_B8G8R8A8_UNORM, |
| 556 | "HAL and AHardwareBuffer pixel format don't match"); |
| 557 | static_assert(HAL_PIXEL_FORMAT_YV12 == AHARDWAREBUFFER_FORMAT_YV12, |
| 558 | "HAL and AHardwareBuffer pixel format don't match"); |
| 559 | static_assert(HAL_PIXEL_FORMAT_Y8 == AHARDWAREBUFFER_FORMAT_Y8, |
| 560 | "HAL and AHardwareBuffer pixel format don't match"); |
| 561 | static_assert(HAL_PIXEL_FORMAT_Y16 == AHARDWAREBUFFER_FORMAT_Y16, |
| 562 | "HAL and AHardwareBuffer pixel format don't match"); |
| 563 | static_assert(HAL_PIXEL_FORMAT_RAW16 == AHARDWAREBUFFER_FORMAT_RAW16, |
| 564 | "HAL and AHardwareBuffer pixel format don't match"); |
| 565 | static_assert(HAL_PIXEL_FORMAT_RAW10 == AHARDWAREBUFFER_FORMAT_RAW10, |
| 566 | "HAL and AHardwareBuffer pixel format don't match"); |
| 567 | static_assert(HAL_PIXEL_FORMAT_RAW12 == AHARDWAREBUFFER_FORMAT_RAW12, |
| 568 | "HAL and AHardwareBuffer pixel format don't match"); |
| 569 | static_assert(HAL_PIXEL_FORMAT_RAW_OPAQUE == AHARDWAREBUFFER_FORMAT_RAW_OPAQUE, |
| 570 | "HAL and AHardwareBuffer pixel format don't match"); |
| 571 | static_assert(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED == AHARDWAREBUFFER_FORMAT_IMPLEMENTATION_DEFINED, |
| 572 | "HAL and AHardwareBuffer pixel format don't match"); |
| 573 | static_assert(HAL_PIXEL_FORMAT_YCBCR_420_888 == AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420, |
| 574 | "HAL and AHardwareBuffer pixel format don't match"); |
Mathias Agopian | 3a11413a | 2017-06-08 19:44:17 -0700 | [diff] [blame] | 575 | static_assert(HAL_PIXEL_FORMAT_YCBCR_422_SP == AHARDWAREBUFFER_FORMAT_YCbCr_422_SP, |
| 576 | "HAL and AHardwareBuffer pixel format don't match"); |
| 577 | static_assert(HAL_PIXEL_FORMAT_YCRCB_420_SP == AHARDWAREBUFFER_FORMAT_YCrCb_420_SP, |
| 578 | "HAL and AHardwareBuffer pixel format don't match"); |
| 579 | static_assert(HAL_PIXEL_FORMAT_YCBCR_422_I == AHARDWAREBUFFER_FORMAT_YCbCr_422_I, |
| 580 | "HAL and AHardwareBuffer pixel format don't match"); |
| 581 | |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 582 | switch (format) { |
Mathias Agopian | 3a11413a | 2017-06-08 19:44:17 -0700 | [diff] [blame] | 583 | case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM: |
| 584 | case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM: |
| 585 | case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM: |
| 586 | case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM: |
| 587 | case AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT: |
| 588 | case AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM: |
| 589 | case AHARDWAREBUFFER_FORMAT_BLOB: |
Craig Donner | e6ecb92 | 2017-12-27 14:59:29 -0800 | [diff] [blame] | 590 | case AHARDWAREBUFFER_FORMAT_D16_UNORM: |
| 591 | case AHARDWAREBUFFER_FORMAT_D24_UNORM: |
| 592 | case AHARDWAREBUFFER_FORMAT_D24_UNORM_S8_UINT: |
| 593 | case AHARDWAREBUFFER_FORMAT_D32_FLOAT: |
| 594 | case AHARDWAREBUFFER_FORMAT_D32_FLOAT_S8_UINT: |
| 595 | case AHARDWAREBUFFER_FORMAT_S8_UINT: |
renn | 4e9e48d | 2018-10-11 14:43:13 -0700 | [diff] [blame] | 596 | case AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420: |
Mathias Agopian | 3a11413a | 2017-06-08 19:44:17 -0700 | [diff] [blame] | 597 | // VNDK formats only -- unfortunately we can't differentiate from where we're called |
| 598 | case AHARDWAREBUFFER_FORMAT_B8G8R8A8_UNORM: |
| 599 | case AHARDWAREBUFFER_FORMAT_YV12: |
| 600 | case AHARDWAREBUFFER_FORMAT_Y8: |
| 601 | case AHARDWAREBUFFER_FORMAT_Y16: |
| 602 | case AHARDWAREBUFFER_FORMAT_RAW16: |
| 603 | case AHARDWAREBUFFER_FORMAT_RAW10: |
| 604 | case AHARDWAREBUFFER_FORMAT_RAW12: |
| 605 | case AHARDWAREBUFFER_FORMAT_RAW_OPAQUE: |
| 606 | case AHARDWAREBUFFER_FORMAT_IMPLEMENTATION_DEFINED: |
Mathias Agopian | 3a11413a | 2017-06-08 19:44:17 -0700 | [diff] [blame] | 607 | case AHARDWAREBUFFER_FORMAT_YCbCr_422_SP: |
| 608 | case AHARDWAREBUFFER_FORMAT_YCrCb_420_SP: |
| 609 | case AHARDWAREBUFFER_FORMAT_YCbCr_422_I: |
| 610 | return true; |
| 611 | |
| 612 | default: |
| 613 | return false; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 614 | } |
| 615 | } |
| 616 | |
renn | 4e9e48d | 2018-10-11 14:43:13 -0700 | [diff] [blame] | 617 | bool AHardwareBuffer_formatIsYuv(uint32_t format) { |
| 618 | switch (format) { |
| 619 | case AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420: |
| 620 | case AHARDWAREBUFFER_FORMAT_YV12: |
| 621 | case AHARDWAREBUFFER_FORMAT_Y8: |
| 622 | case AHARDWAREBUFFER_FORMAT_Y16: |
| 623 | case AHARDWAREBUFFER_FORMAT_YCbCr_422_SP: |
| 624 | case AHARDWAREBUFFER_FORMAT_YCrCb_420_SP: |
| 625 | case AHARDWAREBUFFER_FORMAT_YCbCr_422_I: |
| 626 | return true; |
| 627 | default: |
| 628 | return false; |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | uint32_t AHardwareBuffer_bytesPerPixel(uint32_t format) { |
| 633 | switch (format) { |
| 634 | case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM: |
| 635 | case AHARDWAREBUFFER_FORMAT_D16_UNORM: |
| 636 | return 2; |
| 637 | case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM: |
| 638 | case AHARDWAREBUFFER_FORMAT_D24_UNORM: |
| 639 | return 3; |
| 640 | case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM: |
| 641 | case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM: |
| 642 | case AHARDWAREBUFFER_FORMAT_D32_FLOAT: |
| 643 | case AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM: |
| 644 | case AHARDWAREBUFFER_FORMAT_D24_UNORM_S8_UINT: |
| 645 | return 4; |
| 646 | default: |
| 647 | return 0; |
| 648 | } |
| 649 | } |
| 650 | |
Mathias Agopian | 3a11413a | 2017-06-08 19:44:17 -0700 | [diff] [blame] | 651 | uint32_t AHardwareBuffer_convertFromPixelFormat(uint32_t hal_format) { |
| 652 | return hal_format; |
| 653 | } |
| 654 | |
| 655 | uint32_t AHardwareBuffer_convertToPixelFormat(uint32_t ahardwarebuffer_format) { |
| 656 | return ahardwarebuffer_format; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 657 | } |
| 658 | |
Mathias Agopian | 2c38b56 | 2017-04-20 16:35:39 -0700 | [diff] [blame] | 659 | uint64_t AHardwareBuffer_convertToGrallocUsageBits(uint64_t usage) { |
Craig Donner | e6ecb92 | 2017-12-27 14:59:29 -0800 | [diff] [blame] | 660 | using android::hardware::graphics::common::V1_1::BufferUsage; |
Mathias Agopian | 2c38b56 | 2017-04-20 16:35:39 -0700 | [diff] [blame] | 661 | static_assert(AHARDWAREBUFFER_USAGE_CPU_READ_NEVER == (uint64_t)BufferUsage::CPU_READ_NEVER, |
| 662 | "gralloc and AHardwareBuffer flags don't match"); |
| 663 | static_assert(AHARDWAREBUFFER_USAGE_CPU_READ_RARELY == (uint64_t)BufferUsage::CPU_READ_RARELY, |
| 664 | "gralloc and AHardwareBuffer flags don't match"); |
| 665 | static_assert(AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN == (uint64_t)BufferUsage::CPU_READ_OFTEN, |
| 666 | "gralloc and AHardwareBuffer flags don't match"); |
| 667 | static_assert(AHARDWAREBUFFER_USAGE_CPU_WRITE_NEVER == (uint64_t)BufferUsage::CPU_WRITE_NEVER, |
| 668 | "gralloc and AHardwareBuffer flags don't match"); |
| 669 | static_assert(AHARDWAREBUFFER_USAGE_CPU_WRITE_RARELY == (uint64_t)BufferUsage::CPU_WRITE_RARELY, |
| 670 | "gralloc and AHardwareBuffer flags don't match"); |
| 671 | static_assert(AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN == (uint64_t)BufferUsage::CPU_WRITE_OFTEN, |
| 672 | "gralloc and AHardwareBuffer flags don't match"); |
| 673 | static_assert(AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE == (uint64_t)BufferUsage::GPU_TEXTURE, |
| 674 | "gralloc and AHardwareBuffer flags don't match"); |
Krzysztof Kosiński | a5c54bc | 2018-11-16 18:24:33 -0800 | [diff] [blame] | 675 | static_assert(AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER == (uint64_t)BufferUsage::GPU_RENDER_TARGET, |
Mathias Agopian | 2c38b56 | 2017-04-20 16:35:39 -0700 | [diff] [blame] | 676 | "gralloc and AHardwareBuffer flags don't match"); |
| 677 | static_assert(AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT == (uint64_t)BufferUsage::PROTECTED, |
| 678 | "gralloc and AHardwareBuffer flags don't match"); |
| 679 | static_assert(AHARDWAREBUFFER_USAGE_VIDEO_ENCODE == (uint64_t)BufferUsage::VIDEO_ENCODER, |
| 680 | "gralloc and AHardwareBuffer flags don't match"); |
| 681 | static_assert(AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER == (uint64_t)BufferUsage::GPU_DATA_BUFFER, |
| 682 | "gralloc and AHardwareBuffer flags don't match"); |
| 683 | static_assert(AHARDWAREBUFFER_USAGE_SENSOR_DIRECT_DATA == (uint64_t)BufferUsage::SENSOR_DIRECT_DATA, |
| 684 | "gralloc and AHardwareBuffer flags don't match"); |
Craig Donner | e6ecb92 | 2017-12-27 14:59:29 -0800 | [diff] [blame] | 685 | static_assert(AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP == (uint64_t)BufferUsage::GPU_CUBE_MAP, |
| 686 | "gralloc and AHardwareBuffer flags don't match"); |
| 687 | static_assert(AHARDWAREBUFFER_USAGE_GPU_MIPMAP_COMPLETE == (uint64_t)BufferUsage::GPU_MIPMAP_COMPLETE, |
| 688 | "gralloc and AHardwareBuffer flags don't match"); |
Mathias Agopian | 2c38b56 | 2017-04-20 16:35:39 -0700 | [diff] [blame] | 689 | return usage; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 690 | } |
| 691 | |
Mathias Agopian | 2c38b56 | 2017-04-20 16:35:39 -0700 | [diff] [blame] | 692 | uint64_t AHardwareBuffer_convertFromGrallocUsageBits(uint64_t usage) { |
| 693 | return usage; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | const GraphicBuffer* AHardwareBuffer_to_GraphicBuffer(const AHardwareBuffer* buffer) { |
Pawin Vongmasa | e672cd0 | 2019-02-14 16:01:29 -0800 | [diff] [blame] | 697 | return GraphicBuffer::fromAHardwareBuffer(buffer); |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | GraphicBuffer* AHardwareBuffer_to_GraphicBuffer(AHardwareBuffer* buffer) { |
Pawin Vongmasa | e672cd0 | 2019-02-14 16:01:29 -0800 | [diff] [blame] | 701 | return GraphicBuffer::fromAHardwareBuffer(buffer); |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 702 | } |
| 703 | |
Mathias Agopian | 6196340 | 2017-02-24 16:38:15 -0800 | [diff] [blame] | 704 | const ANativeWindowBuffer* AHardwareBuffer_to_ANativeWindowBuffer(const AHardwareBuffer* buffer) { |
| 705 | return AHardwareBuffer_to_GraphicBuffer(buffer)->getNativeBuffer(); |
| 706 | } |
| 707 | |
| 708 | ANativeWindowBuffer* AHardwareBuffer_to_ANativeWindowBuffer(AHardwareBuffer* buffer) { |
| 709 | return AHardwareBuffer_to_GraphicBuffer(buffer)->getNativeBuffer(); |
| 710 | } |
| 711 | |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 712 | AHardwareBuffer* AHardwareBuffer_from_GraphicBuffer(GraphicBuffer* buffer) { |
Pawin Vongmasa | e672cd0 | 2019-02-14 16:01:29 -0800 | [diff] [blame] | 713 | return buffer->toAHardwareBuffer(); |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | } // namespace android |