forked from robherring/gbm_gralloc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gralloc_gbm.cpp
524 lines (433 loc) · 12.2 KB
/
gralloc_gbm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/*
* Copyright (C) 2010-2011 Chia-I Wu <[email protected]>
* Copyright (C) 2010-2011 LunarG Inc.
* Copyright (C) 2016 Linaro, Ltd., Rob Herring <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#define LOG_TAG "GRALLOC-GBM"
#include <log/log.h>
#include <cutils/atomic.h>
#include <cutils/properties.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include <hardware/gralloc.h>
#include <system/graphics.h>
#include <gbm.h>
#include "gralloc_gbm_priv.h"
#include <android/gralloc_handle.h>
#include <unordered_map>
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define unlikely(x) __builtin_expect(!!(x), 0)
static std::unordered_map<buffer_handle_t, struct gbm_bo *> gbm_bo_handle_map;
struct bo_data_t {
void *map_data;
int lock_count;
int locked_for;
};
void gralloc_gbm_destroy_user_data(struct gbm_bo *bo, void *data)
{
struct bo_data_t *bo_data = (struct bo_data_t *)data;
delete bo_data;
(void)bo;
}
static struct bo_data_t *gbm_bo_data(struct gbm_bo *bo) {
return (struct bo_data_t *)gbm_bo_get_user_data(bo);
}
static uint32_t get_gbm_format(int format)
{
uint32_t fmt;
switch (format) {
case HAL_PIXEL_FORMAT_RGBA_8888:
fmt = GBM_FORMAT_ABGR8888;
break;
case HAL_PIXEL_FORMAT_RGBX_8888:
fmt = GBM_FORMAT_XBGR8888;
break;
case HAL_PIXEL_FORMAT_RGB_888:
fmt = GBM_FORMAT_RGB888;
break;
case HAL_PIXEL_FORMAT_RGB_565:
fmt = GBM_FORMAT_RGB565;
break;
case HAL_PIXEL_FORMAT_BGRA_8888:
fmt = GBM_FORMAT_ARGB8888;
break;
case HAL_PIXEL_FORMAT_YV12:
/* YV12 is planar, but must be a single buffer so ask for GR88 */
fmt = GBM_FORMAT_GR88;
break;
case HAL_PIXEL_FORMAT_RGBA_FP16:
fmt = GBM_FORMAT_ABGR16161616F;
break;
case HAL_PIXEL_FORMAT_RGBA_1010102:
fmt = GBM_FORMAT_ABGR2101010;
break;
case HAL_PIXEL_FORMAT_YCbCr_422_SP:
case HAL_PIXEL_FORMAT_YCrCb_420_SP:
default:
fmt = 0;
break;
}
return fmt;
}
static int gralloc_gbm_get_bpp(int format)
{
int bpp;
switch (format) {
case HAL_PIXEL_FORMAT_RGBA_FP16:
bpp = 8;
break;
case HAL_PIXEL_FORMAT_RGBA_8888:
case HAL_PIXEL_FORMAT_RGBX_8888:
case HAL_PIXEL_FORMAT_BGRA_8888:
case HAL_PIXEL_FORMAT_RGBA_1010102:
bpp = 4;
break;
case HAL_PIXEL_FORMAT_RGB_888:
bpp = 3;
break;
case HAL_PIXEL_FORMAT_RGB_565:
case HAL_PIXEL_FORMAT_YCbCr_422_I:
bpp = 2;
break;
/* planar; only Y is considered */
case HAL_PIXEL_FORMAT_YV12:
case HAL_PIXEL_FORMAT_YCbCr_422_SP:
case HAL_PIXEL_FORMAT_YCrCb_420_SP:
bpp = 1;
break;
default:
bpp = 0;
break;
}
return bpp;
}
static unsigned int get_pipe_bind(int usage)
{
unsigned int bind = 0;
if (usage & (GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN))
bind |= GBM_BO_USE_LINEAR;
if (usage & GRALLOC_USAGE_CURSOR)
;//bind |= GBM_BO_USE_CURSOR;
if (usage & (GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE))
bind |= GBM_BO_USE_RENDERING;
if (usage & GRALLOC_USAGE_HW_FB)
bind |= GBM_BO_USE_SCANOUT;
if (usage & GRALLOC_USAGE_HW_COMPOSER)
bind |= GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING;
return bind;
}
static struct gbm_bo *gbm_import(struct gbm_device *gbm,
buffer_handle_t _handle)
{
struct gbm_bo *bo;
struct gralloc_handle_t *handle = gralloc_handle(_handle);
#ifdef GBM_BO_IMPORT_FD_MODIFIER
struct gbm_import_fd_modifier_data data;
#else
struct gbm_import_fd_data data;
#endif
int format = get_gbm_format(handle->format);
if (handle->prime_fd < 0)
return NULL;
memset(&data, 0, sizeof(data));
data.width = handle->width;
data.height = handle->height;
data.format = format;
/* Adjust the width and height for a GBM GR88 buffer */
if (handle->format == HAL_PIXEL_FORMAT_YV12) {
data.width /= 2;
data.height += handle->height / 2;
}
#ifdef GBM_BO_IMPORT_FD_MODIFIER
data.num_fds = 1;
data.fds[0] = handle->prime_fd;
data.strides[0] = handle->stride;
data.modifier = handle->modifier;
bo = gbm_bo_import(gbm, GBM_BO_IMPORT_FD_MODIFIER, &data, 0);
#else
data.fd = handle->prime_fd;
data.stride = handle->stride;
bo = gbm_bo_import(gbm, GBM_BO_IMPORT_FD, &data, 0);
#endif
return bo;
}
static struct gbm_bo *gbm_alloc(struct gbm_device *gbm,
buffer_handle_t _handle)
{
struct gbm_bo *bo;
struct gralloc_handle_t *handle = gralloc_handle(_handle);
int format = get_gbm_format(handle->format);
int usage = get_pipe_bind(handle->usage);
int width, height;
width = handle->width;
height = handle->height;
if (usage & GBM_BO_USE_CURSOR) {
if (handle->width < 64)
width = 64;
if (handle->height < 64)
height = 64;
}
/*
* For YV12, we request GR88, so halve the width since we're getting
* 16bpp. Then increase the height by 1.5 for the U and V planes.
*/
if (handle->format == HAL_PIXEL_FORMAT_YV12) {
width /= 2;
height += handle->height / 2;
}
ALOGV("create BO, size=%dx%d, fmt=%d, usage=%x",
handle->width, handle->height, handle->format, usage);
bo = gbm_bo_create(gbm, width, height, format, usage);
if (!bo) {
ALOGE("failed to create BO, size=%dx%d, fmt=%d, usage=%x",
handle->width, handle->height, handle->format, usage);
return NULL;
}
handle->prime_fd = gbm_bo_get_fd(bo);
handle->stride = gbm_bo_get_stride(bo);
#ifdef GBM_BO_IMPORT_FD_MODIFIER
handle->modifier = gbm_bo_get_modifier(bo);
#endif
return bo;
}
void gbm_free(buffer_handle_t handle)
{
struct gbm_bo *bo = gralloc_gbm_bo_from_handle(handle);
if (!bo)
return;
gbm_bo_handle_map.erase(handle);
gbm_bo_destroy(bo);
}
/*
* Return the bo of a registered handle.
*/
struct gbm_bo *gralloc_gbm_bo_from_handle(buffer_handle_t handle)
{
return gbm_bo_handle_map[handle];
}
static int gbm_map(buffer_handle_t handle, int enable_write, void **addr)
{
int err = 0;
int flags = GBM_BO_TRANSFER_READ;
struct gbm_bo *bo = gralloc_gbm_bo_from_handle(handle);
struct bo_data_t *bo_data = gbm_bo_data(bo);
uint32_t stride;
if (bo_data->map_data)
return -EINVAL;
if (enable_write)
flags |= GBM_BO_TRANSFER_WRITE;
*addr = gbm_bo_map(bo, 0, 0, gbm_bo_get_width(bo), gbm_bo_get_height(bo),
flags, &stride, &bo_data->map_data);
ALOGV("mapped bo %p at %p", bo, *addr);
if (*addr == NULL)
return -ENOMEM;
assert(stride == gbm_bo_get_stride(bo));
return err;
}
static void gbm_unmap(struct gbm_bo *bo)
{
struct bo_data_t *bo_data = gbm_bo_data(bo);
gbm_bo_unmap(bo, bo_data->map_data);
bo_data->map_data = NULL;
}
void gbm_dev_destroy(struct gbm_device *gbm)
{
int fd = gbm_device_get_fd(gbm);
gbm_device_destroy(gbm);
close(fd);
}
struct gbm_device *gbm_dev_create(void)
{
struct gbm_device *gbm;
char path[PROPERTY_VALUE_MAX];
int fd;
property_get("gralloc.gbm.device", path, "/dev/dri/renderD128");
fd = open(path, O_RDWR | O_CLOEXEC);
if (fd < 0) {
ALOGE("failed to open %s", path);
return NULL;
}
gbm = gbm_create_device(fd);
if (!gbm) {
ALOGE("failed to create gbm device");
close(fd);
}
return gbm;
}
/*
* Register a buffer handle.
*/
int gralloc_gbm_handle_register(buffer_handle_t _handle, struct gbm_device *gbm)
{
struct gbm_bo *bo;
if (!_handle)
return -EINVAL;
if (gbm_bo_handle_map.count(_handle))
return -EINVAL;
bo = gbm_import(gbm, _handle);
if (!bo)
return -EINVAL;
gbm_bo_handle_map.emplace(_handle, bo);
return 0;
}
/*
* Unregister a buffer handle. It is no-op for handles created locally.
*/
int gralloc_gbm_handle_unregister(buffer_handle_t handle)
{
gbm_free(handle);
return 0;
}
/*
* Create a bo.
*/
buffer_handle_t gralloc_gbm_bo_create(struct gbm_device *gbm,
int width, int height, int format, int usage, int *stride)
{
struct gbm_bo *bo;
native_handle_t *handle;
handle = gralloc_handle_create(width, height, format, usage);
if (!handle)
return NULL;
bo = gbm_alloc(gbm, handle);
if (!bo) {
native_handle_delete(handle);
return NULL;
}
gbm_bo_handle_map.emplace(handle, bo);
/* in pixels */
*stride = gralloc_handle(handle)->stride / gralloc_gbm_get_bpp(format);
return handle;
}
/*
* Lock a bo. XXX thread-safety?
*/
int gralloc_gbm_bo_lock(buffer_handle_t handle,
int usage, int /*x*/, int /*y*/, int /*w*/, int /*h*/,
void **addr)
{
struct gralloc_handle_t *gbm_handle = gralloc_handle(handle);
struct gbm_bo *bo = gralloc_gbm_bo_from_handle(handle);
struct bo_data_t *bo_data;
if (!bo)
return -EINVAL;
if ((gbm_handle->usage & usage) != (uint32_t)usage) {
/* make FB special for testing software renderer with */
if (!(gbm_handle->usage & GRALLOC_USAGE_SW_READ_OFTEN) &&
!(gbm_handle->usage & GRALLOC_USAGE_HW_FB) &&
!(gbm_handle->usage & GRALLOC_USAGE_HW_TEXTURE)) {
ALOGE("bo.usage:x%X/usage:x%X is not GRALLOC_USAGE_HW_FB or GRALLOC_USAGE_HW_TEXTURE",
gbm_handle->usage, usage);
return -EINVAL;
}
}
bo_data = gbm_bo_data(bo);
if (!bo_data) {
bo_data = new struct bo_data_t();
gbm_bo_set_user_data(bo, bo_data, gralloc_gbm_destroy_user_data);
}
ALOGV("lock bo %p, cnt=%d, usage=%x", bo, bo_data->lock_count, usage);
/* allow multiple locks with compatible usages */
if (bo_data->lock_count && (bo_data->locked_for & usage) != usage)
return -EINVAL;
usage |= bo_data->locked_for;
if (usage & (GRALLOC_USAGE_SW_WRITE_MASK |
GRALLOC_USAGE_SW_READ_MASK)) {
/* the driver is supposed to wait for the bo */
int write = !!(usage & GRALLOC_USAGE_SW_WRITE_MASK);
int err = gbm_map(handle, write, addr);
if (err)
return err;
}
else {
/* kernel handles the synchronization here */
}
bo_data->lock_count++;
bo_data->locked_for |= usage;
return 0;
}
/*
* Unlock a bo.
*/
int gralloc_gbm_bo_unlock(buffer_handle_t handle)
{
struct gbm_bo *bo = gralloc_gbm_bo_from_handle(handle);
struct bo_data_t *bo_data;
if (!bo)
return -EINVAL;
bo_data = gbm_bo_data(bo);
int mapped = bo_data->locked_for &
(GRALLOC_USAGE_SW_WRITE_MASK | GRALLOC_USAGE_SW_READ_MASK);
if (!bo_data->lock_count)
return 0;
if (mapped)
gbm_unmap(bo);
bo_data->lock_count--;
if (!bo_data->lock_count)
bo_data->locked_for = 0;
return 0;
}
#define GRALLOC_ALIGN(value, base) (((value) + ((base)-1)) & ~((base)-1))
int gralloc_gbm_bo_lock_ycbcr(buffer_handle_t handle,
int usage, int x, int y, int w, int h,
struct android_ycbcr *ycbcr)
{
struct gralloc_handle_t *hnd = gralloc_handle(handle);
int ystride, cstride;
void *addr = 0;
int err;
ALOGV("handle %p, hnd %p, usage 0x%x", handle, hnd, usage);
err = gralloc_gbm_bo_lock(handle, usage, x, y, w, h, &addr);
if (err)
return err;
memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
switch (hnd->format) {
case HAL_PIXEL_FORMAT_YCrCb_420_SP:
ystride = cstride = GRALLOC_ALIGN(hnd->width, 16);
ycbcr->y = addr;
ycbcr->cr = (unsigned char *)addr + ystride * hnd->height;
ycbcr->cb = (unsigned char *)addr + ystride * hnd->height + 1;
ycbcr->ystride = ystride;
ycbcr->cstride = cstride;
ycbcr->chroma_step = 2;
break;
case HAL_PIXEL_FORMAT_YV12:
ystride = hnd->width;
cstride = GRALLOC_ALIGN(ystride / 2, 16);
ycbcr->y = addr;
ycbcr->cr = (unsigned char *)addr + ystride * hnd->height;
ycbcr->cb = (unsigned char *)addr + ystride * hnd->height + cstride * hnd->height / 2;
ycbcr->ystride = ystride;
ycbcr->cstride = cstride;
ycbcr->chroma_step = 1;
break;
default:
ALOGE("Can not lock buffer, invalid format: 0x%x", hnd->format);
return -EINVAL;
}
return 0;
}