blob: d9fca2cbf9371edd00d5afaa1dddaf4d060a029b [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
Zach Reizner4dfdf602016-03-01 13:06:30 -08009static GLuint solid_shader_create()
Haixia Shie04ddfd2014-11-11 19:14:32 -080010{
Zach Reizner4dfdf602016-03-01 13:06:30 -080011 const GLchar *vert =
Zach Reizner38f0fd92016-02-18 14:28:02 -080012 "attribute vec4 vPosition;\n"
13 "attribute vec4 vColor;\n"
14 "varying vec4 vFillColor;\n"
15 "void main() {\n"
16 " gl_Position = vPosition;\n"
17 " vFillColor = vColor;\n"
18 "}\n";
Haixia Shie04ddfd2014-11-11 19:14:32 -080019
Zach Reizner4dfdf602016-03-01 13:06:30 -080020 const GLchar *frag =
Zach Reizner38f0fd92016-02-18 14:28:02 -080021 "precision mediump float;\n"
22 "varying vec4 vFillColor;\n"
23 "void main() {\n"
24 " gl_FragColor = vFillColor;\n"
25 "}\n";
Lauri Peltonen763ca462014-12-17 12:22:21 -080026
Zach Reizner4dfdf602016-03-01 13:06:30 -080027 struct bs_gl_program_create_binding bindings[] = {
28 { 0, "vPosition" }, { 1, "vColor" }, { 0, NULL },
29 };
Haixia Shie04ddfd2014-11-11 19:14:32 -080030
Zach Reizner4dfdf602016-03-01 13:06:30 -080031 return bs_gl_program_create_vert_frag_bind(vert, frag, bindings);
Haixia Shie04ddfd2014-11-11 19:14:32 -080032}
33
Lauri Peltonen95226da2014-12-18 16:39:54 -080034static float f(int i)
35{
Haixia Shie04ddfd2014-11-11 19:14:32 -080036 int a = i % 40;
37 int b = (i / 40) % 6;
38 switch (b) {
Zach Reizner38f0fd92016-02-18 14:28:02 -080039 case 0:
40 case 1:
41 return 0.0f;
42 case 3:
43 case 4:
44 return 1.0f;
45 case 2:
46 return (a / 40.0f);
47 case 5:
48 return 1.0f - (a / 40.0f);
49 default:
50 return 0.0f;
Haixia Shie04ddfd2014-11-11 19:14:32 -080051 }
52}
53
Zach Reizner38f0fd92016-02-18 14:28:02 -080054static void page_flip_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
55 void *data)
Haixia Shi4652b8c2014-11-19 17:55:38 -080056{
57 int *waiting_for_flip = data;
58 *waiting_for_flip = 0;
59}
60
Zach Reizner38f0fd92016-02-18 14:28:02 -080061int main(int argc, char **argv)
Haixia Shie04ddfd2014-11-11 19:14:32 -080062{
Zach Reizner38f0fd92016-02-18 14:28:02 -080063 int fd = -1;
64 if (argc >= 2) {
65 fd = open(argv[1], O_RDWR);
66 if (fd < 0) {
67 bs_debug_error("failed to open card %s", argv[1]);
68 return 1;
69 }
Zach Reiznerbf26be82016-09-15 16:06:21 -070070 } else {
Zach Reizner38f0fd92016-02-18 14:28:02 -080071 fd = bs_drm_open_main_display();
72 if (fd < 0) {
73 bs_debug_error("failed to open card for display");
74 return 1;
75 }
Haixia Shie04ddfd2014-11-11 19:14:32 -080076 }
77
Zach Reizner38f0fd92016-02-18 14:28:02 -080078 struct gbm_device *gbm = gbm_create_device(fd);
79 if (!gbm) {
80 bs_debug_error("failed to create gbm");
81 return 1;
Haixia Shie04ddfd2014-11-11 19:14:32 -080082 }
83
Zach Reizner4dfdf602016-03-01 13:06:30 -080084 struct bs_drm_pipe pipe = { 0 };
Zach Reizner38f0fd92016-02-18 14:28:02 -080085 if (!bs_drm_pipe_make(fd, &pipe)) {
86 bs_debug_error("failed to make pipe");
87 return 1;
88 }
Haixia Shie04ddfd2014-11-11 19:14:32 -080089
Zach Reizner38f0fd92016-02-18 14:28:02 -080090 drmModeConnector *connector = drmModeGetConnector(fd, pipe.connector_id);
91 assert(connector);
92 drmModeModeInfo *mode = &connector->modes[0];
93
94 struct bs_egl *egl = bs_egl_new();
95 if (!bs_egl_setup(egl)) {
96 bs_debug_error("failed to setup egl context");
97 return 1;
98 }
99
100 struct gbm_bo *bos[2];
101 uint32_t ids[2];
102 struct bs_egl_fb *egl_fbs[2];
103 for (size_t fb_index = 0; fb_index < 2; fb_index++) {
104 bos[fb_index] =
105 gbm_bo_create(gbm, mode->hdisplay, mode->vdisplay, GBM_FORMAT_XRGB8888,
106 GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING);
107 if (bos[fb_index] == NULL) {
108 bs_debug_error("failed to allocate framebuffer");
109 return 1;
110 }
111
112 ids[fb_index] = bs_drm_fb_create_gbm(bos[fb_index]);
113 if (ids[fb_index] == 0) {
114 bs_debug_error("failed to create framebuffer id");
115 return 1;
116 }
117
118 EGLImageKHR egl_image = bs_egl_image_create_gbm(egl, bos[fb_index]);
119 if (egl_image == EGL_NO_IMAGE_KHR) {
120 bs_debug_error("failed to create EGLImageKHR from framebuffer");
121 return 1;
122 }
123
124 egl_fbs[fb_index] = bs_egl_fb_new(egl, egl_image);
125 if (!egl_fbs[fb_index]) {
126 bs_debug_error("failed to create framebuffer from EGLImageKHR");
127 return 1;
128 }
129 }
130
131 int ret = drmModeSetCrtc(fd, pipe.crtc_id, ids[0], 0 /* x */, 0 /* y */, &pipe.connector_id,
132 1 /* connector count */, mode);
133 if (ret) {
134 bs_debug_error("failed to set CRTC");
135 return 1;
136 }
137
138 GLuint program = solid_shader_create();
Haixia Shie04ddfd2014-11-11 19:14:32 -0800139 if (!program) {
Zach Reizner38f0fd92016-02-18 14:28:02 -0800140 bs_debug_error("failed to create solid shader");
141 return 1;
Haixia Shie04ddfd2014-11-11 19:14:32 -0800142 }
143
Haixia Shi4652b8c2014-11-19 17:55:38 -0800144 int fb_idx = 1;
Zach Reizner38f0fd92016-02-18 14:28:02 -0800145 for (int i = 0; i <= 500; i++) {
Haixia Shi4652b8c2014-11-19 17:55:38 -0800146 int waiting_for_flip = 1;
Zach Reizner38f0fd92016-02-18 14:28:02 -0800147 // clang-format off
Haixia Shie04ddfd2014-11-11 19:14:32 -0800148 GLfloat verts[] = {
149 0.0f, -0.5f, 0.0f,
150 -0.5f, 0.5f, 0.0f,
151 0.5f, 0.5f, 0.0f
152 };
153 GLfloat colors[] = {
154 1.0f, 0.0f, 0.0f, 1.0f,
155 0.0f, 1.0f, 0.0f, 1.0f,
156 0.0f, 0.0f, 1.0f, 1.0f
157 };
Zach Reizner38f0fd92016-02-18 14:28:02 -0800158 // clang-format on
Haixia Shie04ddfd2014-11-11 19:14:32 -0800159
Zach Reizner38f0fd92016-02-18 14:28:02 -0800160 glBindFramebuffer(GL_FRAMEBUFFER, bs_egl_fb_name(egl_fbs[fb_idx]));
161 glViewport(0, 0, (GLint)mode->hdisplay, (GLint)mode->vdisplay);
Haixia Shi4652b8c2014-11-19 17:55:38 -0800162
Haixia Shie04ddfd2014-11-11 19:14:32 -0800163 glClearColor(f(i), f(i + 80), f(i + 160), 0.0f);
164 glClear(GL_COLOR_BUFFER_BIT);
165
166 glUseProgram(program);
167 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, verts);
168 glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, colors);
169 glEnableVertexAttribArray(0);
170 glEnableVertexAttribArray(1);
171 glDrawArrays(GL_TRIANGLES, 0, 3);
172
173 usleep(1e6 / 120); /* 120 Hz */
174 glFinish();
Zach Reizner38f0fd92016-02-18 14:28:02 -0800175 ret = drmModePageFlip(fd, pipe.crtc_id, ids[fb_idx], DRM_MODE_PAGE_FLIP_EVENT,
176 &waiting_for_flip);
177 if (ret) {
178 bs_debug_error("failed page flip: %d", ret);
179 return 1;
180 }
Haixia Shi4652b8c2014-11-19 17:55:38 -0800181
182 while (waiting_for_flip) {
183 drmEventContext evctx = {
Zach Reizner4dfdf602016-03-01 13:06:30 -0800184 .version = DRM_EVENT_CONTEXT_VERSION,
185 .page_flip_handler = page_flip_handler,
Haixia Shi4652b8c2014-11-19 17:55:38 -0800186 };
187
188 fd_set fds;
189 FD_ZERO(&fds);
Zach Reizner38f0fd92016-02-18 14:28:02 -0800190 FD_SET(fd, &fds);
Haixia Shi4652b8c2014-11-19 17:55:38 -0800191
Zach Reizner38f0fd92016-02-18 14:28:02 -0800192 ret = select(fd + 1, &fds, NULL, NULL, NULL);
Haixia Shi4652b8c2014-11-19 17:55:38 -0800193 if (ret < 0) {
Zach Reizner38f0fd92016-02-18 14:28:02 -0800194 bs_debug_error("select err: %s", strerror(errno));
195 return 1;
Zach Reiznerbf26be82016-09-15 16:06:21 -0700196 } else if (ret == 0) {
Zach Reizner38f0fd92016-02-18 14:28:02 -0800197 bs_debug_error("select timeout");
198 return 1;
199 }
Zach Reizner38f0fd92016-02-18 14:28:02 -0800200 ret = drmHandleEvent(fd, &evctx);
201 if (ret) {
202 bs_debug_error("failed to wait for page flip: %d", ret);
203 return 1;
204 }
Haixia Shi4652b8c2014-11-19 17:55:38 -0800205 }
206 fb_idx = fb_idx ^ 1;
Haixia Shie04ddfd2014-11-11 19:14:32 -0800207 }
208
Zach Reizner38f0fd92016-02-18 14:28:02 -0800209 return 0;
Haixia Shie04ddfd2014-11-11 19:14:32 -0800210}