Update snapshot
[profile/ivi/weston.git] / src / compositor-android.c
1 /*
2  * Copyright © 2012 Collabora, Ltd.
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 #define _GNU_SOURCE
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <math.h>
29 #include <sys/types.h>
30 #include <dirent.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34
35 #include <EGL/egl.h>
36 #include <GLES2/gl2.h>
37
38 #include "compositor.h"
39 #include "android-framebuffer.h"
40 #include "evdev.h"
41
42 struct android_compositor;
43
44 struct android_output {
45         struct android_compositor *compositor;
46         struct weston_output base;
47
48         struct weston_mode mode;
49         struct android_framebuffer *fb;
50 };
51
52 struct android_seat {
53         struct weston_seat base;
54         struct wl_list devices_list;
55 };
56
57 struct android_compositor {
58         struct weston_compositor base;
59
60         struct android_seat *seat;
61 };
62
63 static inline struct android_output *
64 to_android_output(struct weston_output *base)
65 {
66         return container_of(base, struct android_output, base);
67 }
68
69 static inline struct android_seat *
70 to_android_seat(struct weston_seat *base)
71 {
72         return container_of(base, struct android_seat, base);
73 }
74
75 static inline struct android_compositor *
76 to_android_compositor(struct weston_compositor *base)
77 {
78         return container_of(base, struct android_compositor, base);
79 }
80
81 static const char *
82 egl_error_string(EGLint code)
83 {
84 #define MYERRCODE(x) case x: return #x;
85         switch (code) {
86         MYERRCODE(EGL_SUCCESS)
87         MYERRCODE(EGL_NOT_INITIALIZED)
88         MYERRCODE(EGL_BAD_ACCESS)
89         MYERRCODE(EGL_BAD_ALLOC)
90         MYERRCODE(EGL_BAD_ATTRIBUTE)
91         MYERRCODE(EGL_BAD_CONTEXT)
92         MYERRCODE(EGL_BAD_CONFIG)
93         MYERRCODE(EGL_BAD_CURRENT_SURFACE)
94         MYERRCODE(EGL_BAD_DISPLAY)
95         MYERRCODE(EGL_BAD_SURFACE)
96         MYERRCODE(EGL_BAD_MATCH)
97         MYERRCODE(EGL_BAD_PARAMETER)
98         MYERRCODE(EGL_BAD_NATIVE_PIXMAP)
99         MYERRCODE(EGL_BAD_NATIVE_WINDOW)
100         MYERRCODE(EGL_CONTEXT_LOST)
101         default:
102                 return "unknown";
103         }
104 #undef MYERRCODE
105 }
106
107 static void
108 print_egl_error_state(void)
109 {
110         EGLint code;
111
112         code = eglGetError();
113         weston_log("EGL error state: %s (0x%04lx)\n",
114                 egl_error_string(code), (long)code);
115 }
116
117 static void
118 android_finish_frame(void *data)
119 {
120         struct android_output *output = data;
121
122         weston_output_finish_frame(&output->base,
123                                    weston_compositor_get_time());
124 }
125
126 static void
127 android_output_repaint(struct weston_output *base, pixman_region32_t *damage)
128 {
129         struct android_output *output = to_android_output(base);
130         struct android_compositor *compositor = output->compositor;
131         struct weston_plane *primary_plane = &compositor->base.primary_plane;
132         struct wl_event_loop *loop;
133
134         compositor->base.renderer->repaint_output(&output->base, damage);
135
136         pixman_region32_subtract(&primary_plane->damage,
137                                  &primary_plane->damage, damage);
138
139         /* FIXME: does Android have a way to signal page flip done? */
140         loop = wl_display_get_event_loop(compositor->base.wl_display);
141         wl_event_loop_add_idle(loop, android_finish_frame, output);
142 }
143
144 static void
145 android_output_destroy(struct weston_output *base)
146 {
147         struct android_output *output = to_android_output(base);
148
149         wl_list_remove(&output->base.link);
150         weston_output_destroy(&output->base);
151
152         android_framebuffer_destroy(output->fb);
153
154         free(output);
155 }
156
157 static struct android_output *
158 android_output_create(struct android_compositor *compositor)
159 {
160         struct android_output *output;
161
162         output = calloc(1, sizeof *output);
163         if (!output)
164                 return NULL;
165
166         output->fb = android_framebuffer_create();
167         if (!output->fb) {
168                 free(output);
169                 return NULL;
170         }
171
172         output->compositor = compositor;
173         return output;
174 }
175
176 static void
177 android_compositor_add_output(struct android_compositor *compositor,
178                               struct android_output *output)
179 {
180         float mm_width, mm_height;
181
182         output->base.repaint = android_output_repaint;
183         output->base.destroy = android_output_destroy;
184         output->base.assign_planes = NULL;
185         output->base.set_backlight = NULL;
186         output->base.set_dpms = NULL;
187         output->base.switch_mode = NULL;
188
189         /* only one static mode in list */
190         output->mode.flags =
191                 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
192         output->mode.width = output->fb->width;
193         output->mode.height = output->fb->height;
194         output->mode.refresh = ceilf(1000.0f * output->fb->refresh_rate);
195         wl_list_init(&output->base.mode_list);
196         wl_list_insert(&output->base.mode_list, &output->mode.link);
197
198         output->base.current = &output->mode;
199         output->base.origin = &output->mode;
200         output->base.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
201         output->base.make = "unknown";
202         output->base.model = "unknown";
203
204         mm_width  = output->fb->width / output->fb->xdpi * 25.4f;
205         mm_height = output->fb->height / output->fb->ydpi * 25.4f;
206         weston_output_init(&output->base, &compositor->base,
207                            0, 0, round(mm_width), round(mm_height),
208                            WL_OUTPUT_TRANSFORM_NORMAL);
209         wl_list_insert(compositor->base.output_list.prev, &output->base.link);
210 }
211
212 static void
213 android_led_update(struct weston_seat *seat_base, enum weston_led leds)
214 {
215         struct android_seat *seat = to_android_seat(seat_base);
216         struct evdev_device *device;
217
218         wl_list_for_each(device, &seat->devices_list, link)
219                 evdev_led_update(device, leds);
220 }
221
222 static void
223 android_seat_open_device(struct android_seat *seat, const char *devnode)
224 {
225         struct evdev_device *device;
226         int fd;
227
228         /* XXX: check the Android excluded list */
229
230         fd = open(devnode, O_RDWR | O_NONBLOCK | O_CLOEXEC);
231         if (fd < 0) {
232                 weston_log_continue("opening '%s' failed: %s\n", devnode,
233                                     strerror(errno));
234                 return;
235         }
236
237         device = evdev_device_create(&seat->base, devnode, fd);
238         if (!device) {
239                 close(fd);
240                 return;
241         }
242
243         wl_list_insert(seat->devices_list.prev, &device->link);
244 }
245
246 static int
247 is_dot_or_dotdot(const char *str)
248 {
249         return (str[0] == '.' &&
250                 (str[1] == 0 || (str[1] == '.' && str[2] == 0)));
251 }
252
253 static void
254 android_seat_scan_devices(struct android_seat *seat, const char *dirpath)
255 {
256         int ret;
257         DIR *dir;
258         struct dirent *dent;
259         char *devnode = NULL;
260
261         dir = opendir(dirpath);
262         if (!dir) {
263                 weston_log("Could not open input device directory '%s': %s\n",
264                            dirpath, strerror(errno));
265                 return;
266         }
267
268         while ((dent = readdir(dir)) != NULL) {
269                 if (is_dot_or_dotdot(dent->d_name))
270                         continue;
271
272                 ret = asprintf(&devnode, "%s/%s", dirpath, dent->d_name);
273                 if (ret < 0)
274                         continue;
275
276                 android_seat_open_device(seat, devnode);
277                 free(devnode);
278         }
279
280         closedir(dir);
281 }
282
283 static void
284 android_seat_destroy(struct android_seat *seat)
285 {
286         struct evdev_device *device, *next;
287
288         wl_list_for_each_safe(device, next, &seat->devices_list, link)
289                 evdev_device_destroy(device);
290
291         if (seat->base.seat.keyboard)
292                 notify_keyboard_focus_out(&seat->base);
293
294         weston_seat_release(&seat->base);
295         free(seat);
296 }
297
298 static struct android_seat *
299 android_seat_create(struct android_compositor *compositor)
300 {
301         struct android_seat *seat;
302
303         seat = calloc(1, sizeof *seat);
304         if (!seat)
305                 return NULL;
306
307         weston_seat_init(&seat->base, &compositor->base);
308         seat->base.led_update = android_led_update;
309         wl_list_init(&seat->devices_list);
310
311         android_seat_scan_devices(seat, "/dev/input");
312
313         evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
314
315         if (wl_list_empty(&seat->devices_list))
316                 weston_log("Warning: no input devices found.\n");
317
318         /* XXX: implement hotplug support */
319
320         return seat;
321 }
322
323 static int
324 android_egl_choose_config(struct android_compositor *compositor,
325                           struct android_framebuffer *fb,
326                           const EGLint *attribs)
327 {
328         EGLBoolean ret;
329         EGLint count = 0;
330         EGLint matched = 0;
331         EGLConfig *configs;
332         int i;
333
334         /*
335          * The logic is copied from Android frameworks/base/services/
336          * surfaceflinger/DisplayHardware/DisplayHardware.cpp
337          */
338
339         compositor->base.egl_config = NULL;
340
341         ret = eglGetConfigs(compositor->base.egl_display, NULL, 0, &count);
342         if (ret == EGL_FALSE || count < 1)
343                 return -1;
344
345         configs = calloc(count, sizeof *configs);
346         if (!configs)
347                 return -1;
348
349         ret = eglChooseConfig(compositor->base.egl_display, attribs, configs,
350                               count, &matched);
351         if (ret == EGL_FALSE || matched < 1)
352                 goto out;
353
354         for (i = 0; i < matched; ++i) {
355                 EGLint id;
356                 ret = eglGetConfigAttrib(compositor->base.egl_display,
357                                          configs[i], EGL_NATIVE_VISUAL_ID,
358                                          &id);
359                 if (ret == EGL_FALSE)
360                         continue;
361                 if (id > 0 && fb->format == id) {
362                         compositor->base.egl_config = configs[i];
363                         break;
364                 }
365         }
366
367 out:
368         free(configs);
369         if (!compositor->base.egl_config)
370                 return -1;
371
372         return 0;
373 }
374
375 static int
376 android_init_egl(struct android_compositor *compositor,
377                  struct android_output *output)
378 {
379         EGLint eglmajor, eglminor;
380         int ret;
381
382         static const EGLint config_attrs[] = {
383                 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
384                 EGL_RED_SIZE, 1,
385                 EGL_GREEN_SIZE, 1,
386                 EGL_BLUE_SIZE, 1,
387                 EGL_ALPHA_SIZE, 0,
388                 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
389                 EGL_NONE
390         };
391
392         compositor->base.egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
393         if (compositor->base.egl_display == EGL_NO_DISPLAY) {
394                 weston_log("Failed to create EGL display.\n");
395                 print_egl_error_state();
396                 return -1;
397         }
398
399         ret = eglInitialize(compositor->base.egl_display, &eglmajor, &eglminor);
400         if (!ret) {
401                 weston_log("Failed to initialise EGL.\n");
402                 print_egl_error_state();
403                 return -1;
404         }
405
406         ret = android_egl_choose_config(compositor, output->fb, config_attrs);
407         if (ret < 0) {
408                 weston_log("Failed to find an EGL config.\n");
409                 print_egl_error_state();
410                 return -1;
411         }
412
413         output->base.egl_surface =
414                 eglCreateWindowSurface(compositor->base.egl_display,
415                                        compositor->base.egl_config,
416                                        output->fb->native_window,
417                                        NULL);
418         if (output->base.egl_surface == EGL_NO_SURFACE) {
419                 weston_log("Failed to create FB EGLSurface.\n");
420                 print_egl_error_state();
421                 return -1;
422         }
423
424         return 0;
425 }
426
427 static void
428 android_fini_egl(struct android_compositor *compositor)
429 {
430         gles2_renderer_destroy(&compositor->base);
431
432         eglMakeCurrent(compositor->base.egl_display,
433                        EGL_NO_SURFACE, EGL_NO_SURFACE,
434                        EGL_NO_CONTEXT);
435
436         eglTerminate(compositor->base.egl_display);
437         eglReleaseThread();
438 }
439
440 static void
441 android_compositor_destroy(struct weston_compositor *base)
442 {
443         struct android_compositor *compositor = to_android_compositor(base);
444
445         android_seat_destroy(compositor->seat);
446
447         /* destroys outputs, too */
448         weston_compositor_shutdown(&compositor->base);
449
450         android_fini_egl(compositor);
451
452         free(compositor);
453 }
454
455 static struct weston_compositor *
456 android_compositor_create(struct wl_display *display, int argc, char *argv[],
457                           const char *config_file)
458 {
459         struct android_compositor *compositor;
460         struct android_output *output;
461
462         weston_log("initializing android backend\n");
463
464         compositor = calloc(1, sizeof *compositor);
465         if (compositor == NULL)
466                 return NULL;
467
468         if (weston_compositor_init(&compositor->base, display, argc, argv,
469                                    config_file) < 0)
470                 goto err_free;
471
472         compositor->base.destroy = android_compositor_destroy;
473
474         compositor->base.focus = 1;
475
476         output = android_output_create(compositor);
477         if (!output)
478                 goto err_compositor;
479
480         if (android_init_egl(compositor, output) < 0)
481                 goto err_output;
482
483         android_compositor_add_output(compositor, output);
484
485         if (gles2_renderer_init(&compositor->base) < 0)
486                 goto err_egl;
487
488         compositor->seat = android_seat_create(compositor);
489         if (!compositor->seat)
490                 goto err_egl;
491
492         return &compositor->base;
493
494 err_egl:
495         android_fini_egl(compositor);
496 err_output:
497         android_output_destroy(&output->base);
498 err_compositor:
499         weston_compositor_shutdown(&compositor->base);
500 err_free:
501         free(compositor);
502         return NULL;
503 }
504
505 WL_EXPORT struct weston_compositor *
506 backend_init(struct wl_display *display, int argc, char *argv[],
507              const char *config_file)
508 {
509         return android_compositor_create(display, argc, argv, config_file);
510 }