1 /* 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. 5 */ 6 7 #define _GNU_SOURCE 8 #include <stdio.h> 9 #include <stdlib.h> 10 11 #include <hardware/gralloc.h> 12 #include <system/graphics.h> 13 #include <cutils/native_handle.h> 14 #include "sw_sync.h" 15 16 #define ARRAY_SIZE(A) (sizeof(A)/sizeof(*(A))) 17 18 #define CHECK(cond) do {\ 19 if (!(cond)) {\ 20 printf("CHECK failed in %s() %s:%d\n", __func__, __FILE__, __LINE__);\ 21 return 0;\ 22 }\ 23 } while(0) 24 25 #define CHECK_NO_MSG(cond) do {\ 26 if (!(cond)) {\ 27 return 0;\ 28 }\ 29 } while(0) 30 31 /* Private API enumeration -- see <gralloc_drm.h> */ 32 enum { 33 GRALLOC_DRM_GET_STRIDE, 34 GRALLOC_DRM_GET_FORMAT, 35 GRALLOC_DRM_GET_DIMENSIONS, 36 }; 37 38 /* See <system/graphics.h> for definitions. */ 39 static const uint32_t format_list[] = { 40 HAL_PIXEL_FORMAT_BGRA_8888, 41 HAL_PIXEL_FORMAT_BLOB, 42 HAL_PIXEL_FORMAT_FLEX_RGB_888, 43 HAL_PIXEL_FORMAT_FLEX_RGBA_8888, 44 HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, 45 HAL_PIXEL_FORMAT_RAW10, 46 HAL_PIXEL_FORMAT_RAW12, 47 HAL_PIXEL_FORMAT_RAW16, 48 HAL_PIXEL_FORMAT_RAW_OPAQUE, 49 HAL_PIXEL_FORMAT_RGB_565, 50 HAL_PIXEL_FORMAT_RGB_888, 51 HAL_PIXEL_FORMAT_RGBA_8888, 52 HAL_PIXEL_FORMAT_RGBX_8888, 53 HAL_PIXEL_FORMAT_Y16, 54 HAL_PIXEL_FORMAT_Y8, 55 HAL_PIXEL_FORMAT_YCbCr_420_888, 56 HAL_PIXEL_FORMAT_YCbCr_422_888, 57 HAL_PIXEL_FORMAT_YCbCr_422_I, 58 HAL_PIXEL_FORMAT_YCbCr_422_SP, 59 HAL_PIXEL_FORMAT_YCbCr_444_888, 60 HAL_PIXEL_FORMAT_YCrCb_420_SP, 61 HAL_PIXEL_FORMAT_YV12, 62 }; 63 64 /* See <hardware/gralloc.h> for descriptions. */ 65 static const uint32_t usage_list[] = { 66 GRALLOC_USAGE_CURSOR, 67 GRALLOC_USAGE_HW_RENDER, 68 GRALLOC_USAGE_HW_TEXTURE, 69 GRALLOC_USAGE_SW_READ_OFTEN, 70 GRALLOC_USAGE_SW_WRITE_OFTEN, 71 GRALLOC_USAGE_SW_READ_RARELY, 72 GRALLOC_USAGE_SW_WRITE_RARELY, 73 }; 74 75 struct gralloctest { 76 buffer_handle_t handle; /* handle to the buffer */ 77 int w; /* width of buffer */ 78 int h; /* height of buffer */ 79 int format; /* format of the buffer */ 80 int usage; /* bitfield indicating usage */ 81 int fence_fd; /* fence file descriptor */ 82 void *vaddr; /* buffer virtual memory address */ 83 int stride; /* stride in pixels */ 84 struct android_ycbcr ycbcr; /* sw access for yuv buffers */ 85 }; 86 87 /* This function is meant to initialize the test to commonly used defaults. */ 88 void gralloctest_init(struct gralloctest* test, int w, int h, int format, 89 int usage) 90 { 91 test->w = w; 92 test->h = h; 93 test->format = format; 94 test->usage = usage; 95 test->fence_fd = -1; 96 test->vaddr = NULL; 97 test->ycbcr.y = NULL; 98 test->ycbcr.cb = NULL; 99 test->ycbcr.cr = NULL; 100 test->stride = 0; 101 } 102 103 static native_handle_t *duplicate_buffer_handle(buffer_handle_t handle) 104 { 105 native_handle_t *hnd = 106 native_handle_create(handle->numFds, handle->numInts); 107 108 if (hnd == NULL) 109 return NULL; 110 111 const int *old_data = handle->data; 112 int *new_data = hnd->data; 113 114 int i; 115 for (i = 0; i < handle->numFds; i++) { 116 *new_data = dup(*old_data); 117 old_data++; 118 new_data++; 119 } 120 121 memcpy(new_data, old_data, sizeof(int) * handle->numInts); 122 123 return hnd; 124 } 125 126 /**************************************************************** 127 * Wrappers around gralloc_module_t and alloc_device_t functions. 128 * GraphicBufferMapper/GraphicBufferAllocator could replace this 129 * in theory. 130 ***************************************************************/ 131 132 static int allocate(struct alloc_device_t* device, struct gralloctest* test) 133 { 134 int ret; 135 136 ret = device->alloc(device, test->w, test->h, test->format, test->usage, 137 &test->handle, &test->stride); 138 139 CHECK_NO_MSG(ret == 0); 140 CHECK_NO_MSG(test->handle->version > 0); 141 CHECK_NO_MSG(test->handle->numInts >= 0); 142 CHECK_NO_MSG(test->handle->numFds >= 0); 143 CHECK_NO_MSG(test->stride >= 0); 144 145 return 1; 146 } 147 148 static int deallocate(struct alloc_device_t* device, struct gralloctest* test) 149 { 150 int ret; 151 ret = device->free(device, test->handle); 152 CHECK(ret == 0); 153 return 1; 154 } 155 156 static int register_buffer(struct gralloc_module_t* module, 157 struct gralloctest* test) 158 { 159 int ret; 160 ret = module->registerBuffer(module, test->handle); 161 return (ret == 0); 162 } 163 164 static int unregister_buffer(struct gralloc_module_t* module, 165 struct gralloctest* test) 166 { 167 int ret; 168 ret = module->unregisterBuffer(module, test->handle); 169 return (ret == 0); 170 } 171 172 static int lock(struct gralloc_module_t* module, struct gralloctest* test) 173 { 174 int ret; 175 176 ret = module->lock(module, test->handle, test->usage, 0, 0, (test->w)/2, 177 (test->h)/2, &test->vaddr); 178 179 return (ret == 0); 180 } 181 182 static int unlock(struct gralloc_module_t* module, struct gralloctest* test) 183 { 184 int ret; 185 ret = module->unlock(module, test->handle); 186 return (ret == 0); 187 } 188 189 static int lock_ycbcr(struct gralloc_module_t* module, struct gralloctest* test) 190 { 191 int ret; 192 193 ret = module->lock_ycbcr(module, test->handle, test->usage, 0, 0, 194 (test->w)/2, (test->h)/2, &test->ycbcr); 195 196 return (ret == 0); 197 } 198 199 static int lock_async(struct gralloc_module_t* module, struct gralloctest* test) 200 { 201 int ret; 202 203 ret = module->lockAsync(module, test->handle, test->usage, 0, 0, 204 (test->w)/2, (test->h)/2, &test->vaddr, test->fence_fd); 205 206 return (ret == 0); 207 } 208 209 static int unlock_async(struct gralloc_module_t* module, 210 struct gralloctest* test) 211 { 212 int ret; 213 214 ret = module->unlockAsync(module, test->handle, &test->fence_fd); 215 216 return (ret == 0); 217 } 218 219 static int lock_async_ycbcr(struct gralloc_module_t* module, 220 struct gralloctest* test) 221 { 222 int ret; 223 224 ret = module->lockAsync_ycbcr(module, test->handle, test->usage, 225 0, 0, (test->w)/2, (test->h)/2, &test->ycbcr, test->fence_fd); 226 227 return (ret == 0); 228 } 229 230 /************************************************************** 231 * END WRAPPERS * 232 **************************************************************/ 233 234 /* This function tests initialization of gralloc module and allocator. */ 235 static int test_init_gralloc(gralloc_module_t** module, alloc_device_t** device) 236 { 237 hw_module_t const* hw_module; 238 int err; 239 240 err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &hw_module); 241 CHECK(err == 0); 242 243 gralloc_open(hw_module, device); 244 *module = (gralloc_module_t *) hw_module; 245 246 CHECK(*module); 247 CHECK(*device); 248 249 return 1; 250 } 251 252 static int test_close_allocator(alloc_device_t* device) 253 { 254 CHECK(gralloc_close(device) == 0); 255 return 1; 256 } 257 258 /* This function tests allocation with varying buffer dimensions. */ 259 static int test_alloc_varying_sizes(struct alloc_device_t* device) 260 { 261 struct gralloctest test; 262 int i; 263 264 gralloctest_init(&test, 0, 0, HAL_PIXEL_FORMAT_BGRA_8888, 265 GRALLOC_USAGE_SW_READ_OFTEN); 266 267 for (i = 1; i < 1920; i++) { 268 test.w = i; 269 test.h = i; 270 CHECK(allocate(device, &test)); 271 CHECK(deallocate(device, &test)); 272 } 273 274 test.w = 1; 275 for (i = 1; i < 1920; i++) { 276 test.h = i; 277 CHECK(allocate(device, &test)); 278 CHECK(deallocate(device, &test)); 279 } 280 281 test.h = 1; 282 for (i = 1; i < 1920; i++) { 283 test.w = i; 284 CHECK(allocate(device, &test)); 285 CHECK(deallocate(device, &test)); 286 } 287 288 return 1; 289 } 290 291 /* 292 * This function tests that we find at least one working format for each 293 * usage which we consider important. 294 */ 295 static int test_alloc_usage(struct alloc_device_t* device) 296 { 297 int i, j; 298 299 struct gralloctest test; 300 gralloctest_init(&test, 512, 512, HAL_PIXEL_FORMAT_BGRA_8888, 301 GRALLOC_USAGE_SW_READ_OFTEN); 302 303 for (i = 0; i < ARRAY_SIZE(usage_list); i++) { 304 test.usage = usage_list[i]; 305 int found = 0; 306 for (j = 0; j < ARRAY_SIZE(format_list); j++) { 307 test.format = format_list[j]; 308 if (allocate(device, &test)) 309 if (deallocate(device, &test)) 310 found = 1; 311 } 312 CHECK(found); 313 } 314 315 return 1; 316 } 317 318 /* 319 * This function tests the advertised API version. 320 * Version_0_2 added (*lock_ycbcr)() method. 321 * Version_0_3 added fence passing to/from lock/unlock. 322 */ 323 static int test_api(struct gralloc_module_t* module) 324 { 325 326 CHECK(module->registerBuffer); 327 CHECK(module->unregisterBuffer); 328 CHECK(module->lock); 329 CHECK(module->unlock); 330 331 switch (module->common.module_api_version) { 332 case GRALLOC_MODULE_API_VERSION_0_3: 333 CHECK(module->lock_ycbcr); 334 CHECK(module->lockAsync); 335 CHECK(module->unlockAsync); 336 CHECK(module->lockAsync_ycbcr); 337 break; 338 case GRALLOC_MODULE_API_VERSION_0_2: 339 CHECK(module->lock_ycbcr); 340 CHECK(module->lockAsync == NULL); 341 CHECK(module->unlockAsync == NULL); 342 CHECK(module->lockAsync_ycbcr == NULL); 343 break; 344 case GRALLOC_MODULE_API_VERSION_0_1: 345 CHECK(module->lockAsync == NULL); 346 CHECK(module->unlockAsync == NULL); 347 CHECK(module->lockAsync_ycbcr == NULL); 348 CHECK(module->lock_ycbcr == NULL); 349 break; 350 default: 351 return 0; 352 } 353 354 return 1; 355 } 356 357 /* 358 * This function registers, unregisters, locks and unlocks the buffer in 359 * various orders. 360 */ 361 static int test_gralloc_order(struct gralloc_module_t* module, 362 struct alloc_device_t* device) 363 { 364 struct gralloctest test, duplicate; 365 366 gralloctest_init(&test, 512, 512, HAL_PIXEL_FORMAT_BGRA_8888, 367 GRALLOC_USAGE_SW_READ_OFTEN); 368 369 gralloctest_init(&duplicate, 512, 512, HAL_PIXEL_FORMAT_BGRA_8888, 370 GRALLOC_USAGE_SW_READ_OFTEN); 371 372 CHECK(allocate(device, &test)); 373 374 /* 375 * Duplicate the buffer handle to simulate an additional reference 376 * in same process. 377 */ 378 native_handle_t *native_handle = duplicate_buffer_handle(test.handle); 379 duplicate.handle = native_handle; 380 381 CHECK(unregister_buffer(module, &duplicate) == 0); 382 CHECK(register_buffer(module, &duplicate)); 383 384 /* This should be a no-op when the buffer wasn't previously locked. */ 385 CHECK(unlock(module, &duplicate)); 386 387 CHECK(lock(module, &duplicate)); 388 CHECK(duplicate.vaddr); 389 CHECK(unlock(module, &duplicate)); 390 391 CHECK(unregister_buffer(module, &duplicate)); 392 393 CHECK(register_buffer(module, &duplicate)); 394 CHECK(unregister_buffer(module, &duplicate)); 395 CHECK(unregister_buffer(module, &duplicate) == 0); 396 397 CHECK(register_buffer(module, &duplicate)); 398 CHECK(deallocate(device, &test)); 399 400 CHECK(lock(module, &duplicate)); 401 CHECK(unlock(module, &duplicate)); 402 CHECK(unregister_buffer(module, &duplicate)); 403 404 CHECK(native_handle_close(duplicate.handle) == 0); 405 CHECK(native_handle_delete(native_handle) == 0); 406 407 return 1; 408 } 409 410 /* This function tests uninitialized buffer handles. */ 411 static int test_uninitialized_handle(struct gralloc_module_t* module) 412 { 413 struct gralloctest test; 414 buffer_handle_t handle = (buffer_handle_t)(intptr_t)0xdeadbeef; 415 416 gralloctest_init(&test, 512, 512, HAL_PIXEL_FORMAT_BGRA_8888, 417 GRALLOC_USAGE_SW_READ_OFTEN); 418 419 test.handle = handle; 420 421 CHECK(register_buffer(module, &test) == 0); 422 CHECK(lock(module, &test) == 0); 423 CHECK(unlock(module, &test) == 0); 424 CHECK(unregister_buffer(module, &test) == 0); 425 426 return 1; 427 } 428 429 /* This function tests that deallocated buffer handles are invalid. */ 430 static int test_freed_handle(struct gralloc_module_t* module, 431 struct alloc_device_t* device) 432 { 433 struct gralloctest test; 434 435 gralloctest_init(&test, 512, 512, HAL_PIXEL_FORMAT_BGRA_8888, 436 GRALLOC_USAGE_SW_READ_OFTEN); 437 438 CHECK(allocate(device, &test)); 439 CHECK(deallocate(device, &test)); 440 441 CHECK(lock(module, &test) == 0); 442 CHECK(unlock(module, &test) == 0); 443 444 return 1; 445 } 446 447 /* This function tests CPU reads and writes. */ 448 static int test_mapping(struct gralloc_module_t* module, 449 struct alloc_device_t* device) 450 { 451 struct gralloctest test; 452 uint32_t* ptr = NULL; 453 uint32_t magic_number = 0x000ABBA; 454 455 gralloctest_init(&test, 512, 512, HAL_PIXEL_FORMAT_BGRA_8888, 456 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN); 457 458 CHECK(allocate(device, &test)); 459 CHECK(lock(module, &test)); 460 461 ptr = (uint32_t *) test.vaddr; 462 CHECK(ptr); 463 ptr[(test.w)/2] = magic_number; 464 465 CHECK(unlock(module, &test)); 466 test.vaddr = NULL; 467 ptr = NULL; 468 469 CHECK(lock(module, &test)); 470 ptr = (uint32_t *) test.vaddr; 471 CHECK(ptr); 472 CHECK(ptr[test.w/2] == magic_number); 473 474 CHECK(unlock(module, &test)); 475 CHECK(deallocate(device, &test)); 476 477 return 1; 478 } 479 480 /* This function tests the private API we use in ARC++ -- not part of official gralloc. */ 481 static int test_perform(struct gralloc_module_t* module, 482 struct alloc_device_t* device) 483 { 484 struct gralloctest test; 485 uint32_t stride, width, height; 486 int32_t format; 487 488 gralloctest_init(&test, 650, 408, HAL_PIXEL_FORMAT_BGRA_8888, 489 GRALLOC_USAGE_SW_READ_OFTEN); 490 491 CHECK(allocate(device, &test)); 492 493 CHECK(module->perform(module, GRALLOC_DRM_GET_STRIDE, test.handle, 494 &stride) == 0); 495 CHECK(stride == test.stride); 496 497 CHECK(module->perform(module, GRALLOC_DRM_GET_FORMAT, test.handle, 498 &format) == 0); 499 CHECK(format == test.format); 500 501 CHECK(module->perform(module, GRALLOC_DRM_GET_DIMENSIONS, test.handle, 502 &width, &height)== 0); 503 CHECK(width == test.w); 504 CHECK(height == test.h); 505 506 CHECK(deallocate(device, &test)); 507 508 return 1; 509 } 510 511 /* This function tests that only YUV buffers work with *lock_ycbcr. */ 512 static int test_ycbcr(struct gralloc_module_t* module, 513 struct alloc_device_t* device) 514 515 { 516 struct gralloctest test; 517 gralloctest_init(&test, 512, 512, HAL_PIXEL_FORMAT_YCbCr_420_888, 518 GRALLOC_USAGE_SW_READ_OFTEN); 519 520 CHECK(allocate(device, &test)); 521 522 CHECK(lock(module, &test) == 0); 523 CHECK(lock_ycbcr(module, &test)); 524 CHECK(test.ycbcr.y); 525 CHECK(test.ycbcr.cb); 526 CHECK(test.ycbcr.cr); 527 CHECK(unlock(module, &test)); 528 529 CHECK(deallocate(device, &test)); 530 531 test.format = HAL_PIXEL_FORMAT_BGRA_8888; 532 CHECK(allocate(device, &test)); 533 534 CHECK(lock_ycbcr(module, &test) == 0); 535 CHECK(lock(module, &test)); 536 CHECK(unlock(module, &test)); 537 538 CHECK(deallocate(device, &test)); 539 540 return 1; 541 } 542 543 /* This function tests asynchronous locking and unlocking of buffers. */ 544 static int test_async(struct gralloc_module_t* module, 545 struct alloc_device_t* device) 546 547 { 548 struct gralloctest rgba_test, ycbcr_test; 549 int fd; 550 551 gralloctest_init(&rgba_test, 512, 512, 552 HAL_PIXEL_FORMAT_BGRA_8888, GRALLOC_USAGE_SW_READ_OFTEN); 553 554 gralloctest_init(&ycbcr_test, 512, 512, 555 HAL_PIXEL_FORMAT_YCbCr_420_888, GRALLOC_USAGE_SW_READ_OFTEN); 556 557 fd = sw_sync_timeline_create(); 558 rgba_test.fence_fd = sw_sync_fence_create(fd, "fence", 1); 559 ycbcr_test.fence_fd = sw_sync_fence_create(fd, "ycbcr_fence", 2); 560 561 CHECK(allocate(device, &rgba_test)); 562 CHECK(allocate(device, &ycbcr_test)); 563 564 /* 565 * Buffer data should only be available after the fence has been 566 * signaled. 567 */ 568 CHECK(lock_async(module, &rgba_test)); 569 CHECK(lock_async_ycbcr(module, &ycbcr_test)); 570 571 CHECK(rgba_test.vaddr == NULL); 572 CHECK(sw_sync_timeline_inc(fd, 1)); 573 CHECK(rgba_test.vaddr); 574 CHECK(ycbcr_test.ycbcr.y == NULL); 575 CHECK(ycbcr_test.ycbcr.cb == NULL); 576 CHECK(ycbcr_test.ycbcr.cr == NULL); 577 578 CHECK(sw_sync_timeline_inc(fd, 1)); 579 CHECK(ycbcr_test.ycbcr.y); 580 CHECK(ycbcr_test.ycbcr.cb); 581 CHECK(ycbcr_test.ycbcr.cr); 582 583 /* 584 * Wait on the fence returned from unlock_async and check it doesn't 585 * return an error. 586 */ 587 CHECK(unlock_async(module, &rgba_test)); 588 CHECK(unlock_async(module, &ycbcr_test)); 589 590 CHECK(rgba_test.fence_fd > 0); 591 CHECK(ycbcr_test.fence_fd > 0); 592 CHECK(sync_wait(rgba_test.fence_fd, 10000) >= 0); 593 CHECK(sync_wait(ycbcr_test.fence_fd, 10000) >= 0); 594 595 CHECK(close(rgba_test.fence_fd) == 0); 596 CHECK(close(ycbcr_test.fence_fd) == 0); 597 598 CHECK(deallocate(device, &rgba_test)); 599 CHECK(deallocate(device, &ycbcr_test)); 600 601 close(fd); 602 603 return 1; 604 } 605 606 static void print_help(const char* argv0) 607 { 608 printf("usage: %s <test_name>\n\n", argv0); 609 printf("A valid test is one the following:\n"); 610 printf("alloc_varying_sizes\nalloc_usage\napi\ngralloc_order\n"); 611 printf("uninitialized_handle\nfreed_handle\nmapping\nperform\n"); 612 printf("ycbcr\nasync\n"); 613 } 614 615 int main(int argc, char *argv[]) 616 { 617 gralloc_module_t* module = NULL; 618 alloc_device_t* device = NULL; 619 620 setbuf(stdout, NULL); 621 622 if (argc == 2) { 623 char* name = argv[1]; 624 int api; 625 626 if(!test_init_gralloc(&module, &device)) 627 goto fail; 628 629 switch (module->common.module_api_version) { 630 case GRALLOC_MODULE_API_VERSION_0_3: 631 api = 3; 632 break; 633 case GRALLOC_MODULE_API_VERSION_0_2: 634 api = 2; 635 break; 636 default: 637 api = 1; 638 } 639 640 printf("[ RUN ] gralloctest.%s\n", name); 641 642 if (strcmp(name, "alloc_varying_sizes") == 0) { 643 if (!test_alloc_varying_sizes(device)) 644 goto fail; 645 } else if (strcmp(name, "alloc_usage") == 0) { 646 if (!test_alloc_usage(device)) 647 goto fail; 648 } else if (strcmp(name, "api") == 0) { 649 if (!test_api(module)) 650 goto fail; 651 } else if (strcmp(name, "gralloc_order") == 0) { 652 if (!test_gralloc_order(module, device)) 653 goto fail; 654 } else if (strcmp(name, "uninitialized_handle") == 0) { 655 if (!test_uninitialized_handle(module)) 656 goto fail; 657 } else if (strcmp(name, "freed_handle") == 0) { 658 if (!test_freed_handle(module, device)) 659 goto fail; 660 } else if (strcmp(name, "mapping") == 0) { 661 if (!test_mapping(module, device)) 662 goto fail; 663 } else if (strcmp(name, "perform") == 0) { 664 if (!test_perform(module, device)) 665 goto fail; 666 } else if (strcmp(name, "ycbcr") == 0) { 667 if (api >= 2 && !test_ycbcr(module, device)) 668 goto fail; 669 } else if (strcmp(name, "async") == 0) { 670 if (api >= 3 && !test_async(module, device)) 671 goto fail; 672 } else { 673 print_help(argv[0]); 674 goto fail; 675 } 676 677 if(!test_close_allocator(device)) 678 goto fail; 679 680 printf("[ PASSED ] gralloctest.%s\n", name); 681 return 0; 682 683 fail: 684 printf("[ FAILED ] gralloctest.%s\n", name); 685 686 } else { 687 print_help(argv[0]); 688 } 689 690 return 0; 691 } 692