2 * Copyright © 2012 Collabora, Ltd.
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.
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.
29 #include <sys/types.h>
36 #include <GLES2/gl2.h>
38 #include "compositor.h"
39 #include "android-framebuffer.h"
42 struct android_compositor;
44 struct android_output {
45 struct android_compositor *compositor;
46 struct weston_output base;
48 struct weston_mode mode;
49 struct android_framebuffer *fb;
53 struct weston_seat base;
54 struct wl_list devices_list;
57 struct android_compositor {
58 struct weston_compositor base;
60 struct android_seat *seat;
63 static inline struct android_output *
64 to_android_output(struct weston_output *base)
66 return container_of(base, struct android_output, base);
69 static inline struct android_seat *
70 to_android_seat(struct weston_seat *base)
72 return container_of(base, struct android_seat, base);
75 static inline struct android_compositor *
76 to_android_compositor(struct weston_compositor *base)
78 return container_of(base, struct android_compositor, base);
82 egl_error_string(EGLint code)
84 #define MYERRCODE(x) case x: return #x;
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)
108 print_egl_error_state(void)
112 code = eglGetError();
113 weston_log("EGL error state: %s (0x%04lx)\n",
114 egl_error_string(code), (long)code);
118 android_finish_frame(void *data)
120 struct android_output *output = data;
122 weston_output_finish_frame(&output->base,
123 weston_compositor_get_time());
127 android_output_repaint(struct weston_output *base, pixman_region32_t *damage)
129 struct android_output *output = to_android_output(base);
130 struct android_compositor *compositor = output->compositor;
131 struct wl_event_loop *loop;
133 compositor->base.renderer->repaint_output(&output->base, damage);
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);
141 android_output_destroy(struct weston_output *base)
143 struct android_output *output = to_android_output(base);
145 wl_list_remove(&output->base.link);
146 weston_output_destroy(&output->base);
148 android_framebuffer_destroy(output->fb);
153 static struct android_output *
154 android_output_create(struct android_compositor *compositor)
156 struct android_output *output;
158 output = calloc(1, sizeof *output);
162 output->fb = android_framebuffer_create();
168 output->compositor = compositor;
173 android_compositor_add_output(struct android_compositor *compositor,
174 struct android_output *output)
176 float mm_width, mm_height;
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;
185 /* only one static mode in list */
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);
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";
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);
209 android_led_update(struct weston_seat *seat_base, enum weston_led leds)
211 struct android_seat *seat = to_android_seat(seat_base);
212 struct evdev_device *device;
214 wl_list_for_each(device, &seat->devices_list, link)
215 evdev_led_update(device, leds);
219 android_seat_open_device(struct android_seat *seat, const char *devnode)
221 struct evdev_device *device;
224 /* XXX: check the Android excluded list */
226 fd = open(devnode, O_RDWR | O_NONBLOCK | O_CLOEXEC);
228 weston_log_continue("opening '%s' failed: %s\n", devnode,
233 device = evdev_device_create(&seat->base, devnode, fd);
239 wl_list_insert(seat->devices_list.prev, &device->link);
243 is_dot_or_dotdot(const char *str)
245 return (str[0] == '.' &&
246 (str[1] == 0 || (str[1] == '.' && str[2] == 0)));
250 android_seat_scan_devices(struct android_seat *seat, const char *dirpath)
255 char *devnode = NULL;
257 dir = opendir(dirpath);
259 weston_log("Could not open input device directory '%s': %s\n",
260 dirpath, strerror(errno));
264 while ((dent = readdir(dir)) != NULL) {
265 if (is_dot_or_dotdot(dent->d_name))
268 ret = asprintf(&devnode, "%s/%s", dirpath, dent->d_name);
272 android_seat_open_device(seat, devnode);
280 android_seat_destroy(struct android_seat *seat)
282 struct evdev_device *device, *next;
284 wl_list_for_each_safe(device, next, &seat->devices_list, link)
285 evdev_device_destroy(device);
287 if (seat->base.seat.keyboard)
288 notify_keyboard_focus_out(&seat->base);
290 weston_seat_release(&seat->base);
294 static struct android_seat *
295 android_seat_create(struct android_compositor *compositor)
297 struct android_seat *seat;
299 seat = calloc(1, sizeof *seat);
303 weston_seat_init(&seat->base, &compositor->base);
304 seat->base.led_update = android_led_update;
305 wl_list_init(&seat->devices_list);
307 android_seat_scan_devices(seat, "/dev/input");
309 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
311 if (wl_list_empty(&seat->devices_list))
312 weston_log("Warning: no input devices found.\n");
314 /* XXX: implement hotplug support */
320 android_egl_choose_config(struct android_compositor *compositor,
321 struct android_framebuffer *fb,
322 const EGLint *attribs)
331 * The logic is copied from Android frameworks/base/services/
332 * surfaceflinger/DisplayHardware/DisplayHardware.cpp
335 compositor->base.egl_config = NULL;
337 ret = eglGetConfigs(compositor->base.egl_display, NULL, 0, &count);
338 if (ret == EGL_FALSE || count < 1)
341 configs = calloc(count, sizeof *configs);
345 ret = eglChooseConfig(compositor->base.egl_display, attribs, configs,
347 if (ret == EGL_FALSE || matched < 1)
350 for (i = 0; i < matched; ++i) {
352 ret = eglGetConfigAttrib(compositor->base.egl_display,
353 configs[i], EGL_NATIVE_VISUAL_ID,
355 if (ret == EGL_FALSE)
357 if (id > 0 && fb->format == id) {
358 compositor->base.egl_config = configs[i];
365 if (!compositor->base.egl_config)
372 android_init_egl(struct android_compositor *compositor,
373 struct android_output *output)
375 EGLint eglmajor, eglminor;
378 static const EGLint config_attrs[] = {
379 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
384 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
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();
395 ret = eglInitialize(compositor->base.egl_display, &eglmajor, &eglminor);
397 weston_log("Failed to initialise EGL.\n");
398 print_egl_error_state();
402 ret = android_egl_choose_config(compositor, output->fb, config_attrs);
404 weston_log("Failed to find an EGL config.\n");
405 print_egl_error_state();
409 output->base.egl_surface =
410 eglCreateWindowSurface(compositor->base.egl_display,
411 compositor->base.egl_config,
412 output->fb->native_window,
414 if (output->base.egl_surface == EGL_NO_SURFACE) {
415 weston_log("Failed to create FB EGLSurface.\n");
416 print_egl_error_state();
424 android_fini_egl(struct android_compositor *compositor)
426 gles2_renderer_destroy(&compositor->base);
428 eglMakeCurrent(compositor->base.egl_display,
429 EGL_NO_SURFACE, EGL_NO_SURFACE,
432 eglTerminate(compositor->base.egl_display);
437 android_compositor_destroy(struct weston_compositor *base)
439 struct android_compositor *compositor = to_android_compositor(base);
441 android_seat_destroy(compositor->seat);
443 /* destroys outputs, too */
444 weston_compositor_shutdown(&compositor->base);
446 android_fini_egl(compositor);
451 static struct weston_compositor *
452 android_compositor_create(struct wl_display *display, int argc, char *argv[],
453 const char *config_file)
455 struct android_compositor *compositor;
456 struct android_output *output;
458 weston_log("initializing android backend\n");
460 compositor = calloc(1, sizeof *compositor);
461 if (compositor == NULL)
464 if (weston_compositor_init(&compositor->base, display, argc, argv,
468 compositor->base.destroy = android_compositor_destroy;
470 compositor->base.focus = 1;
472 output = android_output_create(compositor);
476 if (android_init_egl(compositor, output) < 0)
479 android_compositor_add_output(compositor, output);
481 if (gles2_renderer_init(&compositor->base) < 0)
484 compositor->seat = android_seat_create(compositor);
485 if (!compositor->seat)
488 return &compositor->base;
491 android_fini_egl(compositor);
493 android_output_destroy(&output->base);
495 weston_compositor_shutdown(&compositor->base);
501 WL_EXPORT struct weston_compositor *
502 backend_init(struct wl_display *display, int argc, char *argv[],
503 const char *config_file)
505 return android_compositor_create(display, argc, argv, config_file);