blob: 7fa8abbc72a2dec446ba7814a7bcc8845f7d2c7a [file] [log] [blame]
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001/*
2 * Copyright © 2010 Intel Corporation
3 * Copyright © 2011 Benjamin Franzke
4 * Copyright © 2012-2013 Collabora, Ltd.
5 *
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that copyright
9 * notice and this permission notice appear in supporting documentation, and
10 * that the name of the copyright holders not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. The copyright holders make no representations
13 * about the suitability of this software for any purpose. It is provided "as
14 * is" without express or implied warranty.
15 *
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22 * OF THIS SOFTWARE.
23 */
24
25#include <stdint.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <cairo.h>
30#include <math.h>
31#include <assert.h>
32
33#include <linux/input.h>
34#include <wayland-client.h>
35
36#include <wayland-egl.h>
37#include <GLES2/gl2.h>
38#include <EGL/egl.h>
39
40#include "window.h"
41
42#if 0
43#define DBG(fmt, ...) \
44 fprintf(stderr, "%d:%s " fmt, __LINE__, __func__, ##__VA_ARGS__)
45#else
46#define DBG(...) do {} while (0)
47#endif
48
49static int32_t option_red_mode;
50static int32_t option_triangle_mode;
51static int32_t option_no_triangle;
52static int32_t option_help;
53
54static const struct weston_option options[] = {
55 { WESTON_OPTION_INTEGER, "red-mode", 'r', &option_red_mode },
56 { WESTON_OPTION_INTEGER, "triangle-mode", 't', &option_triangle_mode },
57 { WESTON_OPTION_BOOLEAN, "no-triangle", 'n', &option_no_triangle },
58 { WESTON_OPTION_BOOLEAN, "help", 'h', &option_help },
59};
60
61static enum subsurface_mode
62int_to_mode(int32_t i)
63{
64 switch (i) {
65 case 0:
66 return SUBSURFACE_DESYNCHRONIZED;
67 case 1:
68 return SUBSURFACE_SYNCHRONIZED;
69 default:
70 fprintf(stderr, "error: %d is not a valid commit mode.\n", i);
71 exit(1);
72 }
73}
74
75static const char help_text[] =
76"Usage: %s [options]\n"
77"\n"
78" -r, --red-mode=MODE\t\tthe commit mode for the red sub-surface (0)\n"
79" -t, --triangle-mode=MODE\tthe commit mode for the GL sub-surface (0)\n"
80" -n, --no-triangle\t\tDo not create the GL sub-surface.\n"
81"\n"
82"The MODE is the wl_subsurface commit mode used by default for the\n"
83"given sub-surface. Valid values are the integers:\n"
84" 0\tfor desynchronized, i.e. free-running\n"
85" 1\tfor synchronized\n"
86"\n"
87"This program demonstrates sub-surfaces with the toytoolkit.\n"
88"The main surface contains the decorations, a green canvas, and a\n"
89"green spinner. One sub-surface is red with a red spinner. These\n"
90"are rendered with Cairo. The other sub-surface contains a spinning\n"
91"triangle rendered in EGL/GLESv2, without Cairo, i.e. it is a raw GL\n"
92"widget.\n"
93"\n"
94"The GL widget animates on its own. The spinners follow wall clock\n"
95"time and update only when their surface is repainted, so you see\n"
96"which surfaces get redrawn. The red sub-surface animates on its own,\n"
97"but can be toggled with the spacebar.\n"
98"\n"
99"Even though the sub-surfaces attempt to animate on their own, they\n"
100"are subject to the commit mode. If commit mode is synchronized,\n"
101"they will need a commit on the main surface to actually display.\n"
102"You can trigger a main surface repaint, without a resize, by\n"
103"hovering the pointer over the title bar buttons.\n"
104"\n"
105"Resizing will temporarily toggle the commit mode of all sub-surfaces\n"
106"to guarantee synchronized rendering on size changes. It also forces\n"
107"a repaint of all surfaces.\n"
108"\n"
109"Using -t1 -r1 is especially useful for trying to catch inconsistent\n"
110"rendering and deadlocks, since free-running sub-surfaces would\n"
111"immediately hide the problem.\n"
112"\n"
113"Key controls:\n"
114" space - toggle red sub-surface animation loop\n"
115" up - step window size shorter\n"
116" down - step window size taller\n"
117"\n";
118
119struct egl_state {
120 EGLDisplay dpy;
121 EGLContext ctx;
122 EGLConfig conf;
123};
124
125struct triangle_gl_state {
126 GLuint rotation_uniform;
127 GLuint pos;
128 GLuint col;
129};
130
131struct triangle {
132 struct egl_state *egl;
133
134 struct wl_surface *wl_surface;
135 struct wl_egl_window *egl_window;
136 EGLSurface egl_surface;
137 int width;
138 int height;
139
140 struct triangle_gl_state gl;
141
142 struct widget *widget;
143 uint32_t time;
144 struct wl_callback *frame_cb;
145};
146
147/******** Pure EGL/GLESv2/libwayland-client component: ***************/
148
149static const char *vert_shader_text =
150 "uniform mat4 rotation;\n"
151 "attribute vec4 pos;\n"
152 "attribute vec4 color;\n"
153 "varying vec4 v_color;\n"
154 "void main() {\n"
155 " gl_Position = rotation * pos;\n"
156 " v_color = color;\n"
157 "}\n";
158
159static const char *frag_shader_text =
160 "precision mediump float;\n"
161 "varying vec4 v_color;\n"
162 "void main() {\n"
163 " gl_FragColor = v_color;\n"
164 "}\n";
165
166static void
167egl_print_config_info(struct egl_state *egl)
168{
169 EGLint r, g, b, a;
170
171 printf("Chosen EGL config details:\n");
172
173 printf("\tRGBA bits");
174 if (eglGetConfigAttrib(egl->dpy, egl->conf, EGL_RED_SIZE, &r) &&
175 eglGetConfigAttrib(egl->dpy, egl->conf, EGL_GREEN_SIZE, &g) &&
176 eglGetConfigAttrib(egl->dpy, egl->conf, EGL_BLUE_SIZE, &b) &&
177 eglGetConfigAttrib(egl->dpy, egl->conf, EGL_ALPHA_SIZE, &a))
178 printf(": %d %d %d %d\n", r, g, b, a);
179 else
180 printf(" unknown\n");
181
182 printf("\tswap interval range");
183 if (eglGetConfigAttrib(egl->dpy, egl->conf, EGL_MIN_SWAP_INTERVAL, &a) &&
184 eglGetConfigAttrib(egl->dpy, egl->conf, EGL_MAX_SWAP_INTERVAL, &b))
185 printf(": %d - %d\n", a, b);
186 else
187 printf(" unknown\n");
188}
189
190static struct egl_state *
191egl_state_create(struct wl_display *display)
192{
193 struct egl_state *egl;
194
195 static const EGLint context_attribs[] = {
196 EGL_CONTEXT_CLIENT_VERSION, 2,
197 EGL_NONE
198 };
199
200 EGLint config_attribs[] = {
201 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
202 EGL_RED_SIZE, 1,
203 EGL_GREEN_SIZE, 1,
204 EGL_BLUE_SIZE, 1,
205 EGL_ALPHA_SIZE, 1,
206 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
207 EGL_NONE
208 };
209
210 EGLint major, minor, n;
211 EGLBoolean ret;
212
213 egl = calloc(1, sizeof *egl);
214 assert(egl);
215
216 egl->dpy = eglGetDisplay(display);
217 assert(egl->dpy);
218
219 ret = eglInitialize(egl->dpy, &major, &minor);
220 assert(ret == EGL_TRUE);
221 ret = eglBindAPI(EGL_OPENGL_ES_API);
222 assert(ret == EGL_TRUE);
223
224 ret = eglChooseConfig(egl->dpy, config_attribs, &egl->conf, 1, &n);
225 assert(ret && n == 1);
226
227 egl->ctx = eglCreateContext(egl->dpy, egl->conf,
228 EGL_NO_CONTEXT, context_attribs);
229 assert(egl->ctx);
230 egl_print_config_info(egl);
231
232 return egl;
233}
234
235static void
236egl_state_destroy(struct egl_state *egl)
237{
238 /* Required, otherwise segfault in egl_dri2.c: dri2_make_current()
239 * on eglReleaseThread(). */
240 eglMakeCurrent(egl->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
241 EGL_NO_CONTEXT);
242
243 eglTerminate(egl->dpy);
244 eglReleaseThread();
245 free(egl);
246}
247
248static void
249egl_make_swapbuffers_nonblock(struct egl_state *egl)
250{
251 EGLint a = EGL_MIN_SWAP_INTERVAL;
252 EGLint b = EGL_MAX_SWAP_INTERVAL;
253
254 if (!eglGetConfigAttrib(egl->dpy, egl->conf, a, &a) ||
255 !eglGetConfigAttrib(egl->dpy, egl->conf, b, &b)) {
256 fprintf(stderr, "warning: swap interval range unknown\n");
257 } else
258 if (a > 0) {
259 fprintf(stderr, "warning: minimum swap interval is %d, "
260 "while 0 is required to not deadlock on resize.\n", a);
261 }
262
263 /*
264 * We rely on the Wayland compositor to sync to vblank anyway.
265 * We just need to be able to call eglSwapBuffers() without the
266 * risk of waiting for a frame callback in it.
267 */
268 if (!eglSwapInterval(egl->dpy, 0)) {
269 fprintf(stderr, "error: eglSwapInterval() failed.\n");
270 }
271}
272
273static GLuint
274create_shader(const char *source, GLenum shader_type)
275{
276 GLuint shader;
277 GLint status;
278
279 shader = glCreateShader(shader_type);
280 assert(shader != 0);
281
282 glShaderSource(shader, 1, (const char **) &source, NULL);
283 glCompileShader(shader);
284
285 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
286 if (!status) {
287 char log[1000];
288 GLsizei len;
289 glGetShaderInfoLog(shader, 1000, &len, log);
290 fprintf(stderr, "Error: compiling %s: %*s\n",
291 shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
292 len, log);
293 exit(1);
294 }
295
296 return shader;
297}
298
299static void
300triangle_init_gl(struct triangle_gl_state *trigl)
301{
302 GLuint frag, vert;
303 GLuint program;
304 GLint status;
305
306 frag = create_shader(frag_shader_text, GL_FRAGMENT_SHADER);
307 vert = create_shader(vert_shader_text, GL_VERTEX_SHADER);
308
309 program = glCreateProgram();
310 glAttachShader(program, frag);
311 glAttachShader(program, vert);
312 glLinkProgram(program);
313
314 glGetProgramiv(program, GL_LINK_STATUS, &status);
315 if (!status) {
316 char log[1000];
317 GLsizei len;
318 glGetProgramInfoLog(program, 1000, &len, log);
319 fprintf(stderr, "Error: linking:\n%*s\n", len, log);
320 exit(1);
321 }
322
323 glUseProgram(program);
324
325 trigl->pos = 0;
326 trigl->col = 1;
327
328 glBindAttribLocation(program, trigl->pos, "pos");
329 glBindAttribLocation(program, trigl->col, "color");
330 glLinkProgram(program);
331
332 trigl->rotation_uniform = glGetUniformLocation(program, "rotation");
333}
334
335static void
336triangle_draw(const struct triangle_gl_state *trigl, uint32_t time)
337{
338 static const GLfloat verts[3][2] = {
339 { -0.5, -0.5 },
340 { 0.5, -0.5 },
341 { 0, 0.5 }
342 };
343 static const GLfloat colors[3][3] = {
344 { 1, 0, 0 },
345 { 0, 1, 0 },
346 { 0, 0, 1 }
347 };
348 GLfloat angle;
349 GLfloat rotation[4][4] = {
350 { 1, 0, 0, 0 },
351 { 0, 1, 0, 0 },
352 { 0, 0, 1, 0 },
353 { 0, 0, 0, 1 }
354 };
355 static const int32_t speed_div = 5;
356
357 angle = (time / speed_div) % 360 * M_PI / 180.0;
358 rotation[0][0] = cos(angle);
359 rotation[0][2] = sin(angle);
360 rotation[2][0] = -sin(angle);
361 rotation[2][2] = cos(angle);
362
363 glUniformMatrix4fv(trigl->rotation_uniform, 1, GL_FALSE,
364 (GLfloat *) rotation);
365
366 glClearColor(0.0, 0.0, 0.0, 0.5);
367 glClear(GL_COLOR_BUFFER_BIT);
368
369 glVertexAttribPointer(trigl->pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
370 glVertexAttribPointer(trigl->col, 3, GL_FLOAT, GL_FALSE, 0, colors);
371 glEnableVertexAttribArray(trigl->pos);
372 glEnableVertexAttribArray(trigl->col);
373
374 glDrawArrays(GL_TRIANGLES, 0, 3);
375
376 glDisableVertexAttribArray(trigl->pos);
377 glDisableVertexAttribArray(trigl->col);
378}
379
380static void
381triangle_frame_callback(void *data, struct wl_callback *callback,
382 uint32_t time);
383
384static const struct wl_callback_listener triangle_frame_listener = {
385 triangle_frame_callback
386};
387
388static void
389triangle_frame_callback(void *data, struct wl_callback *callback,
390 uint32_t time)
391{
392 struct triangle *tri = data;
393
394 DBG("%stime %u\n", callback ? "" : "artificial ", time);
395 assert(callback == tri->frame_cb);
396 tri->time = time;
397
398 if (callback)
399 wl_callback_destroy(callback);
400
401 glViewport(0, 0, tri->width, tri->height);
402
403 triangle_draw(&tri->gl, tri->time);
404
405 tri->frame_cb = wl_surface_frame(tri->wl_surface);
406 wl_callback_add_listener(tri->frame_cb, &triangle_frame_listener, tri);
407
408 eglSwapBuffers(tri->egl->dpy, tri->egl_surface);
409}
410
411static void
412triangle_create_egl_surface(struct triangle *tri, int width, int height)
413{
414 EGLBoolean ret;
415
416 tri->wl_surface = widget_get_wl_surface(tri->widget);
417 tri->egl_window = wl_egl_window_create(tri->wl_surface, width, height);
418 tri->egl_surface = eglCreateWindowSurface(tri->egl->dpy,
419 tri->egl->conf,
420 tri->egl_window, NULL);
421
422 ret = eglMakeCurrent(tri->egl->dpy, tri->egl_surface,
423 tri->egl_surface, tri->egl->ctx);
424 assert(ret == EGL_TRUE);
425
426 egl_make_swapbuffers_nonblock(tri->egl);
427 triangle_init_gl(&tri->gl);
428}
429
430/********* The widget code interfacing the toolkit agnostic code: **********/
431
432static void
433triangle_resize_handler(struct widget *widget,
434 int32_t width, int32_t height, void *data)
435{
436 struct triangle *tri = data;
437
438 DBG("to %dx%d\n", width, height);
439 tri->width = width;
440 tri->height = height;
441
442 if (tri->egl_surface) {
443 wl_egl_window_resize(tri->egl_window, width, height, 0, 0);
444 } else {
445 triangle_create_egl_surface(tri, width, height);
446 triangle_frame_callback(tri, NULL, 0);
447 }
448}
449
450static void
451triangle_redraw_handler(struct widget *widget, void *data)
452{
453 struct triangle *tri = data;
454 int w, h;
455
456 wl_egl_window_get_attached_size(tri->egl_window, &w, &h);
457
458 DBG("previous %dx%d, new %dx%d\n", w, h, tri->width, tri->height);
459
460 /* If size is not changing, do not redraw ahead of time.
461 * That would risk blocking in eglSwapbuffers().
462 */
463 if (w == tri->width && h == tri->height)
464 return;
465
466 if (tri->frame_cb) {
467 wl_callback_destroy(tri->frame_cb);
468 tri->frame_cb = NULL;
469 }
470 triangle_frame_callback(tri, NULL, tri->time);
471}
472
473static void
474set_empty_input_region(struct widget *widget, struct display *display)
475{
476 struct wl_compositor *compositor;
477 struct wl_surface *surface;
478 struct wl_region *region;
479
480 compositor = display_get_compositor(display);
481 surface = widget_get_wl_surface(widget);
482 region = wl_compositor_create_region(compositor);
483 wl_surface_set_input_region(surface, region);
484 wl_region_destroy(region);
485}
486
487static struct triangle *
488triangle_create(struct window *window, struct egl_state *egl)
489{
490 struct triangle *tri;
491
492 tri = calloc(1, sizeof *tri);
493
494 tri->egl = egl;
495 tri->widget = window_add_subsurface(window, tri,
496 int_to_mode(option_triangle_mode));
497 widget_set_resize_handler(tri->widget, triangle_resize_handler);
498 widget_set_redraw_handler(tri->widget, triangle_redraw_handler);
499
500 set_empty_input_region(tri->widget, window_get_display(window));
501
502 return tri;
503}
504
505static void
506triangle_destroy(struct triangle *tri)
507{
508 if (tri->egl_surface)
509 eglDestroySurface(tri->egl->dpy, tri->egl_surface);
510
511 if (tri->egl_window)
512 wl_egl_window_destroy(tri->egl_window);
513
514 widget_destroy(tri->widget);
515 free(tri);
516}
517
518/************** The toytoolkit application code: *********************/
519
520struct demoapp {
521 struct display *display;
522 struct window *window;
523 struct widget *widget;
524 struct widget *subsurface;
525
526 struct egl_state *egl;
527 struct triangle *triangle;
528
529 int animate;
530};
531
532static void
533draw_spinner(cairo_t *cr, const struct rectangle *rect, uint32_t time)
534{
535 double cx, cy, r, angle;
536 unsigned t;
537
538 cx = rect->x + rect->width / 2;
539 cy = rect->y + rect->height / 2;
540 r = (rect->width < rect->height ? rect->width : rect->height) * 0.3;
541 t = time % 2000;
542 angle = t * (M_PI / 500.0);
543
544 cairo_set_line_width(cr, 4.0);
545
546 if (t < 1000)
547 cairo_arc(cr, cx, cy, r, 0.0, angle);
548 else
549 cairo_arc(cr, cx, cy, r, angle, 0.0);
550
551 cairo_stroke(cr);
552}
553
554static void
555sub_redraw_handler(struct widget *widget, void *data)
556{
557 struct demoapp *app = data;
558 cairo_t *cr;
559 struct rectangle allocation;
560 uint32_t time;
561
562 widget_get_allocation(app->subsurface, &allocation);
563
564 cr = widget_cairo_create(widget);
565 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
566
567 /* debug: paint whole surface magenta; no magenta should show */
568 cairo_set_source_rgba(cr, 0.9, 0.0, 0.9, 1.0);
569 cairo_paint(cr);
570
571 cairo_rectangle(cr,
572 allocation.x,
573 allocation.y,
574 allocation.width,
575 allocation.height);
576 cairo_clip(cr);
577
578 cairo_set_source_rgba(cr, 0.8, 0, 0, 0.8);
579 cairo_paint(cr);
580
581 time = widget_get_last_time(widget);
582 cairo_set_source_rgba(cr, 1.0, 0.5, 0.5, 1.0);
583 draw_spinner(cr, &allocation, time);
584
585 cairo_destroy(cr);
586
587 if (app->animate)
588 widget_schedule_redraw(app->subsurface);
589 DBG("%dx%d @ %d,%d, last time %u\n",
590 allocation.width, allocation.height,
591 allocation.x, allocation.y, time);
592}
593
594static void
595sub_resize_handler(struct widget *widget,
596 int32_t width, int32_t height, void *data)
597{
598 DBG("%dx%d\n", width, height);
599 widget_input_region_add(widget, NULL);
600}
601
602static void
603redraw_handler(struct widget *widget, void *data)
604{
605 struct demoapp *app = data;
606 cairo_t *cr;
607 struct rectangle allocation;
608 uint32_t time;
609
610 widget_get_allocation(app->widget, &allocation);
611
612 cr = widget_cairo_create(widget);
613 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
614 cairo_rectangle(cr,
615 allocation.x,
616 allocation.y,
617 allocation.width,
618 allocation.height);
619 cairo_set_source_rgba(cr, 0, 0.8, 0, 0.8);
620 cairo_fill(cr);
621
622 time = widget_get_last_time(widget);
623 cairo_set_source_rgba(cr, 0.5, 1.0, 0.5, 1.0);
624 draw_spinner(cr, &allocation, time);
625
626 cairo_destroy(cr);
627
628 DBG("%dx%d @ %d,%d, last time %u\n",
629 allocation.width, allocation.height,
630 allocation.x, allocation.y, time);
631}
632
633static void
634resize_handler(struct widget *widget,
635 int32_t width, int32_t height, void *data)
636{
637 struct demoapp *app = data;
638 struct rectangle area;
639 int side, h;
640
641 widget_get_allocation(widget, &area);
642
643 side = area.width < area.height ? area.width / 2 : area.height / 2;
644 h = area.height - side;
645
646 widget_set_allocation(app->subsurface,
647 area.x + area.width - side,
648 area.y,
649 side, h);
650
651 if (app->triangle) {
652 widget_set_allocation(app->triangle->widget,
653 area.x + area.width - side,
654 area.y + h,
655 side, side);
656 }
657
658 DBG("green %dx%d, red %dx%d, GL %dx%d\n",
659 area.width, area.height, side, h, side, side);
660}
661
662static void
663keyboard_focus_handler(struct window *window,
664 struct input *device, void *data)
665{
666 struct demoapp *app = data;
667
668 window_schedule_redraw(app->window);
669}
670
671static void
672key_handler(struct window *window, struct input *input, uint32_t time,
673 uint32_t key, uint32_t sym,
674 enum wl_keyboard_key_state state, void *data)
675{
676 struct demoapp *app = data;
677 struct rectangle winrect;
678
679 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
680 return;
681
682 switch (sym) {
683 case XKB_KEY_space:
684 app->animate = !app->animate;
685 window_schedule_redraw(window);
686 break;
687 case XKB_KEY_Up:
688 window_get_allocation(window, &winrect);
689 winrect.height -= 100;
690 if (winrect.height < 150)
691 winrect.height = 150;
692 window_schedule_resize(window, winrect.width, winrect.height);
693 break;
694 case XKB_KEY_Down:
695 window_get_allocation(window, &winrect);
696 winrect.height += 100;
697 if (winrect.height > 600)
698 winrect.height = 600;
699 window_schedule_resize(window, winrect.width, winrect.height);
700 break;
701 case XKB_KEY_Escape:
702 display_exit(app->display);
703 break;
704 }
705}
706
707static struct demoapp *
708demoapp_create(struct display *display)
709{
710 struct demoapp *app;
711
712 app = calloc(1, sizeof *app);
713 if (!app)
714 return NULL;
715
716 app->egl = egl_state_create(display_get_display(display));
717
718 app->display = display;
719 display_set_user_data(app->display, app);
720
721 app->window = window_create(app->display);
722 app->widget = frame_create(app->window, app);
723 window_set_title(app->window, "Wayland Sub-surface Demo");
724
725 window_set_key_handler(app->window, key_handler);
726 window_set_user_data(app->window, app);
727 window_set_keyboard_focus_handler(app->window, keyboard_focus_handler);
728
729 widget_set_redraw_handler(app->widget, redraw_handler);
730 widget_set_resize_handler(app->widget, resize_handler);
731
732 app->subsurface = window_add_subsurface(app->window, app,
733 int_to_mode(option_red_mode));
734 widget_set_redraw_handler(app->subsurface, sub_redraw_handler);
735 widget_set_resize_handler(app->subsurface, sub_resize_handler);
736
737 if (app->egl && !option_no_triangle)
738 app->triangle = triangle_create(app->window, app->egl);
739
740 /* minimum size */
741 widget_schedule_resize(app->widget, 100, 100);
742
743 /* initial size */
744 widget_schedule_resize(app->widget, 400, 300);
745
746 app->animate = 1;
747
748 return app;
749}
750
751static void
752demoapp_destroy(struct demoapp *app)
753{
754 if (app->triangle)
755 triangle_destroy(app->triangle);
756
757 if (app->egl)
758 egl_state_destroy(app->egl);
759
760 widget_destroy(app->subsurface);
761 widget_destroy(app->widget);
762 window_destroy(app->window);
763 free(app);
764}
765
766int
767main(int argc, char *argv[])
768{
769 struct display *display;
770 struct demoapp *app;
771
772 parse_options(options, ARRAY_LENGTH(options), &argc, argv);
773 if (option_help) {
774 printf(help_text, argv[0]);
775 return 0;
776 }
777
778 display = display_create(&argc, argv);
779 if (display == NULL) {
780 fprintf(stderr, "failed to create display: %m\n");
781 return -1;
782 }
783
784 app = demoapp_create(display);
785
786 display_run(display);
787
788 demoapp_destroy(app);
789 display_destroy(display);
790
791 return 0;
792}