Zach Reizner | 842bd99 | 2016-03-08 16:00:25 -0800 | [diff] [blame] | 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 | #include "bs_drm.h" |
| 8 | |
| 9 | int main(int argc, char **argv) |
| 10 | { |
Zach Reizner | 4f8a8de | 2016-03-25 17:26:44 -0700 | [diff] [blame] | 11 | uint32_t cursor_size = 64; |
| 12 | if (argc >= 2) { |
| 13 | char *end_str; |
| 14 | unsigned long new_cursor_size = strtoul(argv[1], &end_str, 0); |
| 15 | if (end_str == argv[1] || new_cursor_size == 0 || new_cursor_size > UINT32_MAX) { |
| 16 | printf( |
| 17 | "usage:\n drm_cursor_test [cursor size]\n\nCursor size defaults to " |
| 18 | "%u\n", |
| 19 | cursor_size); |
| 20 | return 1; |
| 21 | } |
| 22 | cursor_size = (uint32_t)new_cursor_size; |
| 23 | } |
| 24 | |
Zach Reizner | 842bd99 | 2016-03-08 16:00:25 -0800 | [diff] [blame] | 25 | int fd = bs_drm_open_main_display(); |
| 26 | if (fd < 0) { |
| 27 | bs_debug_error("failed to open card for display"); |
| 28 | return 1; |
| 29 | } |
| 30 | |
| 31 | struct gbm_device *gbm = gbm_create_device(fd); |
| 32 | if (!gbm) { |
| 33 | bs_debug_error("failed to create gbm"); |
| 34 | return 1; |
| 35 | } |
| 36 | |
| 37 | struct bs_drm_pipe pipe = { 0 }; |
| 38 | if (!bs_drm_pipe_make(fd, &pipe)) { |
| 39 | bs_debug_error("failed to make pipe"); |
| 40 | return 1; |
| 41 | } |
| 42 | |
| 43 | drmModeConnector *connector = drmModeGetConnector(fd, pipe.connector_id); |
| 44 | drmModeModeInfo *mode = &connector->modes[0]; |
| 45 | uint32_t crtc_id = pipe.crtc_id; |
| 46 | |
| 47 | // Restart the cursor position before binding the crtc so that the old cursor position isn't |
| 48 | // displayed briefly when the display is turned activated. |
| 49 | int ret = drmModeMoveCursor(fd, crtc_id, 0, 0); |
| 50 | if (ret) { |
| 51 | bs_debug_error("failed to move cursor: %d", ret); |
| 52 | return 1; |
| 53 | } |
| 54 | |
Dongseong Hwang | d4a3412 | 2016-04-25 15:46:32 +0300 | [diff] [blame] | 55 | struct gbm_bo *fb_bo = |
| 56 | gbm_bo_create(gbm, mode->hdisplay, mode->vdisplay, GBM_FORMAT_XRGB8888, |
| 57 | GBM_BO_USE_SCANOUT | GBM_BO_USE_LINEAR); |
Zach Reizner | 842bd99 | 2016-03-08 16:00:25 -0800 | [diff] [blame] | 58 | if (!fb_bo) { |
| 59 | bs_debug_error("failed to create buffer object for frame buffer"); |
| 60 | return 1; |
| 61 | } |
| 62 | |
Shirish S | 99c916e | 2017-07-07 15:57:57 +0530 | [diff] [blame] | 63 | struct bs_mapper *mapper = bs_mapper_gem_new(); |
Dongseong Hwang | 9093afe | 2017-03-20 19:16:28 -0700 | [diff] [blame] | 64 | if (mapper == NULL) { |
| 65 | bs_debug_error("failed to create mapper object"); |
| 66 | return 1; |
| 67 | } |
| 68 | |
| 69 | void *map_data; |
| 70 | uint8_t *fb_ptr = bs_mapper_map(mapper, fb_bo, 0, &map_data); |
| 71 | if (fb_ptr == MAP_FAILED) { |
Zach Reizner | 842bd99 | 2016-03-08 16:00:25 -0800 | [diff] [blame] | 72 | bs_debug_error("failed to mmap frame buffer object"); |
| 73 | return 1; |
| 74 | } |
| 75 | uint32_t stride = gbm_bo_get_stride(fb_bo); |
| 76 | for (size_t y = 0; y < mode->vdisplay; y++) { |
| 77 | for (size_t x = 0; x < mode->hdisplay; x++) { |
| 78 | // Solid blue fill |
| 79 | fb_ptr[y * stride + x * 4 + 0] = 0xff; |
| 80 | fb_ptr[y * stride + x * 4 + 1] = 0; |
| 81 | fb_ptr[y * stride + x * 4 + 2] = 0; |
| 82 | fb_ptr[y * stride + x * 4 + 3] = 0; |
| 83 | } |
| 84 | } |
Dongseong Hwang | 9093afe | 2017-03-20 19:16:28 -0700 | [diff] [blame] | 85 | bs_mapper_unmap(mapper, fb_bo, map_data); |
Zach Reizner | 842bd99 | 2016-03-08 16:00:25 -0800 | [diff] [blame] | 86 | |
| 87 | uint32_t fb_id = bs_drm_fb_create_gbm(fb_bo); |
| 88 | if (!fb_id) { |
| 89 | bs_debug_error("failed to create frame buffer from buffer object"); |
| 90 | return 1; |
| 91 | } |
| 92 | |
| 93 | ret = drmModeSetCrtc(fd, crtc_id, fb_id, 0 /* x */, 0 /* y */, &pipe.connector_id, |
| 94 | 1 /* connector count */, mode); |
| 95 | if (ret) { |
| 96 | bs_debug_error("failed to set crtc: %d", ret); |
| 97 | return 1; |
| 98 | } |
| 99 | |
Zach Reizner | 842bd99 | 2016-03-08 16:00:25 -0800 | [diff] [blame] | 100 | struct gbm_bo *cursor_bo = |
| 101 | gbm_bo_create(gbm, cursor_size, cursor_size, GBM_FORMAT_ARGB8888, GBM_BO_USE_CURSOR); |
| 102 | if (!cursor_bo) { |
| 103 | bs_debug_error("failed to create cursor buffer object"); |
| 104 | return 1; |
| 105 | } |
| 106 | |
Gurchetan Singh | a676f1b | 2017-10-16 18:33:29 -0700 | [diff] [blame^] | 107 | const struct bs_draw_format *draw_format = bs_get_draw_format(GBM_FORMAT_ARGB8888); |
| 108 | if (!draw_format) { |
| 109 | bs_debug_error("failed to get draw format"); |
Zach Reizner | 842bd99 | 2016-03-08 16:00:25 -0800 | [diff] [blame] | 110 | return 1; |
| 111 | } |
Gurchetan Singh | a676f1b | 2017-10-16 18:33:29 -0700 | [diff] [blame^] | 112 | |
| 113 | if (!bs_draw_cursor(mapper, cursor_bo, draw_format)) { |
| 114 | bs_debug_error("failed to draw cursor"); |
| 115 | return 1; |
Zach Reizner | 842bd99 | 2016-03-08 16:00:25 -0800 | [diff] [blame] | 116 | } |
Zach Reizner | 842bd99 | 2016-03-08 16:00:25 -0800 | [diff] [blame] | 117 | |
| 118 | ret = drmModeSetCursor(fd, crtc_id, gbm_bo_get_handle(cursor_bo).u32, cursor_size, |
| 119 | cursor_size); |
| 120 | if (ret) { |
| 121 | bs_debug_error("failed to set cursor: %d", ret); |
| 122 | return 1; |
| 123 | } |
| 124 | |
| 125 | // Divisor chosen so that the test would last about 10 seconds @ 60fps. |
| 126 | const uint32_t divisor = 25; |
| 127 | const uint32_t xinc = mode->hdisplay / divisor; |
| 128 | const uint32_t yinc = mode->vdisplay / divisor; |
| 129 | for (uint32_t x = 0; x < divisor; x++) { |
| 130 | for (uint32_t y = 0; y < divisor; y++) { |
| 131 | ret = drmModeMoveCursor(fd, crtc_id, x * xinc + y, y * yinc + x); |
| 132 | if (ret) { |
| 133 | bs_debug_error("failed to move cursor: %d", ret); |
| 134 | return 1; |
| 135 | } |
| 136 | usleep(16667); |
| 137 | } |
| 138 | } |
| 139 | |
Dongseong Hwang | 9093afe | 2017-03-20 19:16:28 -0700 | [diff] [blame] | 140 | bs_mapper_destroy(mapper); |
Zach Reizner | 842bd99 | 2016-03-08 16:00:25 -0800 | [diff] [blame] | 141 | drmModeRmFB(fd, fb_id); |
| 142 | gbm_bo_destroy(fb_bo); |
| 143 | gbm_bo_destroy(cursor_bo); |
| 144 | gbm_device_destroy(gbm); |
| 145 | close(fd); |
| 146 | |
| 147 | return 0; |
| 148 | } |