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 | 2c38b56 | 2017-04-20 16:35:39 -0700 | [diff] [blame^] | 53 | if (desc->reserved != 0) { |
| 54 | ALOGE("AHardwareBuffer_Desc::reserved field must be 0"); |
| 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 | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 104 | outDesc->format = AHardwareBuffer_convertFromPixelFormat( |
| 105 | static_cast<uint32_t>(gbuffer->getPixelFormat())); |
Mathias Agopian | 2c38b56 | 2017-04-20 16:35:39 -0700 | [diff] [blame^] | 106 | outDesc->usage = AHardwareBuffer_convertFromGrallocUsageBits(gbuffer->getUsage()); |
| 107 | outDesc->reserved = 0; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Mathias Agopian | 2c38b56 | 2017-04-20 16:35:39 -0700 | [diff] [blame^] | 110 | int AHardwareBuffer_lock(AHardwareBuffer* buffer, uint64_t usage, |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 111 | int32_t fence, const ARect* rect, void** outVirtualAddress) { |
| 112 | if (!buffer) return BAD_VALUE; |
| 113 | |
Mathias Agopian | 2c38b56 | 2017-04-20 16:35:39 -0700 | [diff] [blame^] | 114 | if (usage & ~(AHARDWAREBUFFER_USAGE_CPU_READ_MASK | |
| 115 | AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK)) { |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 116 | ALOGE("Invalid usage flags passed to AHardwareBuffer_lock; only " |
Mathias Agopian | 2c38b56 | 2017-04-20 16:35:39 -0700 | [diff] [blame^] | 117 | " AHARDWAREBUFFER_USAGE_CPU_* flags are allowed"); |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 118 | return BAD_VALUE; |
| 119 | } |
| 120 | |
Mathias Agopian | 2c38b56 | 2017-04-20 16:35:39 -0700 | [diff] [blame^] | 121 | usage = AHardwareBuffer_convertToGrallocUsageBits(usage); |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 122 | GraphicBuffer* gBuffer = AHardwareBuffer_to_GraphicBuffer(buffer); |
| 123 | Rect bounds; |
| 124 | if (!rect) { |
| 125 | bounds.set(Rect(gBuffer->getWidth(), gBuffer->getHeight())); |
| 126 | } else { |
| 127 | bounds.set(Rect(rect->left, rect->top, rect->right, rect->bottom)); |
| 128 | } |
Mathias Agopian | 2c38b56 | 2017-04-20 16:35:39 -0700 | [diff] [blame^] | 129 | return gBuffer->lockAsync(usage, usage, bounds, outVirtualAddress, fence); |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | int AHardwareBuffer_unlock(AHardwareBuffer* buffer, int32_t* fence) { |
| 133 | if (!buffer) return BAD_VALUE; |
| 134 | |
| 135 | GraphicBuffer* gBuffer = AHardwareBuffer_to_GraphicBuffer(buffer); |
| 136 | return gBuffer->unlockAsync(fence); |
| 137 | } |
| 138 | |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame] | 139 | int AHardwareBuffer_sendHandleToUnixSocket(const AHardwareBuffer* buffer, int socketFd) { |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 140 | if (!buffer) return BAD_VALUE; |
| 141 | const GraphicBuffer* gBuffer = AHardwareBuffer_to_GraphicBuffer(buffer); |
| 142 | |
| 143 | size_t flattenedSize = gBuffer->getFlattenedSize(); |
| 144 | size_t fdCount = gBuffer->getFdCount(); |
| 145 | |
| 146 | std::unique_ptr<uint8_t[]> data(new uint8_t[flattenedSize]); |
| 147 | std::unique_ptr<int[]> fds(new int[fdCount]); |
| 148 | |
| 149 | // Make copies of needed items since flatten modifies them, and we don't |
| 150 | // want to send anything if there's an error during flatten. |
| 151 | size_t flattenedSizeCopy = flattenedSize; |
| 152 | size_t fdCountCopy = fdCount; |
| 153 | void* dataStart = data.get(); |
| 154 | int* fdsStart = fds.get(); |
| 155 | status_t err = gBuffer->flatten(dataStart, flattenedSizeCopy, fdsStart, |
| 156 | fdCountCopy); |
| 157 | if (err != NO_ERROR) { |
| 158 | return err; |
| 159 | } |
| 160 | |
| 161 | struct iovec iov[1]; |
| 162 | iov[0].iov_base = data.get(); |
| 163 | iov[0].iov_len = flattenedSize; |
| 164 | |
Craig Donner | 1b1d300 | 2017-04-12 18:38:07 -0700 | [diff] [blame] | 165 | char buf[CMSG_SPACE(kFdBufferSize)]; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 166 | struct msghdr msg = { |
| 167 | .msg_control = buf, |
| 168 | .msg_controllen = sizeof(buf), |
| 169 | .msg_iov = &iov[0], |
| 170 | .msg_iovlen = 1, |
| 171 | }; |
| 172 | |
| 173 | struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); |
| 174 | cmsg->cmsg_level = SOL_SOCKET; |
| 175 | cmsg->cmsg_type = SCM_RIGHTS; |
| 176 | cmsg->cmsg_len = CMSG_LEN(sizeof(int) * fdCount); |
| 177 | int* fdData = reinterpret_cast<int*>(CMSG_DATA(cmsg)); |
| 178 | memcpy(fdData, fds.get(), sizeof(int) * fdCount); |
| 179 | msg.msg_controllen = cmsg->cmsg_len; |
| 180 | |
| 181 | int result = sendmsg(socketFd, &msg, 0); |
| 182 | if (result <= 0) { |
| 183 | ALOGE("Error writing AHardwareBuffer to socket: error %#x (%s)", |
| 184 | result, strerror(errno)); |
| 185 | return result; |
| 186 | } |
| 187 | return NO_ERROR; |
| 188 | } |
| 189 | |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame] | 190 | int AHardwareBuffer_recvHandleFromUnixSocket(int socketFd, AHardwareBuffer** outBuffer) { |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 191 | if (!outBuffer) return BAD_VALUE; |
| 192 | |
Craig Donner | 1b1d300 | 2017-04-12 18:38:07 -0700 | [diff] [blame] | 193 | static constexpr int kMessageBufferSize = 4096 * sizeof(int); |
| 194 | |
| 195 | std::unique_ptr<char[]> dataBuf(new char[kMessageBufferSize]); |
| 196 | char fdBuf[CMSG_SPACE(kFdBufferSize)]; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 197 | struct iovec iov[1]; |
Craig Donner | 1b1d300 | 2017-04-12 18:38:07 -0700 | [diff] [blame] | 198 | iov[0].iov_base = dataBuf.get(); |
| 199 | iov[0].iov_len = kMessageBufferSize; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 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 | |
Jesse Hall | 1ee859f | 2017-04-17 08:52:32 -0700 | [diff] [blame] | 257 | |
| 258 | // ---------------------------------------------------------------------------- |
| 259 | // VNDK functions |
| 260 | // ---------------------------------------------------------------------------- |
| 261 | |
| 262 | const native_handle_t* AHardwareBuffer_getNativeHandle( |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 263 | const AHardwareBuffer* buffer) { |
| 264 | if (!buffer) return nullptr; |
| 265 | const GraphicBuffer* gbuffer = AHardwareBuffer_to_GraphicBuffer(buffer); |
| 266 | return gbuffer->handle; |
| 267 | } |
| 268 | |
| 269 | |
| 270 | // ---------------------------------------------------------------------------- |
| 271 | // Helpers implementation |
| 272 | // ---------------------------------------------------------------------------- |
| 273 | |
| 274 | namespace android { |
| 275 | |
Craig Donner | a5a719e | 2017-02-24 16:02:08 -0800 | [diff] [blame] | 276 | // A 1:1 mapping of AHardwaqreBuffer bitmasks to gralloc1 bitmasks. |
| 277 | struct UsageMaskMapping { |
| 278 | uint64_t hardwareBufferMask; |
| 279 | uint64_t grallocMask; |
| 280 | }; |
| 281 | |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 282 | static inline bool containsBits(uint64_t mask, uint64_t bitsToCheck) { |
Craig Donner | a5a719e | 2017-02-24 16:02:08 -0800 | [diff] [blame] | 283 | return (mask & bitsToCheck) == bitsToCheck && bitsToCheck; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | uint32_t AHardwareBuffer_convertFromPixelFormat(uint32_t format) { |
| 287 | switch (format) { |
| 288 | case HAL_PIXEL_FORMAT_RGBA_8888: return AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM; |
| 289 | case HAL_PIXEL_FORMAT_RGBX_8888: return AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM; |
| 290 | case HAL_PIXEL_FORMAT_RGB_565: return AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM; |
| 291 | case HAL_PIXEL_FORMAT_RGB_888: return AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM; |
Jesse Hall | 577aa75 | 2017-04-19 15:43:02 -0700 | [diff] [blame] | 292 | case HAL_PIXEL_FORMAT_RGBA_FP16: return AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT; |
| 293 | case HAL_PIXEL_FORMAT_RGBA_1010102: return AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 294 | case HAL_PIXEL_FORMAT_BLOB: return AHARDWAREBUFFER_FORMAT_BLOB; |
| 295 | default:ALOGE("Unknown pixel format %u", format); |
| 296 | return 0; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | uint32_t AHardwareBuffer_convertToPixelFormat(uint32_t format) { |
| 301 | switch (format) { |
| 302 | case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM: return HAL_PIXEL_FORMAT_RGBA_8888; |
| 303 | case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM: return HAL_PIXEL_FORMAT_RGBX_8888; |
| 304 | case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM: return HAL_PIXEL_FORMAT_RGB_565; |
| 305 | case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM: return HAL_PIXEL_FORMAT_RGB_888; |
Jesse Hall | 577aa75 | 2017-04-19 15:43:02 -0700 | [diff] [blame] | 306 | case AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT: return HAL_PIXEL_FORMAT_RGBA_FP16; |
| 307 | case AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM: return HAL_PIXEL_FORMAT_RGBA_1010102; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 308 | case AHARDWAREBUFFER_FORMAT_BLOB: return HAL_PIXEL_FORMAT_BLOB; |
| 309 | default:ALOGE("Unknown AHardwareBuffer format %u", format); |
| 310 | return 0; |
| 311 | } |
| 312 | } |
| 313 | |
Mathias Agopian | 2c38b56 | 2017-04-20 16:35:39 -0700 | [diff] [blame^] | 314 | uint64_t AHardwareBuffer_convertToGrallocUsageBits(uint64_t usage) { |
| 315 | using android::hardware::graphics::common::V1_0::BufferUsage; |
| 316 | static_assert(AHARDWAREBUFFER_USAGE_CPU_READ_NEVER == (uint64_t)BufferUsage::CPU_READ_NEVER, |
| 317 | "gralloc and AHardwareBuffer flags don't match"); |
| 318 | static_assert(AHARDWAREBUFFER_USAGE_CPU_READ_RARELY == (uint64_t)BufferUsage::CPU_READ_RARELY, |
| 319 | "gralloc and AHardwareBuffer flags don't match"); |
| 320 | static_assert(AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN == (uint64_t)BufferUsage::CPU_READ_OFTEN, |
| 321 | "gralloc and AHardwareBuffer flags don't match"); |
| 322 | static_assert(AHARDWAREBUFFER_USAGE_CPU_WRITE_NEVER == (uint64_t)BufferUsage::CPU_WRITE_NEVER, |
| 323 | "gralloc and AHardwareBuffer flags don't match"); |
| 324 | static_assert(AHARDWAREBUFFER_USAGE_CPU_WRITE_RARELY == (uint64_t)BufferUsage::CPU_WRITE_RARELY, |
| 325 | "gralloc and AHardwareBuffer flags don't match"); |
| 326 | static_assert(AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN == (uint64_t)BufferUsage::CPU_WRITE_OFTEN, |
| 327 | "gralloc and AHardwareBuffer flags don't match"); |
| 328 | static_assert(AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE == (uint64_t)BufferUsage::GPU_TEXTURE, |
| 329 | "gralloc and AHardwareBuffer flags don't match"); |
| 330 | static_assert(AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT == (uint64_t)BufferUsage::GPU_RENDER_TARGET, |
| 331 | "gralloc and AHardwareBuffer flags don't match"); |
| 332 | static_assert(AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT == (uint64_t)BufferUsage::PROTECTED, |
| 333 | "gralloc and AHardwareBuffer flags don't match"); |
| 334 | static_assert(AHARDWAREBUFFER_USAGE_VIDEO_ENCODE == (uint64_t)BufferUsage::VIDEO_ENCODER, |
| 335 | "gralloc and AHardwareBuffer flags don't match"); |
| 336 | static_assert(AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER == (uint64_t)BufferUsage::GPU_DATA_BUFFER, |
| 337 | "gralloc and AHardwareBuffer flags don't match"); |
| 338 | static_assert(AHARDWAREBUFFER_USAGE_SENSOR_DIRECT_DATA == (uint64_t)BufferUsage::SENSOR_DIRECT_DATA, |
| 339 | "gralloc and AHardwareBuffer flags don't match"); |
| 340 | return usage; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 341 | } |
| 342 | |
Mathias Agopian | 2c38b56 | 2017-04-20 16:35:39 -0700 | [diff] [blame^] | 343 | uint64_t AHardwareBuffer_convertFromGrallocUsageBits(uint64_t usage) { |
| 344 | return usage; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | const GraphicBuffer* AHardwareBuffer_to_GraphicBuffer(const AHardwareBuffer* buffer) { |
| 348 | return reinterpret_cast<const GraphicBuffer*>(buffer); |
| 349 | } |
| 350 | |
| 351 | GraphicBuffer* AHardwareBuffer_to_GraphicBuffer(AHardwareBuffer* buffer) { |
| 352 | return reinterpret_cast<GraphicBuffer*>(buffer); |
| 353 | } |
| 354 | |
Mathias Agopian | 6196340 | 2017-02-24 16:38:15 -0800 | [diff] [blame] | 355 | const ANativeWindowBuffer* AHardwareBuffer_to_ANativeWindowBuffer(const AHardwareBuffer* buffer) { |
| 356 | return AHardwareBuffer_to_GraphicBuffer(buffer)->getNativeBuffer(); |
| 357 | } |
| 358 | |
| 359 | ANativeWindowBuffer* AHardwareBuffer_to_ANativeWindowBuffer(AHardwareBuffer* buffer) { |
| 360 | return AHardwareBuffer_to_GraphicBuffer(buffer)->getNativeBuffer(); |
| 361 | } |
| 362 | |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 363 | AHardwareBuffer* AHardwareBuffer_from_GraphicBuffer(GraphicBuffer* buffer) { |
| 364 | return reinterpret_cast<AHardwareBuffer*>(buffer); |
| 365 | } |
| 366 | |
| 367 | } // namespace android |