compositor: Store opaque clip for previous frame in weston_surface
[profile/ivi/weston.git] / src / compositor-wayland.c
1 /*
2  * Copyright © 2010-2011 Benjamin Franzke
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and
5  * its documentation for any purpose is hereby granted without fee, provided
6  * that the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation, and that the name of the copyright holders not be used in
9  * advertising or publicity pertaining to distribution of the software
10  * without specific, written prior permission.  The copyright holders make
11  * no representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stddef.h>
28 #define _GNU_SOURCE
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34
35 #include <wayland-client.h>
36 #include <wayland-egl.h>
37
38 #include <GLES2/gl2.h>
39 #include <GLES2/gl2ext.h>
40 #include <EGL/egl.h>
41 #include <EGL/eglext.h>
42
43 #include "compositor.h"
44
45 struct wayland_compositor {
46         struct weston_compositor         base;
47
48         struct {
49                 struct wl_display *display;
50                 struct wl_compositor *compositor;
51                 struct wl_shell *shell;
52                 struct wl_output *output;
53
54                 struct {
55                         int32_t x, y, width, height;
56                 } screen_allocation;
57
58                 struct wl_event_source *wl_source;
59                 uint32_t event_mask;
60         } parent;
61
62         struct {
63                 int32_t top, bottom, left, right;
64                 GLuint texture;
65                 int32_t width, height;
66         } border;
67
68         struct wl_list input_list;
69 };
70
71 struct wayland_output {
72         struct weston_output    base;
73
74         struct {
75                 struct wl_surface       *surface;
76                 struct wl_shell_surface *shell_surface;
77                 struct wl_egl_window    *egl_window;
78         } parent;
79         EGLSurface egl_surface;
80         struct weston_mode      mode;
81 };
82
83 struct wayland_input {
84         struct wayland_compositor *compositor;
85         struct wl_input_device *input_device;
86         struct wl_list link;
87 };
88
89
90 static int
91 texture_border(struct wayland_output *output)
92 {
93         struct wayland_compositor *c =
94                 (struct wayland_compositor *) output->base.compositor;
95         GLfloat *d;
96         unsigned int *p;
97         int i, j, k, n;
98         GLfloat x[4], y[4], u[4], v[4];
99
100         x[0] = -c->border.left;
101         x[1] = 0;
102         x[2] = output->base.current->width;
103         x[3] = output->base.current->width + c->border.right;
104
105         y[0] = -c->border.top;
106         y[1] = 0;
107         y[2] = output->base.current->height;
108         y[3] = output->base.current->height + c->border.bottom;
109
110         u[0] = 0.0;
111         u[1] = (GLfloat) c->border.left / c->border.width;
112         u[2] = (GLfloat) (c->border.width - c->border.right) / c->border.width;
113         u[3] = 1.0;
114
115         v[0] = 0.0;
116         v[1] = (GLfloat) c->border.top / c->border.height;
117         v[2] = (GLfloat) (c->border.height - c->border.bottom) / c->border.height;
118         v[3] = 1.0;
119
120         n = 8;
121         d = wl_array_add(&c->base.vertices, n * 16 * sizeof *d);
122         p = wl_array_add(&c->base.indices, n * 6 * sizeof *p);
123
124         k = 0;
125         for (i = 0; i < 3; i++)
126                 for (j = 0; j < 3; j++) {
127
128                         if (i == 1 && j == 1)
129                                 continue;
130
131                         d[ 0] = x[i];
132                         d[ 1] = y[j];
133                         d[ 2] = u[i];
134                         d[ 3] = v[j];
135
136                         d[ 4] = x[i];
137                         d[ 5] = y[j + 1];
138                         d[ 6] = u[i];
139                         d[ 7] = v[j + 1];
140
141                         d[ 8] = x[i + 1];
142                         d[ 9] = y[j];
143                         d[10] = u[i + 1];
144                         d[11] = v[j];
145
146                         d[12] = x[i + 1];
147                         d[13] = y[j + 1];
148                         d[14] = u[i + 1];
149                         d[15] = v[j + 1];
150
151                         p[0] = k + 0;
152                         p[1] = k + 1;
153                         p[2] = k + 2;
154                         p[3] = k + 2;
155                         p[4] = k + 1;
156                         p[5] = k + 3;
157
158                         d += 16;
159                         p += 6;
160                         k += 4;
161                 }
162
163         return k / 4;
164 }
165
166 static void
167 draw_border(struct wayland_output *output)
168 {
169         struct wayland_compositor *c =
170                 (struct wayland_compositor *) output->base.compositor;
171         struct weston_shader *shader = &c->base.texture_shader;
172         GLfloat *v;
173         int n;
174
175         glDisable(GL_BLEND);
176         glUseProgram(shader->program);
177         c->base.current_shader = shader;
178
179         glUniformMatrix4fv(shader->proj_uniform,
180                            1, GL_FALSE, output->base.matrix.d);
181
182         glUniform1i(shader->tex_uniform, 0);
183         glUniform1f(shader->alpha_uniform, 1);
184         glUniform1f(shader->texwidth_uniform, 1);
185
186         n = texture_border(output);
187
188         glBindTexture(GL_TEXTURE_2D, c->border.texture);
189
190         v = c->base.vertices.data;
191         glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
192         glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
193         glEnableVertexAttribArray(0);
194         glEnableVertexAttribArray(1);
195
196         glDrawElements(GL_TRIANGLES, n * 6,
197                        GL_UNSIGNED_INT, c->base.indices.data);
198
199         glDisableVertexAttribArray(1);
200         glDisableVertexAttribArray(0);
201
202         c->base.vertices.size = 0;
203         c->base.indices.size = 0;
204 }
205
206 static void
207 create_border(struct wayland_compositor *c)
208 {
209         uint32_t *pixels, stride;
210
211         pixels = weston_load_image(DATADIR "/weston/border.png",
212                                    &c->border.width,
213                                    &c->border.height, &stride);
214         if (!pixels) {
215                 fprintf(stderr, "could'nt load border image\n");
216                 return;
217         }
218
219         glGenTextures(1, &c->border.texture);
220         glBindTexture(GL_TEXTURE_2D, c->border.texture);
221         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
222         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
223         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
224         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
225
226         glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
227                      c->border.width,
228                      c->border.height,
229                      0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
230
231         c->border.top = 25;
232         c->border.bottom = 50;
233         c->border.left = 25;
234         c->border.right = 25;
235 }
236
237 static int
238 wayland_input_create(struct wayland_compositor *c)
239 {
240         struct weston_input_device *input;
241
242         input = malloc(sizeof *input);
243         if (input == NULL)
244                 return -1;
245
246         memset(input, 0, sizeof *input);
247         weston_input_device_init(input, &c->base);
248
249         c->base.input_device = &input->input_device;
250
251         return 0;
252 }
253
254 static int
255 wayland_compositor_init_egl(struct wayland_compositor *c)
256 {
257         EGLint major, minor;
258         EGLint n;
259         const char *extensions;
260         EGLint config_attribs[] = {
261                 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
262                 EGL_RED_SIZE, 1,
263                 EGL_GREEN_SIZE, 1,
264                 EGL_BLUE_SIZE, 1,
265                 EGL_ALPHA_SIZE, 1,
266                 EGL_DEPTH_SIZE, 1,
267                 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
268                 EGL_NONE
269         };
270         static const EGLint context_attribs[] = {
271                 EGL_CONTEXT_CLIENT_VERSION, 2,
272                 EGL_NONE
273         };
274
275         c->base.display = eglGetDisplay(c->parent.display);
276         if (c->base.display == NULL) {
277                 fprintf(stderr, "failed to create display\n");
278                 return -1;
279         }
280
281         if (!eglInitialize(c->base.display, &major, &minor)) {
282                 fprintf(stderr, "failed to initialize display\n");
283                 return -1;
284         }
285
286         extensions = eglQueryString(c->base.display, EGL_EXTENSIONS);
287         if (!strstr(extensions, "EGL_KHR_surfaceless_gles2")) {
288                 fprintf(stderr, "EGL_KHR_surfaceless_gles2 not available\n");
289                 return -1;
290         }
291
292         if (!eglBindAPI(EGL_OPENGL_ES_API)) {
293                 fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n");
294                 return -1;
295         }
296         if (!eglChooseConfig(c->base.display, config_attribs,
297                              &c->base.config, 1, &n) || n == 0) {
298                 fprintf(stderr, "failed to choose config: %d\n", n);
299                 return -1;
300         }
301
302         c->base.context = eglCreateContext(c->base.display, c->base.config,
303                                            EGL_NO_CONTEXT, context_attribs);
304         if (c->base.context == NULL) {
305                 fprintf(stderr, "failed to create context\n");
306                 return -1;
307         }
308
309         if (!eglMakeCurrent(c->base.display, EGL_NO_SURFACE,
310                             EGL_NO_SURFACE, c->base.context)) {
311                 fprintf(stderr, "failed to make context current\n");
312                 return -1;
313         }
314
315         return 0;
316 }
317
318 static void
319 frame_done(void *data, struct wl_callback *callback, uint32_t time)
320 {
321         struct weston_output *output = data;
322
323         wl_callback_destroy(callback);
324         weston_output_finish_frame(output, time);
325 }
326
327 static const struct wl_callback_listener frame_listener = {
328         frame_done
329 };
330
331 static void
332 wayland_output_repaint(struct weston_output *output_base,
333                        pixman_region32_t *damage)
334 {
335         struct wayland_output *output = (struct wayland_output *) output_base;
336         struct wayland_compositor *compositor =
337                 (struct wayland_compositor *) output->base.compositor;
338         struct wl_callback *callback;
339         struct weston_surface *surface;
340
341         if (!eglMakeCurrent(compositor->base.display, output->egl_surface,
342                             output->egl_surface, compositor->base.context)) {
343                 fprintf(stderr, "failed to make current\n");
344                 return;
345         }
346
347         wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
348                 weston_surface_draw(surface, &output->base, damage);
349
350         draw_border(output);
351
352         eglSwapBuffers(compositor->base.display, output->egl_surface);
353         callback = wl_surface_frame(output->parent.surface);
354         wl_callback_add_listener(callback, &frame_listener, output);
355
356         return;
357 }
358
359 static void
360 wayland_output_destroy(struct weston_output *output_base)
361 {
362         struct wayland_output *output = (struct wayland_output *) output_base;
363         struct weston_compositor *ec = output->base.compositor;
364
365         eglDestroySurface(ec->display, output->egl_surface);
366         wl_egl_window_destroy(output->parent.egl_window);
367         free(output);
368
369         return;
370 }
371
372 static int
373 wayland_compositor_create_output(struct wayland_compositor *c,
374                                  int width, int height)
375 {
376         struct wayland_output *output;
377
378         output = malloc(sizeof *output);
379         if (output == NULL)
380                 return -1;
381         memset(output, 0, sizeof *output);
382
383         output->mode.flags =
384                 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
385         output->mode.width = width;
386         output->mode.height = height;
387         output->mode.refresh = 60;
388         wl_list_init(&output->base.mode_list);
389         wl_list_insert(&output->base.mode_list, &output->mode.link);
390
391         output->base.current = &output->mode;
392         weston_output_init(&output->base, &c->base, 0, 0, width, height,
393                          WL_OUTPUT_FLIPPED);
394
395         output->base.border.top = c->border.top;
396         output->base.border.bottom = c->border.bottom;
397         output->base.border.left = c->border.left;
398         output->base.border.right = c->border.right;
399
400         weston_output_move(&output->base, 0, 0);
401
402         output->parent.surface =
403                 wl_compositor_create_surface(c->parent.compositor);
404         wl_surface_set_user_data(output->parent.surface, output);
405
406         output->parent.egl_window =
407                 wl_egl_window_create(output->parent.surface,
408                                      width + c->border.left + c->border.right,
409                                      height + c->border.top + c->border.bottom);
410         if (!output->parent.egl_window) {
411                 fprintf(stderr, "failure to create wl_egl_window\n");
412                 goto cleanup_output;
413         }
414
415         output->egl_surface =
416                 eglCreateWindowSurface(c->base.display, c->base.config,
417                                        output->parent.egl_window, NULL);
418         if (!output->egl_surface) {
419                 fprintf(stderr, "failed to create window surface\n");
420                 goto cleanup_window;
421         }
422
423         if (!eglMakeCurrent(c->base.display, output->egl_surface,
424                             output->egl_surface, c->base.context)) {
425                 fprintf(stderr, "failed to make surface current\n");
426                 goto cleanup_surface;
427                 return -1;
428         }
429
430         output->parent.shell_surface =
431                 wl_shell_get_shell_surface(c->parent.shell,
432                                            output->parent.surface);
433         /* FIXME: add shell_surface listener for resizing */
434         wl_shell_surface_set_toplevel(output->parent.shell_surface);
435
436         output->base.repaint = wayland_output_repaint;
437         output->base.destroy = wayland_output_destroy;
438         output->base.assign_planes = NULL;
439         output->base.set_backlight = NULL;
440         output->base.set_dpms = NULL;
441
442         wl_list_insert(c->base.output_list.prev, &output->base.link);
443
444         return 0;
445
446 cleanup_surface:
447         eglDestroySurface(c->base.display, output->egl_surface);
448 cleanup_window:
449         wl_egl_window_destroy(output->parent.egl_window);
450 cleanup_output:
451         /* FIXME: cleanup weston_output */
452         free(output);
453
454         return -1;
455 }
456
457 /* Events received from the wayland-server this compositor is client of: */
458
459 /* parent output interface */
460 static void
461 display_handle_geometry(void *data,
462                         struct wl_output *wl_output,
463                         int x,
464                         int y,
465                         int physical_width,
466                         int physical_height,
467                         int subpixel,
468                         const char *make,
469                         const char *model)
470 {
471         struct wayland_compositor *c = data;
472
473         c->parent.screen_allocation.x = x;
474         c->parent.screen_allocation.y = y;
475 }
476
477 static void
478 display_handle_mode(void *data,
479                     struct wl_output *wl_output,
480                     uint32_t flags,
481                     int width,
482                     int height,
483                     int refresh)
484 {
485         struct wayland_compositor *c = data;
486
487         c->parent.screen_allocation.width = width;
488         c->parent.screen_allocation.height = height;
489 }
490
491 static const struct wl_output_listener output_listener = {
492         display_handle_geometry,
493         display_handle_mode
494 };
495
496 /* parent input interface */
497 static void
498 input_handle_motion(void *data, struct wl_input_device *input_device,
499                     uint32_t time, int32_t sx, int32_t sy)
500 {
501         struct wayland_input *input = data;
502         struct wayland_compositor *c = input->compositor;
503
504         notify_motion(c->base.input_device, time,
505                       sx - c->border.left, sy - c->border.top);
506 }
507
508 static void
509 input_handle_button(void *data,
510                      struct wl_input_device *input_device,
511                      uint32_t time, uint32_t button, uint32_t state)
512 {
513         struct wayland_input *input = data;
514         struct wayland_compositor *c = input->compositor;
515
516         notify_button(c->base.input_device, time, button, state);
517 }
518
519 static void
520 input_handle_key(void *data, struct wl_input_device *input_device,
521                   uint32_t time, uint32_t key, uint32_t state)
522 {
523         struct wayland_input *input = data;
524         struct wayland_compositor *c = input->compositor;
525
526         notify_key(c->base.input_device, time, key, state);
527 }
528
529 static void
530 input_handle_pointer_enter(void *data,
531                            struct wl_input_device *input_device,
532                            uint32_t time, struct wl_surface *surface,
533                            int32_t sx, int32_t sy)
534 {
535         struct wayland_input *input = data;
536         struct wayland_output *output;
537         struct wayland_compositor *c = input->compositor;
538
539         output = wl_surface_get_user_data(surface);
540         notify_pointer_focus(c->base.input_device,
541                              time, &output->base, sx, sy);
542         wl_input_device_attach(input->input_device, time, NULL, 0, 0);
543 }
544
545 static void
546 input_handle_pointer_leave(void *data,
547                            struct wl_input_device *input_device,
548                            uint32_t time, struct wl_surface *surface)
549 {
550         struct wayland_input *input = data;
551         struct wayland_compositor *c = input->compositor;
552
553         notify_pointer_focus(c->base.input_device, time, NULL, 0, 0);
554 }
555
556 static void
557 input_handle_keyboard_enter(void *data,
558                             struct wl_input_device *input_device,
559                             uint32_t time,
560                             struct wl_surface *surface,
561                             struct wl_array *keys)
562 {
563         struct wayland_input *input = data;
564         struct wayland_compositor *c = input->compositor;
565         struct wayland_output *output;
566
567         output = wl_surface_get_user_data(surface);
568         notify_keyboard_focus(c->base.input_device, time, &output->base, keys);
569 }
570
571 static void
572 input_handle_keyboard_leave(void *data,
573                             struct wl_input_device *input_device,
574                             uint32_t time,
575                             struct wl_surface *surface)
576 {
577         struct wayland_input *input = data;
578         struct wayland_compositor *c = input->compositor;
579
580         notify_keyboard_focus(c->base.input_device, time, NULL, NULL);
581 }
582
583 static const struct wl_input_device_listener input_device_listener = {
584         input_handle_motion,
585         input_handle_button,
586         input_handle_key,
587         input_handle_pointer_enter,
588         input_handle_pointer_leave,
589         input_handle_keyboard_enter,
590         input_handle_keyboard_leave,
591 };
592
593 static void
594 display_add_input(struct wayland_compositor *c, uint32_t id)
595 {
596         struct wayland_input *input;
597
598         input = malloc(sizeof *input);
599         if (input == NULL)
600                 return;
601
602         memset(input, 0, sizeof *input);
603
604         input->compositor = c;
605         input->input_device = wl_display_bind(c->parent.display,
606                                               id, &wl_input_device_interface);
607         wl_list_insert(c->input_list.prev, &input->link);
608
609         wl_input_device_add_listener(input->input_device,
610                                      &input_device_listener, input);
611         wl_input_device_set_user_data(input->input_device, input);
612 }
613
614 static void
615 display_handle_global(struct wl_display *display, uint32_t id,
616                       const char *interface, uint32_t version, void *data)
617 {
618         struct wayland_compositor *c = data;
619
620         if (strcmp(interface, "wl_compositor") == 0) {
621                 c->parent.compositor =
622                         wl_display_bind(display, id, &wl_compositor_interface);
623         } else if (strcmp(interface, "wl_output") == 0) {
624                 c->parent.output =
625                         wl_display_bind(display, id, &wl_output_interface);
626                 wl_output_add_listener(c->parent.output, &output_listener, c);
627         } else if (strcmp(interface, "wl_input_device") == 0) {
628                 display_add_input(c, id);
629         } else if (strcmp(interface, "wl_shell") == 0) {
630                 c->parent.shell =
631                         wl_display_bind(display, id, &wl_shell_interface);
632         }
633 }
634
635 static int
636 update_event_mask(uint32_t mask, void *data)
637 {
638         struct wayland_compositor *c = data;
639
640         c->parent.event_mask = mask;
641         if (c->parent.wl_source)
642                 wl_event_source_fd_update(c->parent.wl_source, mask);
643
644         return 0;
645 }
646
647 static int
648 wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
649 {
650         struct wayland_compositor *c = data;
651
652         if (mask & WL_EVENT_READABLE)
653                 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
654         if (mask & WL_EVENT_WRITABLE)
655                 wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE);
656
657         return 1;
658 }
659
660 static void
661 wayland_destroy(struct weston_compositor *ec)
662 {
663         weston_compositor_shutdown(ec);
664
665         free(ec);
666 }
667
668 static struct weston_compositor *
669 wayland_compositor_create(struct wl_display *display,
670                           int width, int height, const char *display_name)
671 {
672         struct wayland_compositor *c;
673         struct wl_event_loop *loop;
674         int fd;
675
676         c = malloc(sizeof *c);
677         if (c == NULL)
678                 return NULL;
679
680         memset(c, 0, sizeof *c);
681
682         c->parent.display = wl_display_connect(display_name);
683
684         if (c->parent.display == NULL) {
685                 fprintf(stderr, "failed to create display: %m\n");
686                 return NULL;
687         }
688
689         wl_list_init(&c->input_list);
690         wl_display_add_global_listener(c->parent.display,
691                                 display_handle_global, c);
692
693         wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
694
695         c->base.wl_display = display;
696         if (wayland_compositor_init_egl(c) < 0)
697                 return NULL;
698
699         c->base.destroy = wayland_destroy;
700
701         /* Can't init base class until we have a current egl context */
702         if (weston_compositor_init(&c->base, display) < 0)
703                 return NULL;
704
705         create_border(c);
706         if (wayland_compositor_create_output(c, width, height) < 0)
707                 return NULL;
708
709         if (wayland_input_create(c) < 0)
710                 return NULL;
711
712         loop = wl_display_get_event_loop(c->base.wl_display);
713
714         fd = wl_display_get_fd(c->parent.display, update_event_mask, c);
715         c->parent.wl_source =
716                 wl_event_loop_add_fd(loop, fd, c->parent.event_mask,
717                                      wayland_compositor_handle_event, c);
718         if (c->parent.wl_source == NULL)
719                 return NULL;
720
721         return &c->base;
722 }
723
724 struct weston_compositor *
725 backend_init(struct wl_display *display, char *options);
726
727 WL_EXPORT struct weston_compositor *
728 backend_init(struct wl_display *display, char *options)
729 {
730         int width = 1024, height = 640, i;
731         char *p, *value, *display_name = NULL;
732
733         static char * const tokens[] = { "width", "height", "display", NULL };
734         
735         p = options;
736         while (i = getsubopt(&p, tokens, &value), i != -1) {
737                 switch (i) {
738                 case 0:
739                         width = strtol(value, NULL, 0);
740                         break;
741                 case 1:
742                         height = strtol(value, NULL, 0);
743                         break;
744                 case 2:
745                         display_name = strdup(value);
746                         break;
747                 }
748         }
749
750         return wayland_compositor_create(display, width, height, display_name);
751 }