Workaround gears depending on cairo-gl
[profile/ivi/weston-ivi-shell.git] / clients / gears.c
1 /*
2  * Copyright © 2008 Kristian Høgsberg
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 <stdint.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <math.h>
30 #include <time.h>
31 #include <cairo.h>
32 #include <glib.h>
33
34 #define GL_GLEXT_PROTOTYPES
35 #define EGL_EGLEXT_PROTOTYPES
36 #include <GL/gl.h>
37 #include <EGL/egl.h>
38 #include <EGL/eglext.h>
39
40 #include "wayland-util.h"
41 #include "wayland-client.h"
42 #include "wayland-glib.h"
43
44 #include "window.h"
45
46 struct gears {
47         struct window *window;
48
49         struct display *d;
50         struct rectangle rectangle;
51
52         EGLDisplay display;
53         EGLContext context;
54         int drm_fd;
55         GLfloat angle;
56         cairo_surface_t *cairo_surface;
57
58         GLint gear_list[3];
59         GLuint fbo, color_rbo[2], depth_rbo;
60         cairo_surface_t *surface[2];
61         int current;
62 };
63
64 struct gear_template {
65         GLfloat material[4];
66         GLfloat inner_radius;
67         GLfloat outer_radius;
68         GLfloat width;
69         GLint teeth;
70         GLfloat tooth_depth;
71 };
72
73 const static struct gear_template gear_templates[] = {
74         { { 0.8, 0.1, 0.0, 1.0 }, 1.0, 4.0, 1.0, 20, 0.7 },
75         { { 0.0, 0.8, 0.2, 1.0 }, 0.5, 2.0, 2.0, 10, 0.7 },
76         { { 0.2, 0.2, 1.0, 1.0 }, 1.3, 2.0, 0.5, 10, 0.7 }, 
77 };
78
79 static GLfloat light_pos[4] = {5.0, 5.0, 10.0, 0.0};
80
81 static void die(const char *msg)
82 {
83         fprintf(stderr, "%s", msg);
84         exit(EXIT_FAILURE);
85 }
86
87 static void
88 make_gear(const struct gear_template *t)
89 {
90         GLint i;
91         GLfloat r0, r1, r2;
92         GLfloat angle, da;
93         GLfloat u, v, len;
94
95         glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, t->material);
96
97         r0 = t->inner_radius;
98         r1 = t->outer_radius - t->tooth_depth / 2.0;
99         r2 = t->outer_radius + t->tooth_depth / 2.0;
100
101         da = 2.0 * M_PI / t->teeth / 4.0;
102
103         glShadeModel(GL_FLAT);
104
105         glNormal3f(0.0, 0.0, 1.0);
106
107         /* draw front face */
108         glBegin(GL_QUAD_STRIP);
109         for (i = 0; i <= t->teeth; i++) {
110                 angle = i * 2.0 * M_PI / t->teeth;
111                 glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
112                 glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
113                 if (i < t->teeth) {
114                         glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
115                         glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
116                 }
117         }
118         glEnd();
119
120         /* draw front sides of teeth */
121         glBegin(GL_QUADS);
122         da = 2.0 * M_PI / t->teeth / 4.0;
123         for (i = 0; i < t->teeth; i++) {
124                 angle = i * 2.0 * M_PI / t->teeth;
125
126                 glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
127                 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), t->width * 0.5);
128                 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), t->width * 0.5);
129                 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
130         }
131         glEnd();
132
133         glNormal3f(0.0, 0.0, -1.0);
134
135         /* draw back face */
136         glBegin(GL_QUAD_STRIP);
137         for (i = 0; i <= t->teeth; i++) {
138                 angle = i * 2.0 * M_PI / t->teeth;
139                 glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
140                 glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
141                 if (i < t->teeth) {
142                         glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
143                         glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
144                 }
145         }
146         glEnd();
147
148         /* draw back sides of teeth */
149         glBegin(GL_QUADS);
150         da = 2.0 * M_PI / t->teeth / 4.0;
151         for (i = 0; i < t->teeth; i++) {
152                 angle = i * 2.0 * M_PI / t->teeth;
153
154                 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
155                 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -t->width * 0.5);
156                 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -t->width * 0.5);
157                 glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
158         }
159         glEnd();
160
161         /* draw outward faces of teeth */
162         glBegin(GL_QUAD_STRIP);
163         for (i = 0; i < t->teeth; i++) {
164                 angle = i * 2.0 * M_PI / t->teeth;
165
166                 glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
167                 glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
168                 u = r2 * cos(angle + da) - r1 * cos(angle);
169                 v = r2 * sin(angle + da) - r1 * sin(angle);
170                 len = sqrt(u * u + v * v);
171                 u /= len;
172                 v /= len;
173                 glNormal3f(v, -u, 0.0);
174                 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), t->width * 0.5);
175                 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -t->width * 0.5);
176                 glNormal3f(cos(angle), sin(angle), 0.0);
177                 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), t->width * 0.5);
178                 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -t->width * 0.5);
179                 u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
180                 v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
181                 glNormal3f(v, -u, 0.0);
182                 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
183                 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
184                 glNormal3f(cos(angle), sin(angle), 0.0);
185         }
186
187         glVertex3f(r1 * cos(0), r1 * sin(0), t->width * 0.5);
188         glVertex3f(r1 * cos(0), r1 * sin(0), -t->width * 0.5);
189
190         glEnd();
191
192         glShadeModel(GL_SMOOTH);
193
194         /* draw inside radius cylinder */
195         glBegin(GL_QUAD_STRIP);
196         for (i = 0; i <= t->teeth; i++) {
197                 angle = i * 2.0 * M_PI / t->teeth;
198                 glNormal3f(-cos(angle), -sin(angle), 0.0);
199                 glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
200                 glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
201         }
202         glEnd();
203 }
204
205 static void
206 allocate_buffer(struct gears *gears)
207 {
208         EGLImageKHR image;
209
210         /* Constrain child size to be square and at least 300x300 */
211         window_get_child_rectangle(gears->window, &gears->rectangle);
212         if (gears->rectangle.width > gears->rectangle.height)
213                 gears->rectangle.height = gears->rectangle.width;
214         else
215                 gears->rectangle.width = gears->rectangle.height;
216         if (gears->rectangle.width < 300) {
217                 gears->rectangle.width = 300;
218                 gears->rectangle.height = 300;
219         }
220
221         window_set_child_size(gears->window, &gears->rectangle);
222         window_draw(gears->window);
223
224         gears->surface[gears->current] = window_get_surface(gears->window);
225 #ifdef HAVE_CAIRO_GL
226         image = display_get_image_for_drm_surface(gears->display,
227                                                   gears->surface[gears->current]);
228 #else /* XXX: hack to make Wayland compile, even if this example doesn't run */
229         die("gears cannot allocate buffer: it was compiled without cairo-gl");
230         return;
231 #endif
232         if (!eglMakeCurrent(gears->display, NULL, NULL, gears->context))
233                 die("faile to make context current\n");
234
235         glBindRenderbuffer(GL_RENDERBUFFER_EXT,
236                            gears->color_rbo[gears->current]);
237         glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER, image);
238
239         glBindRenderbuffer(GL_RENDERBUFFER_EXT, gears->depth_rbo);
240         glRenderbufferStorage(GL_RENDERBUFFER_EXT,
241                               GL_DEPTH_COMPONENT,
242                               gears->rectangle.width + 20 + 32,
243                               gears->rectangle.height + 60 + 32);
244 }
245
246 static void
247 draw_gears(struct gears *gears)
248 {
249         GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
250
251         if (gears->surface[gears->current] == NULL)
252                 allocate_buffer(gears);
253
254         glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER_EXT,
255                                   GL_COLOR_ATTACHMENT0_EXT,
256                                   GL_RENDERBUFFER_EXT,
257                                   gears->color_rbo[gears->current]);
258
259         glViewport(gears->rectangle.x, gears->rectangle.y,
260                    gears->rectangle.width, gears->rectangle.height);
261         glScissor(gears->rectangle.x, gears->rectangle.y,
262                    gears->rectangle.width, gears->rectangle.height);
263
264         glEnable(GL_SCISSOR_TEST);
265         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
266
267         glPushMatrix();
268
269         glTranslatef(0.0, 0.0, -50);
270
271         glRotatef(view_rotx, 1.0, 0.0, 0.0);
272         glRotatef(view_roty, 0.0, 1.0, 0.0);
273         glRotatef(view_rotz, 0.0, 0.0, 1.0);
274
275         glPushMatrix();
276         glTranslatef(-3.0, -2.0, 0.0);
277         glRotatef(gears->angle, 0.0, 0.0, 1.0);
278         glCallList(gears->gear_list[0]);
279         glPopMatrix();
280
281         glPushMatrix();
282         glTranslatef(3.1, -2.0, 0.0);
283         glRotatef(-2.0 * gears->angle - 9.0, 0.0, 0.0, 1.0);
284         glCallList(gears->gear_list[1]);
285         glPopMatrix();
286
287         glPushMatrix();
288         glTranslatef(-3.1, 4.2, 0.0);
289         glRotatef(-2.0 * gears->angle - 25.0, 0.0, 0.0, 1.0);
290         glCallList(gears->gear_list[2]);
291         glPopMatrix();
292
293         glPopMatrix();
294
295         glFlush();
296
297         window_set_surface(gears->window, gears->surface[gears->current]);
298         window_flush(gears->window);
299 }
300
301 static void
302 resize_handler(struct window *window, void *data)
303 {
304         struct gears *gears = data;
305
306         cairo_surface_destroy(gears->surface[0]);
307         gears->surface[0] = NULL;
308         cairo_surface_destroy(gears->surface[1]);
309         gears->surface[1] = NULL;
310 }
311
312 static void
313 keyboard_focus_handler(struct window *window,
314                        struct input *device, void *data)
315 {
316         struct gears *gears = data;
317
318         resize_handler(window, gears);
319 }
320
321 static void
322 redraw_handler(struct window *window, void *data)
323 {
324         struct gears *gears = data;
325
326         draw_gears(gears);
327 }
328
329 static void
330 frame_callback(void *data, uint32_t time)
331 {
332         struct gears *gears = data;
333
334         gears->current = 1 - gears->current;
335
336         gears->angle = (GLfloat) (time % 8192) * 360 / 8192.0;
337
338         window_schedule_redraw(gears->window);
339         wl_display_frame_callback(display_get_display(gears->d),
340                                   frame_callback, gears);
341 }
342
343 static struct gears *
344 gears_create(struct display *display)
345 {
346         const int width = 450, height = 500;
347         struct gears *gears;
348         int i;
349
350         gears = malloc(sizeof *gears);
351         memset(gears, 0, sizeof *gears);
352         gears->d = display;
353         gears->window = window_create(display, "Wayland Gears", width, height);
354
355         gears->display = display_get_egl_display(gears->d);
356         if (gears->display == NULL)
357                 die("failed to create egl display\n");
358
359         eglBindAPI(EGL_OPENGL_API);
360
361         gears->context = eglCreateContext(gears->display,
362                                           NULL, EGL_NO_CONTEXT, NULL);
363         if (gears->context == NULL)
364                 die("failed to create context\n");
365
366         if (!eglMakeCurrent(gears->display, NULL, NULL, gears->context))
367                 die("faile to make context current\n");
368
369         glGenFramebuffers(1, &gears->fbo);
370         glBindFramebuffer(GL_FRAMEBUFFER_EXT, gears->fbo);
371
372         glGenRenderbuffers(2, gears->color_rbo);
373         glGenRenderbuffers(1, &gears->depth_rbo);
374         glBindRenderbuffer(GL_RENDERBUFFER_EXT, gears->depth_rbo);
375         glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER_EXT,
376                                   GL_DEPTH_ATTACHMENT_EXT,
377                                   GL_RENDERBUFFER_EXT,
378                                   gears->depth_rbo);
379         for (i = 0; i < 3; i++) {
380                 gears->gear_list[i] = glGenLists(1);
381                 glNewList(gears->gear_list[i], GL_COMPILE);
382                 make_gear(&gear_templates[i]);
383                 glEndList();
384         }
385
386         glEnable(GL_NORMALIZE);
387
388         glMatrixMode(GL_PROJECTION);
389         glLoadIdentity();
390         glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 200.0);
391         glMatrixMode(GL_MODELVIEW);
392
393         glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
394         glEnable(GL_CULL_FACE);
395         glEnable(GL_LIGHTING);
396         glEnable(GL_LIGHT0);
397         glEnable(GL_DEPTH_TEST);
398         glClearColor(0, 0, 0, 0.92);
399
400         window_set_user_data(gears->window, gears);
401         window_set_resize_handler(gears->window, resize_handler);
402         window_set_keyboard_focus_handler(gears->window, keyboard_focus_handler);
403         window_set_redraw_handler(gears->window, redraw_handler);
404
405         draw_gears(gears);
406         wl_display_frame_callback(display_get_display(gears->d),
407                                   frame_callback, gears);
408
409         return gears;
410 }
411
412 int main(int argc, char *argv[])
413 {
414         struct display *d;
415         struct gears *gears;
416
417         d = display_create(&argc, &argv, NULL);
418         if (d == NULL) {
419                 fprintf(stderr, "failed to create display: %m\n");
420                 return -1;
421         }
422         gears = gears_create(d);
423         display_run(d);
424
425         return 0;
426 }