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 | |
| 19 | #include <android/hardware_buffer.h> |
| 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> |
| 30 | #include <hardware/gralloc1.h> |
| 31 | |
| 32 | #include <private/android/AHardwareBufferHelpers.h> |
| 33 | |
| 34 | |
| 35 | static constexpr int kDataBufferSize = 64 * sizeof(int); // 64 ints |
| 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 | |
| 47 | int format = AHardwareBuffer_convertToPixelFormat(desc->format); |
| 48 | if (format == 0) { |
| 49 | ALOGE("Invalid pixel format %u", desc->format); |
| 50 | return BAD_VALUE; |
| 51 | } |
| 52 | |
| 53 | if (desc->format == AHARDWAREBUFFER_FORMAT_BLOB && desc->height != 1) { |
| 54 | ALOGE("Height must be 1 when using the AHARDWAREBUFFER_FORMAT_BLOB format"); |
| 55 | return BAD_VALUE; |
| 56 | } |
| 57 | |
| 58 | uint64_t producerUsage = 0; |
| 59 | uint64_t consumerUsage = 0; |
Craig Donner | a5a719e | 2017-02-24 16:02:08 -0800 | [diff] [blame] | 60 | AHardwareBuffer_convertToGrallocUsageBits(&producerUsage, &consumerUsage, desc->usage0, |
| 61 | desc->usage1); |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 62 | |
| 63 | sp<GraphicBuffer> gbuffer(new GraphicBuffer( |
| 64 | desc->width, desc->height, format, desc->layers, producerUsage, consumerUsage, |
| 65 | std::string("AHardwareBuffer pid [") + std::to_string(getpid()) + "]")); |
| 66 | |
| 67 | status_t err = gbuffer->initCheck(); |
| 68 | if (err != 0 || gbuffer->handle == 0) { |
| 69 | if (err == NO_MEMORY) { |
| 70 | GraphicBuffer::dumpAllocationsToSystemLog(); |
| 71 | } |
Romain Guy | f8b4ca5 | 2017-03-16 18:39:20 +0000 | [diff] [blame] | 72 | ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%u, h=%u, lc=%u) failed (%s), handle=%p", |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 73 | desc->width, desc->height, desc->layers, strerror(-err), gbuffer->handle); |
| 74 | return err; |
| 75 | } |
| 76 | |
| 77 | *outBuffer = AHardwareBuffer_from_GraphicBuffer(gbuffer.get()); |
| 78 | |
| 79 | // Ensure the buffer doesn't get destroyed when the sp<> goes away. |
| 80 | AHardwareBuffer_acquire(*outBuffer); |
| 81 | return NO_ERROR; |
| 82 | } |
| 83 | |
| 84 | void AHardwareBuffer_acquire(AHardwareBuffer* buffer) { |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame^] | 85 | // 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] | 86 | AHardwareBuffer_to_GraphicBuffer(buffer)->incStrong((void*)AHardwareBuffer_acquire); |
| 87 | } |
| 88 | |
| 89 | void AHardwareBuffer_release(AHardwareBuffer* buffer) { |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame^] | 90 | // incStrong/decStrong token must be the same, doesn't matter what it is |
| 91 | AHardwareBuffer_to_GraphicBuffer(buffer)->decStrong((void*)AHardwareBuffer_acquire); |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void AHardwareBuffer_describe(const AHardwareBuffer* buffer, |
| 95 | AHardwareBuffer_Desc* outDesc) { |
| 96 | if (!buffer || !outDesc) return; |
| 97 | |
| 98 | const GraphicBuffer* gbuffer = AHardwareBuffer_to_GraphicBuffer(buffer); |
| 99 | |
| 100 | outDesc->width = gbuffer->getWidth(); |
| 101 | outDesc->height = gbuffer->getHeight(); |
| 102 | outDesc->layers = gbuffer->getLayerCount(); |
Craig Donner | a5a719e | 2017-02-24 16:02:08 -0800 | [diff] [blame] | 103 | AHardwareBuffer_convertFromGrallocUsageBits(&outDesc->usage0, &outDesc->usage1, |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 104 | gbuffer->getUsage(), gbuffer->getUsage()); |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 105 | outDesc->format = AHardwareBuffer_convertFromPixelFormat( |
| 106 | static_cast<uint32_t>(gbuffer->getPixelFormat())); |
| 107 | } |
| 108 | |
| 109 | int AHardwareBuffer_lock(AHardwareBuffer* buffer, uint64_t usage0, |
| 110 | int32_t fence, const ARect* rect, void** outVirtualAddress) { |
| 111 | if (!buffer) return BAD_VALUE; |
| 112 | |
| 113 | if (usage0 & ~(AHARDWAREBUFFER_USAGE0_CPU_READ_OFTEN | |
| 114 | AHARDWAREBUFFER_USAGE0_CPU_WRITE_OFTEN)) { |
| 115 | ALOGE("Invalid usage flags passed to AHardwareBuffer_lock; only " |
| 116 | " AHARDWAREBUFFER_USAGE0_CPU_* flags are allowed"); |
| 117 | return BAD_VALUE; |
| 118 | } |
| 119 | |
| 120 | uint64_t producerUsage = 0; |
| 121 | uint64_t consumerUsage = 0; |
Craig Donner | a5a719e | 2017-02-24 16:02:08 -0800 | [diff] [blame] | 122 | AHardwareBuffer_convertToGrallocUsageBits(&producerUsage, &consumerUsage, usage0, 0); |
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 | } |
| 130 | return gBuffer->lockAsync(producerUsage, consumerUsage, bounds, |
| 131 | outVirtualAddress, fence); |
| 132 | } |
| 133 | |
| 134 | int AHardwareBuffer_unlock(AHardwareBuffer* buffer, int32_t* fence) { |
| 135 | if (!buffer) return BAD_VALUE; |
| 136 | |
| 137 | GraphicBuffer* gBuffer = AHardwareBuffer_to_GraphicBuffer(buffer); |
| 138 | return gBuffer->unlockAsync(fence); |
| 139 | } |
| 140 | |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame^] | 141 | int AHardwareBuffer_sendHandleToUnixSocket(const AHardwareBuffer* buffer, int socketFd) { |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 142 | if (!buffer) return BAD_VALUE; |
| 143 | const GraphicBuffer* gBuffer = AHardwareBuffer_to_GraphicBuffer(buffer); |
| 144 | |
| 145 | size_t flattenedSize = gBuffer->getFlattenedSize(); |
| 146 | size_t fdCount = gBuffer->getFdCount(); |
| 147 | |
| 148 | std::unique_ptr<uint8_t[]> data(new uint8_t[flattenedSize]); |
| 149 | std::unique_ptr<int[]> fds(new int[fdCount]); |
| 150 | |
| 151 | // Make copies of needed items since flatten modifies them, and we don't |
| 152 | // want to send anything if there's an error during flatten. |
| 153 | size_t flattenedSizeCopy = flattenedSize; |
| 154 | size_t fdCountCopy = fdCount; |
| 155 | void* dataStart = data.get(); |
| 156 | int* fdsStart = fds.get(); |
| 157 | status_t err = gBuffer->flatten(dataStart, flattenedSizeCopy, fdsStart, |
| 158 | fdCountCopy); |
| 159 | if (err != NO_ERROR) { |
| 160 | return err; |
| 161 | } |
| 162 | |
| 163 | struct iovec iov[1]; |
| 164 | iov[0].iov_base = data.get(); |
| 165 | iov[0].iov_len = flattenedSize; |
| 166 | |
| 167 | char buf[CMSG_SPACE(kDataBufferSize)]; |
| 168 | struct msghdr msg = { |
| 169 | .msg_control = buf, |
| 170 | .msg_controllen = sizeof(buf), |
| 171 | .msg_iov = &iov[0], |
| 172 | .msg_iovlen = 1, |
| 173 | }; |
| 174 | |
| 175 | struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); |
| 176 | cmsg->cmsg_level = SOL_SOCKET; |
| 177 | cmsg->cmsg_type = SCM_RIGHTS; |
| 178 | cmsg->cmsg_len = CMSG_LEN(sizeof(int) * fdCount); |
| 179 | int* fdData = reinterpret_cast<int*>(CMSG_DATA(cmsg)); |
| 180 | memcpy(fdData, fds.get(), sizeof(int) * fdCount); |
| 181 | msg.msg_controllen = cmsg->cmsg_len; |
| 182 | |
| 183 | int result = sendmsg(socketFd, &msg, 0); |
| 184 | if (result <= 0) { |
| 185 | ALOGE("Error writing AHardwareBuffer to socket: error %#x (%s)", |
| 186 | result, strerror(errno)); |
| 187 | return result; |
| 188 | } |
| 189 | return NO_ERROR; |
| 190 | } |
| 191 | |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame^] | 192 | int AHardwareBuffer_recvHandleFromUnixSocket(int socketFd, AHardwareBuffer** outBuffer) { |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 193 | if (!outBuffer) return BAD_VALUE; |
| 194 | |
| 195 | char dataBuf[CMSG_SPACE(kDataBufferSize)]; |
| 196 | char fdBuf[CMSG_SPACE(kDataBufferSize)]; |
| 197 | struct iovec iov[1]; |
| 198 | iov[0].iov_base = dataBuf; |
| 199 | iov[0].iov_len = sizeof(dataBuf); |
| 200 | |
| 201 | struct msghdr msg = { |
| 202 | .msg_control = fdBuf, |
| 203 | .msg_controllen = sizeof(fdBuf), |
| 204 | .msg_iov = &iov[0], |
| 205 | .msg_iovlen = 1, |
| 206 | }; |
| 207 | |
| 208 | int result = recvmsg(socketFd, &msg, 0); |
| 209 | if (result <= 0) { |
| 210 | ALOGE("Error reading AHardwareBuffer from socket: error %#x (%s)", |
| 211 | result, strerror(errno)); |
| 212 | return result; |
| 213 | } |
| 214 | |
| 215 | if (msg.msg_iovlen != 1) { |
| 216 | ALOGE("Error reading AHardwareBuffer from socket: bad data length"); |
| 217 | return INVALID_OPERATION; |
| 218 | } |
| 219 | |
| 220 | if (msg.msg_controllen % sizeof(int) != 0) { |
| 221 | ALOGE("Error reading AHardwareBuffer from socket: bad fd length"); |
| 222 | return INVALID_OPERATION; |
| 223 | } |
| 224 | |
| 225 | size_t dataLen = msg.msg_iov[0].iov_len; |
| 226 | const void* data = static_cast<const void*>(msg.msg_iov[0].iov_base); |
| 227 | if (!data) { |
| 228 | ALOGE("Error reading AHardwareBuffer from socket: no buffer data"); |
| 229 | return INVALID_OPERATION; |
| 230 | } |
| 231 | |
| 232 | struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); |
| 233 | if (!cmsg) { |
| 234 | ALOGE("Error reading AHardwareBuffer from socket: no fd header"); |
| 235 | return INVALID_OPERATION; |
| 236 | } |
| 237 | |
| 238 | size_t fdCount = msg.msg_controllen >> 2; |
| 239 | const int* fdData = reinterpret_cast<const int*>(CMSG_DATA(cmsg)); |
| 240 | if (!fdData) { |
| 241 | ALOGE("Error reading AHardwareBuffer from socket: no fd data"); |
| 242 | return INVALID_OPERATION; |
| 243 | } |
| 244 | |
| 245 | GraphicBuffer* gBuffer = new GraphicBuffer(); |
| 246 | status_t err = gBuffer->unflatten(data, dataLen, fdData, fdCount); |
| 247 | if (err != NO_ERROR) { |
| 248 | return err; |
| 249 | } |
| 250 | *outBuffer = AHardwareBuffer_from_GraphicBuffer(gBuffer); |
| 251 | // Ensure the buffer has a positive ref-count. |
| 252 | AHardwareBuffer_acquire(*outBuffer); |
| 253 | |
| 254 | return NO_ERROR; |
| 255 | } |
| 256 | |
| 257 | const struct native_handle* AHardwareBuffer_getNativeHandle( |
| 258 | const AHardwareBuffer* buffer) { |
| 259 | if (!buffer) return nullptr; |
| 260 | const GraphicBuffer* gbuffer = AHardwareBuffer_to_GraphicBuffer(buffer); |
| 261 | return gbuffer->handle; |
| 262 | } |
| 263 | |
| 264 | |
| 265 | // ---------------------------------------------------------------------------- |
| 266 | // Helpers implementation |
| 267 | // ---------------------------------------------------------------------------- |
| 268 | |
| 269 | namespace android { |
| 270 | |
Craig Donner | a5a719e | 2017-02-24 16:02:08 -0800 | [diff] [blame] | 271 | // A 1:1 mapping of AHardwaqreBuffer bitmasks to gralloc1 bitmasks. |
| 272 | struct UsageMaskMapping { |
| 273 | uint64_t hardwareBufferMask; |
| 274 | uint64_t grallocMask; |
| 275 | }; |
| 276 | |
| 277 | static constexpr UsageMaskMapping kUsage0ProducerMapping[] = { |
| 278 | { AHARDWAREBUFFER_USAGE0_CPU_WRITE, GRALLOC1_PRODUCER_USAGE_CPU_WRITE }, |
| 279 | { AHARDWAREBUFFER_USAGE0_CPU_WRITE_OFTEN, GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN }, |
| 280 | { AHARDWAREBUFFER_USAGE0_GPU_COLOR_OUTPUT, GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET }, |
| 281 | { AHARDWAREBUFFER_USAGE0_PROTECTED_CONTENT, GRALLOC1_PRODUCER_USAGE_PROTECTED }, |
| 282 | { AHARDWAREBUFFER_USAGE0_SENSOR_DIRECT_DATA, GRALLOC1_PRODUCER_USAGE_SENSOR_DIRECT_DATA }, |
| 283 | }; |
| 284 | |
| 285 | static constexpr UsageMaskMapping kUsage1ProducerMapping[] = { |
| 286 | { AHARDWAREBUFFER_USAGE1_PRODUCER_PRIVATE_0, GRALLOC1_PRODUCER_USAGE_PRIVATE_0 }, |
| 287 | { AHARDWAREBUFFER_USAGE1_PRODUCER_PRIVATE_1, GRALLOC1_PRODUCER_USAGE_PRIVATE_1 }, |
| 288 | { AHARDWAREBUFFER_USAGE1_PRODUCER_PRIVATE_2, GRALLOC1_PRODUCER_USAGE_PRIVATE_2 }, |
| 289 | { AHARDWAREBUFFER_USAGE1_PRODUCER_PRIVATE_3, GRALLOC1_PRODUCER_USAGE_PRIVATE_3 }, |
| 290 | { AHARDWAREBUFFER_USAGE1_PRODUCER_PRIVATE_4, GRALLOC1_PRODUCER_USAGE_PRIVATE_4 }, |
| 291 | { AHARDWAREBUFFER_USAGE1_PRODUCER_PRIVATE_5, GRALLOC1_PRODUCER_USAGE_PRIVATE_5 }, |
| 292 | { AHARDWAREBUFFER_USAGE1_PRODUCER_PRIVATE_6, GRALLOC1_PRODUCER_USAGE_PRIVATE_6 }, |
| 293 | { AHARDWAREBUFFER_USAGE1_PRODUCER_PRIVATE_7, GRALLOC1_PRODUCER_USAGE_PRIVATE_7 }, |
| 294 | { AHARDWAREBUFFER_USAGE1_PRODUCER_PRIVATE_8, GRALLOC1_PRODUCER_USAGE_PRIVATE_8 }, |
| 295 | { AHARDWAREBUFFER_USAGE1_PRODUCER_PRIVATE_9, GRALLOC1_PRODUCER_USAGE_PRIVATE_9 }, |
| 296 | { AHARDWAREBUFFER_USAGE1_PRODUCER_PRIVATE_10, GRALLOC1_PRODUCER_USAGE_PRIVATE_10 }, |
| 297 | { AHARDWAREBUFFER_USAGE1_PRODUCER_PRIVATE_11, GRALLOC1_PRODUCER_USAGE_PRIVATE_11 }, |
| 298 | { AHARDWAREBUFFER_USAGE1_PRODUCER_PRIVATE_12, GRALLOC1_PRODUCER_USAGE_PRIVATE_12 }, |
| 299 | { AHARDWAREBUFFER_USAGE1_PRODUCER_PRIVATE_13, GRALLOC1_PRODUCER_USAGE_PRIVATE_13 }, |
| 300 | { AHARDWAREBUFFER_USAGE1_PRODUCER_PRIVATE_14, GRALLOC1_PRODUCER_USAGE_PRIVATE_14 }, |
| 301 | { AHARDWAREBUFFER_USAGE1_PRODUCER_PRIVATE_15, GRALLOC1_PRODUCER_USAGE_PRIVATE_15 }, |
| 302 | { AHARDWAREBUFFER_USAGE1_PRODUCER_PRIVATE_16, GRALLOC1_PRODUCER_USAGE_PRIVATE_16 }, |
| 303 | { AHARDWAREBUFFER_USAGE1_PRODUCER_PRIVATE_17, GRALLOC1_PRODUCER_USAGE_PRIVATE_17 }, |
| 304 | { AHARDWAREBUFFER_USAGE1_PRODUCER_PRIVATE_18, GRALLOC1_PRODUCER_USAGE_PRIVATE_18 }, |
| 305 | { AHARDWAREBUFFER_USAGE1_PRODUCER_PRIVATE_19, GRALLOC1_PRODUCER_USAGE_PRIVATE_19 }, |
| 306 | }; |
| 307 | |
| 308 | static constexpr UsageMaskMapping kUsage0ConsumerMapping[] = { |
| 309 | { AHARDWAREBUFFER_USAGE0_CPU_READ, GRALLOC1_CONSUMER_USAGE_CPU_READ }, |
| 310 | { AHARDWAREBUFFER_USAGE0_CPU_READ_OFTEN, GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN }, |
| 311 | { AHARDWAREBUFFER_USAGE0_GPU_SAMPLED_IMAGE, GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE }, |
| 312 | { AHARDWAREBUFFER_USAGE0_GPU_DATA_BUFFER, GRALLOC1_CONSUMER_USAGE_GPU_DATA_BUFFER }, |
| 313 | { AHARDWAREBUFFER_USAGE0_VIDEO_ENCODE, GRALLOC1_CONSUMER_USAGE_VIDEO_ENCODER }, |
| 314 | }; |
| 315 | |
| 316 | static constexpr UsageMaskMapping kUsage1ConsumerMapping[] = { |
| 317 | { AHARDWAREBUFFER_USAGE1_CONSUMER_PRIVATE_0, GRALLOC1_CONSUMER_USAGE_PRIVATE_0 }, |
| 318 | { AHARDWAREBUFFER_USAGE1_CONSUMER_PRIVATE_1, GRALLOC1_CONSUMER_USAGE_PRIVATE_1 }, |
| 319 | { AHARDWAREBUFFER_USAGE1_CONSUMER_PRIVATE_2, GRALLOC1_CONSUMER_USAGE_PRIVATE_2 }, |
| 320 | { AHARDWAREBUFFER_USAGE1_CONSUMER_PRIVATE_3, GRALLOC1_CONSUMER_USAGE_PRIVATE_3 }, |
| 321 | { AHARDWAREBUFFER_USAGE1_CONSUMER_PRIVATE_4, GRALLOC1_CONSUMER_USAGE_PRIVATE_4 }, |
| 322 | { AHARDWAREBUFFER_USAGE1_CONSUMER_PRIVATE_5, GRALLOC1_CONSUMER_USAGE_PRIVATE_5 }, |
| 323 | { AHARDWAREBUFFER_USAGE1_CONSUMER_PRIVATE_6, GRALLOC1_CONSUMER_USAGE_PRIVATE_6 }, |
| 324 | { AHARDWAREBUFFER_USAGE1_CONSUMER_PRIVATE_7, GRALLOC1_CONSUMER_USAGE_PRIVATE_7 }, |
| 325 | { AHARDWAREBUFFER_USAGE1_CONSUMER_PRIVATE_8, GRALLOC1_CONSUMER_USAGE_PRIVATE_8 }, |
| 326 | { AHARDWAREBUFFER_USAGE1_CONSUMER_PRIVATE_9, GRALLOC1_CONSUMER_USAGE_PRIVATE_9 }, |
| 327 | { AHARDWAREBUFFER_USAGE1_CONSUMER_PRIVATE_10, GRALLOC1_CONSUMER_USAGE_PRIVATE_10 }, |
| 328 | { AHARDWAREBUFFER_USAGE1_CONSUMER_PRIVATE_11, GRALLOC1_CONSUMER_USAGE_PRIVATE_11 }, |
| 329 | { AHARDWAREBUFFER_USAGE1_CONSUMER_PRIVATE_12, GRALLOC1_CONSUMER_USAGE_PRIVATE_12 }, |
| 330 | { AHARDWAREBUFFER_USAGE1_CONSUMER_PRIVATE_13, GRALLOC1_CONSUMER_USAGE_PRIVATE_13 }, |
| 331 | { AHARDWAREBUFFER_USAGE1_CONSUMER_PRIVATE_14, GRALLOC1_CONSUMER_USAGE_PRIVATE_14 }, |
| 332 | { AHARDWAREBUFFER_USAGE1_CONSUMER_PRIVATE_15, GRALLOC1_CONSUMER_USAGE_PRIVATE_15 }, |
| 333 | { AHARDWAREBUFFER_USAGE1_CONSUMER_PRIVATE_16, GRALLOC1_CONSUMER_USAGE_PRIVATE_16 }, |
| 334 | { AHARDWAREBUFFER_USAGE1_CONSUMER_PRIVATE_17, GRALLOC1_CONSUMER_USAGE_PRIVATE_17 }, |
| 335 | { AHARDWAREBUFFER_USAGE1_CONSUMER_PRIVATE_18, GRALLOC1_CONSUMER_USAGE_PRIVATE_18 }, |
| 336 | { AHARDWAREBUFFER_USAGE1_CONSUMER_PRIVATE_19, GRALLOC1_CONSUMER_USAGE_PRIVATE_19 }, |
| 337 | }; |
| 338 | |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 339 | static inline bool containsBits(uint64_t mask, uint64_t bitsToCheck) { |
Craig Donner | a5a719e | 2017-02-24 16:02:08 -0800 | [diff] [blame] | 340 | return (mask & bitsToCheck) == bitsToCheck && bitsToCheck; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | uint32_t AHardwareBuffer_convertFromPixelFormat(uint32_t format) { |
| 344 | switch (format) { |
| 345 | case HAL_PIXEL_FORMAT_RGBA_8888: return AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM; |
| 346 | case HAL_PIXEL_FORMAT_RGBX_8888: return AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM; |
| 347 | case HAL_PIXEL_FORMAT_RGB_565: return AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM; |
| 348 | case HAL_PIXEL_FORMAT_RGB_888: return AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM; |
| 349 | case HAL_PIXEL_FORMAT_RGBA_FP16: return AHARDWAREBUFFER_FORMAT_R16G16B16A16_SFLOAT; |
| 350 | case HAL_PIXEL_FORMAT_RGBA_1010102: return AHARDWAREBUFFER_FORMAT_A2R10G10B10_UNORM_PACK32; |
| 351 | case HAL_PIXEL_FORMAT_BLOB: return AHARDWAREBUFFER_FORMAT_BLOB; |
| 352 | default:ALOGE("Unknown pixel format %u", format); |
| 353 | return 0; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | uint32_t AHardwareBuffer_convertToPixelFormat(uint32_t format) { |
| 358 | switch (format) { |
| 359 | case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM: return HAL_PIXEL_FORMAT_RGBA_8888; |
| 360 | case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM: return HAL_PIXEL_FORMAT_RGBX_8888; |
| 361 | case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM: return HAL_PIXEL_FORMAT_RGB_565; |
| 362 | case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM: return HAL_PIXEL_FORMAT_RGB_888; |
| 363 | case AHARDWAREBUFFER_FORMAT_R16G16B16A16_SFLOAT: return HAL_PIXEL_FORMAT_RGBA_FP16; |
| 364 | case AHARDWAREBUFFER_FORMAT_A2R10G10B10_UNORM_PACK32: return HAL_PIXEL_FORMAT_RGBA_1010102; |
| 365 | case AHARDWAREBUFFER_FORMAT_BLOB: return HAL_PIXEL_FORMAT_BLOB; |
| 366 | default:ALOGE("Unknown AHardwareBuffer format %u", format); |
| 367 | return 0; |
| 368 | } |
| 369 | } |
| 370 | |
Craig Donner | a5a719e | 2017-02-24 16:02:08 -0800 | [diff] [blame] | 371 | void AHardwareBuffer_convertToGrallocUsageBits(uint64_t* outProducerUsage, |
| 372 | uint64_t* outConsumerUsage, uint64_t usage0, uint64_t usage1) { |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 373 | *outProducerUsage = 0; |
| 374 | *outConsumerUsage = 0; |
Craig Donner | a5a719e | 2017-02-24 16:02:08 -0800 | [diff] [blame] | 375 | for (const UsageMaskMapping& mapping : kUsage0ProducerMapping) { |
| 376 | if (containsBits(usage0, mapping.hardwareBufferMask)) { |
| 377 | *outProducerUsage |= mapping.grallocMask; |
| 378 | } |
| 379 | } |
| 380 | for (const UsageMaskMapping& mapping : kUsage1ProducerMapping) { |
| 381 | if (containsBits(usage1, mapping.hardwareBufferMask)) { |
| 382 | *outProducerUsage |= mapping.grallocMask; |
| 383 | } |
| 384 | } |
| 385 | for (const UsageMaskMapping& mapping : kUsage0ConsumerMapping) { |
| 386 | if (containsBits(usage0, mapping.hardwareBufferMask)) { |
| 387 | *outConsumerUsage |= mapping.grallocMask; |
| 388 | } |
| 389 | } |
| 390 | for (const UsageMaskMapping& mapping : kUsage1ConsumerMapping) { |
| 391 | if (containsBits(usage1, mapping.hardwareBufferMask)) { |
| 392 | *outConsumerUsage |= mapping.grallocMask; |
| 393 | } |
| 394 | } |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 395 | } |
| 396 | |
Craig Donner | a5a719e | 2017-02-24 16:02:08 -0800 | [diff] [blame] | 397 | void AHardwareBuffer_convertFromGrallocUsageBits(uint64_t* outUsage0, uint64_t* outUsage1, |
| 398 | uint64_t producerUsage, uint64_t consumerUsage) { |
| 399 | *outUsage0 = 0; |
| 400 | *outUsage1 = 0; |
| 401 | for (const UsageMaskMapping& mapping : kUsage0ProducerMapping) { |
| 402 | if (containsBits(producerUsage, mapping.grallocMask)) { |
| 403 | *outUsage0 |= mapping.hardwareBufferMask; |
| 404 | } |
| 405 | } |
| 406 | for (const UsageMaskMapping& mapping : kUsage1ProducerMapping) { |
| 407 | if (containsBits(producerUsage, mapping.grallocMask)) { |
| 408 | *outUsage1 |= mapping.hardwareBufferMask; |
| 409 | } |
| 410 | } |
| 411 | for (const UsageMaskMapping& mapping : kUsage0ConsumerMapping) { |
| 412 | if (containsBits(consumerUsage, mapping.grallocMask)) { |
| 413 | *outUsage0 |= mapping.hardwareBufferMask; |
| 414 | } |
| 415 | } |
| 416 | for (const UsageMaskMapping& mapping : kUsage1ConsumerMapping) { |
| 417 | if (containsBits(consumerUsage, mapping.grallocMask)) { |
| 418 | *outUsage1 |= mapping.hardwareBufferMask; |
| 419 | } |
| 420 | } |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | const GraphicBuffer* AHardwareBuffer_to_GraphicBuffer(const AHardwareBuffer* buffer) { |
| 424 | return reinterpret_cast<const GraphicBuffer*>(buffer); |
| 425 | } |
| 426 | |
| 427 | GraphicBuffer* AHardwareBuffer_to_GraphicBuffer(AHardwareBuffer* buffer) { |
| 428 | return reinterpret_cast<GraphicBuffer*>(buffer); |
| 429 | } |
| 430 | |
Mathias Agopian | 6196340 | 2017-02-24 16:38:15 -0800 | [diff] [blame] | 431 | const ANativeWindowBuffer* AHardwareBuffer_to_ANativeWindowBuffer(const AHardwareBuffer* buffer) { |
| 432 | return AHardwareBuffer_to_GraphicBuffer(buffer)->getNativeBuffer(); |
| 433 | } |
| 434 | |
| 435 | ANativeWindowBuffer* AHardwareBuffer_to_ANativeWindowBuffer(AHardwareBuffer* buffer) { |
| 436 | return AHardwareBuffer_to_GraphicBuffer(buffer)->getNativeBuffer(); |
| 437 | } |
| 438 | |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 439 | AHardwareBuffer* AHardwareBuffer_from_GraphicBuffer(GraphicBuffer* buffer) { |
| 440 | return reinterpret_cast<AHardwareBuffer*>(buffer); |
| 441 | } |
| 442 | |
| 443 | } // namespace android |