compositor-android: Add output before gles2_renderer_init()
[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 wl_event_loop *loop;
132
133         compositor->base.renderer->repaint_output(&output->base, damage);
134
135         /* FIXME: does Android have a way to signal page flip done? */
136         loop = wl_display_get_event_loop(compositor->base.wl_display);
137         wl_event_loop_add_idle(loop, android_finish_frame, output);
138 }
139
140 static void
141 android_output_destroy(struct weston_output *base)
142 {
143         struct android_output *output = to_android_output(base);
144
145         wl_list_remove(&output->base.link);
146         weston_output_destroy(&output->base);
147
148         android_framebuffer_destroy(output->fb);
149
150         free(output);
151 }
152
153 static struct android_output *
154 android_output_create(struct android_compositor *compositor)
155 {
156         struct android_output *output;
157
158         output = calloc(1, sizeof *output);
159         if (!output)
160                 return NULL;
161
162         output->fb = android_framebuffer_create();
163         if (!output->fb) {
164                 free(output);
165                 return NULL;
166         }
167
168         output->compositor = compositor;
169         return output;
170 }
171
172 static void
173 android_compositor_add_output(struct android_compositor *compositor,
174                               struct android_output *output)
175 {
176         float mm_width, mm_height;
177
178         output->base.repaint = android_output_repaint;
179         output->base.destroy = android_output_destroy;
180         output->base.assign_planes = NULL;
181         output->base.set_backlight = NULL;
182         output->base.set_dpms = NULL;
183         output->base.switch_mode = NULL;
184
185         /* only one static mode in list */
186         output->mode.flags =
187                 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
188         output->mode.width = output->fb->width;
189         output->mode.height = output->fb->height;
190         output->mode.refresh = ceilf(1000.0f * output->fb->refresh_rate);
191         wl_list_init(&output->base.mode_list);
192         wl_list_insert(&output->base.mode_list, &output->mode.link);
193
194         output->base.current = &output->mode;
195         output->base.origin = &output->mode;
196         output->base.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
197         output->base.make = "unknown";
198         output->base.model = "unknown";
199
200         mm_width  = output->fb->width / output->fb->xdpi * 25.4f;
201         mm_height = output->fb->height / output->fb->ydpi * 25.4f;
202         weston_output_init(&output->base, &compositor->base,
203                            0, 0, round(mm_width), round(mm_height),
204                            WL_OUTPUT_TRANSFORM_NORMAL);
205         wl_list_insert(compositor->base.output_list.prev, &output->base.link);
206 }
207
208 static void
209 android_led_update(struct weston_seat *seat_base, enum weston_led leds)
210 {
211         struct android_seat *seat = to_android_seat(seat_base);
212         struct evdev_device *device;
213
214         wl_list_for_each(device, &seat->devices_list, link)
215                 evdev_led_update(device, leds);
216 }
217
218 static void
219 android_seat_open_device(struct android_seat *seat, const char *devnode)
220 {
221         struct evdev_device *device;
222         int fd;
223
224         /* XXX: check the Android excluded list */
225
226         fd = open(devnode, O_RDWR | O_NONBLOCK | O_CLOEXEC);
227         if (fd < 0) {
228                 weston_log_continue("opening '%s' failed: %s\n", devnode,
229                                     strerror(errno));
230                 return;
231         }
232
233         device = evdev_device_create(&seat->base, devnode, fd);
234         if (!device) {
235                 close(fd);
236                 return;
237         }
238
239         wl_list_insert(seat->devices_list.prev, &device->link);
240 }
241
242 static int
243 is_dot_or_dotdot(const char *str)
244 {
245         return (str[0] == '.' &&
246                 (str[1] == 0 || (str[1] == '.' && str[2] == 0)));
247 }
248
249 static void
250 android_seat_scan_devices(struct android_seat *seat, const char *dirpath)
251 {
252         int ret;
253         DIR *dir;
254         struct dirent *dent;
255         char *devnode = NULL;
256
257         dir = opendir(dirpath);
258         if (!dir) {
259                 weston_log("Could not open input device directory '%s': %s\n",
260                            dirpath, strerror(errno));
261                 return;
262         }
263
264         while ((dent = readdir(dir)) != NULL) {
265                 if (is_dot_or_dotdot(dent->d_name))
266                         continue;
267
268                 ret = asprintf(&devnode, "%s/%s", dirpath, dent->d_name);
269                 if (ret < 0)
270                         continue;
271
272                 android_seat_open_device(seat, devnode);
273                 free(devnode);
274         }
275
276         closedir(dir);
277 }
278
279 static void
280 android_seat_destroy(struct android_seat *seat)
281 {
282         struct evdev_device *device, *next;
283
284         wl_list_for_each_safe(device, next, &seat->devices_list, link)
285                 evdev_device_destroy(device);
286
287         if (seat->base.seat.keyboard)
288                 notify_keyboard_focus_out(&seat->base);
289
290         weston_seat_release(&seat->base);
291         free(seat);
292 }
293
294 static struct android_seat *
295 android_seat_create(struct android_compositor *compositor)
296 {
297         struct android_seat *seat;
298
299         seat = calloc(1, sizeof *seat);
300         if (!seat)
301                 return NULL;
302
303         weston_seat_init(&seat->base, &compositor->base);
304         seat->base.led_update = android_led_update;
305         wl_list_init(&seat->devices_list);
306
307         android_seat_scan_devices(seat, "/dev/input");
308
309         evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
310
311         if (wl_list_empty(&seat->devices_list))
312                 weston_log("Warning: no input devices found.\n");
313
314         /* XXX: implement hotplug support */
315
316         return seat;
317 }
318
319 static int
320 android_egl_choose_config(struct android_compositor *compositor,
321                           struct android_framebuffer *fb,
322                           const EGLint *attribs)
323 {
324         EGLBoolean ret;
325         EGLint count = 0;
326         EGLint matched = 0;
327         EGLConfig *configs;
328         int i;
329
330         /*
331          * The logic is copied from Android frameworks/base/services/
332          * surfaceflinger/DisplayHardware/DisplayHardware.cpp
333          */
334
335         compositor->base.egl_config = NULL;
336
337         ret = eglGetConfigs(compositor->base.egl_display, NULL, 0, &count);
338         if (ret == EGL_FALSE || count < 1)
339                 return -1;
340
341         configs = calloc(count, sizeof *configs);
342         if (!configs)
343                 return -1;
344
345         ret = eglChooseConfig(compositor->base.egl_display, attribs, configs,
346                               count, &matched);
347         if (ret == EGL_FALSE || matched < 1)
348                 goto out;
349
350         for (i = 0; i < matched; ++i) {
351                 EGLint id;
352                 ret = eglGetConfigAttrib(compositor->base.egl_display,
353                                          configs[i], EGL_NATIVE_VISUAL_ID,
354                                          &id);
355                 if (ret == EGL_FALSE)
356                         continue;
357                 if (id > 0 && fb->format == id) {
358                         compositor->base.egl_config = configs[i];
359                         break;
360                 }
361         }
362
363 out:
364         free(configs);
365         if (!compositor->base.egl_config)
366                 return -1;
367
368         return 0;
369 }
370
371 static int
372 android_init_egl(struct android_compositor *compositor,
373                  struct android_output *output)
374 {
375         EGLint eglmajor, eglminor;
376         int ret;
377
378         static const EGLint config_attrs[] = {
379                 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
380                 EGL_RED_SIZE, 1,
381                 EGL_GREEN_SIZE, 1,
382                 EGL_BLUE_SIZE, 1,
383                 EGL_ALPHA_SIZE, 0,
384                 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
385                 EGL_NONE
386         };
387
388         compositor->base.egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
389         if (compositor->base.egl_display == EGL_NO_DISPLAY) {
390                 weston_log("Failed to create EGL display.\n");
391                 print_egl_error_state();
392                 return -1;
393         }
394
395         ret = eglInitialize(compositor->base.egl_display, &eglmajor, &eglminor);
396         if (!ret) {
397                 weston_log("Failed to initialise EGL.\n");
398                 print_egl_error_state();
399                 return -1;
400         }
401
402         ret = android_egl_choose_config(compositor, output->fb, config_attrs);
403         if (ret < 0) {
404                 weston_log("Failed to find an EGL config.\n");
405                 print_egl_error_state();
406                 return -1;
407         }
408
409         output->base.egl_surface =
410                 eglCreateWindowSurface(compositor->base.egl_display,
411                                        compositor->base.egl_config,
412                                        output->fb->native_window,
413                                        NULL);
414         if (output->base.egl_surface == EGL_NO_SURFACE) {
415                 weston_log("Failed to create FB EGLSurface.\n");
416                 print_egl_error_state();
417                 return -1;
418         }
419
420         return 0;
421 }
422
423 static void
424 android_fini_egl(struct android_compositor *compositor)
425 {
426         gles2_renderer_destroy(&compositor->base);
427
428         eglMakeCurrent(compositor->base.egl_display,
429                        EGL_NO_SURFACE, EGL_NO_SURFACE,
430                        EGL_NO_CONTEXT);
431
432         eglTerminate(compositor->base.egl_display);
433         eglReleaseThread();
434 }
435
436 static void
437 android_compositor_destroy(struct weston_compositor *base)
438 {
439         struct android_compositor *compositor = to_android_compositor(base);
440
441         android_seat_destroy(compositor->seat);
442
443         /* destroys outputs, too */
444         weston_compositor_shutdown(&compositor->base);
445
446         android_fini_egl(compositor);
447
448         free(compositor);
449 }
450
451 static struct weston_compositor *
452 android_compositor_create(struct wl_display *display, int argc, char *argv[],
453                           const char *config_file)
454 {
455         struct android_compositor *compositor;
456         struct android_output *output;
457
458         weston_log("initializing android backend\n");
459
460         compositor = calloc(1, sizeof *compositor);
461         if (compositor == NULL)
462                 return NULL;
463
464         if (weston_compositor_init(&compositor->base, display, argc, argv,
465                                    config_file) < 0)
466                 goto err_free;
467
468         compositor->base.destroy = android_compositor_destroy;
469
470         compositor->base.focus = 1;
471
472         output = android_output_create(compositor);
473         if (!output)
474                 goto err_compositor;
475
476         if (android_init_egl(compositor, output) < 0)
477                 goto err_output;
478
479         android_compositor_add_output(compositor, output);
480
481         if (gles2_renderer_init(&compositor->base) < 0)
482                 goto err_egl;
483
484         compositor->seat = android_seat_create(compositor);
485         if (!compositor->seat)
486                 goto err_egl;
487
488         return &compositor->base;
489
490 err_egl:
491         android_fini_egl(compositor);
492 err_output:
493         android_output_destroy(&output->base);
494 err_compositor:
495         weston_compositor_shutdown(&compositor->base);
496 err_free:
497         free(compositor);
498         return NULL;
499 }
500
501 WL_EXPORT struct weston_compositor *
502 backend_init(struct wl_display *display, int argc, char *argv[],
503              const char *config_file)
504 {
505         return android_compositor_create(display, argc, argv, config_file);
506 }