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