2 * Copyright 2016 The Chromium OS Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
8 * Please run clang-format on this file after making changes:
10 * clang-format -style=file -i gralloctest.c
20 #include <cutils/native_handle.h>
21 #include <hardware/gralloc.h>
22 #include <sync/sync.h>
23 #include <system/graphics.h>
25 #define ALIGN(A, B) (((A) + (B)-1) / (B) * (B))
26 #define ARRAY_SIZE(A) (sizeof(A) / sizeof(*(A)))
31 fprintf(stderr, "[ FAILED ] check in %s() %s:%d\n", __func__, __FILE__, \
37 #define CHECK_NO_MSG(cond) \
44 /* Private API enumeration -- see <gralloc_drm.h> */
45 enum { GRALLOC_DRM_GET_STRIDE,
46 GRALLOC_DRM_GET_FORMAT,
47 GRALLOC_DRM_GET_DIMENSIONS,
48 GRALLOC_DRM_GET_BACKING_STORE,
51 struct gralloctest_context {
52 struct gralloc_module_t *module;
53 struct alloc_device_t *device;
57 struct gralloc_testcase {
59 int (*run_test)(struct gralloctest_context *ctx);
69 static struct combinations combos[] = {
70 { HAL_PIXEL_FORMAT_RGBA_8888,
71 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
72 GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_COMPOSER |
73 GRALLOC_USAGE_HW_FB | GRALLOC_USAGE_CURSOR },
74 { HAL_PIXEL_FORMAT_RGBA_8888,
75 GRALLOC_USAGE_HW_FB | GRALLOC_USAGE_HW_RENDER |
76 GRALLOC_USAGE_HW_COMPOSER },
77 { HAL_PIXEL_FORMAT_RGBX_8888,
78 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN },
79 { HAL_PIXEL_FORMAT_YCbCr_420_888,
80 GRALLOC_USAGE_EXTERNAL_DISP | GRALLOC_USAGE_HW_COMPOSER |
81 GRALLOC_USAGE_HW_TEXTURE },
82 { HAL_PIXEL_FORMAT_YCbCr_420_888,
83 GRALLOC_USAGE_RENDERSCRIPT | GRALLOC_USAGE_SW_READ_OFTEN |
84 GRALLOC_USAGE_SW_WRITE_OFTEN },
85 { HAL_PIXEL_FORMAT_YV12,
86 GRALLOC_USAGE_SW_WRITE_OFTEN | GRALLOC_USAGE_HW_COMPOSER |
87 GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_EXTERNAL_DISP },
88 { HAL_PIXEL_FORMAT_RGB_565,
89 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN },
90 { HAL_PIXEL_FORMAT_BGRA_8888,
91 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN },
92 { HAL_PIXEL_FORMAT_BLOB,
93 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN },
98 buffer_handle_t handle; /* handle to the buffer */
99 int w; /* width of buffer */
100 int h; /* height of buffer */
101 int format; /* format of the buffer */
102 int usage; /* bitfield indicating usage */
103 int fence_fd; /* fence file descriptor */
104 void *vaddr; /* buffer virtual memory address */
105 int stride; /* stride in pixels */
106 struct android_ycbcr ycbcr; /* sw access for yuv buffers */
109 /* This function is meant to initialize the test to commonly used defaults. */
110 void grallocinfo_init(struct grallocinfo *info, int w, int h, int format, int usage)
114 info->format = format;
118 info->ycbcr.y = NULL;
119 info->ycbcr.cb = NULL;
120 info->ycbcr.cr = NULL;
124 static native_handle_t *duplicate_buffer_handle(buffer_handle_t handle)
126 native_handle_t *hnd = native_handle_create(handle->numFds, handle->numInts);
131 const int *old_data = handle->data;
132 int *new_data = hnd->data;
135 for (i = 0; i < handle->numFds; i++) {
136 *new_data = dup(*old_data);
141 memcpy(new_data, old_data, sizeof(int) * handle->numInts);
146 /****************************************************************
147 * Wrappers around gralloc_module_t and alloc_device_t functions.
148 * GraphicBufferMapper/GraphicBufferAllocator could replace this
150 ***************************************************************/
152 static int allocate(struct alloc_device_t *device, struct grallocinfo *info)
156 ret = device->alloc(device, info->w, info->h, info->format, info->usage, &info->handle,
159 CHECK_NO_MSG(ret == 0);
160 CHECK_NO_MSG(info->handle->version > 0);
161 CHECK_NO_MSG(info->handle->numInts >= 0);
162 CHECK_NO_MSG(info->handle->numFds >= 0);
163 CHECK_NO_MSG(info->stride >= 0);
168 static int deallocate(struct alloc_device_t *device, struct grallocinfo *info)
171 ret = device->free(device, info->handle);
176 static int register_buffer(struct gralloc_module_t *module, struct grallocinfo *info)
179 ret = module->registerBuffer(module, info->handle);
183 static int unregister_buffer(struct gralloc_module_t *module, struct grallocinfo *info)
186 ret = module->unregisterBuffer(module, info->handle);
190 static int lock(struct gralloc_module_t *module, struct grallocinfo *info)
194 ret = module->lock(module, info->handle, info->usage, 0, 0, (info->w) / 2, (info->h) / 2,
200 static int unlock(struct gralloc_module_t *module, struct grallocinfo *info)
203 ret = module->unlock(module, info->handle);
207 static int lock_ycbcr(struct gralloc_module_t *module, struct grallocinfo *info)
211 ret = module->lock_ycbcr(module, info->handle, info->usage, 0, 0, (info->w) / 2,
212 (info->h) / 2, &info->ycbcr);
217 static int lock_async(struct gralloc_module_t *module, struct grallocinfo *info)
221 ret = module->lockAsync(module, info->handle, info->usage, 0, 0, (info->w) / 2,
222 (info->h) / 2, &info->vaddr, info->fence_fd);
227 static int unlock_async(struct gralloc_module_t *module, struct grallocinfo *info)
231 ret = module->unlockAsync(module, info->handle, &info->fence_fd);
236 static int lock_async_ycbcr(struct gralloc_module_t *module, struct grallocinfo *info)
240 ret = module->lockAsync_ycbcr(module, info->handle, info->usage, 0, 0, (info->w) / 2,
241 (info->h) / 2, &info->ycbcr, info->fence_fd);
246 /**************************************************************
248 **************************************************************/
250 /* This function tests initialization of gralloc module and allocator. */
251 static struct gralloctest_context *test_init_gralloc()
254 hw_module_t const *hw_module;
255 struct gralloctest_context *ctx = calloc(1, sizeof(*ctx));
257 err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &hw_module);
261 gralloc_open(hw_module, &ctx->device);
262 ctx->module = (gralloc_module_t *)hw_module;
263 if (!ctx->module || !ctx->device)
266 switch (ctx->module->common.module_api_version) {
267 case GRALLOC_MODULE_API_VERSION_0_3:
270 case GRALLOC_MODULE_API_VERSION_0_2:
280 static int test_close_gralloc(struct gralloctest_context *ctx)
282 CHECK(gralloc_close(ctx->device) == 0);
286 /* This function tests allocation with varying buffer dimensions. */
287 static int test_alloc_varying_sizes(struct gralloctest_context *ctx)
289 struct grallocinfo info;
292 grallocinfo_init(&info, 0, 0, HAL_PIXEL_FORMAT_BGRA_8888, GRALLOC_USAGE_SW_READ_OFTEN);
294 for (i = 1; i < 1920; i++) {
297 CHECK(allocate(ctx->device, &info));
298 CHECK(deallocate(ctx->device, &info));
302 for (i = 1; i < 1920; i++) {
304 CHECK(allocate(ctx->device, &info));
305 CHECK(deallocate(ctx->device, &info));
309 for (i = 1; i < 1920; i++) {
311 CHECK(allocate(ctx->device, &info));
312 CHECK(deallocate(ctx->device, &info));
319 * This function tests that we find at least one working format for each
320 * combos which we consider important.
322 static int test_alloc_combinations(struct gralloctest_context *ctx)
326 struct grallocinfo info;
327 grallocinfo_init(&info, 512, 512, 0, 0);
329 for (i = 0; i < ARRAY_SIZE(combos); i++) {
330 info.format = combos[i].format;
331 info.usage = combos[i].usage;
332 CHECK(allocate(ctx->device, &info));
333 CHECK(deallocate(ctx->device, &info));
340 * This function tests the advertised API version.
341 * Version_0_2 added (*lock_ycbcr)() method.
342 * Version_0_3 added fence passing to/from lock/unlock.
344 static int test_api(struct gralloctest_context *ctx)
347 CHECK(ctx->module->registerBuffer);
348 CHECK(ctx->module->unregisterBuffer);
349 CHECK(ctx->module->lock);
350 CHECK(ctx->module->unlock);
352 switch (ctx->module->common.module_api_version) {
353 case GRALLOC_MODULE_API_VERSION_0_3:
354 CHECK(ctx->module->lock_ycbcr);
355 CHECK(ctx->module->lockAsync);
356 CHECK(ctx->module->unlockAsync);
357 CHECK(ctx->module->lockAsync_ycbcr);
359 case GRALLOC_MODULE_API_VERSION_0_2:
360 CHECK(ctx->module->lock_ycbcr);
361 CHECK(ctx->module->lockAsync == NULL);
362 CHECK(ctx->module->unlockAsync == NULL);
363 CHECK(ctx->module->lockAsync_ycbcr == NULL);
365 case GRALLOC_MODULE_API_VERSION_0_1:
366 CHECK(ctx->module->lockAsync == NULL);
367 CHECK(ctx->module->unlockAsync == NULL);
368 CHECK(ctx->module->lockAsync_ycbcr == NULL);
369 CHECK(ctx->module->lock_ycbcr == NULL);
379 * This function registers, unregisters, locks and unlocks the buffer in
382 static int test_gralloc_order(struct gralloctest_context *ctx)
384 struct grallocinfo info, duplicate;
386 grallocinfo_init(&info, 512, 512, HAL_PIXEL_FORMAT_BGRA_8888, GRALLOC_USAGE_SW_READ_OFTEN);
388 grallocinfo_init(&duplicate, 512, 512, HAL_PIXEL_FORMAT_BGRA_8888,
389 GRALLOC_USAGE_SW_READ_OFTEN);
391 CHECK(allocate(ctx->device, &info));
394 * Duplicate the buffer handle to simulate an additional reference
397 native_handle_t *native_handle = duplicate_buffer_handle(info.handle);
398 duplicate.handle = native_handle;
400 CHECK(unregister_buffer(ctx->module, &duplicate) == 0);
401 CHECK(register_buffer(ctx->module, &duplicate));
403 CHECK(unlock(ctx->module, &duplicate) == 0);
405 CHECK(lock(ctx->module, &duplicate));
406 CHECK(duplicate.vaddr);
407 CHECK(unlock(ctx->module, &duplicate));
409 CHECK(unregister_buffer(ctx->module, &duplicate));
411 CHECK(register_buffer(ctx->module, &duplicate));
412 CHECK(unregister_buffer(ctx->module, &duplicate));
413 CHECK(unregister_buffer(ctx->module, &duplicate) == 0);
415 CHECK(register_buffer(ctx->module, &duplicate));
416 CHECK(deallocate(ctx->device, &info));
418 CHECK(lock(ctx->module, &duplicate));
419 CHECK(lock(ctx->module, &duplicate));
420 CHECK(unlock(ctx->module, &duplicate));
421 CHECK(unlock(ctx->module, &duplicate));
422 CHECK(unlock(ctx->module, &duplicate) == 0);
423 CHECK(unregister_buffer(ctx->module, &duplicate));
425 CHECK(native_handle_close(duplicate.handle) == 0);
426 CHECK(native_handle_delete(native_handle) == 0);
431 /* This function tests CPU reads and writes. */
432 static int test_mapping(struct gralloctest_context *ctx)
434 struct grallocinfo info;
435 uint32_t *ptr = NULL;
436 uint32_t magic_number = 0x000ABBA;
438 grallocinfo_init(&info, 512, 512, HAL_PIXEL_FORMAT_BGRA_8888,
439 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
441 CHECK(allocate(ctx->device, &info));
442 CHECK(lock(ctx->module, &info));
444 ptr = (uint32_t *)info.vaddr;
446 ptr[(info.w) / 2] = magic_number;
448 CHECK(unlock(ctx->module, &info));
452 CHECK(lock(ctx->module, &info));
453 ptr = (uint32_t *)info.vaddr;
455 CHECK(ptr[info.w / 2] == magic_number);
457 CHECK(unlock(ctx->module, &info));
458 CHECK(deallocate(ctx->device, &info));
463 /* This function tests the private API we use in ARC++ -- not part of official
465 static int test_perform(struct gralloctest_context *ctx)
469 uint32_t stride, width, height;
470 struct grallocinfo info, duplicate;
471 struct gralloc_module_t *mod = ctx->module;
473 grallocinfo_init(&info, 650, 408, HAL_PIXEL_FORMAT_BGRA_8888, GRALLOC_USAGE_SW_READ_OFTEN);
475 CHECK(allocate(ctx->device, &info));
477 CHECK(mod->perform(mod, GRALLOC_DRM_GET_STRIDE, info.handle, &stride) == 0);
478 CHECK(stride == info.stride);
480 CHECK(mod->perform(mod, GRALLOC_DRM_GET_FORMAT, info.handle, &format) == 0);
481 CHECK(format == info.format);
483 CHECK(mod->perform(mod, GRALLOC_DRM_GET_DIMENSIONS, info.handle, &width, &height) == 0);
484 CHECK(width == info.w);
485 CHECK(height == info.h);
487 native_handle_t *native_handle = duplicate_buffer_handle(info.handle);
488 duplicate.handle = native_handle;
490 CHECK(mod->perform(mod, GRALLOC_DRM_GET_BACKING_STORE, duplicate.handle, &id2));
491 CHECK(register_buffer(mod, &duplicate));
493 CHECK(mod->perform(mod, GRALLOC_DRM_GET_BACKING_STORE, info.handle, &id1) == 0);
494 CHECK(mod->perform(mod, GRALLOC_DRM_GET_BACKING_STORE, duplicate.handle, &id2) == 0);
497 CHECK(unregister_buffer(mod, &duplicate));
498 CHECK(deallocate(ctx->device, &info));
503 /* This function tests that only YUV buffers work with *lock_ycbcr. */
504 static int test_ycbcr(struct gralloctest_context *ctx)
507 struct grallocinfo info;
508 grallocinfo_init(&info, 512, 512, HAL_PIXEL_FORMAT_YCbCr_420_888,
509 GRALLOC_USAGE_SW_READ_OFTEN);
511 CHECK(allocate(ctx->device, &info));
513 CHECK(lock(ctx->module, &info) == 0);
514 CHECK(lock_ycbcr(ctx->module, &info));
516 CHECK(info.ycbcr.cb);
517 CHECK(info.ycbcr.cr);
518 CHECK(unlock(ctx->module, &info));
520 CHECK(deallocate(ctx->device, &info));
522 info.format = HAL_PIXEL_FORMAT_BGRA_8888;
523 CHECK(allocate(ctx->device, &info));
525 CHECK(lock_ycbcr(ctx->module, &info) == 0);
526 CHECK(lock(ctx->module, &info));
527 CHECK(unlock(ctx->module, &info));
529 CHECK(deallocate(ctx->device, &info));
535 * This function tests a method ARC++ uses to query YUV buffer
536 * info -- not part of official gralloc API. This is used in
537 * Mali, Mesa, the ArcCodec and wayland_service.
539 static int test_yuv_info(struct gralloctest_context *ctx)
541 struct grallocinfo info;
542 uintptr_t y_size, c_stride, c_size, cr_offset, cb_offset;
543 uint32_t width, height;
544 width = height = 512;
546 /* <system/graphics.h> defines YV12 as having:
549 * - a horizontal stride multiple of 16 pixels
550 * - a vertical stride equal to the height
552 * y_size = stride * height.
553 * c_stride = ALIGN(stride/2, 16).
554 * c_size = c_stride * height/2.
555 * size = y_size + c_size * 2.
556 * cr_offset = y_size.
557 * cb_offset = y_size + c_size.
560 grallocinfo_init(&info, width, height, HAL_PIXEL_FORMAT_YV12, GRALLOC_USAGE_SW_READ_OFTEN);
562 CHECK(allocate(ctx->device, &info));
564 y_size = info.stride * height;
565 c_stride = ALIGN(info.stride / 2, 16);
566 c_size = c_stride * height / 2;
568 cb_offset = y_size + c_size;
573 * Check if the (*lock_ycbcr) with usage of zero returns the
574 * offsets and strides of the YV12 buffer. This is unofficial
575 * behavior we are testing here.
577 CHECK(lock_ycbcr(ctx->module, &info));
579 CHECK(info.stride == info.ycbcr.ystride);
580 CHECK(c_stride == info.ycbcr.cstride);
581 CHECK(cr_offset == (uintptr_t)info.ycbcr.cr);
582 CHECK(cb_offset == (uintptr_t)info.ycbcr.cb);
584 CHECK(unlock(ctx->module, &info));
586 CHECK(deallocate(ctx->device, &info));
591 /* This function tests asynchronous locking and unlocking of buffers. */
592 static int test_async(struct gralloctest_context *ctx)
595 struct grallocinfo rgba_info, ycbcr_info;
596 grallocinfo_init(&rgba_info, 512, 512, HAL_PIXEL_FORMAT_BGRA_8888,
597 GRALLOC_USAGE_SW_READ_OFTEN);
598 grallocinfo_init(&ycbcr_info, 512, 512, HAL_PIXEL_FORMAT_YCbCr_420_888,
599 GRALLOC_USAGE_SW_READ_OFTEN);
601 CHECK(allocate(ctx->device, &rgba_info));
602 CHECK(allocate(ctx->device, &ycbcr_info));
604 CHECK(lock_async(ctx->module, &rgba_info));
605 CHECK(lock_async_ycbcr(ctx->module, &ycbcr_info));
607 CHECK(rgba_info.vaddr);
608 CHECK(ycbcr_info.ycbcr.y);
609 CHECK(ycbcr_info.ycbcr.cb);
610 CHECK(ycbcr_info.ycbcr.cr);
613 * Wait on the fence returned from unlock_async and check it doesn't
616 CHECK(unlock_async(ctx->module, &rgba_info));
617 CHECK(unlock_async(ctx->module, &ycbcr_info));
619 if (rgba_info.fence_fd >= 0) {
620 CHECK(sync_wait(rgba_info.fence_fd, 10000) >= 0);
621 CHECK(close(rgba_info.fence_fd) == 0);
624 if (ycbcr_info.fence_fd >= 0) {
625 CHECK(sync_wait(ycbcr_info.fence_fd, 10000) >= 0);
626 CHECK(close(ycbcr_info.fence_fd) == 0);
629 CHECK(deallocate(ctx->device, &rgba_info));
630 CHECK(deallocate(ctx->device, &ycbcr_info));
635 static const struct gralloc_testcase tests[] = {
636 { "alloc_varying_sizes", test_alloc_varying_sizes, 1 },
637 { "alloc_combinations", test_alloc_combinations, 1 },
638 { "api", test_api, 1 },
639 { "gralloc_order", test_gralloc_order, 1 },
640 { "mapping", test_mapping, 1 },
641 { "perform", test_perform, 1 },
642 { "ycbcr", test_ycbcr, 2 },
643 { "yuv_info", test_yuv_info, 2 },
644 { "async", test_async, 3 },
647 static void print_help(const char *argv0)
650 printf("usage: %s <test_name>\n\n", argv0);
651 printf("A valid name test is one the following:\n");
652 for (i = 0; i < ARRAY_SIZE(tests); i++)
653 printf("%s\n", tests[i].name);
656 int main(int argc, char *argv[])
659 uint32_t num_run = 0;
661 setbuf(stdout, NULL);
664 char *name = argv[1];
666 struct gralloctest_context *ctx = test_init_gralloc();
668 fprintf(stderr, "[ FAILED ] to initialize gralloc.\n");
672 for (i = 0; i < ARRAY_SIZE(tests); i++) {
673 if (strcmp(tests[i].name, name) && strcmp("all", name))
677 if (ctx->api >= tests[i].required_api)
678 success = tests[i].run_test(ctx);
680 printf("[ RUN ] gralloctest.%s\n", tests[i].name);
682 fprintf(stderr, "[ FAILED ] gralloctest.%s\n", tests[i].name);
685 printf("[ PASSED ] gralloctest.%s\n", tests[i].name);
691 if (!test_close_gralloc(ctx)) {
692 fprintf(stderr, "[ FAILED ] to close gralloc.\n");