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