blob: 81f2a04b312a97c88167c91a813d2fb3c578f2c7 [file] [log] [blame]
Haixia Shi4652b8c2014-11-19 17:55:38 -08001/*
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 Reizner38f0fd92016-02-18 14:28:02 -08007#include "bs_drm.h"
Haixia Shie04ddfd2014-11-11 19:14:32 -08008
Daniele Castagna8a63a602016-10-10 18:43:29 -04009#include <getopt.h>
10
Gurchetan Singh10008e62017-03-09 11:14:38 -080011#define NUM_BUFFERS 2
12
Daniele Castagna72729122017-10-18 17:09:36 -040013static uint32_t allowed_formats[] = {
Gurchetan Singh3f82d6d2017-11-08 14:43:20 -080014 GBM_FORMAT_XRGB8888, GBM_FORMAT_XBGR8888, GBM_FORMAT_XRGB2101010, GBM_FORMAT_XBGR2101010,
Daniele Castagna72729122017-10-18 17:09:36 -040015};
16
17const size_t allowed_formats_length = BS_ARRAY_LEN(allowed_formats);
18
Zach Reizner4dfdf602016-03-01 13:06:30 -080019static GLuint solid_shader_create()
Haixia Shie04ddfd2014-11-11 19:14:32 -080020{
Zach Reizner4dfdf602016-03-01 13:06:30 -080021 const GLchar *vert =
Zach Reizner38f0fd92016-02-18 14:28:02 -080022 "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 Shie04ddfd2014-11-11 19:14:32 -080029
Zach Reizner4dfdf602016-03-01 13:06:30 -080030 const GLchar *frag =
Zach Reizner38f0fd92016-02-18 14:28:02 -080031 "precision mediump float;\n"
32 "varying vec4 vFillColor;\n"
33 "void main() {\n"
34 " gl_FragColor = vFillColor;\n"
35 "}\n";
Lauri Peltonen763ca462014-12-17 12:22:21 -080036
Zach Reizner4dfdf602016-03-01 13:06:30 -080037 struct bs_gl_program_create_binding bindings[] = {
Gurchetan Singh3f82d6d2017-11-08 14:43:20 -080038 { 0, "vPosition" }, { 1, "vColor" }, { 0, NULL },
Zach Reizner4dfdf602016-03-01 13:06:30 -080039 };
Haixia Shie04ddfd2014-11-11 19:14:32 -080040
Zach Reizner4dfdf602016-03-01 13:06:30 -080041 return bs_gl_program_create_vert_frag_bind(vert, frag, bindings);
Haixia Shie04ddfd2014-11-11 19:14:32 -080042}
43
Lauri Peltonen95226da2014-12-18 16:39:54 -080044static float f(int i)
45{
Haixia Shie04ddfd2014-11-11 19:14:32 -080046 int a = i % 40;
47 int b = (i / 40) % 6;
48 switch (b) {
Zach Reizner38f0fd92016-02-18 14:28:02 -080049 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 Shie04ddfd2014-11-11 19:14:32 -080061 }
62}
63
Zach Reizner38f0fd92016-02-18 14:28:02 -080064static void page_flip_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
65 void *data)
Haixia Shi4652b8c2014-11-19 17:55:38 -080066{
67 int *waiting_for_flip = data;
68 *waiting_for_flip = 0;
69}
70
Daniele Castagna72729122017-10-18 17:09:36 -040071static 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 Castagna8a63a602016-10-10 18:43:29 -040085static const struct option longopts[] = {
Daniele Castagna72729122017-10-18 17:09:36 -040086 { "format", required_argument, NULL, 'f' },
Fritz Koenig01ee3d42018-11-07 10:42:17 -080087 { "modifier", required_argument, NULL, 'm' },
Daniele Castagna72729122017-10-18 17:09:36 -040088 { "test-page-flip-format-change", required_argument, NULL, 'p' },
Daniele Castagna8a63a602016-10-10 18:43:29 -040089 { "help", no_argument, NULL, 'h' },
90 { 0, 0, 0, 0 },
91};
92
Fritz Koenig01ee3d42018-11-07 10:42:17 -080093static void print_help(const char *argv0, int fd)
Daniele Castagna8a63a602016-10-10 18:43:29 -040094{
Daniele Castagna72729122017-10-18 17:09:36 -040095 char allowed_formats_string[allowed_formats_length * 6];
96 int i;
97 for (i = 0; i < allowed_formats_length; i++) {
98 uint32_t format = allowed_formats[i];
99 sprintf(allowed_formats_string + i * 6, "%.4s, ", (char *)&format);
100 }
101
102 allowed_formats_string[i * 6 - 2] = 0;
103
Daniele Castagna8a63a602016-10-10 18:43:29 -0400104 // clang-format off
105 printf("usage: %s [OPTIONS] [drm_device_path]\n", argv0);
Fritz Koenig01ee3d42018-11-07 10:42:17 -0800106 printf(" -f, --format <format> defines the fb format.\n");
107 printf(" -m, --modifier <modifier> pass modifiers.\n");
108 printf(" -p, --test-page-flip-format-change <format> test page flips alternating formats.\n");
109 printf(" -h, --help show help\n");
Daniele Castagna8a63a602016-10-10 18:43:29 -0400110 printf("\n");
Fritz Koenig01ee3d42018-11-07 10:42:17 -0800111 printf(" <format> must be one of [ %s ].\n", allowed_formats_string);
112 printf(" <modifier> must be one of ");
113 bs_print_supported_modifiers(fd);
Daniele Castagna72729122017-10-18 17:09:36 -0400114 printf("\n");
Daniele Castagna8a63a602016-10-10 18:43:29 -0400115 printf("\n");
116 // clang-format on
117}
118
Zach Reizner38f0fd92016-02-18 14:28:02 -0800119int main(int argc, char **argv)
Haixia Shie04ddfd2014-11-11 19:14:32 -0800120{
Zach Reizner38f0fd92016-02-18 14:28:02 -0800121 int fd = -1;
Daniele Castagna8a63a602016-10-10 18:43:29 -0400122 bool help_flag = false;
Daniele Castagna72729122017-10-18 17:09:36 -0400123 uint32_t format = GBM_FORMAT_XRGB8888;
Daniele Castagna95d027f2017-11-01 21:56:58 -0400124 uint32_t test_page_flip_format_change = 0;
Fritz Koenig01ee3d42018-11-07 10:42:17 -0800125 uint64_t modifier = DRM_FORMAT_MOD_INVALID;
Daniele Castagna8a63a602016-10-10 18:43:29 -0400126
127 int c = -1;
Fritz Koenig01ee3d42018-11-07 10:42:17 -0800128 while ((c = getopt_long(argc, argv, "m:hp:f:", longopts, NULL)) != -1) {
Daniele Castagna8a63a602016-10-10 18:43:29 -0400129 switch (c) {
Daniele Castagna72729122017-10-18 17:09:36 -0400130 case 'p':
131 test_page_flip_format_change = find_format(optarg);
132 if (!test_page_flip_format_change)
133 help_flag = true;
134 break;
Daniele Castagna8a63a602016-10-10 18:43:29 -0400135 case 'f':
Daniele Castagna72729122017-10-18 17:09:36 -0400136 format = find_format(optarg);
Fritz Koenig01ee3d42018-11-07 10:42:17 -0800137 if (!format) {
138 bs_debug_error("unsupported format: %s", optarg);
Daniele Castagna72729122017-10-18 17:09:36 -0400139 help_flag = true;
Fritz Koenig01ee3d42018-11-07 10:42:17 -0800140 }
141 break;
142 case 'm':
143 modifier = bs_string_to_modifier(optarg);
144 if (modifier == -1) {
145 bs_debug_error("unsupported modifier: %s", optarg);
146 help_flag = true;
147 }
Daniele Castagna8a63a602016-10-10 18:43:29 -0400148 break;
149 case 'h':
150 help_flag = true;
151 break;
152 }
153 }
154
Daniele Castagna8a63a602016-10-10 18:43:29 -0400155 if (optind < argc) {
156 fd = open(argv[optind], O_RDWR);
Zach Reizner38f0fd92016-02-18 14:28:02 -0800157 if (fd < 0) {
Daniele Castagna8a63a602016-10-10 18:43:29 -0400158 bs_debug_error("failed to open card %s", argv[optind]);
Zach Reizner38f0fd92016-02-18 14:28:02 -0800159 return 1;
160 }
Zach Reiznerbf26be82016-09-15 16:06:21 -0700161 } else {
Zach Reizner38f0fd92016-02-18 14:28:02 -0800162 fd = bs_drm_open_main_display();
163 if (fd < 0) {
164 bs_debug_error("failed to open card for display");
165 return 1;
166 }
Haixia Shie04ddfd2014-11-11 19:14:32 -0800167 }
168
Fritz Koenig01ee3d42018-11-07 10:42:17 -0800169 if (help_flag) {
170 print_help(*argv, fd);
171 close(fd);
172 return 1;
173 }
174
Zach Reizner38f0fd92016-02-18 14:28:02 -0800175 struct gbm_device *gbm = gbm_create_device(fd);
176 if (!gbm) {
177 bs_debug_error("failed to create gbm");
178 return 1;
Haixia Shie04ddfd2014-11-11 19:14:32 -0800179 }
180
Zach Reizner4dfdf602016-03-01 13:06:30 -0800181 struct bs_drm_pipe pipe = { 0 };
Zach Reizner38f0fd92016-02-18 14:28:02 -0800182 if (!bs_drm_pipe_make(fd, &pipe)) {
183 bs_debug_error("failed to make pipe");
184 return 1;
185 }
Haixia Shie04ddfd2014-11-11 19:14:32 -0800186
Zach Reizner38f0fd92016-02-18 14:28:02 -0800187 drmModeConnector *connector = drmModeGetConnector(fd, pipe.connector_id);
188 assert(connector);
189 drmModeModeInfo *mode = &connector->modes[0];
190
191 struct bs_egl *egl = bs_egl_new();
192 if (!bs_egl_setup(egl)) {
193 bs_debug_error("failed to setup egl context");
194 return 1;
195 }
196
Gurchetan Singh10008e62017-03-09 11:14:38 -0800197 struct gbm_bo *bos[NUM_BUFFERS];
198 uint32_t ids[NUM_BUFFERS];
199 struct bs_egl_fb *egl_fbs[NUM_BUFFERS];
200 EGLImageKHR egl_images[NUM_BUFFERS];
201 for (size_t fb_index = 0; fb_index < NUM_BUFFERS; fb_index++) {
Daniele Castagna8a63a602016-10-10 18:43:29 -0400202 if (test_page_flip_format_change && fb_index) {
Daniele Castagna72729122017-10-18 17:09:36 -0400203 format = test_page_flip_format_change;
Daniele Castagna8a63a602016-10-10 18:43:29 -0400204 }
205
Fritz Koenig01ee3d42018-11-07 10:42:17 -0800206 if (modifier != DRM_FORMAT_MOD_INVALID) {
207 bos[fb_index] = gbm_bo_create_with_modifiers(
208 gbm, mode->hdisplay, mode->vdisplay, format, &modifier, 1);
209 } else {
210 bos[fb_index] = gbm_bo_create(gbm, mode->hdisplay, mode->vdisplay, format,
211 GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING);
212 }
213
Zach Reizner38f0fd92016-02-18 14:28:02 -0800214 if (bos[fb_index] == NULL) {
215 bs_debug_error("failed to allocate framebuffer");
216 return 1;
217 }
218
219 ids[fb_index] = bs_drm_fb_create_gbm(bos[fb_index]);
220 if (ids[fb_index] == 0) {
221 bs_debug_error("failed to create framebuffer id");
222 return 1;
223 }
224
225 EGLImageKHR egl_image = bs_egl_image_create_gbm(egl, bos[fb_index]);
226 if (egl_image == EGL_NO_IMAGE_KHR) {
227 bs_debug_error("failed to create EGLImageKHR from framebuffer");
228 return 1;
229 }
230
231 egl_fbs[fb_index] = bs_egl_fb_new(egl, egl_image);
232 if (!egl_fbs[fb_index]) {
233 bs_debug_error("failed to create framebuffer from EGLImageKHR");
234 return 1;
235 }
Haixia Shife4e2ff2016-10-17 14:28:55 -0700236 egl_images[fb_index] = egl_image;
Zach Reizner38f0fd92016-02-18 14:28:02 -0800237 }
238
239 int ret = drmModeSetCrtc(fd, pipe.crtc_id, ids[0], 0 /* x */, 0 /* y */, &pipe.connector_id,
240 1 /* connector count */, mode);
241 if (ret) {
242 bs_debug_error("failed to set CRTC");
243 return 1;
244 }
245
246 GLuint program = solid_shader_create();
Haixia Shie04ddfd2014-11-11 19:14:32 -0800247 if (!program) {
Zach Reizner38f0fd92016-02-18 14:28:02 -0800248 bs_debug_error("failed to create solid shader");
249 return 1;
Haixia Shie04ddfd2014-11-11 19:14:32 -0800250 }
251
Haixia Shi4652b8c2014-11-19 17:55:38 -0800252 int fb_idx = 1;
Zach Reizner38f0fd92016-02-18 14:28:02 -0800253 for (int i = 0; i <= 500; i++) {
Haixia Shi4652b8c2014-11-19 17:55:38 -0800254 int waiting_for_flip = 1;
Zach Reizner38f0fd92016-02-18 14:28:02 -0800255 // clang-format off
Haixia Shie04ddfd2014-11-11 19:14:32 -0800256 GLfloat verts[] = {
257 0.0f, -0.5f, 0.0f,
258 -0.5f, 0.5f, 0.0f,
259 0.5f, 0.5f, 0.0f
260 };
261 GLfloat colors[] = {
262 1.0f, 0.0f, 0.0f, 1.0f,
263 0.0f, 1.0f, 0.0f, 1.0f,
264 0.0f, 0.0f, 1.0f, 1.0f
265 };
Zach Reizner38f0fd92016-02-18 14:28:02 -0800266 // clang-format on
Haixia Shie04ddfd2014-11-11 19:14:32 -0800267
Zach Reizner38f0fd92016-02-18 14:28:02 -0800268 glBindFramebuffer(GL_FRAMEBUFFER, bs_egl_fb_name(egl_fbs[fb_idx]));
269 glViewport(0, 0, (GLint)mode->hdisplay, (GLint)mode->vdisplay);
Haixia Shi4652b8c2014-11-19 17:55:38 -0800270
Haixia Shie04ddfd2014-11-11 19:14:32 -0800271 glClearColor(f(i), f(i + 80), f(i + 160), 0.0f);
272 glClear(GL_COLOR_BUFFER_BIT);
273
274 glUseProgram(program);
275 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, verts);
276 glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, colors);
277 glEnableVertexAttribArray(0);
278 glEnableVertexAttribArray(1);
279 glDrawArrays(GL_TRIANGLES, 0, 3);
280
281 usleep(1e6 / 120); /* 120 Hz */
282 glFinish();
Haixia Shife4e2ff2016-10-17 14:28:55 -0700283
284 if (!bs_egl_image_flush_external(egl, egl_images[fb_idx])) {
285 bs_debug_error("failed to call image_flush_external");
286 return 1;
287 }
288
Zach Reizner38f0fd92016-02-18 14:28:02 -0800289 ret = drmModePageFlip(fd, pipe.crtc_id, ids[fb_idx], DRM_MODE_PAGE_FLIP_EVENT,
290 &waiting_for_flip);
291 if (ret) {
292 bs_debug_error("failed page flip: %d", ret);
293 return 1;
294 }
Haixia Shi4652b8c2014-11-19 17:55:38 -0800295
296 while (waiting_for_flip) {
297 drmEventContext evctx = {
Zach Reizner4dfdf602016-03-01 13:06:30 -0800298 .version = DRM_EVENT_CONTEXT_VERSION,
299 .page_flip_handler = page_flip_handler,
Haixia Shi4652b8c2014-11-19 17:55:38 -0800300 };
301
302 fd_set fds;
303 FD_ZERO(&fds);
Zach Reizner38f0fd92016-02-18 14:28:02 -0800304 FD_SET(fd, &fds);
Haixia Shi4652b8c2014-11-19 17:55:38 -0800305
Zach Reizner38f0fd92016-02-18 14:28:02 -0800306 ret = select(fd + 1, &fds, NULL, NULL, NULL);
Haixia Shi4652b8c2014-11-19 17:55:38 -0800307 if (ret < 0) {
Zach Reizner38f0fd92016-02-18 14:28:02 -0800308 bs_debug_error("select err: %s", strerror(errno));
309 return 1;
Zach Reiznerbf26be82016-09-15 16:06:21 -0700310 } else if (ret == 0) {
Zach Reizner38f0fd92016-02-18 14:28:02 -0800311 bs_debug_error("select timeout");
312 return 1;
313 }
Zach Reizner38f0fd92016-02-18 14:28:02 -0800314 ret = drmHandleEvent(fd, &evctx);
315 if (ret) {
316 bs_debug_error("failed to wait for page flip: %d", ret);
317 return 1;
318 }
Haixia Shi4652b8c2014-11-19 17:55:38 -0800319 }
320 fb_idx = fb_idx ^ 1;
Haixia Shie04ddfd2014-11-11 19:14:32 -0800321 }
322
Gurchetan Singh10008e62017-03-09 11:14:38 -0800323 for (size_t fb_index = 0; fb_index < NUM_BUFFERS; fb_index++) {
324 bs_egl_fb_destroy(&egl_fbs[fb_index]);
325 bs_egl_image_destroy(egl, &egl_images[fb_index]);
326 drmModeRmFB(fd, ids[fb_idx]);
327 gbm_bo_destroy(bos[fb_index]);
328 }
329
330 bs_egl_destroy(&egl);
331 gbm_device_destroy(gbm);
332 close(fd);
Zach Reizner38f0fd92016-02-18 14:28:02 -0800333 return 0;
Haixia Shie04ddfd2014-11-11 19:14:32 -0800334}