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> |
Mathias Agopian | 2c38b56 | 2017-04-20 16:35:39 -0700 | [diff] [blame] | 32 | #include <android/hardware/graphics/common/1.0/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 | |
| 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 | |
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); |
| 137 | return gBuffer->unlockAsync(fence); |
| 138 | } |
| 139 | |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame] | 140 | int AHardwareBuffer_sendHandleToUnixSocket(const AHardwareBuffer* buffer, int socketFd) { |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 141 | if (!buffer) return BAD_VALUE; |
| 142 | const GraphicBuffer* gBuffer = AHardwareBuffer_to_GraphicBuffer(buffer); |
| 143 | |
| 144 | size_t flattenedSize = gBuffer->getFlattenedSize(); |
| 145 | size_t fdCount = gBuffer->getFdCount(); |
| 146 | |
| 147 | std::unique_ptr<uint8_t[]> data(new uint8_t[flattenedSize]); |
| 148 | std::unique_ptr<int[]> fds(new int[fdCount]); |
| 149 | |
| 150 | // Make copies of needed items since flatten modifies them, and we don't |
| 151 | // want to send anything if there's an error during flatten. |
| 152 | size_t flattenedSizeCopy = flattenedSize; |
| 153 | size_t fdCountCopy = fdCount; |
| 154 | void* dataStart = data.get(); |
| 155 | int* fdsStart = fds.get(); |
| 156 | status_t err = gBuffer->flatten(dataStart, flattenedSizeCopy, fdsStart, |
| 157 | fdCountCopy); |
| 158 | if (err != NO_ERROR) { |
| 159 | return err; |
| 160 | } |
| 161 | |
| 162 | struct iovec iov[1]; |
| 163 | iov[0].iov_base = data.get(); |
| 164 | iov[0].iov_len = flattenedSize; |
| 165 | |
Craig Donner | 1b1d300 | 2017-04-12 18:38:07 -0700 | [diff] [blame] | 166 | char buf[CMSG_SPACE(kFdBufferSize)]; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 167 | struct msghdr msg = { |
| 168 | .msg_control = buf, |
| 169 | .msg_controllen = sizeof(buf), |
| 170 | .msg_iov = &iov[0], |
| 171 | .msg_iovlen = 1, |
| 172 | }; |
| 173 | |
| 174 | struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); |
| 175 | cmsg->cmsg_level = SOL_SOCKET; |
| 176 | cmsg->cmsg_type = SCM_RIGHTS; |
| 177 | cmsg->cmsg_len = CMSG_LEN(sizeof(int) * fdCount); |
| 178 | int* fdData = reinterpret_cast<int*>(CMSG_DATA(cmsg)); |
| 179 | memcpy(fdData, fds.get(), sizeof(int) * fdCount); |
| 180 | msg.msg_controllen = cmsg->cmsg_len; |
| 181 | |
| 182 | int result = sendmsg(socketFd, &msg, 0); |
| 183 | if (result <= 0) { |
| 184 | ALOGE("Error writing AHardwareBuffer to socket: error %#x (%s)", |
| 185 | result, strerror(errno)); |
| 186 | return result; |
| 187 | } |
| 188 | return NO_ERROR; |
| 189 | } |
| 190 | |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame] | 191 | int AHardwareBuffer_recvHandleFromUnixSocket(int socketFd, AHardwareBuffer** outBuffer) { |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 192 | if (!outBuffer) return BAD_VALUE; |
| 193 | |
Craig Donner | 1b1d300 | 2017-04-12 18:38:07 -0700 | [diff] [blame] | 194 | static constexpr int kMessageBufferSize = 4096 * sizeof(int); |
| 195 | |
| 196 | std::unique_ptr<char[]> dataBuf(new char[kMessageBufferSize]); |
| 197 | char fdBuf[CMSG_SPACE(kFdBufferSize)]; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 198 | struct iovec iov[1]; |
Craig Donner | 1b1d300 | 2017-04-12 18:38:07 -0700 | [diff] [blame] | 199 | iov[0].iov_base = dataBuf.get(); |
| 200 | iov[0].iov_len = kMessageBufferSize; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 201 | |
| 202 | struct msghdr msg = { |
| 203 | .msg_control = fdBuf, |
| 204 | .msg_controllen = sizeof(fdBuf), |
| 205 | .msg_iov = &iov[0], |
| 206 | .msg_iovlen = 1, |
| 207 | }; |
| 208 | |
| 209 | int result = recvmsg(socketFd, &msg, 0); |
| 210 | if (result <= 0) { |
| 211 | ALOGE("Error reading AHardwareBuffer from socket: error %#x (%s)", |
| 212 | result, strerror(errno)); |
| 213 | return result; |
| 214 | } |
| 215 | |
| 216 | if (msg.msg_iovlen != 1) { |
| 217 | ALOGE("Error reading AHardwareBuffer from socket: bad data length"); |
| 218 | return INVALID_OPERATION; |
| 219 | } |
| 220 | |
| 221 | if (msg.msg_controllen % sizeof(int) != 0) { |
| 222 | ALOGE("Error reading AHardwareBuffer from socket: bad fd length"); |
| 223 | return INVALID_OPERATION; |
| 224 | } |
| 225 | |
| 226 | size_t dataLen = msg.msg_iov[0].iov_len; |
| 227 | const void* data = static_cast<const void*>(msg.msg_iov[0].iov_base); |
| 228 | if (!data) { |
| 229 | ALOGE("Error reading AHardwareBuffer from socket: no buffer data"); |
| 230 | return INVALID_OPERATION; |
| 231 | } |
| 232 | |
| 233 | struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); |
| 234 | if (!cmsg) { |
| 235 | ALOGE("Error reading AHardwareBuffer from socket: no fd header"); |
| 236 | return INVALID_OPERATION; |
| 237 | } |
| 238 | |
| 239 | size_t fdCount = msg.msg_controllen >> 2; |
| 240 | const int* fdData = reinterpret_cast<const int*>(CMSG_DATA(cmsg)); |
| 241 | if (!fdData) { |
| 242 | ALOGE("Error reading AHardwareBuffer from socket: no fd data"); |
| 243 | return INVALID_OPERATION; |
| 244 | } |
| 245 | |
| 246 | GraphicBuffer* gBuffer = new GraphicBuffer(); |
| 247 | status_t err = gBuffer->unflatten(data, dataLen, fdData, fdCount); |
| 248 | if (err != NO_ERROR) { |
| 249 | return err; |
| 250 | } |
| 251 | *outBuffer = AHardwareBuffer_from_GraphicBuffer(gBuffer); |
| 252 | // Ensure the buffer has a positive ref-count. |
| 253 | AHardwareBuffer_acquire(*outBuffer); |
| 254 | |
| 255 | return NO_ERROR; |
| 256 | } |
| 257 | |
Jesse Hall | 1ee859f | 2017-04-17 08:52:32 -0700 | [diff] [blame] | 258 | |
| 259 | // ---------------------------------------------------------------------------- |
| 260 | // VNDK functions |
| 261 | // ---------------------------------------------------------------------------- |
| 262 | |
| 263 | const native_handle_t* AHardwareBuffer_getNativeHandle( |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 264 | const AHardwareBuffer* buffer) { |
| 265 | if (!buffer) return nullptr; |
| 266 | const GraphicBuffer* gbuffer = AHardwareBuffer_to_GraphicBuffer(buffer); |
| 267 | return gbuffer->handle; |
| 268 | } |
| 269 | |
| 270 | |
| 271 | // ---------------------------------------------------------------------------- |
| 272 | // Helpers implementation |
| 273 | // ---------------------------------------------------------------------------- |
| 274 | |
| 275 | namespace android { |
| 276 | |
Craig Donner | a5a719e | 2017-02-24 16:02:08 -0800 | [diff] [blame] | 277 | // A 1:1 mapping of AHardwaqreBuffer bitmasks to gralloc1 bitmasks. |
| 278 | struct UsageMaskMapping { |
| 279 | uint64_t hardwareBufferMask; |
| 280 | uint64_t grallocMask; |
| 281 | }; |
| 282 | |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 283 | static inline bool containsBits(uint64_t mask, uint64_t bitsToCheck) { |
Craig Donner | a5a719e | 2017-02-24 16:02:08 -0800 | [diff] [blame] | 284 | return (mask & bitsToCheck) == bitsToCheck && bitsToCheck; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | uint32_t AHardwareBuffer_convertFromPixelFormat(uint32_t format) { |
| 288 | switch (format) { |
| 289 | case HAL_PIXEL_FORMAT_RGBA_8888: return AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM; |
| 290 | case HAL_PIXEL_FORMAT_RGBX_8888: return AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM; |
| 291 | case HAL_PIXEL_FORMAT_RGB_565: return AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM; |
| 292 | case HAL_PIXEL_FORMAT_RGB_888: return AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM; |
Jesse Hall | 577aa75 | 2017-04-19 15:43:02 -0700 | [diff] [blame] | 293 | case HAL_PIXEL_FORMAT_RGBA_FP16: return AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT; |
| 294 | case HAL_PIXEL_FORMAT_RGBA_1010102: return AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 295 | case HAL_PIXEL_FORMAT_BLOB: return AHARDWAREBUFFER_FORMAT_BLOB; |
| 296 | default:ALOGE("Unknown pixel format %u", format); |
| 297 | return 0; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | uint32_t AHardwareBuffer_convertToPixelFormat(uint32_t format) { |
| 302 | switch (format) { |
| 303 | case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM: return HAL_PIXEL_FORMAT_RGBA_8888; |
| 304 | case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM: return HAL_PIXEL_FORMAT_RGBX_8888; |
| 305 | case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM: return HAL_PIXEL_FORMAT_RGB_565; |
| 306 | case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM: return HAL_PIXEL_FORMAT_RGB_888; |
Jesse Hall | 577aa75 | 2017-04-19 15:43:02 -0700 | [diff] [blame] | 307 | case AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT: return HAL_PIXEL_FORMAT_RGBA_FP16; |
| 308 | case AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM: return HAL_PIXEL_FORMAT_RGBA_1010102; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 309 | case AHARDWAREBUFFER_FORMAT_BLOB: return HAL_PIXEL_FORMAT_BLOB; |
| 310 | default:ALOGE("Unknown AHardwareBuffer format %u", format); |
| 311 | return 0; |
| 312 | } |
| 313 | } |
| 314 | |
Mathias Agopian | 2c38b56 | 2017-04-20 16:35:39 -0700 | [diff] [blame] | 315 | uint64_t AHardwareBuffer_convertToGrallocUsageBits(uint64_t usage) { |
| 316 | using android::hardware::graphics::common::V1_0::BufferUsage; |
| 317 | static_assert(AHARDWAREBUFFER_USAGE_CPU_READ_NEVER == (uint64_t)BufferUsage::CPU_READ_NEVER, |
| 318 | "gralloc and AHardwareBuffer flags don't match"); |
| 319 | static_assert(AHARDWAREBUFFER_USAGE_CPU_READ_RARELY == (uint64_t)BufferUsage::CPU_READ_RARELY, |
| 320 | "gralloc and AHardwareBuffer flags don't match"); |
| 321 | static_assert(AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN == (uint64_t)BufferUsage::CPU_READ_OFTEN, |
| 322 | "gralloc and AHardwareBuffer flags don't match"); |
| 323 | static_assert(AHARDWAREBUFFER_USAGE_CPU_WRITE_NEVER == (uint64_t)BufferUsage::CPU_WRITE_NEVER, |
| 324 | "gralloc and AHardwareBuffer flags don't match"); |
| 325 | static_assert(AHARDWAREBUFFER_USAGE_CPU_WRITE_RARELY == (uint64_t)BufferUsage::CPU_WRITE_RARELY, |
| 326 | "gralloc and AHardwareBuffer flags don't match"); |
| 327 | static_assert(AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN == (uint64_t)BufferUsage::CPU_WRITE_OFTEN, |
| 328 | "gralloc and AHardwareBuffer flags don't match"); |
| 329 | static_assert(AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE == (uint64_t)BufferUsage::GPU_TEXTURE, |
| 330 | "gralloc and AHardwareBuffer flags don't match"); |
| 331 | static_assert(AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT == (uint64_t)BufferUsage::GPU_RENDER_TARGET, |
| 332 | "gralloc and AHardwareBuffer flags don't match"); |
| 333 | static_assert(AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT == (uint64_t)BufferUsage::PROTECTED, |
| 334 | "gralloc and AHardwareBuffer flags don't match"); |
| 335 | static_assert(AHARDWAREBUFFER_USAGE_VIDEO_ENCODE == (uint64_t)BufferUsage::VIDEO_ENCODER, |
| 336 | "gralloc and AHardwareBuffer flags don't match"); |
| 337 | static_assert(AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER == (uint64_t)BufferUsage::GPU_DATA_BUFFER, |
| 338 | "gralloc and AHardwareBuffer flags don't match"); |
| 339 | static_assert(AHARDWAREBUFFER_USAGE_SENSOR_DIRECT_DATA == (uint64_t)BufferUsage::SENSOR_DIRECT_DATA, |
| 340 | "gralloc and AHardwareBuffer flags don't match"); |
| 341 | return usage; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 342 | } |
| 343 | |
Mathias Agopian | 2c38b56 | 2017-04-20 16:35:39 -0700 | [diff] [blame] | 344 | uint64_t AHardwareBuffer_convertFromGrallocUsageBits(uint64_t usage) { |
| 345 | return usage; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | const GraphicBuffer* AHardwareBuffer_to_GraphicBuffer(const AHardwareBuffer* buffer) { |
| 349 | return reinterpret_cast<const GraphicBuffer*>(buffer); |
| 350 | } |
| 351 | |
| 352 | GraphicBuffer* AHardwareBuffer_to_GraphicBuffer(AHardwareBuffer* buffer) { |
| 353 | return reinterpret_cast<GraphicBuffer*>(buffer); |
| 354 | } |
| 355 | |
Mathias Agopian | 6196340 | 2017-02-24 16:38:15 -0800 | [diff] [blame] | 356 | const ANativeWindowBuffer* AHardwareBuffer_to_ANativeWindowBuffer(const AHardwareBuffer* buffer) { |
| 357 | return AHardwareBuffer_to_GraphicBuffer(buffer)->getNativeBuffer(); |
| 358 | } |
| 359 | |
| 360 | ANativeWindowBuffer* AHardwareBuffer_to_ANativeWindowBuffer(AHardwareBuffer* buffer) { |
| 361 | return AHardwareBuffer_to_GraphicBuffer(buffer)->getNativeBuffer(); |
| 362 | } |
| 363 | |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 364 | AHardwareBuffer* AHardwareBuffer_from_GraphicBuffer(GraphicBuffer* buffer) { |
| 365 | return reinterpret_cast<AHardwareBuffer*>(buffer); |
| 366 | } |
| 367 | |
| 368 | } // namespace android |