blob: 9a62918be2cb4b9279c220f45f2d842425ab3b86 [file] [log] [blame]
Zach Reizner842bd992016-03-08 16:00:25 -08001/*
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
9int main(int argc, char **argv)
10{
Zach Reizner4f8a8de2016-03-25 17:26:44 -070011 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 Reizner842bd992016-03-08 16:00:25 -080025 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 Hwangd4a34122016-04-25 15:46:32 +030055 struct gbm_bo *fb_bo =
56 gbm_bo_create(gbm, mode->hdisplay, mode->vdisplay, GBM_FORMAT_XRGB8888,
Gurchetan Singh18a05992017-11-08 15:43:57 -080057 GBM_BO_USE_SCANOUT | GBM_BO_USE_SW_WRITE_RARELY);
Zach Reizner842bd992016-03-08 16:00:25 -080058 if (!fb_bo) {
59 bs_debug_error("failed to create buffer object for frame buffer");
60 return 1;
61 }
62
Shirish S99c916e2017-07-07 15:57:57 +053063 struct bs_mapper *mapper = bs_mapper_gem_new();
Dongseong Hwang9093afe2017-03-20 19:16:28 -070064 if (mapper == NULL) {
65 bs_debug_error("failed to create mapper object");
66 return 1;
67 }
68
69 void *map_data;
Satyajit Sahub7e47dd2018-05-07 12:35:50 +053070 uint32_t stride;
71 uint8_t *fb_ptr = bs_mapper_map(mapper, fb_bo, 0, &map_data, &stride);
Dongseong Hwang9093afe2017-03-20 19:16:28 -070072 if (fb_ptr == MAP_FAILED) {
Zach Reizner842bd992016-03-08 16:00:25 -080073 bs_debug_error("failed to mmap frame buffer object");
74 return 1;
75 }
Zach Reizner842bd992016-03-08 16:00:25 -080076 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 Hwang9093afe2017-03-20 19:16:28 -070085 bs_mapper_unmap(mapper, fb_bo, map_data);
Zach Reizner842bd992016-03-08 16:00:25 -080086
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
Gurchetan Singh18a05992017-11-08 15:43:57 -0800100 struct gbm_bo *cursor_bo = gbm_bo_create(gbm, cursor_size, cursor_size, GBM_FORMAT_ARGB8888,
101 GBM_BO_USE_CURSOR | GBM_BO_USE_SW_WRITE_RARELY);
Zach Reizner842bd992016-03-08 16:00:25 -0800102 if (!cursor_bo) {
103 bs_debug_error("failed to create cursor buffer object");
104 return 1;
105 }
106
Gurchetan Singha676f1b2017-10-16 18:33:29 -0700107 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 Reizner842bd992016-03-08 16:00:25 -0800110 return 1;
111 }
Gurchetan Singha676f1b2017-10-16 18:33:29 -0700112
113 if (!bs_draw_cursor(mapper, cursor_bo, draw_format)) {
114 bs_debug_error("failed to draw cursor");
115 return 1;
Zach Reizner842bd992016-03-08 16:00:25 -0800116 }
Zach Reizner842bd992016-03-08 16:00:25 -0800117
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 Hwang9093afe2017-03-20 19:16:28 -0700140 bs_mapper_destroy(mapper);
Zach Reizner842bd992016-03-08 16:00:25 -0800141 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}