blob: 80c5ec2c572dc3859e8fe6a5b9aec40e0f002b46 [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>
Mathias Agopian2c38b562017-04-20 16:35:39 -070032#include <android/hardware/graphics/common/1.0/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) {
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 Agopian07b920e2017-04-25 17:38:08 -070053 if (desc->rfu0 != 0 || desc->rfu1 != 0) {
54 ALOGE("AHardwareBuffer_Desc::rfu fields must be 0");
Mathias Agopian2c38b562017-04-20 16:35:39 -070055 return BAD_VALUE;
56 }
57
Mathias Agopian89ed4c82017-02-09 18:48:34 -080058 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 Agopian2c38b562017-04-20 16:35:39 -070063 uint64_t usage = AHardwareBuffer_convertToGrallocUsageBits(desc->usage);
Mathias Agopian89ed4c82017-02-09 18:48:34 -080064 sp<GraphicBuffer> gbuffer(new GraphicBuffer(
Chris Forbesd4106302017-04-20 12:43:04 -070065 desc->width, desc->height, format, desc->layers, usage,
Mathias Agopian89ed4c82017-02-09 18:48:34 -080066 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 Agopian0556d792017-03-22 15:49:32 -070073 ALOGE("GraphicBuffer(w=%u, h=%u, lc=%u) failed (%s), handle=%p",
Mathias Agopian89ed4c82017-02-09 18:48:34 -080074 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
85void AHardwareBuffer_acquire(AHardwareBuffer* buffer) {
Mathias Agopian000879a2017-03-20 18:07:26 -070086 // incStrong/decStrong token must be the same, doesn't matter what it is
Mathias Agopian89ed4c82017-02-09 18:48:34 -080087 AHardwareBuffer_to_GraphicBuffer(buffer)->incStrong((void*)AHardwareBuffer_acquire);
88}
89
90void AHardwareBuffer_release(AHardwareBuffer* buffer) {
Mathias Agopian000879a2017-03-20 18:07:26 -070091 // incStrong/decStrong token must be the same, doesn't matter what it is
92 AHardwareBuffer_to_GraphicBuffer(buffer)->decStrong((void*)AHardwareBuffer_acquire);
Mathias Agopian89ed4c82017-02-09 18:48:34 -080093}
94
95void 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 Agopian07b920e2017-04-25 17:38:08 -0700104 outDesc->format = AHardwareBuffer_convertFromPixelFormat(uint32_t(gbuffer->getPixelFormat()));
Mathias Agopian2c38b562017-04-20 16:35:39 -0700105 outDesc->usage = AHardwareBuffer_convertFromGrallocUsageBits(gbuffer->getUsage());
Mathias Agopian07b920e2017-04-25 17:38:08 -0700106 outDesc->stride = gbuffer->getStride();
107 outDesc->rfu0 = 0;
108 outDesc->rfu1 = 0;
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800109}
110
Mathias Agopian2c38b562017-04-20 16:35:39 -0700111int AHardwareBuffer_lock(AHardwareBuffer* buffer, uint64_t usage,
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800112 int32_t fence, const ARect* rect, void** outVirtualAddress) {
113 if (!buffer) return BAD_VALUE;
114
Mathias Agopian2c38b562017-04-20 16:35:39 -0700115 if (usage & ~(AHARDWAREBUFFER_USAGE_CPU_READ_MASK |
116 AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK)) {
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800117 ALOGE("Invalid usage flags passed to AHardwareBuffer_lock; only "
Mathias Agopian2c38b562017-04-20 16:35:39 -0700118 " AHARDWAREBUFFER_USAGE_CPU_* flags are allowed");
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800119 return BAD_VALUE;
120 }
121
Mathias Agopian2c38b562017-04-20 16:35:39 -0700122 usage = AHardwareBuffer_convertToGrallocUsageBits(usage);
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800123 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 Agopian2c38b562017-04-20 16:35:39 -0700130 return gBuffer->lockAsync(usage, usage, bounds, outVirtualAddress, fence);
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800131}
132
133int 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 Agopian000879a2017-03-20 18:07:26 -0700140int AHardwareBuffer_sendHandleToUnixSocket(const AHardwareBuffer* buffer, int socketFd) {
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800141 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 Donner1b1d3002017-04-12 18:38:07 -0700166 char buf[CMSG_SPACE(kFdBufferSize)];
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800167 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 Agopian000879a2017-03-20 18:07:26 -0700191int AHardwareBuffer_recvHandleFromUnixSocket(int socketFd, AHardwareBuffer** outBuffer) {
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800192 if (!outBuffer) return BAD_VALUE;
193
Craig Donner1b1d3002017-04-12 18:38:07 -0700194 static constexpr int kMessageBufferSize = 4096 * sizeof(int);
195
196 std::unique_ptr<char[]> dataBuf(new char[kMessageBufferSize]);
197 char fdBuf[CMSG_SPACE(kFdBufferSize)];
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800198 struct iovec iov[1];
Craig Donner1b1d3002017-04-12 18:38:07 -0700199 iov[0].iov_base = dataBuf.get();
200 iov[0].iov_len = kMessageBufferSize;
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800201
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 Hall1ee859f2017-04-17 08:52:32 -0700258
259// ----------------------------------------------------------------------------
260// VNDK functions
261// ----------------------------------------------------------------------------
262
263const native_handle_t* AHardwareBuffer_getNativeHandle(
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800264 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
275namespace android {
276
Craig Donnera5a719e2017-02-24 16:02:08 -0800277// A 1:1 mapping of AHardwaqreBuffer bitmasks to gralloc1 bitmasks.
278struct UsageMaskMapping {
279 uint64_t hardwareBufferMask;
280 uint64_t grallocMask;
281};
282
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800283static inline bool containsBits(uint64_t mask, uint64_t bitsToCheck) {
Craig Donnera5a719e2017-02-24 16:02:08 -0800284 return (mask & bitsToCheck) == bitsToCheck && bitsToCheck;
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800285}
286
287uint32_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 Hall577aa752017-04-19 15:43:02 -0700293 case HAL_PIXEL_FORMAT_RGBA_FP16: return AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT;
294 case HAL_PIXEL_FORMAT_RGBA_1010102: return AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM;
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800295 case HAL_PIXEL_FORMAT_BLOB: return AHARDWAREBUFFER_FORMAT_BLOB;
296 default:ALOGE("Unknown pixel format %u", format);
297 return 0;
298 }
299}
300
301uint32_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 Hall577aa752017-04-19 15:43:02 -0700307 case AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT: return HAL_PIXEL_FORMAT_RGBA_FP16;
308 case AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM: return HAL_PIXEL_FORMAT_RGBA_1010102;
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800309 case AHARDWAREBUFFER_FORMAT_BLOB: return HAL_PIXEL_FORMAT_BLOB;
310 default:ALOGE("Unknown AHardwareBuffer format %u", format);
311 return 0;
312 }
313}
314
Mathias Agopian2c38b562017-04-20 16:35:39 -0700315uint64_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 Agopian89ed4c82017-02-09 18:48:34 -0800342}
343
Mathias Agopian2c38b562017-04-20 16:35:39 -0700344uint64_t AHardwareBuffer_convertFromGrallocUsageBits(uint64_t usage) {
345 return usage;
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800346}
347
348const GraphicBuffer* AHardwareBuffer_to_GraphicBuffer(const AHardwareBuffer* buffer) {
349 return reinterpret_cast<const GraphicBuffer*>(buffer);
350}
351
352GraphicBuffer* AHardwareBuffer_to_GraphicBuffer(AHardwareBuffer* buffer) {
353 return reinterpret_cast<GraphicBuffer*>(buffer);
354}
355
Mathias Agopian61963402017-02-24 16:38:15 -0800356const ANativeWindowBuffer* AHardwareBuffer_to_ANativeWindowBuffer(const AHardwareBuffer* buffer) {
357 return AHardwareBuffer_to_GraphicBuffer(buffer)->getNativeBuffer();
358}
359
360ANativeWindowBuffer* AHardwareBuffer_to_ANativeWindowBuffer(AHardwareBuffer* buffer) {
361 return AHardwareBuffer_to_GraphicBuffer(buffer)->getNativeBuffer();
362}
363
Mathias Agopian89ed4c82017-02-09 18:48:34 -0800364AHardwareBuffer* AHardwareBuffer_from_GraphicBuffer(GraphicBuffer* buffer) {
365 return reinterpret_cast<AHardwareBuffer*>(buffer);
366}
367
368} // namespace android