blob: 2524c8ecf49f521d3a3043a45d569ad2404d49a6 [file] [log] [blame]
Benjamin Franzkeaabdce02011-01-15 00:40:17 +01001/*
2 * Copyright © 2011 Benjamin Franzke
3 *
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.
13 *
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
20 * OF THIS SOFTWARE.
21 */
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <stddef.h>
27#include <stdint.h>
28#include <stdbool.h>
29#include <math.h>
30#include <assert.h>
31
32#include <fcntl.h>
33
34#include <wayland-client.h>
35#include <xf86drm.h>
36
37#define GL_GLEXT_PROTOTYPES
38#define EGL_EGLEXT_PROTOTYPES
39#include <GLES2/gl2.h>
40#include <GLES2/gl2ext.h>
41#include <EGL/egl.h>
42#include <EGL/eglext.h>
43
44struct display {
45 struct wl_display *display;
46 struct {
47 struct wl_compositor *compositor;
48 struct wl_drm *drm;
49 } interface;
50 struct {
51 EGLDisplay dpy;
52 EGLContext ctx;
53 } egl;
54 struct {
55 int fd;
56 const char *device_name;
57 bool authenticated;
58 } drm;
59 uint32_t mask;
60};
61
62struct window {
63 struct display *display;
64 struct {
65 int width, height;
66 } geometry;
67 struct {
68 GLuint fbo;
69 GLuint color_rbo;
70
71 GLuint program;
72 GLuint rotation_uniform;
73
74 GLuint pos;
75 GLuint col;
76 } gl;
77 struct {
78 struct wl_buffer *buffer;
79 struct wl_surface *surface;
80 EGLImageKHR image;
81 } drm_surface;
82};
83
84static 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"
89 "void main() {\n"
90 " gl_Position = rotation * pos;\n"
91 " v_color = color;\n"
92 "}\n";
93
94static const char *frag_shader_text =
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -050095 "precision mediump float;\n"
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010096 "varying vec4 v_color;\n"
97 "void main() {\n"
98 " gl_FragColor = v_color;\n"
99 "}\n";
100
101static void
102init_egl(struct display *display)
103{
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500104 static const EGLint context_attribs[] = {
105 EGL_CONTEXT_CLIENT_VERSION, 2,
106 EGL_NONE
107 };
108
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100109 EGLint major, minor;
110 EGLBoolean ret;
111
112 display->egl.dpy = eglGetDRMDisplayMESA(display->drm.fd);
113 assert(display->egl.dpy);
114
115 ret = eglInitialize(display->egl.dpy, &major, &minor);
116 assert(ret == EGL_TRUE);
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500117 ret = eglBindAPI(EGL_OPENGL_ES_API);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100118 assert(ret == EGL_TRUE);
119
120 display->egl.ctx = eglCreateContext(display->egl.dpy, NULL,
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500121 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100122 assert(display->egl.ctx);
123 ret = eglMakeCurrent(display->egl.dpy, NULL, NULL, display->egl.ctx);
124 assert(ret == EGL_TRUE);
125}
126
127static GLuint
128create_shader(struct window *window, const char *source, GLenum shader_type)
129{
130 GLuint shader;
131 GLint status;
132
133 shader = glCreateShader(shader_type);
134 assert(shader != 0);
135
136 glShaderSource(shader, 1, (const char **) &source, NULL);
137 glCompileShader(shader);
138
139 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
140 if (!status) {
141 char log[1000];
142 GLsizei len;
143 glGetShaderInfoLog(shader, 1000, &len, log);
144 fprintf(stderr, "Error: compiling %s: %*s\n",
145 shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
146 len, log);
147 exit(1);
148 }
149
150 return shader;
151}
152
153static void
154init_gl(struct window *window)
155{
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100156 GLuint frag, vert;
157 GLint status;
158
159 glGenFramebuffers(1, &window->gl.fbo);
160 glBindFramebuffer(GL_FRAMEBUFFER, window->gl.fbo);
161
162 glGenRenderbuffers(1, &window->gl.color_rbo);
163
164 glViewport(0, 0, window->geometry.width, window->geometry.height);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100165
166 frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
167 vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
168
169 window->gl.program = glCreateProgram();
170 glAttachShader(window->gl.program, frag);
171 glAttachShader(window->gl.program, vert);
172 glLinkProgram(window->gl.program);
173
174 glGetProgramiv(window->gl.program, GL_LINK_STATUS, &status);
175 if (!status) {
176 char log[1000];
177 GLsizei len;
178 glGetProgramInfoLog(window->gl.program, 1000, &len, log);
179 fprintf(stderr, "Error: linking:\n%*s\n", len, log);
180 exit(1);
181 }
182
183 glUseProgram(window->gl.program);
184
185 window->gl.pos = 0;
186 window->gl.pos = 1;
187
188 glBindAttribLocation(window->gl.program, window->gl.pos, "pos");
189 glBindAttribLocation(window->gl.program, window->gl.col, "color");
190 glLinkProgram(window->gl.program);
191
192 window->gl.rotation_uniform =
193 glGetUniformLocation(window->gl.program, "rotation");
194}
195
196static void
197create_surface(struct window *window)
198{
199 struct display *display = window->display;
200 struct wl_visual *visual;
201 EGLint name, stride;
202 EGLint image_attribs[] = {
203 EGL_WIDTH, 0,
204 EGL_HEIGHT, 0,
205 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
206 EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
207 EGL_NONE
208 };
209
210 window->drm_surface.surface =
211 wl_compositor_create_surface(display->interface.compositor);
212
213 image_attribs[1] = window->geometry.width;
214 image_attribs[3] = window->geometry.height;
215
216 window->drm_surface.image = eglCreateDRMImageMESA(display->egl.dpy,
217 image_attribs);
218 eglExportDRMImageMESA(display->egl.dpy, window->drm_surface.image,
219 &name, NULL, &stride);
220 visual = wl_display_get_premultiplied_argb_visual(display->display);
221
222 window->drm_surface.buffer =
223 wl_drm_create_buffer(display->interface.drm, name,
224 window->geometry.width,
225 window->geometry.height,
226 stride, visual);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100227
228 wl_surface_attach(window->drm_surface.surface,
229 window->drm_surface.buffer, 0, 0);
230 wl_surface_map_toplevel(window->drm_surface.surface);
231
232 glBindRenderbuffer(GL_RENDERBUFFER, window->gl.color_rbo);
233 glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER,
234 window->drm_surface.image);
235
236 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
237 GL_COLOR_ATTACHMENT0,
238 GL_RENDERBUFFER,
239 window->gl.color_rbo);
240
241 assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) ==
242 GL_FRAMEBUFFER_COMPLETE);
243}
244
245static void
246redraw(void *data, uint32_t time)
247{
248 struct window *window = data;
249 static const GLfloat verts[3][2] = {
250 { -0.5, -0.5 },
251 { 0.5, -0.5 },
252 { 0, 0.5 }
253 };
254 static const GLfloat colors[3][3] = {
255 { 1, 0, 0 },
256 { 0, 1, 0 },
257 { 0, 0, 1 }
258 };
259 GLfloat angle;
260 GLfloat rotation[4][4] = {
261 { 1, 0, 0, 0 },
262 { 0, 1, 0, 0 },
263 { 0, 0, 1, 0 },
264 { 0, 0, 0, 1 }
265 };
266 static const int32_t speed_div = 5;
267 static uint32_t start_time = 0;
268
269 if (start_time == 0)
270 start_time = time;
271
272 angle = ((time-start_time) / speed_div) % 360 * M_PI / 180.0;
273 rotation[0][0] = cos(angle);
274 rotation[0][2] = sin(angle);
275 rotation[2][0] = -sin(angle);
276 rotation[2][2] = cos(angle);
277
278 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
279 (GLfloat *) rotation);
280
281 glClearColor(0.0, 0.0, 0.0, 0.5);
282 glClear(GL_COLOR_BUFFER_BIT);
283
284 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
285 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
286 glEnableVertexAttribArray(window->gl.pos);
287 glEnableVertexAttribArray(window->gl.col);
288
289 glDrawArrays(GL_TRIANGLES, 0, 3);
290
291 glDisableVertexAttribArray(window->gl.pos);
292 glDisableVertexAttribArray(window->gl.col);
293
294 glFlush();
295
296 wl_surface_damage(window->drm_surface.surface, 0, 0,
297 window->geometry.width, window->geometry.height);
298
299 wl_display_frame_callback(window->display->display, redraw, window);
300}
301
302static void
303drm_handle_device(void *data, struct wl_drm *drm, const char *device)
304{
305 struct display *d = data;
306
307 d->drm.device_name = strdup(device);
308}
309
310static void
311drm_handle_authenticated(void *data, struct wl_drm *drm)
312{
313 struct display *d = data;
314
315 d->drm.authenticated = true;
316}
317
318static const struct wl_drm_listener drm_listener = {
319 drm_handle_device,
320 drm_handle_authenticated
321};
322
323static void
324display_handle_global(struct wl_display *display, uint32_t id,
325 const char *interface, uint32_t version, void *data)
326{
327 struct display *d = data;
328
329 if (strcmp(interface, "compositor") == 0) {
330 d->interface.compositor = wl_compositor_create(display, id);
331 } else if (strcmp(interface, "drm") == 0) {
332 d->interface.drm = wl_drm_create(display, id);
333 wl_drm_add_listener(d->interface.drm, &drm_listener, d);
334 }
335}
336
337static int
338event_mask_update(uint32_t mask, void *data)
339{
340 struct display *d = data;
341
342 d->mask = mask;
343
344 return 0;
345}
346
347int
348main(int argc, char **argv)
349{
350 struct display display = { 0 };
351 struct window window = { 0 };
352 drm_magic_t magic;
353 int ret;
354
355 memset(&display, 0, sizeof display);
356 memset(&window, 0, sizeof window);
357
358 window.display = &display;
359 window.geometry.width = 250;
360 window.geometry.height = 250;
361
362 display.display = wl_display_connect(NULL);
363 assert(display.display);
364
365 wl_display_add_global_listener(display.display,
366 display_handle_global, &display);
367 /* process connection events */
368 wl_display_iterate(display.display, WL_DISPLAY_READABLE);
369
370 display.drm.fd = open(display.drm.device_name, O_RDWR);
371 assert(display.drm.fd >= 0);
372
373 ret = drmGetMagic(display.drm.fd, &magic);
374 assert(ret == 0);
375 wl_drm_authenticate(display.interface.drm, magic);
376 wl_display_iterate(display.display, WL_DISPLAY_WRITABLE);
377 while (!display.drm.authenticated)
378 wl_display_iterate(display.display, WL_DISPLAY_READABLE);
379
380 init_egl(&display);
381 init_gl(&window);
382 create_surface(&window);
383
384 wl_display_frame_callback(display.display, redraw, &window);
385
386 wl_display_get_fd(display.display, event_mask_update, &display);
387 while (true)
388 wl_display_iterate(display.display, display.mask);
389
390 return 0;
391}