Haixia Shi | 4652b8c | 2014-11-19 17:55:38 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | |
Zach Reizner | 38f0fd9 | 2016-02-18 14:28:02 -0800 | [diff] [blame] | 7 | #include "bs_drm.h" |
Haixia Shi | e04ddfd | 2014-11-11 19:14:32 -0800 | [diff] [blame] | 8 | |
Daniele Castagna | 8a63a60 | 2016-10-10 18:43:29 -0400 | [diff] [blame] | 9 | #include <getopt.h> |
| 10 | |
Gurchetan Singh | 10008e6 | 2017-03-09 11:14:38 -0800 | [diff] [blame] | 11 | #define NUM_BUFFERS 2 |
| 12 | |
Daniele Castagna | 7272912 | 2017-10-18 17:09:36 -0400 | [diff] [blame] | 13 | static uint32_t allowed_formats[] = { |
Gurchetan Singh | 3f82d6d | 2017-11-08 14:43:20 -0800 | [diff] [blame] | 14 | GBM_FORMAT_XRGB8888, GBM_FORMAT_XBGR8888, GBM_FORMAT_XRGB2101010, GBM_FORMAT_XBGR2101010, |
Daniele Castagna | 7272912 | 2017-10-18 17:09:36 -0400 | [diff] [blame] | 15 | }; |
| 16 | |
| 17 | const size_t allowed_formats_length = BS_ARRAY_LEN(allowed_formats); |
| 18 | |
Zach Reizner | 4dfdf60 | 2016-03-01 13:06:30 -0800 | [diff] [blame] | 19 | static GLuint solid_shader_create() |
Haixia Shi | e04ddfd | 2014-11-11 19:14:32 -0800 | [diff] [blame] | 20 | { |
Zach Reizner | 4dfdf60 | 2016-03-01 13:06:30 -0800 | [diff] [blame] | 21 | const GLchar *vert = |
Zach Reizner | 38f0fd9 | 2016-02-18 14:28:02 -0800 | [diff] [blame] | 22 | "attribute vec4 vPosition;\n" |
| 23 | "attribute vec4 vColor;\n" |
| 24 | "varying vec4 vFillColor;\n" |
| 25 | "void main() {\n" |
| 26 | " gl_Position = vPosition;\n" |
| 27 | " vFillColor = vColor;\n" |
| 28 | "}\n"; |
Haixia Shi | e04ddfd | 2014-11-11 19:14:32 -0800 | [diff] [blame] | 29 | |
Zach Reizner | 4dfdf60 | 2016-03-01 13:06:30 -0800 | [diff] [blame] | 30 | const GLchar *frag = |
Zach Reizner | 38f0fd9 | 2016-02-18 14:28:02 -0800 | [diff] [blame] | 31 | "precision mediump float;\n" |
| 32 | "varying vec4 vFillColor;\n" |
| 33 | "void main() {\n" |
| 34 | " gl_FragColor = vFillColor;\n" |
| 35 | "}\n"; |
Lauri Peltonen | 763ca46 | 2014-12-17 12:22:21 -0800 | [diff] [blame] | 36 | |
Zach Reizner | 4dfdf60 | 2016-03-01 13:06:30 -0800 | [diff] [blame] | 37 | struct bs_gl_program_create_binding bindings[] = { |
Gurchetan Singh | 3f82d6d | 2017-11-08 14:43:20 -0800 | [diff] [blame] | 38 | { 0, "vPosition" }, { 1, "vColor" }, { 0, NULL }, |
Zach Reizner | 4dfdf60 | 2016-03-01 13:06:30 -0800 | [diff] [blame] | 39 | }; |
Haixia Shi | e04ddfd | 2014-11-11 19:14:32 -0800 | [diff] [blame] | 40 | |
Zach Reizner | 4dfdf60 | 2016-03-01 13:06:30 -0800 | [diff] [blame] | 41 | return bs_gl_program_create_vert_frag_bind(vert, frag, bindings); |
Haixia Shi | e04ddfd | 2014-11-11 19:14:32 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Lauri Peltonen | 95226da | 2014-12-18 16:39:54 -0800 | [diff] [blame] | 44 | static float f(int i) |
| 45 | { |
Haixia Shi | e04ddfd | 2014-11-11 19:14:32 -0800 | [diff] [blame] | 46 | int a = i % 40; |
| 47 | int b = (i / 40) % 6; |
| 48 | switch (b) { |
Zach Reizner | 38f0fd9 | 2016-02-18 14:28:02 -0800 | [diff] [blame] | 49 | case 0: |
| 50 | case 1: |
| 51 | return 0.0f; |
| 52 | case 3: |
| 53 | case 4: |
| 54 | return 1.0f; |
| 55 | case 2: |
| 56 | return (a / 40.0f); |
| 57 | case 5: |
| 58 | return 1.0f - (a / 40.0f); |
| 59 | default: |
| 60 | return 0.0f; |
Haixia Shi | e04ddfd | 2014-11-11 19:14:32 -0800 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
Zach Reizner | 38f0fd9 | 2016-02-18 14:28:02 -0800 | [diff] [blame] | 64 | static void page_flip_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec, |
| 65 | void *data) |
Haixia Shi | 4652b8c | 2014-11-19 17:55:38 -0800 | [diff] [blame] | 66 | { |
| 67 | int *waiting_for_flip = data; |
| 68 | *waiting_for_flip = 0; |
| 69 | } |
| 70 | |
Daniele Castagna | 7272912 | 2017-10-18 17:09:36 -0400 | [diff] [blame] | 71 | static uint32_t find_format(char *fourcc) |
| 72 | { |
| 73 | if (!fourcc || strlen(fourcc) < 4) |
| 74 | return 0; |
| 75 | |
| 76 | uint32_t format = fourcc_code(fourcc[0], fourcc[1], fourcc[2], fourcc[3]); |
| 77 | for (int i = 0; i < allowed_formats_length; i++) { |
| 78 | if (allowed_formats[i] == format) |
| 79 | return format; |
| 80 | } |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
Daniele Castagna | 8a63a60 | 2016-10-10 18:43:29 -0400 | [diff] [blame] | 85 | static const struct option longopts[] = { |
Daniele Castagna | 7272912 | 2017-10-18 17:09:36 -0400 | [diff] [blame] | 86 | { "format", required_argument, NULL, 'f' }, |
| 87 | { "test-page-flip-format-change", required_argument, NULL, 'p' }, |
Daniele Castagna | 8a63a60 | 2016-10-10 18:43:29 -0400 | [diff] [blame] | 88 | { "help", no_argument, NULL, 'h' }, |
| 89 | { 0, 0, 0, 0 }, |
| 90 | }; |
| 91 | |
| 92 | static void print_help(const char *argv0) |
| 93 | { |
Daniele Castagna | 7272912 | 2017-10-18 17:09:36 -0400 | [diff] [blame] | 94 | char allowed_formats_string[allowed_formats_length * 6]; |
| 95 | int i; |
| 96 | for (i = 0; i < allowed_formats_length; i++) { |
| 97 | uint32_t format = allowed_formats[i]; |
| 98 | sprintf(allowed_formats_string + i * 6, "%.4s, ", (char *)&format); |
| 99 | } |
| 100 | |
| 101 | allowed_formats_string[i * 6 - 2] = 0; |
| 102 | |
Daniele Castagna | 8a63a60 | 2016-10-10 18:43:29 -0400 | [diff] [blame] | 103 | // clang-format off |
| 104 | printf("usage: %s [OPTIONS] [drm_device_path]\n", argv0); |
Daniele Castagna | 7272912 | 2017-10-18 17:09:36 -0400 | [diff] [blame] | 105 | printf(" -f, --format <format> defines the fb format.\n"); |
| 106 | printf(" -p, --test-page-flip-format-change <format> test page flips alternating formats.\n"); |
Daniele Castagna | 8a63a60 | 2016-10-10 18:43:29 -0400 | [diff] [blame] | 107 | printf(" -h, --help show help\n"); |
| 108 | printf("\n"); |
Daniele Castagna | 7272912 | 2017-10-18 17:09:36 -0400 | [diff] [blame] | 109 | printf(" <format> must be one of [ %s ].\n", allowed_formats_string); |
| 110 | printf("\n"); |
Daniele Castagna | 8a63a60 | 2016-10-10 18:43:29 -0400 | [diff] [blame] | 111 | printf("\n"); |
| 112 | // clang-format on |
| 113 | } |
| 114 | |
Zach Reizner | 38f0fd9 | 2016-02-18 14:28:02 -0800 | [diff] [blame] | 115 | int main(int argc, char **argv) |
Haixia Shi | e04ddfd | 2014-11-11 19:14:32 -0800 | [diff] [blame] | 116 | { |
Zach Reizner | 38f0fd9 | 2016-02-18 14:28:02 -0800 | [diff] [blame] | 117 | int fd = -1; |
Daniele Castagna | 8a63a60 | 2016-10-10 18:43:29 -0400 | [diff] [blame] | 118 | bool help_flag = false; |
Daniele Castagna | 7272912 | 2017-10-18 17:09:36 -0400 | [diff] [blame] | 119 | uint32_t format = GBM_FORMAT_XRGB8888; |
Daniele Castagna | 95d027f | 2017-11-01 21:56:58 -0400 | [diff] [blame] | 120 | uint32_t test_page_flip_format_change = 0; |
Daniele Castagna | 8a63a60 | 2016-10-10 18:43:29 -0400 | [diff] [blame] | 121 | |
| 122 | int c = -1; |
Daniele Castagna | 7272912 | 2017-10-18 17:09:36 -0400 | [diff] [blame] | 123 | while ((c = getopt_long(argc, argv, "hp:f:", longopts, NULL)) != -1) { |
Daniele Castagna | 8a63a60 | 2016-10-10 18:43:29 -0400 | [diff] [blame] | 124 | switch (c) { |
Daniele Castagna | 7272912 | 2017-10-18 17:09:36 -0400 | [diff] [blame] | 125 | case 'p': |
| 126 | test_page_flip_format_change = find_format(optarg); |
| 127 | if (!test_page_flip_format_change) |
| 128 | help_flag = true; |
| 129 | break; |
Daniele Castagna | 8a63a60 | 2016-10-10 18:43:29 -0400 | [diff] [blame] | 130 | case 'f': |
Daniele Castagna | 7272912 | 2017-10-18 17:09:36 -0400 | [diff] [blame] | 131 | format = find_format(optarg); |
| 132 | if (!format) |
| 133 | help_flag = true; |
Daniele Castagna | 8a63a60 | 2016-10-10 18:43:29 -0400 | [diff] [blame] | 134 | break; |
| 135 | case 'h': |
| 136 | help_flag = true; |
| 137 | break; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | if (help_flag) { |
| 142 | print_help(*argv); |
| 143 | } |
| 144 | |
| 145 | if (optind < argc) { |
| 146 | fd = open(argv[optind], O_RDWR); |
Zach Reizner | 38f0fd9 | 2016-02-18 14:28:02 -0800 | [diff] [blame] | 147 | if (fd < 0) { |
Daniele Castagna | 8a63a60 | 2016-10-10 18:43:29 -0400 | [diff] [blame] | 148 | bs_debug_error("failed to open card %s", argv[optind]); |
Zach Reizner | 38f0fd9 | 2016-02-18 14:28:02 -0800 | [diff] [blame] | 149 | return 1; |
| 150 | } |
Zach Reizner | bf26be8 | 2016-09-15 16:06:21 -0700 | [diff] [blame] | 151 | } else { |
Zach Reizner | 38f0fd9 | 2016-02-18 14:28:02 -0800 | [diff] [blame] | 152 | fd = bs_drm_open_main_display(); |
| 153 | if (fd < 0) { |
| 154 | bs_debug_error("failed to open card for display"); |
| 155 | return 1; |
| 156 | } |
Haixia Shi | e04ddfd | 2014-11-11 19:14:32 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Zach Reizner | 38f0fd9 | 2016-02-18 14:28:02 -0800 | [diff] [blame] | 159 | struct gbm_device *gbm = gbm_create_device(fd); |
| 160 | if (!gbm) { |
| 161 | bs_debug_error("failed to create gbm"); |
| 162 | return 1; |
Haixia Shi | e04ddfd | 2014-11-11 19:14:32 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Zach Reizner | 4dfdf60 | 2016-03-01 13:06:30 -0800 | [diff] [blame] | 165 | struct bs_drm_pipe pipe = { 0 }; |
Zach Reizner | 38f0fd9 | 2016-02-18 14:28:02 -0800 | [diff] [blame] | 166 | if (!bs_drm_pipe_make(fd, &pipe)) { |
| 167 | bs_debug_error("failed to make pipe"); |
| 168 | return 1; |
| 169 | } |
Haixia Shi | e04ddfd | 2014-11-11 19:14:32 -0800 | [diff] [blame] | 170 | |
Zach Reizner | 38f0fd9 | 2016-02-18 14:28:02 -0800 | [diff] [blame] | 171 | drmModeConnector *connector = drmModeGetConnector(fd, pipe.connector_id); |
| 172 | assert(connector); |
| 173 | drmModeModeInfo *mode = &connector->modes[0]; |
| 174 | |
| 175 | struct bs_egl *egl = bs_egl_new(); |
| 176 | if (!bs_egl_setup(egl)) { |
| 177 | bs_debug_error("failed to setup egl context"); |
| 178 | return 1; |
| 179 | } |
| 180 | |
Gurchetan Singh | 10008e6 | 2017-03-09 11:14:38 -0800 | [diff] [blame] | 181 | struct gbm_bo *bos[NUM_BUFFERS]; |
| 182 | uint32_t ids[NUM_BUFFERS]; |
| 183 | struct bs_egl_fb *egl_fbs[NUM_BUFFERS]; |
| 184 | EGLImageKHR egl_images[NUM_BUFFERS]; |
| 185 | for (size_t fb_index = 0; fb_index < NUM_BUFFERS; fb_index++) { |
Daniele Castagna | 8a63a60 | 2016-10-10 18:43:29 -0400 | [diff] [blame] | 186 | if (test_page_flip_format_change && fb_index) { |
Daniele Castagna | 7272912 | 2017-10-18 17:09:36 -0400 | [diff] [blame] | 187 | format = test_page_flip_format_change; |
Daniele Castagna | 8a63a60 | 2016-10-10 18:43:29 -0400 | [diff] [blame] | 188 | } |
| 189 | |
Gurchetan Singh | 4338014 | 2017-03-09 10:45:01 -0800 | [diff] [blame] | 190 | bos[fb_index] = gbm_bo_create(gbm, mode->hdisplay, mode->vdisplay, format, |
| 191 | GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING); |
Zach Reizner | 38f0fd9 | 2016-02-18 14:28:02 -0800 | [diff] [blame] | 192 | if (bos[fb_index] == NULL) { |
| 193 | bs_debug_error("failed to allocate framebuffer"); |
| 194 | return 1; |
| 195 | } |
| 196 | |
| 197 | ids[fb_index] = bs_drm_fb_create_gbm(bos[fb_index]); |
| 198 | if (ids[fb_index] == 0) { |
| 199 | bs_debug_error("failed to create framebuffer id"); |
| 200 | return 1; |
| 201 | } |
| 202 | |
| 203 | EGLImageKHR egl_image = bs_egl_image_create_gbm(egl, bos[fb_index]); |
| 204 | if (egl_image == EGL_NO_IMAGE_KHR) { |
| 205 | bs_debug_error("failed to create EGLImageKHR from framebuffer"); |
| 206 | return 1; |
| 207 | } |
| 208 | |
| 209 | egl_fbs[fb_index] = bs_egl_fb_new(egl, egl_image); |
| 210 | if (!egl_fbs[fb_index]) { |
| 211 | bs_debug_error("failed to create framebuffer from EGLImageKHR"); |
| 212 | return 1; |
| 213 | } |
Haixia Shi | fe4e2ff | 2016-10-17 14:28:55 -0700 | [diff] [blame] | 214 | egl_images[fb_index] = egl_image; |
Zach Reizner | 38f0fd9 | 2016-02-18 14:28:02 -0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | int ret = drmModeSetCrtc(fd, pipe.crtc_id, ids[0], 0 /* x */, 0 /* y */, &pipe.connector_id, |
| 218 | 1 /* connector count */, mode); |
| 219 | if (ret) { |
| 220 | bs_debug_error("failed to set CRTC"); |
| 221 | return 1; |
| 222 | } |
| 223 | |
| 224 | GLuint program = solid_shader_create(); |
Haixia Shi | e04ddfd | 2014-11-11 19:14:32 -0800 | [diff] [blame] | 225 | if (!program) { |
Zach Reizner | 38f0fd9 | 2016-02-18 14:28:02 -0800 | [diff] [blame] | 226 | bs_debug_error("failed to create solid shader"); |
| 227 | return 1; |
Haixia Shi | e04ddfd | 2014-11-11 19:14:32 -0800 | [diff] [blame] | 228 | } |
| 229 | |
Haixia Shi | 4652b8c | 2014-11-19 17:55:38 -0800 | [diff] [blame] | 230 | int fb_idx = 1; |
Zach Reizner | 38f0fd9 | 2016-02-18 14:28:02 -0800 | [diff] [blame] | 231 | for (int i = 0; i <= 500; i++) { |
Haixia Shi | 4652b8c | 2014-11-19 17:55:38 -0800 | [diff] [blame] | 232 | int waiting_for_flip = 1; |
Zach Reizner | 38f0fd9 | 2016-02-18 14:28:02 -0800 | [diff] [blame] | 233 | // clang-format off |
Haixia Shi | e04ddfd | 2014-11-11 19:14:32 -0800 | [diff] [blame] | 234 | GLfloat verts[] = { |
| 235 | 0.0f, -0.5f, 0.0f, |
| 236 | -0.5f, 0.5f, 0.0f, |
| 237 | 0.5f, 0.5f, 0.0f |
| 238 | }; |
| 239 | GLfloat colors[] = { |
| 240 | 1.0f, 0.0f, 0.0f, 1.0f, |
| 241 | 0.0f, 1.0f, 0.0f, 1.0f, |
| 242 | 0.0f, 0.0f, 1.0f, 1.0f |
| 243 | }; |
Zach Reizner | 38f0fd9 | 2016-02-18 14:28:02 -0800 | [diff] [blame] | 244 | // clang-format on |
Haixia Shi | e04ddfd | 2014-11-11 19:14:32 -0800 | [diff] [blame] | 245 | |
Zach Reizner | 38f0fd9 | 2016-02-18 14:28:02 -0800 | [diff] [blame] | 246 | glBindFramebuffer(GL_FRAMEBUFFER, bs_egl_fb_name(egl_fbs[fb_idx])); |
| 247 | glViewport(0, 0, (GLint)mode->hdisplay, (GLint)mode->vdisplay); |
Haixia Shi | 4652b8c | 2014-11-19 17:55:38 -0800 | [diff] [blame] | 248 | |
Haixia Shi | e04ddfd | 2014-11-11 19:14:32 -0800 | [diff] [blame] | 249 | glClearColor(f(i), f(i + 80), f(i + 160), 0.0f); |
| 250 | glClear(GL_COLOR_BUFFER_BIT); |
| 251 | |
| 252 | glUseProgram(program); |
| 253 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, verts); |
| 254 | glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, colors); |
| 255 | glEnableVertexAttribArray(0); |
| 256 | glEnableVertexAttribArray(1); |
| 257 | glDrawArrays(GL_TRIANGLES, 0, 3); |
| 258 | |
| 259 | usleep(1e6 / 120); /* 120 Hz */ |
| 260 | glFinish(); |
Haixia Shi | fe4e2ff | 2016-10-17 14:28:55 -0700 | [diff] [blame] | 261 | |
| 262 | if (!bs_egl_image_flush_external(egl, egl_images[fb_idx])) { |
| 263 | bs_debug_error("failed to call image_flush_external"); |
| 264 | return 1; |
| 265 | } |
| 266 | |
Zach Reizner | 38f0fd9 | 2016-02-18 14:28:02 -0800 | [diff] [blame] | 267 | ret = drmModePageFlip(fd, pipe.crtc_id, ids[fb_idx], DRM_MODE_PAGE_FLIP_EVENT, |
| 268 | &waiting_for_flip); |
| 269 | if (ret) { |
| 270 | bs_debug_error("failed page flip: %d", ret); |
| 271 | return 1; |
| 272 | } |
Haixia Shi | 4652b8c | 2014-11-19 17:55:38 -0800 | [diff] [blame] | 273 | |
| 274 | while (waiting_for_flip) { |
| 275 | drmEventContext evctx = { |
Zach Reizner | 4dfdf60 | 2016-03-01 13:06:30 -0800 | [diff] [blame] | 276 | .version = DRM_EVENT_CONTEXT_VERSION, |
| 277 | .page_flip_handler = page_flip_handler, |
Haixia Shi | 4652b8c | 2014-11-19 17:55:38 -0800 | [diff] [blame] | 278 | }; |
| 279 | |
| 280 | fd_set fds; |
| 281 | FD_ZERO(&fds); |
Zach Reizner | 38f0fd9 | 2016-02-18 14:28:02 -0800 | [diff] [blame] | 282 | FD_SET(fd, &fds); |
Haixia Shi | 4652b8c | 2014-11-19 17:55:38 -0800 | [diff] [blame] | 283 | |
Zach Reizner | 38f0fd9 | 2016-02-18 14:28:02 -0800 | [diff] [blame] | 284 | ret = select(fd + 1, &fds, NULL, NULL, NULL); |
Haixia Shi | 4652b8c | 2014-11-19 17:55:38 -0800 | [diff] [blame] | 285 | if (ret < 0) { |
Zach Reizner | 38f0fd9 | 2016-02-18 14:28:02 -0800 | [diff] [blame] | 286 | bs_debug_error("select err: %s", strerror(errno)); |
| 287 | return 1; |
Zach Reizner | bf26be8 | 2016-09-15 16:06:21 -0700 | [diff] [blame] | 288 | } else if (ret == 0) { |
Zach Reizner | 38f0fd9 | 2016-02-18 14:28:02 -0800 | [diff] [blame] | 289 | bs_debug_error("select timeout"); |
| 290 | return 1; |
| 291 | } |
Zach Reizner | 38f0fd9 | 2016-02-18 14:28:02 -0800 | [diff] [blame] | 292 | ret = drmHandleEvent(fd, &evctx); |
| 293 | if (ret) { |
| 294 | bs_debug_error("failed to wait for page flip: %d", ret); |
| 295 | return 1; |
| 296 | } |
Haixia Shi | 4652b8c | 2014-11-19 17:55:38 -0800 | [diff] [blame] | 297 | } |
| 298 | fb_idx = fb_idx ^ 1; |
Haixia Shi | e04ddfd | 2014-11-11 19:14:32 -0800 | [diff] [blame] | 299 | } |
| 300 | |
Gurchetan Singh | 10008e6 | 2017-03-09 11:14:38 -0800 | [diff] [blame] | 301 | for (size_t fb_index = 0; fb_index < NUM_BUFFERS; fb_index++) { |
| 302 | bs_egl_fb_destroy(&egl_fbs[fb_index]); |
| 303 | bs_egl_image_destroy(egl, &egl_images[fb_index]); |
| 304 | drmModeRmFB(fd, ids[fb_idx]); |
| 305 | gbm_bo_destroy(bos[fb_index]); |
| 306 | } |
| 307 | |
| 308 | bs_egl_destroy(&egl); |
| 309 | gbm_device_destroy(gbm); |
| 310 | close(fd); |
Zach Reizner | 38f0fd9 | 2016-02-18 14:28:02 -0800 | [diff] [blame] | 311 | return 0; |
Haixia Shi | e04ddfd | 2014-11-11 19:14:32 -0800 | [diff] [blame] | 312 | } |