blob: af55623ac5652ca53a975fffb8c59ada0e147bcf [file] [log] [blame]
Mathias Agopian89ed4c82017-02-09 18:48:34 -08001/*
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 Hall1ee859f2017-04-17 08:52:32 -070019#include <vndk/hardware_buffer.h>
Mathias Agopian89ed4c82017-02-09 18:48:34 -080020
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 Agopian89ed4c82017-02-09 18:48:34 -080030
31#include <private/android/AHardwareBufferHelpers.h>
Craig Donnere6ecb922017-12-27 14:59:29 -080032#include <android/hardware/graphics/common/1.1/types.h>
Mathias Agopian89ed4c82017-02-09 18:48:34 -080033
34
Craig Donner1b1d3002017-04-12 18:38:07 -070035static constexpr int kFdBufferSize = 128 * sizeof(int); // 128 ints
Mathias Agopian89ed4c82017-02-09 18:48:34 -080036
37using namespace android;
38
39// ----------------------------------------------------------------------------
40// Public functions
41// ----------------------------------------------------------------------------
42
43int AHardwareBuffer_allocate(const AHardwareBuffer_Desc* desc, AHardwareBuffer** outBuffer) {
Krzysztof Kosińskia5c54bc2018-11-16 18:24:33 -080044 if (!outBuffer || !desc) return BAD_VALUE;
45 if (!AHardwareBuffer_isValidDescription(desc, /*log=*/true)) return BAD_VALUE;
Mathias Agopian89ed4c82017-02-09 18:48:34 -080046
Mathias Agopian3a11413a2017-06-08 19:44:17 -070047 int format = AHardwareBuffer_convertToPixelFormat(desc->format);
Krzysztof Kosińskia5c54bc2018-11-16 18:24:33 -080048 uint64_t usage = AHardwareBuffer_convertToGrallocUsageBits(desc->usage);
Mathias Agopian89ed4c82017-02-09 18:48:34 -080049 sp<GraphicBuffer> gbuffer(new GraphicBuffer(
Chris Forbesd4106302017-04-20 12:43:04 -070050 desc->width, desc->height, format, desc->layers, usage,
Mathias Agopian89ed4c82017-02-09 18:48:34 -080051 std::string("AHardwareBuffer pid [") + std::to_string(getpid()) + "]"));
52
53 status_t err = gbuffer->initCheck();
George Burgess IV3a2e1ce2021-01-20 21:06:20 -080054 if (err != 0 || gbuffer->handle == nullptr) {
Mathias Agopian89ed4c82017-02-09 18:48:34 -080055 if (err == NO_MEMORY) {
56 GraphicBuffer::dumpAllocationsToSystemLog();
57 }
Mathias Agopian0556d792017-03-22 15:49:32 -070058 ALOGE("GraphicBuffer(w=%u, h=%u, lc=%u) failed (%s), handle=%p",
Mathias Agopian89ed4c82017-02-09 18:48:34 -080059 desc->width, desc->height, desc->layers, strerror(-err), gbuffer->handle);
George Burgess IV3a2e1ce2021-01-20 21:06:20 -080060 return err == 0 ? UNKNOWN_ERROR : err;
Mathias Agopian89ed4c82017-02-09 18:48:34 -080061 }
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
70void AHardwareBuffer_acquire(AHardwareBuffer* buffer) {
Mathias Agopian000879a2017-03-20 18:07:26 -070071 // incStrong/decStrong token must be the same, doesn't matter what it is
Mathias Agopian89ed4c82017-02-09 18:48:34 -080072 AHardwareBuffer_to_GraphicBuffer(buffer)->incStrong((void*)AHardwareBuffer_acquire);
73}
74
75void AHardwareBuffer_release(AHardwareBuffer* buffer) {
Mathias Agopian000879a2017-03-20 18:07:26 -070076 // incStrong/decStrong token must be the same, doesn't matter what it is
77 AHardwareBuffer_to_GraphicBuffer(buffer)->decStrong((void*)AHardwareBuffer_acquire);
Mathias Agopian89ed4c82017-02-09 18:48:34 -080078}
79
80void 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 Agopian07b920e2017-04-25 17:38:08 -070089 outDesc->format = AHardwareBuffer_convertFromPixelFormat(uint32_t(gbuffer->getPixelFormat()));
Mathias Agopian2c38b562017-04-20 16:35:39 -070090 outDesc->usage = AHardwareBuffer_convertFromGrallocUsageBits(gbuffer->getUsage());
Mathias Agopian07b920e2017-04-25 17:38:08 -070091 outDesc->stride = gbuffer->getStride();
92 outDesc->rfu0 = 0;
93 outDesc->rfu1 = 0;
Mathias Agopian89ed4c82017-02-09 18:48:34 -080094}
95
Valerie Haud2f4daf2019-02-15 13:49:00 -080096int AHardwareBuffer_lockAndGetInfo(AHardwareBuffer* buffer, uint64_t usage,
97 int32_t fence, const ARect* rect, void** outVirtualAddress,
98 int32_t* outBytesPerPixel, int32_t* outBytesPerStride) {
Valerie Hau12d2d642019-03-22 08:47:00 -070099 if (outBytesPerPixel) *outBytesPerPixel = -1;
100 if (outBytesPerStride) *outBytesPerStride = -1;
101
102 if (!buffer) {
103 return BAD_VALUE;
104 }
Valerie Haud2f4daf2019-02-15 13:49:00 -0800105
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 Hau12d2d642019-03-22 08:47:00 -0700135 int32_t bytesPerPixel;
136 int32_t bytesPerStride;
137 int result = gbuffer->lockAsync(usage, usage, bounds, outVirtualAddress, fence, &bytesPerPixel, &bytesPerStride);
Valerie Haud2f4daf2019-02-15 13:49:00 -0800138
139 // if hardware returns -1 for bytes per pixel or bytes per stride, we fail
140 // and unlock the buffer
Valerie Hau12d2d642019-03-22 08:47:00 -0700141 if (bytesPerPixel == -1 || bytesPerStride == -1) {
Valerie Haud2f4daf2019-02-15 13:49:00 -0800142 gbuffer->unlock();
143 return INVALID_OPERATION;
144 }
145
Valerie Hau12d2d642019-03-22 08:47:00 -0700146 if (outBytesPerPixel) *outBytesPerPixel = bytesPerPixel;
147 if (outBytesPerStride) *outBytesPerStride = bytesPerStride;
Valerie Haud2f4daf2019-02-15 13:49:00 -0800148 return result;
149}
150
Mathias Agopian2c38b562017-04-20 16:35:39 -0700151int AHardwareBuffer_lock(AHardwareBuffer* buffer, uint64_t usage,
Valerie Haud2f4daf2019-02-15 13:49:00 -0800152 int32_t fence, const ARect* rect, void** outVirtualAddress) {
153 int32_t bytesPerPixel;
154 int32_t bytesPerStride;
155
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800156 if (!buffer) return BAD_VALUE;
157
Mathias Agopian2c38b562017-04-20 16:35:39 -0700158 if (usage & ~(AHARDWAREBUFFER_USAGE_CPU_READ_MASK |
159 AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK)) {
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800160 ALOGE("Invalid usage flags passed to AHardwareBuffer_lock; only "
Krzysztof Kosińskia5c54bc2018-11-16 18:24:33 -0800161 "AHARDWAREBUFFER_USAGE_CPU_* flags are allowed");
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800162 return BAD_VALUE;
163 }
164
Mathias Agopian2c38b562017-04-20 16:35:39 -0700165 usage = AHardwareBuffer_convertToGrallocUsageBits(usage);
Krzysztof Kosińskia5c54bc2018-11-16 18:24:33 -0800166 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 Agopian89ed4c82017-02-09 18:48:34 -0800174 Rect bounds;
175 if (!rect) {
Krzysztof Kosińskia5c54bc2018-11-16 18:24:33 -0800176 bounds.set(Rect(gbuffer->getWidth(), gbuffer->getHeight()));
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800177 } else {
178 bounds.set(Rect(rect->left, rect->top, rect->right, rect->bottom));
179 }
Valerie Haud2f4daf2019-02-15 13:49:00 -0800180 return gbuffer->lockAsync(usage, usage, bounds, outVirtualAddress, fence, &bytesPerPixel, &bytesPerStride);
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800181}
182
renn4e9e48d2018-10-11 14:43:13 -0700183int 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;
Jason Macnak41209912022-02-16 14:44:44 -0800210 if (format == AHARDWAREBUFFER_FORMAT_YCbCr_P010) {
211 outPlanes->planes[0].pixelStride = 2;
212 } else {
213 outPlanes->planes[0].pixelStride = 1;
214 }
renn4e9e48d2018-10-11 14:43:13 -0700215 outPlanes->planes[0].rowStride = yuvData.ystride;
216 outPlanes->planes[1].data = yuvData.cb;
217 outPlanes->planes[1].pixelStride = yuvData.chroma_step;
218 outPlanes->planes[1].rowStride = yuvData.cstride;
219 outPlanes->planes[2].data = yuvData.cr;
220 outPlanes->planes[2].pixelStride = yuvData.chroma_step;
221 outPlanes->planes[2].rowStride = yuvData.cstride;
222 } else {
223 outPlanes->planeCount = 0;
224 }
225 return result;
226 } else {
227 const uint32_t pixelStride = AHardwareBuffer_bytesPerPixel(format);
228 outPlanes->planeCount = 1;
229 outPlanes->planes[0].pixelStride = pixelStride;
230 outPlanes->planes[0].rowStride = gBuffer->getStride() * pixelStride;
231 return gBuffer->lockAsync(usage, usage, bounds, &outPlanes->planes[0].data, fence);
232 }
233}
234
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800235int AHardwareBuffer_unlock(AHardwareBuffer* buffer, int32_t* fence) {
236 if (!buffer) return BAD_VALUE;
237
238 GraphicBuffer* gBuffer = AHardwareBuffer_to_GraphicBuffer(buffer);
Jiwen 'Steve' Caiae6955f2017-06-02 15:23:26 -0700239 if (fence == nullptr)
240 return gBuffer->unlock();
241 else
242 return gBuffer->unlockAsync(fence);
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800243}
244
Mathias Agopian000879a2017-03-20 18:07:26 -0700245int AHardwareBuffer_sendHandleToUnixSocket(const AHardwareBuffer* buffer, int socketFd) {
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800246 if (!buffer) return BAD_VALUE;
247 const GraphicBuffer* gBuffer = AHardwareBuffer_to_GraphicBuffer(buffer);
248
249 size_t flattenedSize = gBuffer->getFlattenedSize();
250 size_t fdCount = gBuffer->getFdCount();
251
252 std::unique_ptr<uint8_t[]> data(new uint8_t[flattenedSize]);
253 std::unique_ptr<int[]> fds(new int[fdCount]);
254
255 // Make copies of needed items since flatten modifies them, and we don't
256 // want to send anything if there's an error during flatten.
257 size_t flattenedSizeCopy = flattenedSize;
258 size_t fdCountCopy = fdCount;
259 void* dataStart = data.get();
260 int* fdsStart = fds.get();
261 status_t err = gBuffer->flatten(dataStart, flattenedSizeCopy, fdsStart,
262 fdCountCopy);
263 if (err != NO_ERROR) {
264 return err;
265 }
266
267 struct iovec iov[1];
268 iov[0].iov_base = data.get();
269 iov[0].iov_len = flattenedSize;
270
Craig Donner1b1d3002017-04-12 18:38:07 -0700271 char buf[CMSG_SPACE(kFdBufferSize)];
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800272 struct msghdr msg = {
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800273 .msg_iov = &iov[0],
274 .msg_iovlen = 1,
Nick Desaulniers57e41f52019-10-15 14:24:44 -0700275 .msg_control = buf,
276 .msg_controllen = sizeof(buf),
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800277 };
278
279 struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg);
280 cmsg->cmsg_level = SOL_SOCKET;
281 cmsg->cmsg_type = SCM_RIGHTS;
282 cmsg->cmsg_len = CMSG_LEN(sizeof(int) * fdCount);
283 int* fdData = reinterpret_cast<int*>(CMSG_DATA(cmsg));
284 memcpy(fdData, fds.get(), sizeof(int) * fdCount);
285 msg.msg_controllen = cmsg->cmsg_len;
286
Jesse Hall81d31972017-05-15 14:14:01 -0700287 int result;
288 do {
289 result = sendmsg(socketFd, &msg, 0);
290 } while (result == -1 && errno == EINTR);
291 if (result == -1) {
292 result = errno;
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800293 ALOGE("Error writing AHardwareBuffer to socket: error %#x (%s)",
Jesse Hall81d31972017-05-15 14:14:01 -0700294 result, strerror(result));
295 return -result;
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800296 }
Jesse Hall81d31972017-05-15 14:14:01 -0700297
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800298 return NO_ERROR;
299}
300
Mathias Agopian000879a2017-03-20 18:07:26 -0700301int AHardwareBuffer_recvHandleFromUnixSocket(int socketFd, AHardwareBuffer** outBuffer) {
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800302 if (!outBuffer) return BAD_VALUE;
303
Craig Donner1b1d3002017-04-12 18:38:07 -0700304 static constexpr int kMessageBufferSize = 4096 * sizeof(int);
305
306 std::unique_ptr<char[]> dataBuf(new char[kMessageBufferSize]);
307 char fdBuf[CMSG_SPACE(kFdBufferSize)];
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800308 struct iovec iov[1];
Craig Donner1b1d3002017-04-12 18:38:07 -0700309 iov[0].iov_base = dataBuf.get();
310 iov[0].iov_len = kMessageBufferSize;
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800311
312 struct msghdr msg = {
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800313 .msg_iov = &iov[0],
314 .msg_iovlen = 1,
Nick Desaulniers57e41f52019-10-15 14:24:44 -0700315 .msg_control = fdBuf,
316 .msg_controllen = sizeof(fdBuf),
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800317 };
318
Jesse Hall81d31972017-05-15 14:14:01 -0700319 int result;
320 do {
321 result = recvmsg(socketFd, &msg, 0);
322 } while (result == -1 && errno == EINTR);
323 if (result == -1) {
324 result = errno;
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800325 ALOGE("Error reading AHardwareBuffer from socket: error %#x (%s)",
Jesse Hall81d31972017-05-15 14:14:01 -0700326 result, strerror(result));
327 return -result;
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800328 }
329
330 if (msg.msg_iovlen != 1) {
331 ALOGE("Error reading AHardwareBuffer from socket: bad data length");
332 return INVALID_OPERATION;
333 }
334
335 if (msg.msg_controllen % sizeof(int) != 0) {
336 ALOGE("Error reading AHardwareBuffer from socket: bad fd length");
337 return INVALID_OPERATION;
338 }
339
340 size_t dataLen = msg.msg_iov[0].iov_len;
341 const void* data = static_cast<const void*>(msg.msg_iov[0].iov_base);
342 if (!data) {
343 ALOGE("Error reading AHardwareBuffer from socket: no buffer data");
344 return INVALID_OPERATION;
345 }
346
347 struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg);
348 if (!cmsg) {
349 ALOGE("Error reading AHardwareBuffer from socket: no fd header");
350 return INVALID_OPERATION;
351 }
352
353 size_t fdCount = msg.msg_controllen >> 2;
354 const int* fdData = reinterpret_cast<const int*>(CMSG_DATA(cmsg));
355 if (!fdData) {
356 ALOGE("Error reading AHardwareBuffer from socket: no fd data");
357 return INVALID_OPERATION;
358 }
359
360 GraphicBuffer* gBuffer = new GraphicBuffer();
361 status_t err = gBuffer->unflatten(data, dataLen, fdData, fdCount);
362 if (err != NO_ERROR) {
363 return err;
364 }
365 *outBuffer = AHardwareBuffer_from_GraphicBuffer(gBuffer);
366 // Ensure the buffer has a positive ref-count.
367 AHardwareBuffer_acquire(*outBuffer);
368
369 return NO_ERROR;
370}
371
Krzysztof Kosińskia5c54bc2018-11-16 18:24:33 -0800372int AHardwareBuffer_isSupported(const AHardwareBuffer_Desc* desc) {
373 if (!desc) return 0;
374 if (!AHardwareBuffer_isValidDescription(desc, /*log=*/false)) return 0;
375
Valerie Haua2414f02019-02-07 13:42:43 -0800376 bool supported = false;
377 GraphicBuffer* gBuffer = new GraphicBuffer();
378 status_t err = gBuffer->isSupported(desc->width, desc->height, desc->format, desc->layers,
379 desc->usage, &supported);
380
381 if (err == NO_ERROR) {
382 return supported;
383 }
384
385 // function isSupported is not implemented on device or an error occurred during HAL
386 // query. Make a trial allocation.
Krzysztof Kosińskia5c54bc2018-11-16 18:24:33 -0800387 AHardwareBuffer_Desc trialDesc = *desc;
388 trialDesc.width = 4;
389 trialDesc.height = desc->format == AHARDWAREBUFFER_FORMAT_BLOB ? 1 : 4;
Krzysztof Kosińskic4b9ce02019-01-02 16:27:05 -0800390 if (desc->usage & AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP) {
391 trialDesc.layers = desc->layers == 6 ? 6 : 12;
392 } else {
393 trialDesc.layers = desc->layers == 1 ? 1 : 2;
394 }
Krzysztof Kosińskia5c54bc2018-11-16 18:24:33 -0800395 AHardwareBuffer* trialBuffer = nullptr;
396 int result = AHardwareBuffer_allocate(&trialDesc, &trialBuffer);
397 if (result == NO_ERROR) {
398 AHardwareBuffer_release(trialBuffer);
399 return 1;
400 }
401 return 0;
402}
403
Yiwei Zhangead3d6e2020-08-11 16:12:50 -0700404int AHardwareBuffer_getId(const AHardwareBuffer* buffer, uint64_t* outId) {
405 if (!buffer || !outId) return BAD_VALUE;
406
407 const GraphicBuffer* gb = AHardwareBuffer_to_GraphicBuffer(buffer);
408 if (!gb) return BAD_VALUE;
409
410 *outId = gb->getId();
411
412 return OK;
413}
Jesse Hall1ee859f2017-04-17 08:52:32 -0700414
415// ----------------------------------------------------------------------------
416// VNDK functions
417// ----------------------------------------------------------------------------
418
419const native_handle_t* AHardwareBuffer_getNativeHandle(
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800420 const AHardwareBuffer* buffer) {
421 if (!buffer) return nullptr;
422 const GraphicBuffer* gbuffer = AHardwareBuffer_to_GraphicBuffer(buffer);
423 return gbuffer->handle;
424}
425
Jesse Hallfbe58352018-04-14 11:20:59 -0700426int AHardwareBuffer_createFromHandle(const AHardwareBuffer_Desc* desc,
427 const native_handle_t* handle, int32_t method,
428 AHardwareBuffer** outBuffer) {
429 static_assert(static_cast<int32_t>(AHARDWAREBUFFER_CREATE_FROM_HANDLE_METHOD_REGISTER) ==
430 static_cast<int32_t>(GraphicBuffer::TAKE_UNREGISTERED_HANDLE));
431 static_assert(static_cast<int32_t>(AHARDWAREBUFFER_CREATE_FROM_HANDLE_METHOD_CLONE) ==
432 static_cast<int32_t>(GraphicBuffer::CLONE_HANDLE));
433
434 if (!desc || !handle || !outBuffer) return BAD_VALUE;
435 if (!(method == AHARDWAREBUFFER_CREATE_FROM_HANDLE_METHOD_REGISTER ||
436 method == AHARDWAREBUFFER_CREATE_FROM_HANDLE_METHOD_CLONE))
437 return BAD_VALUE;
438 if (desc->rfu0 != 0 || desc->rfu1 != 0) return BAD_VALUE;
439 if (desc->format == AHARDWAREBUFFER_FORMAT_BLOB && desc->height != 1) return BAD_VALUE;
440
441 const int format = AHardwareBuffer_convertToPixelFormat(desc->format);
442 const uint64_t usage = AHardwareBuffer_convertToGrallocUsageBits(desc->usage);
443 const auto wrapMethod = static_cast<GraphicBuffer::HandleWrapMethod>(method);
444 sp<GraphicBuffer> gbuffer(new GraphicBuffer(handle, wrapMethod, desc->width, desc->height,
445 format, desc->layers, usage, desc->stride));
446 status_t err = gbuffer->initCheck();
447 if (err != 0 || gbuffer->handle == 0) return err;
448
449 *outBuffer = AHardwareBuffer_from_GraphicBuffer(gbuffer.get());
450 // Ensure the buffer doesn't get destroyed when the sp<> goes away.
451 AHardwareBuffer_acquire(*outBuffer);
452
453 return NO_ERROR;
454}
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800455
456// ----------------------------------------------------------------------------
457// Helpers implementation
458// ----------------------------------------------------------------------------
459
460namespace android {
461
Krzysztof Kosińskia5c54bc2018-11-16 18:24:33 -0800462bool AHardwareBuffer_isValidDescription(const AHardwareBuffer_Desc* desc, bool log) {
463 if (desc->width == 0 || desc->height == 0 || desc->layers == 0) {
464 ALOGE_IF(log, "Width, height and layers must all be nonzero");
465 return false;
466 }
Craig Donnera5a719e2017-02-24 16:02:08 -0800467
Krzysztof Kosińskia5c54bc2018-11-16 18:24:33 -0800468 if (!AHardwareBuffer_isValidPixelFormat(desc->format)) {
469 ALOGE_IF(log, "Invalid AHardwareBuffer pixel format %u (%#x))",
470 desc->format, desc->format);
471 return false;
472 }
473
474 if (desc->rfu0 != 0 || desc->rfu1 != 0) {
475 ALOGE_IF(log, "AHardwareBuffer_Desc::rfu fields must be 0");
476 return false;
477 }
478
479 if (desc->format == AHARDWAREBUFFER_FORMAT_BLOB) {
480 if (desc->height != 1 || desc->layers != 1) {
481 ALOGE_IF(log, "Height and layers must be 1 for AHARDWAREBUFFER_FORMAT_BLOB");
482 return false;
483 }
484 const uint64_t blobInvalidGpuMask =
485 AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
486 AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER |
487 AHARDWAREBUFFER_USAGE_GPU_MIPMAP_COMPLETE |
488 AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP;
489 if (desc->usage & blobInvalidGpuMask) {
490 ALOGE_IF(log, "Invalid GPU usage flag for AHARDWAREBUFFER_FORMAT_BLOB; "
491 "only AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER is allowed");
492 return false;
493 }
494 if (desc->usage & AHARDWAREBUFFER_USAGE_VIDEO_ENCODE) {
495 ALOGE_IF(log, "AHARDWAREBUFFER_FORMAT_BLOB cannot be encoded as video");
496 return false;
497 }
renn4e9e48d2018-10-11 14:43:13 -0700498 } else if (AHardwareBuffer_formatIsYuv(desc->format)) {
499 if (desc->layers != 1) {
500 ALOGE_IF(log, "Layers must be 1 for YUV formats.");
501 return false;
502 }
503 const uint64_t yuvInvalidGpuMask =
504 AHARDWAREBUFFER_USAGE_GPU_MIPMAP_COMPLETE |
505 AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP;
506 if (desc->usage & yuvInvalidGpuMask) {
507 ALOGE_IF(log, "Invalid usage flags specified for YUV format; "
508 "mip-mapping and cube-mapping are not allowed.");
509 return false;
510 }
Krzysztof Kosińskia5c54bc2018-11-16 18:24:33 -0800511 } else {
512 if (desc->usage & AHARDWAREBUFFER_USAGE_SENSOR_DIRECT_DATA) {
513 ALOGE_IF(log, "AHARDWAREBUFFER_USAGE_SENSOR_DIRECT_DATA requires AHARDWAREBUFFER_FORMAT_BLOB");
514 return false;
515 }
516 if (desc->usage & AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER) {
517 ALOGE_IF(log, "AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER requires AHARDWAREBUFFER_FORMAT_BLOB");
518 return false;
519 }
520 }
521
522 if ((desc->usage & (AHARDWAREBUFFER_USAGE_CPU_READ_MASK | AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK)) &&
523 (desc->usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT)) {
524 ALOGE_IF(log, "AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT requires AHARDWAREBUFFER_USAGE_CPU_READ_NEVER "
525 "and AHARDWAREBUFFER_USAGE_CPU_WRITE_NEVER");
526 return false;
527 }
528
529 if (desc->usage & AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP) {
530 if (desc->width != desc->height) {
531 ALOGE_IF(log, "Cube maps must be square");
532 return false;
533 }
534 if (desc->layers % 6 != 0) {
535 ALOGE_IF(log, "Cube map layers must be a multiple of 6");
536 return false;
537 }
538 }
539 return true;
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800540}
541
Mathias Agopian3a11413a2017-06-08 19:44:17 -0700542bool AHardwareBuffer_isValidPixelFormat(uint32_t format) {
543 static_assert(HAL_PIXEL_FORMAT_RGBA_8888 == AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
544 "HAL and AHardwareBuffer pixel format don't match");
545 static_assert(HAL_PIXEL_FORMAT_RGBX_8888 == AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM,
546 "HAL and AHardwareBuffer pixel format don't match");
547 static_assert(HAL_PIXEL_FORMAT_RGB_565 == AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM,
548 "HAL and AHardwareBuffer pixel format don't match");
549 static_assert(HAL_PIXEL_FORMAT_RGB_888 == AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM,
550 "HAL and AHardwareBuffer pixel format don't match");
551 static_assert(HAL_PIXEL_FORMAT_RGBA_FP16 == AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT,
552 "HAL and AHardwareBuffer pixel format don't match");
553 static_assert(HAL_PIXEL_FORMAT_RGBA_1010102 == AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM,
554 "HAL and AHardwareBuffer pixel format don't match");
555 static_assert(HAL_PIXEL_FORMAT_BLOB == AHARDWAREBUFFER_FORMAT_BLOB,
556 "HAL and AHardwareBuffer pixel format don't match");
Craig Donnere6ecb922017-12-27 14:59:29 -0800557 static_assert(HAL_PIXEL_FORMAT_DEPTH_16 == AHARDWAREBUFFER_FORMAT_D16_UNORM,
558 "HAL and AHardwareBuffer pixel format don't match");
559 static_assert(HAL_PIXEL_FORMAT_DEPTH_24 == AHARDWAREBUFFER_FORMAT_D24_UNORM,
560 "HAL and AHardwareBuffer pixel format don't match");
561 static_assert(HAL_PIXEL_FORMAT_DEPTH_24_STENCIL_8 == AHARDWAREBUFFER_FORMAT_D24_UNORM_S8_UINT,
562 "HAL and AHardwareBuffer pixel format don't match");
563 static_assert(HAL_PIXEL_FORMAT_DEPTH_32F == AHARDWAREBUFFER_FORMAT_D32_FLOAT,
564 "HAL and AHardwareBuffer pixel format don't match");
565 static_assert(HAL_PIXEL_FORMAT_DEPTH_32F_STENCIL_8 == AHARDWAREBUFFER_FORMAT_D32_FLOAT_S8_UINT,
566 "HAL and AHardwareBuffer pixel format don't match");
567 static_assert(HAL_PIXEL_FORMAT_STENCIL_8 == AHARDWAREBUFFER_FORMAT_S8_UINT,
568 "HAL and AHardwareBuffer pixel format don't match");
Mathias Agopian3a11413a2017-06-08 19:44:17 -0700569 static_assert(HAL_PIXEL_FORMAT_BGRA_8888 == AHARDWAREBUFFER_FORMAT_B8G8R8A8_UNORM,
570 "HAL and AHardwareBuffer pixel format don't match");
571 static_assert(HAL_PIXEL_FORMAT_YV12 == AHARDWAREBUFFER_FORMAT_YV12,
572 "HAL and AHardwareBuffer pixel format don't match");
573 static_assert(HAL_PIXEL_FORMAT_Y8 == AHARDWAREBUFFER_FORMAT_Y8,
574 "HAL and AHardwareBuffer pixel format don't match");
575 static_assert(HAL_PIXEL_FORMAT_Y16 == AHARDWAREBUFFER_FORMAT_Y16,
576 "HAL and AHardwareBuffer pixel format don't match");
577 static_assert(HAL_PIXEL_FORMAT_RAW16 == AHARDWAREBUFFER_FORMAT_RAW16,
578 "HAL and AHardwareBuffer pixel format don't match");
579 static_assert(HAL_PIXEL_FORMAT_RAW10 == AHARDWAREBUFFER_FORMAT_RAW10,
580 "HAL and AHardwareBuffer pixel format don't match");
581 static_assert(HAL_PIXEL_FORMAT_RAW12 == AHARDWAREBUFFER_FORMAT_RAW12,
582 "HAL and AHardwareBuffer pixel format don't match");
583 static_assert(HAL_PIXEL_FORMAT_RAW_OPAQUE == AHARDWAREBUFFER_FORMAT_RAW_OPAQUE,
584 "HAL and AHardwareBuffer pixel format don't match");
585 static_assert(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED == AHARDWAREBUFFER_FORMAT_IMPLEMENTATION_DEFINED,
586 "HAL and AHardwareBuffer pixel format don't match");
587 static_assert(HAL_PIXEL_FORMAT_YCBCR_420_888 == AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420,
588 "HAL and AHardwareBuffer pixel format don't match");
Mathias Agopian3a11413a2017-06-08 19:44:17 -0700589 static_assert(HAL_PIXEL_FORMAT_YCBCR_422_SP == AHARDWAREBUFFER_FORMAT_YCbCr_422_SP,
590 "HAL and AHardwareBuffer pixel format don't match");
591 static_assert(HAL_PIXEL_FORMAT_YCRCB_420_SP == AHARDWAREBUFFER_FORMAT_YCrCb_420_SP,
592 "HAL and AHardwareBuffer pixel format don't match");
593 static_assert(HAL_PIXEL_FORMAT_YCBCR_422_I == AHARDWAREBUFFER_FORMAT_YCbCr_422_I,
594 "HAL and AHardwareBuffer pixel format don't match");
Jason Macnak263d41c2022-02-11 16:18:11 -0800595 static_assert(HAL_PIXEL_FORMAT_YCBCR_P010 == AHARDWAREBUFFER_FORMAT_YCbCr_P010,
596 "HAL and AHardwareBuffer pixel format don't match");
Mathias Agopian3a11413a2017-06-08 19:44:17 -0700597
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800598 switch (format) {
Mathias Agopian3a11413a2017-06-08 19:44:17 -0700599 case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM:
600 case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM:
601 case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM:
602 case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM:
603 case AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT:
604 case AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM:
605 case AHARDWAREBUFFER_FORMAT_BLOB:
Craig Donnere6ecb922017-12-27 14:59:29 -0800606 case AHARDWAREBUFFER_FORMAT_D16_UNORM:
607 case AHARDWAREBUFFER_FORMAT_D24_UNORM:
608 case AHARDWAREBUFFER_FORMAT_D24_UNORM_S8_UINT:
609 case AHARDWAREBUFFER_FORMAT_D32_FLOAT:
610 case AHARDWAREBUFFER_FORMAT_D32_FLOAT_S8_UINT:
611 case AHARDWAREBUFFER_FORMAT_S8_UINT:
renn4e9e48d2018-10-11 14:43:13 -0700612 case AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420:
Mathias Agopian3a11413a2017-06-08 19:44:17 -0700613 // VNDK formats only -- unfortunately we can't differentiate from where we're called
614 case AHARDWAREBUFFER_FORMAT_B8G8R8A8_UNORM:
615 case AHARDWAREBUFFER_FORMAT_YV12:
616 case AHARDWAREBUFFER_FORMAT_Y8:
617 case AHARDWAREBUFFER_FORMAT_Y16:
618 case AHARDWAREBUFFER_FORMAT_RAW16:
619 case AHARDWAREBUFFER_FORMAT_RAW10:
620 case AHARDWAREBUFFER_FORMAT_RAW12:
621 case AHARDWAREBUFFER_FORMAT_RAW_OPAQUE:
622 case AHARDWAREBUFFER_FORMAT_IMPLEMENTATION_DEFINED:
Mathias Agopian3a11413a2017-06-08 19:44:17 -0700623 case AHARDWAREBUFFER_FORMAT_YCbCr_422_SP:
624 case AHARDWAREBUFFER_FORMAT_YCrCb_420_SP:
625 case AHARDWAREBUFFER_FORMAT_YCbCr_422_I:
Jason Macnak263d41c2022-02-11 16:18:11 -0800626 case AHARDWAREBUFFER_FORMAT_YCbCr_P010:
Mathias Agopian3a11413a2017-06-08 19:44:17 -0700627 return true;
628
629 default:
630 return false;
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800631 }
632}
633
renn4e9e48d2018-10-11 14:43:13 -0700634bool AHardwareBuffer_formatIsYuv(uint32_t format) {
635 switch (format) {
636 case AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420:
637 case AHARDWAREBUFFER_FORMAT_YV12:
638 case AHARDWAREBUFFER_FORMAT_Y8:
639 case AHARDWAREBUFFER_FORMAT_Y16:
640 case AHARDWAREBUFFER_FORMAT_YCbCr_422_SP:
641 case AHARDWAREBUFFER_FORMAT_YCrCb_420_SP:
642 case AHARDWAREBUFFER_FORMAT_YCbCr_422_I:
Jason Macnak263d41c2022-02-11 16:18:11 -0800643 case AHARDWAREBUFFER_FORMAT_YCbCr_P010:
renn4e9e48d2018-10-11 14:43:13 -0700644 return true;
645 default:
646 return false;
647 }
648}
649
650uint32_t AHardwareBuffer_bytesPerPixel(uint32_t format) {
651 switch (format) {
652 case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM:
653 case AHARDWAREBUFFER_FORMAT_D16_UNORM:
654 return 2;
655 case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM:
656 case AHARDWAREBUFFER_FORMAT_D24_UNORM:
657 return 3;
658 case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM:
659 case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM:
660 case AHARDWAREBUFFER_FORMAT_D32_FLOAT:
661 case AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM:
662 case AHARDWAREBUFFER_FORMAT_D24_UNORM_S8_UINT:
663 return 4;
Jason Macnaka0efa842021-07-08 13:08:14 -0700664 case AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT:
665 return 8;
renn4e9e48d2018-10-11 14:43:13 -0700666 default:
667 return 0;
668 }
669}
670
Mathias Agopian3a11413a2017-06-08 19:44:17 -0700671uint32_t AHardwareBuffer_convertFromPixelFormat(uint32_t hal_format) {
672 return hal_format;
673}
674
675uint32_t AHardwareBuffer_convertToPixelFormat(uint32_t ahardwarebuffer_format) {
676 return ahardwarebuffer_format;
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800677}
678
Mathias Agopian2c38b562017-04-20 16:35:39 -0700679uint64_t AHardwareBuffer_convertToGrallocUsageBits(uint64_t usage) {
Craig Donnere6ecb922017-12-27 14:59:29 -0800680 using android::hardware::graphics::common::V1_1::BufferUsage;
Mathias Agopian2c38b562017-04-20 16:35:39 -0700681 static_assert(AHARDWAREBUFFER_USAGE_CPU_READ_NEVER == (uint64_t)BufferUsage::CPU_READ_NEVER,
682 "gralloc and AHardwareBuffer flags don't match");
683 static_assert(AHARDWAREBUFFER_USAGE_CPU_READ_RARELY == (uint64_t)BufferUsage::CPU_READ_RARELY,
684 "gralloc and AHardwareBuffer flags don't match");
685 static_assert(AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN == (uint64_t)BufferUsage::CPU_READ_OFTEN,
686 "gralloc and AHardwareBuffer flags don't match");
687 static_assert(AHARDWAREBUFFER_USAGE_CPU_WRITE_NEVER == (uint64_t)BufferUsage::CPU_WRITE_NEVER,
688 "gralloc and AHardwareBuffer flags don't match");
689 static_assert(AHARDWAREBUFFER_USAGE_CPU_WRITE_RARELY == (uint64_t)BufferUsage::CPU_WRITE_RARELY,
690 "gralloc and AHardwareBuffer flags don't match");
691 static_assert(AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN == (uint64_t)BufferUsage::CPU_WRITE_OFTEN,
692 "gralloc and AHardwareBuffer flags don't match");
693 static_assert(AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE == (uint64_t)BufferUsage::GPU_TEXTURE,
694 "gralloc and AHardwareBuffer flags don't match");
Krzysztof Kosińskia5c54bc2018-11-16 18:24:33 -0800695 static_assert(AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER == (uint64_t)BufferUsage::GPU_RENDER_TARGET,
Mathias Agopian2c38b562017-04-20 16:35:39 -0700696 "gralloc and AHardwareBuffer flags don't match");
697 static_assert(AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT == (uint64_t)BufferUsage::PROTECTED,
698 "gralloc and AHardwareBuffer flags don't match");
699 static_assert(AHARDWAREBUFFER_USAGE_VIDEO_ENCODE == (uint64_t)BufferUsage::VIDEO_ENCODER,
700 "gralloc and AHardwareBuffer flags don't match");
701 static_assert(AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER == (uint64_t)BufferUsage::GPU_DATA_BUFFER,
702 "gralloc and AHardwareBuffer flags don't match");
703 static_assert(AHARDWAREBUFFER_USAGE_SENSOR_DIRECT_DATA == (uint64_t)BufferUsage::SENSOR_DIRECT_DATA,
704 "gralloc and AHardwareBuffer flags don't match");
Craig Donnere6ecb922017-12-27 14:59:29 -0800705 static_assert(AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP == (uint64_t)BufferUsage::GPU_CUBE_MAP,
706 "gralloc and AHardwareBuffer flags don't match");
707 static_assert(AHARDWAREBUFFER_USAGE_GPU_MIPMAP_COMPLETE == (uint64_t)BufferUsage::GPU_MIPMAP_COMPLETE,
708 "gralloc and AHardwareBuffer flags don't match");
Yiwei Zhangc7a9d3a2021-06-18 20:53:12 +0000709 static_assert(AHARDWAREBUFFER_USAGE_CAMERA_WRITE == (uint64_t)BufferUsage::CAMERA_OUTPUT,
710 "gralloc and AHardwareBuffer flags don't match");
711 static_assert(AHARDWAREBUFFER_USAGE_CAMERA_READ == (uint64_t)BufferUsage::CAMERA_INPUT,
712 "gralloc and AHardwareBuffer flags don't match");
Mathias Agopian2c38b562017-04-20 16:35:39 -0700713 return usage;
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800714}
715
Mathias Agopian2c38b562017-04-20 16:35:39 -0700716uint64_t AHardwareBuffer_convertFromGrallocUsageBits(uint64_t usage) {
717 return usage;
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800718}
719
720const GraphicBuffer* AHardwareBuffer_to_GraphicBuffer(const AHardwareBuffer* buffer) {
Pawin Vongmasae672cd02019-02-14 16:01:29 -0800721 return GraphicBuffer::fromAHardwareBuffer(buffer);
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800722}
723
724GraphicBuffer* AHardwareBuffer_to_GraphicBuffer(AHardwareBuffer* buffer) {
Pawin Vongmasae672cd02019-02-14 16:01:29 -0800725 return GraphicBuffer::fromAHardwareBuffer(buffer);
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800726}
727
Mathias Agopian61963402017-02-24 16:38:15 -0800728const ANativeWindowBuffer* AHardwareBuffer_to_ANativeWindowBuffer(const AHardwareBuffer* buffer) {
729 return AHardwareBuffer_to_GraphicBuffer(buffer)->getNativeBuffer();
730}
731
732ANativeWindowBuffer* AHardwareBuffer_to_ANativeWindowBuffer(AHardwareBuffer* buffer) {
733 return AHardwareBuffer_to_GraphicBuffer(buffer)->getNativeBuffer();
734}
735
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800736AHardwareBuffer* AHardwareBuffer_from_GraphicBuffer(GraphicBuffer* buffer) {
Pawin Vongmasae672cd02019-02-14 16:01:29 -0800737 return buffer->toAHardwareBuffer();
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800738}
739
740} // namespace android