2 * Copyright © 2008 Kristian Høgsberg
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
33 #include <cairo-drm.h>
35 #define GL_GLEXT_PROTOTYPES
36 #define EGL_EGLEXT_PROTOTYPES
39 #include <EGL/eglext.h>
41 #include "wayland-util.h"
42 #include "wayland-client.h"
43 #include "wayland-glib.h"
47 static const char gem_device[] = "/dev/dri/card0";
48 static const char socket_name[] = "\0wayland";
51 struct window *window;
54 struct wl_compositor *compositor;
55 struct rectangle rectangle;
63 cairo_surface_t *cairo_surface;
66 GLuint fbo, color_rbo, depth_rbo;
69 struct gear_template {
78 const static struct gear_template gear_templates[] = {
79 { { 0.8, 0.1, 0.0, 1.0 }, 1.0, 4.0, 1.0, 20, 0.7 },
80 { { 0.0, 0.8, 0.2, 1.0 }, 0.5, 2.0, 2.0, 10, 0.7 },
81 { { 0.2, 0.2, 1.0, 1.0 }, 1.3, 2.0, 0.5, 10, 0.7 },
84 static GLfloat light_pos[4] = {5.0, 5.0, 10.0, 0.0};
86 static void die(const char *msg)
88 fprintf(stderr, "%s", msg);
93 make_gear(const struct gear_template *t)
100 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, t->material);
102 r0 = t->inner_radius;
103 r1 = t->outer_radius - t->tooth_depth / 2.0;
104 r2 = t->outer_radius + t->tooth_depth / 2.0;
106 da = 2.0 * M_PI / t->teeth / 4.0;
108 glShadeModel(GL_FLAT);
110 glNormal3f(0.0, 0.0, 1.0);
112 /* draw front face */
113 glBegin(GL_QUAD_STRIP);
114 for (i = 0; i <= t->teeth; i++) {
115 angle = i * 2.0 * M_PI / t->teeth;
116 glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
117 glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
119 glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
120 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
125 /* draw front sides of teeth */
127 da = 2.0 * M_PI / t->teeth / 4.0;
128 for (i = 0; i < t->teeth; i++) {
129 angle = i * 2.0 * M_PI / t->teeth;
131 glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
132 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), t->width * 0.5);
133 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), t->width * 0.5);
134 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
138 glNormal3f(0.0, 0.0, -1.0);
141 glBegin(GL_QUAD_STRIP);
142 for (i = 0; i <= t->teeth; i++) {
143 angle = i * 2.0 * M_PI / t->teeth;
144 glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
145 glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
147 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
148 glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
153 /* draw back sides of teeth */
155 da = 2.0 * M_PI / t->teeth / 4.0;
156 for (i = 0; i < t->teeth; i++) {
157 angle = i * 2.0 * M_PI / t->teeth;
159 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
160 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -t->width * 0.5);
161 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -t->width * 0.5);
162 glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
166 /* draw outward faces of teeth */
167 glBegin(GL_QUAD_STRIP);
168 for (i = 0; i < t->teeth; i++) {
169 angle = i * 2.0 * M_PI / t->teeth;
171 glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
172 glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
173 u = r2 * cos(angle + da) - r1 * cos(angle);
174 v = r2 * sin(angle + da) - r1 * sin(angle);
175 len = sqrt(u * u + v * v);
178 glNormal3f(v, -u, 0.0);
179 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), t->width * 0.5);
180 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -t->width * 0.5);
181 glNormal3f(cos(angle), sin(angle), 0.0);
182 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), t->width * 0.5);
183 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -t->width * 0.5);
184 u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
185 v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
186 glNormal3f(v, -u, 0.0);
187 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
188 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
189 glNormal3f(cos(angle), sin(angle), 0.0);
192 glVertex3f(r1 * cos(0), r1 * sin(0), t->width * 0.5);
193 glVertex3f(r1 * cos(0), r1 * sin(0), -t->width * 0.5);
197 glShadeModel(GL_SMOOTH);
199 /* draw inside radius cylinder */
200 glBegin(GL_QUAD_STRIP);
201 for (i = 0; i <= t->teeth; i++) {
202 angle = i * 2.0 * M_PI / t->teeth;
203 glNormal3f(-cos(angle), -sin(angle), 0.0);
204 glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
205 glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
211 draw_gears(struct gears *gears)
213 GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
215 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
219 glTranslatef(0.0, 0.0, -50);
221 glRotatef(view_rotx, 1.0, 0.0, 0.0);
222 glRotatef(view_roty, 0.0, 1.0, 0.0);
223 glRotatef(view_rotz, 0.0, 0.0, 1.0);
226 glTranslatef(-3.0, -2.0, 0.0);
227 glRotatef(gears->angle, 0.0, 0.0, 1.0);
228 glCallList(gears->gear_list[0]);
232 glTranslatef(3.1, -2.0, 0.0);
233 glRotatef(-2.0 * gears->angle - 9.0, 0.0, 0.0, 1.0);
234 glCallList(gears->gear_list[1]);
238 glTranslatef(-3.1, 4.2, 0.0);
239 glRotatef(-2.0 * gears->angle - 25.0, 0.0, 0.0, 1.0);
240 glCallList(gears->gear_list[2]);
249 resize_window(struct gears *gears)
254 EGL_IMAGE_FORMAT_MESA, EGL_IMAGE_FORMAT_ARGB8888_MESA,
255 EGL_IMAGE_USE_MESA, EGL_IMAGE_USE_SHARE_MESA |
256 EGL_IMAGE_USE_SCANOUT_MESA,
260 /* Constrain child size to be square and at least 300x300 */
261 window_get_child_rectangle(gears->window, &gears->rectangle);
262 if (gears->rectangle.width > gears->rectangle.height)
263 gears->rectangle.height = gears->rectangle.width;
265 gears->rectangle.width = gears->rectangle.height;
266 if (gears->rectangle.width < 300) {
267 gears->rectangle.width = 300;
268 gears->rectangle.height = 300;
270 window_set_child_size(gears->window, &gears->rectangle);
272 window_draw(gears->window);
275 eglDestroyImageKHR(gears->display, gears->image);
276 attribs[1] = gears->rectangle.width;
277 attribs[3] = gears->rectangle.height;
278 gears->image = eglCreateDRMImageMESA(gears->display, attribs);
280 glBindRenderbuffer(GL_RENDERBUFFER_EXT, gears->color_rbo);
281 glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER, gears->image);
283 glBindRenderbuffer(GL_RENDERBUFFER_EXT, gears->depth_rbo);
284 glRenderbufferStorage(GL_RENDERBUFFER_EXT,
286 gears->rectangle.width,
287 gears->rectangle.height);
289 glViewport(0, 0, gears->rectangle.width, gears->rectangle.height);
295 resize_handler(struct window *window, void *data)
297 struct gears *gears = data;
299 /* Right now, resizing the window from the per-frame callback
300 * is fine, since the window drawing code is so slow that we
301 * can't draw more than one window per frame anyway. However,
302 * once we implement faster resizing, this will show lag
303 * between pointer motion and window size even if resizing is
304 * fast. We need to keep processing motion events and posting
305 * new frames as fast as possible so when the server
306 * composites the next frame it will have the most recent size
307 * possible, like what we do for window moves. */
313 keyboard_focus_handler(struct window *window,
314 struct wl_input_device *device, void *data)
316 struct gears *gears = data;
322 handle_acknowledge(void *data,
323 struct wl_compositor *compositor,
324 uint32_t key, uint32_t frame)
326 struct gears *gears = data;
330 resize_window(gears);
337 handle_frame(void *data,
338 struct wl_compositor *compositor,
339 uint32_t frame, uint32_t timestamp)
341 struct gears *gears = data;
344 eglExportDRMImageMESA(gears->display,
345 gears->image, &name, NULL, &stride);
347 window_copy(gears->window, &gears->rectangle, name, stride);
349 window_commit(gears->window, 10);
351 gears->angle = (GLfloat) (timestamp % 8192) * 360 / 8192.0;
354 static const struct wl_compositor_listener compositor_listener = {
359 static struct gears *
360 gears_create(struct display *display, int drm_fd)
362 PFNEGLGETTYPEDDISPLAYMESA get_typed_display_mesa;
363 const int x = 200, y = 200, width = 450, height = 500;
364 EGLint major, minor, count;
369 static const EGLint config_attribs[] = {
371 EGL_NO_SURFACE_CAPABLE_MESA, EGL_OPENGL_BIT,
372 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
376 get_typed_display_mesa =
377 (PFNEGLGETTYPEDDISPLAYMESA) eglGetProcAddress("eglGetTypedDisplayMESA");
378 if (get_typed_display_mesa == NULL)
379 die("eglGetDisplayMESA() not found\n");
381 gears = malloc(sizeof *gears);
382 memset(gears, 0, sizeof *gears);
384 gears->window = window_create(display, "Wayland Gears",
385 x, y, width, height);
387 gears->display = get_typed_display_mesa(EGL_DRM_DISPLAY_TYPE_MESA,
389 if (gears->display == NULL)
390 die("failed to create egl display\n");
392 if (!eglInitialize(gears->display, &major, &minor))
393 die("failed to initialize display\n");
395 if (!eglChooseConfig(gears->display, config_attribs, &config, 1, &count) ||
397 die("eglChooseConfig() failed\n");
399 eglBindAPI(EGL_OPENGL_API);
401 gears->context = eglCreateContext(gears->display, config, EGL_NO_CONTEXT, NULL);
402 if (gears->context == NULL)
403 die("failed to create context\n");
405 if (!eglMakeCurrent(gears->display, NULL, NULL, gears->context))
406 die("faile to make context current\n");
408 glGenFramebuffers(1, &gears->fbo);
409 glBindFramebuffer(GL_FRAMEBUFFER_EXT, gears->fbo);
411 glGenRenderbuffers(1, &gears->color_rbo);
412 glBindRenderbuffer(GL_RENDERBUFFER_EXT, gears->color_rbo);
413 glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER_EXT,
414 GL_COLOR_ATTACHMENT0_EXT,
418 glGenRenderbuffers(1, &gears->depth_rbo);
419 glBindRenderbuffer(GL_RENDERBUFFER_EXT, gears->depth_rbo);
420 glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER_EXT,
421 GL_DEPTH_ATTACHMENT_EXT,
424 for (i = 0; i < 3; i++) {
425 gears->gear_list[i] = glGenLists(1);
426 glNewList(gears->gear_list[i], GL_COMPILE);
427 make_gear(&gear_templates[i]);
431 glEnable(GL_NORMALIZE);
433 glMatrixMode(GL_PROJECTION);
435 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 200.0);
436 glMatrixMode(GL_MODELVIEW);
438 glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
439 glEnable(GL_CULL_FACE);
440 glEnable(GL_LIGHTING);
442 glEnable(GL_DEPTH_TEST);
443 glClearColor(0, 0, 0, 0.92);
445 if (glCheckFramebufferStatus (GL_FRAMEBUFFER_EXT) != GL_FRAMEBUFFER_COMPLETE)
446 fprintf(stderr, "framebuffer incomplete\n");
448 gears->compositor = display_get_compositor(display);
450 resize_window(gears);
452 handle_frame(gears, gears->compositor, 0, 0);
454 window_set_resize_handler(gears->window, resize_handler, gears);
455 window_set_keyboard_focus_handler(gears->window, keyboard_focus_handler, gears);
457 wl_compositor_add_listener(gears->compositor,
458 &compositor_listener, gears);
463 int main(int argc, char *argv[])
465 struct wl_display *display;
472 fd = open(gem_device, O_RDWR);
474 fprintf(stderr, "drm open failed: %m\n");
478 display = wl_display_create(socket_name, sizeof socket_name);
479 if (display == NULL) {
480 fprintf(stderr, "failed to create display: %m\n");
484 d = display_create(display, fd);
486 loop = g_main_loop_new(NULL, FALSE);
487 source = wl_glib_source_new(display);
488 g_source_attach(source, NULL);
490 gears = gears_create(d, fd);
492 g_main_loop_run(loop);