2 * Copyright © 2011 Benjamin Franzke
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
34 #include <wayland-client.h>
37 #define GL_GLEXT_PROTOTYPES
38 #define EGL_EGLEXT_PROTOTYPES
39 #include <GLES2/gl2.h>
40 #include <GLES2/gl2ext.h>
42 #include <EGL/eglext.h>
45 struct wl_display *display;
47 struct wl_compositor *compositor;
56 const char *device_name;
63 struct display *display;
72 GLuint rotation_uniform;
78 struct wl_buffer *buffer;
79 struct wl_surface *surface;
84 static const char *vert_shader_text =
85 "uniform mat4 rotation;\n"
86 "attribute vec4 pos;\n"
87 "attribute vec4 color;\n"
88 "varying vec4 v_color;\n"
90 " gl_Position = rotation * pos;\n"
94 static const char *frag_shader_text =
95 "precision mediump float;\n"
96 "varying vec4 v_color;\n"
98 " gl_FragColor = v_color;\n"
102 init_egl(struct display *display)
104 static const EGLint context_attribs[] = {
105 EGL_CONTEXT_CLIENT_VERSION, 2,
112 display->egl.dpy = eglGetDRMDisplayMESA(display->drm.fd);
113 assert(display->egl.dpy);
115 ret = eglInitialize(display->egl.dpy, &major, &minor);
116 assert(ret == EGL_TRUE);
117 ret = eglBindAPI(EGL_OPENGL_ES_API);
118 assert(ret == EGL_TRUE);
120 display->egl.ctx = eglCreateContext(display->egl.dpy, NULL,
121 EGL_NO_CONTEXT, context_attribs);
122 assert(display->egl.ctx);
123 ret = eglMakeCurrent(display->egl.dpy, NULL, NULL, display->egl.ctx);
124 assert(ret == EGL_TRUE);
128 create_shader(struct window *window, const char *source, GLenum shader_type)
133 shader = glCreateShader(shader_type);
136 glShaderSource(shader, 1, (const char **) &source, NULL);
137 glCompileShader(shader);
139 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
143 glGetShaderInfoLog(shader, 1000, &len, log);
144 fprintf(stderr, "Error: compiling %s: %*s\n",
145 shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
154 init_gl(struct window *window)
160 glGenFramebuffers(1, &window->gl.fbo);
161 glBindFramebuffer(GL_FRAMEBUFFER, window->gl.fbo);
163 glGenRenderbuffers(1, &window->gl.color_rbo);
165 glViewport(0, 0, window->geometry.width, window->geometry.height);
166 ar = (GLfloat)window->geometry.width / (GLfloat)window->geometry.height;
168 frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
169 vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
171 window->gl.program = glCreateProgram();
172 glAttachShader(window->gl.program, frag);
173 glAttachShader(window->gl.program, vert);
174 glLinkProgram(window->gl.program);
176 glGetProgramiv(window->gl.program, GL_LINK_STATUS, &status);
180 glGetProgramInfoLog(window->gl.program, 1000, &len, log);
181 fprintf(stderr, "Error: linking:\n%*s\n", len, log);
185 glUseProgram(window->gl.program);
190 glBindAttribLocation(window->gl.program, window->gl.pos, "pos");
191 glBindAttribLocation(window->gl.program, window->gl.col, "color");
192 glLinkProgram(window->gl.program);
194 window->gl.rotation_uniform =
195 glGetUniformLocation(window->gl.program, "rotation");
199 create_surface(struct window *window)
201 struct display *display = window->display;
202 struct wl_visual *visual;
204 EGLint image_attribs[] = {
207 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
208 EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
212 window->drm_surface.surface =
213 wl_compositor_create_surface(display->interface.compositor);
215 image_attribs[1] = window->geometry.width;
216 image_attribs[3] = window->geometry.height;
218 window->drm_surface.image = eglCreateDRMImageMESA(display->egl.dpy,
220 eglExportDRMImageMESA(display->egl.dpy, window->drm_surface.image,
221 &name, NULL, &stride);
222 visual = wl_display_get_premultiplied_argb_visual(display->display);
224 window->drm_surface.buffer =
225 wl_drm_create_buffer(display->interface.drm, name,
226 window->geometry.width,
227 window->geometry.height,
229 /* Process wl_drm_create_buffer. */
230 wl_display_iterate(display->display, WL_DISPLAY_WRITABLE);
232 wl_surface_attach(window->drm_surface.surface,
233 window->drm_surface.buffer, 0, 0);
234 wl_surface_map_toplevel(window->drm_surface.surface);
236 glBindRenderbuffer(GL_RENDERBUFFER, window->gl.color_rbo);
237 glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER,
238 window->drm_surface.image);
240 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
241 GL_COLOR_ATTACHMENT0,
243 window->gl.color_rbo);
245 assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) ==
246 GL_FRAMEBUFFER_COMPLETE);
250 redraw(void *data, uint32_t time)
252 struct window *window = data;
253 static const GLfloat verts[3][2] = {
258 static const GLfloat colors[3][3] = {
264 GLfloat rotation[4][4] = {
270 static const int32_t speed_div = 5;
271 static uint32_t start_time = 0;
276 angle = ((time-start_time) / speed_div) % 360 * M_PI / 180.0;
277 rotation[0][0] = cos(angle);
278 rotation[0][2] = sin(angle);
279 rotation[2][0] = -sin(angle);
280 rotation[2][2] = cos(angle);
282 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
283 (GLfloat *) rotation);
285 glClearColor(0.0, 0.0, 0.0, 0.5);
286 glClear(GL_COLOR_BUFFER_BIT);
288 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
289 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
290 glEnableVertexAttribArray(window->gl.pos);
291 glEnableVertexAttribArray(window->gl.col);
293 glDrawArrays(GL_TRIANGLES, 0, 3);
295 glDisableVertexAttribArray(window->gl.pos);
296 glDisableVertexAttribArray(window->gl.col);
300 wl_surface_damage(window->drm_surface.surface, 0, 0,
301 window->geometry.width, window->geometry.height);
303 wl_display_frame_callback(window->display->display, redraw, window);
307 drm_handle_device(void *data, struct wl_drm *drm, const char *device)
309 struct display *d = data;
311 d->drm.device_name = strdup(device);
315 drm_handle_authenticated(void *data, struct wl_drm *drm)
317 struct display *d = data;
319 d->drm.authenticated = true;
322 static const struct wl_drm_listener drm_listener = {
324 drm_handle_authenticated
328 display_handle_global(struct wl_display *display, uint32_t id,
329 const char *interface, uint32_t version, void *data)
331 struct display *d = data;
333 if (strcmp(interface, "compositor") == 0) {
334 d->interface.compositor = wl_compositor_create(display, id);
335 } else if (strcmp(interface, "drm") == 0) {
336 d->interface.drm = wl_drm_create(display, id);
337 wl_drm_add_listener(d->interface.drm, &drm_listener, d);
342 event_mask_update(uint32_t mask, void *data)
344 struct display *d = data;
352 main(int argc, char **argv)
354 struct display display = { 0 };
355 struct window window = { 0 };
359 memset(&display, 0, sizeof display);
360 memset(&window, 0, sizeof window);
362 window.display = &display;
363 window.geometry.width = 250;
364 window.geometry.height = 250;
366 display.display = wl_display_connect(NULL);
367 assert(display.display);
369 wl_display_add_global_listener(display.display,
370 display_handle_global, &display);
371 /* process connection events */
372 wl_display_iterate(display.display, WL_DISPLAY_READABLE);
374 display.drm.fd = open(display.drm.device_name, O_RDWR);
375 assert(display.drm.fd >= 0);
377 ret = drmGetMagic(display.drm.fd, &magic);
379 wl_drm_authenticate(display.interface.drm, magic);
380 wl_display_iterate(display.display, WL_DISPLAY_WRITABLE);
381 while (!display.drm.authenticated)
382 wl_display_iterate(display.display, WL_DISPLAY_READABLE);
386 create_surface(&window);
388 wl_display_frame_callback(display.display, redraw, &window);
390 wl_display_get_fd(display.display, event_mask_update, &display);
392 wl_display_iterate(display.display, display.mask);