blob: 9eebfd0a0cab8e778ec826eb93f5d1f95a8eae65 [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' },
87 { "test-page-flip-format-change", required_argument, NULL, 'p' },
Daniele Castagna8a63a602016-10-10 18:43:29 -040088 { "help", no_argument, NULL, 'h' },
89 { 0, 0, 0, 0 },
90};
91
92static void print_help(const char *argv0)
93{
Daniele Castagna72729122017-10-18 17:09:36 -040094 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 Castagna8a63a602016-10-10 18:43:29 -0400103 // clang-format off
104 printf("usage: %s [OPTIONS] [drm_device_path]\n", argv0);
Daniele Castagna72729122017-10-18 17:09:36 -0400105 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 Castagna8a63a602016-10-10 18:43:29 -0400107 printf(" -h, --help show help\n");
108 printf("\n");
Daniele Castagna72729122017-10-18 17:09:36 -0400109 printf(" <format> must be one of [ %s ].\n", allowed_formats_string);
110 printf("\n");
Daniele Castagna8a63a602016-10-10 18:43:29 -0400111 printf("\n");
112 // clang-format on
113}
114
Zach Reizner38f0fd92016-02-18 14:28:02 -0800115int main(int argc, char **argv)
Haixia Shie04ddfd2014-11-11 19:14:32 -0800116{
Zach Reizner38f0fd92016-02-18 14:28:02 -0800117 int fd = -1;
Daniele Castagna8a63a602016-10-10 18:43:29 -0400118 bool help_flag = false;
Daniele Castagna72729122017-10-18 17:09:36 -0400119 uint32_t format = GBM_FORMAT_XRGB8888;
Daniele Castagna95d027f2017-11-01 21:56:58 -0400120 uint32_t test_page_flip_format_change = 0;
Daniele Castagna8a63a602016-10-10 18:43:29 -0400121
122 int c = -1;
Daniele Castagna72729122017-10-18 17:09:36 -0400123 while ((c = getopt_long(argc, argv, "hp:f:", longopts, NULL)) != -1) {
Daniele Castagna8a63a602016-10-10 18:43:29 -0400124 switch (c) {
Daniele Castagna72729122017-10-18 17:09:36 -0400125 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 Castagna8a63a602016-10-10 18:43:29 -0400130 case 'f':
Daniele Castagna72729122017-10-18 17:09:36 -0400131 format = find_format(optarg);
132 if (!format)
133 help_flag = true;
Daniele Castagna8a63a602016-10-10 18:43:29 -0400134 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 Reizner38f0fd92016-02-18 14:28:02 -0800147 if (fd < 0) {
Daniele Castagna8a63a602016-10-10 18:43:29 -0400148 bs_debug_error("failed to open card %s", argv[optind]);
Zach Reizner38f0fd92016-02-18 14:28:02 -0800149 return 1;
150 }
Zach Reiznerbf26be82016-09-15 16:06:21 -0700151 } else {
Zach Reizner38f0fd92016-02-18 14:28:02 -0800152 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 Shie04ddfd2014-11-11 19:14:32 -0800157 }
158
Zach Reizner38f0fd92016-02-18 14:28:02 -0800159 struct gbm_device *gbm = gbm_create_device(fd);
160 if (!gbm) {
161 bs_debug_error("failed to create gbm");
162 return 1;
Haixia Shie04ddfd2014-11-11 19:14:32 -0800163 }
164
Zach Reizner4dfdf602016-03-01 13:06:30 -0800165 struct bs_drm_pipe pipe = { 0 };
Zach Reizner38f0fd92016-02-18 14:28:02 -0800166 if (!bs_drm_pipe_make(fd, &pipe)) {
167 bs_debug_error("failed to make pipe");
168 return 1;
169 }
Haixia Shie04ddfd2014-11-11 19:14:32 -0800170
Zach Reizner38f0fd92016-02-18 14:28:02 -0800171 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 Singh10008e62017-03-09 11:14:38 -0800181 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 Castagna8a63a602016-10-10 18:43:29 -0400186 if (test_page_flip_format_change && fb_index) {
Daniele Castagna72729122017-10-18 17:09:36 -0400187 format = test_page_flip_format_change;
Daniele Castagna8a63a602016-10-10 18:43:29 -0400188 }
189
Gurchetan Singh43380142017-03-09 10:45:01 -0800190 bos[fb_index] = gbm_bo_create(gbm, mode->hdisplay, mode->vdisplay, format,
191 GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING);
Zach Reizner38f0fd92016-02-18 14:28:02 -0800192 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 Shife4e2ff2016-10-17 14:28:55 -0700214 egl_images[fb_index] = egl_image;
Zach Reizner38f0fd92016-02-18 14:28:02 -0800215 }
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 Shie04ddfd2014-11-11 19:14:32 -0800225 if (!program) {
Zach Reizner38f0fd92016-02-18 14:28:02 -0800226 bs_debug_error("failed to create solid shader");
227 return 1;
Haixia Shie04ddfd2014-11-11 19:14:32 -0800228 }
229
Haixia Shi4652b8c2014-11-19 17:55:38 -0800230 int fb_idx = 1;
Zach Reizner38f0fd92016-02-18 14:28:02 -0800231 for (int i = 0; i <= 500; i++) {
Haixia Shi4652b8c2014-11-19 17:55:38 -0800232 int waiting_for_flip = 1;
Zach Reizner38f0fd92016-02-18 14:28:02 -0800233 // clang-format off
Haixia Shie04ddfd2014-11-11 19:14:32 -0800234 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 Reizner38f0fd92016-02-18 14:28:02 -0800244 // clang-format on
Haixia Shie04ddfd2014-11-11 19:14:32 -0800245
Zach Reizner38f0fd92016-02-18 14:28:02 -0800246 glBindFramebuffer(GL_FRAMEBUFFER, bs_egl_fb_name(egl_fbs[fb_idx]));
247 glViewport(0, 0, (GLint)mode->hdisplay, (GLint)mode->vdisplay);
Haixia Shi4652b8c2014-11-19 17:55:38 -0800248
Haixia Shie04ddfd2014-11-11 19:14:32 -0800249 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 Shife4e2ff2016-10-17 14:28:55 -0700261
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 Reizner38f0fd92016-02-18 14:28:02 -0800267 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 Shi4652b8c2014-11-19 17:55:38 -0800273
274 while (waiting_for_flip) {
275 drmEventContext evctx = {
Zach Reizner4dfdf602016-03-01 13:06:30 -0800276 .version = DRM_EVENT_CONTEXT_VERSION,
277 .page_flip_handler = page_flip_handler,
Haixia Shi4652b8c2014-11-19 17:55:38 -0800278 };
279
280 fd_set fds;
281 FD_ZERO(&fds);
Zach Reizner38f0fd92016-02-18 14:28:02 -0800282 FD_SET(fd, &fds);
Haixia Shi4652b8c2014-11-19 17:55:38 -0800283
Zach Reizner38f0fd92016-02-18 14:28:02 -0800284 ret = select(fd + 1, &fds, NULL, NULL, NULL);
Haixia Shi4652b8c2014-11-19 17:55:38 -0800285 if (ret < 0) {
Zach Reizner38f0fd92016-02-18 14:28:02 -0800286 bs_debug_error("select err: %s", strerror(errno));
287 return 1;
Zach Reiznerbf26be82016-09-15 16:06:21 -0700288 } else if (ret == 0) {
Zach Reizner38f0fd92016-02-18 14:28:02 -0800289 bs_debug_error("select timeout");
290 return 1;
291 }
Zach Reizner38f0fd92016-02-18 14:28:02 -0800292 ret = drmHandleEvent(fd, &evctx);
293 if (ret) {
294 bs_debug_error("failed to wait for page flip: %d", ret);
295 return 1;
296 }
Haixia Shi4652b8c2014-11-19 17:55:38 -0800297 }
298 fb_idx = fb_idx ^ 1;
Haixia Shie04ddfd2014-11-11 19:14:32 -0800299 }
300
Gurchetan Singh10008e62017-03-09 11:14:38 -0800301 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 Reizner38f0fd92016-02-18 14:28:02 -0800311 return 0;
Haixia Shie04ddfd2014-11-11 19:14:32 -0800312}